Completed
Push — feature/fixing_cost ( f58af6...414fbf )
by Laurent
01:41
created

addOrderDiscount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
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())->setDate($command->getYear(), 12, 31)->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
        //Damages
159
        $this->addDamages($object, $command->getPilot()->getCountForType('damage'), $command->getPilot()->getCountForType('invoiced_damage'), $startYearTimestamp, $endYearTimestamp);
160
161
        $this->addOrderDiscount($object, $command->getPilot()->getCountForType('1'), $this->t1->getService(), $command->getYear());
162
        $this->addOrderDiscount($object, $command->getPilot()->getCountForType('2'), $this->t2->getService(), $command->getYear());
163
        $this->addOrderDiscount($object, $command->getPilot()->getCountForType('orga'), $this->tOrganisator, $command->getYear());
164
        $this->addOrderDiscount($object, $command->getPilot()->getCountForType('orga_T6'), $this->tInstructor, $command->getYear());
165
166
        //Additional bonus
167
        $this->addAdditionalBonusToOrder($command, $object);
168
169
        $object->fetch($id);
170
        $object->generateDocument($command->getModelPdf(), $this->langs, $command->isDetailsHidden(),
171
            $command->isDescriptionHidden(), $command->isReferenceHidden());
172
173
        // Validate
174
        $object->fetch($id);
175
        $object->validate($this->user);
176
177
        // Generate document
178
        $object->fetch($id);
179
        $object->generateDocument($command->getModelPdf(), $this->langs, $command->isDetailsHidden(),
180
            $command->isDescriptionHidden(), $command->isReferenceHidden());
181
    }
182
183
    /**
184
     * @param Pilot $pilot
185
     *
186
     * @return int
187
     */
188
    private function getCompanyIdFromPilot(Pilot $pilot)
189
    {
190
        $expenseNoteUser = new User($this->db);
191
        $expenseNoteUser->fetch($pilot->getId());
192
193
        $adherent = new Adherent($this->db);
194
        $adherent->fetch($expenseNoteUser->fk_member);
195
196
        return $adherent->fk_soc;
197
    }
198
199
    /**
200
     * @param Facture $object
201
     * @param Product $service
202
     * @param int     $qty
203
     * @param string  $startDate
204
     * @param string  $endDate
205
     */
206
    private function addOrderLine($object, $service, $qty, $startDate, $endDate)
207
    {
208
        $pu_ht = price2num($service->price, 'MU');
209
        $pu_ttc = price2num($service->price_ttc, 'MU');
210
        $pu_ht_devise = price2num($service->price, 'MU');
211
212
        $object->addline(
213
            $service->description,
214
            $pu_ht,
215
            $qty,
216
            $service->tva_tx,
217
            $this->localtax1_tx,
218
            $this->localtax2_tx,
219
            $service->id,
220
            0,
221
            $startDate,
222
            $endDate,
223
            0,
224
            0,
225
            '',
226
            'TTC',
227
            $pu_ttc,
228
            1,
229
            -1,
230
            0,
231
            '',
232
            0,
233
            0,
234
            '',
235
            '',
236
            $service->label,
237
            [],
238
            100,
239
            '',
240
            0,
241
            $pu_ht_devise
242
        );
243
    }
244
245
    /**
246
     * @param Facture         $order
247
     * @param FlightTypeCount $pilotFlightCount
248
     * @param Product         $service
249
     * @param string          $year
250
     */
251
    private function addOrderDiscount($order, $pilotFlightCount, $service, $year)
252
    {
253
        $pu_ht = price2num($pilotFlightCount->getCost()->getValue(), 'MU');
254
        $desc = $year . " - " . $service->label . " - (" . $pilotFlightCount->getCount() . " * " . $pilotFlightCount->getFactor() . ")";
255
256
        $discountid = $this->getCompany($order->socid)->set_remise_except($pu_ht, $this->user, $desc, $service->tva_tx);
257
        $order->insert_discount($discountid);
258
    }
259
260
    /**
261
     * @param int $id
262
     *
263
     * @return Societe
264
     */
265
    private function getCompany($id)
266
    {
267
        $soc = new Societe($this->db);
268
        $soc->fetch($id);
269
270
        return $soc;
271
    }
272
273
    /**
274
     * @param CreatePilotYearBillCommand $command
275
     * @param Facture                    $object
276
     */
277
    private function addAdditionalBonusToOrder(CreatePilotYearBillCommand $command, $object)
278
    {
279
        if ((int) $command->getAdditionalBonus() <= 0) {
280
            return;
281
        }
282
283
        $pointsHt = $command->getAdditionalBonus()/(1+6/100);
284
        $desc = sprintf("%s - %s", $command->getYear(), $command->getBonusAdditionalMessage());
285
286
        $discountid = $this->getCompany($object->socid)->set_remise_except($pointsHt, $this->user, $desc, 6);
287
        $object->insert_discount($discountid);
288
    }
289
290
    /**
291
     * Adds the damages.
292
     *
293
     * @param Facture $object
294
     * @param FlightTypeCount $damage
295
     * @param FlightTypeCount $invoicedDamage
296
     * @param string $start
297
     * @param string $end
298
     */
299
    private function addDamages(Facture $object, FlightTypeCount $damage, FlightTypeCount $invoicedDamage, $start, $end)
300
    {
301
        $price = $damage->getCost()->addCost($invoicedDamage->getCost())->getValue();
302
303
        if($price <= 0 ){
304
            return;
305
        }
306
307
        $tDamage = new Product($this->db);
308
        $tDamage->label = 'Réparations';
309
        $tDamage->tva_tx = 21;
310
        $tDamage->price_ttc = $price;
311
        $tDamage->price = $tDamage->price_ttc / (1 + $tDamage->tva_tx / 100);
312
313
        $this->addOrderLine($object, $tDamage, 1, $start, $end);
314
    }
315
316
317
}