forked from opensourcepos/opensourcepos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSale_lib.php
More file actions
1725 lines (1489 loc) · 56 KB
/
Copy pathSale_lib.php
File metadata and controls
1725 lines (1489 loc) · 56 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
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
namespace app\Libraries;
use App\Models\Attribute;
use App\Models\Customer;
use App\Models\Dinner_table;
use App\Models\Item;
use App\Models\Item_kit_items;
use App\Models\Item_quantity;
use App\Models\Item_taxes;
use App\Models\Enums\Rounding_mode;
use App\Models\Sale;
use CodeIgniter\Session\Session;
use App\Models\Stock_location;
use Config\OSPOS;
use ReflectionException;
/**
* Sale library
*
* Library with utilities to manage sales
*/
class Sale_lib
{
private const KEY_SHORTCUT_DEFAULTS = [
'cancel' => ['value' => '27 | ESC', 'code' => 27, 'label' => 'ESC'],
'items' => ['value' => '49 | ALT + 1', 'code' => 49, 'label' => 'ALT + 1'],
'customers' => ['value' => '50 | ALT + 2', 'code' => 50, 'label' => 'ALT + 2'],
'suspend' => ['value' => '51 | ALT + 3', 'code' => 51, 'label' => 'ALT + 3'],
'suspended' => ['value' => '52 | ALT + 4', 'code' => 52, 'label' => 'ALT + 4'],
'amount' => ['value' => '53 | ALT + 5', 'code' => 53, 'label' => 'ALT + 5'],
'payment' => ['value' => '54 | ALT + 6', 'code' => 54, 'label' => 'ALT + 6'],
'complete' => ['value' => '55 | ALT + 7', 'code' => 55, 'label' => 'ALT + 7'],
'finish' => ['value' => '56 | ALT + 8', 'code' => 56, 'label' => 'ALT + 8'],
'help' => ['value' => '57 | ALT + 9', 'code' => 57, 'label' => 'ALT + 9'],
];
private Attribute $attribute;
private Customer $customer;
private Dinner_table $dinner_table;
private Item $item;
private Item_kit_items $item_kit_items;
private Item_quantity $item_quantity;
private Item_taxes $item_taxes;
private Sale $sale;
private Stock_location $stock_location;
private Session $session;
private array $config;
public function __construct()
{
$this->session = session();
$this->attribute = model(Attribute::class);
$this->customer = model(Customer::class);
$this->dinner_table = model(Dinner_table::class);
$this->item = model(Item::class);
$this->item_kit_items = model(Item_kit_items::class);
$this->item_quantity = model(Item_quantity::class);
$this->item_taxes = model(Item_taxes::class);
$this->sale = model(Sale::class);
$this->stock_location = model(Stock_location::class);
$this->config = config(OSPOS::class)->settings;
}
/**
* @return array
*/
public function get_line_sequence_options(): array
{
return [
'0' => lang('Sales.entry'),
'1' => lang('Sales.group_by_type'),
'2' => lang('Sales.group_by_category')
];
}
/**
* @return array
*/
public function get_register_mode_options(): array
{
$register_modes = [];
if (!$this->config['invoice_enable']) {
$register_modes['sale'] = lang('Sales.sale');
} else {
$register_modes['sale'] = lang('Sales.receipt');
$register_modes['sale_quote'] = lang('Sales.quote');
if ($this->config['work_order_enable']) {
$register_modes['sale_work_order'] = lang('Sales.work_order');
}
$register_modes['sale_invoice'] = lang('Sales.invoice');
}
$register_modes['return'] = lang('Sales.return');
return $register_modes;
}
private const ALLOWED_INVOICE_TYPES = [
'invoice',
'tax_invoice',
'custom_invoice',
'custom_tax_invoice'
];
private const ALLOWED_RECEIPT_TEMPLATES = [
'receipt_default',
'receipt_short'
];
public function get_invoice_type_options(): array
{
$invoice_types = [];
$invoice_types['invoice'] = lang('Sales.invoice_type_invoice');
$invoice_types['tax_invoice'] = lang('Sales.invoice_type_tax_invoice');
$invoice_types['custom_invoice'] = lang('Sales.invoice_type_custom_invoice');
$invoice_types['custom_tax_invoice'] = lang('Sales.invoice_type_custom_tax_invoice');
return $invoice_types;
}
/**
* Returns the available keyboard shortcut choices for the configuration screen.
*
* @return array<string, string>
*/
public function getKeyShortcutsOptions(): array
{
$keyShortcuts = [];
foreach (self::KEY_SHORTCUT_DEFAULTS as $shortcut) {
$keyShortcuts[$shortcut['value']] = $shortcut['label'];
}
return $keyShortcuts;
}
/**
* Returns parsed shortcut bindings from app_config with sensible defaults.
*
* @return array<string, array{value:string,code:int,label:string}>
*/
public function getKeyShortcuts(): array
{
$keyboardShortcuts = [];
foreach (self::KEY_SHORTCUT_DEFAULTS as $name => $default) {
$value = $this->config["key_$name"] ?? $default['value'];
$parts = array_map('trim', explode('|', $value, 2));
$keyboardShortcuts[$name] = [
'value' => $value,
'code' => (int)($parts[0] ?? $default['code']),
'label' => $parts[1] ?? $default['label']
];
}
return $keyboardShortcuts;
}
public static function isValidInvoiceType(string $invoice_type): bool
{
return in_array($invoice_type, self::ALLOWED_INVOICE_TYPES, true);
}
public static function isValidReceiptTemplate(string $receipt_template): bool
{
return in_array($receipt_template, self::ALLOWED_RECEIPT_TEMPLATES, true);
}
/**
* @return array
*/
public function get_cart(): array
{
if (!$this->session->get('sales_cart')) {
$this->set_cart([]);
}
return $this->session->get('sales_cart');
}
/**
* @param array $cart
* @return array
*/
public function sort_and_filter_cart(array $cart): array
{
if (empty($cart)) {
return $cart;
}
$filtered_cart = [];
foreach ($cart as $k => $v) { // TODO: We should not be using single-letter variable names for readability. Several of these foreach loops should be refactored.
if ($v['print_option'] == PRINT_YES) {
if ($v['price'] == 0.0) {
$v['discount'] = 0.0;
}
$filtered_cart[] = $v;
}
}
// TODO: This set of if/elseif/else needs to be converted to a switch statement
// Entry sequence (this will render kits in the expected sequence)
if ($this->config['line_sequence'] == '0') {
$sort = [];
foreach ($filtered_cart as $k => $v) {
$sort['line'][$k] = $v['line'];
}
array_multisort($sort['line'], SORT_ASC, $filtered_cart);
}
// Group by Stock Type (nonstock first - type 1, stock next - type 0)
elseif ($this->config['line_sequence'] == '1') { // TODO: Need to change these to constants
$sort = [];
foreach ($filtered_cart as $k => $v) {
$sort['stock_type'][$k] = $v['stock_type'];
$sort['description'][$k] = $v['description'];
$sort['name'][$k] = $v['name'];
}
array_multisort($sort['stock_type'], SORT_DESC, $sort['description'], SORT_ASC, $sort['name'], SORT_ASC, $filtered_cart);
}
// Group by Item Category
elseif ($this->config['line_sequence'] == '2') { // TODO: Need to change these to constants
$sort = [];
foreach ($filtered_cart as $k => $v) {
$sort['category'][$k] = $v['stock_type'];
$sort['description'][$k] = $v['description'];
$sort['name'][$k] = $v['name'];
}
array_multisort($sort['category'], SORT_DESC, $sort['description'], SORT_ASC, $sort['name'], SORT_ASC, $filtered_cart);
}
// Group by entry sequence in descending sequence (the Standard)
else {
$sort = [];
foreach ($filtered_cart as $k => $v) {
$sort['line'][$k] = $v['line'];
}
array_multisort($sort['line'], SORT_ASC, $filtered_cart);
}
return $filtered_cart;
}
/**
* @param array $cart_data
* @return void
*/
public function set_cart(array $cart_data): void
{
$this->session->set('sales_cart', $cart_data);
}
/**
* @return void
*/
public function empty_cart(): void
{
$this->session->remove('sales_cart');
}
/**
* @return void
*/
public function remove_temp_items(): void
{
// Loop through the cart items and delete temporary items specific to this sale
$cart = $this->get_cart();
foreach ($cart as $line => $item) {
if ($item['item_type'] == ITEM_TEMP) { // TODO: === ?
$this->item->delete($item['item_id']);
}
}
}
/**
* @return string
*/
public function get_comment(): string
{
// Avoid returning a null that results in a 0 in the comment if nothing is set/available
$comment = $this->session->get('sales_comment');
return empty($comment) ? '' : $comment;
}
/**
* @param string $comment
* @return void
*/
public function set_comment(string $comment): void
{
$this->session->set('sales_comment', $comment);
}
/**
* @return void
*/
public function clear_comment(): void
{
$this->session->remove('sales_comment');
}
/**
* @return string|null
*/
public function get_invoice_number(): ?string
{
return $this->session->get('sales_invoice_number');
}
/**
* @return string|null
*/
public function get_quote_number(): ?string
{
return $this->session->get('sales_quote_number');
}
/**
* @return string|null
*/
public function get_work_order_number(): ?string
{
return $this->session->get('sales_work_order_number');
}
/**
* @return int|null
*/
public function get_sale_type(): ?int
{
return $this->session->get('sale_type', 0);
}
/**
* @param int $invoice_number
* @param bool $keep_custom
* @return void
*/
public function set_invoice_number(int $invoice_number, bool $keep_custom = false): void
{
$current_invoice_number = $this->session->get('sales_invoice_number');
if (!$keep_custom || empty($current_invoice_number)) {
$this->session->set('sales_invoice_number', $invoice_number);
}
}
/**
* @param string|null $quote_number
* @param bool $keep_custom
* @return void
*/
public function set_quote_number(?string $quote_number, bool $keep_custom = false): void
{
$current_quote_number = $this->session->get('sales_quote_number');
if (!$keep_custom || empty($current_quote_number)) {
$this->session->set('sales_quote_number', $quote_number);
}
}
/**
* @param string|null $work_order_number
* @param bool $keep_custom
* @return void
*/
public function set_work_order_number(?string $work_order_number, bool $keep_custom = false): void
{
$current_work_order_number = $this->session->get('sales_work_order_number');
if (!$keep_custom || empty($current_work_order_number)) {
$this->session->set('sales_work_order_number', $work_order_number);
}
}
/**
* @param int $sale_type
* @param bool $keep_custom
* @return void
*/
public function set_sale_type(int $sale_type, bool $keep_custom = false): void
{
$current_sale_type = $this->session->get('sale_type');
if (!$keep_custom || empty($current_sale_type)) {
$this->session->set('sale_type', $sale_type);
}
}
/**
* @return void
*/
public function clear_invoice_number(): void
{
$this->session->remove('sales_invoice_number');
}
/**
* @return void
*/
public function clear_quote_number(): void
{
$this->session->remove('sales_quote_number');
}
/**
* @return void
*/
public function clear_work_order_number(): void
{
$this->session->remove('sales_work_order_number');
}
/**
* @return void
*/
public function clear_sale_type(): void
{
$this->session->remove('sale_type');
}
/**
* @param int $suspended_id
* @return void
*/
public function set_suspended_id(int $suspended_id): void
{
$this->session->set('suspended_id', $suspended_id);
}
/**
* @return int
*/
public function get_suspended_id(): int
{
return $this->session->get('suspended_id');
}
/**
* @return bool
*/
public function is_invoice_mode(): bool
{
return ($this->session->get('sales_mode') == 'sale_invoice' && $this->config['invoice_enable']);
}
/**
* @return bool
*/
public function is_sale_by_receipt_mode(): bool // TODO: This function is not called anywhere in the code.
{
return ($this->session->get('sales_mode') == 'sale'); // TODO: === ?
}
/**
* @return bool
*/
public function is_quote_mode(): bool
{
return ($this->session->get('sales_mode') == 'sale_quote'); // TODO: === ?
}
/**
* @return bool
*/
public function is_return_mode(): bool
{
return ($this->session->get('sales_mode') == 'return'); // TODO: === ?
}
/**
* @return bool
*/
public function is_work_order_mode(): bool
{
return ($this->session->get('sales_mode') == 'sale_work_order'); // TODO: === ?
}
/**
* @param string $price_work_orders
* @return void
*/
public function set_price_work_orders(string $price_work_orders): void
{
$this->session->set('sales_price_work_orders', $price_work_orders);
}
/**
* @return bool
*/
public function is_price_work_orders(): bool
{
return ($this->session->get('sales_price_work_orders') == 'true' // TODO: === ?
|| $this->session->get('sales_price_work_orders') == '1'); // TODO: === ?
}
/**
* @param bool $print_after_sale
* @return void
*/
public function set_print_after_sale(bool $print_after_sale): void
{
$this->session->set('sales_print_after_sale', $print_after_sale);
}
/**
* @return bool
*/
public function is_print_after_sale(): bool
{ // TODO: this needs to be converted to a switch statement
if ($this->config['print_receipt_check_behaviour'] == 'always') { // TODO: 'behaviour' is the british spelling, but the rest of the code is in American English. Not a big deal, but noticed. Also ===
return true;
} elseif ($this->config['print_receipt_check_behaviour'] == 'never') { // TODO: === ?
return false;
} else { // Remember last setting, session based though
return ($this->session->get('sales_print_after_sale') == 'true' // TODO: === ?
|| $this->session->get('sales_print_after_sale') == '1'); // TODO: === ?
}
}
/**
* @param string $email_receipt
* @return void
*/
public function set_email_receipt(string $email_receipt): void
{
$this->session->set('sales_email_receipt', $email_receipt);
}
/**
* @return void
*/
public function clear_email_receipt(): void
{
$this->session->remove('sales_email_receipt');
}
/**
* @return bool
*/
public function is_email_receipt(): bool
{ // TODO: this needs to be converted to a switch statement
if ($this->config['email_receipt_check_behaviour'] == 'always') { // TODO: 'behaviour' is the british spelling, but the rest of the code is in American English. Not a big deal, but noticed. Also ===
return true;
} elseif ($this->config['email_receipt_check_behaviour'] == 'never') { // TODO: === ?
return false;
} else { // Remember last setting, session based though
return ($this->session->get('sales_email_receipt') == 'true' // TODO: === ?
|| $this->session->get('sales_email_receipt') == '1'); // TODO: === ?
}
}
/**
* Multiple Payments
*/
public function get_payments(): array
{
if (!$this->session->get('sales_payments')) {
$this->set_payments([]);
}
return $this->session->get('sales_payments');
}
/**
* Multiple Payments
*/
public function set_payments(array $payments_data): void
{
$this->session->set('sales_payments', $payments_data);
}
/**
* Adds a new payment to the payments array or updates an existing one.
* It will also disable cash_mode if a non-qualifying payment type is added.
* @param string $payment_id
* @param string $payment_amount
* @param int $cash_adjustment
*/
public function add_payment(string $payment_id, string $payment_amount, int $cash_adjustment = CASH_ADJUSTMENT_FALSE): void
{
$payments = $this->get_payments();
if (isset($payments[$payment_id])) {
// payment_method already exists, add to payment_amount
$payments[$payment_id]['payment_amount'] = bcadd($payments[$payment_id]['payment_amount'], $payment_amount);
} else {
// Add to existing array
$payment = [
$payment_id => [
'payment_type' => $payment_id,
'payment_amount' => $payment_amount,
'cash_refund' => 0,
'cash_adjustment' => $cash_adjustment
]
];
$payments += $payment;
}
if ($this->session->get('cash_mode')) {
if ($this->session->get('cash_rounding') && $payment_id != lang('Sales.cash') && $payment_id != lang('Sales.cash_adjustment')) {
$this->session->set('cash_mode', CASH_MODE_FALSE);
}
}
$this->set_payments($payments);
}
/**
* Multiple Payments
*/
public function edit_payment(string $payment_id, float $payment_amount): bool
{
$payments = $this->get_payments();
if (isset($payments[$payment_id])) {
$payments[$payment_id]['payment_type'] = $payment_id;
$payments[$payment_id]['payment_amount'] = $payment_amount;
$this->set_payments($payments);
return true;
}
return false;
}
/**
* Delete the selected payment from the payment array and if cash rounding is enabled
* and the payment type is one of the cash types then automatically delete the other
* @param string $payment_id
*/
public function delete_payment(string $payment_id): void
{
$payments = $this->get_payments();
$decoded_payment_id = urldecode($payment_id);
unset($payments[$decoded_payment_id]);
$cash_rounding = $this->reset_cash_rounding();
if ($cash_rounding) {
if ($decoded_payment_id == lang('Sales.cash')) { // TODO: === ?
unset($payments[lang('Sales.cash_adjustment')]);
}
if ($decoded_payment_id == lang('Sales.cash_adjustment')) { // TODO: === ?
unset($payments[lang('Sales.cash')]);
}
}
$this->set_payments($payments);
}
/**
* Multiple Payments
*/
public function empty_payments(): void // TODO: function verbs are very inconsistent in these libraries.
{
$this->session->remove('sales_payments');
}
/**
* Retrieve the total payments made, excluding any cash adjustments
* and establish if cash_mode is in play
*/
public function get_payments_total(): string
{
$subtotal = '0.0';
$cash_mode_eligible = CASH_MODE_TRUE;
foreach ($this->get_payments() as $payments) {
if (!$payments['cash_adjustment']) {
$subtotal = bcadd($payments['payment_amount'], $subtotal);
}
if (lang('Sales.cash') != $payments['payment_type'] && lang('Sales.cash_adjustment') != $payments['payment_type']) {
$cash_mode_eligible = CASH_MODE_FALSE;
}
}
if ($cash_mode_eligible && $this->session->get('cash_rounding')) { // TODO: $cache_mode_eligible will always evaluate to true
$this->session->set('cash_mode', CASH_MODE_TRUE);
}
return $subtotal;
}
/**
* Returns 'subtotal', 'total', 'cash_total', 'payment_total', 'amount_due', 'cash_amount_due', 'paid_in_full'
* 'subtotal', 'discounted_subtotal', 'tax_exclusive_subtotal', 'item_count', 'total_units', 'cash_adjustment_amount'
*/
public function get_totals(array $taxes): array
{
$totals = [];
$prediscount_subtotal = '0.0';
$subtotal = '0.0';
$total = '0.0';
$total_discount = '0.0';
$item_count = 0;
$total_units = 0.0;
foreach ($this->get_cart() as $item) {
if ($item['stock_type'] == HAS_STOCK) {
$item_count++;
$total_units += $item['quantity'];
}
$discount_amount = $this->get_item_discount($item['quantity'], $item['price'], $item['discount'], $item['discount_type']);
$total_discount = bcadd($total_discount, $discount_amount);
$extended_amount = $this->get_extended_amount($item['quantity'], $item['price']);
$extended_discounted_amount = $this->get_extended_amount($item['quantity'], $item['price'], $discount_amount);
$prediscount_subtotal = bcadd($prediscount_subtotal, $extended_amount);
$total = bcadd($total, $extended_discounted_amount);
$subtotal = bcadd($subtotal, $extended_discounted_amount);
}
$totals['prediscount_subtotal'] = $prediscount_subtotal;
$totals['total_discount'] = $total_discount;
$sales_tax = '0';
foreach ($taxes as $tax) {
if ($tax['tax_type'] === Tax_lib::TAX_TYPE_EXCLUDED) {
$total = bcadd($total, $tax['sale_tax_amount']);
$sales_tax = bcadd($sales_tax, $tax['sale_tax_amount']);
} else {
$subtotal = bcsub($subtotal, $tax['sale_tax_amount']);
}
}
$totals['subtotal'] = $subtotal;
$totals['total'] = $total;
$totals['tax_total'] = $sales_tax;
$payment_total = $this->get_payments_total();
$totals['payment_total'] = $payment_total;
$cash_rounding = $this->session->get('cash_rounding');
$cash_mode = $this->session->get('cash_mode');
if ($cash_rounding) {
$cash_total = $this->check_for_cash_rounding($total);
$totals['cash_total'] = $cash_total;
} else {
$cash_total = $total;
$totals['cash_total'] = $cash_total;
}
$amount_due = bcsub($total, $payment_total);
$totals['amount_due'] = $amount_due;
$cash_amount_due = bcsub($cash_total, $payment_total);
$totals['cash_amount_due'] = $cash_amount_due;
if ($cash_mode) { // TODO: Convert to ternary notation
$current_due = $cash_amount_due;
} else {
$current_due = $amount_due;
}
// 0 decimal -> 1 / 2 = 0.5, 1 decimals -> 0.1 / 2 = 0.05, 2 decimals -> 0.01 / 2 = 0.005
$threshold = bcpow('10', (string)-totals_decimals()) / 2;
if ($this->get_mode() == 'return') { // TODO: Convert to ternary notation.
$totals['payments_cover_total'] = $current_due > -$threshold;
} else {
$totals['payments_cover_total'] = $current_due < $threshold;
}
$totals['item_count'] = $item_count;
$totals['total_units'] = $total_units;
$totals['cash_adjustment_amount'] = 0.0;
if ($totals['payments_cover_total']) {
$totals['cash_adjustment_amount'] = round($cash_total - $totals['total'], totals_decimals(), PHP_ROUND_HALF_UP);
}
$cash_mode = $this->session->get('cash_mode'); // TODO: This variable is never used.
return $totals;
}
/**
* Multiple Payments
*/
public function get_amount_due(): string
{
// Payment totals need to be identified first so that we know whether or not there is a non-cash payment involved
$payment_total = $this->get_payments_total();
$sales_total = $this->get_total();
$amount_due = bcsub($sales_total, $payment_total);
$precision = totals_decimals();
$rounded_due = bccomp((string)round((float)$amount_due, $precision, PHP_ROUND_HALF_UP), '0', $precision); // TODO: Is round() currency safe?
// Take care of rounding error introduced by round tripping payment amount to the browser
return $rounded_due == 0 ? '0' : $amount_due; // TODO: ===
}
/**
* @return int
*/
public function get_customer(): int
{
if (!$this->session->get('sales_customer')) {
$this->set_customer(-1); // TODO: Replace -1 with a constant
}
return $this->session->get('sales_customer');
}
/**
* @param int $customer_id
* @return void
*/
public function set_customer(int $customer_id): void
{
$this->session->set('sales_customer', $customer_id);
}
/**
* @return void
*/
public function remove_customer(): void
{
$this->session->remove('sales_customer');
}
/**
* @return int
*/
public function get_employee(): int
{
if (!$this->session->get('sales_employee')) {
$this->set_employee(-1); // TODO: Replace -1 with a constant
}
return $this->session->get('sales_employee');
}
/**
* @param int $employee_id
* @return void
*/
public function set_employee(int $employee_id): void
{
$this->session->set('sales_employee', $employee_id);
}
/**
* @return void
*/
public function remove_employee(): void
{
$this->session->remove('sales_employee');
}
/**
* @return string
*/
public function get_mode(): string
{
if (!$this->session->get('sales_mode')) {
$this->set_mode('sale');
}
return $this->session->get('sales_mode');
}
/**
* @param string $mode
* @return void
*/
public function set_mode(string $mode): void
{
$this->session->set('sales_mode', $mode);
}
/**
* @return void
*/
public function clear_mode(): void
{
$this->session->remove('sales_mode');
}
/**
* @return int|null
*/
public function get_dinner_table(): ?int
{
if (!$this->session->get('dinner_table')) {
if ($this->config['dinner_table_enable']) {
$this->set_dinner_table(1); // TODO: Replace 1 with constant
}
}
return $this->session->get('dinner_table');
}
/**
* @param int|null $dinner_table
* @return void
*/
public function set_dinner_table(?int $dinner_table): void
{
$this->session->set('dinner_table', $dinner_table);
}
/**
* @return void
*/
public function clear_table(): void
{
$this->session->remove('dinner_table');
}
/**
* @return int
*/
public function get_sale_location(): int
{
if (!$this->session->get('sales_location')) {
$this->set_sale_location($this->stock_location->get_default_location_id('sales'));
}
return $this->session->get('sales_location');
}
/**
* @param int $location
* @return void
*/
public function set_sale_location(int $location): void
{
$this->session->set('sales_location', $location);
}
/**
* @param string $payment_type
* @return void
*/
public function set_payment_type(string $payment_type): void
{
$this->session->set('payment_type', $payment_type);
}
/**
* @return string|null
*/
public function get_payment_type(): ?string
{
return $this->session->get('payment_type');
}
/**
* @return void
*/
public function clear_sale_location(): void
{
$this->session->remove('sales_location');
}
/**
* @param string $value
* @return void
*/
public function set_giftcard_remainder(string $value): void
{
$this->session->set('sales_giftcard_remainder', $value);
}
/**
* @return string|null
*/
public function get_giftcard_remainder(): ?string
{
return $this->session->get('sales_giftcard_remainder');
}
/**
* @return void
*/
public function clear_giftcard_remainder(): void
{
$this->session->remove('sales_giftcard_remainder');
}
/**
* @param string $value
* @return void
*/
public function set_rewards_remainder(string $value): void
{
$this->session->set('sales_rewards_remainder', $value);
}
/**
* @return string|null
*/