supplier_proposal.class.php 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333
  1. <?php
  2. /* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  4. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2005 Marc Barilley <marc@ocebo.com>
  6. * Copyright (C) 2005-2013 Regis Houssin <regis.houssin@inodbox.com>
  7. * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
  8. * Copyright (C) 2008 Raphael Bertrand <raphael.bertrand@resultic.fr>
  9. * Copyright (C) 2010-2020 Juanjo Menent <jmenent@2byte.es>
  10. * Copyright (C) 2010-2018 Philippe Grand <philippe.grand@atoo-net.com>
  11. * Copyright (C) 2012-2014 Christophe Battarel <christophe.battarel@altairis.fr>
  12. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  13. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  14. * Copyright (C) 2016 Ferran Marcet <fmarcet@2byte.es>
  15. * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
  16. * Copyright (C) 2019-2022 Frédéric France <frederic.france@netlogic.fr>
  17. * Copyright (C) 2020 Tobias Sekan <tobias.sekan@startmail.com>
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 3 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  31. */
  32. /**
  33. * \file htdocs/supplier_proposal/class/supplier_proposal.class.php
  34. * \brief File of class to manage supplier proposals
  35. */
  36. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  38. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  39. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  40. require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
  41. require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
  42. require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
  43. /**
  44. * Class to manage price ask supplier
  45. */
  46. class SupplierProposal extends CommonObject
  47. {
  48. use CommonIncoterm;
  49. /**
  50. * @var string ID to identify managed object
  51. */
  52. public $element = 'supplier_proposal';
  53. /**
  54. * @var string Name of table without prefix where object is stored
  55. */
  56. public $table_element = 'supplier_proposal';
  57. /**
  58. * @var string Name of subtable line
  59. */
  60. public $table_element_line = 'supplier_proposaldet';
  61. /**
  62. * @var string Field with ID of parent key if this field has a parent
  63. */
  64. public $fk_element = 'fk_supplier_proposal';
  65. /**
  66. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  67. */
  68. public $picto = 'supplier_proposal';
  69. /**
  70. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  71. * @var int
  72. */
  73. public $ismultientitymanaged = 1;
  74. /**
  75. * 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
  76. * @var integer
  77. */
  78. public $restrictiononfksoc = 1;
  79. /**
  80. * {@inheritdoc}
  81. */
  82. protected $table_ref_field = 'ref';
  83. public $socid; // Id client
  84. /**
  85. * @deprecated
  86. * @see $user_author_id
  87. */
  88. public $author;
  89. public $ref_fourn; //Reference saisie lors de l'ajout d'une ligne à la demande
  90. public $ref_supplier; //Reference saisie lors de l'ajout d'une ligne à la demande
  91. public $statut; // 0 (draft), 1 (validated), 2 (signed), 3 (not signed), 4 (processed/billed)
  92. /**
  93. * @var integer|string Date of proposal
  94. */
  95. public $date;
  96. /**
  97. * @var integer|string date_livraison
  98. * @deprecated
  99. */
  100. public $date_livraison;
  101. /**
  102. * @var integer|string date_livraison
  103. */
  104. public $delivery_date;
  105. /**
  106. * @deprecated
  107. * @see $date_creation
  108. */
  109. public $datec;
  110. /**
  111. * @var integer|string date_creation
  112. */
  113. public $date_creation;
  114. /**
  115. * @deprecated
  116. * @see $date_validation
  117. */
  118. public $datev;
  119. /**
  120. * @var integer|string date_validation
  121. */
  122. public $date_validation;
  123. public $user_author_id;
  124. public $user_valid_id;
  125. public $user_close_id;
  126. /**
  127. * @deprecated
  128. * @see $price_ht
  129. */
  130. public $price;
  131. /**
  132. * @deprecated
  133. * @see $total_tva
  134. */
  135. public $tva;
  136. /**
  137. * @deprecated
  138. * @see $total_ttc
  139. */
  140. public $total;
  141. public $cond_reglement_code;
  142. public $mode_reglement_code;
  143. public $remise = 0;
  144. public $remise_percent = 0;
  145. public $remise_absolue = 0;
  146. public $extraparams = array();
  147. public $lines = array();
  148. public $line;
  149. public $labelStatus = array();
  150. public $labelStatusShort = array();
  151. public $nbtodo;
  152. public $nbtodolate;
  153. // Multicurrency
  154. /**
  155. * @var int ID
  156. */
  157. public $fk_multicurrency;
  158. public $multicurrency_code;
  159. public $multicurrency_tx;
  160. public $multicurrency_total_ht;
  161. public $multicurrency_total_tva;
  162. public $multicurrency_total_ttc;
  163. /**
  164. * Draft status
  165. */
  166. const STATUS_DRAFT = 0;
  167. /**
  168. * Validated status
  169. */
  170. const STATUS_VALIDATED = 1;
  171. /**
  172. * Signed quote
  173. */
  174. const STATUS_SIGNED = 2;
  175. /**
  176. * Not signed quote, canceled
  177. */
  178. const STATUS_NOTSIGNED = 3;
  179. /**
  180. * Billed or closed/processed quote
  181. */
  182. const STATUS_CLOSE = 4;
  183. /**
  184. * Constructor
  185. *
  186. * @param DoliDB $db Database handler
  187. * @param int $socid Id third party
  188. * @param int $supplier_proposalid Id supplier_proposal
  189. */
  190. public function __construct($db, $socid = "", $supplier_proposalid = 0)
  191. {
  192. global $conf, $langs;
  193. $this->db = $db;
  194. $this->socid = $socid;
  195. $this->id = $supplier_proposalid;
  196. }
  197. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  198. /**
  199. * Add line into array ->lines
  200. *
  201. * @param int $idproduct Product Id to add
  202. * @param int $qty Quantity
  203. * @param int $remise_percent Discount effected on Product
  204. * @return int <0 if KO, >0 if OK
  205. *
  206. * TODO Remplacer les appels a cette fonction par generation objet Ligne
  207. */
  208. public function add_product($idproduct, $qty, $remise_percent = 0)
  209. {
  210. // phpcs:enable
  211. global $conf, $mysoc;
  212. if (!$qty) {
  213. $qty = 1;
  214. }
  215. dol_syslog(get_class($this)."::add_product $idproduct, $qty, $remise_percent");
  216. if ($idproduct > 0) {
  217. $prod = new Product($this->db);
  218. $prod->fetch($idproduct);
  219. $productdesc = $prod->description;
  220. $tva_tx = get_default_tva($mysoc, $this->thirdparty, $prod->id);
  221. $tva_npr = get_default_npr($mysoc, $this->thirdparty, $prod->id);
  222. if (empty($tva_tx)) {
  223. $tva_npr = 0;
  224. }
  225. $localtax1_tx = get_localtax($tva_tx, 1, $mysoc, $this->thirdparty, $tva_npr);
  226. $localtax2_tx = get_localtax($tva_tx, 2, $mysoc, $this->thirdparty, $tva_npr);
  227. // multiprix
  228. if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) {
  229. $price = $prod->multiprices[$this->thirdparty->price_level];
  230. } else {
  231. $price = $prod->price;
  232. }
  233. $line = new SupplierProposalLine($this->db);
  234. $line->fk_product = $idproduct;
  235. $line->desc = $productdesc;
  236. $line->qty = $qty;
  237. $line->subprice = $price;
  238. $line->remise_percent = $remise_percent;
  239. $line->tva_tx = $tva_tx;
  240. $this->lines[] = $line;
  241. return 1;
  242. }
  243. return -1;
  244. }
  245. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  246. /**
  247. * Adding line of fixed discount in the proposal in DB
  248. *
  249. * @param int $idremise Id of fixed discount
  250. * @return int >0 if OK, <0 if KO
  251. */
  252. public function insert_discount($idremise)
  253. {
  254. // phpcs:enable
  255. global $langs;
  256. include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  257. include_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
  258. $this->db->begin();
  259. $remise = new DiscountAbsolute($this->db);
  260. $result = $remise->fetch($idremise);
  261. if ($result > 0) {
  262. if ($remise->fk_facture) { // Protection against multiple submission
  263. $this->error = $langs->trans("ErrorDiscountAlreadyUsed");
  264. $this->db->rollback();
  265. return -5;
  266. }
  267. $supplier_proposalligne = new SupplierProposalLine($this->db);
  268. $supplier_proposalligne->fk_supplier_proposal = $this->id;
  269. $supplier_proposalligne->fk_remise_except = $remise->id;
  270. $supplier_proposalligne->desc = $remise->description; // Description ligne
  271. $supplier_proposalligne->tva_tx = $remise->tva_tx;
  272. $supplier_proposalligne->subprice = -$remise->amount_ht;
  273. $supplier_proposalligne->fk_product = 0; // Id produit predefini
  274. $supplier_proposalligne->qty = 1;
  275. $supplier_proposalligne->remise = 0;
  276. $supplier_proposalligne->remise_percent = 0;
  277. $supplier_proposalligne->rang = -1;
  278. $supplier_proposalligne->info_bits = 2;
  279. $supplier_proposalligne->total_ht = -$remise->amount_ht;
  280. $supplier_proposalligne->total_tva = -$remise->amount_tva;
  281. $supplier_proposalligne->total_ttc = -$remise->amount_ttc;
  282. $result = $supplier_proposalligne->insert();
  283. if ($result > 0) {
  284. $result = $this->update_price(1);
  285. if ($result > 0) {
  286. $this->db->commit();
  287. return 1;
  288. } else {
  289. $this->db->rollback();
  290. return -1;
  291. }
  292. } else {
  293. $this->error = $supplier_proposalligne->error;
  294. $this->db->rollback();
  295. return -2;
  296. }
  297. } else {
  298. $this->db->rollback();
  299. return -2;
  300. }
  301. }
  302. /**
  303. * Add a proposal line into database (linked to product/service or not)
  304. * Les parametres sont deja cense etre juste et avec valeurs finales a l'appel
  305. * de cette methode. Aussi, pour le taux tva, il doit deja avoir ete defini
  306. * par l'appelant par la methode get_default_tva(societe_vendeuse,societe_acheteuse,'',produit)
  307. * et le desc doit deja avoir la bonne valeur (a l'appelant de gerer le multilangue)
  308. *
  309. * @param string $desc Description de la ligne
  310. * @param double $pu_ht Prix unitaire
  311. * @param double $qty Quantite
  312. * @param double $txtva Taux de tva
  313. * @param double $txlocaltax1 Local tax 1 rate
  314. * @param double $txlocaltax2 Local tax 2 rate
  315. * @param int $fk_product Product/Service ID predefined
  316. * @param double $remise_percent Percentage discount of the line
  317. * @param string $price_base_type HT or TTC
  318. * @param double $pu_ttc Prix unitaire TTC
  319. * @param int $info_bits Bits of type of lines
  320. * @param int $type Type of line (product, service)
  321. * @param int $rang Position of line
  322. * @param int $special_code Special code (also used by externals modules!)
  323. * @param int $fk_parent_line Id of parent line
  324. * @param int $fk_fournprice Id supplier price. If 0, we will take best price. If -1 we keep it empty.
  325. * @param int $pa_ht Buying price without tax
  326. * @param string $label ???
  327. * @param array $array_options extrafields array
  328. * @param string $ref_supplier Supplier price reference
  329. * @param int $fk_unit Id of the unit to use.
  330. * @param string $origin 'order', 'supplier_proposal', ...
  331. * @param int $origin_id Id of origin line
  332. * @param double $pu_ht_devise Amount in currency
  333. * @param int $date_start Date start
  334. * @param int $date_end Date end
  335. * @return int >0 if OK, <0 if KO
  336. *
  337. * @see add_product()
  338. */
  339. public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $fk_product = 0, $remise_percent = 0, $price_base_type = 'HT', $pu_ttc = 0, $info_bits = 0, $type = 0, $rang = -1, $special_code = 0, $fk_parent_line = 0, $fk_fournprice = 0, $pa_ht = 0, $label = '', $array_options = 0, $ref_supplier = '', $fk_unit = '', $origin = '', $origin_id = 0, $pu_ht_devise = 0, $date_start = 0, $date_end = 0)
  340. {
  341. global $mysoc, $conf, $langs;
  342. dol_syslog(get_class($this)."::addline supplier_proposalid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_except=$remise_percent, price_base_type=$price_base_type, pu_ttc=$pu_ttc, info_bits=$info_bits, type=$type");
  343. include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  344. // Clean parameters
  345. if (empty($remise_percent)) {
  346. $remise_percent = 0;
  347. }
  348. if (empty($qty)) {
  349. $qty = 0;
  350. }
  351. if (empty($info_bits)) {
  352. $info_bits = 0;
  353. }
  354. if (empty($rang)) {
  355. $rang = 0;
  356. }
  357. if (empty($fk_parent_line) || $fk_parent_line < 0) {
  358. $fk_parent_line = 0;
  359. }
  360. if (empty($pu_ht)) {
  361. $pu_ht = 0;
  362. }
  363. $remise_percent = price2num($remise_percent);
  364. $qty = price2num($qty);
  365. $pu_ht = price2num($pu_ht);
  366. $pu_ttc = price2num($pu_ttc);
  367. if (!preg_match('/\((.*)\)/', $txtva)) {
  368. $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
  369. }
  370. $txlocaltax1 = price2num($txlocaltax1);
  371. $txlocaltax2 = price2num($txlocaltax2);
  372. $pa_ht = price2num($pa_ht);
  373. if ($price_base_type == 'HT') {
  374. $pu = $pu_ht;
  375. } else {
  376. $pu = $pu_ttc;
  377. }
  378. // Check parameters
  379. if ($type < 0) {
  380. return -1;
  381. }
  382. if ($this->statut == self::STATUS_DRAFT) {
  383. $this->db->begin();
  384. if ($fk_product > 0) {
  385. if (!empty($conf->global->SUPPLIER_PROPOSAL_WITH_PREDEFINED_PRICES_ONLY)) {
  386. // Check quantity is enough
  387. dol_syslog(get_class($this)."::addline we check supplier prices fk_product=".$fk_product." fk_fournprice=".$fk_fournprice." qty=".$qty." ref_supplier=".$ref_supplier);
  388. $prod = new Product($this->db);
  389. if ($prod->fetch($fk_product) > 0) {
  390. $product_type = $prod->type;
  391. $label = $prod->label;
  392. $fk_prod_fourn_price = $fk_fournprice;
  393. // We use 'none' instead of $ref_supplier, because fourn_ref may not exists anymore. So we will take the first supplier price ok.
  394. // If we want a dedicated supplier price, we must provide $fk_prod_fourn_price.
  395. $result = $prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product, 'none', ($this->fk_soc ? $this->fk_soc : $this->socid)); // Search on couple $fk_prod_fourn_price/$qty first, then on triplet $qty/$fk_product/$ref_supplier/$this->fk_soc
  396. if ($result > 0) {
  397. $pu = $prod->fourn_pu; // Unit price supplier price set by get_buyprice
  398. $ref_supplier = $prod->ref_supplier; // Ref supplier price set by get_buyprice
  399. // is remise percent not keyed but present for the product we add it
  400. if ($remise_percent == 0 && $prod->remise_percent != 0) {
  401. $remise_percent = $prod->remise_percent;
  402. }
  403. }
  404. if ($result == 0) { // If result == 0, we failed to found the supplier reference price
  405. $langs->load("errors");
  406. $this->error = "Ref ".$prod->ref." ".$langs->trans("ErrorQtyTooLowForThisSupplier");
  407. $this->db->rollback();
  408. dol_syslog(get_class($this)."::addline we did not found supplier price, so we can't guess unit price");
  409. //$pu = $prod->fourn_pu; // We do not overwrite unit price
  410. //$ref = $prod->ref_fourn; // We do not overwrite ref supplier price
  411. return -1;
  412. }
  413. if ($result == -1) {
  414. $langs->load("errors");
  415. $this->error = "Ref ".$prod->ref." ".$langs->trans("ErrorQtyTooLowForThisSupplier");
  416. $this->db->rollback();
  417. dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_DEBUG);
  418. return -1;
  419. }
  420. if ($result < -1) {
  421. $this->error = $prod->error;
  422. $this->errors = $prod->errors;
  423. $this->db->rollback();
  424. dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_ERR);
  425. return -1;
  426. }
  427. } else {
  428. $this->error = $prod->error;
  429. $this->errors = $prod->errors;
  430. $this->db->rollback();
  431. return -1;
  432. }
  433. }
  434. } else {
  435. $product_type = $type;
  436. }
  437. // Calcul du total TTC et de la TVA pour la ligne a partir de
  438. // qty, pu, remise_percent et txtva
  439. // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
  440. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
  441. $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
  442. // Clean vat code
  443. $reg = array();
  444. $vat_src_code = '';
  445. if (preg_match('/\((.*)\)/', $txtva, $reg)) {
  446. $vat_src_code = $reg[1];
  447. $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
  448. }
  449. if (!empty($conf->multicurrency->enabled) && $pu_ht_devise > 0) {
  450. $pu = 0;
  451. }
  452. $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
  453. $total_ht = $tabprice[0];
  454. $total_tva = $tabprice[1];
  455. $total_ttc = $tabprice[2];
  456. $total_localtax1 = $tabprice[9];
  457. $total_localtax2 = $tabprice[10];
  458. $pu = $pu_ht = $tabprice[3];
  459. // MultiCurrency
  460. $multicurrency_total_ht = $tabprice[16];
  461. $multicurrency_total_tva = $tabprice[17];
  462. $multicurrency_total_ttc = $tabprice[18];
  463. $pu_ht_devise = $tabprice[19];
  464. // Rang to use
  465. $ranktouse = $rang;
  466. if ($ranktouse == -1) {
  467. $rangmax = $this->line_max($fk_parent_line);
  468. $ranktouse = $rangmax + 1;
  469. }
  470. // TODO A virer
  471. // Anciens indicateurs: $price, $remise (a ne plus utiliser)
  472. $price = $pu;
  473. $remise = 0;
  474. if ($remise_percent > 0) {
  475. $remise = round(($pu * $remise_percent / 100), 2);
  476. $price = $pu - $remise;
  477. }
  478. // Insert line
  479. $this->line = new SupplierProposalLine($this->db);
  480. $this->line->fk_supplier_proposal = $this->id;
  481. $this->line->label = $label;
  482. $this->line->desc = $desc;
  483. $this->line->qty = $qty;
  484. $this->line->vat_src_code = $vat_src_code;
  485. $this->line->tva_tx = $txtva;
  486. $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
  487. $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
  488. $this->line->localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0];
  489. $this->line->localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2];
  490. $this->line->fk_product = $fk_product;
  491. $this->line->remise_percent = $remise_percent;
  492. $this->line->subprice = $pu_ht;
  493. $this->line->rang = $ranktouse;
  494. $this->line->info_bits = $info_bits;
  495. $this->line->total_ht = $total_ht;
  496. $this->line->total_tva = $total_tva;
  497. $this->line->total_localtax1 = $total_localtax1;
  498. $this->line->total_localtax2 = $total_localtax2;
  499. $this->line->total_ttc = $total_ttc;
  500. $this->line->product_type = $type;
  501. $this->line->special_code = $special_code;
  502. $this->line->fk_parent_line = $fk_parent_line;
  503. $this->line->fk_unit = $fk_unit;
  504. $this->line->origin = $origin;
  505. $this->line->origin_id = $origin_id;
  506. $this->line->ref_fourn = $this->db->escape($ref_supplier);
  507. $this->line->date_start = $date_start;
  508. $this->line->date_end = $date_end;
  509. // infos marge
  510. if (!empty($fk_product) && $fk_product > 0 && empty($fk_fournprice) && empty($pa_ht)) {
  511. // When fk_fournprice is 0, we take the lowest buying price
  512. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  513. $productFournisseur = new ProductFournisseur($this->db);
  514. $productFournisseur->find_min_price_product_fournisseur($fk_product);
  515. $this->line->fk_fournprice = $productFournisseur->product_fourn_price_id;
  516. } else {
  517. $this->line->fk_fournprice = ($fk_fournprice > 0 ? $fk_fournprice : 0); // If fk_fournprice is -1, we will not use fk_fournprice
  518. }
  519. $this->line->pa_ht = $pa_ht;
  520. //var_dump($this->line->fk_fournprice);exit;
  521. // Multicurrency
  522. $this->line->fk_multicurrency = $this->fk_multicurrency;
  523. $this->line->multicurrency_code = $this->multicurrency_code;
  524. $this->line->multicurrency_subprice = $pu_ht_devise;
  525. $this->line->multicurrency_total_ht = $multicurrency_total_ht;
  526. $this->line->multicurrency_total_tva = $multicurrency_total_tva;
  527. $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
  528. // Mise en option de la ligne
  529. if (empty($qty) && empty($special_code)) {
  530. $this->line->special_code = 3;
  531. }
  532. if (is_array($array_options) && count($array_options) > 0) {
  533. $this->line->array_options = $array_options;
  534. }
  535. $result = $this->line->insert();
  536. if ($result > 0) {
  537. // Reorder if child line
  538. if (!empty($fk_parent_line)) {
  539. $this->line_order(true, 'DESC');
  540. }
  541. // Mise a jour informations denormalisees au niveau de la propale meme
  542. $result = $this->update_price(1, 'auto', 0, $this->thirdparty); // This method is designed to add line from user input so total calculation must be done using 'auto' mode.
  543. if ($result > 0) {
  544. $this->db->commit();
  545. return $this->line->id;
  546. } else {
  547. $this->error = $this->error();
  548. $this->errors = $this->errors();
  549. $this->db->rollback();
  550. return -1;
  551. }
  552. } else {
  553. $this->error = $this->line->error;
  554. $this->errors = $this->line->errors;
  555. $this->db->rollback();
  556. return -2;
  557. }
  558. } else {
  559. $this->error = 'BadStatusOfObjectToAddLine';
  560. return -5;
  561. }
  562. }
  563. /**
  564. * Update a proposal line
  565. *
  566. * @param int $rowid Id de la ligne
  567. * @param double $pu Prix unitaire (HT ou TTC selon price_base_type)
  568. * @param double $qty Quantity
  569. * @param double $remise_percent Remise effectuee sur le produit
  570. * @param double $txtva Taux de TVA
  571. * @param double $txlocaltax1 Local tax 1 rate
  572. * @param double $txlocaltax2 Local tax 2 rate
  573. * @param string $desc Description
  574. * @param double $price_base_type HT ou TTC
  575. * @param int $info_bits Miscellaneous informations
  576. * @param int $special_code Special code (also used by externals modules!)
  577. * @param int $fk_parent_line Id of parent line (0 in most cases, used by modules adding sublevels into lines).
  578. * @param int $skip_update_total Keep fields total_xxx to 0 (used for special lines by some modules)
  579. * @param int $fk_fournprice Id of origin supplier price
  580. * @param int $pa_ht Price (without tax) of product when it was bought
  581. * @param string $label ???
  582. * @param int $type 0/1=Product/service
  583. * @param array $array_options extrafields array
  584. * @param string $ref_supplier Supplier price reference
  585. * @param int $fk_unit Id of the unit to use.
  586. * @param double $pu_ht_devise Unit price in currency
  587. * @return int 0 if OK, <0 if KO
  588. */
  589. public function updateline($rowid, $pu, $qty, $remise_percent, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $desc = '', $price_base_type = 'HT', $info_bits = 0, $special_code = 0, $fk_parent_line = 0, $skip_update_total = 0, $fk_fournprice = 0, $pa_ht = 0, $label = '', $type = 0, $array_options = 0, $ref_supplier = '', $fk_unit = '', $pu_ht_devise = 0)
  590. {
  591. global $conf, $user, $langs, $mysoc;
  592. dol_syslog(get_class($this)."::updateLine $rowid, $pu, $qty, $remise_percent, $txtva, $desc, $price_base_type, $info_bits");
  593. include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  594. // Clean parameters
  595. $remise_percent = price2num($remise_percent);
  596. $qty = price2num($qty);
  597. $pu = price2num($pu);
  598. if (!preg_match('/\((.*)\)/', $txtva)) {
  599. $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
  600. }
  601. $txlocaltax1 = price2num($txlocaltax1);
  602. $txlocaltax2 = price2num($txlocaltax2);
  603. $pa_ht = price2num($pa_ht);
  604. if (empty($qty) && empty($special_code)) {
  605. $special_code = 3; // Set option tag
  606. }
  607. if (!empty($qty) && $special_code == 3) {
  608. $special_code = 0; // Remove option tag
  609. }
  610. if ($this->statut == 0) {
  611. $this->db->begin();
  612. // Calcul du total TTC et de la TVA pour la ligne a partir de
  613. // qty, pu, remise_percent et txtva
  614. // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
  615. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
  616. $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $mysoc, $this->thirdparty);
  617. // Clean vat code
  618. $reg = array();
  619. $vat_src_code = '';
  620. if (preg_match('/\((.*)\)/', $txtva, $reg)) {
  621. $vat_src_code = $reg[1];
  622. $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
  623. }
  624. if (!empty($conf->multicurrency->enabled) && $pu_ht_devise > 0) {
  625. $pu = 0;
  626. }
  627. $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
  628. $total_ht = $tabprice[0];
  629. $total_tva = $tabprice[1];
  630. $total_ttc = $tabprice[2];
  631. $total_localtax1 = $tabprice[9];
  632. $total_localtax2 = $tabprice[10];
  633. $pu_ht = $tabprice[3];
  634. $pu_tva = $tabprice[4];
  635. $pu_ttc = $tabprice[5];
  636. // MultiCurrency
  637. $multicurrency_total_ht = $tabprice[16];
  638. $multicurrency_total_tva = $tabprice[17];
  639. $multicurrency_total_ttc = $tabprice[18];
  640. $pu_ht_devise = $tabprice[19];
  641. $pu = $pu_ht;
  642. if ($price_base_type == 'TTC') {
  643. $pu = $pu_ttc;
  644. }
  645. //Fetch current line from the database and then clone the object and set it in $oldline property
  646. $line = new SupplierProposalLine($this->db);
  647. $line->fetch($rowid);
  648. $line->fetch_optionals();
  649. // Stock previous line records
  650. $staticline = clone $line;
  651. $line->oldline = $staticline;
  652. $this->line = $line;
  653. $this->line->context = $this->context;
  654. // Reorder if fk_parent_line change
  655. if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
  656. $rangmax = $this->line_max($fk_parent_line);
  657. $this->line->rang = $rangmax + 1;
  658. }
  659. $this->line->id = $rowid;
  660. $this->line->label = $label;
  661. $this->line->desc = $desc;
  662. $this->line->qty = $qty;
  663. $this->line->product_type = $type;
  664. $this->line->vat_src_code = $vat_src_code;
  665. $this->line->tva_tx = $txtva;
  666. $this->line->localtax1_tx = $txlocaltax1;
  667. $this->line->localtax2_tx = $txlocaltax2;
  668. $this->line->localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0];
  669. $this->line->localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2];
  670. $this->line->remise_percent = $remise_percent;
  671. $this->line->subprice = $pu;
  672. $this->line->info_bits = $info_bits;
  673. $this->line->total_ht = $total_ht;
  674. $this->line->total_tva = $total_tva;
  675. $this->line->total_localtax1 = $total_localtax1;
  676. $this->line->total_localtax2 = $total_localtax2;
  677. $this->line->total_ttc = $total_ttc;
  678. $this->line->special_code = $special_code;
  679. $this->line->fk_parent_line = $fk_parent_line;
  680. $this->line->skip_update_total = $skip_update_total;
  681. $this->line->ref_fourn = $ref_supplier;
  682. $this->line->fk_unit = $fk_unit;
  683. // infos marge
  684. if (!empty($fk_product) && $fk_product > 0 && empty($fk_fournprice) && empty($pa_ht)) {
  685. // by external module, take lowest buying price
  686. include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  687. $productFournisseur = new ProductFournisseur($this->db);
  688. $productFournisseur->find_min_price_product_fournisseur($fk_product);
  689. $this->line->fk_fournprice = $productFournisseur->product_fourn_price_id;
  690. } else {
  691. $this->line->fk_fournprice = $fk_fournprice;
  692. }
  693. $this->line->pa_ht = $pa_ht;
  694. if (is_array($array_options) && count($array_options) > 0) {
  695. // We replace values in this->line->array_options only for entries defined into $array_options
  696. foreach ($array_options as $key => $value) {
  697. $this->line->array_options[$key] = $array_options[$key];
  698. }
  699. }
  700. // Multicurrency
  701. $this->line->multicurrency_subprice = $pu_ht_devise;
  702. $this->line->multicurrency_total_ht = $multicurrency_total_ht;
  703. $this->line->multicurrency_total_tva = $multicurrency_total_tva;
  704. $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
  705. $result = $this->line->update();
  706. if ($result > 0) {
  707. // Reorder if child line
  708. if (!empty($fk_parent_line)) {
  709. $this->line_order(true, 'DESC');
  710. }
  711. $this->update_price(1);
  712. $this->fk_supplier_proposal = $this->id;
  713. $this->db->commit();
  714. return $result;
  715. } else {
  716. $this->error = $this->db->error();
  717. $this->db->rollback();
  718. return -1;
  719. }
  720. } else {
  721. dol_syslog(get_class($this)."::updateline Erreur -2 SupplierProposal en mode incompatible pour cette action");
  722. return -2;
  723. }
  724. }
  725. /**
  726. * Delete detail line
  727. *
  728. * @param int $lineid Id of line to delete
  729. * @return int >0 if OK, <0 if KO
  730. */
  731. public function deleteline($lineid)
  732. {
  733. if ($this->statut == 0) {
  734. $line = new SupplierProposalLine($this->db);
  735. // For triggers
  736. $line->fetch($lineid);
  737. if ($line->delete() > 0) {
  738. $this->update_price(1);
  739. return 1;
  740. } else {
  741. return -1;
  742. }
  743. } else {
  744. return -2;
  745. }
  746. }
  747. /**
  748. * Create commercial proposal into database
  749. * this->ref can be set or empty. If empty, we will use "(PROVid)"
  750. *
  751. * @param User $user User that create
  752. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  753. * @return int <0 if KO, >=0 if OK
  754. */
  755. public function create($user, $notrigger = 0)
  756. {
  757. global $langs, $conf, $mysoc, $hookmanager;
  758. $error = 0;
  759. $now = dol_now();
  760. dol_syslog(get_class($this)."::create");
  761. // Check parameters
  762. $result = $this->fetch_thirdparty();
  763. if ($result < 0) {
  764. $this->error = "Failed to fetch company";
  765. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  766. return -3;
  767. }
  768. if (!empty($this->ref)) { // We check that ref is not already used
  769. $result = self::isExistingObject($this->element, 0, $this->ref); // Check ref is not yet used
  770. if ($result > 0) {
  771. $this->error = 'ErrorRefAlreadyExists';
  772. dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING);
  773. $this->db->rollback();
  774. return -1;
  775. }
  776. }
  777. // Set tmp vars
  778. $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
  779. // Multicurrency
  780. if (!empty($this->multicurrency_code)) {
  781. list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code, $now);
  782. }
  783. if (empty($this->fk_multicurrency)) {
  784. $this->multicurrency_code = $conf->currency;
  785. $this->fk_multicurrency = 0;
  786. $this->multicurrency_tx = 1;
  787. }
  788. $this->db->begin();
  789. // Insert into database
  790. $sql = "INSERT INTO ".MAIN_DB_PREFIX."supplier_proposal (";
  791. $sql .= "fk_soc";
  792. $sql .= ", price";
  793. $sql .= ", remise";
  794. $sql .= ", remise_percent";
  795. $sql .= ", remise_absolue";
  796. $sql .= ", total_tva";
  797. $sql .= ", total_ttc";
  798. $sql .= ", datec";
  799. $sql .= ", ref";
  800. $sql .= ", fk_user_author";
  801. $sql .= ", note_private";
  802. $sql .= ", note_public";
  803. $sql .= ", model_pdf";
  804. $sql .= ", fk_cond_reglement";
  805. $sql .= ", fk_mode_reglement";
  806. $sql .= ", fk_account";
  807. $sql .= ", date_livraison";
  808. $sql .= ", fk_shipping_method";
  809. $sql .= ", fk_projet";
  810. $sql .= ", entity";
  811. $sql .= ", fk_multicurrency";
  812. $sql .= ", multicurrency_code";
  813. $sql .= ", multicurrency_tx";
  814. $sql .= ") ";
  815. $sql .= " VALUES (";
  816. $sql .= ((int) $this->socid);
  817. $sql .= ", 0";
  818. $sql .= ", ".((double) $this->remise);
  819. $sql .= ", ".($this->remise_percent ? ((double) $this->remise_percent) : 'null');
  820. $sql .= ", ".($this->remise_absolue ? ((double) $this->remise_absolue) : 'null');
  821. $sql .= ", 0";
  822. $sql .= ", 0";
  823. $sql .= ", '".$this->db->idate($now)."'";
  824. $sql .= ", '(PROV)'";
  825. $sql .= ", ".($user->id > 0 ? ((int) $user->id) : "null");
  826. $sql .= ", '".$this->db->escape($this->note_private)."'";
  827. $sql .= ", '".$this->db->escape($this->note_public)."'";
  828. $sql .= ", '".$this->db->escape($this->model_pdf)."'";
  829. $sql .= ", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) : 'NULL');
  830. $sql .= ", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) : 'NULL');
  831. $sql .= ", ".($this->fk_account > 0 ? ((int) $this->fk_account) : 'NULL');
  832. $sql .= ", ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : "null");
  833. $sql .= ", ".($this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) : 'NULL');
  834. $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : "null");
  835. $sql .= ", ".((int) $conf->entity);
  836. $sql .= ", ".((int) $this->fk_multicurrency);
  837. $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
  838. $sql .= ", ".((double) $this->multicurrency_tx);
  839. $sql .= ")";
  840. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  841. $resql = $this->db->query($sql);
  842. if ($resql) {
  843. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."supplier_proposal");
  844. if ($this->id) {
  845. $this->ref = '(PROV'.$this->id.')';
  846. $sql = 'UPDATE '.MAIN_DB_PREFIX."supplier_proposal SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".((int) $this->id);
  847. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  848. $resql = $this->db->query($sql);
  849. if (!$resql) {
  850. $error++;
  851. }
  852. if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
  853. $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
  854. }
  855. // Add object linked
  856. if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
  857. foreach ($this->linked_objects as $origin => $tmp_origin_id) {
  858. if (is_array($tmp_origin_id)) { // New behaviour, if linked_object can have several links per type, so is something like array('contract'=>array(id1, id2, ...))
  859. foreach ($tmp_origin_id as $origin_id) {
  860. $ret = $this->add_object_linked($origin, $origin_id);
  861. if (!$ret) {
  862. dol_print_error($this->db);
  863. $error++;
  864. }
  865. }
  866. }
  867. }
  868. }
  869. /*
  870. * Insertion du detail des produits dans la base
  871. */
  872. if (!$error) {
  873. $fk_parent_line = 0;
  874. $num = count($this->lines);
  875. for ($i = 0; $i < $num; $i++) {
  876. // Reset fk_parent_line for no child products and special product
  877. if (($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line)) || $this->lines[$i]->product_type == 9) {
  878. $fk_parent_line = 0;
  879. }
  880. $result = $this->addline(
  881. $this->lines[$i]->desc,
  882. $this->lines[$i]->subprice,
  883. $this->lines[$i]->qty,
  884. $this->lines[$i]->tva_tx,
  885. $this->lines[$i]->localtax1_tx,
  886. $this->lines[$i]->localtax2_tx,
  887. $this->lines[$i]->fk_product,
  888. $this->lines[$i]->remise_percent,
  889. 'HT',
  890. 0,
  891. 0,
  892. $this->lines[$i]->product_type,
  893. $this->lines[$i]->rang,
  894. $this->lines[$i]->special_code,
  895. $fk_parent_line,
  896. $this->lines[$i]->fk_fournprice,
  897. $this->lines[$i]->pa_ht,
  898. empty($this->lines[$i]->label) ? '' : $this->lines[$i]->label, // deprecated
  899. $this->lines[$i]->array_options,
  900. $this->lines[$i]->ref_fourn,
  901. $this->lines[$i]->fk_unit,
  902. 'supplier_proposal',
  903. $this->lines[$i]->rowid
  904. );
  905. if ($result < 0) {
  906. $error++;
  907. $this->error = $this->db->error;
  908. dol_print_error($this->db);
  909. break;
  910. }
  911. // Defined the new fk_parent_line
  912. if ($result > 0 && $this->lines[$i]->product_type == 9) {
  913. $fk_parent_line = $result;
  914. }
  915. }
  916. }
  917. if (!$error) {
  918. // Mise a jour infos denormalisees
  919. $resql = $this->update_price(1);
  920. if ($resql) {
  921. $action = 'update';
  922. // Actions on extra fields
  923. if (!$error) {
  924. $result = $this->insertExtraFields();
  925. if ($result < 0) {
  926. $error++;
  927. }
  928. }
  929. if (!$error && !$notrigger) {
  930. // Call trigger
  931. $result = $this->call_trigger('PROPOSAL_SUPPLIER_CREATE', $user);
  932. if ($result < 0) {
  933. $error++;
  934. }
  935. // End call triggers
  936. }
  937. } else {
  938. $this->error = $this->db->lasterror();
  939. $error++;
  940. }
  941. }
  942. } else {
  943. $this->error = $this->db->lasterror();
  944. $error++;
  945. }
  946. if (!$error) {
  947. $this->db->commit();
  948. dol_syslog(get_class($this)."::create done id=".$this->id);
  949. return $this->id;
  950. } else {
  951. $this->db->rollback();
  952. return -2;
  953. }
  954. } else {
  955. $this->error = $this->db->lasterror();
  956. $this->db->rollback();
  957. return -1;
  958. }
  959. }
  960. /**
  961. * Load an object from its id and create a new one in database
  962. *
  963. * @param User $user User making the clone
  964. * @param int $fromid Id of thirdparty
  965. * @return int New id of clone
  966. */
  967. public function createFromClone(User $user, $fromid = 0)
  968. {
  969. global $conf, $hookmanager;
  970. $error = 0;
  971. $now = dol_now();
  972. $this->db->begin();
  973. // get extrafields so they will be clone
  974. foreach ($this->lines as $line) {
  975. $line->fetch_optionals();
  976. }
  977. // Load source object
  978. $objFrom = clone $this;
  979. $objsoc = new Societe($this->db);
  980. // Change socid if needed
  981. if (!empty($fromid) && $fromid != $this->socid) {
  982. if ($objsoc->fetch($fromid) > 0) {
  983. $this->socid = $objsoc->id;
  984. $this->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
  985. $this->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
  986. $this->fk_project = '';
  987. }
  988. // TODO Change product price if multi-prices
  989. } else {
  990. $objsoc->fetch($this->socid);
  991. }
  992. $this->id = 0;
  993. $this->statut = 0;
  994. if (empty($conf->global->SUPPLIER_PROPOSAL_ADDON) || !is_readable(DOL_DOCUMENT_ROOT."/core/modules/supplier_proposal/".$conf->global->SUPPLIER_PROPOSAL_ADDON.".php")) {
  995. $this->error = 'ErrorSetupNotComplete';
  996. return -1;
  997. }
  998. // Clear fields
  999. $this->user_author = $user->id;
  1000. $this->user_valid = '';
  1001. $this->date = $now;
  1002. // Set ref
  1003. require_once DOL_DOCUMENT_ROOT."/core/modules/supplier_proposal/".$conf->global->SUPPLIER_PROPOSAL_ADDON.'.php';
  1004. $obj = $conf->global->SUPPLIER_PROPOSAL_ADDON;
  1005. $modSupplierProposal = new $obj;
  1006. $this->ref = $modSupplierProposal->getNextValue($objsoc, $this);
  1007. // Create clone
  1008. $this->context['createfromclone'] = 'createfromclone';
  1009. $result = $this->create($user);
  1010. if ($result < 0) {
  1011. $error++;
  1012. }
  1013. if (!$error) {
  1014. // Hook of thirdparty module
  1015. if (is_object($hookmanager)) {
  1016. $parameters = array('objFrom'=>$objFrom);
  1017. $action = '';
  1018. $reshook = $hookmanager->executeHooks('createFrom', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  1019. if ($reshook < 0) {
  1020. $error++;
  1021. }
  1022. }
  1023. }
  1024. unset($this->context['createfromclone']);
  1025. // End
  1026. if (!$error) {
  1027. $this->db->commit();
  1028. return $this->id;
  1029. } else {
  1030. $this->db->rollback();
  1031. return -1;
  1032. }
  1033. }
  1034. /**
  1035. * Load a proposal from database and its ligne array
  1036. *
  1037. * @param int $rowid id of object to load
  1038. * @param string $ref Ref of proposal
  1039. * @return int >0 if OK, <0 if KO
  1040. */
  1041. public function fetch($rowid, $ref = '')
  1042. {
  1043. global $conf;
  1044. $sql = "SELECT p.rowid, p.entity, p.ref, p.remise, p.remise_percent, p.remise_absolue, p.fk_soc";
  1045. $sql .= ", p.total_ttc, p.total_tva, p.localtax1, p.localtax2, p.total_ht";
  1046. $sql .= ", p.datec";
  1047. $sql .= ", p.date_valid as datev";
  1048. $sql .= ", p.date_livraison as delivery_date";
  1049. $sql .= ", p.model_pdf, p.extraparams";
  1050. $sql .= ", p.note_private, p.note_public";
  1051. $sql .= ", p.fk_projet as fk_project, p.fk_statut";
  1052. $sql .= ", p.fk_user_author, p.fk_user_valid, p.fk_user_cloture";
  1053. $sql .= ", p.fk_cond_reglement";
  1054. $sql .= ", p.fk_mode_reglement";
  1055. $sql .= ', p.fk_account';
  1056. $sql .= ", p.fk_shipping_method";
  1057. $sql .= ", p.fk_multicurrency, p.multicurrency_code, p.multicurrency_tx, p.multicurrency_total_ht, p.multicurrency_total_tva, p.multicurrency_total_ttc";
  1058. $sql .= ", c.label as statut_label";
  1059. $sql .= ", cr.code as cond_reglement_code, cr.libelle as cond_reglement, cr.libelle_facture as cond_reglement_libelle_doc";
  1060. $sql .= ", cp.code as mode_reglement_code, cp.libelle as mode_reglement";
  1061. $sql .= " FROM ".MAIN_DB_PREFIX."c_propalst as c, ".MAIN_DB_PREFIX."supplier_proposal as p";
  1062. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as cp ON p.fk_mode_reglement = cp.id';
  1063. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as cr ON p.fk_cond_reglement = cr.rowid';
  1064. $sql .= " WHERE p.fk_statut = c.id";
  1065. $sql .= " AND p.entity IN (".getEntity('supplier_proposal').")";
  1066. if ($ref) {
  1067. $sql .= " AND p.ref = '".$this->db->escape($ref)."'";
  1068. } else {
  1069. $sql .= " AND p.rowid = ".((int) $rowid);
  1070. }
  1071. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  1072. $resql = $this->db->query($sql);
  1073. if ($resql) {
  1074. if ($this->db->num_rows($resql)) {
  1075. $obj = $this->db->fetch_object($resql);
  1076. $this->id = $obj->rowid;
  1077. $this->entity = $obj->entity;
  1078. $this->ref = $obj->ref;
  1079. $this->remise = $obj->remise;
  1080. $this->remise_percent = $obj->remise_percent;
  1081. $this->remise_absolue = $obj->remise_absolue;
  1082. $this->total_ht = $obj->total_ht;
  1083. $this->total_tva = $obj->total_tva;
  1084. $this->total_localtax1 = $obj->localtax1;
  1085. $this->total_localtax2 = $obj->localtax2;
  1086. $this->total_ttc = $obj->total_ttc;
  1087. $this->socid = $obj->fk_soc;
  1088. $this->fk_project = $obj->fk_project;
  1089. $this->model_pdf = $obj->model_pdf;
  1090. $this->modelpdf = $obj->model_pdf; // deprecated
  1091. $this->note = $obj->note_private; // TODO deprecated
  1092. $this->note_private = $obj->note_private;
  1093. $this->note_public = $obj->note_public;
  1094. $this->statut = (int) $obj->fk_statut;
  1095. $this->statut_libelle = $obj->statut_label;
  1096. $this->datec = $this->db->jdate($obj->datec); // TODO deprecated
  1097. $this->datev = $this->db->jdate($obj->datev); // TODO deprecated
  1098. $this->date_creation = $this->db->jdate($obj->datec); //Creation date
  1099. $this->date_validation = $this->db->jdate($obj->datev); //Validation date
  1100. $this->date_livraison = $this->db->jdate($obj->delivery_date); // deprecated
  1101. $this->delivery_date = $this->db->jdate($obj->delivery_date);
  1102. $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method : null;
  1103. $this->mode_reglement_id = $obj->fk_mode_reglement;
  1104. $this->mode_reglement_code = $obj->mode_reglement_code;
  1105. $this->mode_reglement = $obj->mode_reglement;
  1106. $this->fk_account = ($obj->fk_account > 0) ? $obj->fk_account : null;
  1107. $this->cond_reglement_id = $obj->fk_cond_reglement;
  1108. $this->cond_reglement_code = $obj->cond_reglement_code;
  1109. $this->cond_reglement = $obj->cond_reglement;
  1110. $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
  1111. $this->extraparams = (array) json_decode($obj->extraparams, true);
  1112. $this->user_author_id = $obj->fk_user_author;
  1113. $this->user_valid_id = $obj->fk_user_valid;
  1114. $this->user_close_id = $obj->fk_user_cloture;
  1115. // Multicurrency
  1116. $this->fk_multicurrency = $obj->fk_multicurrency;
  1117. $this->multicurrency_code = $obj->multicurrency_code;
  1118. $this->multicurrency_tx = $obj->multicurrency_tx;
  1119. $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
  1120. $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
  1121. $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
  1122. if ($obj->fk_statut == 0) {
  1123. $this->brouillon = 1;
  1124. }
  1125. // Retrieve all extrafield
  1126. // fetch optionals attributes and labels
  1127. $this->fetch_optionals();
  1128. $this->db->free($resql);
  1129. $this->lines = array();
  1130. // Lines of supplier proposals
  1131. $sql = "SELECT d.rowid, d.fk_supplier_proposal, d.fk_parent_line, d.label as custom_label, d.description, d.price, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.qty, d.fk_remise_except, d.remise_percent, d.subprice, d.fk_product,";
  1132. $sql .= " d.info_bits, d.total_ht, d.total_tva, d.total_localtax1, d.total_localtax2, d.total_ttc, d.fk_product_fournisseur_price as fk_fournprice, d.buy_price_ht as pa_ht, d.special_code, d.rang, d.product_type,";
  1133. $sql .= ' p.ref as product_ref, p.description as product_desc, p.fk_product_type, p.label as product_label,';
  1134. $sql .= ' d.ref_fourn as ref_produit_fourn,';
  1135. $sql .= ' d.fk_multicurrency, d.multicurrency_code, d.multicurrency_subprice, d.multicurrency_total_ht, d.multicurrency_total_tva, d.multicurrency_total_ttc, d.fk_unit';
  1136. $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposaldet as d";
  1137. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid";
  1138. $sql .= " WHERE d.fk_supplier_proposal = ".((int) $this->id);
  1139. $sql .= " ORDER by d.rang";
  1140. $result = $this->db->query($sql);
  1141. if ($result) {
  1142. $num = $this->db->num_rows($result);
  1143. $i = 0;
  1144. while ($i < $num) {
  1145. $objp = $this->db->fetch_object($result);
  1146. $line = new SupplierProposalLine($this->db);
  1147. $line->rowid = $objp->rowid; // deprecated
  1148. $line->id = $objp->rowid;
  1149. $line->fk_supplier_proposal = $objp->fk_supplier_proposal;
  1150. $line->fk_parent_line = $objp->fk_parent_line;
  1151. $line->product_type = $objp->product_type;
  1152. $line->label = $objp->custom_label;
  1153. $line->desc = $objp->description; // Description ligne
  1154. $line->qty = $objp->qty;
  1155. $line->tva_tx = $objp->tva_tx;
  1156. $line->localtax1_tx = $objp->localtax1_tx;
  1157. $line->localtax2_tx = $objp->localtax2_tx;
  1158. $line->subprice = $objp->subprice;
  1159. $line->fk_remise_except = $objp->fk_remise_except;
  1160. $line->remise_percent = $objp->remise_percent;
  1161. $line->info_bits = $objp->info_bits;
  1162. $line->total_ht = $objp->total_ht;
  1163. $line->total_tva = $objp->total_tva;
  1164. $line->total_localtax1 = $objp->total_localtax1;
  1165. $line->total_localtax2 = $objp->total_localtax2;
  1166. $line->total_ttc = $objp->total_ttc;
  1167. $line->fk_fournprice = $objp->fk_fournprice;
  1168. $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
  1169. $line->pa_ht = $marginInfos[0];
  1170. $line->marge_tx = $marginInfos[1];
  1171. $line->marque_tx = $marginInfos[2];
  1172. $line->special_code = $objp->special_code;
  1173. $line->rang = $objp->rang;
  1174. $line->fk_product = $objp->fk_product;
  1175. $line->ref = $objp->product_ref; // deprecated
  1176. $line->product_ref = $objp->product_ref;
  1177. $line->libelle = $objp->product_label; // deprecated
  1178. $line->product_label = $objp->product_label;
  1179. $line->product_desc = $objp->product_desc; // Description produit
  1180. $line->fk_product_type = $objp->fk_product_type;
  1181. $line->ref_fourn = $objp->ref_produit_fourn;
  1182. // Multicurrency
  1183. $line->fk_multicurrency = $objp->fk_multicurrency;
  1184. $line->multicurrency_code = $objp->multicurrency_code;
  1185. $line->multicurrency_subprice = $objp->multicurrency_subprice;
  1186. $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
  1187. $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
  1188. $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
  1189. $line->fk_unit = $objp->fk_unit;
  1190. $this->lines[$i] = $line;
  1191. $i++;
  1192. }
  1193. $this->db->free($result);
  1194. } else {
  1195. $this->error = $this->db->error();
  1196. return -1;
  1197. }
  1198. // Retrieve all extrafield
  1199. // fetch optionals attributes and labels
  1200. $this->fetch_optionals();
  1201. return 1;
  1202. }
  1203. $this->error = "Record Not Found";
  1204. return 0;
  1205. } else {
  1206. $this->error = $this->db->error();
  1207. return -1;
  1208. }
  1209. }
  1210. /**
  1211. * Set status to validated
  1212. *
  1213. * @param User $user Object user that validate
  1214. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1215. * @return int <0 if KO, >=0 if OK
  1216. */
  1217. public function valid($user, $notrigger = 0)
  1218. {
  1219. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1220. global $conf, $langs;
  1221. $error = 0;
  1222. $now = dol_now();
  1223. if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->supplier_proposal->creer))
  1224. || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->supplier_proposal->validate_advance))) {
  1225. $this->db->begin();
  1226. // Numbering module definition
  1227. $soc = new Societe($this->db);
  1228. $result = $soc->fetch($this->socid);
  1229. if ($result < 0) {
  1230. return -1;
  1231. }
  1232. // Define new ref
  1233. if (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref)) { // empty should not happened, but when it occurs, the test save life
  1234. $num = $this->getNextNumRef($soc);
  1235. } else {
  1236. $num = $this->ref;
  1237. }
  1238. $this->newref = dol_sanitizeFileName($num);
  1239. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal";
  1240. $sql .= " SET ref = '".$this->db->escape($num)."',";
  1241. $sql .= " fk_statut = 1, date_valid='".$this->db->idate($now)."', fk_user_valid=".((int) $user->id);
  1242. $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = 0";
  1243. dol_syslog(get_class($this)."::valid", LOG_DEBUG);
  1244. $resql = $this->db->query($sql);
  1245. if (!$resql) {
  1246. dol_print_error($this->db);
  1247. $error++;
  1248. }
  1249. // Trigger calls
  1250. if (!$error && !$notrigger) {
  1251. // Call trigger
  1252. $result = $this->call_trigger('PROPOSAL_SUPPLIER_VALIDATE', $user);
  1253. if ($result < 0) {
  1254. $error++;
  1255. }
  1256. // End call triggers
  1257. }
  1258. if (!$error) {
  1259. $this->oldref = $this->ref;
  1260. // Rename directory if dir was a temporary ref
  1261. if (preg_match('/^[\(]?PROV/i', $this->ref)) {
  1262. // Now we rename also files into index
  1263. $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'supplier_proposal/".$this->db->escape($this->newref)."'";
  1264. $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'supplier_proposal/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
  1265. $resql = $this->db->query($sql);
  1266. if (!$resql) {
  1267. $error++; $this->error = $this->db->lasterror();
  1268. }
  1269. // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
  1270. $oldref = dol_sanitizeFileName($this->ref);
  1271. $newref = dol_sanitizeFileName($num);
  1272. $dirsource = $conf->supplier_proposal->dir_output.'/'.$oldref;
  1273. $dirdest = $conf->supplier_proposal->dir_output.'/'.$newref;
  1274. if (!$error && file_exists($dirsource)) {
  1275. dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
  1276. if (@rename($dirsource, $dirdest)) {
  1277. dol_syslog("Rename ok");
  1278. // Rename docs starting with $oldref with $newref
  1279. $listoffiles = dol_dir_list($conf->supplier_proposal->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
  1280. foreach ($listoffiles as $fileentry) {
  1281. $dirsource = $fileentry['name'];
  1282. $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
  1283. $dirsource = $fileentry['path'].'/'.$dirsource;
  1284. $dirdest = $fileentry['path'].'/'.$dirdest;
  1285. @rename($dirsource, $dirdest);
  1286. }
  1287. }
  1288. }
  1289. }
  1290. $this->ref = $num;
  1291. $this->brouillon = 0;
  1292. $this->statut = 1;
  1293. $this->user_valid_id = $user->id;
  1294. $this->datev = $now;
  1295. $this->db->commit();
  1296. return 1;
  1297. } else {
  1298. $this->db->rollback();
  1299. return -1;
  1300. }
  1301. } else {
  1302. dol_syslog("You don't have permission to validate supplier proposal", LOG_WARNING);
  1303. return -2;
  1304. }
  1305. }
  1306. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1307. /**
  1308. * Set delivery date
  1309. *
  1310. * @param User $user Object user that modify
  1311. * @param int $delivery_date Delivery date
  1312. * @return int <0 if ko, >0 if ok
  1313. * @deprecated Use setDeliveryDate
  1314. */
  1315. public function set_date_livraison($user, $delivery_date)
  1316. {
  1317. // phpcs:enable
  1318. return $this->setDeliveryDate($user, $delivery_date);
  1319. }
  1320. /**
  1321. * Set delivery date
  1322. *
  1323. * @param User $user Object user that modify
  1324. * @param int $delivery_date Delivery date
  1325. * @return int <0 if ko, >0 if ok
  1326. */
  1327. public function setDeliveryDate($user, $delivery_date)
  1328. {
  1329. if (!empty($user->rights->supplier_proposal->creer)) {
  1330. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal ";
  1331. $sql .= " SET date_livraison = ".($delivery_date != '' ? "'".$this->db->idate($delivery_date)."'" : 'null');
  1332. $sql .= " WHERE rowid = ".((int) $this->id);
  1333. if ($this->db->query($sql)) {
  1334. $this->date_livraison = $delivery_date;
  1335. $this->delivery_date = $delivery_date;
  1336. return 1;
  1337. } else {
  1338. $this->error = $this->db->error();
  1339. dol_syslog(get_class($this)."::setDeliveryDate Erreur SQL");
  1340. return -1;
  1341. }
  1342. }
  1343. return 0;
  1344. }
  1345. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1346. /**
  1347. * Set an overall discount on the proposal
  1348. *
  1349. * @param User $user Object user that modify
  1350. * @param double $remise Amount discount
  1351. * @return int <0 if ko, >0 if ok
  1352. */
  1353. public function set_remise_percent($user, $remise)
  1354. {
  1355. // phpcs:enable
  1356. $remise = trim($remise) ?trim($remise) : 0;
  1357. if (!empty($user->rights->supplier_proposal->creer)) {
  1358. $remise = price2num($remise, 2);
  1359. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal SET remise_percent = ".((float) $remise);
  1360. $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = 0";
  1361. if ($this->db->query($sql)) {
  1362. $this->remise_percent = ((float) $remise);
  1363. $this->update_price(1);
  1364. return 1;
  1365. } else {
  1366. $this->error = $this->db->error();
  1367. return -1;
  1368. }
  1369. }
  1370. return 0;
  1371. }
  1372. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1373. /**
  1374. * Set an absolute overall discount on the proposal
  1375. *
  1376. * @param User $user Object user that modify
  1377. * @param double $remise Amount discount
  1378. * @return int <0 if ko, >0 if ok
  1379. */
  1380. public function set_remise_absolue($user, $remise)
  1381. {
  1382. // phpcs:enable
  1383. if (empty($remise)) {
  1384. $remise = 0;
  1385. }
  1386. $remise = price2num($remise);
  1387. if (!empty($user->rights->supplier_proposal->creer)) {
  1388. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal ";
  1389. $sql .= " SET remise_absolue = ".((float) $remise);
  1390. $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = 0";
  1391. if ($this->db->query($sql)) {
  1392. $this->remise_absolue = $remise;
  1393. $this->update_price(1);
  1394. return 1;
  1395. } else {
  1396. $this->error = $this->db->error();
  1397. return -1;
  1398. }
  1399. }
  1400. return 0;
  1401. }
  1402. /**
  1403. * Reopen the commercial proposal
  1404. *
  1405. * @param User $user Object user that close
  1406. * @param int $statut Statut
  1407. * @param string $note Comment
  1408. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1409. * @return int <0 if KO, >0 if OK
  1410. */
  1411. public function reopen($user, $statut, $note = '', $notrigger = 0)
  1412. {
  1413. global $langs, $conf;
  1414. $this->statut = $statut;
  1415. $error = 0;
  1416. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal";
  1417. $sql .= " SET fk_statut = ".((int) $this->statut).",";
  1418. if (!empty($note)) {
  1419. $sql .= " note_private = '".$this->db->escape($note)."',";
  1420. }
  1421. $sql .= " date_cloture=NULL, fk_user_cloture=NULL";
  1422. $sql .= " WHERE rowid = ".((int) $this->id);
  1423. $this->db->begin();
  1424. dol_syslog(get_class($this)."::reopen", LOG_DEBUG);
  1425. $resql = $this->db->query($sql);
  1426. if (!$resql) {
  1427. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  1428. }
  1429. if (!$error) {
  1430. if (!$notrigger) {
  1431. // Call trigger
  1432. $result = $this->call_trigger('PROPOSAL_SUPPLIER_REOPEN', $user);
  1433. if ($result < 0) {
  1434. $error++;
  1435. }
  1436. // End call triggers
  1437. }
  1438. }
  1439. // Commit or rollback
  1440. if ($error) {
  1441. if (!empty($this->errors)) {
  1442. foreach ($this->errors as $errmsg) {
  1443. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  1444. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  1445. }
  1446. }
  1447. $this->db->rollback();
  1448. return -1 * $error;
  1449. } else {
  1450. $this->db->commit();
  1451. return 1;
  1452. }
  1453. }
  1454. /**
  1455. * Close the askprice
  1456. *
  1457. * @param User $user Object user that close
  1458. * @param int $status Status
  1459. * @param string $note Comment
  1460. * @return int <0 if KO, >0 if OK
  1461. */
  1462. public function cloture($user, $status, $note)
  1463. {
  1464. global $langs, $conf;
  1465. $hidedetails = 0;
  1466. $hidedesc = 0;
  1467. $hideref = 0;
  1468. $this->statut = $status;
  1469. $error = 0;
  1470. $now = dol_now();
  1471. $this->db->begin();
  1472. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal";
  1473. $sql .= " SET fk_statut = ".((int) $status).", note_private = '".$this->db->escape($note)."', date_cloture='".$this->db->idate($now)."', fk_user_cloture=".$user->id;
  1474. $sql .= " WHERE rowid = ".((int) $this->id);
  1475. $resql = $this->db->query($sql);
  1476. if ($resql) {
  1477. $modelpdf = $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF_ODT_CLOSED ? $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF_ODT_CLOSED : (empty($this->model_pdf) ? '' : $this->model_pdf);
  1478. $triggerName = 'PROPOSAL_SUPPLIER_CLOSE_REFUSED';
  1479. if ($status == 2) {
  1480. $triggerName = 'PROPOSAL_SUPPLIER_CLOSE_SIGNED';
  1481. $modelpdf = $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF_ODT_TOBILL ? $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF_ODT_TOBILL : (empty($this->model_pdf) ? '' : $this->model_pdf);
  1482. if (!empty($conf->global->SUPPLIER_PROPOSAL_UPDATE_PRICE_ON_SUPPlIER_PROPOSAL)) { // TODO This option was not tested correctly. Error if product ref does not exists
  1483. $result = $this->updateOrCreatePriceFournisseur($user);
  1484. }
  1485. }
  1486. if ($status == 4) {
  1487. $triggerName = 'PROPOSAL_SUPPLIER_CLASSIFY_BILLED';
  1488. }
  1489. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  1490. // Define output language
  1491. $outputlangs = $langs;
  1492. if (!empty($conf->global->MAIN_MULTILANGS)) {
  1493. $outputlangs = new Translate("", $conf);
  1494. $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $this->thirdparty->default_lang);
  1495. $outputlangs->setDefaultLang($newlang);
  1496. }
  1497. //$ret=$object->fetch($id); // Reload to get new records
  1498. $this->generateDocument($modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  1499. }
  1500. // Call trigger
  1501. $result = $this->call_trigger($triggerName, $user);
  1502. if ($result < 0) {
  1503. $error++;
  1504. }
  1505. // End call triggers
  1506. if (!$error) {
  1507. $this->db->commit();
  1508. return 1;
  1509. } else {
  1510. $this->db->rollback();
  1511. return -1;
  1512. }
  1513. } else {
  1514. $this->error = $this->db->lasterror();
  1515. $this->errors[] = $this->db->lasterror();
  1516. $this->db->rollback();
  1517. return -1;
  1518. }
  1519. }
  1520. /**
  1521. * Add or update supplier price according to result of proposal
  1522. *
  1523. * @param User $user Object user
  1524. * @return int > 0 if OK
  1525. */
  1526. public function updateOrCreatePriceFournisseur($user)
  1527. {
  1528. global $conf;
  1529. dol_syslog(get_class($this)."::updateOrCreatePriceFournisseur", LOG_DEBUG);
  1530. foreach ($this->lines as $product) {
  1531. if ($product->subprice <= 0) {
  1532. continue;
  1533. }
  1534. $productsupplier = new ProductFournisseur($this->db);
  1535. $multicurrency_tx = 1;
  1536. $fk_multicurrency = 0;
  1537. if (empty($this->thirdparty)) {
  1538. $this->fetch_thirdparty();
  1539. }
  1540. $ref_fourn = $product->ref_fourn;
  1541. if (empty($ref_fourn)) {
  1542. $ref_fourn = $product->ref_supplier;
  1543. }
  1544. if (!empty($conf->multicurrency->enabled) && !empty($product->multicurrency_code)) {
  1545. list($fk_multicurrency, $multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $product->multicurrency_code);
  1546. }
  1547. $productsupplier->id = $product->fk_product;
  1548. $productsupplier->update_buyprice($product->qty, $product->total_ht, $user, 'HT', $this->thirdparty, '', $ref_fourn, $product->tva_tx, 0, 0, 0, $product->info_bits, '', '', array(), '', $product->multicurrency_total_ht, 'HT', $multicurrency_tx, $product->multicurrency_code, '', '', '');
  1549. }
  1550. return 1;
  1551. }
  1552. /**
  1553. * Upate ProductFournisseur
  1554. *
  1555. * @param int $idProductFournPrice id of llx_product_fournisseur_price
  1556. * @param Product $product contain informations to update
  1557. * @param User $user Object user
  1558. * @return int <0 if KO, >0 if OK
  1559. */
  1560. public function updatePriceFournisseur($idProductFournPrice, $product, $user)
  1561. {
  1562. $price = price2num($product->subprice * $product->qty, 'MU');
  1563. $unitPrice = price2num($product->subprice, 'MU');
  1564. $sql = 'UPDATE '.MAIN_DB_PREFIX.'product_fournisseur_price SET '.(!empty($product->ref_fourn) ? 'ref_fourn = "'.$this->db->escape($product->ref_fourn).'", ' : '').' price ='.((float) $price).', unitprice ='.((float) $unitPrice).' WHERE rowid = '.((int) $idProductFournPrice);
  1565. $resql = $this->db->query($sql);
  1566. if (!$resql) {
  1567. $this->error = $this->db->error();
  1568. $this->db->rollback();
  1569. return -1;
  1570. }
  1571. return 1;
  1572. }
  1573. /**
  1574. * Create ProductFournisseur
  1575. *
  1576. * @param Product $product Object Product
  1577. * @param User $user Object user
  1578. * @return int <0 if KO, >0 if OK
  1579. */
  1580. public function createPriceFournisseur($product, $user)
  1581. {
  1582. global $conf;
  1583. $price = price2num($product->subprice * $product->qty, 'MU');
  1584. $qty = price2num($product->qty);
  1585. $unitPrice = price2num($product->subprice, 'MU');
  1586. $now = dol_now();
  1587. $values = array(
  1588. "'".$this->db->idate($now)."'",
  1589. $product->fk_product,
  1590. $this->thirdparty->id,
  1591. "'".$product->ref_fourn."'",
  1592. $price,
  1593. $qty,
  1594. $unitPrice,
  1595. $product->tva_tx,
  1596. $user->id
  1597. );
  1598. if (!empty($conf->multicurrency->enabled)) {
  1599. if (!empty($product->multicurrency_code)) {
  1600. include_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
  1601. $multicurrency = new MultiCurrency($this->db); //need to fetch because empty fk_multicurrency and rate
  1602. $multicurrency->fetch(0, $product->multicurrency_code);
  1603. if (!empty($multicurrency->id)) {
  1604. $values[] = $multicurrency->id;
  1605. $values[] = "'".$product->multicurrency_code."'";
  1606. $values[] = $product->multicurrency_subprice;
  1607. $values[] = $product->multicurrency_total_ht;
  1608. $values[] = $multicurrency->rate->rate;
  1609. } else {
  1610. for ($i = 0; $i < 5; $i++) {
  1611. $values[] = 'NULL';
  1612. }
  1613. }
  1614. }
  1615. }
  1616. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'product_fournisseur_price ';
  1617. $sql .= '(datec, fk_product, fk_soc, ref_fourn, price, quantity, unitprice, tva_tx, fk_user';
  1618. if (!empty($conf->multicurrency->enabled) && !empty($product->multicurrency_code)) {
  1619. $sql .= ',fk_multicurrency, multicurrency_code, multicurrency_unitprice, multicurrency_price, multicurrency_tx';
  1620. }
  1621. $sql .= ') VALUES ('.implode(',', $values).')';
  1622. $resql = $this->db->query($sql);
  1623. if (!$resql) {
  1624. $this->error = $this->db->error();
  1625. $this->db->rollback();
  1626. return -1;
  1627. }
  1628. return 1;
  1629. }
  1630. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1631. /**
  1632. * Set draft status
  1633. *
  1634. * @param User $user Object user that modify
  1635. * @return int <0 if KO, >0 if OK
  1636. */
  1637. public function setDraft($user)
  1638. {
  1639. // phpcs:enable
  1640. global $conf, $langs;
  1641. $error = 0;
  1642. if ($this->statut == self::STATUS_DRAFT) {
  1643. dol_syslog(get_class($this)."::setDraft already draft status", LOG_WARNING);
  1644. return 0;
  1645. }
  1646. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal";
  1647. $sql .= " SET fk_statut = ".self::STATUS_DRAFT;
  1648. $sql .= " WHERE rowid = ".((int) $this->id);
  1649. if ($this->db->query($sql)) {
  1650. if (!$error) {
  1651. $this->oldcopy = clone $this;
  1652. }
  1653. if (!$error) {
  1654. // Call trigger
  1655. $result = $this->call_trigger('PROPOSAL_SUPPLIER_UNVALIDATE', $user);
  1656. if ($result < 0) {
  1657. $error++;
  1658. }
  1659. }
  1660. if (!$error) {
  1661. $this->statut = self::STATUS_DRAFT;
  1662. $this->brouillon = 1;
  1663. $this->db->commit();
  1664. return 1;
  1665. } else {
  1666. $this->db->rollback();
  1667. return -1;
  1668. }
  1669. } else {
  1670. return -1;
  1671. }
  1672. }
  1673. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1674. /**
  1675. * Return list of askprice (eventually filtered on user) into an array
  1676. *
  1677. * @param int $shortlist 0=Return array[id]=ref, 1=Return array[](id=>id,ref=>ref,name=>name)
  1678. * @param int $draft 0=not draft, 1=draft
  1679. * @param int $notcurrentuser 0=all user, 1=not current user
  1680. * @param int $socid Id third pary
  1681. * @param int $limit For pagination
  1682. * @param int $offset For pagination
  1683. * @param string $sortfield Sort criteria
  1684. * @param string $sortorder Sort order
  1685. * @return int -1 if KO, array with result if OK
  1686. */
  1687. public function liste_array($shortlist = 0, $draft = 0, $notcurrentuser = 0, $socid = 0, $limit = 0, $offset = 0, $sortfield = 'p.datec', $sortorder = 'DESC')
  1688. {
  1689. // phpcs:enable
  1690. global $conf, $user;
  1691. $ga = array();
  1692. $sql = "SELECT s.rowid, s.nom as name, s.client,";
  1693. $sql .= " p.rowid as supplier_proposalid, p.fk_statut, p.total_ht, p.ref, p.remise, ";
  1694. $sql .= " p.datep as dp, p.fin_validite as datelimite";
  1695. if (empty($user->rights->societe->client->voir) && !$socid) {
  1696. $sql .= ", sc.fk_soc, sc.fk_user";
  1697. }
  1698. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."supplier_proposal as p, ".MAIN_DB_PREFIX."c_propalst as c";
  1699. if (empty($user->rights->societe->client->voir) && !$socid) {
  1700. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  1701. }
  1702. $sql .= " WHERE p.entity IN (".getEntity('supplier_proposal').")";
  1703. $sql .= " AND p.fk_soc = s.rowid";
  1704. $sql .= " AND p.fk_statut = c.id";
  1705. if (empty($user->rights->societe->client->voir) && !$socid) { //restriction
  1706. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  1707. }
  1708. if ($socid) {
  1709. $sql .= " AND s.rowid = ".((int) $socid);
  1710. }
  1711. if ($draft) {
  1712. $sql .= " AND p.fk_statut = 0";
  1713. }
  1714. if ($notcurrentuser > 0) {
  1715. $sql .= " AND p.fk_user_author <> ".$user->id;
  1716. }
  1717. $sql .= $this->db->order($sortfield, $sortorder);
  1718. $sql .= $this->db->plimit($limit, $offset);
  1719. $result = $this->db->query($sql);
  1720. if ($result) {
  1721. $num = $this->db->num_rows($result);
  1722. if ($num) {
  1723. $i = 0;
  1724. while ($i < $num) {
  1725. $obj = $this->db->fetch_object($result);
  1726. if ($shortlist == 1) {
  1727. $ga[$obj->supplier_proposalid] = $obj->ref;
  1728. } elseif ($shortlist == 2) {
  1729. $ga[$obj->supplier_proposalid] = $obj->ref.' ('.$obj->name.')';
  1730. } else {
  1731. $ga[$i]['id'] = $obj->supplier_proposalid;
  1732. $ga[$i]['ref'] = $obj->ref;
  1733. $ga[$i]['name'] = $obj->name;
  1734. }
  1735. $i++;
  1736. }
  1737. }
  1738. return $ga;
  1739. } else {
  1740. dol_print_error($this->db);
  1741. return -1;
  1742. }
  1743. }
  1744. /**
  1745. * Delete askprice
  1746. *
  1747. * @param User $user Object user that delete
  1748. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1749. * @return int 1 if ok, otherwise if error
  1750. */
  1751. public function delete($user, $notrigger = 0)
  1752. {
  1753. global $conf, $langs;
  1754. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1755. $error = 0;
  1756. $this->db->begin();
  1757. if (!$notrigger) {
  1758. // Call trigger
  1759. $result = $this->call_trigger('PROPOSAL_SUPPLIER_DELETE', $user);
  1760. if ($result < 0) {
  1761. $error++;
  1762. }
  1763. // End call triggers
  1764. }
  1765. if (!$error) {
  1766. $main = MAIN_DB_PREFIX.'supplier_proposaldet';
  1767. $ef = $main."_extrafields";
  1768. $sqlef = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_supplier_proposal = ".((int) $this->id).")";
  1769. $sql = "DELETE FROM ".MAIN_DB_PREFIX."supplier_proposaldet WHERE fk_supplier_proposal = ".((int) $this->id);
  1770. if ($this->db->query($sql)) {
  1771. $sql = "DELETE FROM ".MAIN_DB_PREFIX."supplier_proposal WHERE rowid = ".((int) $this->id);
  1772. if ($this->db->query($sqlef) && $this->db->query($sql)) {
  1773. // Delete linked object
  1774. $res = $this->deleteObjectLinked();
  1775. if ($res < 0) {
  1776. $error++;
  1777. }
  1778. if (!$error) {
  1779. // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
  1780. $this->deleteEcmFiles();
  1781. // We remove directory
  1782. $ref = dol_sanitizeFileName($this->ref);
  1783. if ($conf->supplier_proposal->dir_output && !empty($this->ref)) {
  1784. $dir = $conf->supplier_proposal->dir_output."/".$ref;
  1785. $file = $dir."/".$ref.".pdf";
  1786. if (file_exists($file)) {
  1787. dol_delete_preview($this);
  1788. if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers
  1789. $this->error = 'ErrorFailToDeleteFile';
  1790. $this->errors = array('ErrorFailToDeleteFile');
  1791. $this->db->rollback();
  1792. return 0;
  1793. }
  1794. }
  1795. if (file_exists($dir)) {
  1796. $res = @dol_delete_dir_recursive($dir);
  1797. if (!$res) {
  1798. $this->error = 'ErrorFailToDeleteDir';
  1799. $this->errors = array('ErrorFailToDeleteDir');
  1800. $this->db->rollback();
  1801. return 0;
  1802. }
  1803. }
  1804. }
  1805. }
  1806. // Removed extrafields
  1807. if (!$error) {
  1808. $result = $this->deleteExtraFields();
  1809. if ($result < 0) {
  1810. $error++;
  1811. $errorflag = -4;
  1812. dol_syslog(get_class($this)."::delete erreur ".$errorflag." ".$this->error, LOG_ERR);
  1813. }
  1814. }
  1815. if (!$error) {
  1816. dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG);
  1817. $this->db->commit();
  1818. return 1;
  1819. } else {
  1820. $this->error = $this->db->lasterror();
  1821. $this->db->rollback();
  1822. return 0;
  1823. }
  1824. } else {
  1825. $this->error = $this->db->lasterror();
  1826. $this->db->rollback();
  1827. return -3;
  1828. }
  1829. } else {
  1830. $this->error = $this->db->lasterror();
  1831. $this->db->rollback();
  1832. return -2;
  1833. }
  1834. } else {
  1835. $this->db->rollback();
  1836. return -1;
  1837. }
  1838. }
  1839. /**
  1840. * Object SupplierProposal Information
  1841. *
  1842. * @param int $id Proposal id
  1843. * @return void
  1844. */
  1845. public function info($id)
  1846. {
  1847. $sql = "SELECT c.rowid, ";
  1848. $sql .= " c.datec, c.date_valid as datev, c.date_cloture as dateo,";
  1849. $sql .= " c.fk_user_author, c.fk_user_valid, c.fk_user_cloture";
  1850. $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal as c";
  1851. $sql .= " WHERE c.rowid = ".((int) $id);
  1852. $result = $this->db->query($sql);
  1853. if ($result) {
  1854. if ($this->db->num_rows($result)) {
  1855. $obj = $this->db->fetch_object($result);
  1856. $this->id = $obj->rowid;
  1857. $this->date_creation = $this->db->jdate($obj->datec);
  1858. $this->date_validation = $this->db->jdate($obj->datev);
  1859. $this->date_cloture = $this->db->jdate($obj->dateo);
  1860. $cuser = new User($this->db);
  1861. $cuser->fetch($obj->fk_user_author);
  1862. $this->user_creation = $cuser;
  1863. if ($obj->fk_user_valid) {
  1864. $vuser = new User($this->db);
  1865. $vuser->fetch($obj->fk_user_valid);
  1866. $this->user_validation = $vuser;
  1867. }
  1868. if ($obj->fk_user_cloture) {
  1869. $cluser = new User($this->db);
  1870. $cluser->fetch($obj->fk_user_cloture);
  1871. $this->user_cloture = $cluser;
  1872. }
  1873. }
  1874. $this->db->free($result);
  1875. } else {
  1876. dol_print_error($this->db);
  1877. }
  1878. }
  1879. /**
  1880. * Return label of status of proposal (draft, validated, ...)
  1881. *
  1882. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  1883. * @return string Label
  1884. */
  1885. public function getLibStatut($mode = 0)
  1886. {
  1887. return $this->LibStatut((isset($this->statut) ? $this->statut : $this->status), $mode);
  1888. }
  1889. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1890. /**
  1891. * Return label of a status (draft, validated, ...)
  1892. *
  1893. * @param int $status Id status
  1894. * @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
  1895. * @return string Label
  1896. */
  1897. public function LibStatut($status, $mode = 1)
  1898. {
  1899. // phpcs:enable
  1900. // Init/load array of translation of status
  1901. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  1902. global $langs;
  1903. $langs->load("supplier_proposal");
  1904. $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv("SupplierProposalStatusDraft");
  1905. $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv("SupplierProposalStatusValidated");
  1906. $this->labelStatus[self::STATUS_SIGNED] = $langs->transnoentitiesnoconv("SupplierProposalStatusSigned");
  1907. $this->labelStatus[self::STATUS_NOTSIGNED] = $langs->transnoentitiesnoconv("SupplierProposalStatusNotSigned");
  1908. $this->labelStatus[self::STATUS_CLOSE] = $langs->transnoentitiesnoconv("SupplierProposalStatusClosed");
  1909. $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv("SupplierProposalStatusDraftShort");
  1910. $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv("Opened");
  1911. $this->labelStatusShort[self::STATUS_SIGNED] = $langs->transnoentitiesnoconv("SupplierProposalStatusSignedShort");
  1912. $this->labelStatusShort[self::STATUS_NOTSIGNED] = $langs->transnoentitiesnoconv("SupplierProposalStatusNotSignedShort");
  1913. $this->labelStatusShort[self::STATUS_CLOSE] = $langs->transnoentitiesnoconv("SupplierProposalStatusClosedShort");
  1914. }
  1915. $statusnew = '';
  1916. if ($status == self::STATUS_DRAFT) {
  1917. $statusnew = 'status0';
  1918. } elseif ($status == self::STATUS_VALIDATED) {
  1919. $statusnew = 'status1';
  1920. } elseif ($status == self::STATUS_SIGNED) {
  1921. $statusnew = 'status4';
  1922. } elseif ($status == self::STATUS_NOTSIGNED) {
  1923. $statusnew = 'status9';
  1924. } elseif ($status == self::STATUS_CLOSE) {
  1925. $statusnew = 'status6';
  1926. }
  1927. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusnew, $mode);
  1928. }
  1929. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1930. /**
  1931. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  1932. *
  1933. * @param User $user Object user
  1934. * @param int $mode "opened" for askprice to close, "signed" for proposal to invoice
  1935. * @return int <0 if KO, >0 if OK
  1936. */
  1937. public function load_board($user, $mode)
  1938. {
  1939. // phpcs:enable
  1940. global $conf, $user, $langs;
  1941. $now = dol_now();
  1942. $this->nbtodo = $this->nbtodolate = 0;
  1943. $clause = " WHERE";
  1944. $sql = "SELECT p.rowid, p.ref, p.datec as datec, p.date_cloture as datefin";
  1945. $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal as p";
  1946. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  1947. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc";
  1948. $sql .= " WHERE sc.fk_user = ".((int) $user->id);
  1949. $clause = " AND";
  1950. }
  1951. $sql .= $clause." p.entity IN (".getEntity('supplier_proposal').")";
  1952. if ($mode == 'opened') {
  1953. $sql .= " AND p.fk_statut = 1";
  1954. }
  1955. if ($mode == 'signed') {
  1956. $sql .= " AND p.fk_statut = 2";
  1957. }
  1958. if ($user->socid) {
  1959. $sql .= " AND p.fk_soc = ".((int) $user->socid);
  1960. }
  1961. $resql = $this->db->query($sql);
  1962. if ($resql) {
  1963. $label = $labelShort = '';
  1964. $status = '';
  1965. if ($mode == 'opened') {
  1966. $delay_warning = !empty($conf->supplier_proposal->cloture->warning_delay) ? $conf->supplier_proposal->cloture->warning_delay : 0;
  1967. $status = self::STATUS_VALIDATED;
  1968. $label = $langs->trans("SupplierProposalsToClose");
  1969. $labelShort = $langs->trans("ToAcceptRefuse");
  1970. }
  1971. if ($mode == 'signed') {
  1972. $delay_warning = !empty($conf->supplier_proposal->facturation->warning_delay) ? $conf->supplier_proposal->facturation->warning_delay : 0;
  1973. $status = self::STATUS_SIGNED;
  1974. $label = $langs->trans("SupplierProposalsToProcess"); // May be billed or ordered
  1975. $labelShort = $langs->trans("ToClose");
  1976. }
  1977. $response = new WorkboardResponse();
  1978. $response->warning_delay = $delay_warning / 60 / 60 / 24;
  1979. $response->label = $label;
  1980. $response->labelShort = $labelShort;
  1981. $response->url = DOL_URL_ROOT.'/supplier_proposal/list.php?search_status='.$status;
  1982. $response->img = img_object('', "propal");
  1983. // This assignment in condition is not a bug. It allows walking the results.
  1984. while ($obj = $this->db->fetch_object($resql)) {
  1985. $response->nbtodo++;
  1986. if ($mode == 'opened') {
  1987. $datelimit = $this->db->jdate($obj->datefin);
  1988. if ($datelimit < ($now - $delay_warning)) {
  1989. $response->nbtodolate++;
  1990. }
  1991. }
  1992. // TODO Definir regle des propales a facturer en retard
  1993. // if ($mode == 'signed' && ! count($this->FactureListeArray($obj->rowid))) $this->nbtodolate++;
  1994. }
  1995. return $response;
  1996. } else {
  1997. $this->error = $this->db->lasterror();
  1998. return -1;
  1999. }
  2000. }
  2001. /**
  2002. * Initialise an instance with random values.
  2003. * Used to build previews or test instances.
  2004. * id must be 0 if object instance is a specimen.
  2005. *
  2006. * @return void
  2007. */
  2008. public function initAsSpecimen()
  2009. {
  2010. global $user, $langs, $conf;
  2011. // Load array of products prodids
  2012. $num_prods = 0;
  2013. $prodids = array();
  2014. $sql = "SELECT rowid";
  2015. $sql .= " FROM ".MAIN_DB_PREFIX."product";
  2016. $sql .= " WHERE entity IN (".getEntity('product').")";
  2017. $sql .= $this->db->plimit(100);
  2018. $resql = $this->db->query($sql);
  2019. if ($resql) {
  2020. $num_prods = $this->db->num_rows($resql);
  2021. $i = 0;
  2022. while ($i < $num_prods) {
  2023. $i++;
  2024. $row = $this->db->fetch_row($resql);
  2025. $prodids[$i] = $row[0];
  2026. }
  2027. }
  2028. // Initialise parametres
  2029. $this->id = 0;
  2030. $this->ref = 'SPECIMEN';
  2031. $this->specimen = 1;
  2032. $this->socid = 1;
  2033. $this->date = time();
  2034. $this->cond_reglement_id = 1;
  2035. $this->cond_reglement_code = 'RECEP';
  2036. $this->mode_reglement_id = 7;
  2037. $this->mode_reglement_code = 'CHQ';
  2038. $this->note_public = 'This is a comment (public)';
  2039. $this->note_private = 'This is a comment (private)';
  2040. // Lines
  2041. $nbp = 5;
  2042. $xnbp = 0;
  2043. while ($xnbp < $nbp) {
  2044. $line = new SupplierProposalLine($this->db);
  2045. $line->desc = $langs->trans("Description")." ".$xnbp;
  2046. $line->qty = 1;
  2047. $line->subprice = 100;
  2048. $line->tva_tx = 19.6;
  2049. $line->localtax1_tx = 0;
  2050. $line->localtax2_tx = 0;
  2051. if ($xnbp == 2) {
  2052. $line->total_ht = 50;
  2053. $line->total_ttc = 59.8;
  2054. $line->total_tva = 9.8;
  2055. $line->remise_percent = 50;
  2056. } else {
  2057. $line->total_ht = 100;
  2058. $line->total_ttc = 119.6;
  2059. $line->total_tva = 19.6;
  2060. $line->remise_percent = 00;
  2061. }
  2062. if ($num_prods > 0) {
  2063. $prodid = mt_rand(1, $num_prods);
  2064. $line->fk_product = $prodids[$prodid];
  2065. }
  2066. $this->lines[$xnbp] = $line;
  2067. $this->total_ht += $line->total_ht;
  2068. $this->total_tva += $line->total_tva;
  2069. $this->total_ttc += $line->total_ttc;
  2070. $xnbp++;
  2071. }
  2072. }
  2073. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2074. /**
  2075. * Load indicator this->nb of global stats widget
  2076. *
  2077. * @return int <0 if ko, >0 if ok
  2078. */
  2079. public function load_state_board()
  2080. {
  2081. // phpcs:enable
  2082. global $conf, $user;
  2083. $this->nb = array();
  2084. $clause = "WHERE";
  2085. $sql = "SELECT count(p.rowid) as nb";
  2086. $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal as p";
  2087. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
  2088. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  2089. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
  2090. $sql .= " WHERE sc.fk_user = ".((int) $user->id);
  2091. $clause = "AND";
  2092. }
  2093. $sql .= " ".$clause." p.entity IN (".getEntity('supplier_proposal').")";
  2094. $resql = $this->db->query($sql);
  2095. if ($resql) {
  2096. // This assignment in condition is not a bug. It allows walking the results.
  2097. while ($obj = $this->db->fetch_object($resql)) {
  2098. $this->nb["supplier_proposals"] = $obj->nb;
  2099. }
  2100. $this->db->free($resql);
  2101. return 1;
  2102. } else {
  2103. dol_print_error($this->db);
  2104. $this->error = $this->db->lasterror();
  2105. return -1;
  2106. }
  2107. }
  2108. /**
  2109. * Returns the reference to the following non used Proposal used depending on the active numbering module
  2110. * defined into SUPPLIER_PROPOSAL_ADDON
  2111. *
  2112. * @param Societe $soc Object thirdparty
  2113. * @return string Reference libre pour la propale
  2114. */
  2115. public function getNextNumRef($soc)
  2116. {
  2117. global $conf, $db, $langs;
  2118. $langs->load("supplier_proposal");
  2119. if (!empty($conf->global->SUPPLIER_PROPOSAL_ADDON)) {
  2120. $mybool = false;
  2121. $file = $conf->global->SUPPLIER_PROPOSAL_ADDON.".php";
  2122. $classname = $conf->global->SUPPLIER_PROPOSAL_ADDON;
  2123. // Include file with class
  2124. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  2125. foreach ($dirmodels as $reldir) {
  2126. $dir = dol_buildpath($reldir."core/modules/supplier_proposal/");
  2127. // Load file with numbering class (if found)
  2128. $mybool |= @include_once $dir.$file;
  2129. }
  2130. if (!$mybool) {
  2131. dol_print_error('', "Failed to include file ".$file);
  2132. return '';
  2133. }
  2134. $obj = new $classname();
  2135. $numref = "";
  2136. $numref = $obj->getNextValue($soc, $this);
  2137. if ($numref != "") {
  2138. return $numref;
  2139. } else {
  2140. $this->error = $obj->error;
  2141. return "";
  2142. }
  2143. } else {
  2144. $langs->load("errors");
  2145. print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("SupplierProposal"));
  2146. return "";
  2147. }
  2148. }
  2149. /**
  2150. * Return clicable link of object (with eventually picto)
  2151. *
  2152. * @param int $withpicto Add picto into link
  2153. * @param string $option Where point the link ('compta', 'expedition', 'document', ...)
  2154. * @param string $get_params Parametres added to url
  2155. * @param int $notooltip 1=Disable tooltip
  2156. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  2157. * @param int $addlinktonotes Add link to show notes
  2158. * @return string String with URL
  2159. */
  2160. public function getNomUrl($withpicto = 0, $option = '', $get_params = '', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
  2161. {
  2162. global $langs, $conf, $user, $hookmanager;
  2163. if (!empty($conf->dol_no_mouse_hover)) {
  2164. $notooltip = 1; // Force disable tooltips
  2165. }
  2166. $url = '';
  2167. $result = '';
  2168. $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("SupplierProposal").'</u>';
  2169. if (isset($this->status)) {
  2170. $label .= ' '.$this->getLibStatut(5);
  2171. }
  2172. if (!empty($this->ref)) {
  2173. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  2174. }
  2175. if (!empty($this->ref_fourn)) {
  2176. $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_fourn;
  2177. }
  2178. if (!empty($this->total_ht)) {
  2179. $label .= '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
  2180. }
  2181. if (!empty($this->total_tva)) {
  2182. $label .= '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
  2183. }
  2184. if (!empty($this->total_ttc)) {
  2185. $label .= '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
  2186. }
  2187. if ($option == '') {
  2188. $url = DOL_URL_ROOT.'/supplier_proposal/card.php?id='.$this->id.$get_params;
  2189. }
  2190. if ($option == 'document') {
  2191. $url = DOL_URL_ROOT.'/supplier_proposal/document.php?id='.$this->id.$get_params;
  2192. }
  2193. if ($option !== 'nolink') {
  2194. // Add param to save lastsearch_values or not
  2195. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  2196. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  2197. $add_save_lastsearch_values = 1;
  2198. }
  2199. if ($add_save_lastsearch_values) {
  2200. $url .= '&save_lastsearch_values=1';
  2201. }
  2202. }
  2203. $linkclose = '';
  2204. if (empty($notooltip) && $user->rights->propal->lire) {
  2205. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  2206. $label = $langs->trans("ShowSupplierProposal");
  2207. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  2208. }
  2209. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  2210. $linkclose .= ' class="classfortooltip"';
  2211. }
  2212. $linkstart = '<a href="'.$url.'"';
  2213. $linkstart .= $linkclose.'>';
  2214. $linkend = '</a>';
  2215. $result .= $linkstart;
  2216. if ($withpicto) {
  2217. $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  2218. }
  2219. if ($withpicto != 2) {
  2220. $result .= $this->ref;
  2221. }
  2222. $result .= $linkend;
  2223. if ($addlinktonotes) {
  2224. $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
  2225. if ($txttoshow) {
  2226. $notetoshow = $langs->trans("ViewPrivateNote").':<br>'.dol_string_nohtmltag($txttoshow, 1);
  2227. $result .= ' <span class="note inline-block">';
  2228. $result .= '<a href="'.DOL_URL_ROOT.'/supplier_proposal/note.php?id='.$this->id.'" class="classfortooltip" title="'.dol_escape_htmltag($notetoshow).'">';
  2229. $result .= img_picto('', 'note');
  2230. $result .= '</a>';
  2231. //$result.=img_picto($langs->trans("ViewNote"),'object_generic');
  2232. //$result.='</a>';
  2233. $result .= '</span>';
  2234. }
  2235. }
  2236. global $action;
  2237. $hookmanager->initHooks(array($this->element . 'dao'));
  2238. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  2239. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  2240. if ($reshook > 0) {
  2241. $result = $hookmanager->resPrint;
  2242. } else {
  2243. $result .= $hookmanager->resPrint;
  2244. }
  2245. return $result;
  2246. }
  2247. /**
  2248. * Retrieve an array of supplier proposal lines
  2249. *
  2250. * @return int >0 if OK, <0 if KO
  2251. */
  2252. public function getLinesArray()
  2253. {
  2254. // For other object, here we call fetch_lines. But fetch_lines does not exists on supplier proposal
  2255. $sql = 'SELECT pt.rowid, pt.label as custom_label, pt.description, pt.fk_product, pt.fk_remise_except,';
  2256. $sql .= ' pt.qty, pt.tva_tx, pt.remise_percent, pt.subprice, pt.info_bits,';
  2257. $sql .= ' pt.total_ht, pt.total_tva, pt.total_ttc, pt.fk_product_fournisseur_price as fk_fournprice, pt.buy_price_ht as pa_ht, pt.special_code, pt.localtax1_tx, pt.localtax2_tx,';
  2258. $sql .= ' pt.product_type, pt.rang, pt.fk_parent_line,';
  2259. $sql .= ' p.label as product_label, p.ref, p.fk_product_type, p.rowid as prodid,';
  2260. $sql .= ' p.description as product_desc, pt.ref_fourn as ref_supplier,';
  2261. $sql .= ' pt.fk_multicurrency, pt.multicurrency_code, pt.multicurrency_subprice, pt.multicurrency_total_ht, pt.multicurrency_total_tva, pt.multicurrency_total_ttc, pt.fk_unit';
  2262. $sql .= ' FROM '.MAIN_DB_PREFIX.'supplier_proposaldet as pt';
  2263. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product=p.rowid';
  2264. $sql .= ' WHERE pt.fk_supplier_proposal = '.((int) $this->id);
  2265. $sql .= ' ORDER BY pt.rang ASC, pt.rowid';
  2266. dol_syslog(get_class($this).'::getLinesArray', LOG_DEBUG);
  2267. $resql = $this->db->query($sql);
  2268. if ($resql) {
  2269. $num = $this->db->num_rows($resql);
  2270. $i = 0;
  2271. while ($i < $num) {
  2272. $obj = $this->db->fetch_object($resql);
  2273. $this->lines[$i] = new SupplierProposalLine($this->db);
  2274. $this->lines[$i]->id = $obj->rowid; // for backward compatibility
  2275. $this->lines[$i]->rowid = $obj->rowid;
  2276. $this->lines[$i]->label = $obj->custom_label;
  2277. $this->lines[$i]->description = $obj->description;
  2278. $this->lines[$i]->fk_product = $obj->fk_product;
  2279. $this->lines[$i]->ref = $obj->ref;
  2280. $this->lines[$i]->product_label = $obj->product_label;
  2281. $this->lines[$i]->product_desc = $obj->product_desc;
  2282. $this->lines[$i]->fk_product_type = $obj->fk_product_type; // deprecated
  2283. $this->lines[$i]->product_type = $obj->product_type;
  2284. $this->lines[$i]->qty = $obj->qty;
  2285. $this->lines[$i]->subprice = $obj->subprice;
  2286. $this->lines[$i]->fk_remise_except = $obj->fk_remise_except;
  2287. $this->lines[$i]->remise_percent = $obj->remise_percent;
  2288. $this->lines[$i]->tva_tx = $obj->tva_tx;
  2289. $this->lines[$i]->info_bits = $obj->info_bits;
  2290. $this->lines[$i]->total_ht = $obj->total_ht;
  2291. $this->lines[$i]->total_tva = $obj->total_tva;
  2292. $this->lines[$i]->total_ttc = $obj->total_ttc;
  2293. $this->lines[$i]->fk_fournprice = $obj->fk_fournprice;
  2294. $marginInfos = getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->lines[$i]->fk_fournprice, $obj->pa_ht);
  2295. $this->lines[$i]->pa_ht = $marginInfos[0];
  2296. $this->lines[$i]->marge_tx = $marginInfos[1];
  2297. $this->lines[$i]->marque_tx = $marginInfos[2];
  2298. $this->lines[$i]->fk_parent_line = $obj->fk_parent_line;
  2299. $this->lines[$i]->special_code = $obj->special_code;
  2300. $this->lines[$i]->rang = $obj->rang;
  2301. $this->lines[$i]->ref_fourn = $obj->ref_supplier; // deprecated
  2302. $this->lines[$i]->ref_supplier = $obj->ref_supplier;
  2303. // Multicurrency
  2304. $this->lines[$i]->fk_multicurrency = $obj->fk_multicurrency;
  2305. $this->lines[$i]->multicurrency_code = $obj->multicurrency_code;
  2306. $this->lines[$i]->multicurrency_subprice = $obj->multicurrency_subprice;
  2307. $this->lines[$i]->multicurrency_total_ht = $obj->multicurrency_total_ht;
  2308. $this->lines[$i]->multicurrency_total_tva = $obj->multicurrency_total_tva;
  2309. $this->lines[$i]->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
  2310. $this->lines[$i]->fk_unit = $obj->fk_unit;
  2311. $i++;
  2312. }
  2313. $this->db->free($resql);
  2314. return 1;
  2315. } else {
  2316. $this->error = $this->db->error();
  2317. return -1;
  2318. }
  2319. }
  2320. /**
  2321. * Create a document onto disk according to template module.
  2322. *
  2323. * @param string $modele Force model to use ('' to not force)
  2324. * @param Translate $outputlangs Object langs to use for output
  2325. * @param int $hidedetails Hide details of lines
  2326. * @param int $hidedesc Hide description
  2327. * @param int $hideref Hide ref
  2328. * @param null|array $moreparams Array to provide more information
  2329. * @return int 0 if KO, 1 if OK
  2330. */
  2331. public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
  2332. {
  2333. global $conf, $langs;
  2334. $langs->load("supplier_proposal");
  2335. $outputlangs->load("products");
  2336. if (!dol_strlen($modele)) {
  2337. $modele = 'aurore';
  2338. if ($this->model_pdf) {
  2339. $modele = $this->model_pdf;
  2340. } elseif (!empty($conf->global->SUPPLIER_PROPOSAL_ADDON_PDF)) {
  2341. $modele = $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF;
  2342. }
  2343. }
  2344. $modelpath = "core/modules/supplier_proposal/doc/";
  2345. return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  2346. }
  2347. /**
  2348. * Function used to replace a thirdparty id with another one.
  2349. *
  2350. * @param DoliDB $db Database handler
  2351. * @param int $origin_id Old thirdparty id
  2352. * @param int $dest_id New thirdparty id
  2353. * @return bool
  2354. */
  2355. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  2356. {
  2357. $tables = array(
  2358. 'supplier_proposal'
  2359. );
  2360. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  2361. }
  2362. }
  2363. /**
  2364. * Class to manage supplier_proposal lines
  2365. */
  2366. class SupplierProposalLine extends CommonObjectLine
  2367. {
  2368. /**
  2369. * @var DoliDB Database handler.
  2370. */
  2371. public $db;
  2372. /**
  2373. * @var string Error code (or message)
  2374. */
  2375. public $error = '';
  2376. /**
  2377. * @var string ID to identify managed object
  2378. */
  2379. public $element = 'supplier_proposaldet';
  2380. /**
  2381. * @var string Name of table without prefix where object is stored
  2382. */
  2383. public $table_element = 'supplier_proposaldet';
  2384. public $oldline;
  2385. /**
  2386. * @var int ID
  2387. */
  2388. public $id;
  2389. /**
  2390. * @var int ID
  2391. */
  2392. public $fk_supplier_proposal;
  2393. /**
  2394. * @var int ID
  2395. */
  2396. public $fk_parent_line;
  2397. public $desc; // Description ligne
  2398. /**
  2399. * @var int ID
  2400. */
  2401. public $fk_product; // Id produit predefini
  2402. /**
  2403. * @deprecated
  2404. * @see $product_type
  2405. */
  2406. public $fk_product_type;
  2407. /**
  2408. * Product type
  2409. * @var int
  2410. * @see Product::TYPE_PRODUCT, Product::TYPE_SERVICE
  2411. */
  2412. public $product_type = Product::TYPE_PRODUCT;
  2413. public $qty;
  2414. public $tva_tx;
  2415. public $vat_src_code;
  2416. public $subprice;
  2417. public $remise_percent;
  2418. /**
  2419. * @var int ID
  2420. */
  2421. public $fk_remise_except;
  2422. public $rang = 0;
  2423. /**
  2424. * @var int ID
  2425. */
  2426. public $fk_fournprice;
  2427. public $pa_ht;
  2428. public $marge_tx;
  2429. public $marque_tx;
  2430. public $special_code; // Tag for special lines (exlusive tags)
  2431. // 1: frais de port
  2432. // 2: ecotaxe
  2433. // 3: option line (when qty = 0)
  2434. public $info_bits = 0; // Liste d'options cumulables:
  2435. // Bit 0: 0 si TVA normal - 1 si TVA NPR
  2436. // Bit 1: 0 ligne normale - 1 si ligne de remise fixe
  2437. public $total_ht; // Total HT de la ligne toute quantite et incluant la remise ligne
  2438. public $total_tva; // Total TVA de la ligne toute quantite et incluant la remise ligne
  2439. public $total_ttc; // Total TTC de la ligne toute quantite et incluant la remise ligne
  2440. public $date_start;
  2441. public $date_end;
  2442. // From llx_product
  2443. /**
  2444. * @deprecated
  2445. * @see $product_ref
  2446. */
  2447. public $ref;
  2448. /**
  2449. * Product reference
  2450. * @var string
  2451. */
  2452. public $product_ref;
  2453. /**
  2454. * @deprecated
  2455. * @see $product_label
  2456. */
  2457. public $libelle;
  2458. /**
  2459. * Product label
  2460. * @var string
  2461. */
  2462. public $product_label;
  2463. /**
  2464. * Product description
  2465. * @var string
  2466. */
  2467. public $product_desc;
  2468. public $localtax1_tx; // Local tax 1
  2469. public $localtax2_tx; // Local tax 2
  2470. public $localtax1_type; // Local tax 1 type
  2471. public $localtax2_type; // Local tax 2 type
  2472. public $total_localtax1; // Line total local tax 1
  2473. public $total_localtax2; // Line total local tax 2
  2474. public $skip_update_total; // Skip update price total for special lines
  2475. public $ref_fourn;
  2476. public $ref_supplier;
  2477. // Multicurrency
  2478. /**
  2479. * @var int ID
  2480. */
  2481. public $fk_multicurrency;
  2482. public $multicurrency_code;
  2483. public $multicurrency_subprice;
  2484. public $multicurrency_total_ht;
  2485. public $multicurrency_total_tva;
  2486. public $multicurrency_total_ttc;
  2487. /**
  2488. * Class line Contructor
  2489. *
  2490. * @param DoliDB $db Database handler
  2491. */
  2492. public function __construct($db)
  2493. {
  2494. $this->db = $db;
  2495. }
  2496. /**
  2497. * Retrieve the propal line object
  2498. *
  2499. * @param int $rowid Propal line id
  2500. * @return int <0 if KO, >0 if OK
  2501. */
  2502. public function fetch($rowid)
  2503. {
  2504. $sql = 'SELECT pd.rowid, pd.fk_supplier_proposal, pd.fk_parent_line, pd.fk_product, pd.label as custom_label, pd.description, pd.price, pd.qty, pd.tva_tx,';
  2505. $sql .= ' pd.date_start, pd.date_end,';
  2506. $sql .= ' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice,';
  2507. $sql .= ' pd.info_bits, pd.total_ht, pd.total_tva, pd.total_ttc, pd.fk_product_fournisseur_price as fk_fournprice, pd.buy_price_ht as pa_ht, pd.special_code, pd.rang,';
  2508. $sql .= ' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,';
  2509. $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
  2510. $sql .= ' pd.product_type, pd.ref_fourn as ref_produit_fourn,';
  2511. $sql .= ' pd.fk_multicurrency, pd.multicurrency_code, pd.multicurrency_subprice, pd.multicurrency_total_ht, pd.multicurrency_total_tva, pd.multicurrency_total_ttc, pd.fk_unit';
  2512. $sql .= ' FROM '.MAIN_DB_PREFIX.'supplier_proposaldet as pd';
  2513. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pd.fk_product = p.rowid';
  2514. $sql .= ' WHERE pd.rowid = '.((int) $rowid);
  2515. $result = $this->db->query($sql);
  2516. if ($result) {
  2517. $objp = $this->db->fetch_object($result);
  2518. $this->id = $objp->rowid;
  2519. $this->fk_supplier_proposal = $objp->fk_supplier_proposal;
  2520. $this->fk_parent_line = $objp->fk_parent_line;
  2521. $this->label = $objp->custom_label;
  2522. $this->desc = $objp->description;
  2523. $this->qty = $objp->qty;
  2524. $this->subprice = $objp->subprice;
  2525. $this->tva_tx = $objp->tva_tx;
  2526. $this->remise_percent = $objp->remise_percent;
  2527. $this->fk_remise_except = $objp->fk_remise_except;
  2528. $this->fk_product = $objp->fk_product;
  2529. $this->info_bits = $objp->info_bits;
  2530. $this->date_start = $this->db->jdate($objp->date_start);
  2531. $this->date_end = $this->db->jdate($objp->date_end);
  2532. $this->total_ht = $objp->total_ht;
  2533. $this->total_tva = $objp->total_tva;
  2534. $this->total_ttc = $objp->total_ttc;
  2535. $this->fk_fournprice = $objp->fk_fournprice;
  2536. $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
  2537. $this->pa_ht = $marginInfos[0];
  2538. $this->marge_tx = $marginInfos[1];
  2539. $this->marque_tx = $marginInfos[2];
  2540. $this->special_code = $objp->special_code;
  2541. $this->product_type = $objp->product_type;
  2542. $this->rang = $objp->rang;
  2543. $this->ref = $objp->product_ref; // deprecated
  2544. $this->product_ref = $objp->product_ref;
  2545. $this->libelle = $objp->product_label; // deprecated
  2546. $this->product_label = $objp->product_label;
  2547. $this->product_desc = $objp->product_desc;
  2548. $this->ref_fourn = $objp->ref_produit_forun;
  2549. // Multicurrency
  2550. $this->fk_multicurrency = $objp->fk_multicurrency;
  2551. $this->multicurrency_code = $objp->multicurrency_code;
  2552. $this->multicurrency_subprice = $objp->multicurrency_subprice;
  2553. $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
  2554. $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
  2555. $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
  2556. $this->fk_unit = $objp->fk_unit;
  2557. $this->db->free($result);
  2558. return 1;
  2559. } else {
  2560. dol_print_error($this->db);
  2561. return -1;
  2562. }
  2563. }
  2564. /**
  2565. * Insert object line propal in database
  2566. *
  2567. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  2568. * @return int <0 if KO, >0 if OK
  2569. */
  2570. public function insert($notrigger = 0)
  2571. {
  2572. global $conf, $langs, $user;
  2573. $error = 0;
  2574. dol_syslog(get_class($this)."::insert rang=".$this->rang);
  2575. // Clean parameters
  2576. if (empty($this->tva_tx)) {
  2577. $this->tva_tx = 0;
  2578. }
  2579. if (empty($this->localtax1_tx)) {
  2580. $this->localtax1_tx = 0;
  2581. }
  2582. if (empty($this->localtax2_tx)) {
  2583. $this->localtax2_tx = 0;
  2584. }
  2585. if (empty($this->localtax1_type)) {
  2586. $this->localtax1_type = 0;
  2587. }
  2588. if (empty($this->localtax2_type)) {
  2589. $this->localtax2_type = 0;
  2590. }
  2591. if (empty($this->total_localtax1)) {
  2592. $this->total_localtax1 = 0;
  2593. }
  2594. if (empty($this->total_localtax2)) {
  2595. $this->total_localtax2 = 0;
  2596. }
  2597. if (empty($this->rang)) {
  2598. $this->rang = 0;
  2599. }
  2600. if (empty($this->remise)) {
  2601. $this->remise = 0;
  2602. }
  2603. if (empty($this->remise_percent)) {
  2604. $this->remise_percent = 0;
  2605. }
  2606. if (empty($this->info_bits)) {
  2607. $this->info_bits = 0;
  2608. }
  2609. if (empty($this->special_code)) {
  2610. $this->special_code = 0;
  2611. }
  2612. if (empty($this->fk_parent_line)) {
  2613. $this->fk_parent_line = 0;
  2614. }
  2615. if (empty($this->fk_fournprice)) {
  2616. $this->fk_fournprice = 0;
  2617. }
  2618. if (empty($this->fk_unit)) {
  2619. $this->fk_unit = 0;
  2620. }
  2621. if (empty($this->subprice)) {
  2622. $this->subprice = 0;
  2623. }
  2624. if (empty($this->pa_ht)) {
  2625. $this->pa_ht = 0;
  2626. }
  2627. // if buy price not defined, define buyprice as configured in margin admin
  2628. if ($this->pa_ht == 0) {
  2629. $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
  2630. if ($result < 0) {
  2631. return $result;
  2632. } else {
  2633. $this->pa_ht = $result;
  2634. }
  2635. }
  2636. // Check parameters
  2637. if ($this->product_type < 0) {
  2638. return -1;
  2639. }
  2640. $this->db->begin();
  2641. // Insert line into database
  2642. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'supplier_proposaldet';
  2643. $sql .= ' (fk_supplier_proposal, fk_parent_line, label, description, fk_product, product_type,';
  2644. $sql .= ' date_start, date_end,';
  2645. $sql .= ' fk_remise_except, qty, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
  2646. $sql .= ' subprice, remise_percent, ';
  2647. $sql .= ' info_bits, ';
  2648. $sql .= ' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,';
  2649. $sql .= ' ref_fourn,';
  2650. $sql .= ' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc, fk_unit)';
  2651. $sql .= " VALUES (".$this->fk_supplier_proposal.",";
  2652. $sql .= " ".($this->fk_parent_line > 0 ? ((int) $this->db->escape($this->fk_parent_line)) : "null").",";
  2653. $sql .= " ".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
  2654. $sql .= " '".$this->db->escape($this->desc)."',";
  2655. $sql .= " ".($this->fk_product ? ((int) $this->fk_product) : "null").",";
  2656. $sql .= " '".$this->db->escape($this->product_type)."',";
  2657. $sql .= " ".($this->date_start ? "'".$this->db->idate($this->date_start)."'" : "null").",";
  2658. $sql .= " ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null").",";
  2659. $sql .= " ".($this->fk_remise_except ? ((int) $this->db->escape($this->fk_remise_except)) : "null").",";
  2660. $sql .= " ".price2num($this->qty, 'MS').",";
  2661. $sql .= " ".price2num($this->tva_tx).",";
  2662. $sql .= " ".price2num($this->localtax1_tx).",";
  2663. $sql .= " ".price2num($this->localtax2_tx).",";
  2664. $sql .= " '".$this->db->escape($this->localtax1_type)."',";
  2665. $sql .= " '".$this->db->escape($this->localtax2_type)."',";
  2666. $sql .= " ".(!empty($this->subprice) ? price2num($this->subprice, 'MU') : "null").",";
  2667. $sql .= " ".((float) $this->remise_percent).",";
  2668. $sql .= " ".(isset($this->info_bits) ? ((int) $this->info_bits) : "null").",";
  2669. $sql .= " ".price2num($this->total_ht, 'MT').",";
  2670. $sql .= " ".price2num($this->total_tva, 'MT').",";
  2671. $sql .= " ".price2num($this->total_localtax1, 'MT').",";
  2672. $sql .= " ".price2num($this->total_localtax2, 'MT').",";
  2673. $sql .= " ".price2num($this->total_ttc, 'MT').",";
  2674. $sql .= " ".(!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) : "null").",";
  2675. $sql .= " ".(isset($this->pa_ht) ? price2num($this->pa_ht, 'MU') : "null").",";
  2676. $sql .= ' '.((int) $this->special_code).',';
  2677. $sql .= ' '.((int) $this->rang).',';
  2678. $sql .= " '".$this->db->escape($this->ref_fourn)."'";
  2679. $sql .= ", ".($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) : 'null');
  2680. $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
  2681. $sql .= ", ".price2num($this->multicurrency_subprice, 'CU');
  2682. $sql .= ", ".price2num($this->multicurrency_total_ht, 'CT');
  2683. $sql .= ", ".price2num($this->multicurrency_total_tva, 'CT');
  2684. $sql .= ", ".price2num($this->multicurrency_total_ttc, 'CT');
  2685. $sql .= ", ".($this->fk_unit ? ((int) $this->fk_unit) : 'null');
  2686. $sql .= ')';
  2687. dol_syslog(get_class($this).'::insert', LOG_DEBUG);
  2688. $resql = $this->db->query($sql);
  2689. if ($resql) {
  2690. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'supplier_proposaldet');
  2691. if (!$error) {
  2692. $result = $this->insertExtraFields();
  2693. if ($result < 0) {
  2694. $error++;
  2695. }
  2696. }
  2697. if (!$error && !$notrigger) {
  2698. // Call trigger
  2699. $result = $this->call_trigger('LINESUPPLIER_PROPOSAL_INSERT', $user);
  2700. if ($result < 0) {
  2701. $this->db->rollback();
  2702. return -1;
  2703. }
  2704. // End call triggers
  2705. }
  2706. $this->db->commit();
  2707. return 1;
  2708. } else {
  2709. $this->error = $this->db->error()." sql=".$sql;
  2710. $this->db->rollback();
  2711. return -1;
  2712. }
  2713. }
  2714. /**
  2715. * Delete line in database
  2716. *
  2717. * @return int <0 if ko, >0 if ok
  2718. */
  2719. public function delete()
  2720. {
  2721. global $conf, $langs, $user;
  2722. $error = 0;
  2723. $this->db->begin();
  2724. $sql = "DELETE FROM ".MAIN_DB_PREFIX."supplier_proposaldet WHERE rowid = ".((int) $this->id);
  2725. dol_syslog("SupplierProposalLine::delete", LOG_DEBUG);
  2726. if ($this->db->query($sql)) {
  2727. // Remove extrafields
  2728. if (!$error) {
  2729. $result = $this->deleteExtraFields();
  2730. if ($result < 0) {
  2731. $error++;
  2732. dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
  2733. }
  2734. }
  2735. // Call trigger
  2736. $result = $this->call_trigger('LINESUPPLIER_PROPOSAL_DELETE', $user);
  2737. if ($result < 0) {
  2738. $this->db->rollback();
  2739. return -1;
  2740. }
  2741. // End call triggers
  2742. $this->db->commit();
  2743. return 1;
  2744. } else {
  2745. $this->error = $this->db->error()." sql=".$sql;
  2746. $this->db->rollback();
  2747. return -1;
  2748. }
  2749. }
  2750. /**
  2751. * Update propal line object into DB
  2752. *
  2753. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  2754. * @return int <0 if ko, >0 if ok
  2755. */
  2756. public function update($notrigger = 0)
  2757. {
  2758. global $conf, $langs, $user;
  2759. $error = 0;
  2760. // Clean parameters
  2761. if (empty($this->tva_tx)) {
  2762. $this->tva_tx = 0;
  2763. }
  2764. if (empty($this->localtax1_tx)) {
  2765. $this->localtax1_tx = 0;
  2766. }
  2767. if (empty($this->localtax2_tx)) {
  2768. $this->localtax2_tx = 0;
  2769. }
  2770. if (empty($this->total_localtax1)) {
  2771. $this->total_localtax1 = 0;
  2772. }
  2773. if (empty($this->total_localtax2)) {
  2774. $this->total_localtax2 = 0;
  2775. }
  2776. if (empty($this->localtax1_type)) {
  2777. $this->localtax1_type = 0;
  2778. }
  2779. if (empty($this->localtax2_type)) {
  2780. $this->localtax2_type = 0;
  2781. }
  2782. if (empty($this->marque_tx)) {
  2783. $this->marque_tx = 0;
  2784. }
  2785. if (empty($this->marge_tx)) {
  2786. $this->marge_tx = 0;
  2787. }
  2788. if (empty($this->remise_percent)) {
  2789. $this->remise_percent = 0;
  2790. }
  2791. if (empty($this->info_bits)) {
  2792. $this->info_bits = 0;
  2793. }
  2794. if (empty($this->special_code)) {
  2795. $this->special_code = 0;
  2796. }
  2797. if (empty($this->fk_parent_line)) {
  2798. $this->fk_parent_line = 0;
  2799. }
  2800. if (empty($this->fk_fournprice)) {
  2801. $this->fk_fournprice = 0;
  2802. }
  2803. if (empty($this->fk_unit)) {
  2804. $this->fk_unit = 0;
  2805. }
  2806. if (empty($this->subprice)) {
  2807. $this->subprice = 0;
  2808. }
  2809. if (empty($this->pa_ht)) {
  2810. $this->pa_ht = 0;
  2811. }
  2812. // if buy price not defined, define buyprice as configured in margin admin
  2813. if ($this->pa_ht == 0) {
  2814. $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
  2815. if ($result < 0) {
  2816. return $result;
  2817. } else {
  2818. $this->pa_ht = $result;
  2819. }
  2820. }
  2821. $this->db->begin();
  2822. // Mise a jour ligne en base
  2823. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposaldet SET";
  2824. $sql .= " description='".$this->db->escape($this->desc)."'";
  2825. $sql .= " , label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
  2826. $sql .= " , product_type=".((int) $this->product_type);
  2827. $sql .= " , date_start=".($this->date_start ? "'".$this->db->idate($this->date_start)."'" : "null");
  2828. $sql .= " , date_end=".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
  2829. $sql .= " , tva_tx='".price2num($this->tva_tx)."'";
  2830. $sql .= " , localtax1_tx=".price2num($this->localtax1_tx);
  2831. $sql .= " , localtax2_tx=".price2num($this->localtax2_tx);
  2832. $sql .= " , localtax1_type='".$this->db->escape($this->localtax1_type)."'";
  2833. $sql .= " , localtax2_type='".$this->db->escape($this->localtax2_type)."'";
  2834. $sql .= " , qty='".price2num($this->qty)."'";
  2835. $sql .= " , subprice=".price2num($this->subprice)."";
  2836. $sql .= " , remise_percent=".price2num($this->remise_percent)."";
  2837. $sql .= " , info_bits='".$this->db->escape($this->info_bits)."'";
  2838. if (empty($this->skip_update_total)) {
  2839. $sql .= " , total_ht=".price2num($this->total_ht)."";
  2840. $sql .= " , total_tva=".price2num($this->total_tva)."";
  2841. $sql .= " , total_ttc=".price2num($this->total_ttc)."";
  2842. $sql .= " , total_localtax1=".price2num($this->total_localtax1)."";
  2843. $sql .= " , total_localtax2=".price2num($this->total_localtax2)."";
  2844. }
  2845. $sql .= " , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? "'".$this->db->escape($this->fk_fournprice)."'" : "null");
  2846. $sql .= " , buy_price_ht=".price2num($this->pa_ht);
  2847. if (strlen($this->special_code)) {
  2848. $sql .= " , special_code=".((int) $this->special_code);
  2849. }
  2850. $sql .= " , fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line : "null");
  2851. if (!empty($this->rang)) {
  2852. $sql .= ", rang=".((int) $this->rang);
  2853. }
  2854. $sql .= " , ref_fourn=".(!empty($this->ref_fourn) ? "'".$this->db->escape($this->ref_fourn)."'" : "null");
  2855. $sql .= " , fk_unit=".($this->fk_unit ? $this->fk_unit : 'null');
  2856. // Multicurrency
  2857. $sql .= " , multicurrency_subprice=".price2num($this->multicurrency_subprice)."";
  2858. $sql .= " , multicurrency_total_ht=".price2num($this->multicurrency_total_ht)."";
  2859. $sql .= " , multicurrency_total_tva=".price2num($this->multicurrency_total_tva)."";
  2860. $sql .= " , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc)."";
  2861. $sql .= " WHERE rowid = ".((int) $this->id);
  2862. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  2863. $resql = $this->db->query($sql);
  2864. if ($resql) {
  2865. if (!$error) {
  2866. $result = $this->insertExtraFields();
  2867. if ($result < 0) {
  2868. $error++;
  2869. }
  2870. }
  2871. if (!$error && !$notrigger) {
  2872. // Call trigger
  2873. $result = $this->call_trigger('LINESUPPLIER_PROPOSAL_UPDATE', $user);
  2874. if ($result < 0) {
  2875. $this->db->rollback();
  2876. return -1;
  2877. }
  2878. // End call triggers
  2879. }
  2880. $this->db->commit();
  2881. return 1;
  2882. } else {
  2883. $this->error = $this->db->error();
  2884. $this->db->rollback();
  2885. return -2;
  2886. }
  2887. }
  2888. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2889. /**
  2890. * Update DB line fields total_xxx
  2891. * Used by migration
  2892. *
  2893. * @return int <0 if ko, >0 if ok
  2894. */
  2895. public function update_total()
  2896. {
  2897. // phpcs:enable
  2898. $this->db->begin();
  2899. // Mise a jour ligne en base
  2900. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposaldet SET";
  2901. $sql .= " total_ht=".price2num($this->total_ht, 'MT');
  2902. $sql .= ",total_tva=".price2num($this->total_tva, 'MT');
  2903. $sql .= ",total_ttc=".price2num($this->total_ttc, 'MT');
  2904. $sql .= " WHERE rowid = ".((int) $this->id);
  2905. dol_syslog("SupplierProposalLine::update_total", LOG_DEBUG);
  2906. $resql = $this->db->query($sql);
  2907. if ($resql) {
  2908. $this->db->commit();
  2909. return 1;
  2910. } else {
  2911. $this->error = $this->db->error();
  2912. $this->db->rollback();
  2913. return -2;
  2914. }
  2915. }
  2916. }