supplier_proposal.class.php 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352
  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. $error++;
  1026. }
  1027. }
  1028. }
  1029. unset($this->context['createfromclone']);
  1030. // End
  1031. if (!$error) {
  1032. $this->db->commit();
  1033. return $this->id;
  1034. } else {
  1035. $this->db->rollback();
  1036. return -1;
  1037. }
  1038. }
  1039. /**
  1040. * Load a proposal from database and its ligne array
  1041. *
  1042. * @param int $rowid id of object to load
  1043. * @param string $ref Ref of proposal
  1044. * @return int >0 if OK, <0 if KO
  1045. */
  1046. public function fetch($rowid, $ref = '')
  1047. {
  1048. global $conf;
  1049. $sql = "SELECT p.rowid, p.entity, p.ref, p.remise, p.remise_percent, p.remise_absolue, p.fk_soc";
  1050. $sql .= ", p.total_ttc, p.total_tva, p.localtax1, p.localtax2, p.total_ht";
  1051. $sql .= ", p.datec";
  1052. $sql .= ", p.date_valid as datev";
  1053. $sql .= ", p.date_livraison as delivery_date";
  1054. $sql .= ", p.model_pdf, p.extraparams";
  1055. $sql .= ", p.note_private, p.note_public";
  1056. $sql .= ", p.fk_projet as fk_project, p.fk_statut";
  1057. $sql .= ", p.fk_user_author, p.fk_user_valid, p.fk_user_cloture";
  1058. $sql .= ", p.fk_cond_reglement";
  1059. $sql .= ", p.fk_mode_reglement";
  1060. $sql .= ', p.fk_account';
  1061. $sql .= ", p.fk_shipping_method";
  1062. $sql .= ", p.fk_multicurrency, p.multicurrency_code, p.multicurrency_tx, p.multicurrency_total_ht, p.multicurrency_total_tva, p.multicurrency_total_ttc";
  1063. $sql .= ", c.label as statut_label";
  1064. $sql .= ", cr.code as cond_reglement_code, cr.libelle as cond_reglement, cr.libelle_facture as cond_reglement_libelle_doc";
  1065. $sql .= ", cp.code as mode_reglement_code, cp.libelle as mode_reglement";
  1066. $sql .= " FROM ".MAIN_DB_PREFIX."c_propalst as c, ".MAIN_DB_PREFIX."supplier_proposal as p";
  1067. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as cp ON p.fk_mode_reglement = cp.id';
  1068. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as cr ON p.fk_cond_reglement = cr.rowid';
  1069. $sql .= " WHERE p.fk_statut = c.id";
  1070. $sql .= " AND p.entity IN (".getEntity('supplier_proposal').")";
  1071. if ($ref) {
  1072. $sql .= " AND p.ref = '".$this->db->escape($ref)."'";
  1073. } else {
  1074. $sql .= " AND p.rowid = ".((int) $rowid);
  1075. }
  1076. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  1077. $resql = $this->db->query($sql);
  1078. if ($resql) {
  1079. if ($this->db->num_rows($resql)) {
  1080. $obj = $this->db->fetch_object($resql);
  1081. $this->id = $obj->rowid;
  1082. $this->entity = $obj->entity;
  1083. $this->ref = $obj->ref;
  1084. $this->remise = $obj->remise;
  1085. $this->remise_percent = $obj->remise_percent;
  1086. $this->remise_absolue = $obj->remise_absolue;
  1087. $this->total_ht = $obj->total_ht;
  1088. $this->total_tva = $obj->total_tva;
  1089. $this->total_localtax1 = $obj->localtax1;
  1090. $this->total_localtax2 = $obj->localtax2;
  1091. $this->total_ttc = $obj->total_ttc;
  1092. $this->socid = $obj->fk_soc;
  1093. $this->fk_project = $obj->fk_project;
  1094. $this->model_pdf = $obj->model_pdf;
  1095. $this->modelpdf = $obj->model_pdf; // deprecated
  1096. $this->note = $obj->note_private; // TODO deprecated
  1097. $this->note_private = $obj->note_private;
  1098. $this->note_public = $obj->note_public;
  1099. $this->statut = (int) $obj->fk_statut;
  1100. $this->statut_libelle = $obj->statut_label;
  1101. $this->datec = $this->db->jdate($obj->datec); // TODO deprecated
  1102. $this->datev = $this->db->jdate($obj->datev); // TODO deprecated
  1103. $this->date_creation = $this->db->jdate($obj->datec); //Creation date
  1104. $this->date_validation = $this->db->jdate($obj->datev); //Validation date
  1105. $this->date_livraison = $this->db->jdate($obj->delivery_date); // deprecated
  1106. $this->delivery_date = $this->db->jdate($obj->delivery_date);
  1107. $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method : null;
  1108. $this->mode_reglement_id = $obj->fk_mode_reglement;
  1109. $this->mode_reglement_code = $obj->mode_reglement_code;
  1110. $this->mode_reglement = $obj->mode_reglement;
  1111. $this->fk_account = ($obj->fk_account > 0) ? $obj->fk_account : null;
  1112. $this->cond_reglement_id = $obj->fk_cond_reglement;
  1113. $this->cond_reglement_code = $obj->cond_reglement_code;
  1114. $this->cond_reglement = $obj->cond_reglement;
  1115. $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
  1116. $this->extraparams = (array) json_decode($obj->extraparams, true);
  1117. $this->user_author_id = $obj->fk_user_author;
  1118. $this->user_valid_id = $obj->fk_user_valid;
  1119. $this->user_close_id = $obj->fk_user_cloture;
  1120. // Multicurrency
  1121. $this->fk_multicurrency = $obj->fk_multicurrency;
  1122. $this->multicurrency_code = $obj->multicurrency_code;
  1123. $this->multicurrency_tx = $obj->multicurrency_tx;
  1124. $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
  1125. $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
  1126. $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
  1127. if ($obj->fk_statut == 0) {
  1128. $this->brouillon = 1;
  1129. }
  1130. // Retrieve all extrafield
  1131. // fetch optionals attributes and labels
  1132. $this->fetch_optionals();
  1133. $this->db->free($resql);
  1134. $this->lines = array();
  1135. // Lines of supplier proposals
  1136. $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,";
  1137. $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,";
  1138. $sql .= ' p.ref as product_ref, p.description as product_desc, p.fk_product_type, p.label as product_label,';
  1139. $sql .= ' d.ref_fourn as ref_produit_fourn,';
  1140. $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';
  1141. $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposaldet as d";
  1142. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid";
  1143. $sql .= " WHERE d.fk_supplier_proposal = ".((int) $this->id);
  1144. $sql .= " ORDER by d.rang";
  1145. $result = $this->db->query($sql);
  1146. if ($result) {
  1147. $num = $this->db->num_rows($result);
  1148. $i = 0;
  1149. while ($i < $num) {
  1150. $objp = $this->db->fetch_object($result);
  1151. $line = new SupplierProposalLine($this->db);
  1152. $line->rowid = $objp->rowid; // deprecated
  1153. $line->id = $objp->rowid;
  1154. $line->fk_supplier_proposal = $objp->fk_supplier_proposal;
  1155. $line->fk_parent_line = $objp->fk_parent_line;
  1156. $line->product_type = $objp->product_type;
  1157. $line->label = $objp->custom_label;
  1158. $line->desc = $objp->description; // Description ligne
  1159. $line->qty = $objp->qty;
  1160. $line->tva_tx = $objp->tva_tx;
  1161. $line->localtax1_tx = $objp->localtax1_tx;
  1162. $line->localtax2_tx = $objp->localtax2_tx;
  1163. $line->subprice = $objp->subprice;
  1164. $line->fk_remise_except = $objp->fk_remise_except;
  1165. $line->remise_percent = $objp->remise_percent;
  1166. $line->info_bits = $objp->info_bits;
  1167. $line->total_ht = $objp->total_ht;
  1168. $line->total_tva = $objp->total_tva;
  1169. $line->total_localtax1 = $objp->total_localtax1;
  1170. $line->total_localtax2 = $objp->total_localtax2;
  1171. $line->total_ttc = $objp->total_ttc;
  1172. $line->fk_fournprice = $objp->fk_fournprice;
  1173. $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
  1174. $line->pa_ht = $marginInfos[0];
  1175. $line->marge_tx = $marginInfos[1];
  1176. $line->marque_tx = $marginInfos[2];
  1177. $line->special_code = $objp->special_code;
  1178. $line->rang = $objp->rang;
  1179. $line->fk_product = $objp->fk_product;
  1180. $line->ref = $objp->product_ref; // deprecated
  1181. $line->product_ref = $objp->product_ref;
  1182. $line->libelle = $objp->product_label; // deprecated
  1183. $line->product_label = $objp->product_label;
  1184. $line->product_desc = $objp->product_desc; // Description produit
  1185. $line->fk_product_type = $objp->fk_product_type;
  1186. $line->ref_fourn = $objp->ref_produit_fourn;
  1187. // Multicurrency
  1188. $line->fk_multicurrency = $objp->fk_multicurrency;
  1189. $line->multicurrency_code = $objp->multicurrency_code;
  1190. $line->multicurrency_subprice = $objp->multicurrency_subprice;
  1191. $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
  1192. $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
  1193. $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
  1194. $line->fk_unit = $objp->fk_unit;
  1195. $this->lines[$i] = $line;
  1196. $i++;
  1197. }
  1198. $this->db->free($result);
  1199. } else {
  1200. $this->error = $this->db->error();
  1201. return -1;
  1202. }
  1203. // Retrieve all extrafield
  1204. // fetch optionals attributes and labels
  1205. $this->fetch_optionals();
  1206. return 1;
  1207. }
  1208. $this->error = "Record Not Found";
  1209. return 0;
  1210. } else {
  1211. $this->error = $this->db->error();
  1212. return -1;
  1213. }
  1214. }
  1215. /**
  1216. * Set status to validated
  1217. *
  1218. * @param User $user Object user that validate
  1219. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1220. * @return int <0 if KO, >=0 if OK
  1221. */
  1222. public function valid($user, $notrigger = 0)
  1223. {
  1224. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1225. global $conf, $langs;
  1226. $error = 0;
  1227. $now = dol_now();
  1228. if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->supplier_proposal->creer))
  1229. || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->supplier_proposal->validate_advance))) {
  1230. $this->db->begin();
  1231. // Numbering module definition
  1232. $soc = new Societe($this->db);
  1233. $result = $soc->fetch($this->socid);
  1234. if ($result < 0) {
  1235. return -1;
  1236. }
  1237. // Define new ref
  1238. if (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref)) { // empty should not happened, but when it occurs, the test save life
  1239. $num = $this->getNextNumRef($soc);
  1240. } else {
  1241. $num = $this->ref;
  1242. }
  1243. $this->newref = dol_sanitizeFileName($num);
  1244. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal";
  1245. $sql .= " SET ref = '".$this->db->escape($num)."',";
  1246. $sql .= " fk_statut = 1, date_valid='".$this->db->idate($now)."', fk_user_valid=".((int) $user->id);
  1247. $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = 0";
  1248. dol_syslog(get_class($this)."::valid", LOG_DEBUG);
  1249. $resql = $this->db->query($sql);
  1250. if (!$resql) {
  1251. dol_print_error($this->db);
  1252. $error++;
  1253. }
  1254. // Trigger calls
  1255. if (!$error && !$notrigger) {
  1256. // Call trigger
  1257. $result = $this->call_trigger('PROPOSAL_SUPPLIER_VALIDATE', $user);
  1258. if ($result < 0) {
  1259. $error++;
  1260. }
  1261. // End call triggers
  1262. }
  1263. if (!$error) {
  1264. $this->oldref = $this->ref;
  1265. // Rename directory if dir was a temporary ref
  1266. if (preg_match('/^[\(]?PROV/i', $this->ref)) {
  1267. // Now we rename also files into index
  1268. $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)."'";
  1269. $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'supplier_proposal/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
  1270. $resql = $this->db->query($sql);
  1271. if (!$resql) {
  1272. $error++; $this->error = $this->db->lasterror();
  1273. }
  1274. // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
  1275. $oldref = dol_sanitizeFileName($this->ref);
  1276. $newref = dol_sanitizeFileName($num);
  1277. $dirsource = $conf->supplier_proposal->dir_output.'/'.$oldref;
  1278. $dirdest = $conf->supplier_proposal->dir_output.'/'.$newref;
  1279. if (!$error && file_exists($dirsource)) {
  1280. dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
  1281. if (@rename($dirsource, $dirdest)) {
  1282. dol_syslog("Rename ok");
  1283. // Rename docs starting with $oldref with $newref
  1284. $listoffiles = dol_dir_list($conf->supplier_proposal->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
  1285. foreach ($listoffiles as $fileentry) {
  1286. $dirsource = $fileentry['name'];
  1287. $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
  1288. $dirsource = $fileentry['path'].'/'.$dirsource;
  1289. $dirdest = $fileentry['path'].'/'.$dirdest;
  1290. @rename($dirsource, $dirdest);
  1291. }
  1292. }
  1293. }
  1294. }
  1295. $this->ref = $num;
  1296. $this->brouillon = 0;
  1297. $this->statut = 1;
  1298. $this->user_valid_id = $user->id;
  1299. $this->datev = $now;
  1300. $this->db->commit();
  1301. return 1;
  1302. } else {
  1303. $this->db->rollback();
  1304. return -1;
  1305. }
  1306. } else {
  1307. dol_syslog("You don't have permission to validate supplier proposal", LOG_WARNING);
  1308. return -2;
  1309. }
  1310. }
  1311. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1312. /**
  1313. * Set delivery date
  1314. *
  1315. * @param User $user Object user that modify
  1316. * @param int $delivery_date Delivery date
  1317. * @return int <0 if ko, >0 if ok
  1318. * @deprecated Use setDeliveryDate
  1319. */
  1320. public function set_date_livraison($user, $delivery_date)
  1321. {
  1322. // phpcs:enable
  1323. return $this->setDeliveryDate($user, $delivery_date);
  1324. }
  1325. /**
  1326. * Set delivery date
  1327. *
  1328. * @param User $user Object user that modify
  1329. * @param int $delivery_date Delivery date
  1330. * @return int <0 if ko, >0 if ok
  1331. */
  1332. public function setDeliveryDate($user, $delivery_date)
  1333. {
  1334. if (!empty($user->rights->supplier_proposal->creer)) {
  1335. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal ";
  1336. $sql .= " SET date_livraison = ".($delivery_date != '' ? "'".$this->db->idate($delivery_date)."'" : 'null');
  1337. $sql .= " WHERE rowid = ".((int) $this->id);
  1338. if ($this->db->query($sql)) {
  1339. $this->date_livraison = $delivery_date;
  1340. $this->delivery_date = $delivery_date;
  1341. return 1;
  1342. } else {
  1343. $this->error = $this->db->error();
  1344. dol_syslog(get_class($this)."::setDeliveryDate Erreur SQL");
  1345. return -1;
  1346. }
  1347. }
  1348. return 0;
  1349. }
  1350. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1351. /**
  1352. * Set an overall discount on the proposal
  1353. *
  1354. * @param User $user Object user that modify
  1355. * @param double $remise Amount discount
  1356. * @return int <0 if ko, >0 if ok
  1357. */
  1358. public function set_remise_percent($user, $remise)
  1359. {
  1360. // phpcs:enable
  1361. $remise = trim($remise) ?trim($remise) : 0;
  1362. if (!empty($user->rights->supplier_proposal->creer)) {
  1363. $remise = price2num($remise, 2);
  1364. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal SET remise_percent = ".((float) $remise);
  1365. $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = 0";
  1366. if ($this->db->query($sql)) {
  1367. $this->remise_percent = ((float) $remise);
  1368. $this->update_price(1);
  1369. return 1;
  1370. } else {
  1371. $this->error = $this->db->error();
  1372. return -1;
  1373. }
  1374. }
  1375. return 0;
  1376. }
  1377. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1378. /**
  1379. * Set an absolute overall discount on the proposal
  1380. *
  1381. * @param User $user Object user that modify
  1382. * @param double $remise Amount discount
  1383. * @return int <0 if ko, >0 if ok
  1384. */
  1385. public function set_remise_absolue($user, $remise)
  1386. {
  1387. // phpcs:enable
  1388. if (empty($remise)) {
  1389. $remise = 0;
  1390. }
  1391. $remise = price2num($remise);
  1392. if (!empty($user->rights->supplier_proposal->creer)) {
  1393. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal ";
  1394. $sql .= " SET remise_absolue = ".((float) $remise);
  1395. $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = 0";
  1396. if ($this->db->query($sql)) {
  1397. $this->remise_absolue = $remise;
  1398. $this->update_price(1);
  1399. return 1;
  1400. } else {
  1401. $this->error = $this->db->error();
  1402. return -1;
  1403. }
  1404. }
  1405. return 0;
  1406. }
  1407. /**
  1408. * Reopen the commercial proposal
  1409. *
  1410. * @param User $user Object user that close
  1411. * @param int $statut Statut
  1412. * @param string $note Comment
  1413. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1414. * @return int <0 if KO, >0 if OK
  1415. */
  1416. public function reopen($user, $statut, $note = '', $notrigger = 0)
  1417. {
  1418. global $langs, $conf;
  1419. $this->statut = $statut;
  1420. $error = 0;
  1421. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal";
  1422. $sql .= " SET fk_statut = ".((int) $this->statut).",";
  1423. if (!empty($note)) {
  1424. $sql .= " note_private = '".$this->db->escape($note)."',";
  1425. }
  1426. $sql .= " date_cloture=NULL, fk_user_cloture=NULL";
  1427. $sql .= " WHERE rowid = ".((int) $this->id);
  1428. $this->db->begin();
  1429. dol_syslog(get_class($this)."::reopen", LOG_DEBUG);
  1430. $resql = $this->db->query($sql);
  1431. if (!$resql) {
  1432. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  1433. }
  1434. if (!$error) {
  1435. if (!$notrigger) {
  1436. // Call trigger
  1437. $result = $this->call_trigger('PROPOSAL_SUPPLIER_REOPEN', $user);
  1438. if ($result < 0) {
  1439. $error++;
  1440. }
  1441. // End call triggers
  1442. }
  1443. }
  1444. // Commit or rollback
  1445. if ($error) {
  1446. if (!empty($this->errors)) {
  1447. foreach ($this->errors as $errmsg) {
  1448. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  1449. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  1450. }
  1451. }
  1452. $this->db->rollback();
  1453. return -1 * $error;
  1454. } else {
  1455. $this->db->commit();
  1456. return 1;
  1457. }
  1458. }
  1459. /**
  1460. * Close the askprice
  1461. *
  1462. * @param User $user Object user that close
  1463. * @param int $status Status
  1464. * @param string $note Comment
  1465. * @return int <0 if KO, >0 if OK
  1466. */
  1467. public function cloture($user, $status, $note)
  1468. {
  1469. global $langs, $conf;
  1470. $hidedetails = 0;
  1471. $hidedesc = 0;
  1472. $hideref = 0;
  1473. $this->statut = $status;
  1474. $error = 0;
  1475. $now = dol_now();
  1476. $this->db->begin();
  1477. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal";
  1478. $sql .= " SET fk_statut = ".((int) $status).", note_private = '".$this->db->escape($note)."', date_cloture='".$this->db->idate($now)."', fk_user_cloture=".$user->id;
  1479. $sql .= " WHERE rowid = ".((int) $this->id);
  1480. $resql = $this->db->query($sql);
  1481. if ($resql) {
  1482. $modelpdf = $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF_ODT_CLOSED ? $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF_ODT_CLOSED : (empty($this->model_pdf) ? '' : $this->model_pdf);
  1483. $triggerName = 'PROPOSAL_SUPPLIER_CLOSE_REFUSED';
  1484. if ($status == 2) {
  1485. $triggerName = 'PROPOSAL_SUPPLIER_CLOSE_SIGNED';
  1486. $modelpdf = $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF_ODT_TOBILL ? $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF_ODT_TOBILL : (empty($this->model_pdf) ? '' : $this->model_pdf);
  1487. 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
  1488. $result = $this->updateOrCreatePriceFournisseur($user);
  1489. }
  1490. }
  1491. if ($status == 4) {
  1492. $triggerName = 'PROPOSAL_SUPPLIER_CLASSIFY_BILLED';
  1493. }
  1494. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  1495. // Define output language
  1496. $outputlangs = $langs;
  1497. if (!empty($conf->global->MAIN_MULTILANGS)) {
  1498. $outputlangs = new Translate("", $conf);
  1499. $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $this->thirdparty->default_lang);
  1500. $outputlangs->setDefaultLang($newlang);
  1501. }
  1502. //$ret=$object->fetch($id); // Reload to get new records
  1503. $this->generateDocument($modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  1504. }
  1505. // Call trigger
  1506. $result = $this->call_trigger($triggerName, $user);
  1507. if ($result < 0) {
  1508. $error++;
  1509. }
  1510. // End call triggers
  1511. if (!$error) {
  1512. $this->db->commit();
  1513. return 1;
  1514. } else {
  1515. $this->db->rollback();
  1516. return -1;
  1517. }
  1518. } else {
  1519. $this->error = $this->db->lasterror();
  1520. $this->errors[] = $this->db->lasterror();
  1521. $this->db->rollback();
  1522. return -1;
  1523. }
  1524. }
  1525. /**
  1526. * Add or update supplier price according to result of proposal
  1527. *
  1528. * @param User $user Object user
  1529. * @return int > 0 if OK
  1530. */
  1531. public function updateOrCreatePriceFournisseur($user)
  1532. {
  1533. global $conf;
  1534. dol_syslog(get_class($this)."::updateOrCreatePriceFournisseur", LOG_DEBUG);
  1535. foreach ($this->lines as $product) {
  1536. if ($product->subprice <= 0) {
  1537. continue;
  1538. }
  1539. $productsupplier = new ProductFournisseur($this->db);
  1540. $multicurrency_tx = 1;
  1541. $fk_multicurrency = 0;
  1542. if (empty($this->thirdparty)) {
  1543. $this->fetch_thirdparty();
  1544. }
  1545. $ref_fourn = $product->ref_fourn;
  1546. if (empty($ref_fourn)) {
  1547. $ref_fourn = $product->ref_supplier;
  1548. }
  1549. if (!empty($conf->multicurrency->enabled) && !empty($product->multicurrency_code)) {
  1550. list($fk_multicurrency, $multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $product->multicurrency_code);
  1551. }
  1552. $productsupplier->id = $product->fk_product;
  1553. $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, '', '', '');
  1554. }
  1555. return 1;
  1556. }
  1557. /**
  1558. * Upate ProductFournisseur
  1559. *
  1560. * @param int $idProductFournPrice id of llx_product_fournisseur_price
  1561. * @param Product $product contain informations to update
  1562. * @param User $user Object user
  1563. * @return int <0 if KO, >0 if OK
  1564. */
  1565. public function updatePriceFournisseur($idProductFournPrice, $product, $user)
  1566. {
  1567. $price = price2num($product->subprice * $product->qty, 'MU');
  1568. $unitPrice = price2num($product->subprice, 'MU');
  1569. $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);
  1570. $resql = $this->db->query($sql);
  1571. if (!$resql) {
  1572. $this->error = $this->db->error();
  1573. $this->db->rollback();
  1574. return -1;
  1575. }
  1576. return 1;
  1577. }
  1578. /**
  1579. * Create ProductFournisseur
  1580. *
  1581. * @param Product $product Object Product
  1582. * @param User $user Object user
  1583. * @return int <0 if KO, >0 if OK
  1584. */
  1585. public function createPriceFournisseur($product, $user)
  1586. {
  1587. global $conf;
  1588. $price = price2num($product->subprice * $product->qty, 'MU');
  1589. $qty = price2num($product->qty);
  1590. $unitPrice = price2num($product->subprice, 'MU');
  1591. $now = dol_now();
  1592. $values = array(
  1593. "'".$this->db->idate($now)."'",
  1594. $product->fk_product,
  1595. $this->thirdparty->id,
  1596. "'".$product->ref_fourn."'",
  1597. $price,
  1598. $qty,
  1599. $unitPrice,
  1600. $product->tva_tx,
  1601. $user->id
  1602. );
  1603. if (!empty($conf->multicurrency->enabled)) {
  1604. if (!empty($product->multicurrency_code)) {
  1605. include_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
  1606. $multicurrency = new MultiCurrency($this->db); //need to fetch because empty fk_multicurrency and rate
  1607. $multicurrency->fetch(0, $product->multicurrency_code);
  1608. if (!empty($multicurrency->id)) {
  1609. $values[] = $multicurrency->id;
  1610. $values[] = "'".$product->multicurrency_code."'";
  1611. $values[] = $product->multicurrency_subprice;
  1612. $values[] = $product->multicurrency_total_ht;
  1613. $values[] = $multicurrency->rate->rate;
  1614. } else {
  1615. for ($i = 0; $i < 5; $i++) {
  1616. $values[] = 'NULL';
  1617. }
  1618. }
  1619. }
  1620. }
  1621. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'product_fournisseur_price ';
  1622. $sql .= '(datec, fk_product, fk_soc, ref_fourn, price, quantity, unitprice, tva_tx, fk_user';
  1623. if (!empty($conf->multicurrency->enabled) && !empty($product->multicurrency_code)) {
  1624. $sql .= ',fk_multicurrency, multicurrency_code, multicurrency_unitprice, multicurrency_price, multicurrency_tx';
  1625. }
  1626. $sql .= ') VALUES ('.implode(',', $values).')';
  1627. $resql = $this->db->query($sql);
  1628. if (!$resql) {
  1629. $this->error = $this->db->error();
  1630. $this->db->rollback();
  1631. return -1;
  1632. }
  1633. return 1;
  1634. }
  1635. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1636. /**
  1637. * Set draft status
  1638. *
  1639. * @param User $user Object user that modify
  1640. * @return int <0 if KO, >0 if OK
  1641. */
  1642. public function setDraft($user)
  1643. {
  1644. // phpcs:enable
  1645. global $conf, $langs;
  1646. $error = 0;
  1647. if ($this->statut == self::STATUS_DRAFT) {
  1648. dol_syslog(get_class($this)."::setDraft already draft status", LOG_WARNING);
  1649. return 0;
  1650. }
  1651. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposal";
  1652. $sql .= " SET fk_statut = ".self::STATUS_DRAFT;
  1653. $sql .= " WHERE rowid = ".((int) $this->id);
  1654. if ($this->db->query($sql)) {
  1655. if (!$error) {
  1656. $this->oldcopy = clone $this;
  1657. }
  1658. if (!$error) {
  1659. // Call trigger
  1660. $result = $this->call_trigger('PROPOSAL_SUPPLIER_UNVALIDATE', $user);
  1661. if ($result < 0) {
  1662. $error++;
  1663. }
  1664. }
  1665. if (!$error) {
  1666. $this->statut = self::STATUS_DRAFT;
  1667. $this->brouillon = 1;
  1668. $this->db->commit();
  1669. return 1;
  1670. } else {
  1671. $this->db->rollback();
  1672. return -1;
  1673. }
  1674. } else {
  1675. return -1;
  1676. }
  1677. }
  1678. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1679. /**
  1680. * Return list of askprice (eventually filtered on user) into an array
  1681. *
  1682. * @param int $shortlist 0=Return array[id]=ref, 1=Return array[](id=>id,ref=>ref,name=>name)
  1683. * @param int $draft 0=not draft, 1=draft
  1684. * @param int $notcurrentuser 0=all user, 1=not current user
  1685. * @param int $socid Id third pary
  1686. * @param int $limit For pagination
  1687. * @param int $offset For pagination
  1688. * @param string $sortfield Sort criteria
  1689. * @param string $sortorder Sort order
  1690. * @return int -1 if KO, array with result if OK
  1691. */
  1692. public function liste_array($shortlist = 0, $draft = 0, $notcurrentuser = 0, $socid = 0, $limit = 0, $offset = 0, $sortfield = 'p.datec', $sortorder = 'DESC')
  1693. {
  1694. // phpcs:enable
  1695. global $conf, $user;
  1696. $ga = array();
  1697. $sql = "SELECT s.rowid, s.nom as name, s.client,";
  1698. $sql .= " p.rowid as supplier_proposalid, p.fk_statut, p.total_ht, p.ref, p.remise, ";
  1699. $sql .= " p.datep as dp, p.fin_validite as datelimite";
  1700. if (empty($user->rights->societe->client->voir) && !$socid) {
  1701. $sql .= ", sc.fk_soc, sc.fk_user";
  1702. }
  1703. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."supplier_proposal as p, ".MAIN_DB_PREFIX."c_propalst as c";
  1704. if (empty($user->rights->societe->client->voir) && !$socid) {
  1705. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  1706. }
  1707. $sql .= " WHERE p.entity IN (".getEntity('supplier_proposal').")";
  1708. $sql .= " AND p.fk_soc = s.rowid";
  1709. $sql .= " AND p.fk_statut = c.id";
  1710. if (empty($user->rights->societe->client->voir) && !$socid) { //restriction
  1711. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  1712. }
  1713. if ($socid) {
  1714. $sql .= " AND s.rowid = ".((int) $socid);
  1715. }
  1716. if ($draft) {
  1717. $sql .= " AND p.fk_statut = 0";
  1718. }
  1719. if ($notcurrentuser > 0) {
  1720. $sql .= " AND p.fk_user_author <> ".$user->id;
  1721. }
  1722. $sql .= $this->db->order($sortfield, $sortorder);
  1723. $sql .= $this->db->plimit($limit, $offset);
  1724. $result = $this->db->query($sql);
  1725. if ($result) {
  1726. $num = $this->db->num_rows($result);
  1727. if ($num) {
  1728. $i = 0;
  1729. while ($i < $num) {
  1730. $obj = $this->db->fetch_object($result);
  1731. if ($shortlist == 1) {
  1732. $ga[$obj->supplier_proposalid] = $obj->ref;
  1733. } elseif ($shortlist == 2) {
  1734. $ga[$obj->supplier_proposalid] = $obj->ref.' ('.$obj->name.')';
  1735. } else {
  1736. $ga[$i]['id'] = $obj->supplier_proposalid;
  1737. $ga[$i]['ref'] = $obj->ref;
  1738. $ga[$i]['name'] = $obj->name;
  1739. }
  1740. $i++;
  1741. }
  1742. }
  1743. return $ga;
  1744. } else {
  1745. dol_print_error($this->db);
  1746. return -1;
  1747. }
  1748. }
  1749. /**
  1750. * Delete askprice
  1751. *
  1752. * @param User $user Object user that delete
  1753. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1754. * @return int 1 if ok, otherwise if error
  1755. */
  1756. public function delete($user, $notrigger = 0)
  1757. {
  1758. global $conf, $langs;
  1759. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1760. $error = 0;
  1761. $this->db->begin();
  1762. if (!$notrigger) {
  1763. // Call trigger
  1764. $result = $this->call_trigger('PROPOSAL_SUPPLIER_DELETE', $user);
  1765. if ($result < 0) {
  1766. $error++;
  1767. }
  1768. // End call triggers
  1769. }
  1770. if (!$error) {
  1771. $main = MAIN_DB_PREFIX.'supplier_proposaldet';
  1772. $ef = $main."_extrafields";
  1773. $sqlef = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_supplier_proposal = ".((int) $this->id).")";
  1774. $sql = "DELETE FROM ".MAIN_DB_PREFIX."supplier_proposaldet WHERE fk_supplier_proposal = ".((int) $this->id);
  1775. if ($this->db->query($sql)) {
  1776. $sql = "DELETE FROM ".MAIN_DB_PREFIX."supplier_proposal WHERE rowid = ".((int) $this->id);
  1777. if ($this->db->query($sqlef) && $this->db->query($sql)) {
  1778. // Delete linked object
  1779. $res = $this->deleteObjectLinked();
  1780. if ($res < 0) {
  1781. $error++;
  1782. }
  1783. if (!$error) {
  1784. // Delete record into ECM index (Note that delete is also done when deleting files with the dol_delete_dir_recursive
  1785. $this->deleteEcmFiles();
  1786. // We remove directory
  1787. $ref = dol_sanitizeFileName($this->ref);
  1788. if ($conf->supplier_proposal->dir_output && !empty($this->ref)) {
  1789. $dir = $conf->supplier_proposal->dir_output."/".$ref;
  1790. $file = $dir."/".$ref.".pdf";
  1791. if (file_exists($file)) {
  1792. dol_delete_preview($this);
  1793. if (!dol_delete_file($file, 0, 0, 0, $this)) { // For triggers
  1794. $this->error = 'ErrorFailToDeleteFile';
  1795. $this->errors = array('ErrorFailToDeleteFile');
  1796. $this->db->rollback();
  1797. return 0;
  1798. }
  1799. }
  1800. if (file_exists($dir)) {
  1801. $res = @dol_delete_dir_recursive($dir);
  1802. if (!$res) {
  1803. $this->error = 'ErrorFailToDeleteDir';
  1804. $this->errors = array('ErrorFailToDeleteDir');
  1805. $this->db->rollback();
  1806. return 0;
  1807. }
  1808. }
  1809. }
  1810. }
  1811. // Removed extrafields
  1812. if (!$error) {
  1813. $result = $this->deleteExtraFields();
  1814. if ($result < 0) {
  1815. $error++;
  1816. $errorflag = -4;
  1817. dol_syslog(get_class($this)."::delete erreur ".$errorflag." ".$this->error, LOG_ERR);
  1818. }
  1819. }
  1820. if (!$error) {
  1821. dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG);
  1822. $this->db->commit();
  1823. return 1;
  1824. } else {
  1825. $this->error = $this->db->lasterror();
  1826. $this->db->rollback();
  1827. return 0;
  1828. }
  1829. } else {
  1830. $this->error = $this->db->lasterror();
  1831. $this->db->rollback();
  1832. return -3;
  1833. }
  1834. } else {
  1835. $this->error = $this->db->lasterror();
  1836. $this->db->rollback();
  1837. return -2;
  1838. }
  1839. } else {
  1840. $this->db->rollback();
  1841. return -1;
  1842. }
  1843. }
  1844. /**
  1845. * Object SupplierProposal Information
  1846. *
  1847. * @param int $id Proposal id
  1848. * @return void
  1849. */
  1850. public function info($id)
  1851. {
  1852. $sql = "SELECT c.rowid, ";
  1853. $sql .= " c.datec, c.date_valid as datev, c.date_cloture as dateo,";
  1854. $sql .= " c.fk_user_author, c.fk_user_valid, c.fk_user_cloture";
  1855. $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal as c";
  1856. $sql .= " WHERE c.rowid = ".((int) $id);
  1857. $result = $this->db->query($sql);
  1858. if ($result) {
  1859. if ($this->db->num_rows($result)) {
  1860. $obj = $this->db->fetch_object($result);
  1861. $this->id = $obj->rowid;
  1862. $this->date_creation = $this->db->jdate($obj->datec);
  1863. $this->date_validation = $this->db->jdate($obj->datev);
  1864. $this->date_cloture = $this->db->jdate($obj->dateo);
  1865. $cuser = new User($this->db);
  1866. $cuser->fetch($obj->fk_user_author);
  1867. $this->user_creation = $cuser;
  1868. if ($obj->fk_user_valid) {
  1869. $vuser = new User($this->db);
  1870. $vuser->fetch($obj->fk_user_valid);
  1871. $this->user_validation = $vuser;
  1872. }
  1873. if ($obj->fk_user_cloture) {
  1874. $cluser = new User($this->db);
  1875. $cluser->fetch($obj->fk_user_cloture);
  1876. $this->user_cloture = $cluser;
  1877. }
  1878. }
  1879. $this->db->free($result);
  1880. } else {
  1881. dol_print_error($this->db);
  1882. }
  1883. }
  1884. /**
  1885. * Return label of status of proposal (draft, validated, ...)
  1886. *
  1887. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  1888. * @return string Label
  1889. */
  1890. public function getLibStatut($mode = 0)
  1891. {
  1892. return $this->LibStatut((isset($this->statut) ? $this->statut : $this->status), $mode);
  1893. }
  1894. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1895. /**
  1896. * Return label of a status (draft, validated, ...)
  1897. *
  1898. * @param int $status Id status
  1899. * @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
  1900. * @return string Label
  1901. */
  1902. public function LibStatut($status, $mode = 1)
  1903. {
  1904. // phpcs:enable
  1905. // Init/load array of translation of status
  1906. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  1907. global $langs;
  1908. $langs->load("supplier_proposal");
  1909. $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv("SupplierProposalStatusDraft");
  1910. $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv("SupplierProposalStatusValidated");
  1911. $this->labelStatus[self::STATUS_SIGNED] = $langs->transnoentitiesnoconv("SupplierProposalStatusSigned");
  1912. $this->labelStatus[self::STATUS_NOTSIGNED] = $langs->transnoentitiesnoconv("SupplierProposalStatusNotSigned");
  1913. $this->labelStatus[self::STATUS_CLOSE] = $langs->transnoentitiesnoconv("SupplierProposalStatusClosed");
  1914. $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv("SupplierProposalStatusDraftShort");
  1915. $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv("Opened");
  1916. $this->labelStatusShort[self::STATUS_SIGNED] = $langs->transnoentitiesnoconv("SupplierProposalStatusSignedShort");
  1917. $this->labelStatusShort[self::STATUS_NOTSIGNED] = $langs->transnoentitiesnoconv("SupplierProposalStatusNotSignedShort");
  1918. $this->labelStatusShort[self::STATUS_CLOSE] = $langs->transnoentitiesnoconv("SupplierProposalStatusClosedShort");
  1919. }
  1920. $statusnew = '';
  1921. if ($status == self::STATUS_DRAFT) {
  1922. $statusnew = 'status0';
  1923. } elseif ($status == self::STATUS_VALIDATED) {
  1924. $statusnew = 'status1';
  1925. } elseif ($status == self::STATUS_SIGNED) {
  1926. $statusnew = 'status4';
  1927. } elseif ($status == self::STATUS_NOTSIGNED) {
  1928. $statusnew = 'status9';
  1929. } elseif ($status == self::STATUS_CLOSE) {
  1930. $statusnew = 'status6';
  1931. }
  1932. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusnew, $mode);
  1933. }
  1934. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1935. /**
  1936. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  1937. *
  1938. * @param User $user Object user
  1939. * @param int $mode "opened" for askprice to close, "signed" for proposal to invoice
  1940. * @return int <0 if KO, >0 if OK
  1941. */
  1942. public function load_board($user, $mode)
  1943. {
  1944. // phpcs:enable
  1945. global $conf, $user, $langs;
  1946. $now = dol_now();
  1947. $this->nbtodo = $this->nbtodolate = 0;
  1948. $clause = " WHERE";
  1949. $sql = "SELECT p.rowid, p.ref, p.datec as datec, p.date_cloture as datefin";
  1950. $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal as p";
  1951. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  1952. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc";
  1953. $sql .= " WHERE sc.fk_user = ".((int) $user->id);
  1954. $clause = " AND";
  1955. }
  1956. $sql .= $clause." p.entity IN (".getEntity('supplier_proposal').")";
  1957. if ($mode == 'opened') {
  1958. $sql .= " AND p.fk_statut = 1";
  1959. }
  1960. if ($mode == 'signed') {
  1961. $sql .= " AND p.fk_statut = 2";
  1962. }
  1963. if ($user->socid) {
  1964. $sql .= " AND p.fk_soc = ".((int) $user->socid);
  1965. }
  1966. $resql = $this->db->query($sql);
  1967. if ($resql) {
  1968. $label = $labelShort = '';
  1969. $status = '';
  1970. if ($mode == 'opened') {
  1971. $delay_warning = !empty($conf->supplier_proposal->cloture->warning_delay) ? $conf->supplier_proposal->cloture->warning_delay : 0;
  1972. $status = self::STATUS_VALIDATED;
  1973. $label = $langs->trans("SupplierProposalsToClose");
  1974. $labelShort = $langs->trans("ToAcceptRefuse");
  1975. }
  1976. if ($mode == 'signed') {
  1977. $delay_warning = !empty($conf->supplier_proposal->facturation->warning_delay) ? $conf->supplier_proposal->facturation->warning_delay : 0;
  1978. $status = self::STATUS_SIGNED;
  1979. $label = $langs->trans("SupplierProposalsToProcess"); // May be billed or ordered
  1980. $labelShort = $langs->trans("ToClose");
  1981. }
  1982. $response = new WorkboardResponse();
  1983. $response->warning_delay = $delay_warning / 60 / 60 / 24;
  1984. $response->label = $label;
  1985. $response->labelShort = $labelShort;
  1986. $response->url = DOL_URL_ROOT.'/supplier_proposal/list.php?search_status='.$status;
  1987. $response->img = img_object('', "propal");
  1988. // This assignment in condition is not a bug. It allows walking the results.
  1989. while ($obj = $this->db->fetch_object($resql)) {
  1990. $response->nbtodo++;
  1991. if ($mode == 'opened') {
  1992. $datelimit = $this->db->jdate($obj->datefin);
  1993. if ($datelimit < ($now - $delay_warning)) {
  1994. $response->nbtodolate++;
  1995. }
  1996. }
  1997. // TODO Definir regle des propales a facturer en retard
  1998. // if ($mode == 'signed' && ! count($this->FactureListeArray($obj->rowid))) $this->nbtodolate++;
  1999. }
  2000. return $response;
  2001. } else {
  2002. $this->error = $this->db->lasterror();
  2003. return -1;
  2004. }
  2005. }
  2006. /**
  2007. * Initialise an instance with random values.
  2008. * Used to build previews or test instances.
  2009. * id must be 0 if object instance is a specimen.
  2010. *
  2011. * @return void
  2012. */
  2013. public function initAsSpecimen()
  2014. {
  2015. global $user, $langs, $conf;
  2016. // Load array of products prodids
  2017. $num_prods = 0;
  2018. $prodids = array();
  2019. $sql = "SELECT rowid";
  2020. $sql .= " FROM ".MAIN_DB_PREFIX."product";
  2021. $sql .= " WHERE entity IN (".getEntity('product').")";
  2022. $sql .= $this->db->plimit(100);
  2023. $resql = $this->db->query($sql);
  2024. if ($resql) {
  2025. $num_prods = $this->db->num_rows($resql);
  2026. $i = 0;
  2027. while ($i < $num_prods) {
  2028. $i++;
  2029. $row = $this->db->fetch_row($resql);
  2030. $prodids[$i] = $row[0];
  2031. }
  2032. }
  2033. // Initialise parametres
  2034. $this->id = 0;
  2035. $this->ref = 'SPECIMEN';
  2036. $this->specimen = 1;
  2037. $this->socid = 1;
  2038. $this->date = time();
  2039. $this->cond_reglement_id = 1;
  2040. $this->cond_reglement_code = 'RECEP';
  2041. $this->mode_reglement_id = 7;
  2042. $this->mode_reglement_code = 'CHQ';
  2043. $this->note_public = 'This is a comment (public)';
  2044. $this->note_private = 'This is a comment (private)';
  2045. // Lines
  2046. $nbp = 5;
  2047. $xnbp = 0;
  2048. while ($xnbp < $nbp) {
  2049. $line = new SupplierProposalLine($this->db);
  2050. $line->desc = $langs->trans("Description")." ".$xnbp;
  2051. $line->qty = 1;
  2052. $line->subprice = 100;
  2053. $line->tva_tx = 19.6;
  2054. $line->localtax1_tx = 0;
  2055. $line->localtax2_tx = 0;
  2056. if ($xnbp == 2) {
  2057. $line->total_ht = 50;
  2058. $line->total_ttc = 59.8;
  2059. $line->total_tva = 9.8;
  2060. $line->remise_percent = 50;
  2061. } else {
  2062. $line->total_ht = 100;
  2063. $line->total_ttc = 119.6;
  2064. $line->total_tva = 19.6;
  2065. $line->remise_percent = 00;
  2066. }
  2067. if ($num_prods > 0) {
  2068. $prodid = mt_rand(1, $num_prods);
  2069. $line->fk_product = $prodids[$prodid];
  2070. }
  2071. $this->lines[$xnbp] = $line;
  2072. $this->total_ht += $line->total_ht;
  2073. $this->total_tva += $line->total_tva;
  2074. $this->total_ttc += $line->total_ttc;
  2075. $xnbp++;
  2076. }
  2077. }
  2078. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2079. /**
  2080. * Load indicator this->nb of global stats widget
  2081. *
  2082. * @return int <0 if ko, >0 if ok
  2083. */
  2084. public function load_state_board()
  2085. {
  2086. // phpcs:enable
  2087. global $conf, $user;
  2088. $this->nb = array();
  2089. $clause = "WHERE";
  2090. $sql = "SELECT count(p.rowid) as nb";
  2091. $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal as p";
  2092. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
  2093. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  2094. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
  2095. $sql .= " WHERE sc.fk_user = ".((int) $user->id);
  2096. $clause = "AND";
  2097. }
  2098. $sql .= " ".$clause." p.entity IN (".getEntity('supplier_proposal').")";
  2099. $resql = $this->db->query($sql);
  2100. if ($resql) {
  2101. // This assignment in condition is not a bug. It allows walking the results.
  2102. while ($obj = $this->db->fetch_object($resql)) {
  2103. $this->nb["supplier_proposals"] = $obj->nb;
  2104. }
  2105. $this->db->free($resql);
  2106. return 1;
  2107. } else {
  2108. dol_print_error($this->db);
  2109. $this->error = $this->db->lasterror();
  2110. return -1;
  2111. }
  2112. }
  2113. /**
  2114. * Returns the reference to the following non used Proposal used depending on the active numbering module
  2115. * defined into SUPPLIER_PROPOSAL_ADDON
  2116. *
  2117. * @param Societe $soc Object thirdparty
  2118. * @return string Reference libre pour la propale
  2119. */
  2120. public function getNextNumRef($soc)
  2121. {
  2122. global $conf, $db, $langs;
  2123. $langs->load("supplier_proposal");
  2124. if (!empty($conf->global->SUPPLIER_PROPOSAL_ADDON)) {
  2125. $mybool = false;
  2126. $file = $conf->global->SUPPLIER_PROPOSAL_ADDON.".php";
  2127. $classname = $conf->global->SUPPLIER_PROPOSAL_ADDON;
  2128. // Include file with class
  2129. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  2130. foreach ($dirmodels as $reldir) {
  2131. $dir = dol_buildpath($reldir."core/modules/supplier_proposal/");
  2132. // Load file with numbering class (if found)
  2133. $mybool |= @include_once $dir.$file;
  2134. }
  2135. if (!$mybool) {
  2136. dol_print_error('', "Failed to include file ".$file);
  2137. return '';
  2138. }
  2139. $obj = new $classname();
  2140. $numref = "";
  2141. $numref = $obj->getNextValue($soc, $this);
  2142. if ($numref != "") {
  2143. return $numref;
  2144. } else {
  2145. $this->error = $obj->error;
  2146. return "";
  2147. }
  2148. } else {
  2149. $langs->load("errors");
  2150. print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("SupplierProposal"));
  2151. return "";
  2152. }
  2153. }
  2154. /**
  2155. * Return clicable link of object (with eventually picto)
  2156. *
  2157. * @param int $withpicto Add picto into link
  2158. * @param string $option Where point the link ('compta', 'expedition', 'document', ...)
  2159. * @param string $get_params Parametres added to url
  2160. * @param int $notooltip 1=Disable tooltip
  2161. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  2162. * @param int $addlinktonotes Add link to show notes
  2163. * @return string String with URL
  2164. */
  2165. public function getNomUrl($withpicto = 0, $option = '', $get_params = '', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0)
  2166. {
  2167. global $langs, $conf, $user, $hookmanager;
  2168. if (!empty($conf->dol_no_mouse_hover)) {
  2169. $notooltip = 1; // Force disable tooltips
  2170. }
  2171. $url = '';
  2172. $result = '';
  2173. $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("SupplierProposal").'</u>';
  2174. if (isset($this->status)) {
  2175. $label .= ' '.$this->getLibStatut(5);
  2176. }
  2177. if (!empty($this->ref)) {
  2178. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  2179. }
  2180. if (!empty($this->ref_fourn)) {
  2181. $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_fourn;
  2182. }
  2183. if (!empty($this->total_ht)) {
  2184. $label .= '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
  2185. }
  2186. if (!empty($this->total_tva)) {
  2187. $label .= '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
  2188. }
  2189. if (!empty($this->total_ttc)) {
  2190. $label .= '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
  2191. }
  2192. if ($option == '') {
  2193. $url = DOL_URL_ROOT.'/supplier_proposal/card.php?id='.$this->id.$get_params;
  2194. }
  2195. if ($option == 'document') {
  2196. $url = DOL_URL_ROOT.'/supplier_proposal/document.php?id='.$this->id.$get_params;
  2197. }
  2198. if ($option !== 'nolink') {
  2199. // Add param to save lastsearch_values or not
  2200. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  2201. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  2202. $add_save_lastsearch_values = 1;
  2203. }
  2204. if ($add_save_lastsearch_values) {
  2205. $url .= '&save_lastsearch_values=1';
  2206. }
  2207. }
  2208. $linkclose = '';
  2209. if (empty($notooltip) && $user->rights->propal->lire) {
  2210. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  2211. $label = $langs->trans("ShowSupplierProposal");
  2212. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  2213. }
  2214. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  2215. $linkclose .= ' class="classfortooltip"';
  2216. }
  2217. $linkstart = '<a href="'.$url.'"';
  2218. $linkstart .= $linkclose.'>';
  2219. $linkend = '</a>';
  2220. $result .= $linkstart;
  2221. if ($withpicto) {
  2222. $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  2223. }
  2224. if ($withpicto != 2) {
  2225. $result .= $this->ref;
  2226. }
  2227. $result .= $linkend;
  2228. if ($addlinktonotes) {
  2229. $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
  2230. if ($txttoshow) {
  2231. $notetoshow = $langs->trans("ViewPrivateNote").':<br>'.dol_string_nohtmltag($txttoshow, 1);
  2232. $result .= ' <span class="note inline-block">';
  2233. $result .= '<a href="'.DOL_URL_ROOT.'/supplier_proposal/note.php?id='.$this->id.'" class="classfortooltip" title="'.dol_escape_htmltag($notetoshow).'">';
  2234. $result .= img_picto('', 'note');
  2235. $result .= '</a>';
  2236. //$result.=img_picto($langs->trans("ViewNote"),'object_generic');
  2237. //$result.='</a>';
  2238. $result .= '</span>';
  2239. }
  2240. }
  2241. global $action;
  2242. $hookmanager->initHooks(array($this->element . 'dao'));
  2243. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  2244. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  2245. if ($reshook > 0) {
  2246. $result = $hookmanager->resPrint;
  2247. } else {
  2248. $result .= $hookmanager->resPrint;
  2249. }
  2250. return $result;
  2251. }
  2252. /**
  2253. * Retrieve an array of supplier proposal lines
  2254. *
  2255. * @return int >0 if OK, <0 if KO
  2256. */
  2257. public function getLinesArray()
  2258. {
  2259. // For other object, here we call fetch_lines. But fetch_lines does not exists on supplier proposal
  2260. $sql = 'SELECT pt.rowid, pt.label as custom_label, pt.description, pt.fk_product, pt.fk_remise_except,';
  2261. $sql .= ' pt.qty, pt.tva_tx, pt.remise_percent, pt.subprice, pt.info_bits,';
  2262. $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,';
  2263. $sql .= ' pt.product_type, pt.rang, pt.fk_parent_line,';
  2264. $sql .= ' p.label as product_label, p.ref, p.fk_product_type, p.rowid as prodid,';
  2265. $sql .= ' p.description as product_desc, pt.ref_fourn as ref_supplier,';
  2266. $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';
  2267. $sql .= ' FROM '.MAIN_DB_PREFIX.'supplier_proposaldet as pt';
  2268. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product=p.rowid';
  2269. $sql .= ' WHERE pt.fk_supplier_proposal = '.((int) $this->id);
  2270. $sql .= ' ORDER BY pt.rang ASC, pt.rowid';
  2271. dol_syslog(get_class($this).'::getLinesArray', LOG_DEBUG);
  2272. $resql = $this->db->query($sql);
  2273. if ($resql) {
  2274. $num = $this->db->num_rows($resql);
  2275. $i = 0;
  2276. while ($i < $num) {
  2277. $obj = $this->db->fetch_object($resql);
  2278. $this->lines[$i] = new SupplierProposalLine($this->db);
  2279. $this->lines[$i]->id = $obj->rowid; // for backward compatibility
  2280. $this->lines[$i]->rowid = $obj->rowid;
  2281. $this->lines[$i]->label = $obj->custom_label;
  2282. $this->lines[$i]->description = $obj->description;
  2283. $this->lines[$i]->fk_product = $obj->fk_product;
  2284. $this->lines[$i]->ref = $obj->ref;
  2285. $this->lines[$i]->product_label = $obj->product_label;
  2286. $this->lines[$i]->product_desc = $obj->product_desc;
  2287. $this->lines[$i]->fk_product_type = $obj->fk_product_type; // deprecated
  2288. $this->lines[$i]->product_type = $obj->product_type;
  2289. $this->lines[$i]->qty = $obj->qty;
  2290. $this->lines[$i]->subprice = $obj->subprice;
  2291. $this->lines[$i]->fk_remise_except = $obj->fk_remise_except;
  2292. $this->lines[$i]->remise_percent = $obj->remise_percent;
  2293. $this->lines[$i]->tva_tx = $obj->tva_tx;
  2294. $this->lines[$i]->info_bits = $obj->info_bits;
  2295. $this->lines[$i]->total_ht = $obj->total_ht;
  2296. $this->lines[$i]->total_tva = $obj->total_tva;
  2297. $this->lines[$i]->total_ttc = $obj->total_ttc;
  2298. $this->lines[$i]->fk_fournprice = $obj->fk_fournprice;
  2299. $marginInfos = getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->lines[$i]->fk_fournprice, $obj->pa_ht);
  2300. $this->lines[$i]->pa_ht = $marginInfos[0];
  2301. $this->lines[$i]->marge_tx = $marginInfos[1];
  2302. $this->lines[$i]->marque_tx = $marginInfos[2];
  2303. $this->lines[$i]->fk_parent_line = $obj->fk_parent_line;
  2304. $this->lines[$i]->special_code = $obj->special_code;
  2305. $this->lines[$i]->rang = $obj->rang;
  2306. $this->lines[$i]->ref_fourn = $obj->ref_supplier; // deprecated
  2307. $this->lines[$i]->ref_supplier = $obj->ref_supplier;
  2308. // Multicurrency
  2309. $this->lines[$i]->fk_multicurrency = $obj->fk_multicurrency;
  2310. $this->lines[$i]->multicurrency_code = $obj->multicurrency_code;
  2311. $this->lines[$i]->multicurrency_subprice = $obj->multicurrency_subprice;
  2312. $this->lines[$i]->multicurrency_total_ht = $obj->multicurrency_total_ht;
  2313. $this->lines[$i]->multicurrency_total_tva = $obj->multicurrency_total_tva;
  2314. $this->lines[$i]->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
  2315. $this->lines[$i]->fk_unit = $obj->fk_unit;
  2316. $i++;
  2317. }
  2318. $this->db->free($resql);
  2319. return 1;
  2320. } else {
  2321. $this->error = $this->db->error();
  2322. return -1;
  2323. }
  2324. }
  2325. /**
  2326. * Create a document onto disk according to template module.
  2327. *
  2328. * @param string $modele Force model to use ('' to not force)
  2329. * @param Translate $outputlangs Object langs to use for output
  2330. * @param int $hidedetails Hide details of lines
  2331. * @param int $hidedesc Hide description
  2332. * @param int $hideref Hide ref
  2333. * @param null|array $moreparams Array to provide more information
  2334. * @return int 0 if KO, 1 if OK
  2335. */
  2336. public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
  2337. {
  2338. global $conf, $langs;
  2339. $langs->load("supplier_proposal");
  2340. $outputlangs->load("products");
  2341. if (!dol_strlen($modele)) {
  2342. $modele = 'aurore';
  2343. if ($this->model_pdf) {
  2344. $modele = $this->model_pdf;
  2345. } elseif (!empty($conf->global->SUPPLIER_PROPOSAL_ADDON_PDF)) {
  2346. $modele = $conf->global->SUPPLIER_PROPOSAL_ADDON_PDF;
  2347. }
  2348. }
  2349. $modelpath = "core/modules/supplier_proposal/doc/";
  2350. return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  2351. }
  2352. /**
  2353. * Function used to replace a thirdparty id with another one.
  2354. *
  2355. * @param DoliDB $db Database handler
  2356. * @param int $origin_id Old thirdparty id
  2357. * @param int $dest_id New thirdparty id
  2358. * @return bool
  2359. */
  2360. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  2361. {
  2362. $tables = array(
  2363. 'supplier_proposal'
  2364. );
  2365. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  2366. }
  2367. /**
  2368. * Function used to replace a product id with another one.
  2369. *
  2370. * @param DoliDB $db Database handler
  2371. * @param int $origin_id Old product id
  2372. * @param int $dest_id New product id
  2373. * @return bool
  2374. */
  2375. public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
  2376. {
  2377. $tables = array(
  2378. 'supplier_proposaldet'
  2379. );
  2380. return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
  2381. }
  2382. }
  2383. /**
  2384. * Class to manage supplier_proposal lines
  2385. */
  2386. class SupplierProposalLine extends CommonObjectLine
  2387. {
  2388. /**
  2389. * @var DoliDB Database handler.
  2390. */
  2391. public $db;
  2392. /**
  2393. * @var string Error code (or message)
  2394. */
  2395. public $error = '';
  2396. /**
  2397. * @var string ID to identify managed object
  2398. */
  2399. public $element = 'supplier_proposaldet';
  2400. /**
  2401. * @var string Name of table without prefix where object is stored
  2402. */
  2403. public $table_element = 'supplier_proposaldet';
  2404. public $oldline;
  2405. /**
  2406. * @var int ID
  2407. */
  2408. public $id;
  2409. /**
  2410. * @var int ID
  2411. */
  2412. public $fk_supplier_proposal;
  2413. /**
  2414. * @var int ID
  2415. */
  2416. public $fk_parent_line;
  2417. public $desc; // Description ligne
  2418. /**
  2419. * @var int ID
  2420. */
  2421. public $fk_product; // Id produit predefini
  2422. /**
  2423. * @deprecated
  2424. * @see $product_type
  2425. */
  2426. public $fk_product_type;
  2427. /**
  2428. * Product type
  2429. * @var int
  2430. * @see Product::TYPE_PRODUCT, Product::TYPE_SERVICE
  2431. */
  2432. public $product_type = Product::TYPE_PRODUCT;
  2433. public $qty;
  2434. public $tva_tx;
  2435. public $vat_src_code;
  2436. public $subprice;
  2437. public $remise_percent;
  2438. /**
  2439. * @var int ID
  2440. */
  2441. public $fk_remise_except;
  2442. public $rang = 0;
  2443. /**
  2444. * @var int ID
  2445. */
  2446. public $fk_fournprice;
  2447. public $pa_ht;
  2448. public $marge_tx;
  2449. public $marque_tx;
  2450. public $special_code; // Tag for special lines (exlusive tags)
  2451. // 1: frais de port
  2452. // 2: ecotaxe
  2453. // 3: option line (when qty = 0)
  2454. public $info_bits = 0; // Liste d'options cumulables:
  2455. // Bit 0: 0 si TVA normal - 1 si TVA NPR
  2456. // Bit 1: 0 ligne normale - 1 si ligne de remise fixe
  2457. public $total_ht; // Total HT de la ligne toute quantite et incluant la remise ligne
  2458. public $total_tva; // Total TVA de la ligne toute quantite et incluant la remise ligne
  2459. public $total_ttc; // Total TTC de la ligne toute quantite et incluant la remise ligne
  2460. public $date_start;
  2461. public $date_end;
  2462. // From llx_product
  2463. /**
  2464. * @deprecated
  2465. * @see $product_ref
  2466. */
  2467. public $ref;
  2468. /**
  2469. * Product reference
  2470. * @var string
  2471. */
  2472. public $product_ref;
  2473. /**
  2474. * @deprecated
  2475. * @see $product_label
  2476. */
  2477. public $libelle;
  2478. /**
  2479. * Product label
  2480. * @var string
  2481. */
  2482. public $product_label;
  2483. /**
  2484. * Product description
  2485. * @var string
  2486. */
  2487. public $product_desc;
  2488. public $localtax1_tx; // Local tax 1
  2489. public $localtax2_tx; // Local tax 2
  2490. public $localtax1_type; // Local tax 1 type
  2491. public $localtax2_type; // Local tax 2 type
  2492. public $total_localtax1; // Line total local tax 1
  2493. public $total_localtax2; // Line total local tax 2
  2494. public $skip_update_total; // Skip update price total for special lines
  2495. public $ref_fourn;
  2496. public $ref_supplier;
  2497. // Multicurrency
  2498. /**
  2499. * @var int ID
  2500. */
  2501. public $fk_multicurrency;
  2502. public $multicurrency_code;
  2503. public $multicurrency_subprice;
  2504. public $multicurrency_total_ht;
  2505. public $multicurrency_total_tva;
  2506. public $multicurrency_total_ttc;
  2507. /**
  2508. * Class line Contructor
  2509. *
  2510. * @param DoliDB $db Database handler
  2511. */
  2512. public function __construct($db)
  2513. {
  2514. $this->db = $db;
  2515. }
  2516. /**
  2517. * Retrieve the propal line object
  2518. *
  2519. * @param int $rowid Propal line id
  2520. * @return int <0 if KO, >0 if OK
  2521. */
  2522. public function fetch($rowid)
  2523. {
  2524. $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,';
  2525. $sql .= ' pd.date_start, pd.date_end,';
  2526. $sql .= ' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice,';
  2527. $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,';
  2528. $sql .= ' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,';
  2529. $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
  2530. $sql .= ' pd.product_type, pd.ref_fourn as ref_produit_fourn,';
  2531. $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';
  2532. $sql .= ' FROM '.MAIN_DB_PREFIX.'supplier_proposaldet as pd';
  2533. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pd.fk_product = p.rowid';
  2534. $sql .= ' WHERE pd.rowid = '.((int) $rowid);
  2535. $result = $this->db->query($sql);
  2536. if ($result) {
  2537. $objp = $this->db->fetch_object($result);
  2538. $this->id = $objp->rowid;
  2539. $this->fk_supplier_proposal = $objp->fk_supplier_proposal;
  2540. $this->fk_parent_line = $objp->fk_parent_line;
  2541. $this->label = $objp->custom_label;
  2542. $this->desc = $objp->description;
  2543. $this->qty = $objp->qty;
  2544. $this->subprice = $objp->subprice;
  2545. $this->tva_tx = $objp->tva_tx;
  2546. $this->remise_percent = $objp->remise_percent;
  2547. $this->fk_remise_except = $objp->fk_remise_except;
  2548. $this->fk_product = $objp->fk_product;
  2549. $this->info_bits = $objp->info_bits;
  2550. $this->date_start = $this->db->jdate($objp->date_start);
  2551. $this->date_end = $this->db->jdate($objp->date_end);
  2552. $this->total_ht = $objp->total_ht;
  2553. $this->total_tva = $objp->total_tva;
  2554. $this->total_ttc = $objp->total_ttc;
  2555. $this->fk_fournprice = $objp->fk_fournprice;
  2556. $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
  2557. $this->pa_ht = $marginInfos[0];
  2558. $this->marge_tx = $marginInfos[1];
  2559. $this->marque_tx = $marginInfos[2];
  2560. $this->special_code = $objp->special_code;
  2561. $this->product_type = $objp->product_type;
  2562. $this->rang = $objp->rang;
  2563. $this->ref = $objp->product_ref; // deprecated
  2564. $this->product_ref = $objp->product_ref;
  2565. $this->libelle = $objp->product_label; // deprecated
  2566. $this->product_label = $objp->product_label;
  2567. $this->product_desc = $objp->product_desc;
  2568. $this->ref_fourn = $objp->ref_produit_forun;
  2569. // Multicurrency
  2570. $this->fk_multicurrency = $objp->fk_multicurrency;
  2571. $this->multicurrency_code = $objp->multicurrency_code;
  2572. $this->multicurrency_subprice = $objp->multicurrency_subprice;
  2573. $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
  2574. $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
  2575. $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
  2576. $this->fk_unit = $objp->fk_unit;
  2577. $this->db->free($result);
  2578. return 1;
  2579. } else {
  2580. dol_print_error($this->db);
  2581. return -1;
  2582. }
  2583. }
  2584. /**
  2585. * Insert object line propal in database
  2586. *
  2587. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  2588. * @return int <0 if KO, >0 if OK
  2589. */
  2590. public function insert($notrigger = 0)
  2591. {
  2592. global $conf, $langs, $user;
  2593. $error = 0;
  2594. dol_syslog(get_class($this)."::insert rang=".$this->rang);
  2595. // Clean parameters
  2596. if (empty($this->tva_tx)) {
  2597. $this->tva_tx = 0;
  2598. }
  2599. if (empty($this->localtax1_tx)) {
  2600. $this->localtax1_tx = 0;
  2601. }
  2602. if (empty($this->localtax2_tx)) {
  2603. $this->localtax2_tx = 0;
  2604. }
  2605. if (empty($this->localtax1_type)) {
  2606. $this->localtax1_type = 0;
  2607. }
  2608. if (empty($this->localtax2_type)) {
  2609. $this->localtax2_type = 0;
  2610. }
  2611. if (empty($this->total_localtax1)) {
  2612. $this->total_localtax1 = 0;
  2613. }
  2614. if (empty($this->total_localtax2)) {
  2615. $this->total_localtax2 = 0;
  2616. }
  2617. if (empty($this->rang)) {
  2618. $this->rang = 0;
  2619. }
  2620. if (empty($this->remise_percent)) {
  2621. $this->remise_percent = 0;
  2622. }
  2623. if (empty($this->info_bits)) {
  2624. $this->info_bits = 0;
  2625. }
  2626. if (empty($this->special_code)) {
  2627. $this->special_code = 0;
  2628. }
  2629. if (empty($this->fk_parent_line)) {
  2630. $this->fk_parent_line = 0;
  2631. }
  2632. if (empty($this->fk_fournprice)) {
  2633. $this->fk_fournprice = 0;
  2634. }
  2635. if (empty($this->fk_unit)) {
  2636. $this->fk_unit = 0;
  2637. }
  2638. if (empty($this->subprice)) {
  2639. $this->subprice = 0;
  2640. }
  2641. if (empty($this->pa_ht)) {
  2642. $this->pa_ht = 0;
  2643. }
  2644. // if buy price not defined, define buyprice as configured in margin admin
  2645. if ($this->pa_ht == 0) {
  2646. $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
  2647. if ($result < 0) {
  2648. return $result;
  2649. } else {
  2650. $this->pa_ht = $result;
  2651. }
  2652. }
  2653. // Check parameters
  2654. if ($this->product_type < 0) {
  2655. return -1;
  2656. }
  2657. $this->db->begin();
  2658. // Insert line into database
  2659. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'supplier_proposaldet';
  2660. $sql .= ' (fk_supplier_proposal, fk_parent_line, label, description, fk_product, product_type,';
  2661. $sql .= ' date_start, date_end,';
  2662. $sql .= ' fk_remise_except, qty, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
  2663. $sql .= ' subprice, remise_percent, ';
  2664. $sql .= ' info_bits, ';
  2665. $sql .= ' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,';
  2666. $sql .= ' ref_fourn,';
  2667. $sql .= ' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc, fk_unit)';
  2668. $sql .= " VALUES (".$this->fk_supplier_proposal.",";
  2669. $sql .= " ".($this->fk_parent_line > 0 ? ((int) $this->db->escape($this->fk_parent_line)) : "null").",";
  2670. $sql .= " ".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
  2671. $sql .= " '".$this->db->escape($this->desc)."',";
  2672. $sql .= " ".($this->fk_product ? ((int) $this->fk_product) : "null").",";
  2673. $sql .= " '".$this->db->escape($this->product_type)."',";
  2674. $sql .= " ".($this->date_start ? "'".$this->db->idate($this->date_start)."'" : "null").",";
  2675. $sql .= " ".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null").",";
  2676. $sql .= " ".($this->fk_remise_except ? ((int) $this->db->escape($this->fk_remise_except)) : "null").",";
  2677. $sql .= " ".price2num($this->qty, 'MS').",";
  2678. $sql .= " ".price2num($this->tva_tx).",";
  2679. $sql .= " ".price2num($this->localtax1_tx).",";
  2680. $sql .= " ".price2num($this->localtax2_tx).",";
  2681. $sql .= " '".$this->db->escape($this->localtax1_type)."',";
  2682. $sql .= " '".$this->db->escape($this->localtax2_type)."',";
  2683. $sql .= " ".(!empty($this->subprice) ? price2num($this->subprice, 'MU') : "null").",";
  2684. $sql .= " ".((float) $this->remise_percent).",";
  2685. $sql .= " ".(isset($this->info_bits) ? ((int) $this->info_bits) : "null").",";
  2686. $sql .= " ".price2num($this->total_ht, 'MT').",";
  2687. $sql .= " ".price2num($this->total_tva, 'MT').",";
  2688. $sql .= " ".price2num($this->total_localtax1, 'MT').",";
  2689. $sql .= " ".price2num($this->total_localtax2, 'MT').",";
  2690. $sql .= " ".price2num($this->total_ttc, 'MT').",";
  2691. $sql .= " ".(!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) : "null").",";
  2692. $sql .= " ".(isset($this->pa_ht) ? price2num($this->pa_ht, 'MU') : "null").",";
  2693. $sql .= ' '.((int) $this->special_code).',';
  2694. $sql .= ' '.((int) $this->rang).',';
  2695. $sql .= " '".$this->db->escape($this->ref_fourn)."'";
  2696. $sql .= ", ".($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) : 'null');
  2697. $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
  2698. $sql .= ", ".price2num($this->multicurrency_subprice, 'CU');
  2699. $sql .= ", ".price2num($this->multicurrency_total_ht, 'CT');
  2700. $sql .= ", ".price2num($this->multicurrency_total_tva, 'CT');
  2701. $sql .= ", ".price2num($this->multicurrency_total_ttc, 'CT');
  2702. $sql .= ", ".($this->fk_unit ? ((int) $this->fk_unit) : 'null');
  2703. $sql .= ')';
  2704. dol_syslog(get_class($this).'::insert', LOG_DEBUG);
  2705. $resql = $this->db->query($sql);
  2706. if ($resql) {
  2707. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'supplier_proposaldet');
  2708. if (!$error) {
  2709. $result = $this->insertExtraFields();
  2710. if ($result < 0) {
  2711. $error++;
  2712. }
  2713. }
  2714. if (!$error && !$notrigger) {
  2715. // Call trigger
  2716. $result = $this->call_trigger('LINESUPPLIER_PROPOSAL_INSERT', $user);
  2717. if ($result < 0) {
  2718. $this->db->rollback();
  2719. return -1;
  2720. }
  2721. // End call triggers
  2722. }
  2723. $this->db->commit();
  2724. return 1;
  2725. } else {
  2726. $this->error = $this->db->error()." sql=".$sql;
  2727. $this->db->rollback();
  2728. return -1;
  2729. }
  2730. }
  2731. /**
  2732. * Delete line in database
  2733. *
  2734. * @return int <0 if ko, >0 if ok
  2735. */
  2736. public function delete()
  2737. {
  2738. global $conf, $langs, $user;
  2739. $error = 0;
  2740. $this->db->begin();
  2741. $sql = "DELETE FROM ".MAIN_DB_PREFIX."supplier_proposaldet WHERE rowid = ".((int) $this->id);
  2742. dol_syslog("SupplierProposalLine::delete", LOG_DEBUG);
  2743. if ($this->db->query($sql)) {
  2744. // Remove extrafields
  2745. if (!$error) {
  2746. $result = $this->deleteExtraFields();
  2747. if ($result < 0) {
  2748. $error++;
  2749. dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
  2750. }
  2751. }
  2752. // Call trigger
  2753. $result = $this->call_trigger('LINESUPPLIER_PROPOSAL_DELETE', $user);
  2754. if ($result < 0) {
  2755. $this->db->rollback();
  2756. return -1;
  2757. }
  2758. // End call triggers
  2759. $this->db->commit();
  2760. return 1;
  2761. } else {
  2762. $this->error = $this->db->error()." sql=".$sql;
  2763. $this->db->rollback();
  2764. return -1;
  2765. }
  2766. }
  2767. /**
  2768. * Update propal line object into DB
  2769. *
  2770. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  2771. * @return int <0 if ko, >0 if ok
  2772. */
  2773. public function update($notrigger = 0)
  2774. {
  2775. global $conf, $langs, $user;
  2776. $error = 0;
  2777. // Clean parameters
  2778. if (empty($this->tva_tx)) {
  2779. $this->tva_tx = 0;
  2780. }
  2781. if (empty($this->localtax1_tx)) {
  2782. $this->localtax1_tx = 0;
  2783. }
  2784. if (empty($this->localtax2_tx)) {
  2785. $this->localtax2_tx = 0;
  2786. }
  2787. if (empty($this->total_localtax1)) {
  2788. $this->total_localtax1 = 0;
  2789. }
  2790. if (empty($this->total_localtax2)) {
  2791. $this->total_localtax2 = 0;
  2792. }
  2793. if (empty($this->localtax1_type)) {
  2794. $this->localtax1_type = 0;
  2795. }
  2796. if (empty($this->localtax2_type)) {
  2797. $this->localtax2_type = 0;
  2798. }
  2799. if (empty($this->marque_tx)) {
  2800. $this->marque_tx = 0;
  2801. }
  2802. if (empty($this->marge_tx)) {
  2803. $this->marge_tx = 0;
  2804. }
  2805. if (empty($this->remise_percent)) {
  2806. $this->remise_percent = 0;
  2807. }
  2808. if (empty($this->info_bits)) {
  2809. $this->info_bits = 0;
  2810. }
  2811. if (empty($this->special_code)) {
  2812. $this->special_code = 0;
  2813. }
  2814. if (empty($this->fk_parent_line)) {
  2815. $this->fk_parent_line = 0;
  2816. }
  2817. if (empty($this->fk_fournprice)) {
  2818. $this->fk_fournprice = 0;
  2819. }
  2820. if (empty($this->fk_unit)) {
  2821. $this->fk_unit = 0;
  2822. }
  2823. if (empty($this->subprice)) {
  2824. $this->subprice = 0;
  2825. }
  2826. if (empty($this->pa_ht)) {
  2827. $this->pa_ht = 0;
  2828. }
  2829. // if buy price not defined, define buyprice as configured in margin admin
  2830. if ($this->pa_ht == 0) {
  2831. $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
  2832. if ($result < 0) {
  2833. return $result;
  2834. } else {
  2835. $this->pa_ht = $result;
  2836. }
  2837. }
  2838. $this->db->begin();
  2839. // Mise a jour ligne en base
  2840. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposaldet SET";
  2841. $sql .= " description='".$this->db->escape($this->desc)."'";
  2842. $sql .= " , label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
  2843. $sql .= " , product_type=".((int) $this->product_type);
  2844. $sql .= " , date_start=".($this->date_start ? "'".$this->db->idate($this->date_start)."'" : "null");
  2845. $sql .= " , date_end=".($this->date_end ? "'".$this->db->idate($this->date_end)."'" : "null");
  2846. $sql .= " , tva_tx='".price2num($this->tva_tx)."'";
  2847. $sql .= " , localtax1_tx=".price2num($this->localtax1_tx);
  2848. $sql .= " , localtax2_tx=".price2num($this->localtax2_tx);
  2849. $sql .= " , localtax1_type='".$this->db->escape($this->localtax1_type)."'";
  2850. $sql .= " , localtax2_type='".$this->db->escape($this->localtax2_type)."'";
  2851. $sql .= " , qty='".price2num($this->qty)."'";
  2852. $sql .= " , subprice=".price2num($this->subprice)."";
  2853. $sql .= " , remise_percent=".price2num($this->remise_percent)."";
  2854. $sql .= " , info_bits='".$this->db->escape($this->info_bits)."'";
  2855. if (empty($this->skip_update_total)) {
  2856. $sql .= " , total_ht=".price2num($this->total_ht)."";
  2857. $sql .= " , total_tva=".price2num($this->total_tva)."";
  2858. $sql .= " , total_ttc=".price2num($this->total_ttc)."";
  2859. $sql .= " , total_localtax1=".price2num($this->total_localtax1)."";
  2860. $sql .= " , total_localtax2=".price2num($this->total_localtax2)."";
  2861. }
  2862. $sql .= " , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? "'".$this->db->escape($this->fk_fournprice)."'" : "null");
  2863. $sql .= " , buy_price_ht=".price2num($this->pa_ht);
  2864. if (strlen($this->special_code)) {
  2865. $sql .= " , special_code=".((int) $this->special_code);
  2866. }
  2867. $sql .= " , fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line : "null");
  2868. if (!empty($this->rang)) {
  2869. $sql .= ", rang=".((int) $this->rang);
  2870. }
  2871. $sql .= " , ref_fourn=".(!empty($this->ref_fourn) ? "'".$this->db->escape($this->ref_fourn)."'" : "null");
  2872. $sql .= " , fk_unit=".($this->fk_unit ? $this->fk_unit : 'null');
  2873. // Multicurrency
  2874. $sql .= " , multicurrency_subprice=".price2num($this->multicurrency_subprice)."";
  2875. $sql .= " , multicurrency_total_ht=".price2num($this->multicurrency_total_ht)."";
  2876. $sql .= " , multicurrency_total_tva=".price2num($this->multicurrency_total_tva)."";
  2877. $sql .= " , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc)."";
  2878. $sql .= " WHERE rowid = ".((int) $this->id);
  2879. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  2880. $resql = $this->db->query($sql);
  2881. if ($resql) {
  2882. if (!$error) {
  2883. $result = $this->insertExtraFields();
  2884. if ($result < 0) {
  2885. $error++;
  2886. }
  2887. }
  2888. if (!$error && !$notrigger) {
  2889. // Call trigger
  2890. $result = $this->call_trigger('LINESUPPLIER_PROPOSAL_MODIFY', $user);
  2891. if ($result < 0) {
  2892. $this->db->rollback();
  2893. return -1;
  2894. }
  2895. // End call triggers
  2896. }
  2897. $this->db->commit();
  2898. return 1;
  2899. } else {
  2900. $this->error = $this->db->error();
  2901. $this->db->rollback();
  2902. return -2;
  2903. }
  2904. }
  2905. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2906. /**
  2907. * Update DB line fields total_xxx
  2908. * Used by migration
  2909. *
  2910. * @return int <0 if ko, >0 if ok
  2911. */
  2912. public function update_total()
  2913. {
  2914. // phpcs:enable
  2915. $this->db->begin();
  2916. // Mise a jour ligne en base
  2917. $sql = "UPDATE ".MAIN_DB_PREFIX."supplier_proposaldet SET";
  2918. $sql .= " total_ht=".price2num($this->total_ht, 'MT');
  2919. $sql .= ",total_tva=".price2num($this->total_tva, 'MT');
  2920. $sql .= ",total_ttc=".price2num($this->total_ttc, 'MT');
  2921. $sql .= " WHERE rowid = ".((int) $this->id);
  2922. dol_syslog("SupplierProposalLine::update_total", LOG_DEBUG);
  2923. $resql = $this->db->query($sql);
  2924. if ($resql) {
  2925. $this->db->commit();
  2926. return 1;
  2927. } else {
  2928. $this->error = $this->db->error();
  2929. $this->db->rollback();
  2930. return -2;
  2931. }
  2932. }
  2933. }