fix_unpaid.php 5.65 KB
<?php
namespace Tz\WordPress\Tools\UserManager;

use Tz, Tz\Common;
use Tz\WordPress\CBV;
use Tz\WordPress\CBV\User;
use Tz\WordPress\UAM;
use Tz\WordPress\Tools;
use Tz\WordPress\Tools\Auth;
use Exception, StdClass;
use WP_User;

global $wpdb;

// Get the original timezone (so we can set it back) and figure out the
// timestamp for March 1 UTC
$original_timezone = date_default_timezone_get();
date_default_timezone_set('UTC');
$begin_date = strtotime('March 1');
// Set the timezone back to original setting
date_default_timezone_set($original_timezone);

if (! empty($_POST['do_fix']) && $_POST['do_fix'] == 'y') {
    $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);
    }
}

?>
<style>

h4 {
	float: left;
	padding-right: 1em;
}

.merge-users {
	float: left;
	padding-right: 2em;
}

.merge-users label {
	float: left;
	font-weight: bold;
	padding-right: 1em;
}

.merge-users input {
	float: left;
}

.clear {
	float: left;
	clear: both;
}

.user-list, .user-list li {
	float: left;
	clear: both;
}

.changed {
    margin-top: 5px;
	clear: both;
	display: none;
}

.changed h6 {
    padding: 0px;
    margin: 0px 0px 10px 0px;
    font-size: 11px;
    text-transform: uppercase;
}

.changed ul {
    margin: 0;
    padding: 0;
}

.changed ul li {
    margin: 0px 0px 3px 0px;
    padding: 0px;
    font-size: 11px;
	background: none;
}
.merge-table td { padding:10px;}
</style>

<div class="wrap">

    <div id="icon-users" class="icon32"><br /></div>
    <h2>Fix Unpaid Invoices</h2>
    
    <div class="validation-errors" style="display:none; margin: 10px 0px 10px;">
    	<div class="error-wrap">
    		<ul></ul>
    	</div>
    </div>
    
    <div id="post-body" style="padding:10px 0px 15px 0px;">
        <form method="post" id="fix_unpaid_invoices">
            <!--<table cellspacing="0" class="widefat post fixed merge-table">
                <thead>
                    <tr>
                        <th width="50%"><label for="search_user_from">Merge <strong><em>FROM</em></strong> User:</label></th> 
                        <th width="50%"><label for="search_user_to">Merge <strong><em>TO</em></strong> User:</label></th>               
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            <div class="merge-users">                				
                				<input class="clear" type="text" name="search_user_from" value="<?php echo $_POST['search_user_from'] ?>" />
                				<input type="button" name="btn_user_from" value="Search" />&nbsp;<img id="search_user_from_spinner" class="spinners" src="/wp-content/tz-tools/com/Branding/images/spinner.gif" style="display:none;" />

                			</div>
                        </td>
                        <td>
                            <div class="merge-users">                				
                				<input class="clear" type="text" name="search_user_to" value="<?php echo $_POST['search_user_to'] ?>" />
                				<input type="button" name="btn_user_to" value="Search" />&nbsp;<img id="search_user_to_spinner" class="spinners" src="/wp-content/tz-tools/com/Branding/images/spinner.gif" style="display:none;" />
                			</div>
                        </td>
                    </tr>
                    <tr>
                        <td><div id="user-list-from"></div></td>
                        <td><div id="user-list-to"></div></td>
                    </tr>
                </tbody>
            </table>-->
    
    		<div id="field-list" class="changed">
    			<table cellspacing="0" class="widefat post fixed merge-table"></table>
    		</div>
    
            <div style="clear:both;"></div>
    
            <div style="margin-top:10px;">
            <input type="hidden" name="do_fix" value="y" />
            <input type="submit" value="Do It" />
            </div>
            
        </form>
    
    </div>

</div>