/
calculation.php
1857 lines (1425 loc) · 87.5 KB
/
calculation.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
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
/**
* @package Phoca Cart
* @author Jan Pavelka - https://www.phoca.cz
* @copyright Copyright (C) Jan Pavelka https://www.phoca.cz
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 and later
* @cms Joomla
* @copyright Copyright (C) Open Source Matters. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*/
defined('_JEXEC') or die();
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
class PhocacartCartCalculation
{
public $correctsubtotal = 1;// because of rounding 0.25 -> 0.3 or 0.2 it can come to difference e.g. 0.01
public $posbruttocalculation = 1;
protected $type = array(0, 1);
public function __construct() {
$app = Factory::getApplication();
$paramsC = PhocacartUtils::getComponentParameters();
// Affect only calculation in POS cart
// Not receipts, invoices
// Display or hide brutto prices in POS cart
$this->posbruttocalculation = PhocacartPos::isPos() ? $paramsC->get('pos_brutto_calculation', 1) : 0;
}
public function setType($type = array(0, 1)) {
$this->type = $type;
}
// ==============
// BASIC PRODUCT
// ==============
public function calculateBasicProducts(&$fullItems, &$fullItemsGroup, &$total, &$stock, &$minqty, &$minmultipleqty, $items) {
$app = Factory::getApplication();
$paramsC = PhocacartUtils::getComponentParameters();
$tax_calculation = $paramsC->get('tax_calculation', 0);
// Moved to product parameters
//$min_ quantity_calculation = $paramsC->get( 'min_ quantity_calculation', 0 );
//$stock_ calculation = $paramsC->get( 'stock_ calculation', 0 );
$price = new PhocacartPrice();
$total['netto'] = 0;
$total['brutto'] = 0;
$total['brutto_currency'] = 0;
$total['tax'] = array();
$total['weight'] = 0;
$total['volume'] = 0;
$total['dnetto'] = 0;
$total['quantity'] = 0;
$total['length'] = 0;
$total['width'] = 0;
$total['height'] = 0;
$total['points_needed'] = 0;
$total['points_received'] = 0;
// Free shipping or payment
$total['free_shipping'] = 0;
$total['free_payment'] = 0;
// Discount fixed amount
$total['discountcartfixedamount'] = array();
$total['couponcartfixedamount'] = array();
$total['discountcarttxtsuffix'] = '';
$total['couponcarttxtsuffix'] = '';
$total['rewardproductusedtotal'] = '';
$total['rewardproducttxtsuffix'] = '';
$total['countallproducts'] = 0;
$total['countphysicalproducts'] = 0;
$total['countdigitalproducts'] = 0;
$total['countpriceondemandproducts'] = 0;
// OPTIONS (VARIANTS) QUANTITY
// The same option can be in different items
$optionsQuantity = array();
// Rounding
$total['rounding'] = 0;
$total['rounding_currency'] = 0;
$total['wdnetto'] = 0;//Subtotal after all discounts
$total['rounding_currency'] = 0;
foreach ($items as $k => $v) {
$item = explode(':', $k);
$itemId = $item[0];
// Define
$fullItems[$k]['id'] = (int)$itemId;
$fullItems[$k]['idkey'] = (string)$k;
$fullItems[$k]['netto'] = 0;
$fullItems[$k]['brutto'] = 0;
$fullItems[$k]['tax'] = 0;
$fullItems[$k]['final'] = 0;// netto or brutto * quantity
$fullItems[$k]['nettodiscount'] = 0;
$fullItems[$k]['bruttodiscount'] = 0;
$fullItems[$k]['taxdiscount'] = 0;
$fullItems[$k]['finaldiscount'] = 0;
$fullItems[$k]['taxid'] = 0;
$fullItems[$k]['taxkey'] = '';// Tax Id: Country Tax Id: Region Tax Id
$fullItems[$k]['taxtitle'] = '';
$fullItems[$k]['weight'] = '';
$fullItems[$k]['volume'] = '';
$fullItems[$k]['quantity'] = $fQ = (int)$v['quantity'];// Quantity of product - one product
$fullItems[$k]['catid'] = 0;
$fullItems[$k]['alias'] = '';
$fullItems[$k]['sku'] = '';
$fullItems[$k]['image'] = '';
$fullItems[$k]['title'] = '';
$fullItems[$k]['stock'] = 0; // database value set in product settings
$fullItems[$k]['stockvalid'] = 1; // variable to inform if stock validity is ok
$fullItems[$k]['stockcalculation'] = 0;
$fullItems[$k]['minqty'] = 0; // database value set in product settings
$fullItems[$k]['minmultipleqty'] = 0;
$fullItems[$k]['minqtyvalid'] = 1; // variable to inform if minimum order is ok
$fullItems[$k]['minmultipleqtyvalid'] = 1;
// DISCOUNTS (Product, Cart, Voucher) / Fixed amount / Percentage
$fullItems[$k]['discountproduct'] = 0;
$fullItems[$k]['discountproducttitle'] = '';
$fullItems[$k]['discountcart'] = 0;
$fullItems[$k]['discountcarttitle'] = '';
$fullItems[$k]['discountcartfixedid'] = 0;
$fullItems[$k]['discountcartid'] = 0;
$fullItems[$k]['couponcart'] = 0;
$fullItems[$k]['couponcarttitle'] = '';
$fullItems[$k]['couponcartfixedid'] = 0;
$fullItems[$k]['couponcartid'] = 0;
$fullItems[$k]['rewardproduct'] = 0;
$fullItems[$k]['rewardproducttitle'] = Text::_('COM_PHOCACART_REWARD_POINTS');
$fullItems[$k]['rewardproductpoints'] = 0;
$fullItems[$k]['rewardproducttxtsuffix'] = '';
$fullItems[$k]['points_needed'] = 0;
$fullItems[$k]['points_received'] = 0;
$fullItems[$k]['type'] = 0;
// GROUP QUANTITY
// Get quantity of a group. Group is sum of all product variations
// - explained in PhocacartDiscountProduct::getProductDiscount
$fullItemsGroup[$itemId]['id'] = (int)$itemId;
$fullItemsGroup[$itemId]['title'] = '';
if (isset($fullItemsGroup[$itemId]['quantity'])) {
$fullItemsGroup[$itemId]['quantity'] += (int)$v['quantity'];
} else {
$fullItemsGroup[$itemId]['quantity'] = (int)$v['quantity'];
}
$total['quantity'] += (int)$v['quantity'];
// ATTRIBUTES
$attribs = array();
if (!empty($item[1])) {
$attribs = unserialize(base64_decode($item[1]));
}
// ITEM D - product info from database
$itemD = PhocacartProduct::getProduct((int)$itemId, (int)$v['catid'], $this->type);
// Correct the tax rate - no tax calculation, no tax rate for each product
if (!empty($itemD) && $tax_calculation == 0) {
$itemD->taxrate = 0;
}
if (isset($itemD->id) && (int)$itemD->id > 0) {
$fullItems[$k]['title'] = $itemD->title;
$fullItems[$k]['catid'] = $itemD->catid;
$fullItems[$k]['alias'] = $itemD->alias;
$fullItems[$k]['sku'] = $itemD->sku;
$fullItems[$k]['image'] = $itemD->image;
$fullItems[$k]['type'] = $itemD->type;
$fullItems[$k]['default_price'] = $itemD->price;
$fullItems[$k]['price'] = $price->getPriceItem($itemD->price, $itemD->group_price, 0);
$fullItems[$k]['taxid'] = $itemD->taxid;
$fullItems[$k]['taxrate'] = $itemD->taxrate;
$fullItems[$k]['taxtitle'] = Text::_($itemD->taxtitle);
$fullItems[$k]['taxcountryid'] = $itemD->taxcountryid;
$fullItems[$k]['taxregionid'] = $itemD->taxregionid;
$fullItems[$k]['taxpluginid'] = $itemD->taxpluginid;
$taxKey = PhocacartTax::getTaxKey($itemD->taxid, $itemD->taxcountryid, $itemD->taxregionid, $itemD->taxpluginid);
$fullItems[$k]['taxkey'] = $taxKey;
$fullItems[$k]['taxcalctype'] = $itemD->taxcalculationtype;
$fullItems[$k]['weight'] = $itemD->weight;
$fullItems[$k]['volume'] = $itemD->volume;
$fullItems[$k]['stock'] = $itemD->stock;
$fullItems[$k]['stockadvanced'] = 0;
$fullItems[$k]['stockcalculation'] = $itemD->stock_calculation;
$fullItems[$k]['minqty'] = $itemD->min_quantity;
$fullItems[$k]['minmultipleqty'] = $itemD->min_multiple_quantity;
$fullItems[$k]['minqtycalculation'] = $itemD->min_quantity_calculation;
$fullItems[$k]['default_points_received'] = $itemD->points_received;
$pointsN = PhocacartReward::getPoints($itemD->points_needed, 'needed');
$pointsR = PhocacartReward::getPoints($itemD->points_received, 'received', $itemD->group_points_received);
$fullItems[$k]['points_needed'] = $pointsN;
$fullItems[$k]['points_received'] = $pointsR;
// Group
$fullItemsGroup[$itemId]['minqty'] = $itemD->min_quantity;
$fullItemsGroup[$itemId]['minmultipleqty'] = $itemD->min_multiple_quantity;
$fullItemsGroup[$itemId]['title'] = $itemD->title;
$fullItemsGroup[$itemId]['minqtyvalid'] = 1;
$fullItemsGroup[$itemId]['minmultipleqtyvalid'] = 1;
$priceI = $price->getPriceItems($itemD->price, $itemD->taxid, $itemD->taxrate, $itemD->taxcalculationtype, $itemD->taxtitle, 0, '', 0, 1, $itemD->group_price, $itemD->taxhide);
// Get price from advanced stock managment TO DO group price
if ($fullItems[$k]['stockcalculation'] == 3) {
$aA = PhocacartAttribute::sanitizeAttributeArray($attribs);
$price->getPriceItemsChangedByAttributes($priceI, $aA, $price, $itemD, 1);
}
$fullItems[$k]['netto'] = $priceI['netto'];
$fullItems[$k]['brutto'] = $priceI['brutto'];
$fullItems[$k]['tax'] = $priceI['tax'];
// Advanced Stock Calculation
if ($fullItems[$k]['stockcalculation'] == 2 || $fullItems[$k]['stockcalculation'] == 3) {
$fullItems[$k]['stockadvanced'] = PhocacartAttribute::getCombinationsStockByKey($k);
}
// Total
$total['netto'] += ($fullItems[$k]['netto'] * $fQ);
$total['brutto'] += ($fullItems[$k]['brutto'] * $fQ);
$total['weight'] += ($fullItems[$k]['weight'] * $fQ);
$total['volume'] += ($fullItems[$k]['volume'] * $fQ);
$total['length'] = $itemD->length > $total['length'] ? $itemD->length : $total['length'];
$total['width'] = $itemD->width > $total['width'] ? $itemD->width : $total['width'];
$total['height'] = $itemD->height > $total['height'] ? $itemD->height : $total['height'];
$total['points_needed'] += ($fullItems[$k]['points_needed'] * $fQ);
$total['points_received'] += ($fullItems[$k]['points_received'] * $fQ);
// TAX
if (!isset($total['tax'][$taxKey]['tax'])) {
$total['tax'][$taxKey]['tax'] = 0;// Define
}
if (!isset($total['tax'][$taxKey]['netto'])) {
$total['tax'][$taxKey]['netto'] = 0;// Define (set netto for each tax)
}
if (!isset($total['tax'][$taxKey]['brutto'])) {
$total['tax'][$taxKey]['brutto'] = 0;// Define
}
$total['tax'][$taxKey]['tax'] += ($fullItems[$k]['tax'] * $fQ);
$total['tax'][$taxKey]['netto'] += ($fullItems[$k]['netto'] * $fQ);
$total['tax'][$taxKey]['brutto'] += ($fullItems[$k]['brutto'] * $fQ);
$taxSuffix = '';
if ($itemD->taxcalculationtype == 1) {
$taxSuffix = ' (' . ($price->getTaxFormat($itemD->taxrate, $itemD->taxcalculationtype, 0)) . ')';
}
$total['tax'][$taxKey]['title'] = Text::_($itemD->taxtitle) . $taxSuffix;
$total['tax'][$taxKey]['title_lang'] = $itemD->taxtitle;
$total['tax'][$taxKey]['title_lang_suffix2'] = '(' . $taxSuffix . ')';
$total['tax'][$taxKey]['type'] = $itemD->taxcalculationtype;
$total['tax'][$taxKey]['rate'] = $itemD->taxrate;
$total['tax'][$taxKey]['taxid'] = $itemD->taxid;
$total['tax'][$taxKey]['taxhide'] = $itemD->taxhide;
// PRODUCTTYPE Digital product
$total['countallproducts']++;
if ($itemD->type == 0) {
$total['countphysicalproducts']++;
} else if ($itemD->type == 1) {
$total['countdigitalproducts']++;
} else if ($itemD->type == 2) {
// physical and digital
// This rule can be changed but for now e.g. we test if the product is digital to ensure that the shipping will be skipped
// if the product is both - digital and physical, we cannot skip shipping so we do not count it as digital
// Uncomment if you need to opposite rule
//$total['countphysicalproducts']++;
//$total['countdigitalproducts']++;
} else if ($itemD->type == 3) {
$total['countpriceondemandproducts']++;
} else if ($itemD->type == 4) {
// Gift Vouchers are even digital products
$total['countdigitalproducts']++;
}
// ==========
// ATTRIBUTES
// ==========
//
// Stock handling - one variant can be set in e.g. two products, so we need to count attributes stock:
if (!empty($attribs)) {
foreach ($attribs as $k2 => $v2) {
// Make array from all attributes even they are not multiple - to go through the foreach
if (!is_array($v2)) {
$v2 = array(0 => $v2);
}
if (!empty($v2)) {
// Be aware the k3 is not the key of attribute
// this is the k2
foreach ($v2 as $k3 => $v3) {
if ((int)$k2 > 0 && (int)$k3 > 0) {
$attrib = PhocacartAttribute::getAttributeValue((int)$k3, (int)$k2);
// Price is set as fixed with help of advanced stock management
if ($fullItems[$k]['stockcalculation'] != 3) {
/* if (!$attrib->aid) {
$app->enqueueMessage(Text::_('COM_PHOCACART_ERROR_PRODUCT_ATTRIBUTE_NOT_EXISTS_PLEASE_RECHECK_PRODUCTS_IN_YOUR_CART'), 'error');
break;
}*/
if (isset($attrib->title) && isset($attrib->amount) && isset($attrib->operator)) {
// If there is fixed VAT - don't change it in attributes - it is just fix - set taxtrate to 0
if ($itemD->taxcalculationtype == 2) {
$priceA = $price->getPriceItems($attrib->amount, $itemD->taxid, 0, $itemD->taxcalculationtype, $itemD->taxtitle, 0, '', 0, 1, null, $itemD->taxhide);
} else {
$priceA = $price->getPriceItems($attrib->amount, $itemD->taxid, $itemD->taxrate, $itemD->taxcalculationtype, $itemD->taxtitle, 0, '', 0, 1, null, $itemD->taxhide);
}
//$fQ = (int)$fullItems[$k]['quantity'];
// Price
if ($attrib->operator == '-') {
$nettoBefore = $fullItems[$k]['netto'];
$fullItems[$k]['netto'] -= $priceA['netto'];
if ($fullItems[$k]['netto'] < 0) {
$total['netto'] -= ($nettoBefore * $fQ);
$fullItems[$k]['netto'] = 0;
} else {
$total['netto'] -= ($priceA['netto'] * $fQ);
}
$bruttoBefore = $fullItems[$k]['brutto'];
$fullItems[$k]['brutto'] -= $priceA['brutto'];
if ($fullItems[$k]['brutto'] < 0 ) {
$total['brutto'] -= ($bruttoBefore * $fQ);
$fullItems[$k]['brutto'] = 0;
} else {
$total['brutto'] -= ($priceA['brutto'] * $fQ);
}
$taxBefore = $fullItems[$k]['tax'];
$fullItems[$k]['tax'] -= $priceA['tax'];
if ($fullItems[$k]['tax'] < 0 ) {
$total['tax'][$taxKey]['tax'] -= ($taxBefore * $fQ);
$total['tax'][$taxKey]['netto'] -= ($nettoBefore * $fQ);
$total['tax'][$taxKey]['brutto'] -= ($bruttoBefore * $fQ);
$fullItems[$k]['tax'] = 0;
} else {
$total['tax'][$taxKey]['tax'] -= ($priceA['tax'] * $fQ);
$total['tax'][$taxKey]['netto'] -= ($priceA['netto'] * $fQ);
$total['tax'][$taxKey]['brutto'] -= ($priceA['brutto'] * $fQ);
}
} else if ($attrib->operator == '+') {
$fullItems[$k]['brutto'] += $priceA['brutto'];// * multiply in render checkout
$fullItems[$k]['netto'] += $priceA['netto']; // * multiply in render checkout
$fullItems[$k]['tax'] += $priceA['tax']; // * multiply in render checkout
$total['netto'] += ($priceA['netto'] * $fQ);
$total['brutto'] += ($priceA['brutto'] * $fQ);
$total['tax'][$taxKey]['tax'] += ($priceA['tax'] * $fQ);
$total['tax'][$taxKey]['netto'] += ($priceA['netto'] * $fQ);
$total['tax'][$taxKey]['brutto'] += ($priceA['brutto'] * $fQ);
}
}
}
// Weight
if ($attrib->operator_weight == '-') {
$fullItems[$k]['weight'] -= $attrib->weight;
$fullItems[$k]['weight'] < 0 ? $fullItems[$k]['weight'] = 0 : $total['weight'] -= ($attrib->weight * $fullItems[$k]['quantity']);
} else if ($attrib->operator_weight == '+') {
$fullItems[$k]['weight'] += $attrib->weight;
$total['weight'] += ($attrib->weight * $fullItems[$k]['quantity']);
}
// Volume
if ($attrib->operator_volume == '-') {
$fullItems[$k]['volume'] -= $attrib->volume;
$fullItems[$k]['volume'] < 0 ? $fullItems[$k]['volume'] = 0 : $total['volume'] -= ($attrib->volume * $fullItems[$k]['quantity']);
} else if ($attrib->operator_volume == '+') {
$fullItems[$k]['volume'] += $attrib->volume;
$total['volume'] += ($attrib->volume * $fullItems[$k]['quantity']);
}
if (isset($optionsQuantity[$attrib->id])) {
$optionsQuantity[$attrib->id] += (int)$fQ;
} else {
$optionsQuantity[$attrib->id] = (int)$fQ;
}
// STOCK-1 ... we count each product variation separately
if ($fullItems[$k]['stockcalculation'] == 1 && (int)$optionsQuantity[$attrib->id] > (int)$attrib->stock) {
$total['stockvalid'] = 0;
$fullItems[$k]['stockvalid'] = 0;
$stock['valid'] = 0;
}
// Attribute values
$fullItems[$k]['attributes'][$attrib->aid][$k3]['aid'] = $attrib->aid; // Attribute Id
$fullItems[$k]['attributes'][$attrib->aid][$k3]['atitle'] = $attrib->atitle;
$fullItems[$k]['attributes'][$attrib->aid][$k3]['atype'] = $attrib->atype;
$fullItems[$k]['attributes'][$attrib->aid][$k3]['oid'] = $attrib->id;// Option Id
$fullItems[$k]['attributes'][$attrib->aid][$k3]['otitle'] = $attrib->title;
$fullItems[$k]['attributes'][$attrib->aid][$k3]['oimage'] = $attrib->image;
$fullItems[$k]['attributes'][$attrib->aid][$k3]['ovalue'] = PhocacartAttribute::setAttributeValue($attrib->atype, $v3, false, true, $attrib->type);
$fullItems[$k]['attributes'][$attrib->aid][$k3]['otype'] = $attrib->type;
//$fullItems[$k]['attributes'][$attrib->aid][$k3]['odownloadfile']= $attrib->download_file;
}
}
}
}
}
// ==============================
// MINIUM ORDER AMOUNT
// ==============================
// THERE CAN BE THREE METHODS HOW TO COUNT MINIMUM ORDER AMOUNT
// a) every product is unique (Product A - Option A, Product A - Option B are two different products)
// b) there are product groups (Product A- Option A, Product A - Option B is still one product - product A)
// c) advanced stock management - in this case it is the same like a)
if ($fullItems[$k]['minqtycalculation'] == 1 || $fullItems[$k]['minqtycalculation'] == 2) {
// a)
// MINIMUM QUANTITY - FOR ITEM - PRODUCT VARIATION - each product variation
if ((int)$fullItems[$k]['quantity'] < (int)$fullItems[$k]['minqty']) {
$minqty['valid'] = 0;
$fullItems[$k]['minqtyvalid'] = 0;
}
if ((int)$fullItems[$k]['minmultipleqty'] == 0) {
// Do not modulo by zero
// Set it back because we are in foreach
$minmultipleqty['valid'] = 1;
$fullItems[$k]['minmultipleqtyvalid'] = 1;
} else if (((int)$fullItems[$k]['quantity']) % (int)$fullItems[$k]['minmultipleqty'] != 0) {
$minmultipleqty['valid'] = 0;
$fullItems[$k]['minmultipleqtyvalid'] = 0;
}
} else {
// b)
// MINIMUM QUANTITY - FOR GROUP (Group is the same product but with different options values) - MAIN PRODUCT
if (empty($fullItemsGroup[$itemId]['minqty'])) {
$minqty['valid'] = 1;
$fullItemsGroup[$itemId]['minqtyvalid'] = 1;
} else if ((int)$fullItemsGroup[$itemId]['quantity'] < (int)$fullItemsGroup[$itemId]['minqty']) {
$minqty['valid'] = 0;
$fullItemsGroup[$itemId]['minqtyvalid'] = 0;
} else {
// Set it back because we are in foreach
$minqty['valid'] = 1;
$fullItemsGroup[$itemId]['minqtyvalid'] = 1;
}
// MINIMUM MULTIPLE QUANTITY
if (empty($fullItemsGroup[$itemId]['minmultipleqty'])) {
$minmultipleqty['valid'] = 1;
$fullItemsGroup[$itemId]['minmultipleqtyvalid'] = 1;
} else if ($fullItemsGroup[$itemId]['minmultipleqty'] == 0) {
// Do not modulo by zero
// Set it back because we are in foreach
$minmultipleqty['valid'] = 1;
$fullItemsGroup[$itemId]['minmultipleqtyvalid'] = 1;
} else if (((int)$fullItemsGroup[$itemId]['quantity']) % (int)$fullItemsGroup[$itemId]['minmultipleqty'] != 0) {
$minmultipleqty['valid'] = 0;
$fullItemsGroup[$itemId]['minmultipleqtyvalid'] = 0;
} else {
// Set it back because we are in foreach
$minmultipleqty['valid'] = 1;
$fullItemsGroup[$itemId]['minmultipleqtyvalid'] = 1;
}
}
// ==============================
// STOCK VALID
// ==============================
// The difference between STOCK-1, STOCK-0
// b) STOCK-0 - There is only one main product even it is divided into more product variations
// - so we count only main product - group
// a) STOCK-1 - Each product variation is one product but this means that product without any variation
// - is in one one of the product variation:
// Product 1 Option A - one product
// Product 1 Option B - one product
// Product 1 (no options) - one product - as sum there are 3 products
// c) STOCK-2 - advanced stock management
// Product 1 Option A - one product
// Product 1 Option B - one product
// Product 1 (no options) - one product - as sum there are 3 products
// Product 1 Option A + Option B - one product
// STOCK-2 ... we count main product as own product variation - only in case it does not have any attributes
// ... but combination of attributes can create different products
if (($fullItems[$k]['stockcalculation'] == 2 || $fullItems[$k]['stockcalculation'] == 3) && (int)$fullItems[$k]['quantity'] > (int)$fullItems[$k]['stockadvanced']) {
$stock['valid'] = 0;// Global - some of the product is out of stock
$fullItems[$k]['stockvalid'] = 0;// Current product is out of stock
}
// STOCK-1 ... we count main product as own product variation - only in case it does not have any attributes
// variations of product are checked in ohter place (cca line 271)
// THIS IS DIVEDED RULE - ONE HERE, SECOND ABOVE IN ATTRIBUTES FOREACH
if ($fullItems[$k]['stockcalculation'] == 1 && empty($fullItems[$k]['attributes']) && (int)$fullItems[$k]['quantity'] > (int)$fullItems[$k]['stock']) {
$stock['valid'] = 0;// Global - some of the product is out of stock
$fullItems[$k]['stockvalid'] = 0;// Current product is out of stock
}
// STOCK-0 ... we count main product as group: Product 1 Option A ... 5 + Product 1 Option B ... 5 = 10
if ($fullItems[$k]['stockcalculation'] == 0 && (int)$fullItemsGroup[$itemId]['quantity'] > (int)$fullItems[$k]['stock']) {
$stock['valid'] = 0;// Global - some of the product is out of stock
$fullItems[$k]['stockvalid'] = 0;// Current product is out of stock
}
$fullItems[$k]['final'] = $fullItems[$k]['netto'] && !$this->posbruttocalculation ? $fullItems[$k]['netto'] * $fQ : $fullItems[$k]['brutto'] * $fQ;
}
if ($this->correctsubtotal) {
$this->correctSubTotal($fullItems[$k], $total);
}
}
}
// ================
// REWARD POINTS
// ================
public function calculateRewardDiscounts(&$fullItems, &$fullItemsGroup, &$total, $rewardCart) {
$reward = new PhocacartReward();
$rewards = array();
$rewards['used'] = $reward->checkReward((int)$rewardCart['used']);
$rewards['usedtotal'] = 0;
foreach ($fullItems as $k => $v) {
if (isset($v['points_needed']) && (int)$v['points_needed'] > 0) {
$rewards['needed'] = $v['quantity'] * $v['points_needed'];
$reward->calculatedRewardDiscountProduct($rewards);
if (isset($rewards['percentage']) && $rewards['percentage'] > 0) {
$fullItems[$k]['rewardproduct'] = 1;
$fullItems[$k]['rewardproductpoints'] = $rewards['usedproduct'];
$fullItems[$k]['rewardproducttitle'] = Text::_('COM_PHOCACART_REWARD_POINTS');
PhocacartCalculation::calculateDiscountPercentage($rewards['percentage'], $v['quantity'], $fullItems[$k], $total, $v['taxkey']);
$fullItems[$k]['rewardproducttxtsuffix'] = ' (' . $rewards['usedproduct'] . ')';
$total['rewardproducttxtsuffix'] = ' (' . $rewards['usedtotal'] . ')';
$total['rewardproductusedtotal'] = $rewards['usedtotal'];
PhocacartCalculation::correctItemsIfNull($fullItems[$k]);
PhocacartCalculation::correctTotalIfNull($total, $v['taxkey']);
}
$fullItems[$k]['final'] = $fullItems[$k]['netto'] && !$this->posbruttocalculation ? $fullItems[$k]['netto'] * $v['quantity'] : $fullItems[$k]['brutto'] * $v['quantity'];
if (isset($fullItems[$k]['nettodiscount']) && !$this->posbruttocalculation) {
$fullItems[$k]['finaldiscount'] = $fullItems[$k]['nettodiscount'] * $v['quantity'];
} else if (isset($fullItems[$k]['bruttodiscount'])) {
$fullItems[$k]['finaldiscount'] = $fullItems[$k]['bruttodiscount'] * $v['quantity'];
}
if ($this->correctsubtotal) {
$this->correctSubTotal($fullItems[$k], $total);
}
}
}
}
// ================
// PRODUCT DISCOUNT
// ================
public function calculateProductDiscounts(&$fullItems, &$fullItemsGroup, &$total) {
foreach ($fullItems as $k => $v) {
// Get quantity of a group. Group is sum of all product variations
// - explained in PhocacartDiscountProduct::getProductDiscount
$groupId = $v['id'];
$v['quantitygroup'] = $v['quantity'];// define
if (isset($fullItemsGroup[$groupId]['quantity'])) {
$v['quantitygroup'] = $fullItemsGroup[$groupId]['quantity'];
}
$discount = PhocacartDiscountProduct::getProductDiscount($v['id'], $v['quantitygroup'], $v['quantity']);
if (isset($discount['discount']) && isset($discount['calculation_type'])) {
$fullItems[$k]['discountproduct'] = 1;
$fullItems[$k]['discountproducttitle'] = $discount['title'];
if ($discount['calculation_type'] == 0) {
// FIXED AMOUNT
if (isset($v['netto']) && $v['netto'] > 0) {
//$r = $discount['discount'] / $v['quantity'] * 100 / $v['netto'];// Ratio to use it for brutto and tax
// PRODUCT DISCOUNT - DON'T DIVIDE IT INTO QUANTITY
//if you set 500 fixed amount as discount - it applies to each quantity
$r = $discount['discount'] * 100 / $v['netto'];
} else {
$r = 0;
}
PhocacartCalculation::calculateDiscountFixedAmount($r, $v['quantity'], $fullItems[$k], $total, $v['taxkey']);
} else {
// PERCENTAGE
PhocacartCalculation::calculateDiscountPercentage($discount['discount'], $v['quantity'], $fullItems[$k], $total, $v['taxkey']);
}
PhocacartCalculation::correctItemsIfNull($fullItems[$k]);
PhocacartCalculation::correctTotalIfNull($total, $v['taxkey']);
}
$fullItems[$k]['final'] = $fullItems[$k]['netto'] && !$this->posbruttocalculation ? $fullItems[$k]['netto'] * $v['quantity'] : $fullItems[$k]['brutto'] * $v['quantity'];
if (isset($fullItems[$k]['nettodiscount']) && !$this->posbruttocalculation) {
$fullItems[$k]['finaldiscount'] = $fullItems[$k]['nettodiscount'] * $v['quantity'];
} else if (isset($fullItems[$k]['bruttodiscount'])) {
$fullItems[$k]['finaldiscount'] = $fullItems[$k]['bruttodiscount'] * $v['quantity'];
}
if ($this->correctsubtotal) {
$this->correctSubTotal($fullItems[$k], $total);
}
}
}
// =============
// CART DISCOUNT
// =============
public function calculateCartDiscounts(&$fullItems, &$fullItemsGroup, &$total, &$cartDiscount) {
// If there are more cart discounts e.g. separated by different rules
// remove the suffix because it will be not valid
$discountSuffixItems = array();
foreach ($fullItems as $k => $v) {
$discount = PhocacartDiscountCart::getCartDiscount($v['id'], $v['catid'], $total['quantity'], $total['netto'], $total['subtotalnetto']);
// First check if there is even some discount
if ($discount) {
$discountId = $discount['id'];
$discountAmount = $discount['discount'];
if ($discount['free_shipping'] == 1) {
$total['free_shipping'] = $discount['free_shipping'];
}
if ($discount['free_payment'] == 1) {
$total['free_payment'] = $discount['free_payment'];
}
if (isset($discount['discount']) && isset($discount['calculation_type'])) {
$fullItems[$k]['discountcart'] = 1;
$fullItems[$k]['discountcarttitle'] = $discount['title'];
$fullItems[$k]['discountcartid'] = $discount['id'];
if ($discount['calculation_type'] == 0) {
// FIXED AMOUNT
// We need to divide fixed discount amount to products which meet the discount ID rule
// There can be two products in the cart and each can meet other discount rules
if (isset($total['discountcartfixedamount'][$discountId]['quantity'])) {
$total['discountcartfixedamount'][$discountId]['quantity'] += $v['quantity'];
} else {
$total['discountcartfixedamount'][$discountId]['quantity'] = $v['quantity'];
}
if (isset($total['discountcartfixedamount'][$discountId]['netto'])) {
$total['discountcartfixedamount'][$discountId]['netto'] += $v['netto'] * $v['quantity'];
} else {
$total['discountcartfixedamount'][$discountId]['netto'] = $v['netto'] * $v['quantity'];
}
$total['discountcartfixedamount'][$discountId]['discount'] = $discountAmount;
$fullItems[$k]['discountcartfixedid'] = $discountId;
} else {
// PERCENTAGE
PhocacartCalculation::calculateDiscountPercentage($discount['discount'], $v['quantity'], $fullItems[$k], $total, $v['taxkey']);
$discountSuffixItems[$discount['discount']] = $discount['discount'];
if (count($discountSuffixItems) > 1) {
// There are different types of discounts, remove the suffix Cart discount (10%) become Cart discount. Because if there is
// e.g. 5% and 10% then the 10% in () will be misleading
$total['discountcarttxtsuffix'] = '';
} else {
$price = new PhocacartPrice();
$total['discountcarttxtsuffix'] = ' (' . $price->cleanPrice($discount['discount']) . ' %)';
}
}
$cartDiscount['id'] = $discount['id'];
$cartDiscount['title'] = $discount['title'];
}
}
$fullItems[$k]['final'] = $fullItems[$k]['netto'] && !$this->posbruttocalculation ? $fullItems[$k]['netto'] * $v['quantity'] : $fullItems[$k]['brutto'] * $v['quantity'];
/*
* Must be done in recalculateCartDiscounts()
*
if (isset($fullItems[$k]['nettodiscount']) && !$this->posbruttocalculation) {
$fullItems[$k]['finaldiscount'] = $fullItems[$k]['nettodiscount'] * $v['quantity'];
} else if (isset($fullItems[$k]['bruttodiscount'])) {
$fullItems[$k]['finaldiscount'] = $fullItems[$k]['bruttodiscount'] * $v['quantity'];
}*/
if ($this->correctsubtotal) {
//$this->correctSubTotal($fullItems[$k], $total);
}
}
}
/*
* Used only for fixed amount of discount
*/
public function recalculateCartDiscounts(&$fullItems, &$fullItemsGroup, &$total) {
foreach ($fullItems as $k => $v) {
// Fixed amount - we need to recalculate
$dF = $v['discountcartfixedid'];
if (isset($total['discountcartfixedamount'][$dF]['discount']) && isset($total['discountcartfixedamount'][$dF]['netto'])) {
$dPRel = 0;
if ($total['discountcartfixedamount'][$dF]['netto'] > 0) {
$dPRel = $total['discountcartfixedamount'][$dF]['discount'] / $total['discountcartfixedamount'][$dF]['netto'];
}
// CART DISCOUNT - DIVIDE IT INTO QUANTITY
$dPFix = $dPRel * $v['netto'] * $v['quantity'];
if ($v['netto'] > 0) {
$r = $dPFix * 100 / $v['netto'] / $v['quantity'];// Ratio to use it for brutto and tax but you need to divide it into qunatity
} else {
$r = 0;
}
PhocacartCalculation::calculateDiscountFixedAmount($r, $v['quantity'], $fullItems[$k], $total, $v['taxkey']);
PhocacartCalculation::correctItemsIfNull($fullItems[$k]);
PhocacartCalculation::correctTotalIfNull($total, $v['taxkey']);
}
$fullItems[$k]['final'] = $fullItems[$k]['netto'] && !$this->posbruttocalculation ? $fullItems[$k]['netto'] * $v['quantity'] : $fullItems[$k]['brutto'] * $v['quantity'];
if (isset($fullItems[$k]['nettodiscount']) && !$this->posbruttocalculation) {
$fullItems[$k]['finaldiscount'] = $fullItems[$k]['nettodiscount'] * $v['quantity'];
} else if (isset($fullItems[$k]['bruttodiscount'])) {
$fullItems[$k]['finaldiscount'] = $fullItems[$k]['bruttodiscount'] * $v['quantity'];
}
if ($this->correctsubtotal) {
$this->correctSubTotal($fullItems[$k], $total);
}
}
}
// ============
// CART COUPON
// ============
/*
* $coupon ... coupon ID and Title set in checkout
* $couponO ... coupon object
* $couponDb ... all the information from coupon
* $validCoupon ... is or isn't valid TRUE/FALSE
*/
public function calculateCartCoupons(&$fullItems, &$fullItemsGroup, &$total, &$coupon) {
$couponO = new PhocacartCoupon();
$couponO->setType($this->type);
$couponO->setCoupon($coupon['id']);
$couponDb = $couponO->getCoupon();
foreach ($fullItems as $k => $v) {
$validCoupon = $couponO->checkCoupon(0, $v['id'], $v['catid'], $total['quantity'], $total['netto'], $total['subtotalnetto']);
if ($validCoupon) {
$validCouponId = $couponDb['id'];
$validCouponAmount = $couponDb['discount'];
if ($couponDb['free_shipping'] == 1) {
$total['free_shipping'] = $couponDb['free_shipping'];
}
if ($couponDb['free_payment'] == 1) {
$total['free_payment'] = $couponDb['free_payment'];
}
if (isset($couponDb['discount']) && isset($couponDb['calculation_type'])) {
$fullItems[$k]['couponcart'] = 1;
$fullItems[$k]['couponcarttitle'] = $couponDb['title'];
$fullItems[$k]['couponcartid'] = $couponDb['id'];
if ($couponDb['calculation_type'] == 0) {
// FIXED AMOUNT
// We need to divide fixed couponDb amount to products which meet the couponDb ID rule
// There can be two products in the cart and each can meet other couponDb rules
if (isset($total['couponcartfixedamount'][$validCouponId]['quantity'])) {
$total['couponcartfixedamount'][$validCouponId]['quantity'] += $v['quantity'];
} else {
$total['couponcartfixedamount'][$validCouponId]['quantity'] = $v['quantity'];
}
if (isset($total['couponcartfixedamount'][$validCouponId]['netto'])) {
$total['couponcartfixedamount'][$validCouponId]['netto'] += $v['netto'] * $v['quantity'];
} else {
$total['couponcartfixedamount'][$validCouponId]['netto'] = $v['netto'] * $v['quantity'];
}
$total['couponcartfixedamount'][$validCouponId]['discount'] = $validCouponAmount;
$fullItems[$k]['couponcartfixedid'] = $validCouponId;
} else {
// PERCENTAGE
PhocacartCalculation::calculateDiscountPercentage($couponDb['discount'], $v['quantity'], $fullItems[$k], $total, $v['taxkey']);
PhocacartCalculation::correctItemsIfNull($fullItems[$k]);
PhocacartCalculation::correctTotalIfNull($total, $v['taxkey']);
$price = new PhocacartPrice();
$total['couponcarttxtsuffix'] = ' (' . $price->cleanPrice($couponDb['discount']) . ' %)';
}
}
}
// !!! VALID COUPON
// In case the coupon is valid at least for one product or one category it is then valid
// and will be divided into valid products/categories
// As global we mark it as valid - so change the valid coupon variable only in case it is valid
if ($validCoupon == 1) {
$coupon['valid'] = $validCoupon;
}
$fullItems[$k]['final'] = $fullItems[$k]['netto'] && !$this->posbruttocalculation ? $fullItems[$k]['netto'] * $v['quantity'] : $fullItems[$k]['brutto'] * $v['quantity'];
/*
*
* Must be done in recalculateCartCoupons for fixed coupons
*/
if (isset($couponDb['calculation_type']) && $couponDb['calculation_type'] != 0) {
if (isset($fullItems[$k]['nettodiscount']) && !$this->posbruttocalculation) {
$fullItems[$k]['finaldiscount'] = $fullItems[$k]['nettodiscount'] * $v['quantity'];
} else if (isset($fullItems[$k]['bruttodiscount'])) {
$fullItems[$k]['finaldiscount'] = $fullItems[$k]['bruttodiscount'] * $v['quantity'];
}
}
if ($this->correctsubtotal) {
$this->correctSubTotal($fullItems[$k], $total);
}
}
}
/*
* Used only for fixed amount of coupon
*/
public function recalculateCartCoupons(&$fullItems, &$fullItemsGroup, &$total) {
foreach ($fullItems as $k => $v) {
$dF = $v['couponcartfixedid'];
if (isset($total['couponcartfixedamount'][$dF]['discount']) && isset($total['couponcartfixedamount'][$dF]['netto'])) {
$dPRel = 0;
if ($total['couponcartfixedamount'][$dF]['netto'] > 0) {
$dPRel = $total['couponcartfixedamount'][$dF]['discount'] / $total['couponcartfixedamount'][$dF]['netto'];
}
// CART DISCOUNT - DIVIDE IT INTO QUANTITY
$dPFix = $dPRel * $v['netto'] * $v['quantity'];
if ($v['netto'] > 0) {
$r = $dPFix * 100 / $v['netto'] / $v['quantity'];// Ratio to use it for brutto and tax but you need to divide it into qunatity
} else {
$r = 0;
}
PhocacartCalculation::calculateDiscountFixedAmount($r, $v['quantity'], $fullItems[$k], $total, $v['taxkey']);
PhocacartCalculation::correctItemsIfNull($fullItems[$k]);
PhocacartCalculation::correctTotalIfNull($total, $v['taxkey']);
}
$fullItems[$k]['final'] = $fullItems[$k]['netto'] && !$this->posbruttocalculation ? $fullItems[$k]['netto'] * $v['quantity'] : $fullItems[$k]['brutto'] * $v['quantity'];
if (isset($fullItems[$k]['nettodiscount']) && !$this->posbruttocalculation) {
$fullItems[$k]['finaldiscount'] = $fullItems[$k]['nettodiscount'] * $v['quantity'];
} else if (isset($fullItems[$k]['bruttodiscount'])) {
$fullItems[$k]['finaldiscount'] = $fullItems[$k]['bruttodiscount'] * $v['quantity'];
}
if ($this->correctsubtotal) {
$this->correctSubTotal($fullItems[$k], $total);
}
}
}