Changeset 2770 for sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/wordcamp-budgets-dashboard.php
- Timestamp:
- 03/21/2016 02:09:51 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/includes/wordcamp-budgets-dashboard.php
r2747 r2770 16 16 add_action( 'admin_init', __NAMESPACE__ . '\process_action_approve', 11 ); 17 17 add_action( 'admin_init', __NAMESPACE__ . '\process_action_set_pending_payment', 11 ); 18 19 add_action( 'admin_init', __NAMESPACE__ . '\process_import_request', 11 ); 18 20 19 21 /** … … 129 131 */ 130 132 function render_import_tab() { 131 echo '<p>Move along, nothing to see here.</p>'; 133 ?> 134 <?php if ( isset( WCB_Import_Results::$data ) ) : ?> 135 <h2>Import Results</h2> 136 <pre><?php echo esc_html( print_r( WCB_Import_Results::$data, true ) ); ?></pre> 137 <?php endif; ?> 138 139 <p>Import payment results from JPM reports CSV.</p> 140 <form method="post" enctype="multipart/form-data"> 141 <input type="hidden" name="wcpn-request-import" value="1" /> 142 <?php wp_nonce_field( 'import', 'wcpn-request-import' ); ?> 143 <label>Import File:</label> 144 <input type="file" name="wcpn-import-file" /> 145 <?php submit_button( 'Import' ); ?> 146 </form> 147 <?php 132 148 } 133 149 … … 613 629 add_settings_error( 'wcb-dashboard', 'success', 'Success! Request has been marked as Pending Payment.', 'updated' ); 614 630 } 631 632 /** 633 * Process a payments import, runs during init. 634 */ 635 function process_import_request() { 636 if ( empty( $_POST['wcpn-request-import'] ) ) { 637 return; 638 } 639 640 if ( ! current_user_can( 'manage_network' ) || ! check_admin_referer( 'import', 'wcpn-request-import' ) ) { 641 return; 642 } 643 644 if ( empty( $_FILES['wcpn-import-file'] ) ) { 645 wp_die( 'Please select a file to import.' ); 646 } 647 648 $file = $_FILES['wcpn-import-file']; 649 if ( $file['type'] != 'text/csv' ) { 650 wp_die( 'Please upload a text/csv file.' ); 651 } 652 653 if ( $file['size'] < 1 ) { 654 wp_die( 'Please upload a file that is not empty.' ); 655 } 656 657 if ( $file['error'] ) { 658 wp_die( 'Some other error has occurred. Sorry.' ); 659 } 660 661 $handle = fopen( $file['tmp_name'], 'r' ); 662 $count = 0; 663 $header = array(); 664 $results = array(); 665 666 while ( ( $line = fgetcsv( $handle ) ) !== false ) { 667 // Skip first line. 668 if ( ++$count == 1 ) { 669 continue; 670 } 671 672 $entry = array( 673 'type' => strtolower( $line[11] ), 674 'status' => strtolower( $line[7] ), 675 'amount' => round( floatval( $line[13] ), 2 ), 676 'currency' => strtoupper( $line[14] ), 677 'blog_id' => null, 678 'post_id' => null, 679 'processed' => false, 680 'data' => null, 681 ); 682 683 switch ( $entry['type'] ) { 684 case 'wire': 685 if ( ! empty( $line[44] ) && preg_match( '#^wcb-([0-9]+)-([0-9]+)$#', $line[44], $matches ) ) { 686 $entry['blog_id'] = $matches[1]; 687 $entry['post_id'] = $matches[2]; 688 } 689 break; 690 case 'ach': 691 if ( ! empty( $line[91] ) && preg_match( '#^([0-9]+)-([0-9]+)$#', $line[91], $matches ) ) { 692 $entry['blog_id'] = $matches[1]; 693 $entry['post_id'] = $matches[2]; 694 } 695 break; 696 } 697 698 if ( empty( $entry['blog_id'] ) || empty( $entry['post_id'] ) ) { 699 $results[] = $entry; 700 continue; 701 } 702 703 // Don't consume memory. 704 wp_suspend_cache_addition( true ); 705 switch_to_blog( $entry['blog_id'] ); 706 707 $results[] = _import_process_entry( $entry ); 708 709 restore_current_blog(); 710 wp_suspend_cache_addition( false ); 711 } 712 713 fclose( $handle ); 714 715 WCB_Import_Results::$data = $results; 716 } 717 718 /** 719 * Process a single import entry. 720 * 721 * Runs in a switch_to_blog() context. 722 * 723 * @param $entry Array 724 * @return Array 725 */ 726 function _import_process_entry( $entry ) { 727 $post = get_post( $entry['post_id'] ); 728 if ( ! $post || ! in_array( $post->post_type, array( 'wcp_payment_request', 'wcb_reimbursement' ) ) ) { 729 $entry['data'] = 'Post not found or post type mismatch'; 730 return $entry; 731 } 732 733 $currency = false; 734 $amout = false; 735 736 if ( $post->post_type == 'wcb_reimbursement' ) { 737 $currency = get_post_meta( $post->ID, '_wcbrr_currency', true ); 738 $expenses = get_post_meta( $post->ID, '_wcbrr_expenses', true ); 739 foreach ( $expenses as $expense ) { 740 if ( ! empty( $expense['_wcbrr_amount'] ) ) { 741 $amount += floatval( $expense['_wcbrr_amount'] ); 742 } 743 } 744 } elseif ( $post->post_type == 'wcp_payment_request' ) { 745 $currency = get_post_meta( $post->ID, '_camppayments_currency', true ); 746 $amount = floatval( get_post_meta( $post->ID, '_camppayments_payment_amount', true ) ); 747 } 748 $amount = round( $amount, 2 ); 749 750 if ( empty( $entry['currency'] ) || $entry['currency'] != $currency ) { 751 $entry['data'] = 'Currency mismatch'; 752 return $entry; 753 } 754 755 $entry['amount'] = floatval( $entry['amount'] ); 756 $entry['amount'] = round( $entry['amount'], 2 ); 757 758 if ( $entry['amount'] !== $amount ) { 759 $entry['data'] = 'Payment amount mismatch'; 760 return $entry; 761 } 762 763 // @todo Do some magic here. 764 765 // All good. 766 $entry['processed'] = true; 767 return $entry; 768 } 769 770 class WCB_Import_Results { 771 public static $data; 772 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)