-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweekly.php
338 lines (314 loc) · 11.7 KB
/
weekly.php
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/**
* Dokan Dashboard Template
*
* Dokan Main Dahsboard template for Fron-end
*
* @since 2.4
*
* @package dokan
*/
?>
<div class="dokan-dashboard-wrap">
<?php
/**
* dokan_dashboard_content_before hook
*
* @hooked get_dashboard_side_navigation
*
* @since 2.4
*/
do_action('dokan_dashboard_content_before');
?>
<div class="dokan-dashboard-content">
<?php
/**
* dokan_dashboard_content_before hook
*
* @hooked show_seller_dashboard_notice
*
* @since 2.4
*/
do_action('dokan_help_content_inside_before');
$day = date('w');
$week_start = date('Y-m-d', strtotime('-' . $day . ' days'));
$saturday = date('m/d/Y', strtotime($week_start . ' -1 day'));
?>
<article class="help-content-area">
<h1>Weekly Orders</h1>
<div>
<h4>Start Date</h4>
<div class="input _messages">
<input type="text" value="<?php echo $saturday?>" id="startdate" class="date form-control dokan-ajax-search-textfield w50">
<br/>
<a id="cancel" href="javascript:getReport()" class="block button fullwidth inline w50 distanttop">Generate Report</a>
</div>
<div class="loader hidden"></div>
<div id="messages"></div>
<div class="distanttop hidden" id="get_pdf">
<a id="pdf_link" href="javascript:" target="_blank" class="block button fullwidth inline">Open Report</a>
</div>
</div>
<?php if ( current_user_can('administrator') ) {
?>
<br/>
<br/>
<h1 class="distanttop">Weekly Orders for All Vendors</h1>
<div>
<h4>Start Date</h4>
<div class="input _messages">
<input type="text" value="<?php echo $saturday?>" id="startdate_all" class="date form-control dokan-ajax-search-textfield w50">
<br/>
<a id="cancel" href="javascript:getReportAll()" class="block button fullwidth inline w50 distanttop">Generate Report</a>
</div>
<div class="loader hidden"></div>
<div id="messages_all"></div>
<div class="distanttop hidden" id="get_pdf_all">
<a id="pdf_link_all" href="javascript:" target="_blank" class="block button fullwidth inline">Open Report</a>
</div>
</div>
<?php }; ?>
</article>
<!-- .dashboard-content-area -->
<?php
/**
* dokan_dashboard_content_inside_after hook
*
* @since 2.4
*/
do_action('dokan_dashboard_content_inside_after');
?>
</div>
<!-- .dokan-dashboard-content -->
<?php
/**
* dokan_dashboard_content_after hook
*
* @since 2.4
*/
do_action('dokan_dashboard_content_after');
echo '<script type="text/javascript">var webfixAjax = {"ajaxurl": "' . admin_url('admin-ajax.php') . '","nextNonce": "' . wp_create_nonce('myajax-next-nonce') . '"};</script>';
?>
</div>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="<?php echo get_stylesheet_directory_uri(); ?>/js/jquery.date.min.js"></script>
<script>
var startDate;
function setActivity(busy) {
if (busy == true) {
$('.loader').removeClass('hidden').show();
} else {
$('.loader').addClass('hidden').hide();
}
}
function getReport() {
setActivity(true);
$('#messages').html('');
$('#get_pdf').hide();
startDate = $('#startdate').val();
console.log('startDate', startDate);
$.ajax({
type : 'POST',
url : webfixAjax.ajaxurl,
data : {
nonce : webfixAjax.nextNonce,
action : 'save_report',
date : startDate
},
success : function(response) {
console.log(response);
if (response.result == true) {
$('#pdf_link').attr('href', response.data);
$('#get_pdf').removeClass('hidden').show();
$('#messages').html('Success, click on Open Report to review or print');
} else {
$('#messages').html(response.messages.join('<br/>'));
}
setActivity(false);
},
error : function(error) {
setActivity(false);
}
});
}
function getReportAll() {
setActivity(true);
$('#messages_all').html('');
$('#get_pdf_all').hide();
startDate = $('#startdate_all').val();
console.log('startDate', startDate);
$.ajax({
type : 'POST',
url : webfixAjax.ajaxurl,
data : {
nonce : webfixAjax.nextNonce,
action : 'print_orders',
date : startDate
},
success : function(response) {
console.log(response);
if (response.result == true) {
$('#pdf_link_all').attr('href', response.data);
$('#get_pdf_all').removeClass('hidden').show();
$('#messages_all').html('Success, click on Open Report to review or print');
} else {
$('#messages_all').html(response.messages.join('<br/>'));
}
setActivity(false);
},
error : function(error) {
setActivity(false);
}
});
}
</script>
<script>
$().ready(function() {
$('input[type=text].date').datepicker({
autoHide : true
});
});
</script>
<?php
global $wpdb;
$status_where = $wpdb -> prepare(' AND order_status = %s', 'wc-completed');
$date_query = $wpdb -> prepare(' AND DATE( p.post_date ) >= %s AND DATE( p.post_date ) <= %s', '2018-01-06', '2018-01-13');
$sql = "SELECT do.*, p.post_date
FROM {$wpdb->prefix}dokan_orders AS do
LEFT JOIN $wpdb->posts p ON do.order_id = p.ID
WHERE
p.post_status != 'trash'
$status_where
$date_query
GROUP BY do.order_id
ORDER BY p.post_date ASC";
$orders = $wpdb -> get_results($sql);
$orders_html .= '';
if ($orders) {
$order_index = 0;
$stamp_orders = current_time('Ymd', 0);
// Build an array of "orders". Because it's multivendor, each customer may have multiple orders
// from the same checkout.
foreach ($orders as $order) {
// The WC_Order object contains date and payment information of transaction
$the_order = new WC_Order($order -> order_id);
//$messages[] = $order -> order_id;
$date_order = new DateTime($order -> post_date);
$customer_phone = esc_html(get_post_meta($order -> order_id, '_billing_phone', true));
$user_info = '';
if ($the_order -> get_user_id()) {
$user_info = get_userdata($the_order -> get_user_id());
}
$customer_id = $the_order -> get_user_id();
if (!empty($user_info)) {
$user_display = '';
$sort_key = $user_info -> last_name;
if ($user_info -> first_name || $user_info -> last_name) {
$user_display .= esc_html($user_info -> first_name . ' ' . $user_info -> last_name);
} else {
$user_display .= esc_html($user_info -> display_name);
} } else {
$customer_id = $stamp_orders . sprintf('%04d', $order_index);
$user = esc_html(get_post_meta($order -> order_id, '_billing_first_name', true)) . ' ' . esc_html(get_post_meta($order -> order_id, '_billing_last_name', true)) . ' (guest)';
$sort_key = 0;
//$order -> billing_first_name
$order_index++;
}
if ('0000-00-00 00:00:00' == dokan_get_date_created($the_order)) {
$t_time = $h_time = __('Unpublished', 'dokan-lite');
} else {
$t_time = get_the_time(__('Y/m/d g:i:s A', 'dokan-lite'), dokan_get_prop($the_order, 'id'));
$gmt_time = strtotime(dokan_get_date_created($the_order) . ' UTC');
$time_diff = current_time('timestamp', 1) - $gmt_time;
if ($time_diff > 0 && $time_diff < 24 * 60 * 60)
$h_time = sprintf(__('%s ago', 'dokan-lite'), human_time_diff($gmt_time, current_time('timestamp', 1)));
else
$h_time = get_the_time(__('Y/m/d h:i:s A', 'dokan-lite'), dokan_get_prop($the_order, 'id'));
}
$items = '';
// There will be one order per customer per vendor. So the following debug might show:
// Ingrid, Ingrid, Ingrid, Tom, Tom, etc...
//mz_pr($the_order->data['billing']['first_name']);
foreach ($the_order -> get_items() as $item_id => $item_data) {
$product = $item_data -> get_product();
if (is_object($product)) {
$product_name = $product -> get_name();
// If it's the farmdrop fee, don't include it in the report
if (strpos(strtolower($product_name), 'fee') !== false) {
} else {$item_quantity = $item_data -> get_quantity();
$store_info = dokan_get_store_info($order -> seller_id);
$vendor = $store_info['store_name'];
$item_total = $item_data -> get_total();
// If it's not a user, do not add to items all
if ($sort_key === 0) continue;
$items_all[] = array('id' => $product -> id,
'order_id' => $order -> order_id,
'customer_id' => $customer_id,
'customer_lastname' => $sort_key,
'customer' => $user_display . ' (Phone: ' . format_phone_number($customer_phone) . ')',
'order_date_post' => $date_order -> format('m/d/Y h:i:s A'),
'order_date' => esc_html(apply_filters('post_date_column_time', dokan_date_time_format($h_time, true), dokan_get_prop($the_order, 'id'))),
'name' => $product_name,
'vendor' => $vendor,
'quantity' => $item_quantity,
'total' => $item_total);
}
}
}
}
$array_consolidated = array();
// Loop through the item's all array and build a single key for each customer
foreach ($items_all as $item) {
if (isset($array_consolidated[$item['customer_id']])) {
$array_consolidated[$item['customer_id']]['items'][] = array('name' => $item['name'], 'vendor' => $item['vendor'], 'quantity' => $item['quantity'], 'total' => $item['total'], 'date' => $item['order_date_post']);
} else {
$array_consolidated[$item['customer_id']]['customer'] = $item['customer'];
$array_consolidated[$item['customer_id']]['customer_lastname'] = $item['customer_lastname'];
$array_consolidated[$item['customer_id']]['items'] = array( array('name' => $item['name'], 'vendor' => $item['vendor'], 'quantity' => $item['quantity'], 'total' => $item['total'], 'date' => $item['order_date_post']));
}
}
//inspect($array_consolidated);
//ksort($array_consolidated, SORT_NATURAL);
usort($array_consolidated, function($a, $b) {
if ($a == $b) {
return 0;
}
return ($a['customer_lastname'] < $b['customer_lastname']) ? -1 : 1;
});
$messages[] = inspect($array_consolidated, false, true);
if ($array_consolidated) {
$page_break = false;
foreach ($array_consolidated as $customer) {
if ($page_break == true) {
$orders_html .= '<div style="page-break-before:always"></div>';
} else {
$page_break = true;
}
$orders_html .= '
<h2 style="text-align:left;vertical-align:top;font-size:15pt;"><strong>' . strtoupper($customer['customer']) . '<strong></h2>
';
$orders_html .= '<table style="width:100%;"><tbody>';
$orders_html .= '<thead>
<tr>
<th style="border-bottom: 1px solid #ddd;text-align:left;">Item</th>
<th style="border-bottom: 1px solid #ddd;text-align:left;">Vendor</th>
<th style="border-bottom: 1px solid #ddd;text-align:right;">Quantity</th>
<th style="border-bottom: 1px solid #ddd;text-align:right;">Total</th>
<th style="border-bottom: 1px solid #ddd;text-align:right;">Date</th>
</tr>
</thead>';
foreach ($customer['items'] as $item) {
$orders_html .= '<tr>
<td style="border-bottom: 1px solid #ddd;text-align:left;vertical-align:top;">' . $item['name'] . '</td>
<td style="border-bottom: 1px solid #ddd;text-align:left;vertical-align:top;">' . $item['vendor'] . '</td>
<td style="border-bottom: 1px solid #ddd;text-align:right;vertical-align:top;">' . $item['quantity'] . '</td>
<td style="border-bottom: 1px solid #ddd;text-align:right;vertical-align:top;">' . $item['total'] . '</td>
<td style="border-bottom: 1px solid #ddd;text-align:right;vertical-align:top;">' . $item['date'] . '</td>
</tr>';
}
$orders_html .= '</tbody></table>';
}
}
}
?>
<!-- .dokan-dashboard-wrap -->