Completed
Branch feature/create_monthly_billing (30fb74)
by Laurent
02:14
created

flagFlightAsBilled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
require_once __DIR__ . '/../class/bbcvols.class.php';
4
require_once __DIR__ . '/CreatePilotMonthBillCommand.php';
5
require_once __DIR__ . '/../../product/class/product.class.php';
6
require_once __DIR__ . '/../../compta/facture/class/facture.class.php';
7
require_once __DIR__ . '/../../user/class/user.class.php';
8
require_once __DIR__ . '/../../adherents/class/adherent.class.php';
9
10
/**
11
 * @author Laurent De Coninck <[email protected]>
12
 */
13
class CreatePilotMonthBillCommandHandler
14
{
15
16
    /**
17
     * @var \DoliDB
18
     */
19
    protected $db;
20
21
    /**
22
     * @var stdClass
23
     */
24
    private $conf;
25
26
    /**
27
     * @var User
28
     */
29
    protected $user;
30
31
    /**
32
     * @var Translate
33
     */
34
    private $langs;
35
36
    /**
37
     * @param DoliDB    $db
38
     * @param stdClass  $conf
39
     * @param User      $user
40
     * @param Translate $langs
41
     */
42 View Code Duplication
    public function __construct($db, $conf, $user, $langs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
43
    {
44
        $this->db = $db;
45
        $this->conf = $conf;
46
        $this->user = $user;
47
        $this->langs = $langs;
48
    }
49
50
    /**
51
     * @param CreatePilotMonthBillCommand $command
52
     */
53 View Code Duplication
    public function handle($command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
54
    {
55
        $object = new Facture($this->db);
56
        $object->fetch_thirdparty();
57
58
        $object->socid = $this->getCustomer($command->getReceiverId())->id;
59
        $object->type = $command->getBillingType();
60
        $object->number = "provisoire";
61
        $object->date = $this->generateBillDate($command->getYear(), $command->getMonth());
62
        $object->date_pointoftax = "";
63
        $object->note_public = $command->getPublicNote();
64
        $object->note_private = $command->getPrivateNote();
65
        $object->ref_client = "";
66
        $object->ref_int = "";
67
        $object->modelpdf = $command->getModelDocument();
68
        $object->cond_reglement_id = $command->getBillingCondition();
69
        $object->mode_reglement_id = $command->getBillType();
70
        $object->fk_account = $this->getBankAccount();
71
72
        $id = $object->create($this->user);
73
74
        if ($id <= 0) {
75
            throw new \InvalidArgumentException('Error during bill creation');
76
        }
77
78
        $flightProduct = $this->getProduct();
79
        foreach ($command->getFlights() as $flight) {
80
            $this->addOrderLine($object, $flightProduct, $flight);
81
            $this->addLinks($object, $flight);
82
            $this->flagFlightAsBilled($flight);
83
        }
84
85
        $this->generateBillDocument($command, $object, $id);
86
87
        $this->validates($object, $id);
88
89
        $this->generateBillDocument($command, $object, $id);
90
    }
91
92
    /**
93
     * @return Product
94
     */
95 View Code Duplication
    private function getProduct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
96
    {
97
        $flightProduct = new Product($this->db);
98
99
        if ($flightProduct->fetch($this->conf->BBC_FLIGHT_TYPE_CUSTOMER) <= 0) {
100
            throw new \InvalidArgumentException('Default product not configured');
101
        }
102
103
        return $flightProduct;
104
    }
105
106
    /**
107
     * @param int $receiverId
108
     *
109
     * @return Client
110
     * @throws CustomerNotFoundException
111
     */
112
    private function getCustomer($receiverId)
113
    {
114
        $user = new User($this->db);
115
        $res = $user->fetch($receiverId);
116
        if ($res <= 0) {
117
            throw new CustomerNotFoundException('User not found');
118
        }
119
120
        $member = new Adherent($this->db);
121
        $res = $member->fetch($user->fk_member);
122
        if ($res <= 0) {
123
            throw new CustomerNotFoundException('Member not found');
124
        }
125
126
        $customer = new Client($this->db);
127
        if ($customer->fetch($member->fk_soc) <= 0) {
128
            throw new CustomerNotFoundException();
129
        }
130
131
        return $customer;
132
    }
133
134
    /**
135
     * @param Facture $object
136
     * @param Bbcvols $flight
137
     *
138
     * @throws ContactNotAddedException
139
     */
140
    private function addContacts($object, $flight)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
141
    {
142
        $this->addContactOnBill($object, $flight->fk_pilot, 'BBC_PILOT');
143
        $this->addContactOnBill($object, $flight->fk_receiver, 'BBC_RECEIVER');
144
        $this->addContactOnBill($object, $flight->fk_organisateur, 'BBC_ORGANISATOR');
145
    }
146
147
    /**
148
     * @param Facture $bill
149
     * @param int     $contactId
150
     * @param string  $contactType
151
     *
152
     * @throws ContactNotAddedException
153
     */
154
    private function addContactOnBill(Facture $bill, $contactId, $contactType)
155
    {
156
        if ($bill->add_contact($contactId, $contactType, 'internal') < 0) {
157
            throw new ContactNotAddedException($contactType);
158
        }
159
    }
160
161
    /**
162
     * @param Facture $object
163
     * @param Bbcvols $flight
164
     */
165
    private function addLinks($object, $flight)
166
    {
167
        $object->add_object_linked('flightlog_bbcvols', $flight->getId());
168
    }
169
170
    /**
171
     * @param Facture $object
172
     * @param int     $id
173
     */
174
    private function validates($object, $id)
175
    {
176
        $object->fetch($id);
177
        $object->validate($this->user);
178
    }
179
180
    /**
181
     * @return int
182
     */
183
    private function isReferenceHidden()
184
    {
185
        return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
186
    }
187
188
    /**
189
     * @return int
190
     */
191
    private function isDescriptionHidden()
192
    {
193
        return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
194
    }
195
196
    /**
197
     * @return int
198
     */
199
    private function isDetailHidden()
200
    {
201
        return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
202
    }
203
204
    /**
205
     * @param CreatePilotMonthBillCommand $command
206
     * @param Facture                     $object
207
     * @param int                         $id
208
     */
209 View Code Duplication
    private function generateBillDocument(CreatePilotMonthBillCommand $command, $object, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
210
    {
211
        $object->fetch($id);
212
        $object->generateDocument(
213
            $command->getModelDocument(),
214
            $this->langs,
215
            $this->isDetailHidden(),
216
            $this->isDescriptionHidden(),
217
            $this->isReferenceHidden()
218
        );
219
    }
220
221
    /**
222
     * @param Product $flightProduct
223
     * @param Bbcvols $flight
224
     *
225
     * @return float|int
226
     */
227
    private function computeDiscounts($flightProduct, $flight)
228
    {
229
        return ($flightProduct->price_ttc - ($flight->cost / $flight->nbrPax)) * 100 / $flightProduct->price_ttc;
230
    }
231
232
    /**
233
     * @param Facture $facture
234
     * @param Product $flightProduct
235
     * @param Bbcvols $flight
236
     */
237
    private function addOrderLine($facture, $flightProduct, $flight)
238
    {
239
        $localtax1_tx = get_localtax(0, 1, $facture->thirdparty);
240
        $localtax2_tx = get_localtax(0, 2, $facture->thirdparty);
241
242
        $pu_ht = price2num($flightProduct->price, 'MU');
243
        $pu_ttc = price2num($flightProduct->price_ttc, 'MU');
244
        $pu_ht_devise = price2num($flightProduct->price, 'MU');
245
246
        $discount = $this->computeDiscounts($flightProduct, $flight);
247
248
        if (!is_numeric($flight->nbrPax)) {
249
            throw new \InvalidArgumentException(sprintf('%s is not a number', $flight->nbrPax));
250
        }
251
252
        $result = $facture->addline(
253
            $flight->toString(),
254
            $pu_ht,
255
            $flight->nbrPax,
256
            $flightProduct->tva_tx,
257
            $localtax1_tx,
258
            $localtax2_tx,
259
            $flightProduct->id,
260
            $discount,
261
            $flight->date,
262
            $flight->date,
263
            0,
264
            0,
265
            '',
266
            'TTC',
267
            $pu_ttc,
268
            Facture::TYPE_STANDARD,
269
            -1,
270
            0,
271
            '',
272
            0,
273
            0,
274
            '',
275
            '',
276
            $flightProduct->label,
277
            [],
278
            100,
279
            '',
280
            0,
281
            $pu_ht_devise
282
        );
283
284
        if ($result <= 0) {
285
            throw new \InvalidArgumentException('Error during order line creation');
286
        }
287
    }
288
289
    /**
290
     * @param Bbcvols $flight
291
     */
292
    private function flagFlightAsBilled($flight)
293
    {
294
        $flight->is_facture = true;
295
        $flight->update($this->user);
296
    }
297
298
    /**
299
     * @return int
300
     */
301
    private function getBankAccount()
302
    {
303
        return $this->conf->BBC_DEFAULT_BANK_ACCOUNT;
304
    }
305
306
    /**
307
     * @param int $year
308
     * @param int $month
309
     *
310
     * @return int
311
     */
312
    private function generateBillDate($year, $month)
313
    {
314
        $day = cal_days_in_month(CAL_GREGORIAN, $month, $year);
315
        $date = (new DateTime())->setDate($year, $month, $day);
316
        return $date->getTimestamp();
317
    }
318
}