1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once __DIR__ . '/../exceptions/ContactNotAddedException.php'; |
4
|
|
|
require_once __DIR__ . '/../exceptions/CustomerNotFoundException.php'; |
5
|
|
|
require_once __DIR__ . '/../exceptions/FlightNotFoundException.php'; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Laurent De Coninck <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class CreateFlightBillCommandHandler |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \DoliDB |
14
|
|
|
*/ |
15
|
|
|
protected $db; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var stdClass |
19
|
|
|
*/ |
20
|
|
|
private $conf; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var User |
24
|
|
|
*/ |
25
|
|
|
protected $user; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Translate |
29
|
|
|
*/ |
30
|
|
|
private $langs; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param DoliDB $db |
34
|
|
|
* @param stdClass $conf |
35
|
|
|
* @param User $user |
36
|
|
|
* @param Translate $langs |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function __construct($db, $conf, $user, $langs) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$this->db = $db; |
41
|
|
|
$this->conf = $conf; |
42
|
|
|
$this->user = $user; |
43
|
|
|
$this->langs = $langs; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param CreateFlightBillCommand $command |
48
|
|
|
*/ |
49
|
|
|
public function handle(CreateFlightBillCommand $command) |
50
|
|
|
{ |
51
|
|
|
$flightProduct = $this->getProduct(); |
52
|
|
|
$flight = $this->getFlight($command->getFlightId()); |
53
|
|
|
|
54
|
|
|
$object = new Facture($this->db); |
55
|
|
|
$object->fetch_thirdparty(); |
56
|
|
|
|
57
|
|
|
$object->socid = $this->getCustomer($flight)->id; |
58
|
|
|
$object->type = $command->getBillType(); |
59
|
|
|
$object->number = "provisoire"; |
60
|
|
|
$object->date = (new DateTime())->getTimestamp(); |
61
|
|
|
$object->date_pointoftax = ""; |
62
|
|
|
$object->note_public = $command->getPublicNote(); |
63
|
|
|
$object->note_private = $command->getPrivateNote(); |
64
|
|
|
$object->ref_client = ""; |
65
|
|
|
$object->ref_int = ""; |
66
|
|
|
$object->modelpdf = $command->getModelDocument(); |
67
|
|
|
$object->cond_reglement_id = $command->getBillingCondition(); |
68
|
|
|
$object->mode_reglement_id = $command->getBillingType(); |
69
|
|
|
$object->fk_account = $command->getBankAccount(); |
70
|
|
|
|
71
|
|
|
$id = $object->create($this->user); |
72
|
|
|
|
73
|
|
|
if ($id <= 0) { |
74
|
|
|
throw new \InvalidArgumentException('Error during bill creation'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->addOrderLine($object, $flightProduct, $flight, $command->getNbrPax()); |
78
|
|
|
|
79
|
|
|
$this->addLinks($object, $flight); |
80
|
|
|
$this->addContacts($object, $flight); |
81
|
|
|
|
82
|
|
|
$this->generateBillDocument($command, $object, $id); |
83
|
|
|
|
84
|
|
|
$this->validates($object, $id); |
85
|
|
|
|
86
|
|
|
$this->generateBillDocument($command, $object, $id); |
87
|
|
|
$this->flagFlightAsBilled($flight); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Product |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
private function getProduct() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$flightProduct = new Product($this->db); |
96
|
|
|
|
97
|
|
|
if ($flightProduct->fetch($this->conf->BBC_FLIGHT_TYPE_CUSTOMER) <= 0) { |
98
|
|
|
throw new \InvalidArgumentException('Default product not configured'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $flightProduct; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Bbcvols $flight |
106
|
|
|
* |
107
|
|
|
* @return Client |
108
|
|
|
* |
109
|
|
|
* @throws CustomerNotFoundException |
110
|
|
|
*/ |
111
|
|
|
private function getCustomer(Bbcvols $flight) |
112
|
|
|
{ |
113
|
|
|
$customer = new Client($this->db); |
114
|
|
|
|
115
|
|
|
if($flight->fk_receiver) { |
116
|
|
|
return $this->fetchCustomerFromFlight($flight); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($customer->fetch($this->conf->BBC_FLIGHT_TYPE_CUSTOMER) <= 0) { |
120
|
|
|
throw new CustomerNotFoundException(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $customer; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param Bbcvols $flight |
128
|
|
|
* |
129
|
|
|
* @return Client |
130
|
|
|
*/ |
131
|
|
|
private function fetchCustomerFromFlight($flight) |
132
|
|
|
{ |
133
|
|
|
$user = new User($this->db); |
134
|
|
|
$res = $user->fetch($flight->fk_receiver); |
135
|
|
|
if ($res <= 0) { |
136
|
|
|
throw new CustomerNotFoundException('User not found'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$member = new Adherent($this->db); |
140
|
|
|
$res = $member->fetch($user->fk_member); |
141
|
|
|
if ($res <= 0) { |
142
|
|
|
throw new CustomerNotFoundException('Member not found'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$customer = new Client($this->db); |
146
|
|
|
if ($customer->fetch($member->fk_soc) <= 0) { |
147
|
|
|
throw new CustomerNotFoundException(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $customer; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param int $flightId |
155
|
|
|
* |
156
|
|
|
* @return Bbcvols |
157
|
|
|
* |
158
|
|
|
* @throws FlightNotFoundException |
159
|
|
|
*/ |
160
|
|
|
private function getFlight($flightId) |
161
|
|
|
{ |
162
|
|
|
$flight = new Bbcvols($this->db); |
163
|
|
|
|
164
|
|
|
if ($flight->fetch($flightId) <= 0) { |
165
|
|
|
throw new FlightNotFoundException(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $flight; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param Facture $object |
173
|
|
|
* @param Bbcvols $flight |
174
|
|
|
* |
175
|
|
|
* @throws ContactNotAddedException |
176
|
|
|
*/ |
177
|
|
|
private function addContacts($object, $flight) |
178
|
|
|
{ |
179
|
|
|
$this->addContactOnBill($object, $flight->fk_pilot, 'BBC_PILOT'); |
180
|
|
|
$this->addContactOnBill($object, $flight->fk_receiver, 'BBC_RECEIVER'); |
181
|
|
|
$this->addContactOnBill($object, $flight->fk_organisateur, 'BBC_ORGANISATOR'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param Facture $bill |
186
|
|
|
* @param int $contactId |
187
|
|
|
* @param string $contactType |
188
|
|
|
* |
189
|
|
|
* @throws ContactNotAddedException |
190
|
|
|
*/ |
191
|
|
|
private function addContactOnBill(Facture $bill, $contactId, $contactType) |
192
|
|
|
{ |
193
|
|
|
if ($bill->add_contact($contactId, $contactType, 'internal') < 0) { |
194
|
|
|
throw new ContactNotAddedException($contactType); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param Facture $object |
200
|
|
|
* @param Bbcvols $flight |
201
|
|
|
*/ |
202
|
|
|
private function addLinks($object, $flight) |
203
|
|
|
{ |
204
|
|
|
$object->add_object_linked('flightlog_bbcvols', $flight->getId()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param Facture $object |
209
|
|
|
* @param int $id |
210
|
|
|
*/ |
211
|
|
|
private function validates($object, $id) |
212
|
|
|
{ |
213
|
|
|
$object->fetch($id); |
214
|
|
|
$object->validate($this->user); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return int |
219
|
|
|
*/ |
220
|
|
|
private function isReferenceHidden() |
221
|
|
|
{ |
222
|
|
|
return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return int |
227
|
|
|
*/ |
228
|
|
|
private function isDescriptionHidden() |
229
|
|
|
{ |
230
|
|
|
return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return int |
235
|
|
|
*/ |
236
|
|
|
private function isDetailHidden() |
237
|
|
|
{ |
238
|
|
|
return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param CreateFlightBillCommand $command |
243
|
|
|
* @param Facture $object |
244
|
|
|
* @param int $id |
245
|
|
|
*/ |
246
|
|
|
private function generateBillDocument(CreateFlightBillCommand $command, $object, $id) |
247
|
|
|
{ |
248
|
|
|
$object->fetch($id); |
249
|
|
|
$object->generateDocument( |
250
|
|
|
$command->getModelDocument(), |
251
|
|
|
$this->langs, |
252
|
|
|
$this->isDetailHidden(), |
253
|
|
|
$this->isDescriptionHidden(), |
254
|
|
|
$this->isReferenceHidden() |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param $flightProduct |
260
|
|
|
* @param $flight |
261
|
|
|
* |
262
|
|
|
* @return float|int |
263
|
|
|
*/ |
264
|
|
|
private function computeDiscounts($flightProduct, $flight) |
265
|
|
|
{ |
266
|
|
|
return ($flightProduct->price_ttc - ($flight->cost / $flight->nbrPax)) * 100 / $flightProduct->price_ttc; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param Facture $object |
271
|
|
|
* @param Product $flightProduct |
272
|
|
|
* @param Bbcvols $flight |
273
|
|
|
*/ |
274
|
|
|
private function addOrderLine($object, $flightProduct, $flight, $nbrPax) |
275
|
|
|
{ |
276
|
|
|
$localtax1_tx = get_localtax(0, 1, $object->thirdparty); |
277
|
|
|
$localtax2_tx = get_localtax(0, 2, $object->thirdparty); |
278
|
|
|
|
279
|
|
|
$pu_ht = price2num($flightProduct->price, 'MU'); |
280
|
|
|
$pu_ttc = price2num($flightProduct->price_ttc, 'MU'); |
281
|
|
|
$pu_ht_devise = price2num($flightProduct->price, 'MU'); |
282
|
|
|
|
283
|
|
|
$discount = $this->computeDiscounts($flightProduct, $flight); |
284
|
|
|
|
285
|
|
|
$result = $object->addline( |
286
|
|
|
$flightProduct->description, |
287
|
|
|
$pu_ht, |
288
|
|
|
$nbrPax, |
289
|
|
|
$flightProduct->tva_tx, |
290
|
|
|
$localtax1_tx, |
291
|
|
|
$localtax2_tx, |
292
|
|
|
$flightProduct->id, |
293
|
|
|
$discount, |
294
|
|
|
$flight->date, |
295
|
|
|
$flight->date, |
296
|
|
|
0, |
297
|
|
|
0, |
298
|
|
|
'', |
299
|
|
|
'TTC', |
300
|
|
|
$pu_ttc, |
301
|
|
|
Facture::TYPE_STANDARD, |
302
|
|
|
-1, |
303
|
|
|
0, |
304
|
|
|
'', |
305
|
|
|
0, |
306
|
|
|
0, |
307
|
|
|
'', |
308
|
|
|
'', |
309
|
|
|
$flightProduct->label, |
310
|
|
|
[], |
311
|
|
|
100, |
312
|
|
|
'', |
313
|
|
|
0, |
314
|
|
|
$pu_ht_devise |
315
|
|
|
); |
316
|
|
|
|
317
|
|
|
if ($result <= 0) { |
318
|
|
|
throw new \InvalidArgumentException('Error during order line creation'); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param Bbcvols $flight |
324
|
|
|
*/ |
325
|
|
|
private function flagFlightAsBilled($flight) |
326
|
|
|
{ |
327
|
|
|
$flight->is_facture = true; |
328
|
|
|
$flight->update($this->user); |
329
|
|
|
} |
330
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.