Completed
Push — master ( 81d11b...3691ca )
by Laurent
01:59
created

generateBillDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 3
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
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
    public function __construct($db, $conf, $user, $langs)
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 = $this->getCustomer()->id;
58
        $object->type = $command->getBillType();
59
        $object->number = "provisoire";
60
        $object->date = $flight->date;
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);
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
    private function getProduct()
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
     * @return Fournisseur
106
     *
107
     * @throws CustomerNotFoundException
108
     */
109
    private function getCustomer()
110
    {
111
        $customer = new Fournisseur($this->db);
112
113
        if ($customer->fetch($this->conf->BBC_FLIGHT_DEFAULT_CUSTOMER) <= 0) {
114
            throw new CustomerNotFoundException();
115
        }
116
117
        return $customer;
118
    }
119
120
    /**
121
     * @param int $flightId
122
     *
123
     * @return Bbcvols
124
     *
125
     * @throws FlightNotFoundException
126
     */
127
    private function getFlight($flightId)
128
    {
129
        $flight = new Bbcvols($this->db);
130
131
        if ($flight->fetch($flightId) <= 0) {
132
            throw new FlightNotFoundException();
133
        }
134
135
        return $flight;
136
    }
137
138
    /**
139
     * @param Facture $object
140
     * @param Bbcvols $flight
141
     *
142
     * @throws ContactNotAddedException
143
     */
144
    private function addContacts($object, $flight)
145
    {
146
        $this->addContactOnBill($object, $flight->fk_pilot, 'BBC_PILOT');
147
        $this->addContactOnBill($object, $flight->fk_receiver, 'BBC_RECEIVER');
148
        $this->addContactOnBill($object, $flight->fk_organisateur, 'BBC_ORGANISATOR');
149
    }
150
151
    /**
152
     * @param Facture $bill
153
     * @param int     $contactId
154
     * @param string  $contactType
155
     *
156
     * @throws ContactNotAddedException
157
     */
158
    private function addContactOnBill(Facture $bill, $contactId, $contactType)
159
    {
160
        if ($bill->add_contact($contactId, $contactType, 'internal') < 0) {
161
            throw new ContactNotAddedException($contactType);
162
        }
163
    }
164
165
    /**
166
     * @param Facture $object
167
     * @param Bbcvols $flight
168
     */
169
    private function addLinks($object, $flight)
170
    {
171
        $object->add_object_linked('flightlog_bbcvols', $flight->getId());
172
    }
173
174
    /**
175
     * @param Facture $object
176
     * @param int     $id
177
     */
178
    private function validates($object, $id)
179
    {
180
        $object->fetch($id);
181
        $object->validate($this->user);
182
    }
183
184
    /**
185
     * @return int
186
     */
187
    private function isReferenceHidden()
188
    {
189
        return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
190
    }
191
192
    /**
193
     * @return int
194
     */
195
    private function isDescriptionHidden()
196
    {
197
        return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
198
    }
199
200
    /**
201
     * @return int
202
     */
203
    private function isDetailHidden()
204
    {
205
        return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
206
    }
207
208
    /**
209
     * @param CreateFlightBillCommand $command
210
     * @param Facture                 $object
211
     * @param int                     $id
212
     */
213
    private function generateBillDocument(CreateFlightBillCommand $command, $object, $id)
214
    {
215
        $object->fetch($id);
216
        $object->generateDocument(
217
            $command->getModelDocument(),
218
            $this->langs,
219
            $this->isDetailHidden(),
220
            $this->isDescriptionHidden(),
221
            $this->isReferenceHidden()
222
        );
223
    }
224
225
    /**
226
     * @param $flightProduct
227
     * @param $flight
228
     *
229
     * @return float|int
230
     */
231
    private function computeDiscounts($flightProduct, $flight)
232
    {
233
        return ($flightProduct->price_ttc - ($flight->cost / $flight->nbrPax)) * 100 / $flightProduct->price_ttc;
234
    }
235
236
    /**
237
     * @param Facture $object
238
     * @param Product $flightProduct
239
     * @param Bbcvols $flight
240
     */
241
    private function addOrderLine($object, $flightProduct, $flight)
242
    {
243
        $localtax1_tx = get_localtax(0, 1, $object->thirdparty);
244
        $localtax2_tx = get_localtax(0, 2, $object->thirdparty);
245
246
        $pu_ht = price2num($flightProduct->price, 'MU');
247
        $pu_ttc = price2num($flightProduct->price_ttc, 'MU');
248
        $pu_ht_devise = price2num($flightProduct->price, 'MU');
249
250
        $discount = $this->computeDiscounts($flightProduct, $flight);
251
252
        $result = $object->addline(
253
            $flightProduct->description,
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
}