-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
commonobject.class.php
3317 lines (2960 loc) · 117 KB
/
commonobject.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) 2006-2013 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2013 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2010-2013 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
* Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2012-2013 Christophe Battarel <christophe.battarel@altairis.fr>
* Copyright (C) 2011-2012 Philippe Grand <philippe.grand@atoo-net.com>
* Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
*
* 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/core/class/commonobject.class.php
* \ingroup core
* \brief File of parent class of all other business classes (invoices, contracts, proposals, orders, ...)
*/
/**
* Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
*/
abstract class CommonObject
{
protected $db;
public $error;
public $errors;
public $canvas; // Contains canvas name if it is
public $name;
public $lastname;
public $firstname;
public $civility_id;
public $import_key;
public $array_options=array();
public $linkedObjectsIds; // Loaded by ->fetchObjectLinked
public $linkedObjects; // Loaded by ->fetchObjectLinked
// No constructor as it is an abstract class
/**
* Method to output saved errors
*
* @return string String with errors
*/
function errorsToString()
{
return $this->error.(is_array($this->errors)?(($this->error!=''?' ':'').join(',',$this->errors)):'');
}
/**
* Return full name (civility+' '+name+' '+lastname)
*
* @param Translate $langs Language object for translation of civility
* @param int $option 0=No option, 1=Add civility
* @param int $nameorder -1=Auto, 0=Lastname+Firstname, 1=Firstname+Lastname
* @param int $maxlen Maximum length
* @return string String with full name
*/
function getFullName($langs,$option=0,$nameorder=-1,$maxlen=0)
{
global $conf;
//print "lastname=".$this->lastname." name=".$this->name." nom=".$this->nom."<br>\n";
$lastname=$this->lastname;
$firstname=$this->firstname;
if (empty($lastname)) $lastname=($this->lastname?$this->lastname:($this->name?$this->name:$this->nom));
if (empty($firstname)) $firstname=$this->firstname;
$ret='';
if ($option && $this->civilite_id)
{
if ($langs->transnoentitiesnoconv("Civility".$this->civilite_id)!="Civility".$this->civilite_id) $ret.=$langs->transnoentitiesnoconv("Civility".$this->civilite_id).' ';
else $ret.=$this->civilite_id.' ';
}
$ret.=dolGetFirstLastname($firstname, $lastname, $nameorder);
return dol_trunc($ret,$maxlen);
}
/**
* Return full address of contact
*
* @param int $withcountry 1=Add country into address string
* @param string $sep Separator to use to build string
* @return string Full address string
*/
function getFullAddress($withcountry=0,$sep="\n")
{
if ($withcountry && $this->country_id && (empty($this->country_code) || empty($this->country)))
{
require_once DOL_DOCUMENT_ROOT .'/core/lib/company.lib.php';
$tmparray=getCountry($this->country_id,'all');
$this->country_code=$tmparray['code'];
$this->country =$tmparray['label'];
}
return dol_format_address($this, $withcountry, $sep);
}
/**
* Check if ref is used.
*
* @return int <0 if KO, 0 if not found, >0 if found
*/
function verifyNumRef()
{
global $conf;
$sql = "SELECT rowid";
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE ref = '".$this->ref."'";
$sql.= " AND entity = ".$conf->entity;
dol_syslog(get_class($this)."::verifyNumRef sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
return $num;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::verifyNumRef ".$this->error, LOG_ERR);
return -1;
}
}
/**
* Add a link between element $this->element and a contact
*
* @param int $fk_socpeople Id of contact to link
* @param int $type_contact Type of contact (code or id)
* @param int $source external=Contact extern (llx_socpeople), internal=Contact intern (llx_user)
* @param int $notrigger Disable all triggers
* @return int <0 if KO, >0 if OK
*/
function add_contact($fk_socpeople, $type_contact, $source='external',$notrigger=0)
{
global $user,$conf,$langs;
$error=0;
dol_syslog(get_class($this)."::add_contact $fk_socpeople, $type_contact, $source");
// Check parameters
if ($fk_socpeople <= 0)
{
$this->error=$langs->trans("ErrorWrongValueForParameter","1");
dol_syslog(get_class($this)."::add_contact ".$this->error,LOG_ERR);
return -1;
}
if (! $type_contact)
{
$this->error=$langs->trans("ErrorWrongValueForParameter","2");
dol_syslog(get_class($this)."::add_contact ".$this->error,LOG_ERR);
return -2;
}
$id_type_contact=0;
if (is_numeric($type_contact))
{
$id_type_contact=$type_contact;
}
else
{
// On recherche id type_contact
$sql = "SELECT tc.rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."c_type_contact as tc";
$sql.= " WHERE element='".$this->element."'";
$sql.= " AND source='".$source."'";
$sql.= " AND code='".$type_contact."' AND active=1";
$resql=$this->db->query($sql);
if ($resql)
{
$obj = $this->db->fetch_object($resql);
$id_type_contact=$obj->rowid;
}
}
$datecreate = dol_now();
// Insertion dans la base
$sql = "INSERT INTO ".MAIN_DB_PREFIX."element_contact";
$sql.= " (element_id, fk_socpeople, datecreate, statut, fk_c_type_contact) ";
$sql.= " VALUES (".$this->id.", ".$fk_socpeople." , " ;
$sql.= "'".$this->db->idate($datecreate)."'";
$sql.= ", 4, '". $id_type_contact . "' ";
$sql.= ")";
dol_syslog(get_class($this)."::add_contact sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
if (! $notrigger)
{
// Call triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers(strtoupper($this->element).'_ADD_CONTACT',$this,$user,$langs,$conf);
if ($result < 0) {
$error++; $this->errors=$interface->errors;
}
// End call triggers
}
return 1;
}
else
{
if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
{
$this->error=$this->db->errno();
return -2;
}
else
{
$this->error=$this->db->error();
dol_syslog($this->error,LOG_ERR);
return -1;
}
}
}
/**
* Update a link to contact line
*
* @param int $rowid Id of line contact-element
* @param int $statut New status of link
* @param int $type_contact_id Id of contact type (not modified if 0)
* @param int $fk_socpeople Id of soc_people to update (not modified if 0)
* @return int <0 if KO, >= 0 if OK
*/
function update_contact($rowid, $statut, $type_contact_id=0, $fk_socpeople=0)
{
// Insertion dans la base
$sql = "UPDATE ".MAIN_DB_PREFIX."element_contact set";
$sql.= " statut = ".$statut;
if ($type_contact_id) $sql.= ", fk_c_type_contact = '".$type_contact_id ."'";
if ($fk_socpeople) $sql.= ", fk_socpeople = '".$fk_socpeople ."'";
$sql.= " where rowid = ".$rowid;
$resql=$this->db->query($sql);
if ($resql)
{
return 0;
}
else
{
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Delete a link to contact line
*
* @param int $rowid Id of contact link line to delete
* @param int $notrigger Disable all triggers
* @return int >0 if OK, <0 if KO
*/
function delete_contact($rowid, $notrigger=0)
{
global $user,$langs,$conf;
$error=0;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
$sql.= " WHERE rowid =".$rowid;
dol_syslog(get_class($this)."::delete_contact sql=".$sql);
if ($this->db->query($sql))
{
if (! $notrigger)
{
// Call triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers(strtoupper($this->element).'_DELETE_CONTACT',$this,$user,$langs,$conf);
if ($result < 0) {
$error++; $this->errors=$interface->errors;
}
// End call triggers
}
return 1;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::delete_contact error=".$this->error, LOG_ERR);
return -1;
}
}
/**
* Delete all links between an object $this and all its contacts
*
* @return int >0 if OK, <0 if KO
*/
function delete_linked_contact()
{
$temp = array();
$typeContact = $this->liste_type_contact('');
foreach($typeContact as $key => $value)
{
array_push($temp,$key);
}
$listId = implode(",", $temp);
$sql = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
$sql.= " WHERE element_id =".$this->id;
$sql.= " AND fk_c_type_contact IN (".$listId.")";
dol_syslog(get_class($this)."::delete_linked_contact sql=".$sql, LOG_DEBUG);
if ($this->db->query($sql))
{
return 1;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::delete_linked_contact error=".$this->error, LOG_ERR);
return -1;
}
}
/**
* Get array of all contacts for an object
*
* @param int $statut Status of lines to get (-1=all)
* @param string $source Source of contact: external or thirdparty (llx_socpeople) or internal (llx_user)
* @param int $list 0:Return array contains all properties, 1:Return array contains just id
* @return array Array of contacts
*/
function liste_contact($statut=-1,$source='external',$list=0)
{
global $langs;
$tab=array();
$sql = "SELECT ec.rowid, ec.statut, ec.fk_socpeople as id"; // This field contains id of llx_socpeople or id of llx_user
if ($source == 'internal') $sql.=", '-1' as socid";
if ($source == 'external' || $source == 'thirdparty') $sql.=", t.fk_soc as socid";
$sql.= ", t.civilite as civility, t.lastname as lastname, t.firstname, t.email";
$sql.= ", tc.source, tc.element, tc.code, tc.libelle";
$sql.= " FROM ".MAIN_DB_PREFIX."c_type_contact tc";
$sql.= ", ".MAIN_DB_PREFIX."element_contact ec";
if ($source == 'internal') $sql.=" LEFT JOIN ".MAIN_DB_PREFIX."user t on ec.fk_socpeople = t.rowid";
if ($source == 'external'|| $source == 'thirdparty') $sql.=" LEFT JOIN ".MAIN_DB_PREFIX."socpeople t on ec.fk_socpeople = t.rowid";
$sql.= " WHERE ec.element_id =".$this->id;
$sql.= " AND ec.fk_c_type_contact=tc.rowid";
$sql.= " AND tc.element='".$this->element."'";
if ($source == 'internal') $sql.= " AND tc.source = 'internal'";
if ($source == 'external' || $source == 'thirdparty') $sql.= " AND tc.source = 'external'";
$sql.= " AND tc.active=1";
if ($statut >= 0) $sql.= " AND ec.statut = '".$statut."'";
$sql.=" ORDER BY t.lastname ASC";
dol_syslog(get_class($this)."::liste_contact sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
$num=$this->db->num_rows($resql);
$i=0;
while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
if (! $list)
{
$transkey="TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
$libelle_type=($langs->trans($transkey)!=$transkey ? $langs->trans($transkey) : $obj->libelle);
$tab[$i]=array('source'=>$obj->source,'socid'=>$obj->socid,'id'=>$obj->id,
'nom'=>$obj->lastname, // For backward compatibility
'civility'=>$obj->civility, 'lastname'=>$obj->lastname, 'firstname'=>$obj->firstname, 'email'=>$obj->email,
'rowid'=>$obj->rowid,'code'=>$obj->code,'libelle'=>$libelle_type,'status'=>$obj->statut);
}
else
{
$tab[$i]=$obj->id;
}
$i++;
}
return $tab;
}
else
{
$this->error=$this->db->error();
dol_print_error($this->db);
return -1;
}
}
/**
* Update status of a contact linked to object
*
* @param int $rowid Id of link between object and contact
* @return int <0 if KO, >=0 if OK
*/
function swapContactStatus($rowid)
{
$sql = "SELECT ec.datecreate, ec.statut, ec.fk_socpeople, ec.fk_c_type_contact,";
$sql.= " tc.code, tc.libelle";
//$sql.= ", s.fk_soc";
$sql.= " FROM (".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc)";
//$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as s ON ec.fk_socpeople=s.rowid"; // Si contact de type external, alors il est lie a une societe
$sql.= " WHERE ec.rowid =".$rowid;
$sql.= " AND ec.fk_c_type_contact=tc.rowid";
$sql.= " AND tc.element = '".$this->element."'";
dol_syslog(get_class($this)."::swapContactStatus sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
$obj = $this->db->fetch_object($resql);
$newstatut = ($obj->statut == 4) ? 5 : 4;
$result = $this->update_contact($rowid, $newstatut);
$this->db->free($resql);
return $result;
}
else
{
$this->error=$this->db->error();
dol_print_error($this->db);
return -1;
}
}
/**
* Return array with list of possible values for type of contacts
*
* @param string $source 'internal', 'external' or 'all'
* @param string $order Sort order by : 'code' or 'rowid'
* @param string $option 0=Return array id->label, 1=Return array code->label
* @param string $activeonly 0=all type of contact, 1=only the active
* @return array Array list of type of contacts (id->label if option=0, code->label if option=1)
*/
function liste_type_contact($source='internal', $order='code', $option=0, $activeonly=0)
{
global $langs;
$tab = array();
$sql = "SELECT DISTINCT tc.rowid, tc.code, tc.libelle";
$sql.= " FROM ".MAIN_DB_PREFIX."c_type_contact as tc";
$sql.= " WHERE tc.element='".$this->element."'";
if ($activeonly == 1)
$sql.= " AND tc.active=1"; // only the active type
if (! empty($source)) $sql.= " AND tc.source='".$source."'";
$sql.= " ORDER by tc.".$order;
//print "sql=".$sql;
$resql=$this->db->query($sql);
if ($resql)
{
$num=$this->db->num_rows($resql);
$i=0;
while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
$transkey="TypeContact_".$this->element."_".$source."_".$obj->code;
$libelle_type=($langs->trans($transkey)!=$transkey ? $langs->trans($transkey) : $obj->libelle);
if (empty($option)) $tab[$obj->rowid]=$libelle_type;
else $tab[$obj->code]=$libelle_type;
$i++;
}
return $tab;
}
else
{
$this->error=$this->db->lasterror();
//dol_print_error($this->db);
return null;
}
}
/**
* Return id of contacts for a source and a contact code.
* Example: contact client de facturation ('external', 'BILLING')
* Example: contact client de livraison ('external', 'SHIPPING')
* Example: contact interne suivi paiement ('internal', 'SALESREPFOLL')
*
* @param string $source 'external' or 'internal'
* @param string $code 'BILLING', 'SHIPPING', 'SALESREPFOLL', ...
* @param int $status limited to a certain status
* @return array List of id for such contacts
*/
function getIdContact($source,$code,$status=0)
{
global $conf;
$result=array();
$i=0;
$sql = "SELECT ec.fk_socpeople";
$sql.= " FROM ".MAIN_DB_PREFIX."element_contact as ec,";
if ($source == 'internal') $sql.= " ".MAIN_DB_PREFIX."user as c,";
if ($source == 'external') $sql.= " ".MAIN_DB_PREFIX."socpeople as c,";
$sql.= " ".MAIN_DB_PREFIX."c_type_contact as tc";
$sql.= " WHERE ec.element_id = ".$this->id;
$sql.= " AND ec.fk_socpeople = c.rowid";
if ($source == 'internal') $sql.= " AND c.entity IN (0,".$conf->entity.")";
if ($source == 'external') $sql.= " AND c.entity IN (".getEntity('societe', 1).")";
$sql.= " AND ec.fk_c_type_contact = tc.rowid";
$sql.= " AND tc.element = '".$this->element."'";
$sql.= " AND tc.source = '".$source."'";
$sql.= " AND tc.code = '".$code."'";
$sql.= " AND tc.active = 1";
if ($status) $sql.= " AND ec.statut = ".$status;
dol_syslog(get_class($this)."::getIdContact sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
while ($obj = $this->db->fetch_object($resql))
{
$result[$i]=$obj->fk_socpeople;
$i++;
}
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::getIdContact ".$this->error, LOG_ERR);
return null;
}
return $result;
}
/**
* Charge le contact d'id $id dans this->contact
*
* @param int $contactid Id du contact
* @return int <0 if KO, >0 if OK
*/
function fetch_contact($contactid)
{
require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
$contact = new Contact($this->db);
$result=$contact->fetch($contactid);
$this->contact = $contact;
return $result;
}
/**
* Load the third party of object from id $this->socid into this->thirdpary
*
* @return int <0 if KO, >0 if OK
*/
function fetch_thirdparty()
{
global $conf;
if (empty($this->socid)) return 0;
$thirdparty = new Societe($this->db);
$result=$thirdparty->fetch($this->socid);
$this->client = $thirdparty; // deprecated
$this->thirdparty = $thirdparty;
// Use first price level if level not defined for third party
if (! empty($conf->global->PRODUIT_MULTIPRICES) && empty($this->thirdparty->price_level))
{
$this->client->price_level=1; // deprecated
$this->thirdparty->price_level=1;
}
return $result;
}
/**
* Load data for barcode
*
* @return int <0 if KO, >=0 if OK
*/
function fetch_barcode()
{
global $conf;
dol_syslog(get_class($this).'::fetch_barcode this->element='.$this->element.' this->barcode_type='.$this->barcode_type);
$idtype=$this->barcode_type;
if (! $idtype)
{
if ($this->element == 'product') $idtype = $conf->global->PRODUIT_DEFAULT_BARCODE_TYPE;
else if ($this->element == 'societe') $idtype = $conf->global->GENBARCODE_BARCODETYPE_THIRDPARTY;
else dol_print_error('','Call fetch_barcode with barcode_type not defined and cant be guessed');
}
if ($idtype > 0)
{
if (empty($this->barcode_type) || empty($this->barcode_type_code) || empty($this->barcode_type_label) || empty($this->barcode_type_coder)) // If data not already loaded
{
$sql = "SELECT rowid, code, libelle as label, coder";
$sql.= " FROM ".MAIN_DB_PREFIX."c_barcode_type";
$sql.= " WHERE rowid = ".$idtype;
dol_syslog(get_class($this).'::fetch_barcode sql='.$sql);
$resql = $this->db->query($sql);
if ($resql)
{
$obj = $this->db->fetch_object($resql);
$this->barcode_type = $obj->rowid;
$this->barcode_type_code = $obj->code;
$this->barcode_type_label = $obj->label;
$this->barcode_type_coder = $obj->coder;
return 1;
}
else
{
dol_print_error($this->db);
return -1;
}
}
}
else return 0;
}
/**
* Charge le projet d'id $this->fk_project dans this->projet
*
* @return int <0 if KO, >=0 if OK
*/
function fetch_projet()
{
if (empty($this->fk_project)) return 0;
$project = new Project($this->db);
$result = $project->fetch($this->fk_project);
$this->projet = $project;
return $result;
}
/**
* Charge le user d'id userid dans this->user
*
* @param int $userid Id du contact
* @return int <0 if KO, >0 if OK
*/
function fetch_user($userid)
{
$user = new User($this->db);
$result=$user->fetch($userid);
$this->user = $user;
return $result;
}
/**
* Read linked origin object
*
* @return void
*/
function fetch_origin()
{
// TODO uniformise code
if ($this->origin == 'shipping') $this->origin = 'expedition';
if ($this->origin == 'delivery') $this->origin = 'livraison';
$origin = $this->origin;
$classname = ucfirst($origin);
$this->$origin = new $classname($this->db);
$this->$origin->fetch($this->origin_id);
}
/**
* Load object from specific field
*
* @param string $table Table element or element line
* @param string $field Field selected
* @param string $key Import key
* @return int <0 if KO, >0 if OK
*/
function fetchObjectFrom($table,$field,$key)
{
global $conf;
$result=false;
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$table;
$sql.= " WHERE ".$field." = '".$key."'";
$sql.= " AND entity = ".$conf->entity;
dol_syslog(get_class($this).'::fetchObjectFrom sql='.$sql);
$resql = $this->db->query($sql);
if ($resql)
{
$row = $this->db->fetch_row($resql);
$result = $this->fetch($row[0]);
}
return $result;
}
/**
* Load value from specific field
*
* @param string $table Table of element or element line
* @param int $id Element id
* @param string $field Field selected
* @return int <0 if KO, >0 if OK
*/
function getValueFrom($table, $id, $field)
{
$result=false;
$sql = "SELECT ".$field." FROM ".MAIN_DB_PREFIX.$table;
$sql.= " WHERE rowid = ".$id;
dol_syslog(get_class($this).'::getValueFrom sql='.$sql);
$resql = $this->db->query($sql);
if ($resql)
{
$row = $this->db->fetch_row($resql);
$result = $row[0];
}
return $result;
}
/**
* Update a specific field into database
*
* @param string $field Field to update
* @param mixte $value New value
* @param string $table To force other table element or element line (should not be used)
* @param int $id To force other object id (should not be used)
* @param string $format Data format ('text', 'date'). 'text' is used if not defined
* @param string $id_field To force rowid field name. 'rowid' is used it not defined
* @param string $user Update last update fields also if user object provided
* @return int <0 if KO, >0 if OK
*/
function setValueFrom($field, $value, $table='', $id='', $format='', $id_field='', $user='')
{
global $conf;
if (empty($table)) $table=$this->table_element;
if (empty($id)) $id=$this->id;
if (empty($format)) $format='text';
if (empty($id_field)) $id_field='rowid';
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX.$table." SET ";
if ($format == 'text') $sql.= $field." = '".$this->db->escape($value)."'";
else if ($format == 'date') $sql.= $field." = '".$this->db->idate($value)."'";
if (is_object($user)) $sql.=", fk_user_modif = ".$user->id;
$sql.= " WHERE ".$id_field." = ".$id;
dol_syslog(get_class($this)."::".__FUNCTION__." sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->lasterror();
$this->db->rollback();
return -1;
}
}
/**
* Load properties id_previous and id_next
*
* @param string $filter Optional filter
* @param int $fieldid Name of field to use for the select MAX and MIN
* @return int <0 if KO, >0 if OK
*/
function load_previous_next_ref($filter,$fieldid)
{
global $conf, $user;
if (! $this->table_element)
{
dol_print_error('',get_class($this)."::load_previous_next_ref was called on objet with property table_element not defined", LOG_ERR);
return -1;
}
// this->ismultientitymanaged contains
// 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
$alias = 's';
if ($this->element == 'societe') $alias = 'te';
$sql = "SELECT MAX(te.".$fieldid.")";
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." as te";
if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 2 || ($this->element != 'societe' && empty($this->isnolinkedbythird) && empty($user->rights->societe->client->voir))) $sql.= ", ".MAIN_DB_PREFIX."societe as s"; // If we need to link to societe to limit select to entity
if (empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON ".$alias.".rowid = sc.fk_soc";
$sql.= " WHERE te.".$fieldid." < '".$this->db->escape($this->ref)."'";
if (empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir) $sql.= " AND sc.fk_user = " .$user->id;
if (! empty($filter)) $sql.=" AND ".$filter;
if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 2 || ($this->element != 'societe' && empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir)) $sql.= ' AND te.fk_soc = s.rowid'; // If we need to link to societe to limit select to entity
if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) $sql.= ' AND te.entity IN ('.getEntity($this->element, 1).')';
//print $sql."<br>";
$result = $this->db->query($sql);
if (! $result)
{
$this->error=$this->db->error();
return -1;
}
$row = $this->db->fetch_row($result);
$this->ref_previous = $row[0];
$sql = "SELECT MIN(te.".$fieldid.")";
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." as te";
if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 2 || ($this->element != 'societe' && empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir)) $sql.= ", ".MAIN_DB_PREFIX."societe as s"; // If we need to link to societe to limit select to entity
if (empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON ".$alias.".rowid = sc.fk_soc";
$sql.= " WHERE te.".$fieldid." > '".$this->db->escape($this->ref)."'";
if (empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir) $sql.= " AND sc.fk_user = " .$user->id;
if (! empty($filter)) $sql.=" AND ".$filter;
if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 2 || ($this->element != 'societe' && empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir)) $sql.= ' AND te.fk_soc = s.rowid'; // If we need to link to societe to limit select to entity
if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) $sql.= ' AND te.entity IN ('.getEntity($this->element, 1).')';
// Rem: Bug in some mysql version: SELECT MIN(rowid) FROM llx_socpeople WHERE rowid > 1 when one row in database with rowid=1, returns 1 instead of null
//print $sql."<br>";
$result = $this->db->query($sql);
if (! $result)
{
$this->error=$this->db->error();
return -2;
}
$row = $this->db->fetch_row($result);
$this->ref_next = $row[0];
return 1;
}
/**
* Return list of id of contacts of project
*
* @param string $source Source of contact: external (llx_socpeople) or internal (llx_user) or thirdparty (llx_societe)
* @return array Array of id of contacts (if source=external or internal)
* Array of id of third parties with at least one contact on project (if source=thirdparty)
*/
function getListContactId($source='external')
{
$contactAlreadySelected = array();
$tab = $this->liste_contact(-1,$source);
$num=count($tab);
$i = 0;
while ($i < $num)
{
if ($source == 'thirdparty') $contactAlreadySelected[$i] = $tab[$i]['socid'];
else $contactAlreadySelected[$i] = $tab[$i]['id'];
$i++;
}
return $contactAlreadySelected;
}
/**
* Link element with a project
*
* @param int $projectid Project id to link element to
* @return int <0 if KO, >0 if OK
*/
function setProject($projectid)
{
if (! $this->table_element)
{
dol_syslog(get_class($this)."::setProject was called on objet with property table_element not defined",LOG_ERR);
return -1;
}
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
if ($projectid) $sql.= ' SET fk_projet = '.$projectid;
else $sql.= ' SET fk_projet = NULL';
$sql.= ' WHERE rowid = '.$this->id;
dol_syslog(get_class($this)."::setProject sql=".$sql);
if ($this->db->query($sql))
{
$this->fk_project = $projectid;
return 1;
}
else
{
dol_print_error($this->db);
return -1;
}
}
/**
* Change the payments methods
*
* @param int $id Id of new payment method
* @return int >0 if OK, <0 if KO
*/
function setPaymentMethods($id)
{
dol_syslog(get_class($this).'::setPaymentMethods('.$id.')');
if ($this->statut >= 0 || $this->element == 'societe')
{
// TODO uniformize field name
$fieldname = 'fk_mode_reglement';
if ($this->element == 'societe') $fieldname = 'mode_reglement';
if (get_class($this) == 'Fournisseur') $fieldname = 'mode_reglement_supplier';
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
$sql .= ' SET '.$fieldname.' = '.$id;
$sql .= ' WHERE rowid='.$this->id;
if ($this->db->query($sql))
{
$this->mode_reglement_id = $id;
return 1;
}
else
{
dol_syslog(get_class($this).'::setPaymentMethods Erreur '.$sql.' - '.$this->db->error());
$this->error=$this->db->error();
return -1;
}
}
else
{
dol_syslog(get_class($this).'::setPaymentMethods, status of the object is incompatible');
$this->error='Status of the object is incompatible '.$this->statut;
return -2;
}
}
/**
* Change the payments terms
*
* @param int $id Id of new payment terms
* @return int >0 if OK, <0 if KO
*/
function setPaymentTerms($id)
{
dol_syslog(get_class($this).'::setPaymentTerms('.$id.')');
if ($this->statut >= 0 || $this->element == 'societe')
{
// TODO uniformize field name
$fieldname = 'fk_cond_reglement';
if ($this->element == 'societe') $fieldname = 'cond_reglement';
if (get_class($this) == 'Fournisseur') $fieldname = 'cond_reglement_supplier';
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
$sql .= ' SET '.$fieldname.' = '.$id;
$sql .= ' WHERE rowid='.$this->id;
if ($this->db->query($sql))
{
$this->cond_reglement_id = $id;
$this->cond_reglement = $id; // for compatibility
return 1;
}
else
{
dol_syslog(get_class($this).'::setPaymentTerms Erreur '.$sql.' - '.$this->db->error());
$this->error=$this->db->error();
return -1;
}
}
else
{
dol_syslog(get_class($this).'::setPaymentTerms, status of the object is incompatible');
$this->error='Status of the object is incompatible '.$this->statut;
return -2;
}
}
/**
* Define delivery address
*
* @param int $id Address id
* @return int <0 si ko, >0 si ok
*/
function setDeliveryAddress($id)
{
$fieldname = 'fk_delivery_address';