ee92c36e by Chris Boden

Fixed loop on invoice fix

1 parent 3624166b
...@@ -21,44 +21,51 @@ $begin_date = strtotime('March 1'); ...@@ -21,44 +21,51 @@ $begin_date = strtotime('March 1');
21 date_default_timezone_set($original_timezone); 21 date_default_timezone_set($original_timezone);
22 22
23 if (! empty($_POST['do_fix']) && $_POST['do_fix'] == 'y') { 23 if (! empty($_POST['do_fix']) && $_POST['do_fix'] == 'y') {
24 $users = $wpdb->get_results("SELECT ID FROM {$wpdb->users} LIMIT 0,1000"); 24 $count_result = mysql_query("SELECT COUNT(*) FROM `{$wpdb->users}`", $wpdb->dbh);
25 foreach ($users as $user) { 25 list($num) = mysql_fetch_row($count_result);
26 $invoices = get_user_meta($user->ID, 'invoices', TRUE); 26 $max = 1000;
27 foreach ($invoices as $invoice_id) { 27
28 // Get the transaction status 28 for ($ui = 0; $ui < $num; $ui += $max) {
29 $trans_status = get_post_meta($invoice_id, 'trans_status', TRUE); 29 $users = $wpdb->get_results("SELECT ID FROM `{$wpdb->users}` LIMIT {$ui}, {$max}");
30 // Don't do anything unless the status is 'unpaid' 30 foreach ($users as $user) {
31 if ($trans_status != 'unpaid') { 31 $invoices = get_user_meta($user->ID, 'invoices', TRUE);
32 continue; 32 foreach ($invoices as $invoice_id) {
33 } 33 // Get the transaction status
34 // Get the invoice and check if it's newer than March 1 UTC 34 $trans_status = get_post_meta($invoice_id, 'trans_status', TRUE);
35 $invoice = get_post($invoice_id); 35 // Don't do anything unless the status is 'unpaid'
36 if (! strtotime($invoice->post_date) > $begin_date) { 36 if ($trans_status != 'unpaid') {
37 continue; 37 continue;
38 } 38 }
39 39 // Get the invoice and check if it's newer than March 1 UTC
40 // Here's the heavy work: get the payment data and add appropriate taxes 40 $invoice = get_post($invoice_id);
41 $payment = get_post_meta($invoice_id, 'payment', TRUE); 41 if (! strtotime($invoice->post_date) > $begin_date) {
42 $trans_amount = get_post_meta($invoice_id, 'trans_amount', TRUE); 42 continue;
43 43 }
44 // Get the user's profile preference and use it to get the tax percent 44
45 if (! empty($user->profile_preference)) { 45 // Here's the heavy work: get the payment data and add appropriate taxes
46 $preference = strtolower($user->profile_preference) . '_province'; 46 $payment = get_post_meta($invoice_id, 'payment', TRUE);
47 $tax_percent = getTaxesByProvince($user->$preference) * .01; 47 $trans_amount = get_post_meta($invoice_id, 'trans_amount', TRUE);
48 } else { 48
49 $tax_percent = getTaxesByProvince($payment['bt_province']) * .01; 49 // Get the user's profile preference and use it to get the tax percent
50 } 50 if (! empty($user->profile_preference)) {
51 51 $preference = strtolower($user->profile_preference) . '_province';
52 52 $tax_percent = getTaxesByProvince($user->$preference) * .01;
53 $payment['total_taxes'] = $payment['total_cost'] * $tax_percent; 53 } else {
54 $payment['total'] = number_format($payment['total_cost'] - $payment['total_discounts'] + $payment['total_taxes'], 2); 54 $tax_percent = getTaxesByProvince($payment['bt_province']) * .01;
55 $trans_amount = $payment['total']; 55 }
56 56
57 // Now update the post 57
58 update_post_meta($invoice_id, 'payment', $payment); 58 $payment['total_taxes'] = $payment['total_cost'] * $tax_percent;
59 update_post_meta($invoice_id, 'trans_amount', $trans_amount); 59 $payment['total'] = number_format($payment['total_cost'] - $payment['total_discounts'] + $payment['total_taxes'], 2);
60 } 60 $trans_amount = $payment['total'];
61 } 61
62 // Now update the post
63 update_post_meta($invoice_id, 'payment', $payment);
64 update_post_meta($invoice_id, 'trans_amount', $trans_amount);
65 }
66 }
67 unset($users, $invoices);
68 }
62 } 69 }
63 70
64 ?> 71 ?>
......