societe.class.php 193 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601
  1. <?php
  2. /* Copyright (C) 2002-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2021 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  5. * Copyright (C) 2003 Brian Fraval <brian@fraval.org>
  6. * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
  7. * Copyright (C) 2005-2017 Regis Houssin <regis.houssin@inodbox.com>
  8. * Copyright (C) 2008 Patrick Raguin <patrick.raguin@auguria.net>
  9. * Copyright (C) 2010-2018 Juanjo Menent <jmenent@2byte.es>
  10. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  11. * Copyright (C) 2013-2023 Alexandre Spangaro <aspangaro@open-dsi.fr>
  12. * Copyright (C) 2013 Peter Fontaine <contact@peterfontaine.fr>
  13. * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
  14. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  15. * Copyright (C) 2017 Rui Strecht <rui.strecht@aliartalentos.com>
  16. * Copyright (C) 2018 Philippe Grand <philippe.grand@atoo-net.com>
  17. * Copyright (C) 2019-2020 Josep Lluís Amador <joseplluis@lliuretic.cat>
  18. * Copyright (C) 2019-2023 Frédéric France <frederic.france@netlogic.fr>
  19. * Copyright (C) 2020 Open-Dsi <support@open-dsi.fr>
  20. * Copyright (C) 2022 ButterflyOfFire <butterflyoffire+dolibarr@protonmail.com>
  21. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; either version 3 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  35. */
  36. /**
  37. * \file htdocs/societe/class/societe.class.php
  38. * \ingroup societe
  39. * \brief File for third party class
  40. */
  41. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  42. require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
  43. require_once DOL_DOCUMENT_ROOT.'/core/class/commonsocialnetworks.class.php';
  44. require_once DOL_DOCUMENT_ROOT.'/core/class/commonpeople.class.php';
  45. require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
  46. /**
  47. * Class to manage third parties objects (customers, suppliers, prospects...)
  48. */
  49. class Societe extends CommonObject
  50. {
  51. use CommonIncoterm;
  52. use CommonSocialNetworks;
  53. use CommonPeople;
  54. /**
  55. * @var string ID of module.
  56. */
  57. public $module = 'societe';
  58. /**
  59. * @var string ID to identify managed object
  60. */
  61. public $element = 'societe';
  62. /**
  63. * @var string Name of table without prefix where object is stored
  64. */
  65. public $table_element = 'societe';
  66. /**
  67. * @var string Field with ID of parent key if this field has a parent or for child tables
  68. */
  69. public $fk_element = 'fk_soc';
  70. /**
  71. * @var string Fields for combobox
  72. */
  73. public $fieldsforcombobox = 'nom,name_alias';
  74. /**
  75. * @var array List of child tables. To test if we can delete object.
  76. */
  77. protected $childtables = array(
  78. 'supplier_proposal' => array('name' => 'SupplierProposal'),
  79. 'propal' => array('name' => 'Proposal'),
  80. 'commande' => array('name' => 'Order'),
  81. 'facture' => array('name' => 'Invoice'),
  82. 'facture_rec' => array('name' => 'RecurringInvoiceTemplate'),
  83. 'contrat' => array('name' => 'Contract'),
  84. 'fichinter' => array('name' => 'Fichinter'),
  85. 'facture_fourn' => array('name' => 'SupplierInvoice'),
  86. 'commande_fournisseur' => array('name' => 'SupplierOrder'),
  87. 'projet' => array('name' => 'Project'),
  88. 'expedition' => array('name' => 'Shipment'),
  89. 'prelevement_lignes' => array('name' => 'DirectDebitRecord'),
  90. );
  91. /**
  92. * @var array List of child tables. To know object to delete on cascade.
  93. * if name like with @ClassName:FilePathClass:ParentFkFieldName' it will call method deleteByParentField (with parentId as parameters) and FieldName to fetch and delete child object
  94. */
  95. protected $childtablesoncascade = array(
  96. 'societe_prices',
  97. 'product_fournisseur_price',
  98. 'product_customer_price_log',
  99. 'product_customer_price',
  100. '@Contact:/contact/class/contact.class.php:fk_soc',
  101. 'adherent',
  102. 'societe_account',
  103. 'societe_rib',
  104. 'societe_remise',
  105. 'societe_remise_except',
  106. 'societe_commerciaux',
  107. 'categorie',
  108. 'notify',
  109. 'notify_def',
  110. 'actioncomm',
  111. );
  112. /**
  113. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  114. */
  115. public $picto = 'company';
  116. /**
  117. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  118. * @var int
  119. */
  120. public $ismultientitymanaged = 1;
  121. /**
  122. * @var int Does object support extrafields ? 0=No, 1=Yes
  123. */
  124. public $isextrafieldmanaged = 1;
  125. /**
  126. * 0=Default, 1=View may be restricted to sales representative only if no permission to see all or to company of external user if external user
  127. * @var integer
  128. */
  129. public $restrictiononfksoc = 1;
  130. /**
  131. * @var Societe To store a cloned copy of object before to edit it and keep track of old properties
  132. */
  133. public $oldcopy;
  134. /**
  135. * array of supplier categories
  136. * @var array
  137. */
  138. public $SupplierCategories = array();
  139. /**
  140. * prefixCustomerIsRequired
  141. * @var int
  142. */
  143. public $prefixCustomerIsRequired;
  144. /**
  145. * 'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter]]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'text:none', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
  146. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  147. * 'label' the translation key.
  148. * 'picto' is code of a picto to show before value in forms
  149. * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM)
  150. * 'position' is the sort order of field.
  151. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  152. * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
  153. * 'noteditable' says if field is not editable (1 or 0)
  154. * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.
  155. * 'index' if we want an index in database.
  156. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  157. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  158. * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
  159. * 'css' and 'cssview' and 'csslist' is the CSS style to use on field. 'css' is used in creation and update. 'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'maxwidth200', 'wordbreak', 'tdoverflowmax200'
  160. * 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
  161. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  162. * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.
  163. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  164. * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
  165. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  166. *
  167. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  168. */
  169. /**
  170. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  171. */
  172. public $fields = array(
  173. 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
  174. 'parent' =>array('type'=>'integer', 'label'=>'Parent', 'enabled'=>1, 'visible'=>-1, 'position'=>20),
  175. 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>25),
  176. 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>30),
  177. 'nom' =>array('type'=>'varchar(128)', 'label'=>'Nom', 'enabled'=>1, 'visible'=>-1, 'position'=>35, 'showoncombobox'=>1),
  178. 'name_alias' =>array('type'=>'varchar(128)', 'label'=>'Name alias', 'enabled'=>1, 'visible'=>-1, 'position'=>36, 'showoncombobox'=>2),
  179. 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>40, 'index'=>1),
  180. 'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'RefExt', 'enabled'=>1, 'visible'=>0, 'position'=>45),
  181. 'code_client' =>array('type'=>'varchar(24)', 'label'=>'CustomerCode', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
  182. 'code_fournisseur' =>array('type'=>'varchar(24)', 'label'=>'SupplierCode', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
  183. 'code_compta' =>array('type'=>'varchar(24)', 'label'=>'CustomerAccountancyCode', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
  184. 'code_compta_fournisseur' =>array('type'=>'varchar(24)', 'label'=>'SupplierAccountancyCode', 'enabled'=>1, 'visible'=>-1, 'position'=>70),
  185. 'address' =>array('type'=>'varchar(255)', 'label'=>'Address', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
  186. 'zip' =>array('type'=>'varchar(25)', 'label'=>'Zip', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
  187. 'town' =>array('type'=>'varchar(50)', 'label'=>'Town', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
  188. 'fk_departement' =>array('type'=>'integer', 'label'=>'State', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
  189. 'fk_pays' =>array('type'=>'integer:Ccountry:core/class/ccountry.class.php', 'label'=>'Country', 'enabled'=>1, 'visible'=>-1, 'position'=>95),
  190. 'phone' =>array('type'=>'varchar(20)', 'label'=>'Phone', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
  191. 'fax' =>array('type'=>'varchar(20)', 'label'=>'Fax', 'enabled'=>1, 'visible'=>-1, 'position'=>105),
  192. 'url' =>array('type'=>'varchar(255)', 'label'=>'Url', 'enabled'=>1, 'visible'=>-1, 'position'=>110),
  193. 'email' =>array('type'=>'varchar(128)', 'label'=>'Email', 'enabled'=>1, 'visible'=>-1, 'position'=>115),
  194. 'socialnetworks' =>array('type'=>'text', 'label'=>'Socialnetworks', 'enabled'=>1, 'visible'=>-1, 'position'=>120),
  195. 'fk_effectif' =>array('type'=>'integer', 'label'=>'Workforce', 'enabled'=>1, 'visible'=>-1, 'position'=>170),
  196. 'fk_typent' =>array('type'=>'integer', 'label'=>'TypeOfCompany', 'enabled'=>1, 'visible'=>-1, 'position'=>175, 'csslist'=>'minwidth200'),
  197. 'fk_forme_juridique' =>array('type'=>'integer', 'label'=>'JuridicalStatus', 'enabled'=>1, 'visible'=>-1, 'position'=>180),
  198. 'fk_currency' =>array('type'=>'varchar(3)', 'label'=>'Currency', 'enabled'=>1, 'visible'=>-1, 'position'=>185),
  199. 'siren' =>array('type'=>'varchar(128)', 'label'=>'Idprof1', 'enabled'=>1, 'visible'=>-1, 'position'=>190),
  200. 'siret' =>array('type'=>'varchar(128)', 'label'=>'Idprof2', 'enabled'=>1, 'visible'=>-1, 'position'=>195),
  201. 'ape' =>array('type'=>'varchar(128)', 'label'=>'Idprof3', 'enabled'=>1, 'visible'=>-1, 'position'=>200),
  202. 'idprof4' =>array('type'=>'varchar(128)', 'label'=>'Idprof4', 'enabled'=>1, 'visible'=>-1, 'position'=>205),
  203. 'idprof5' =>array('type'=>'varchar(128)', 'label'=>'Idprof5', 'enabled'=>1, 'visible'=>-1, 'position'=>206),
  204. 'idprof6' =>array('type'=>'varchar(128)', 'label'=>'Idprof6', 'enabled'=>1, 'visible'=>-1, 'position'=>207),
  205. 'tva_intra' =>array('type'=>'varchar(20)', 'label'=>'Tva intra', 'enabled'=>1, 'visible'=>-1, 'position'=>210),
  206. 'capital' =>array('type'=>'double(24,8)', 'label'=>'Capital', 'enabled'=>1, 'visible'=>-1, 'position'=>215),
  207. 'fk_stcomm' =>array('type'=>'integer', 'label'=>'CommercialStatus', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>220),
  208. 'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>225),
  209. 'note_private' =>array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>230),
  210. 'prefix_comm' =>array('type'=>'varchar(5)', 'label'=>'Prefix comm', 'enabled'=>"getDolGlobalInt('SOCIETE_USEPREFIX')", 'visible'=>-1, 'position'=>235),
  211. 'client' =>array('type'=>'tinyint(4)', 'label'=>'Client', 'enabled'=>1, 'visible'=>-1, 'position'=>240),
  212. 'fournisseur' =>array('type'=>'tinyint(4)', 'label'=>'Fournisseur', 'enabled'=>1, 'visible'=>-1, 'position'=>245),
  213. 'supplier_account' =>array('type'=>'varchar(32)', 'label'=>'Supplier account', 'enabled'=>1, 'visible'=>-1, 'position'=>250),
  214. 'fk_prospectlevel' =>array('type'=>'varchar(12)', 'label'=>'ProspectLevel', 'enabled'=>1, 'visible'=>-1, 'position'=>255),
  215. 'customer_bad' =>array('type'=>'tinyint(4)', 'label'=>'Customer bad', 'enabled'=>1, 'visible'=>-1, 'position'=>260),
  216. 'customer_rate' =>array('type'=>'double', 'label'=>'Customer rate', 'enabled'=>1, 'visible'=>-1, 'position'=>265),
  217. 'supplier_rate' =>array('type'=>'double', 'label'=>'Supplier rate', 'enabled'=>1, 'visible'=>-1, 'position'=>270),
  218. 'fk_user_creat' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'position'=>275),
  219. 'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>280),
  220. //'remise_client' =>array('type'=>'double', 'label'=>'CustomerDiscount', 'enabled'=>1, 'visible'=>-1, 'position'=>285, 'isameasure'=>1),
  221. //'remise_supplier' =>array('type'=>'double', 'label'=>'SupplierDiscount', 'enabled'=>1, 'visible'=>-1, 'position'=>290, 'isameasure'=>1),
  222. 'mode_reglement' =>array('type'=>'tinyint(4)', 'label'=>'Mode reglement', 'enabled'=>1, 'visible'=>-1, 'position'=>295),
  223. 'cond_reglement' =>array('type'=>'tinyint(4)', 'label'=>'Cond reglement', 'enabled'=>1, 'visible'=>-1, 'position'=>300),
  224. 'deposit_percent' =>array('type'=>'varchar(63)', 'label'=>'DepositPercent', 'enabled'=>1, 'visible'=>-1, 'position'=>301),
  225. 'mode_reglement_supplier' =>array('type'=>'integer', 'label'=>'Mode reglement supplier', 'enabled'=>1, 'visible'=>-1, 'position'=>305),
  226. 'cond_reglement_supplier' =>array('type'=>'integer', 'label'=>'Cond reglement supplier', 'enabled'=>1, 'visible'=>-1, 'position'=>308),
  227. 'outstanding_limit' =>array('type'=>'double(24,8)', 'label'=>'OutstandingBill', 'enabled'=>1, 'visible'=>-1, 'position'=>310, 'isameasure'=>1),
  228. 'order_min_amount' =>array('type'=>'double(24,8)', 'label'=>'Order min amount', 'enabled'=>'isModEnabled("commande") && !empty($conf->global->ORDER_MANAGE_MIN_AMOUNT)', 'visible'=>-1, 'position'=>315, 'isameasure'=>1),
  229. 'supplier_order_min_amount' =>array('type'=>'double(24,8)', 'label'=>'Supplier order min amount', 'enabled'=>'isModEnabled("commande") && !empty($conf->global->ORDER_MANAGE_MIN_AMOUNT)', 'visible'=>-1, 'position'=>320, 'isameasure'=>1),
  230. 'fk_shipping_method' =>array('type'=>'integer', 'label'=>'Fk shipping method', 'enabled'=>1, 'visible'=>-1, 'position'=>330),
  231. 'tva_assuj' =>array('type'=>'tinyint(4)', 'label'=>'Tva assuj', 'enabled'=>1, 'visible'=>-1, 'position'=>335),
  232. 'localtax1_assuj' =>array('type'=>'tinyint(4)', 'label'=>'Localtax1 assuj', 'enabled'=>1, 'visible'=>-1, 'position'=>340),
  233. 'localtax1_value' =>array('type'=>'double(6,3)', 'label'=>'Localtax1 value', 'enabled'=>1, 'visible'=>-1, 'position'=>345),
  234. 'localtax2_assuj' =>array('type'=>'tinyint(4)', 'label'=>'Localtax2 assuj', 'enabled'=>1, 'visible'=>-1, 'position'=>350),
  235. 'localtax2_value' =>array('type'=>'double(6,3)', 'label'=>'Localtax2 value', 'enabled'=>1, 'visible'=>-1, 'position'=>355),
  236. 'vat_reverse_charge' =>array('type'=>'tinyint(4)', 'label'=>'Vat reverse charge', 'enabled'=>1, 'visible'=>-1, 'position'=>335),
  237. 'barcode' =>array('type'=>'varchar(255)', 'label'=>'Barcode', 'enabled'=>1, 'visible'=>-1, 'position'=>360),
  238. 'price_level' =>array('type'=>'integer', 'label'=>'Price level', 'enabled'=>'$conf->global->PRODUIT_MULTIPRICES || $conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES', 'visible'=>-1, 'position'=>365),
  239. 'default_lang' =>array('type'=>'varchar(6)', 'label'=>'Default lang', 'enabled'=>1, 'visible'=>-1, 'position'=>370),
  240. 'canvas' =>array('type'=>'varchar(32)', 'label'=>'Canvas', 'enabled'=>1, 'visible'=>-1, 'position'=>375),
  241. 'fk_barcode_type' =>array('type'=>'integer', 'label'=>'Fk barcode type', 'enabled'=>1, 'visible'=>-1, 'position'=>405),
  242. 'webservices_url' =>array('type'=>'varchar(255)', 'label'=>'Webservices url', 'enabled'=>1, 'visible'=>-1, 'position'=>410),
  243. 'webservices_key' =>array('type'=>'varchar(128)', 'label'=>'Webservices key', 'enabled'=>1, 'visible'=>-1, 'position'=>415),
  244. 'fk_incoterms' =>array('type'=>'integer', 'label'=>'Fk incoterms', 'enabled'=>1, 'visible'=>-1, 'position'=>425),
  245. 'location_incoterms' =>array('type'=>'varchar(255)', 'label'=>'Location incoterms', 'enabled'=>1, 'visible'=>-1, 'position'=>430),
  246. 'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>435),
  247. 'last_main_doc' =>array('type'=>'varchar(255)', 'label'=>'LastMainDoc', 'enabled'=>1, 'visible'=>-1, 'position'=>270),
  248. 'fk_multicurrency' =>array('type'=>'integer', 'label'=>'Fk multicurrency', 'enabled'=>1, 'visible'=>-1, 'position'=>440),
  249. 'multicurrency_code' =>array('type'=>'varchar(255)', 'label'=>'Multicurrency code', 'enabled'=>1, 'visible'=>-1, 'position'=>445),
  250. 'fk_account' =>array('type'=>'integer', 'label'=>'PaymentBankAccount', 'enabled'=>1, 'visible'=>-1, 'position'=>450),
  251. 'fk_warehouse' =>array('type'=>'integer', 'label'=>'Warehouse', 'enabled'=>1, 'visible'=>-1, 'position'=>455),
  252. 'logo' =>array('type'=>'varchar(255)', 'label'=>'Logo', 'enabled'=>1, 'visible'=>-1, 'position'=>400),
  253. 'logo_squarred' =>array('type'=>'varchar(255)', 'label'=>'Logo squarred', 'enabled'=>1, 'visible'=>-1, 'position'=>401),
  254. 'status' =>array('type'=>'tinyint(4)', 'label'=>'Status', 'enabled'=>1, 'visible'=>-1, 'position'=>500),
  255. 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000),
  256. );
  257. /**
  258. * @var int Entity
  259. */
  260. public $entity;
  261. /**
  262. * Thirdparty name
  263. * @var string
  264. * @deprecated Use $name instead
  265. * @see $name
  266. */
  267. public $nom;
  268. /**
  269. * @var string Thirdparty name
  270. */
  271. public $name;
  272. /**
  273. * Alias names (commercial, trademark or alias names)
  274. * @var string
  275. */
  276. public $name_alias;
  277. /**
  278. * @var int Physical thirdparty not a company
  279. */
  280. public $particulier;
  281. /**
  282. * Thirdparty status : 0=activity ceased, 1= in activity
  283. * @var int
  284. */
  285. public $status = 1;
  286. /**
  287. * Id of region
  288. * @var int
  289. */
  290. public $region_code;
  291. /**
  292. * @var string Region name
  293. */
  294. public $region;
  295. /**
  296. * @var int country_id
  297. */
  298. public $country_id;
  299. /**
  300. * @var string State code
  301. * @deprecated Use state_code instead
  302. * @see $state_code
  303. */
  304. public $departement_code;
  305. /**
  306. * @var string
  307. * @deprecated Use state instead
  308. * @see $state
  309. */
  310. public $departement;
  311. /**
  312. * @var string
  313. * @deprecated Use country instead
  314. * @see $country
  315. */
  316. public $pays;
  317. /**
  318. * Phone number
  319. * @var string
  320. */
  321. public $phone;
  322. /**
  323. * Fax number
  324. * @var string
  325. */
  326. public $fax;
  327. /**
  328. * Email
  329. * @var string
  330. */
  331. public $email;
  332. /**
  333. * No Email
  334. * @var int Set if company email found into unsubscribe of emailing list table
  335. */
  336. public $no_email;
  337. /**
  338. * Skype username
  339. * @var string
  340. * @deprecated
  341. */
  342. public $skype;
  343. /**
  344. * Twitter username
  345. * @var string
  346. * @deprecated
  347. */
  348. public $twitter;
  349. /**
  350. * Facebook username
  351. * @var string
  352. * @deprecated
  353. */
  354. public $facebook;
  355. /**
  356. * LinkedIn username
  357. * @var string
  358. * @deprecated
  359. */
  360. public $linkedin;
  361. /**
  362. * Webpage
  363. * @var string
  364. */
  365. public $url;
  366. /**
  367. * Barcode value
  368. * @var string
  369. */
  370. public $barcode;
  371. // 6 professional id (usage depends on country)
  372. /**
  373. * Professional ID 1 (Ex: Siren in France)
  374. * @var string
  375. */
  376. public $idprof1;
  377. /**
  378. * @var string Professional ID 1
  379. * @deprecated
  380. * @see $idprof1
  381. */
  382. public $siren;
  383. /**
  384. * Professional ID 2 (Ex: Siret in France)
  385. * @var string
  386. */
  387. public $idprof2;
  388. /**
  389. * @var string Professional ID 2
  390. * @deprecated
  391. * @see $idprof2
  392. */
  393. public $siret;
  394. /**
  395. * Professional ID 3 (Ex: Ape in France)
  396. * @var string
  397. */
  398. public $idprof3;
  399. /**
  400. * @var string Professional ID 3
  401. * @deprecated
  402. * @see $idprof3
  403. */
  404. public $ape;
  405. /**
  406. * Professional ID 4 (Ex: RCS in France)
  407. * @var string
  408. */
  409. public $idprof4;
  410. /**
  411. * Professional ID 5
  412. * @var string
  413. */
  414. public $idprof5;
  415. /**
  416. * Professional ID 6
  417. * @var string
  418. */
  419. public $idprof6;
  420. /**
  421. * Social object of the company
  422. * @var string
  423. */
  424. public $socialobject;
  425. /**
  426. * @var string Prefix comm
  427. */
  428. public $prefix_comm;
  429. /**
  430. * @var int Vat concerned
  431. */
  432. public $tva_assuj = 1;
  433. /**
  434. * Intracommunitary VAT ID
  435. * @var string
  436. */
  437. public $tva_intra;
  438. /**
  439. * @var int Vat reverse-charge concerned
  440. */
  441. public $vat_reverse_charge = 0;
  442. // Local taxes
  443. public $localtax1_assuj;
  444. public $localtax1_value;
  445. public $localtax2_assuj;
  446. public $localtax2_value;
  447. /**
  448. * @var string Manager
  449. */
  450. public $managers;
  451. /**
  452. * @var float Capital
  453. */
  454. public $capital;
  455. /**
  456. * @var int Type thirdparty
  457. */
  458. public $typent_id = 0;
  459. public $typent_code;
  460. public $effectif;
  461. public $effectif_id = 0;
  462. public $forme_juridique_code;
  463. public $forme_juridique = 0;
  464. public $remise_percent;
  465. public $remise_supplier_percent;
  466. public $mode_reglement_id;
  467. public $cond_reglement_id;
  468. public $deposit_percent;
  469. public $mode_reglement_supplier_id;
  470. public $cond_reglement_supplier_id;
  471. public $transport_mode_supplier_id;
  472. /**
  473. * @var int ID
  474. */
  475. public $fk_prospectlevel;
  476. /**
  477. * @var string second name
  478. */
  479. public $name_bis;
  480. //Log data
  481. /**
  482. * Date of last update
  483. * @var integer|string
  484. */
  485. public $date_modification;
  486. /**
  487. * User that made last update
  488. * @var User
  489. * @deprecated
  490. */
  491. public $user_modification;
  492. /**
  493. * Date of creation
  494. * @var integer|string
  495. */
  496. public $date_creation;
  497. /**
  498. * User that created the thirdparty
  499. * @var User
  500. * @deprecated
  501. */
  502. public $user_creation;
  503. /**
  504. * 0=no customer, 1=customer, 2=prospect, 3=customer and prospect
  505. * @var int
  506. */
  507. public $client = 0;
  508. /**
  509. * 0=no prospect, 1=prospect
  510. * @var int
  511. */
  512. public $prospect = 0;
  513. /**
  514. * 0=no supplier, 1=supplier
  515. * @var int
  516. */
  517. public $fournisseur;
  518. /**
  519. * Client code. E.g: CU2014-003
  520. * @var string
  521. */
  522. public $code_client;
  523. /**
  524. * Supplier code. E.g: SU2014-003
  525. * @var string
  526. */
  527. public $code_fournisseur;
  528. /**
  529. * Accounting code for client
  530. * @var string
  531. */
  532. public $code_compta_client;
  533. /**
  534. * Duplicate of code_compta_client (for backward compatibility)
  535. * @var string
  536. * @deprecated
  537. * @see $code_compta_client
  538. */
  539. public $code_compta;
  540. /**
  541. * Accounting code for customer
  542. * @var string
  543. */
  544. public $accountancy_code_customer;
  545. /**
  546. * Accounting code for supplier
  547. * @var string
  548. */
  549. public $code_compta_fournisseur;
  550. /**
  551. * Accounting code for supplier
  552. * @var string
  553. */
  554. public $accountancy_code_supplier;
  555. /**
  556. * Accounting code for product (for level 3 of suggestion of prouct accounting account)
  557. * @var string
  558. */
  559. public $code_compta_product;
  560. /**
  561. * @var string
  562. * @deprecated Note is split in public and private notes
  563. * @see $note_public, $note_private
  564. */
  565. public $note;
  566. /**
  567. * Private note
  568. * @var string
  569. */
  570. public $note_private;
  571. /**
  572. * Public note
  573. * @var string
  574. */
  575. public $note_public;
  576. /**
  577. * Status prospect id
  578. * @var int
  579. */
  580. public $stcomm_id;
  581. /**
  582. * Status prospect picto
  583. * @var string
  584. */
  585. public $stcomm_picto;
  586. /**
  587. * Status prospect label
  588. * @var int
  589. */
  590. public $status_prospect_label;
  591. /**
  592. * Assigned price level
  593. * @var int
  594. */
  595. public $price_level;
  596. /**
  597. * @var string outstanding limit
  598. */
  599. public $outstanding_limit;
  600. /**
  601. * @var string Min order amount
  602. */
  603. public $order_min_amount;
  604. /**
  605. * @var string Supplier min order amount
  606. */
  607. public $supplier_order_min_amount;
  608. /**
  609. * Id of sales representative to link (used for thirdparty creation). Not filled by a fetch, because we can have several sales representatives.
  610. * @var int
  611. */
  612. public $commercial_id;
  613. /**
  614. * Id of parent thirdparty (if one)
  615. * @var int
  616. */
  617. public $parent;
  618. /**
  619. * Default language code of thirdparty (en_US, ...)
  620. * @var string
  621. */
  622. public $default_lang;
  623. /**
  624. * @var string Ref
  625. */
  626. public $ref;
  627. /**
  628. * External user reference.
  629. * This is to allow external systems to store their id and make self-developed synchronizing functions easier to build.
  630. * @var string
  631. */
  632. public $ref_ext;
  633. /**
  634. * Import key.
  635. * Set when the thirdparty has been created through an import process. This is to relate those created thirdparties
  636. * to an import process
  637. * @var string
  638. */
  639. public $import_key;
  640. /**
  641. * Supplier WebServices URL
  642. * @var string
  643. */
  644. public $webservices_url;
  645. /**
  646. * Supplier WebServices Key
  647. * @var string
  648. */
  649. public $webservices_key;
  650. /**
  651. * @var string Logo
  652. */
  653. public $logo;
  654. /**
  655. * @var string logo small
  656. */
  657. public $logo_small;
  658. /**
  659. * @var string Logo mini
  660. */
  661. public $logo_mini;
  662. /**
  663. * @var string Logo squarred
  664. */
  665. public $logo_squarred;
  666. /**
  667. * @var string Logo squarred small
  668. */
  669. public $logo_squarred_small;
  670. /**
  671. * @var string Logo squarred mini
  672. */
  673. public $logo_squarred_mini;
  674. /**
  675. * @var string Accountancy account for sales
  676. */
  677. public $accountancy_code_sell;
  678. /**
  679. * @var string Accountancy account for bought
  680. */
  681. public $accountancy_code_buy;
  682. // Multicurrency
  683. /**
  684. * @var int ID
  685. */
  686. public $fk_multicurrency;
  687. // Warehouse
  688. /**
  689. * @var int ID
  690. */
  691. public $fk_warehouse;
  692. /**
  693. * @var string Multicurrency code
  694. */
  695. public $multicurrency_code;
  696. // Fields loaded by fetchPartnerships()
  697. public $partnerships = array();
  698. /**
  699. * @var Account|string Default BAN account
  700. */
  701. public $bank_account;
  702. const STATUS_CEASED = 0;
  703. const STATUS_INACTIVITY = 1;
  704. /**
  705. * Third party type is no customer
  706. */
  707. const NO_CUSTOMER = 0;
  708. /**
  709. * Third party type is a customer
  710. */
  711. const CUSTOMER = 1;
  712. /**
  713. * Third party type is a prospect
  714. */
  715. const PROSPECT = 2;
  716. /**
  717. * Third party type is a customer and a prospect
  718. */
  719. const CUSTOMER_AND_PROSPECT = 3;
  720. /**
  721. * Third party supplier flag is not supplier
  722. */
  723. const NO_SUPPLIER = 0;
  724. /**
  725. * Third party supplier flag is a supplier
  726. */
  727. const SUPPLIER = 1;
  728. /**
  729. * Constructor
  730. *
  731. * @param DoliDB $db Database handler
  732. */
  733. public function __construct($db)
  734. {
  735. global $conf;
  736. $this->db = $db;
  737. $this->client = 0;
  738. $this->prospect = 0;
  739. $this->fournisseur = 0;
  740. $this->typent_id = 0;
  741. $this->effectif_id = 0;
  742. $this->forme_juridique_code = 0;
  743. $this->tva_assuj = 1;
  744. $this->vat_reverse_charge = 0;
  745. $this->status = 1;
  746. if (getDolGlobalString('COMPANY_SHOW_ADDRESS_SELECTLIST')) {
  747. $this->fields['address']['showoncombobox'] = $conf->global->COMPANY_SHOW_ADDRESS_SELECTLIST;
  748. $this->fields['zip']['showoncombobox'] = $conf->global->COMPANY_SHOW_ADDRESS_SELECTLIST;
  749. $this->fields['town']['showoncombobox'] = $conf->global->COMPANY_SHOW_ADDRESS_SELECTLIST;
  750. //$this->fields['fk_pays']['showoncombobox'] = $conf->global->COMPANY_SHOW_ADDRESS_SELECTLIST;
  751. }
  752. }
  753. /**
  754. * Create third party in database.
  755. * $this->code_client = -1 and $this->code_fournisseur = -1 means automatic assignement.
  756. *
  757. * @param User $user Object of user that ask creation
  758. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  759. * @return int >=0 if OK, <0 if KO
  760. */
  761. public function create(User $user, $notrigger = 0)
  762. {
  763. global $langs, $conf, $mysoc;
  764. $error = 0;
  765. // Clean parameters
  766. if (empty($this->status)) {
  767. $this->status = 0;
  768. }
  769. $this->name = $this->name ?trim($this->name) : trim($this->nom);
  770. $this->setUpperOrLowerCase();
  771. $this->nom = $this->name; // For backward compatibility
  772. if (empty($this->client)) {
  773. $this->client = 0;
  774. }
  775. if (empty($this->fournisseur)) {
  776. $this->fournisseur = 0;
  777. }
  778. $this->import_key = trim($this->import_key);
  779. $this->accountancy_code_customer = trim($this->code_compta);
  780. $this->accountancy_code_supplier = trim($this->code_compta_fournisseur);
  781. $this->accountancy_code_buy = trim($this->accountancy_code_buy);
  782. $this->accountancy_code_sell = trim($this->accountancy_code_sell);
  783. if (!empty($this->multicurrency_code)) {
  784. $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code);
  785. }
  786. if (empty($this->fk_multicurrency)) {
  787. $this->multicurrency_code = '';
  788. $this->fk_multicurrency = 0;
  789. }
  790. dol_syslog(get_class($this)."::create ".$this->name);
  791. $now = dol_now();
  792. $this->db->begin();
  793. // For automatic creation during create action (not used by Dolibarr GUI, can be used by scripts)
  794. if ($this->code_client == -1 || $this->code_client === 'auto') {
  795. $this->get_codeclient($this, 0);
  796. }
  797. if ($this->code_fournisseur == -1 || $this->code_fournisseur === 'auto') {
  798. $this->get_codefournisseur($this, 1);
  799. }
  800. // Check more parameters (including mandatory setup
  801. // If error, this->errors[] is filled
  802. $result = $this->verify();
  803. if ($result >= 0) {
  804. $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
  805. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe (";
  806. $sql .= "nom";
  807. $sql .= ", name_alias";
  808. $sql .= ", entity";
  809. $sql .= ", datec";
  810. $sql .= ", fk_user_creat";
  811. $sql .= ", fk_typent";
  812. $sql .= ", canvas";
  813. $sql .= ", status";
  814. $sql .= ", ref_ext";
  815. $sql .= ", fk_stcomm";
  816. $sql .= ", fk_incoterms";
  817. $sql .= ", location_incoterms";
  818. $sql .= ", import_key";
  819. $sql .= ", fk_multicurrency";
  820. $sql .= ", multicurrency_code";
  821. if (!getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
  822. $sql .= ", vat_reverse_charge";
  823. $sql .= ", accountancy_code_buy";
  824. $sql .= ", accountancy_code_sell";
  825. }
  826. $sql .= ") VALUES ('".$this->db->escape($this->name)."', '".$this->db->escape($this->name_alias)."', ".((int) $this->entity).", '".$this->db->idate($now)."'";
  827. $sql .= ", ".(!empty($user->id) ? ((int) $user->id) : "null");
  828. $sql .= ", ".(!empty($this->typent_id) ? ((int) $this->typent_id) : "null");
  829. $sql .= ", ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null");
  830. $sql .= ", ".((int) $this->status);
  831. $sql .= ", ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null");
  832. $sql .= ", 0";
  833. $sql .= ", ".(int) $this->fk_incoterms;
  834. $sql .= ", '".$this->db->escape($this->location_incoterms)."'";
  835. $sql .= ", ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
  836. $sql .= ", ".(int) $this->fk_multicurrency;
  837. $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
  838. if (!getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
  839. $sql .= ", ".(empty($this->vat_reverse_charge) ? '0' : '1');
  840. $sql .= ", '" . $this->db->escape($this->accountancy_code_buy) . "'";
  841. $sql .= ", '" . $this->db->escape($this->accountancy_code_sell) . "'";
  842. }
  843. $sql .= ")";
  844. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  845. $result = $this->db->query($sql);
  846. if ($result) {
  847. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."societe");
  848. $ret = $this->update($this->id, $user, 0, 1, 1, 'add');
  849. // update accountancy for this entity
  850. if (!$error && getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
  851. $this->db->query("DELETE FROM ".MAIN_DB_PREFIX."societe_perentity WHERE fk_soc = ".((int) $this->id)." AND entity = ".((int) $conf->entity));
  852. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_perentity (";
  853. $sql .= " fk_soc";
  854. $sql .= ", entity";
  855. $sql .= ", vat_reverse_charge";
  856. $sql .= ", accountancy_code_customer";
  857. $sql .= ", accountancy_code_supplier";
  858. $sql .= ", accountancy_code_buy";
  859. $sql .= ", accountancy_code_sell";
  860. $sql .= ") VALUES (";
  861. $sql .= $this->id;
  862. $sql .= ", ".((int) $conf->entity);
  863. $sql .= ", ".(empty($this->vat_reverse_charge) ? '0' : '1');
  864. $sql .= ", '".$this->db->escape($this->accountancy_code_customer)."'";
  865. $sql .= ", '".$this->db->escape($this->accountancy_code_supplier)."'";
  866. $sql .= ", '".$this->db->escape($this->accountancy_code_buy)."'";
  867. $sql .= ", '".$this->db->escape($this->accountancy_code_sell)."'";
  868. $sql .= ")";
  869. $result = $this->db->query($sql);
  870. if (!$result) {
  871. $error++;
  872. $this->error = 'ErrorFailedToUpdateAccountancyForEntity';
  873. }
  874. }
  875. // Ajout du commercial affecte
  876. if ($this->commercial_id != '' && $this->commercial_id != -1) {
  877. $this->add_commercial($user, $this->commercial_id);
  878. } elseif (!$user->hasRight('societe', 'client', 'voir')) {
  879. // si un commercial cree un client il lui est affecte automatiquement
  880. $this->add_commercial($user, $user->id);
  881. }
  882. if ($ret >= 0) {
  883. if (! $notrigger) {
  884. // Call trigger
  885. $result = $this->call_trigger('COMPANY_CREATE', $user);
  886. if ($result < 0) {
  887. $error++;
  888. }
  889. // End call triggers
  890. }
  891. } else {
  892. $error++;
  893. }
  894. if (!$error) {
  895. dol_syslog(get_class($this)."::Create success id=".$this->id);
  896. $this->db->commit();
  897. return $this->id;
  898. } else {
  899. dol_syslog(get_class($this)."::Create echec update ".$this->error.(empty($this->errors) ? '' : ' '.join(',', $this->errors)), LOG_ERR);
  900. $this->db->rollback();
  901. return -4;
  902. }
  903. } else {
  904. if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  905. $this->error = $langs->trans("ErrorCompanyNameAlreadyExists", $this->name); // duplicate on a field (code or profid or ...)
  906. $result = -1;
  907. } else {
  908. $this->error = $this->db->lasterror();
  909. $result = -2;
  910. }
  911. $this->db->rollback();
  912. return $result;
  913. }
  914. } else {
  915. $this->db->rollback();
  916. dol_syslog(get_class($this)."::Create fails verify ".join(',', $this->errors), LOG_WARNING);
  917. return -3;
  918. }
  919. }
  920. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  921. /**
  922. * Create a contact/address from thirdparty
  923. *
  924. * @param User $user Object user
  925. * @param int $no_email 1=Do not send mailing, 0=Ok to recieve mailling
  926. * @param array $tags Array of tag to affect to contact
  927. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  928. * @return int <0 if KO, >0 if OK
  929. */
  930. public function create_individual(User $user, $no_email = 0, $tags = array(), $notrigger = 0)
  931. {
  932. global $conf;
  933. $error = 0;
  934. $this->db->begin();
  935. // phpcs:enable
  936. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  937. $contact = new Contact($this->db);
  938. $contact->name = $this->name_bis;
  939. $contact->firstname = $this->firstname;
  940. $contact->civility_id = $this->civility_id;
  941. $contact->socid = $this->id; // fk_soc
  942. $contact->statut = 1; // deprecated
  943. $contact->status = 1;
  944. $contact->priv = 0;
  945. $contact->country_id = $this->country_id;
  946. $contact->state_id = $this->state_id;
  947. $contact->address = $this->address;
  948. $contact->email = $this->email;
  949. $contact->zip = $this->zip;
  950. $contact->town = $this->town;
  951. $this->setUpperOrLowerCase();
  952. $contact->phone_pro = $this->phone;
  953. $contactId = $contact->create($user, $notrigger);
  954. if ($contactId < 0) {
  955. $error++;
  956. $this->error = $contact->error;
  957. $this->errors = $contact->errors;
  958. dol_syslog(get_class($this)."::create_individual ERROR:".$this->error, LOG_ERR);
  959. }
  960. if (empty($error) && is_array($tags) && !empty($tags)) {
  961. $result = $contact->setCategories($tags);
  962. if ($result < 0) {
  963. $error++;
  964. $this->error = $contact->error;
  965. $this->errors = array_merge($this->errors, $contact->errors);
  966. dol_syslog(get_class($this)."::create_individual Affect Tag ERROR:".$this->error, LOG_ERR);
  967. $contactId = $result;
  968. }
  969. }
  970. if (empty($error) && isModEnabled('mailing') && !empty($contact->email) && isset($no_email)) {
  971. $result = $contact->setNoEmail($no_email);
  972. if ($result < 0) {
  973. $this->error = $contact->error;
  974. $this->errors = array_merge($this->errors, $contact->errors);
  975. dol_syslog(get_class($this)."::create_individual set mailing status ERROR:".$this->error, LOG_ERR);
  976. $contactId = $result;
  977. }
  978. }
  979. if (empty($error)) {
  980. dol_syslog(get_class($this)."::create_individual success");
  981. $this->db->commit();
  982. } else {
  983. $this->db->rollback();
  984. }
  985. return $contactId;
  986. }
  987. /**
  988. * Check properties of third party are ok (like name, third party codes, ...)
  989. * Used before an add or update.
  990. *
  991. * @return int 0 if OK, <0 if KO
  992. */
  993. public function verify()
  994. {
  995. global $conf, $langs, $mysoc;
  996. $error = 0;
  997. $this->errors = array();
  998. $result = 0;
  999. $this->name = trim($this->name);
  1000. $this->nom = $this->name; // For backward compatibility
  1001. if (!$this->name) {
  1002. $this->errors[] = 'ErrorBadThirdPartyName';
  1003. $result = -2;
  1004. }
  1005. if ($this->client) {
  1006. $rescode = $this->check_codeclient();
  1007. if ($rescode != 0 && $rescode != -5) {
  1008. if ($rescode == -1) {
  1009. $this->errors[] = 'ErrorBadCustomerCodeSyntax';
  1010. } elseif ($rescode == -2) {
  1011. $this->errors[] = 'ErrorCustomerCodeRequired';
  1012. } elseif ($rescode == -3) {
  1013. $this->errors[] = 'ErrorCustomerCodeAlreadyUsed';
  1014. } elseif ($rescode == -4) {
  1015. $this->errors[] = 'ErrorPrefixRequired';
  1016. } else {
  1017. $this->errors[] = 'ErrorUnknownOnCustomerCodeCheck';
  1018. }
  1019. $result = -3;
  1020. }
  1021. }
  1022. if ($this->fournisseur) {
  1023. $rescode = $this->check_codefournisseur();
  1024. if ($rescode != 0 && $rescode != -5) {
  1025. if ($rescode == -1) {
  1026. $this->errors[] = 'ErrorBadSupplierCodeSyntax';
  1027. } elseif ($rescode == -2) {
  1028. $this->errors[] = 'ErrorSupplierCodeRequired';
  1029. } elseif ($rescode == -3) {
  1030. $this->errors[] = 'ErrorSupplierCodeAlreadyUsed';
  1031. } elseif ($rescode == -4) {
  1032. $this->errors[] = 'ErrorPrefixRequired';
  1033. } else {
  1034. $this->errors[] = 'ErrorUnknownOnSupplierCodeCheck';
  1035. }
  1036. $result = -3;
  1037. }
  1038. }
  1039. // Check for duplicate or mandatory fields defined into setup
  1040. $array_to_check = array('IDPROF1', 'IDPROF2', 'IDPROF3', 'IDPROF4', 'IDPROF5', 'IDPROF6', 'EMAIL', 'TVA_INTRA', 'ACCOUNTANCY_CODE_CUSTOMER', 'ACCOUNTANCY_CODE_SUPPLIER');
  1041. foreach ($array_to_check as $key) {
  1042. $keymin = strtolower($key);
  1043. if ($key == 'ACCOUNTANCY_CODE_CUSTOMER') $keymin = 'code_compta';
  1044. elseif ($key == 'ACCOUNTANCY_CODE_SUPPLIER') $keymin = 'code_compta_fournisseur';
  1045. $i = (int) preg_replace('/[^0-9]/', '', $key);
  1046. $vallabel = $this->$keymin;
  1047. if ($i > 0) {
  1048. if ($this->isACompany()) {
  1049. // Check for mandatory prof id (but only if country is same than ours)
  1050. if ($mysoc->country_id > 0 && $this->country_id == $mysoc->country_id) {
  1051. $idprof_mandatory = 'SOCIETE_'.$key.'_MANDATORY';
  1052. if (!$vallabel && getDolGlobalString($idprof_mandatory)) {
  1053. $langs->load("errors");
  1054. $error++;
  1055. $this->errors[] = $langs->trans("ErrorProdIdIsMandatory", $langs->transcountry('ProfId'.$i, $this->country_code)).' ('.$langs->trans("ForbiddenBySetupRules").')';
  1056. }
  1057. }
  1058. }
  1059. // Check for unicity on profid
  1060. if (!$error && $vallabel && $this->id_prof_verifiable($i)) {
  1061. if ($this->id_prof_exists($keymin, $vallabel, ($this->id > 0 ? $this->id : 0))) {
  1062. $langs->load("errors");
  1063. $error++;
  1064. $this->errors[] = $langs->transcountry('ProfId'.$i, $this->country_code)." ".$langs->trans("ErrorProdIdAlreadyExist", $vallabel).' ('.$langs->trans("ForbiddenBySetupRules").')';
  1065. }
  1066. }
  1067. } else {
  1068. //var_dump($conf->global->SOCIETE_EMAIL_UNIQUE);
  1069. //var_dump($conf->global->SOCIETE_EMAIL_MANDATORY);
  1070. if ($key == 'EMAIL') {
  1071. // Check for mandatory
  1072. if (getDolGlobalString('SOCIETE_EMAIL_MANDATORY') && !isValidEMail($this->email)) {
  1073. $langs->load("errors");
  1074. $error++;
  1075. $this->errors[] = $langs->trans("ErrorBadEMail", $this->email).' ('.$langs->trans("ForbiddenBySetupRules").')';
  1076. }
  1077. // Check for unicity
  1078. if (!$error && $vallabel && getDolGlobalString('SOCIETE_EMAIL_UNIQUE')) {
  1079. if ($this->id_prof_exists($keymin, $vallabel, ($this->id > 0 ? $this->id : 0))) {
  1080. $langs->load("errors");
  1081. $error++; $this->errors[] = $langs->trans('Email')." ".$langs->trans("ErrorProdIdAlreadyExist", $vallabel).' ('.$langs->trans("ForbiddenBySetupRules").')';
  1082. }
  1083. }
  1084. } elseif ($key == 'TVA_INTRA') {
  1085. // Check for unicity
  1086. if ($vallabel && getDolGlobalString('SOCIETE_VAT_INTRA_UNIQUE')) {
  1087. if ($this->id_prof_exists($keymin, $vallabel, ($this->id > 0 ? $this->id : 0))) {
  1088. $langs->load("errors");
  1089. $error++; $this->errors[] = $langs->trans('VATIntra')." ".$langs->trans("ErrorProdIdAlreadyExist", $vallabel).' ('.$langs->trans("ForbiddenBySetupRules").')';
  1090. }
  1091. }
  1092. } elseif ($key == 'ACCOUNTANCY_CODE_CUSTOMER' && !empty($this->client)) {
  1093. // Check for unicity
  1094. if ($vallabel && getDolGlobalString('SOCIETE_ACCOUNTANCY_CODE_CUSTOMER_UNIQUE')) {
  1095. if ($this->id_prof_exists($keymin, $vallabel, ($this->id > 0 ? $this->id : 0))) {
  1096. $langs->loadLangs(array("errors", 'compta'));
  1097. $error++;
  1098. $this->errors[] = $langs->trans('CustomerAccountancyCodeShort') . " " . $langs->trans("ErrorProdIdAlreadyExist", $vallabel) . ' (' . $langs->trans("ForbiddenBySetupRules") . ')';
  1099. }
  1100. }
  1101. // Check for mandatory
  1102. if (getDolGlobalString('SOCIETE_ACCOUNTANCY_CODE_CUSTOMER_MANDATORY') && (!isset($vallabel) || trim($vallabel) === '')) {
  1103. $langs->loadLangs(array("errors", 'compta'));
  1104. $error++;
  1105. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv('CustomerAccountancyCodeShort')) . ' (' . $langs->trans("ForbiddenBySetupRules") . ')';
  1106. }
  1107. } elseif ($key == 'ACCOUNTANCY_CODE_SUPPLIER' && !empty($this->fournisseur)) {
  1108. // Check for unicity
  1109. if ($vallabel && getDolGlobalString('SOCIETE_ACCOUNTANCY_CODE_SUPPLIER_UNIQUE')) {
  1110. if ($this->id_prof_exists($keymin, $vallabel, ($this->id > 0 ? $this->id : 0))) {
  1111. $langs->loadLangs(array("errors", 'compta'));
  1112. $error++;
  1113. $this->errors[] = $langs->trans('SupplierAccountancyCodeShort') . " " . $langs->trans("ErrorProdIdAlreadyExist", $vallabel) . ' (' . $langs->trans("ForbiddenBySetupRules") . ')';
  1114. }
  1115. }
  1116. // Check for mandatory
  1117. if (getDolGlobalString('SOCIETE_ACCOUNTANCY_CODE_SUPPLIER_MANDATORY') && (!isset($vallabel) || trim($vallabel) === '')) {
  1118. $langs->loadLangs(array("errors", 'compta'));
  1119. $error++;
  1120. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv('SupplierAccountancyCodeShort')) . ' (' . $langs->trans("ForbiddenBySetupRules") . ')';
  1121. }
  1122. }
  1123. }
  1124. }
  1125. if ($error) {
  1126. $result = -4;
  1127. }
  1128. return $result;
  1129. }
  1130. /**
  1131. * Update parameters of third party
  1132. *
  1133. * @param int $id Id of company (deprecated, use 0 here and call update on an object loaded by a fetch)
  1134. * @param User $user User who requests the update
  1135. * @param int $call_trigger 0=no, 1=yes
  1136. * @param int $allowmodcodeclient Inclut modif code client et code compta
  1137. * @param int $allowmodcodefournisseur Inclut modif code fournisseur et code compta fournisseur
  1138. * @param string $action 'add' or 'update' or 'merge'
  1139. * @param int $nosyncmember Do not synchronize info of linked member
  1140. * @return int <0 if KO, >=0 if OK
  1141. */
  1142. public function update($id, $user = '', $call_trigger = 1, $allowmodcodeclient = 0, $allowmodcodefournisseur = 0, $action = 'update', $nosyncmember = 1)
  1143. {
  1144. global $langs, $conf, $hookmanager;
  1145. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  1146. if (empty($id)) {
  1147. $id = $this->id;
  1148. }
  1149. $error = 0;
  1150. dol_syslog(get_class($this)."::Update id=".$id." call_trigger=".$call_trigger." allowmodcodeclient=".$allowmodcodeclient." allowmodcodefournisseur=".$allowmodcodefournisseur);
  1151. $now = dol_now();
  1152. // Clean parameters
  1153. $this->id = $id;
  1154. $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);
  1155. $this->name = $this->name ? trim($this->name) : trim($this->nom);
  1156. $this->nom = $this->name; // For backward compatibility
  1157. $this->name_alias = trim($this->name_alias);
  1158. $this->ref_ext = trim($this->ref_ext);
  1159. $this->address = $this->address ?trim($this->address) : trim($this->address);
  1160. $this->zip = $this->zip ?trim($this->zip) : trim($this->zip);
  1161. $this->town = $this->town ?trim($this->town) : trim($this->town);
  1162. $this->state_id = trim($this->state_id);
  1163. $this->country_id = ($this->country_id > 0) ? $this->country_id : 0;
  1164. $this->phone = trim($this->phone);
  1165. $this->phone = preg_replace("/\s/", "", $this->phone);
  1166. $this->phone = preg_replace("/\./", "", $this->phone);
  1167. $this->fax = trim($this->fax);
  1168. $this->fax = preg_replace("/\s/", "", $this->fax);
  1169. $this->fax = preg_replace("/\./", "", $this->fax);
  1170. $this->email = trim($this->email);
  1171. $this->url = $this->url ?clean_url($this->url, 0) : '';
  1172. $this->note_private = trim($this->note_private);
  1173. $this->note_public = trim($this->note_public);
  1174. $this->idprof1 = trim($this->idprof1);
  1175. $this->idprof2 = trim($this->idprof2);
  1176. $this->idprof3 = trim($this->idprof3);
  1177. $this->idprof4 = trim($this->idprof4);
  1178. $this->idprof5 = (!empty($this->idprof5) ?trim($this->idprof5) : '');
  1179. $this->idprof6 = (!empty($this->idprof6) ?trim($this->idprof6) : '');
  1180. $this->prefix_comm = trim($this->prefix_comm);
  1181. $this->outstanding_limit = price2num($this->outstanding_limit);
  1182. $this->order_min_amount = price2num($this->order_min_amount);
  1183. $this->supplier_order_min_amount = price2num($this->supplier_order_min_amount);
  1184. $this->tva_assuj = trim($this->tva_assuj);
  1185. $this->tva_intra = dol_sanitizeFileName($this->tva_intra, '');
  1186. $this->vat_reverse_charge = empty($this->vat_reverse_charge) ? '0' : '1';
  1187. if (empty($this->status)) {
  1188. $this->status = 0;
  1189. }
  1190. if (!empty($this->multicurrency_code)) {
  1191. $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code);
  1192. }
  1193. if (empty($this->fk_multicurrency)) {
  1194. $this->multicurrency_code = '';
  1195. $this->fk_multicurrency = 0;
  1196. }
  1197. // Local taxes
  1198. $this->localtax1_assuj = trim($this->localtax1_assuj);
  1199. $this->localtax2_assuj = trim($this->localtax2_assuj);
  1200. $this->localtax1_value = trim($this->localtax1_value);
  1201. $this->localtax2_value = trim($this->localtax2_value);
  1202. if ($this->capital != '') {
  1203. $this->capital = price2num(trim($this->capital));
  1204. }
  1205. if (!is_numeric($this->capital)) {
  1206. $this->capital = ''; // '' = undef
  1207. }
  1208. $this->effectif_id = trim($this->effectif_id);
  1209. $this->forme_juridique_code = trim($this->forme_juridique_code);
  1210. //Gencod
  1211. $this->barcode = trim($this->barcode);
  1212. // For automatic creation
  1213. if ($this->code_client == -1 || $this->code_client === 'auto') {
  1214. $this->get_codeclient($this, 0);
  1215. }
  1216. if ($this->code_fournisseur == -1 || $this->code_fournisseur === 'auto') {
  1217. $this->get_codefournisseur($this, 1);
  1218. }
  1219. $this->code_compta_client = trim(empty($this->code_compta) ? $this->code_compta_client : $this->code_compta);
  1220. $this->code_compta = $this->code_compta_client; // for backward compatibility
  1221. $this->code_compta_fournisseur = trim($this->code_compta_fournisseur);
  1222. // Check parameters. More tests are done later in the ->verify()
  1223. if (!is_numeric($this->client) && !is_numeric($this->fournisseur)) {
  1224. $langs->load("errors");
  1225. $this->error = $langs->trans("BadValueForParameterClientOrSupplier");
  1226. return -1;
  1227. }
  1228. $customer = false;
  1229. if (!empty($allowmodcodeclient) && !empty($this->client)) {
  1230. // If $allowmodcodeclient is set and value is not set, we generate it
  1231. if (empty($this->code_compta_client)) {
  1232. $ret = $this->get_codecompta('customer');
  1233. if ($ret < 0) {
  1234. return -1;
  1235. }
  1236. }
  1237. $customer = true;
  1238. }
  1239. $supplier = false;
  1240. if (!empty($allowmodcodefournisseur) && !empty($this->fournisseur)) {
  1241. // If $allowmodcodefournisseur is set and value is not set, we generate it
  1242. if (empty($this->code_compta_fournisseur)) {
  1243. $ret = $this->get_codecompta('supplier');
  1244. if ($ret < 0) {
  1245. return -1;
  1246. }
  1247. }
  1248. $supplier = true;
  1249. }
  1250. //Web services
  1251. $this->webservices_url = $this->webservices_url ?clean_url($this->webservices_url, 0) : '';
  1252. $this->webservices_key = trim($this->webservices_key);
  1253. $this->accountancy_code_buy = trim($this->accountancy_code_buy);
  1254. $this->accountancy_code_sell = trim($this->accountancy_code_sell);
  1255. //Incoterms
  1256. $this->fk_incoterms = (int) $this->fk_incoterms;
  1257. $this->location_incoterms = trim($this->location_incoterms);
  1258. $this->db->begin();
  1259. // Check name is required and codes are ok or unique.
  1260. // If error, this->errors[] is filled
  1261. $result = 0;
  1262. if ($action != 'add' && $action != 'merge') {
  1263. // We don't check when update called during a create because verify was already done.
  1264. // For a merge, we suppose source data is clean and a customer code of a deleted thirdparty must be accepted into a target thirdparty with empty code without duplicate error
  1265. $result = $this->verify();
  1266. // If there is only one error and error is ErrorBadCustomerCodeSyntax and we don't change customer code, we allow the update
  1267. // So we can update record that were using and old numbering rule.
  1268. if (is_array($this->errors)) {
  1269. if (in_array('ErrorBadCustomerCodeSyntax', $this->errors) && is_object($this->oldcopy) && $this->oldcopy->code_client == $this->code_client) {
  1270. if (($key = array_search('ErrorBadCustomerCodeSyntax', $this->errors)) !== false) {
  1271. unset($this->errors[$key]); // Remove error message
  1272. }
  1273. }
  1274. if (in_array('ErrorBadSupplierCodeSyntax', $this->errors) && is_object($this->oldcopy) && $this->oldcopy->code_fournisseur == $this->code_fournisseur) {
  1275. if (($key = array_search('ErrorBadSupplierCodeSyntax', $this->errors)) !== false) {
  1276. unset($this->errors[$key]); // Remove error message
  1277. }
  1278. }
  1279. if (empty($this->errors)) { // If there is no more error, we can make like if there is no error at all
  1280. $result = 0;
  1281. }
  1282. }
  1283. }
  1284. $this->setUpperOrLowerCase();
  1285. if ($result >= 0) {
  1286. dol_syslog(get_class($this)."::update verify ok or not done");
  1287. $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET ";
  1288. $sql .= "entity = ".$this->db->escape($this->entity);
  1289. $sql .= ",nom = '".$this->db->escape($this->name)."'"; // Required
  1290. $sql .= ",name_alias = '".$this->db->escape($this->name_alias)."'";
  1291. $sql .= ",ref_ext = ".(!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null");
  1292. $sql .= ",address = '".$this->db->escape($this->address)."'";
  1293. $sql .= ",zip = ".(!empty($this->zip) ? "'".$this->db->escape($this->zip)."'" : "null");
  1294. $sql .= ",town = ".(!empty($this->town) ? "'".$this->db->escape($this->town)."'" : "null");
  1295. $sql .= ",fk_departement = ".((!empty($this->state_id) && $this->state_id > 0) ? ((int) $this->state_id) : 'null');
  1296. $sql .= ",fk_pays = ".((!empty($this->country_id) && $this->country_id > 0) ? ((int) $this->country_id) : 'null');
  1297. $sql .= ",phone = ".(!empty($this->phone) ? "'".$this->db->escape($this->phone)."'" : "null");
  1298. $sql .= ",fax = ".(!empty($this->fax) ? "'".$this->db->escape($this->fax)."'" : "null");
  1299. $sql .= ",email = ".(!empty($this->email) ? "'".$this->db->escape($this->email)."'" : "null");
  1300. $sql .= ",socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";
  1301. $sql .= ",url = ".(!empty($this->url) ? "'".$this->db->escape($this->url)."'" : "null");
  1302. $sql .= ",parent = ".($this->parent > 0 ? $this->parent : "null");
  1303. $sql .= ",note_private = ".(!empty($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null");
  1304. $sql .= ",note_public = ".(!empty($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null");
  1305. $sql .= ",siren = '".$this->db->escape($this->idprof1)."'";
  1306. $sql .= ",siret = '".$this->db->escape($this->idprof2)."'";
  1307. $sql .= ",ape = '".$this->db->escape($this->idprof3)."'";
  1308. $sql .= ",idprof4 = '".$this->db->escape($this->idprof4)."'";
  1309. $sql .= ",idprof5 = '".$this->db->escape($this->idprof5)."'";
  1310. $sql .= ",idprof6 = '".$this->db->escape($this->idprof6)."'";
  1311. $sql .= ",tva_assuj = ".($this->tva_assuj != '' ? "'".$this->db->escape($this->tva_assuj)."'" : "null");
  1312. $sql .= ",tva_intra = '".$this->db->escape($this->tva_intra)."'";
  1313. if (!getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
  1314. $sql .= ",vat_reverse_charge = " . ($this->vat_reverse_charge != '' ? "'" . $this->db->escape($this->vat_reverse_charge) . "'" : 0);
  1315. }
  1316. $sql .= ",status = ".((int) $this->status);
  1317. // Local taxes
  1318. $sql .= ",localtax1_assuj = ".($this->localtax1_assuj != '' ? "'".$this->db->escape($this->localtax1_assuj)."'" : "null");
  1319. $sql .= ",localtax2_assuj = ".($this->localtax2_assuj != '' ? "'".$this->db->escape($this->localtax2_assuj)."'" : "null");
  1320. if ($this->localtax1_assuj == 1) {
  1321. if ($this->localtax1_value != '') {
  1322. $sql .= ",localtax1_value =".$this->localtax1_value;
  1323. } else {
  1324. $sql .= ",localtax1_value =0.000";
  1325. }
  1326. } else {
  1327. $sql .= ",localtax1_value =0.000";
  1328. }
  1329. if ($this->localtax2_assuj == 1) {
  1330. if ($this->localtax2_value != '') {
  1331. $sql .= ",localtax2_value =".$this->localtax2_value;
  1332. } else {
  1333. $sql .= ",localtax2_value =0.000";
  1334. }
  1335. } else {
  1336. $sql .= ",localtax2_value =0.000";
  1337. }
  1338. $sql .= ",capital = ".($this->capital == '' ? "null" : $this->capital);
  1339. $sql .= ",prefix_comm = ".(!empty($this->prefix_comm) ? "'".$this->db->escape($this->prefix_comm)."'" : "null");
  1340. $sql .= ",fk_effectif = ".($this->effectif_id > 0 ? ((int) $this->effectif_id) : "null");
  1341. if (isset($this->stcomm_id)) {
  1342. $sql .= ",fk_stcomm=".(int) $this->stcomm_id;
  1343. }
  1344. if (isset($this->typent_id)) {
  1345. $sql .= ",fk_typent = ".($this->typent_id > 0 ? ((int) $this->typent_id) : "0");
  1346. }
  1347. $sql .= ",fk_forme_juridique = ".(!empty($this->forme_juridique_code) ? "'".$this->db->escape($this->forme_juridique_code)."'" : "null");
  1348. $sql .= ",mode_reglement = ".(!empty($this->mode_reglement_id) ? "'".$this->db->escape($this->mode_reglement_id)."'" : "null");
  1349. $sql .= ",cond_reglement = ".(!empty($this->cond_reglement_id) ? "'".$this->db->escape($this->cond_reglement_id)."'" : "null");
  1350. $sql .= ",deposit_percent = ".(!empty($this->deposit_percent) ? "'".$this->db->escape($this->deposit_percent)."'" : "null");
  1351. $sql .= ",transport_mode = ".(!empty($this->transport_mode_id) ? "'".$this->db->escape($this->transport_mode_id)."'" : "null");
  1352. $sql .= ",mode_reglement_supplier = ".(!empty($this->mode_reglement_supplier_id) ? "'".$this->db->escape($this->mode_reglement_supplier_id)."'" : "null");
  1353. $sql .= ",cond_reglement_supplier = ".(!empty($this->cond_reglement_supplier_id) ? "'".$this->db->escape($this->cond_reglement_supplier_id)."'" : "null");
  1354. $sql .= ",transport_mode_supplier = ".(!empty($this->transport_mode_supplier_id) ? "'".$this->db->escape($this->transport_mode_supplier_id)."'" : "null");
  1355. $sql .= ",fk_shipping_method = ".(!empty($this->shipping_method_id) ? "'".$this->db->escape($this->shipping_method_id)."'" : "null");
  1356. $sql .= ",client = ".(!empty($this->client) ? $this->client : 0);
  1357. $sql .= ",fournisseur = ".(!empty($this->fournisseur) ? $this->fournisseur : 0);
  1358. $sql .= ",barcode = ".(!empty($this->barcode) ? "'".$this->db->escape($this->barcode)."'" : "null");
  1359. $sql .= ",default_lang = ".(!empty($this->default_lang) ? "'".$this->db->escape($this->default_lang)."'" : "null");
  1360. $sql .= ",logo = ".(!empty($this->logo) ? "'".$this->db->escape($this->logo)."'" : "null");
  1361. $sql .= ",logo_squarred = ".(!empty($this->logo_squarred) ? "'".$this->db->escape($this->logo_squarred)."'" : "null");
  1362. $sql .= ",outstanding_limit= ".($this->outstanding_limit != '' ? $this->outstanding_limit : 'null');
  1363. $sql .= ",order_min_amount= ".($this->order_min_amount != '' ? $this->order_min_amount : 'null');
  1364. $sql .= ",supplier_order_min_amount= ".($this->supplier_order_min_amount != '' ? $this->supplier_order_min_amount : 'null');
  1365. $sql .= ",fk_prospectlevel='".$this->db->escape($this->fk_prospectlevel)."'";
  1366. if (!getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
  1367. $sql .= ", accountancy_code_buy = '" . $this->db->escape($this->accountancy_code_buy) . "'";
  1368. $sql .= ", accountancy_code_sell= '" . $this->db->escape($this->accountancy_code_sell) . "'";
  1369. if ($customer) {
  1370. $sql .= ", code_compta = ".(!empty($this->code_compta_client) ? "'".$this->db->escape($this->code_compta_client)."'" : "null");
  1371. }
  1372. if ($supplier) {
  1373. $sql .= ", code_compta_fournisseur = ".(($this->code_compta_fournisseur != "") ? "'".$this->db->escape($this->code_compta_fournisseur)."'" : "null");
  1374. }
  1375. }
  1376. $sql .= ",webservices_url = ".(!empty($this->webservices_url) ? "'".$this->db->escape($this->webservices_url)."'" : "null");
  1377. $sql .= ",webservices_key = ".(!empty($this->webservices_key) ? "'".$this->db->escape($this->webservices_key)."'" : "null");
  1378. //Incoterms
  1379. $sql .= ", fk_incoterms = ".((int) $this->fk_incoterms);
  1380. $sql .= ", location_incoterms = ".(!empty($this->location_incoterms) ? "'".$this->db->escape($this->location_incoterms)."'" : "null");
  1381. if ($customer) {
  1382. $sql .= ", code_client = ".(!empty($this->code_client) ? "'".$this->db->escape($this->code_client)."'" : "null");
  1383. }
  1384. if ($supplier) {
  1385. $sql .= ", code_fournisseur = ".(!empty($this->code_fournisseur) ? "'".$this->db->escape($this->code_fournisseur)."'" : "null");
  1386. }
  1387. $sql .= ", fk_user_modif = ".($user->id > 0 ? $user->id : "null");
  1388. $sql .= ", fk_multicurrency = ".(int) $this->fk_multicurrency;
  1389. $sql .= ", multicurrency_code = '".$this->db->escape($this->multicurrency_code)."'";
  1390. $sql .= ", model_pdf = '".$this->db->escape($this->model_pdf)."'";
  1391. $sql .= " WHERE rowid = ".(int) $id;
  1392. $resql = $this->db->query($sql);
  1393. if ($resql) {
  1394. if (is_object($this->oldcopy)) { // If we have information on old values
  1395. if ($this->oldcopy->country_id != $this->country_id) {
  1396. unset($this->country_code);
  1397. unset($this->country);
  1398. }
  1399. if ($this->oldcopy->state_id != $this->state_id) {
  1400. unset($this->state_code);
  1401. unset($this->state);
  1402. }
  1403. } else {
  1404. unset($this->country_code); // We clean this, in the doubt, because it may have been changed after an update of country_id
  1405. unset($this->country);
  1406. unset($this->state_code);
  1407. unset($this->state);
  1408. }
  1409. $nbrowsaffected = $this->db->affected_rows($resql);
  1410. if (!$error && $nbrowsaffected) {
  1411. // Update information on linked member if it is an update
  1412. if (!$nosyncmember && isModEnabled('adherent')) {
  1413. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  1414. dol_syslog(get_class($this)."::update update linked member");
  1415. $lmember = new Adherent($this->db);
  1416. $result = $lmember->fetch(0, 0, $this->id);
  1417. if ($result > 0) {
  1418. $lmember->company = $this->name;
  1419. //$lmember->firstname=$this->firstname?$this->firstname:$lmember->firstname; // We keep firstname and lastname of member unchanged
  1420. //$lmember->lastname=$this->lastname?$this->lastname:$lmember->lastname; // We keep firstname and lastname of member unchanged
  1421. $lmember->address = $this->address;
  1422. $lmember->zip = $this->zip;
  1423. $lmember->town = $this->town;
  1424. $lmember->email = $this->email;
  1425. $lmember->socialnetworks = $this->socialnetworks;
  1426. $lmember->phone = $this->phone;
  1427. $lmember->state_id = $this->state_id;
  1428. $lmember->country_id = $this->country_id;
  1429. $lmember->default_lang = $this->default_lang;
  1430. $result = $lmember->update($user, 0, 1, 1, 1); // Use nosync to 1 to avoid cyclic updates
  1431. if ($result < 0) {
  1432. $this->error = $lmember->error;
  1433. $this->errors = array_merge($this->errors, $lmember->errors);
  1434. dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
  1435. $error++;
  1436. }
  1437. } elseif ($result < 0) {
  1438. $this->error = $lmember->error;
  1439. $error++;
  1440. }
  1441. }
  1442. }
  1443. $action = 'update';
  1444. // update accountancy for this entity
  1445. if (!$error && getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
  1446. $this->db->query("DELETE FROM ".MAIN_DB_PREFIX."societe_perentity WHERE fk_soc = ".((int) $this->id)." AND entity = ".((int) $conf->entity));
  1447. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_perentity (";
  1448. $sql .= " fk_soc";
  1449. $sql .= ", entity";
  1450. $sql .= ", vat_reverse_charge";
  1451. $sql .= ", accountancy_code_customer";
  1452. $sql .= ", accountancy_code_supplier";
  1453. $sql .= ", accountancy_code_buy";
  1454. $sql .= ", accountancy_code_sell";
  1455. $sql .= ") VALUES (";
  1456. $sql .= $this->id;
  1457. $sql .= ", ".$conf->entity;
  1458. $sql .= ", ".(empty($this->vat_reverse_charge) ? '0' : '1');
  1459. $sql .= ", '".$this->db->escape($this->code_compta_client)."'";
  1460. $sql .= ", '".$this->db->escape($this->code_compta_fournisseur)."'";
  1461. $sql .= ", '".$this->db->escape($this->accountancy_code_buy)."'";
  1462. $sql .= ", '".$this->db->escape($this->accountancy_code_sell)."'";
  1463. $sql .= ")";
  1464. $result = $this->db->query($sql);
  1465. if (!$result) {
  1466. $error++;
  1467. $this->error = 'ErrorFailedToUpdateAccountancyForEntity';
  1468. }
  1469. }
  1470. // Actions on extra fields
  1471. if (!$error) {
  1472. $result = $this->insertExtraFields();
  1473. if ($result < 0) {
  1474. $error++;
  1475. }
  1476. }
  1477. // Actions on extra languages
  1478. if (!$error && !getDolGlobalString('MAIN_EXTRALANGUAGES_DISABLED')) { // For avoid conflicts if trigger used
  1479. $result = $this->insertExtraLanguages();
  1480. if ($result < 0) {
  1481. $error++;
  1482. }
  1483. }
  1484. if (!$error && $call_trigger) {
  1485. // Call trigger
  1486. $result = $this->call_trigger('COMPANY_MODIFY', $user);
  1487. if ($result < 0) {
  1488. $error++;
  1489. }
  1490. // End call triggers
  1491. }
  1492. if (!$error) {
  1493. dol_syslog(get_class($this)."::Update success");
  1494. $this->db->commit();
  1495. return 1;
  1496. } else {
  1497. $this->db->rollback();
  1498. return -1;
  1499. }
  1500. } else {
  1501. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  1502. // Doublon
  1503. $this->error = $langs->trans("ErrorDuplicateField");
  1504. $result = -1;
  1505. } else {
  1506. $this->error = $this->db->lasterror();
  1507. $result = -2;
  1508. }
  1509. $this->db->rollback();
  1510. return $result;
  1511. }
  1512. } else {
  1513. $this->db->rollback();
  1514. dol_syslog(get_class($this)."::Update fails verify ".join(',', $this->errors), LOG_WARNING);
  1515. return -3;
  1516. }
  1517. }
  1518. /**
  1519. * Load a third party from database into memory
  1520. *
  1521. * @param int $rowid Id of third party to load
  1522. * @param string $ref Reference of third party, name (Warning, this can return several records)
  1523. * @param string $ref_ext External reference of third party (Warning, this information is a free field not provided by Dolibarr)
  1524. * @param string $barcode Barcode of third party to load
  1525. * @param string $idprof1 Prof id 1 of third party (Warning, this can return several records)
  1526. * @param string $idprof2 Prof id 2 of third party (Warning, this can return several records)
  1527. * @param string $idprof3 Prof id 3 of third party (Warning, this can return several records)
  1528. * @param string $idprof4 Prof id 4 of third party (Warning, this can return several records)
  1529. * @param string $idprof5 Prof id 5 of third party (Warning, this can return several records)
  1530. * @param string $idprof6 Prof id 6 of third party (Warning, this can return several records)
  1531. * @param string $email Email of third party (Warning, this can return several records)
  1532. * @param string $ref_alias Name_alias of third party (Warning, this can return several records)
  1533. * @return int >0 if OK, <0 if KO or if two records found for same ref or idprof, 0 if not found.
  1534. */
  1535. public function fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
  1536. {
  1537. global $langs;
  1538. global $conf;
  1539. if (empty($rowid) && empty($ref) && empty($ref_ext) && empty($barcode) && empty($idprof1) && empty($idprof2) && empty($idprof3) && empty($idprof4) && empty($idprof5) && empty($idprof6) && empty($email)) {
  1540. return -1;
  1541. }
  1542. $sql = 'SELECT s.rowid, s.nom as name, s.name_alias, s.entity, s.ref_ext, s.address, s.datec as date_creation, s.prefix_comm';
  1543. $sql .= ', s.status, s.fk_warehouse';
  1544. $sql .= ', s.price_level';
  1545. $sql .= ', s.tms as date_modification, s.fk_user_creat, s.fk_user_modif';
  1546. $sql .= ', s.phone, s.fax, s.email';
  1547. $sql .= ', s.socialnetworks';
  1548. $sql .= ', s.url, s.zip, s.town, s.note_private, s.note_public, s.client, s.fournisseur';
  1549. $sql .= ', s.siren as idprof1, s.siret as idprof2, s.ape as idprof3, s.idprof4, s.idprof5, s.idprof6';
  1550. $sql .= ', s.capital, s.tva_intra';
  1551. $sql .= ', s.fk_typent as typent_id';
  1552. $sql .= ', s.fk_effectif as effectif_id';
  1553. $sql .= ', s.fk_forme_juridique as forme_juridique_code';
  1554. $sql .= ', s.webservices_url, s.webservices_key, s.model_pdf, s.last_main_doc';
  1555. if (!getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
  1556. $sql .= ', s.code_compta, s.code_compta_fournisseur, s.accountancy_code_buy, s.accountancy_code_sell';
  1557. $sql .= ', s.vat_reverse_charge as soc_vat_reverse_charge';
  1558. } else {
  1559. $sql .= ', spe.accountancy_code_customer as code_compta, spe.accountancy_code_supplier as code_compta_fournisseur, spe.accountancy_code_buy, spe.accountancy_code_sell';
  1560. $sql .= ', spe.vat_reverse_charge as spe_vat_reverse_charge';
  1561. }
  1562. $sql .= ', s.code_client, s.code_fournisseur, s.parent, s.barcode';
  1563. $sql .= ', s.fk_departement as state_id, s.fk_pays as country_id, s.fk_stcomm, s.mode_reglement, s.cond_reglement, s.deposit_percent, s.transport_mode';
  1564. $sql .= ', s.fk_account, s.tva_assuj';
  1565. $sql .= ', s.mode_reglement_supplier, s.cond_reglement_supplier, s.transport_mode_supplier';
  1566. $sql .= ', s.localtax1_assuj, s.localtax1_value, s.localtax2_assuj, s.localtax2_value, s.fk_prospectlevel, s.default_lang, s.logo, s.logo_squarred';
  1567. $sql .= ', s.fk_shipping_method';
  1568. $sql .= ', s.outstanding_limit, s.import_key, s.canvas, s.fk_incoterms, s.location_incoterms';
  1569. $sql .= ', s.order_min_amount, s.supplier_order_min_amount';
  1570. $sql .= ', s.fk_multicurrency, s.multicurrency_code';
  1571. $sql .= ', fj.libelle as forme_juridique';
  1572. $sql .= ', e.libelle as effectif';
  1573. $sql .= ', c.code as country_code, c.label as country';
  1574. $sql .= ', d.code_departement as state_code, d.nom as state';
  1575. $sql .= ', r.rowid as region_id, r.code_region as region_code';
  1576. $sql .= ', st.libelle as stcomm, st.picto as stcomm_picto';
  1577. $sql .= ', te.code as typent_code';
  1578. $sql .= ', i.libelle as label_incoterms';
  1579. if (!isModEnabled('multicompany')) {
  1580. $sql .= ', s.remise_client, s.remise_supplier';
  1581. } else {
  1582. $sql .= ', sr.remise_client, sr2.remise_supplier';
  1583. }
  1584. $sql .= ' FROM '.MAIN_DB_PREFIX.'societe as s';
  1585. if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
  1586. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = ".((int) $conf->entity);
  1587. }
  1588. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_effectif as e ON s.fk_effectif = e.id';
  1589. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid';
  1590. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcomm as st ON s.fk_stcomm = st.id';
  1591. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_forme_juridique as fj ON s.fk_forme_juridique = fj.code';
  1592. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_departements as d ON s.fk_departement = d.rowid';
  1593. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_regions as r ON d.fk_region = r.code_region ';
  1594. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_typent as te ON s.fk_typent = te.id';
  1595. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON s.fk_incoterms = i.rowid';
  1596. // With default setup, llx_societe_remise is a history table in default setup and current value is in llx_societe.
  1597. // We use it for real value when multicompany is on. A better place would be into llx_societe_perentity.
  1598. if (isModEnabled('multicompany')) {
  1599. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_remise as sr ON sr.rowid = (SELECT MAX(rowid) FROM '.MAIN_DB_PREFIX.'societe_remise WHERE fk_soc = s.rowid AND entity IN ('.getEntity('discount').'))';
  1600. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe_remise_supplier as sr2 ON sr2.rowid = (SELECT MAX(rowid) FROM '.MAIN_DB_PREFIX.'societe_remise_supplier WHERE fk_soc = s.rowid AND entity IN ('.getEntity('discount').'))';
  1601. }
  1602. $sql .= ' WHERE s.entity IN ('.getEntity($this->element).')';
  1603. if ($rowid) {
  1604. $sql .= ' AND s.rowid = '.((int) $rowid);
  1605. }
  1606. if ($ref) {
  1607. $sql .= " AND s.nom = '".$this->db->escape($ref)."'";
  1608. }
  1609. if ($ref_alias) {
  1610. $sql .= " AND s.name_alias = '".$this->db->escape($ref_alias)."'";
  1611. }
  1612. if ($ref_ext) {
  1613. $sql .= " AND s.ref_ext = '".$this->db->escape($ref_ext)."'";
  1614. }
  1615. if ($barcode) {
  1616. $sql .= " AND s.barcode = '".$this->db->escape($barcode)."'";
  1617. }
  1618. if ($idprof1) {
  1619. $sql .= " AND s.siren = '".$this->db->escape($idprof1)."'";
  1620. }
  1621. if ($idprof2) {
  1622. $sql .= " AND s.siret = '".$this->db->escape($idprof2)."'";
  1623. }
  1624. if ($idprof3) {
  1625. $sql .= " AND s.ape = '".$this->db->escape($idprof3)."'";
  1626. }
  1627. if ($idprof4) {
  1628. $sql .= " AND s.idprof4 = '".$this->db->escape($idprof4)."'";
  1629. }
  1630. if ($idprof5) {
  1631. $sql .= " AND s.idprof5 = '".$this->db->escape($idprof5)."'";
  1632. }
  1633. if ($idprof6) {
  1634. $sql .= " AND s.idprof6 = '".$this->db->escape($idprof6)."'";
  1635. }
  1636. if ($email) {
  1637. $sql .= " AND s.email = '".$this->db->escape($email)."'";
  1638. }
  1639. $resql = $this->db->query($sql);
  1640. if ($resql) {
  1641. $num = $this->db->num_rows($resql);
  1642. if ($num > 1) {
  1643. $this->error = 'Fetch found several records. Rename one of thirdparties to avoid duplicate.';
  1644. dol_syslog($this->error, LOG_ERR);
  1645. $result = -2;
  1646. } elseif ($num) { // $num = 1
  1647. $obj = $this->db->fetch_object($resql);
  1648. $this->id = $obj->rowid;
  1649. $this->entity = $obj->entity;
  1650. $this->canvas = $obj->canvas;
  1651. $this->ref = $obj->rowid;
  1652. $this->name = $obj->name;
  1653. $this->nom = $obj->name; // deprecated
  1654. $this->name_alias = $obj->name_alias;
  1655. $this->ref_ext = $obj->ref_ext;
  1656. $this->date_creation = $this->db->jdate($obj->date_creation);
  1657. $this->date_modification = $this->db->jdate($obj->date_modification);
  1658. $this->user_creation_id = $obj->fk_user_creat;
  1659. $this->user_modification_id = $obj->fk_user_modif;
  1660. $this->address = $obj->address;
  1661. $this->zip = $obj->zip;
  1662. $this->town = $obj->town;
  1663. $this->country_id = $obj->country_id;
  1664. $this->country_code = $obj->country_id ? $obj->country_code : '';
  1665. $this->country = $obj->country_id ? (($langs->transnoentities('Country'.$obj->country_code) != 'Country'.$obj->country_code) ? $langs->transnoentities('Country'.$obj->country_code) : $obj->country) : '';
  1666. $this->state_id = $obj->state_id;
  1667. $this->state_code = $obj->state_code;
  1668. $this->region_id = $obj->region_id;
  1669. $this->region_code = $obj->region_code;
  1670. $this->state = ($obj->state != '-' ? $obj->state : '');
  1671. $transcode = $langs->trans('StatusProspect'.$obj->fk_stcomm);
  1672. $label = ($transcode != 'StatusProspect'.$obj->fk_stcomm ? $transcode : $obj->stcomm);
  1673. $this->stcomm_id = $obj->fk_stcomm; // id status prospect
  1674. $this->status_prospect_label = $label; // label status prospect
  1675. $this->stcomm_picto = $obj->stcomm_picto; // picto statut commercial
  1676. $this->email = $obj->email;
  1677. $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array());
  1678. $this->url = $obj->url;
  1679. $this->phone = $obj->phone;
  1680. $this->fax = $obj->fax;
  1681. $this->parent = $obj->parent;
  1682. $this->idprof1 = $obj->idprof1;
  1683. $this->idprof2 = $obj->idprof2;
  1684. $this->idprof3 = $obj->idprof3;
  1685. $this->idprof4 = $obj->idprof4;
  1686. $this->idprof5 = $obj->idprof5;
  1687. $this->idprof6 = $obj->idprof6;
  1688. $this->capital = $obj->capital;
  1689. $this->code_client = $obj->code_client;
  1690. $this->code_fournisseur = $obj->code_fournisseur;
  1691. $this->code_compta = $obj->code_compta; // For backward compatibility
  1692. $this->code_compta_client = $obj->code_compta;
  1693. $this->code_compta_fournisseur = $obj->code_compta_fournisseur;
  1694. $this->barcode = $obj->barcode;
  1695. $this->tva_assuj = $obj->tva_assuj;
  1696. $this->tva_intra = $obj->tva_intra;
  1697. if (!empty($obj->spe_vat_reverse_charge)) {
  1698. $this->vat_reverse_charge = $obj->spe_vat_reverse_charge;
  1699. } elseif (!empty($obj->soc_vat_reverse_charge)) {
  1700. $this->vat_reverse_charge = $obj->soc_vat_reverse_charge;
  1701. } else {
  1702. $this->vat_reverse_charge = 0;
  1703. }
  1704. $this->status = $obj->status;
  1705. // Local Taxes
  1706. $this->localtax1_assuj = $obj->localtax1_assuj;
  1707. $this->localtax2_assuj = $obj->localtax2_assuj;
  1708. $this->localtax1_value = $obj->localtax1_value;
  1709. $this->localtax2_value = $obj->localtax2_value;
  1710. $this->typent_id = $obj->typent_id;
  1711. $this->typent_code = $obj->typent_code;
  1712. $this->effectif_id = $obj->effectif_id;
  1713. $this->effectif = $obj->effectif_id ? $obj->effectif : '';
  1714. $this->forme_juridique_code = $obj->forme_juridique_code;
  1715. $this->forme_juridique = $obj->forme_juridique_code ? $obj->forme_juridique : '';
  1716. $this->fk_prospectlevel = $obj->fk_prospectlevel;
  1717. $this->prefix_comm = $obj->prefix_comm;
  1718. $this->remise_percent = $obj->remise_client ? price2num($obj->remise_client) : 0; // 0.000000 must be 0
  1719. $this->remise_supplier_percent = $obj->remise_supplier;
  1720. $this->mode_reglement_id = $obj->mode_reglement;
  1721. $this->cond_reglement_id = $obj->cond_reglement;
  1722. $this->deposit_percent = $obj->deposit_percent;
  1723. $this->transport_mode_id = $obj->transport_mode;
  1724. $this->mode_reglement_supplier_id = $obj->mode_reglement_supplier;
  1725. $this->cond_reglement_supplier_id = $obj->cond_reglement_supplier;
  1726. $this->transport_mode_supplier_id = $obj->transport_mode_supplier;
  1727. $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method : null;
  1728. $this->fk_account = $obj->fk_account;
  1729. $this->client = $obj->client;
  1730. $this->fournisseur = $obj->fournisseur;
  1731. $this->note = $obj->note_private; // TODO Deprecated for backward comtability
  1732. $this->note_private = $obj->note_private;
  1733. $this->note_public = $obj->note_public;
  1734. $this->model_pdf = $obj->model_pdf;
  1735. $this->default_lang = $obj->default_lang;
  1736. $this->logo = $obj->logo;
  1737. $this->logo_squarred = $obj->logo_squarred;
  1738. $this->webservices_url = $obj->webservices_url;
  1739. $this->webservices_key = $obj->webservices_key;
  1740. $this->accountancy_code_buy = $obj->accountancy_code_buy;
  1741. $this->accountancy_code_sell = $obj->accountancy_code_sell;
  1742. $this->outstanding_limit = $obj->outstanding_limit;
  1743. $this->order_min_amount = $obj->order_min_amount;
  1744. $this->supplier_order_min_amount = $obj->supplier_order_min_amount;
  1745. // multiprix
  1746. $this->price_level = $obj->price_level;
  1747. // warehouse
  1748. $this->fk_warehouse = $obj->fk_warehouse;
  1749. $this->import_key = $obj->import_key;
  1750. //Incoterms
  1751. $this->fk_incoterms = $obj->fk_incoterms;
  1752. $this->location_incoterms = $obj->location_incoterms;
  1753. $this->label_incoterms = $obj->label_incoterms;
  1754. // multicurrency
  1755. $this->fk_multicurrency = $obj->fk_multicurrency;
  1756. $this->multicurrency_code = $obj->multicurrency_code;
  1757. // pdf
  1758. $this->model_pdf = $obj->model_pdf;
  1759. $this->last_main_doc = $obj->last_main_doc;
  1760. $result = 1;
  1761. // fetch optionals attributes and labels
  1762. $this->fetch_optionals();
  1763. } else {
  1764. $result = 0;
  1765. }
  1766. $this->db->free($resql);
  1767. } else {
  1768. $this->error = $this->db->lasterror();
  1769. $this->errors[] = $this->db->lasterror();
  1770. $result = -3;
  1771. }
  1772. // Use first price level if level not defined for third party
  1773. if ((getDolGlobalString('PRODUIT_MULTIPRICES') || getDolGlobalString('PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES')) && empty($this->price_level)) {
  1774. $this->price_level = 1;
  1775. }
  1776. return $result;
  1777. }
  1778. /**
  1779. * Delete a third party from database and all its dependencies (contacts, rib...)
  1780. *
  1781. * @param int $id Id of third party to delete
  1782. * @param User|null $fuser User who ask to delete thirdparty
  1783. * @param int $call_trigger 0=No, 1=yes
  1784. * @return int <0 if KO, 0 if nothing done, >0 if OK
  1785. */
  1786. public function delete($id, User $fuser = null, $call_trigger = 1)
  1787. {
  1788. global $langs, $conf, $user;
  1789. if (empty($fuser)) {
  1790. $fuser = $user;
  1791. }
  1792. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1793. $entity = isset($this->entity) ? $this->entity : $conf->entity;
  1794. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  1795. $error = 0;
  1796. // Test if child exists
  1797. $objectisused = $this->isObjectUsed($id);
  1798. if (empty($objectisused)) {
  1799. $this->db->begin();
  1800. // User is mandatory for trigger call
  1801. if (!$error && $call_trigger) {
  1802. // Call trigger
  1803. $result = $this->call_trigger('COMPANY_DELETE', $fuser);
  1804. if ($result < 0) {
  1805. $error++;
  1806. }
  1807. // End call triggers
  1808. }
  1809. if (!$error) {
  1810. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  1811. $static_cat = new Categorie($this->db);
  1812. $toute_categs = array();
  1813. // Fill $toute_categs array with an array of (type => array of ("Categorie" instance))
  1814. if ($this->client || $this->prospect) {
  1815. $toute_categs['customer'] = $static_cat->containing($this->id, Categorie::TYPE_CUSTOMER);
  1816. }
  1817. if ($this->fournisseur) {
  1818. $toute_categs['supplier'] = $static_cat->containing($this->id, Categorie::TYPE_SUPPLIER);
  1819. }
  1820. // Remove each "Categorie"
  1821. foreach ($toute_categs as $type => $categs_type) {
  1822. foreach ($categs_type as $cat) {
  1823. $cat->del_type($this, $type);
  1824. }
  1825. }
  1826. }
  1827. if (!$error) {
  1828. foreach ($this->childtablesoncascade as $tabletodelete) {
  1829. $deleteFromObject = explode(':', $tabletodelete);
  1830. if (count($deleteFromObject) >= 2) {
  1831. $className = str_replace('@', '', $deleteFromObject[0]);
  1832. $filepath = $deleteFromObject[1];
  1833. $columnName = $deleteFromObject[2];
  1834. if (dol_include_once($filepath)) {
  1835. $child_object = new $className($this->db);
  1836. $result = $child_object->deleteByParentField($id, $columnName);
  1837. if ($result < 0) {
  1838. $error++;
  1839. $this->errors[] = $child_object->error;
  1840. break;
  1841. }
  1842. } else {
  1843. $error++;
  1844. $this->errors[] = 'Cannot include child class file '.$filepath;
  1845. break;
  1846. }
  1847. } else {
  1848. $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete;
  1849. $sql .= " WHERE fk_soc = ".((int) $id);
  1850. if (!$this->db->query($sql)) {
  1851. $error++;
  1852. $this->errors[] = $this->db->lasterror();
  1853. break;
  1854. }
  1855. }
  1856. }
  1857. }
  1858. // Removed extrafields
  1859. if (!$error) {
  1860. $result = $this->deleteExtraFields();
  1861. if ($result < 0) {
  1862. $error++;
  1863. dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
  1864. }
  1865. }
  1866. // Remove links to subsidiaries companies
  1867. if (!$error) {
  1868. $sql = "UPDATE ".MAIN_DB_PREFIX."societe";
  1869. $sql .= " SET parent = NULL";
  1870. $sql .= " WHERE parent = ".((int) $id);
  1871. if (!$this->db->query($sql)) {
  1872. $error++;
  1873. $this->errors[] = $this->db->lasterror();
  1874. }
  1875. }
  1876. // Remove third party
  1877. if (!$error) {
  1878. if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) {
  1879. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_perentity";
  1880. $sql .= " WHERE fk_soc = ".((int) $id);
  1881. if (!$this->db->query($sql)) {
  1882. $error++;
  1883. $this->errors[] = $this->db->lasterror();
  1884. }
  1885. }
  1886. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe";
  1887. $sql .= " WHERE rowid = ".((int) $id);
  1888. if (!$this->db->query($sql)) {
  1889. $error++;
  1890. $this->errors[] = $this->db->lasterror();
  1891. }
  1892. }
  1893. if (!$error) {
  1894. $this->db->commit();
  1895. // Delete directory
  1896. if (!empty($conf->societe->multidir_output[$entity])) {
  1897. $docdir = $conf->societe->multidir_output[$entity]."/".$id;
  1898. if (dol_is_dir($docdir)) {
  1899. dol_delete_dir_recursive($docdir);
  1900. }
  1901. }
  1902. return 1;
  1903. } else {
  1904. dol_syslog($this->error, LOG_ERR);
  1905. $this->db->rollback();
  1906. return -1;
  1907. }
  1908. } else {
  1909. dol_syslog("Can't remove thirdparty with id ".$id.". There is ".$objectisused." childs", LOG_WARNING);
  1910. }
  1911. return 0;
  1912. }
  1913. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1914. /**
  1915. * Define third party as a customer
  1916. *
  1917. * @return int <0 if KO, >0 if OK
  1918. * @deprecated
  1919. * @see setAsCustomer()
  1920. */
  1921. public function set_as_client()
  1922. {
  1923. global $conf;
  1924. // phpcs:enable
  1925. dol_syslog(get_class($this)."::set_as_client is deprecated use setAsCustomer instead", LOG_NOTICE);
  1926. return $this->setAsCustomer();
  1927. }
  1928. /**
  1929. * Define third party as a customer
  1930. *
  1931. * @return int <0 if KO, >0 if OK
  1932. * @since dolibarr v19
  1933. */
  1934. public function setAsCustomer()
  1935. {
  1936. if ($this->id) {
  1937. $newclient = 1;
  1938. if (($this->client == 2 || $this->client == 3) && !getDolGlobalInt('SOCIETE_DISABLE_PROSPECTSCUSTOMERS')) {
  1939. $newclient = 3; //If prospect, we keep prospect tag
  1940. }
  1941. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
  1942. $sql .= " SET client = ".((int) $newclient);
  1943. $sql .= " WHERE rowid = ".((int) $this->id);
  1944. $resql = $this->db->query($sql);
  1945. if ($resql) {
  1946. $this->client = $newclient;
  1947. return 1;
  1948. } else {
  1949. return -1;
  1950. }
  1951. }
  1952. return 0;
  1953. }
  1954. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1955. /**
  1956. * Defines the company as a customer
  1957. *
  1958. * @param float $remise Value in % of the discount
  1959. * @param string $note Note/Reason for changing the discount
  1960. * @param User $user User who sets the discount
  1961. * @return int <0 if KO, >0 if OK
  1962. */
  1963. public function set_remise_client($remise, $note, User $user)
  1964. {
  1965. // phpcs:enable
  1966. global $conf, $langs;
  1967. // Parameter cleaning
  1968. $note = trim($note);
  1969. if (!$note) {
  1970. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("NoteReason"));
  1971. return -2;
  1972. }
  1973. dol_syslog(get_class($this)."::set_remise_client ".$remise.", ".$note.", ".$user->id);
  1974. if ($this->id) {
  1975. $this->db->begin();
  1976. $now = dol_now();
  1977. // Position current discount
  1978. $sql = "UPDATE ".MAIN_DB_PREFIX."societe ";
  1979. $sql .= " SET remise_client = '".$this->db->escape($remise)."'";
  1980. $sql .= " WHERE rowid = ".((int) $this->id);
  1981. $resql = $this->db->query($sql);
  1982. if (!$resql) {
  1983. $this->db->rollback();
  1984. $this->error = $this->db->error();
  1985. return -1;
  1986. }
  1987. // Writes trace in discount history
  1988. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise";
  1989. $sql .= " (entity, datec, fk_soc, remise_client, note, fk_user_author)";
  1990. $sql .= " VALUES (".$conf->entity.", '".$this->db->idate($now)."', ".((int) $this->id).", '".$this->db->escape($remise)."',";
  1991. $sql .= " '".$this->db->escape($note)."',";
  1992. $sql .= " ".((int) $user->id);
  1993. $sql .= ")";
  1994. $resql = $this->db->query($sql);
  1995. if (!$resql) {
  1996. $this->db->rollback();
  1997. $this->error = $this->db->lasterror();
  1998. return -1;
  1999. }
  2000. $this->db->commit();
  2001. return 1;
  2002. }
  2003. return -1;
  2004. }
  2005. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2006. /**
  2007. * Defines the company as a customer
  2008. *
  2009. * @param float $remise Value in % of the discount
  2010. * @param string $note Note/Reason for changing the discount
  2011. * @param User $user User who sets the discount
  2012. * @return int <0 if KO, >0 if OK
  2013. */
  2014. public function set_remise_supplier($remise, $note, User $user)
  2015. {
  2016. // phpcs:enable
  2017. global $conf, $langs;
  2018. // Parameter cleaning
  2019. $note = trim($note);
  2020. if (!$note) {
  2021. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("NoteReason"));
  2022. return -2;
  2023. }
  2024. dol_syslog(get_class($this)."::set_remise_supplier ".$remise.", ".$note.", ".$user->id);
  2025. if ($this->id) {
  2026. $this->db->begin();
  2027. $now = dol_now();
  2028. // Position current discount
  2029. $sql = "UPDATE ".MAIN_DB_PREFIX."societe ";
  2030. $sql .= " SET remise_supplier = '".$this->db->escape($remise)."'";
  2031. $sql .= " WHERE rowid = ".((int) $this->id);
  2032. $resql = $this->db->query($sql);
  2033. if (!$resql) {
  2034. $this->db->rollback();
  2035. $this->error = $this->db->error();
  2036. return -1;
  2037. }
  2038. // Writes trace in discount history
  2039. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise_supplier";
  2040. $sql .= " (entity, datec, fk_soc, remise_supplier, note, fk_user_author)";
  2041. $sql .= " VALUES (".$conf->entity.", '".$this->db->idate($now)."', ".((int) $this->id).", '".$this->db->escape($remise)."',";
  2042. $sql .= " '".$this->db->escape($note)."',";
  2043. $sql .= " ".((int) $user->id);
  2044. $sql .= ")";
  2045. $resql = $this->db->query($sql);
  2046. if (!$resql) {
  2047. $this->db->rollback();
  2048. $this->error = $this->db->lasterror();
  2049. return -1;
  2050. }
  2051. $this->db->commit();
  2052. return 1;
  2053. }
  2054. return -1;
  2055. }
  2056. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2057. /**
  2058. * Add a discount for third party
  2059. *
  2060. * @param float $remise Amount of discount
  2061. * @param User $user User adding discount
  2062. * @param string $desc Reason of discount
  2063. * @param string $vatrate VAT rate (may contain the vat code too). Exemple: '1.23', '1.23 (ABC)', ...
  2064. * @param int $discount_type 0 => customer discount, 1 => supplier discount
  2065. * @param string $price_base_type Price base type 'HT' or 'TTC'
  2066. * @return int <0 if KO, id of discount record if OK
  2067. */
  2068. public function set_remise_except($remise, User $user, $desc, $vatrate = '', $discount_type = 0, $price_base_type = 'HT')
  2069. {
  2070. // phpcs:enable
  2071. global $langs;
  2072. // Clean parameters
  2073. $remise = price2num($remise);
  2074. $desc = trim($desc);
  2075. // Check parameters
  2076. if (!($remise > 0)) {
  2077. $this->error = $langs->trans("ErrorWrongValueForParameter", "1");
  2078. return -1;
  2079. }
  2080. if (!$desc) {
  2081. $this->error = $langs->trans("ErrorWrongValueForParameter", "3");
  2082. return -2;
  2083. }
  2084. if ($this->id > 0) {
  2085. // Clean vat code
  2086. $reg = array();
  2087. $vat_src_code = '';
  2088. if (preg_match('/\((.*)\)/', $vatrate, $reg)) {
  2089. $vat_src_code = $reg[1];
  2090. $vatrate = preg_replace('/\s*\(.*\)/', '', $vatrate); // Remove code into vatrate.
  2091. }
  2092. require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
  2093. $discount = new DiscountAbsolute($this->db);
  2094. $discount->fk_soc = $this->id;
  2095. $discount->discount_type = $discount_type;
  2096. if ($price_base_type == 'TTC') {
  2097. $discount->amount_ttc = $discount->multicurrency_amount_ttc = price2num($remise, 'MT');
  2098. $discount->amount_ht = $discount->multicurrency_amount_ht = price2num($remise / (1 + $vatrate / 100), 'MT');
  2099. $discount->amount_tva = $discount->multicurrency_amount_tva = price2num($discount->amount_ttc - $discount->amount_ht, 'MT');
  2100. } else {
  2101. $discount->amount_ht = $discount->multicurrency_amount_ht = price2num($remise, 'MT');
  2102. $discount->amount_tva = $discount->multicurrency_amount_tva = price2num($remise * $vatrate / 100, 'MT');
  2103. $discount->amount_ttc = $discount->multicurrency_amount_ttc = price2num($discount->amount_ht + $discount->amount_tva, 'MT');
  2104. }
  2105. $discount->tva_tx = price2num($vatrate);
  2106. $discount->vat_src_code = $vat_src_code;
  2107. $discount->description = $desc;
  2108. $result = $discount->create($user);
  2109. if ($result > 0) {
  2110. return $result;
  2111. } else {
  2112. $this->error = $discount->error;
  2113. return -3;
  2114. }
  2115. } else {
  2116. return 0;
  2117. }
  2118. }
  2119. /**
  2120. * Returns amount of included taxes of the current discounts/credits available from the company
  2121. *
  2122. * @param User $user Filter on a user author of discounts
  2123. * @param string $filter Other filter
  2124. * @param integer $maxvalue Filter on max value for discount
  2125. * @param int $discount_type 0 => customer discount, 1 => supplier discount
  2126. * @return int <0 if KO, Credit note amount otherwise
  2127. */
  2128. public function getAvailableDiscounts($user = '', $filter = '', $maxvalue = 0, $discount_type = 0)
  2129. {
  2130. require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
  2131. $discountstatic = new DiscountAbsolute($this->db);
  2132. $result = $discountstatic->getAvailableDiscounts($this, $user, $filter, $maxvalue, $discount_type);
  2133. if ($result >= 0) {
  2134. return $result;
  2135. } else {
  2136. $this->error = $discountstatic->error;
  2137. return -1;
  2138. }
  2139. }
  2140. /**
  2141. * Return array of sales representatives
  2142. *
  2143. * @param User $user Object user (not used)
  2144. * @param int $mode 0=Array with properties, 1=Array of id.
  2145. * @param string $sortfield List of sort fields, separated by comma. Example: 't1.fielda,t2.fieldb'
  2146. * @param string $sortorder Sort order, separated by comma. Example: 'ASC,DESC';
  2147. * @return array|int Array of sales representatives of third party or <0 if KO
  2148. */
  2149. public function getSalesRepresentatives(User $user, $mode = 0, $sortfield = null, $sortorder = null)
  2150. {
  2151. global $conf;
  2152. $reparray = array();
  2153. $sql = "SELECT DISTINCT u.rowid, u.login, u.lastname, u.firstname, u.office_phone, u.job, u.email, u.statut as status, u.entity, u.photo, u.gender";
  2154. $sql .= ", u.office_fax, u.user_mobile, u.personal_mobile";
  2155. $sql .= " FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc, ".MAIN_DB_PREFIX."user as u";
  2156. if (isModEnabled('multicompany') && getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE')) {
  2157. $sql .= ", ".MAIN_DB_PREFIX."usergroup_user as ug";
  2158. $sql .= " WHERE ((ug.fk_user = sc.fk_user";
  2159. $sql .= " AND ug.entity = ".$conf->entity.")";
  2160. $sql .= " OR u.admin = 1)";
  2161. } else {
  2162. $sql .= " WHERE entity in (0, ".$conf->entity.")";
  2163. }
  2164. $sql .= " AND u.rowid = sc.fk_user AND sc.fk_soc = ".((int) $this->id);
  2165. if (empty($sortfield) && empty($sortorder)) {
  2166. $sortfield = 'u.lastname,u.firstname';
  2167. $sortorder = 'ASC,ASC';
  2168. }
  2169. $sql .= $this->db->order($sortfield, $sortorder);
  2170. $resql = $this->db->query($sql);
  2171. if ($resql) {
  2172. $num = $this->db->num_rows($resql);
  2173. $i = 0;
  2174. while ($i < $num) {
  2175. $obj = $this->db->fetch_object($resql);
  2176. if (empty($mode)) {
  2177. $reparray[$i]['id'] = $obj->rowid;
  2178. $reparray[$i]['lastname'] = $obj->lastname;
  2179. $reparray[$i]['firstname'] = $obj->firstname;
  2180. $reparray[$i]['email'] = $obj->email;
  2181. $reparray[$i]['phone'] = $obj->office_phone;
  2182. $reparray[$i]['office_phone'] = $obj->office_phone; // Pro phone
  2183. $reparray[$i]['office_fax'] = $obj->office_fax;
  2184. $reparray[$i]['user_mobile'] = $obj->user_mobile; // Pro mobile
  2185. $reparray[$i]['personal_mobile'] = $obj->personal_mobile; // Personal mobile
  2186. $reparray[$i]['job'] = $obj->job;
  2187. $reparray[$i]['statut'] = $obj->status; // deprecated
  2188. $reparray[$i]['status'] = $obj->status;
  2189. $reparray[$i]['entity'] = $obj->entity;
  2190. $reparray[$i]['login'] = $obj->login;
  2191. $reparray[$i]['photo'] = $obj->photo;
  2192. $reparray[$i]['gender'] = $obj->gender;
  2193. } else {
  2194. $reparray[] = $obj->rowid;
  2195. }
  2196. $i++;
  2197. }
  2198. return $reparray;
  2199. } else {
  2200. dol_print_error($this->db);
  2201. return -1;
  2202. }
  2203. }
  2204. /**
  2205. * Set the price level
  2206. *
  2207. * @param int $price_level Level of price
  2208. * @param User $user Use making change
  2209. * @return int <0 if KO, >0 if OK
  2210. */
  2211. public function setPriceLevel($price_level, User $user)
  2212. {
  2213. if ($this->id) {
  2214. $now = dol_now();
  2215. $sql = "UPDATE ".MAIN_DB_PREFIX."societe";
  2216. $sql .= " SET price_level = ".((int) $price_level);
  2217. $sql .= " WHERE rowid = ".((int) $this->id);
  2218. if (!$this->db->query($sql)) {
  2219. dol_print_error($this->db);
  2220. return -1;
  2221. }
  2222. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_prices";
  2223. $sql .= " (datec, fk_soc, price_level, fk_user_author)";
  2224. $sql .= " VALUES ('".$this->db->idate($now)."', ".((int) $this->id).", ".((int) $price_level).", ".((int) $user->id).")";
  2225. if (!$this->db->query($sql)) {
  2226. dol_print_error($this->db);
  2227. return -1;
  2228. }
  2229. return 1;
  2230. }
  2231. return -1;
  2232. }
  2233. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2234. /**
  2235. * Add link to sales representative
  2236. *
  2237. * @param User $user Object user
  2238. * @param int $commid Id of user
  2239. * @return int <=0 if KO, >0 if OK
  2240. */
  2241. public function add_commercial(User $user, $commid)
  2242. {
  2243. // phpcs:enable
  2244. $error = 0;
  2245. if ($this->id > 0 && $commid > 0) {
  2246. $this->db->begin();
  2247. if (!$error) {
  2248. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_commerciaux";
  2249. $sql .= " WHERE fk_soc = ".((int) $this->id)." AND fk_user = ".((int) $commid);
  2250. $resql = $this->db->query($sql);
  2251. if (!$resql) {
  2252. dol_syslog(get_class($this)."::add_commercial Error ".$this->db->lasterror());
  2253. $error++;
  2254. }
  2255. }
  2256. if (!$error) {
  2257. $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_commerciaux";
  2258. $sql .= " (fk_soc, fk_user)";
  2259. $sql .= " VALUES (".((int) $this->id).", ".((int) $commid).")";
  2260. $resql = $this->db->query($sql);
  2261. if (!$resql) {
  2262. dol_syslog(get_class($this)."::add_commercial Error ".$this->db->lasterror());
  2263. $error++;
  2264. }
  2265. }
  2266. if (!$error) {
  2267. $this->context = array('commercial_modified' => $commid);
  2268. $result = $this->call_trigger('COMPANY_LINK_SALE_REPRESENTATIVE', $user);
  2269. if ($result < 0) {
  2270. $error++;
  2271. }
  2272. }
  2273. if (!$error) {
  2274. $this->db->commit();
  2275. return 1;
  2276. } else {
  2277. $this->db->rollback();
  2278. return -1;
  2279. }
  2280. }
  2281. return 0;
  2282. }
  2283. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2284. /**
  2285. * Add link to sales representative
  2286. *
  2287. * @param User $user Object user
  2288. * @param int $commid Id of user
  2289. * @return void
  2290. */
  2291. public function del_commercial(User $user, $commid)
  2292. {
  2293. // phpcs:enable
  2294. $error = 0;
  2295. $this->context = array('commercial_modified'=>$commid);
  2296. $result = $this->call_trigger('COMPANY_UNLINK_SALE_REPRESENTATIVE', $user);
  2297. if ($result < 0) {
  2298. $error++;
  2299. }
  2300. if ($this->id > 0 && $commid > 0) {
  2301. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_commerciaux ";
  2302. $sql .= " WHERE fk_soc = ".((int) $this->id)." AND fk_user = ".((int) $commid);
  2303. if (!$this->db->query($sql)) {
  2304. dol_syslog(get_class($this)."::del_commercial Erreur");
  2305. }
  2306. }
  2307. }
  2308. /**
  2309. * getTooltipContentArray
  2310. *
  2311. * @param array $params params to construct tooltip data
  2312. * @since v18
  2313. * @return array
  2314. */
  2315. public function getTooltipContentArray($params)
  2316. {
  2317. global $conf, $langs, $user;
  2318. $langs->loadLangs(['companies', 'commercial']);
  2319. $datas = array();
  2320. $option = $params['option'] ?? '';
  2321. $nofetch = !empty($params['nofetch']);
  2322. $noaliasinname = (empty($params['noaliasinname']) ? 0 : $params['noaliasinname']);
  2323. if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
  2324. return ['optimize' => $langs->trans("ShowCompany")];
  2325. }
  2326. if (!empty($this->logo) && class_exists('Form')) {
  2327. $photo = '<div class="photointooltip floatright">';
  2328. $photo .= Form::showphoto('societe', $this, 0, 40, 0, 'photoref', 'mini', 0); // Important, we must force height so image will have height tags and if image is inside a tooltip, the tooltip manager can calculate height and position correctly the tooltip.
  2329. $photo .= '</div>';
  2330. $datas['photo'] = $photo;
  2331. } elseif (!empty($this->logo_squarred) && class_exists('Form')) {
  2332. /*$label.= '<div class="photointooltip">';
  2333. $label.= Form::showphoto('societe', $this, 0, 40, 0, 'photowithmargin', 'mini', 0); // Important, we must force height so image will have height tags and if image is inside a tooltip, the tooltip manager can calculate height and position correctly the tooltip.
  2334. $label.= '</div><div style="clear: both;"></div>';*/
  2335. }
  2336. $datas['divopen'] = '<div class="centpercent">';
  2337. if ($option == 'customer' || $option == 'compta' || $option == 'category') {
  2338. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Customer").'</u>';
  2339. } elseif ($option == 'prospect' && !getDolGlobalString('SOCIETE_DISABLE_PROSPECTS')) {
  2340. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Prospect").'</u>';
  2341. } elseif ($option == 'supplier' || $option == 'category_supplier') {
  2342. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Supplier").'</u>';
  2343. } elseif ($option == 'agenda') {
  2344. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("ThirdParty").'</u>';
  2345. } elseif ($option == 'project') {
  2346. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("ThirdParty").'</u>';
  2347. } elseif ($option == 'margin') {
  2348. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("ThirdParty").'</u>';
  2349. } elseif ($option == 'contact') {
  2350. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("ThirdParty").'</u>';
  2351. } elseif ($option == 'ban') {
  2352. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("ThirdParty").'</u>';
  2353. }
  2354. // By default
  2355. if (empty($datas['picto'])) {
  2356. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("ThirdParty").'</u>';
  2357. }
  2358. if (isset($this->status)) {
  2359. $datas['status'] = ' '.$this->getLibStatut(5);
  2360. }
  2361. if (isset($this->client) && isset($this->fournisseur)) {
  2362. $datas['type'] = ' &nbsp; ' . $this->getTypeUrl(1);
  2363. }
  2364. $datas['name'] = '<br><b>'.$langs->trans('Name').':</b> '.dol_escape_htmltag(dol_string_nohtmltag($this->name));
  2365. if (!empty($this->name_alias) && empty($noaliasinname)) {
  2366. $datas['namealias'] = ' ('.dol_escape_htmltag(dol_string_nohtmltag($this->name_alias)).')';
  2367. }
  2368. if (!empty($this->email)) {
  2369. $datas['email'] = '<br>'.img_picto('', 'email', 'class="pictofixedwidth"').$this->email;
  2370. }
  2371. if (!empty($this->url)) {
  2372. $datas['url'] = '<br>'.img_picto('', 'globe', 'class="pictofixedwidth"').$this->url;
  2373. }
  2374. if (!empty($this->phone) || !empty($this->fax)) {
  2375. $phonelist = array();
  2376. if ($this->phone) {
  2377. $phonelist[] = dol_print_phone($this->phone, $this->country_code, $this->id, 0, '', '&nbsp', 'phone');
  2378. }
  2379. if ($this->fax) {
  2380. $phonelist[] = dol_print_phone($this->fax, $this->country_code, $this->id, 0, '', '&nbsp', 'fax');
  2381. }
  2382. $datas['phonelist'] = '<br>'.implode('&nbsp;', $phonelist);
  2383. }
  2384. if (!empty($this->address)) {
  2385. $datas['address'] = '<br><b>'.$langs->trans("Address").':</b> '.dol_format_address($this, 1, ' ', $langs); // Address + country
  2386. } elseif (!empty($this->country_code)) {
  2387. $datas['address'] = '<br><b>'.$langs->trans('Country').':</b> '.$this->country_code;
  2388. }
  2389. if (!empty($this->tva_intra) || (getDolGlobalString('SOCIETE_SHOW_FIELD_IN_TOOLTIP') && strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'vatnumber') !== false)) {
  2390. $datas['vatintra'] = '<br><b>'.$langs->trans('VATIntra').':</b> '.dol_escape_htmltag($this->tva_intra);
  2391. }
  2392. if (getDolGlobalString('SOCIETE_SHOW_FIELD_IN_TOOLTIP')) {
  2393. if (strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'profid1') !== false && $langs->trans('ProfId1'.$this->country_code) != '-') {
  2394. $datas['profid1'] = '<br><b>'.$langs->trans('ProfId1'.$this->country_code).':</b> '.$this->idprof1;
  2395. }
  2396. if (strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'profid2') !== false && $langs->trans('ProfId2'.$this->country_code) != '-') {
  2397. $datas['profid2'] = '<br><b>'.$langs->trans('ProfId2'.$this->country_code).':</b> '.$this->idprof2;
  2398. }
  2399. if (strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'profid3') !== false && $langs->trans('ProfId3'.$this->country_code) != '-') {
  2400. $datas['profid3'] = '<br><b>'.$langs->trans('ProfId3'.$this->country_code).':</b> '.$this->idprof3;
  2401. }
  2402. if (strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'profid4') !== false && $langs->trans('ProfId4'.$this->country_code) != '-') {
  2403. $datas['profid4'] = '<br><b>'.$langs->trans('ProfId4'.$this->country_code).':</b> '.$this->idprof4;
  2404. }
  2405. if (strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'profid5') !== false && $langs->trans('ProfId5'.$this->country_code) != '-') {
  2406. $datas['profid5'] = '<br><b>'.$langs->trans('ProfId5'.$this->country_code).':</b> '.$this->idprof5;
  2407. }
  2408. if (strpos($conf->global->SOCIETE_SHOW_FIELD_IN_TOOLTIP, 'profid6') !== false && $langs->trans('ProfId6'.$this->country_code) != '-') {
  2409. $datas['profid6'] = '<br><b>'.$langs->trans('ProfId6'.$this->country_code).':</b> '.$this->idprof6;
  2410. }
  2411. }
  2412. $datas['separator'] = '<br>';
  2413. if (!empty($this->code_client) && ($this->client == 1 || $this->client == 3)) {
  2414. $datas['customercode'] = '<br><b>'.$langs->trans('CustomerCode').':</b> '.$this->code_client;
  2415. }
  2416. if (isModEnabled('accounting') && ($this->client == 1 || $this->client == 3)) {
  2417. $langs->load('compta');
  2418. $datas['accountancycustomercode'] = '<br><b>'.$langs->trans('CustomerAccountancyCode').':</b> '.($this->code_compta ? $this->code_compta : $this->code_compta_client);
  2419. }
  2420. // show categories for this record only in ajax to not overload lists
  2421. if (!$nofetch && isModEnabled('categorie') && $this->client) {
  2422. require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
  2423. $form = new Form($this->db);
  2424. $datas['categories_customer'] = '<br>' . $form->showCategories($this->id, Categorie::TYPE_CUSTOMER, 1, 1);
  2425. }
  2426. if (!empty($this->code_fournisseur) && $this->fournisseur) {
  2427. $datas['suppliercode'] = '<br><b>'.$langs->trans('SupplierCode').':</b> '.$this->code_fournisseur;
  2428. }
  2429. if (isModEnabled('accounting') && $this->fournisseur) {
  2430. $langs->load('compta');
  2431. $datas['accountancysuppliercode'] = '<br><b>'.$langs->trans('SupplierAccountancyCode').':</b> '.$this->code_compta_fournisseur;
  2432. }
  2433. // show categories for this record only in ajax to not overload lists
  2434. if (!$nofetch && isModEnabled('categorie') && $this->fournisseur) {
  2435. require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
  2436. $form = new Form($this->db);
  2437. $datas['categories_supplier'] = '<br>' . $form->showCategories($this->id, Categorie::TYPE_SUPPLIER, 1, 1);
  2438. }
  2439. $datas['divclose'] = '</div>';
  2440. return $datas;
  2441. }
  2442. /**
  2443. * Return a link on thirdparty (with picto)
  2444. *
  2445. * @param int $withpicto Add picto into link (0=No picto, 1=Include picto with link, 2=Picto only)
  2446. * @param string $option Target of link ('', 'customer', 'prospect', 'supplier', 'project')
  2447. * @param int $maxlen Max length of name
  2448. * @param int $notooltip 1=Disable tooltip
  2449. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  2450. * @param int $noaliasinname 1=Do not add alias into the link ref
  2451. * @param string $target add attribute target
  2452. * @return string String with URL
  2453. */
  2454. public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $notooltip = 0, $save_lastsearch_value = -1, $noaliasinname = 0, $target = '')
  2455. {
  2456. global $conf, $langs, $hookmanager;
  2457. if (!empty($conf->dol_no_mouse_hover)) {
  2458. $notooltip = 1; // Force disable tooltips
  2459. }
  2460. $name = $this->name ? $this->name : $this->nom;
  2461. if (getDolGlobalString('SOCIETE_ON_SEARCH_AND_LIST_GO_ON_CUSTOMER_OR_SUPPLIER_CARD')) {
  2462. if (empty($option) && $this->client > 0) {
  2463. $option = 'customer';
  2464. }
  2465. if (empty($option) && $this->fournisseur > 0) {
  2466. $option = 'supplier';
  2467. }
  2468. }
  2469. if (getDolGlobalString('SOCIETE_ADD_REF_IN_LIST') && (!empty($withpicto))) {
  2470. $code = '';
  2471. if (($this->client) && (!empty($this->code_client)) && (getDolGlobalInt('SOCIETE_ADD_REF_IN_LIST') == 1 || getDolGlobalInt('SOCIETE_ADD_REF_IN_LIST') == 2)) {
  2472. $code = $this->code_client.' - ';
  2473. }
  2474. if (($this->fournisseur) && (!empty($this->code_fournisseur)) && (getDolGlobalInt('SOCIETE_ADD_REF_IN_LIST') == 1 || getDolGlobalInt('SOCIETE_ADD_REF_IN_LIST') == 3)) {
  2475. $code .= $this->code_fournisseur.' - ';
  2476. }
  2477. if ($code) {
  2478. if (getDolGlobalInt('SOCIETE_ADD_REF_IN_LIST') == 1) {
  2479. $name = $code.' '.$name;
  2480. } else {
  2481. $name = $code;
  2482. }
  2483. }
  2484. }
  2485. if (!empty($this->name_alias) && empty($noaliasinname)) {
  2486. $name .= ' ('.$this->name_alias.')';
  2487. }
  2488. $result = '';
  2489. $params = [
  2490. 'id' => $this->id,
  2491. 'objecttype' => $this->element,
  2492. 'option' => $option,
  2493. 'nofetch' => 1,
  2494. ];
  2495. $classfortooltip = 'classfortooltip';
  2496. $dataparams = '';
  2497. if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
  2498. $classfortooltip = 'classforajaxtooltip';
  2499. $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
  2500. $label = '';
  2501. } else {
  2502. $label = implode($this->getTooltipContentArray($params));
  2503. }
  2504. $linkstart = '';
  2505. $linkend = '';
  2506. if ($option == 'customer' || $option == 'compta' || $option == 'category') {
  2507. $linkstart = '<a href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$this->id;
  2508. } elseif ($option == 'prospect' && !getDolGlobalString('SOCIETE_DISABLE_PROSPECTS')) {
  2509. $linkstart = '<a href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$this->id;
  2510. } elseif ($option == 'supplier' || $option == 'category_supplier') {
  2511. $linkstart = '<a href="'.DOL_URL_ROOT.'/fourn/card.php?socid='.$this->id;
  2512. } elseif ($option == 'agenda') {
  2513. $linkstart = '<a href="'.DOL_URL_ROOT.'/societe/agenda.php?socid='.$this->id;
  2514. } elseif ($option == 'project') {
  2515. $linkstart = '<a href="'.DOL_URL_ROOT.'/societe/project.php?socid='.$this->id;
  2516. } elseif ($option == 'margin') {
  2517. $linkstart = '<a href="'.DOL_URL_ROOT.'/margin/tabs/thirdpartyMargins.php?socid='.$this->id.'&type=1';
  2518. } elseif ($option == 'contact') {
  2519. $linkstart = '<a href="'.DOL_URL_ROOT.'/societe/contact.php?socid='.$this->id;
  2520. } elseif ($option == 'ban') {
  2521. $linkstart = '<a href="'.DOL_URL_ROOT.'/societe/paymentmodes.php?socid='.$this->id;
  2522. }
  2523. // By default
  2524. if (empty($linkstart)) {
  2525. $linkstart = '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$this->id;
  2526. }
  2527. // Add type of canvas
  2528. $linkstart .= (!empty($this->canvas) ? '&canvas='.$this->canvas : '');
  2529. // Add param to save lastsearch_values or not
  2530. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  2531. if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  2532. $add_save_lastsearch_values = 1;
  2533. }
  2534. if ($add_save_lastsearch_values) {
  2535. $linkstart .= '&save_lastsearch_values=1';
  2536. }
  2537. $linkstart .= '"';
  2538. $linkclose = '';
  2539. if (empty($notooltip)) {
  2540. if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
  2541. $label = $langs->trans("ShowCompany");
  2542. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  2543. }
  2544. $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
  2545. $linkclose .= $dataparams.' class="'.$classfortooltip.' refurl valignmiddle"';
  2546. $target_value = array('_self', '_blank', '_parent', '_top');
  2547. if (in_array($target, $target_value)) {
  2548. $linkclose .= ' target="'.dol_escape_htmltag($target).'"';
  2549. }
  2550. } else {
  2551. $linkclose .= ' class="valignmiddle"';
  2552. }
  2553. $linkstart .= $linkclose.'>';
  2554. $linkend = '</a>';
  2555. global $user;
  2556. if (!$user->hasRight('societe', 'client', 'voir') && $user->socid > 0 && $this->id != $user->socid) {
  2557. $linkstart = '';
  2558. $linkend = '';
  2559. }
  2560. $result .= $linkstart;
  2561. if ($withpicto) {
  2562. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), (' class="'.(($withpicto != 2) ? 'paddingright' : '').'"'), 0, 0, $notooltip ? 0 : 1);
  2563. }
  2564. if ($withpicto != 2) {
  2565. $result .= dol_escape_htmltag($maxlen ? dol_trunc($name, $maxlen) : $name);
  2566. }
  2567. $result .= $linkend;
  2568. global $action;
  2569. $hookmanager->initHooks(array('thirdpartydao'));
  2570. $parameters = array(
  2571. 'id'=>$this->id,
  2572. 'getnomurl' => &$result,
  2573. 'withpicto '=> $withpicto,
  2574. 'option'=> $option,
  2575. 'maxlen'=> $maxlen,
  2576. 'notooltip'=> $notooltip,
  2577. 'save_lastsearch_value'=> $save_lastsearch_value
  2578. );
  2579. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  2580. if ($reshook > 0) {
  2581. $result = $hookmanager->resPrint;
  2582. } else {
  2583. $result .= $hookmanager->resPrint;
  2584. }
  2585. return $result;
  2586. }
  2587. /**
  2588. * Return link(s) on type of thirdparty (with picto)
  2589. *
  2590. * @param int $withpicto Add picto into link (0=No picto, 1=Include picto with link, 2=Picto only)
  2591. * @param string $option ''=All
  2592. * @param int $notooltip 1=Disable tooltip
  2593. * @param string $tag Tag 'a' or 'span'
  2594. * @return string String with URL
  2595. */
  2596. public function getTypeUrl($withpicto = 0, $option = '', $notooltip = 0, $tag = 'a')
  2597. {
  2598. global $conf, $langs;
  2599. $s = '';
  2600. if (empty($option) || preg_match('/prospect/', $option)) {
  2601. if (($this->client == 2 || $this->client == 3) && !getDolGlobalString('SOCIETE_DISABLE_PROSPECTS')) {
  2602. $s .= '<'.$tag.' class="customer-back opacitymedium" title="'.$langs->trans("Prospect").'" href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$this->id.'">'.dol_substr($langs->trans("Prospect"), 0, 1).'</'.$tag.'>';
  2603. }
  2604. }
  2605. if (empty($option) || preg_match('/customer/', $option)) {
  2606. if (($this->client == 1 || $this->client == 3) && !getDolGlobalString('SOCIETE_DISABLE_CUSTOMERS')) {
  2607. $s .= '<'.$tag.' class="customer-back" title="'.$langs->trans("Customer").'" href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$this->id.'">'.dol_substr($langs->trans("Customer"), 0, 1).'</'.$tag.'>';
  2608. }
  2609. }
  2610. if (empty($option) || preg_match('/supplier/', $option)) {
  2611. if ((isModEnabled("supplier_order") || isModEnabled("supplier_invoice")) && $this->fournisseur) {
  2612. $s .= '<'.$tag.' class="vendor-back" title="'.$langs->trans("Supplier").'" href="'.DOL_URL_ROOT.'/fourn/card.php?socid='.$this->id.'">'.dol_substr($langs->trans("Supplier"), 0, 1).'</'.$tag.'>';
  2613. }
  2614. }
  2615. return $s;
  2616. }
  2617. /**
  2618. * Return label of status (activity, closed)
  2619. *
  2620. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  2621. * @return string Label of status
  2622. */
  2623. public function getLibStatut($mode = 0)
  2624. {
  2625. return $this->LibStatut($this->status, $mode);
  2626. }
  2627. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2628. /**
  2629. * Return the label of a given status
  2630. *
  2631. * @param int $status Status id
  2632. * @param int $mode 0=Long label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Long label, 5=Short label + Picto, 6=Long label + Picto
  2633. * @return string Status label
  2634. */
  2635. public function LibStatut($status, $mode = 0)
  2636. {
  2637. // phpcs:enable
  2638. global $langs;
  2639. $langs->load('companies');
  2640. $statusType = 'status4';
  2641. if ($status == 0) {
  2642. $statusType = 'status6';
  2643. }
  2644. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  2645. $this->labelStatus[0] = $langs->transnoentitiesnoconv("ActivityCeased");
  2646. $this->labelStatus[1] = $langs->transnoentitiesnoconv("InActivity");
  2647. $this->labelStatusShort[0] = $langs->transnoentitiesnoconv("ActivityCeased");
  2648. $this->labelStatusShort[1] = $langs->transnoentitiesnoconv("InActivity");
  2649. }
  2650. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  2651. }
  2652. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2653. /**
  2654. * Return list of contacts emails existing for third party
  2655. *
  2656. * @param int $addthirdparty 1=Add also a record for thirdparty email, 2=Same than 1 but add text ThirdParty in grey
  2657. * @return array Array of contacts emails
  2658. */
  2659. public function thirdparty_and_contact_email_array($addthirdparty = 0)
  2660. {
  2661. // phpcs:enable
  2662. global $langs;
  2663. $contact_emails = $this->contact_property_array('email', 1);
  2664. if ($this->email && $addthirdparty) {
  2665. if (empty($this->name)) {
  2666. $this->name = $this->nom;
  2667. }
  2668. $contact_emails['thirdparty'] = ($addthirdparty == 2 ? '<span class="opacitymedium">' : '').$langs->transnoentitiesnoconv("ThirdParty").($addthirdparty == 2 ? '</span>' : '').': '.dol_trunc($this->name, 16)." <".$this->email.">";
  2669. }
  2670. //var_dump($contact_emails)
  2671. return $contact_emails;
  2672. }
  2673. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2674. /**
  2675. * Return list of contacts mobile phone existing for third party
  2676. *
  2677. * @return array Array of contacts emails
  2678. */
  2679. public function thirdparty_and_contact_phone_array()
  2680. {
  2681. // phpcs:enable
  2682. global $langs;
  2683. $contact_phone = $this->contact_property_array('mobile');
  2684. if (!empty($this->phone)) { // If a phone of thirdparty is defined, we add it ot mobile of contacts
  2685. if (empty($this->name)) {
  2686. $this->name = $this->nom;
  2687. }
  2688. // TODO: Tester si tel non deja present dans tableau contact
  2689. $contact_phone['thirdparty'] = $langs->transnoentitiesnoconv("ThirdParty").': '.dol_trunc($this->name, 16)." <".$this->phone.">";
  2690. }
  2691. return $contact_phone;
  2692. }
  2693. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2694. /**
  2695. * Return list of contacts emails or mobile existing for third party
  2696. *
  2697. * @param string $mode 'email' or 'mobile'
  2698. * @param int $hidedisabled 1=Hide contact if disabled
  2699. * @return array Array of contacts emails or mobile. Example: array(id=>'Name <email>')
  2700. */
  2701. public function contact_property_array($mode = 'email', $hidedisabled = 0)
  2702. {
  2703. // phpcs:enable
  2704. global $langs;
  2705. $contact_property = array();
  2706. $sql = "SELECT rowid, email, statut as status, phone_mobile, lastname, poste, firstname";
  2707. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople";
  2708. $sql .= " WHERE fk_soc = ".((int) $this->id);
  2709. $sql .= " ORDER BY lastname, firstname";
  2710. $resql = $this->db->query($sql);
  2711. if ($resql) {
  2712. $nump = $this->db->num_rows($resql);
  2713. if ($nump) {
  2714. $sepa = "("; $sepb = ")";
  2715. if ($mode == 'email') {
  2716. //$sepa="&lt;"; $sepb="&gt;";
  2717. $sepa = "<"; $sepb = ">";
  2718. }
  2719. $i = 0;
  2720. while ($i < $nump) {
  2721. $obj = $this->db->fetch_object($resql);
  2722. if ($mode == 'email') {
  2723. $property = $obj->email;
  2724. } elseif ($mode == 'mobile') {
  2725. $property = $obj->phone_mobile;
  2726. } else {
  2727. $property = $obj->$mode;
  2728. }
  2729. // Show all contact. If hidedisabled is 1, showonly contacts with status = 1
  2730. if ($obj->status == 1 || empty($hidedisabled)) {
  2731. if (empty($property)) {
  2732. if ($mode == 'email') {
  2733. $property = $langs->transnoentitiesnoconv("NoEMail");
  2734. } elseif ($mode == 'mobile') {
  2735. $property = $langs->transnoentitiesnoconv("NoMobilePhone");
  2736. }
  2737. }
  2738. if (!empty($obj->poste)) {
  2739. $contact_property[$obj->rowid] = trim(dolGetFirstLastname($obj->firstname, $obj->lastname)).($obj->poste ? " - ".$obj->poste : "").(($mode != 'poste' && $property) ? " ".$sepa.$property.$sepb : '');
  2740. } else {
  2741. $contact_property[$obj->rowid] = trim(dolGetFirstLastname($obj->firstname, $obj->lastname)).(($mode != 'poste' && $property) ? " ".$sepa.$property.$sepb : '');
  2742. }
  2743. }
  2744. $i++;
  2745. }
  2746. }
  2747. } else {
  2748. dol_print_error($this->db);
  2749. }
  2750. return $contact_property;
  2751. }
  2752. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2753. /**
  2754. * Returns the contact list of this company
  2755. *
  2756. * @return array array of contacts
  2757. */
  2758. public function contact_array()
  2759. {
  2760. // phpcs:enable
  2761. $contacts = array();
  2762. $sql = "SELECT rowid, lastname, firstname FROM ".MAIN_DB_PREFIX."socpeople WHERE fk_soc = ".((int) $this->id);
  2763. $resql = $this->db->query($sql);
  2764. if ($resql) {
  2765. $nump = $this->db->num_rows($resql);
  2766. if ($nump) {
  2767. $i = 0;
  2768. while ($i < $nump) {
  2769. $obj = $this->db->fetch_object($resql);
  2770. $contacts[$obj->rowid] = dolGetFirstLastname($obj->firstname, $obj->lastname);
  2771. $i++;
  2772. }
  2773. }
  2774. } else {
  2775. dol_print_error($this->db);
  2776. }
  2777. return $contacts;
  2778. }
  2779. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2780. /**
  2781. * Returns the contact list of this company
  2782. *
  2783. * @return array $contacts array of contacts
  2784. */
  2785. public function contact_array_objects()
  2786. {
  2787. // phpcs:enable
  2788. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  2789. $contacts = array();
  2790. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."socpeople WHERE fk_soc = ".((int) $this->id);
  2791. $resql = $this->db->query($sql);
  2792. if ($resql) {
  2793. $nump = $this->db->num_rows($resql);
  2794. if ($nump) {
  2795. $i = 0;
  2796. while ($i < $nump) {
  2797. $obj = $this->db->fetch_object($resql);
  2798. $contact = new Contact($this->db);
  2799. $contact->fetch($obj->rowid);
  2800. $contacts[] = $contact;
  2801. $i++;
  2802. }
  2803. }
  2804. } else {
  2805. dol_print_error($this->db);
  2806. }
  2807. return $contacts;
  2808. }
  2809. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2810. /**
  2811. * Return property of contact from its id
  2812. *
  2813. * @param int $rowid id of contact
  2814. * @param string $mode 'email' or 'mobile'
  2815. * @return string Email of contact with format: "Full name <email>"
  2816. */
  2817. public function contact_get_property($rowid, $mode)
  2818. {
  2819. // phpcs:enable
  2820. $contact_property = '';
  2821. if (empty($rowid)) {
  2822. return '';
  2823. }
  2824. $sql = "SELECT rowid, email, phone_mobile, lastname, firstname";
  2825. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople";
  2826. $sql .= " WHERE rowid = ".((int) $rowid);
  2827. $resql = $this->db->query($sql);
  2828. if ($resql) {
  2829. $nump = $this->db->num_rows($resql);
  2830. if ($nump) {
  2831. $obj = $this->db->fetch_object($resql);
  2832. if ($mode == 'email') {
  2833. $contact_property = dol_string_nospecial(dolGetFirstLastname($obj->firstname, $obj->lastname), ' ', array(","))." <".$obj->email.">";
  2834. } elseif ($mode == 'mobile') {
  2835. $contact_property = $obj->phone_mobile;
  2836. }
  2837. }
  2838. return $contact_property;
  2839. } else {
  2840. dol_print_error($this->db);
  2841. }
  2842. return '';
  2843. }
  2844. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2845. /**
  2846. * Return bank number property of thirdparty (label or rum)
  2847. *
  2848. * @param string $mode 'label' or 'rum' or 'format'
  2849. * @return string Bank label or RUM or '' if no bank account found
  2850. */
  2851. public function display_rib($mode = 'label')
  2852. {
  2853. // phpcs:enable
  2854. require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
  2855. $bac = new CompanyBankAccount($this->db);
  2856. $bac->fetch(0, $this->id);
  2857. if ($bac->id > 0) { // If a bank account has been found for company $this->id
  2858. if ($mode == 'label') {
  2859. return $bac->getRibLabel(true);
  2860. } elseif ($mode == 'rum') {
  2861. if (empty($bac->rum)) {
  2862. require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
  2863. $prelevement = new BonPrelevement($this->db);
  2864. $bac->fetch_thirdparty();
  2865. $bac->rum = $prelevement->buildRumNumber($bac->thirdparty->code_client, $bac->datec, $bac->id);
  2866. }
  2867. return $bac->rum;
  2868. } elseif ($mode == 'format') {
  2869. return $bac->frstrecur;
  2870. } else {
  2871. return 'BadParameterToFunctionDisplayRib';
  2872. }
  2873. } else {
  2874. return '';
  2875. }
  2876. }
  2877. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2878. /**
  2879. * Return Array of RIB
  2880. *
  2881. * @return array|int 0 if KO, Array of CompanyBanckAccount if OK
  2882. */
  2883. public function get_all_rib()
  2884. {
  2885. // phpcs:enable
  2886. require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
  2887. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_rib WHERE type='ban' AND fk_soc = ".((int) $this->id);
  2888. $result = $this->db->query($sql);
  2889. if (!$result) {
  2890. $this->error++;
  2891. $this->errors[] = $this->db->lasterror;
  2892. return 0;
  2893. } else {
  2894. $num_rows = $this->db->num_rows($result);
  2895. $rib_array = array();
  2896. if ($num_rows) {
  2897. while ($obj = $this->db->fetch_object($result)) {
  2898. $rib = new CompanyBankAccount($this->db);
  2899. $rib->fetch($obj->rowid);
  2900. $rib_array[] = $rib;
  2901. }
  2902. }
  2903. return $rib_array;
  2904. }
  2905. }
  2906. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2907. /**
  2908. * Assigns a customer code from the code control module.
  2909. * Return value is stored into this->code_client
  2910. *
  2911. * @param Societe $objsoc Object thirdparty
  2912. * @param int $type Should be 0 to say customer
  2913. * @return void
  2914. */
  2915. public function get_codeclient($objsoc = 0, $type = 0)
  2916. {
  2917. // phpcs:enable
  2918. global $conf;
  2919. if (getDolGlobalString('SOCIETE_CODECLIENT_ADDON')) {
  2920. $module = $conf->global->SOCIETE_CODECLIENT_ADDON;
  2921. $dirsociete = array_merge(array('/core/modules/societe/'), $conf->modules_parts['societe']);
  2922. foreach ($dirsociete as $dirroot) {
  2923. $res = dol_include_once($dirroot.$module.'.php');
  2924. if ($res) {
  2925. break;
  2926. }
  2927. }
  2928. $mod = new $module();
  2929. $this->code_client = $mod->getNextValue($objsoc, $type);
  2930. $this->prefixCustomerIsRequired = $mod->prefixIsRequired;
  2931. dol_syslog(get_class($this)."::get_codeclient code_client=".$this->code_client." module=".$module);
  2932. }
  2933. }
  2934. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2935. /**
  2936. * Assigns a vendor code from the code control module.
  2937. * Return value is stored into this->code_fournisseur
  2938. *
  2939. * @param Societe $objsoc Object thirdparty
  2940. * @param int $type Should be 1 to say supplier
  2941. * @return void
  2942. */
  2943. public function get_codefournisseur($objsoc = 0, $type = 1)
  2944. {
  2945. // phpcs:enable
  2946. global $conf;
  2947. if (getDolGlobalString('SOCIETE_CODECLIENT_ADDON')) {
  2948. $module = $conf->global->SOCIETE_CODECLIENT_ADDON;
  2949. $dirsociete = array_merge(array('/core/modules/societe/'), $conf->modules_parts['societe']);
  2950. foreach ($dirsociete as $dirroot) {
  2951. $res = dol_include_once($dirroot.$module.'.php');
  2952. if ($res) {
  2953. break;
  2954. }
  2955. }
  2956. $mod = new $module();
  2957. $this->code_fournisseur = $mod->getNextValue($objsoc, $type);
  2958. dol_syslog(get_class($this)."::get_codefournisseur code_fournisseur=".$this->code_fournisseur." module=".$module);
  2959. }
  2960. }
  2961. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2962. /**
  2963. * Check if a client code is editable based on the parameters of the
  2964. * code control module.
  2965. *
  2966. * @return int 0=No, 1=Yes
  2967. */
  2968. public function codeclient_modifiable()
  2969. {
  2970. // phpcs:enable
  2971. global $conf;
  2972. if (getDolGlobalString('SOCIETE_CODECLIENT_ADDON')) {
  2973. $module = $conf->global->SOCIETE_CODECLIENT_ADDON;
  2974. $dirsociete = array_merge(array('/core/modules/societe/'), $conf->modules_parts['societe']);
  2975. foreach ($dirsociete as $dirroot) {
  2976. $res = dol_include_once($dirroot.$module.'.php');
  2977. if ($res) {
  2978. break;
  2979. }
  2980. }
  2981. $mod = new $module();
  2982. dol_syslog(get_class($this)."::codeclient_modifiable code_client=".$this->code_client." module=".$module);
  2983. if ($mod->code_modifiable_null && !$this->code_client) {
  2984. return 1;
  2985. }
  2986. if ($mod->code_modifiable_invalide && $this->check_codeclient() < 0) {
  2987. return 1;
  2988. }
  2989. if ($mod->code_modifiable) {
  2990. return 1; // A mettre en dernier
  2991. }
  2992. return 0;
  2993. } else {
  2994. return 0;
  2995. }
  2996. }
  2997. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2998. /**
  2999. * Check if a vendor code is editable in the code control module configuration
  3000. *
  3001. * @return int 0=No, 1=Yes
  3002. */
  3003. public function codefournisseur_modifiable()
  3004. {
  3005. // phpcs:enable
  3006. global $conf;
  3007. if (getDolGlobalString('SOCIETE_CODECLIENT_ADDON')) {
  3008. $module = $conf->global->SOCIETE_CODECLIENT_ADDON;
  3009. $dirsociete = array_merge(array('/core/modules/societe/'), $conf->modules_parts['societe']);
  3010. foreach ($dirsociete as $dirroot) {
  3011. $res = dol_include_once($dirroot.$module.'.php');
  3012. if ($res) {
  3013. break;
  3014. }
  3015. }
  3016. $mod = new $module();
  3017. dol_syslog(get_class($this)."::codefournisseur_modifiable code_founisseur=".$this->code_fournisseur." module=".$module);
  3018. if ($mod->code_modifiable_null && !$this->code_fournisseur) {
  3019. return 1;
  3020. }
  3021. if ($mod->code_modifiable_invalide && $this->check_codefournisseur() < 0) {
  3022. return 1;
  3023. }
  3024. if ($mod->code_modifiable) {
  3025. return 1; // A mettre en dernier
  3026. }
  3027. return 0;
  3028. } else {
  3029. return 0;
  3030. }
  3031. }
  3032. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3033. /**
  3034. * Check customer code
  3035. *
  3036. * @return int 0 if OK
  3037. * -1 ErrorBadCustomerCodeSyntax
  3038. * -2 ErrorCustomerCodeRequired
  3039. * -3 ErrorCustomerCodeAlreadyUsed
  3040. * -4 ErrorPrefixRequired
  3041. * -5 NotConfigured - Setup empty so any value may be ok or not
  3042. * -6 Other (see this->error)
  3043. */
  3044. public function check_codeclient()
  3045. {
  3046. // phpcs:enable
  3047. global $conf;
  3048. if (getDolGlobalString('SOCIETE_CODECLIENT_ADDON')) {
  3049. $module = $conf->global->SOCIETE_CODECLIENT_ADDON;
  3050. $dirsociete = array_merge(array('/core/modules/societe/'), $conf->modules_parts['societe']);
  3051. foreach ($dirsociete as $dirroot) {
  3052. $res = dol_include_once($dirroot.$module.'.php');
  3053. if ($res) {
  3054. break;
  3055. }
  3056. }
  3057. $mod = new $module();
  3058. dol_syslog(get_class($this)."::check_codeclient code_client=".$this->code_client." module=".$module);
  3059. $result = $mod->verif($this->db, $this->code_client, $this, 0);
  3060. if ($result) { // If error
  3061. $this->error = $mod->error;
  3062. $this->errors = $mod->errors;
  3063. }
  3064. return $result;
  3065. } else {
  3066. return 0;
  3067. }
  3068. }
  3069. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3070. /**
  3071. * Check supplier code
  3072. *
  3073. * @return int 0 if OK
  3074. * -1 ErrorBadCustomerCodeSyntax
  3075. * -2 ErrorCustomerCodeRequired
  3076. * -3 ErrorCustomerCodeAlreadyUsed
  3077. * -4 ErrorPrefixRequired
  3078. * -5 NotConfigured - Setup empty so any value may be ok or not
  3079. * -6 Other (see this->error)
  3080. */
  3081. public function check_codefournisseur()
  3082. {
  3083. // phpcs:enable
  3084. global $conf;
  3085. if (getDolGlobalString('SOCIETE_CODECLIENT_ADDON')) {
  3086. $module = $conf->global->SOCIETE_CODECLIENT_ADDON;
  3087. $dirsociete = array_merge(array('/core/modules/societe/'), $conf->modules_parts['societe']);
  3088. foreach ($dirsociete as $dirroot) {
  3089. $res = dol_include_once($dirroot.$module.'.php');
  3090. if ($res) {
  3091. break;
  3092. }
  3093. }
  3094. $mod = new $module();
  3095. dol_syslog(get_class($this)."::check_codefournisseur code_fournisseur=".$this->code_fournisseur." module=".$module);
  3096. $result = $mod->verif($this->db, $this->code_fournisseur, $this, 1);
  3097. if ($result) { // If error
  3098. $this->error = $mod->error;
  3099. $this->errors = $mod->errors;
  3100. }
  3101. return $result;
  3102. } else {
  3103. return 0;
  3104. }
  3105. }
  3106. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3107. /**
  3108. * Assigns a accounting code from the accounting code module.
  3109. * Computed value is stored into this->code_compta or this->code_compta_fournisseur according to $type.
  3110. * May be identical to the one entered or generated automatically. Currently, only the automatic generation is implemented.
  3111. *
  3112. * @param string $type Type of thirdparty ('customer' or 'supplier')
  3113. * @return int 0 if OK, <0 if $type is not valid
  3114. */
  3115. public function get_codecompta($type)
  3116. {
  3117. // phpcs:enable
  3118. global $conf;
  3119. if (getDolGlobalString('SOCIETE_CODECOMPTA_ADDON')) {
  3120. $module=$conf->global->SOCIETE_CODECOMPTA_ADDON;
  3121. $res = false;
  3122. $dirsociete = array_merge(array('/core/modules/societe/'), $conf->modules_parts['societe']);
  3123. foreach ($dirsociete as $dirroot) {
  3124. $res = dol_include_once($dirroot.$module.'.php');
  3125. if ($res) {
  3126. break;
  3127. }
  3128. }
  3129. if ($res) {
  3130. $mod = new $module();
  3131. // Set code count in $mod->code
  3132. $result = $mod->get_code($this->db, $this, $type);
  3133. if ($type == 'customer') {
  3134. $this->code_compta_client = $mod->code;
  3135. $this->code_compta = $this->code_compta_client; // For backward compatibility
  3136. } elseif ($type == 'supplier') {
  3137. $this->code_compta_fournisseur = $mod->code;
  3138. }
  3139. return $result;
  3140. } else {
  3141. $this->error = 'ErrorAccountancyCodeNotDefined';
  3142. return -1;
  3143. }
  3144. } else {
  3145. if ($type == 'customer') {
  3146. $this->code_compta_client = '';
  3147. $this->code_compta = '';
  3148. } elseif ($type == 'supplier') {
  3149. $this->code_compta_fournisseur = '';
  3150. }
  3151. return 0;
  3152. }
  3153. }
  3154. /**
  3155. * Define parent company of current company
  3156. *
  3157. * @param int $id Id of thirdparty to set or '' to remove
  3158. * @return int <0 if KO, >0 if OK
  3159. */
  3160. public function setParent($id)
  3161. {
  3162. dol_syslog(get_class($this).'::setParent', LOG_DEBUG);
  3163. if ($this->id) {
  3164. // Check if the id we want to add as parent has not already one parent that is the current id we try to update
  3165. if ($id > 0) {
  3166. $sameparent = $this->validateFamilyTree($id, $this->id, 0);
  3167. if ($sameparent < 0) {
  3168. return -1;
  3169. }
  3170. if ($sameparent == 1) {
  3171. setEventMessages('ParentCompanyToAddIsAlreadyAChildOfModifiedCompany', null, 'warnings');
  3172. return -1;
  3173. }
  3174. }
  3175. $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe SET parent = '.($id > 0 ? $id : 'null').' WHERE rowid = '.((int) $this->id);
  3176. $resql = $this->db->query($sql);
  3177. if ($resql) {
  3178. $this->parent = $id;
  3179. return 1;
  3180. } else {
  3181. return -1;
  3182. }
  3183. }
  3184. return -1;
  3185. }
  3186. /**
  3187. * Check if a thirdparty $idchild is or not inside the parents (or grand parents) of another thirdparty id $idparent.
  3188. *
  3189. * @param int $idparent Id of thirdparty to check
  3190. * @param int $idchild Id of thirdparty to compare to
  3191. * @param int $counter Counter to protect against infinite loops
  3192. * @return int <0 if KO, 0 if OK or 1 if at some level a parent company was the child to compare to
  3193. */
  3194. public function validateFamilyTree($idparent, $idchild, $counter = 0)
  3195. {
  3196. if ($counter > 100) {
  3197. dol_syslog("Too high level of parent - child for company. May be an infinite loop ?", LOG_WARNING);
  3198. }
  3199. $sql = 'SELECT s.parent';
  3200. $sql .= ' FROM '.MAIN_DB_PREFIX.'societe as s';
  3201. $sql .= ' WHERE rowid = '.((int) $idparent);
  3202. $resql = $this->db->query($sql);
  3203. if ($resql) {
  3204. $obj = $this->db->fetch_object($resql);
  3205. if ($obj->parent == '') {
  3206. return 0;
  3207. } elseif ($obj->parent == $idchild) {
  3208. return 1;
  3209. } else {
  3210. $sameparent = $this->validateFamilyTree($obj->parent, $idchild, ($counter + 1));
  3211. }
  3212. return $sameparent;
  3213. } else {
  3214. return -1;
  3215. }
  3216. }
  3217. /**
  3218. * Get parents for company
  3219. *
  3220. * @param int $company_id ID of company to search parent
  3221. * @param array $parents List of companies ID found
  3222. * @return array
  3223. */
  3224. public function getParentsForCompany($company_id, $parents = array())
  3225. {
  3226. global $langs;
  3227. if ($company_id > 0) {
  3228. $sql = "SELECT parent FROM " . MAIN_DB_PREFIX . "societe WHERE rowid = ".((int) $company_id);
  3229. $resql = $this->db->query($sql);
  3230. if ($resql) {
  3231. if ($obj = $this->db->fetch_object($resql)) {
  3232. $parent = $obj->parent;
  3233. if ($parent > 0 && !in_array($parent, $parents)) {
  3234. $parents[] = $parent;
  3235. return $this->getParentsForCompany($parent, $parents);
  3236. } else {
  3237. return $parents;
  3238. }
  3239. }
  3240. $this->db->free($resql);
  3241. } else {
  3242. setEventMessage($langs->trans('GetCompanyParentsError', $this->db->lasterror()), 'errors');
  3243. }
  3244. }
  3245. // Return a default value when $company_id is not greater than 0
  3246. return array();
  3247. }
  3248. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3249. /**
  3250. * Returns if a profid sould be verified to be unique
  3251. *
  3252. * @param int $idprof 1,2,3,4,5,6 (Example: 1=siren, 2=siret, 3=naf, 4=rcs/rm, 5=eori, 6=idprof6)
  3253. * @return boolean true if the ID must be unique
  3254. */
  3255. public function id_prof_verifiable($idprof)
  3256. {
  3257. // phpcs:enable
  3258. global $conf;
  3259. switch ($idprof) {
  3260. case 1:
  3261. $ret = (!getDolGlobalString('SOCIETE_IDPROF1_UNIQUE') ? false : true);
  3262. break;
  3263. case 2:
  3264. $ret = (!getDolGlobalString('SOCIETE_IDPROF2_UNIQUE') ? false : true);
  3265. break;
  3266. case 3:
  3267. $ret = (!getDolGlobalString('SOCIETE_IDPROF3_UNIQUE') ? false : true);
  3268. break;
  3269. case 4:
  3270. $ret = (!getDolGlobalString('SOCIETE_IDPROF4_UNIQUE') ? false : true);
  3271. break;
  3272. case 5:
  3273. $ret = (!getDolGlobalString('SOCIETE_IDPROF5_UNIQUE') ? false : true);
  3274. break;
  3275. case 6:
  3276. $ret = (!getDolGlobalString('SOCIETE_IDPROF6_UNIQUE') ? false : true);
  3277. break;
  3278. default:
  3279. $ret = false;
  3280. }
  3281. return $ret;
  3282. }
  3283. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3284. /**
  3285. * Verify if a profid exists into database for others thirds
  3286. *
  3287. * @param string $idprof 'idprof1','idprof2','idprof3','idprof4','idprof5','idprof6','email' (Example: idprof1=siren, idprof2=siret, idprof3=naf, idprof4=rcs/rm)
  3288. * @param string $value Value of profid
  3289. * @param int $socid Id of thirdparty to exclude (if update)
  3290. * @return boolean True if exists, False if not
  3291. */
  3292. public function id_prof_exists($idprof, $value, $socid = 0)
  3293. {
  3294. // phpcs:enable
  3295. $field = $idprof;
  3296. switch ($idprof) { // For backward compatibility
  3297. case '1':
  3298. case 'idprof1':
  3299. $field = "siren";
  3300. break;
  3301. case '2':
  3302. case 'idprof2':
  3303. $field = "siret";
  3304. break;
  3305. case '3':
  3306. case 'idprof3':
  3307. $field = "ape";
  3308. break;
  3309. case '4':
  3310. case 'idprof4':
  3311. $field = "idprof4";
  3312. break;
  3313. case '5':
  3314. $field = "idprof5";
  3315. break;
  3316. case '6':
  3317. $field = "idprof6";
  3318. break;
  3319. }
  3320. //Verify duplicate entries
  3321. $sql = "SELECT COUNT(*) as nb FROM ".MAIN_DB_PREFIX."societe WHERE ".$field." = '".$this->db->escape($value)."' AND entity IN (".getEntity('societe').")";
  3322. if ($socid) {
  3323. $sql .= " AND rowid <> ".$socid;
  3324. }
  3325. $resql = $this->db->query($sql);
  3326. if ($resql) {
  3327. $obj = $this->db->fetch_object($resql);
  3328. $count = $obj->nb;
  3329. } else {
  3330. $count = 0;
  3331. print $this->db->error();
  3332. }
  3333. $this->db->free($resql);
  3334. if ($count > 0) {
  3335. return true;
  3336. } else {
  3337. return false;
  3338. }
  3339. }
  3340. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3341. /**
  3342. * Check the validity of a professional identifier according to the country of the company (siren, siret, ...)
  3343. *
  3344. * @param int $idprof 1,2,3,4 (Exemple: 1=siren,2=siret,3=naf,4=rcs/rm)
  3345. * @param Societe $soc Objet societe
  3346. * @return int <=0 if KO, >0 if OK
  3347. * TODO better to have this in a lib than into a business class
  3348. */
  3349. public function id_prof_check($idprof, $soc)
  3350. {
  3351. // phpcs:enable
  3352. global $conf;
  3353. $ok = 1;
  3354. if (getDolGlobalString('MAIN_DISABLEPROFIDRULES')) {
  3355. return 1;
  3356. }
  3357. // Check SIREN if country FR
  3358. if ($idprof == 1 && $soc->country_code == 'FR') {
  3359. $chaine = trim($this->idprof1);
  3360. $chaine = preg_replace('/(\s)/', '', $chaine);
  3361. if (!is_numeric($chaine)) {
  3362. return -1;
  3363. }
  3364. if (dol_strlen($chaine) != 9) {
  3365. return -1;
  3366. }
  3367. // on prend chaque chiffre un par un
  3368. // si son index (position dans la chaîne en commence à 0 au premier caractère) est impair
  3369. // on double sa valeur et si cette dernière est supérieure à 9, on lui retranche 9
  3370. // on ajoute cette valeur à la somme totale
  3371. $sum = 0;
  3372. for ($index = 0; $index < 9; $index++) {
  3373. $number = (int) $chaine[$index];
  3374. if (($index % 2) != 0) {
  3375. if (($number *= 2) > 9) {
  3376. $number -= 9;
  3377. }
  3378. }
  3379. $sum += $number;
  3380. }
  3381. // le numéro est valide si la somme des chiffres est multiple de 10
  3382. if (($sum % 10) != 0) {
  3383. return -1;
  3384. }
  3385. }
  3386. // Verifie SIRET si pays FR
  3387. if ($idprof == 2 && $soc->country_code == 'FR') {
  3388. $chaine = trim($this->idprof2);
  3389. $chaine = preg_replace('/(\s)/', '', $chaine);
  3390. if (!is_numeric($chaine)) {
  3391. return -1;
  3392. }
  3393. if (dol_strlen($chaine) != 14) {
  3394. return -1;
  3395. }
  3396. // on prend chaque chiffre un par un
  3397. // si son index (position dans la chaîne en commence à 0 au premier caractère) est pair
  3398. // on double sa valeur et si cette dernière est supérieure à 9, on lui retranche 9
  3399. // on ajoute cette valeur à la somme totale
  3400. $sum = 0;
  3401. for ($index = 0; $index < 14; $index++) {
  3402. $number = (int) $chaine[$index];
  3403. if (($index % 2) == 0) {
  3404. if (($number *= 2) > 9) {
  3405. $number -= 9;
  3406. }
  3407. }
  3408. $sum += $number;
  3409. }
  3410. // le numéro est valide si la somme des chiffres est multiple de 10
  3411. if (($sum % 10) != 0) {
  3412. return -1;
  3413. }
  3414. }
  3415. //Verify CIF/NIF/NIE if pays ES
  3416. //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
  3417. if ($idprof == 1 && $soc->country_code == 'ES') {
  3418. $string = trim($this->idprof1);
  3419. $string = preg_replace('/(\s)/', '', $string);
  3420. $string = strtoupper($string);
  3421. //Check format
  3422. 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)) {
  3423. return 0;
  3424. }
  3425. $num = array();
  3426. for ($i = 0; $i < 9; $i++) {
  3427. $num[$i] = substr($string, $i, 1);
  3428. }
  3429. //Check NIF
  3430. if (preg_match('/(^[0-9]{8}[A-Z]{1}$)/', $string)) {
  3431. if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($string, 0, 8) % 23, 1)) {
  3432. return 1;
  3433. } else {
  3434. return -1;
  3435. }
  3436. }
  3437. //algorithm checking type code CIF
  3438. $sum = $num[2] + $num[4] + $num[6];
  3439. for ($i = 1; $i < 8; $i += 2) {
  3440. $sum += intval(substr((2 * $num[$i]), 0, 1)) + intval(substr((2 * $num[$i]), 1, 1));
  3441. }
  3442. $n = 10 - substr($sum, strlen($sum) - 1, 1);
  3443. //Chek special NIF
  3444. if (preg_match('/^[KLM]{1}/', $string)) {
  3445. if ($num[8] == chr(64 + $n) || $num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr($string, 1, 8) % 23, 1)) {
  3446. return 1;
  3447. } else {
  3448. return -1;
  3449. }
  3450. }
  3451. //Check CIF
  3452. if (preg_match('/^[ABCDEFGHJNPQRSUVW]{1}/', $string)) {
  3453. if ($num[8] == chr(64 + $n) || $num[8] == substr($n, strlen($n) - 1, 1)) {
  3454. return 2;
  3455. } else {
  3456. return -2;
  3457. }
  3458. }
  3459. //Check NIE T
  3460. if (preg_match('/^[T]{1}/', $string)) {
  3461. if ($num[8] == preg_match('/^[T]{1}[A-Z0-9]{8}$/', $string)) {
  3462. return 3;
  3463. } else {
  3464. return -3;
  3465. }
  3466. }
  3467. //Check NIE XYZ
  3468. if (preg_match('/^[XYZ]{1}/', $string)) {
  3469. if ($num[8] == substr('TRWAGMYFPDXBNJZSQVHLCKE', substr(str_replace(array('X', 'Y', 'Z'), array('0', '1', '2'), $string), 0, 8) % 23, 1)) {
  3470. return 3;
  3471. } else {
  3472. return -3;
  3473. }
  3474. }
  3475. //Can not be verified
  3476. return -4;
  3477. }
  3478. //Verify NIF if country is PT
  3479. //Returns: 1 if NIF ok, -1 if NIF bad, 0 if unexpected bad
  3480. if ($idprof == 1 && $soc->country_code == 'PT') {
  3481. $string = trim($this->idprof1);
  3482. $string = preg_replace('/(\s)/', '', $string);
  3483. //Check NIF
  3484. if (preg_match('/(^[0-9]{9}$)/', $string)) {
  3485. return 1;
  3486. } else {
  3487. return -1;
  3488. }
  3489. }
  3490. //Verify NIF if country is DZ
  3491. //Returns: 1 if NIF ok, -1 if NIF bad, 0 if unexpected bad
  3492. if ($idprof == 1 && $soc->country_code == 'DZ') {
  3493. $string = trim($this->idprof1);
  3494. $string = preg_replace('/(\s)/', '', $string);
  3495. //Check NIF
  3496. if (preg_match('/(^[0-9]{15}$)/', $string)) {
  3497. return 1;
  3498. } else {
  3499. return -1;
  3500. }
  3501. }
  3502. //Verify ID Prof 1 if country is BE (xxxx.xxx.xxx) (https://economie.fgov.be/fr/themes/entreprises/banque-carrefour-des/actualites/structure-du-numero)
  3503. //Returns: 1 if ok, -1 if bad, 0 if unexpected bad
  3504. if ($idprof == 1 && $soc->country_code == 'BE') {
  3505. $string = trim($this->idprof1);
  3506. $string = preg_replace('/(\s)/', '', $string);
  3507. //Check
  3508. if (preg_match('/(^[0-9]{4}\.[0-9]{3}\.[0-9]{3}$)/', $string)) {
  3509. return 1;
  3510. } else {
  3511. return -1;
  3512. }
  3513. }
  3514. return $ok;
  3515. }
  3516. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3517. /**
  3518. * Return an url to check online a professional id or empty string
  3519. *
  3520. * @param int $idprof 1,2,3,4 (Example: 1=siren,2=siret,3=naf,4=rcs/rm)
  3521. * @param Societe $thirdparty Object thirdparty
  3522. * @return string Url or empty string if no URL known
  3523. * TODO better in a lib than into business class
  3524. */
  3525. public function id_prof_url($idprof, $thirdparty)
  3526. {
  3527. // phpcs:enable
  3528. global $conf, $langs, $hookmanager;
  3529. $url = '';
  3530. $action = '';
  3531. $hookmanager->initHooks(array('idprofurl'));
  3532. $parameters = array('idprof'=>$idprof, 'company'=>$thirdparty);
  3533. $reshook = $hookmanager->executeHooks('getIdProfUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  3534. if (empty($reshook)) {
  3535. if (getDolGlobalString('MAIN_DISABLEPROFIDRULES')) {
  3536. return '';
  3537. }
  3538. // TODO Move links to validate professional ID into a dictionary table "country" + "link"
  3539. $strippedIdProf1 = str_replace(' ', '', $thirdparty->idprof1);
  3540. if ($idprof == 1 && $thirdparty->country_code == 'FR') {
  3541. $url = 'https://annuaire-entreprises.data.gouv.fr/entreprise/'.$strippedIdProf1; // See also http://avis-situation-sirene.insee.fr/
  3542. }
  3543. if ($idprof == 1 && ($thirdparty->country_code == 'GB' || $thirdparty->country_code == 'UK')) {
  3544. $url = 'https://beta.companieshouse.gov.uk/company/'.$strippedIdProf1;
  3545. }
  3546. if ($idprof == 1 && $thirdparty->country_code == 'ES') {
  3547. $url = 'http://www.e-informa.es/servlet/app/portal/ENTP/screen/SProducto/prod/ETIQUETA_EMPRESA/nif/'.$strippedIdProf1;
  3548. }
  3549. if ($idprof == 1 && $thirdparty->country_code == 'IN') {
  3550. $url = 'http://www.tinxsys.com/TinxsysInternetWeb/dealerControllerServlet?tinNumber='.$strippedIdProf1.';&searchBy=TIN&backPage=searchByTin_Inter.jsp';
  3551. }
  3552. if ($idprof == 1 && $thirdparty->country_code == 'DZ') {
  3553. $url = 'http://nif.mfdgi.gov.dz/nif.asp?Nif='.$strippedIdProf1;
  3554. }
  3555. if ($idprof == 1 && $thirdparty->country_code == 'PT') {
  3556. $url = 'http://www.nif.pt/'.$strippedIdProf1;
  3557. }
  3558. if ($url) {
  3559. return '<a target="_blank" rel="noopener noreferrer" href="'.$url.'">'.$langs->trans("Check").'</a>';
  3560. }
  3561. } else {
  3562. return $hookmanager->resPrint;
  3563. }
  3564. return '';
  3565. }
  3566. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3567. /**
  3568. * Indicates if the company has projects
  3569. *
  3570. * @return bool true if the company has projects, false otherwise
  3571. */
  3572. public function has_projects()
  3573. {
  3574. // phpcs:enable
  3575. $sql = "SELECT COUNT(*) as numproj FROM ".MAIN_DB_PREFIX."projet WHERE fk_soc = ".((int) $this->id);
  3576. $resql = $this->db->query($sql);
  3577. if ($resql) {
  3578. $obj = $this->db->fetch_object($resql);
  3579. $count = $obj->numproj;
  3580. } else {
  3581. $count = 0;
  3582. print $this->db->error();
  3583. }
  3584. $this->db->free($resql);
  3585. return ($count > 0);
  3586. }
  3587. /**
  3588. * Load information for tab info
  3589. *
  3590. * @param int $id Id of thirdparty to load
  3591. * @return void
  3592. */
  3593. public function info($id)
  3594. {
  3595. $sql = "SELECT s.rowid, s.nom as name, s.datec, tms as datem,";
  3596. $sql .= " fk_user_creat, fk_user_modif";
  3597. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  3598. $sql .= " WHERE s.rowid = ".((int) $id);
  3599. $result = $this->db->query($sql);
  3600. if ($result) {
  3601. if ($this->db->num_rows($result)) {
  3602. $obj = $this->db->fetch_object($result);
  3603. $this->id = $obj->rowid;
  3604. $this->user_creation_id = $obj->fk_user_creat;
  3605. $this->user_modification_id = $obj->fk_user_modif;
  3606. $this->date_creation = $this->db->jdate($obj->datec);
  3607. $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
  3608. $this->ref = $obj->name;
  3609. }
  3610. $this->db->free($result);
  3611. } else {
  3612. dol_print_error($this->db);
  3613. }
  3614. }
  3615. /**
  3616. * Return if third party is a company (Business) or an end user (Consumer)
  3617. *
  3618. * @return boolean true=is a company, false=a and user
  3619. */
  3620. public function isACompany()
  3621. {
  3622. global $conf;
  3623. // Define if third party is treated as company (or not) when nature is unknown
  3624. $isacompany = !getDolGlobalString('MAIN_UNKNOWN_CUSTOMERS_ARE_COMPANIES') ? 0 : 1; // 0 by default
  3625. if (!empty($this->tva_intra)) {
  3626. $isacompany = 1;
  3627. } elseif (!empty($this->idprof1) || !empty($this->idprof2) || !empty($this->idprof3) || !empty($this->idprof4) || !empty($this->idprof5) || !empty($this->idprof6)) {
  3628. $isacompany = 1;
  3629. } elseif (!empty($this->typent_code) && $this->typent_code != 'TE_UNKNOWN') {
  3630. // TODO Add a field is_a_company into dictionary
  3631. if (preg_match('/^TE_PRIVATE/', $this->typent_code)) {
  3632. $isacompany = 0;
  3633. } else {
  3634. $isacompany = 1;
  3635. }
  3636. }
  3637. return $isacompany;
  3638. }
  3639. /**
  3640. * Return if a company is inside the EEC (European Economic Community)
  3641. *
  3642. * @return boolean true = country inside EEC, false = country outside EEC
  3643. */
  3644. public function isInEEC()
  3645. {
  3646. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  3647. return isInEEC($this);
  3648. }
  3649. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3650. /**
  3651. * Load the list of provider categories
  3652. *
  3653. * @return int 0 if success, <> 0 if error
  3654. */
  3655. public function LoadSupplierCateg()
  3656. {
  3657. // phpcs:enable
  3658. $this->SupplierCategories = array();
  3659. $sql = "SELECT rowid, label";
  3660. $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
  3661. $sql .= " WHERE type = ".Categorie::TYPE_SUPPLIER;
  3662. $resql = $this->db->query($sql);
  3663. if ($resql) {
  3664. while ($obj = $this->db->fetch_object($resql)) {
  3665. $this->SupplierCategories[$obj->rowid] = $obj->label;
  3666. }
  3667. return 0;
  3668. } else {
  3669. return -1;
  3670. }
  3671. }
  3672. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3673. /**
  3674. * Insert link supplier - category
  3675. *
  3676. * @param int $categorie_id Id of category
  3677. * @return int 0 if success, <> 0 if error
  3678. */
  3679. public function AddFournisseurInCategory($categorie_id)
  3680. {
  3681. // phpcs:enable
  3682. if ($categorie_id > 0 && $this->id > 0) {
  3683. $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_fournisseur (fk_categorie, fk_soc) ";
  3684. $sql .= " VALUES (".((int) $categorie_id).", ".((int) $this->id).")";
  3685. if ($resql = $this->db->query($sql)) {
  3686. return 0;
  3687. }
  3688. } else {
  3689. return 0;
  3690. }
  3691. return -1;
  3692. }
  3693. /**
  3694. * Return number of mass Emailing received by this contacts with its email
  3695. *
  3696. * @return int Number of EMailings
  3697. */
  3698. public function getNbOfEMailings()
  3699. {
  3700. $sql = "SELECT count(mc.email) as nb";
  3701. $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m";
  3702. $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' ";
  3703. $sql .= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success
  3704. $resql = $this->db->query($sql);
  3705. if ($resql) {
  3706. $obj = $this->db->fetch_object($resql);
  3707. $nb = $obj->nb;
  3708. $this->db->free($resql);
  3709. return $nb;
  3710. } else {
  3711. $this->error = $this->db->error();
  3712. return -1;
  3713. }
  3714. }
  3715. /**
  3716. * Set "blacklist" mailing status
  3717. *
  3718. * @param int $no_email 1=Do not send mailing, 0=Ok to recieve mailling
  3719. * @return int <0 if KO, >0 if OK
  3720. */
  3721. public function setNoEmail($no_email)
  3722. {
  3723. $error = 0;
  3724. // Update mass emailing flag into table mailing_unsubscribe
  3725. if ($this->email) {
  3726. $this->db->begin();
  3727. if ($no_email) {
  3728. $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing', 0).") AND email = '".$this->db->escape($this->email)."'";
  3729. $resql = $this->db->query($sql);
  3730. if ($resql) {
  3731. $obj = $this->db->fetch_object($resql);
  3732. $noemail = $obj->nb;
  3733. if (empty($noemail)) {
  3734. $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_unsubscribe(email, entity, date_creat) VALUES ('".$this->db->escape($this->email)."', ".getEntity('mailing', 0).", '".$this->db->idate(dol_now())."')";
  3735. $resql = $this->db->query($sql);
  3736. if (!$resql) {
  3737. $error++;
  3738. $this->error = $this->db->lasterror();
  3739. $this->errors[] = $this->error;
  3740. }
  3741. }
  3742. } else {
  3743. $error++;
  3744. $this->error = $this->db->lasterror();
  3745. $this->errors[] = $this->error;
  3746. }
  3747. } else {
  3748. $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = '".$this->db->escape($this->email)."' AND entity IN (".getEntity('mailing', 0).")";
  3749. $resql = $this->db->query($sql);
  3750. if (!$resql) {
  3751. $error++;
  3752. $this->error = $this->db->lasterror();
  3753. $this->errors[] = $this->error;
  3754. }
  3755. }
  3756. if (empty($error)) {
  3757. $this->no_email = $no_email;
  3758. $this->db->commit();
  3759. return 1;
  3760. } else {
  3761. $this->db->rollback();
  3762. return $error * -1;
  3763. }
  3764. }
  3765. return 0;
  3766. }
  3767. /**
  3768. * get "blacklist" mailing status
  3769. * set no_email attribut to 1 or 0
  3770. *
  3771. * @return int <0 if KO, >0 if OK
  3772. */
  3773. public function getNoEmail()
  3774. {
  3775. if ($this->email) {
  3776. $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($this->email)."'";
  3777. $resql = $this->db->query($sql);
  3778. if ($resql) {
  3779. $obj = $this->db->fetch_object($resql);
  3780. $this->no_email = $obj->nb;
  3781. return 1;
  3782. } else {
  3783. $this->error = $this->db->lasterror();
  3784. $this->errors[] = $this->error;
  3785. return -1;
  3786. }
  3787. }
  3788. return 0;
  3789. }
  3790. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3791. /**
  3792. * Create a third party into database from a member object
  3793. *
  3794. * @param Adherent $member Object member
  3795. * @param string $socname Name of third party to force
  3796. * @param string $socalias Alias name of third party to force
  3797. * @param string $customercode Customer code
  3798. * @return int <0 if KO, id of created account if OK
  3799. */
  3800. public function create_from_member(Adherent $member, $socname = '', $socalias = '', $customercode = '')
  3801. {
  3802. // phpcs:enable
  3803. global $conf, $user, $langs;
  3804. dol_syslog(get_class($this)."::create_from_member", LOG_DEBUG);
  3805. $fullname = $member->getFullName($langs);
  3806. if ($member->morphy == 'mor') {
  3807. if (empty($socname)) {
  3808. $socname = $member->company? $member->company : $member->societe;
  3809. }
  3810. if (!empty($fullname) && empty($socalias)) {
  3811. $socalias = $fullname;
  3812. }
  3813. } elseif (empty($socname) && $member->morphy == 'phy') {
  3814. if (empty($socname)) {
  3815. $socname = $fullname;
  3816. }
  3817. if (!empty($member->company) && empty($socalias)) {
  3818. $socalias = $member->company;
  3819. }
  3820. }
  3821. $name = $socname;
  3822. $alias = $socalias ? $socalias : '';
  3823. // Positionne parametres
  3824. $this->nom = $name; // TODO deprecated
  3825. $this->name = $name;
  3826. $this->name_alias = $alias;
  3827. $this->address = $member->address;
  3828. $this->zip = $member->zip;
  3829. $this->town = $member->town;
  3830. $this->country_code = $member->country_code;
  3831. $this->country_id = $member->country_id;
  3832. $this->phone = $member->phone; // Prof phone
  3833. $this->email = $member->email;
  3834. $this->socialnetworks = $member->socialnetworks;
  3835. $this->entity = $member->entity;
  3836. $this->client = 1; // A member is a customer by default
  3837. $this->code_client = ($customercode ? $customercode : -1);
  3838. $this->code_fournisseur = -1;
  3839. $this->typent_code = ($member->morphy == 'phy' ? 'TE_PRIVATE' : 0);
  3840. $this->typent_id = $this->typent_code ? dol_getIdFromCode($this->db, $this->typent_code, 'c_typent', 'id', 'code') : 0;
  3841. $this->db->begin();
  3842. // Cree et positionne $this->id
  3843. $result = $this->create($user);
  3844. if ($result >= 0) {
  3845. // Auto-create contact on thirdparty creation
  3846. if (getDolGlobalString('THIRDPARTY_DEFAULT_CREATE_CONTACT')) {
  3847. // Fill fields needed by contact
  3848. $this->name_bis = $member->lastname;
  3849. $this->firstname = $member->firstname;
  3850. $this->civility_id = $member->civility_id;
  3851. dol_syslog("We ask to create a contact/address too", LOG_DEBUG);
  3852. $result = $this->create_individual($user);
  3853. if ($result < 0) {
  3854. setEventMessages($this->error, $this->errors, 'errors');
  3855. $this->db->rollback();
  3856. return -1;
  3857. }
  3858. }
  3859. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent";
  3860. $sql .= " SET fk_soc = ".((int) $this->id);
  3861. $sql .= " WHERE rowid = ".((int) $member->id);
  3862. $resql = $this->db->query($sql);
  3863. if ($resql) {
  3864. $this->db->commit();
  3865. return $this->id;
  3866. } else {
  3867. $this->error = $this->db->error();
  3868. $this->db->rollback();
  3869. return -1;
  3870. }
  3871. } else {
  3872. // $this->error deja positionne
  3873. dol_syslog(get_class($this)."::create_from_member - 2 - ".$this->error." - ".join(',', $this->errors), LOG_ERR);
  3874. $this->db->rollback();
  3875. return $result;
  3876. }
  3877. }
  3878. /**
  3879. * Set properties with value into $conf
  3880. *
  3881. * @param Conf $conf Conf object (possibility to use another entity)
  3882. * @return void
  3883. */
  3884. public function setMysoc(Conf $conf)
  3885. {
  3886. global $langs;
  3887. $this->id = 0;
  3888. $this->entity = $conf->entity;
  3889. $this->name = getDolGlobalString('MAIN_INFO_SOCIETE_NOM');
  3890. $this->nom = $this->name; // deprecated
  3891. $this->address = getDolGlobalString('MAIN_INFO_SOCIETE_ADDRESS');
  3892. $this->zip = getDolGlobalString('MAIN_INFO_SOCIETE_ZIP');
  3893. $this->town = getDolGlobalString('MAIN_INFO_SOCIETE_TOWN');
  3894. $this->region_code = getDolGlobalString('MAIN_INFO_SOCIETE_REGION');
  3895. $this->socialobject = getDolGlobalString('MAIN_INFO_SOCIETE_OBJECT');
  3896. $this->note_private = getDolGlobalString('MAIN_INFO_SOCIETE_NOTE');
  3897. // We define country_id, country_code and country
  3898. $country_id = $country_code = $country_label = '';
  3899. if (getDolGlobalString('MAIN_INFO_SOCIETE_COUNTRY')) {
  3900. $tmp = explode(':', $conf->global->MAIN_INFO_SOCIETE_COUNTRY);
  3901. $country_id = $tmp[0];
  3902. if (!empty($tmp[1])) { // If $conf->global->MAIN_INFO_SOCIETE_COUNTRY is "id:code:label"
  3903. $country_code = $tmp[1];
  3904. $country_label = $tmp[2];
  3905. } else {
  3906. // For backward compatibility
  3907. dol_syslog("Your country setup use an old syntax. Reedit it using setup area.", LOG_WARNING);
  3908. include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  3909. $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
  3910. $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
  3911. }
  3912. }
  3913. $this->country_id = $country_id;
  3914. $this->country_code = $country_code;
  3915. $this->country = $country_label;
  3916. if (is_object($langs)) {
  3917. $this->country = ($langs->trans('Country'.$country_code) != 'Country'.$country_code) ? $langs->trans('Country'.$country_code) : $country_label;
  3918. }
  3919. //TODO This could be replicated for region but function `getRegion` didn't exist, so I didn't added it.
  3920. // We define state_id, state_code and state
  3921. $state_id = 0; $state_code = $state_label = '';
  3922. if (getDolGlobalString('MAIN_INFO_SOCIETE_STATE')) {
  3923. $tmp = explode(':', $conf->global->MAIN_INFO_SOCIETE_STATE);
  3924. $state_id = $tmp[0];
  3925. if (!empty($tmp[1])) { // If $conf->global->MAIN_INFO_SOCIETE_STATE is "id:code:label"
  3926. $state_code = $tmp[1];
  3927. $state_label = $tmp[2];
  3928. } else { // For backward compatibility
  3929. dol_syslog("Your setup of State has an old syntax (entity=".$conf->entity."). Go in Home - Setup - Organization then Save should remove this error.", LOG_ERR);
  3930. include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  3931. $state_code = getState($state_id, 2, $this->db); // This need a SQL request, but it's the old feature that should not be used anymore
  3932. $state_label = getState($state_id, 0, $this->db); // This need a SQL request, but it's the old feature that should not be used anymore
  3933. }
  3934. }
  3935. $this->state_id = $state_id;
  3936. $this->state_code = $state_code;
  3937. $this->state = $state_label;
  3938. if (is_object($langs)) {
  3939. $this->state = ($langs->trans('State'.$state_code) != 'State'.$state_code) ? $langs->trans('State'.$state_code) : $state_label;
  3940. }
  3941. $this->phone = getDolGlobalString('MAIN_INFO_SOCIETE_TEL');
  3942. $this->fax = getDolGlobalString('MAIN_INFO_SOCIETE_FAX');
  3943. $this->url = getDolGlobalString('MAIN_INFO_SOCIETE_WEB');
  3944. // Social networks
  3945. $facebook_url = getDolGlobalString('MAIN_INFO_SOCIETE_FACEBOOK_URL');
  3946. $twitter_url = getDolGlobalString('MAIN_INFO_SOCIETE_TWITTER_URL');
  3947. $linkedin_url = getDolGlobalString('MAIN_INFO_SOCIETE_LINKEDIN_URL');
  3948. $instagram_url = getDolGlobalString('MAIN_INFO_SOCIETE_INSTAGRAM_URL');
  3949. $youtube_url = getDolGlobalString('MAIN_INFO_SOCIETE_YOUTUBE_URL');
  3950. $github_url = getDolGlobalString('MAIN_INFO_SOCIETE_GITHUB_URL');
  3951. $this->socialnetworks = array();
  3952. if (!empty($facebook_url)) {
  3953. $this->socialnetworks['facebook'] = $facebook_url;
  3954. }
  3955. if (!empty($twitter_url)) {
  3956. $this->socialnetworks['twitter'] = $twitter_url;
  3957. }
  3958. if (!empty($linkedin_url)) {
  3959. $this->socialnetworks['linkedin'] = $linkedin_url;
  3960. }
  3961. if (!empty($instagram_url)) {
  3962. $this->socialnetworks['instagram'] = $instagram_url;
  3963. }
  3964. if (!empty($youtube_url)) {
  3965. $this->socialnetworks['youtube'] = $youtube_url;
  3966. }
  3967. if (!empty($github_url)) {
  3968. $this->socialnetworks['github'] = $github_url;
  3969. }
  3970. // Id prof generiques
  3971. $this->idprof1 = getDolGlobalString('MAIN_INFO_SIREN');
  3972. $this->idprof2 = getDolGlobalString('MAIN_INFO_SIRET');
  3973. $this->idprof3 = getDolGlobalString('MAIN_INFO_APE');
  3974. $this->idprof4 = getDolGlobalString('MAIN_INFO_RCS');
  3975. $this->idprof5 = getDolGlobalString('MAIN_INFO_PROFID5');
  3976. $this->idprof6 = getDolGlobalString('MAIN_INFO_PROFID6');
  3977. $this->tva_intra = getDolGlobalString('MAIN_INFO_TVAINTRA'); // VAT number, not necessarly INTRA.
  3978. $this->managers = getDolGlobalString('MAIN_INFO_SOCIETE_MANAGERS');
  3979. $this->capital = getDolGlobalString('MAIN_INFO_CAPITAL');
  3980. $this->forme_juridique_code = getDolGlobalString('MAIN_INFO_SOCIETE_FORME_JURIDIQUE');
  3981. $this->email = getDolGlobalString('MAIN_INFO_SOCIETE_MAIL');
  3982. $this->default_lang = getDolGlobalString('MAIN_LANG_DEFAULT', 'auto');
  3983. $this->logo =getDolGlobalString('MAIN_INFO_SOCIETE_LOGO');
  3984. $this->logo_small = getDolGlobalString('MAIN_INFO_SOCIETE_LOGO_SMALL');
  3985. $this->logo_mini = getDolGlobalString('MAIN_INFO_SOCIETE_LOGO_MINI');
  3986. $this->logo_squarred = getDolGlobalString('MAIN_INFO_SOCIETE_LOGO_SQUARRED');
  3987. $this->logo_squarred_small = getDolGlobalString('MAIN_INFO_SOCIETE_LOGO_SQUARRED_SMALL');
  3988. $this->logo_squarred_mini = getDolGlobalString('MAIN_INFO_SOCIETE_LOGO_SQUARRED_MINI');
  3989. // Define if company use vat or not
  3990. $this->tva_assuj = $conf->global->FACTURE_TVAOPTION;
  3991. // Define if company use local taxes
  3992. $this->localtax1_assuj = ((isset($conf->global->FACTURE_LOCAL_TAX1_OPTION) && (getDolGlobalString('FACTURE_LOCAL_TAX1_OPTION') == '1' || getDolGlobalString('FACTURE_LOCAL_TAX1_OPTION') == 'localtax1on')) ? 1 : 0);
  3993. $this->localtax2_assuj = ((isset($conf->global->FACTURE_LOCAL_TAX2_OPTION) && (getDolGlobalString('FACTURE_LOCAL_TAX2_OPTION') == '1' || getDolGlobalString('FACTURE_LOCAL_TAX2_OPTION') == 'localtax2on')) ? 1 : 0);
  3994. }
  3995. /**
  3996. * Initialise an instance with random values.
  3997. * Used to build previews or test instances.
  3998. * id must be 0 if object instance is a specimen.
  3999. *
  4000. * @return int >0 if ok
  4001. */
  4002. public function initAsSpecimen()
  4003. {
  4004. $now = dol_now();
  4005. // Initialize parameters
  4006. $this->id = 0;
  4007. $this->entity = 1;
  4008. $this->name = 'THIRDPARTY SPECIMEN '.dol_print_date($now, 'dayhourlog');
  4009. $this->nom = $this->name; // For backward compatibility
  4010. $this->ref_ext = 'Ref ext';
  4011. $this->specimen = 1;
  4012. $this->address = '21 jump street';
  4013. $this->zip = '99999';
  4014. $this->town = 'MyTown';
  4015. $this->state_id = 1;
  4016. $this->state_code = 'AA';
  4017. $this->state = 'MyState';
  4018. $this->country_id = 1;
  4019. $this->country_code = 'FR';
  4020. $this->email = 'specimen@specimen.com';
  4021. $this->socialnetworks = array(
  4022. 'skype' => 'skypepseudo',
  4023. 'twitter' => 'twitterpseudo',
  4024. 'facebook' => 'facebookpseudo',
  4025. 'linkedin' => 'linkedinpseudo',
  4026. );
  4027. $this->url = 'http://www.specimen.com';
  4028. $this->phone = '0909090901';
  4029. $this->fax = '0909090909';
  4030. $this->code_client = 'CC-'.dol_print_date($now, 'dayhourlog');
  4031. $this->code_fournisseur = 'SC-'.dol_print_date($now, 'dayhourlog');
  4032. $this->capital = 10000;
  4033. $this->client = 1;
  4034. $this->prospect = 1;
  4035. $this->fournisseur = 1;
  4036. $this->tva_assuj = 1;
  4037. $this->tva_intra = 'EU1234567';
  4038. $this->note_public = 'This is a comment (public)';
  4039. $this->note_private = 'This is a comment (private)';
  4040. $this->idprof1 = 'idprof1';
  4041. $this->idprof2 = 'idprof2';
  4042. $this->idprof3 = 'idprof3';
  4043. $this->idprof4 = 'idprof4';
  4044. $this->idprof5 = 'idprof5';
  4045. $this->idprof6 = 'idprof6';
  4046. return 1;
  4047. }
  4048. /**
  4049. * Check if we must use localtax feature or not according to country (country of $mysoc in most cases).
  4050. *
  4051. * @param int $localTaxNum To get info for only localtax1 or localtax2
  4052. * @return boolean true or false
  4053. */
  4054. public function useLocalTax($localTaxNum = 0)
  4055. {
  4056. $sql = "SELECT t.localtax1, t.localtax2";
  4057. $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
  4058. $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$this->db->escape($this->country_code)."'";
  4059. $sql .= " AND t.active = 1";
  4060. $sql .= " AND t.entity IN (".getEntity('c_tva').")";
  4061. if (empty($localTaxNum)) {
  4062. $sql .= " AND (t.localtax1_type <> '0' OR t.localtax2_type <> '0')";
  4063. } elseif ($localTaxNum == 1) {
  4064. $sql .= " AND t.localtax1_type <> '0'";
  4065. } elseif ($localTaxNum == 2) {
  4066. $sql .= " AND t.localtax2_type <> '0'";
  4067. }
  4068. $resql = $this->db->query($sql);
  4069. if ($resql) {
  4070. return ($this->db->num_rows($resql) > 0);
  4071. } else {
  4072. return false;
  4073. }
  4074. }
  4075. /**
  4076. * Check if we must use NPR Vat (french stupid rule) or not according to country (country of $mysoc in most cases).
  4077. *
  4078. * @return boolean true or false
  4079. */
  4080. public function useNPR()
  4081. {
  4082. $sql = "SELECT t.rowid";
  4083. $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
  4084. $sql .= " WHERE t.fk_pays = c.rowid AND c.code = '".$this->db->escape($this->country_code)."'";
  4085. $sql .= " AND t.active = 1 AND t.recuperableonly = 1";
  4086. $sql .= " AND t.entity IN (".getEntity('c_tva').")";
  4087. dol_syslog("useNPR", LOG_DEBUG);
  4088. $resql = $this->db->query($sql);
  4089. if ($resql) {
  4090. return ($this->db->num_rows($resql) > 0);
  4091. } else {
  4092. return false;
  4093. }
  4094. }
  4095. /**
  4096. * Check if we must use revenue stamps feature or not according to country (country of $mysocin most cases).
  4097. * Table c_revenuestamp contains the country and value of stamp per invoice.
  4098. *
  4099. * @return boolean true or false
  4100. */
  4101. public function useRevenueStamp()
  4102. {
  4103. $sql = "SELECT COUNT(*) as nb";
  4104. $sql .= " FROM ".MAIN_DB_PREFIX."c_revenuestamp as r, ".MAIN_DB_PREFIX."c_country as c";
  4105. $sql .= " WHERE r.fk_pays = c.rowid AND c.code = '".$this->db->escape($this->country_code)."'";
  4106. $sql .= " AND r.active = 1";
  4107. dol_syslog("useRevenueStamp", LOG_DEBUG);
  4108. $resql = $this->db->query($sql);
  4109. if ($resql) {
  4110. $obj = $this->db->fetch_object($resql);
  4111. return (($obj->nb > 0) ?true:false);
  4112. } else {
  4113. $this->error = $this->db->lasterror();
  4114. return false;
  4115. }
  4116. }
  4117. /**
  4118. * Return prostect level
  4119. *
  4120. * @return string Label of prospect status
  4121. */
  4122. public function getLibProspLevel()
  4123. {
  4124. return $this->LibProspLevel($this->fk_prospectlevel);
  4125. }
  4126. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  4127. /**
  4128. * Return label of prospect level
  4129. *
  4130. * @param int $fk_prospectlevel Prospect level
  4131. * @return string label of level
  4132. */
  4133. public function LibProspLevel($fk_prospectlevel)
  4134. {
  4135. // phpcs:enable
  4136. global $langs;
  4137. $lib = $langs->trans("ProspectLevel".$fk_prospectlevel);
  4138. // If lib not found in language file, we get label from cache/databse
  4139. if ($lib == $langs->trans("ProspectLevel".$fk_prospectlevel)) {
  4140. $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');
  4141. }
  4142. return $lib;
  4143. }
  4144. /**
  4145. * Return status of prospect
  4146. *
  4147. * @param int $mode 0=label long, 1=label short, 2=Picto + Label short, 3=Picto, 4=Picto + Label long
  4148. * @param string $label Label to use for status for added status
  4149. * @return string Label
  4150. */
  4151. public function getLibProspCommStatut($mode = 0, $label = '')
  4152. {
  4153. return $this->LibProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);
  4154. }
  4155. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  4156. /**
  4157. * Return label of a given status
  4158. *
  4159. * @param int|string $status Id or code for prospection status
  4160. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  4161. * @param string $label Label to use for status for added status
  4162. * @param string $picto Name of image file to show ('filenew', ...)
  4163. * If no extension provided, we use '.png'. Image must be stored into theme/xxx/img directory.
  4164. * Example: picto.png if picto.png is stored into htdocs/theme/mytheme/img
  4165. * Example: picto.png@mymodule if picto.png is stored into htdocs/mymodule/img
  4166. * Example: /mydir/mysubdir/picto.png if picto.png is stored into htdocs/mydir/mysubdir (pictoisfullpath must be set to 1)
  4167. * @return string Label of prospection status
  4168. */
  4169. public function LibProspCommStatut($status, $mode = 0, $label = '', $picto = '')
  4170. {
  4171. // phpcs:enable
  4172. global $langs;
  4173. $langs->load('customers');
  4174. if ($mode == 2) {
  4175. if ($status == '-1' || $status == 'ST_NO') {
  4176. return img_action($langs->trans("StatusProspect-1"), -1, $picto, 'class="inline-block valignmiddle"').' '.$langs->trans("StatusProspect-1");
  4177. } elseif ($status == '0' || $status == 'ST_NEVER') {
  4178. return img_action($langs->trans("StatusProspect0"), 0, $picto, 'class="inline-block valignmiddle"').' '.$langs->trans("StatusProspect0");
  4179. } elseif ($status == '1' || $status == 'ST_TODO') {
  4180. return img_action($langs->trans("StatusProspect1"), 1, $picto, 'class="inline-block valignmiddle"').' '.$langs->trans("StatusProspect1");
  4181. } elseif ($status == '2' || $status == 'ST_PEND') {
  4182. return img_action($langs->trans("StatusProspect2"), 2, $picto, 'class="inline-block valignmiddle"').' '.$langs->trans("StatusProspect2");
  4183. } elseif ($status == '3' || $status == 'ST_DONE') {
  4184. return img_action($langs->trans("StatusProspect3"), 3, $picto, 'class="inline-block valignmiddle"').' '.$langs->trans("StatusProspect3");
  4185. } else {
  4186. return img_action(($langs->trans("StatusProspect".$status) != "StatusProspect".$status) ? $langs->trans("StatusProspect".$status) : $label, 0, $picto, 'class="inline-block valignmiddle"').' '.(($langs->trans("StatusProspect".$status) != "StatusProspect".$status) ? $langs->trans("StatusProspect".$status) : $label);
  4187. }
  4188. } elseif ($mode == 3) {
  4189. if ($status == '-1' || $status == 'ST_NO') {
  4190. return img_action($langs->trans("StatusProspect-1"), -1, $picto, 'class="inline-block valignmiddle"');
  4191. } elseif ($status == '0' || $status == 'ST_NEVER') {
  4192. return img_action($langs->trans("StatusProspect0"), 0, $picto, 'class="inline-block valignmiddle"');
  4193. } elseif ($status == '1' || $status == 'ST_TODO') {
  4194. return img_action($langs->trans("StatusProspect1"), 1, $picto, 'class="inline-block valignmiddle"');
  4195. } elseif ($status == '2' || $status == 'ST_PEND') {
  4196. return img_action($langs->trans("StatusProspect2"), 2, $picto, 'class="inline-block valignmiddle"');
  4197. } elseif ($status == '3' || $status == 'ST_DONE') {
  4198. return img_action($langs->trans("StatusProspect3"), 3, $picto, 'class="inline-block valignmiddle"');
  4199. } else {
  4200. return img_action(($langs->trans("StatusProspect".$status) != "StatusProspect".$status) ? $langs->trans("StatusProspect".$status) : $label, 0, $picto, 'class="inline-block valignmiddle"');
  4201. }
  4202. } elseif ($mode == 4) {
  4203. if ($status == '-1' || $status == 'ST_NO') {
  4204. return img_action($langs->trans("StatusProspect-1"), -1, $picto, 'class="inline-block valignmiddle"').' '.$langs->trans("StatusProspect-1");
  4205. } elseif ($status == '0' || $status == 'ST_NEVER') {
  4206. return img_action($langs->trans("StatusProspect0"), 0, $picto, 'class="inline-block valignmiddle"').' '.$langs->trans("StatusProspect0");
  4207. } elseif ($status == '1' || $status == 'ST_TODO') {
  4208. return img_action($langs->trans("StatusProspect1"), 1, $picto, 'class="inline-block valignmiddle"').' '.$langs->trans("StatusProspect1");
  4209. } elseif ($status == '2' || $status == 'ST_PEND') {
  4210. return img_action($langs->trans("StatusProspect2"), 2, $picto, 'class="inline-block valignmiddle"').' '.$langs->trans("StatusProspect2");
  4211. } elseif ($status == '3' || $status == 'ST_DONE') {
  4212. return img_action($langs->trans("StatusProspect3"), 3, $picto, 'class="inline-block valignmiddle"').' '.$langs->trans("StatusProspect3");
  4213. } else {
  4214. return img_action(($langs->trans("StatusProspect".$status) != "StatusProspect".$status) ? $langs->trans("StatusProspect".$status) : $label, 0, $picto, 'class="inline-block valignmiddle"').' '.(($langs->trans("StatusProspect".$status) != "StatusProspect".$status) ? $langs->trans("StatusProspect".$status) : $label);
  4215. }
  4216. }
  4217. return "Error, mode/status not found";
  4218. }
  4219. /**
  4220. * Return amount of proposal not yet paid and total an dlist of all proposals
  4221. *
  4222. * @param string $mode 'customer' or 'supplier'
  4223. * @return array array('opened'=>Amount including tax that remains to pay, 'total_ht'=>Total amount without tax of all objects paid or not, 'total_ttc'=>Total amunt including tax of all object paid or not)
  4224. */
  4225. public function getOutstandingProposals($mode = 'customer')
  4226. {
  4227. $table = 'propal';
  4228. if ($mode == 'supplier') {
  4229. $table = 'supplier_proposal';
  4230. }
  4231. $sql = "SELECT rowid, ref, total_ht, total_ttc, fk_statut as status FROM ".MAIN_DB_PREFIX.$table." as f";
  4232. $sql .= " WHERE fk_soc = ".((int) $this->id);
  4233. if ($mode == 'supplier') {
  4234. $sql .= " AND entity IN (".getEntity('supplier_proposal').")";
  4235. } else {
  4236. $sql .= " AND entity IN (".getEntity('propal').")";
  4237. }
  4238. dol_syslog("getOutstandingProposals for fk_soc = ".((int) $this->id), LOG_DEBUG);
  4239. $resql = $this->db->query($sql);
  4240. if ($resql) {
  4241. $outstandingOpened = 0;
  4242. $outstandingTotal = 0;
  4243. $outstandingTotalIncTax = 0;
  4244. $arrayofref = array();
  4245. while ($obj = $this->db->fetch_object($resql)) {
  4246. $arrayofref[$obj->rowid] = $obj->ref;
  4247. $outstandingTotal += $obj->total_ht;
  4248. $outstandingTotalIncTax += $obj->total_ttc;
  4249. if ($obj->status != 0) {
  4250. // Not a draft
  4251. $outstandingOpened += $obj->total_ttc;
  4252. }
  4253. }
  4254. return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax, 'refs'=>$arrayofref); // 'opened' is 'incl taxes'
  4255. } else {
  4256. return array();
  4257. }
  4258. }
  4259. /**
  4260. * Return amount of order not yet paid and total and list of all orders
  4261. *
  4262. * @param string $mode 'customer' or 'supplier'
  4263. * @return array array('opened'=>Amount including tax that remains to pay, 'total_ht'=>Total amount without tax of all objects paid or not, 'total_ttc'=>Total amunt including tax of all object paid or not)
  4264. */
  4265. public function getOutstandingOrders($mode = 'customer')
  4266. {
  4267. $table = 'commande';
  4268. if ($mode == 'supplier') {
  4269. $table = 'commande_fournisseur';
  4270. }
  4271. $sql = "SELECT rowid, ref, total_ht, total_ttc, fk_statut as status FROM ".MAIN_DB_PREFIX.$table." as f";
  4272. $sql .= " WHERE fk_soc = ".((int) $this->id);
  4273. if ($mode == 'supplier') {
  4274. $sql .= " AND entity IN (".getEntity('supplier_order').")";
  4275. } else {
  4276. $sql .= " AND entity IN (".getEntity('commande').")";
  4277. }
  4278. dol_syslog("getOutstandingOrders", LOG_DEBUG);
  4279. $resql = $this->db->query($sql);
  4280. if ($resql) {
  4281. $outstandingOpened = 0;
  4282. $outstandingTotal = 0;
  4283. $outstandingTotalIncTax = 0;
  4284. $arrayofref = array();
  4285. while ($obj = $this->db->fetch_object($resql)) {
  4286. $arrayofref[$obj->rowid] = $obj->ref;
  4287. $outstandingTotal += $obj->total_ht;
  4288. $outstandingTotalIncTax += $obj->total_ttc;
  4289. if ($obj->status != 0) {
  4290. // Not a draft
  4291. $outstandingOpened += $obj->total_ttc;
  4292. }
  4293. }
  4294. return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax, 'refs'=>$arrayofref); // 'opened' is 'incl taxes'
  4295. } else {
  4296. return array();
  4297. }
  4298. }
  4299. /**
  4300. * Return amount of bill not yet paid and total of all invoices
  4301. *
  4302. * @param string $mode 'customer' or 'supplier'
  4303. * @param int $late 0 => all invoice, 1=> only late
  4304. * @return array array('opened'=>Amount including tax that remains to pay, 'total_ht'=>Total amount without tax of all objects paid or not, 'total_ttc'=>Total amunt including tax of all object paid or not)
  4305. */
  4306. public function getOutstandingBills($mode = 'customer', $late = 0)
  4307. {
  4308. $table = 'facture';
  4309. if ($mode == 'supplier') {
  4310. $table = 'facture_fourn';
  4311. }
  4312. /* Accurate value of remain to pay is to sum remaintopay for each invoice
  4313. $paiement = $invoice->getSommePaiement();
  4314. $creditnotes=$invoice->getSumCreditNotesUsed();
  4315. $deposits=$invoice->getSumDepositsUsed();
  4316. $alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT');
  4317. $remaintopay=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT');
  4318. */
  4319. $sql = "SELECT rowid, ref, total_ht, total_ttc, paye, type, fk_statut as status, close_code FROM ".MAIN_DB_PREFIX.$table." as f";
  4320. $sql .= " WHERE fk_soc = ".((int) $this->id);
  4321. if (!empty($late)) {
  4322. $sql .= " AND date_lim_reglement < '".$this->db->idate(dol_now())."'";
  4323. }
  4324. if ($mode == 'supplier') {
  4325. $sql .= " AND entity IN (".getEntity('facture_fourn').")";
  4326. } else {
  4327. $sql .= " AND entity IN (".getEntity('invoice').")";
  4328. }
  4329. dol_syslog("getOutstandingBills", LOG_DEBUG);
  4330. $resql = $this->db->query($sql);
  4331. if ($resql) {
  4332. $outstandingOpened = 0;
  4333. $outstandingTotal = 0;
  4334. $outstandingTotalIncTax = 0;
  4335. $arrayofref = array();
  4336. $arrayofrefopened = array();
  4337. if ($mode == 'supplier') {
  4338. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  4339. $tmpobject = new FactureFournisseur($this->db);
  4340. } else {
  4341. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  4342. $tmpobject = new Facture($this->db);
  4343. }
  4344. while ($obj = $this->db->fetch_object($resql)) {
  4345. $arrayofref[$obj->rowid] = $obj->ref;
  4346. $tmpobject->id = $obj->rowid;
  4347. if ($obj->status != $tmpobject::STATUS_DRAFT // Not a draft
  4348. && !($obj->status == $tmpobject::STATUS_ABANDONED && $obj->close_code == 'replaced') // Not a replaced invoice
  4349. ) {
  4350. $outstandingTotal += $obj->total_ht;
  4351. $outstandingTotalIncTax += $obj->total_ttc;
  4352. }
  4353. $remaintopay = 0;
  4354. if ($obj->paye == 0
  4355. && $obj->status != $tmpobject::STATUS_DRAFT // Not a draft
  4356. && $obj->status != $tmpobject::STATUS_ABANDONED // Not abandonned
  4357. && $obj->status != $tmpobject::STATUS_CLOSED) { // Not classified as paid
  4358. //$sql .= " AND (status <> 3 OR close_code <> 'abandon')"; // Not abandonned for undefined reason
  4359. $paiement = $tmpobject->getSommePaiement();
  4360. $creditnotes = $tmpobject->getSumCreditNotesUsed();
  4361. $deposits = $tmpobject->getSumDepositsUsed();
  4362. $remaintopay = ($obj->total_ttc - $paiement - $creditnotes - $deposits);
  4363. $outstandingOpened += $remaintopay;
  4364. }
  4365. //if credit note is converted but not used
  4366. // TODO Do this also for customer ?
  4367. if ($mode == 'supplier' && $obj->type == FactureFournisseur::TYPE_CREDIT_NOTE && $tmpobject->isCreditNoteUsed()) {
  4368. $remainingcreditnote = $tmpobject->getSumFromThisCreditNotesNotUsed();
  4369. $remaintopay -= $remainingcreditnote;
  4370. $outstandingOpened -= $remainingcreditnote;
  4371. }
  4372. if ($remaintopay) {
  4373. $arrayofrefopened[$obj->rowid] = $obj->ref;
  4374. }
  4375. }
  4376. return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax, 'refs'=>$arrayofref, 'refsopened'=>$arrayofrefopened); // 'opened' is 'incl taxes'
  4377. } else {
  4378. dol_syslog("Sql error ".$this->db->lasterror, LOG_ERR);
  4379. return array();
  4380. }
  4381. }
  4382. /**
  4383. * Return label of status customer is prospect/customer
  4384. *
  4385. * @return string Label
  4386. * @see getTypeUrl()
  4387. */
  4388. public function getLibCustProspStatut()
  4389. {
  4390. return $this->LibCustProspStatut($this->client);
  4391. }
  4392. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  4393. /**
  4394. * Return the label of the customer/prospect status
  4395. *
  4396. * @param int $status Id of prospection status
  4397. * @return string Label of prospection status
  4398. */
  4399. public function LibCustProspStatut($status)
  4400. {
  4401. // phpcs:enable
  4402. global $langs;
  4403. $langs->load('companies');
  4404. if ($status == 0) {
  4405. return $langs->trans("NorProspectNorCustomer");
  4406. } elseif ($status == 1) {
  4407. return $langs->trans("Customer");
  4408. } elseif ($status == 2) {
  4409. return $langs->trans("Prospect");
  4410. } elseif ($status == 3) {
  4411. return $langs->trans("ProspectCustomer");
  4412. }
  4413. return '';
  4414. }
  4415. /**
  4416. * Create a document onto disk according to template module.
  4417. *
  4418. * @param string $modele Generator to use. Caller must set it to obj->model_pdf or GETPOST('model','alpha') for example.
  4419. * @param Translate $outputlangs objet lang a utiliser pour traduction
  4420. * @param int $hidedetails Hide details of lines
  4421. * @param int $hidedesc Hide description
  4422. * @param int $hideref Hide ref
  4423. * @param null|array $moreparams Array to provide more information
  4424. * @return int <0 if KO, >0 if OK
  4425. */
  4426. public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
  4427. {
  4428. global $conf, $user, $langs;
  4429. if (!empty($moreparams) && !empty($moreparams['use_companybankid'])) {
  4430. $modelpath = "core/modules/bank/doc/";
  4431. include_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
  4432. $companybankaccount = new CompanyBankAccount($this->db);
  4433. $result = $companybankaccount->fetch($moreparams['use_companybankid']);
  4434. if (!$result) {
  4435. dol_print_error($this->db, $companybankaccount->error, $companybankaccount->errors);
  4436. }
  4437. $result = $companybankaccount->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  4438. $this->last_main_doc=$companybankaccount->last_main_doc;
  4439. } else {
  4440. // Positionne le modele sur le nom du modele a utiliser
  4441. if (!dol_strlen($modele)) {
  4442. if (getDolGlobalString('COMPANY_ADDON_PDF')) {
  4443. $modele = $conf->global->COMPANY_ADDON_PDF;
  4444. } else {
  4445. print $langs->trans("Error")." ".$langs->trans("Error_COMPANY_ADDON_PDF_NotDefined");
  4446. return 0;
  4447. }
  4448. }
  4449. if (!isset($this->bank_account)) {
  4450. require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
  4451. $bac = new CompanyBankAccount($this->db);
  4452. $result = $bac->fetch(0, $this->id);
  4453. if ($result > 0) {
  4454. $this->bank_account = $bac;
  4455. } else {
  4456. $this->bank_account = '';
  4457. }
  4458. }
  4459. $modelpath = "core/modules/societe/doc/";
  4460. $result = $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  4461. }
  4462. return $result;
  4463. }
  4464. /**
  4465. * Sets object to supplied categories.
  4466. *
  4467. * Deletes object from existing categories not supplied.
  4468. * Adds it to non existing supplied categories.
  4469. * Existing categories are left untouch.
  4470. *
  4471. * @param int[]|int $categories Category ID or array of Categories IDs
  4472. * @param string $type_categ Category type ('customer' or 'supplier')
  4473. * @return int <0 if KO, >0 if OK
  4474. */
  4475. public function setCategories($categories, $type_categ)
  4476. {
  4477. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  4478. // Decode type
  4479. if (!in_array($type_categ, array(Categorie::TYPE_CUSTOMER, Categorie::TYPE_SUPPLIER))) {
  4480. dol_syslog(__METHOD__.': Type '.$type_categ.'is an unknown company category type. Done nothing.', LOG_ERR);
  4481. return -1;
  4482. }
  4483. return parent::setCategoriesCommon($categories, $type_categ);
  4484. }
  4485. /**
  4486. * Sets sales representatives of the thirdparty
  4487. *
  4488. * @param int[]|int $salesrep User ID or array of user IDs
  4489. * @param bool $onlyAdd Only add (no delete before)
  4490. * @return int <0 if KO, >0 if OK
  4491. */
  4492. public function setSalesRep($salesrep, $onlyAdd = false)
  4493. {
  4494. global $user;
  4495. // Handle single user
  4496. if (!is_array($salesrep)) {
  4497. $salesrep = array($salesrep);
  4498. }
  4499. $to_del = array(); // Nothing to delete
  4500. $to_add = $salesrep;
  4501. if ($onlyAdd === false) {
  4502. // Get current users
  4503. $existing = $this->getSalesRepresentatives($user, 1);
  4504. // Diff
  4505. if (is_array($existing)) {
  4506. $to_del = array_diff($existing, $salesrep);
  4507. $to_add = array_diff($salesrep, $existing);
  4508. }
  4509. }
  4510. $error = 0;
  4511. // Process
  4512. foreach ($to_del as $del) {
  4513. $this->del_commercial($user, $del);
  4514. }
  4515. foreach ($to_add as $add) {
  4516. $result = $this->add_commercial($user, $add);
  4517. if ($result < 0) {
  4518. $error++;
  4519. break;
  4520. }
  4521. }
  4522. return $error ? -1 : 1;
  4523. }
  4524. /**
  4525. * Define third-party type of current company
  4526. *
  4527. * @param int $typent_id third party type rowid in llx_c_typent
  4528. * @return int <0 if KO, >0 if OK
  4529. */
  4530. public function setThirdpartyType($typent_id)
  4531. {
  4532. global $user;
  4533. dol_syslog(__METHOD__, LOG_DEBUG);
  4534. if ($this->id) {
  4535. $result = $this->setValueFrom('fk_typent', $typent_id, '', null, '', '', $user, 'COMPANY_MODIFY');
  4536. if ($result > 0) {
  4537. $this->typent_id = $typent_id;
  4538. $this->typent_code = dol_getIdFromCode($this->db, $this->typent_id, 'c_typent', 'id', 'code');
  4539. return 1;
  4540. } else {
  4541. return -1;
  4542. }
  4543. } else {
  4544. return -1;
  4545. }
  4546. }
  4547. /**
  4548. * Function used to replace a thirdparty id with another one.
  4549. * It must be used within a transaction to avoid trouble
  4550. *
  4551. * @param DoliDB $dbs Database handler, because function is static we name it $dbs not $db to avoid breaking coding test
  4552. * @param int $origin_id Old thirdparty id (will be removed)
  4553. * @param int $dest_id New thirdparty id
  4554. * @return bool True if success, False if error
  4555. */
  4556. public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
  4557. {
  4558. if ($origin_id == $dest_id) {
  4559. dol_syslog('Error: Try to merge a thirdparty into itself');
  4560. return false;
  4561. }
  4562. /**
  4563. * Thirdparty commercials cannot be the same in both thirdparties so we look for them and remove some to avoid duplicate.
  4564. * Because this function is meant to be executed within a transaction, we won't take care of begin/commit.
  4565. */
  4566. $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'societe_commerciaux ';
  4567. $sql .= ' WHERE fk_soc = '.(int) $dest_id.' AND fk_user IN ( ';
  4568. $sql .= ' SELECT fk_user ';
  4569. $sql .= ' FROM '.MAIN_DB_PREFIX.'societe_commerciaux ';
  4570. $sql .= ' WHERE fk_soc = '.(int) $origin_id.') ';
  4571. $resql = $dbs->query($sql);
  4572. while ($obj = $dbs->fetch_object($resql)) {
  4573. $dbs->query('DELETE FROM '.MAIN_DB_PREFIX.'societe_commerciaux WHERE rowid = '.((int) $obj->rowid));
  4574. }
  4575. /**
  4576. * llx_societe_extrafields table must not be here because we don't care about the old thirdparty data
  4577. * Do not include llx_societe because it will be replaced later
  4578. */
  4579. $tables = array(
  4580. 'societe_commerciaux',
  4581. 'societe_prices',
  4582. 'societe_remise',
  4583. 'societe_remise_except',
  4584. 'societe_rib'
  4585. );
  4586. return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
  4587. }
  4588. /**
  4589. * Sets an accountancy code for a thirdparty.
  4590. * Also calls COMPANY_MODIFY trigger when modified
  4591. *
  4592. * @param string $type It can be only 'buy' or 'sell'
  4593. * @param string $value Accountancy code
  4594. * @return int <0 KO >0 OK
  4595. */
  4596. public function setAccountancyCode($type, $value)
  4597. {
  4598. global $user, $langs, $conf;
  4599. $this->db->begin();
  4600. $field = 'accountancy_code_sell';
  4601. if ($type == 'buy') {
  4602. $field = 'accountancy_code_buy';
  4603. } elseif ($type == 'sell') {
  4604. $field = 'accountancy_code_sell';
  4605. } else {
  4606. return -1;
  4607. }
  4608. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET ";
  4609. $sql .= $field." = '".$this->db->escape($value)."'";
  4610. $sql .= " WHERE rowid = ".((int) $this->id);
  4611. dol_syslog(get_class($this)."::".__FUNCTION__, LOG_DEBUG);
  4612. $resql = $this->db->query($sql);
  4613. if ($resql) {
  4614. // Call triggers
  4615. include_once DOL_DOCUMENT_ROOT.'/core/class/interfaces.class.php';
  4616. $interface = new Interfaces($this->db);
  4617. $result = $interface->run_triggers('COMPANY_MODIFY', $this, $user, $langs, $conf);
  4618. if ($result < 0) {
  4619. $this->errors = $interface->errors;
  4620. $this->db->rollback();
  4621. return -1;
  4622. }
  4623. // End call triggers
  4624. $this->$field = $value;
  4625. $this->db->commit();
  4626. return 1;
  4627. } else {
  4628. $this->error = $this->db->lasterror();
  4629. $this->db->rollback();
  4630. return -1;
  4631. }
  4632. }
  4633. /**
  4634. * Function to get partnerships array
  4635. *
  4636. * @param string $mode 'member' or 'thirdparty'
  4637. * @return int <0 if KO, >0 if OK
  4638. */
  4639. public function fetchPartnerships($mode)
  4640. {
  4641. global $langs;
  4642. require_once DOL_DOCUMENT_ROOT.'/partnership/class/partnership.class.php';
  4643. $this->partnerships[] = array();
  4644. return 1;
  4645. }
  4646. /**
  4647. * Return clicable link of object (with eventually picto)
  4648. *
  4649. * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
  4650. * @param array $arraydata Array of data
  4651. * @return string HTML Code for Kanban thumb.
  4652. */
  4653. public function getKanbanView($option = '', $arraydata = null)
  4654. {
  4655. $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
  4656. $return = '<div class="box-flex-item box-flex-grow-zero">';
  4657. $return .= '<div class="info-box info-box-sm">';
  4658. $return .= '<span class="info-box-icon bg-infobox-action">';
  4659. $return .= img_picto('', $this->picto);
  4660. $return .= '</span>';
  4661. $return .= '<div class="info-box-content">';
  4662. $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
  4663. if ($selected >= 0) {
  4664. $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
  4665. }
  4666. if (property_exists($this, 'code_client')) {
  4667. $return .= '<br><span class="info-box-label opacitymedium">'.$this->code_client.'</span>';
  4668. }
  4669. if (method_exists($this, 'getLibStatut')) {
  4670. $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
  4671. }
  4672. $return .= '</div>';
  4673. $return .= '</div>';
  4674. $return .= '</div>';
  4675. return $return;
  4676. }
  4677. /**
  4678. * Get array of all contacts for a society (stored in societe_contacts instead of element_contacts for all other objects)
  4679. *
  4680. * @param int $list 0:Return array contains all properties, 1:Return array contains just id
  4681. * @param string $code Filter on this code of contact type ('SHIPPING', 'BILLING', ...)
  4682. * @param string $element Filter on this element of default contact type ('facture', 'propal', 'commande' ...)
  4683. * @return array|int Array of contacts, -1 if error
  4684. *
  4685. */
  4686. public function getContacts($list = 0, $code = '', $element = '')
  4687. {
  4688. // phpcs:enable
  4689. global $langs;
  4690. $tab = array();
  4691. $sql = "SELECT sc.rowid, sc.fk_socpeople as id, sc.fk_c_type_contact"; // This field contains id of llx_socpeople or id of llx_user
  4692. $sql .= ", t.fk_soc as socid, t.statut as statuscontact";
  4693. $sql .= ", t.civility as civility, t.lastname as lastname, t.firstname, t.email";
  4694. $sql .= ", tc.source, tc.element, tc.code, tc.libelle as type_label";
  4695. $sql .= " FROM ".$this->db->prefix()."c_type_contact tc";
  4696. $sql .= ", ".$this->db->prefix()."societe_contacts sc";
  4697. $sql .= " LEFT JOIN ".$this->db->prefix()."socpeople t on sc.fk_socpeople = t.rowid";
  4698. $sql .= " WHERE sc.fk_soc = ".((int) $this->id);
  4699. $sql .= " AND sc.fk_c_type_contact = tc.rowid";
  4700. if (!empty($element)) {
  4701. $sql .= " AND tc.element = '".$this->db->escape($element)."'";
  4702. }
  4703. if ($code) {
  4704. $sql .= " AND tc.code = '".$this->db->escape($code)."'";
  4705. }
  4706. $sql .= " AND sc.entity IN (".getEntity($this->element).")";
  4707. $sql .= " AND tc.source = 'external'";
  4708. $sql .= " AND tc.active=1";
  4709. $sql .= " ORDER BY t.lastname ASC";
  4710. dol_syslog(get_class($this)."::getContacts", LOG_DEBUG);
  4711. $resql = $this->db->query($sql);
  4712. if ($resql) {
  4713. $num = $this->db->num_rows($resql);
  4714. $i = 0;
  4715. while ($i < $num) {
  4716. $obj = $this->db->fetch_object($resql);
  4717. if (!$list) {
  4718. $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
  4719. $libelle_type = ($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->type_label);
  4720. $tab[$i] = array(
  4721. 'source' => $obj->source,
  4722. 'socid' => $obj->socid,
  4723. 'id' => $obj->id,
  4724. 'nom' => $obj->lastname, // For backward compatibility
  4725. 'civility' => $obj->civility,
  4726. 'lastname' => $obj->lastname,
  4727. 'firstname' => $obj->firstname,
  4728. 'email'=>$obj->email,
  4729. 'login'=> (empty($obj->login) ? '' : $obj->login),
  4730. 'photo' => (empty($obj->photo) ? '' : $obj->photo),
  4731. 'statuscontact' => $obj->statuscontact,
  4732. 'rowid' => $obj->rowid,
  4733. 'code' => $obj->code,
  4734. 'element' => $obj->element,
  4735. 'libelle' => $libelle_type,
  4736. 'status' => $obj->statuslink,
  4737. 'fk_c_type_contact' => $obj->fk_c_type_contact
  4738. );
  4739. } else {
  4740. $tab[$i] = $obj->id;
  4741. }
  4742. $i++;
  4743. }
  4744. return $tab;
  4745. } else {
  4746. $this->error = $this->db->lasterror();
  4747. dol_print_error($this->db);
  4748. return -1;
  4749. }
  4750. }
  4751. /**
  4752. * Merge a company with another one, deleting the given company.
  4753. * The company given in parameter will be removed.
  4754. *
  4755. * @param int $soc_origin_id Company to merge the data from
  4756. * @return int -1 if error
  4757. */
  4758. public function mergeCompany($soc_origin_id)
  4759. {
  4760. global $langs, $hookmanager, $user, $action;
  4761. $error = 0;
  4762. $soc_origin = new Societe($this->db); // The thirdparty that we will delete
  4763. if (!$error && $soc_origin->fetch($soc_origin_id) < 1) {
  4764. $this->error = $langs->trans('ErrorRecordNotFound');
  4765. $error++;
  4766. }
  4767. if (!$error) {
  4768. $this->db->begin();
  4769. // Recopy some data
  4770. $this->client = $this->client | $soc_origin->client;
  4771. $this->fournisseur = $this->fournisseur | $soc_origin->fournisseur;
  4772. $listofproperties = array(
  4773. 'address', 'zip', 'town', 'state_id', 'country_id', 'phone', 'fax', 'email', 'socialnetworks', 'url', 'barcode',
  4774. 'idprof1', 'idprof2', 'idprof3', 'idprof4', 'idprof5', 'idprof6',
  4775. 'tva_intra', 'effectif_id', 'forme_juridique', 'remise_percent', 'remise_supplier_percent', 'mode_reglement_supplier_id', 'cond_reglement_supplier_id', 'name_bis',
  4776. 'stcomm_id', 'outstanding_limit', 'price_level', 'parent', 'default_lang', 'ref', 'ref_ext', 'import_key', 'fk_incoterms', 'fk_multicurrency',
  4777. 'code_client', 'code_fournisseur', 'code_compta', 'code_compta_fournisseur',
  4778. 'model_pdf',
  4779. );
  4780. foreach ($listofproperties as $property) {
  4781. if (empty($this->$property)) {
  4782. $this->$property = $soc_origin->$property;
  4783. }
  4784. }
  4785. // Concat some data
  4786. $listofproperties = array(
  4787. 'note_public', 'note_private'
  4788. );
  4789. foreach ($listofproperties as $property) {
  4790. $this->$property = dol_concatdesc($this->$property, $soc_origin->$property);
  4791. }
  4792. // Merge extrafields
  4793. if (is_array($soc_origin->array_options)) {
  4794. foreach ($soc_origin->array_options as $key => $val) {
  4795. if (empty($this->array_options[$key])) {
  4796. $this->array_options[$key] = $val;
  4797. }
  4798. }
  4799. }
  4800. // If alias name is not defined on target thirdparty, we can store in it the old name of company.
  4801. if (empty($this->name_bis) && $this->name != $soc_origin->name) {
  4802. $this->name_bis = $this->name;
  4803. }
  4804. // Merge categories
  4805. $static_cat = new Categorie($this->db);
  4806. $custcats_ori = $static_cat->containing($soc_origin->id, 'customer', 'id');
  4807. $custcats = $static_cat->containing($this->id, 'customer', 'id');
  4808. $custcats = array_merge($custcats, $custcats_ori);
  4809. $this->setCategories($custcats, 'customer');
  4810. $suppcats_ori = $static_cat->containing($soc_origin->id, 'supplier', 'id');
  4811. $suppcats = $static_cat->containing($this->id, 'supplier', 'id');
  4812. $suppcats = array_merge($suppcats, $suppcats_ori);
  4813. $this->setCategories($suppcats, 'supplier');
  4814. // If thirdparty has a new code that is same than origin, we clean origin code to avoid duplicate key from database unique keys.
  4815. if ($soc_origin->code_client == $this->code_client
  4816. || $soc_origin->code_fournisseur == $this->code_fournisseur
  4817. || $soc_origin->barcode == $this->barcode) {
  4818. dol_syslog("We clean customer and supplier code so we will be able to make the update of target");
  4819. $soc_origin->code_client = '';
  4820. $soc_origin->code_fournisseur = '';
  4821. $soc_origin->barcode = '';
  4822. $soc_origin->update($soc_origin->id, $user, 0, 1, 1, 'merge');
  4823. }
  4824. // Update
  4825. $result = $this->update($this->id, $user, 0, 1, 1, 'merge');
  4826. if ($result < 0) {
  4827. $error++;
  4828. }
  4829. // Move links
  4830. if (!$error) {
  4831. $objects = array(
  4832. 'Adherent' => '/adherents/class/adherent.class.php',
  4833. 'Don' => array('file' => '/don/class/don.class.php', 'enabled' => isModEnabled('don')),
  4834. 'Societe' => '/societe/class/societe.class.php',
  4835. //'Categorie' => '/categories/class/categorie.class.php',
  4836. 'ActionComm' => '/comm/action/class/actioncomm.class.php',
  4837. 'Propal' => '/comm/propal/class/propal.class.php',
  4838. 'Commande' => '/commande/class/commande.class.php',
  4839. 'Facture' => '/compta/facture/class/facture.class.php',
  4840. 'FactureRec' => '/compta/facture/class/facture-rec.class.php',
  4841. 'LignePrelevement' => '/compta/prelevement/class/ligneprelevement.class.php',
  4842. 'Mo' => '/mrp/class/mo.class.php',
  4843. 'Contact' => '/contact/class/contact.class.php',
  4844. 'Contrat' => '/contrat/class/contrat.class.php',
  4845. 'Expedition' => '/expedition/class/expedition.class.php',
  4846. 'Fichinter' => '/fichinter/class/fichinter.class.php',
  4847. 'CommandeFournisseur' => '/fourn/class/fournisseur.commande.class.php',
  4848. 'FactureFournisseur' => '/fourn/class/fournisseur.facture.class.php',
  4849. 'FactureFournisseurRec' => '/fourn/class/fournisseur.facture-rec.class.php',
  4850. 'Reception' => '/reception/class/reception.class.php',
  4851. 'SupplierProposal' => '/supplier_proposal/class/supplier_proposal.class.php',
  4852. 'ProductFournisseur' => '/fourn/class/fournisseur.product.class.php',
  4853. 'Delivery' => '/delivery/class/delivery.class.php',
  4854. 'Product' => '/product/class/product.class.php',
  4855. 'Project' => '/projet/class/project.class.php',
  4856. 'Ticket' => array('file' => '/ticket/class/ticket.class.php', 'enabled' => isModEnabled('ticket')),
  4857. 'User' => '/user/class/user.class.php',
  4858. 'Account' => '/compta/bank/class/account.class.php',
  4859. 'ConferenceOrBoothAttendee' => '/eventorganization/class/conferenceorboothattendee.class.php'
  4860. );
  4861. //First, all core objects must update their tables
  4862. foreach ($objects as $object_name => $object_file) {
  4863. if (is_array($object_file)) {
  4864. if (empty($object_file['enabled'])) {
  4865. continue;
  4866. }
  4867. $object_file = $object_file['file'];
  4868. }
  4869. require_once DOL_DOCUMENT_ROOT.$object_file;
  4870. if (!$error && !$object_name::replaceThirdparty($this->db, $soc_origin->id, $this->id)) {
  4871. $error++;
  4872. $this->error = $this->db->lasterror();
  4873. break;
  4874. }
  4875. }
  4876. }
  4877. // External modules should update their ones too
  4878. if (!$error) {
  4879. $parameters = array('soc_origin' => $soc_origin->id, 'soc_dest' => $this->id);
  4880. $reshook = $hookmanager->executeHooks('replaceThirdparty', $parameters, $this, $action);
  4881. if ($reshook < 0) {
  4882. $this->error = $hookmanager->error;
  4883. $this->errors = $hookmanager->errors;
  4884. $error++;
  4885. }
  4886. }
  4887. if (!$error) {
  4888. $this->context = array('merge'=>1, 'mergefromid'=>$soc_origin->id, 'mergefromname'=>$soc_origin->name);
  4889. // Call trigger
  4890. $result = $this->call_trigger('COMPANY_MODIFY', $user);
  4891. if ($result < 0) {
  4892. $error++;
  4893. }
  4894. // End call triggers
  4895. }
  4896. if (!$error) {
  4897. // We finally remove the old thirdparty
  4898. if ($soc_origin->delete($soc_origin->id, $user) < 1) {
  4899. $this->error = $soc_origin->error;
  4900. $this->errors = $soc_origin->errors;
  4901. $error++;
  4902. }
  4903. }
  4904. if (!$error) {
  4905. $this->db->commit();
  4906. return 0;
  4907. } else {
  4908. $langs->load("errors");
  4909. $this->error = $langs->trans('ErrorsThirdpartyMerge');
  4910. $this->db->rollback();
  4911. return -1;
  4912. }
  4913. }
  4914. return -1;
  4915. }
  4916. }