-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
commande.class.php
3305 lines (2933 loc) · 118 KB
/
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-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2013 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
* Copyright (C) 2010-2012 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2011 Jean Heimburger <jean@tiaris.info>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
* Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
*
* 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/commande/class/commande.class.php
* \ingroup commande
* \brief Fichier des classes de commandes
*/
include_once DOL_DOCUMENT_ROOT.'/core/class/commonorder.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT .'/margin/lib/margins.lib.php';
/**
* Class to manage customers orders
*/
class Commande extends CommonOrder
{
public $element='commande';
public $table_element='commande';
public $table_element_line = 'commandedet';
public $class_element_line = 'OrderLine';
public $fk_element = 'fk_commande';
protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
var $id;
var $socid; // Id client
var $client; // Objet societe client (a charger par fetch_client)
var $ref;
var $ref_client;
var $ref_ext;
var $ref_int;
var $contactid;
var $fk_project;
var $statut; // -1=Canceled, 0=Draft, 1=Validated, (2=Accepted/On process not managed for customer orders), 3=Closed (Sent/Received, billed or not)
var $facturee; // Facturee ou non
var $brouillon;
var $cond_reglement_id;
var $cond_reglement_code;
var $mode_reglement_id;
var $mode_reglement_code;
var $availability_id;
var $availability_code;
var $demand_reason_id;
var $demand_reason_code;
var $fk_delivery_address;
var $address;
var $date; // Date commande
var $date_commande; // Date commande (deprecated)
var $date_livraison; // Date livraison souhaitee
var $fk_remise_except;
var $remise_percent;
var $total_ht; // Total net of tax
var $total_ttc; // Total with tax
var $total_tva; // Total VAT
var $total_localtax1; // Total Local tax 1
var $total_localtax2; // Total Local tax 2
var $remise_absolue;
var $modelpdf;
var $info_bits;
var $rang;
var $special_code;
var $source; // Origin of order
var $note; // deprecated
var $note_private;
var $note_public;
var $extraparams=array();
var $origin;
var $origin_id;
var $linked_objects=array();
var $user_author_id;
var $lines = array();
// Pour board
var $nbtodo;
var $nbtodolate;
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function __construct($db)
{
$this->db = $db;
$this->remise = 0;
$this->remise_percent = 0;
$this->products = array();
}
/**
* Returns the reference to the following non used Order depending on the active numbering module
* defined into COMMANDE_ADDON
*
* @param Societe $soc Object thirdparty
* @return string Order free reference
*/
function getNextNumRef($soc)
{
global $db, $langs, $conf;
$langs->load("order");
$dir = DOL_DOCUMENT_ROOT . "/core/modules/commande";
if (! empty($conf->global->COMMANDE_ADDON))
{
$file = $conf->global->COMMANDE_ADDON.".php";
// Chargement de la classe de numerotation
$classname = $conf->global->COMMANDE_ADDON;
$result=include_once $dir.'/'.$file;
if ($result)
{
$obj = new $classname();
$numref = "";
$numref = $obj->getNextValue($soc,$this);
if ( $numref != "")
{
return $numref;
}
else
{
dol_print_error($db,get_class($this)."::getNextNumRef ".$obj->error);
return "";
}
}
else
{
print $langs->trans("Error")." ".$langs->trans("Error_COMMANDE_ADDON_NotDefined");
return "";
}
}
else
{
print $langs->trans("Error")." ".$langs->trans("Error_COMMANDE_ADDON_NotDefined");
return "";
}
}
/**
* Validate order
*
* @param User $user User making status change
* @param int $idwarehouse Id of warehouse to use for stock decrease
* @return int <=0 if OK, >0 if KO
*/
function valid($user, $idwarehouse=0)
{
global $conf,$langs;
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$error=0;
// Protection
if ($this->statut == 1)
{
dol_syslog(get_class($this)."::valid no draft status", LOG_WARNING);
return 0;
}
if (! $user->rights->commande->valider)
{
$this->error='Permission denied';
dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
return -1;
}
$now=dol_now();
$this->db->begin();
// Definition du nom de module de numerotation de commande
$soc = new Societe($this->db);
$soc->fetch($this->socid);
// Class of company linked to order
$result=$soc->set_as_client();
// Define new ref
if (! $error && (preg_match('/^[\(]?PROV/i', $this->ref)))
{
$num = $this->getNextNumRef($soc);
}
else
{
$num = $this->ref;
}
// Validate
$sql = "UPDATE ".MAIN_DB_PREFIX."commande";
$sql.= " SET ref = '".$num."',";
$sql.= " fk_statut = 1,";
$sql.= " date_valid='".$this->db->idate($now)."',";
$sql.= " fk_user_valid = ".$user->id;
$sql.= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::valid() sql=".$sql);
$resql=$this->db->query($sql);
if (! $resql)
{
dol_syslog(get_class($this)."::valid Echec update - 10 - sql=".$sql, LOG_ERR);
dol_print_error($this->db);
$error++;
}
if (! $error)
{
// If stock is incremented on validate order, we must increment it
if ($result >= 0 && ! empty($conf->stock->enabled) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1)
{
require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
$langs->load("agenda");
// Loop on each line
$cpt=count($this->lines);
for ($i = 0; $i < $cpt; $i++)
{
if ($this->lines[$i]->fk_product > 0)
{
$mouvP = new MouvementStock($this->db);
// We decrement stock of product (and sub-products)
$result=$mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("OrderValidatedInDolibarr",$num));
if ($result < 0) { $error++; }
}
}
}
}
if (! $error)
{
$this->oldref='';
// Rename directory if dir was a temporary ref
if (preg_match('/^[\(]?PROV/i', $this->ref))
{
// On renomme repertoire ($this->ref = ancienne ref, $numfa = nouvelle ref)
// afin de ne pas perdre les fichiers attaches
$comref = dol_sanitizeFileName($this->ref);
$snum = dol_sanitizeFileName($num);
$dirsource = $conf->commande->dir_output.'/'.$comref;
$dirdest = $conf->commande->dir_output.'/'.$snum;
if (file_exists($dirsource))
{
dol_syslog(get_class($this)."::valid() rename dir ".$dirsource." into ".$dirdest);
if (@rename($dirsource, $dirdest))
{
$this->oldref = $comref;
dol_syslog("Rename ok");
// Suppression ancien fichier PDF dans nouveau rep
dol_delete_file($conf->commande->dir_output.'/'.$snum.'/'.$comref.'*.*');
}
}
}
}
// Set new ref and current status
if (! $error)
{
$this->ref = $num;
$this->statut = 1;
}
if (! $error)
{
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('ORDER_VALIDATE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Set draft status
*
* @param User $user Object user that modify
* @param int $idwarehouse Id warehouse to use for stock change.
* @return int <0 if KO, >0 if OK
*/
function set_draft($user, $idwarehouse=-1)
{
global $conf,$langs;
$error=0;
// Protection
if ($this->statut <= 0)
{
return 0;
}
if (! $user->rights->commande->valider)
{
$this->error='Permission denied';
return -1;
}
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."commande";
$sql.= " SET fk_statut = 0";
$sql.= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::set_draft sql=".$sql, LOG_DEBUG);
if ($this->db->query($sql))
{
// If stock is decremented on validate order, we must reincrement it
if (! empty($conf->stock->enabled) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1)
{
require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
$langs->load("agenda");
$num=count($this->lines);
for ($i = 0; $i < $num; $i++)
{
if ($this->lines[$i]->fk_product > 0)
{
$mouvP = new MouvementStock($this->db);
// We increment stock of product (and sub-products)
$result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("OrderBackToDraftInDolibarr",$this->ref));
if ($result < 0) { $error++; }
}
}
if (!$error)
{
$this->statut=0;
$this->db->commit();
return $result;
}
else
{
$this->error=$mouvP->error;
$this->db->rollback();
return $result;
}
}
$this->statut=0;
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error();
$this->db->rollback();
dol_syslog($this->error, LOG_ERR);
return -1;
}
}
/**
* Tag the order as validated (opened)
* Function used when order is reopend after being closed.
*
* @param User $user Object user that change status
* @return int <0 if KO, 0 if nothing is done, >0 if OK
*/
function set_reopen($user)
{
global $conf,$langs;
$error=0;
if ($this->statut != 3)
{
return 0;
}
$this->db->begin();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'commande';
$sql.= ' SET fk_statut=1, facture=0';
$sql.= ' WHERE rowid = '.$this->id;
dol_syslog(get_class($this)."::set_reopen sql=".$sql);
$resql = $this->db->query($sql);
if ($resql)
{
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('ORDER_REOPEN',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
else
{
$error++;
$this->error=$this->db->error();
dol_print_error($this->db);
}
if (! $error)
{
$this->statut = 1;
$this->billed = 0;
$this->facturee = 0; // deprecated
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -1;
}
}
/**
* Close order
*
* @param User $user Objet user that close
* @return int <0 if KO, >0 if OK
*/
function cloture($user)
{
global $conf, $langs;
$error=0;
if ($user->rights->commande->valider)
{
$this->db->begin();
$now=dol_now();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'commande';
$sql.= ' SET fk_statut = 3,';
$sql.= ' fk_user_cloture = '.$user->id.',';
$sql.= ' date_cloture = '.$this->db->idate($now);
$sql.= ' WHERE rowid = '.$this->id.' AND fk_statut > 0';
if ($this->db->query($sql))
{
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('ORDER_CLOSE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
if (! $error)
{
$this->statut=3;
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -1;
}
}
else
{
$this->error=$this->db->lasterror();
dol_syslog($this->error, LOG_ERR);
$this->db->rollback();
return -1;
}
}
}
/**
* Cancel an order
* If stock is decremented on order validation, we must reincrement it
*
* @param int $idwarehouse Id warehouse to use for stock change.
* @return int <0 if KO, >0 if OK
*/
function cancel($idwarehouse=-1)
{
global $conf,$user,$langs;
$error=0;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."commande";
$sql.= " SET fk_statut = -1";
$sql.= " WHERE rowid = ".$this->id;
$sql.= " AND fk_statut = 1";
dol_syslog(get_class($this)."::cancel sql=".$sql, LOG_DEBUG);
if ($this->db->query($sql))
{
// If stock is decremented on validate order, we must reincrement it
if (! empty($conf->stock->enabled) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1)
{
require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
$langs->load("agenda");
$num=count($this->lines);
for ($i = 0; $i < $num; $i++)
{
if ($this->lines[$i]->fk_product > 0)
{
$mouvP = new MouvementStock($this->db);
// We increment stock of product (and sub-products)
$result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("OrderCanceledInDolibarr",$this->ref));
if ($result < 0) {
$error++;
}
}
}
}
if (! $error)
{
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('ORDER_CANCEL',$this,$user,$langs,$conf);
if ($result < 0) {
$error++; $this->errors=$interface->errors;
}
// Fin appel triggers
}
if (! $error)
{
$this->statut=-1;
$this->db->commit();
return 1;
}
else
{
$this->error=$mouvP->error;
$this->db->rollback();
return -1;
}
}
else
{
$this->error=$this->db->error();
$this->db->rollback();
dol_syslog($this->error, LOG_ERR);
return -1;
}
}
/**
* Create order
* Note that this->ref can be set or empty. If empty, we will use "(PROV)"
*
* @param User $user Objet user that make creation
* @param int $notrigger Disable all triggers
* @return int <0 if KO, >0 if OK
*/
function create($user, $notrigger=0)
{
global $conf,$langs,$mysoc,$hookmanager;
$error=0;
// Clean parameters
$this->brouillon = 1; // On positionne en mode brouillon la commande
dol_syslog(get_class($this)."::create user=".$user->id);
// Check parameters
$soc = new Societe($this->db);
$result=$soc->fetch($this->socid);
if ($result < 0)
{
$this->error="Failed to fetch company";
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
return -2;
}
if (! empty($conf->global->COMMANDE_REQUIRE_SOURCE) && $this->source < 0)
{
$this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Source"));
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
return -1;
}
// $date_commande is deprecated
$date = ($this->date_commande ? $this->date_commande : $this->date);
$now=dol_now();
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commande (";
$sql.= " ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_client, ref_int";
$sql.= ", model_pdf, fk_cond_reglement, fk_mode_reglement, fk_availability, fk_input_reason, date_livraison, fk_delivery_address";
$sql.= ", remise_absolue, remise_percent";
$sql.= ", entity";
$sql.= ")";
$sql.= " VALUES ('(PROV)',".$this->socid.", ".$this->db->idate($now).", ".$user->id;
$sql.= ", ".($this->fk_project?$this->fk_project:"null");
$sql.= ", ".$this->db->idate($date);
$sql.= ", ".($this->source>=0 && $this->source != '' ?$this->source:'null');
$sql.= ", '".$this->db->escape($this->note_private)."'";
$sql.= ", '".$this->db->escape($this->note_public)."'";
$sql.= ", '".$this->db->escape($this->ref_client)."'";
$sql.= ", ".($this->ref_int?"'".$this->db->escape($this->ref_int)."'":"null");
$sql.= ", '".$this->modelpdf."'";
$sql.= ", ".($this->cond_reglement_id>0?"'".$this->cond_reglement_id."'":"null");
$sql.= ", ".($this->mode_reglement_id>0?"'".$this->mode_reglement_id."'":"null");
$sql.= ", ".($this->availability_id>0?"'".$this->availability_id."'":"null");
$sql.= ", ".($this->demand_reason_id>0?"'".$this->demand_reason_id."'":"null");
$sql.= ", ".($this->date_livraison?"'".$this->db->idate($this->date_livraison)."'":"null");
$sql.= ", ".($this->fk_delivery_address>0?$this->fk_delivery_address:'NULL');
$sql.= ", ".($this->remise_absolue>0?$this->remise_absolue:'NULL');
$sql.= ", '".$this->remise_percent."'";
$sql.= ", ".$conf->entity;
$sql.= ")";
dol_syslog(get_class($this)."::create sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'commande');
if ($this->id)
{
$fk_parent_line=0;
$num=count($this->lines);
/*
* Insertion du detail des produits dans la base
*/
for ($i=0;$i<$num;$i++)
{
// Reset fk_parent_line for no child products and special product
if (($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line)) || $this->lines[$i]->product_type == 9) {
$fk_parent_line = 0;
}
$result = $this->addline(
$this->id,
$this->lines[$i]->desc,
$this->lines[$i]->subprice,
$this->lines[$i]->qty,
$this->lines[$i]->tva_tx,
$this->lines[$i]->localtax1_tx,
$this->lines[$i]->localtax2_tx,
$this->lines[$i]->fk_product,
$this->lines[$i]->remise_percent,
$this->lines[$i]->info_bits,
$this->lines[$i]->fk_remise_except,
'HT',
0,
$this->lines[$i]->date_start,
$this->lines[$i]->date_end,
$this->lines[$i]->product_type,
$this->lines[$i]->rang,
$this->lines[$i]->special_code,
$fk_parent_line,
$this->lines[$i]->fk_fournprice,
$this->lines[$i]->pa_ht,
$this->lines[$i]->label
);
if ($result < 0)
{
$this->error=$this->db->lasterror();
dol_print_error($this->db);
$this->db->rollback();
return -1;
}
// Defined the new fk_parent_line
if ($result > 0 && $this->lines[$i]->product_type == 9) {
$fk_parent_line = $result;
}
}
// Mise a jour ref
$sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET ref='(PROV".$this->id.")' WHERE rowid=".$this->id;
if ($this->db->query($sql))
{
if ($this->id)
{
$this->ref="(PROV".$this->id.")";
// Add object linked
if (is_array($this->linked_objects) && ! empty($this->linked_objects))
{
foreach($this->linked_objects as $origin => $origin_id)
{
$ret = $this->add_object_linked($origin, $origin_id);
if (! $ret)
{
dol_print_error($this->db);
$error++;
}
// TODO mutualiser
if ($origin == 'propal' && $origin_id)
{
// On recupere les differents contact interne et externe
$prop = new Propal($this->db, $this->socid, $origin_id);
// On recupere le commercial suivi propale
$this->userid = $prop->getIdcontact('internal', 'SALESREPFOLL');
if ($this->userid)
{
//On passe le commercial suivi propale en commercial suivi commande
$this->add_contact($this->userid[0], 'SALESREPFOLL', 'internal');
}
// On recupere le contact client suivi propale
$this->contactid = $prop->getIdcontact('external', 'CUSTOMER');
if ($this->contactid)
{
//On passe le contact client suivi propale en contact client suivi commande
$this->add_contact($this->contactid[0], 'CUSTOMER', 'external');
}
}
}
}
}
if (! $error)
{
// Actions on extra fields (by external module or standard code)
// FIXME le hook fait double emploi avec le trigger !!
$hookmanager->initHooks(array('orderdao'));
$parameters=array('socid'=>$this->id);
$reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if (empty($reshook))
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
}
else if ($reshook < 0) $error++;
}
if (! $notrigger)
{
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('ORDER_CREATE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
$this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
return -1;
}
}
}
else
{
dol_print_error($this->db);
$this->db->rollback();
return -1;
}
}
/**
* Load an object from its id and create a new one in database
*
* @param int $socid Id of thirdparty
* @return int New id of clone
*/
function createFromClone($socid=0)
{
global $conf,$user,$langs,$hookmanager;
$error=0;
$this->db->begin();
// Load source object
$objFrom = dol_clone($this);
// Change socid if needed
if (! empty($socid) && $socid != $this->socid)
{
$objsoc = new Societe($this->db);
if ($objsoc->fetch($socid)>0)
{
$this->socid = $objsoc->id;
$this->cond_reglement_id = (! empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
$this->mode_reglement_id = (! empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
$this->fk_project = '';
$this->fk_delivery_address = '';
}
// TODO Change product price if multi-prices
}
$this->id=0;
$this->statut=0;
// Clear fields
$this->user_author_id = $user->id;
$this->user_valid = '';
$this->date_creation = '';
$this->date_validation = '';
$this->ref_client = '';
// Create clone
$result=$this->create($user);
if ($result < 0) $error++;
if (! $error)
{
// Hook of thirdparty module
if (is_object($hookmanager))
{
$parameters=array('objFrom'=>$objFrom);
$action='';
$reshook=$hookmanager->executeHooks('createFrom',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) $error++;
}
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('ORDER_CLONE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
// End
if (! $error)
{
$this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
return -1;
}
}
/**
* Load an object from a proposal and create a new order into database
*
* @param Object $object Object source
* @return int <0 if KO, 0 if nothing done, 1 if OK
*/
function createFromProposal($object)
{
global $conf,$user,$langs,$hookmanager;
$error=0;
// Signed proposal
if ($object->statut == 2)
{
$this->date_commande = dol_now();
$this->source = 0;
$num=count($object->lines);
for ($i = 0; $i < $num; $i++)
{
$line = new OrderLine($this->db);
$line->libelle = $object->lines[$i]->libelle;
$line->label = $object->lines[$i]->label;
$line->desc = $object->lines[$i]->desc;
$line->price = $object->lines[$i]->price;
$line->subprice = $object->lines[$i]->subprice;
$line->tva_tx = $object->lines[$i]->tva_tx;
$line->localtax1_tx = $object->lines[$i]->localtax1_tx;
$line->localtax2_tx = $object->lines[$i]->localtax2_tx;
$line->qty = $object->lines[$i]->qty;
$line->fk_remise_except = $object->lines[$i]->fk_remise_except;
$line->remise_percent = $object->lines[$i]->remise_percent;
$line->fk_product = $object->lines[$i]->fk_product;
$line->info_bits = $object->lines[$i]->info_bits;
$line->product_type = $object->lines[$i]->product_type;
$line->rang = $object->lines[$i]->rang;
$line->special_code = $object->lines[$i]->special_code;
$line->fk_parent_line = $object->lines[$i]->fk_parent_line;
$line->fk_fournprice = $object->lines[$i]->fk_fournprice;
$marginInfos = getMarginInfos($object->lines[$i]->subprice, $object->lines[$i]->remise_percent, $object->lines[$i]->tva_tx, $object->lines[$i]->localtax1_tx, $object->lines[$i]->localtax2_tx, $object->lines[$i]->fk_fournprice, $object->lines[$i]->pa_ht);
$line->pa_ht = $marginInfos[0];
$line->marge_tx = $marginInfos[1];
$line->marque_tx = $marginInfos[2];
$this->lines[$i] = $line;
}
$this->socid = $object->socid;
$this->fk_project = $object->fk_project;
$this->cond_reglement_id = $object->cond_reglement_id;
$this->mode_reglement_id = $object->mode_reglement_id;
$this->availability_id = $object->availability_id;
$this->demand_reason_id = $object->demand_reason_id;
$this->date_livraison = $object->date_livraison;
$this->fk_delivery_address = $object->fk_delivery_address;
$this->contact_id = $object->contactid;
$this->ref_client = $object->ref_client;
$this->note_private = $object->note_private;
$this->note_public = $object->note_public;
$this->origin = $object->element;
$this->origin_id = $object->id;
// Possibility to add external linked objects with hooks
$this->linked_objects[$this->origin] = $this->origin_id;
if (is_array($object->other_linked_objects) && ! empty($object->other_linked_objects))
{
$this->linked_objects = array_merge($this->linked_objects, $object->other_linked_objects);
}
$ret = $this->create($user);
if ($ret > 0)
{
// Actions hooked (by external module)
$hookmanager->initHooks(array('orderdao'));
$parameters=array('objFrom'=>$object);
$action='';
$reshook=$hookmanager->executeHooks('createFrom',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) $error++;
if (! $error)
{
// Ne pas passer par la commande provisoire
if ($conf->global->COMMANDE_VALID_AFTER_CLOSE_PROPAL == 1)
{
$this->fetch($ret);
$this->valid($user);
}
return 1;
}
else return -1;
}
else return -1;
}
else return 0;
}
/**
* Add an order line into database (linked to product/service or not)