Completed
Push — feature/fixing_cost ( ef981e )
by Laurent
01:53
created

fetchCustomerFromFlight()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 21
rs 9.584
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A CreateFlightBillCommandHandler::addContactOnBill() 0 6 2
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)
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...
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 = $command->getCustomerId();
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()
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...
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 int $flightId
106
     *
107
     * @return Bbcvols
108
     *
109
     * @throws FlightNotFoundException
110
     */
111
    private function getFlight($flightId)
112
    {
113
        $flight = new Bbcvols($this->db);
114
115
        if ($flight->fetch($flightId) <= 0) {
116
            throw new FlightNotFoundException();
117
        }
118
119
        return $flight;
120
    }
121
122
    /**
123
     * @param Facture $object
124
     * @param Bbcvols $flight
125
     *
126
     * @throws ContactNotAddedException
127
     */
128
    private function addContacts($object, $flight)
129
    {
130
        $this->addContactOnBill($object, $flight->fk_pilot, 'BBC_PILOT');
131
        $this->addContactOnBill($object, $flight->fk_receiver, 'BBC_RECEIVER');
132
        $this->addContactOnBill($object, $flight->fk_organisateur, 'BBC_ORGANISATOR');
133
    }
134
135
    /**
136
     * @param Facture $bill
137
     * @param int     $contactId
138
     * @param string  $contactType
139
     *
140
     * @throws ContactNotAddedException
141
     */
142
    private function addContactOnBill(Facture $bill, $contactId, $contactType)
143
    {
144
        if ($bill->add_contact($contactId, $contactType, 'internal') < 0) {
145
            throw new ContactNotAddedException($contactType);
146
        }
147
    }
148
149
    /**
150
     * @param Facture $object
151
     * @param Bbcvols $flight
152
     */
153
    private function addLinks($object, $flight)
154
    {
155
        $object->add_object_linked('flightlog_bbcvols', $flight->getId());
156
    }
157
158
    /**
159
     * @param Facture $object
160
     * @param int     $id
161
     */
162
    private function validates($object, $id)
163
    {
164
        $object->fetch($id);
165
        $object->validate($this->user);
166
    }
167
168
    /**
169
     * @return int
170
     */
171
    private function isReferenceHidden()
172
    {
173
        return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
174
    }
175
176
    /**
177
     * @return int
178
     */
179
    private function isDescriptionHidden()
180
    {
181
        return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
182
    }
183
184
    /**
185
     * @return int
186
     */
187
    private function isDetailHidden()
188
    {
189
        return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
190
    }
191
192
    /**
193
     * @param CreateFlightBillCommand $command
194
     * @param Facture                 $object
195
     * @param int                     $id
196
     */
197
    private function generateBillDocument(CreateFlightBillCommand $command, $object, $id)
198
    {
199
        $object->fetch($id);
200
        $object->generateDocument(
201
            $command->getModelDocument(),
202
            $this->langs,
203
            $this->isDetailHidden(),
204
            $this->isDescriptionHidden(),
205
            $this->isReferenceHidden()
206
        );
207
    }
208
209
    /**
210
     * @param $flightProduct
211
     * @param $flight
212
     *
213
     * @return float|int
214
     */
215
    private function computeDiscounts($flightProduct, $flight)
216
    {
217
        return ($flightProduct->price_ttc - ($flight->cost / $flight->nbrPax)) * 100 / $flightProduct->price_ttc;
218
    }
219
220
    /**
221
     * @param Facture $object
222
     * @param Product $flightProduct
223
     * @param Bbcvols $flight
224
     */
225
    private function addOrderLine($object, $flightProduct, $flight, $nbrPax)
226
    {
227
        $localtax1_tx = get_localtax(0, 1, $object->thirdparty);
228
        $localtax2_tx = get_localtax(0, 2, $object->thirdparty);
229
230
        $pu_ht = price2num($flightProduct->price, 'MU');
231
        $pu_ttc = price2num($flightProduct->price_ttc, 'MU');
232
        $pu_ht_devise = price2num($flightProduct->price, 'MU');
233
234
        $discount = $this->computeDiscounts($flightProduct, $flight);
235
236
        $result = $object->addline(
237
            $flightProduct->description,
238
            $pu_ht,
239
            $nbrPax,
240
            $flightProduct->tva_tx,
241
            $localtax1_tx,
242
            $localtax2_tx,
243
            $flightProduct->id,
244
            $discount,
245
            $flight->date,
246
            $flight->date,
247
            0,
248
            0,
249
            '',
250
            'TTC',
251
            $pu_ttc,
252
            Facture::TYPE_STANDARD,
253
            -1,
254
            0,
255
            '',
256
            0,
257
            0,
258
            '',
259
            '',
260
            $flightProduct->label,
261
            [],
262
            100,
263
            '',
264
            0,
265
            $pu_ht_devise
266
        );
267
268
        if ($result <= 0) {
269
            throw new \InvalidArgumentException('Error during order line creation');
270
        }
271
    }
272
273
    /**
274
     * @param Bbcvols $flight
275
     */
276
    private function flagFlightAsBilled($flight)
277
    {
278
        $flight->is_facture = true;
279
        $flight->update($this->user);
280
    }
281
}