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