This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
order-handeler-NEW.php
489 lines (407 loc) · 17.2 KB
/
order-handeler-NEW.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
/*
* Star CloudPRNT for WooCommerce Order Handler
* Edit version by Tim @ 19 Dec 2020
* use for mC2 and mC3 printers
* mC printer setting: max width + 180C rotate
*
*/
function star_cloudprnt_get_column_separated_data($columns, $max_chars)
{
//$max_chars = STAR_CLOUDPRNT_MAX_CHARACTERS_TWO_INCH;
$total_columns = count($columns);
if ($total_columns == 0) return "";
if ($total_columns == 1) return $columns[0];
if ($total_columns == 2)
{
$total_characters = strlen($columns[0])+strlen($columns[1]);
$total_whitespace = $max_chars - $total_characters;
if ($total_whitespace < 0) return "";
return $columns[0].str_repeat(" ", $total_whitespace).$columns[1];
}
$total_characters = 0;
foreach ($columns as $column)
{
$total_characters += strlen($column);
}
$total_whitespace = $max_chars - $total_characters;
if ($total_whitespace < 0) return "";
$total_spaces = $total_columns-1;
$space_width = floor($total_whitespace / $total_spaces);
$result = $columns[0].str_repeat(" ", $space_width);
for ($i = 1; $i < ($total_columns-1); $i++)
{
$result .= $columns[$i].str_repeat(" ", $space_width);
}
$result .= $columns[$total_columns-1];
return $result;
}
function star_cloudprnt_get_seperator($max_chars)
{
//$max_chars = STAR_CLOUDPRNT_MAX_CHARACTERS_TWO_INCH;
return str_repeat('_', $max_chars);
}
function star_cloudprnt_parse_order_status($status)
{
if ($status === 'wc-pending') return 'Pending Payment';
else if ($status === 'wc-processing') return 'Processing';
else if ($status === 'wc-on-hold') return 'On Hold';
else if ($status === 'wc-completed') return 'Completed';
else if ($status === 'wc-cancelled') return 'Cancelled';
else if ($status === 'wc-refunded') return 'Refunded';
else if ($status === 'wc-failed') return 'Failed';
else return "Unknown";
}
function star_cloudprnt_get_wc_order_notes($order_id){
//make sure it's a number
$order_id = intval($order_id);
//get the post
$post = get_post($order_id);
//if there's no post, return as error
if (!$post) return false;
return $post->post_excerpt;
}
function star_cloudprnt_get_codepage_currency_symbol()
{
$encoding = get_option('star-cloudprnt-printer-encoding-select');
$symbol = get_woocommerce_currency_symbol();
if ($encoding === "UTF-8") {
if ($symbol === "£") return "£"; // £ pound
else if ($symbol === "$") return "$"; // $ dollar
else if ($symbol === "€") return "€"; // € euro
} elseif ($encoding == "1252"){
if ($symbol === "£") return "\xA3"; // £ pound
else if ($symbol === "$") return "\x24"; // $ dollar
else if ($symbol === "€") return "\x80"; // € euro
} else {
if ($symbol === "£") return "GBP"; // £ pound
else if ($symbol === "$") return ""; // $ dollar
else if ($symbol === "€") return "EUR"; // € euro
}
return ""; // return blank by default
}
function star_cloudprnt_get_formatted_variation($variation, $order, $item_id)
{
$return = '';
if (is_array($variation))
{
$variation_list = array();
foreach ($variation as $name => $value)
{
// If the value is missing, get the value from the item
if (!$value)
{
$meta_name = esc_attr(str_replace('attribute_', '', $name));
$value = $order->get_item_meta($item_id, $meta_name, true);
}
// If this is a term slug, get the term's nice name
if (taxonomy_exists(esc_attr(str_replace('attribute_', '', $name))))
{
$term = get_term_by('slug', $value, esc_attr(str_replace('attribute_', '', $name)));
if (!is_wp_error($term) && ! empty($term->name))
{
$value = $term->name;
}
}
else
{
$value = ucwords(str_replace( '-', ' ', $value ));
}
$variation_list[] = wc_attribute_label(str_replace('attribute_', '', $name)) . ': ' . rawurldecode($value);
}
$return .= implode('||', $variation_list);
}
return $return;
}
function filterHTML($data)
{
/* Filter known html key words, convert to printer appropriate commands */
$encoding = get_option('star-cloudprnt-printer-encoding-select');
$ukp = "£";
if ($encoding == "1252"){
$ukp = "\xA3"; // £ pound
}
$data = str_replace("£", $ukp, $data); // convert any UTF8 '£' to a codepage normalised '£'
$data = str_replace("£", $ukp, $data);
$data = str_replace('–', '-', $data);
$data = str_replace('>', '>', $data);
$data = str_replace('<', '<', $data);
return $data;
}
function star_cloudprnt_create_receipt_items($order, &$printer, $max_chars)
{
$order_items = $order->get_items();
foreach ($order_items as $item_id => $item_data)
{
$product_name = $item_data['name'];
$product_id = $item_data['product_id'];
$variation_id = $item_data['variation_id'];
$item_qty = wc_get_order_item_meta($item_id, "_qty", true);
$item_total_price = floatval(wc_get_order_item_meta($item_id, "_line_total", true))
+floatval(wc_get_order_item_meta($item_id, "_line_tax", true));
$item_price = floatval($item_total_price) / intval($item_qty);
$currencyHex = star_cloudprnt_get_codepage_currency_symbol();
if ($variation_id != 0)
{
$product_variation = new WC_Product_Variation( $variation_id );
$product_name = $product_variation->get_title();
}
$formatted_item_price = number_format($item_price, 2, '.', '');
$formatted_total_price = number_format($item_total_price, 2, '.', '');
$printer->set_text_emphasized();
//$printer->add_text_line(str_replace('–', '-', $product_name)." - ID: ".$product_id."");
// add Tim 14-10
$printer->add_text_line(star_cloudprnt_get_column_separated_data(array(" ".
$item_qty."x - ".$product_name), $max_chars));
//$printer->add_text_line(filterHTML($product_name." - ID: ".$product_id.""));
// end
$printer->cancel_text_emphasized();
$meta = $item_data->get_formatted_meta_data("_", TRUE);
foreach ($meta as $meta_key => $meta_item)
{
// Don't use display_key and display_value, because those are formatted as html
$printer->add_text_line(filterHTML(" ".$meta_item->key.": ".$meta_item->value));
}
// edit tim 14-10; disable lines
// $printer->add_text_line(star_cloudprnt_get_column_separated_data(array(" Qty: ".
// $item_qty." x Cost: ".$currencyHex.$formatted_item_price,
// $currencyHex.$formatted_total_price), $max_chars));
// end
}
}
function star_cloudprnt_create_receipt_order_meta_data($meta_data, $order, &$printer, $max_chars)
{
if(get_option('star-cloudprnt-print-order-meta-cb') != 'on')
return;
// List of meta data item keys to print and formatted output
$fields = array(
"pi_delivery_type" => "[value]",
"pi_delivery_date" => "[value]",
"pi_delivery_time" => "[value]"
);
foreach (array_keys($fields) as $key)
{
if($order->meta_exists($key))
{
$printer->set_font_magnification(2, 1); // edit Tim 14-10-2020
$printer->add_text_line(str_replace('[value]', $order->get_meta($key), $fields[$key]));
$printer->set_font_magnification(1, 1); // edit Tim 14-10-2020
}
}
$printer->add_text_line("");
// Code for enumarationg/printing all fields
/*
$is_printed = false;
foreach ($meta_data as $item_id => $meta_data_item)
{
if(! $is_printed)
{
$is_printed = true;
$printer->set_text_emphasized();
$printer->add_text_line("Additional Order Information");
$printer->cancel_text_emphasized();
}
$item_data = $meta_data_item->get_data();
$printer->add_text_line($item_data["key"].": ".$item_data["value"]);
}
if($is_printed) $printer->add_text_line("");
*/
}
function star_cloudprnt_create_address($order, $order_meta, &$printer)
{
$fname = $order_meta['_shipping_first_name'][0];
$lname = $order_meta['_shipping_last_name'][0];
$a1 = $order_meta['_shipping_address_1'][0];
$a2 = $order_meta['_shipping_address_2'][0];
$postcode = $order_meta['_shipping_postcode'][0];
$city = $order_meta['_shipping_city'][0];
$state = $order_meta['_shipping_state'][0];
$tel = $order_meta['_billing_phone'][0];
$printer->set_text_emphasized();
if ($a1 == '')
{
$printer->add_text_line("Billing Address:");
$printer->cancel_text_emphasized();
$fname = $order_meta['_billing_first_name'][0];
$lname = $order_meta['_billing_last_name'][0];
$a1 = $order_meta['_billing_address_1'][0];
$a2 = $order_meta['_billing_address_2'][0];
$postcode = $order_meta['_billing_postcode'][0];
$city = $order_meta['_billing_city'][0];
$state = $order_meta['_billing_state'][0];
}
else
{
$printer->add_text_line("Shipping Address:");
$printer->cancel_text_emphasized();
}
$printer->add_text_line($fname." ".$lname);
$printer->add_text_line($a1);
if ($a2 != '') $printer->add_text_line($a2);
if ($postcode != '') $printer->add_text_line($postcode);
if ($city != '') $printer->add_text_line($city);
//if ($state != '') $printer->add_text_line($state);
//if ($postcode != '') $printer->add_text_line($postcode);
$printer->add_text_line("Tel: ".$tel);
}
function star_cloudprnt_print_order_summary($selectedPrinter, $file, $order_id)
{
$order = wc_get_order($order_id);
$order_number = $order->get_order_number(); // Displayed order number may be different to order_id when using some plugins
$shipping_items = @array_shift($order->get_items('shipping'));
$order_meta = get_post_meta($order_id);
$meta_data = $order->get_meta_data();
$date_format = get_option( 'date_format' );
$time_format = get_option( 'time_format' );
if ($selectedPrinter['format'] == "txt") {
$printer = new Star_CloudPRNT_Text_Plain_Job($selectedPrinter, $file);
} else if ($selectedPrinter['format'] == "slt") {
$printer = new Star_CloudPRNT_Star_Line_Mode_Job($selectedPrinter, $file);
} else if ($selectedPrinter['format'] == "slm") {
$printer = new Star_CloudPRNT_Star_Line_Mode_Job($selectedPrinter, $file);
} else if ($selectedPrinter['format'] == "spt") {
$printer = new Star_CloudPRNT_Star_Prnt_Job($selectedPrinter, $file);
} else {
$printer = new Star_CloudPRNT_Text_Plain_Job($selectedPrinter, $file);
}
$printer->set_codepage(get_option('star-cloudprnt-printer-encoding-select'));
if (get_option('star-cloudprnt-print-logo-top-input')) $printer->add_nv_logo(esc_attr(get_option('star-cloudprnt-print-logo-top-input')));
$printer->set_text_emphasized();
$printer->set_text_center_align();
$printer->set_font_magnification(2, 2);
if($selectedPrinter['columns'] < 40) {
$printer->add_text_line("ORDER");
// $printer->add_text_line("NOTE"); edit TV 19-12
} else {
$printer->add_text_line("ORDER");
}
$printer->set_text_left_align();
$printer->cancel_text_emphasized();
$printer->set_font_magnification(1, 1);
//$printer->add_new_line(1); //edit Tim 14-10
//$printer->add_text_line(star_cloudprnt_get_column_separated_data(array("Order #".$order_id, date("d-m-y H:i:s", time())), $selectedPrinter['columns']));
//$printer->add_text_line(star_cloudprnt_get_column_separated_data(array("Order #".$order_number, date("d-m-y H:i:s", time())), $selectedPrinter['columns']));
//$printer->add_text_line(star_cloudprnt_get_column_separated_data(array("Order #".$order_number, date("{$date_format} {$time_format}", current_time('timestamp'))), $selectedPrinter['columns']));
$printer->add_text_line("Order #".$order_number, date("{$date_format} {$time_format}", current_time('timestamp')));
//$printer->add_new_line(1); //edit Tim 14-10
//$printer->add_text_line("Order Status: ".star_cloudprnt_parse_order_status($order->post->post_status));
//$printer->add_text_line("Order Status: ".$order->get_status()); // edit Tim 14-10
//$printer->add_text_line("Order Date: ".$order->order_date);
//$printer->add_text_line("Order Date: ".$order->get_date_created());
$order_date = date("{$date_format} {$time_format}", $order->get_date_created()->getOffsetTimestamp());
//$printer->add_text_line(Order date:"{$order_date}");
$printer->add_text_line("{$order_date}");
if (isset($shipping_items['name']))
{
// edit Tim 14-10
// $printer->add_new_line(1);
// $printer->add_text_line("Shipping Method: ".$shipping_items['name']);
// end
}
//$printer->add_text_line("Payment Method: ".$order_meta['_payment_method_title'][0]); //edit Tim 14-10
$printer->add_new_line(1);
//$printer->add_text_line(star_cloudprnt_get_column_separated_data(array('ITEM', 'TOTAL'), $selectedPrinter['columns'])); //edit Tim 14-10
$printer->add_text_line(star_cloudprnt_get_seperator($selectedPrinter['columns']));
star_cloudprnt_create_receipt_items($order, $printer, $selectedPrinter['columns']);
$printer->add_new_line(1);
// edit Tim 14-10
//$printer->set_text_right_align();
//$formatted_overall_total_price = number_format($order_meta['_order_total'][0], 2, '.', '');
//$printer->add_text_line("TOTAL ".star_cloudprnt_get_codepage_currency_symbol().$formatted_overall_total_price);
//$printer->set_text_left_align();
//$printer->add_new_line(1);
//$printer->add_text_line("All prices are inclusive of tax (if applicable).");
//$printer->add_new_line(1);
$printer->add_text_line(star_cloudprnt_get_seperator($selectedPrinter['columns']));
// end
star_cloudprnt_create_receipt_order_meta_data($meta_data, $order, $printer, $selectedPrinter['columns']);
star_cloudprnt_create_address($order, $order_meta, $printer);
$printer->add_new_line(1);
$printer->set_text_emphasized();
$printer->add_text_line("Customer notes:");
$printer->cancel_text_emphasized();
$notes = star_cloudprnt_get_wc_order_notes($order_id);
$printer->add_text_line(empty($notes) ? "none" : $notes);
if (get_option('star-cloudprnt-print-logo-bottom-input')) $printer->add_nv_logo(esc_attr(get_option('star-cloudprnt-print-logo-bottom-input')));
$copies=intval(get_option("star-cloudprnt-print-copies-input"));
//$copies = intval($copies);
if($copies < 1) $copies = 1;
$printer->printjob($copies);
}
function star_cloudprnt_trigger_print($order_id)
{
$extension = STAR_CLOUDPRNT_SPOOL_FILE_FORMAT;
$selectedPrinterMac = "";
$selectedPrinter = array();
$printerList = star_cloudprnt_get_printer_list();
if (!empty($printerList))
{
foreach ($printerList as $printer)
{
if (get_option('star-cloudprnt-printer-select') == $printer['name'])
{
$selectedPrinter = $printer;
$selectedPrinterMac = $printer['printerMAC'];
break;
}
}
if (sizeof($selectedPrinter) == 0) {
$selectedPrinter = $printerList[0];
}
/* Decide best printer emulation and print width as far as possible
NOTE: this is not the ideal way, but suits the existing
code structure. Will be reviewed.
*/
$encodings = $selectedPrinter['Encodings'];
$columns = STAR_CLOUDPRNT_MAX_CHARACTERS_THREE_INCH;
if (strpos($encodings, "application/vnd.star.line;") !== false) {
/* There is no guarantee that printers will always return zero spacing between
the encoding name and separating semi-colon. But, definitely the HIX does, socket_accept
this is enough to ensure that thermal print mode is always used on HIX printers
with pre 1.5 firmware. This matches older plugin behaviour and therefore
avoids breaking customer sites.
*/
$extension = "slt";
} else if (strpos($encodings, "application/vnd.star.linematrix") !== false) {
$extension = "slm";
$columns = STAR_CLOUDPRNT_MAX_CHARACTERS_DOT_THREE_INCH;
} else if (strpos($encodings, "application/vnd.star.line") !== false) {
// a second check for Line mode - just in case the above one didn't catch item
// and after the "linemodematrix" check, to avoid a false match.
$extension = "slt";
} else if (strpos($encodings, 'application/vnd.star.starprnt') !== false) {
$extension = "spt";
} else if (strpos($encodings, "text/plain") !== false) {
$extension = "txt";
}
if ($selectedPrinter['ClientType'] == "Star mC-Print2") {
$columns = STAR_CLOUDPRNT_MAX_CHARACTERS_TWO_INCH;
}
//var_dump($selectedPrinter);
//print("Chosen Print Format:".$extension.", Columns:".$columns. "<br/>");
$selectedPrinter['format'] = $extension;
$selectedPrinter['columns'] = $columns;
$file = STAR_CLOUDPRNT_PRINTER_PENDING_SAVE_PATH.star_cloudprnt_get_os_path("/order_".$order_id."_".time().".".$extension);
if ($selectedPrinter !== "") star_cloudprnt_print_order_summary($selectedPrinter, $file, $order_id);
}
}
function star_cloudprnt_order_reprint_action( $actions ) {
global $theorder;
$actions['star_cloudprnt_reprint_action'] = __( 'Print via Star CloudPRNT', 'my-textdomain' );
return $actions;
}
function star_cloudprnt_reprint($order)
{
star_cloudprnt_trigger_print($order->get_id());
}
function star_cloudprnt_setup_order_handler()
{
if (selected(get_option('star-cloudprnt-select'), "enable", false) !== "" && star_cloudprnt_is_woo_activated())
{
add_action( 'woocommerce_order_actions', 'star_cloudprnt_order_reprint_action' );
//add_action('woocommerce_thankyou', 'star_cloudprnt_trigger_print', 1, 1);
add_action('woocommerce_order_status_processing', 'star_cloudprnt_trigger_print', 1, 1);
add_action('woocommerce_order_action_star_cloudprnt_reprint_action', 'star_cloudprnt_reprint', 1, 1 );
}
}
?>