societe.class.php 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216
  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-2012 Regis Houssin <regis.houssin@capnetworks.com>
  8. * Copyright (C) 2008 Patrick Raguin <patrick.raguin@auguria.net>
  9. * Copyright (C) 2010-2014 Juanjo Menent <jmenent@2byte.es>
  10. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  11. * Copyright (C) 2013 Alexandre Spangaro <alexandre.spangaro@gmail.com>
  12. * Copyright (C) 2013 Peter Fontaine <contact@peterfontaine.fr>
  13. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. */
  28. /**
  29. * \file htdocs/societe/class/societe.class.php
  30. * \ingroup societe
  31. * \brief File for third party class
  32. */
  33. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  34. /**
  35. * Class to manage third parties objects (customers, suppliers, prospects...)
  36. */
  37. class Societe extends CommonObject
  38. {
  39. public $element='societe';
  40. public $table_element = 'societe';
  41. public $fk_element='fk_soc';
  42. protected $childtables=array("propal","commande","facture","contrat","facture_fourn","commande_fournisseur","projet"); // To test if we can delete object
  43. /**
  44. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  45. * @var int
  46. */
  47. protected $ismultientitymanaged = 1;
  48. var $id;
  49. var $name;
  50. var $entity;
  51. /**
  52. * @deprecated Use $name instead
  53. */
  54. var $nom;
  55. var $firstname;
  56. var $lastname;
  57. var $particulier;
  58. var $civility_id;
  59. var $address;
  60. var $zip;
  61. var $town;
  62. /**
  63. * 0=activity ceased, 1= in activity
  64. * @var int
  65. */
  66. var $status;
  67. /**
  68. * Id of department
  69. */
  70. var $state_id;
  71. var $state_code;
  72. var $state;
  73. /**
  74. * @deprecated Use state_code instead
  75. */
  76. var $departement_code;
  77. /**
  78. * @deprecated Use state instead
  79. */
  80. var $departement;
  81. /**
  82. * @deprecated Use country instead
  83. */
  84. var $pays;
  85. var $country_id;
  86. var $country_code;
  87. var $country;
  88. /**
  89. * Phone number
  90. * @var string
  91. */
  92. var $phone;
  93. /**
  94. * Fax number
  95. * @var string
  96. */
  97. var $fax;
  98. /**
  99. * Email
  100. * @var string
  101. */
  102. var $email;
  103. /**
  104. * Skype username
  105. * @var string
  106. */
  107. var $skype;
  108. /**
  109. * Webpage
  110. * @var string
  111. */
  112. var $url;
  113. //! barcode
  114. /**
  115. * Barcode value
  116. * @var string
  117. */
  118. var $barcode;
  119. /**
  120. * ID of bardode type
  121. * @var int
  122. */
  123. var $barcode_type;
  124. /**
  125. * code (loaded by fetch_barcode)
  126. * @var string
  127. */
  128. var $barcode_type_code;
  129. /**
  130. * label (loaded by fetch_barcode)
  131. * @var string
  132. */
  133. var $barcode_type_label;
  134. /**
  135. * coder (loaded by fetch_barcode)
  136. * @var string
  137. */
  138. var $barcode_type_coder;
  139. // 6 professional id (usage depends on country)
  140. /**
  141. * Professional ID 1 (Ex: Siren in France)
  142. * @var string
  143. */
  144. var $idprof1;
  145. /**
  146. * Professional ID 2 (Ex: Siret in France)
  147. * @var string
  148. */
  149. var $idprof2;
  150. /**
  151. * Professional ID 3 (Ex: Ape in France)
  152. * @var string
  153. */
  154. var $idprof3;
  155. /**
  156. * Professional ID 4 (Ex: RCS in France)
  157. * @var string
  158. */
  159. var $idprof4;
  160. /**
  161. * Professional ID 5
  162. * @var string
  163. */
  164. var $idprof5;
  165. /**
  166. * Professional ID 6
  167. * @var string
  168. */
  169. var $idprof6;
  170. var $prefix_comm;
  171. var $tva_assuj;
  172. /**
  173. * Intracommunitary VAT ID
  174. * @var string
  175. */
  176. var $tva_intra;
  177. // Local taxes
  178. var $localtax1_assuj;
  179. var $localtax1_value;
  180. var $localtax2_assuj;
  181. var $localtax2_value;
  182. var $managers;
  183. var $capital;
  184. var $typent_id;
  185. var $typent_code;
  186. var $effectif;
  187. var $effectif_id;
  188. var $forme_juridique_code;
  189. var $forme_juridique;
  190. var $remise_percent;
  191. var $mode_reglement_id;
  192. var $cond_reglement_id;
  193. var $mode_reglement_supplier_id;
  194. var $cond_reglement_supplier_id;
  195. var $fk_prospectlevel;
  196. var $name_bis;
  197. //Log data
  198. /**
  199. * Date of last update
  200. */
  201. var $date_modification;
  202. /**
  203. * User that made last update
  204. */
  205. var $user_modification;
  206. /**
  207. * Date of creation
  208. */
  209. var $date_creation;
  210. /**
  211. * User that created the thirdparty
  212. */
  213. var $user_creation;
  214. var $specimen;
  215. /**
  216. * 0=no customer, 1=customer, 2=prospect, 3=customer and prospect
  217. * @var int
  218. */
  219. var $client;
  220. /**
  221. * 0=no prospect, 1=prospect
  222. * @var int
  223. */
  224. var $prospect;
  225. /**
  226. * 0=no supplier, 1=supplier
  227. * @var int
  228. */
  229. var $fournisseur;
  230. /**
  231. * Client code. E.g: CU2014-003
  232. * @var string
  233. */
  234. var $code_client;
  235. /**
  236. * Supplier code. E.g: SU2014-003
  237. * @var string
  238. */
  239. var $code_fournisseur;
  240. /**
  241. * Accounting code for client
  242. * @var string
  243. */
  244. var $code_compta;
  245. /**
  246. * Accounting code for suppliers
  247. * @var string
  248. */
  249. var $code_compta_fournisseur;
  250. /**
  251. * @deprecated Note is split in public and private notes
  252. */
  253. var $note;
  254. /**
  255. * Private note
  256. * @var string
  257. */
  258. var $note_private;
  259. /**
  260. * Public note
  261. * @var string
  262. */
  263. var $note_public;
  264. //! code statut prospect
  265. var $stcomm_id;
  266. var $statut_commercial;
  267. /**
  268. * Assigned price level
  269. * @var int
  270. */
  271. var $price_level;
  272. var $outstanding_limit;
  273. /**
  274. * Id of sales representative to link (used for thirdparty creation). Not filled by a fetch, because we can have several sales representatives.
  275. */
  276. var $commercial_id;
  277. var $parent;
  278. var $default_lang;
  279. var $ref;
  280. var $ref_int;
  281. /**
  282. * External user reference.
  283. * This is to allow external systems to store their id and make self-developed synchronizing functions easier to
  284. * build.
  285. * @var string
  286. */
  287. var $ref_ext;
  288. /**
  289. * Import key.
  290. * Set when the thirdparty has been created through an import process. This is to relate those created thirdparties
  291. * to an import process
  292. * @var string
  293. */
  294. var $import_key;
  295. /**
  296. * Supplier WebServices URL
  297. * @var string
  298. */
  299. var $webservices_url;
  300. /**
  301. * Supplier WebServices Key
  302. * @var string
  303. */
  304. var $webservices_key;
  305. var $logo;
  306. var $logo_small;
  307. var $logo_mini;
  308. var $array_options;
  309. /**
  310. * To contains a clone of this when we need to save old properties of object
  311. */
  312. var $oldcopy;
  313. /**
  314. * Constructor
  315. *
  316. * @param DoliDB $db Database handler
  317. */
  318. public function __construct($db)
  319. {
  320. $this->db = $db;
  321. $this->client = 0;
  322. $this->prospect = 0;
  323. $this->fournisseur = 0;
  324. $this->typent_id = 0;
  325. $this->effectif_id = 0;
  326. $this->forme_juridique_code = 0;
  327. $this->tva_assuj = 1;
  328. $this->status = 1;
  329. return 1;
  330. }
  331. /**
  332. * Create third party in database
  333. *
  334. * @param User $user Object of user that ask creation
  335. * @return int >= 0 if OK, < 0 if KO
  336. */
  337. function create($user='')
  338. {
  339. global $langs,$conf;
  340. $error=0;
  341. // Clean parameters
  342. if (empty($this->status)) $this->status=0;
  343. $this->name=$this->name?trim($this->name):trim($this->nom);
  344. if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->name=ucwords($this->name);
  345. $this->nom=$this->name; // For backward compatibility
  346. if (empty($this->client)) $this->client=0;
  347. if (empty($this->fournisseur)) $this->fournisseur=0;
  348. $this->import_key = trim($this->import_key);
  349. dol_syslog(get_class($this)."::create ".$this->name);
  350. // Check parameters
  351. if (! empty($conf->global->SOCIETE_MAIL_REQUIRED) && ! isValidEMail($this->email))
  352. {
  353. $langs->load("errors");
  354. $this->error = $langs->trans("ErrorBadEMail",$this->email);
  355. return -1;
  356. }
  357. $now=dol_now();
  358. $this->db->begin();
  359. // For automatic creation during create action (not used by Dolibarr GUI, can be used by scripts)
  360. if ($this->code_client == -1) $this->get_codeclient($this,0);
  361. if ($this->code_fournisseur == -1) $this->get_codefournisseur($this,1);
  362. // Check more parameters
  363. // If error, this->errors[] is filled
  364. $result = $this->verify();
  365. if ($result >= 0)
  366. {
  367. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe (nom, entity, datec, fk_user_creat, canvas, status, ref_int, ref_ext, fk_stcomm, import_key)";
  368. $sql.= " VALUES ('".$this->db->escape($this->name)."', ".$conf->entity.", '".$this->db->idate($now)."'";
  369. $sql.= ", ".(! empty($user->id) ? "'".$user->id."'":"null");
  370. $sql.= ", ".(! empty($this->canvas) ? "'".$this->canvas."'":"null");
  371. $sql.= ", ".$this->status;
  372. $sql.= ", ".(! empty($this->ref_int) ? "'".$this->ref_int."'":"null");
  373. $sql.= ", ".(! empty($this->ref_ext) ? "'".$this->ref_ext."'":"null");
  374. $sql.= ", 0";
  375. $sql.= ", ".(! empty($this->import_key) ? "'".$this->import_key."'":"null").")";
  376. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  377. $result=$this->db->query($sql);
  378. if ($result)
  379. {
  380. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe");
  381. $ret = $this->update($this->id,$user,0,1,1,'add');
  382. // Ajout du commercial affecte
  383. if ($this->commercial_id != '' && $this->commercial_id != -1)
  384. {
  385. $this->add_commercial($user, $this->commercial_id);
  386. }
  387. // si un commercial cree un client il lui est affecte automatiquement
  388. else if (!$user->rights->societe->client->voir)
  389. {
  390. $this->add_commercial($user, $user->id);
  391. }
  392. if ($ret >= 0)
  393. {
  394. // Call trigger
  395. $result=$this->call_trigger('COMPANY_CREATE',$user);
  396. if ($result < 0) $error++;
  397. // End call triggers
  398. }
  399. else $error++;
  400. if (! $error)
  401. {
  402. dol_syslog(get_class($this)."::Create success id=".$this->id);
  403. $this->db->commit();
  404. return $this->id;
  405. }
  406. else
  407. {
  408. dol_syslog(get_class($this)."::Create echec update ".$this->error, LOG_ERR);
  409. $this->db->rollback();
  410. return -3;
  411. }
  412. }
  413. else
  414. {
  415. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  416. {
  417. $this->error=$langs->trans("ErrorCompanyNameAlreadyExists",$this->name);
  418. $result=-1;
  419. }
  420. else
  421. {
  422. $this->error=$this->db->lasterror();
  423. $result=-2;
  424. }
  425. $this->db->rollback();
  426. return $result;
  427. }
  428. }
  429. else
  430. {
  431. $this->db->rollback();
  432. dol_syslog(get_class($this)."::Create fails verify ".join(',',$this->errors), LOG_WARNING);
  433. return -3;
  434. }
  435. }
  436. /**
  437. * Create a contact/address from thirdparty
  438. *
  439. * @param User $user Object user
  440. * @return int <0 if KO, >0 if OK
  441. */
  442. function create_individual(User $user)
  443. {
  444. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  445. $contact=new Contact($this->db);
  446. $contact->name = $this->name_bis;
  447. $contact->firstname = $this->firstname;
  448. $contact->civility_id = $this->civility_id;
  449. $contact->socid = $this->id; // fk_soc
  450. $contact->statut = 1;
  451. $contact->priv = 0;
  452. $contact->country_id = $this->country_id;
  453. $contact->state_id = $this->state_id;
  454. $contact->address = $this->address;
  455. $contact->email = $this->email;
  456. $contact->zip = $this->zip;
  457. $contact->town = $this->town;
  458. $contact->phone_pro = $this->phone;
  459. $result = $contact->create($user);
  460. if ($result < 0)
  461. {
  462. $this->error = $contact->error;
  463. $this->errors = $contact->errors;
  464. dol_syslog(get_class($this)."::create_individual ERROR:" . $this->error, LOG_ERR);
  465. }
  466. return $result;
  467. }
  468. /**
  469. * Check properties of third party are ok (like name, third party codes, ...)
  470. * Used before an add or update.
  471. *
  472. * @return int 0 if OK, <0 if KO
  473. */
  474. function verify()
  475. {
  476. $this->errors=array();
  477. $result = 0;
  478. $this->name = trim($this->name);
  479. $this->nom=$this->name; // For backward compatibility
  480. if (! $this->name)
  481. {
  482. $this->errors[] = 'ErrorBadThirdPartyName';
  483. $result = -2;
  484. }
  485. if ($this->client)
  486. {
  487. $rescode = $this->check_codeclient();
  488. if ($rescode <> 0)
  489. {
  490. if ($rescode == -1)
  491. {
  492. $this->errors[] = 'ErrorBadCustomerCodeSyntax';
  493. }
  494. if ($rescode == -2)
  495. {
  496. $this->errors[] = 'ErrorCustomerCodeRequired';
  497. }
  498. if ($rescode == -3)
  499. {
  500. $this->errors[] = 'ErrorCustomerCodeAlreadyUsed';
  501. }
  502. if ($rescode == -4)
  503. {
  504. $this->errors[] = 'ErrorPrefixRequired';
  505. }
  506. $result = -3;
  507. }
  508. }
  509. if ($this->fournisseur)
  510. {
  511. $rescode = $this->check_codefournisseur();
  512. if ($rescode <> 0)
  513. {
  514. if ($rescode == -1)
  515. {
  516. $this->errors[] = 'ErrorBadSupplierCodeSyntax';
  517. }
  518. if ($rescode == -2)
  519. {
  520. $this->errors[] = 'ErrorSupplierCodeRequired';
  521. }
  522. if ($rescode == -3)
  523. {
  524. $this->errors[] = 'ErrorSupplierCodeAlreadyUsed';
  525. }
  526. if ($rescode == -5)
  527. {
  528. $this->errors[] = 'ErrorprefixRequired';
  529. }
  530. $result = -3;
  531. }
  532. }
  533. return $result;
  534. }
  535. /**
  536. * Update parameters of third party
  537. *
  538. * @param int $id id societe
  539. * @param User $user Utilisateur qui demande la mise a jour
  540. * @param int $call_trigger 0=non, 1=oui
  541. * @param int $allowmodcodeclient Inclut modif code client et code compta
  542. * @param int $allowmodcodefournisseur Inclut modif code fournisseur et code compta fournisseur
  543. * @param string $action 'add' or 'update'
  544. * @param int $nosyncmember Do not synchronize info of linked member
  545. * @return int <0 if KO, >=0 if OK
  546. */
  547. function update($id, $user='', $call_trigger=1, $allowmodcodeclient=0, $allowmodcodefournisseur=0, $action='update', $nosyncmember=1)
  548. {
  549. global $langs,$conf,$hookmanager;
  550. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  551. $error=0;
  552. dol_syslog(get_class($this)."::Update id=".$id." call_trigger=".$call_trigger." allowmodcodeclient=".$allowmodcodeclient." allowmodcodefournisseur=".$allowmodcodefournisseur);
  553. $now=dol_now();
  554. // Clean parameters
  555. $this->id = $id;
  556. $this->name = $this->name?trim($this->name):trim($this->nom);
  557. $this->nom = $this->name; // For backward compatibility
  558. $this->ref_ext = trim($this->ref_ext);
  559. $this->address = $this->address?trim($this->address):trim($this->address);
  560. $this->zip = $this->zip?trim($this->zip):trim($this->zip);
  561. $this->town = $this->town?trim($this->town):trim($this->town);
  562. $this->state_id = trim($this->state_id);
  563. $this->country_id = ($this->country_id > 0)?$this->country_id:0;
  564. $this->phone = trim($this->phone);
  565. $this->phone = preg_replace("/\s/","",$this->phone);
  566. $this->phone = preg_replace("/\./","",$this->phone);
  567. $this->fax = trim($this->fax);
  568. $this->fax = preg_replace("/\s/","",$this->fax);
  569. $this->fax = preg_replace("/\./","",$this->fax);
  570. $this->email = trim($this->email);
  571. $this->skype = trim($this->skype);
  572. $this->url = $this->url?clean_url($this->url,0):'';
  573. $this->idprof1 = trim($this->idprof1);
  574. $this->idprof2 = trim($this->idprof2);
  575. $this->idprof3 = trim($this->idprof3);
  576. $this->idprof4 = trim($this->idprof4);
  577. $this->idprof5 = (! empty($this->idprof5)?trim($this->idprof5):'');
  578. $this->idprof6 = (! empty($this->idprof6)?trim($this->idprof6):'');
  579. $this->prefix_comm = trim($this->prefix_comm);
  580. $this->tva_assuj = trim($this->tva_assuj);
  581. $this->tva_intra = dol_sanitizeFileName($this->tva_intra,'');
  582. if (empty($this->status)) $this->status = 0;
  583. // Local taxes
  584. $this->localtax1_assuj=trim($this->localtax1_assuj);
  585. $this->localtax2_assuj=trim($this->localtax2_assuj);
  586. $this->localtax1_value=trim($this->localtax1_value);
  587. $this->localtax2_value=trim($this->localtax2_value);
  588. $this->capital=price2num(trim($this->capital),'MT');
  589. if (empty($this->capital) || ! is_numeric($this->capital)) $this->capital = 0;
  590. $this->effectif_id=trim($this->effectif_id);
  591. $this->forme_juridique_code=trim($this->forme_juridique_code);
  592. //Gencod
  593. $this->barcode=trim($this->barcode);
  594. // For automatic creation
  595. if ($this->code_client == -1) $this->get_codeclient($this,0);
  596. if ($this->code_fournisseur == -1) $this->get_codefournisseur($this,1);
  597. $this->code_compta=trim($this->code_compta);
  598. $this->code_compta_fournisseur=trim($this->code_compta_fournisseur);
  599. // Check parameters
  600. if (! empty($conf->global->SOCIETE_MAIL_REQUIRED) && ! isValidEMail($this->email))
  601. {
  602. $langs->load("errors");
  603. $this->error = $langs->trans("ErrorBadEMail",$this->email);
  604. return -1;
  605. }
  606. if (! is_numeric($this->client) && ! is_numeric($this->fournisseur))
  607. {
  608. $langs->load("errors");
  609. $this->error = $langs->trans("BadValueForParameterClientOrSupplier");
  610. return -1;
  611. }
  612. $customer=false;
  613. if (! empty($allowmodcodeclient) && ! empty($this->client))
  614. {
  615. // Attention get_codecompta peut modifier le code suivant le module utilise
  616. if (empty($this->code_compta))
  617. {
  618. $ret=$this->get_codecompta('customer');
  619. if ($ret < 0) return -1;
  620. }
  621. $customer=true;
  622. }
  623. $supplier=false;
  624. if (! empty($allowmodcodefournisseur) && ! empty($this->fournisseur))
  625. {
  626. // Attention get_codecompta peut modifier le code suivant le module utilise
  627. if (empty($this->code_compta_fournisseur))
  628. {
  629. $ret=$this->get_codecompta('supplier');
  630. if ($ret < 0) return -1;
  631. }
  632. $supplier=true;
  633. }
  634. //Web services
  635. $this->webservices_url = $this->webservices_url?clean_url($this->webservices_url,0):'';
  636. $this->webservices_key = trim($this->webservices_key);
  637. $this->db->begin();
  638. // Check name is required and codes are ok or unique.
  639. // If error, this->errors[] is filled
  640. $result = 0;
  641. if ($action != 'add') $result = $this->verify(); // We don't check when update called during a create because verify was already done
  642. if ($result >= 0)
  643. {
  644. dol_syslog(get_class($this)."::update verify ok or not done");
  645. $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET ";
  646. $sql .= "nom = '" . $this->db->escape($this->name) ."'"; // Required
  647. $sql .= ",ref_ext = " .(! empty($this->ref_ext)?"'".$this->db->escape($this->ref_ext) ."'":"null");
  648. $sql .= ",address = '" . $this->db->escape($this->address) ."'";
  649. $sql .= ",zip = ".(! empty($this->zip)?"'".$this->zip."'":"null");
  650. $sql .= ",town = ".(! empty($this->town)?"'".$this->db->escape($this->town)."'":"null");
  651. $sql .= ",fk_departement = '" . (! empty($this->state_id)?$this->state_id:'0') ."'";
  652. $sql .= ",fk_pays = '" . (! empty($this->country_id)?$this->country_id:'0') ."'";
  653. $sql .= ",phone = ".(! empty($this->phone)?"'".$this->db->escape($this->phone)."'":"null");
  654. $sql .= ",fax = ".(! empty($this->fax)?"'".$this->db->escape($this->fax)."'":"null");
  655. $sql .= ",email = ".(! empty($this->email)?"'".$this->db->escape($this->email)."'":"null");
  656. $sql .= ",skype = ".(! empty($this->skype)?"'".$this->db->escape($this->skype)."'":"null");
  657. $sql .= ",url = ".(! empty($this->url)?"'".$this->db->escape($this->url)."'":"null");
  658. $sql .= ",siren = '". $this->db->escape($this->idprof1) ."'";
  659. $sql .= ",siret = '". $this->db->escape($this->idprof2) ."'";
  660. $sql .= ",ape = '". $this->db->escape($this->idprof3) ."'";
  661. $sql .= ",idprof4 = '". $this->db->escape($this->idprof4) ."'";
  662. $sql .= ",idprof5 = '". $this->db->escape($this->idprof5) ."'";
  663. $sql .= ",idprof6 = '". $this->db->escape($this->idprof6) ."'";
  664. $sql .= ",tva_assuj = ".($this->tva_assuj!=''?"'".$this->tva_assuj."'":"null");
  665. $sql .= ",tva_intra = '" . $this->db->escape($this->tva_intra) ."'";
  666. $sql .= ",status = " .$this->status;
  667. // Local taxes
  668. $sql .= ",localtax1_assuj = ".($this->localtax1_assuj!=''?"'".$this->localtax1_assuj."'":"null");
  669. $sql .= ",localtax2_assuj = ".($this->localtax2_assuj!=''?"'".$this->localtax2_assuj."'":"null");
  670. if($this->localtax1_assuj==1)
  671. {
  672. if($this->localtax1_value!='')
  673. {
  674. $sql .=",localtax1_value =".$this->localtax1_value;
  675. }
  676. else $sql .=",localtax1_value =0.000";
  677. }
  678. else $sql .=",localtax1_value =0.000";
  679. if($this->localtax2_assuj==1)
  680. {
  681. if($this->localtax2_value!='')
  682. {
  683. $sql .=",localtax2_value =".$this->localtax2_value;
  684. }
  685. else $sql .=",localtax2_value =0.000";
  686. }
  687. else $sql .=",localtax2_value =0.000";
  688. $sql .= ",capital = ".$this->capital;
  689. $sql .= ",prefix_comm = ".(! empty($this->prefix_comm)?"'".$this->db->escape($this->prefix_comm)."'":"null");
  690. $sql .= ",fk_effectif = ".(! empty($this->effectif_id)?"'".$this->effectif_id."'":"null");
  691. $sql .= ",fk_typent = ".(! empty($this->typent_id)?"'".$this->typent_id."'":"0");
  692. $sql .= ",fk_forme_juridique = ".(! empty($this->forme_juridique_code)?"'".$this->forme_juridique_code."'":"null");
  693. $sql .= ",client = " . (! empty($this->client)?$this->client:0);
  694. $sql .= ",fournisseur = " . (! empty($this->fournisseur)?$this->fournisseur:0);
  695. $sql .= ",barcode = ".(! empty($this->barcode)?"'".$this->barcode."'":"null");
  696. $sql .= ",default_lang = ".(! empty($this->default_lang)?"'".$this->default_lang."'":"null");
  697. $sql .= ",logo = ".(! empty($this->logo)?"'".$this->logo."'":"null");
  698. $sql .= ",webservices_url = ".(! empty($this->webservices_url)?"'".$this->db->escape($this->webservices_url)."'":"null");
  699. $sql .= ",webservices_key = ".(! empty($this->webservices_key)?"'".$this->db->escape($this->webservices_key)."'":"null");
  700. if ($customer)
  701. {
  702. $sql .= ", code_client = ".(! empty($this->code_client)?"'".$this->db->escape($this->code_client)."'":"null");
  703. $sql .= ", code_compta = ".(! empty($this->code_compta)?"'".$this->db->escape($this->code_compta)."'":"null");
  704. }
  705. if ($supplier)
  706. {
  707. $sql .= ", code_fournisseur = ".(! empty($this->code_fournisseur)?"'".$this->db->escape($this->code_fournisseur)."'":"null");
  708. $sql .= ", code_compta_fournisseur = ".(! empty($this->code_compta_fournisseur)?"'".$this->db->escape($this->code_compta_fournisseur)."'":"null");
  709. }
  710. $sql .= ", fk_user_modif = ".(! empty($user->id)?"'".$user->id."'":"null");
  711. $sql .= " WHERE rowid = '" . $id ."'";
  712. dol_syslog(get_class($this)."::Update", LOG_DEBUG);
  713. $resql=$this->db->query($sql);
  714. if ($resql)
  715. {
  716. unset($this->country_code); // We clean this because it may have been changed after an update of country_id
  717. unset($this->country);
  718. unset($this->state_code);
  719. unset($this->state);
  720. $nbrowsaffected = $this->db->affected_rows($resql);
  721. if (! $error && $nbrowsaffected)
  722. {
  723. // Update information on linked member if it is an update
  724. if (! $nosyncmember && ! empty($conf->adherent->enabled))
  725. {
  726. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  727. dol_syslog(get_class($this)."::update update linked member");
  728. $lmember=new Adherent($this->db);
  729. $result=$lmember->fetch(0, 0, $this->id);
  730. if ($result > 0)
  731. {
  732. $lmember->societe=$this->name;
  733. //$lmember->firstname=$this->firstname?$this->firstname:$lmember->firstname; // We keep firstname and lastname of member unchanged
  734. //$lmember->lastname=$this->lastname?$this->lastname:$lmember->lastname; // We keep firstname and lastname of member unchanged
  735. $lmember->address=$this->address;
  736. $lmember->email=$this->email;
  737. $lmember->skype=$this->skype;
  738. $lmember->phone=$this->phone;
  739. $result=$lmember->update($user,0,1,1,1); // Use nosync to 1 to avoid cyclic updates
  740. if ($result < 0)
  741. {
  742. $this->error=$lmember->error;
  743. dol_syslog(get_class($this)."::update ".$this->error,LOG_ERR);
  744. $error++;
  745. }
  746. }
  747. else if ($result < 0)
  748. {
  749. $this->error=$lmember->error;
  750. $error++;
  751. }
  752. }
  753. }
  754. $action='update';
  755. // Actions on extra fields (by external module or standard code)
  756. // FIXME le hook fait double emploi avec le trigger !!
  757. $hookmanager->initHooks(array('thirdpartydao'));
  758. $parameters=array('socid'=>$this->id);
  759. $reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  760. if (empty($reshook))
  761. {
  762. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
  763. {
  764. $result=$this->insertExtraFields();
  765. if ($result < 0)
  766. {
  767. $error++;
  768. }
  769. }
  770. }
  771. else if ($reshook < 0) $error++;
  772. if (! $error && $call_trigger)
  773. {
  774. // Call trigger
  775. $result=$this->call_trigger('COMPANY_MODIFY',$user);
  776. if ($result < 0) $error++;
  777. // End call triggers
  778. }
  779. if (! $error)
  780. {
  781. dol_syslog(get_class($this)."::Update success");
  782. $this->db->commit();
  783. return 1;
  784. }
  785. else
  786. {
  787. $this->db->rollback();
  788. return -1;
  789. }
  790. }
  791. else
  792. {
  793. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  794. {
  795. // Doublon
  796. $this->error = $langs->trans("ErrorDuplicateField");
  797. $result = -1;
  798. }
  799. else
  800. {
  801. $result = -2;
  802. }
  803. $this->db->rollback();
  804. return $result;
  805. }
  806. }
  807. else
  808. {
  809. $this->db->rollback();
  810. dol_syslog(get_class($this)."::Update fails verify ".join(',',$this->errors), LOG_WARNING);
  811. return -3;
  812. }
  813. }
  814. /**
  815. * Load a third party from database into memory
  816. *
  817. * @param int $rowid Id of third party to load
  818. * @param string $ref Reference of third party, name (Warning, this can return several records)
  819. * @param string $ref_ext External reference of third party (Warning, this information is a free field not provided by Dolibarr)
  820. * @param string $ref_int Internal reference of third party
  821. * @param string $idprof1 Prof id 1 of third party (Warning, this can return several records)
  822. * @param string $idprof2 Prof id 2 of third party (Warning, this can return several records)
  823. * @param string $idprof3 Prof id 3 of third party (Warning, this can return several records)
  824. * @param string $idprof4 Prof id 4 of third party (Warning, this can return several records)
  825. * @return int >0 if OK, <0 if KO or if two records found for same ref or idprof, 0 if not found.
  826. */
  827. function fetch($rowid, $ref='', $ref_ext='', $ref_int='', $idprof1='',$idprof2='',$idprof3='',$idprof4='')
  828. {
  829. global $langs;
  830. global $conf;
  831. if (empty($rowid) && empty($ref) && empty($ref_ext) && empty($ref_int)) return -1;
  832. $sql = 'SELECT s.rowid, s.nom as name, s.entity, s.ref_ext, s.ref_int, s.address, s.datec as date_creation, s.prefix_comm';
  833. $sql .= ', s.status';
  834. $sql .= ', s.price_level';
  835. $sql .= ', s.tms as date_modification';
  836. $sql .= ', s.phone, s.fax, s.email, s.skype, s.url, s.zip, s.town, s.note_private, s.note_public, s.client, s.fournisseur';
  837. $sql .= ', s.siren as idprof1, s.siret as idprof2, s.ape as idprof3, s.idprof4, s.idprof5, s.idprof6';
  838. $sql .= ', s.capital, s.tva_intra';
  839. $sql .= ', s.fk_typent as typent_id';
  840. $sql .= ', s.fk_effectif as effectif_id';
  841. $sql .= ', s.fk_forme_juridique as forme_juridique_code';
  842. $sql .= ', s.webservices_url, s.webservices_key';
  843. $sql .= ', s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur, s.parent, s.barcode';
  844. $sql .= ', s.fk_departement, s.fk_pays as country_id, s.fk_stcomm, s.remise_client, s.mode_reglement, s.cond_reglement, s.tva_assuj';
  845. $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';
  846. $sql .= ', s.outstanding_limit, s.import_key, s.canvas';
  847. $sql .= ', fj.libelle as forme_juridique';
  848. $sql .= ', e.libelle as effectif';
  849. $sql .= ', c.code as country_code, c.label as country';
  850. $sql .= ', d.code_departement as state_code, d.nom as state';
  851. $sql .= ', st.libelle as stcomm';
  852. $sql .= ', te.code as typent_code';
  853. $sql .= ' FROM '.MAIN_DB_PREFIX.'societe as s';
  854. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_effectif as e ON s.fk_effectif = e.id';
  855. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid';
  856. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcomm as st ON s.fk_stcomm = st.id';
  857. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_forme_juridique as fj ON s.fk_forme_juridique = fj.code';
  858. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON s.fk_departement = d.rowid';
  859. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_typent as te ON s.fk_typent = te.id';
  860. if ($rowid) $sql .= ' WHERE s.rowid = '.$rowid;
  861. if ($ref) $sql .= " WHERE s.nom = '".$this->db->escape($ref)."' AND s.entity IN (".getEntity($this->element, 1).")";
  862. if ($ref_ext) $sql .= " WHERE s.ref_ext = '".$this->db->escape($ref_ext)."' AND s.entity IN (".getEntity($this->element, 1).")";
  863. if ($ref_int) $sql .= " WHERE s.ref_int = '".$this->db->escape($ref_int)."' AND s.entity IN (".getEntity($this->element, 1).")";
  864. if ($idprof1) $sql .= " WHERE s.siren = '".$this->db->escape($idprof1)."' AND s.entity IN (".getEntity($this->element, 1).")";
  865. if ($idprof2) $sql .= " WHERE s.siret = '".$this->db->escape($idprof2)."' AND s.entity IN (".getEntity($this->element, 1).")";
  866. if ($idprof3) $sql .= " WHERE s.ape = '".$this->db->escape($idprof3)."' AND s.entity IN (".getEntity($this->element, 1).")";
  867. if ($idprof4) $sql .= " WHERE s.idprof4 = '".$this->db->escape($idprof4)."' AND s.entity IN (".getEntity($this->element, 1).")";
  868. $resql=$this->db->query($sql);
  869. dol_syslog(get_class($this)."::fetch ".$sql);
  870. if ($resql)
  871. {
  872. $num=$this->db->num_rows($resql);
  873. if ($num > 1)
  874. {
  875. $this->error='Fetch several records found for ref='.$ref;
  876. dol_syslog($this->error, LOG_ERR);
  877. $result = -2;
  878. }
  879. if ($num)
  880. {
  881. $obj = $this->db->fetch_object($resql);
  882. $this->id = $obj->rowid;
  883. $this->entity = $obj->entity;
  884. $this->canvas = $obj->canvas;
  885. $this->ref = $obj->rowid;
  886. $this->name = $obj->name;
  887. $this->nom = $obj->name; // deprecated
  888. $this->ref_ext = $obj->ref_ext;
  889. $this->ref_int = $obj->ref_int;
  890. $this->date_creation = $this->db->jdate($obj->date_creation);
  891. $this->date_modification = $this->db->jdate($obj->date_modification);
  892. $this->address = $obj->address;
  893. $this->zip = $obj->zip;
  894. $this->town = $obj->town;
  895. $this->country_id = $obj->country_id;
  896. $this->country_code = $obj->country_id?$obj->country_code:'';
  897. $this->country = $obj->country_id?($langs->trans('Country'.$obj->country_code)!='Country'.$obj->country_code?$langs->transnoentities('Country'.$obj->country_code):$obj->country):'';
  898. $this->state_id = $obj->fk_departement;
  899. $this->state_code = $obj->state_code;
  900. $this->state = ($obj->state!='-'?$obj->state:'');
  901. $transcode=$langs->trans('StatusProspect'.$obj->fk_stcomm);
  902. $libelle=($transcode!='StatusProspect'.$obj->fk_stcomm?$transcode:$obj->stcomm);
  903. $this->stcomm_id = $obj->fk_stcomm; // id statut commercial
  904. $this->statut_commercial = $libelle; // libelle statut commercial
  905. $this->email = $obj->email;
  906. $this->skype = $obj->skype;
  907. $this->url = $obj->url;
  908. $this->phone = $obj->phone;
  909. $this->fax = $obj->fax;
  910. $this->parent = $obj->parent;
  911. $this->idprof1 = $obj->idprof1;
  912. $this->idprof2 = $obj->idprof2;
  913. $this->idprof3 = $obj->idprof3;
  914. $this->idprof4 = $obj->idprof4;
  915. $this->idprof5 = $obj->idprof5;
  916. $this->idprof6 = $obj->idprof6;
  917. $this->capital = $obj->capital;
  918. $this->code_client = $obj->code_client;
  919. $this->code_fournisseur = $obj->code_fournisseur;
  920. $this->code_compta = $obj->code_compta;
  921. $this->code_compta_fournisseur = $obj->code_compta_fournisseur;
  922. $this->barcode = $obj->barcode;
  923. $this->tva_assuj = $obj->tva_assuj;
  924. $this->tva_intra = $obj->tva_intra;
  925. $this->status = $obj->status;
  926. // Local Taxes
  927. $this->localtax1_assuj = $obj->localtax1_assuj;
  928. $this->localtax2_assuj = $obj->localtax2_assuj;
  929. $this->localtax1_value = $obj->localtax1_value;
  930. $this->localtax2_value = $obj->localtax2_value;
  931. $this->typent_id = $obj->typent_id;
  932. $this->typent_code = $obj->typent_code;
  933. $this->effectif_id = $obj->effectif_id;
  934. $this->effectif = $obj->effectif_id?$obj->effectif:'';
  935. $this->forme_juridique_code= $obj->forme_juridique_code;
  936. $this->forme_juridique = $obj->forme_juridique_code?$obj->forme_juridique:'';
  937. $this->fk_prospectlevel = $obj->fk_prospectlevel;
  938. $this->prefix_comm = $obj->prefix_comm;
  939. $this->remise_percent = $obj->remise_client;
  940. $this->mode_reglement_id = $obj->mode_reglement;
  941. $this->cond_reglement_id = $obj->cond_reglement;
  942. $this->mode_reglement_supplier_id = $obj->mode_reglement_supplier;
  943. $this->cond_reglement_supplier_id = $obj->cond_reglement_supplier;
  944. $this->client = $obj->client;
  945. $this->fournisseur = $obj->fournisseur;
  946. $this->note = $obj->note_private; // TODO Deprecated for backward comtability
  947. $this->note_private = $obj->note_private;
  948. $this->note_public = $obj->note_public;
  949. $this->default_lang = $obj->default_lang;
  950. $this->logo = $obj->logo;
  951. $this->webservices_url = $obj->webservices_url;
  952. $this->webservices_key = $obj->webservices_key;
  953. $this->outstanding_limit = $obj->outstanding_limit;
  954. // multiprix
  955. $this->price_level = $obj->price_level;
  956. $this->import_key = $obj->import_key;
  957. $result = 1;
  958. // Retreive all extrafield for thirdparty
  959. // fetch optionals attributes and labels
  960. require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
  961. $extrafields=new ExtraFields($this->db);
  962. $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
  963. $this->fetch_optionals($this->id,$extralabels);
  964. }
  965. else
  966. {
  967. $result = 0;
  968. }
  969. $this->db->free($resql);
  970. }
  971. else
  972. {
  973. $this->error=$this->db->lasterror();
  974. $result = -3;
  975. }
  976. // Use first price level if level not defined for third party
  977. if (! empty($conf->global->PRODUIT_MULTIPRICES) && empty($this->price_level)) $this->price_level=1;
  978. return $result;
  979. }
  980. /**
  981. * Search and fetch thirparties by name
  982. *
  983. * @param string $name Name
  984. * @param int $type Type of thirdparties (0=any, 1=customer, 2=prospect, 3=supplier)
  985. * @param array $filters Array of couple field name/value to filter the companies with the same name
  986. * @param boolean $exact Exact string search (true/false)
  987. * @param boolean $case Case sensitive (true/false)
  988. * @param boolean $similar Add test if string inside name into database, or name into database inside string. Do not use this: Not compatible with other database.
  989. * @param string $clause Clause for filters
  990. * @return array|int <0 if KO, array of thirdparties object if OK
  991. */
  992. function searchByName($name, $type='0', $filters = array(), $exact = false, $case = false, $similar = false, $clause = 'AND')
  993. {
  994. $thirdparties = array();
  995. dol_syslog("searchByName name=".$name." type=".$type." exact=".$exact);
  996. // Check parameter
  997. if (empty($name))
  998. {
  999. $this->errors[]='ErrorBadValueForParameter';
  1000. return -1;
  1001. }
  1002. // Generation requete recherche
  1003. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe";
  1004. $sql.= " WHERE entity IN (".getEntity('category',1).")";
  1005. if (! empty($type))
  1006. {
  1007. if ($type == 1 || $type == 2)
  1008. $sql.= " AND client = ".$type;
  1009. elseif ($type == 3)
  1010. $sql.= " AND fournisseur = 1";
  1011. }
  1012. if (! empty($name))
  1013. {
  1014. if (! $exact)
  1015. {
  1016. if (preg_match('/^([\*])?[^*]+([\*])?$/', $name, $regs) && count($regs) > 1)
  1017. {
  1018. $name = str_replace('*', '%', $name);
  1019. }
  1020. else
  1021. {
  1022. $name = '%'.$name.'%';
  1023. }
  1024. }
  1025. $sql.= " AND ";
  1026. if (is_array($filters) && ! empty($filters))
  1027. $sql.= "(";
  1028. if ($similar)
  1029. {
  1030. // For test similitude (string inside name into database, or name into database inside string)
  1031. // Do not use this. Not compatible with other database.
  1032. $sql.= "(LOCATE('".$this->db->escape($name)."', nom) > 0 OR LOCATE(nom, '".$this->db->escape($name)."') > 0)";
  1033. }
  1034. else
  1035. {
  1036. if (! $case)
  1037. $sql.= "nom LIKE '".$this->db->escape($name)."'";
  1038. else
  1039. $sql.= "nom LIKE BINARY '".$this->db->escape($name)."'";
  1040. }
  1041. }
  1042. if (is_array($filters) && ! empty($filters))
  1043. {
  1044. foreach($filters as $field => $value)
  1045. {
  1046. if (! $exact)
  1047. {
  1048. if (preg_match('/^([\*])?[^*]+([\*])?$/', $value, $regs) && count($regs) > 1)
  1049. {
  1050. $value = str_replace('*', '%', $value);
  1051. }
  1052. else
  1053. {
  1054. $value = '%'.$value.'%';
  1055. }
  1056. }
  1057. if (! $case)
  1058. $sql.= " ".$clause." ".$field." LIKE '".$this->db->escape($value)."'";
  1059. else
  1060. $sql.= " ".$clause." ".$field." LIKE BINARY '".$this->db->escape($value)."'";
  1061. }
  1062. if (! empty($name))
  1063. $sql.= ")";
  1064. }
  1065. $res = $this->db->query($sql);
  1066. if ($res)
  1067. {
  1068. while ($rec = $this->db->fetch_array($res))
  1069. {
  1070. $soc = new Societe($this->db);
  1071. $soc->fetch($rec['rowid']);
  1072. $thirdparties[] = $soc;
  1073. }
  1074. return $thirdparties;
  1075. }
  1076. else
  1077. {
  1078. $this->error=$this->db->lasterror();
  1079. return -1;
  1080. }
  1081. }
  1082. /**
  1083. * Delete a third party from database and all its dependencies (contacts, rib...)
  1084. *
  1085. * @param int $id Id of third party to delete
  1086. * @return int <0 if KO, 0 if nothing done, >0 if OK
  1087. */
  1088. function delete($id)
  1089. {
  1090. global $user, $langs, $conf;
  1091. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1092. $entity=isset($this->entity)?$this->entity:$conf->entity;
  1093. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  1094. $error = 0;
  1095. // Test if child exists
  1096. $objectisused = $this->isObjectUsed($id);
  1097. if (empty($objectisused))
  1098. {
  1099. $this->db->begin();
  1100. // Call trigger
  1101. $result=$this->call_trigger('COMPANY_DELETE',$user);
  1102. if ($result < 0) $error++;
  1103. // End call triggers
  1104. if (! $error)
  1105. {
  1106. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  1107. $static_cat = new Categorie($this->db);
  1108. $toute_categs = array();
  1109. // Fill $toute_categs array with an array of (type => array of ("Categorie" instance))
  1110. if ($this->client || $this->prospect)
  1111. {
  1112. $toute_categs ['societe'] = $static_cat->containing($this->id,2);
  1113. }
  1114. if ($this->fournisseur)
  1115. {
  1116. $toute_categs ['fournisseur'] = $static_cat->containing($this->id,1);
  1117. }
  1118. // Remove each "Categorie"
  1119. foreach ($toute_categs as $type => $categs_type)
  1120. {
  1121. foreach ($categs_type as $cat)
  1122. {
  1123. $cat->del_type($this, $type);
  1124. }
  1125. }
  1126. }
  1127. // Remove contacts
  1128. if (! $error)
  1129. {
  1130. $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
  1131. $sql.= " WHERE fk_soc = " . $id;
  1132. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  1133. if (! $this->db->query($sql))
  1134. {
  1135. $error++;
  1136. $this->error .= $this->db->lasterror();
  1137. }
  1138. }
  1139. // Update link in member table
  1140. if (! $error)
  1141. {
  1142. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent";
  1143. $sql.= " SET fk_soc = NULL WHERE fk_soc = " . $id;
  1144. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  1145. if (! $this->db->query($sql))
  1146. {
  1147. $error++;
  1148. $this->error .= $this->db->lasterror();
  1149. dol_syslog(get_class($this)."::delete erreur -1 ".$this->error, LOG_ERR);
  1150. }
  1151. }
  1152. // Remove ban
  1153. if (! $error)
  1154. {
  1155. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_rib";
  1156. $sql.= " WHERE fk_soc = " . $id;
  1157. dol_syslog(get_class($this)."::Delete", LOG_DEBUG);
  1158. if (! $this->db->query($sql))
  1159. {
  1160. $error++;
  1161. $this->error = $this->db->lasterror();
  1162. }
  1163. }
  1164. // Remove associated users
  1165. if (! $error)
  1166. {
  1167. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_commerciaux";
  1168. $sql.= " WHERE fk_soc = " . $id;
  1169. dol_syslog(get_class($this)."::Delete", LOG_DEBUG);
  1170. if (! $this->db->query($sql))
  1171. {
  1172. $error++;
  1173. $this->error = $this->db->lasterror();
  1174. }
  1175. }
  1176. // Removed extrafields
  1177. if ((! $error) && (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) // For avoid conflicts if trigger used
  1178. {
  1179. $result=$this->deleteExtraFields();
  1180. if ($result < 0)
  1181. {
  1182. $error++;
  1183. dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
  1184. }
  1185. }
  1186. // Remove third party
  1187. if (! $error)
  1188. {
  1189. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe";
  1190. $sql.= " WHERE rowid = " . $id;
  1191. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  1192. if (! $this->db->query($sql))
  1193. {
  1194. $error++;
  1195. $this->error = $this->db->lasterror();
  1196. }
  1197. }
  1198. if (! $error)
  1199. {
  1200. $this->db->commit();
  1201. // Delete directory
  1202. if (! empty($conf->societe->multidir_output[$entity]))
  1203. {
  1204. $docdir = $conf->societe->multidir_output[$entity] . "/" . $id;
  1205. if (dol_is_dir($docdir))
  1206. {
  1207. dol_delete_dir_recursive($docdir);
  1208. }
  1209. }
  1210. return 1;
  1211. }
  1212. else
  1213. {
  1214. $this->db->rollback();
  1215. return -1;
  1216. }
  1217. }
  1218. else dol_syslog("Can't remove thirdparty with id ".$id.". There is ".$objectisused." childs", LOG_WARNING);
  1219. return 0;
  1220. }
  1221. /**
  1222. * Define third party as a customer
  1223. *
  1224. * @return int <0 if KO, >0 if OK
  1225. */
  1226. function set_as_client()
  1227. {
  1228. if ($this->id)
  1229. {
  1230. $newclient=1;
  1231. if ($this->client == 2 || $this->client == 3) $newclient=3; //If prospect, we keep prospect tag
  1232. $sql = "UPDATE ".MAIN_DB_PREFIX."societe";
  1233. $sql.= " SET client = ".$newclient;
  1234. $sql.= " WHERE rowid = " . $this->id;
  1235. $resql=$this->db->query($sql);
  1236. if ($resql)
  1237. {
  1238. $this->client = $newclient;
  1239. return 1;
  1240. }
  1241. else return -1;
  1242. }
  1243. return 0;
  1244. }
  1245. /**
  1246. * Definit la societe comme un client
  1247. *
  1248. * @param float $remise Valeur en % de la remise
  1249. * @param string $note Note/Motif de modification de la remise
  1250. * @param User $user Utilisateur qui definie la remise
  1251. * @return int <0 if KO, >0 if OK
  1252. */
  1253. function set_remise_client($remise, $note, User $user)
  1254. {
  1255. global $langs;
  1256. // Nettoyage parametres
  1257. $note=trim($note);
  1258. if (! $note)
  1259. {
  1260. $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Note"));
  1261. return -2;
  1262. }
  1263. dol_syslog(get_class($this)."::set_remise_client ".$remise.", ".$note.", ".$user->id);
  1264. if ($this->id)
  1265. {
  1266. $this->db->begin();
  1267. $now=dol_now();
  1268. // Positionne remise courante
  1269. $sql = "UPDATE ".MAIN_DB_PREFIX."societe ";
  1270. $sql.= " SET remise_client = '".$remise."'";
  1271. $sql.= " WHERE rowid = " . $this->id .";";
  1272. $resql=$this->db->query($sql);
  1273. if (! $resql)
  1274. {
  1275. $this->db->rollback();
  1276. $this->error=$this->db->error();
  1277. return -1;
  1278. }
  1279. // Ecrit trace dans historique des remises
  1280. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise";
  1281. $sql.= " (datec, fk_soc, remise_client, note, fk_user_author)";
  1282. $sql.= " VALUES ('".$this->db->idate($now)."', ".$this->id.", '".$remise."',";
  1283. $sql.= " '".$this->db->escape($note)."',";
  1284. $sql.= " ".$user->id;
  1285. $sql.= ")";
  1286. $resql=$this->db->query($sql);
  1287. if (! $resql)
  1288. {
  1289. $this->db->rollback();
  1290. $this->error=$this->db->lasterror();
  1291. return -1;
  1292. }
  1293. $this->db->commit();
  1294. return 1;
  1295. }
  1296. }
  1297. /**
  1298. * Add a discount for third party
  1299. *
  1300. * @param float $remise Amount of discount
  1301. * @param User $user User adding discount
  1302. * @param string $desc Reason of discount
  1303. * @param float $tva_tx VAT rate
  1304. * @return int <0 if KO, id of discount record if OK
  1305. */
  1306. function set_remise_except($remise, User $user, $desc, $tva_tx=0)
  1307. {
  1308. global $langs;
  1309. // Clean parameters
  1310. $remise = price2num($remise);
  1311. $desc = trim($desc);
  1312. // Check parameters
  1313. if (! $remise > 0)
  1314. {
  1315. $this->error=$langs->trans("ErrorWrongValueForParameter","1");
  1316. return -1;
  1317. }
  1318. if (! $desc)
  1319. {
  1320. $this->error=$langs->trans("ErrorWrongValueForParameter","3");
  1321. return -2;
  1322. }
  1323. if ($this->id)
  1324. {
  1325. require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
  1326. $discount = new DiscountAbsolute($this->db);
  1327. $discount->fk_soc=$this->id;
  1328. $discount->amount_ht=price2num($remise,'MT');
  1329. $discount->amount_tva=price2num($remise*$tva_tx/100,'MT');
  1330. $discount->amount_ttc=price2num($discount->amount_ht+$discount->amount_tva,'MT');
  1331. $discount->tva_tx=price2num($tva_tx,'MT');
  1332. $discount->description=$desc;
  1333. $result=$discount->create($user);
  1334. if ($result > 0)
  1335. {
  1336. return $result;
  1337. }
  1338. else
  1339. {
  1340. $this->error=$discount->error;
  1341. return -3;
  1342. }
  1343. }
  1344. else return 0;
  1345. }
  1346. /**
  1347. * Renvoie montant TTC des reductions/avoirs en cours disponibles de la societe
  1348. *
  1349. * @param User $user Filtre sur un user auteur des remises
  1350. * @param string $filter Filtre autre
  1351. * @param string $maxvalue Filter on max value for discount
  1352. * @return int <0 if KO, Credit note amount otherwise
  1353. */
  1354. function getAvailableDiscounts($user='',$filter='',$maxvalue=0)
  1355. {
  1356. require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
  1357. $discountstatic=new DiscountAbsolute($this->db);
  1358. $result=$discountstatic->getAvailableDiscounts($this,$user,$filter,$maxvalue);
  1359. if ($result >= 0)
  1360. {
  1361. return $result;
  1362. }
  1363. else
  1364. {
  1365. $this->error=$discountstatic->error;
  1366. return -1;
  1367. }
  1368. }
  1369. /**
  1370. * Return array of sales representatives
  1371. *
  1372. * @param User $user Object user
  1373. * @return array Array of sales representatives of third party
  1374. */
  1375. function getSalesRepresentatives(User $user)
  1376. {
  1377. global $conf;
  1378. $reparray=array();
  1379. $sql = "SELECT u.rowid, u.lastname, u.firstname";
  1380. $sql.= " FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc, ".MAIN_DB_PREFIX."user as u";
  1381. $sql.= " WHERE u.rowid = sc.fk_user AND sc.fk_soc =".$this->id;
  1382. $sql.= " AND entity in (0, ".$conf->entity.")";
  1383. $resql = $this->db->query($sql);
  1384. if ($resql)
  1385. {
  1386. $num = $this->db->num_rows($resql);
  1387. $i=0;
  1388. while ($i < $num)
  1389. {
  1390. $obj = $this->db->fetch_object($resql);
  1391. $reparray[$i]['id']=$obj->rowid;
  1392. $reparray[$i]['lastname']=$obj->lastname;
  1393. $reparray[$i]['firstname']=$obj->firstname;
  1394. $i++;
  1395. }
  1396. return $reparray;
  1397. }
  1398. else {
  1399. dol_print_error($this->db);
  1400. return -1;
  1401. }
  1402. }
  1403. /**
  1404. * Set the price level
  1405. *
  1406. * @param int $price_level Level of price
  1407. * @param User $user Use making change
  1408. * @return int <0 if KO, >0 if OK
  1409. */
  1410. function set_price_level($price_level, User $user)
  1411. {
  1412. if ($this->id)
  1413. {
  1414. $now=dol_now();
  1415. $sql = "UPDATE ".MAIN_DB_PREFIX."societe";
  1416. $sql .= " SET price_level = '".$price_level."'";
  1417. $sql .= " WHERE rowid = " . $this->id;
  1418. if (! $this->db->query($sql))
  1419. {
  1420. dol_print_error($this->db);
  1421. return -1;
  1422. }
  1423. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_prices";
  1424. $sql .= " (datec, fk_soc, price_level, fk_user_author)";
  1425. $sql .= " VALUES ('".$this->db->idate($now)."',".$this->id.",'".$price_level."',".$user->id.")";
  1426. if (! $this->db->query($sql))
  1427. {
  1428. dol_print_error($this->db);
  1429. return -1;
  1430. }
  1431. return 1;
  1432. }
  1433. return -1;
  1434. }
  1435. /**
  1436. * Add link to sales representative
  1437. *
  1438. * @param User $user Object user
  1439. * @param int $commid Id of user
  1440. * @return void
  1441. */
  1442. function add_commercial(User $user, $commid)
  1443. {
  1444. if ($this->id > 0 && $commid > 0)
  1445. {
  1446. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_commerciaux";
  1447. $sql.= " WHERE fk_soc = ".$this->id." AND fk_user =".$commid;
  1448. $this->db->query($sql);
  1449. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_commerciaux";
  1450. $sql.= " ( fk_soc, fk_user )";
  1451. $sql.= " VALUES (".$this->id.",".$commid.")";
  1452. if (! $this->db->query($sql) )
  1453. {
  1454. dol_syslog(get_class($this)."::add_commercial Erreur");
  1455. }
  1456. }
  1457. }
  1458. /**
  1459. * Add link to sales representative
  1460. *
  1461. * @param User $user Object user
  1462. * @param int $commid Id of user
  1463. * @return void
  1464. */
  1465. function del_commercial(User $user, $commid)
  1466. {
  1467. if ($this->id > 0 && $commid > 0)
  1468. {
  1469. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_commerciaux ";
  1470. $sql .= " WHERE fk_soc = ".$this->id." AND fk_user =".$commid;
  1471. if (! $this->db->query($sql) )
  1472. {
  1473. dol_syslog(get_class($this)."::del_commercial Erreur");
  1474. }
  1475. }
  1476. }
  1477. /**
  1478. * Return a link on thirdparty (with picto)
  1479. *
  1480. * @param int $withpicto Add picto into link (0=No picto, 1=Include picto with link, 2=Picto only)
  1481. * @param string $option Target of link ('', 'customer', 'prospect', 'supplier')
  1482. * @param int $maxlen Max length of text
  1483. * @return string String with URL
  1484. */
  1485. function getNomUrl($withpicto=0,$option='',$maxlen=0)
  1486. {
  1487. global $conf,$langs;
  1488. $name=$this->name?$this->name:$this->nom;
  1489. if ($conf->global->SOCIETE_ADD_REF_IN_LIST && (!empty($withpicto))) {
  1490. if (($this->client) && (! empty ( $this->code_client ))) {
  1491. $code = $this->code_client . ' - ';
  1492. }
  1493. if (($this->fournisseur) && (! empty ( $this->code_fournisseur ))) {
  1494. $code .= $this->code_fournisseur . ' - ';
  1495. }
  1496. $name =$code.' '.$name;
  1497. }
  1498. $result='';
  1499. $lien=$lienfin='';
  1500. if ($option == 'customer' || $option == 'compta')
  1501. {
  1502. $lien = '<a href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$this->id;
  1503. }
  1504. else if ($option == 'prospect' && empty($conf->global->SOCIETE_DISABLE_PROSPECTS))
  1505. {
  1506. $lien = '<a href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$this->id;
  1507. }
  1508. else if ($option == 'supplier')
  1509. {
  1510. $lien = '<a href="'.DOL_URL_ROOT.'/fourn/card.php?socid='.$this->id;
  1511. }
  1512. else if ($option == 'category')
  1513. {
  1514. $lien = '<a href="'.DOL_URL_ROOT.'/categories/categorie.php?id='.$this->id.'&type=2';
  1515. }
  1516. else if ($option == 'category_supplier')
  1517. {
  1518. $lien = '<a href="'.DOL_URL_ROOT.'/categories/categorie.php?id='.$this->id.'&type=1';
  1519. }
  1520. // By default
  1521. if (empty($lien))
  1522. {
  1523. $lien = '<a href="'.DOL_URL_ROOT.'/societe/soc.php?socid='.$this->id;
  1524. }
  1525. // Add type of canvas
  1526. $lien.=(!empty($this->canvas)?'&canvas='.$this->canvas:'').'">';
  1527. $lienfin='</a>';
  1528. if ($withpicto) $result.=($lien.img_object($langs->trans("ShowCompany").': '.$name,'company').$lienfin);
  1529. if ($withpicto && $withpicto != 2) $result.=' ';
  1530. $result.=$lien.($maxlen?dol_trunc($name,$maxlen):$name).$lienfin;
  1531. return $result;
  1532. }
  1533. /**
  1534. * Return label of status (activity, closed)
  1535. *
  1536. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long
  1537. * @return string Libelle
  1538. */
  1539. function getLibStatut($mode=0)
  1540. {
  1541. return $this->LibStatut($this->status,$mode);
  1542. }
  1543. /**
  1544. * Renvoi le libelle d'un statut donne
  1545. *
  1546. * @param int $statut Id statut
  1547. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  1548. * @return string Libelle du statut
  1549. */
  1550. function LibStatut($statut,$mode=0)
  1551. {
  1552. global $langs;
  1553. $langs->load('companies');
  1554. if ($mode == 0)
  1555. {
  1556. if ($statut==0) return $langs->trans("ActivityCeased");
  1557. if ($statut==1) return $langs->trans("InActivity");
  1558. }
  1559. if ($mode == 1)
  1560. {
  1561. if ($statut==0) return $langs->trans("ActivityCeased");
  1562. if ($statut==1) return $langs->trans("InActivity");
  1563. }
  1564. if ($mode == 2)
  1565. {
  1566. if ($statut==0) return img_picto($langs->trans("ActivityCeased"),'statut5').' '.$langs->trans("ActivityCeased");
  1567. if ($statut==1) return img_picto($langs->trans("InActivity"),'statut4').' '.$langs->trans("InActivity");
  1568. }
  1569. if ($mode == 3)
  1570. {
  1571. if ($statut==0) return img_picto($langs->trans("ActivityCeased"),'statut5');
  1572. if ($statut==1) return img_picto($langs->trans("InActivity"),'statut4');
  1573. }
  1574. if ($mode == 4)
  1575. {
  1576. if ($statut==0) return img_picto($langs->trans("ActivityCeased"),'statut5').' '.$langs->trans("ActivityCeased");
  1577. if ($statut==1) return img_picto($langs->trans("InActivity"),'statut4').' '.$langs->trans("InActivity");
  1578. }
  1579. if ($mode == 5)
  1580. {
  1581. if ($statut==0) return $langs->trans("ActivityCeased").' '.img_picto($langs->trans("ActivityCeased"),'statut5');
  1582. if ($statut==1) return $langs->trans("InActivity").' '.img_picto($langs->trans("InActivity"),'statut4');
  1583. }
  1584. }
  1585. /**
  1586. * Return list of contacts emails existing for third party
  1587. *
  1588. * @param int $addthirdparty 1=Add also a record for thirdparty email
  1589. * @return array Array of contacts emails
  1590. */
  1591. function thirdparty_and_contact_email_array($addthirdparty=0)
  1592. {
  1593. global $langs;
  1594. $contact_emails = $this->contact_property_array('email');
  1595. if ($this->email && $addthirdparty)
  1596. {
  1597. if (empty($this->name)) $this->name=$this->nom;
  1598. // TODO: Tester si email non deja present dans tableau contact
  1599. $contact_emails['thirdparty']=$langs->trans("ThirdParty").': '.dol_trunc($this->name,16)." &lt;".$this->email."&gt;";
  1600. }
  1601. return $contact_emails;
  1602. }
  1603. /**
  1604. * Return list of contacts mobile phone existing for third party
  1605. *
  1606. * @return array Array of contacts emails
  1607. */
  1608. function thirdparty_and_contact_phone_array()
  1609. {
  1610. global $langs;
  1611. $contact_phone = $this->contact_property_array('mobile');
  1612. if (! empty($this->phone)) // If a phone of thirdparty is defined, we add it ot mobile of contacts
  1613. {
  1614. if (empty($this->name)) $this->name=$this->nom;
  1615. // TODO: Tester si tel non deja present dans tableau contact
  1616. $contact_phone['thirdparty']=$langs->trans("ThirdParty").': '.dol_trunc($this->name,16)." &lt;".$this->phone."&gt;";
  1617. }
  1618. return $contact_phone;
  1619. }
  1620. /**
  1621. * Return list of contacts emails or mobile existing for third party
  1622. *
  1623. * @param string $mode 'email' or 'mobile'
  1624. * @param int $hidedisabled 1=Hide contact if disabled
  1625. * @return array Array of contacts emails or mobile array(id=>'Name <email>')
  1626. */
  1627. function contact_property_array($mode='email', $hidedisabled=0)
  1628. {
  1629. global $langs;
  1630. $contact_property = array();
  1631. $sql = "SELECT rowid, email, statut, phone_mobile, lastname, poste, firstname";
  1632. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople";
  1633. $sql.= " WHERE fk_soc = '".$this->id."'";
  1634. $resql=$this->db->query($sql);
  1635. if ($resql)
  1636. {
  1637. $nump = $this->db->num_rows($resql);
  1638. if ($nump)
  1639. {
  1640. $sepa="("; $sepb=")";
  1641. if ($mode == 'email')
  1642. {
  1643. $sepa="&lt;"; $sepb="&gt;";
  1644. }
  1645. $i = 0;
  1646. while ($i < $nump)
  1647. {
  1648. $obj = $this->db->fetch_object($resql);
  1649. if ($mode == 'email') $property=$obj->email;
  1650. else if ($mode == 'mobile') $property=$obj->phone_mobile;
  1651. else $property=$obj->$mode;
  1652. // Show all contact. If hidedisabled is 1, showonly contacts with status = 1
  1653. if ($obj->statut == 1 || empty($hidedisabled))
  1654. {
  1655. if (empty($property))
  1656. {
  1657. if ($mode == 'email') $property=$langs->trans("NoEMail");
  1658. else if ($mode == 'mobile') $property=$langs->trans("NoMobilePhone");
  1659. }
  1660. if (!empty($obj->poste))
  1661. {
  1662. $contact_property[$obj->rowid] = trim(dolGetFirstLastname($obj->firstname,$obj->lastname)).($obj->poste?" - ".$obj->poste:"").(($mode != 'poste' && $property)?" ".$sepa.$property.$sepb:'');
  1663. }
  1664. else
  1665. {
  1666. $contact_property[$obj->rowid] = trim(dolGetFirstLastname($obj->firstname,$obj->lastname)).(($mode != 'poste' && $property)?" ".$sepa.$property.$sepb:'');
  1667. }
  1668. }
  1669. $i++;
  1670. }
  1671. }
  1672. }
  1673. else
  1674. {
  1675. dol_print_error($this->db);
  1676. }
  1677. return $contact_property;
  1678. }
  1679. /**
  1680. * Renvoie la liste des contacts de cette societe
  1681. *
  1682. * @return array tableau des contacts
  1683. */
  1684. function contact_array()
  1685. {
  1686. $contacts = array();
  1687. $sql = "SELECT rowid, lastname, firstname FROM ".MAIN_DB_PREFIX."socpeople WHERE fk_soc = '".$this->id."'";
  1688. $resql=$this->db->query($sql);
  1689. if ($resql)
  1690. {
  1691. $nump = $this->db->num_rows($resql);
  1692. if ($nump)
  1693. {
  1694. $i = 0;
  1695. while ($i < $nump)
  1696. {
  1697. $obj = $this->db->fetch_object($resql);
  1698. $contacts[$obj->rowid] = dolGetFirstLastname($obj->firstname,$obj->lastname);
  1699. $i++;
  1700. }
  1701. }
  1702. }
  1703. else
  1704. {
  1705. dol_print_error($this->db);
  1706. }
  1707. return $contacts;
  1708. }
  1709. /**
  1710. * Renvoie la liste des contacts de cette societe
  1711. *
  1712. * @return array $contacts tableau des contacts
  1713. */
  1714. function contact_array_objects()
  1715. {
  1716. require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
  1717. $contacts = array();
  1718. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."socpeople WHERE fk_soc = '".$this->id."'";
  1719. $resql=$this->db->query($sql);
  1720. if ($resql)
  1721. {
  1722. $nump = $this->db->num_rows($resql);
  1723. if ($nump)
  1724. {
  1725. $i = 0;
  1726. while ($i < $nump)
  1727. {
  1728. $obj = $this->db->fetch_object($resql);
  1729. $contact = new Contact($this->db);
  1730. $contact->fetch($obj->rowid);
  1731. $contacts[] = $contact;
  1732. $i++;
  1733. }
  1734. }
  1735. }
  1736. else
  1737. {
  1738. dol_print_error($this->db);
  1739. }
  1740. return $contacts;
  1741. }
  1742. /**
  1743. * Return property of contact from its id
  1744. *
  1745. * @param int $rowid id of contact
  1746. * @param string $mode 'email' or 'mobile'
  1747. * @return string email of contact
  1748. */
  1749. function contact_get_property($rowid,$mode)
  1750. {
  1751. $contact_property='';
  1752. if (empty($rowid)) return '';
  1753. $sql = "SELECT rowid, email, phone_mobile, lastname, firstname";
  1754. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople";
  1755. $sql.= " WHERE rowid = '".$rowid."'";
  1756. $resql=$this->db->query($sql);
  1757. if ($resql)
  1758. {
  1759. $nump = $this->db->num_rows($resql);
  1760. if ($nump)
  1761. {
  1762. $obj = $this->db->fetch_object($resql);
  1763. if ($mode == 'email') $contact_property = dolGetFirstLastname($obj->firstname, $obj->lastname)." <".$obj->email.">";
  1764. else if ($mode == 'mobile') $contact_property = $obj->phone_mobile;
  1765. }
  1766. return $contact_property;
  1767. }
  1768. else
  1769. {
  1770. dol_print_error($this->db);
  1771. }
  1772. }
  1773. /**
  1774. * Return bank number property of thirdparty
  1775. *
  1776. * @return string Bank number
  1777. */
  1778. function display_rib()
  1779. {
  1780. require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php';
  1781. $bac = new CompanyBankAccount($this->db);
  1782. $bac->fetch(0,$this->id);
  1783. return $bac->getRibLabel(true);
  1784. }
  1785. /**
  1786. * Return Array of RIB
  1787. *
  1788. * @return array|int 0 if KO, Array of CompanyBanckAccount if OK
  1789. */
  1790. function get_all_rib()
  1791. {
  1792. require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php';
  1793. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_rib WHERE fk_soc = ".$this->id;
  1794. $result = $this->db->query($sql);
  1795. if (!$result) {
  1796. $this->error++;
  1797. $this->errors[] = $this->db->lasterror;
  1798. return 0;
  1799. } else {
  1800. $num_rows = $this->db->num_rows($result);
  1801. $rib_array = array();
  1802. if ($num_rows) {
  1803. while ($obj = $this->db->fetch_object($result)) {
  1804. $rib = new CompanyBankAccount($this->db);
  1805. $rib->fetch($obj->rowid);
  1806. $rib_array[] = $rib;
  1807. }
  1808. }
  1809. return $rib_array;
  1810. }
  1811. }
  1812. /**
  1813. * Attribut un code client a partir du module de controle des codes.
  1814. * Return value is stored into this->code_client
  1815. *
  1816. * @param Societe $objsoc Object thirdparty
  1817. * @param int $type Should be 0 to say customer
  1818. * @return void
  1819. */
  1820. function get_codeclient($objsoc=0,$type=0)
  1821. {
  1822. global $conf;
  1823. if (! empty($conf->global->SOCIETE_CODECLIENT_ADDON))
  1824. {
  1825. $module=$conf->global->SOCIETE_CODECLIENT_ADDON;
  1826. $dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
  1827. foreach ($dirsociete as $dirroot)
  1828. {
  1829. $res=dol_include_once($dirroot.$module.'.php');
  1830. if ($res) break;
  1831. }
  1832. $mod = new $module();
  1833. $this->code_client = $mod->getNextValue($objsoc,$type);
  1834. $this->prefixCustomerIsRequired = $mod->prefixIsRequired;
  1835. dol_syslog(get_class($this)."::get_codeclient code_client=".$this->code_client." module=".$module);
  1836. }
  1837. }
  1838. /**
  1839. * Attribut un code fournisseur a partir du module de controle des codes.
  1840. * Return value is stored into this->code_fournisseur
  1841. *
  1842. * @param Societe $objsoc Object thirdparty
  1843. * @param int $type Should be 1 to say supplier
  1844. * @return void
  1845. */
  1846. function get_codefournisseur($objsoc=0,$type=1)
  1847. {
  1848. global $conf;
  1849. if (! empty($conf->global->SOCIETE_CODECLIENT_ADDON))
  1850. {
  1851. $module=$conf->global->SOCIETE_CODECLIENT_ADDON;
  1852. $dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
  1853. foreach ($dirsociete as $dirroot)
  1854. {
  1855. $res=dol_include_once($dirroot.$module.'.php');
  1856. if ($res) break;
  1857. }
  1858. $mod = new $module();
  1859. $this->code_fournisseur = $mod->getNextValue($objsoc,$type);
  1860. dol_syslog(get_class($this)."::get_codefournisseur code_fournisseur=".$this->code_fournisseur." module=".$module);
  1861. }
  1862. }
  1863. /**
  1864. * Verifie si un code client est modifiable en fonction des parametres
  1865. * du module de controle des codes.
  1866. *
  1867. * @return int 0=No, 1=Yes
  1868. */
  1869. function codeclient_modifiable()
  1870. {
  1871. global $conf;
  1872. if (! empty($conf->global->SOCIETE_CODECLIENT_ADDON))
  1873. {
  1874. $module=$conf->global->SOCIETE_CODECLIENT_ADDON;
  1875. $dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
  1876. foreach ($dirsociete as $dirroot)
  1877. {
  1878. $res=dol_include_once($dirroot.$module.'.php');
  1879. if ($res) break;
  1880. }
  1881. $mod = new $module();
  1882. dol_syslog(get_class($this)."::codeclient_modifiable code_client=".$this->code_client." module=".$module);
  1883. if ($mod->code_modifiable_null && ! $this->code_client) return 1;
  1884. if ($mod->code_modifiable_invalide && $this->check_codeclient() < 0) return 1;
  1885. if ($mod->code_modifiable) return 1; // A mettre en dernier
  1886. return 0;
  1887. }
  1888. else
  1889. {
  1890. return 0;
  1891. }
  1892. }
  1893. /**
  1894. * Verifie si un code fournisseur est modifiable dans configuration du module de controle des codes
  1895. *
  1896. * @return int 0=No, 1=Yes
  1897. */
  1898. function codefournisseur_modifiable()
  1899. {
  1900. global $conf;
  1901. if (! empty($conf->global->SOCIETE_CODECLIENT_ADDON))
  1902. {
  1903. $module=$conf->global->SOCIETE_CODECLIENT_ADDON;
  1904. $dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
  1905. foreach ($dirsociete as $dirroot)
  1906. {
  1907. $res=dol_include_once($dirroot.$module.'.php');
  1908. if ($res) break;
  1909. }
  1910. $mod = new $module();
  1911. dol_syslog(get_class($this)."::codefournisseur_modifiable code_founisseur=".$this->code_fournisseur." module=".$module);
  1912. if ($mod->code_modifiable_null && ! $this->code_fournisseur) return 1;
  1913. if ($mod->code_modifiable_invalide && $this->check_codefournisseur() < 0) return 1;
  1914. if ($mod->code_modifiable) return 1; // A mettre en dernier
  1915. return 0;
  1916. }
  1917. else
  1918. {
  1919. return 0;
  1920. }
  1921. }
  1922. /**
  1923. * Check customer code
  1924. *
  1925. * @return int 0 if OK
  1926. * -1 ErrorBadCustomerCodeSyntax
  1927. * -2 ErrorCustomerCodeRequired
  1928. * -3 ErrorCustomerCodeAlreadyUsed
  1929. * -4 ErrorPrefixRequired
  1930. */
  1931. function check_codeclient()
  1932. {
  1933. global $conf;
  1934. if (! empty($conf->global->SOCIETE_CODECLIENT_ADDON))
  1935. {
  1936. $module=$conf->global->SOCIETE_CODECLIENT_ADDON;
  1937. $dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
  1938. foreach ($dirsociete as $dirroot)
  1939. {
  1940. $res=dol_include_once($dirroot.$module.'.php');
  1941. if ($res) break;
  1942. }
  1943. $mod = new $module();
  1944. dol_syslog(get_class($this)."::check_codeclient code_client=".$this->code_client." module=".$module);
  1945. $result = $mod->verif($this->db, $this->code_client, $this, 0);
  1946. return $result;
  1947. }
  1948. else
  1949. {
  1950. return 0;
  1951. }
  1952. }
  1953. /**
  1954. * Check supplier code
  1955. *
  1956. * @return int 0 if OK
  1957. * -1 ErrorBadCustomerCodeSyntax
  1958. * -2 ErrorCustomerCodeRequired
  1959. * -3 ErrorCustomerCodeAlreadyUsed
  1960. * -4 ErrorPrefixRequired
  1961. */
  1962. function check_codefournisseur()
  1963. {
  1964. global $conf;
  1965. if (! empty($conf->global->SOCIETE_CODECLIENT_ADDON))
  1966. {
  1967. $module=$conf->global->SOCIETE_CODECLIENT_ADDON;
  1968. $dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
  1969. foreach ($dirsociete as $dirroot)
  1970. {
  1971. $res=dol_include_once($dirroot.$module.'.php');
  1972. if ($res) break;
  1973. }
  1974. $mod = new $module();
  1975. dol_syslog(get_class($this)."::check_codefournisseur code_fournisseur=".$this->code_fournisseur." module=".$module);
  1976. $result = $mod->verif($this->db, $this->code_fournisseur, $this, 1);
  1977. return $result;
  1978. }
  1979. else
  1980. {
  1981. return 0;
  1982. }
  1983. }
  1984. /**
  1985. * Renvoie un code compta, suivant le module de code compta.
  1986. * Peut etre identique a celui saisit ou genere automatiquement.
  1987. * A ce jour seule la generation automatique est implementee
  1988. *
  1989. * @param string $type Type of thirdparty ('customer' or 'supplier')
  1990. * @return string Code compta si ok, 0 si aucun, <0 si ko
  1991. */
  1992. function get_codecompta($type)
  1993. {
  1994. global $conf;
  1995. if (! empty($conf->global->SOCIETE_CODECOMPTA_ADDON))
  1996. {
  1997. $file='';
  1998. $dirsociete=array_merge(array('/core/modules/societe/'), $conf->modules_parts['societe']);
  1999. foreach ($dirsociete as $dirroot)
  2000. {
  2001. if (file_exists(DOL_DOCUMENT_ROOT.'/'.$dirroot.$conf->global->SOCIETE_CODECOMPTA_ADDON.".php"))
  2002. {
  2003. $file=$dirroot.$conf->global->SOCIETE_CODECOMPTA_ADDON.".php";
  2004. break;
  2005. }
  2006. }
  2007. if (! empty($file))
  2008. {
  2009. dol_include_once($file);
  2010. $classname = $conf->global->SOCIETE_CODECOMPTA_ADDON;
  2011. $mod = new $classname;
  2012. // Defini code compta dans $mod->code
  2013. $result = $mod->get_code($this->db, $this, $type);
  2014. if ($type == 'customer') $this->code_compta = $mod->code;
  2015. else if ($type == 'supplier') $this->code_compta_fournisseur = $mod->code;
  2016. return $result;
  2017. }
  2018. else
  2019. {
  2020. $this->error = 'ErrorAccountancyCodeNotDefined';
  2021. return -1;
  2022. }
  2023. }
  2024. else
  2025. {
  2026. if ($type == 'customer') $this->code_compta = '';
  2027. else if ($type == 'supplier') $this->code_compta_fournisseur = '';
  2028. return 0;
  2029. }
  2030. }
  2031. /**
  2032. * Define parent commany of current company
  2033. *
  2034. * @param int $id Id of thirdparty to set or '' to remove
  2035. * @return int <0 if KO, >0 if OK
  2036. */
  2037. function set_parent($id)
  2038. {
  2039. if ($this->id)
  2040. {
  2041. $sql = "UPDATE ".MAIN_DB_PREFIX."societe";
  2042. $sql.= " SET parent = ".($id > 0 ? $id : "null");
  2043. $sql.= " WHERE rowid = " . $this->id;
  2044. dol_syslog(get_class($this).'::set_parent', LOG_DEBUG);
  2045. $resql=$this->db->query($sql);
  2046. if ($resql)
  2047. {
  2048. return 1;
  2049. }
  2050. else
  2051. {
  2052. return -1;
  2053. }
  2054. }
  2055. else return -1;
  2056. }
  2057. /**
  2058. * Returns if a profid sould be verified
  2059. *
  2060. * @param int $idprof 1,2,3,4 (Exemple: 1=siren,2=siret,3=naf,4=rcs/rm)
  2061. * @return boolean true , false
  2062. */
  2063. function id_prof_verifiable($idprof)
  2064. {
  2065. global $conf;
  2066. switch($idprof)
  2067. {
  2068. case 1:
  2069. $ret=(!$conf->global->SOCIETE_IDPROF1_UNIQUE?false:true);
  2070. break;
  2071. case 2:
  2072. $ret=(!$conf->global->SOCIETE_IDPROF2_UNIQUE?false:true);
  2073. break;
  2074. case 3:
  2075. $ret=(!$conf->global->SOCIETE_IDPROF3_UNIQUE?false:true);
  2076. break;
  2077. case 4:
  2078. $ret=(!$conf->global->SOCIETE_IDPROF4_UNIQUE?false:true);
  2079. break;
  2080. default:
  2081. $ret=false;
  2082. }
  2083. return $ret;
  2084. }
  2085. /**
  2086. * Verify if a profid exists into database for others thirds
  2087. *
  2088. * @param int $idprof 1,2,3,4 (Example: 1=siren,2=siret,3=naf,4=rcs/rm)
  2089. * @param string $value Value of profid
  2090. * @param int $socid Id of thirdparty if update
  2091. * @return boolean true if exists, false if not
  2092. */
  2093. function id_prof_exists($idprof,$value,$socid=0)
  2094. {
  2095. switch($idprof)
  2096. {
  2097. case 1:
  2098. $field="siren";
  2099. break;
  2100. case 2:
  2101. $field="siret";
  2102. break;
  2103. case 3:
  2104. $field="ape";
  2105. break;
  2106. case 4:
  2107. $field="idprof4";
  2108. break;
  2109. }
  2110. //Verify duplicate entries
  2111. $sql = "SELECT COUNT(*) as idprof FROM ".MAIN_DB_PREFIX."societe WHERE ".$field." = '".$value."'";
  2112. if($socid) $sql .= " AND rowid <> ".$socid;
  2113. $resql = $this->db->query($sql);
  2114. if ($resql)
  2115. {
  2116. $obj = $this->db->fetch_object($resql);
  2117. $count = $obj->idprof;
  2118. }
  2119. else
  2120. {
  2121. $count = 0;
  2122. print $this->db->error();
  2123. }
  2124. $this->db->free($resql);
  2125. if ($count > 0) return true;
  2126. else return false;
  2127. }
  2128. /**
  2129. * Verifie la validite d'un identifiant professionnel en fonction du pays de la societe (siren, siret, ...)
  2130. *
  2131. * @param int $idprof 1,2,3,4 (Exemple: 1=siren,2=siret,3=naf,4=rcs/rm)
  2132. * @param Societe $soc Objet societe
  2133. * @return int <=0 if KO, >0 if OK
  2134. * TODO not in business class
  2135. */
  2136. function id_prof_check($idprof,$soc)
  2137. {
  2138. global $conf;
  2139. $ok=1;
  2140. if (! empty($conf->global->MAIN_DISABLEPROFIDRULES)) return 1;
  2141. // Verifie SIREN si pays FR
  2142. if ($idprof == 1 && $soc->country_code == 'FR')
  2143. {
  2144. $chaine=trim($this->idprof1);
  2145. $chaine=preg_replace('/(\s)/','',$chaine);
  2146. if (dol_strlen($chaine) != 9) return -1;
  2147. $sum = 0;
  2148. for ($i = 0 ; $i < 10 ; $i = $i+2)
  2149. {
  2150. $sum = $sum + substr($this->idprof1, (8 - $i), 1);
  2151. }
  2152. for ($i = 1 ; $i < 9 ; $i = $i+2)
  2153. {
  2154. $ps = 2 * substr($this->idprof1, (8 - $i), 1);
  2155. if ($ps > 9)
  2156. {
  2157. $ps = substr($ps, 0,1) + substr($ps, 1, 1);
  2158. }
  2159. $sum = $sum + $ps;
  2160. }
  2161. if (substr($sum, -1) != 0) return -1;
  2162. }
  2163. // Verifie SIRET si pays FR
  2164. if ($idprof == 2 && $soc->country_code == 'FR')
  2165. {
  2166. $chaine=trim($this->idprof2);
  2167. $chaine=preg_replace('/(\s)/','',$chaine);
  2168. if (dol_strlen($chaine) != 14) return -1;
  2169. }
  2170. //Verify CIF/NIF/NIE if pays ES
  2171. //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
  2172. if ($idprof == 1 && $soc->country_code == 'ES')
  2173. {
  2174. $string=trim($this->idprof1);
  2175. $string=preg_replace('/(\s)/','',$string);
  2176. $string = strtoupper($string);
  2177. for ($i = 0; $i < 9; $i ++)
  2178. $num[$i] = substr($string, $i, 1);
  2179. //Check format
  2180. 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))
  2181. return 0;
  2182. //Check NIF
  2183. if (preg_match('/(^[0-9]{8}[A-Z]{1}$)/', $string))
  2184. if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($string, 0, 8) % 23, 1))
  2185. return 1;
  2186. else
  2187. return -1;
  2188. //algorithm checking type code CIF
  2189. $sum = $num[2] + $num[4] + $num[6];
  2190. for ($i = 1; $i < 8; $i += 2)
  2191. $sum += substr((2 * $num[$i]),0,1) + substr((2 * $num[$i]),1,1);
  2192. $n = 10 - substr($sum, strlen($sum) - 1, 1);
  2193. //Chek special NIF
  2194. if (preg_match('/^[KLM]{1}/', $string))
  2195. if ($num[8] == chr(64 + $n) || $num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($string, 1, 8) % 23, 1))
  2196. return 1;
  2197. else
  2198. return -1;
  2199. //Check CIF
  2200. if (preg_match('/^[ABCDEFGHJNPQRSUVW]{1}/', $string))
  2201. if ($num[8] == chr(64 + $n) || $num[8] == substr($n, strlen($n) - 1, 1))
  2202. return 2;
  2203. else
  2204. return -2;
  2205. //Check NIE T
  2206. if (preg_match('/^[T]{1}/', $string))
  2207. if ($num[8] == preg_match('/^[T]{1}[A-Z0-9]{8}$/', $string))
  2208. return 3;
  2209. else
  2210. return -3;
  2211. //Check NIE XYZ
  2212. if (preg_match('/^[XYZ]{1}/', $string))
  2213. if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr(str_replace(array('X','Y','Z'), array('0','1','2'), $string), 0, 8) % 23, 1))
  2214. return 3;
  2215. else
  2216. return -3;
  2217. //Can not be verified
  2218. return -4;
  2219. }
  2220. return $ok;
  2221. }
  2222. /**
  2223. * Renvoi url de verification d'un identifiant professionnal
  2224. *
  2225. * @param int $idprof 1,2,3,4 (Exemple: 1=siren,2=siret,3=naf,4=rcs/rm)
  2226. * @param Societe $soc Objet societe
  2227. * @return string url ou chaine vide si aucune url connue
  2228. * TODO not in business class
  2229. */
  2230. function id_prof_url($idprof,$soc)
  2231. {
  2232. global $conf,$langs;
  2233. if (! empty($conf->global->MAIN_DISABLEPROFIDRULES)) return '';
  2234. $url='';
  2235. if ($idprof == 1 && $soc->country_code == 'FR') $url='http://www.societe.com/cgi-bin/recherche?rncs='.$soc->idprof1;
  2236. if ($idprof == 1 && ($soc->country_code == 'GB' || $soc->country_code == 'UK')) $url='http://www.companieshouse.gov.uk/WebCHeck/findinfolink/';
  2237. if ($idprof == 1 && $soc->country_code == 'ES') $url='http://www.e-informa.es/servlet/app/portal/ENTP/screen/SProducto/prod/ETIQUETA_EMPRESA/nif/'.$soc->idprof1;
  2238. if ($idprof == 1 && $soc->country_code == 'IN') $url='http://www.tinxsys.com/TinxsysInternetWeb/dealerControllerServlet?tinNumber='.$soc->idprof1.';&searchBy=TIN&backPage=searchByTin_Inter.jsp';
  2239. if ($url) return '<a target="_blank" href="'.$url.'">['.$langs->trans("Check").']</a>';
  2240. return '';
  2241. }
  2242. /**
  2243. * Indique si la societe a des projets
  2244. *
  2245. * @return bool true si la societe a des projets, false sinon
  2246. */
  2247. function has_projects()
  2248. {
  2249. $sql = 'SELECT COUNT(*) as numproj FROM '.MAIN_DB_PREFIX.'projet WHERE fk_soc = ' . $this->id;
  2250. $resql = $this->db->query($sql);
  2251. if ($resql)
  2252. {
  2253. $obj = $this->db->fetch_object($resql);
  2254. $count = $obj->numproj;
  2255. }
  2256. else
  2257. {
  2258. $count = 0;
  2259. print $this->db->error();
  2260. }
  2261. $this->db->free($resql);
  2262. return ($count > 0);
  2263. }
  2264. /**
  2265. * Load information for tab info
  2266. *
  2267. * @param int $id Id of thirdparty to load
  2268. * @return void
  2269. */
  2270. function info($id)
  2271. {
  2272. $sql = "SELECT s.rowid, s.nom as name, s.datec as date_creation, tms as date_modification,";
  2273. $sql.= " fk_user_creat, fk_user_modif";
  2274. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  2275. $sql.= " WHERE s.rowid = ".$id;
  2276. $result=$this->db->query($sql);
  2277. if ($result)
  2278. {
  2279. if ($this->db->num_rows($result))
  2280. {
  2281. $obj = $this->db->fetch_object($result);
  2282. $this->id = $obj->rowid;
  2283. if ($obj->fk_user_creat) {
  2284. $cuser = new User($this->db);
  2285. $cuser->fetch($obj->fk_user_creat);
  2286. $this->user_creation = $cuser;
  2287. }
  2288. if ($obj->fk_user_modif) {
  2289. $muser = new User($this->db);
  2290. $muser->fetch($obj->fk_user_modif);
  2291. $this->user_modification = $muser;
  2292. }
  2293. $this->ref = $obj->name;
  2294. $this->date_creation = $this->db->jdate($obj->date_creation);
  2295. $this->date_modification = $this->db->jdate($obj->date_modification);
  2296. }
  2297. $this->db->free($result);
  2298. }
  2299. else
  2300. {
  2301. dol_print_error($this->db);
  2302. }
  2303. }
  2304. /**
  2305. * Return if third party is a company (Business) or an end user (Consumer)
  2306. *
  2307. * @return boolean true=is a company, false=a and user
  2308. */
  2309. function isACompany()
  2310. {
  2311. global $conf;
  2312. // Define if third party is treated as company (or not) when nature is unknown
  2313. $isacompany=empty($conf->global->MAIN_UNKNOWN_CUSTOMERS_ARE_COMPANIES)?0:1; // 0 by default
  2314. if (! empty($this->tva_intra)) $isacompany=1;
  2315. else if (! empty($this->typent_code) && in_array($this->typent_code,array('TE_PRIVATE'))) $isacompany=0;
  2316. else if (! empty($this->typent_code) && in_array($this->typent_code,array('TE_SMALL','TE_MEDIUM','TE_LARGE'))) $isacompany=1;
  2317. return $isacompany;
  2318. }
  2319. /**
  2320. * Charge la liste des categories fournisseurs
  2321. *
  2322. * @return int 0 if success, <> 0 if error
  2323. */
  2324. function LoadSupplierCateg()
  2325. {
  2326. $this->SupplierCategories = array();
  2327. $sql = "SELECT rowid, label";
  2328. $sql.= " FROM ".MAIN_DB_PREFIX."categorie";
  2329. $sql.= " WHERE type = 1";
  2330. $resql=$this->db->query($sql);
  2331. if ($resql)
  2332. {
  2333. while ($obj = $this->db->fetch_object($resql) )
  2334. {
  2335. $this->SupplierCategories[$obj->rowid] = $obj->label;
  2336. }
  2337. return 0;
  2338. }
  2339. else
  2340. {
  2341. return -1;
  2342. }
  2343. }
  2344. /**
  2345. * Charge la liste des categories fournisseurs
  2346. *
  2347. * @param int $categorie_id Id of category
  2348. * @return int 0 if success, <> 0 if error
  2349. */
  2350. function AddFournisseurInCategory($categorie_id)
  2351. {
  2352. if ($categorie_id > 0)
  2353. {
  2354. $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_fournisseur (fk_categorie, fk_societe) ";
  2355. $sql.= " VALUES ('".$categorie_id."','".$this->id."');";
  2356. if ($resql=$this->db->query($sql)) return 0;
  2357. }
  2358. else
  2359. {
  2360. return 0;
  2361. }
  2362. return -1;
  2363. }
  2364. /**
  2365. * Create a third party into database from a member object
  2366. *
  2367. * @param Adherent $member Object member
  2368. * @param string $socname Name of third party to force
  2369. * @return int <0 if KO, id of created account if OK
  2370. */
  2371. function create_from_member(Adherent $member,$socname='')
  2372. {
  2373. global $user,$langs;
  2374. $name = $socname?$socname:$member->societe;
  2375. if (empty($name)) $name=$member->getFullName($langs);
  2376. // Positionne parametres
  2377. $this->nom=$name; // TODO obsolete
  2378. $this->name=$name;
  2379. $this->address=$member->address;
  2380. $this->zip=$member->zip;
  2381. $this->town=$member->town;
  2382. $this->country_code=$member->country_code;
  2383. $this->country_id=$member->country_id;
  2384. $this->phone=$member->phone; // Prof phone
  2385. $this->email=$member->email;
  2386. $this->skype=$member->skype;
  2387. $this->client = 1; // A member is a customer by default
  2388. $this->code_client = -1;
  2389. $this->code_fournisseur = -1;
  2390. $this->db->begin();
  2391. // Cree et positionne $this->id
  2392. $result=$this->create($user);
  2393. if ($result >= 0)
  2394. {
  2395. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent";
  2396. $sql.= " SET fk_soc=".$this->id;
  2397. $sql.= " WHERE rowid=".$member->id;
  2398. dol_syslog(get_class($this)."::create_from_member", LOG_DEBUG);
  2399. $resql=$this->db->query($sql);
  2400. if ($resql)
  2401. {
  2402. $this->db->commit();
  2403. return $this->id;
  2404. }
  2405. else
  2406. {
  2407. $this->error=$this->db->error();
  2408. $this->db->rollback();
  2409. return -1;
  2410. }
  2411. }
  2412. else
  2413. {
  2414. // $this->error deja positionne
  2415. dol_syslog(get_class($this)."::create_from_member - 2 - ".$this->error." - ".join(',',$this->errors), LOG_ERR);
  2416. $this->db->rollback();
  2417. return $result;
  2418. }
  2419. }
  2420. /**
  2421. * Set properties with value into $conf
  2422. *
  2423. * @param Conf $conf Conf object (possibility to use another entity)
  2424. * @return void
  2425. */
  2426. function setMysoc(Conf $conf)
  2427. {
  2428. global $langs;
  2429. $this->id=0;
  2430. $this->name=empty($conf->global->MAIN_INFO_SOCIETE_NOM)?'':$conf->global->MAIN_INFO_SOCIETE_NOM;
  2431. $this->address=empty($conf->global->MAIN_INFO_SOCIETE_ADDRESS)?'':$conf->global->MAIN_INFO_SOCIETE_ADDRESS;
  2432. $this->zip=empty($conf->global->MAIN_INFO_SOCIETE_ZIP)?'':$conf->global->MAIN_INFO_SOCIETE_ZIP;
  2433. $this->town=empty($conf->global->MAIN_INFO_SOCIETE_TOWN)?'':$conf->global->MAIN_INFO_SOCIETE_TOWN;
  2434. $this->state_id=empty($conf->global->MAIN_INFO_SOCIETE_STATE)?'':$conf->global->MAIN_INFO_SOCIETE_STATE;
  2435. /* Disabled: we don't want any SQL request into method setMySoc. This method set object from env only.
  2436. If we need label, label must be loaded by output that need it from id (label depends on output language)
  2437. require_once DOL_DOCUMENT_ROOT .'/core/lib/company.lib.php';
  2438. if (!empty($conf->global->MAIN_INFO_SOCIETE_STATE)) {
  2439. $this->state_id= $conf->global->MAIN_INFO_SOCIETE_STATE;
  2440. $this->state = getState($this->state_id);
  2441. }
  2442. */
  2443. $this->note_private=empty($conf->global->MAIN_INFO_SOCIETE_NOTE)?'':$conf->global->MAIN_INFO_SOCIETE_NOTE;
  2444. $this->nom=$this->name; // deprecated
  2445. // We define country_id, country_code and country
  2446. $country_id=$country_code=$country_label='';
  2447. if (! empty($conf->global->MAIN_INFO_SOCIETE_COUNTRY))
  2448. {
  2449. $tmp=explode(':',$conf->global->MAIN_INFO_SOCIETE_COUNTRY);
  2450. $country_id=$tmp[0];
  2451. if (! empty($tmp[1])) // If $conf->global->MAIN_INFO_SOCIETE_COUNTRY is "id:code:label"
  2452. {
  2453. $country_code=$tmp[1];
  2454. $country_label=$tmp[2];
  2455. }
  2456. else // For backward compatibility
  2457. {
  2458. dol_syslog("Your country setup use an old syntax. Reedit it using setup area.", LOG_ERR);
  2459. include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  2460. $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
  2461. $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
  2462. }
  2463. }
  2464. $this->country_id=$country_id;
  2465. $this->country_code=$country_code;
  2466. $this->country=$country_label;
  2467. if (is_object($langs)) $this->country=($langs->trans('Country'.$country_code)!='Country'.$country_code)?$langs->trans('Country'.$country_code):$country_label;
  2468. $this->phone=empty($conf->global->MAIN_INFO_SOCIETE_TEL)?'':$conf->global->MAIN_INFO_SOCIETE_TEL;
  2469. $this->fax=empty($conf->global->MAIN_INFO_SOCIETE_FAX)?'':$conf->global->MAIN_INFO_SOCIETE_FAX;
  2470. $this->url=empty($conf->global->MAIN_INFO_SOCIETE_WEB)?'':$conf->global->MAIN_INFO_SOCIETE_WEB;
  2471. // Id prof generiques
  2472. $this->idprof1=empty($conf->global->MAIN_INFO_SIREN)?'':$conf->global->MAIN_INFO_SIREN;
  2473. $this->idprof2=empty($conf->global->MAIN_INFO_SIRET)?'':$conf->global->MAIN_INFO_SIRET;
  2474. $this->idprof3=empty($conf->global->MAIN_INFO_APE)?'':$conf->global->MAIN_INFO_APE;
  2475. $this->idprof4=empty($conf->global->MAIN_INFO_RCS)?'':$conf->global->MAIN_INFO_RCS;
  2476. $this->idprof5=empty($conf->global->MAIN_INFO_PROFID5)?'':$conf->global->MAIN_INFO_PROFID5;
  2477. $this->idprof6=empty($conf->global->MAIN_INFO_PROFID6)?'':$conf->global->MAIN_INFO_PROFID6;
  2478. $this->tva_intra=empty($conf->global->MAIN_INFO_TVAINTRA)?'':$conf->global->MAIN_INFO_TVAINTRA; // VAT number, not necessarly INTRA.
  2479. $this->managers=empty($conf->global->MAIN_INFO_SOCIETE_MANAGERS)?'':$conf->global->MAIN_INFO_SOCIETE_MANAGERS;
  2480. $this->capital=empty($conf->global->MAIN_INFO_CAPITAL)?'':$conf->global->MAIN_INFO_CAPITAL;
  2481. $this->forme_juridique_code=empty($conf->global->MAIN_INFO_SOCIETE_FORME_JURIDIQUE)?'':$conf->global->MAIN_INFO_SOCIETE_FORME_JURIDIQUE;
  2482. $this->email=empty($conf->global->MAIN_INFO_SOCIETE_MAIL)?'':$conf->global->MAIN_INFO_SOCIETE_MAIL;
  2483. $this->logo=empty($conf->global->MAIN_INFO_SOCIETE_LOGO)?'':$conf->global->MAIN_INFO_SOCIETE_LOGO;
  2484. $this->logo_small=empty($conf->global->MAIN_INFO_SOCIETE_LOGO_SMALL)?'':$conf->global->MAIN_INFO_SOCIETE_LOGO_SMALL;
  2485. $this->logo_mini=empty($conf->global->MAIN_INFO_SOCIETE_LOGO_MINI)?'':$conf->global->MAIN_INFO_SOCIETE_LOGO_MINI;
  2486. // Define if company use vat or not
  2487. $this->tva_assuj=$conf->global->FACTURE_TVAOPTION;
  2488. // Define if company use local taxes
  2489. $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);
  2490. $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);
  2491. }
  2492. /**
  2493. * Initialise an instance with random values.
  2494. * Used to build previews or test instances.
  2495. * id must be 0 if object instance is a specimen.
  2496. *
  2497. * @return void
  2498. */
  2499. function initAsSpecimen()
  2500. {
  2501. $now=dol_now();
  2502. // Initialize parameters
  2503. $this->id=0;
  2504. $this->name = 'THIRDPARTY SPECIMEN '.dol_print_date($now,'dayhourlog');
  2505. $this->nom = $this->name; // For backward compatibility
  2506. $this->ref_ext = 'Ref ext';
  2507. $this->specimen=1;
  2508. $this->address='21 jump street';
  2509. $this->zip='99999';
  2510. $this->town='MyTown';
  2511. $this->state_id=1;
  2512. $this->state_code='AA';
  2513. $this->state='MyState';
  2514. $this->country_id=1;
  2515. $this->country_code='FR';
  2516. $this->email='specimen@specimen.com';
  2517. $this->skype='tom.hanson';
  2518. $this->url='http://www.specimen.com';
  2519. $this->phone='0909090901';
  2520. $this->fax='0909090909';
  2521. $this->code_client='CC-'.dol_print_date($now,'dayhourlog');
  2522. $this->code_fournisseur='SC-'.dol_print_date($now,'dayhourlog');
  2523. $this->capital=10000;
  2524. $this->client=1;
  2525. $this->prospect=1;
  2526. $this->fournisseur=1;
  2527. $this->tva_assuj=1;
  2528. $this->tva_intra='EU1234567';
  2529. $this->note_public='This is a comment (public)';
  2530. $this->note_private='This is a comment (private)';
  2531. $this->idprof1='idprof1';
  2532. $this->idprof2='idprof2';
  2533. $this->idprof3='idprof3';
  2534. $this->idprof4='idprof4';
  2535. $this->idprof5='idprof5';
  2536. $this->idprof6='idprof6';
  2537. }
  2538. /**
  2539. * Check if we must use localtax feature or not according to country (country of $mysocin most cases).
  2540. *
  2541. * @param int $localTaxNum To get info for only localtax1 or localtax2
  2542. * @return boolean true or false
  2543. */
  2544. function useLocalTax($localTaxNum=0)
  2545. {
  2546. $sql = "SELECT t.localtax1, t.localtax2";
  2547. $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
  2548. $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$this->country_code."'";
  2549. $sql .= " AND t.active = 1";
  2550. if (empty($localTaxNum)) $sql .= " AND (t.localtax1_type <> '0' OR t.localtax2_type <> '0')";
  2551. elseif ($localTaxNum == 1) $sql .= " AND t.localtax1_type <> '0'";
  2552. elseif ($localTaxNum == 2) $sql .= " AND t.localtax2_type <> '0'";
  2553. dol_syslog("useLocalTax", LOG_DEBUG);
  2554. $resql=$this->db->query($sql);
  2555. if ($resql)
  2556. {
  2557. return ($this->db->num_rows($resql) > 0);
  2558. }
  2559. else return false;
  2560. }
  2561. /**
  2562. * Check if we must use revenue stamps feature or not according to country (country of $mysocin most cases).
  2563. *
  2564. * @return boolean true or false
  2565. */
  2566. function useRevenueStamp()
  2567. {
  2568. $sql = "SELECT COUNT(*) as nb";
  2569. $sql .= " FROM ".MAIN_DB_PREFIX."c_revenuestamp as r, ".MAIN_DB_PREFIX."c_country as c";
  2570. $sql .= " WHERE r.fk_pays = c.rowid AND c.code = '".$this->country_code."'";
  2571. $sql .= " AND r.active = 1";
  2572. dol_syslog("useRevenueStamp", LOG_DEBUG);
  2573. $resql=$this->db->query($sql);
  2574. if ($resql)
  2575. {
  2576. $obj=$this->db->fetch_object($resql);
  2577. return (($obj->nb > 0)?true:false);
  2578. }
  2579. else
  2580. {
  2581. $this->error=$this->db->lasterror();
  2582. return false;
  2583. }
  2584. }
  2585. /**
  2586. * Return prostect level
  2587. *
  2588. * @return string Libelle
  2589. */
  2590. function getLibProspLevel()
  2591. {
  2592. return $this->LibProspLevel($this->fk_prospectlevel);
  2593. }
  2594. /**
  2595. * Return label of prospect level
  2596. *
  2597. * @param int $fk_prospectlevel Prospect level
  2598. * @return string label of level
  2599. */
  2600. function LibProspLevel($fk_prospectlevel)
  2601. {
  2602. global $langs;
  2603. $lib=$langs->trans("ProspectLevel".$fk_prospectlevel);
  2604. // If lib not found in language file, we get label from cache/databse
  2605. if ($lib == $langs->trans("ProspectLevel".$fk_prospectlevel))
  2606. {
  2607. $lib=$langs->getLabelFromKey($this->db,$fk_prospectlevel,'c_prospectlevel','code','label');
  2608. }
  2609. return $lib;
  2610. }
  2611. /**
  2612. * Set prospect level
  2613. *
  2614. * @param User $user Utilisateur qui definie la remise
  2615. * @return int <0 if KO, >0 if OK
  2616. */
  2617. function set_prospect_level(User $user)
  2618. {
  2619. if ($this->id)
  2620. {
  2621. $this->db->begin();
  2622. // Positionne remise courante
  2623. $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET ";
  2624. $sql.= " fk_prospectlevel='".$this->fk_prospectlevel."'";
  2625. $sql.= ",fk_user_modif='".$user->id."'";
  2626. $sql.= " WHERE rowid = ".$this->id;
  2627. dol_syslog(get_class($this)."::set_prospect_level", LOG_DEBUG);
  2628. $resql=$this->db->query($sql);
  2629. if (! $resql)
  2630. {
  2631. $this->db->rollback();
  2632. $this->error=$this->db->error();
  2633. return -1;
  2634. }
  2635. $this->db->commit();
  2636. return 1;
  2637. }
  2638. }
  2639. /**
  2640. * Return status of prospect
  2641. *
  2642. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long
  2643. * @return string Libelle
  2644. */
  2645. function getLibProspCommStatut($mode=0)
  2646. {
  2647. return $this->LibProspCommStatut($this->stcomm_id,$mode);
  2648. }
  2649. /**
  2650. * Return label of a given status
  2651. *
  2652. * @param int $statut Id statut
  2653. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  2654. * @return string Libelle du statut
  2655. */
  2656. function LibProspCommStatut($statut,$mode=0)
  2657. {
  2658. global $langs;
  2659. $langs->load('customers');
  2660. if ($mode == 2)
  2661. {
  2662. if ($statut == -1) return img_action($langs->trans("StatusProspect-1"),-1).' '.$langs->trans("StatusProspect-1");
  2663. if ($statut == 0) return img_action($langs->trans("StatusProspect0"), 0).' '.$langs->trans("StatusProspect0");
  2664. if ($statut == 1) return img_action($langs->trans("StatusProspect1"), 1).' '.$langs->trans("StatusProspect1");
  2665. if ($statut == 2) return img_action($langs->trans("StatusProspect2"), 2).' '.$langs->trans("StatusProspect2");
  2666. if ($statut == 3) return img_action($langs->trans("StatusProspect3"), 3).' '.$langs->trans("StatusProspect3");
  2667. }
  2668. if ($mode == 3)
  2669. {
  2670. if ($statut == -1) return img_action($langs->trans("StatusProspect-1"),-1);
  2671. if ($statut == 0) return img_action($langs->trans("StatusProspect0"), 0);
  2672. if ($statut == 1) return img_action($langs->trans("StatusProspect1"), 1);
  2673. if ($statut == 2) return img_action($langs->trans("StatusProspect2"), 2);
  2674. if ($statut == 3) return img_action($langs->trans("StatusProspect3"), 3);
  2675. }
  2676. if ($mode == 4)
  2677. {
  2678. if ($statut == -1) return img_action($langs->trans("StatusProspect-1"),-1).' '.$langs->trans("StatusProspect-1");
  2679. if ($statut == 0) return img_action($langs->trans("StatusProspect0"), 0).' '.$langs->trans("StatusProspect0");
  2680. if ($statut == 1) return img_action($langs->trans("StatusProspect1"), 1).' '.$langs->trans("StatusProspect1");
  2681. if ($statut == 2) return img_action($langs->trans("StatusProspect2"), 2).' '.$langs->trans("StatusProspect2");
  2682. if ($statut == 3) return img_action($langs->trans("StatusProspect3"), 3).' '.$langs->trans("StatusProspect3");
  2683. }
  2684. return "Error, mode/status not found";
  2685. }
  2686. /**
  2687. * Set commnunication level
  2688. *
  2689. * @param User $user User making change
  2690. * @return int <0 if KO, >0 if OK
  2691. */
  2692. function set_commnucation_level($user)
  2693. {
  2694. if ($this->id)
  2695. {
  2696. $this->db->begin();
  2697. // Positionne remise courante
  2698. $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET ";
  2699. $sql.= " fk_stcomm='".$this->stcomm_id."'";
  2700. $sql.= ",fk_user_modif='".$user->id."'";
  2701. $sql.= " WHERE rowid = ".$this->id;
  2702. dol_syslog(get_class($this)."::set_commnucation_level", LOG_DEBUG);
  2703. $resql=$this->db->query($sql);
  2704. if (! $resql)
  2705. {
  2706. $this->db->rollback();
  2707. $this->error=$this->db->error();
  2708. return -1;
  2709. }
  2710. $this->db->commit();
  2711. return 1;
  2712. }
  2713. }
  2714. /**
  2715. * Set outstanding value
  2716. *
  2717. * @param User $user User making change
  2718. * @return int <0 if KO, >0 if OK
  2719. */
  2720. function set_OutstandingBill (User $user)
  2721. {
  2722. if ($this->id)
  2723. {
  2724. $this->db->begin();
  2725. // Clean parameters
  2726. $outstanding = price2num($this->outstanding_limit);
  2727. // Set outstanding amount
  2728. $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET ";
  2729. $sql.= " outstanding_limit= '".($outstanding!=''?$outstanding:'null')."'";
  2730. $sql.= " WHERE rowid = ".$this->id;
  2731. dol_syslog(get_class($this)."::set_outstanding", LOG_DEBUG);
  2732. $resql=$this->db->query($sql);
  2733. if ($resql)
  2734. {
  2735. $this->db->commit();
  2736. return 1;
  2737. }
  2738. else
  2739. {
  2740. $this->db->rollback();
  2741. $this->error=$this->db->lasterror();
  2742. return -1;
  2743. }
  2744. }
  2745. }
  2746. /**
  2747. * Return amount of bill not paid
  2748. *
  2749. * @return int Amount in debt for thirdparty
  2750. */
  2751. function get_OutstandingBill()
  2752. {
  2753. /* Accurate value of remain to pay is to sum remaintopay for each invoice
  2754. $paiement = $invoice->getSommePaiement();
  2755. $creditnotes=$invoice->getSumCreditNotesUsed();
  2756. $deposits=$invoice->getSumDepositsUsed();
  2757. $alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT');
  2758. $remaintopay=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT');
  2759. */
  2760. $sql = "SELECT rowid, total_ttc FROM ".MAIN_DB_PREFIX."facture as f";
  2761. $sql .= " WHERE fk_soc = ". $this->id;
  2762. $sql .= " AND paye = 0";
  2763. $sql .= " AND fk_statut <> 0"; // Not a draft
  2764. //$sql .= " AND (fk_statut <> 3 OR close_code <> 'abandon')"; // Not abandonned for undefined reason
  2765. $sql .= " AND fk_statut <> 3"; // Not abandonned
  2766. $sql .= " AND fk_statut <> 2"; // Not clasified as paid
  2767. dol_syslog("get_OutstandingBill", LOG_DEBUG);
  2768. $resql=$this->db->query($sql);
  2769. if ($resql)
  2770. {
  2771. $outstandingBill = 0;
  2772. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  2773. $facturestatic=new Facture($this->db);
  2774. while($obj=$this->db->fetch_object($resql)) {
  2775. $facturestatic->id=$obj->rowid;
  2776. $paiement = $facturestatic->getSommePaiement();
  2777. $creditnotes = $facturestatic->getSumCreditNotesUsed();
  2778. $deposits = $facturestatic->getSumDepositsUsed();
  2779. $outstandingBill+= $obj->total_ttc - $paiement - $creditnotes - $deposits;
  2780. }
  2781. return $outstandingBill;
  2782. }
  2783. else
  2784. return 0;
  2785. }
  2786. /**
  2787. * Return label of status customer is prospect/customer
  2788. *
  2789. * @return string Label
  2790. */
  2791. function getLibCustProspStatut()
  2792. {
  2793. return $this->LibCustProspStatut($this->client);
  2794. }
  2795. /**
  2796. * Renvoi le libelle d'un statut donne
  2797. *
  2798. * @param int $statut Id statut
  2799. * @return string Libelle du statut
  2800. */
  2801. function LibCustProspStatut($statut)
  2802. {
  2803. global $langs;
  2804. $langs->load('companies');
  2805. if ($statut==0) return $langs->trans("NorProspectNorCustomer");
  2806. if ($statut==1) return $langs->trans("Customer");
  2807. if ($statut==2) return $langs->trans("Prospect");
  2808. if ($statut==3) return $langs->trans("ProspectCustomer");
  2809. }
  2810. }