societe.class.php 120 KB

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