dolibarr  9.0.0
societe.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2002-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
5  * Copyright (C) 2003 Brian Fraval <brian@fraval.org>
6  * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
7  * Copyright (C) 2005-2017 Regis Houssin <regis.houssin@inodbox.com>
8  * Copyright (C) 2008 Patrick Raguin <patrick.raguin@auguria.net>
9  * Copyright (C) 2010-2018 Juanjo Menent <jmenent@2byte.es>
10  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
11  * Copyright (C) 2013 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
12  * Copyright (C) 2013 Peter Fontaine <contact@peterfontaine.fr>
13  * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
14  * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
15  * Copyright (C) 2017 Rui Strecht <rui.strecht@aliartalentos.com>
16  * Copyright (C) 2018 Philippe Grand <philippe.grand@atoo-net.com>
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <http://www.gnu.org/licenses/>.
30  */
31 
37 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
38 require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
39 
43 class Societe extends CommonObject
44 {
48  public $element='societe';
49 
53  public $table_element = 'societe';
54 
58  public $fk_element='fk_soc';
59 
60  public $fieldsforcombobox='nom,name_alias';
61  protected $childtables=array("supplier_proposal"=>'SupplierProposal',"propal"=>'Proposal',"commande"=>'Order',"facture"=>'Invoice',"facture_rec"=>'RecurringInvoiceTemplate',"contrat"=>'Contract',"fichinter"=>'Fichinter',"facture_fourn"=>'SupplierInvoice',"commande_fournisseur"=>'SupplierOrder',"projet"=>'Project',"expedition"=>'Shipment',"prelevement_lignes"=>'DirectDebitRecord'); // To test if we can delete object
62  protected $childtablesoncascade=array("societe_prices", "societe_log", "societe_address", "product_fournisseur_price", "product_customer_price_log", "product_customer_price", "socpeople", "adherent", "societe_rib", "societe_remise", "societe_remise_except", "societe_commerciaux", "categorie", "notify", "notify_def", "actioncomm");
63  public $picto = 'company';
64 
69  public $ismultientitymanaged = 1;
70 
75  public $restrictiononfksoc = 1;
76 
77 
78  // BEGIN MODULEBUILDER PROPERTIES
82  public $fields=array(
83  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'index'=>1, 'position'=>1, 'comment'=>'Id'),
84  'nom' =>array('type'=>'varchar(128)', 'label'=>'Name', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'comment'=>'Reference of object'),
85  'name_alias' =>array('type'=>'varchar(128)', 'label'=>'Name', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'comment'=>'Reference of object'),
86  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'default'=>1, 'notnull'=>1, 'index'=>1, 'position'=>20),
87  'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>60),
88  'note_private' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>61),
89  'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>500),
90  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>501),
91  //'date_valid' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>502),
92  'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>510),
93  'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511),
94  //'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512),
95  'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'index'=>1, 'position'=>1000),
96  );
97 
98 
102  public $entity;
103 
110  public $nom;
111 
115  public $name;
116 
121  public $name_alias;
122 
123  public $particulier;
124 
128  public $address;
129 
130  public $zip;
131  public $town;
132 
137  public $status=1;
138 
143  public $state_id;
144  public $state_code;
145  public $state;
146 
151  public $region_code;
152  public $region;
153 
160  public $departement_code;
161 
167  public $departement;
168 
174  public $pays;
175 
180  public $phone;
185  public $fax;
190  public $email;
195  public $skype;
200  public $twitter;
205  public $facebook;
210  public $url;
211 
213 
217  public $barcode;
218 
219  // 6 professional id (usage depends on country)
220 
225  public $idprof1;
226 
231  public $idprof2;
232 
237  public $idprof3;
238 
243  public $idprof4;
244 
249  public $idprof5;
250 
255  public $idprof6;
256 
257  public $prefix_comm;
258 
259  public $tva_assuj=1;
264  public $tva_intra;
265 
266  // Local taxes
267  public $localtax1_assuj;
268  public $localtax1_value;
269  public $localtax2_assuj;
270  public $localtax2_value;
271 
272  public $managers;
273  public $capital;
274  public $typent_id=0;
275  public $typent_code;
276  public $effectif;
277  public $effectif_id=0;
278  public $forme_juridique_code;
279  public $forme_juridique=0;
280 
281  public $remise_percent;
282  public $remise_supplier_percent;
283  public $mode_reglement_supplier_id;
284  public $cond_reglement_supplier_id;
285 
289  public $fk_prospectlevel;
290 
291  public $name_bis;
292 
293  //Log data
294 
299  public $date_modification;
304  public $user_modification;
309  public $date_creation;
314  public $user_creation;
315 
316 
317  public $specimen;
318 
323  public $client=0;
328  public $prospect=0;
333  public $fournisseur;
334 
339  public $code_client;
340 
345  public $code_fournisseur;
346 
351  public $code_compta;
352 
357  public $code_compta_client;
358 
363  public $code_compta_fournisseur;
364 
370  public $note;
371 
376  public $note_private;
377 
382  public $note_public;
383 
385  public $stcomm_id;
386  public $statut_commercial;
387 
392  public $price_level;
393  public $outstanding_limit;
394 
399  public $supplier_order_min_amount;
400 
405  public $commercial_id;
410  public $parent;
415  public $default_lang;
416 
420  public $ref;
421 
422  public $ref_int;
429  public $ref_ext;
430 
437  public $import_key;
438 
443  public $webservices_url;
444 
449  public $webservices_key;
450 
451  public $logo;
452  public $logo_small;
453  public $logo_mini;
454 
455  public $array_options;
456 
457  // Incoterms
461  public $fk_incoterms;
462 
463  public $location_incoterms;
464  public $libelle_incoterms; //Used into tooltip
465 
466  // Multicurrency
470  public $fk_multicurrency;
471 
472  public $multicurrency_code;
473 
474 
475  // END MODULEBUILDER PROPERTIES
476 
477 
483  public function __construct($db)
484  {
485  $this->db = $db;
486 
487  $this->client = 0;
488  $this->prospect = 0;
489  $this->fournisseur = 0;
490  $this->typent_id = 0;
491  $this->effectif_id = 0;
492  $this->forme_juridique_code = 0;
493  $this->tva_assuj = 1;
494  $this->status = 1;
495  }
496 
497 
505  function create(User $user)
506  {
507  global $langs,$conf,$mysoc;
508 
509  $error=0;
510 
511  // Clean parameters
512  if (empty($this->status)) $this->status=0;
513  $this->name=$this->name?trim($this->name):trim($this->nom);
514  if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->name=ucwords($this->name);
515  $this->nom=$this->name; // For backward compatibility
516  if (empty($this->client)) $this->client=0;
517  if (empty($this->fournisseur)) $this->fournisseur=0;
518  $this->import_key = trim($this->import_key);
519 
520  if (!empty($this->multicurrency_code)) $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code);
521  if (empty($this->fk_multicurrency))
522  {
523  $this->multicurrency_code = '';
524  $this->fk_multicurrency = 0;
525  }
526 
527  dol_syslog(get_class($this)."::create ".$this->name);
528 
529  $now=dol_now();
530 
531  $this->db->begin();
532 
533  // For automatic creation during create action (not used by Dolibarr GUI, can be used by scripts)
534  if ($this->code_client == -1 || $this->code_client === 'auto') $this->get_codeclient($this,0);
535  if ($this->code_fournisseur == -1 || $this->code_fournisseur === 'auto') $this->get_codefournisseur($this,1);
536 
537  // Check more parameters (including mandatory setup
538  // If error, this->errors[] is filled
539  $result = $this->verify();
540 
541  if ($result >= 0)
542  {
543  $this->entity = ((isset($this->entity) && is_numeric($this->entity))?$this->entity:$conf->entity);
544 
545  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe (nom, name_alias, entity, datec, fk_user_creat, canvas, status, ref_int, ref_ext, fk_stcomm, fk_incoterms, location_incoterms ,import_key, fk_multicurrency, multicurrency_code)";
546  $sql.= " VALUES ('".$this->db->escape($this->name)."', '".$this->db->escape($this->name_alias)."', ".$this->db->escape($this->entity).", '".$this->db->idate($now)."'";
547  $sql.= ", ".(! empty($user->id) ? "'".$user->id."'":"null");
548  $sql.= ", ".(! empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'":"null");
549  $sql.= ", ".$this->status;
550  $sql.= ", ".(! empty($this->ref_int) ? "'".$this->db->escape($this->ref_int)."'":"null");
551  $sql.= ", ".(! empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'":"null");
552  $sql.= ", 0";
553  $sql.= ", ".(int) $this->fk_incoterms;
554  $sql.= ", '".$this->db->escape($this->location_incoterms)."'";
555  $sql.= ", ".(! empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'":"null");
556  $sql.= ", ".(int) $this->fk_multicurrency;
557  $sql.= ", '".$this->db->escape($this->multicurrency_code)."')";
558 
559  dol_syslog(get_class($this)."::create", LOG_DEBUG);
560  $result=$this->db->query($sql);
561  if ($result)
562  {
563  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe");
564 
565  $ret = $this->update($this->id,$user,0,1,1,'add');
566 
567  // Ajout du commercial affecte
568  if ($this->commercial_id != '' && $this->commercial_id != -1)
569  {
570  $this->add_commercial($user, $this->commercial_id);
571  }
572  // si un commercial cree un client il lui est affecte automatiquement
573  elseif (empty($user->rights->societe->client->voir))
574  {
575  $this->add_commercial($user, $user->id);
576  }
577 
578  if ($ret >= 0)
579  {
580  // Call trigger
581  $result=$this->call_trigger('COMPANY_CREATE',$user);
582  if ($result < 0) $error++;
583  // End call triggers
584  }
585  else $error++;
586 
587  if (! $error)
588  {
589  dol_syslog(get_class($this)."::Create success id=".$this->id);
590  $this->db->commit();
591  return $this->id;
592  }
593  else
594  {
595  dol_syslog(get_class($this)."::Create echec update ".$this->error." ".join(',',$this->errors), LOG_ERR);
596  $this->db->rollback();
597  return -4;
598  }
599  }
600  else
601  {
602  if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
603  {
604  $this->error=$langs->trans("ErrorCompanyNameAlreadyExists",$this->name); // duplicate on a field (code or profid or ...)
605  $result=-1;
606  }
607  else
608  {
609  $this->error=$this->db->lasterror();
610  $result=-2;
611  }
612  $this->db->rollback();
613  return $result;
614  }
615  }
616  else
617  {
618  $this->db->rollback();
619  dol_syslog(get_class($this)."::Create fails verify ".join(',',$this->errors), LOG_WARNING);
620  return -3;
621  }
622  }
623 
624 
625  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
632  function create_individual(User $user)
633  {
634  // phpcs:enable
635  require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
636  $contact=new Contact($this->db);
637 
638  $contact->name = $this->name_bis;
639  $contact->firstname = $this->firstname;
640  $contact->civility_id = $this->civility_id;
641  $contact->socid = $this->id; // fk_soc
642  $contact->statut = 1;
643  $contact->priv = 0;
644  $contact->country_id = $this->country_id;
645  $contact->state_id = $this->state_id;
646  $contact->address = $this->address;
647  $contact->email = $this->email;
648  $contact->zip = $this->zip;
649  $contact->town = $this->town;
650  $contact->phone_pro = $this->phone;
651 
652  $result = $contact->create($user);
653  if ($result < 0)
654  {
655  $this->error = $contact->error;
656  $this->errors = $contact->errors;
657  dol_syslog(get_class($this)."::create_individual ERROR:" . $this->error, LOG_ERR);
658  }
659 
660  return $result;
661  }
662 
669  function verify()
670  {
671  global $conf, $langs, $mysoc;
672 
673  $error = 0;
674  $this->errors=array();
675 
676  $result = 0;
677  $this->name = trim($this->name);
678  $this->nom=$this->name; // For backward compatibility
679 
680  if (! $this->name)
681  {
682  $this->errors[] = 'ErrorBadThirdPartyName';
683  $result = -2;
684  }
685 
686  if ($this->client)
687  {
688  $rescode = $this->check_codeclient();
689  if ($rescode <> 0)
690  {
691  if ($rescode == -1)
692  {
693  $this->errors[] = 'ErrorBadCustomerCodeSyntax';
694  }
695  elseif ($rescode == -2)
696  {
697  $this->errors[] = 'ErrorCustomerCodeRequired';
698  }
699  elseif ($rescode == -3)
700  {
701  $this->errors[] = 'ErrorCustomerCodeAlreadyUsed';
702  }
703  elseif ($rescode == -4)
704  {
705  $this->errors[] = 'ErrorPrefixRequired';
706  }
707  $result = -3;
708  }
709  }
710 
711  if ($this->fournisseur)
712  {
713  $rescode = $this->check_codefournisseur();
714  if ($rescode <> 0)
715  {
716  if ($rescode == -1)
717  {
718  $this->errors[] = 'ErrorBadSupplierCodeSyntax';
719  }
720  elseif ($rescode == -2)
721  {
722  $this->errors[] = 'ErrorSupplierCodeRequired';
723  }
724  elseif ($rescode == -3)
725  {
726  $this->errors[] = 'ErrorSupplierCodeAlreadyUsed';
727  }
728  elseif ($rescode == -5)
729  {
730  $this->errors[] = 'ErrorprefixRequired';
731  }
732  $result = -3;
733  }
734  }
735 
736  // Check for duplicate or mandatory fields defined into setup
737  $array_to_check=array('IDPROF1','IDPROF2','IDPROF3','IDPROF4','IDPROF5','IDPROF6','EMAIL');
738  foreach($array_to_check as $key)
739  {
740  $keymin=strtolower($key);
741  $i=(int) preg_replace('/[^0-9]/','',$key);
742  $vallabel=$this->$keymin;
743 
744  if ($i > 0)
745  {
746  if ($this->isACompany())
747  {
748  // Check for unicity
749  if ($vallabel && $this->id_prof_verifiable($i))
750  {
751  if ($this->id_prof_exists($keymin, $vallabel, ($this->id > 0 ? $this->id : 0)))
752  {
753  $langs->load("errors");
754  $error++; $this->errors[] = $langs->transcountry('ProfId'.$i, $this->country_code)." ".$langs->trans("ErrorProdIdAlreadyExist", $vallabel).' ('.$langs->trans("ForbiddenBySetupRules").')';
755  }
756  }
757 
758  // Check for mandatory prof id (but only if country is other than ours)
759  if ($mysoc->country_id > 0 && $this->country_id == $mysoc->country_id)
760  {
761  $idprof_mandatory ='SOCIETE_'.$key.'_MANDATORY';
762  if (! $vallabel && ! empty($conf->global->$idprof_mandatory))
763  {
764  $langs->load("errors");
765  $error++;
766  $this->errors[] = $langs->trans("ErrorProdIdIsMandatory", $langs->transcountry('ProfId'.$i, $this->country_code)).' ('.$langs->trans("ForbiddenBySetupRules").')';
767  }
768  }
769  }
770  }
771  else
772  {
773  //var_dump($conf->global->SOCIETE_EMAIL_UNIQUE);
774  //var_dump($conf->global->SOCIETE_EMAIL_MANDATORY);
775  if ($key == 'EMAIL')
776  {
777  // Check for unicity
778  if ($vallabel && ! empty($conf->global->SOCIETE_EMAIL_UNIQUE))
779  {
780  if ($this->id_prof_exists($keymin, $vallabel, ($this->id > 0 ? $this->id : 0)))
781  {
782  $langs->load("errors");
783  $error++; $this->errors[] = $langs->trans('Email')." ".$langs->trans("ErrorProdIdAlreadyExist", $vallabel).' ('.$langs->trans("ForbiddenBySetupRules").')';
784  }
785  }
786 
787  // Check for mandatory
788  if (! empty($conf->global->SOCIETE_EMAIL_MANDATORY) && ! isValidEMail($this->email))
789  {
790  $langs->load("errors");
791  $error++;
792  $this->errors[] = $langs->trans("ErrorBadEMail", $this->email).' ('.$langs->trans("ForbiddenBySetupRules").')';
793  }
794  }
795  }
796  }
797 
798  if ($error) $result = -4;
799 
800  return $result;
801  }
802 
815  function update($id, $user='', $call_trigger=1, $allowmodcodeclient=0, $allowmodcodefournisseur=0, $action='update', $nosyncmember=1)
816  {
817  global $langs,$conf,$hookmanager;
818 
819  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
820 
821  if (empty($id)) $id = $this->id;
822 
823  $error=0;
824 
825  dol_syslog(get_class($this)."::Update id=".$id." call_trigger=".$call_trigger." allowmodcodeclient=".$allowmodcodeclient." allowmodcodefournisseur=".$allowmodcodefournisseur);
826 
827  $now=dol_now();
828 
829  // Clean parameters
830  $this->id = $id;
831  $this->entity = ((isset($this->entity) && is_numeric($this->entity))?$this->entity:$conf->entity);
832  $this->name = $this->name?trim($this->name):trim($this->nom);
833  $this->nom = $this->name; // For backward compatibility
834  $this->name_alias = trim($this->name_alias);
835  $this->ref_ext = trim($this->ref_ext);
836  $this->address = $this->address?trim($this->address):trim($this->address);
837  $this->zip = $this->zip?trim($this->zip):trim($this->zip);
838  $this->town = $this->town?trim($this->town):trim($this->town);
839  $this->state_id = trim($this->state_id);
840  $this->country_id = ($this->country_id > 0)?$this->country_id:0;
841  $this->phone = trim($this->phone);
842  $this->phone = preg_replace("/\s/","",$this->phone);
843  $this->phone = preg_replace("/\./","",$this->phone);
844  $this->fax = trim($this->fax);
845  $this->fax = preg_replace("/\s/","",$this->fax);
846  $this->fax = preg_replace("/\./","",$this->fax);
847  $this->email = trim($this->email);
848  $this->skype = trim($this->skype);
849  $this->twitter = trim($this->twitter);
850  $this->facebook = trim($this->facebook);
851  $this->url = $this->url?clean_url($this->url,0):'';
852  $this->note_private = trim($this->note_private);
853  $this->note_public = trim($this->note_public);
854  $this->idprof1 = trim($this->idprof1);
855  $this->idprof2 = trim($this->idprof2);
856  $this->idprof3 = trim($this->idprof3);
857  $this->idprof4 = trim($this->idprof4);
858  $this->idprof5 = (! empty($this->idprof5)?trim($this->idprof5):'');
859  $this->idprof6 = (! empty($this->idprof6)?trim($this->idprof6):'');
860  $this->prefix_comm = trim($this->prefix_comm);
861  $this->outstanding_limit = price2num($this->outstanding_limit);
862  $this->order_min_amount = price2num($this->order_min_amount);
863  $this->supplier_order_min_amount = price2num($this->supplier_order_min_amount);
864 
865  $this->tva_assuj = trim($this->tva_assuj);
866  $this->tva_intra = dol_sanitizeFileName($this->tva_intra,'');
867  if (empty($this->status)) $this->status = 0;
868 
869  if (!empty($this->multicurrency_code)) $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code);
870  if (empty($this->fk_multicurrency))
871  {
872  $this->multicurrency_code = '';
873  $this->fk_multicurrency = 0;
874  }
875 
876  // Local taxes
877  $this->localtax1_assuj=trim($this->localtax1_assuj);
878  $this->localtax2_assuj=trim($this->localtax2_assuj);
879 
880  $this->localtax1_value=trim($this->localtax1_value);
881  $this->localtax2_value=trim($this->localtax2_value);
882 
883  if ($this->capital != '') $this->capital=price2num(trim($this->capital));
884  if (! is_numeric($this->capital)) $this->capital = ''; // '' = undef
885 
886  $this->effectif_id=trim($this->effectif_id);
887  $this->forme_juridique_code=trim($this->forme_juridique_code);
888 
889  //Gencod
890  $this->barcode=trim($this->barcode);
891 
892  // For automatic creation
893  if ($this->code_client == -1 || $this->code_client === 'auto') $this->get_codeclient($this,0);
894  if ($this->code_fournisseur == -1 || $this->code_fournisseur === 'auto') $this->get_codefournisseur($this,1);
895 
896  $this->code_compta=trim($this->code_compta);
897  $this->code_compta_fournisseur=trim($this->code_compta_fournisseur);
898 
899  // Check parameters. More tests are done later in the ->verify()
900  if (! is_numeric($this->client) && ! is_numeric($this->fournisseur))
901  {
902  $langs->load("errors");
903  $this->error = $langs->trans("BadValueForParameterClientOrSupplier");
904  return -1;
905  }
906 
907  $customer=false;
908  if (! empty($allowmodcodeclient) && ! empty($this->client))
909  {
910  // Attention get_codecompta peut modifier le code suivant le module utilise
911  if (empty($this->code_compta))
912  {
913  $ret=$this->get_codecompta('customer');
914  if ($ret < 0) return -1;
915  }
916 
917  $customer=true;
918  }
919 
920  $supplier=false;
921  if (! empty($allowmodcodefournisseur) && ! empty($this->fournisseur))
922  {
923  // Attention get_codecompta peut modifier le code suivant le module utilise
924  if (empty($this->code_compta_fournisseur))
925  {
926  $ret=$this->get_codecompta('supplier');
927  if ($ret < 0) return -1;
928  }
929 
930  $supplier=true;
931  }
932 
933  //Web services
934  $this->webservices_url = $this->webservices_url?clean_url($this->webservices_url,0):'';
935  $this->webservices_key = trim($this->webservices_key);
936 
937  //Incoterms
938  $this->fk_incoterms = (int) $this->fk_incoterms;
939  $this->location_incoterms = trim($this->location_incoterms);
940 
941  $this->db->begin();
942 
943  // Check name is required and codes are ok or unique.
944  // If error, this->errors[] is filled
945  $result = 0;
946  if ($action != 'add' && $action != 'merge')
947  {
948  // We don't check when update called during a create because verify was already done.
949  // For a merge, we suppose source data is clean and a customer code of a deleted thirdparty must be accepted into a target thirdparty with empty code without duplicate error
950  $result = $this->verify();
951 
952  // If there is only one error and error is ErrorBadCustomerCodeSyntax and we don't change customer code, we allow the update
953  // So we can update record that were using and old numbering rule.
954  if (is_array($this->errors))
955  {
956  if (in_array('ErrorBadCustomerCodeSyntax', $this->errors) && is_object($this->oldcopy) && $this->oldcopy->code_client == $this->code_client)
957  {
958  if (($key = array_search('ErrorBadCustomerCodeSyntax', $this->errors)) !== false) unset($this->errors[$key]); // Remove error message
959  }
960  if (in_array('ErrorBadSupplierCodeSyntax', $this->errors) && is_object($this->oldcopy) && $this->oldcopy->code_fournisseur == $this->code_fournisseur)
961  {
962  if (($key = array_search('ErrorBadSupplierCodeSyntax', $this->errors)) !== false) unset($this->errors[$key]); // Remove error message
963  }
964  if (empty($this->errors)) // If there is no more error, we can make like if there is no error at all
965  {
966  $result = 0;
967  }
968  }
969  }
970 
971  if ($result >= 0)
972  {
973  dol_syslog(get_class($this)."::update verify ok or not done");
974 
975  $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET ";
976  $sql .= "entity = " . $this->db->escape($this->entity);
977  $sql .= ",nom = '" . $this->db->escape($this->name) ."'"; // Required
978  $sql .= ",name_alias = '" . $this->db->escape($this->name_alias) ."'";
979  $sql .= ",ref_ext = " .(! empty($this->ref_ext)?"'".$this->db->escape($this->ref_ext) ."'":"null");
980  $sql .= ",address = '" . $this->db->escape($this->address) ."'";
981 
982  $sql .= ",zip = ".(! empty($this->zip)?"'".$this->db->escape($this->zip)."'":"null");
983  $sql .= ",town = ".(! empty($this->town)?"'".$this->db->escape($this->town)."'":"null");
984 
985  $sql .= ",fk_departement = '" . (! empty($this->state_id)?$this->state_id:'0') ."'";
986  $sql .= ",fk_pays = '" . (! empty($this->country_id)?$this->country_id:'0') ."'";
987 
988  $sql .= ",phone = ".(! empty($this->phone)?"'".$this->db->escape($this->phone)."'":"null");
989  $sql .= ",fax = ".(! empty($this->fax)?"'".$this->db->escape($this->fax)."'":"null");
990  $sql .= ",email = ".(! empty($this->email)?"'".$this->db->escape($this->email)."'":"null");
991  $sql .= ",skype = ".(! empty($this->skype)?"'".$this->db->escape($this->skype)."'":"null");
992  $sql .= ",twitter = ".(! empty($this->twitter)?"'".$this->db->escape($this->twitter)."'":"null");
993  $sql .= ",facebook = ".(! empty($this->facebook)?"'".$this->db->escape($this->facebook)."'":"null");
994  $sql .= ",url = ".(! empty($this->url)?"'".$this->db->escape($this->url)."'":"null");
995 
996  $sql .= ",parent = " . ($this->parent > 0 ? $this->parent : "null");
997 
998  $sql .= ",note_private = ".(! empty($this->note_private)?"'".$this->db->escape($this->note_private)."'":"null");
999  $sql .= ",note_public = ".(! empty($this->note_public)?"'".$this->db->escape($this->note_public)."'":"null");
1000 
1001  $sql .= ",siren = '". $this->db->escape($this->idprof1) ."'";
1002  $sql .= ",siret = '". $this->db->escape($this->idprof2) ."'";
1003  $sql .= ",ape = '". $this->db->escape($this->idprof3) ."'";
1004  $sql .= ",idprof4 = '". $this->db->escape($this->idprof4) ."'";
1005  $sql .= ",idprof5 = '". $this->db->escape($this->idprof5) ."'";
1006  $sql .= ",idprof6 = '". $this->db->escape($this->idprof6) ."'";
1007 
1008  $sql .= ",tva_assuj = ".($this->tva_assuj!=''?"'".$this->db->escape($this->tva_assuj)."'":"null");
1009  $sql .= ",tva_intra = '" . $this->db->escape($this->tva_intra) ."'";
1010  $sql .= ",status = " .$this->status;
1011 
1012  // Local taxes
1013  $sql .= ",localtax1_assuj = ".($this->localtax1_assuj!=''?"'".$this->db->escape($this->localtax1_assuj)."'":"null");
1014  $sql .= ",localtax2_assuj = ".($this->localtax2_assuj!=''?"'".$this->db->escape($this->localtax2_assuj)."'":"null");
1015  if($this->localtax1_assuj==1)
1016  {
1017  if($this->localtax1_value!='')
1018  {
1019  $sql .=",localtax1_value =".$this->localtax1_value;
1020  }
1021  else $sql .=",localtax1_value =0.000";
1022  }
1023  else $sql .=",localtax1_value =0.000";
1024 
1025  if($this->localtax2_assuj==1)
1026  {
1027  if($this->localtax2_value!='')
1028  {
1029  $sql .=",localtax2_value =".$this->localtax2_value;
1030  }
1031  else $sql .=",localtax2_value =0.000";
1032  }
1033  else $sql .=",localtax2_value =0.000";
1034 
1035  $sql .= ",capital = ".($this->capital == '' ? "null" : $this->capital);
1036 
1037  $sql .= ",prefix_comm = ".(! empty($this->prefix_comm)?"'".$this->db->escape($this->prefix_comm)."'":"null");
1038 
1039  $sql .= ",fk_effectif = ".(! empty($this->effectif_id)?"'".$this->db->escape($this->effectif_id)."'":"null");
1040  if (isset($this->stcomm_id))
1041  {
1042  $sql .= ",fk_stcomm=".(!empty($this->stcomm_id) ? $this->stcomm_id : "0");
1043  }
1044  $sql .= ",fk_typent = ".(! empty($this->typent_id)?"'".$this->db->escape($this->typent_id)."'":"0");
1045 
1046  $sql .= ",fk_forme_juridique = ".(! empty($this->forme_juridique_code)?"'".$this->db->escape($this->forme_juridique_code)."'":"null");
1047 
1048  $sql .= ",mode_reglement = ".(! empty($this->mode_reglement_id)?"'".$this->db->escape($this->mode_reglement_id)."'":"null");
1049  $sql .= ",cond_reglement = ".(! empty($this->cond_reglement_id)?"'".$this->db->escape($this->cond_reglement_id)."'":"null");
1050  $sql .= ",mode_reglement_supplier = ".(! empty($this->mode_reglement_supplier_id)?"'".$this->db->escape($this->mode_reglement_supplier_id)."'":"null");
1051  $sql .= ",cond_reglement_supplier = ".(! empty($this->cond_reglement_supplier_id)?"'".$this->db->escape($this->cond_reglement_supplier_id)."'":"null");
1052  $sql .= ",fk_shipping_method = ".(! empty($this->shipping_method_id)?"'".$this->db->escape($this->shipping_method_id)."'":"null");
1053 
1054  $sql .= ",client = " . (! empty($this->client)?$this->client:0);
1055  $sql .= ",fournisseur = " . (! empty($this->fournisseur)?$this->fournisseur:0);
1056  $sql .= ",barcode = ".(! empty($this->barcode)?"'".$this->db->escape($this->barcode)."'":"null");
1057  $sql .= ",default_lang = ".(! empty($this->default_lang)?"'".$this->db->escape($this->default_lang)."'":"null");
1058  $sql .= ",logo = ".(! empty($this->logo)?"'".$this->db->escape($this->logo)."'":"null");
1059  $sql .= ",outstanding_limit= ".($this->outstanding_limit!=''?$this->outstanding_limit:'null');
1060  $sql .= ",order_min_amount= ".($this->order_min_amount!=''?$this->order_min_amount:'null');
1061  $sql .= ",supplier_order_min_amount= ".($this->supplier_order_min_amount!=''?$this->supplier_order_min_amount:'null');
1062  $sql .= ",fk_prospectlevel='".$this->db->escape($this->fk_prospectlevel)."'";
1063 
1064  $sql .= ",webservices_url = ".(! empty($this->webservices_url)?"'".$this->db->escape($this->webservices_url)."'":"null");
1065  $sql .= ",webservices_key = ".(! empty($this->webservices_key)?"'".$this->db->escape($this->webservices_key)."'":"null");
1066 
1067  //Incoterms
1068  $sql.= ", fk_incoterms = ".$this->fk_incoterms;
1069  $sql.= ", location_incoterms = ".(! empty($this->location_incoterms)?"'".$this->db->escape($this->location_incoterms)."'":"null");
1070 
1071  if ($customer)
1072  {
1073  $sql .= ", code_client = ".(! empty($this->code_client)?"'".$this->db->escape($this->code_client)."'":"null");
1074  $sql .= ", code_compta = ".(! empty($this->code_compta)?"'".$this->db->escape($this->code_compta)."'":"null");
1075  }
1076 
1077  if ($supplier)
1078  {
1079  $sql .= ", code_fournisseur = ".(! empty($this->code_fournisseur)?"'".$this->db->escape($this->code_fournisseur)."'":"null");
1080  $sql .= ", code_compta_fournisseur = ".(! empty($this->code_compta_fournisseur)?"'".$this->db->escape($this->code_compta_fournisseur)."'":"null");
1081  }
1082  $sql .= ", fk_user_modif = ".($user->id > 0 ? $user->id:"null");
1083  $sql .= ", fk_multicurrency = ".(int) $this->fk_multicurrency;
1084  $sql .= ", multicurrency_code = '".$this->db->escape($this->multicurrency_code)."'";
1085  $sql .= " WHERE rowid = " . (int) $id;
1086 
1087  $resql=$this->db->query($sql);
1088  if ($resql)
1089  {
1090  if (is_object($this->oldcopy)) // If we have information on old values
1091  {
1092  if ($this->oldcopy->country_id != $this->country_id)
1093  {
1094  unset($this->country_code);
1095  unset($this->country);
1096  }
1097  if ($this->oldcopy->state_id != $this->state_id)
1098  {
1099  unset($this->state_code);
1100  unset($this->state);
1101  }
1102  }
1103  else
1104  {
1105  unset($this->country_code); // We clean this, in the doubt, because it may have been changed after an update of country_id
1106  unset($this->country);
1107  unset($this->state_code);
1108  unset($this->state);
1109  }
1110 
1111  $nbrowsaffected = $this->db->affected_rows($resql);
1112 
1113  if (! $error && $nbrowsaffected)
1114  {
1115  // Update information on linked member if it is an update
1116  if (! $nosyncmember && ! empty($conf->adherent->enabled))
1117  {
1118  require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
1119 
1120  dol_syslog(get_class($this)."::update update linked member");
1121 
1122  $lmember=new Adherent($this->db);
1123  $result=$lmember->fetch(0, 0, $this->id);
1124 
1125  if ($result > 0)
1126  {
1127  $lmember->societe=$this->name;
1128  //$lmember->firstname=$this->firstname?$this->firstname:$lmember->firstname; // We keep firstname and lastname of member unchanged
1129  //$lmember->lastname=$this->lastname?$this->lastname:$lmember->lastname; // We keep firstname and lastname of member unchanged
1130  $lmember->address=$this->address;
1131  $lmember->email=$this->email;
1132  $lmember->skype=$this->skype;
1133  $lmember->twitter=$this->twitter;
1134  $lmember->facebook=$this->facebook;
1135  $lmember->phone=$this->phone;
1136 
1137  $result=$lmember->update($user,0,1,1,1); // Use nosync to 1 to avoid cyclic updates
1138  if ($result < 0)
1139  {
1140  $this->error=$lmember->error;
1141  dol_syslog(get_class($this)."::update ".$this->error,LOG_ERR);
1142  $error++;
1143  }
1144  }
1145  elseif ($result < 0)
1146  {
1147  $this->error=$lmember->error;
1148  $error++;
1149  }
1150  }
1151  }
1152 
1153  $action='update';
1154 
1155  // Actions on extra fields
1156  if (! $error && empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
1157  {
1158  $result=$this->insertExtraFields();
1159  if ($result < 0)
1160  {
1161  $error++;
1162  }
1163  }
1164 
1165  if (! $error && $call_trigger)
1166  {
1167  // Call trigger
1168  $result=$this->call_trigger('COMPANY_MODIFY',$user);
1169  if ($result < 0) $error++;
1170  // End call triggers
1171  }
1172 
1173  if (! $error)
1174  {
1175  dol_syslog(get_class($this)."::Update success");
1176  $this->db->commit();
1177  return 1;
1178  }
1179  else
1180  {
1181  $this->db->rollback();
1182  return -1;
1183  }
1184  }
1185  else
1186  {
1187  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
1188  {
1189  // Doublon
1190  $this->error = $langs->trans("ErrorDuplicateField");
1191  $result = -1;
1192  }
1193  else
1194  {
1195  $this->error = $this->db->lasterror();
1196  $result = -2;
1197  }
1198  $this->db->rollback();
1199  return $result;
1200  }
1201  }
1202  else
1203  {
1204  $this->db->rollback();
1205  dol_syslog(get_class($this)."::Update fails verify ".join(',',$this->errors), LOG_WARNING);
1206  return -3;
1207  }
1208  }
1209 
1227  function fetch($rowid, $ref='', $ref_ext='', $ref_int='', $idprof1='',$idprof2='',$idprof3='',$idprof4='',$idprof5='',$idprof6='', $email='', $ref_alias='')
1228  {
1229  global $langs;
1230  global $conf;
1231 
1232  if (empty($rowid) && empty($ref) && empty($ref_ext) && empty($ref_int) && empty($idprof1) && empty($idprof2) && empty($idprof3) && empty($idprof4) && empty($idprof5) && empty($idprof6) && empty($email)) return -1;
1233 
1234  $sql = 'SELECT s.rowid, s.nom as name, s.name_alias, s.entity, s.ref_ext, s.ref_int, s.address, s.datec as date_creation, s.prefix_comm';
1235  $sql .= ', s.status';
1236  $sql .= ', s.price_level';
1237  $sql .= ', s.tms as date_modification, s.fk_user_creat, s.fk_user_modif';
1238  $sql .= ', s.phone, s.fax, s.email, s.skype, s.twitter, s.facebook, s.url, s.zip, s.town, s.note_private, s.note_public, s.model_pdf, s.client, s.fournisseur';
1239  $sql .= ', s.siren as idprof1, s.siret as idprof2, s.ape as idprof3, s.idprof4, s.idprof5, s.idprof6';
1240  $sql .= ', s.capital, s.tva_intra';
1241  $sql .= ', s.fk_typent as typent_id';
1242  $sql .= ', s.fk_effectif as effectif_id';
1243  $sql .= ', s.fk_forme_juridique as forme_juridique_code';
1244  $sql .= ', s.webservices_url, s.webservices_key';
1245  $sql .= ', s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur, s.parent, s.barcode';
1246  $sql .= ', s.fk_departement, s.fk_pays as country_id, s.fk_stcomm, s.remise_supplier, s.mode_reglement, s.cond_reglement, s.fk_account, s.tva_assuj';
1247  $sql .= ', s.mode_reglement_supplier, s.cond_reglement_supplier, s.localtax1_assuj, s.localtax1_value, s.localtax2_assuj, s.localtax2_value, s.fk_prospectlevel, s.default_lang, s.logo';
1248  $sql .= ', s.fk_shipping_method';
1249  $sql .= ', s.outstanding_limit, s.import_key, s.canvas, s.fk_incoterms, s.location_incoterms';
1250  $sql .= ', s.order_min_amount, s.supplier_order_min_amount';
1251  $sql .= ', s.fk_multicurrency, s.multicurrency_code';
1252  $sql .= ', fj.libelle as forme_juridique';
1253  $sql .= ', e.libelle as effectif';
1254  $sql .= ', c.code as country_code, c.label as country';
1255  $sql .= ', d.code_departement as state_code, d.nom as state';
1256  $sql .= ', st.libelle as stcomm';
1257  $sql .= ', te.code as typent_code';
1258  $sql .= ', i.libelle as libelle_incoterms';
1259  $sql .= ', sr.remise_client';
1260  $sql .= ' FROM '.MAIN_DB_PREFIX.'societe as s';
1261  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_effectif as e ON s.fk_effectif = e.id';
1262  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid';
1263  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcomm as st ON s.fk_stcomm = st.id';
1264  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_forme_juridique as fj ON s.fk_forme_juridique = fj.code';
1265  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON s.fk_departement = d.rowid';
1266  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_typent as te ON s.fk_typent = te.id';
1267  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON s.fk_incoterms = i.rowid';
1268  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_remise as sr ON sr.rowid = (SELECT MAX(rowid) FROM '.MAIN_DB_PREFIX.'societe_remise WHERE fk_soc = s.rowid AND entity = '.$conf->entity.')';
1269 
1270  $sql .= ' WHERE s.entity IN ('.getEntity($this->element).')';
1271  if ($rowid) $sql .= ' AND s.rowid = '.$rowid;
1272  if ($ref) $sql .= " AND s.nom = '".$this->db->escape($ref)."'";
1273  if ($ref_alias) $sql .= " AND s.nom_alias = '".$this->db->escape($ref_alias)."'";
1274  if ($ref_ext) $sql .= " AND s.ref_ext = '".$this->db->escape($ref_ext)."'";
1275  if ($ref_int) $sql .= " AND s.ref_int = '".$this->db->escape($ref_int)."'";
1276  if ($idprof1) $sql .= " AND s.siren = '".$this->db->escape($idprof1)."'";
1277  if ($idprof2) $sql .= " AND s.siret = '".$this->db->escape($idprof2)."'";
1278  if ($idprof3) $sql .= " AND s.ape = '".$this->db->escape($idprof3)."'";
1279  if ($idprof4) $sql .= " AND s.idprof4 = '".$this->db->escape($idprof4)."'";
1280  if ($idprof5) $sql .= " AND s.idprof5 = '".$this->db->escape($idprof5)."'";
1281  if ($idprof6) $sql .= " AND s.idprof6 = '".$this->db->escape($idprof6)."'";
1282  if ($email) $sql .= " AND s.email = '".$this->db->escape($email)."'";
1283 
1284  $resql=$this->db->query($sql);
1285  if ($resql)
1286  {
1287  $num=$this->db->num_rows($resql);
1288  if ($num > 1)
1289  {
1290  $this->error='Fetch found several records. Rename one of tirdparties to avoid duplicate.';
1291  dol_syslog($this->error, LOG_ERR);
1292  $result = -2;
1293  }
1294  elseif ($num) // $num = 1
1295  {
1296  $obj = $this->db->fetch_object($resql);
1297 
1298  $this->id = $obj->rowid;
1299  $this->entity = $obj->entity;
1300  $this->canvas = $obj->canvas;
1301 
1302  $this->ref = $obj->rowid;
1303  $this->name = $obj->name;
1304  $this->nom = $obj->name; // deprecated
1305  $this->name_alias = $obj->name_alias;
1306  $this->ref_ext = $obj->ref_ext;
1307  $this->ref_int = $obj->ref_int;
1308 
1309  $this->date_creation = $this->db->jdate($obj->date_creation);
1310  $this->date_modification = $this->db->jdate($obj->date_modification);
1311  $this->user_creation = $obj->fk_user_creat;
1312  $this->user_modification = $obj->fk_user_modif;
1313 
1314  $this->address = $obj->address;
1315  $this->zip = $obj->zip;
1316  $this->town = $obj->town;
1317 
1318  $this->country_id = $obj->country_id;
1319  $this->country_code = $obj->country_id?$obj->country_code:'';
1320  $this->country = $obj->country_id?($langs->trans('Country'.$obj->country_code)!='Country'.$obj->country_code?$langs->transnoentities('Country'.$obj->country_code):$obj->country):'';
1321 
1322  $this->state_id = $obj->fk_departement;
1323  $this->state_code = $obj->state_code;
1324  $this->state = ($obj->state!='-'?$obj->state:'');
1325 
1326  $transcode=$langs->trans('StatusProspect'.$obj->fk_stcomm);
1327  $libelle=($transcode!='StatusProspect'.$obj->fk_stcomm?$transcode:$obj->stcomm);
1328  $this->stcomm_id = $obj->fk_stcomm; // id statut commercial
1329  $this->statut_commercial = $libelle; // libelle statut commercial
1330 
1331  $this->email = $obj->email;
1332  $this->skype = $obj->skype;
1333  $this->twitter = $obj->twitter;
1334  $this->facebook = $obj->facebook;
1335  $this->url = $obj->url;
1336  $this->phone = $obj->phone;
1337  $this->fax = $obj->fax;
1338 
1339  $this->parent = $obj->parent;
1340 
1341  $this->idprof1 = $obj->idprof1;
1342  $this->idprof2 = $obj->idprof2;
1343  $this->idprof3 = $obj->idprof3;
1344  $this->idprof4 = $obj->idprof4;
1345  $this->idprof5 = $obj->idprof5;
1346  $this->idprof6 = $obj->idprof6;
1347 
1348  $this->capital = $obj->capital;
1349 
1350  $this->code_client = $obj->code_client;
1351  $this->code_fournisseur = $obj->code_fournisseur;
1352 
1353  $this->code_compta = $obj->code_compta;
1354  $this->code_compta_fournisseur = $obj->code_compta_fournisseur;
1355 
1356  $this->barcode = $obj->barcode;
1357 
1358  $this->tva_assuj = $obj->tva_assuj;
1359  $this->tva_intra = $obj->tva_intra;
1360  $this->status = $obj->status;
1361 
1362  // Local Taxes
1363  $this->localtax1_assuj = $obj->localtax1_assuj;
1364  $this->localtax2_assuj = $obj->localtax2_assuj;
1365 
1366  $this->localtax1_value = $obj->localtax1_value;
1367  $this->localtax2_value = $obj->localtax2_value;
1368 
1369  $this->typent_id = $obj->typent_id;
1370  $this->typent_code = $obj->typent_code;
1371 
1372  $this->effectif_id = $obj->effectif_id;
1373  $this->effectif = $obj->effectif_id?$obj->effectif:'';
1374 
1375  $this->forme_juridique_code= $obj->forme_juridique_code;
1376  $this->forme_juridique = $obj->forme_juridique_code?$obj->forme_juridique:'';
1377 
1378  $this->fk_prospectlevel = $obj->fk_prospectlevel;
1379 
1380  $this->prefix_comm = $obj->prefix_comm;
1381 
1382  $this->remise_percent = price2num($obj->remise_client); // 0.000000 must be 0
1383  $this->remise_supplier_percent = $obj->remise_supplier;
1384  $this->mode_reglement_id = $obj->mode_reglement;
1385  $this->cond_reglement_id = $obj->cond_reglement;
1386  $this->mode_reglement_supplier_id = $obj->mode_reglement_supplier;
1387  $this->cond_reglement_supplier_id = $obj->cond_reglement_supplier;
1388  $this->shipping_method_id = ($obj->fk_shipping_method>0)?$obj->fk_shipping_method:null;
1389  $this->fk_account = $obj->fk_account;
1390 
1391  $this->client = $obj->client;
1392  $this->fournisseur = $obj->fournisseur;
1393 
1394  $this->note = $obj->note_private; // TODO Deprecated for backward comtability
1395  $this->note_private = $obj->note_private;
1396  $this->note_public = $obj->note_public;
1397  $this->modelpdf = $obj->model_pdf;
1398  $this->default_lang = $obj->default_lang;
1399  $this->logo = $obj->logo;
1400 
1401  $this->webservices_url = $obj->webservices_url;
1402  $this->webservices_key = $obj->webservices_key;
1403 
1404  $this->outstanding_limit = $obj->outstanding_limit;
1405  $this->order_min_amount = $obj->order_min_amount;
1406  $this->supplier_order_min_amount = $obj->supplier_order_min_amount;
1407 
1408  // multiprix
1409  $this->price_level = $obj->price_level;
1410 
1411  $this->import_key = $obj->import_key;
1412 
1413  //Incoterms
1414  $this->fk_incoterms = $obj->fk_incoterms;
1415  $this->location_incoterms = $obj->location_incoterms;
1416  $this->libelle_incoterms = $obj->libelle_incoterms;
1417 
1418  // multicurrency
1419  $this->fk_multicurrency = $obj->fk_multicurrency;
1420  $this->multicurrency_code = $obj->multicurrency_code;
1421 
1422  $result = 1;
1423 
1424  // fetch optionals attributes and labels
1425  $this->fetch_optionals();
1426  }
1427  else
1428  {
1429  $result = 0;
1430  }
1431 
1432  $this->db->free($resql);
1433  }
1434  else
1435  {
1436  $this->error=$this->db->lasterror();
1437  $result = -3;
1438  }
1439 
1440  // Use first price level if level not defined for third party
1441  if ((! empty($conf->global->PRODUIT_MULTIPRICES) || ! empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) && empty($this->price_level)) $this->price_level=1;
1442 
1443  return $result;
1444  }
1445 
1454  function delete($id, User $fuser=null, $call_trigger=1)
1455  {
1456  global $langs, $conf, $user;
1457 
1458  if (empty($fuser)) $fuser=$user;
1459 
1460  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1461 
1462  $entity=isset($this->entity)?$this->entity:$conf->entity;
1463 
1464  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1465  $error = 0;
1466 
1467  // Test if child exists
1468  $objectisused = $this->isObjectUsed($id);
1469  if (empty($objectisused))
1470  {
1471  $this->db->begin();
1472 
1473  // User is mandatory for trigger call
1474  if (! $error && $call_trigger)
1475  {
1476  // Call trigger
1477  $result=$this->call_trigger('COMPANY_DELETE',$fuser);
1478  if ($result < 0) $error++;
1479  // End call triggers
1480  }
1481 
1482  if (! $error)
1483  {
1484  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1485  $static_cat = new Categorie($this->db);
1486  $toute_categs = array();
1487 
1488  // Fill $toute_categs array with an array of (type => array of ("Categorie" instance))
1489  if ($this->client || $this->prospect)
1490  {
1491  $toute_categs['customer'] = $static_cat->containing($this->id,Categorie::TYPE_CUSTOMER);
1492  }
1493  if ($this->fournisseur)
1494  {
1495  $toute_categs['supplier'] = $static_cat->containing($this->id,Categorie::TYPE_SUPPLIER);
1496  }
1497 
1498  // Remove each "Categorie"
1499  foreach ($toute_categs as $type => $categs_type)
1500  {
1501  foreach ($categs_type as $cat)
1502  {
1503  $cat->del_type($this, $type);
1504  }
1505  }
1506  }
1507 
1508  foreach ($this->childtablesoncascade as $tabletodelete)
1509  {
1510  if (! $error)
1511  {
1512  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete;
1513  $sql.= " WHERE fk_soc = " . $id;
1514  if (! $this->db->query($sql))
1515  {
1516  $error++;
1517  $this->errors[] = $this->db->lasterror();
1518  }
1519  }
1520  }
1521 
1522  // Removed extrafields
1523  if ((! $error) && (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) // For avoid conflicts if trigger used
1524  {
1525  $result=$this->deleteExtraFields();
1526  if ($result < 0)
1527  {
1528  $error++;
1529  dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
1530  }
1531  }
1532 
1533  // Remove links to subsidiaries companies
1534  if (! $error)
1535  {
1536  $sql = "UPDATE ".MAIN_DB_PREFIX."societe";
1537  $sql.= " SET parent = NULL";
1538  $sql.= " WHERE parent = " . $id;
1539  if (! $this->db->query($sql))
1540  {
1541  $error++;
1542  $this->errors[] = $this->db->lasterror();
1543  }
1544  }
1545 
1546  // Remove third party
1547  if (! $error)
1548  {
1549  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe";
1550  $sql.= " WHERE rowid = " . $id;
1551  if (! $this->db->query($sql))
1552  {
1553  $error++;
1554  $this->errors[] = $this->db->lasterror();
1555  }
1556  }
1557 
1558  if (! $error)
1559  {
1560  $this->db->commit();
1561 
1562  // Delete directory
1563  if (! empty($conf->societe->multidir_output[$entity]))
1564  {
1565  $docdir = $conf->societe->multidir_output[$entity] . "/" . $id;
1566  if (dol_is_dir($docdir))
1567  {
1568  dol_delete_dir_recursive($docdir);
1569  }
1570  }
1571 
1572  return 1;
1573  }
1574  else
1575  {
1576  dol_syslog($this->error, LOG_ERR);
1577  $this->db->rollback();
1578  return -1;
1579  }
1580  }
1581  else dol_syslog("Can't remove thirdparty with id ".$id.". There is ".$objectisused." childs", LOG_WARNING);
1582  return 0;
1583  }
1584 
1585  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1591  function set_as_client()
1592  {
1593  // phpcs:enable
1594  if ($this->id)
1595  {
1596  $newclient=1;
1597  if ($this->client == 2 || $this->client == 3) $newclient=3; //If prospect, we keep prospect tag
1598  $sql = "UPDATE ".MAIN_DB_PREFIX."societe";
1599  $sql.= " SET client = ".$newclient;
1600  $sql.= " WHERE rowid = " . $this->id;
1601 
1602  $resql=$this->db->query($sql);
1603  if ($resql)
1604  {
1605  $this->client = $newclient;
1606  return 1;
1607  }
1608  else return -1;
1609  }
1610  return 0;
1611  }
1612 
1613  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1622  function set_remise_client($remise, $note, User $user)
1623  {
1624  // phpcs:enable
1625  global $conf, $langs;
1626 
1627  // Nettoyage parametres
1628  $note=trim($note);
1629  if (! $note)
1630  {
1631  $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("NoteReason"));
1632  return -2;
1633  }
1634 
1635  dol_syslog(get_class($this)."::set_remise_client ".$remise.", ".$note.", ".$user->id);
1636 
1637  if ($this->id)
1638  {
1639  $this->db->begin();
1640 
1641  $now=dol_now();
1642 
1643  // Positionne remise courante
1644  $sql = "UPDATE ".MAIN_DB_PREFIX."societe ";
1645  $sql.= " SET remise_client = '".$this->db->escape($remise)."'";
1646  $sql.= " WHERE rowid = " . $this->id;
1647  $resql=$this->db->query($sql);
1648  if (! $resql)
1649  {
1650  $this->db->rollback();
1651  $this->error=$this->db->error();
1652  return -1;
1653  }
1654 
1655  // Ecrit trace dans historique des remises
1656  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise";
1657  $sql.= " (entity, datec, fk_soc, remise_client, note, fk_user_author)";
1658  $sql.= " VALUES (".$conf->entity.", '".$this->db->idate($now)."', ".$this->id.", '".$this->db->escape($remise)."',";
1659  $sql.= " '".$this->db->escape($note)."',";
1660  $sql.= " ".$user->id;
1661  $sql.= ")";
1662 
1663  $resql=$this->db->query($sql);
1664  if (! $resql)
1665  {
1666  $this->db->rollback();
1667  $this->error=$this->db->lasterror();
1668  return -1;
1669  }
1670 
1671  $this->db->commit();
1672  return 1;
1673  }
1674  }
1675 
1676  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1685  function set_remise_supplier($remise, $note, User $user)
1686  {
1687  // phpcs:enable
1688  global $conf, $langs;
1689 
1690  // Nettoyage parametres
1691  $note=trim($note);
1692  if (! $note)
1693  {
1694  $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("NoteReason"));
1695  return -2;
1696  }
1697 
1698  dol_syslog(get_class($this)."::set_remise_supplier ".$remise.", ".$note.", ".$user->id);
1699 
1700  if ($this->id)
1701  {
1702  $this->db->begin();
1703 
1704  $now=dol_now();
1705 
1706  // Positionne remise courante
1707  $sql = "UPDATE ".MAIN_DB_PREFIX."societe ";
1708  $sql.= " SET remise_supplier = '".$this->db->escape($remise)."'";
1709  $sql.= " WHERE rowid = " . $this->id;
1710  $resql=$this->db->query($sql);
1711  if (! $resql)
1712  {
1713  $this->db->rollback();
1714  $this->error=$this->db->error();
1715  return -1;
1716  }
1717 
1718  // Ecrit trace dans historique des remises
1719  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise_supplier";
1720  $sql.= " (entity, datec, fk_soc, remise_supplier, note, fk_user_author)";
1721  $sql.= " VALUES (".$conf->entity.", '".$this->db->idate($now)."', ".$this->id.", '".$this->db->escape($remise)."',";
1722  $sql.= " '".$this->db->escape($note)."',";
1723  $sql.= " ".$user->id;
1724  $sql.= ")";
1725 
1726  $resql=$this->db->query($sql);
1727  if (! $resql)
1728  {
1729  $this->db->rollback();
1730  $this->error=$this->db->lasterror();
1731  return -1;
1732  }
1733 
1734  $this->db->commit();
1735  return 1;
1736  }
1737  }
1738 
1739  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1750  function set_remise_except($remise, User $user, $desc, $tva_tx=0, $discount_type=0)
1751  {
1752  // phpcs:enable
1753  global $langs;
1754 
1755  // Clean parameters
1756  $remise = price2num($remise);
1757  $desc = trim($desc);
1758 
1759  // Check parameters
1760  if (! $remise > 0)
1761  {
1762  $this->error=$langs->trans("ErrorWrongValueForParameter","1");
1763  return -1;
1764  }
1765  if (! $desc)
1766  {
1767  $this->error=$langs->trans("ErrorWrongValueForParameter","3");
1768  return -2;
1769  }
1770 
1771  if ($this->id)
1772  {
1773  require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1774 
1775  $discount = new DiscountAbsolute($this->db);
1776  $discount->fk_soc=$this->id;
1777 
1778  $discount->discount_type=$discount_type;
1779 
1780  $discount->amount_ht=$discount->multicurrency_amount_ht=price2num($remise,'MT');
1781  $discount->amount_tva=$discount->multicurrency_amount_tva=price2num($remise*$tva_tx/100,'MT');
1782  $discount->amount_ttc=$discount->multicurrency_amount_ttc=price2num($discount->amount_ht+$discount->amount_tva,'MT');
1783 
1784  $discount->tva_tx=price2num($tva_tx,'MT');
1785  $discount->description=$desc;
1786 
1787  $result=$discount->create($user);
1788  if ($result > 0)
1789  {
1790  return $result;
1791  }
1792  else
1793  {
1794  $this->error=$discount->error;
1795  return -3;
1796  }
1797  }
1798  else return 0;
1799  }
1800 
1810  function getAvailableDiscounts($user='',$filter='',$maxvalue=0,$discount_type=0)
1811  {
1812  require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1813 
1814  $discountstatic=new DiscountAbsolute($this->db);
1815  $result=$discountstatic->getAvailableDiscounts($this,$user,$filter,$maxvalue,$discount_type);
1816  if ($result >= 0)
1817  {
1818  return $result;
1819  }
1820  else
1821  {
1822  $this->error=$discountstatic->error;
1823  return -1;
1824  }
1825  }
1826 
1834  function getSalesRepresentatives(User $user, $mode=0)
1835  {
1836  global $conf;
1837 
1838  $reparray=array();
1839 
1840  $sql = "SELECT DISTINCT u.rowid, u.login, u.lastname, u.firstname, u.email, u.statut, u.entity, u.photo";
1841  $sql.= " FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc, ".MAIN_DB_PREFIX."user as u";
1842  if (! empty($conf->multicompany->enabled) && ! empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE))
1843  {
1844  $sql.= ", ".MAIN_DB_PREFIX."usergroup_user as ug";
1845  $sql.= " WHERE ((ug.fk_user = sc.fk_user";
1846  $sql.= " AND ug.entity = ".$conf->entity.")";
1847  $sql.= " OR u.admin = 1)";
1848  }
1849  else
1850  $sql.= " WHERE entity in (0, ".$conf->entity.")";
1851 
1852  $sql.= " AND u.rowid = sc.fk_user AND sc.fk_soc = ".$this->id;
1853 
1854  $resql = $this->db->query($sql);
1855  if ($resql)
1856  {
1857  $num = $this->db->num_rows($resql);
1858  $i=0;
1859  while ($i < $num)
1860  {
1861  $obj = $this->db->fetch_object($resql);
1862 
1863  if (empty($mode))
1864  {
1865  $reparray[$i]['id']=$obj->rowid;
1866  $reparray[$i]['lastname']=$obj->lastname;
1867  $reparray[$i]['firstname']=$obj->firstname;
1868  $reparray[$i]['email']=$obj->email;
1869  $reparray[$i]['statut']=$obj->statut;
1870  $reparray[$i]['entity']=$obj->entity;
1871  $reparray[$i]['login']=$obj->login;
1872  $reparray[$i]['photo']=$obj->photo;
1873  }
1874  else
1875  {
1876  $reparray[]=$obj->rowid;
1877  }
1878  $i++;
1879  }
1880  return $reparray;
1881  }
1882  else {
1883  dol_print_error($this->db);
1884  return -1;
1885  }
1886  }
1887 
1888  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1896  function set_price_level($price_level, User $user)
1897  {
1898  // phpcs:enable
1899  if ($this->id)
1900  {
1901  $now=dol_now();
1902 
1903  $sql = "UPDATE ".MAIN_DB_PREFIX."societe";
1904  $sql .= " SET price_level = '".$this->db->escape($price_level)."'";
1905  $sql .= " WHERE rowid = " . $this->id;
1906 
1907  if (! $this->db->query($sql))
1908  {
1909  dol_print_error($this->db);
1910  return -1;
1911  }
1912 
1913  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_prices";
1914  $sql .= " (datec, fk_soc, price_level, fk_user_author)";
1915  $sql .= " VALUES ('".$this->db->idate($now)."', ".$this->id.", '".$this->db->escape($price_level)."', ".$user->id.")";
1916 
1917  if (! $this->db->query($sql))
1918  {
1919  dol_print_error($this->db);
1920  return -1;
1921  }
1922  return 1;
1923  }
1924  return -1;
1925  }
1926 
1927  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1935  function add_commercial(User $user, $commid)
1936  {
1937  // phpcs:enable
1938  $error=0;
1939 
1940 
1941  if ($this->id > 0 && $commid > 0)
1942  {
1943  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_commerciaux";
1944  $sql.= " WHERE fk_soc = ".$this->id." AND fk_user =".$commid;
1945 
1946  $this->db->query($sql);
1947 
1948  $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_commerciaux";
1949  $sql.= " ( fk_soc, fk_user )";
1950  $sql.= " VALUES (".$this->id.",".$commid.")";
1951 
1952  if (! $this->db->query($sql) )
1953  {
1954  dol_syslog(get_class($this)."::add_commercial Erreur");
1955  }
1956  else {
1957  $this->context=array('commercial_modified'=>$commid);
1958 
1959  $result=$this->call_trigger('COMPANY_LINK_SALE_REPRESENTATIVE',$user);
1960  if ($result < 0) $error++;
1961  }
1962  }
1963  }
1964 
1965  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
1973  function del_commercial(User $user, $commid)
1974  {
1975  // phpcs:enable
1976  $error=0;
1977  $this->context=array('commercial_modified'=>$commid);
1978 
1979  $result=$this->call_trigger('COMPANY_UNLINK_SALE_REPRESENTATIVE',$user);
1980  if ($result < 0) $error++;
1981 
1982  if ($this->id > 0 && $commid > 0)
1983  {
1984  $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_commerciaux ";
1985  $sql .= " WHERE fk_soc = ".$this->id." AND fk_user =".$commid;
1986 
1987  if (! $this->db->query($sql) )
1988  {
1989  dol_syslog(get_class($this)."::del_commercial Erreur");
1990  }
1991  }
1992  }
1993 
1994 
2005  function getNomUrl($withpicto=0, $option='', $maxlen=0, $notooltip=0, $save_lastsearch_value=-1)
2006  {
2007  global $conf, $langs, $hookmanager;
2008 
2009  if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
2010 
2011  $name=$this->name?$this->name:$this->nom;
2012 
2013  if(!empty($conf->global->SOCIETE_ON_SEARCH_AND_LIST_GO_ON_CUSTOMER_OR_SUPPLIER_CARD)){
2014 
2015  if(empty($option) && $this->client > 0) $option = 'customer';
2016  if(empty($option) && $this->fournisseur > 0) $option = 'supplier';
2017  }
2018 
2019 
2020  if (! empty($conf->global->SOCIETE_ADD_REF_IN_LIST) && (!empty($withpicto)))
2021  {
2022  $code = '';
2023  if (($this->client) && (! empty ( $this->code_client ))
2024  && ($conf->global->SOCIETE_ADD_REF_IN_LIST == 1
2025  || $conf->global->SOCIETE_ADD_REF_IN_LIST == 2
2026  )
2027  )
2028  {
2029  $code = $this->code_client . ' - ';
2030  }
2031 
2032  if (($this->fournisseur) && (! empty ( $this->code_fournisseur ))
2033  && ($conf->global->SOCIETE_ADD_REF_IN_LIST == 1
2034  || $conf->global->SOCIETE_ADD_REF_IN_LIST == 3
2035  )
2036  )
2037  {
2038  $code .= $this->code_fournisseur . ' - ';
2039  }
2040 
2041  if ($conf->global->SOCIETE_ADD_REF_IN_LIST == 1)
2042  {
2043  $name =$code.' '.$name;
2044  }
2045  else
2046  {
2047  $name =$code;
2048  }
2049  }
2050 
2051  if (!empty($this->name_alias)) $name .= ' ('.$this->name_alias.')';
2052 
2053  $result=''; $label='';
2054  $linkstart=''; $linkend='';
2055 
2056  if (! empty($this->logo) && class_exists('Form'))
2057  {
2058  $label.= '<div class="photointooltip">';
2059  $label.= Form::showphoto('societe', $this, 0, 40, 0, 'photowithmargin', 'mini', 0); // Important, we must force height so image will have height tags and if image is inside a tooltip, the tooltip manager can calculate height and position correctly the tooltip.
2060  $label.= '</div><div style="clear: both;"></div>';
2061  }
2062 
2063  $label.= '<div class="centpercent">';
2064 
2065  if ($option == 'customer' || $option == 'compta' || $option == 'category' || $option == 'category_supplier')
2066  {
2067  $label.= '<u>' . $langs->trans("ShowCustomer") . '</u>';
2068  $linkstart = '<a href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$this->id;
2069  }
2070  elseif ($option == 'prospect' && empty($conf->global->SOCIETE_DISABLE_PROSPECTS))
2071  {
2072  $label.= '<u>' . $langs->trans("ShowProspect") . '</u>';
2073  $linkstart = '<a href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$this->id;
2074  }
2075  elseif ($option == 'supplier')
2076  {
2077  $label.= '<u>' . $langs->trans("ShowSupplier") . '</u>';
2078  $linkstart = '<a href="'.DOL_URL_ROOT.'/fourn/card.php?socid='.$this->id;
2079  }
2080  elseif ($option == 'agenda')
2081  {
2082  $label.= '<u>' . $langs->trans("ShowAgenda") . '</u>';
2083  $linkstart = '<a href="'.DOL_URL_ROOT.'/societe/agenda.php?socid='.$this->id;
2084  }
2085  elseif ($option == 'project')
2086  {
2087  $label.= '<u>' . $langs->trans("ShowProject") . '</u>';
2088  $linkstart = '<a href="'.DOL_URL_ROOT.'/societe/project.php?socid='.$this->id;
2089  }
2090  elseif ($option == 'margin')
2091  {
2092  $label.= '<u>' . $langs->trans("ShowMargin") . '</u>';
2093  $linkstart = '<a href="'.DOL_URL_ROOT.'/margin/tabs/thirdpartyMargins.php?socid='.$this->id.'&type=1';
2094  }
2095  elseif ($option == 'contact')
2096  {
2097  $label.= '<u>' . $langs->trans("ShowContacts") . '</u>';
2098  $linkstart = '<a href="'.DOL_URL_ROOT.'/societe/contact.php?socid='.$this->id;
2099  }
2100  elseif ($option == 'ban')
2101  {
2102  $label.= '<u>' . $langs->trans("ShowBan") . '</u>';
2103  $linkstart = '<a href="'.DOL_URL_ROOT.'/societe/paymentmodes.php?socid='.$this->id;
2104  }
2105 
2106  // By default
2107  if (empty($linkstart))
2108  {
2109  $label.= '<u>' . $langs->trans("ShowCompany") . '</u>';
2110  $linkstart = '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$this->id;
2111  }
2112 
2113  if (! empty($this->name))
2114  {
2115  $label.= '<br><b>' . $langs->trans('Name') . ':</b> '. $this->name;
2116  if (! empty($this->name_alias)) $label.=' ('.$this->name_alias.')';
2117  $label.= '<br><b>' . $langs->trans('Email') . ':</b> '. $this->email;
2118  }
2119  if (! empty($this->country_code))
2120  $label.= '<br><b>' . $langs->trans('Country') . ':</b> '. $this->country_code;
2121  if (! empty($this->tva_intra) || (! empty($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP) && strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'vatnumber') !== false))
2122  $label.= '<br><b>' . $langs->trans('VATIntra') . ':</b> '. $this->tva_intra;
2123  if (! empty($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP))
2124  {
2125  if (strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'profid1') !== false) $label.= '<br><b>' . $langs->trans('ProfId1'.$this->country_code) . ':</b> '. $this->idprof1;
2126  if (strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'profid2') !== false) $label.= '<br><b>' . $langs->trans('ProfId2'.$this->country_code) . ':</b> '. $this->idprof2;
2127  if (strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'profid3') !== false) $label.= '<br><b>' . $langs->trans('ProfId3'.$this->country_code) . ':</b> '. $this->idprof3;
2128  if (strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'profid4') !== false) $label.= '<br><b>' . $langs->trans('ProfId4'.$this->country_code) . ':</b> '. $this->idprof4;
2129  if (strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'profid5') !== false) $label.= '<br><b>' . $langs->trans('ProfId5'.$this->country_code) . ':</b> '. $this->idprof5;
2130  if (strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'profid6') !== false) $label.= '<br><b>' . $langs->trans('ProfId6'.$this->country_code) . ':</b> '. $this->idprof6;
2131  }
2132  if (! empty($this->code_client) && ($this->client == 1 || $this->client == 3))
2133  $label.= '<br><b>' . $langs->trans('CustomerCode') . ':</b> '. $this->code_client;
2134  if (! empty($this->code_fournisseur) && $this->fournisseur)
2135  $label.= '<br><b>' . $langs->trans('SupplierCode') . ':</b> '. $this->code_fournisseur;
2136  if (! empty($conf->accounting->enabled) && ($this->client == 1 || $this->client == 3))
2137  $label.= '<br><b>' . $langs->trans('CustomerAccountancyCode') . ':</b> '. ($this->code_compta ? $this->code_compta : $this->code_compta_client);
2138  if (! empty($conf->accounting->enabled) && $this->fournisseur)
2139  $label.= '<br><b>' . $langs->trans('SupplierAccountancyCode') . ':</b> '. $this->code_compta_fournisseur;
2140 
2141  $label.= '</div>';
2142 
2143  // Add type of canvas
2144  $linkstart.=(!empty($this->canvas)?'&canvas='.$this->canvas:'');
2145  // Add param to save lastsearch_values or not
2146  $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
2147  if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
2148  if ($add_save_lastsearch_values) $linkstart.='&save_lastsearch_values=1';
2149  $linkstart.='"';
2150 
2151  $linkclose='';
2152  if (empty($notooltip))
2153  {
2154  if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
2155  {
2156  $label=$langs->trans("ShowCompany");
2157  $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
2158  }
2159  $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
2160  $linkclose.=' class="classfortooltip refurl"';
2161 
2162  /*
2163  $hookmanager->initHooks(array('thirdpartydao'));
2164  $parameters=array('id'=>$this->id);
2165  $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
2166  if ($reshook > 0) $linkclose = $hookmanager->resPrint;
2167  */
2168  }
2169  $linkstart.=$linkclose.'>';
2170  $linkend='</a>';
2171 
2172  global $user;
2173  if (! $user->rights->societe->client->voir && $user->societe_id > 0 && $this->id != $user->societe_id)
2174  {
2175  $linkstart='';
2176  $linkend='';
2177  }
2178 
2179  $result.=$linkstart;
2180  if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip valigntextbottom"'), 0, 0, $notooltip?0:1);
2181  if ($withpicto != 2) $result.=($maxlen?dol_trunc($name,$maxlen):$name);
2182  $result.=$linkend;
2183 
2184  global $action;
2185  $hookmanager->initHooks(array('thirdpartydao'));
2186  $parameters=array('id'=>$this->id, 'getnomurl'=>$result);
2187  $reshook=$hookmanager->executeHooks('getNomUrl',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
2188  if ($reshook > 0) $result = $hookmanager->resPrint;
2189  else $result .= $hookmanager->resPrint;
2190 
2191  return $result;
2192  }
2193 
2200  function getLibStatut($mode=0)
2201  {
2202  return $this->LibStatut($this->status,$mode);
2203  }
2204 
2205  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2213  function LibStatut($statut,$mode=0)
2214  {
2215  // phpcs:enable
2216  global $langs;
2217  $langs->load('companies');
2218 
2219  if ($mode == 0)
2220  {
2221  if ($statut==0) return $langs->trans("ActivityCeased");
2222  elseif ($statut==1) return $langs->trans("InActivity");
2223  }
2224  elseif ($mode == 1)
2225  {
2226  if ($statut==0) return $langs->trans("ActivityCeased");
2227  elseif ($statut==1) return $langs->trans("InActivity");
2228  }
2229  elseif ($mode == 2)
2230  {
2231  if ($statut==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2232  elseif ($statut==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2233  }
2234  elseif ($mode == 3)
2235  {
2236  if ($statut==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2237  elseif ($statut==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2238  }
2239  elseif ($mode == 4)
2240  {
2241  if ($statut==0) return img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"').' '.$langs->trans("ActivityCeased");
2242  elseif ($statut==1) return img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"').' '.$langs->trans("InActivity");
2243  }
2244  elseif ($mode == 5)
2245  {
2246  if ($statut==0) return '<span class="hideonsmartphone">'.$langs->trans("ActivityCeased").'</span> '.img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2247  elseif ($statut==1) return '<span class="hideonsmartphone">'.$langs->trans("InActivity").'</span> '.img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2248  }
2249  elseif ($mode == 6)
2250  {
2251  if ($statut==0) return '<span class="hideonsmartphone">'.$langs->trans("ActivityCeased").'</span> '.img_picto($langs->trans("ActivityCeased"),'statut5', 'class="pictostatus"');
2252  elseif ($statut==1) return '<span class="hideonsmartphone">'.$langs->trans("InActivity").'</span> '.img_picto($langs->trans("InActivity"),'statut4', 'class="pictostatus"');
2253  }
2254  }
2255 
2256  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2263  function thirdparty_and_contact_email_array($addthirdparty=0)
2264  {
2265  // phpcs:enable
2266  global $langs;
2267 
2268  $contact_emails = $this->contact_property_array('email',1);
2269  if ($this->email && $addthirdparty)
2270  {
2271  if (empty($this->name)) $this->name=$this->nom;
2272  $contact_emails['thirdparty']=$langs->transnoentitiesnoconv("ThirdParty").': '.dol_trunc($this->name,16)." <".$this->email.">";
2273  }
2274  //var_dump($contact_emails)
2275  return $contact_emails;
2276  }
2277 
2278  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2285  {
2286  // phpcs:enable
2287  global $langs;
2288 
2289  $contact_phone = $this->contact_property_array('mobile');
2290 
2291  if (! empty($this->phone)) // If a phone of thirdparty is defined, we add it ot mobile of contacts
2292  {
2293  if (empty($this->name)) $this->name=$this->nom;
2294  // TODO: Tester si tel non deja present dans tableau contact
2295  $contact_phone['thirdparty']=$langs->transnoentitiesnoconv("ThirdParty").': '.dol_trunc($this->name,16)." <".$this->phone.">";
2296  }
2297  return $contact_phone;
2298  }
2299 
2300  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2308  function contact_property_array($mode='email', $hidedisabled=0)
2309  {
2310  // phpcs:enable
2311  global $langs;
2312 
2313  $contact_property = array();
2314 
2315 
2316  $sql = "SELECT rowid, email, statut, phone_mobile, lastname, poste, firstname";
2317  $sql.= " FROM ".MAIN_DB_PREFIX."socpeople";
2318  $sql.= " WHERE fk_soc = ".$this->id;
2319 
2320  $resql=$this->db->query($sql);
2321  if ($resql)
2322  {
2323  $nump = $this->db->num_rows($resql);
2324  if ($nump)
2325  {
2326  $sepa="("; $sepb=")";
2327  if ($mode == 'email')
2328  {
2329  //$sepa="&lt;"; $sepb="&gt;";
2330  $sepa="<"; $sepb=">";
2331  }
2332  $i = 0;
2333  while ($i < $nump)
2334  {
2335  $obj = $this->db->fetch_object($resql);
2336  if ($mode == 'email') $property=$obj->email;
2337  else if ($mode == 'mobile') $property=$obj->phone_mobile;
2338  else $property=$obj->$mode;
2339 
2340  // Show all contact. If hidedisabled is 1, showonly contacts with status = 1
2341  if ($obj->statut == 1 || empty($hidedisabled))
2342  {
2343  if (empty($property))
2344  {
2345  if ($mode == 'email') $property=$langs->transnoentitiesnoconv("NoEMail");
2346  else if ($mode == 'mobile') $property=$langs->transnoentitiesnoconv("NoMobilePhone");
2347  }
2348 
2349  if (!empty($obj->poste))
2350  {
2351  $contact_property[$obj->rowid] = trim(dolGetFirstLastname($obj->firstname,$obj->lastname)).($obj->poste?" - ".$obj->poste:"").(($mode != 'poste' && $property)?" ".$sepa.$property.$sepb:'');
2352  }
2353  else
2354  {
2355  $contact_property[$obj->rowid] = trim(dolGetFirstLastname($obj->firstname,$obj->lastname)).(($mode != 'poste' && $property)?" ".$sepa.$property.$sepb:'');
2356  }
2357  }
2358  $i++;
2359  }
2360  }
2361  }
2362  else
2363  {
2364  dol_print_error($this->db);
2365  }
2366  return $contact_property;
2367  }
2368 
2369 
2370  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2376  function contact_array()
2377  {
2378  // phpcs:enable
2379  $contacts = array();
2380 
2381  $sql = "SELECT rowid, lastname, firstname FROM ".MAIN_DB_PREFIX."socpeople WHERE fk_soc = ".$this->id;
2382  $resql=$this->db->query($sql);
2383  if ($resql)
2384  {
2385  $nump = $this->db->num_rows($resql);
2386  if ($nump)
2387  {
2388  $i = 0;
2389  while ($i < $nump)
2390  {
2391  $obj = $this->db->fetch_object($resql);
2392  $contacts[$obj->rowid] = dolGetFirstLastname($obj->firstname,$obj->lastname);
2393  $i++;
2394  }
2395  }
2396  }
2397  else
2398  {
2399  dol_print_error($this->db);
2400  }
2401  return $contacts;
2402  }
2403 
2404  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2411  {
2412  // phpcs:enable
2413  require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
2414  $contacts = array();
2415 
2416  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."socpeople WHERE fk_soc = ".$this->id;
2417  $resql=$this->db->query($sql);
2418  if ($resql)
2419  {
2420  $nump = $this->db->num_rows($resql);
2421  if ($nump)
2422  {
2423  $i = 0;
2424  while ($i < $nump)
2425  {
2426  $obj = $this->db->fetch_object($resql);
2427  $contact = new Contact($this->db);
2428  $contact->fetch($obj->rowid);
2429  $contacts[] = $contact;
2430  $i++;
2431  }
2432  }
2433  }
2434  else
2435  {
2436  dol_print_error($this->db);
2437  }
2438  return $contacts;
2439  }
2440 
2441  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2449  function contact_get_property($rowid,$mode)
2450  {
2451  // phpcs:enable
2452  $contact_property='';
2453 
2454  if (empty($rowid)) return '';
2455 
2456  $sql = "SELECT rowid, email, phone_mobile, lastname, firstname";
2457  $sql.= " FROM ".MAIN_DB_PREFIX."socpeople";
2458  $sql.= " WHERE rowid = '".$rowid."'";
2459 
2460  $resql=$this->db->query($sql);
2461  if ($resql)
2462  {
2463  $nump = $this->db->num_rows($resql);
2464 
2465  if ($nump)
2466  {
2467  $obj = $this->db->fetch_object($resql);
2468 
2469  if ($mode == 'email') $contact_property = dol_string_nospecial(dolGetFirstLastname($obj->firstname, $obj->lastname), ' ', array(","))." <".$obj->email.">";
2470  else if ($mode == 'mobile') $contact_property = $obj->phone_mobile;
2471  }
2472  return $contact_property;
2473  }
2474  else
2475  {
2476  dol_print_error($this->db);
2477  }
2478  }
2479 
2480 
2481  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2488  function display_rib($mode='label')
2489  {
2490  // phpcs:enable
2491  require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php';
2492 
2493  $bac = new CompanyBankAccount($this->db);
2494  $bac->fetch(0,$this->id);
2495 
2496  if ($mode == 'label')
2497  {
2498  return $bac->getRibLabel(true);
2499  }
2500  elseif ($mode == 'rum')
2501  {
2502  if (empty($bac->rum))
2503  {
2504  require_once DOL_DOCUMENT_ROOT . '/compta/prelevement/class/bonprelevement.class.php';
2505  $prelevement = new BonPrelevement($this->db);
2506  $bac->fetch_thirdparty();
2507  $bac->rum = $prelevement->buildRumNumber($bac->thirdparty->code_client, $bac->datec, $bac->id);
2508  }
2509  return $bac->rum;
2510  }
2511  elseif ($mode == 'format')
2512  {
2513  return $bac->frstrecur;
2514  }
2515 
2516  return 'BadParameterToFunctionDisplayRib';
2517  }
2518 
2519  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2525  function get_all_rib()
2526  {
2527  // phpcs:enable
2528  require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php';
2529  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_rib WHERE type='ban' AND fk_soc = ".$this->id;
2530  $result = $this->db->query($sql);
2531  if (!$result) {
2532  $this->error++;
2533  $this->errors[] = $this->db->lasterror;
2534  return 0;
2535  } else {
2536  $num_rows = $this->db->num_rows($result);
2537  $rib_array = array();
2538  if ($num_rows) {
2539  while ($obj = $this->db->fetch_object($result)) {
2540  $rib = new CompanyBankAccount($this->db);
2541  $rib->fetch($obj->rowid);
2542  $rib_array[] = $rib;
2543  }
2544  }
2545  return $rib_array;
2546  }
2547  }
2548 
2549  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2558  function get_codeclient($objsoc=0,$type=0)
2559  {
2560  // phpcs:enable
2561  global $conf;
2562  if (! empty($conf->global->SOCIETE_CODECLIENT_ADDON))
2563  {
2564  $module=$conf->global->SOCIETE_CODECLIENT_ADDON;
2565 
2566  $dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
2567  foreach ($dirsociete as $dirroot)
2568  {
2569  $res=dol_include_once($dirroot.$module.'.php');
2570  if ($res) break;
2571  }
2572  $mod = new $module();
2573 
2574  $this->code_client = $mod->getNextValue($objsoc,$type);
2575  $this->prefixCustomerIsRequired = $mod->prefixIsRequired;
2576 
2577  dol_syslog(get_class($this)."::get_codeclient code_client=".$this->code_client." module=".$module);
2578  }
2579  }
2580 
2581  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2590  function get_codefournisseur($objsoc=0,$type=1)
2591  {
2592  // phpcs:enable
2593  global $conf;
2594  if (! empty($conf->global->SOCIETE_CODECLIENT_ADDON))
2595  {
2596  $module=$conf->global->SOCIETE_CODECLIENT_ADDON;
2597 
2598  $dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
2599  foreach ($dirsociete as $dirroot)
2600  {
2601  $res=dol_include_once($dirroot.$module.'.php');
2602  if ($res) break;
2603  }
2604  $mod = new $module();
2605 
2606  $this->code_fournisseur = $mod->getNextValue($objsoc,$type);
2607 
2608  dol_syslog(get_class($this)."::get_codefournisseur code_fournisseur=".$this->code_fournisseur." module=".$module);
2609  }
2610  }
2611 
2612  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2620  {
2621  // phpcs:enable
2622  global $conf;
2623  if (! empty($conf->global->SOCIETE_CODECLIENT_ADDON))
2624  {
2625  $module=$conf->global->SOCIETE_CODECLIENT_ADDON;
2626 
2627  $dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
2628  foreach ($dirsociete as $dirroot)
2629  {
2630  $res=dol_include_once($dirroot.$module.'.php');
2631  if ($res) break;
2632  }
2633 
2634  $mod = new $module();
2635 
2636  dol_syslog(get_class($this)."::codeclient_modifiable code_client=".$this->code_client." module=".$module);
2637  if ($mod->code_modifiable_null && ! $this->code_client) return 1;
2638  if ($mod->code_modifiable_invalide && $this->check_codeclient() < 0) return 1;
2639  if ($mod->code_modifiable) return 1; // A mettre en dernier
2640  return 0;
2641  }
2642  else
2643  {
2644  return 0;
2645  }
2646  }
2647 
2648 
2649  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2656  {
2657  // phpcs:enable
2658  global $conf;
2659  if (! empty($conf->global->SOCIETE_CODECLIENT_ADDON))
2660  {
2661  $module=$conf->global->SOCIETE_CODECLIENT_ADDON;
2662 
2663  $dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
2664  foreach ($dirsociete as $dirroot)
2665  {
2666  $res=dol_include_once($dirroot.$module.'.php');
2667  if ($res) break;
2668  }
2669 
2670  $mod = new $module();
2671 
2672  dol_syslog(get_class($this)."::codefournisseur_modifiable code_founisseur=".$this->code_fournisseur." module=".$module);
2673  if ($mod->code_modifiable_null && ! $this->code_fournisseur) return 1;
2674  if ($mod->code_modifiable_invalide && $this->check_codefournisseur() < 0) return 1;
2675  if ($mod->code_modifiable) return 1; // A mettre en dernier
2676  return 0;
2677  }
2678  else
2679  {
2680  return 0;
2681  }
2682  }
2683 
2684 
2685  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2695  function check_codeclient()
2696  {
2697  // phpcs:enable
2698  global $conf;
2699  if (! empty($conf->global->SOCIETE_CODECLIENT_ADDON))
2700  {
2701  $module=$conf->global->SOCIETE_CODECLIENT_ADDON;
2702 
2703  $dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
2704  foreach ($dirsociete as $dirroot)
2705  {
2706  $res=dol_include_once($dirroot.$module.'.php');
2707  if ($res) break;
2708  }
2709 
2710  $mod = new $module();
2711 
2712  dol_syslog(get_class($this)."::check_codeclient code_client=".$this->code_client." module=".$module);
2713  $result = $mod->verif($this->db, $this->code_client, $this, 0);
2714  return $result;
2715  }
2716  else
2717  {
2718  return 0;
2719  }
2720  }
2721 
2722  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2733  {
2734  // phpcs:enable
2735  global $conf;
2736  if (! empty($conf->global->SOCIETE_CODECLIENT_ADDON))
2737  {
2738  $module=$conf->global->SOCIETE_CODECLIENT_ADDON;
2739 
2740  $dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
2741  foreach ($dirsociete as $dirroot)
2742  {
2743  $res=dol_include_once($dirroot.$module.'.php');
2744  if ($res) break;
2745  }
2746 
2747  $mod = new $module();
2748 
2749  dol_syslog(get_class($this)."::check_codefournisseur code_fournisseur=".$this->code_fournisseur." module=".$module);
2750  $result = $mod->verif($this->db, $this->code_fournisseur, $this, 1);
2751  return $result;
2752  }
2753  else
2754  {
2755  return 0;
2756  }
2757  }
2758 
2759  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2768  function get_codecompta($type)
2769  {
2770  // phpcs:enable
2771  global $conf;
2772 
2773  if (! empty($conf->global->SOCIETE_CODECOMPTA_ADDON))
2774  {
2775  $res=false;
2776  $dirsociete=array_merge(array('/core/modules/societe/'), $conf->modules_parts['societe']);
2777  foreach ($dirsociete as $dirroot)
2778  {
2779  $res=dol_include_once($dirroot.$conf->global->SOCIETE_CODECOMPTA_ADDON.'.php');
2780  if ($res) break;
2781  }
2782 
2783  if ($res)
2784  {
2785  $classname = $conf->global->SOCIETE_CODECOMPTA_ADDON;
2786  $mod = new $classname;
2787 
2788  // Defini code compta dans $mod->code
2789  $result = $mod->get_code($this->db, $this, $type);
2790 
2791  if ($type == 'customer') $this->code_compta = $mod->code;
2792  else if ($type == 'supplier') $this->code_compta_fournisseur = $mod->code;
2793 
2794  return $result;
2795  }
2796  else
2797  {
2798  $this->error = 'ErrorAccountancyCodeNotDefined';
2799  return -1;
2800  }
2801  }
2802  else
2803  {
2804  if ($type == 'customer') $this->code_compta = '';
2805  else if ($type == 'supplier') $this->code_compta_fournisseur = '';
2806 
2807  return 0;
2808  }
2809  }
2810 
2811  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2818  function set_parent($id)
2819  {
2820  // phpcs:enable
2821  if ($this->id)
2822  {
2823  $sql = "UPDATE ".MAIN_DB_PREFIX."societe";
2824  $sql.= " SET parent = ".($id > 0 ? $id : "null");
2825  $sql.= " WHERE rowid = " . $this->id;
2826  dol_syslog(get_class($this).'::set_parent', LOG_DEBUG);
2827  $resql=$this->db->query($sql);
2828  if ($resql)
2829  {
2830  $this->parent = $id;
2831  return 1;
2832  }
2833  else
2834  {
2835  return -1;
2836  }
2837  }
2838  else return -1;
2839  }
2840 
2841  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2848  function id_prof_verifiable($idprof)
2849  {
2850  // phpcs:enable
2851  global $conf;
2852 
2853  switch($idprof)
2854  {
2855  case 1:
2856  $ret=(!$conf->global->SOCIETE_IDPROF1_UNIQUE?false:true);
2857  break;
2858  case 2:
2859  $ret=(!$conf->global->SOCIETE_IDPROF2_UNIQUE?false:true);
2860  break;
2861  case 3:
2862  $ret=(!$conf->global->SOCIETE_IDPROF3_UNIQUE?false:true);
2863  break;
2864  case 4:
2865  $ret=(!$conf->global->SOCIETE_IDPROF4_UNIQUE?false:true);
2866  break;
2867  case 5:
2868  $ret=(!$conf->global->SOCIETE_IDPROF5_UNIQUE?false:true);
2869  break;
2870  case 6:
2871  $ret=(!$conf->global->SOCIETE_IDPROF6_UNIQUE?false:true);
2872  break;
2873  default:
2874  $ret=false;
2875  }
2876 
2877  return $ret;
2878  }
2879 
2880  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2889  function id_prof_exists($idprof, $value, $socid=0)
2890  {
2891  // phpcs:enable
2892  $field = $idprof;
2893 
2894  switch($idprof) // For backward compatibility
2895  {
2896  case '1':
2897  case 'idprof1':
2898  $field="siren";
2899  break;
2900  case '2':
2901  case 'idprof2':
2902  $field="siret";
2903  break;
2904  case '3':
2905  case 'idprof3':
2906  $field="ape";
2907  break;
2908  case '4':
2909  case 'idprof4':
2910  $field="idprof4";
2911  break;
2912  case '5':
2913  $field="idprof5";
2914  break;
2915  case '6':
2916  $field="idprof6";
2917  break;
2918  }
2919 
2920  //Verify duplicate entries
2921  $sql = "SELECT COUNT(*) as idprof FROM ".MAIN_DB_PREFIX."societe WHERE ".$field." = '".$value."' AND entity IN (".getEntity('societe').")";
2922  if($socid) $sql .= " AND rowid <> ".$socid;
2923  $resql = $this->db->query($sql);
2924  if ($resql)
2925  {
2926  $obj = $this->db->fetch_object($resql);
2927  $count = $obj->idprof;
2928  }
2929  else
2930  {
2931  $count = 0;
2932  print $this->db->error();
2933  }
2934  $this->db->free($resql);
2935 
2936  if ($count > 0) return true;
2937  else return false;
2938  }
2939 
2940  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2949  function id_prof_check($idprof,$soc)
2950  {
2951  // phpcs:enable
2952  global $conf;
2953 
2954  $ok=1;
2955 
2956  if (! empty($conf->global->MAIN_DISABLEPROFIDRULES)) return 1;
2957 
2958  // Verifie SIREN si pays FR
2959  if ($idprof == 1 && $soc->country_code == 'FR')
2960  {
2961  $chaine=trim($this->idprof1);
2962  $chaine=preg_replace('/(\s)/','',$chaine);
2963 
2964  if (!is_numeric($chaine)) return -1;
2965  if (dol_strlen($chaine) != 9) return -1;
2966 
2967  // on prend chaque chiffre un par un
2968  // si son index (position dans la chaîne en commence à 0 au premier caractère) est impair
2969  // on double sa valeur et si cette dernière est supérieure à 9, on lui retranche 9
2970  // on ajoute cette valeur à la somme totale
2971 
2972  for ($index = 0; $index < 9; $index ++)
2973  {
2974  $number = (int) $siren[$index];
2975  if (($index % 2) != 0) { if (($number *= 2) > 9) $number -= 9; }
2976  $sum += $number;
2977  }
2978 
2979  // le numéro est valide si la somme des chiffres est multiple de 10
2980  if (($sum % 10) != 0) return -1;
2981  }
2982 
2983  // Verifie SIRET si pays FR
2984  if ($idprof == 2 && $soc->country_code == 'FR')
2985  {
2986  $chaine=trim($this->idprof2);
2987  $chaine=preg_replace('/(\s)/','',$chaine);
2988 
2989  if (!is_numeric($chaine)) return -1;
2990  if (dol_strlen($chaine) != 14) return -1;
2991 
2992  // on prend chaque chiffre un par un
2993  // si son index (position dans la chaîne en commence à 0 au premier caractère) est pair
2994  // on double sa valeur et si cette dernière est supérieure à 9, on lui retranche 9
2995  // on ajoute cette valeur à la somme totale
2996 
2997  for ($index = 0; $index < 14; $index ++)
2998  {
2999  $number = (int) $chaine[$index];
3000  if (($index % 2) == 0) { if (($number *= 2) > 9) $number -= 9; }
3001  $sum += $number;
3002  }
3003 
3004  // le numéro est valide si la somme des chiffres est multiple de 10
3005  if (($sum % 10) != 0) return -1;
3006  }
3007 
3008  //Verify CIF/NIF/NIE if pays ES
3009  //Returns: 1 if NIF ok, 2 if CIF ok, 3 if NIE ok, -1 if NIF bad, -2 if CIF bad, -3 if NIE bad, 0 if unexpected bad
3010  if ($idprof == 1 && $soc->country_code == 'ES')
3011  {
3012  $string=trim($this->idprof1);
3013  $string=preg_replace('/(\s)/','',$string);
3014  $string = strtoupper($string);
3015 
3016  for ($i = 0; $i < 9; $i ++)
3017  $num[$i] = substr($string, $i, 1);
3018 
3019  //Check format
3020  if (!preg_match('/((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)/', $string))
3021  return 0;
3022 
3023  //Check NIF
3024  if (preg_match('/(^[0-9]{8}[A-Z]{1}$)/', $string))
3025  if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($string, 0, 8) % 23, 1))
3026  return 1;
3027  else
3028  return -1;
3029 
3030  //algorithm checking type code CIF
3031  $sum = $num[2] + $num[4] + $num[6];
3032  for ($i = 1; $i < 8; $i += 2)
3033  $sum += intval(substr((2 * $num[$i]),0,1)) + intval(substr((2 * $num[$i]),1,1));
3034  $n = 10 - substr($sum, strlen($sum) - 1, 1);
3035 
3036  //Chek special NIF
3037  if (preg_match('/^[KLM]{1}/', $string))
3038  if ($num[8] == chr(64 + $n) || $num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($string, 1, 8) % 23, 1))
3039  return 1;
3040  else
3041  return -1;
3042 
3043  //Check CIF
3044  if (preg_match('/^[ABCDEFGHJNPQRSUVW]{1}/', $string))
3045  if ($num[8] == chr(64 + $n) || $num[8] == substr($n, strlen($n) - 1, 1))
3046  return 2;
3047  else
3048  return -2;
3049 
3050  //Check NIE T
3051  if (preg_match('/^[T]{1}/', $string))
3052  if ($num[8] == preg_match('/^[T]{1}[A-Z0-9]{8}$/', $string))
3053  return 3;
3054  else
3055  return -3;
3056 
3057  //Check NIE XYZ
3058  if (preg_match('/^[XYZ]{1}/', $string))
3059  if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr(str_replace(array('X','Y','Z'), array('0','1','2'), $string), 0, 8) % 23, 1))
3060  return 3;
3061  else
3062  return -3;
3063 
3064  //Can not be verified
3065  return -4;
3066  }
3067 
3068  //Verify NIF if country is PT
3069  //Returns: 1 if NIF ok, -1 if NIF bad, 0 if unexpected bad
3070  if ($idprof == 1 && $soc->country_code == 'PT')
3071  {
3072  $string=trim($this->idprof1);
3073  $string=preg_replace('/(\s)/','',$string);
3074 
3075  for ($i = 0; $i < 9; $i ++) {
3076  $num[$i] = substr($string, $i, 1);
3077  }
3078 
3079  //Check NIF
3080  if (preg_match('/(^[0-9]{9}$)/', $string)) {
3081  return 1;
3082  }
3083  else {
3084  return -1;
3085  }
3086  }
3087 
3088  return $ok;
3089  }
3090 
3091  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
3100  function id_prof_url($idprof,$thirdparty)
3101  {
3102  // phpcs:enable
3103  global $conf,$langs,$hookmanager;
3104 
3105  $url='';
3106  $action = '';
3107 
3108  $hookmanager->initHooks(array('idprofurl'));
3109  $parameters=array('idprof'=>$idprof, 'company'=>$thirdparty);
3110  $reshook=$hookmanager->executeHooks('getIdProfUrl',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
3111  if (empty($reshook)) {
3112  if (! empty($conf->global->MAIN_DISABLEPROFIDRULES)) {
3113  return '';
3114  }
3115 
3116  // TODO Move links to validate professional ID into a dictionary table "country" + "link"
3117  $strippedIdProf1 = str_replace(' ', '', $thirdparty->idprof1);
3118  if ($idprof == 1 && $thirdparty->country_code == 'FR') {
3119  $url='http://www.societe.com/cgi-bin/search?champs='.$strippedIdProf1; // See also http://avis-situation-sirene.insee.fr/
3120  }
3121  if ($idprof == 1 && ($thirdparty->country_code == 'GB' || $thirdparty->country_code == 'UK')) {
3122  $url='https://beta.companieshouse.gov.uk/company/'.$strippedIdProf1;
3123  }
3124  if ($idprof == 1 && $thirdparty->country_code == 'ES') {
3125  $url='http://www.e-informa.es/servlet/app/portal/ENTP/screen/SProducto/prod/ETIQUETA_EMPRESA/nif/'.$strippedIdProf1;
3126  }
3127  if ($idprof == 1 && $thirdparty->country_code == 'IN') {
3128  $url='http://www.tinxsys.com/TinxsysInternetWeb/dealerControllerServlet?tinNumber='.$strippedIdProf1.';&searchBy=TIN&backPage=searchByTin_Inter.jsp';
3129  }
3130  if ($idprof == 1 && $thirdparty->country_code == 'PT') {
3131  $url='http://www.nif.pt/'.$strippedIdProf1;
3132  }
3133 
3134  if ($url) {
3135  return '<a target="_blank" href="'.$url.'">'.$langs->trans("Check").'</a>';
3136  }
3137  }
3138  else {
3139  return $hookmanager->resPrint;
3140  }
3141 
3142  return '';
3143  }
3144 
3145  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
3151  function has_projects()
3152  {
3153  // phpcs:enable
3154  $sql = 'SELECT COUNT(*) as numproj FROM '.MAIN_DB_PREFIX.'projet WHERE fk_soc = ' . $this->id;
3155  $resql = $this->db->query($sql);
3156  if ($resql)
3157  {
3158  $obj = $this->db->fetch_object($resql);
3159  $count = $obj->numproj;
3160  }
3161  else
3162  {
3163  $count = 0;
3164  print $this->db->error();
3165  }
3166  $this->db->free($resql);
3167  return ($count > 0);
3168  }
3169 
3170 
3177  function info($id)
3178  {
3179  $sql = "SELECT s.rowid, s.nom as name, s.datec as date_creation, tms as date_modification,";
3180  $sql.= " fk_user_creat, fk_user_modif";
3181  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
3182  $sql.= " WHERE s.rowid = ".$id;
3183 
3184  $result=$this->db->query($sql);
3185  if ($result)
3186  {
3187  if ($this->db->num_rows($result))
3188  {
3189  $obj = $this->db->fetch_object($result);
3190 
3191  $this->id = $obj->rowid;
3192 
3193  if ($obj->fk_user_creat) {
3194  $cuser = new User($this->db);
3195  $cuser->fetch($obj->fk_user_creat);
3196  $this->user_creation = $cuser;
3197  }
3198 
3199  if ($obj->fk_user_modif) {
3200  $muser = new User($this->db);
3201  $muser->fetch($obj->fk_user_modif);
3202  $this->user_modification = $muser;
3203  }
3204 
3205  $this->ref = $obj->name;
3206  $this->date_creation = $this->db->jdate($obj->date_creation);
3207  $this->date_modification = $this->db->jdate($obj->date_modification);
3208  }
3209 
3210  $this->db->free($result);
3211  }
3212  else
3213  {
3214  dol_print_error($this->db);
3215  }
3216  }
3217 
3223  function isACompany()
3224  {
3225  global $conf;
3226 
3227  // Define if third party is treated as company (or not) when nature is unknown
3228  $isacompany=empty($conf->global->MAIN_UNKNOWN_CUSTOMERS_ARE_COMPANIES)?0:1; // 0 by default
3229  if (! empty($this->tva_intra)) $isacompany=1;
3230  else if (! empty($this->typent_code) && $this->typent_code != 'TE_UNKNOWN')
3231  {
3232  // TODO Add a field is_a_company into dictionary
3233  if (preg_match('/^TE_PRIVATE/', $this->typent_code)) $isacompany=0;
3234  else $isacompany=1;
3235  }
3236 
3237  return $isacompany;
3238  }
3239 
3245  function isInEEC()
3246  {
3247  require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
3248  return isInEEC($this);
3249  }
3250 
3251  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
3258  {
3259  // phpcs:enable
3260  $this->SupplierCategories = array();
3261  $sql = "SELECT rowid, label";
3262  $sql.= " FROM ".MAIN_DB_PREFIX."categorie";
3263  $sql.= " WHERE type = ".Categorie::TYPE_SUPPLIER;
3264 
3265  $resql=$this->db->query($sql);
3266  if ($resql)
3267  {
3268  while ($obj = $this->db->fetch_object($resql) )
3269  {
3270  $this->SupplierCategories[$obj->rowid] = $obj->label;
3271  }
3272  return 0;
3273  }
3274  else
3275  {
3276  return -1;
3277  }
3278  }
3279 
3280  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
3287  function AddFournisseurInCategory($categorie_id)
3288  {
3289  // phpcs:enable
3290  if ($categorie_id > 0 && $this->id > 0)
3291  {
3292  $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_fournisseur (fk_categorie, fk_soc) ";
3293  $sql.= " VALUES (".$categorie_id.", ".$this->id.")";
3294 
3295  if ($resql=$this->db->query($sql)) return 0;
3296  }
3297  else
3298  {
3299  return 0;
3300  }
3301  return -1;
3302  }
3303 
3304 
3305  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
3315  function create_from_member(Adherent $member, $socname='', $socalias='', $customercode='')
3316  {
3317  // phpcs:enable
3318  global $user,$langs;
3319 
3320  dol_syslog(get_class($this)."::create_from_member", LOG_DEBUG);
3321 
3322  $name = $socname?$socname:$member->societe;
3323  if (empty($name)) $name=$member->getFullName($langs);
3324 
3325  $alias = $socalias?$socalias:'';
3326 
3327  // Positionne parametres
3328  $this->nom=$name; // TODO deprecated
3329  $this->name=$name;
3330  $this->name_alias=$alias;
3331  $this->address=$member->address;
3332  $this->zip=$member->zip;
3333  $this->town=$member->town;
3334  $this->country_code=$member->country_code;
3335  $this->country_id=$member->country_id;
3336  $this->phone=$member->phone; // Prof phone
3337  $this->email=$member->email;
3338  $this->skype=$member->skype;
3339  $this->twitter=$member->twitter;
3340  $this->facebook=$member->facebook;
3341 
3342  $this->client = 1; // A member is a customer by default
3343  $this->code_client = ($customercode?$customercode:-1);
3344  $this->code_fournisseur = -1;
3345 
3346  $this->db->begin();
3347 
3348  // Cree et positionne $this->id
3349  $result=$this->create($user);
3350  if ($result >= 0)
3351  {
3352  $sql = "UPDATE ".MAIN_DB_PREFIX."adherent";
3353  $sql.= " SET fk_soc=".$this->id;
3354  $sql.= " WHERE rowid=".$member->id;
3355 
3356  $resql=$this->db->query($sql);
3357  if ($resql)
3358  {
3359  $this->db->commit();
3360  return $this->id;
3361  }
3362  else
3363  {
3364  $this->error=$this->db->error();
3365 
3366  $this->db->rollback();
3367  return -1;
3368  }
3369  }
3370  else
3371  {
3372  // $this->error deja positionne
3373  dol_syslog(get_class($this)."::create_from_member - 2 - ".$this->error." - ".join(',',$this->errors), LOG_ERR);
3374 
3375  $this->db->rollback();
3376  return $result;
3377  }
3378  }
3379 
3386  function setMysoc(Conf $conf)
3387  {
3388  global $langs;
3389 
3390  $this->id=0;
3391  $this->name=empty($conf->global->MAIN_INFO_SOCIETE_NOM)?'':$conf->global->MAIN_INFO_SOCIETE_NOM;
3392  $this->address=empty($conf->global->MAIN_INFO_SOCIETE_ADDRESS)?'':$conf->global->MAIN_INFO_SOCIETE_ADDRESS;
3393  $this->zip=empty($conf->global->MAIN_INFO_SOCIETE_ZIP)?'':$conf->global->MAIN_INFO_SOCIETE_ZIP;
3394  $this->town=empty($conf->global->MAIN_INFO_SOCIETE_TOWN)?'':$conf->global->MAIN_INFO_SOCIETE_TOWN;
3395  $this->state_id=empty($conf->global->MAIN_INFO_SOCIETE_STATE)?'':$conf->global->MAIN_INFO_SOCIETE_STATE;
3396  $this->region_code=empty($conf->global->MAIN_INFO_SOCIETE_REGION)?'':$conf->global->MAIN_INFO_SOCIETE_REGION;
3397  $this->object=empty($conf->global->MAIN_INFO_SOCIETE_OBJECT)?'':$conf->global->MAIN_INFO_SOCIETE_OBJECT;
3398 
3399  $this->note_private=empty($conf->global->MAIN_INFO_SOCIETE_NOTE)?'':$conf->global->MAIN_INFO_SOCIETE_NOTE;
3400 
3401  $this->nom=$this->name; // deprecated
3402 
3403  // We define country_id, country_code and country
3404  $country_id=$country_code=$country_label='';
3405  if (! empty($conf->global->MAIN_INFO_SOCIETE_COUNTRY))
3406  {
3407  $tmp=explode(':',$conf->global->MAIN_INFO_SOCIETE_COUNTRY);
3408  $country_id=$tmp[0];
3409  if (! empty($tmp[1])) // If $conf->global->MAIN_INFO_SOCIETE_COUNTRY is "id:code:label"
3410  {
3411  $country_code=$tmp[1];
3412  $country_label=$tmp[2];
3413  }
3414  else // For backward compatibility
3415  {
3416  dol_syslog("Your country setup use an old syntax. Reedit it using setup area.", LOG_ERR);
3417  include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
3418  $country_code=getCountry($country_id,2,$this->db); // This need a SQL request, but it's the old feature that should not be used anymore
3419  $country_label=getCountry($country_id,0,$this->db); // This need a SQL request, but it's the old feature that should not be used anymore
3420  }
3421  }
3422  $this->country_id=$country_id;
3423  $this->country_code=$country_code;
3424  $this->country=$country_label;
3425  if (is_object($langs)) $this->country=($langs->trans('Country'.$country_code)!='Country'.$country_code)?$langs->trans('Country'.$country_code):$country_label;
3426 
3427  $this->phone=empty($conf->global->MAIN_INFO_SOCIETE_TEL)?'':$conf->global->MAIN_INFO_SOCIETE_TEL;
3428  $this->fax=empty($conf->global->MAIN_INFO_SOCIETE_FAX)?'':$conf->global->MAIN_INFO_SOCIETE_FAX;
3429  $this->url=empty($conf->global->MAIN_INFO_SOCIETE_WEB)?'':$conf->global->MAIN_INFO_SOCIETE_WEB;
3430  // Id prof generiques
3431  $this->idprof1=empty($conf->global->MAIN_INFO_SIREN)?'':$conf->global->MAIN_INFO_SIREN;
3432  $this->idprof2=empty($conf->global->MAIN_INFO_SIRET)?'':$conf->global->MAIN_INFO_SIRET;
3433  $this->idprof3=empty($conf->global->MAIN_INFO_APE)?'':$conf->global->MAIN_INFO_APE;
3434  $this->idprof4=empty($conf->global->MAIN_INFO_RCS)?'':$conf->global->MAIN_INFO_RCS;
3435  $this->idprof5=empty($conf->global->MAIN_INFO_PROFID5)?'':$conf->global->MAIN_INFO_PROFID5;
3436  $this->idprof6=empty($conf->global->MAIN_INFO_PROFID6)?'':$conf->global->MAIN_INFO_PROFID6;
3437  $this->tva_intra=empty($conf->global->MAIN_INFO_TVAINTRA)?'':$conf->global->MAIN_INFO_TVAINTRA; // VAT number, not necessarly INTRA.
3438  $this->managers=empty($conf->global->MAIN_INFO_SOCIETE_MANAGERS)?'':$conf->global->MAIN_INFO_SOCIETE_MANAGERS;
3439  $this->capital=empty($conf->global->MAIN_INFO_CAPITAL)?'':$conf->global->MAIN_INFO_CAPITAL;
3440  $this->forme_juridique_code=empty($conf->global->MAIN_INFO_SOCIETE_FORME_JURIDIQUE)?'':$conf->global->MAIN_INFO_SOCIETE_FORME_JURIDIQUE;
3441  $this->email=empty($conf->global->MAIN_INFO_SOCIETE_MAIL)?'':$conf->global->MAIN_INFO_SOCIETE_MAIL;
3442  $this->logo=empty($conf->global->MAIN_INFO_SOCIETE_LOGO)?'':$conf->global->MAIN_INFO_SOCIETE_LOGO;
3443  $this->logo_small=empty($conf->global->MAIN_INFO_SOCIETE_LOGO_SMALL)?'':$conf->global->MAIN_INFO_SOCIETE_LOGO_SMALL;
3444  $this->logo_mini=empty($conf->global->MAIN_INFO_SOCIETE_LOGO_MINI)?'':$conf->global->MAIN_INFO_SOCIETE_LOGO_MINI;
3445 
3446  // Define if company use vat or not
3447  $this->tva_assuj=$conf->global->FACTURE_TVAOPTION;
3448 
3449  // Define if company use local taxes
3450  $this->localtax1_assuj=((isset($conf->global->FACTURE_LOCAL_TAX1_OPTION) && ($conf->global->FACTURE_LOCAL_TAX1_OPTION=='1' || $conf->global->FACTURE_LOCAL_TAX1_OPTION=='localtax1on'))?1:0);
3451  $this->localtax2_assuj=((isset($conf->global->FACTURE_LOCAL_TAX2_OPTION) && ($conf->global->FACTURE_LOCAL_TAX2_OPTION=='1' || $conf->global->FACTURE_LOCAL_TAX2_OPTION=='localtax2on'))?1:0);
3452  }
3453 
3461  function initAsSpecimen()
3462  {
3463  $now=dol_now();
3464 
3465  // Initialize parameters
3466  $this->id=0;
3467  $this->name = 'THIRDPARTY SPECIMEN '.dol_print_date($now,'dayhourlog');
3468  $this->nom = $this->name; // For backward compatibility
3469  $this->ref_ext = 'Ref ext';
3470  $this->specimen=1;
3471  $this->address='21 jump street';
3472  $this->zip='99999';
3473  $this->town='MyTown';
3474  $this->state_id=1;
3475  $this->state_code='AA';
3476  $this->state='MyState';
3477  $this->country_id=1;
3478  $this->country_code='FR';
3479  $this->email='specimen@specimen.com';
3480  $this->skype='tom.hanson';
3481  $this->twitter='tomhanson';
3482  $this->facebook='tomhanson';
3483  $this->url='http://www.specimen.com';
3484 
3485  $this->phone='0909090901';
3486  $this->fax='0909090909';
3487 
3488  $this->code_client='CC-'.dol_print_date($now,'dayhourlog');
3489  $this->code_fournisseur='SC-'.dol_print_date($now,'dayhourlog');
3490  $this->capital=10000;
3491  $this->client=1;
3492  $this->prospect=1;
3493  $this->fournisseur=1;
3494  $this->tva_assuj=1;
3495  $this->tva_intra='EU1234567';
3496  $this->note_public='This is a comment (public)';
3497  $this->note_private='This is a comment (private)';
3498 
3499  $this->idprof1='idprof1';
3500  $this->idprof2='idprof2';
3501  $this->idprof3='idprof3';
3502  $this->idprof4='idprof4';
3503  $this->idprof5='idprof5';
3504  $this->idprof6='idprof6';
3505  }
3506 
3513  function useLocalTax($localTaxNum=0)
3514  {
3515  $sql = "SELECT t.localtax1, t.localtax2";
3516  $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
3517  $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$this->db->escape($this->country_code)."'";
3518  $sql .= " AND t.active = 1";
3519  if (empty($localTaxNum)) $sql .= " AND (t.localtax1_type <> '0' OR t.localtax2_type <> '0')";
3520  elseif ($localTaxNum == 1) $sql .= " AND t.localtax1_type <> '0'";
3521  elseif ($localTaxNum == 2) $sql .= " AND t.localtax2_type <> '0'";
3522 
3523  dol_syslog("useLocalTax", LOG_DEBUG);
3524  $resql=$this->db->query($sql);
3525  if ($resql)
3526  {
3527  return ($this->db->num_rows($resql) > 0);
3528  }
3529  else return false;
3530  }
3531 
3537  function useNPR()
3538  {
3539  $sql = "SELECT t.rowid";
3540  $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
3541  $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$this->db->escape($this->country_code)."'";
3542  $sql .= " AND t.active = 1 AND t.recuperableonly = 1";
3543 
3544  dol_syslog("useNPR", LOG_DEBUG);
3545  $resql=$this->db->query($sql);
3546  if ($resql)
3547  {
3548  return ($this->db->num_rows($resql) > 0);
3549  }
3550  else return false;
3551  }
3552 
3558  function useRevenueStamp()
3559  {
3560  $sql = "SELECT COUNT(*) as nb";
3561  $sql .= " FROM ".MAIN_DB_PREFIX."c_revenuestamp as r, ".MAIN_DB_PREFIX."c_country as c";
3562  $sql .= " WHERE r.fk_pays = c.rowid AND c.code = '".$this->db->escape($this->country_code)."'";
3563  $sql .= " AND r.active = 1";
3564 
3565  dol_syslog("useRevenueStamp", LOG_DEBUG);
3566  $resql=$this->db->query($sql);
3567  if ($resql)
3568  {
3569  $obj=$this->db->fetch_object($resql);
3570  return (($obj->nb > 0)?true:false);
3571  }
3572  else
3573  {
3574  $this->error=$this->db->lasterror();
3575  return false;
3576  }
3577  }
3578 
3584  function getLibProspLevel()
3585  {
3586  return $this->LibProspLevel($this->fk_prospectlevel);
3587  }
3588 
3589  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
3596  function LibProspLevel($fk_prospectlevel)
3597  {
3598  // phpcs:enable
3599  global $langs;
3600 
3601  $lib=$langs->trans("ProspectLevel".$fk_prospectlevel);
3602  // If lib not found in language file, we get label from cache/databse
3603  if ($lib == $langs->trans("ProspectLevel".$fk_prospectlevel))
3604  {
3605  $lib=$langs->getLabelFromKey($this->db,$fk_prospectlevel,'c_prospectlevel','code','label');
3606  }
3607  return $lib;
3608  }
3609 
3610 
3611  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
3619  function set_prospect_level(User $user)
3620  {
3621  // phpcs:enable
3622  return $this->update($this->id, $user);
3623  }
3624 
3632  function getLibProspCommStatut($mode=0, $label='')
3633  {
3634  return $this->LibProspCommStatut($this->stcomm_id, $mode, $label);
3635  }
3636 
3637  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
3646  function LibProspCommStatut($statut, $mode=0, $label='')
3647  {
3648  // phpcs:enable
3649  global $langs;
3650  $langs->load('customers');
3651 
3652  if ($mode == 2)
3653  {
3654  if ($statut == '-1' || $statut == 'ST_NO') return img_action($langs->trans("StatusProspect-1"),-1).' '.$langs->trans("StatusProspect-1");
3655  elseif ($statut == '0' || $statut == 'ST_NEVER') return img_action($langs->trans("StatusProspect0"), 0).' '.$langs->trans("StatusProspect0");
3656  elseif ($statut == '1' || $statut == 'ST_TODO') return img_action($langs->trans("StatusProspect1"), 1).' '.$langs->trans("StatusProspect1");
3657  elseif ($statut == '2' || $statut == 'ST_PEND') return img_action($langs->trans("StatusProspect2"), 2).' '.$langs->trans("StatusProspect2");
3658  elseif ($statut == '3' || $statut == 'ST_DONE') return img_action($langs->trans("StatusProspect3"), 3).' '.$langs->trans("StatusProspect3");
3659  else
3660  {
3661  return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0).' '.(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label);
3662  }
3663  }
3664  if ($mode == 3)
3665  {
3666  if ($statut == '-1' || $statut == 'ST_NO') return img_action($langs->trans("StatusProspect-1"),-1);
3667  elseif ($statut == '0' || $statut == 'ST_NEVER') return img_action($langs->trans("StatusProspect0"), 0);
3668  elseif ($statut == '1' || $statut == 'ST_TODO') return img_action($langs->trans("StatusProspect1"), 1);
3669  elseif ($statut == '2' || $statut == 'ST_PEND') return img_action($langs->trans("StatusProspect2"), 2);
3670  elseif ($statut == '3' || $statut == 'ST_DONE') return img_action($langs->trans("StatusProspect3"), 3);
3671  else
3672  {
3673  return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0);
3674  }
3675  }
3676  if ($mode == 4)
3677  {
3678  if ($statut == '-1' || $statut == 'ST_NO') return img_action($langs->trans("StatusProspect-1"),-1).' '.$langs->trans("StatusProspect-1");
3679  elseif ($statut == '0' || $statut == 'ST_NEVER') return img_action($langs->trans("StatusProspect0"), 0).' '.$langs->trans("StatusProspect0");
3680  elseif ($statut == '1' || $statut == 'ST_TODO') return img_action($langs->trans("StatusProspect1"), 1).' '.$langs->trans("StatusProspect1");
3681  elseif ($statut == '2' || $statut == 'ST_PEND') return img_action($langs->trans("StatusProspect2"), 2).' '.$langs->trans("StatusProspect2");
3682  elseif ($statut == '3' || $statut == 'ST_DONE') return img_action($langs->trans("StatusProspect3"), 3).' '.$langs->trans("StatusProspect3");
3683  else
3684  {
3685  return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0).' '.(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label);
3686  }
3687  }
3688 
3689  return "Error, mode/status not found";
3690  }
3691 
3692  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
3700  function set_OutstandingBill(User $user)
3701  {
3702  // phpcs:enable
3703  return $this->update($this->id, $user);
3704  }
3705 
3712  function getOutstandingProposals($mode='customer')
3713  {
3714  $table='propal';
3715  if ($mode == 'supplier') $table = 'supplier_proposal';
3716 
3717  $sql = "SELECT rowid, total_ht, total as total_ttc, fk_statut FROM ".MAIN_DB_PREFIX.$table." as f";
3718  $sql .= " WHERE fk_soc = ". $this->id;
3719  if ($mode == 'supplier') {
3720  $sql .= " AND entity IN (".getEntity('supplier_proposal').")";
3721  } else {
3722  $sql .= " AND entity IN (".getEntity('propal').")";
3723  }
3724 
3725  dol_syslog("getOutstandingProposals", LOG_DEBUG);
3726  $resql=$this->db->query($sql);
3727  if ($resql)
3728  {
3729  $outstandingOpened = 0;
3730  $outstandingTotal = 0;
3731  $outstandingTotalIncTax = 0;
3732  while($obj=$this->db->fetch_object($resql)) {
3733  $outstandingTotal+= $obj->total_ht;
3734  $outstandingTotalIncTax+= $obj->total_ttc;
3735  if ($obj->fk_statut != 0) // Not a draft
3736  {
3737  $outstandingOpened+=$obj->total_ttc;
3738  }
3739  }
3740  return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax); // 'opened' is 'incl taxes'
3741  }
3742  else
3743  return array();
3744  }
3745 
3752  function getOutstandingOrders($mode='customer')
3753  {
3754  $table='commande';
3755  if ($mode == 'supplier') $table = 'commande_fournisseur';
3756 
3757  $sql = "SELECT rowid, total_ht, total_ttc, fk_statut FROM ".MAIN_DB_PREFIX.$table." as f";
3758  $sql .= " WHERE fk_soc = ". $this->id;
3759  if ($mode == 'supplier') {
3760  $sql .= " AND entity IN (".getEntity('supplier_order').")";
3761  } else {
3762  $sql .= " AND entity IN (".getEntity('commande').")";
3763  }
3764 
3765  dol_syslog("getOutstandingOrders", LOG_DEBUG);
3766  $resql=$this->db->query($sql);
3767  if ($resql)
3768  {
3769  $outstandingOpened = 0;
3770  $outstandingTotal = 0;
3771  $outstandingTotalIncTax = 0;
3772  while($obj=$this->db->fetch_object($resql)) {
3773  $outstandingTotal+= $obj->total_ht;
3774  $outstandingTotalIncTax+= $obj->total_ttc;
3775  if ($obj->fk_statut != 0) // Not a draft
3776  {
3777  $outstandingOpened+=$obj->total_ttc;
3778  }
3779  }
3780  return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax); // 'opened' is 'incl taxes'
3781  }
3782  else
3783  return array();
3784  }
3785 
3792  function getOutstandingBills($mode='customer')
3793  {
3794  $table='facture';
3795  if ($mode == 'supplier') $table = 'facture_fourn';
3796 
3797  /* Accurate value of remain to pay is to sum remaintopay for each invoice
3798  $paiement = $invoice->getSommePaiement();
3799  $creditnotes=$invoice->getSumCreditNotesUsed();
3800  $deposits=$invoice->getSumDepositsUsed();
3801  $alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT');
3802  $remaintopay=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT');
3803  */
3804  if ($mode == 'supplier') $sql = "SELECT rowid, total_ht as total_ht, total_ttc, paye, fk_statut, close_code FROM ".MAIN_DB_PREFIX.$table." as f";
3805  else $sql = "SELECT rowid, total as total_ht, total_ttc, paye, fk_statut, close_code FROM ".MAIN_DB_PREFIX.$table." as f";
3806  $sql .= " WHERE fk_soc = ". $this->id;
3807  if ($mode == 'supplier') {
3808  $sql .= " AND entity IN (".getEntity('facture_fourn').")";
3809  } else {
3810  $sql .= " AND entity IN (".getEntity('facture').")";
3811  }
3812 
3813  dol_syslog("getOutstandingBills", LOG_DEBUG);
3814  $resql=$this->db->query($sql);
3815  if ($resql)
3816  {
3817  $outstandingOpened = 0;
3818  $outstandingTotal = 0;
3819  $outstandingTotalIncTax = 0;
3820  if ($mode == 'supplier')
3821  {
3822  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
3823  $tmpobject=new FactureFournisseur($this->db);
3824  }
3825  else
3826  {
3827  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
3828  $tmpobject=new Facture($this->db);
3829  }
3830  while($obj=$this->db->fetch_object($resql)) {
3831  $tmpobject->id=$obj->rowid;
3832  if ($obj->fk_statut != 0 // Not a draft
3833  && ! ($obj->fk_statut == 3 && $obj->close_code == 'replaced') // Not a replaced invoice
3834  )
3835  {
3836  $outstandingTotal+= $obj->total_ht;
3837  $outstandingTotalIncTax+= $obj->total_ttc;
3838  }
3839  if ($obj->paye == 0
3840  && $obj->fk_statut != 0 // Not a draft
3841  && $obj->fk_statut != 3 // Not abandonned
3842  && $obj->fk_statut != 2) // Not classified as paid
3843  //$sql .= " AND (fk_statut <> 3 OR close_code <> 'abandon')"; // Not abandonned for undefined reason
3844  {
3845  $paiement = $tmpobject->getSommePaiement();
3846  $creditnotes = $tmpobject->getSumCreditNotesUsed();
3847  $deposits = $tmpobject->getSumDepositsUsed();
3848  $outstandingOpened+=$obj->total_ttc - $paiement - $creditnotes - $deposits;
3849  }
3850  }
3851  return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax); // 'opened' is 'incl taxes'
3852  }
3853  else
3854  {
3855  return array();
3856  }
3857  }
3858 
3859  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
3868  {
3869  // phpcs:enable
3870  /* Accurate value of remain to pay is to sum remaintopay for each invoice
3871  $paiement = $invoice->getSommePaiement();
3872  $creditnotes=$invoice->getSumCreditNotesUsed();
3873  $deposits=$invoice->getSumDepositsUsed();
3874  $alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT');
3875  $remaintopay=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT');
3876  */
3877  $sql = "SELECT rowid, total_ttc FROM ".MAIN_DB_PREFIX."facture as f";
3878  $sql .= " WHERE fk_soc = ". $this->id;
3879  $sql .= " AND paye = 0";
3880  $sql .= " AND fk_statut <> 0"; // Not a draft
3881  $sql .= " AND entity IN (".getEntity('invoice').")";
3882  //$sql .= " AND (fk_statut <> 3 OR close_code <> 'abandon')"; // Not abandonned for undefined reason
3883  $sql .= " AND fk_statut <> 3"; // Not abandonned
3884  $sql .= " AND fk_statut <> 2"; // Not clasified as paid
3885 
3886  dol_syslog("get_OutstandingBill", LOG_DEBUG);
3887  $resql=$this->db->query($sql);
3888  if ($resql)
3889  {
3890  $outstandingAmount = 0;
3891  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
3892  $tmpobject=new Facture($this->db);
3893  while($obj=$this->db->fetch_object($resql)) {
3894  $tmpobject->id=$obj->rowid;
3895  $paiement = $tmpobject->getSommePaiement();
3896  $creditnotes = $tmpobject->getSumCreditNotesUsed();
3897  $deposits = $tmpobject->getSumDepositsUsed();
3898  $outstandingAmount+= $obj->total_ttc - $paiement - $creditnotes - $deposits;
3899  }
3900  return $outstandingAmount;
3901  }
3902  else
3903  return 0;
3904  }
3905 
3912  {
3913  return $this->LibCustProspStatut($this->client);
3914  }
3915 
3916  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
3923  function LibCustProspStatut($statut)
3924  {
3925  // phpcs:enable
3926  global $langs;
3927  $langs->load('companies');
3928 
3929  if ($statut==0) return $langs->trans("NorProspectNorCustomer");
3930  if ($statut==1) return $langs->trans("Customer");
3931  if ($statut==2) return $langs->trans("Prospect");
3932  if ($statut==3) return $langs->trans("ProspectCustomer");
3933  }
3934 
3935 
3947  public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
3948  {
3949  global $conf,$user,$langs;
3950 
3951  if (! empty($moreparams) && ! empty($moreparams['use_companybankid']))
3952  {
3953  $modelpath = "core/modules/bank/doc/";
3954 
3955  include_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
3956  $companybankaccount = new CompanyBankAccount($this->db);
3957  $result = $companybankaccount->fetch($moreparams['use_companybankid']);
3958  if (! $result) dol_print_error($this->db, $companybankaccount->error, $companybankaccount->errors);
3959  $result=$companybankaccount->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3960  }
3961  else
3962  {
3963  // Positionne le modele sur le nom du modele a utiliser
3964  if (! dol_strlen($modele))
3965  {
3966  if (! empty($conf->global->COMPANY_ADDON_PDF))
3967  {
3968  $modele = $conf->global->COMPANY_ADDON_PDF;
3969  }
3970  else
3971  {
3972  print $langs->trans("Error")." ".$langs->trans("Error_COMPANY_ADDON_PDF_NotDefined");
3973  return 0;
3974  }
3975  }
3976 
3977  $modelpath = "core/modules/societe/doc/";
3978 
3979  $result=$this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
3980  }
3981 
3982  return $result;
3983  }
3984 
3985 
3997  public function setCategories($categories, $type)
3998  {
3999  require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
4000 
4001  // Decode type
4002  if ($type == 'customer') {
4003  $type_id = Categorie::TYPE_CUSTOMER;
4004  $type_text = 'customer';
4005  } elseif ($type == 'supplier') {
4006  $type_id = Categorie::TYPE_SUPPLIER;
4007  $type_text = 'supplier';
4008  } else {
4009  dol_syslog(__METHOD__ . ': Type ' . $type . 'is an unknown company category type. Done nothing.', LOG_ERR);
4010  return -1;
4011  }
4012 
4013  // Handle single category
4014  if (!is_array($categories)) {
4015  $categories = array($categories);
4016  }
4017 
4018  // Get current categories
4019  $c = new Categorie($this->db);
4020  $existing = $c->containing($this->id, $type_id, 'id');
4021 
4022  // Diff
4023  if (is_array($existing)) {
4024  $to_del = array_diff($existing, $categories);
4025  $to_add = array_diff($categories, $existing);
4026  } else {
4027  $to_del = array(); // Nothing to delete
4028  $to_add = $categories;
4029  }
4030 
4031  $error = 0;
4032 
4033  // Process
4034  foreach ($to_del as $del) {
4035  if ($c->fetch($del) > 0) {
4036  $c->del_type($this, $type_text);
4037  }
4038  }
4039  foreach ($to_add as $add) {
4040  if ($c->fetch($add) > 0)
4041  {
4042  $result = $c->add_type($this, $type_text);
4043  if ($result < 0)
4044  {
4045  $error++;
4046  $this->error = $c->error;
4047  $this->errors = $c->errors;
4048  break;
4049  }
4050  }
4051  }
4052 
4053  return $error ? -1 : 1;
4054  }
4055 
4062  public function setSalesRep($salesrep)
4063  {
4064  global $user;
4065 
4066  // Handle single user
4067  if (!is_array($salesrep)) {
4068  $salesrep = array($salesrep);
4069  }
4070 
4071  // Get current users
4072  $existing = $this->getSalesRepresentatives($user, 1);
4073 
4074  // Diff
4075  if (is_array($existing)) {
4076  $to_del = array_diff($existing, $salesrep);
4077  $to_add = array_diff($salesrep, $existing);
4078  } else {
4079  $to_del = array(); // Nothing to delete
4080  $to_add = $salesrep;
4081  }
4082 
4083  $error = 0;
4084 
4085  // Process
4086  foreach ($to_del as $del) {
4087  $this->del_commercial($user, $del);
4088  }
4089  foreach ($to_add as $add) {
4090  $result = $this->add_commercial($user, $add);
4091  if ($result < 0)
4092  {
4093  $error++;
4094  $this->error = $c->error;
4095  $this->errors = $c->errors;
4096  break;
4097  }
4098  }
4099 
4100  return $error ? -1 : 1;
4101  }
4102 
4103 
4113  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
4114  {
4115  if ($origin_id == $dest_id)
4116  {
4117  dol_syslog('Error: Try to merge a thirdparty into itself');
4118  return false;
4119  }
4120 
4125  $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'societe_commerciaux ';
4126  $sql .= ' WHERE fk_soc = '.(int) $dest_id.' AND fk_user IN ( ';
4127  $sql .= ' SELECT fk_user ';
4128  $sql .= ' FROM '.MAIN_DB_PREFIX.'societe_commerciaux ';
4129  $sql .= ' WHERE fk_soc = '.(int) $origin_id.') ';
4130 
4131  $query = $db->query($sql);
4132 
4133  while ($result = $db->fetch_object($query)) {
4134  $db->query('DELETE FROM '.MAIN_DB_PREFIX.'societe_commerciaux WHERE rowid = '.$result->rowid);
4135  }
4136 
4141  $tables = array(
4142  'societe_address',
4143  'societe_commerciaux',
4144  'societe_log',
4145  'societe_prices',
4146  'societe_remise',
4147  'societe_remise_except',
4148  'societe_rib'
4149  );
4150 
4151  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
4152  }
4153 }
__construct($db)
Constructor.
useLocalTax($localTaxNum=0)
Check if we must use localtax feature or not according to country (country of $mysoc in most cases)...
check_codeclient()
Check customer code.
create(User $user)
Create third party in database.
thirdparty_and_contact_email_array($addthirdparty=0)
Return list of contacts emails existing for third party.
getLibStatut($mode=0)
Return label of status (activity, closed)
Class to stock current configuration.
Definition: conf.class.php:33
id_prof_url($idprof, $thirdparty)
Return an url to check online a professional id or empty string.
del_commercial(User $user, $commid)
Add link to sales representative.
create_from_member(Adherent $member, $socname='', $socalias='', $customercode='')
Create a third party into database from a member object.
print
Draft customers invoices.
Definition: index.php:91
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
getLibCustProspStatut()
Return label of status customer is prospect/customer.
static getIdFromCode(&$db, $code)
Get id of currency from code.
useNPR()
Check if we must use NPR Vat (french stupid rule) or not according to country (country of $mysoc in m...
LibCustProspStatut($statut)
Renvoi le libelle d&#39;un statut donne.
initAsSpecimen()
Initialise an instance with random values.
isACompany()
Return if third party is a company (Business) or an end user (Consumer)
set_remise_supplier($remise, $note, User $user)
Definit la societe comme un client.
add_commercial(User $user, $commid)
Add link to sales representative.
isObjectUsed($id=0)
Function to check if an object is used by others.
Class to manage contact/addresses.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
isInEEC()
Return if a company is inside the EEC (European Economic Community)
set_parent($id)
Define parent commany of current company.
get_codefournisseur($objsoc=0, $type=1)
Attribut un code fournisseur a partir du module de controle des codes.
Class to manage Dolibarr users.
Definition: user.class.php:41
Class to manage Dolibarr database access.
getNomUrl($withpicto=0, $option='', $maxlen=0, $notooltip=0, $save_lastsearch_value=-1)
Return a link on thirdparty (with picto)
setCategories($categories, $type)
Sets object to supplied categories.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
Class to manage bank accounts description of third parties.
thirdparty_and_contact_phone_array()
Return list of contacts mobile phone existing for third party.
clean_url($url, $http=1)
Clean an url string.
static showphoto($modulepart, $object, $width=100, $height=0, $caneditfield=0, $cssclass='photowithmargin', $imagesize='', $addlinktofullsize=1, $cache=0, $forcecapture='')
Return HTML code to output a photo.
getOutstandingProposals($mode='customer')
Return amount of order not paid and total.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:438
id_prof_exists($idprof, $value, $socid=0)
Verify if a profid exists into database for others thirds.
getCountry($searchkey, $withcode='', $dbtouse=0, $outputlangs='', $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
Class to manage suppliers invoices.
getFullName($langs, $option=0, $nameorder=-1, $maxlen=0)
Return full name (civility+&#39; &#39;+name+&#39; &#39;+lastname)
LibProspLevel($fk_prospectlevel)
Return label of prospect level.
LibProspCommStatut($statut, $mode=0, $label='')
Return label of a given status.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:103
set_remise_except($remise, User $user, $desc, $tva_tx=0, $discount_type=0)
Add a discount for third party.
contact_property_array($mode='email', $hidedisabled=0)
Return list of contacts emails or mobile existing for third party.
create_individual(User $user)
Create a contact/address from thirdparty.
has_projects()
Indique si la societe a des projets.
getOutstandingOrders($mode='customer')
Return amount of order not paid and total.
LoadSupplierCateg()
Charge la liste des categories fournisseurs.
id_prof_verifiable($idprof)
Returns if a profid sould be verified.
fetch($rowid, $ref='', $ref_ext='', $ref_int='', $idprof1='', $idprof2='', $idprof3='', $idprof4='', $idprof5='', $idprof6='', $email='', $ref_alias='')
Load a third party from database into memory.
getAvailableDiscounts($user='', $filter='', $maxvalue=0, $discount_type=0)
Renvoie montant TTC des reductions/avoirs en cours disponibles de la societe.
get_codeclient($objsoc=0, $type=0)
Attribut un code client a partir du module de controle des codes.
fetch_object($resultset)
Renvoie la ligne courante (comme un objet) pour le curseur resultset.
setSalesRep($salesrep)
Sets sales representatives of the thirdparty.
getLibProspLevel()
Return prostect level.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage third parties objects (customers, suppliers, prospects...)
img_action($titlealt, $numaction)
Show logo action.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Class to manage categories.
$order_min_amount
Min order amounts.
getLibProspCommStatut($mode=0, $label='')
Return status of prospect.
if(! function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
codeclient_modifiable()
Verifie si un code client est modifiable en fonction des parametres du module de controle des codes...
set_as_client()
Define third party as a customer.
LibStatut($statut, $mode=0)
Renvoi le libelle d&#39;un statut donne.
if(! empty($arrayfields['s.nom']['checked'])) print_liste_field_titre($arrayfields['s.nom']['label'] s nom
Definition: list.php:573
Class to manage withdrawal receipts.
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
contact_get_property($rowid, $mode)
Return property of contact from its id.
set_remise_client($remise, $note, User $user)
Definit la societe comme un client.
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories) ...
Definition: files.lib.php:1273
Class to manage members of a foundation.
info($id)
Load information for tab info.
set_OutstandingBill(User $user)
Set outstanding value.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
get_OutstandingBill()
Return amount of bill not paid.
deleteExtraFields()
Delete all extra fields values for the current object.
dol_now($mode='gmt')
Return date for now.
query($query, $usesavepoint=0, $type='auto')
Execute a SQL request and return the resultset.
get_codecompta($type)
Renvoie un code compta, suivant le module de code compta.
set_prospect_level(User $user)
Set prospect level.
contact_array_objects()
Renvoie la liste des contacts de cette societe.
verify()
Check properties of third party are ok (like name, third party codes, ...) Used before an add or upda...
getOutstandingBills($mode='customer')
Return amount of bill not paid and total.
useRevenueStamp()
Check if we must use revenue stamps feature or not according to country (country of $mysocin most cas...
codefournisseur_modifiable()
Verifie si un code fournisseur est modifiable dans configuration du module de controle des codes...
$stcomm_id
code statut prospect
getSalesRepresentatives(User $user, $mode=0)
Return array of sales representatives.
Class to manage absolute discounts.
contact_array()
Renvoie la liste des contacts de cette societe.
Class to manage invoices.
setMysoc(Conf $conf)
Set properties with value into $conf.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
AddFournisseurInCategory($categorie_id)
Insert link supplier - category.
check_codefournisseur()
Check supplier code.
set_price_level($price_level, User $user)
Set the price level.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='')
Clean a string from all punctuation characters to use it as a ref or login.
get_all_rib()
Return Array of RIB.
id_prof_check($idprof, $soc)
Verifie la validite d&#39;un identifiant professionnel en fonction du pays de la societe (siren...
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
display_rib($mode='label')
Return bank number property of thirdparty (label or rum)
update($id, $user='', $call_trigger=1, $allowmodcodeclient=0, $allowmodcodefournisseur=0, $action='update', $nosyncmember=1)
Update parameters of third party.