-
Notifications
You must be signed in to change notification settings - Fork 17
/
commerce_pos.module
1351 lines (1193 loc) · 37.7 KB
/
commerce_pos.module
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Hooks and utility functions for commerce_pos.
*/
define('COMMERCE_PAYMENT_STATUS_VOID', 'void');
define('COMMERCE_POS_PAYMENT_STATUS_CANCELED', 'commerce_pos_canceled');
define('COMMERCE_POS_PAYMENT_METHOD_STATUS_ENABLED', 1);
/**
* Implements hook_menu().
*/
function commerce_pos_menu() {
$items = array();
$items['admin/commerce/pos'] = array(
'title' => 'Point of Sale',
'description' => 'Carry out in-person retail transactions.',
'page callback' => 'commerce_pos_redirect',
'access arguments' => array(
array(
'process commerce pos sales',
'process commerce pos returns',
),
),
'access callback' => '_commerce_pos_access_check',
'file' => 'includes/commerce_pos.pages.inc',
);
$items['admin/commerce/pos/transaction'] = array(
'title' => 'Transaction',
'page callback' => 'commerce_pos_page',
'page arguments' => array('commerce_pos_transaction_form'),
'access callback' => '_commerce_pos_access_check',
'access arguments' => array('process commerce pos sales'),
'file' => 'includes/commerce_pos.transaction.inc',
);
$items['admin/commerce/pos/transaction/payment'] = array(
'title' => 'Pay',
'page callback' => 'commerce_pos_page',
'page arguments' => array('commerce_pos_payment'),
'access callback' => '_commerce_pos_access_check',
'access arguments' => array('process commerce pos sales'),
'file' => 'includes/commerce_pos.payment.inc',
);
$items['admin/commerce/pos/login'] = array(
'title' => 'Employee Login',
'page callback' => 'commerce_pos_page',
'page arguments' => array('commerce_pos_login'),
'access arguments' => array('access content'),
'file' => 'includes/commerce_pos.login.inc',
'type' => MENU_CALLBACK,
);
$items['admin/commerce/pos/product/autocomplete'] = array(
'title' => 'Product Autocomplete',
'page callback' => 'commerce_pos_product_autocomplete',
'access callback' => '_commerce_pos_access_check',
'access arguments' => array(
array(
'process commerce pos sales',
'process commerce pos returns',
'process commerce pos exchanges',
),
),
'file' => 'includes/commerce_pos.common.inc',
'type' => MENU_CALLBACK,
);
$items['admin/commerce/pos/user/autocomplete'] = array(
'title' => 'User Autocomplete',
'page callback' => 'commerce_pos_user_autocomplete',
'access callback' => '_commerce_pos_access_check',
'access arguments' => array(
array(
'process commerce pos sales',
'process commerce pos returns',
'process commerce pos exchanges',
),
),
'file' => 'includes/commerce_pos.common.inc',
'type' => MENU_CALLBACK,
);
$items['admin/commerce/config/pos'] = array(
'title' => 'Point of Sale',
'description' => 'Administer POS.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer commerce pos'),
'file path' => drupal_get_path('module', 'system'),
'file' => 'system.admin.inc',
'weight' => 10,
);
$items['admin/commerce/config/pos/settings'] = array(
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_pos_settings'),
'access arguments' => array('administer commerce pos'),
'file' => 'includes/commerce_pos.admin.inc',
);
return $items;
}
/**
* Implements hook_permission().
*/
function commerce_pos_permission() {
return array(
'administer commerce pos' => array(
'title' => t('Administer Commerce POS'),
),
'process commerce pos sales' => array(
'title' => t('Process Commerce POS sales'),
),
'process commerce pos returns' => array(
'title' => t('Process Commerce POS returns'),
),
'process commerce pos exchanges' => array(
'title' => t('Process Commerce POS exchanges'),
),
);
}
/**
* Implements hook_theme().
*/
function commerce_pos_theme($existing, $type, $theme, $path) {
return array(
'commerce_pos_transaction' => array(
'render element' => 'form',
'template' => 'commerce-pos-transaction',
'path' => $path . '/theme',
),
'commerce_pos_payment' => array(
'render element' => 'form',
'template' => 'commerce-pos-payment',
'path' => $path . '/theme',
),
'commerce_pos_keypad' => array(
'render element' => 'element',
'template' => 'commerce-pos-keypad',
'path' => $path . '/theme',
),
'commerce_pos_order_balance_summary' => array(
'file' => 'commerce_pos.theme.inc',
'path' => $path . '/theme',
'variables' => array(
'order' => NULL,
'display_only' => FALSE,
'include_balance' => TRUE,
'pos_transaction' => NULL,
),
),
'commerce_pos_transaction_actions' => array(
'file' => 'commerce_pos.theme.inc',
'path' => $path . '/theme',
'variables' => array(
'transaction' => NULL,
'pos_transaction' => NULL,
),
),
'commerce_pos_transaction_balance' => array(
'template' => 'commerce-pos-transaction-balance',
'path' => $path . '/theme',
'variables' => array(
'rows' => array(),
'form' => NULL,
'totals' => array(),
'view' => NULL,
),
),
'commerce_pos_header' => array(
'template' => 'commerce-pos-header',
'file' => 'commerce_pos.theme.inc',
'path' => $path . '/theme',
'variables' => array(
'account' => NULL,
),
),
'commerce_pos_product_result' => array(
'template' => 'commerce-pos-product-result',
'file' => 'commerce_pos.theme.inc',
'path' => $path . '/theme',
'variables' => array(
'product' => NULL,
),
),
'commerce_pos_product_summary' => array(
'file' => 'commerce_pos.theme.inc',
'path' => $path . '/theme',
'variables' => array(
'order' => NULL,
'attributes' => array(),
),
),
'commerce_pos_price_formatted_components' => array(
'variables' => array(
'components' => array(),
'price' => array(),
),
),
);
}
/**
* Implements hook_views_api().
*/
function commerce_pos_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'commerce_pos') . '/includes/views',
);
}
/**
* Implements hook_commerce_pos_transaction_base_info().
*/
function commerce_pos_commerce_pos_transaction_base_info() {
return array(
'commerce_pos_transaction_base_actions' => array(
'class' => 'CommercePosTransactionBaseActions',
'types' => array(
CommercePosService::TRANSACTION_TYPE_SALE,
CommercePosService::TRANSACTION_TYPE_RETURN,
CommercePosService::TRANSACTION_TYPE_EXCHANGE,
),
),
);
}
/**
* Implements hook_commerce_payment_method_info().
*/
function commerce_pos_commerce_payment_method_info() {
// Note that checkout and terminal are both FALSE. This payment method is
// only meant to by internally used by the POS system.
$payment_methods['commerce_pos_change'] = array(
'base' => 'commerce_pos_change',
'title' => t('Change'),
'description' => t('Change given to a customer.'),
'active' => TRUE,
'checkout' => FALSE,
'terminal' => FALSE,
'fieldable' => FALSE,
);
return $payment_methods;
}
/**
* Implements hook_commerce_payment_transaction_status_info().
*/
function commerce_pos_commerce_payment_transaction_status_info() {
$statuses[COMMERCE_PAYMENT_STATUS_VOID] = array(
'status' => COMMERCE_PAYMENT_STATUS_VOID,
'title' => t('Void'),
'icon' => drupal_get_path('module', 'commerce_pos') . '/images/icon-void.png',
'total' => FALSE,
);
$statuses[COMMERCE_POS_PAYMENT_STATUS_CANCELED] = array(
'status' => COMMERCE_POS_PAYMENT_STATUS_CANCELED,
'title' => t('Canceled'),
'icon' => drupal_get_path('module', 'commerce_payment') . '/theme/icon-failure.png',
'total' => FALSE,
);
return $statuses;
}
/**
* Implements hook_commerce_order_state_info().
*
* Defines a new state that orders created by the POS live in until they have
* been fully processed.
*/
function commerce_pos_commerce_order_state_info() {
$order_states = array();
$order_states['commerce_pos'] = array(
'name' => 'commerce_pos',
'title' => t('Point of Sale'),
'description' => t('Orders in this state are currently being generated by the POS.'),
'weight' => 10,
'default_status' => 'creating',
);
return $order_states;
}
/**
* Implements hook_commerce_order_status_info().
*/
function commerce_pos_commerce_order_status_info() {
$order_statuses = array();
$order_statuses['commerce_pos_in_progress'] = array(
'name' => 'commerce_pos_in_progress',
'title' => t('POS - In Progress'),
'state' => 'commerce_pos',
);
$order_statuses['commerce_pos_parked'] = array(
'name' => 'commerce_pos_parked',
'title' => t('POS - Parked'),
'state' => 'commerce_pos',
);
$order_statuses['commerce_pos_voided'] = array(
'name' => 'commerce_pos_voided',
'title' => t('POS - Voided'),
'state' => 'commerce_pos',
);
$order_statuses['commerce_pos_returned'] = array(
'name' => 'commerce_pos_returned',
'title' => t('POS - Returned'),
'state' => 'commerce_pos',
);
return $order_statuses;
}
/**
* Implements hook_commerce_line_item_type_info().
*/
function commerce_pos_commerce_line_item_type_info() {
return array(
'commerce_pos_return' => array(
'type' => 'commerce_pos_return',
'name' => t('POS return'),
'product' => TRUE,
'description' => t('Line item for POS returned products'),
'add_form_submit_value' => t('Add POS return'),
'base' => 'commerce_product_line_item',
),
);
}
/**
* Implements hook_image_default_styles().
*/
function commerce_pos_image_default_styles() {
$styles = array();
$styles['commerce_pos_thumbnail'] = array(
'label' => 'Commerce POS thumbnail',
'effects' => array(
array(
'name' => 'image_scale_and_crop',
'data' => array(
'width' => 75,
'height' => 75,
),
'weight' => 0,
),
),
);
return $styles;
}
/**
* Implements hook_commerce_pos_balance_summary_row_info().
*
* Taken from commerce_payment_commerce_payment_totals_row_info().
*/
function commerce_payment_commerce_pos_balance_summary_row_info($totals, $order) {
$rows = array();
if ($order) {
// @TODO: Fix for when there's a FALSE $balance.
$balance = commerce_payment_order_balance($order, $totals);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$original_balance = $order_wrapper->commerce_order_total->amount->value();
}
if (count($totals) == 0) {
// Add a row for the remaining balance on the order.
if ($order) {
$order_to_pay_classes = array('order-to-pay');
if ($balance['amount'] > 0 && $original_balance > 0 || $balance['amount'] < 0 && $original_balance < 0) {
$order_to_pay_classes[] = 'order-to-pay-positive';
}
else {
$order_to_pay_classes[] = 'order-to-pay-none';
}
$rows[] = array(
'data' => array(
array(
'data' => ($balance['amount'] < 0) ? t('Refund') : t('To pay'),
'class' => array('label'),
),
array(
'data' => commerce_pos_commerce_currency_format($balance['amount'], $balance['currency_code']),
'class' => array('balance'),
),
),
'class' => $order_to_pay_classes,
'weight' => 10,
);
if ($balance['amount'] < 0 && $original_balance > 0) {
$rows[] = array(
'data' => array(
array('data' => t('Change'), 'class' => array('label')),
array(
'data' => commerce_pos_commerce_currency_format($balance['amount'], $balance['currency_code']),
'class' => array('balance'),
),
),
'class' => array('order-change'),
'weight' => 15,
);
}
}
}
elseif (count($totals) == 1) {
// Otherwise if there's only a single currency total...
$currency_code = key($totals);
// Add a row for the total amount paid.
$rows[] = array(
'data' => array(
array(
'data' => ($totals[$currency_code] < 0) ? t('Total Refunded') : t('Total Paid'),
'class' => array('label'),
),
array(
'data' => commerce_pos_commerce_currency_format(abs($totals[$currency_code]), $currency_code),
'class' => array('total'),
),
),
'class' => array('total-paid'),
'weight' => 0,
);
// Add a row for the remaining balance on the order.
if ($order) {
$order_to_pay_classes = array('order-to-pay');
if ($balance['amount'] > 0 && $original_balance > 0 || $balance['amount'] < 0 && $original_balance < 0) {
$order_to_pay_classes[] = 'order-to-pay-positive';
}
else {
$order_to_pay_classes[] = 'order-to-pay-none';
}
$rows[] = array(
'data' => array(
array(
'data' => ($balance < 0) ? t('To Refund') : t('To Pay'),
'class' => array('label'),
),
array(
'data' => commerce_pos_commerce_currency_format($balance['amount'], $balance['currency_code']),
'class' => array('balance'),
),
),
'class' => $order_to_pay_classes,
'weight' => 10,
);
if ($balance['amount'] < 0 & $original_balance > 0) {
$rows[] = array(
'data' => array(
array('data' => t('Change'), 'class' => array('label')),
array(
'data' => commerce_currency_format($balance['amount'] * -1, $balance['currency_code']),
'class' => array('balance'),
),
),
'class' => array('order-change'),
'weight' => 15,
);
}
}
}
else {
$weight = 0;
foreach ($totals as $currency_code => $amount) {
$order_to_pay_classes = array(
'order-to-pay',
'order-to-pay-' . $currency_code,
);
if ($amount > 0) {
$order_to_pay_classes[] = 'order-to-pay-positive';
}
else {
$order_to_pay_classes[] = 'order-to-pay-none';
}
$rows[] = array(
'data' => array(
array(
'data' => ($amount < 0) ? t('To Refund (@currency_code)', array('@currency_code' => $currency_code)) : t('To pay (@currency_code)', array('@currency_code' => $currency_code)),
'class' => array('label'),
),
array(
'data' => commerce_pos_commerce_currency_format($amount, $currency_code),
'class' => array('balance'),
),
),
'class' => $order_to_pay_classes,
'weight' => $weight++,
);
if ($amount < 0) {
$rows[] = array(
'data' => array(
array(
'data' => t('Change (@currency_code)', array('@currency_code' => $currency_code)),
'class' => array('label'),
),
array(
'data' => commerce_pos_commerce_currency_format($amount, $currency_code),
'class' => array('balance'),
),
),
'class' => array('order-change', 'order-change-' . $currency_code),
'weight' => $weight++,
);
}
}
}
return $rows;
}
/**
* Implements hook_commerce_price_formatted_components_alter().
*/
function commerce_pos_commerce_price_formatted_components_alter(&$components, $price, $entity) {
$is_pos_order = (isset($entity->type) && $entity->type == 'commerce_order' && $entity->status == 'commerce_pos_in_progress');
if ($is_pos_order) {
foreach ($components as $component_name => &$component) {
// Make sure discount components show up before tax, which has a weight of 0.
if (strpos($component_name, 'discount|') === 0) {
$component['weight'] = -10;
}
switch ($component_name) {
case 'base_price':
$component['title'] = t('Subtotal (excl taxes)');
break;
case 'commerce_price_formatted_amount':
$component['title'] = t('Total');
break;
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function commerce_pos_form_commerce_cop_edit_payment_form_alter(&$form, &$form_state) {
$pos_default = 0;
if (!empty($form['id']['#default_value'])) {
$enabled_payment_methods = variable_get('commerce_pos_payment_methods', array());
$payment_id = $form['id']['#default_value'];
if (in_array($payment_id, $enabled_payment_methods)) {
$pos_default = 1;
}
}
$form['pos'] = array(
'#type' => 'checkbox',
'#title' => t('Available on POS'),
'#description' => t('TRUE or FALSE indicating whether or not this payment method can be used in the POS interface.'),
'#default_value' => $pos_default,
);
$form['actions']['#weight'] = 50;
$form['#submit'][] = 'commerce_pos_cop_edit_form_submit';
}
/**
* Additional submit handler for the Commerce COP payment method form.
*
* This looks to see whether the "Available POS" checkbox was checked or not
* and add/removes the payment method from the list of enabled methods for the
* POS.
*/
function commerce_pos_cop_edit_form_submit($form, &$form_state) {
if (isset($form_state['values']['pos'])) {
$payment_method_id = $form_state['values']['id'];
if (!empty($form_state['values']['pos'])) {
commerce_pos_enable_payment_method($payment_method_id);
}
else {
commerce_pos_disable_payment_method($payment_method_id);
}
}
}
/**
* Page callback: Displays the POS user interface.
*
* @param int $form_id
* The ID of the form to load.
*
* @return array
* renderable array
*/
function commerce_pos_page($form_id = NULL) {
global $user;
if (empty($form_id)) {
return MENU_NOT_FOUND;
}
$page['header'] = array(
'#markup' => theme('commerce_pos_header', array('account' => $user)),
);
$page['form'] = drupal_get_form($form_id, 'return');
return $page;
}
/**
* Retrieves a list of Commerce Custom Offline Payment methods that are enabled on the POS.
*/
function commerce_pos_get_payment_methods() {
// First get a list of all offline payment methods.
$cop_payment_methods = commerce_cop_get_payments();
$enabled_payment_methods = variable_get('commerce_pos_payment_methods', array());
$enabled_pos_methods = array();
foreach ($cop_payment_methods as $method_id => $payment_method) {
if (!empty($payment_method['status']) && in_array($method_id, $enabled_payment_methods)) {
$enabled_pos_methods[$method_id] = $payment_method;
}
}
return $enabled_pos_methods;
}
/**
* Retrieves a list of payment options for the Point of Sale payment form.
*/
function commerce_pos_get_payment_options() {
$options = array();
$methods = commerce_pos_get_payment_methods();
foreach ($methods as $method) {
$options[$method['id']] = array(
'id' => $method['id'],
'title' => $method['title'],
);
}
$options += module_invoke_all('commerce_pos_payment_options_info');
return $options;
}
/**
* Enables a Commerce Custom Offline Payment method for use in the POS.
*/
function commerce_pos_enable_payment_method($method_id) {
$enabled_payment_methods = variable_get('commerce_pos_payment_methods', array());
if (!in_array($method_id, $enabled_payment_methods)) {
$enabled_payment_methods[] = $method_id;
variable_set('commerce_pos_payment_methods', $enabled_payment_methods);
return TRUE;
}
return FALSE;
}
/**
* Disables a Commerce Custom Offline Payment method from use in the POS.
*
* @param int $method_id
* COP method to be disabled.
*
* @return bool
* True if the method exists, false if it does not.
*/
function commerce_pos_disable_payment_method($method_id) {
$enabled_payment_methods = variable_get('commerce_pos_payment_methods', array());
foreach ($enabled_payment_methods as $key => $enabled_method_id) {
if ($method_id == $enabled_method_id) {
array_splice($enabled_payment_methods, $key, 1);
variable_set('commerce_pos_payment_methods', $enabled_payment_methods);
return TRUE;
}
}
return FALSE;
}
/**
* Ensures the phone number field is present on the user entity.
*/
function commerce_pos_user_phone_number_configure() {
// Look for or add a text field to the user type to store their phone number.
$field_name = 'commerce_pos_phone_number';
$field = field_info_field($field_name);
$instance = field_info_instance('user', $field_name, 'user');
if (empty($field)) {
// Create the base field first.
$field = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_types' => array(),
'field_name' => $field_name,
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'locked' => 0,
'module' => 'text',
'settings' => array(
'max_length' => 255,
),
'translatable' => 0,
'type' => 'text',
);
field_create_field($field);
}
if (empty($instance)) {
// Create the field instance.
$instance = array(
'bundle' => 'user',
'default_value' => NULL,
'deleted' => 0,
'description' => '',
'display' => array(
'default' => array(
'label' => 'above',
'module' => 'text',
'settings' => array(),
'type' => 'text_default',
'weight' => 0,
),
),
'entity_type' => 'user',
'field_name' => $field_name,
'label' => 'POS Phone Number',
'required' => 0,
'settings' => array(
'text_processing' => 0,
'user_register_form' => 0,
),
'widget' => array(
'active' => 1,
'module' => 'text',
'settings' => array(
'size' => 60,
),
'type' => 'text_textfield',
'weight' => 11,
),
);
field_create_instance($instance);
}
}
/**
* Returns a sorted array of POS balance table rows.
*
* @param array $totals
* An array of payment totals whose keys are currency codes and values are the
* total amount paid in each currency.
* @param object $order
* If available, the order object to which the payments apply.
*
* @return array
* An array of table row data as expected by theme_table().
*
* @see hook_commerce_payment_totals_row_info()
*/
function commerce_pos_balance_summary_rows(array $totals, $order) {
// Retrieve rows defined by the hook and allow other modules to alter them.
$rows = module_invoke_all('commerce_pos_balance_summary_row_info', $totals, $order);
drupal_alter('commerce_pos_balance_summary_row_info', $rows, $totals, $order);
// Sort the rows by weight and return the array.
uasort($rows, 'drupal_sort_weight');
return $rows;
}
/**
* Returns a summary view of a POS transaction's order.
*/
function commerce_pos_balance_summary($order, $display_only = FALSE, $include_balance = TRUE) {
return theme('commerce_pos_order_balance_summary', array(
'order' => $order,
'display_only' => $display_only,
'include_balance' => $include_balance,
'pos_transaction' => CommercePosService::getOrderTransaction($order->order_id),
));
}
/**
* Retrieves a product's display node id.
*
* @param int $product_id
* The product ID to retrieve the display node for.
*
* @return int
* The nid of the display node, or 0 if none was found.
*/
function commerce_pos_get_product_display_nid($product_id) {
global $language;
// Iterate through fields which refer to products.
foreach (commerce_info_fields('commerce_product_reference', 'node') as $field) {
$query = 'SELECT entity_id, language FROM {field_data_' . $field['field_name'] . '}
WHERE entity_type = :node
AND ' . $field['field_name'] . '_product_id = :product_id
AND (language = :language OR language = :language_none)';
$result = db_query($query, array(
':node' => 'node',
'product_id' => $product_id,
'language' => $language->language,
'language_none' => LANGUAGE_NONE,
));
$no_lang_id = 0;
foreach ($result as $row) {
if ($row->language == $language->language) {
return $row->entity_id;
}
elseif ($row->language == LANGUAGE_NONE) {
$no_lang_id = $row->entity_id;
}
}
// If we made it this far, there were either no matches or the only match
// was one without a language set.
return $no_lang_id;
}
return 0;
}
/**
* Formats an order's price as components.
*
* A customized version of the 'commerce_price_formatted_components' display
* from commerce_price_field_formatter_view().
*
* @param object $order_wrapper
* The entity metadata wrapper for the order.
* @param string $field_name
* The name of the price field to format.
* @param bool $print
* A boolean indicating if this price is being displayed in a print view.
*
* @return array
* A render array for the formatted price field.
*/
function commerce_pos_price_order_format($order_wrapper, $field_name, $print = FALSE) {
$price = $order_wrapper->{$field_name}->value();
$components = array();
$weight = 0;
foreach ($price['data']['components'] as $key => $component) {
$component_type = commerce_price_component_type_load($component['name']);
if (empty($components[$component['name']])) {
$components[$component['name']] = array(
'title' => check_plain($component_type['display_title']),
'price' => commerce_price_component_total($price, $component['name']),
'weight' => $component_type['weight'],
);
$weight = max($weight, $component_type['weight']);
}
}
// If there is only a single component and its price equals the field's,
// then remove it and just show the actual price amount.
if (count($components) == 1 && in_array('base_price', array_keys($components))) {
$components = array();
}
// Add the actual field value to the array.
$components['commerce_price_formatted_amount'] = array(
'title' => t('Order total'),
'price' => $price,
'weight' => $weight + 1,
);
$price['#commerce_pos_print'] = $print;
$order = $order_wrapper->value();
drupal_alter('commerce_price_formatted_components', $components, $price, $order);
// Sort the components by weight.
uasort($components, 'drupal_sort_weight');
// Format the prices for display.
foreach ($components as $key => &$component) {
$formatted_price = commerce_pos_commerce_currency_format(
$component['price']['amount'],
$component['price']['currency_code'],
$order_wrapper->value()
);
$component['formatted_price'] = $formatted_price;
}
return array(
'#markup' => theme('commerce_pos_price_formatted_components', array(
'components' => $components,
'price' => $price,
)),
);
}
/**
* Formats an empty order with 0 value as components.
*
* A customized version of the 'commerce_price_formatted_components' display
* from commerce_price_field_formatter_view().
*
* @param array|null $currency_code
* The currency code of the order.
*
* @return array
* A render array for the formatted price field.
*/
function commerce_pos_empty_order_format_total(array $currency_code = NULL) {
$currency = empty($currency_code) ? commerce_default_currency() : $currency_code;
$formatted_price = commerce_currency_format(0, $currency_code);
$price = array(
'amount' => 0,
'currency_code' => $currency,
);
$components = array(
'commerce_price_formatted_amount' => array(
'title' => t('Total'),
'price' => $price,
'weight' => 1,
'formatted_price' => '(' . $formatted_price . ')',
),
);
return array(
'#markup' => theme('commerce_pos_price_formatted_components', array(
'components' => $components,
'price' => $price,
)),
);
}
/**
* Builds an array of product data for the product autocomplete.
*
* @param int $product_id
* Product id of the product to build.
*
* @return array|bool
* Array of product data for the autocomplete, or false if product couldn't be loaded.
*/
function _commerce_pos_product_autocomplete_build($product_id) {
$data = FALSE;
if ($product = commerce_product_load($product_id)) {
$data = array(
'markup' => theme('commerce_pos_product_result', array('product' => $product)),
'title' => $product->title,
);
}
return $data;
}