|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Service; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Model\AbstractProduct; |
|
8
|
|
|
use Application\Model\Order; |
|
9
|
|
|
use Application\Model\OrderLine; |
|
10
|
|
|
use Application\Model\Product; |
|
11
|
|
|
use Application\Model\Subscription; |
|
12
|
|
|
use Application\Model\User; |
|
13
|
|
|
use Application\Repository\UserRepository; |
|
14
|
|
|
use Doctrine\ORM\EntityManager; |
|
15
|
|
|
use Ecodev\Felix\Api\Exception; |
|
16
|
|
|
use Money\Money; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Service to create order and transactions for products and their quantity |
|
20
|
|
|
*/ |
|
21
|
|
|
class Invoicer |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var EntityManager |
|
25
|
|
|
*/ |
|
26
|
|
|
private $entityManager; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var UserRepository |
|
30
|
|
|
*/ |
|
31
|
|
|
private $userRepository; |
|
32
|
|
|
|
|
33
|
1 |
|
public function __construct(EntityManager $entityManager) |
|
34
|
|
|
{ |
|
35
|
1 |
|
$this->entityManager = $entityManager; |
|
36
|
1 |
|
$this->userRepository = $this->entityManager->getRepository(User::class); |
|
37
|
1 |
|
} |
|
38
|
|
|
|
|
39
|
14 |
|
public function createOrder(array $orderInput): ?Order |
|
40
|
|
|
{ |
|
41
|
14 |
|
$lines = $orderInput['orderLines']; |
|
42
|
14 |
|
if (!$lines) { |
|
43
|
|
|
return null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
14 |
|
$order = new Order(); |
|
47
|
14 |
|
$order->setPaymentMethod($orderInput['paymentMethod']); |
|
48
|
14 |
|
$order->setFirstName($orderInput['firstName'] ?? ''); |
|
49
|
14 |
|
$order->setLastName($orderInput['lastName'] ?? ''); |
|
50
|
14 |
|
$order->setStreet($orderInput['street'] ?? ''); |
|
51
|
14 |
|
$order->setLocality($orderInput['locality'] ?? ''); |
|
52
|
14 |
|
$order->setPostcode($orderInput['postcode'] ?? ''); |
|
53
|
14 |
|
$order->setCountry($orderInput['country'] ?? null); |
|
54
|
|
|
|
|
55
|
14 |
|
$this->userRepository->getAclFilter()->runWithoutAcl(function () use ($lines, $order): void { |
|
56
|
14 |
|
$this->entityManager->persist($order); |
|
57
|
|
|
|
|
58
|
14 |
|
$total = Money::CHF(0); |
|
59
|
14 |
|
foreach ($lines as $line) { |
|
60
|
14 |
|
$args = $this->extractArgsFromLine($line); |
|
61
|
|
|
|
|
62
|
13 |
|
$orderLine = $this->createOrderLine($order, ...$args); |
|
|
|
|
|
|
63
|
13 |
|
$total = $total->add($orderLine->getBalanceCHF()); |
|
64
|
|
|
} |
|
65
|
14 |
|
}); |
|
66
|
|
|
|
|
67
|
13 |
|
return $order; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
13 |
|
private function createOrderLine(Order $order, ?AbstractProduct $product, Money $pricePerUnit, int $quantity, bool $isCHF, string $type, array $additionalEmails): OrderLine |
|
71
|
|
|
{ |
|
72
|
13 |
|
$orderLine = new OrderLine(); |
|
73
|
13 |
|
$this->entityManager->persist($orderLine); |
|
74
|
13 |
|
$orderLine->setOrder($order); |
|
75
|
|
|
|
|
76
|
13 |
|
$this->updateOrderLine($orderLine, $product, $pricePerUnit, $quantity, $isCHF, $type, $additionalEmails); |
|
77
|
|
|
|
|
78
|
13 |
|
return $orderLine; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
15 |
|
private function extractArgsFromLine($input): array |
|
82
|
|
|
{ |
|
83
|
15 |
|
$product = $input['product'] ?? null; |
|
84
|
15 |
|
$subscription = $input['subscription'] ?? null; |
|
85
|
15 |
|
$quantity = $input['quantity']; |
|
86
|
15 |
|
$isCHF = $input['isCHF']; |
|
87
|
15 |
|
$type = $input['type']; |
|
88
|
15 |
|
$additionalEmails = $input['additionalEmails']; |
|
89
|
15 |
|
$pricePerUnit = $input['pricePerUnit'] ?? null; |
|
90
|
15 |
|
$this->assertExactlyOneNotNull($product, $subscription, $pricePerUnit); |
|
91
|
|
|
|
|
92
|
15 |
|
$abstractProduct = $product ?? $subscription; |
|
93
|
15 |
|
$pricePerUnit = $this->getPricePerUnit($abstractProduct, $pricePerUnit, $isCHF); |
|
94
|
|
|
|
|
95
|
14 |
|
if ($additionalEmails && !$subscription) { |
|
96
|
|
|
throw new Exception('Cannot submit additionalEmails without a subscription'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
14 |
|
if ($additionalEmails && !$subscription->isPro()) { |
|
100
|
|
|
throw new Exception('Cannot submit additionalEmails with a subscription that is not pro'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// User cannot choose type of a subscription |
|
104
|
14 |
|
if ($subscription) { |
|
105
|
2 |
|
$type = $subscription->getType(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return [ |
|
109
|
14 |
|
$abstractProduct, |
|
110
|
14 |
|
$pricePerUnit, |
|
111
|
14 |
|
$quantity, |
|
112
|
14 |
|
$isCHF, |
|
113
|
14 |
|
$type, |
|
114
|
14 |
|
$additionalEmails, |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
15 |
|
private function assertExactlyOneNotNull(...$args): void |
|
119
|
|
|
{ |
|
120
|
15 |
|
$onlyNotNull = array_filter($args, function ($val) { |
|
121
|
15 |
|
return $val !== null; |
|
122
|
15 |
|
}); |
|
123
|
|
|
|
|
124
|
15 |
|
if (count($onlyNotNull) !== 1) { |
|
125
|
|
|
throw new Exception('Must have a product, or a subscription, or a pricePerUnit. And not a mixed of those.'); |
|
126
|
|
|
} |
|
127
|
15 |
|
} |
|
128
|
|
|
|
|
129
|
4 |
|
public function updateOrderLineAndTransactionLine(OrderLine $orderLine, array $line): void |
|
130
|
|
|
{ |
|
131
|
4 |
|
$this->userRepository->getAclFilter()->runWithoutAcl(function () use ($orderLine, $line): void { |
|
132
|
4 |
|
$args = $this->extractArgsFromLine($line); |
|
133
|
4 |
|
$this->updateOrderLine($orderLine, ...$args); |
|
|
|
|
|
|
134
|
4 |
|
}); |
|
135
|
4 |
|
} |
|
136
|
|
|
|
|
137
|
14 |
|
private function updateOrderLine(OrderLine $orderLine, ?AbstractProduct $product, Money $pricePerUnit, int $quantity, bool $isCHF, string $type, array $additionalEmails): void |
|
138
|
|
|
{ |
|
139
|
14 |
|
if ($isCHF) { |
|
140
|
13 |
|
$balanceCHF = $pricePerUnit->multiply($quantity); |
|
141
|
13 |
|
$balanceEUR = Money::EUR(0); |
|
142
|
|
|
} else { |
|
143
|
2 |
|
$balanceCHF = Money::CHF(0); |
|
144
|
2 |
|
$balanceEUR = $pricePerUnit->multiply($quantity); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
14 |
|
if (!$product) { |
|
148
|
3 |
|
$orderLine->setDonation(); |
|
149
|
11 |
|
} elseif ($product instanceof Product) { |
|
150
|
9 |
|
$orderLine->setProduct($product); |
|
151
|
2 |
|
} elseif ($product instanceof Subscription) { |
|
152
|
2 |
|
$orderLine->setSubscription($product); |
|
153
|
|
|
} else { |
|
154
|
|
|
throw new Exception('Unsupported subclass of product'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
14 |
|
$orderLine->setIsCHF($isCHF); |
|
158
|
14 |
|
$orderLine->setType($type); |
|
159
|
14 |
|
$orderLine->setQuantity($quantity); |
|
160
|
14 |
|
$orderLine->setBalanceCHF($balanceCHF); |
|
161
|
14 |
|
$orderLine->setBalanceEUR($balanceEUR); |
|
162
|
14 |
|
$orderLine->setAdditionalEmails($additionalEmails); |
|
163
|
14 |
|
} |
|
164
|
|
|
|
|
165
|
15 |
|
private function getPricePerUnit(?AbstractProduct $product, ?Money $pricePerUnit, bool $isCHF): Money |
|
166
|
|
|
{ |
|
167
|
15 |
|
if ($product && $isCHF) { |
|
168
|
11 |
|
return $product->getPricePerUnitCHF(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
5 |
|
if ($product) { |
|
172
|
1 |
|
return $product->getPricePerUnitEUR(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
4 |
|
if ($pricePerUnit === null || !$pricePerUnit->isPositive()) { |
|
176
|
1 |
|
throw new Exception('A donation must have strictly positive price'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
// The API always assume CHF, but if the client specifically say it is EUR, we need to convert |
|
180
|
3 |
|
if (!$isCHF) { |
|
181
|
1 |
|
return Money::EUR($pricePerUnit->getAmount()); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
2 |
|
return $pricePerUnit; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|