fix_unpaid.php
5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?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" /> <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" /> <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>