ee92c36e by Chris Boden

Fixed loop on invoice fix

1 parent 3624166b
......@@ -21,44 +21,51 @@ $begin_date = strtotime('March 1');
date_default_timezone_set($original_timezone);
if (! empty($_POST['do_fix']) && $_POST['do_fix'] == 'y') {
$users = $wpdb->get_results("SELECT ID FROM {$wpdb->users} LIMIT 0,1000");
foreach ($users as $user) {
$invoices = get_user_meta($user->ID, 'invoices', TRUE);
foreach ($invoices as $invoice_id) {
// Get the transaction status
$trans_status = get_post_meta($invoice_id, 'trans_status', TRUE);
// Don't do anything unless the status is 'unpaid'
if ($trans_status != 'unpaid') {
continue;
}
// Get the invoice and check if it's newer than March 1 UTC
$invoice = get_post($invoice_id);
if (! strtotime($invoice->post_date) > $begin_date) {
continue;
}
// Here's the heavy work: get the payment data and add appropriate taxes
$payment = get_post_meta($invoice_id, 'payment', TRUE);
$trans_amount = get_post_meta($invoice_id, 'trans_amount', TRUE);
// Get the user's profile preference and use it to get the tax percent
if (! empty($user->profile_preference)) {
$preference = strtolower($user->profile_preference) . '_province';
$tax_percent = getTaxesByProvince($user->$preference) * .01;
} else {
$tax_percent = getTaxesByProvince($payment['bt_province']) * .01;
}
$payment['total_taxes'] = $payment['total_cost'] * $tax_percent;
$payment['total'] = number_format($payment['total_cost'] - $payment['total_discounts'] + $payment['total_taxes'], 2);
$trans_amount = $payment['total'];
// Now update the post
update_post_meta($invoice_id, 'payment', $payment);
update_post_meta($invoice_id, 'trans_amount', $trans_amount);
}
}
$count_result = mysql_query("SELECT COUNT(*) FROM `{$wpdb->users}`", $wpdb->dbh);
list($num) = mysql_fetch_row($count_result);
$max = 1000;
for ($ui = 0; $ui < $num; $ui += $max) {
$users = $wpdb->get_results("SELECT ID FROM `{$wpdb->users}` LIMIT {$ui}, {$max}");
foreach ($users as $user) {
$invoices = get_user_meta($user->ID, 'invoices', TRUE);
foreach ($invoices as $invoice_id) {
// Get the transaction status
$trans_status = get_post_meta($invoice_id, 'trans_status', TRUE);
// Don't do anything unless the status is 'unpaid'
if ($trans_status != 'unpaid') {
continue;
}
// Get the invoice and check if it's newer than March 1 UTC
$invoice = get_post($invoice_id);
if (! strtotime($invoice->post_date) > $begin_date) {
continue;
}
// Here's the heavy work: get the payment data and add appropriate taxes
$payment = get_post_meta($invoice_id, 'payment', TRUE);
$trans_amount = get_post_meta($invoice_id, 'trans_amount', TRUE);
// Get the user's profile preference and use it to get the tax percent
if (! empty($user->profile_preference)) {
$preference = strtolower($user->profile_preference) . '_province';
$tax_percent = getTaxesByProvince($user->$preference) * .01;
} else {
$tax_percent = getTaxesByProvince($payment['bt_province']) * .01;
}
$payment['total_taxes'] = $payment['total_cost'] * $tax_percent;
$payment['total'] = number_format($payment['total_cost'] - $payment['total_discounts'] + $payment['total_taxes'], 2);
$trans_amount = $payment['total'];
// Now update the post
update_post_meta($invoice_id, 'payment', $payment);
update_post_meta($invoice_id, 'trans_amount', $trans_amount);
}
}
unset($users, $invoices);
}
}
?>
......