/
fournisseur.commande.class.php
3559 lines (3127 loc) · 128 KB
/
fournisseur.commande.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerke@telenet.be>
* Copyright (C) 2010-2014 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2010-2018 Philippe Grand <philippe.grand@atoo-net.com>
* Copyright (C) 2012-2015 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
* Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
* Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/fourn/class/fournisseur.commande.class.php
* \ingroup fournisseur,commande
* \brief File of class to manage suppliers orders
*/
include_once DOL_DOCUMENT_ROOT.'/core/class/commonorder.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
if (! empty($conf->productbatch->enabled)) require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
/**
* Class to manage predefined suppliers products
*/
class CommandeFournisseur extends CommonOrder
{
/**
* @var string ID to identify managed object
*/
public $element='order_supplier';
/**
* @var string Name of table without prefix where object is stored
*/
public $table_element='commande_fournisseur';
/**
* @var int Name of subtable line
*/
public $table_element_line = 'commande_fournisseurdet';
/**
* @var int Field with ID of parent key if this field has a parent
*/
public $fk_element = 'fk_commande';
public $picto='order';
/**
* 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
* @var int
*/
public $ismultientitymanaged = 1;
/**
* 0=Default, 1=View may be restricted to sales representative only if no permission to see all or to company of external user if external user
* @var integer
*/
public $restrictiononfksoc = 1;
/**
* {@inheritdoc}
*/
protected $table_ref_field = 'ref';
/**
* @var int ID
*/
public $id;
/**
* Supplier order reference
* @var string
*/
public $ref;
public $ref_supplier;
public $brouillon;
public $statut; // 0=Draft -> 1=Validated -> 2=Approved -> 3=Ordered/Process runing -> 4=Received partially -> 5=Received totally -> (reopen) 4=Received partially
// -> 7=Canceled/Never received -> (reopen) 3=Process runing
// -> 6=Canceled -> (reopen) 2=Approved
// -> 9=Refused -> (reopen) 1=Validated
// Note: billed or not is on another field "billed"
public $statuts; // List of status
public $socid;
public $fourn_id;
public $date;
public $date_valid;
public $date_approve;
public $date_approve2; // Used when SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED is set
public $date_commande;
/**
* Delivery date
*/
public $date_livraison;
public $total_ht;
public $total_tva;
public $total_localtax1; // Total Local tax 1
public $total_localtax2; // Total Local tax 2
public $total_ttc;
public $source;
/**
* @deprecated
* @see note_private, note_public
*/
public $note;
public $note_private;
public $note_public;
public $model_pdf;
/**
* @var int ID
*/
public $fk_project;
public $cond_reglement_id;
public $cond_reglement_code;
/**
* @var int ID
*/
public $fk_account;
public $mode_reglement_id;
public $mode_reglement_code;
public $user_author_id;
public $user_valid_id;
public $user_approve_id;
public $user_approve_id2; // Used when SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED is set
//Incoterms
public $fk_incoterms;
public $location_incoterms;
public $libelle_incoterms; //Used into tooltip
public $extraparams=array();
/**
* @var CommandeFournisseurLigne[]
*/
public $lines = array();
//Add for supplier_proposal
public $origin;
public $origin_id;
public $linked_objects=array();
// Multicurrency
/**
* @var int ID
*/
public $fk_multicurrency;
public $multicurrency_code;
public $multicurrency_tx;
public $multicurrency_total_ht;
public $multicurrency_total_tva;
public $multicurrency_total_ttc;
/**
* Draft status
*/
const STATUS_DRAFT = 0;
/**
* Validated status
*/
const STATUS_VALIDATED = 1;
/**
* Accepted
*/
const STATUS_ACCEPTED = 2;
/**
* Order sent, shipment on process
*/
const STATUS_ORDERSENT = 3;
/**
* Received partially
*/
const STATUS_RECEIVED_PARTIALLY = 4;
/**
* Received completely
*/
const STATUS_RECEIVED_COMPLETELY = 5;
/**
* Order canceled
*/
const STATUS_CANCELED = 6;
/**
* Order canceled/never received
*/
const STATUS_CANCELED_AFTER_ORDER = 7;
/**
* Refused
*/
const STATUS_REFUSED = 9;
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db)
{
$this->db = $db;
$this->products = array();
}
/**
* Get object and lines from database
*
* @param int $id Id of order to load
* @param string $ref Ref of object
* @return int >0 if OK, <0 if KO, 0 if not found
*/
public function fetch($id, $ref='')
{
global $conf;
// Check parameters
if (empty($id) && empty($ref)) return -1;
$sql = "SELECT c.rowid, c.entity, c.ref, ref_supplier, c.fk_soc, c.fk_statut, c.amount_ht, c.total_ht, c.total_ttc, c.tva as total_vat,";
$sql.= " c.localtax1, c.localtax2, ";
$sql.= " c.date_creation, c.date_valid, c.date_approve, c.date_approve2,";
$sql.= " c.fk_user_author, c.fk_user_valid, c.fk_user_approve, c.fk_user_approve2,";
$sql.= " c.date_commande as date_commande, c.date_livraison as date_livraison, c.fk_cond_reglement, c.fk_mode_reglement, c.fk_projet as fk_project, c.remise_percent, c.source, c.fk_input_method,";
$sql.= " c.fk_account,";
$sql.= " c.note_private, c.note_public, c.model_pdf, c.extraparams, c.billed,";
$sql.= " c.fk_multicurrency, c.multicurrency_code, c.multicurrency_tx, c.multicurrency_total_ht, c.multicurrency_total_tva, c.multicurrency_total_ttc,";
$sql.= " cm.libelle as methode_commande,";
$sql.= " cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle,";
$sql.= " p.code as mode_reglement_code, p.libelle as mode_reglement_libelle";
$sql.= ', c.fk_incoterms, c.location_incoterms';
$sql.= ', i.libelle as libelle_incoterms';
$sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as c";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_payment_term as cr ON c.fk_cond_reglement = cr.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as p ON c.fk_mode_reglement = p.id";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_input_method as cm ON cm.rowid = c.fk_input_method";
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON c.fk_incoterms = i.rowid';
$sql.= " WHERE c.entity = ".$conf->entity;
if ($ref) $sql.= " AND c.ref='".$this->db->escape($ref)."'";
else $sql.= " AND c.rowid=".$id;
dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$obj = $this->db->fetch_object($resql);
if (! $obj)
{
$this->error='Bill with id '.$id.' not found';
dol_syslog(get_class($this).'::fetch '.$this->error);
return 0;
}
$this->id = $obj->rowid;
$this->entity = $obj->entity;
$this->ref = $obj->ref;
$this->ref_supplier = $obj->ref_supplier;
$this->socid = $obj->fk_soc;
$this->fourn_id = $obj->fk_soc;
$this->statut = $obj->fk_statut;
$this->billed = $obj->billed;
$this->user_author_id = $obj->fk_user_author;
$this->user_valid_id = $obj->fk_user_valid;
$this->user_approve_id = $obj->fk_user_approve;
$this->user_approve_id2 = $obj->fk_user_approve2;
$this->total_ht = $obj->total_ht;
$this->total_tva = $obj->total_vat;
$this->total_localtax1 = $obj->localtax1;
$this->total_localtax2 = $obj->localtax2;
$this->total_ttc = $obj->total_ttc;
$this->date = $this->db->jdate($obj->date_creation);
$this->date_valid = $this->db->jdate($obj->date_valid);
$this->date_approve = $this->db->jdate($obj->date_approve);
$this->date_approve2 = $this->db->jdate($obj->date_approve2);
$this->date_commande = $this->db->jdate($obj->date_commande); // date we make the order to supplier
$this->date_livraison = $this->db->jdate($obj->date_livraison);
$this->remise_percent = $obj->remise_percent;
$this->methode_commande_id = $obj->fk_input_method;
$this->methode_commande = $obj->methode_commande;
$this->source = $obj->source;
$this->fk_project = $obj->fk_project;
$this->cond_reglement_id = $obj->fk_cond_reglement;
$this->cond_reglement_code = $obj->cond_reglement_code;
$this->cond_reglement = $obj->cond_reglement_libelle;
$this->cond_reglement_doc = $obj->cond_reglement_libelle;
$this->fk_account = $obj->fk_account;
$this->mode_reglement_id = $obj->fk_mode_reglement;
$this->mode_reglement_code = $obj->mode_reglement_code;
$this->mode_reglement = $obj->mode_reglement_libelle;
$this->note = $obj->note_private; // deprecated
$this->note_private = $obj->note_private;
$this->note_public = $obj->note_public;
$this->modelpdf = $obj->model_pdf;
//Incoterms
$this->fk_incoterms = $obj->fk_incoterms;
$this->location_incoterms = $obj->location_incoterms;
$this->libelle_incoterms = $obj->libelle_incoterms;
// Multicurrency
$this->fk_multicurrency = $obj->fk_multicurrency;
$this->multicurrency_code = $obj->multicurrency_code;
$this->multicurrency_tx = $obj->multicurrency_tx;
$this->multicurrency_total_ht = $obj->multicurrency_total_ht;
$this->multicurrency_total_tva = $obj->multicurrency_total_tva;
$this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
$this->extraparams = (array) json_decode($obj->extraparams, true);
$this->db->free($resql);
// Retreive all extrafield
// fetch optionals attributes and labels
$this->fetch_optionals();
if ($this->statut == 0) $this->brouillon = 1;
/*
* Lines
*/
$result=$this->fetch_lines();
if ($result < 0)
{
return -1;
}
else
{
return 1;
}
}
else
{
$this->error=$this->db->error()." sql=".$sql;
return -1;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
/**
* Load array lines
*
* @param int $only_product Return only physical products
* @return int <0 if KO, >0 if OK
*/
function fetch_lines($only_product=0)
{
// phpcs:enable
//$result=$this->fetch_lines();
$this->lines=array();
$sql = "SELECT l.rowid, l.ref as ref_supplier, l.fk_product, l.product_type, l.label, l.description, l.qty,";
$sql.= " l.vat_src_code, l.tva_tx, l.remise_percent, l.subprice,";
$sql.= " l.localtax1_tx, l. localtax2_tx, l.localtax1_type, l. localtax2_type, l.total_localtax1, l.total_localtax2,";
$sql.= " l.total_ht, l.total_tva, l.total_ttc, l.special_code, l.fk_parent_line, l.rang,";
$sql.= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc,";
$sql.= " l.fk_unit,";
$sql.= " l.date_start, l.date_end,";
$sql.= ' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc';
$sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as l";
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
$sql.= " WHERE l.fk_commande = ".$this->id;
if ($only_product) $sql .= ' AND p.fk_product_type = 0';
$sql.= " ORDER BY l.rang, l.rowid";
//print $sql;
dol_syslog(get_class($this)."::fetch get lines", LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
$num = $this->db->num_rows($result);
$i = 0;
while ($i < $num)
{
$objp = $this->db->fetch_object($result);
$line = new CommandeFournisseurLigne($this->db);
$line->id = $objp->rowid;
$line->desc = $objp->description;
$line->description = $objp->description;
$line->qty = $objp->qty;
$line->tva_tx = $objp->tva_tx;
$line->localtax1_tx = $objp->localtax1_tx;
$line->localtax2_tx = $objp->localtax2_tx;
$line->localtax1_type = $objp->localtax1_type;
$line->localtax2_type = $objp->localtax2_type;
$line->subprice = $objp->subprice;
$line->pu_ht = $objp->subprice;
$line->remise_percent = $objp->remise_percent;
$line->vat_src_code = $objp->vat_src_code;
$line->total_ht = $objp->total_ht;
$line->total_tva = $objp->total_tva;
$line->total_localtax1 = $objp->total_localtax1;
$line->total_localtax2 = $objp->total_localtax2;
$line->total_ttc = $objp->total_ttc;
$line->product_type = $objp->product_type;
$line->fk_product = $objp->fk_product;
$line->libelle = $objp->product_label;
$line->product_label = $objp->product_label;
$line->product_desc = $objp->product_desc;
$line->ref = $objp->product_ref; // Ref of product
$line->product_ref = $objp->product_ref; // Ref of product
$line->ref_fourn = $objp->ref_supplier; // The supplier ref of price when product was added. May have change since
$line->ref_supplier = $objp->ref_supplier; // The supplier ref of price when product was added. May have change since
$line->date_start = $this->db->jdate($objp->date_start);
$line->date_end = $this->db->jdate($objp->date_end);
$line->fk_unit = $objp->fk_unit;
// Multicurrency
$line->fk_multicurrency = $objp->fk_multicurrency;
$line->multicurrency_code = $objp->multicurrency_code;
$line->multicurrency_subprice = $objp->multicurrency_subprice;
$line->multicurrency_total_ht = $objp->multicurrency_total_ht;
$line->multicurrency_total_tva = $objp->multicurrency_total_tva;
$line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
$line->special_code = $objp->special_code;
$line->fk_parent_line = $objp->fk_parent_line;
$line->rang = $objp->rang;
// Retreive all extrafield
// fetch optionals attributes and labels
$line->fetch_optionals();
$this->lines[$i] = $line;
$i++;
}
$this->db->free($result);
return $num;
}
else
{
$this->error=$this->db->error()." sql=".$sql;
return -1;
}
}
/**
* Validate an order
*
* @param User $user Validator User
* @param int $idwarehouse Id of warehouse to use for stock decrease
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers
* @return int <0 if KO, >0 if OK
*/
public function valid($user,$idwarehouse=0,$notrigger=0)
{
global $langs,$conf;
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$error=0;
dol_syslog(get_class($this)."::valid");
$result = 0;
if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->fournisseur->commande->creer))
|| (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->fournisseur->supplier_order_advance->validate)))
{
$this->db->begin();
// Definition of supplier order numbering model name
$soc = new Societe($this->db);
$soc->fetch($this->fourn_id);
// Check if object has a temporary ref
if (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref)) // empty should not happened, but when it occurs, the test save life
{
$num = $this->getNextNumRef($soc);
}
else
{
$num = $this->ref;
}
$this->newref = $num;
$sql = 'UPDATE '.MAIN_DB_PREFIX."commande_fournisseur";
$sql.= " SET ref='".$this->db->escape($num)."',";
$sql.= " fk_statut = ".self::STATUS_VALIDATED.",";
$sql.= " date_valid='".$this->db->idate(dol_now())."',";
$sql.= " fk_user_valid = ".$user->id;
$sql.= " WHERE rowid = ".$this->id;
$sql.= " AND fk_statut = ".self::STATUS_DRAFT;
$resql=$this->db->query($sql);
if (! $resql)
{
dol_print_error($this->db);
$error++;
}
if (! $error && ! $notrigger)
{
// Call trigger
$result=$this->call_trigger('ORDER_SUPPLIER_VALIDATE',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->oldref = $this->ref;
// Rename directory if dir was a temporary ref
if (preg_match('/^[\(]?PROV/i', $this->ref))
{
// We rename directory ($this->ref = ancienne ref, $num = nouvelle ref)
// in order not to lose the attached files
$oldref = dol_sanitizeFileName($this->ref);
$newref = dol_sanitizeFileName($num);
$dirsource = $conf->fournisseur->commande->dir_output.'/'.$oldref;
$dirdest = $conf->fournisseur->commande->dir_output.'/'.$newref;
if (file_exists($dirsource))
{
dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
if (@rename($dirsource, $dirdest))
{
dol_syslog("Rename ok");
// Rename docs starting with $oldref with $newref
$listoffiles=dol_dir_list($conf->fournisseur->commande->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref,'/'));
foreach($listoffiles as $fileentry)
{
$dirsource=$fileentry['name'];
$dirdest=preg_replace('/^'.preg_quote($oldref,'/').'/',$newref, $dirsource);
$dirsource=$fileentry['path'].'/'.$dirsource;
$dirdest=$fileentry['path'].'/'.$dirdest;
@rename($dirsource, $dirdest);
}
}
}
}
}
if (! $error)
{
$result = 1;
$this->statut = self::STATUS_VALIDATED;
$this->ref = $num;
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -1;
}
}
else
{
$this->error='NotAuthorized';
dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
return -1;
}
}
/**
* Return label of the status of object
*
* @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=short label + picto
* @return string Label
*/
public function getLibStatut($mode=0)
{
return $this->LibStatut($this->statut,$mode,$this->billed);
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
/**
* Return label of a status
*
* @param int $statut Id statut
* @param int $mode 0=Long label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Long label, 5=Short label + Picto
* @param int $billed 1=Billed
* @return string Label of status
*/
function LibStatut($statut,$mode=0,$billed=0)
{
// phpcs:enable
global $conf, $langs;
if (empty($this->statuts) || empty($this->statutshort))
{
$langs->load('orders');
$this->statuts[0] = 'StatusOrderDraft';
$this->statuts[1] = 'StatusOrderValidated';
$this->statuts[2] = 'StatusOrderApproved';
if (empty($conf->global->SUPPLIER_ORDER_USE_DISPATCH_STATUS)) $this->statuts[3] = 'StatusOrderOnProcess';
else $this->statuts[3] = 'StatusOrderOnProcessWithValidation';
$this->statuts[4] = 'StatusOrderReceivedPartially';
$this->statuts[5] = 'StatusOrderReceivedAll';
$this->statuts[6] = 'StatusOrderCanceled'; // Approved->Canceled
$this->statuts[7] = 'StatusOrderCanceled'; // Process running->canceled
//$this->statuts[8] = 'StatusOrderBilled'; // Everything is finished, order received totally and bill received
$this->statuts[9] = 'StatusOrderRefused';
// List of language codes for status
$this->statutshort[0] = 'StatusOrderDraftShort';
$this->statutshort[1] = 'StatusOrderValidatedShort';
$this->statutshort[2] = 'StatusOrderApprovedShort';
$this->statutshort[3] = 'StatusOrderOnProcessShort';
$this->statutshort[4] = 'StatusOrderReceivedPartiallyShort';
$this->statutshort[5] = 'StatusOrderReceivedAllShort';
$this->statutshort[6] = 'StatusOrderCanceledShort';
$this->statutshort[7] = 'StatusOrderCanceledShort';
$this->statutshort[9] = 'StatusOrderRefusedShort';
}
$billedtext='';
//if ($statut==5 && $this->billed == 1) $statut = 8;
if ($billed == 1) $billedtext=$langs->trans("Billed");
if ($mode == 0)
{
return $langs->trans($this->statuts[$statut]);
}
elseif ($mode == 1)
{
return $langs->trans($this->statutshort[$statut]);
}
elseif ($mode == 2)
{
return $langs->trans($this->statuts[$statut]);
}
elseif ($mode == 3)
{
if ($statut==0) return img_picto($langs->trans($this->statuts[$statut]),'statut0');
elseif ($statut==1) return img_picto($langs->trans($this->statuts[$statut]),'statut1');
elseif ($statut==2) return img_picto($langs->trans($this->statuts[$statut]),'statut3');
elseif ($statut==3) return img_picto($langs->trans($this->statuts[$statut]),'statut3');
elseif ($statut==4) return img_picto($langs->trans($this->statuts[$statut]),'statut3');
elseif ($statut==5) return img_picto($langs->trans($this->statuts[$statut]),'statut6');
elseif ($statut==6 || $statut==7) return img_picto($langs->trans($this->statuts[$statut]),'statut5');
elseif ($statut==9) return img_picto($langs->trans($this->statuts[$statut]),'statut5');
}
elseif ($mode == 4)
{
if ($statut==0) return img_picto($langs->trans($this->statuts[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
elseif ($statut==1) return img_picto($langs->trans($this->statuts[$statut]),'statut1').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
elseif ($statut==2) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
elseif ($statut==3) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
elseif ($statut==4) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
elseif ($statut==5) return img_picto($langs->trans($this->statuts[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
elseif ($statut==6 || $statut==7) return img_picto($langs->trans($this->statuts[$statut]),'statut5').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
elseif ($statut==9) return img_picto($langs->trans($this->statuts[$statut]),'statut5').' '.$langs->trans($this->statuts[$statut]).($billedtext?' - '.$billedtext:'');
}
elseif ($mode == 5)
{
if ($statut==0) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut0');
elseif ($statut==1) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut1');
elseif ($statut==2) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut3');
elseif ($statut==3) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut3');
elseif ($statut==4) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut3');
elseif ($statut==5) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut6');
elseif ($statut==6 || $statut==7) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut5');
elseif ($statut==9) return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$statut]).' </span>'.img_picto($langs->trans($this->statuts[$statut]),'statut5');
}
}
/**
* Return clicable name (with picto eventually)
*
* @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
* @param string $option On what the link points
* @param int $notooltip 1=Disable tooltip
* @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
* @return string Chain with URL
*/
public function getNomUrl($withpicto=0, $option='', $notooltip=0, $save_lastsearch_value=-1)
{
global $langs, $conf;
$result='';
$label = '<u>' . $langs->trans("ShowOrder") . '</u>';
if (! empty($this->ref))
$label .= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
if (! empty($this->ref_supplier))
$label.= '<br><b>' . $langs->trans('RefSupplier') . ':</b> ' . $this->ref_supplier;
if (! empty($this->total_ht))
$label.= '<br><b>' . $langs->trans('AmountHT') . ':</b> ' . price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
if (! empty($this->total_tva))
$label.= '<br><b>' . $langs->trans('VAT') . ':</b> ' . price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
if (! empty($this->total_ttc))
$label.= '<br><b>' . $langs->trans('AmountTTC') . ':</b> ' . price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
$picto='order';
$url = DOL_URL_ROOT.'/fourn/commande/card.php?id='.$this->id;
if ($option !== 'nolink')
{
// Add param to save lastsearch_values or not
$add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
}
$linkclose='';
if (empty($notooltip))
{
if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
{
$label=$langs->trans("ShowOrder");
$linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
}
$linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
$linkclose.=' class="classfortooltip"';
}
$linkstart = '<a href="'.$url.'"';
$linkstart.=$linkclose.'>';
$linkend='</a>';
$result .= $linkstart;
if ($withpicto) $result.=img_object(($notooltip?'':$label), $this->picto, ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1);
if ($withpicto != 2) $result.= $this->ref;
$result .= $linkend;
return $result;
}
/**
* Returns the following order reference not used depending on the numbering model activated
* defined within COMMANDE_SUPPLIER_ADDON_NUMBER
*
* @param Company $soc company object
* @return string free reference for the invoice
*/
public function getNextNumRef($soc)
{
global $db, $langs, $conf;
$langs->load("orders");
if (! empty($conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER))
{
$mybool = false;
$file = $conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER.'.php';
$classname=$conf->global->COMMANDE_SUPPLIER_ADDON_NUMBER;
// Include file with class
$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
foreach ($dirmodels as $reldir) {
$dir = dol_buildpath($reldir."core/modules/supplier_order/");
// Load file with numbering class (if found)
$mybool|=@include_once $dir.$file;
}
if ($mybool === false) {
dol_print_error('',"Failed to include file ".$file);
return '';
}
$obj = new $classname();
$numref = $obj->getNextValue($soc,$this);
if ( $numref != "")
{
return $numref;
}
else
{
$this->error = $obj->error;
return -1;
}
}
else
{
$this->error = "Error_COMMANDE_SUPPLIER_ADDON_NotDefined";
return -2;
}
}
/**
* Class invoiced the supplier order
*
* @param User $user Object user making the change
* @return int <0 if KO, >0 if KO
*/
public function classifyBilled(User $user)
{
$error=0;
$this->db->begin();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'commande_fournisseur SET billed = 1';
$sql .= ' WHERE rowid = '.$this->id.' AND fk_statut > '.self::STATUS_DRAFT;
if ($this->db->query($sql))
{
if (! $error)
{
// Call trigger
$result=$this->call_trigger('ORDER_SUPPLIER_CLASSIFY_BILLED',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->billed=1;
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -1;
}
}
else
{
dol_print_error($this->db);
$this->db->rollback();
return -1;
}
}
/**
* Approve a supplier order
*
* @param User $user Object user
* @param int $idwarehouse Id of warhouse for stock change
* @param int $secondlevel 0=Standard approval, 1=Second level approval (used when option SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED is set)
* @return int <0 if KO, >0 if OK
*/
public function approve($user, $idwarehouse=0, $secondlevel=0)
{
global $langs,$conf;
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$error=0;
dol_syslog(get_class($this)."::approve");
if ($user->rights->fournisseur->commande->approuver)
{
$now = dol_now();
$this->db->begin();
// Definition of order numbering model name
$soc = new Societe($this->db);
$soc->fetch($this->fourn_id);
// Check if object has a temporary ref
if (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref)) // empty should not happened, but when it occurs, the test save life
{
$num = $this->getNextNumRef($soc);
}
else
{
$num = $this->ref;
}
$this->newref = $num;
// Do we have to change status now ? (If double approval is required and first approval, we keep status to 1 = validated)
$movetoapprovestatus=true;
$comment='';
$sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
$sql.= " SET ref='".$this->db->escape($num)."',";
if (empty($secondlevel)) // standard or first level approval
{
$sql.= " date_approve='".$this->db->idate($now)."',";
$sql.= " fk_user_approve = ".$user->id;
if (! empty($conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED) && $conf->global->MAIN_FEATURES_LEVEL > 0 && $this->total_ht >= $conf->global->SUPPLIER_ORDER_3_STEPS_TO_BE_APPROVED)
{
if (empty($this->user_approve_id2))
{
$movetoapprovestatus=false; // second level approval not done
$comment=' (first level)';
}
}
}
else // request a second level approval
{
$sql.= " date_approve2='".$this->db->idate($now)."',";
$sql.= " fk_user_approve2 = ".$user->id;
if (empty($this->user_approve_id)) $movetoapprovestatus=false; // first level approval not done
$comment=' (second level)';
}
// If double approval is required and first approval, we keep status to 1 = validated
if ($movetoapprovestatus) $sql.= ", fk_statut = ".self::STATUS_ACCEPTED;
else $sql.= ", fk_statut = ".self::STATUS_VALIDATED;
$sql.= " WHERE rowid = ".$this->id;
$sql.= " AND fk_statut = ".self::STATUS_VALIDATED;
if ($this->db->query($sql))
{
if (! empty($conf->global->SUPPLIER_ORDER_AUTOADD_USER_CONTACT))
{
$result=$this->add_contact($user->id, 'SALESREPFOLL', 'internal', 1);
if ($result < 0 && $result != -2) // -2 means already exists
{
$error++;
}
}
// If stock is incremented on validate order, we must increment it
if (! $error && $movetoapprovestatus && ! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER))
{
require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
$langs->load("agenda");
$cpt=count($this->lines);
for ($i = 0; $i < $cpt; $i++)
{
// Product with reference
if ($this->lines[$i]->fk_product > 0)
{
$this->line = $this->lines[$i];
$mouvP = new MouvementStock($this->db);
$mouvP->origin = &$this;
// We decrement stock of product (and sub-products)
$up_ht_disc=$this->lines[$i]->subprice;
if (! empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) $up_ht_disc=price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100, 'MU');
$result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("OrderApprovedInDolibarr",$this->ref));
if ($result < 0) { $error++; }
unset($this->line);
}
}
}
if (! $error)
{
// Call trigger
$result=$this->call_trigger('ORDER_SUPPLIER_APPROVE',$user);
if ($result < 0) $error++;
// End call triggers
}
if (! $error)
{
$this->ref = $this->newref;
if ($movetoapprovestatus) $this->statut = self::STATUS_ACCEPTED;
else $this->statut = self::STATUS_VALIDATED;
if (empty($secondlevel)) // standard or first level approval
{
$this->date_approve = $now;
$this->user_approve_id = $user->id;
}
else // request a second level approval
{
$this->date_approve2 = $now;