-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCashier.php
141 lines (114 loc) · 3.78 KB
/
Cashier.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
<?php
/**
* @author enea dhack <[email protected]>
*/
declare(strict_types=1);
namespace Vaened\PriceEngine;
use BackedEnum;
use Brick\Money\Money;
use UnitEnum;
use Vaened\PriceEngine\Adjustments\{Adjustments, Charge, Discount};
use Vaened\PriceEngine\Adjustments\Taxation\{TaxStripper, Taxes};
use Vaened\PriceEngine\Money\{Amount};
use function dd;
abstract class Cashier implements Summary
{
protected AdjustmentManager $discounts;
protected AdjustmentManager $charges;
private AdjustmentManager $taxes;
private Price $price;
private UnitRate $unitRate;
public function __construct(
Amount $amount,
private int $quantity,
Taxes $taxes = new Taxes([]),
Adjustments $charges = new Adjustments([]),
Adjustments $discounts = new Adjustments([]),
)
{
$allTaxes = $taxes->merge($amount->taxes())
->only($amount->applicableCodes());
$applicableTaxes = $allTaxes->toAdjustments();
$this->initializePrice(
netUnitPrice: TaxStripper::for($allTaxes)->clean($amount->value()),
taxes : $applicableTaxes
);
$this->initializeMoneyAdjustments($discounts, $charges, $applicableTaxes);
}
abstract protected function createUnitRate(Price $price): UnitRate;
public function update(int $quantity): void
{
$this->quantity = $quantity;
$this->discounts->update($quantity);
$this->charges->update($quantity);
$this->taxes->update($quantity);
}
public function apply(Discount ...$discounts): void
{
$this->discounts->add($discounts);
}
public function cancelDiscount(BackedEnum|UnitEnum|string $discountCode): void
{
$this->discounts->remove($discountCode);
}
public function discounts(): Modifiers
{
return $this->discounts->modifiers();
}
public function add(Charge ...$charges): void
{
$this->charges->add($charges);
}
public function revertCharge(BackedEnum|UnitEnum|string $chargeCode): void
{
$this->charges->remove($chargeCode);
}
public function charges(): Modifiers
{
return $this->charges->modifiers();
}
public function taxes(): Modifiers
{
return $this->taxes->modifiers();
}
public function quantity(): int
{
return $this->quantity;
}
public function priceBreakdown(): UnitRate
{
return $this->unitRate;
}
public function unitPrice(): Price
{
return $this->price;
}
public function subtotal(): Price
{
return $this->unitPrice()->multipliedBy($this->quantity());
}
public function total(): Money
{
return $this->subtotal()
->net()
->plus($this->taxes()->total())
->plus($this->charges()->total())
->minus($this->discounts()->total());
}
protected function initializePrice(Money $netUnitPrice, Adjustments $taxes): void
{
$this->price = new Price(
$netUnitPrice,
grossUnitPrice: $netUnitPrice->plus(
AdjustmentManager::totalize($netUnitPrice, $taxes)
),
);
}
protected function initializeMoneyAdjustments(Adjustments $discounts, Adjustments $charges, Adjustments $taxes): void
{
$this->unitRate = $this->createUnitRate($this->price);
$this->discounts = new AdjustmentManager($discounts, $this->unitRate->discountable(), $this->quantity);
$this->charges = new AdjustmentManager($charges, $this->unitRate->chargeable(), $this->quantity);
$this->taxes = new AdjustmentManager($taxes, $this->unitRate->taxable(), $this->quantity);
}
}