Completed
Push — master ( 84cfc6...1601f8 )
by Laurent
01:38
created

CreatePilotYearBillCommandHandler::addOrderLine()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 33
nc 1
nop 5
dl 0
loc 38
rs 8.8571
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
102
        $this->tInstructor = new Product($this->db);
103
        $this->tInstructor = 'Vols dont vous êtes instructeur/examinateur';
0 ignored issues
show
Documentation Bug introduced by
It seems like 'Vols dont vous êtes instructeur/examinateur' of type string is incompatible with the declared type object<Product> of property $tInstructor.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
104
    }
105
106
    /**
107
     * @param CreatePilotYearBillCommand $command
108
     */
109
    public function __invoke(CreatePilotYearBillCommand $command)
110
    {
111
        $object = new Facture($this->db);
112
        $object->fetch_thirdparty();
113
114
        $object->socid = $this->getCompanyIdFromPilot($command->getPilot());
115
        $object->type = $command->getBillType();
116
        $object->number = "provisoire";
117
        $object->date = (new DateTime())->getTimestamp();
118
        $object->date_pointoftax = "";
119
        $object->note_public = $command->getPublicNote();
120
        $object->note_private = $command->getPrivateNote();
121
        $object->ref_client = "";
122
        $object->ref_int = "";
123
        $object->modelpdf = $command->getModelPdf();
124
        $object->cond_reglement_id = $command->getReglementCondition();
125
        $object->mode_reglement_id = $command->getReglementMode();
126
        $object->fk_account = $command->getBankAccount();
127
128
        $id = $object->create($this->user);
129
130
        if ($id <= 0) {
131
            throw new \InvalidArgumentException('Error while creating order');
132
        }
133
134
        $this->localtax1_tx = get_localtax(0, 1, $object->thirdparty);
135
        $this->localtax2_tx = get_localtax(0, 2, $object->thirdparty);
136
137
        $startYearTimestamp = (new \DateTime())->setDate($command->getYear(), 1, 1)->getTimestamp();
138
        $endYearTimestamp = (new \DateTime())->setDate($command->getYear(), 12, 31)->getTimestamp();
139
140
        //T3
141
        $this->addOrderLine($object, $this->t3->getService(), $command->getPilot()->getCountForType('3')->getCount(),
142
            $startYearTimestamp, $endYearTimestamp);
143
144
        //T4
145
        $this->addOrderLine($object, $this->t4->getService(), $command->getPilot()->getCountForType('4')->getCount(),
146
            $startYearTimestamp, $endYearTimestamp);
147
148
        //T6
149
        $this->addOrderLine($object, $this->t6->getService(), $command->getPilot()->getCountForType('6')->getCount(),
150
            $startYearTimestamp, $endYearTimestamp);
151
152
        //T7
153
        $this->addOrderLine($object, $this->t7->getService(), $command->getPilot()->getCountForType('7')->getCount(),
154
            $startYearTimestamp, $endYearTimestamp);
155
156
        $this->addOrderDiscount($object, $command->getPilot()->getCountForType('1'), $this->t1, $command->getYear());
157
        $this->addOrderDiscount($object, $command->getPilot()->getCountForType('2'), $this->t2, $command->getYear());
158
        $this->addOrderDiscount($object, $command->getPilot()->getCountForType('orga'), $this->tOrganisator,
159
            $command->getYear());
160
        $this->addOrderDiscount($object, $command->getPilot()->getCountForType('orga_T6'), $this->tInstructor,
161
            $command->getYear());
162
163
        //Additional bonus
164
        $this->addAdditionalBonusToOrder($command, $object);
165
166
        $object->fetch($id);
167
        $object->generateDocument($command->getModelPdf(), $this->langs, $command->isDetailsHidden(), $command->isDescriptionHidden(), $command->isReferenceHidden());
168
169
        // Validate
170
        $object->fetch($id);
171
        $object->validate($this->user);
172
173
        // Generate document
174
        $object->fetch($id);
175
        $object->generateDocument($command->getModelPdf(), $this->langs, $command->isDetailsHidden(), $command->isDescriptionHidden(), $command->isReferenceHidden());
176
    }
177
178
    /**
179
     * @param Pilot $pilot
180
     *
181
     * @return int
182
     */
183
    private function getCompanyIdFromPilot(Pilot $pilot)
184
    {
185
        $expenseNoteUser = new User($this->db);
186
        $expenseNoteUser->fetch($pilot->getId());
187
188
        $adherent = new Adherent($this->db);
189
        $adherent->fetch($expenseNoteUser->fk_member);
190
191
        return $adherent->fk_soc;
192
    }
193
194
    /**
195
     * @param Facture $object
196
     * @param Product $service
197
     * @param int     $qty
198
     * @param string  $startDate
199
     * @param string  $endDate
200
     */
201
    private function addOrderLine($object, $service, $qty, $startDate, $endDate)
202
    {
203
        $pu_ht = price2num($service->price, 'MU');
204
        $pu_ttc = price2num($service->price_ttc, 'MU');
205
        $pu_ht_devise = price2num($service->price, 'MU');
0 ignored issues
show
Unused Code introduced by
$pu_ht_devise is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
206
207
        $object->addline(
208
            $service->description,
209
            $pu_ht,
210
            $qty,
211
            0,
212
            $this->localtax1_tx,
213
            $this->localtax2_tx,
214
            $service->id,
215
            0,
216
            $startDate,
217
            $endDate,
218
            0,
219
            0,
220
            '',
221
            'TTC',
222
            $pu_ttc,
223
            1,
224
            -1,
225
            0,
226
            '',
227
            0,
228
            0,
229
            '',
230
            '',
231
            $service->label,
232
            [],
233
            100,
234
            '',
235
            0,
236
            0
237
        );
238
    }
239
240
    /**
241
     * @param Facture         $order
242
     * @param FlightTypeCount $pilotFlightCount
243
     * @param Product         $service
244
     * @param string          $year
245
     */
246
    private function addOrderDiscount($order, $pilotFlightCount, $service, $year)
247
    {
248
        $pu_ht = price2num($pilotFlightCount->getCost()->getValue(), 'MU');
249
        $desc = $year . " - " . $service->label . " - (" . $pilotFlightCount->getCount() . " * " . $pilotFlightCount->getFactor() . ")";
250
251
        $discountid = $this->getCompany($order->socid)->set_remise_except($pu_ht, $this->user, $desc, 0);
252
        $order->insert_discount($discountid);
253
    }
254
255
    /**
256
     * @param int $id
257
     *
258
     * @return Societe
259
     */
260
    private function getCompany($id)
261
    {
262
        $soc = new Societe($this->db);
263
        $soc->fetch($id);
264
265
        return $soc;
266
    }
267
268
    /**
269
     * @param CreatePilotYearBillCommand $command
270
     * @param Facture                    $object
271
     */
272
    private function addAdditionalBonusToOrder(CreatePilotYearBillCommand $command, $object)
273
    {
274
        if ((int)$command->getAdditionalBonus() <= 0) {
275
            return;
276
        }
277
278
        $pu_ht = price2num($command->getAdditionalBonus(), 'MU');
279
        $desc = sprintf("%s - %s", $command->getYear(), $command->getBonusAdditionalMessage());
280
281
        $discountid = $this->getCompany($object->socid)->set_remise_except($pu_ht, $this->user, $desc, 0);
282
        $object->insert_discount($discountid);
283
    }
284
285
286
}