-
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathProduct.php
More file actions
713 lines (584 loc) · 17.5 KB
/
Product.php
File metadata and controls
713 lines (584 loc) · 17.5 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
<?php
declare(strict_types=1);
namespace Extcode\Cart\Domain\Model\Cart;
/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
class Product
{
protected ?Cart $cart = null;
protected ?float $specialPrice = null;
protected array $quantityDiscounts = [];
protected float $gross;
protected float $net;
protected float $tax;
protected string $error;
protected bool $isVirtualProduct = false;
protected ?float $serviceAttribute1 = null;
protected ?float $serviceAttribute2 = null;
protected ?float $serviceAttribute3 = null;
protected array $beVariants = [];
protected ?FeVariant $feVariant = null;
protected array $additional = [];
protected int $minNumberInCart = 0;
protected int $maxNumberInCart = 0;
/**
* Number of products in stock.
*/
protected int $stock = 0;
/**
* Is the stock management active for this product?
*/
protected bool $handleStock = false;
/**
* Is the stock management for this product covered in the backend variants?
*/
protected bool $handleStockInVariants = false;
protected DetailPageLink $detailPageLink;
public function __construct(
protected string $productType,
protected int $productId,
protected string $sku,
protected string $title,
protected float $price,
protected TaxClass $taxClass,
protected int $quantity,
protected bool $isNetPrice = false,
FeVariant $feVariant = null
) {
if ($feVariant) {
$this->feVariant = $feVariant;
}
$this->calcGross();
$this->calcTax();
$this->calcNet();
}
public function setCart(Cart $cart): void
{
$this->cart = $cart;
}
public function getSku(): string
{
return $this->sku;
}
public function setSku(string $sku): void
{
$this->sku = $sku;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): void
{
$this->title = $title;
}
public function isNetPrice(): bool
{
return $this->isNetPrice;
}
public function addBeVariants(array $newVariants): void
{
foreach ($newVariants as $newVariant) {
$this->addBeVariant($newVariant);
}
}
public function addBeVariant(BeVariant $newVariant): void
{
$newVariantsId = $newVariant->getId();
if (!empty($this->beVariants) && array_key_exists($newVariantsId, $this->beVariants)) {
$variant = $this->beVariants[$newVariantsId];
if ($variant->getBeVariants()) {
$variant->addBeVariants($newVariant->getBeVariants());
} else {
$newQuantity = $variant->getQuantity() + $newVariant->getQuantity();
$variant->setQuantity($newQuantity);
}
} else {
$newVariant->setProduct($this);
$this->beVariants[$newVariantsId] = $newVariant;
}
$this->reCalc();
}
public function changeVariantsQuantity(array $variantQuantity): void
{
foreach ($variantQuantity as $variantId => $quantity) {
/** @var BeVariant $variant */
$variant = $this->beVariants[$variantId];
if (ctype_digit((string)$quantity)) {
$quantity = (int)$quantity;
$variant->changeQuantity($quantity);
} elseif (is_array($quantity)) {
$variant->changeVariantsQuantity($quantity);
}
$this->reCalc();
}
}
public function getFeVariant(): ?FeVariant
{
return $this->feVariant;
}
public function getBeVariants(): array
{
return $this->beVariants;
}
public function getBeVariantById(string $variantId): ?BeVariant
{
return $this->beVariants[$variantId] ?? null;
}
public function removeBeVariants(array $variantsArray): int
{
foreach ($variantsArray as $variantId => $value) {
/** @var BeVariant $variant */
$variant = $this->beVariants[$variantId];
if ($variant) {
if (is_array($value)) {
$variant->removeBeVariants($value);
if (!$variant->getBeVariants()) {
unset($this->beVariants[$variantId]);
}
$this->reCalc();
} else {
unset($this->beVariants[$variantId]);
$this->reCalc();
}
} else {
return -1;
}
}
return 1;
}
public function removeVariantById(string $variantId): void
{
unset($this->beVariants[$variantId]);
$this->reCalc();
}
public function removeVariant(string $variantId): void
{
$this->removeVariantById($variantId);
}
public function changeVariantById(string $variantId, int $newQuantity): void
{
$this->beVariants[$variantId]->changeQuantity($newQuantity);
$this->reCalc();
}
public function getProductType(): string
{
return $this->productType;
}
public function getProductId(): int
{
return $this->productId;
}
public function getId(): string
{
return $this->getTableProductId();
}
protected function getTableProductId(): string
{
$tableProductId = $this->getProductType() . '_' . $this->getProductId();
if ($this->getFeVariant()) {
$tableProductId .= '_' . $this->getFeVariant()->getId();
}
return $tableProductId;
}
public function getPrice(): float
{
return $this->price;
}
public function getTranslatedPrice(): float
{
if ($this->cart) {
return $this->cart->translatePrice($this->getPrice());
}
return $this->getPrice();
}
public function getSpecialPrice(): ?float
{
return $this->specialPrice;
}
public function getTranslatedSpecialPrice(): ?float
{
if ($this->cart) {
return $this->cart->translatePrice($this->getSpecialPrice());
}
return $this->getSpecialPrice();
}
public function setSpecialPrice(float $specialPrice): void
{
$this->specialPrice = $specialPrice;
}
public function getQuantityDiscounts(): array
{
return $this->quantityDiscounts;
}
public function getQuantityDiscountPrice(int $quantity = null): float
{
$price = $this->getTranslatedPrice();
$quantity = $quantity ?: $this->getQuantity();
if ($this->getQuantityDiscounts()) {
foreach ($this->getQuantityDiscounts() as $quantityDiscount) {
if (($quantityDiscount['quantity'] <= $quantity) && ($quantityDiscount['price'] < $price)) {
$price = $quantityDiscount['price'];
}
}
}
return $price;
}
public function setQuantityDiscounts(array $quantityDiscounts): void
{
$this->quantityDiscounts = $quantityDiscounts;
}
/**
* Returns Best Price (min of Price and Special Price)
*/
public function getBestPrice(int $quantity = null): ?float
{
$bestPrice = $this->getQuantityDiscountPrice($quantity);
$translatedSpecialPrice = $this->getTranslatedSpecialPrice();
if ($translatedSpecialPrice !== null && $translatedSpecialPrice < $bestPrice) {
$bestPrice = $translatedSpecialPrice;
}
return $bestPrice;
}
/**
* Returns Best Price Discount
*/
public function getDiscount(): float
{
$discount = $this->getTranslatedPrice() - $this->getBestPrice();
return $discount;
}
/**
* Returns Special Price
*/
public function getSpecialPriceDiscount(): float
{
$discount = 0.0;
if (($this->getTranslatedPrice() != 0.0) && ($this->getTranslatedSpecialPrice())) {
$discount = (($this->getTranslatedPrice() - $this->getTranslatedSpecialPrice()) / $this->getTranslatedPrice()) * 100;
}
return $discount;
}
public function getPriceTax(): float
{
return ($this->getBestPrice() / (1 + $this->taxClass->getCalc())) * ($this->taxClass->getCalc());
}
public function getTaxClass(): TaxClass
{
return $this->taxClass;
}
public function setTaxClass(TaxClass $taxClass): void
{
$this->taxClass = $taxClass;
}
public function changeQuantity(int $newQuantity): void
{
if ($this->quantity != $newQuantity) {
if ($newQuantity === 0) {
$this->cart->removeProduct($this);
} else {
$this->quantity = $newQuantity;
$this->reCalc();
}
}
}
public function changeQuantities(array $newQuantities): void
{
foreach ($newQuantities as $newQuantityKey => $newQuantityValue) {
$newQuantityKey = (string)$newQuantityKey;
$newQuantityValue = (int)$newQuantityValue;
if ($newQuantityValue === 0) {
$this->removeVariantById((string)$newQuantityKey);
} else {
$this->getBeVariantById((string)$newQuantityKey)->setQuantity($newQuantityValue);
$this->reCalc();
}
}
}
public function getQuantity(): int
{
return $this->quantity;
}
public function isQuantityInRange(): bool
{
return !$this->isQuantityBelowRange() && !$this->isQuantityAboveRange();
}
/**
* returns 0 if quantity is in rage
* returns -1 if minimum was not reached
* returns 1 if maximum was exceeded
*
* @deprecated
*/
public function getQuantityIsLeavingRange(): int
{
if ($this->getQuantityBelowRange()) {
return -1;
}
if ($this->getQuantityAboveRange()) {
return 1;
}
return 0;
}
/**
* @deprecated
*/
public function getQuantityBelowRange(): bool
{
return $this->isQuantityBelowRange();
}
public function isQuantityBelowRange(): bool
{
return $this->quantity < $this->minNumberInCart;
}
/**
* @deprecated
*/
public function getQuantityAboveRange(): bool
{
return $this->isQuantityAboveRange();
}
public function isQuantityAboveRange(): bool
{
return ($this->maxNumberInCart > 0) && ($this->quantity > $this->maxNumberInCart);
}
public function getGross(): float
{
$gross = 0.0;
if ($this->isQuantityInRange()) {
$this->calcGross();
$gross = $this->gross;
}
return $gross;
}
public function getNet(): float
{
$net = 0.0;
if ($this->isQuantityInRange()) {
$this->calcNet();
$net = $this->net;
}
return $net;
}
public function getTax(): float
{
$tax = 0.0;
if ($this->isQuantityInRange()) {
$this->calcTax();
$tax = $this->tax;
}
return $tax;
}
/**
* @return mixed
*/
public function getError()
{
return $this->error;
}
public function isVirtualProduct(): bool
{
return $this->isVirtualProduct;
}
public function setIsVirtualProduct(bool $isVirtualProduct): void
{
$this->isVirtualProduct = $isVirtualProduct;
}
public function getServiceAttribute1(): ?float
{
return $this->serviceAttribute1;
}
public function setServiceAttribute1(float $serviceAttribute1): void
{
$this->serviceAttribute1 = $serviceAttribute1;
}
public function getServiceAttribute2(): ?float
{
return $this->serviceAttribute2;
}
public function setServiceAttribute2(float $serviceAttribute2): void
{
$this->serviceAttribute2 = $serviceAttribute2;
}
public function getServiceAttribute3(): ?float
{
return $this->serviceAttribute3;
}
public function setServiceAttribute3(float $serviceAttribute3): void
{
$this->serviceAttribute3 = $serviceAttribute3;
}
public function toArray(): array
{
$productArr = [
'productType' => $this->productType,
'productId' => $this->productId,
'id' => $this->getId(),
'sku' => $this->sku,
'title' => $this->title,
'price' => $this->price,
'specialPrice' => $this->specialPrice,
'quantityDiscounts' => $this->quantityDiscounts,
'taxClass' => $this->taxClass,
'quantity' => $this->quantity,
'price_total' => $this->gross,
'price_total_gross' => $this->gross,
'price_total_net' => $this->net,
'tax' => $this->tax,
'additional' => $this->additional,
];
if ($this->beVariants) {
$variantArr = [];
foreach ($this->beVariants as $variant) {
$variantArr[] = [$variant->getId() => $variant->toArray()];
}
$productArr[] = ['variants' => $variantArr];
}
return $productArr;
}
public function toJson(): string
{
return json_encode($this->toArray());
}
protected function calcGross(): void
{
if ($this->isNetPrice == false) {
if ($this->beVariants) {
$sum = 0.0;
foreach ($this->beVariants as $variant) {
/** @var BeVariant $variant */
$sum += $variant->getGross();
}
$this->gross = $sum;
} else {
$this->gross = $this->getBestPrice() * $this->quantity;
}
} else {
$this->calcNet();
$this->calcTax();
$this->gross = $this->net + $this->tax;
}
}
protected function calcTax(): void
{
if ($this->isNetPrice == false) {
$this->tax = ($this->gross / (1 + $this->getTaxClass()->getCalc())) * ($this->getTaxClass()->getCalc());
} else {
$this->tax = ($this->net * $this->getTaxClass()->getCalc());
}
}
protected function calcNet(): void
{
if ($this->isNetPrice == true) {
if ($this->beVariants) {
$sum = 0.0;
foreach ($this->beVariants as $variant) {
/** @var BeVariant $variant */
$sum += $variant->getNet();
}
$this->net = $sum;
} else {
$this->net = $this->getBestPrice() * $this->quantity;
}
} else {
$this->calcGross();
$this->calcTax();
$this->net = $this->gross - $this->tax;
}
}
protected function reCalc(): void
{
if ($this->beVariants) {
$quantity = 0;
foreach ($this->beVariants as $variant) {
/** @var BeVariant $variant */
$quantity += $variant->getQuantity();
}
if ($this->quantity != $quantity) {
$this->quantity = $quantity;
}
}
$this->calcGross();
$this->calcTax();
$this->calcNet();
}
public function getAdditionalArray(): array
{
return $this->additional;
}
public function setAdditionalArray(array $additional): void
{
$this->additional = $additional;
}
/**
* @return mixed
*/
public function getAdditional(string $key)
{
return $this->additional[$key];
}
public function setAdditional(string $key, mixed $value): void
{
$this->additional[$key] = $value;
}
public function getMinNumberInCart(): int
{
return $this->minNumberInCart;
}
public function setMinNumberInCart(int $minNumberInCart): void
{
if ($minNumberInCart < 0 ||
($this->maxNumberInCart > 0 && $minNumberInCart > $this->maxNumberInCart)
) {
throw new \InvalidArgumentException();
}
$this->minNumberInCart = $minNumberInCart;
}
public function getMaxNumberInCart(): int
{
return $this->maxNumberInCart;
}
public function setMaxNumberInCart(int $maxNumberInCart): void
{
if ($maxNumberInCart < 0 || $maxNumberInCart < $this->minNumberInCart) {
throw new \InvalidArgumentException();
}
$this->maxNumberInCart = $maxNumberInCart;
}
public function getStock(): int
{
return $this->stock;
}
public function setStock(int $stock): void
{
$this->stock = $stock;
}
public function isHandleStock(): bool
{
return $this->handleStock;
}
public function setHandleStock(bool $handleStock): void
{
$this->handleStock = $handleStock;
}
public function isHandleStockInVariants(): bool
{
return $this->handleStockInVariants;
}
public function setHandleStockInVariants(bool $handleStockInVariants): void
{
$this->handleStockInVariants = $handleStockInVariants;
}
public function getDetailPageLink(): DetailPageLink
{
return $this->detailPageLink;
}
public function setDetailPageLink(DetailPageLink $detailPageLink): void
{
$this->detailPageLink = $detailPageLink;
}
}