Passed
Branch develop (c36c8e)
by
unknown
39:14
created

Facturation::id()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2007-2008 Jeremie Ollivier <[email protected]>
3
 * Copyright (C) 2008-2010 Laurent Destailleur   <[email protected]>
4
 * Copyright (C) 2010      Juanjo Menent    <[email protected]>
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
21
22
23
/**
24
 * Class to manage invoices for pos module (cashdesk)
25
 */
26
class Facturation
27
{
28
    /**
29
     * Attributs "volatiles" : reinitialises apres chaque traitement d'un article
30
     * <p>Attributs "volatiles" : reinitialises apres chaque traitement d'un article</p>
31
     * int $id			=> 'rowid' du produit dans llx_product
32
     * string $ref		=> 'ref' du produit dans llx_product
33
     * int $qte			=> Quantite pour le produit en cours de traitement
34
     * int $stock		=> Stock theorique pour le produit en cours de traitement
35
     * int $remise_percent	=> Remise en pourcent sur le produit en cours
36
     * int $montant_remise	=> Remise en pourcent sur le produit en cours
37
     * int $prix		=> Prix HT du produit en cours
38
     * int $tva			=> 'rowid' du taux de tva dans llx_c_tva
39
     */
40
41
    /**
42
	 * @var int ID
43
	 */
44
	public $id;
45
46
    protected $ref;
47
    protected $qte;
48
    protected $stock;
49
    protected $remise_percent;
50
    protected $montant_remise;
51
    protected $prix;
52
    protected $tva;
53
54
    /**
55
     * Attributs persistants : utilises pour toute la duree de la vente (jusqu'a validation ou annulation)
56
     * string $num_facture	=> Numero de la facture (de la forme FAYYMM-XXXX)
57
     * string $mode_reglement	=> Mode de reglement (ESP, CB ou CHQ)
58
     * int $montant_encaisse	=> Montant encaisse en cas de reglement en especes
59
     * int $montant_rendu	=> Monnaie rendue en cas de reglement en especes
60
     * int $paiement_le		=> Date de paiement en cas de paiement differe
61
     *
62
     * int $prix_total_ht	=> Prix total hors taxes
63
     * int $montant_tva		=> Montant total de la TVA, tous taux confondus
64
     * int $prix_total_ttc	=> Prix total TTC
65
     */
66
    protected $num_facture;
67
    protected $mode_reglement;
68
    protected $montant_encaisse;
69
    protected $montant_rendu;
70
    protected $paiement_le;
71
72
    protected $prix_total_ht;
73
    protected $montant_tva;
74
    protected $prix_total_ttc;
75
76
77
    /**
78
     *	Constructor
79
     */
80
    public function __construct()
81
    {
82
        $this->raz();
83
        $this->razPers();
84
    }
85
86
87
    // Data processing methods
88
89
90
    /**
91
     *  Add a product into cart
92
     *
93
     *  @return	void
94
     */
95
    public function ajoutArticle()
96
    {
97
        global $conf, $db, $mysoc;
98
99
        $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
100
101
        $societe = new Societe($db);
102
        $societe->fetch($thirdpartyid);
103
104
        $product = new Product($db);
105
        $product->fetch($this->id);
106
107
108
        $vatrowid = $this->tva();
109
110
        $tmp = getTaxesFromId($vatrowid);
111
        $txtva = $tmp['rate'].(empty($tmp['code']) ? '' : ' ('.$tmp['code'].')');
112
        $vat_npr = $tmp['npr'];
113
114
        $localtaxarray = getLocalTaxesFromRate($vatrowid, 0, $societe, $mysoc, 1);
115
116
        // Clean vat code
117
        $reg = array();
118
        $vat_src_code = '';
119
        if (preg_match('/\((.*)\)/', $txtva, $reg))
120
        {
121
            $vat_src_code = $reg[1];
122
            $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
123
        }
124
125
        // Define part of HT, VAT, TTC
126
        $resultarray = calcul_price_total($this->qte, $this->prix(), $this->remisePercent(), $txtva, -1, -1, 0, 'HT', $vat_npr, $product->type, $mysoc, $localtaxarray);
127
128
        // Calculation of total HT without discount
129
        $total_ht = $resultarray[0];
130
        $total_vat = $resultarray[1];
131
        $total_ttc = $resultarray[2];
132
        $total_localtax1 = $resultarray[9];
133
        $total_localtax2 = $resultarray[10];
134
135
        // Calculation of the discount amount
136
        if ($this->remisePercent())
137
        {
138
            $remise_percent = $this->remisePercent();
139
        } else {
140
            $remise_percent = 0;
141
        }
142
        $montant_remise_ht = ($resultarray[6] - $resultarray[0]);
143
        $this->amountDiscount($montant_remise_ht);
144
145
        $newcartarray = $_SESSION['poscart'];
146
147
        $i = 0;
148
        if (!is_null($newcartarray) && !empty($newcartarray)) {
149
            $i = count($newcartarray);
150
        }
151
152
        $newcartarray[$i]['id'] = $i;
153
        $newcartarray[$i]['ref'] = $product->ref;
154
        $newcartarray[$i]['label'] = $product->label;
155
        $newcartarray[$i]['price'] = $product->price;
156
        $newcartarray[$i]['price_ttc'] = $product->price_ttc;
157
158
        if (!empty($conf->global->PRODUIT_MULTIPRICES))
159
        {
160
            if (isset($product->multiprices[$societe->price_level]))
161
            {
162
                $newcartarray[$i]['price'] = $product->multiprices[$societe->price_level];
163
                $newcartarray[$i]['price_ttc'] = $product->multiprices_ttc[$societe->price_level];
164
            }
165
        }
166
167
        $newcartarray[$i]['fk_article'] = $this->id;
168
        $newcartarray[$i]['qte'] = $this->qte();
169
        $newcartarray[$i]['fk_tva'] = $this->tva(); // Vat rowid
170
        $newcartarray[$i]['remise_percent'] = $remise_percent;
171
        $newcartarray[$i]['remise'] = price2num($montant_remise_ht);
172
        $newcartarray[$i]['total_ht'] = price2num($total_ht, 'MT');
173
        $newcartarray[$i]['total_ttc'] = price2num($total_ttc, 'MT');
174
        $newcartarray[$i]['total_vat'] = price2num($total_vat, 'MT');
175
        $newcartarray[$i]['total_localtax1'] = price2num($total_localtax1, 'MT');
176
        $newcartarray[$i]['total_localtax2'] = price2num($total_localtax2, 'MT');
177
        $_SESSION['poscart'] = $newcartarray;
178
179
        $this->raz();
180
    }
181
182
    /**
183
     *  Remove a product from panel
184
     *
185
     *  @param  int		$aArticle	Id of line into cart to remove
186
     *  @return	void
187
     */
188
    public function supprArticle($aArticle)
189
    {
190
        $poscart = $_SESSION['poscart'];
191
192
        $j = 0;
193
        $newposcart = array();
194
        foreach ($poscart as $key => $val)
195
        {
196
            if ($poscart[$key]['id'] != $aArticle)
197
            {
198
                $newposcart[$j] = $poscart[$key];
199
                $newposcart[$j]['id'] = $j;
200
                $j++;
201
            }
202
        }
203
        unset($poscart);
204
        //var_dump($poscart);exit;
205
        $_SESSION['poscart'] = $newposcart;
206
    }
207
208
    /**
209
     * Calculation of total HT, total TTC and VAT amounts
210
     *
211
     * @return	int		Total
212
     */
213
    public function calculTotaux()
214
    {
215
        global $db;
216
217
        $total_ht = 0;
218
        $total_ttc = 0;
219
        $total_vat = 0;
220
        $total_localtax1 = 0;
221
        $total_localtax2 = 0;
222
223
        $tab = (!empty($_SESSION['poscart']) ? $_SESSION['poscart'] : array());
224
225
        $tab_size = count($tab);
226
        for ($i = 0; $i < $tab_size; $i++)
227
        {
228
            // Total HT
229
            $remise = $tab[$i]['remise'];
230
            $total_ht += ($tab[$i]['total_ht']);
231
            $total_vat += ($tab[$i]['total_vat']);
232
            $total_ttc += ($tab[$i]['total_ttc']);
233
            $total_localtax1 += ($tab[$i]['total_localtax1']);
234
            $total_localtax2 += ($tab[$i]['total_localtax2']);
235
        }
236
237
        $this->prix_total_ttc = $total_ttc;
238
        $this->prix_total_ht = $total_ht;
239
        $this->prix_total_vat = $total_vat;
240
        $this->prix_total_localtax1 = $total_localtax1;
241
        $this->prix_total_localtax2 = $total_localtax2;
242
243
        $this->montant_tva = $total_ttc - $total_ht;
244
        //print 'total: '.$this->prix_total_ttc; exit;
245
    }
246
247
    /**
248
     * Reset attributes
249
     *
250
     * @return	void
251
     */
252
    public function raz()
253
    {
254
        $this->id('RESET');
255
        $this->ref('RESET');
256
        $this->qte('RESET');
257
        $this->stock('RESET');
258
        $this->remisePercent('RESET');
259
        $this->amountDiscount('RESET');
260
        $this->prix('RESET');
261
        $this->tva('RESET');
262
    }
263
264
    /**
265
     * Resetting persistent attributes
266
     *
267
     *  @return	void
268
     */
269
    private function razPers()
270
    {
271
        $this->numInvoice('RESET');
272
        $this->getSetPaymentMode('RESET');
273
        $this->amountCollected('RESET');
274
        $this->amountReturned('RESET');
275
        $this->paiementLe('RESET');
276
277
        $this->amountWithoutTax('RESET');
278
        $this->amountVat('RESET');
279
        $this->amountWithTax('RESET');
280
    }
281
282
283
    // Methods for modifying protected attributes
284
285
    /**
286
     * Getter for id
287
     *
288
     * @param	int		$aId	Id
289
     * @return  int             Id
290
     */
291
    public function id($aId = null)
292
    {
293
294
        if (!$aId)
295
        {
296
            return $this->id;
297
        } elseif ($aId == 'RESET')
298
        {
299
            $this->id = null;
300
        } else {
301
            $this->id = $aId;
302
        }
303
    }
304
305
    /**
306
     * Getter for ref
307
     *
308
     * @param	string	$aRef	Ref
309
     * @return	string			Ref
310
     */
311
    public function ref($aRef = null)
312
    {
313
314
        if (is_null($aRef))
315
        {
316
            return $this->ref;
317
        } elseif ($aRef == 'RESET')
318
        {
319
            $this->ref = null;
320
        } else {
321
            $this->ref = $aRef;
322
        }
323
    }
324
325
    /**
326
     * Getter for qte
327
     *
328
     * @param	int		$aQte		Qty
329
     * @return	int					Qty
330
     */
331
    public function qte($aQte = null)
332
    {
333
        if (is_null($aQte))
334
        {
335
            return $this->qte;
336
        } elseif ($aQte == 'RESET')
337
        {
338
            $this->qte = null;
339
        } else {
340
            $this->qte = $aQte;
341
        }
342
    }
343
344
    /**
345
     * Getter for stock
346
     *
347
     * @param   string	$aStock		Stock
348
     * @return	string				Stock
349
     */
350
    public function stock($aStock = null)
351
    {
352
353
        if (is_null($aStock))
354
        {
355
            return $this->stock;
356
        } elseif ($aStock == 'RESET')
357
        {
358
            $this->stock = null;
359
        } else {
360
            $this->stock = $aStock;
361
        }
362
    }
363
364
    /**
365
     * Getter for remise_percent
366
     *
367
     * @param	string	$aRemisePercent		Discount
368
     * @return	string						Discount
369
     */
370
    public function remisePercent($aRemisePercent = null)
371
    {
372
373
        if (is_null($aRemisePercent))
374
        {
375
            return $this->remise_percent;
376
        } elseif ($aRemisePercent == 'RESET')
377
        {
378
            $this->remise_percent = null;
379
        } else {
380
            $this->remise_percent = $aRemisePercent;
381
        }
382
    }
383
384
    /**
385
     * Getter for montant_remise
386
     *
387
     * @param	int		$aMontantRemise		Amount
388
     * @return	string						Amount
389
     */
390
    public function amountDiscount($aMontantRemise = null)
391
    {
392
393
        if (is_null($aMontantRemise)) {
394
            return $this->montant_remise;
395
        } elseif ($aMontantRemise == 'RESET') {
396
            $this->montant_remise = null;
397
        } else {
398
            $this->montant_remise = $aMontantRemise;
399
        }
400
    }
401
402
    /**
403
     * Getter for prix
404
     *
405
     * @param	int		$aPrix		Price
406
     * @return	string				Stock
407
     */
408
    public function prix($aPrix = null)
409
    {
410
411
        if (is_null($aPrix)) {
412
            return $this->prix;
413
        } elseif ($aPrix == 'RESET') {
414
            $this->prix = null;
415
        } else {
416
            $this->prix = $aPrix;
417
        }
418
    }
419
420
    /**
421
     * Getter for tva
422
     *
423
     * @param	int		$aTva		Vat
424
     * @return	int					Vat
425
     */
426
    public function tva($aTva = null)
427
    {
428
        if (is_null($aTva)) {
429
            return $this->tva;
430
        } elseif ($aTva == 'RESET') {
431
            $this->tva = null;
432
        } else {
433
            $this->tva = $aTva;
434
        }
435
    }
436
437
    /**
438
     * Get num invoice
439
     *
440
     * @param string	$aNumFacture		Invoice ref
441
     * @return	string						Invoice ref
442
     */
443
    public function numInvoice($aNumFacture = null)
444
    {
445
        if (is_null($aNumFacture)) {
446
            return $this->num_facture;
447
        } elseif ($aNumFacture == 'RESET') {
448
            $this->num_facture = null;
449
        } else {
450
            $this->num_facture = $aNumFacture;
451
        }
452
    }
453
454
    /**
455
     * Get payment mode
456
     *
457
     * @param	int		$aModeReglement		Payment mode
458
     * @return	int							Payment mode
459
     */
460
    public function getSetPaymentMode($aModeReglement = null)
461
    {
462
463
        if (is_null($aModeReglement)) {
464
            return $this->mode_reglement;
465
        } elseif ($aModeReglement == 'RESET') {
466
            $this->mode_reglement = null;
467
        } else {
468
            $this->mode_reglement = $aModeReglement;
469
        }
470
    }
471
472
    /**
473
     * Get amount
474
     *
475
     * @param	int		$aMontantEncaisse		Amount
476
     * @return	int								Amount
477
     */
478
    public function amountCollected($aMontantEncaisse = null)
479
    {
480
481
        if (is_null($aMontantEncaisse)) {
482
            return $this->montant_encaisse;
483
        } elseif ($aMontantEncaisse == 'RESET') {
484
            $this->montant_encaisse = null;
485
        } else {
486
            $this->montant_encaisse = $aMontantEncaisse;
487
        }
488
    }
489
490
    /**
491
     * Get amount
492
     *
493
     * @param	int			$aMontantRendu		Amount
494
     * @return	int								Amount
495
     */
496
    public function amountReturned($aMontantRendu = null)
497
    {
498
499
        if (is_null($aMontantRendu)) {
500
            return $this->montant_rendu;
501
        } elseif ($aMontantRendu == 'RESET') {
502
            $this->montant_rendu = null;
503
        } else {
504
            $this->montant_rendu = $aMontantRendu;
505
        }
506
    }
507
508
    /**
509
     * Get payment date
510
     *
511
     * @param	integer		$aPaiementLe		Date
512
     * @return	integer							Date
513
     */
514
    public function paiementLe($aPaiementLe = null)
515
    {
516
        if (is_null($aPaiementLe)) {
517
            return $this->paiement_le;
518
        } elseif ($aPaiementLe == 'RESET') {
519
            $this->paiement_le = null;
520
        } else {
521
            $this->paiement_le = $aPaiementLe;
522
        }
523
    }
524
525
    /**
526
     * Get total HT
527
     *
528
     * @param	int		$aTotalHt		Total amount
529
     * @return	int						Total amount
530
     */
531
    public function amountWithoutTax($aTotalHt = null)
532
    {
533
        if (is_null($aTotalHt)) {
534
            return $this->prix_total_ht;
535
        } elseif ($aTotalHt == 'RESET') {
536
            $this->prix_total_ht = null;
537
        } else {
538
            $this->prix_total_ht = $aTotalHt;
539
        }
540
    }
541
542
    /**
543
     * Get amount vat
544
     *
545
     * @param	int		$aMontantTva	Amount vat
546
     * @return	int						Amount vat
547
     */
548
    public function amountVat($aMontantTva = null)
549
    {
550
        if (is_null($aMontantTva)) {
551
            return $this->montant_tva;
552
        } elseif ($aMontantTva == 'RESET') {
553
            $this->montant_tva = null;
554
        } else {
555
            $this->montant_tva = $aMontantTva;
556
        }
557
    }
558
559
    /**
560
     * Get total TTC
561
     *
562
     * @param	int		$aTotalTtc		Amount ttc
563
     * @return	int						Amount ttc
564
     */
565
    public function amountWithTax($aTotalTtc = null)
566
    {
567
        if (is_null($aTotalTtc))
568
        {
569
            return $this->prix_total_ttc;
570
        } elseif ($aTotalTtc == 'RESET')
571
        {
572
            $this->prix_total_ttc = null;
573
        } else {
574
            $this->prix_total_ttc = $aTotalTtc;
575
        }
576
    }
577
}
578