supplier_proposal.class.php 108 KB

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