1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @author Laurent De Coninck <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
class CreatePilotYearBillCommandHandler |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \DoliDB |
13
|
|
|
*/ |
14
|
|
|
private $db; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \stdClass |
18
|
|
|
*/ |
19
|
|
|
private $conf; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var User |
23
|
|
|
*/ |
24
|
|
|
private $user; |
25
|
|
|
|
26
|
|
|
private $langs; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Bbctypes |
30
|
|
|
*/ |
31
|
|
|
private $t1; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Bbctypes |
35
|
|
|
*/ |
36
|
|
|
private $t2; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Bbctypes |
40
|
|
|
*/ |
41
|
|
|
private $t3; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Bbctypes |
45
|
|
|
*/ |
46
|
|
|
private $t4; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Bbctypes |
50
|
|
|
*/ |
51
|
|
|
private $t5; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Bbctypes |
55
|
|
|
*/ |
56
|
|
|
private $t6; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Bbctypes |
60
|
|
|
*/ |
61
|
|
|
private $t7; |
62
|
|
|
|
63
|
|
|
private $localtax1_tx; |
64
|
|
|
|
65
|
|
|
private $localtax2_tx; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var Product |
69
|
|
|
*/ |
70
|
|
|
private $tOrganisator; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var Product |
74
|
|
|
*/ |
75
|
|
|
private $tInstructor; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param DoliDB $db |
79
|
|
|
* @param stdClass $conf |
80
|
|
|
* @param User $user |
81
|
|
|
* @param $langs |
82
|
|
|
* @param array|Bbctypes[] $flightTypes |
83
|
|
|
*/ |
84
|
|
|
public function __construct(DoliDB $db, stdClass $conf, User $user, $langs, $flightTypes) |
85
|
|
|
{ |
86
|
|
|
$this->db = $db; |
87
|
|
|
$this->conf = $conf; |
88
|
|
|
$this->user = $user; |
89
|
|
|
$this->langs = $langs; |
90
|
|
|
|
91
|
|
|
$this->t1 = $flightTypes['1']; |
92
|
|
|
$this->t2 = $flightTypes['2']; |
93
|
|
|
$this->t3 = $flightTypes['3']; |
94
|
|
|
$this->t4 = $flightTypes['4']; |
95
|
|
|
$this->t5 = $flightTypes['5']; |
96
|
|
|
$this->t6 = $flightTypes['6']; |
97
|
|
|
$this->t7 = $flightTypes['7']; |
98
|
|
|
|
99
|
|
|
$this->tOrganisator = new Product($this->db); |
100
|
|
|
$this->tOrganisator->label = 'Vols dont vous êtes organisateur'; |
101
|
|
|
$this->tOrganisator->tva_tx = $this->t1->getService()->tva_tx; |
102
|
|
|
|
103
|
|
|
$this->tInstructor = new Product($this->db); |
104
|
|
|
$this->tInstructor->label = 'Vols dont vous êtes instructeur/examinateur'; |
105
|
|
|
$this->tInstructor->tva_tx = $this->t1->getService()->tva_tx; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param CreatePilotYearBillCommand $command |
110
|
|
|
*/ |
111
|
|
|
public function __invoke(CreatePilotYearBillCommand $command) |
112
|
|
|
{ |
113
|
|
|
$object = new Facture($this->db); |
114
|
|
|
$object->fetch_thirdparty(); |
115
|
|
|
|
116
|
|
|
$object->socid = $this->getCompanyIdFromPilot($command->getPilot()); |
117
|
|
|
$object->type = $command->getBillType(); |
118
|
|
|
$object->number = "provisoire"; |
119
|
|
|
$object->date = (new DateTime())->getTimestamp(); |
120
|
|
|
$object->date_pointoftax = ""; |
121
|
|
|
$object->note_public = $command->getPublicNote(); |
122
|
|
|
$object->note_private = $command->getPrivateNote(); |
123
|
|
|
$object->ref_client = ""; |
124
|
|
|
$object->ref_int = ""; |
125
|
|
|
$object->modelpdf = $command->getModelPdf(); |
126
|
|
|
$object->cond_reglement_id = $command->getReglementCondition(); |
127
|
|
|
$object->mode_reglement_id = $command->getReglementMode(); |
128
|
|
|
$object->fk_account = $command->getBankAccount(); |
129
|
|
|
|
130
|
|
|
$id = $object->create($this->user); |
131
|
|
|
|
132
|
|
|
if ($id <= 0) { |
133
|
|
|
throw new \InvalidArgumentException('Error while creating order'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->localtax1_tx = get_localtax(0, 1, $object->thirdparty); |
137
|
|
|
$this->localtax2_tx = get_localtax(0, 2, $object->thirdparty); |
138
|
|
|
|
139
|
|
|
$startYearTimestamp = (new \DateTime())->setDate($command->getYear(), 1, 1)->getTimestamp(); |
140
|
|
|
$endYearTimestamp = (new \DateTime())->setDate($command->getYear(), 12, 31)->getTimestamp(); |
141
|
|
|
|
142
|
|
|
//T3 |
143
|
|
|
$this->addOrderLine($object, $this->t3->getService(), $command->getPilot()->getCountForType('3')->getCount(), |
144
|
|
|
$startYearTimestamp, $endYearTimestamp); |
145
|
|
|
|
146
|
|
|
//T4 |
147
|
|
|
$this->addOrderLine($object, $this->t4->getService(), $command->getPilot()->getCountForType('4')->getCount(), |
148
|
|
|
$startYearTimestamp, $endYearTimestamp); |
149
|
|
|
|
150
|
|
|
//T6 |
151
|
|
|
$this->addOrderLine($object, $this->t6->getService(), $command->getPilot()->getCountForType('6')->getCount(), |
152
|
|
|
$startYearTimestamp, $endYearTimestamp); |
153
|
|
|
|
154
|
|
|
//T7 |
155
|
|
|
$this->addOrderLine($object, $this->t7->getService(), $command->getPilot()->getCountForType('7')->getCount(), |
156
|
|
|
$startYearTimestamp, $endYearTimestamp); |
157
|
|
|
|
158
|
|
|
$this->addOrderDiscount($object, $command->getPilot()->getCountForType('1'), $this->t1->getService(), $command->getYear()); |
159
|
|
|
$this->addOrderDiscount($object, $command->getPilot()->getCountForType('2'), $this->t2->getService(), $command->getYear()); |
160
|
|
|
$this->addOrderDiscount($object, $command->getPilot()->getCountForType('orga'), $this->tOrganisator, |
161
|
|
|
$command->getYear()); |
162
|
|
|
$this->addOrderDiscount($object, $command->getPilot()->getCountForType('orga_T6'), $this->tInstructor, |
163
|
|
|
$command->getYear()); |
164
|
|
|
|
165
|
|
|
//Additional bonus |
166
|
|
|
$this->addAdditionalBonusToOrder($command, $object); |
167
|
|
|
|
168
|
|
|
$object->fetch($id); |
169
|
|
|
$object->generateDocument($command->getModelPdf(), $this->langs, $command->isDetailsHidden(), |
170
|
|
|
$command->isDescriptionHidden(), $command->isReferenceHidden()); |
171
|
|
|
|
172
|
|
|
// Validate |
173
|
|
|
$object->fetch($id); |
174
|
|
|
$object->validate($this->user); |
175
|
|
|
|
176
|
|
|
// Generate document |
177
|
|
|
$object->fetch($id); |
178
|
|
|
$object->generateDocument($command->getModelPdf(), $this->langs, $command->isDetailsHidden(), |
179
|
|
|
$command->isDescriptionHidden(), $command->isReferenceHidden()); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param Pilot $pilot |
184
|
|
|
* |
185
|
|
|
* @return int |
186
|
|
|
*/ |
187
|
|
|
private function getCompanyIdFromPilot(Pilot $pilot) |
188
|
|
|
{ |
189
|
|
|
$expenseNoteUser = new User($this->db); |
190
|
|
|
$expenseNoteUser->fetch($pilot->getId()); |
191
|
|
|
|
192
|
|
|
$adherent = new Adherent($this->db); |
193
|
|
|
$adherent->fetch($expenseNoteUser->fk_member); |
194
|
|
|
|
195
|
|
|
return $adherent->fk_soc; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param Facture $object |
200
|
|
|
* @param Product $service |
201
|
|
|
* @param int $qty |
202
|
|
|
* @param string $startDate |
203
|
|
|
* @param string $endDate |
204
|
|
|
*/ |
205
|
|
|
private function addOrderLine($object, $service, $qty, $startDate, $endDate) |
206
|
|
|
{ |
207
|
|
|
$pu_ht = price2num($service->price, 'MU'); |
208
|
|
|
$pu_ttc = price2num($service->price_ttc, 'MU'); |
209
|
|
|
$pu_ht_devise = price2num($service->price, 'MU'); |
210
|
|
|
|
211
|
|
|
$object->addline( |
212
|
|
|
$service->description, |
213
|
|
|
$pu_ht, |
214
|
|
|
$qty, |
215
|
|
|
$service->tva_tx, |
216
|
|
|
$this->localtax1_tx, |
217
|
|
|
$this->localtax2_tx, |
218
|
|
|
$service->id, |
219
|
|
|
0, |
220
|
|
|
$startDate, |
221
|
|
|
$endDate, |
222
|
|
|
0, |
223
|
|
|
0, |
224
|
|
|
'', |
225
|
|
|
'TTC', |
226
|
|
|
$pu_ttc, |
227
|
|
|
1, |
228
|
|
|
-1, |
229
|
|
|
0, |
230
|
|
|
'', |
231
|
|
|
0, |
232
|
|
|
0, |
233
|
|
|
'', |
234
|
|
|
'', |
235
|
|
|
$service->label, |
236
|
|
|
[], |
237
|
|
|
100, |
238
|
|
|
'', |
239
|
|
|
0, |
240
|
|
|
$pu_ht_devise |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param Facture $order |
246
|
|
|
* @param FlightTypeCount $pilotFlightCount |
247
|
|
|
* @param Product $service |
248
|
|
|
* @param string $year |
249
|
|
|
*/ |
250
|
|
|
private function addOrderDiscount($order, $pilotFlightCount, $service, $year) |
251
|
|
|
{ |
252
|
|
|
$pu_ht = price2num($pilotFlightCount->getCost()->getValue(), 'MU'); |
253
|
|
|
$desc = $year . " - " . $service->label . " - (" . $pilotFlightCount->getCount() . " * " . $pilotFlightCount->getFactor() . ")"; |
254
|
|
|
|
255
|
|
|
$discountid = $this->getCompany($order->socid)->set_remise_except($pu_ht, $this->user, $desc, $service->tva_tx); |
256
|
|
|
$order->insert_discount($discountid); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param int $id |
261
|
|
|
* |
262
|
|
|
* @return Societe |
263
|
|
|
*/ |
264
|
|
|
private function getCompany($id) |
265
|
|
|
{ |
266
|
|
|
$soc = new Societe($this->db); |
267
|
|
|
$soc->fetch($id); |
268
|
|
|
|
269
|
|
|
return $soc; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param CreatePilotYearBillCommand $command |
274
|
|
|
* @param Facture $object |
275
|
|
|
*/ |
276
|
|
|
private function addAdditionalBonusToOrder(CreatePilotYearBillCommand $command, $object) |
277
|
|
|
{ |
278
|
|
|
if ((int) $command->getAdditionalBonus() <= 0) { |
279
|
|
|
return; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$pu_ht = price2num($command->getAdditionalBonus(), 'MU'); |
283
|
|
|
$desc = sprintf("%s - %s", $command->getYear(), $command->getBonusAdditionalMessage()); |
284
|
|
|
|
285
|
|
|
$discountid = $this->getCompany($object->socid)->set_remise_except($pu_ht, $this->user, $desc, 0); |
286
|
|
|
$object->insert_discount($discountid); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
|
290
|
|
|
} |