propal.class.php 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414
  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-2017 Philippe Grand <philippe.grand@atoo-net.com>
  11. * Copyright (C) 2012-2014 Christophe Battarel <christophe.battarel@altairis.fr>
  12. * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
  13. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  14. * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
  15. * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
  16. * Copyright (C) 2018-2021 Frédéric France <frederic.france@netlogic.fr>
  17. * Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es>
  18. * Copyright (C) 2022 ATM Consulting <contact@atm-consulting.fr>
  19. * Copyright (C) 2022 OpenDSI <support@open-dsi.fr>
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License as published by
  23. * the Free Software Foundation; either version 3 of the License, or
  24. * (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  33. */
  34. /**
  35. * \file htdocs/comm/propal/class/propal.class.php
  36. * \brief File of class to manage proposals
  37. */
  38. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  39. require_once DOL_DOCUMENT_ROOT."/core/class/commonobjectline.class.php";
  40. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  42. require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
  43. require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
  44. require_once DOL_DOCUMENT_ROOT.'/core/class/commonincoterm.class.php';
  45. /**
  46. * Class to manage proposals
  47. */
  48. class Propal extends CommonObject
  49. {
  50. use CommonIncoterm;
  51. /**
  52. * @var string ID to identify managed object
  53. */
  54. public $element = 'propal';
  55. /**
  56. * @var string Name of table without prefix where object is stored
  57. */
  58. public $table_element = 'propal';
  59. /**
  60. * @var int Name of subtable line
  61. */
  62. public $table_element_line = 'propaldet';
  63. /**
  64. * @var string Fieldname with ID of parent key if this field has a parent
  65. */
  66. public $fk_element = 'fk_propal';
  67. /**
  68. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  69. */
  70. public $picto = 'propal';
  71. /**
  72. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  73. * @var int
  74. */
  75. public $ismultientitymanaged = 1;
  76. /**
  77. * 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
  78. * @var integer
  79. */
  80. public $restrictiononfksoc = 1;
  81. /**
  82. * {@inheritdoc}
  83. */
  84. protected $table_ref_field = 'ref';
  85. /**
  86. * ID of the client
  87. * @var int
  88. */
  89. public $socid;
  90. /**
  91. * ID of the contact
  92. * @var int
  93. */
  94. public $contactid;
  95. public $author;
  96. /**
  97. * Ref from thirdparty
  98. * @var string
  99. */
  100. public $ref_client;
  101. /**
  102. * Status of the quote
  103. * @var int
  104. * @see Propal::STATUS_DRAFT, Propal::STATUS_VALIDATED, Propal::STATUS_SIGNED, Propal::STATUS_NOTSIGNED, Propal::STATUS_BILLED
  105. */
  106. public $statut;
  107. /**
  108. * Status of the quote
  109. * @var int
  110. * @see Propal::STATUS_DRAFT, Propal::STATUS_VALIDATED, Propal::STATUS_SIGNED, Propal::STATUS_NOTSIGNED, Propal::STATUS_BILLED
  111. */
  112. public $status;
  113. /**
  114. * @deprecated
  115. * @see $date_creation
  116. */
  117. public $datec;
  118. /**
  119. * @var integer|string $date_creation;
  120. */
  121. public $date_creation;
  122. /**
  123. * @deprecated
  124. * @see $date_validation
  125. */
  126. public $datev;
  127. /**
  128. * @var integer|string $date_validation;
  129. */
  130. public $date_validation;
  131. /**
  132. * @var integer|string $date_signature;
  133. */
  134. public $date_signature;
  135. /**
  136. * @var User $user_signature
  137. */
  138. public $user_signature;
  139. /**
  140. * @var integer|string date of the quote;
  141. */
  142. public $date;
  143. /**
  144. * @deprecated
  145. * @see $date
  146. */
  147. public $datep;
  148. /**
  149. * @var int Date expected for delivery
  150. * @deprecated
  151. */
  152. public $date_livraison; // deprecated; Use delivery_date instead.
  153. /**
  154. * @var integer|string $delivery_date;
  155. */
  156. public $delivery_date; // Date expected of shipment (date starting shipment, not the reception that occurs some days after)
  157. public $fin_validite;
  158. public $user_author_id;
  159. public $user_valid_id;
  160. public $user_close_id;
  161. /**
  162. * @deprecated
  163. * @see $total_ht
  164. */
  165. public $price;
  166. /**
  167. * @deprecated
  168. * @see $total_tva
  169. */
  170. public $tva;
  171. /**
  172. * @deprecated
  173. * @see $total_ttc
  174. */
  175. public $total;
  176. public $cond_reglement_code;
  177. public $mode_reglement_code;
  178. public $remise_percent;
  179. /**
  180. * @deprecated
  181. */
  182. public $remise;
  183. /**
  184. * @deprecated
  185. */
  186. public $remise_absolue;
  187. /**
  188. * @var int ID
  189. * @deprecated
  190. */
  191. public $fk_address;
  192. public $address_type;
  193. public $address;
  194. public $availability_id;
  195. public $availability_code;
  196. public $duree_validite;
  197. public $demand_reason_id;
  198. public $demand_reason_code;
  199. public $warehouse_id;
  200. public $extraparams = array();
  201. /**
  202. * @var PropaleLigne[]
  203. */
  204. public $lines = array();
  205. public $line;
  206. public $labelStatus = array();
  207. public $labelStatusShort = array();
  208. // Multicurrency
  209. /**
  210. * @var int ID
  211. */
  212. public $fk_multicurrency;
  213. public $multicurrency_code;
  214. public $multicurrency_tx;
  215. public $multicurrency_total_ht;
  216. public $multicurrency_total_tva;
  217. public $multicurrency_total_ttc;
  218. /**
  219. * 'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
  220. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  221. * 'label' the translation key.
  222. * 'enabled' is a condition when the field must be managed.
  223. * 'position' is the sort order of field.
  224. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  225. * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
  226. * 'noteditable' says if field is not editable (1 or 0)
  227. * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.
  228. * 'index' if we want an index in database.
  229. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  230. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  231. * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
  232. * 'css' is the CSS style to use on field. For example: 'maxwidth200'
  233. * 'help' is a string visible as a tooltip on field
  234. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  235. * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.
  236. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  237. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  238. *
  239. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  240. */
  241. // BEGIN MODULEBUILDER PROPERTIES
  242. /**
  243. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  244. */
  245. public $fields = array(
  246. 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
  247. 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>15, 'index'=>1),
  248. 'ref' =>array('type'=>'varchar(30)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'showoncombobox'=>1, 'position'=>20),
  249. 'ref_client' =>array('type'=>'varchar(255)', 'label'=>'RefCustomer', 'enabled'=>1, 'visible'=>-1, 'position'=>22),
  250. 'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'RefExt', 'enabled'=>1, 'visible'=>0, 'position'=>40),
  251. 'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'position'=>23),
  252. 'fk_projet' =>array('type'=>'integer:Project:projet/class/project.class.php:1:fk_statut=1', 'label'=>'Fk projet', 'enabled'=>1, 'visible'=>-1, 'position'=>24),
  253. 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>25),
  254. 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
  255. 'datep' =>array('type'=>'date', 'label'=>'Date', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
  256. 'fin_validite' =>array('type'=>'datetime', 'label'=>'DateEnd', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
  257. 'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>70),
  258. 'date_cloture' =>array('type'=>'datetime', 'label'=>'DateClosing', 'enabled'=>1, 'visible'=>-1, 'position'=>75),
  259. 'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
  260. 'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>85),
  261. 'fk_user_valid' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
  262. 'fk_user_cloture' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user cloture', 'enabled'=>1, 'visible'=>-1, 'position'=>95),
  263. 'price' =>array('type'=>'double', 'label'=>'Price', 'enabled'=>1, 'visible'=>-1, 'position'=>105),
  264. 'remise_percent' =>array('type'=>'double', 'label'=>'RelativeDiscount', 'enabled'=>1, 'visible'=>-1, 'position'=>110),
  265. //'remise_absolue' =>array('type'=>'double', 'label'=>'CustomerRelativeDiscount', 'enabled'=>1, 'visible'=>-1, 'position'=>115),
  266. //'remise' =>array('type'=>'double', 'label'=>'Remise', 'enabled'=>1, 'visible'=>-1, 'position'=>120),
  267. 'total_ht' =>array('type'=>'double(24,8)', 'label'=>'TotalHT', 'enabled'=>1, 'visible'=>-1, 'position'=>125, 'isameasure'=>1),
  268. 'total_tva' =>array('type'=>'double(24,8)', 'label'=>'VAT', 'enabled'=>1, 'visible'=>-1, 'position'=>130, 'isameasure'=>1),
  269. 'localtax1' =>array('type'=>'double(24,8)', 'label'=>'LocalTax1', 'enabled'=>1, 'visible'=>-1, 'position'=>135, 'isameasure'=>1),
  270. 'localtax2' =>array('type'=>'double(24,8)', 'label'=>'LocalTax2', 'enabled'=>1, 'visible'=>-1, 'position'=>140, 'isameasure'=>1),
  271. 'total_ttc' =>array('type'=>'double(24,8)', 'label'=>'TotalTTC', 'enabled'=>1, 'visible'=>-1, 'position'=>145, 'isameasure'=>1),
  272. 'fk_account' =>array('type'=>'integer', 'label'=>'BankAccount', 'enabled'=>1, 'visible'=>-1, 'position'=>150),
  273. 'fk_currency' =>array('type'=>'varchar(3)', 'label'=>'Currency', 'enabled'=>1, 'visible'=>-1, 'position'=>155),
  274. 'fk_cond_reglement' =>array('type'=>'integer', 'label'=>'PaymentTerm', 'enabled'=>1, 'visible'=>-1, 'position'=>160),
  275. 'fk_mode_reglement' =>array('type'=>'integer', 'label'=>'PaymentMode', 'enabled'=>1, 'visible'=>-1, 'position'=>165),
  276. 'note_private' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>170),
  277. 'note_public' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>175),
  278. 'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'PDFTemplate', 'enabled'=>1, 'visible'=>0, 'position'=>180),
  279. 'date_livraison' =>array('type'=>'date', 'label'=>'DateDeliveryPlanned', 'enabled'=>1, 'visible'=>-1, 'position'=>185),
  280. 'fk_shipping_method' =>array('type'=>'integer', 'label'=>'ShippingMethod', 'enabled'=>1, 'visible'=>-1, 'position'=>190),
  281. 'fk_warehouse' =>array('type'=>'integer:Entrepot:product/stock/class/entrepot.class.php', 'label'=>'Fk warehouse', 'enabled'=>1, 'visible'=>-1, 'position'=>191),
  282. 'fk_availability' =>array('type'=>'integer', 'label'=>'Availability', 'enabled'=>1, 'visible'=>-1, 'position'=>195),
  283. 'fk_delivery_address' =>array('type'=>'integer', 'label'=>'DeliveryAddress', 'enabled'=>1, 'visible'=>0, 'position'=>200), // deprecated
  284. 'fk_input_reason' =>array('type'=>'integer', 'label'=>'InputReason', 'enabled'=>1, 'visible'=>-1, 'position'=>205),
  285. 'extraparams' =>array('type'=>'varchar(255)', 'label'=>'Extraparams', 'enabled'=>1, 'visible'=>-1, 'position'=>215),
  286. 'fk_incoterms' =>array('type'=>'integer', 'label'=>'IncotermCode', 'enabled'=>'$conf->incoterm->enabled', 'visible'=>-1, 'position'=>220),
  287. 'location_incoterms' =>array('type'=>'varchar(255)', 'label'=>'IncotermLabel', 'enabled'=>'$conf->incoterm->enabled', 'visible'=>-1, 'position'=>225),
  288. 'fk_multicurrency' =>array('type'=>'integer', 'label'=>'MulticurrencyID', 'enabled'=>1, 'visible'=>-1, 'position'=>230),
  289. 'multicurrency_code' =>array('type'=>'varchar(255)', 'label'=>'MulticurrencyCurrency', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>235),
  290. 'multicurrency_tx' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyRate', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>240, 'isameasure'=>1),
  291. 'multicurrency_total_ht' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyAmountHT', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>245, 'isameasure'=>1),
  292. 'multicurrency_total_tva' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyAmountVAT', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>250, 'isameasure'=>1),
  293. 'multicurrency_total_ttc' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyAmountTTC', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>255, 'isameasure'=>1),
  294. 'last_main_doc' =>array('type'=>'varchar(255)', 'label'=>'LastMainDoc', 'enabled'=>1, 'visible'=>-1, 'position'=>260),
  295. 'fk_statut' =>array('type'=>'smallint(6)', 'label'=>'Status', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
  296. 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>900),
  297. );
  298. // END MODULEBUILDER PROPERTIES
  299. /**
  300. * Draft status
  301. */
  302. const STATUS_DRAFT = 0;
  303. /**
  304. * Validated status
  305. */
  306. const STATUS_VALIDATED = 1;
  307. /**
  308. * Signed quote
  309. */
  310. const STATUS_SIGNED = 2;
  311. /**
  312. * Not signed quote
  313. */
  314. const STATUS_NOTSIGNED = 3;
  315. /**
  316. * Billed or processed quote
  317. */
  318. const STATUS_BILLED = 4; // Todo rename into STATUS_CLOSE ?
  319. /**
  320. * Constructor
  321. *
  322. * @param DoliDB $db Database handler
  323. * @param int $socid Id third party
  324. * @param int $propalid Id proposal
  325. */
  326. public function __construct($db, $socid = 0, $propalid = 0)
  327. {
  328. global $conf, $langs;
  329. $this->db = $db;
  330. $this->socid = $socid;
  331. $this->id = $propalid;
  332. $this->duree_validite = ((int) $conf->global->PROPALE_VALIDITY_DURATION);
  333. }
  334. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  335. /**
  336. * Add line into array ->lines
  337. * $this->thirdparty should be loaded
  338. *
  339. * @param int $idproduct Product Id to add
  340. * @param int $qty Quantity
  341. * @param int $remise_percent Discount effected on Product
  342. * @return int <0 if KO, >0 if OK
  343. *
  344. * TODO Replace calls to this function by generation objet Ligne
  345. */
  346. public function add_product($idproduct, $qty, $remise_percent = 0)
  347. {
  348. // phpcs:enable
  349. global $conf, $mysoc;
  350. if (!$qty) {
  351. $qty = 1;
  352. }
  353. dol_syslog(get_class($this)."::add_product $idproduct, $qty, $remise_percent");
  354. if ($idproduct > 0) {
  355. $prod = new Product($this->db);
  356. $prod->fetch($idproduct);
  357. $productdesc = $prod->description;
  358. $tva_tx = get_default_tva($mysoc, $this->thirdparty, $prod->id);
  359. $tva_npr = get_default_npr($mysoc, $this->thirdparty, $prod->id);
  360. if (empty($tva_tx)) {
  361. $tva_npr = 0;
  362. }
  363. $vat_src_code = ''; // May be defined into tva_tx
  364. $localtax1_tx = get_localtax($tva_tx, 1, $mysoc, $this->thirdparty, $tva_npr);
  365. $localtax2_tx = get_localtax($tva_tx, 2, $mysoc, $this->thirdparty, $tva_npr);
  366. // multiprices
  367. if ($conf->global->PRODUIT_MULTIPRICES && $this->thirdparty->price_level) {
  368. $price = $prod->multiprices[$this->thirdparty->price_level];
  369. } else {
  370. $price = $prod->price;
  371. }
  372. $line = new PropaleLigne($this->db);
  373. $line->fk_product = $idproduct;
  374. $line->desc = $productdesc;
  375. $line->qty = $qty;
  376. $line->subprice = $price;
  377. $line->remise_percent = $remise_percent;
  378. $line->vat_src_code = $vat_src_code;
  379. $line->tva_tx = $tva_tx;
  380. $line->fk_unit = $prod->fk_unit;
  381. if ($tva_npr) {
  382. $line->info_bits = 1;
  383. }
  384. $this->lines[] = $line;
  385. }
  386. }
  387. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  388. /**
  389. * Adding line of fixed discount in the proposal in DB
  390. *
  391. * @param int $idremise Id of fixed discount
  392. * @return int >0 if OK, <0 if KO
  393. */
  394. public function insert_discount($idremise)
  395. {
  396. // phpcs:enable
  397. global $langs;
  398. include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  399. include_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
  400. $this->db->begin();
  401. $remise = new DiscountAbsolute($this->db);
  402. $result = $remise->fetch($idremise);
  403. if ($result > 0) {
  404. if ($remise->fk_facture) { // Protection against multiple submission
  405. $this->error = $langs->trans("ErrorDiscountAlreadyUsed");
  406. $this->db->rollback();
  407. return -5;
  408. }
  409. $line = new PropaleLigne($this->db);
  410. $this->line->context = $this->context;
  411. $line->fk_propal = $this->id;
  412. $line->fk_remise_except = $remise->id;
  413. $line->desc = $remise->description; // Description ligne
  414. $line->vat_src_code = $remise->vat_src_code;
  415. $line->tva_tx = $remise->tva_tx;
  416. $line->subprice = -$remise->amount_ht;
  417. $line->fk_product = 0; // Id produit predefined
  418. $line->qty = 1;
  419. $line->remise = 0;
  420. $line->remise_percent = 0;
  421. $line->rang = -1;
  422. $line->info_bits = 2;
  423. // TODO deprecated
  424. $line->price = -$remise->amount_ht;
  425. $line->total_ht = -$remise->amount_ht;
  426. $line->total_tva = -$remise->amount_tva;
  427. $line->total_ttc = -$remise->amount_ttc;
  428. $result = $line->insert();
  429. if ($result > 0) {
  430. $result = $this->update_price(1);
  431. if ($result > 0) {
  432. $this->db->commit();
  433. return 1;
  434. } else {
  435. $this->db->rollback();
  436. return -1;
  437. }
  438. } else {
  439. $this->error = $line->error;
  440. $this->errors = $line->errors;
  441. $this->db->rollback();
  442. return -2;
  443. }
  444. } else {
  445. $this->db->rollback();
  446. return -2;
  447. }
  448. }
  449. /**
  450. * Add a proposal line into database (linked to product/service or not)
  451. * The parameters are already supposed to be appropriate and with final values to the call
  452. * of this method. Also, for the VAT rate, it must have already been defined
  453. * by whose calling the method get_default_tva (societe_vendeuse, societe_acheteuse, '' product)
  454. * and desc must already have the right value (it's up to the caller to manage multilanguage)
  455. *
  456. * @param string $desc Description of line
  457. * @param float $pu_ht Unit price
  458. * @param float $qty Quantity
  459. * @param float $txtva Force Vat rate, -1 for auto (Can contain the vat_src_code too with syntax '9.9 (CODE)')
  460. * @param float $txlocaltax1 Local tax 1 rate (deprecated, use instead txtva with code inside)
  461. * @param float $txlocaltax2 Local tax 2 rate (deprecated, use instead txtva with code inside)
  462. * @param int $fk_product Product/Service ID predefined
  463. * @param float $remise_percent Pourcentage de remise de la ligne
  464. * @param string $price_base_type HT or TTC
  465. * @param float $pu_ttc Prix unitaire TTC
  466. * @param int $info_bits Bits for type of lines
  467. * @param int $type Type of line (0=product, 1=service). Not used if fk_product is defined, the type of product is used.
  468. * @param int $rang Position of line
  469. * @param int $special_code Special code (also used by externals modules!)
  470. * @param int $fk_parent_line Id of parent line
  471. * @param int $fk_fournprice Id supplier price
  472. * @param int $pa_ht Buying price without tax
  473. * @param string $label ???
  474. * @param int $date_start Start date of the line
  475. * @param int $date_end End date of the line
  476. * @param array $array_options extrafields array
  477. * @param string $fk_unit Code of the unit to use. Null to use the default one
  478. * @param string $origin Depend on global conf MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION can be 'orderdet', 'propaldet'..., else 'order','propal,'....
  479. * @param int $origin_id Depend on global conf MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION can be Id of origin object (aka line id), else object id
  480. * @param double $pu_ht_devise Unit price in currency
  481. * @param int $fk_remise_except Id discount if line is from a discount
  482. * @return int >0 if OK, <0 if KO
  483. * @see add_product()
  484. */
  485. public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.0, $fk_product = 0, $remise_percent = 0.0, $price_base_type = 'HT', $pu_ttc = 0.0, $info_bits = 0, $type = 0, $rang = -1, $special_code = 0, $fk_parent_line = 0, $fk_fournprice = 0, $pa_ht = 0, $label = '', $date_start = '', $date_end = '', $array_options = 0, $fk_unit = null, $origin = '', $origin_id = 0, $pu_ht_devise = 0, $fk_remise_except = 0)
  486. {
  487. global $mysoc, $conf, $langs;
  488. dol_syslog(get_class($this)."::addline propalid=$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, fk_remise_except=".$fk_remise_except);
  489. if ($this->statut == self::STATUS_DRAFT) {
  490. include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  491. // Clean parameters
  492. if (empty($remise_percent)) {
  493. $remise_percent = 0;
  494. }
  495. if (empty($qty)) {
  496. $qty = 0;
  497. }
  498. if (empty($info_bits)) {
  499. $info_bits = 0;
  500. }
  501. if (empty($rang)) {
  502. $rang = 0;
  503. }
  504. if (empty($fk_parent_line) || $fk_parent_line < 0) {
  505. $fk_parent_line = 0;
  506. }
  507. $remise_percent = price2num($remise_percent);
  508. $qty = price2num($qty);
  509. $pu_ht = price2num($pu_ht);
  510. $pu_ht_devise = price2num($pu_ht_devise);
  511. $pu_ttc = price2num($pu_ttc);
  512. if (!preg_match('/\((.*)\)/', $txtva)) {
  513. $txtva = price2num($txtva); // $txtva can have format '5,1' or '5.1' or '5.1(XXX)', we must clean only if '5,1'
  514. }
  515. $txlocaltax1 = price2num($txlocaltax1);
  516. $txlocaltax2 = price2num($txlocaltax2);
  517. $pa_ht = price2num($pa_ht);
  518. if ($price_base_type == 'HT') {
  519. $pu = $pu_ht;
  520. } else {
  521. $pu = $pu_ttc;
  522. }
  523. // Check parameters
  524. if ($type < 0) {
  525. return -1;
  526. }
  527. if ($date_start && $date_end && $date_start > $date_end) {
  528. $langs->load("errors");
  529. $this->error = $langs->trans('ErrorStartDateGreaterEnd');
  530. return -1;
  531. }
  532. $this->db->begin();
  533. $product_type = $type;
  534. if (!empty($fk_product) && $fk_product > 0) {
  535. $product = new Product($this->db);
  536. $result = $product->fetch($fk_product);
  537. $product_type = $product->type;
  538. if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_PROPOSAL) && $product_type == 0 && $product->stock_reel < $qty) {
  539. $langs->load("errors");
  540. $this->error = $langs->trans('ErrorStockIsNotEnoughToAddProductOnProposal', $product->ref);
  541. $this->db->rollback();
  542. return -3;
  543. }
  544. }
  545. // Calcul du total TTC et de la TVA pour la ligne a partir de
  546. // qty, pu, remise_percent et txtva
  547. // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
  548. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
  549. $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
  550. // Clean vat code
  551. $reg = array();
  552. $vat_src_code = '';
  553. $reg = array();
  554. if (preg_match('/\((.*)\)/', $txtva, $reg)) {
  555. $vat_src_code = $reg[1];
  556. $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
  557. }
  558. $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
  559. $total_ht = $tabprice[0];
  560. $total_tva = $tabprice[1];
  561. $total_ttc = $tabprice[2];
  562. $total_localtax1 = $tabprice[9];
  563. $total_localtax2 = $tabprice[10];
  564. $pu_ht = $tabprice[3];
  565. $pu_tva = $tabprice[4];
  566. $pu_ttc = $tabprice[5];
  567. // MultiCurrency
  568. $multicurrency_total_ht = $tabprice[16];
  569. $multicurrency_total_tva = $tabprice[17];
  570. $multicurrency_total_ttc = $tabprice[18];
  571. $pu_ht_devise = $tabprice[19];
  572. // Rang to use
  573. $ranktouse = $rang;
  574. if ($ranktouse == -1) {
  575. $rangmax = $this->line_max($fk_parent_line);
  576. $ranktouse = $rangmax + 1;
  577. }
  578. // TODO A virer
  579. // Anciens indicateurs: $price, $remise (a ne plus utiliser)
  580. $price = $pu;
  581. $remise = 0;
  582. if ($remise_percent > 0) {
  583. $remise = round(($pu * $remise_percent / 100), 2);
  584. $price = $pu - $remise;
  585. }
  586. // Insert line
  587. $this->line = new PropaleLigne($this->db);
  588. $this->line->context = $this->context;
  589. $this->line->fk_propal = $this->id;
  590. $this->line->label = $label;
  591. $this->line->desc = $desc;
  592. $this->line->qty = $qty;
  593. $this->line->vat_src_code = $vat_src_code;
  594. $this->line->tva_tx = $txtva;
  595. $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
  596. $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
  597. $this->line->localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0];
  598. $this->line->localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2];
  599. $this->line->fk_product = $fk_product;
  600. $this->line->product_type = $type;
  601. $this->line->fk_remise_except = $fk_remise_except;
  602. $this->line->remise_percent = $remise_percent;
  603. $this->line->subprice = $pu_ht;
  604. $this->line->rang = $ranktouse;
  605. $this->line->info_bits = $info_bits;
  606. $this->line->total_ht = $total_ht;
  607. $this->line->total_tva = $total_tva;
  608. $this->line->total_localtax1 = $total_localtax1;
  609. $this->line->total_localtax2 = $total_localtax2;
  610. $this->line->total_ttc = $total_ttc;
  611. $this->line->special_code = $special_code;
  612. $this->line->fk_parent_line = $fk_parent_line;
  613. $this->line->fk_unit = $fk_unit;
  614. $this->line->date_start = $date_start;
  615. $this->line->date_end = $date_end;
  616. $this->line->fk_fournprice = $fk_fournprice;
  617. $this->line->pa_ht = $pa_ht;
  618. $this->line->origin_id = $origin_id;
  619. $this->line->origin = $origin;
  620. // Multicurrency
  621. $this->line->fk_multicurrency = $this->fk_multicurrency;
  622. $this->line->multicurrency_code = $this->multicurrency_code;
  623. $this->line->multicurrency_subprice = $pu_ht_devise;
  624. $this->line->multicurrency_total_ht = $multicurrency_total_ht;
  625. $this->line->multicurrency_total_tva = $multicurrency_total_tva;
  626. $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
  627. // Mise en option de la ligne
  628. if (empty($qty) && empty($special_code)) {
  629. $this->line->special_code = 3;
  630. }
  631. // TODO deprecated
  632. $this->line->price = $price;
  633. $this->line->remise = $remise;
  634. if (is_array($array_options) && count($array_options) > 0) {
  635. $this->line->array_options = $array_options;
  636. }
  637. $result = $this->line->insert();
  638. if ($result > 0) {
  639. // Reorder if child line
  640. if (!empty($fk_parent_line)) {
  641. $this->line_order(true, 'DESC');
  642. }
  643. // Mise a jour informations denormalisees au niveau de la propale meme
  644. $result = $this->update_price(1, 'auto', 0, $mysoc); // This method is designed to add line from user input so total calculation must be done using 'auto' mode.
  645. if ($result > 0) {
  646. $this->db->commit();
  647. return $this->line->id;
  648. } else {
  649. $this->error = $this->db->error();
  650. $this->db->rollback();
  651. return -1;
  652. }
  653. } else {
  654. $this->error = $this->line->error;
  655. $this->errors = $this->line->errors;
  656. $this->db->rollback();
  657. return -2;
  658. }
  659. } else {
  660. dol_syslog(get_class($this)."::addline status of proposal must be Draft to allow use of ->addline()", LOG_ERR);
  661. return -3;
  662. }
  663. }
  664. /**
  665. * Update a proposal line
  666. *
  667. * @param int $rowid Id of line
  668. * @param float $pu Unit price (HT or TTC depending on price_base_type)
  669. * @param float $qty Quantity
  670. * @param float $remise_percent Discount on line
  671. * @param float $txtva VAT Rate (Can be '1.23' or '1.23 (ABC)')
  672. * @param float $txlocaltax1 Local tax 1 rate
  673. * @param float $txlocaltax2 Local tax 2 rate
  674. * @param string $desc Description
  675. * @param string $price_base_type HT or TTC
  676. * @param int $info_bits Miscellaneous informations
  677. * @param int $special_code Special code (also used by externals modules!)
  678. * @param int $fk_parent_line Id of parent line (0 in most cases, used by modules adding sublevels into lines).
  679. * @param int $skip_update_total Keep fields total_xxx to 0 (used for special lines by some modules)
  680. * @param int $fk_fournprice Id of origin supplier price
  681. * @param int $pa_ht Price (without tax) of product when it was bought
  682. * @param string $label ???
  683. * @param int $type 0/1=Product/service
  684. * @param int $date_start Start date of the line
  685. * @param int $date_end End date of the line
  686. * @param array $array_options extrafields array
  687. * @param string $fk_unit Code of the unit to use. Null to use the default one
  688. * @param double $pu_ht_devise Unit price in currency
  689. * @param int $notrigger disable line update trigger
  690. * @return int 0 if OK, <0 if KO
  691. */
  692. public function updateline($rowid, $pu, $qty, $remise_percent, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.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, $date_start = '', $date_end = '', $array_options = 0, $fk_unit = null, $pu_ht_devise = 0, $notrigger = 0)
  693. {
  694. global $mysoc, $langs;
  695. dol_syslog(get_class($this)."::updateLine rowid=$rowid, pu=$pu, qty=$qty, remise_percent=$remise_percent,
  696. txtva=$txtva, desc=$desc, price_base_type=$price_base_type, info_bits=$info_bits, special_code=$special_code, fk_parent_line=$fk_parent_line, pa_ht=$pa_ht, type=$type, date_start=$date_start, date_end=$date_end");
  697. include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  698. // Clean parameters
  699. $remise_percent = price2num($remise_percent);
  700. $qty = price2num($qty);
  701. $pu = price2num($pu);
  702. $pu_ht_devise = price2num($pu_ht_devise);
  703. if (!preg_match('/\((.*)\)/', $txtva)) {
  704. $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
  705. }
  706. $txlocaltax1 = price2num($txlocaltax1);
  707. $txlocaltax2 = price2num($txlocaltax2);
  708. $pa_ht = price2num($pa_ht);
  709. if (empty($qty) && empty($special_code)) {
  710. $special_code = 3; // Set option tag
  711. }
  712. if (!empty($qty) && $special_code == 3) {
  713. $special_code = 0; // Remove option tag
  714. }
  715. if (empty($type)) {
  716. $type = 0;
  717. }
  718. if ($date_start && $date_end && $date_start > $date_end) {
  719. $langs->load("errors");
  720. $this->error = $langs->trans('ErrorStartDateGreaterEnd');
  721. return -1;
  722. }
  723. if ($this->statut == self::STATUS_DRAFT) {
  724. $this->db->begin();
  725. // Calcul du total TTC et de la TVA pour la ligne a partir de
  726. // qty, pu, remise_percent et txtva
  727. // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
  728. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
  729. $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
  730. // Clean vat code
  731. $reg = array();
  732. $vat_src_code = '';
  733. if (preg_match('/\((.*)\)/', $txtva, $reg)) {
  734. $vat_src_code = $reg[1];
  735. $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
  736. }
  737. $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise);
  738. $total_ht = $tabprice[0];
  739. $total_tva = $tabprice[1];
  740. $total_ttc = $tabprice[2];
  741. $total_localtax1 = $tabprice[9];
  742. $total_localtax2 = $tabprice[10];
  743. $pu_ht = $tabprice[3];
  744. $pu_tva = $tabprice[4];
  745. $pu_ttc = $tabprice[5];
  746. // MultiCurrency
  747. $multicurrency_total_ht = $tabprice[16];
  748. $multicurrency_total_tva = $tabprice[17];
  749. $multicurrency_total_ttc = $tabprice[18];
  750. $pu_ht_devise = $tabprice[19];
  751. // Anciens indicateurs: $price, $remise (a ne plus utiliser)
  752. $price = $pu;
  753. $remise = 0;
  754. if ($remise_percent > 0) {
  755. $remise = round(($pu * $remise_percent / 100), 2);
  756. $price = $pu - $remise;
  757. }
  758. //Fetch current line from the database and then clone the object and set it in $oldline property
  759. $line = new PropaleLigne($this->db);
  760. $line->fetch($rowid);
  761. $staticline = clone $line;
  762. $line->oldline = $staticline;
  763. $this->line = $line;
  764. $this->line->context = $this->context;
  765. // Reorder if fk_parent_line change
  766. if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
  767. $rangmax = $this->line_max($fk_parent_line);
  768. $this->line->rang = $rangmax + 1;
  769. }
  770. $this->line->id = $rowid;
  771. $this->line->label = $label;
  772. $this->line->desc = $desc;
  773. $this->line->qty = $qty;
  774. $this->line->product_type = $type;
  775. $this->line->vat_src_code = $vat_src_code;
  776. $this->line->tva_tx = $txtva;
  777. $this->line->localtax1_tx = $txlocaltax1;
  778. $this->line->localtax2_tx = $txlocaltax2;
  779. $this->line->localtax1_type = $localtaxes_type[0];
  780. $this->line->localtax2_type = $localtaxes_type[2];
  781. $this->line->remise_percent = $remise_percent;
  782. $this->line->subprice = $pu_ht;
  783. $this->line->info_bits = $info_bits;
  784. $this->line->total_ht = $total_ht;
  785. $this->line->total_tva = $total_tva;
  786. $this->line->total_localtax1 = $total_localtax1;
  787. $this->line->total_localtax2 = $total_localtax2;
  788. $this->line->total_ttc = $total_ttc;
  789. $this->line->special_code = $special_code;
  790. $this->line->fk_parent_line = $fk_parent_line;
  791. $this->line->skip_update_total = $skip_update_total;
  792. $this->line->fk_unit = $fk_unit;
  793. $this->line->fk_fournprice = $fk_fournprice;
  794. $this->line->pa_ht = $pa_ht;
  795. $this->line->date_start = $date_start;
  796. $this->line->date_end = $date_end;
  797. // TODO deprecated
  798. $this->line->price = $price;
  799. $this->line->remise = $remise;
  800. if (is_array($array_options) && count($array_options) > 0) {
  801. // We replace values in this->line->array_options only for entries defined into $array_options
  802. foreach ($array_options as $key => $value) {
  803. $this->line->array_options[$key] = $array_options[$key];
  804. }
  805. }
  806. // Multicurrency
  807. $this->line->multicurrency_subprice = $pu_ht_devise;
  808. $this->line->multicurrency_total_ht = $multicurrency_total_ht;
  809. $this->line->multicurrency_total_tva = $multicurrency_total_tva;
  810. $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
  811. $result = $this->line->update($notrigger);
  812. if ($result > 0) {
  813. // Reorder if child line
  814. if (!empty($fk_parent_line)) {
  815. $this->line_order(true, 'DESC');
  816. }
  817. $this->update_price(1);
  818. $this->fk_propal = $this->id;
  819. $this->rowid = $rowid;
  820. $this->db->commit();
  821. return $result;
  822. } else {
  823. $this->error = $this->line->error;
  824. $this->errors = $this->line->errors;
  825. $this->db->rollback();
  826. return -1;
  827. }
  828. } else {
  829. dol_syslog(get_class($this)."::updateline Erreur -2 Propal en mode incompatible pour cette action");
  830. return -2;
  831. }
  832. }
  833. /**
  834. * Delete detail line
  835. *
  836. * @param int $lineid Id of line to delete
  837. * @return int >0 if OK, <0 if KO
  838. */
  839. public function deleteline($lineid)
  840. {
  841. global $user;
  842. if ($this->statut == self::STATUS_DRAFT) {
  843. $this->db->begin();
  844. $line = new PropaleLigne($this->db);
  845. // For triggers
  846. $line->fetch($lineid);
  847. if ($line->delete($user) > 0) {
  848. $this->update_price(1);
  849. $this->db->commit();
  850. return 1;
  851. } else {
  852. $this->db->rollback();
  853. return -1;
  854. }
  855. } else {
  856. $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
  857. return -2;
  858. }
  859. }
  860. /**
  861. * Create commercial proposal into database
  862. * this->ref can be set or empty. If empty, we will use "(PROVid)"
  863. *
  864. * @param User $user User that create
  865. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  866. * @return int <0 if KO, >=0 if OK
  867. */
  868. public function create($user, $notrigger = 0)
  869. {
  870. global $conf, $hookmanager;
  871. $error = 0;
  872. $now = dol_now();
  873. // Clean parameters
  874. if (empty($this->date)) {
  875. $this->date = $this->datep;
  876. }
  877. $this->fin_validite = $this->date + ($this->duree_validite * 24 * 3600);
  878. if (empty($this->availability_id)) {
  879. $this->availability_id = 0;
  880. }
  881. if (empty($this->demand_reason_id)) {
  882. $this->demand_reason_id = 0;
  883. }
  884. // Multicurrency (test on $this->multicurrency_tx because we should take the default rate only if not using origin rate)
  885. if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) {
  886. list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code, $this->date);
  887. } else {
  888. $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code);
  889. }
  890. if (empty($this->fk_multicurrency)) {
  891. $this->multicurrency_code = $conf->currency;
  892. $this->fk_multicurrency = 0;
  893. $this->multicurrency_tx = 1;
  894. }
  895. // Set tmp vars
  896. $delivery_date = empty($this->delivery_date) ? $this->date_livraison : $this->delivery_date;
  897. dol_syslog(get_class($this)."::create");
  898. // Check parameters
  899. $result = $this->fetch_thirdparty();
  900. if ($result < 0) {
  901. $this->error = "Failed to fetch company";
  902. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  903. return -3;
  904. }
  905. // Check parameters
  906. if (!empty($this->ref)) { // We check that ref is not already used
  907. $result = self::isExistingObject($this->element, 0, $this->ref); // Check ref is not yet used
  908. if ($result > 0) {
  909. $this->error = 'ErrorRefAlreadyExists';
  910. dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING);
  911. $this->db->rollback();
  912. return -1;
  913. }
  914. }
  915. if (empty($this->date)) {
  916. $this->error = "Date of proposal is required";
  917. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  918. return -4;
  919. }
  920. $this->db->begin();
  921. // Insert into database
  922. $sql = "INSERT INTO ".MAIN_DB_PREFIX."propal (";
  923. $sql .= "fk_soc";
  924. $sql .= ", price";
  925. $sql .= ", remise";
  926. $sql .= ", remise_percent";
  927. $sql .= ", remise_absolue";
  928. $sql .= ", total_tva";
  929. $sql .= ", total_ttc";
  930. $sql .= ", datep";
  931. $sql .= ", datec";
  932. $sql .= ", ref";
  933. $sql .= ", fk_user_author";
  934. $sql .= ", note_private";
  935. $sql .= ", note_public";
  936. $sql .= ", model_pdf";
  937. $sql .= ", fin_validite";
  938. $sql .= ", fk_cond_reglement";
  939. $sql .= ", fk_mode_reglement";
  940. $sql .= ", fk_account";
  941. $sql .= ", ref_client";
  942. $sql .= ", date_livraison";
  943. $sql .= ", fk_shipping_method";
  944. $sql .= ", fk_warehouse";
  945. $sql .= ", fk_availability";
  946. $sql .= ", fk_input_reason";
  947. $sql .= ", fk_projet";
  948. $sql .= ", fk_incoterms";
  949. $sql .= ", location_incoterms";
  950. $sql .= ", entity";
  951. $sql .= ", fk_multicurrency";
  952. $sql .= ", multicurrency_code";
  953. $sql .= ", multicurrency_tx";
  954. $sql .= ") ";
  955. $sql .= " VALUES (";
  956. $sql .= $this->socid;
  957. $sql .= ", 0";
  958. $sql .= ", ".((float) $this->remise); // deprecated
  959. $sql .= ", ".($this->remise_percent ? ((float) $this->remise_percent) : 'NULL');
  960. $sql .= ", ".($this->remise_absolue ? ((float) $this->remise_absolue) : 'NULL'); // deprecated
  961. $sql .= ", 0";
  962. $sql .= ", 0";
  963. $sql .= ", '".$this->db->idate($this->date)."'";
  964. $sql .= ", '".$this->db->idate($now)."'";
  965. $sql .= ", '(PROV)'";
  966. $sql .= ", ".($user->id > 0 ? ((int) $user->id) : "NULL");
  967. $sql .= ", '".$this->db->escape($this->note_private)."'";
  968. $sql .= ", '".$this->db->escape($this->note_public)."'";
  969. $sql .= ", '".$this->db->escape($this->model_pdf)."'";
  970. $sql .= ", ".($this->fin_validite != '' ? "'".$this->db->idate($this->fin_validite)."'" : "NULL");
  971. $sql .= ", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) : 'NULL');
  972. $sql .= ", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) : 'NULL');
  973. $sql .= ", ".($this->fk_account > 0 ? ((int) $this->fk_account) : 'NULL');
  974. $sql .= ", '".$this->db->escape($this->ref_client)."'";
  975. $sql .= ", ".(empty($delivery_date) ? "NULL" : "'".$this->db->idate($delivery_date)."'");
  976. $sql .= ", ".($this->shipping_method_id > 0 ? $this->shipping_method_id : 'NULL');
  977. $sql .= ", ".($this->warehouse_id > 0 ? $this->warehouse_id : 'NULL');
  978. $sql .= ", ".$this->availability_id;
  979. $sql .= ", ".$this->demand_reason_id;
  980. $sql .= ", ".($this->fk_project ? $this->fk_project : "null");
  981. $sql .= ", ".(int) $this->fk_incoterms;
  982. $sql .= ", '".$this->db->escape($this->location_incoterms)."'";
  983. $sql .= ", ".setEntity($this);
  984. $sql .= ", ".(int) $this->fk_multicurrency;
  985. $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
  986. $sql .= ", ".(double) $this->multicurrency_tx;
  987. $sql .= ")";
  988. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  989. $resql = $this->db->query($sql);
  990. if ($resql) {
  991. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."propal");
  992. if ($this->id) {
  993. $this->ref = '(PROV'.$this->id.')';
  994. $sql = 'UPDATE '.MAIN_DB_PREFIX."propal SET ref='".$this->db->escape($this->ref)."' WHERE rowid=".((int) $this->id);
  995. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  996. $resql = $this->db->query($sql);
  997. if (!$resql) {
  998. $error++;
  999. }
  1000. if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
  1001. $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
  1002. }
  1003. // Add object linked
  1004. if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
  1005. foreach ($this->linked_objects as $origin => $tmp_origin_id) {
  1006. 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, ...))
  1007. foreach ($tmp_origin_id as $origin_id) {
  1008. $ret = $this->add_object_linked($origin, $origin_id);
  1009. if (!$ret) {
  1010. $this->error = $this->db->lasterror();
  1011. $error++;
  1012. }
  1013. }
  1014. } else // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
  1015. {
  1016. $origin_id = $tmp_origin_id;
  1017. $ret = $this->add_object_linked($origin, $origin_id);
  1018. if (!$ret) {
  1019. $this->error = $this->db->lasterror();
  1020. $error++;
  1021. }
  1022. }
  1023. }
  1024. }
  1025. /*
  1026. * Insertion du detail des produits dans la base
  1027. * Insert products detail in database
  1028. */
  1029. if (!$error) {
  1030. $fk_parent_line = 0;
  1031. $num = count($this->lines);
  1032. for ($i = 0; $i < $num; $i++) {
  1033. if (!is_object($this->lines[$i])) { // If this->lines is not array of objects, coming from REST API
  1034. // Convert into object this->lines[$i].
  1035. $line = (object) $this->lines[$i];
  1036. } else {
  1037. $line = $this->lines[$i];
  1038. }
  1039. // Reset fk_parent_line for line that are not child lines or special product
  1040. if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
  1041. $fk_parent_line = 0;
  1042. }
  1043. // Complete vat rate with code
  1044. $vatrate = $line->tva_tx;
  1045. if ($line->vat_src_code && !preg_match('/\(.*\)/', $vatrate)) {
  1046. $vatrate .= ' ('.$line->vat_src_code.')';
  1047. }
  1048. if (!empty($conf->global->MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION)) {
  1049. $originid = $line->origin_id;
  1050. $origintype = $line->origin;
  1051. } else {
  1052. $originid = $line->id;
  1053. $origintype = $this->element;
  1054. }
  1055. $result = $this->addline(
  1056. $line->desc,
  1057. $line->subprice,
  1058. $line->qty,
  1059. $vatrate,
  1060. $line->localtax1_tx,
  1061. $line->localtax2_tx,
  1062. $line->fk_product,
  1063. $line->remise_percent,
  1064. 'HT',
  1065. 0,
  1066. $line->info_bits,
  1067. $line->product_type,
  1068. $line->rang,
  1069. $line->special_code,
  1070. $fk_parent_line,
  1071. $line->fk_fournprice,
  1072. $line->pa_ht,
  1073. $line->label,
  1074. $line->date_start,
  1075. $line->date_end,
  1076. $line->array_options,
  1077. $line->fk_unit,
  1078. $origintype,
  1079. $originid
  1080. );
  1081. if ($result < 0) {
  1082. $error++;
  1083. $this->error = $this->db->error;
  1084. dol_print_error($this->db);
  1085. break;
  1086. }
  1087. // Defined the new fk_parent_line
  1088. if ($result > 0 && $line->product_type == 9) {
  1089. $fk_parent_line = $result;
  1090. }
  1091. }
  1092. }
  1093. // Set delivery address
  1094. /*if (! $error && $this->fk_delivery_address)
  1095. {
  1096. $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
  1097. $sql.= " SET fk_delivery_address = ".((int) $this->fk_delivery_address);
  1098. $sql.= " WHERE ref = '".$this->db->escape($this->ref)."'";
  1099. $sql.= " AND entity = ".setEntity($this);
  1100. $result=$this->db->query($sql);
  1101. }*/
  1102. if (!$error) {
  1103. // Mise a jour infos denormalisees
  1104. $resql = $this->update_price(1);
  1105. if ($resql) {
  1106. $action = 'update';
  1107. // Actions on extra fields
  1108. if (!$error) {
  1109. $result = $this->insertExtraFields();
  1110. if ($result < 0) {
  1111. $error++;
  1112. }
  1113. }
  1114. if (!$error && !$notrigger) {
  1115. // Call trigger
  1116. $result = $this->call_trigger('PROPAL_CREATE', $user);
  1117. if ($result < 0) {
  1118. $error++;
  1119. }
  1120. // End call triggers
  1121. }
  1122. } else {
  1123. $this->error = $this->db->lasterror();
  1124. $error++;
  1125. }
  1126. }
  1127. } else {
  1128. $this->error = $this->db->lasterror();
  1129. $error++;
  1130. }
  1131. if (!$error) {
  1132. $this->db->commit();
  1133. dol_syslog(get_class($this)."::create done id=".$this->id);
  1134. return $this->id;
  1135. } else {
  1136. $this->db->rollback();
  1137. return -2;
  1138. }
  1139. } else {
  1140. $this->error = $this->db->lasterror();
  1141. $this->db->rollback();
  1142. return -1;
  1143. }
  1144. }
  1145. /**
  1146. * Load an object from its id and create a new one in database
  1147. *
  1148. * @param User $user User making the clone
  1149. * @param int $socid Id of thirdparty
  1150. * @param int $forceentity Entity id to force
  1151. * @return int New id of clone
  1152. */
  1153. public function createFromClone(User $user, $socid = 0, $forceentity = null)
  1154. {
  1155. global $conf, $hookmanager;
  1156. dol_include_once('/projet/class/project.class.php');
  1157. $error = 0;
  1158. $now = dol_now();
  1159. dol_syslog(__METHOD__, LOG_DEBUG);
  1160. $object = new self($this->db);
  1161. $this->db->begin();
  1162. // Load source object
  1163. $object->fetch($this->id);
  1164. $objsoc = new Societe($this->db);
  1165. // Change socid if needed
  1166. if (!empty($socid) && $socid != $object->socid) {
  1167. if ($objsoc->fetch($socid) > 0) {
  1168. $object->socid = $objsoc->id;
  1169. $object->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
  1170. $object->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
  1171. $object->fk_delivery_address = '';
  1172. /*if (!empty($conf->projet->enabled))
  1173. {
  1174. $project = new Project($db);
  1175. if ($this->fk_project > 0 && $project->fetch($this->fk_project)) {
  1176. if ($project->socid <= 0) $clonedObj->fk_project = $this->fk_project;
  1177. else $clonedObj->fk_project = '';
  1178. } else {
  1179. $clonedObj->fk_project = '';
  1180. }
  1181. }*/
  1182. $object->fk_project = ''; // A cloned proposal is set by default to no project.
  1183. }
  1184. // reset ref_client
  1185. $object->ref_client = '';
  1186. // TODO Change product price if multi-prices
  1187. } else {
  1188. $objsoc->fetch($object->socid);
  1189. }
  1190. $object->id = 0;
  1191. $object->ref = '';
  1192. $object->entity = (!empty($forceentity) ? $forceentity : $object->entity);
  1193. $object->statut = self::STATUS_DRAFT;
  1194. // Clear fields
  1195. $object->user_author = $user->id;
  1196. $object->user_valid = '';
  1197. $object->date = $now;
  1198. $object->datep = $now; // deprecated
  1199. $object->fin_validite = $object->date + ($object->duree_validite * 24 * 3600);
  1200. if (empty($conf->global->MAIN_KEEP_REF_CUSTOMER_ON_CLONING)) {
  1201. $object->ref_client = '';
  1202. }
  1203. if ($conf->global->MAIN_DONT_KEEP_NOTE_ON_CLONING == 1) {
  1204. $object->note_private = '';
  1205. $object->note_public = '';
  1206. }
  1207. // Create clone
  1208. $object->context['createfromclone'] = 'createfromclone';
  1209. $result = $object->create($user);
  1210. if ($result < 0) {
  1211. $this->error = $object->error;
  1212. $this->errors = array_merge($this->errors, $object->errors);
  1213. $error++;
  1214. }
  1215. if (!$error) {
  1216. // copy internal contacts
  1217. if ($object->copy_linked_contact($this, 'internal') < 0) {
  1218. $error++;
  1219. }
  1220. }
  1221. if (!$error) {
  1222. // copy external contacts if same company
  1223. if ($this->socid == $object->socid) {
  1224. if ($object->copy_linked_contact($this, 'external') < 0) {
  1225. $error++;
  1226. }
  1227. }
  1228. }
  1229. if (!$error) {
  1230. // Hook of thirdparty module
  1231. if (is_object($hookmanager)) {
  1232. $parameters = array('objFrom'=>$this, 'clonedObj'=>$object);
  1233. $action = '';
  1234. $reshook = $hookmanager->executeHooks('createFrom', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1235. if ($reshook < 0) {
  1236. $error++;
  1237. }
  1238. }
  1239. }
  1240. unset($object->context['createfromclone']);
  1241. // End
  1242. if (!$error) {
  1243. $this->db->commit();
  1244. return $object->id;
  1245. } else {
  1246. $this->db->rollback();
  1247. return -1;
  1248. }
  1249. }
  1250. /**
  1251. * Load a proposal from database. Get also lines.
  1252. *
  1253. * @param int $rowid id of object to load
  1254. * @param string $ref Ref of proposal
  1255. * @param string $ref_ext Ref ext of proposal
  1256. * @return int >0 if OK, <0 if KO
  1257. */
  1258. public function fetch($rowid, $ref = '', $ref_ext = '')
  1259. {
  1260. $sql = "SELECT p.rowid, p.ref, p.entity, p.remise, p.remise_percent, p.remise_absolue, p.fk_soc";
  1261. $sql .= ", p.total_ttc, p.total_tva, p.localtax1, p.localtax2, p.total_ht";
  1262. $sql .= ", p.datec";
  1263. $sql .= ", p.date_valid as datev";
  1264. $sql .= ", p.datep as dp";
  1265. $sql .= ", p.fin_validite as dfv";
  1266. $sql .= ", p.date_livraison as delivery_date";
  1267. $sql .= ", p.model_pdf, p.last_main_doc, p.ref_client, p.extraparams";
  1268. $sql .= ", p.note_private, p.note_public";
  1269. $sql .= ", p.fk_projet as fk_project, p.fk_statut";
  1270. $sql .= ", p.fk_user_author, p.fk_user_valid, p.fk_user_cloture";
  1271. $sql .= ", p.fk_delivery_address";
  1272. $sql .= ", p.fk_availability";
  1273. $sql .= ", p.fk_input_reason";
  1274. $sql .= ", p.fk_cond_reglement";
  1275. $sql .= ", p.fk_mode_reglement";
  1276. $sql .= ', p.fk_account';
  1277. $sql .= ", p.fk_shipping_method";
  1278. $sql .= ", p.fk_warehouse";
  1279. $sql .= ", p.fk_incoterms, p.location_incoterms";
  1280. $sql .= ", p.fk_multicurrency, p.multicurrency_code, p.multicurrency_tx, p.multicurrency_total_ht, p.multicurrency_total_tva, p.multicurrency_total_ttc";
  1281. $sql .= ", p.tms as date_modification";
  1282. $sql .= ", i.libelle as label_incoterms";
  1283. $sql .= ", c.label as statut_label";
  1284. $sql .= ", ca.code as availability_code, ca.label as availability";
  1285. $sql .= ", dr.code as demand_reason_code, dr.label as demand_reason";
  1286. $sql .= ", cr.code as cond_reglement_code, cr.libelle as cond_reglement, cr.libelle_facture as cond_reglement_libelle_doc";
  1287. $sql .= ", cp.code as mode_reglement_code, cp.libelle as mode_reglement";
  1288. $sql .= " FROM ".MAIN_DB_PREFIX."propal as p";
  1289. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_propalst as c ON p.fk_statut = c.id';
  1290. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as cp ON p.fk_mode_reglement = cp.id AND cp.entity IN ('.getEntity('c_paiement').')';
  1291. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as cr ON p.fk_cond_reglement = cr.rowid AND cr.entity IN ('.getEntity('c_payment_term').')';
  1292. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_availability as ca ON p.fk_availability = ca.rowid';
  1293. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_input_reason as dr ON p.fk_input_reason = dr.rowid';
  1294. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_incoterms as i ON p.fk_incoterms = i.rowid';
  1295. if ($ref) {
  1296. $sql .= " WHERE p.entity IN (".getEntity('propal').")"; // Dont't use entity if you use rowid
  1297. $sql .= " AND p.ref='".$this->db->escape($ref)."'";
  1298. } else {
  1299. $sql .= " WHERE p.rowid = ".((int) $rowid);
  1300. }
  1301. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  1302. $resql = $this->db->query($sql);
  1303. if ($resql) {
  1304. if ($this->db->num_rows($resql)) {
  1305. $obj = $this->db->fetch_object($resql);
  1306. $this->id = $obj->rowid;
  1307. $this->entity = $obj->entity;
  1308. $this->ref = $obj->ref;
  1309. $this->ref_client = $obj->ref_client;
  1310. $this->remise = $obj->remise;
  1311. $this->remise_percent = $obj->remise_percent;
  1312. $this->remise_absolue = $obj->remise_absolue;
  1313. $this->total = $obj->total_ttc; // TODO deprecated
  1314. $this->total_ttc = $obj->total_ttc;
  1315. $this->total_ht = $obj->total_ht;
  1316. $this->total_tva = $obj->total_tva;
  1317. $this->total_localtax1 = $obj->localtax1;
  1318. $this->total_localtax2 = $obj->localtax2;
  1319. $this->socid = $obj->fk_soc;
  1320. $this->thirdparty = null; // Clear if another value was already set by fetch_thirdparty
  1321. $this->fk_project = $obj->fk_project;
  1322. $this->project = null; // Clear if another value was already set by fetch_projet
  1323. $this->model_pdf = $obj->model_pdf;
  1324. $this->modelpdf = $obj->model_pdf; // deprecated
  1325. $this->last_main_doc = $obj->last_main_doc;
  1326. $this->note = $obj->note_private; // TODO deprecated
  1327. $this->note_private = $obj->note_private;
  1328. $this->note_public = $obj->note_public;
  1329. $this->status = (int) $obj->fk_statut;
  1330. $this->statut = $this->status; // deprecated
  1331. $this->statut_libelle = $obj->statut_label;
  1332. $this->datec = $this->db->jdate($obj->datec); // TODO deprecated
  1333. $this->datev = $this->db->jdate($obj->datev); // TODO deprecated
  1334. $this->date_creation = $this->db->jdate($obj->datec); //Creation date
  1335. $this->date_validation = $this->db->jdate($obj->datev); //Validation date
  1336. $this->date_modification = $this->db->jdate($obj->date_modification); // tms
  1337. $this->date = $this->db->jdate($obj->dp); // Proposal date
  1338. $this->datep = $this->db->jdate($obj->dp); // deprecated
  1339. $this->fin_validite = $this->db->jdate($obj->dfv);
  1340. $this->date_livraison = $this->db->jdate($obj->delivery_date); // deprecated
  1341. $this->delivery_date = $this->db->jdate($obj->delivery_date);
  1342. $this->shipping_method_id = ($obj->fk_shipping_method > 0) ? $obj->fk_shipping_method : null;
  1343. $this->warehouse_id = ($obj->fk_warehouse > 0) ? $obj->fk_warehouse : null;
  1344. $this->availability_id = $obj->fk_availability;
  1345. $this->availability_code = $obj->availability_code;
  1346. $this->availability = $obj->availability;
  1347. $this->demand_reason_id = $obj->fk_input_reason;
  1348. $this->demand_reason_code = $obj->demand_reason_code;
  1349. $this->demand_reason = $obj->demand_reason;
  1350. $this->fk_address = $obj->fk_delivery_address;
  1351. $this->mode_reglement_id = $obj->fk_mode_reglement;
  1352. $this->mode_reglement_code = $obj->mode_reglement_code;
  1353. $this->mode_reglement = $obj->mode_reglement;
  1354. $this->fk_account = ($obj->fk_account > 0) ? $obj->fk_account : null;
  1355. $this->cond_reglement_id = $obj->fk_cond_reglement;
  1356. $this->cond_reglement_code = $obj->cond_reglement_code;
  1357. $this->cond_reglement = $obj->cond_reglement;
  1358. $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
  1359. $this->extraparams = (array) json_decode($obj->extraparams, true);
  1360. $this->user_author_id = $obj->fk_user_author;
  1361. $this->user_valid_id = $obj->fk_user_valid;
  1362. $this->user_close_id = $obj->fk_user_cloture;
  1363. //Incoterms
  1364. $this->fk_incoterms = $obj->fk_incoterms;
  1365. $this->location_incoterms = $obj->location_incoterms;
  1366. $this->label_incoterms = $obj->label_incoterms;
  1367. // Multicurrency
  1368. $this->fk_multicurrency = $obj->fk_multicurrency;
  1369. $this->multicurrency_code = $obj->multicurrency_code;
  1370. $this->multicurrency_tx = $obj->multicurrency_tx;
  1371. $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
  1372. $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
  1373. $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
  1374. if ($obj->fk_statut == self::STATUS_DRAFT) {
  1375. $this->brouillon = 1;
  1376. }
  1377. // Retrieve all extrafield
  1378. // fetch optionals attributes and labels
  1379. $this->fetch_optionals();
  1380. $this->db->free($resql);
  1381. $this->lines = array();
  1382. // Lines
  1383. $result = $this->fetch_lines();
  1384. if ($result < 0) {
  1385. return -3;
  1386. }
  1387. return 1;
  1388. }
  1389. $this->error = "Record Not Found";
  1390. return 0;
  1391. } else {
  1392. $this->error = $this->db->lasterror();
  1393. return -1;
  1394. }
  1395. }
  1396. /**
  1397. * Update database
  1398. *
  1399. * @param User $user User that modify
  1400. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  1401. * @return int <0 if KO, >0 if OK
  1402. */
  1403. public function update(User $user, $notrigger = 0)
  1404. {
  1405. global $conf;
  1406. $error = 0;
  1407. // Clean parameters
  1408. if (isset($this->ref)) {
  1409. $this->ref = trim($this->ref);
  1410. }
  1411. if (isset($this->ref_client)) {
  1412. $this->ref_client = trim($this->ref_client);
  1413. }
  1414. if (isset($this->note) || isset($this->note_private)) {
  1415. $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
  1416. }
  1417. if (isset($this->note_public)) {
  1418. $this->note_public = trim($this->note_public);
  1419. }
  1420. if (isset($this->model_pdf)) {
  1421. $this->model_pdf = trim($this->model_pdf);
  1422. }
  1423. if (isset($this->import_key)) {
  1424. $this->import_key = trim($this->import_key);
  1425. }
  1426. if (!empty($this->duree_validite) && is_numeric($this->duree_validite)) {
  1427. $this->fin_validite = $this->date + ($this->duree_validite * 24 * 3600);
  1428. }
  1429. // Check parameters
  1430. // Put here code to add control on parameters values
  1431. // Update request
  1432. $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET";
  1433. $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
  1434. $sql .= " ref_client=".(isset($this->ref_client) ? "'".$this->db->escape($this->ref_client)."'" : "null").",";
  1435. $sql .= " ref_ext=".(isset($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : "null").",";
  1436. $sql .= " fk_soc=".(isset($this->socid) ? $this->socid : "null").",";
  1437. $sql .= " datep=".(strval($this->date) != '' ? "'".$this->db->idate($this->date)."'" : 'null').",";
  1438. if (!empty($this->fin_validite)) {
  1439. $sql .= " fin_validite=".(strval($this->fin_validite) != '' ? "'".$this->db->idate($this->fin_validite)."'" : 'null').",";
  1440. }
  1441. $sql .= " date_valid=".(strval($this->date_validation) != '' ? "'".$this->db->idate($this->date_validation)."'" : 'null').",";
  1442. $sql .= " total_tva=".(isset($this->total_tva) ? $this->total_tva : "null").",";
  1443. $sql .= " localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 : "null").",";
  1444. $sql .= " localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 : "null").",";
  1445. $sql .= " total_ht=".(isset($this->total_ht) ? $this->total_ht : "null").",";
  1446. $sql .= " total_ttc=".(isset($this->total_ttc) ? $this->total_ttc : "null").",";
  1447. $sql .= " fk_statut=".(isset($this->statut) ? $this->statut : "null").",";
  1448. $sql .= " fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id : "null").",";
  1449. $sql .= " fk_user_valid=".(isset($this->user_valid) ? $this->user_valid : "null").",";
  1450. $sql .= " fk_projet=".(isset($this->fk_project) ? $this->fk_project : "null").",";
  1451. $sql .= " fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->cond_reglement_id : "null").",";
  1452. $sql .= " fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->mode_reglement_id : "null").",";
  1453. $sql .= " fk_input_reason=".(isset($this->demand_reason_id) ? $this->demand_reason_id : "null").",";
  1454. $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
  1455. $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
  1456. $sql .= " model_pdf=".(isset($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null").",";
  1457. $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null")."";
  1458. $sql .= " WHERE rowid=".((int) $this->id);
  1459. $this->db->begin();
  1460. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  1461. $resql = $this->db->query($sql);
  1462. if (!$resql) {
  1463. $error++;
  1464. $this->errors[] = "Error ".$this->db->lasterror();
  1465. }
  1466. if (!$error) {
  1467. $result = $this->insertExtraFields();
  1468. if ($result < 0) {
  1469. $error++;
  1470. }
  1471. }
  1472. if (!$error && !$notrigger) {
  1473. // Call trigger
  1474. $result = $this->call_trigger('PROPAL_MODIFY', $user);
  1475. if ($result < 0) {
  1476. $error++;
  1477. }
  1478. // End call triggers
  1479. }
  1480. // Commit or rollback
  1481. if ($error) {
  1482. foreach ($this->errors as $errmsg) {
  1483. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  1484. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  1485. }
  1486. $this->db->rollback();
  1487. return -1 * $error;
  1488. } else {
  1489. $this->db->commit();
  1490. return 1;
  1491. }
  1492. }
  1493. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1494. /**
  1495. * Load array lines
  1496. *
  1497. * @param int $only_product Return only physical products
  1498. * @param int $loadalsotranslation Return translation for products
  1499. * @param string $filters Filter on other fields
  1500. *
  1501. * @return int <0 if KO, >0 if OK
  1502. */
  1503. public function fetch_lines($only_product = 0, $loadalsotranslation = 0, $filters = '')
  1504. {
  1505. global $langs, $conf;
  1506. // phpcs:enable
  1507. $this->lines = array();
  1508. $sql = 'SELECT d.rowid, d.fk_propal, d.fk_parent_line, d.label as custom_label, d.description, d.price, d.vat_src_code, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.localtax1_type, d.localtax2_type, d.qty, d.fk_remise_except, d.remise_percent, d.subprice, d.fk_product,';
  1509. $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,';
  1510. $sql .= ' d.fk_unit,';
  1511. $sql .= ' p.ref as product_ref, p.description as product_desc, p.fk_product_type, p.label as product_label, p.tobatch as product_tobatch, p.barcode as product_barcode,';
  1512. $sql .= ' p.weight, p.weight_units, p.volume, p.volume_units,';
  1513. $sql .= ' d.date_start, d.date_end,';
  1514. $sql .= ' d.fk_multicurrency, d.multicurrency_code, d.multicurrency_subprice, d.multicurrency_total_ht, d.multicurrency_total_tva, d.multicurrency_total_ttc';
  1515. $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as d';
  1516. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON (d.fk_product = p.rowid)';
  1517. $sql .= ' WHERE d.fk_propal = '.((int) $this->id);
  1518. if ($only_product) {
  1519. $sql .= ' AND p.fk_product_type = 0';
  1520. }
  1521. if ($filters) {
  1522. $sql .= $filters;
  1523. }
  1524. $sql .= ' ORDER by d.rang';
  1525. dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
  1526. $result = $this->db->query($sql);
  1527. if ($result) {
  1528. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  1529. $num = $this->db->num_rows($result);
  1530. $i = 0;
  1531. while ($i < $num) {
  1532. $objp = $this->db->fetch_object($result);
  1533. $line = new PropaleLigne($this->db);
  1534. $line->rowid = $objp->rowid; //Deprecated
  1535. $line->id = $objp->rowid;
  1536. $line->fk_propal = $objp->fk_propal;
  1537. $line->fk_parent_line = $objp->fk_parent_line;
  1538. $line->product_type = $objp->product_type;
  1539. $line->label = $objp->custom_label;
  1540. $line->desc = $objp->description; // Description ligne
  1541. $line->description = $objp->description; // Description ligne
  1542. $line->qty = $objp->qty;
  1543. $line->vat_src_code = $objp->vat_src_code;
  1544. $line->tva_tx = $objp->tva_tx;
  1545. $line->localtax1_tx = $objp->localtax1_tx;
  1546. $line->localtax2_tx = $objp->localtax2_tx;
  1547. $line->localtax1_type = $objp->localtax1_type;
  1548. $line->localtax2_type = $objp->localtax2_type;
  1549. $line->subprice = $objp->subprice;
  1550. $line->fk_remise_except = $objp->fk_remise_except;
  1551. $line->remise_percent = $objp->remise_percent;
  1552. $line->price = $objp->price; // TODO deprecated
  1553. $line->info_bits = $objp->info_bits;
  1554. $line->total_ht = $objp->total_ht;
  1555. $line->total_tva = $objp->total_tva;
  1556. $line->total_localtax1 = $objp->total_localtax1;
  1557. $line->total_localtax2 = $objp->total_localtax2;
  1558. $line->total_ttc = $objp->total_ttc;
  1559. $line->fk_fournprice = $objp->fk_fournprice;
  1560. $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
  1561. $line->pa_ht = $marginInfos[0];
  1562. $line->marge_tx = $marginInfos[1];
  1563. $line->marque_tx = $marginInfos[2];
  1564. $line->special_code = $objp->special_code;
  1565. $line->rang = $objp->rang;
  1566. $line->fk_product = $objp->fk_product;
  1567. $line->ref = $objp->product_ref; // deprecated
  1568. $line->libelle = $objp->product_label; // deprecated
  1569. $line->product_ref = $objp->product_ref;
  1570. $line->product_label = $objp->product_label;
  1571. $line->product_desc = $objp->product_desc; // Description produit
  1572. $line->product_tobatch = $objp->product_tobatch;
  1573. $line->product_barcode = $objp->product_barcode;
  1574. $line->fk_product_type = $objp->fk_product_type; // deprecated
  1575. $line->fk_unit = $objp->fk_unit;
  1576. $line->weight = $objp->weight;
  1577. $line->weight_units = $objp->weight_units;
  1578. $line->volume = $objp->volume;
  1579. $line->volume_units = $objp->volume_units;
  1580. $line->date_start = $this->db->jdate($objp->date_start);
  1581. $line->date_end = $this->db->jdate($objp->date_end);
  1582. // Multicurrency
  1583. $line->fk_multicurrency = $objp->fk_multicurrency;
  1584. $line->multicurrency_code = $objp->multicurrency_code;
  1585. $line->multicurrency_subprice = $objp->multicurrency_subprice;
  1586. $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
  1587. $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
  1588. $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
  1589. $line->fetch_optionals();
  1590. // multilangs
  1591. if (!empty($conf->global->MAIN_MULTILANGS) && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
  1592. $line = new Product($this->db);
  1593. $line->fetch($objp->fk_product);
  1594. $line->getMultiLangs();
  1595. }
  1596. $this->lines[$i] = $line;
  1597. //dol_syslog("1 ".$line->fk_product);
  1598. //print "xx $i ".$this->lines[$i]->fk_product;
  1599. $i++;
  1600. }
  1601. $this->db->free($result);
  1602. return $num;
  1603. } else {
  1604. $this->error = $this->db->lasterror();
  1605. return -3;
  1606. }
  1607. }
  1608. /**
  1609. * Set status to validated
  1610. *
  1611. * @param User $user Object user that validate
  1612. * @param int $notrigger 1=Does not execute triggers, 0=execute triggers
  1613. * @return int <0 if KO, 0=Nothing done, >=0 if OK
  1614. */
  1615. public function valid($user, $notrigger = 0)
  1616. {
  1617. global $conf;
  1618. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1619. $error = 0;
  1620. // Protection
  1621. if ($this->statut == self::STATUS_VALIDATED) {
  1622. dol_syslog(get_class($this)."::valid action abandonned: already validated", LOG_WARNING);
  1623. return 0;
  1624. }
  1625. if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->propal->creer))
  1626. || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->propal->propal_advance->validate)))) {
  1627. $this->error = 'ErrorPermissionDenied';
  1628. dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
  1629. return -1;
  1630. }
  1631. $now = dol_now();
  1632. $this->db->begin();
  1633. // Numbering module definition
  1634. $soc = new Societe($this->db);
  1635. $soc->fetch($this->socid);
  1636. // Define new ref
  1637. if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
  1638. $num = $this->getNextNumRef($soc);
  1639. } else {
  1640. $num = $this->ref;
  1641. }
  1642. $this->newref = dol_sanitizeFileName($num);
  1643. $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
  1644. $sql .= " SET ref = '".$this->db->escape($num)."',";
  1645. $sql .= " fk_statut = ".self::STATUS_VALIDATED.", date_valid='".$this->db->idate($now)."', fk_user_valid=".((int) $user->id);
  1646. $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".self::STATUS_DRAFT;
  1647. dol_syslog(get_class($this)."::valid", LOG_DEBUG);
  1648. $resql = $this->db->query($sql);
  1649. if (!$resql) {
  1650. dol_print_error($this->db);
  1651. $error++;
  1652. }
  1653. // Trigger calls
  1654. if (!$error && !$notrigger) {
  1655. // Call trigger
  1656. $result = $this->call_trigger('PROPAL_VALIDATE', $user);
  1657. if ($result < 0) {
  1658. $error++;
  1659. }
  1660. // End call triggers
  1661. }
  1662. if (!$error) {
  1663. $this->oldref = $this->ref;
  1664. // Rename directory if dir was a temporary ref
  1665. if (preg_match('/^[\(]?PROV/i', $this->ref)) {
  1666. // Now we rename also files into index
  1667. $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'propale/".$this->db->escape($this->newref)."'";
  1668. $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'propale/".$this->db->escape($this->ref)."' and entity = ".((int) $conf->entity);
  1669. $resql = $this->db->query($sql);
  1670. if (!$resql) {
  1671. $error++;
  1672. $this->error = $this->db->lasterror();
  1673. }
  1674. // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
  1675. $oldref = dol_sanitizeFileName($this->ref);
  1676. $newref = dol_sanitizeFileName($num);
  1677. $dirsource = $conf->propal->multidir_output[$this->entity].'/'.$oldref;
  1678. $dirdest = $conf->propal->multidir_output[$this->entity].'/'.$newref;
  1679. if (!$error && file_exists($dirsource)) {
  1680. dol_syslog(get_class($this)."::validate rename dir ".$dirsource." into ".$dirdest);
  1681. if (@rename($dirsource, $dirdest)) {
  1682. dol_syslog("Rename ok");
  1683. // Rename docs starting with $oldref with $newref
  1684. $listoffiles = dol_dir_list($dirdest, 'files', 1, '^'.preg_quote($oldref, '/'));
  1685. foreach ($listoffiles as $fileentry) {
  1686. $dirsource = $fileentry['name'];
  1687. $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
  1688. $dirsource = $fileentry['path'].'/'.$dirsource;
  1689. $dirdest = $fileentry['path'].'/'.$dirdest;
  1690. @rename($dirsource, $dirdest);
  1691. }
  1692. }
  1693. }
  1694. }
  1695. $this->ref = $num;
  1696. $this->brouillon = 0;
  1697. $this->statut = self::STATUS_VALIDATED;
  1698. $this->user_valid_id = $user->id;
  1699. $this->datev = $now;
  1700. $this->db->commit();
  1701. return 1;
  1702. } else {
  1703. $this->db->rollback();
  1704. return -1;
  1705. }
  1706. }
  1707. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1708. /**
  1709. * Define proposal date
  1710. *
  1711. * @param User $user Object user that modify
  1712. * @param int $date Date
  1713. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1714. * @return int <0 if KO, >0 if OK
  1715. */
  1716. public function set_date($user, $date, $notrigger = 0)
  1717. {
  1718. // phpcs:enable
  1719. if (empty($date)) {
  1720. $this->error = 'ErrorBadParameter';
  1721. dol_syslog(get_class($this)."::set_date ".$this->error, LOG_ERR);
  1722. return -1;
  1723. }
  1724. if (!empty($user->rights->propal->creer)) {
  1725. $error = 0;
  1726. $this->db->begin();
  1727. $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET datep = '".$this->db->idate($date)."'";
  1728. $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".self::STATUS_DRAFT;
  1729. dol_syslog(__METHOD__, LOG_DEBUG);
  1730. $resql = $this->db->query($sql);
  1731. if (!$resql) {
  1732. $this->errors[] = $this->db->error();
  1733. $error++;
  1734. }
  1735. if (!$error) {
  1736. $this->oldcopy = clone $this;
  1737. $this->date = $date;
  1738. $this->datep = $date; // deprecated
  1739. }
  1740. if (!$notrigger && empty($error)) {
  1741. // Call trigger
  1742. $result = $this->call_trigger('PROPAL_MODIFY', $user);
  1743. if ($result < 0) {
  1744. $error++;
  1745. }
  1746. // End call triggers
  1747. }
  1748. if (!$error) {
  1749. $this->db->commit();
  1750. return 1;
  1751. } else {
  1752. foreach ($this->errors as $errmsg) {
  1753. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  1754. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  1755. }
  1756. $this->db->rollback();
  1757. return -1 * $error;
  1758. }
  1759. }
  1760. }
  1761. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1762. /**
  1763. * Define end validity date
  1764. *
  1765. * @param User $user Object user that modify
  1766. * @param int $date_fin_validite End of validity date
  1767. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1768. * @return int <0 if KO, >0 if OK
  1769. */
  1770. public function set_echeance($user, $date_fin_validite, $notrigger = 0)
  1771. {
  1772. // phpcs:enable
  1773. if (!empty($user->rights->propal->creer)) {
  1774. $error = 0;
  1775. $this->db->begin();
  1776. $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fin_validite = ".($date_fin_validite != '' ? "'".$this->db->idate($date_fin_validite)."'" : 'null');
  1777. $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".self::STATUS_DRAFT;
  1778. dol_syslog(__METHOD__, LOG_DEBUG);
  1779. $resql = $this->db->query($sql);
  1780. if (!$resql) {
  1781. $this->errors[] = $this->db->error();
  1782. $error++;
  1783. }
  1784. if (!$error) {
  1785. $this->oldcopy = clone $this;
  1786. $this->fin_validite = $date_fin_validite;
  1787. }
  1788. if (!$notrigger && empty($error)) {
  1789. // Call trigger
  1790. $result = $this->call_trigger('PROPAL_MODIFY', $user);
  1791. if ($result < 0) {
  1792. $error++;
  1793. }
  1794. // End call triggers
  1795. }
  1796. if (!$error) {
  1797. $this->db->commit();
  1798. return 1;
  1799. } else {
  1800. foreach ($this->errors as $errmsg) {
  1801. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  1802. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  1803. }
  1804. $this->db->rollback();
  1805. return -1 * $error;
  1806. }
  1807. }
  1808. }
  1809. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1810. /**
  1811. * Set delivery date
  1812. *
  1813. * @param User $user Object user that modify
  1814. * @param int $delivery_date Delivery date
  1815. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1816. * @return int <0 if ko, >0 if ok
  1817. * @deprecated Use setDeliveryDate
  1818. */
  1819. public function set_date_livraison($user, $delivery_date, $notrigger = 0)
  1820. {
  1821. // phpcs:enable
  1822. return $this->setDeliveryDate($user, $delivery_date, $notrigger);
  1823. }
  1824. /**
  1825. * Set delivery date
  1826. *
  1827. * @param User $user Object user that modify
  1828. * @param int $delivery_date Delivery date
  1829. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1830. * @return int <0 if ko, >0 if ok
  1831. */
  1832. public function setDeliveryDate($user, $delivery_date, $notrigger = 0)
  1833. {
  1834. if (!empty($user->rights->propal->creer)) {
  1835. $error = 0;
  1836. $this->db->begin();
  1837. $sql = "UPDATE ".MAIN_DB_PREFIX."propal ";
  1838. $sql .= " SET date_livraison = ".($delivery_date != '' ? "'".$this->db->idate($delivery_date)."'" : 'null');
  1839. $sql .= " WHERE rowid = ".((int) $this->id);
  1840. dol_syslog(__METHOD__, LOG_DEBUG);
  1841. $resql = $this->db->query($sql);
  1842. if (!$resql) {
  1843. $this->errors[] = $this->db->error();
  1844. $error++;
  1845. }
  1846. if (!$error) {
  1847. $this->oldcopy = clone $this;
  1848. $this->date_livraison = $delivery_date;
  1849. $this->delivery_date = $delivery_date;
  1850. }
  1851. if (!$notrigger && empty($error)) {
  1852. // Call trigger
  1853. $result = $this->call_trigger('PROPAL_MODIFY', $user);
  1854. if ($result < 0) {
  1855. $error++;
  1856. }
  1857. // End call triggers
  1858. }
  1859. if (!$error) {
  1860. $this->db->commit();
  1861. return 1;
  1862. } else {
  1863. foreach ($this->errors as $errmsg) {
  1864. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  1865. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  1866. }
  1867. $this->db->rollback();
  1868. return -1 * $error;
  1869. }
  1870. }
  1871. }
  1872. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1873. /**
  1874. * Set delivery
  1875. *
  1876. * @param User $user Object user that modify
  1877. * @param int $id Availability id
  1878. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1879. * @return int <0 if KO, >0 if OK
  1880. */
  1881. public function set_availability($user, $id, $notrigger = 0)
  1882. {
  1883. // phpcs:enable
  1884. if (!empty($user->rights->propal->creer) && $this->statut >= self::STATUS_DRAFT) {
  1885. $error = 0;
  1886. $this->db->begin();
  1887. $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
  1888. $sql .= " SET fk_availability = ".((int) $id);
  1889. $sql .= " WHERE rowid = ".((int) $this->id);
  1890. dol_syslog(__METHOD__.' availability('.$id.')', LOG_DEBUG);
  1891. $resql = $this->db->query($sql);
  1892. if (!$resql) {
  1893. $this->errors[] = $this->db->error();
  1894. $error++;
  1895. }
  1896. if (!$error) {
  1897. $this->oldcopy = clone $this;
  1898. $this->fk_availability = $id;
  1899. $this->availability_id = $id;
  1900. }
  1901. if (!$notrigger && empty($error)) {
  1902. // Call trigger
  1903. $result = $this->call_trigger('PROPAL_MODIFY', $user);
  1904. if ($result < 0) {
  1905. $error++;
  1906. }
  1907. // End call triggers
  1908. }
  1909. if (!$error) {
  1910. $this->db->commit();
  1911. return 1;
  1912. } else {
  1913. foreach ($this->errors as $errmsg) {
  1914. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  1915. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  1916. }
  1917. $this->db->rollback();
  1918. return -1 * $error;
  1919. }
  1920. } else {
  1921. $error_str = 'Propal status do not meet requirement '.$this->statut;
  1922. dol_syslog(__METHOD__.$error_str, LOG_ERR);
  1923. $this->error = $error_str;
  1924. $this->errors[] = $this->error;
  1925. return -2;
  1926. }
  1927. }
  1928. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1929. /**
  1930. * Set source of demand
  1931. *
  1932. * @param User $user Object user that modify
  1933. * @param int $id Input reason id
  1934. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1935. * @return int <0 if KO, >0 if OK
  1936. */
  1937. public function set_demand_reason($user, $id, $notrigger = 0)
  1938. {
  1939. // phpcs:enable
  1940. if (!empty($user->rights->propal->creer) && $this->statut >= self::STATUS_DRAFT) {
  1941. $error = 0;
  1942. $this->db->begin();
  1943. $sql = "UPDATE ".MAIN_DB_PREFIX."propal ";
  1944. $sql .= " SET fk_input_reason = ".((int) $id);
  1945. $sql .= " WHERE rowid = ".((int) $this->id);
  1946. dol_syslog(__METHOD__, LOG_DEBUG);
  1947. $resql = $this->db->query($sql);
  1948. if (!$resql) {
  1949. $this->errors[] = $this->db->error();
  1950. $error++;
  1951. }
  1952. if (!$error) {
  1953. $this->oldcopy = clone $this;
  1954. $this->fk_input_reason = $id;
  1955. $this->demand_reason_id = $id;
  1956. }
  1957. if (!$notrigger && empty($error)) {
  1958. // Call trigger
  1959. $result = $this->call_trigger('PROPAL_MODIFY', $user);
  1960. if ($result < 0) {
  1961. $error++;
  1962. }
  1963. // End call triggers
  1964. }
  1965. if (!$error) {
  1966. $this->db->commit();
  1967. return 1;
  1968. } else {
  1969. foreach ($this->errors as $errmsg) {
  1970. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  1971. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  1972. }
  1973. $this->db->rollback();
  1974. return -1 * $error;
  1975. }
  1976. } else {
  1977. $error_str = 'Propal status do not meet requirement '.$this->statut;
  1978. dol_syslog(__METHOD__.$error_str, LOG_ERR);
  1979. $this->error = $error_str;
  1980. $this->errors[] = $this->error;
  1981. return -2;
  1982. }
  1983. }
  1984. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1985. /**
  1986. * Set customer reference number
  1987. *
  1988. * @param User $user Object user that modify
  1989. * @param string $ref_client Customer reference
  1990. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1991. * @return int <0 if ko, >0 if ok
  1992. */
  1993. public function set_ref_client($user, $ref_client, $notrigger = 0)
  1994. {
  1995. // phpcs:enable
  1996. if (!empty($user->rights->propal->creer)) {
  1997. $error = 0;
  1998. $this->db->begin();
  1999. $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET ref_client = ".(empty($ref_client) ? 'NULL' : "'".$this->db->escape($ref_client)."'");
  2000. $sql .= " WHERE rowid = ".((int) $this->id);
  2001. dol_syslog(__METHOD__.' $this->id='.$this->id.', ref_client='.$ref_client, LOG_DEBUG);
  2002. $resql = $this->db->query($sql);
  2003. if (!$resql) {
  2004. $this->errors[] = $this->db->error();
  2005. $error++;
  2006. }
  2007. if (!$error) {
  2008. $this->oldcopy = clone $this;
  2009. $this->ref_client = $ref_client;
  2010. }
  2011. if (!$notrigger && empty($error)) {
  2012. // Call trigger
  2013. $result = $this->call_trigger('PROPAL_MODIFY', $user);
  2014. if ($result < 0) {
  2015. $error++;
  2016. }
  2017. // End call triggers
  2018. }
  2019. if (!$error) {
  2020. $this->db->commit();
  2021. return 1;
  2022. } else {
  2023. foreach ($this->errors as $errmsg) {
  2024. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  2025. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  2026. }
  2027. $this->db->rollback();
  2028. return -1 * $error;
  2029. }
  2030. } else {
  2031. return -1;
  2032. }
  2033. }
  2034. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2035. /**
  2036. * Set an overall discount on the proposal
  2037. *
  2038. * @param User $user Object user that modify
  2039. * @param double $remise Amount discount
  2040. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  2041. * @return int <0 if ko, >0 if ok
  2042. */
  2043. public function set_remise_percent($user, $remise, $notrigger = 0)
  2044. {
  2045. // phpcs:enable
  2046. $remise = trim($remise) ?trim($remise) : 0;
  2047. if (!empty($user->rights->propal->creer)) {
  2048. $remise = price2num($remise, 2);
  2049. $error = 0;
  2050. $this->db->begin();
  2051. $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET remise_percent = ".((float) $remise);
  2052. $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".self::STATUS_DRAFT;
  2053. dol_syslog(__METHOD__, LOG_DEBUG);
  2054. $resql = $this->db->query($sql);
  2055. if (!$resql) {
  2056. $this->errors[] = $this->db->error();
  2057. $error++;
  2058. }
  2059. if (!$error) {
  2060. $this->oldcopy = clone $this;
  2061. $this->remise_percent = $remise;
  2062. $this->update_price(1);
  2063. }
  2064. if (!$notrigger && empty($error)) {
  2065. // Call trigger
  2066. $result = $this->call_trigger('PROPAL_MODIFY', $user);
  2067. if ($result < 0) {
  2068. $error++;
  2069. }
  2070. // End call triggers
  2071. }
  2072. if (!$error) {
  2073. $this->db->commit();
  2074. return 1;
  2075. } else {
  2076. foreach ($this->errors as $errmsg) {
  2077. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  2078. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  2079. }
  2080. $this->db->rollback();
  2081. return -1 * $error;
  2082. }
  2083. }
  2084. }
  2085. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2086. /**
  2087. * Set an absolute overall discount on the proposal
  2088. *
  2089. * @param User $user Object user that modify
  2090. * @param double $remise Amount discount
  2091. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  2092. * @return int <0 if ko, >0 if ok
  2093. */
  2094. public function set_remise_absolue($user, $remise, $notrigger = 0)
  2095. {
  2096. // phpcs:enable
  2097. if (empty($remise)) {
  2098. $remise = 0;
  2099. }
  2100. $remise = price2num($remise);
  2101. if (!empty($user->rights->propal->creer)) {
  2102. $error = 0;
  2103. $this->db->begin();
  2104. $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
  2105. $sql .= " SET remise_absolue = ".((float) $remise);
  2106. $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".self::STATUS_DRAFT;
  2107. dol_syslog(__METHOD__, LOG_DEBUG);
  2108. $resql = $this->db->query($sql);
  2109. if (!$resql) {
  2110. $this->errors[] = $this->db->error();
  2111. $error++;
  2112. }
  2113. if (!$error) {
  2114. $this->oldcopy = clone $this;
  2115. $this->update_price(1);
  2116. }
  2117. if (!$notrigger && empty($error)) {
  2118. // Call trigger
  2119. $result = $this->call_trigger('PROPAL_MODIFY', $user);
  2120. if ($result < 0) {
  2121. $error++;
  2122. }
  2123. // End call triggers
  2124. }
  2125. if (!$error) {
  2126. $this->db->commit();
  2127. return 1;
  2128. } else {
  2129. foreach ($this->errors as $errmsg) {
  2130. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  2131. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  2132. }
  2133. $this->db->rollback();
  2134. return -1 * $error;
  2135. }
  2136. }
  2137. }
  2138. /**
  2139. * Reopen the commercial proposal
  2140. *
  2141. * @param User $user Object user that close
  2142. * @param int $status Status
  2143. * @param string $note Comment
  2144. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  2145. * @return int <0 if KO, >0 if OK
  2146. */
  2147. public function reopen($user, $status, $note = '', $notrigger = 0)
  2148. {
  2149. $error = 0;
  2150. $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
  2151. $sql .= " SET fk_statut = ".((int) $status).",";
  2152. if (!empty($note)) {
  2153. $sql .= " note_private = '".$this->db->escape($note)."',";
  2154. }
  2155. $sql .= " date_cloture=NULL, fk_user_cloture=NULL";
  2156. $sql .= " WHERE rowid = ".((int) $this->id);
  2157. $this->db->begin();
  2158. dol_syslog(get_class($this)."::reopen", LOG_DEBUG);
  2159. $resql = $this->db->query($sql);
  2160. if (!$resql) {
  2161. $error++;
  2162. $this->errors[] = "Error ".$this->db->lasterror();
  2163. }
  2164. if (!$error) {
  2165. if (!$notrigger) {
  2166. // Call trigger
  2167. $result = $this->call_trigger('PROPAL_REOPEN', $user);
  2168. if ($result < 0) {
  2169. $error++;
  2170. }
  2171. // End call triggers
  2172. }
  2173. }
  2174. // Commit or rollback
  2175. if ($error) {
  2176. if (!empty($this->errors)) {
  2177. foreach ($this->errors as $errmsg) {
  2178. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  2179. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  2180. }
  2181. }
  2182. $this->db->rollback();
  2183. return -1 * $error;
  2184. } else {
  2185. $this->statut = $status;
  2186. $this->status = $status;
  2187. $this->db->commit();
  2188. return 1;
  2189. }
  2190. }
  2191. /**
  2192. * Close/set the commercial proposal to status signed or refused (fill also date signature)
  2193. *
  2194. * @param User $user Object user that close
  2195. * @param int $status Status (self::STATUS_BILLED or self::STATUS_REFUSED)
  2196. * @param string $note Complete private note with this note
  2197. * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
  2198. * @return int <0 if KO, >0 if OK
  2199. */
  2200. public function closeProposal($user, $status, $note = '', $notrigger = 0)
  2201. {
  2202. global $langs,$conf;
  2203. $error = 0;
  2204. $now = dol_now();
  2205. $this->db->begin();
  2206. $newprivatenote = dol_concatdesc($this->note_private, $note);
  2207. $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
  2208. $sql .= " SET fk_statut = ".((int) $status).", note_private = '".$this->db->escape($newprivatenote)."', date_signature='".$this->db->idate($now)."', fk_user_signature=".$user->id;
  2209. $sql .= " WHERE rowid = ".((int) $this->id);
  2210. $resql = $this->db->query($sql);
  2211. if ($resql) {
  2212. // Status self::STATUS_REFUSED by default
  2213. $modelpdf = !empty($conf->global->PROPALE_ADDON_PDF_ODT_CLOSED) ? $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED : $this->model_pdf;
  2214. $trigger_name = 'PROPAL_CLOSE_REFUSED';
  2215. if ($status == self::STATUS_SIGNED) { // Status self::STATUS_SIGNED
  2216. $trigger_name = 'PROPAL_CLOSE_SIGNED';
  2217. $modelpdf = !empty($conf->global->PROPALE_ADDON_PDF_ODT_TOBILL) ? $conf->global->PROPALE_ADDON_PDF_ODT_TOBILL : $this->model_pdf;
  2218. // The connected company is classified as a client
  2219. $soc=new Societe($this->db);
  2220. $soc->id = $this->socid;
  2221. $result = $soc->set_as_client();
  2222. if ($result < 0) {
  2223. $this->error=$this->db->lasterror();
  2224. $this->db->rollback();
  2225. return -2;
  2226. }
  2227. }
  2228. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  2229. // Define output language
  2230. $outputlangs = $langs;
  2231. if (!empty($conf->global->MAIN_MULTILANGS)) {
  2232. $outputlangs = new Translate("", $conf);
  2233. $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $this->thirdparty->default_lang);
  2234. $outputlangs->setDefaultLang($newlang);
  2235. }
  2236. // PDF
  2237. $hidedetails = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
  2238. $hidedesc = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
  2239. $hideref = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
  2240. //$ret=$object->fetch($id); // Reload to get new records
  2241. $this->generateDocument($modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  2242. }
  2243. if (!$error) {
  2244. $this->oldcopy= clone $this;
  2245. $this->statut = $status;
  2246. $this->status = $status;
  2247. $this->date_signature = $now;
  2248. $this->note_private = $newprivatenote;
  2249. }
  2250. if (!$notrigger && empty($error)) {
  2251. // Call trigger
  2252. $result=$this->call_trigger($trigger_name, $user);
  2253. if ($result < 0) {
  2254. $error++;
  2255. }
  2256. // End call triggers
  2257. }
  2258. if (!$error ) {
  2259. $this->db->commit();
  2260. return 1;
  2261. } else {
  2262. $this->statut = $this->oldcopy->statut;
  2263. $this->status = $this->oldcopy->statut;
  2264. $this->date_signature = $this->oldcopy->date_signature;
  2265. $this->note_private = $this->oldcopy->note_private;
  2266. $this->db->rollback();
  2267. return -1;
  2268. }
  2269. } else {
  2270. $this->error = $this->db->lasterror();
  2271. $this->db->rollback();
  2272. return -1;
  2273. }
  2274. }
  2275. /**
  2276. * Classify the proposal to status Billed
  2277. *
  2278. * @param User $user Object user
  2279. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  2280. * @param string $note Complete private note with this note
  2281. * @return int <0 if KO, 0 = nothing done, >0 if OK
  2282. */
  2283. public function classifyBilled(User $user, $notrigger = 0, $note = '')
  2284. {
  2285. global $conf, $langs;
  2286. $error = 0;
  2287. $now = dol_now();
  2288. $num = 0;
  2289. $triggerName = 'PROPAL_CLASSIFY_BILLED';
  2290. $this->db->begin();
  2291. $newprivatenote = dol_concatdesc($this->note_private, $note);
  2292. $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal SET fk_statut = '.self::STATUS_BILLED.", ";
  2293. $sql .= " note_private = '".$this->db->escape($newprivatenote)."', date_cloture='".$this->db->idate($now)."', fk_user_cloture=".((int) $user->id);
  2294. $sql .= ' WHERE rowid = '.((int) $this->id).' AND fk_statut = '.((int) self::STATUS_SIGNED);
  2295. dol_syslog(__METHOD__, LOG_DEBUG);
  2296. $resql = $this->db->query($sql);
  2297. if (!$resql) {
  2298. $this->errors[] = $this->db->error();
  2299. $error++;
  2300. } else {
  2301. $num = $this->db->affected_rows($resql);
  2302. }
  2303. if (!$error) {
  2304. $modelpdf = $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED ? $conf->global->PROPALE_ADDON_PDF_ODT_CLOSED : $this->model_pdf;
  2305. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  2306. // Define output language
  2307. $outputlangs = $langs;
  2308. if (!empty($conf->global->MAIN_MULTILANGS)) {
  2309. $outputlangs = new Translate("", $conf);
  2310. $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $this->thirdparty->default_lang);
  2311. $outputlangs->setDefaultLang($newlang);
  2312. }
  2313. // PDF
  2314. $hidedetails = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
  2315. $hidedesc = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
  2316. $hideref = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
  2317. //$ret=$object->fetch($id); // Reload to get new records
  2318. $this->generateDocument($modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  2319. }
  2320. $this->oldcopy = clone $this;
  2321. $this->statut = self::STATUS_BILLED;
  2322. $this->date_cloture = $now;
  2323. $this->note_private = $newprivatenote;
  2324. }
  2325. if (!$notrigger && empty($error)) {
  2326. // Call trigger
  2327. $result = $this->call_trigger($triggerName, $user);
  2328. if ($result < 0) {
  2329. $error++;
  2330. }
  2331. // End call triggers
  2332. }
  2333. if (!$error) {
  2334. $this->db->commit();
  2335. return $num;
  2336. } else {
  2337. foreach ($this->errors as $errmsg) {
  2338. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  2339. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  2340. }
  2341. $this->db->rollback();
  2342. return -1 * $error;
  2343. }
  2344. }
  2345. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2346. /**
  2347. * Set draft status
  2348. *
  2349. * @param User $user Object user that modify
  2350. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  2351. * @return int <0 if KO, >0 if OK
  2352. */
  2353. public function setDraft($user, $notrigger = 0)
  2354. {
  2355. // phpcs:enable
  2356. $error = 0;
  2357. // Protection
  2358. if ($this->statut <= self::STATUS_DRAFT) {
  2359. return 0;
  2360. }
  2361. dol_syslog(get_class($this)."::setDraft", LOG_DEBUG);
  2362. $this->db->begin();
  2363. $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
  2364. $sql .= " SET fk_statut = ".self::STATUS_DRAFT;
  2365. $sql .= " WHERE rowid = ".((int) $this->id);
  2366. $resql = $this->db->query($sql);
  2367. if (!$resql) {
  2368. $this->errors[] = $this->db->error();
  2369. $error++;
  2370. }
  2371. if (!$error) {
  2372. $this->oldcopy = clone $this;
  2373. }
  2374. if (!$notrigger && empty($error)) {
  2375. // Call trigger
  2376. $result = $this->call_trigger('PROPAL_MODIFY', $user);
  2377. if ($result < 0) {
  2378. $error++;
  2379. }
  2380. // End call triggers
  2381. }
  2382. if (!$error) {
  2383. $this->statut = self::STATUS_DRAFT;
  2384. $this->brouillon = 1;
  2385. $this->db->commit();
  2386. return 1;
  2387. } else {
  2388. foreach ($this->errors as $errmsg) {
  2389. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  2390. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  2391. }
  2392. $this->db->rollback();
  2393. return -1 * $error;
  2394. }
  2395. }
  2396. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2397. /**
  2398. * Return list of proposal (eventually filtered on user) into an array
  2399. *
  2400. * @param int $shortlist 0=Return array[id]=ref, 1=Return array[](id=>id,ref=>ref,name=>name)
  2401. * @param int $draft 0=not draft, 1=draft
  2402. * @param int $notcurrentuser 0=all user, 1=not current user
  2403. * @param int $socid Id third pary
  2404. * @param int $limit For pagination
  2405. * @param int $offset For pagination
  2406. * @param string $sortfield Sort criteria
  2407. * @param string $sortorder Sort order
  2408. * @return int -1 if KO, array with result if OK
  2409. */
  2410. public function liste_array($shortlist = 0, $draft = 0, $notcurrentuser = 0, $socid = 0, $limit = 0, $offset = 0, $sortfield = 'p.datep', $sortorder = 'DESC')
  2411. {
  2412. // phpcs:enable
  2413. global $user;
  2414. $ga = array();
  2415. $sql = "SELECT s.rowid, s.nom as name, s.client,";
  2416. $sql .= " p.rowid as propalid, p.fk_statut, p.total_ht, p.ref, p.remise, ";
  2417. $sql .= " p.datep as dp, p.fin_validite as datelimite";
  2418. if (empty($user->rights->societe->client->voir) && !$socid) {
  2419. $sql .= ", sc.fk_soc, sc.fk_user";
  2420. }
  2421. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."propal as p, ".MAIN_DB_PREFIX."c_propalst as c";
  2422. if (empty($user->rights->societe->client->voir) && !$socid) {
  2423. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  2424. }
  2425. $sql .= " WHERE p.entity IN (".getEntity('propal').")";
  2426. $sql .= " AND p.fk_soc = s.rowid";
  2427. $sql .= " AND p.fk_statut = c.id";
  2428. if (empty($user->rights->societe->client->voir) && !$socid) { //restriction
  2429. $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
  2430. }
  2431. if ($socid) {
  2432. $sql .= " AND s.rowid = ".((int) $socid);
  2433. }
  2434. if ($draft) {
  2435. $sql .= " AND p.fk_statut = ".self::STATUS_DRAFT;
  2436. }
  2437. if ($notcurrentuser > 0) {
  2438. $sql .= " AND p.fk_user_author <> ".((int) $user->id);
  2439. }
  2440. $sql .= $this->db->order($sortfield, $sortorder);
  2441. $sql .= $this->db->plimit($limit, $offset);
  2442. $result = $this->db->query($sql);
  2443. if ($result) {
  2444. $num = $this->db->num_rows($result);
  2445. if ($num) {
  2446. $i = 0;
  2447. while ($i < $num) {
  2448. $obj = $this->db->fetch_object($result);
  2449. if ($shortlist == 1) {
  2450. $ga[$obj->propalid] = $obj->ref;
  2451. } elseif ($shortlist == 2) {
  2452. $ga[$obj->propalid] = $obj->ref.' ('.$obj->name.')';
  2453. } else {
  2454. $ga[$i]['id'] = $obj->propalid;
  2455. $ga[$i]['ref'] = $obj->ref;
  2456. $ga[$i]['name'] = $obj->name;
  2457. }
  2458. $i++;
  2459. }
  2460. }
  2461. return $ga;
  2462. } else {
  2463. dol_print_error($this->db);
  2464. return -1;
  2465. }
  2466. }
  2467. /**
  2468. * Returns an array with the numbers of related invoices
  2469. *
  2470. * @return array Array of invoices
  2471. */
  2472. public function getInvoiceArrayList()
  2473. {
  2474. return $this->InvoiceArrayList($this->id);
  2475. }
  2476. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2477. /**
  2478. * Returns an array with id and ref of related invoices
  2479. *
  2480. * @param int $id Id propal
  2481. * @return array Array of invoices id
  2482. */
  2483. public function InvoiceArrayList($id)
  2484. {
  2485. // phpcs:enable
  2486. $ga = array();
  2487. $linkedInvoices = array();
  2488. $this->fetchObjectLinked($id, $this->element);
  2489. foreach ($this->linkedObjectsIds as $objecttype => $objectid) {
  2490. // Nouveau système du comon object renvoi des rowid et non un id linéaire de 1 à n
  2491. // On parcourt donc une liste d'objets en tant qu'objet unique
  2492. foreach ($objectid as $key => $object) {
  2493. // Cas des factures liees directement
  2494. if ($objecttype == 'facture') {
  2495. $linkedInvoices[] = $object;
  2496. } else {
  2497. // Cas des factures liees par un autre objet (ex: commande)
  2498. $this->fetchObjectLinked($object, $objecttype);
  2499. foreach ($this->linkedObjectsIds as $subobjecttype => $subobjectid) {
  2500. foreach ($subobjectid as $subkey => $subobject) {
  2501. if ($subobjecttype == 'facture') {
  2502. $linkedInvoices[] = $subobject;
  2503. }
  2504. }
  2505. }
  2506. }
  2507. }
  2508. }
  2509. if (count($linkedInvoices) > 0) {
  2510. $sql = "SELECT rowid as facid, ref, total_ht as total, datef as df, fk_user_author, fk_statut, paye";
  2511. $sql .= " FROM ".MAIN_DB_PREFIX."facture";
  2512. $sql .= " WHERE rowid IN (".$this->db->sanitize(implode(',', $linkedInvoices)).")";
  2513. dol_syslog(get_class($this)."::InvoiceArrayList", LOG_DEBUG);
  2514. $resql = $this->db->query($sql);
  2515. if ($resql) {
  2516. $tab_sqlobj = array();
  2517. $nump = $this->db->num_rows($resql);
  2518. for ($i = 0; $i < $nump; $i++) {
  2519. $sqlobj = $this->db->fetch_object($resql);
  2520. $tab_sqlobj[] = $sqlobj;
  2521. }
  2522. $this->db->free($resql);
  2523. $nump = count($tab_sqlobj);
  2524. if ($nump) {
  2525. $i = 0;
  2526. while ($i < $nump) {
  2527. $obj = array_shift($tab_sqlobj);
  2528. $ga[$i] = $obj;
  2529. $i++;
  2530. }
  2531. }
  2532. return $ga;
  2533. } else {
  2534. return -1;
  2535. }
  2536. } else {
  2537. return $ga;
  2538. }
  2539. }
  2540. /**
  2541. * Delete proposal
  2542. *
  2543. * @param User $user Object user that delete
  2544. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  2545. * @return int >0 if OK, <=0 if KO
  2546. */
  2547. public function delete($user, $notrigger = 0)
  2548. {
  2549. global $conf;
  2550. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  2551. $error = 0;
  2552. $this->db->begin();
  2553. if (!$notrigger) {
  2554. // Call trigger
  2555. $result = $this->call_trigger('PROPAL_DELETE', $user);
  2556. if ($result < 0) {
  2557. $error++;
  2558. }
  2559. // End call triggers
  2560. }
  2561. // Delete extrafields of lines and lines
  2562. if (!$error && !empty($this->table_element_line)) {
  2563. $tabletodelete = $this->table_element_line;
  2564. $sqlef = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete."_extrafields WHERE fk_object IN (SELECT rowid FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".((int) $this->id).")";
  2565. $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".((int) $this->id);
  2566. if (!$this->db->query($sqlef) || !$this->db->query($sql)) {
  2567. $error++;
  2568. $this->error = $this->db->lasterror();
  2569. $this->errors[] = $this->error;
  2570. dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
  2571. }
  2572. }
  2573. if (!$error) {
  2574. // Delete linked object
  2575. $res = $this->deleteObjectLinked();
  2576. if ($res < 0) {
  2577. $error++;
  2578. }
  2579. }
  2580. if (!$error) {
  2581. // Delete linked contacts
  2582. $res = $this->delete_linked_contact();
  2583. if ($res < 0) {
  2584. $error++;
  2585. }
  2586. }
  2587. // Removed extrafields of object
  2588. if (!$error) {
  2589. $result = $this->deleteExtraFields();
  2590. if ($result < 0) {
  2591. $error++;
  2592. dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
  2593. }
  2594. }
  2595. // Delete main record
  2596. if (!$error) {
  2597. $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".((int) $this->id);
  2598. $res = $this->db->query($sql);
  2599. if (!$res) {
  2600. $error++;
  2601. $this->error = $this->db->lasterror();
  2602. $this->errors[] = $this->error;
  2603. dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
  2604. }
  2605. }
  2606. // Delete record into ECM index and physically
  2607. if (!$error) {
  2608. $res = $this->deleteEcmFiles(0); // Deleting files physically is done later with the dol_delete_dir_recursive
  2609. if (!$res) {
  2610. $error++;
  2611. }
  2612. }
  2613. if (!$error) {
  2614. // We remove directory
  2615. $ref = dol_sanitizeFileName($this->ref);
  2616. if ($conf->propal->multidir_output[$this->entity] && !empty($this->ref)) {
  2617. $dir = $conf->propal->multidir_output[$this->entity]."/".$ref;
  2618. $file = $dir."/".$ref.".pdf";
  2619. if (file_exists($file)) {
  2620. dol_delete_preview($this);
  2621. if (!dol_delete_file($file, 0, 0, 0, $this)) {
  2622. $this->error = 'ErrorFailToDeleteFile';
  2623. $this->errors[] = $this->error;
  2624. $this->db->rollback();
  2625. return 0;
  2626. }
  2627. }
  2628. if (file_exists($dir)) {
  2629. $res = @dol_delete_dir_recursive($dir);
  2630. if (!$res) {
  2631. $this->error = 'ErrorFailToDeleteDir';
  2632. $this->errors[] = $this->error;
  2633. $this->db->rollback();
  2634. return 0;
  2635. }
  2636. }
  2637. }
  2638. }
  2639. if (!$error) {
  2640. dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG);
  2641. $this->db->commit();
  2642. return 1;
  2643. } else {
  2644. $this->db->rollback();
  2645. return -1;
  2646. }
  2647. }
  2648. /**
  2649. * Change the delivery time
  2650. *
  2651. * @param int $availability_id Id of new delivery time
  2652. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  2653. * @return int >0 if OK, <0 if KO
  2654. * @deprecated use set_availability
  2655. */
  2656. public function availability($availability_id, $notrigger = 0)
  2657. {
  2658. global $user;
  2659. if ($this->statut >= self::STATUS_DRAFT) {
  2660. $error = 0;
  2661. $this->db->begin();
  2662. $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal';
  2663. $sql .= ' SET fk_availability = '.((int) $availability_id);
  2664. $sql .= ' WHERE rowid='.((int) $this->id);
  2665. dol_syslog(__METHOD__.' availability('.$availability_id.')', LOG_DEBUG);
  2666. $resql = $this->db->query($sql);
  2667. if (!$resql) {
  2668. $this->errors[] = $this->db->error();
  2669. $error++;
  2670. }
  2671. if (!$error) {
  2672. $this->oldcopy = clone $this;
  2673. $this->availability_id = $availability_id;
  2674. }
  2675. if (!$notrigger && empty($error)) {
  2676. // Call trigger
  2677. $result = $this->call_trigger('PROPAL_MODIFY', $user);
  2678. if ($result < 0) {
  2679. $error++;
  2680. }
  2681. // End call triggers
  2682. }
  2683. if (!$error) {
  2684. $this->db->commit();
  2685. return 1;
  2686. } else {
  2687. foreach ($this->errors as $errmsg) {
  2688. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  2689. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  2690. }
  2691. $this->db->rollback();
  2692. return -1 * $error;
  2693. }
  2694. } else {
  2695. $error_str = 'Propal status do not meet requirement '.$this->statut;
  2696. dol_syslog(__METHOD__.$error_str, LOG_ERR);
  2697. $this->error = $error_str;
  2698. $this->errors[] = $this->error;
  2699. return -2;
  2700. }
  2701. }
  2702. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2703. /**
  2704. * Change source demand
  2705. *
  2706. * @param int $demand_reason_id Id of new source demand
  2707. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  2708. * @return int >0 si ok, <0 si ko
  2709. * @deprecated use set_demand_reason
  2710. */
  2711. public function demand_reason($demand_reason_id, $notrigger = 0)
  2712. {
  2713. // phpcs:enable
  2714. global $user;
  2715. if ($this->statut >= self::STATUS_DRAFT) {
  2716. $error = 0;
  2717. $this->db->begin();
  2718. $sql = 'UPDATE '.MAIN_DB_PREFIX.'propal';
  2719. $sql .= ' SET fk_input_reason = '.((int) $demand_reason_id);
  2720. $sql .= ' WHERE rowid='.((int) $this->id);
  2721. dol_syslog(__METHOD__.' demand_reason('.$demand_reason_id.')', LOG_DEBUG);
  2722. $resql = $this->db->query($sql);
  2723. if (!$resql) {
  2724. $this->errors[] = $this->db->error();
  2725. $error++;
  2726. }
  2727. if (!$error) {
  2728. $this->oldcopy = clone $this;
  2729. $this->demand_reason_id = $demand_reason_id;
  2730. }
  2731. if (!$notrigger && empty($error)) {
  2732. // Call trigger
  2733. $result = $this->call_trigger('PROPAL_MODIFY', $user);
  2734. if ($result < 0) {
  2735. $error++;
  2736. }
  2737. // End call triggers
  2738. }
  2739. if (!$error) {
  2740. $this->db->commit();
  2741. return 1;
  2742. } else {
  2743. foreach ($this->errors as $errmsg) {
  2744. dol_syslog(__METHOD__.' Error: '.$errmsg, LOG_ERR);
  2745. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  2746. }
  2747. $this->db->rollback();
  2748. return -1 * $error;
  2749. }
  2750. } else {
  2751. $error_str = 'Propal status do not meet requirement '.$this->statut;
  2752. dol_syslog(__METHOD__.$error_str, LOG_ERR);
  2753. $this->error = $error_str;
  2754. $this->errors[] = $this->error;
  2755. return -2;
  2756. }
  2757. }
  2758. /**
  2759. * Object Proposal Information
  2760. *
  2761. * @param int $id Proposal id
  2762. * @return void
  2763. */
  2764. public function info($id)
  2765. {
  2766. $sql = "SELECT c.rowid, ";
  2767. $sql .= " c.datec, c.date_valid as datev, c.date_signature, c.date_cloture as dateo,";
  2768. $sql .= " c.fk_user_author, c.fk_user_valid, c.fk_user_signature, c.fk_user_cloture";
  2769. $sql .= " FROM ".MAIN_DB_PREFIX."propal as c";
  2770. $sql .= " WHERE c.rowid = ".((int) $id);
  2771. $result = $this->db->query($sql);
  2772. if ($result) {
  2773. if ($this->db->num_rows($result)) {
  2774. $obj = $this->db->fetch_object($result);
  2775. $this->id = $obj->rowid;
  2776. $this->date_creation = $this->db->jdate($obj->datec);
  2777. $this->date_validation = $this->db->jdate($obj->datev);
  2778. $this->date_signature = $this->db->jdate($obj->date_signature);
  2779. $this->date_cloture = $this->db->jdate($obj->dateo);
  2780. $cuser = new User($this->db);
  2781. $cuser->fetch($obj->fk_user_author);
  2782. $this->user_creation = $cuser;
  2783. if ($obj->fk_user_valid) {
  2784. $vuser = new User($this->db);
  2785. $vuser->fetch($obj->fk_user_valid);
  2786. $this->user_validation = $vuser;
  2787. }
  2788. if ($obj->fk_user_signature) {
  2789. $user_signature = new User($this->db);
  2790. $user_signature->fetch($obj->fk_user_signature);
  2791. $this->user_signature = $user_signature;
  2792. }
  2793. if ($obj->fk_user_cloture) {
  2794. $cluser = new User($this->db);
  2795. $cluser->fetch($obj->fk_user_cloture);
  2796. $this->user_cloture = $cluser;
  2797. }
  2798. }
  2799. $this->db->free($result);
  2800. } else {
  2801. dol_print_error($this->db);
  2802. }
  2803. }
  2804. /**
  2805. * Return label of status of proposal (draft, validated, ...)
  2806. *
  2807. * @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
  2808. * @return string Label
  2809. */
  2810. public function getLibStatut($mode = 0)
  2811. {
  2812. return $this->LibStatut($this->statut, $mode);
  2813. }
  2814. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2815. /**
  2816. * Return label of a status (draft, validated, ...)
  2817. *
  2818. * @param int $status Id status
  2819. * @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
  2820. * @return string Label
  2821. */
  2822. public function LibStatut($status, $mode = 1)
  2823. {
  2824. // phpcs:enable
  2825. global $conf;
  2826. // Init/load array of translation of status
  2827. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  2828. global $langs;
  2829. $langs->load("propal");
  2830. $this->labelStatus[0] = $langs->transnoentitiesnoconv("PropalStatusDraft");
  2831. $this->labelStatus[1] = $langs->transnoentitiesnoconv("PropalStatusValidated");
  2832. $this->labelStatus[2] = $langs->transnoentitiesnoconv("PropalStatusSigned");
  2833. $this->labelStatus[3] = $langs->transnoentitiesnoconv("PropalStatusNotSigned");
  2834. $this->labelStatus[4] = $langs->transnoentitiesnoconv("PropalStatusBilled");
  2835. $this->labelStatusShort[0] = $langs->transnoentitiesnoconv("PropalStatusDraftShort");
  2836. $this->labelStatusShort[1] = $langs->transnoentitiesnoconv("PropalStatusValidatedShort");
  2837. $this->labelStatusShort[2] = $langs->transnoentitiesnoconv("PropalStatusSignedShort");
  2838. $this->labelStatusShort[3] = $langs->transnoentitiesnoconv("PropalStatusNotSignedShort");
  2839. $this->labelStatusShort[4] = $langs->transnoentitiesnoconv("PropalStatusBilledShort");
  2840. }
  2841. $statusType = '';
  2842. if ($status == self::STATUS_DRAFT) {
  2843. $statusType = 'status0';
  2844. } elseif ($status == self::STATUS_VALIDATED) {
  2845. $statusType = 'status1';
  2846. } elseif ($status == self::STATUS_SIGNED) {
  2847. $statusType = 'status4';
  2848. } elseif ($status == self::STATUS_NOTSIGNED) {
  2849. $statusType = 'status9';
  2850. } elseif ($status == self::STATUS_BILLED) {
  2851. $statusType = 'status6';
  2852. }
  2853. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  2854. }
  2855. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  2856. /**
  2857. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  2858. *
  2859. * @param User $user Object user
  2860. * @param int $mode "opened" for proposal to close, "signed" for proposal to invoice
  2861. * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK
  2862. */
  2863. public function load_board($user, $mode)
  2864. {
  2865. // phpcs:enable
  2866. global $conf, $langs;
  2867. $clause = " WHERE";
  2868. $sql = "SELECT p.rowid, p.ref, p.datec as datec, p.fin_validite as datefin, p.total_ht";
  2869. $sql .= " FROM ".MAIN_DB_PREFIX."propal as p";
  2870. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  2871. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON p.fk_soc = sc.fk_soc";
  2872. $sql .= " WHERE sc.fk_user = ".((int) $user->id);
  2873. $clause = " AND";
  2874. }
  2875. $sql .= $clause." p.entity IN (".getEntity('propal').")";
  2876. if ($mode == 'opened') {
  2877. $sql .= " AND p.fk_statut = ".self::STATUS_VALIDATED;
  2878. }
  2879. if ($mode == 'signed') {
  2880. $sql .= " AND p.fk_statut = ".self::STATUS_SIGNED;
  2881. }
  2882. if ($user->socid) {
  2883. $sql .= " AND p.fk_soc = ".((int) $user->socid);
  2884. }
  2885. $resql = $this->db->query($sql);
  2886. if ($resql) {
  2887. $langs->load("propal");
  2888. $now = dol_now();
  2889. $delay_warning = 0;
  2890. $status = 0;
  2891. $label = $labelShort = '';
  2892. if ($mode == 'opened') {
  2893. $delay_warning = $conf->propal->cloture->warning_delay;
  2894. $status = self::STATUS_VALIDATED;
  2895. $label = $langs->transnoentitiesnoconv("PropalsToClose");
  2896. $labelShort = $langs->transnoentitiesnoconv("ToAcceptRefuse");
  2897. }
  2898. if ($mode == 'signed') {
  2899. $delay_warning = $conf->propal->facturation->warning_delay;
  2900. $status = self::STATUS_SIGNED;
  2901. $label = $langs->trans("PropalsToBill"); // We set here bill but may be billed or ordered
  2902. $labelShort = $langs->trans("ToBill");
  2903. }
  2904. $response = new WorkboardResponse();
  2905. $response->warning_delay = $delay_warning / 60 / 60 / 24;
  2906. $response->label = $label;
  2907. $response->labelShort = $labelShort;
  2908. $response->url = DOL_URL_ROOT.'/comm/propal/list.php?search_status='.$status.'&mainmenu=commercial&leftmenu=propals';
  2909. $response->url_late = DOL_URL_ROOT.'/comm/propal/list.php?search_status='.$status.'&mainmenu=commercial&leftmenu=propals&sortfield=p.datep&sortorder=asc';
  2910. $response->img = img_object('', "propal");
  2911. // This assignment in condition is not a bug. It allows walking the results.
  2912. while ($obj = $this->db->fetch_object($resql)) {
  2913. $response->nbtodo++;
  2914. $response->total += $obj->total_ht;
  2915. if ($mode == 'opened') {
  2916. $datelimit = $this->db->jdate($obj->datefin);
  2917. if ($datelimit < ($now - $delay_warning)) {
  2918. $response->nbtodolate++;
  2919. }
  2920. }
  2921. // TODO Definir regle des propales a facturer en retard
  2922. // if ($mode == 'signed' && ! count($this->FactureListeArray($obj->rowid))) $this->nbtodolate++;
  2923. }
  2924. return $response;
  2925. } else {
  2926. $this->error = $this->db->error();
  2927. return -1;
  2928. }
  2929. }
  2930. /**
  2931. * Initialise an instance with random values.
  2932. * Used to build previews or test instances.
  2933. * id must be 0 if object instance is a specimen.
  2934. *
  2935. * @return void
  2936. */
  2937. public function initAsSpecimen()
  2938. {
  2939. global $conf, $langs;
  2940. // Load array of products prodids
  2941. $num_prods = 0;
  2942. $prodids = array();
  2943. $sql = "SELECT rowid";
  2944. $sql .= " FROM ".MAIN_DB_PREFIX."product";
  2945. $sql .= " WHERE entity IN (".getEntity('product').")";
  2946. $sql .= $this->db->plimit(100);
  2947. $resql = $this->db->query($sql);
  2948. if ($resql) {
  2949. $num_prods = $this->db->num_rows($resql);
  2950. $i = 0;
  2951. while ($i < $num_prods) {
  2952. $i++;
  2953. $row = $this->db->fetch_row($resql);
  2954. $prodids[$i] = $row[0];
  2955. }
  2956. }
  2957. // Initialise parametres
  2958. $this->id = 0;
  2959. $this->ref = 'SPECIMEN';
  2960. $this->ref_client = 'NEMICEPS';
  2961. $this->specimen = 1;
  2962. $this->socid = 1;
  2963. $this->date = time();
  2964. $this->fin_validite = $this->date + 3600 * 24 * 30;
  2965. $this->cond_reglement_id = 1;
  2966. $this->cond_reglement_code = 'RECEP';
  2967. $this->mode_reglement_id = 7;
  2968. $this->mode_reglement_code = 'CHQ';
  2969. $this->availability_id = 1;
  2970. $this->availability_code = 'AV_NOW';
  2971. $this->demand_reason_id = 1;
  2972. $this->demand_reason_code = 'SRC_00';
  2973. $this->note_public = 'This is a comment (public)';
  2974. $this->note_private = 'This is a comment (private)';
  2975. $this->multicurrency_tx = 1;
  2976. $this->multicurrency_code = $conf->currency;
  2977. // Lines
  2978. $nbp = 5;
  2979. $xnbp = 0;
  2980. while ($xnbp < $nbp) {
  2981. $line = new PropaleLigne($this->db);
  2982. $line->desc = $langs->trans("Description")." ".$xnbp;
  2983. $line->qty = 1;
  2984. $line->subprice = 100;
  2985. $line->price = 100;
  2986. $line->tva_tx = 20;
  2987. $line->localtax1_tx = 0;
  2988. $line->localtax2_tx = 0;
  2989. if ($xnbp == 2) {
  2990. $line->total_ht = 50;
  2991. $line->total_ttc = 60;
  2992. $line->total_tva = 10;
  2993. $line->remise_percent = 50;
  2994. } else {
  2995. $line->total_ht = 100;
  2996. $line->total_ttc = 120;
  2997. $line->total_tva = 20;
  2998. $line->remise_percent = 00;
  2999. }
  3000. if ($num_prods > 0) {
  3001. $prodid = mt_rand(1, $num_prods);
  3002. $line->fk_product = $prodids[$prodid];
  3003. $line->product_ref = 'SPECIMEN';
  3004. }
  3005. $this->lines[$xnbp] = $line;
  3006. $this->total_ht += $line->total_ht;
  3007. $this->total_tva += $line->total_tva;
  3008. $this->total_ttc += $line->total_ttc;
  3009. $xnbp++;
  3010. }
  3011. }
  3012. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3013. /**
  3014. * Charge indicateurs this->nb de tableau de bord
  3015. *
  3016. * @return int <0 if ko, >0 if ok
  3017. */
  3018. public function load_state_board()
  3019. {
  3020. // phpcs:enable
  3021. global $user;
  3022. $this->nb = array();
  3023. $clause = "WHERE";
  3024. $sql = "SELECT count(p.rowid) as nb";
  3025. $sql .= " FROM ".MAIN_DB_PREFIX."propal as p";
  3026. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON p.fk_soc = s.rowid";
  3027. if (empty($user->rights->societe->client->voir) && !$user->socid) {
  3028. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
  3029. $sql .= " WHERE sc.fk_user = ".((int) $user->id);
  3030. $clause = "AND";
  3031. }
  3032. $sql .= " ".$clause." p.entity IN (".getEntity('propal').")";
  3033. $resql = $this->db->query($sql);
  3034. if ($resql) {
  3035. // This assignment in condition is not a bug. It allows walking the results.
  3036. while ($obj = $this->db->fetch_object($resql)) {
  3037. $this->nb["proposals"] = $obj->nb;
  3038. }
  3039. $this->db->free($resql);
  3040. return 1;
  3041. } else {
  3042. dol_print_error($this->db);
  3043. $this->error = $this->db->error();
  3044. return -1;
  3045. }
  3046. }
  3047. /**
  3048. * Returns the reference to the following non used Proposal used depending on the active numbering module
  3049. * defined into PROPALE_ADDON
  3050. *
  3051. * @param Societe $soc Object thirdparty
  3052. * @return string Reference libre pour la propale
  3053. */
  3054. public function getNextNumRef($soc)
  3055. {
  3056. global $conf, $langs;
  3057. $langs->load("propal");
  3058. $classname = $conf->global->PROPALE_ADDON;
  3059. if (!empty($classname)) {
  3060. $mybool = false;
  3061. $file = $classname.".php";
  3062. // Include file with class
  3063. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  3064. foreach ($dirmodels as $reldir) {
  3065. $dir = dol_buildpath($reldir."core/modules/propale/");
  3066. // Load file with numbering class (if found)
  3067. $mybool |= @include_once $dir.$file;
  3068. }
  3069. if (!$mybool) {
  3070. dol_print_error('', "Failed to include file ".$file);
  3071. return '';
  3072. }
  3073. $obj = new $classname();
  3074. $numref = "";
  3075. $numref = $obj->getNextValue($soc, $this);
  3076. if ($numref != "") {
  3077. return $numref;
  3078. } else {
  3079. $this->error = $obj->error;
  3080. //dol_print_error($db,"Propale::getNextNumRef ".$obj->error);
  3081. return "";
  3082. }
  3083. } else {
  3084. $langs->load("errors");
  3085. print $langs->trans("Error")." ".$langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv("Proposal"));
  3086. return "";
  3087. }
  3088. }
  3089. /**
  3090. * Return clicable link of object (with eventually picto)
  3091. *
  3092. * @param int $withpicto Add picto into link
  3093. * @param string $option Where point the link ('expedition', 'document', ...)
  3094. * @param string $get_params Parametres added to url
  3095. * @param int $notooltip 1=Disable tooltip
  3096. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  3097. * @param int $addlinktonotes -1=Disable, 0=Just add label show notes, 1=Add private note (only internal user), 2=Add public note (internal or external user), 3=Add private (internal user) and public note (internal and external user)
  3098. * @return string String with URL
  3099. */
  3100. public function getNomUrl($withpicto = 0, $option = '', $get_params = '', $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = -1)
  3101. {
  3102. global $langs, $conf, $user, $hookmanager;
  3103. if (!empty($conf->dol_no_mouse_hover)) {
  3104. $notooltip = 1; // Force disable tooltips
  3105. }
  3106. $result = '';
  3107. $label = '';
  3108. $url = '';
  3109. if ($user->rights->propal->lire) {
  3110. $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Proposal").'</u>';
  3111. if (isset($this->statut)) {
  3112. $label .= ' '.$this->getLibStatut(5);
  3113. }
  3114. if (!empty($this->ref)) {
  3115. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  3116. }
  3117. if (!empty($this->ref_client)) {
  3118. $label .= '<br><b>'.$langs->trans('RefCustomer').':</b> '.$this->ref_client;
  3119. }
  3120. if (!empty($this->total_ht)) {
  3121. $label .= '<br><b>'.$langs->trans('AmountHT').':</b> '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
  3122. }
  3123. if (!empty($this->total_tva)) {
  3124. $label .= '<br><b>'.$langs->trans('VAT').':</b> '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
  3125. }
  3126. if (!empty($this->total_ttc)) {
  3127. $label .= '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
  3128. }
  3129. if (!empty($this->delivery_date)) {
  3130. $label .= '<br><b>'.$langs->trans('DeliveryDate').':</b> '.dol_print_date($this->delivery_date, 'dayhour');
  3131. }
  3132. if ($option == '') {
  3133. $url = DOL_URL_ROOT.'/comm/propal/card.php?id='.$this->id.$get_params;
  3134. } elseif ($option == 'compta') { // deprecated
  3135. $url = DOL_URL_ROOT.'/comm/propal/card.php?id='.$this->id.$get_params;
  3136. } elseif ($option == 'expedition') {
  3137. $url = DOL_URL_ROOT.'/expedition/propal.php?id='.$this->id.$get_params;
  3138. } elseif ($option == 'document') {
  3139. $url = DOL_URL_ROOT.'/comm/propal/document.php?id='.$this->id.$get_params;
  3140. }
  3141. if ($option != 'nolink') {
  3142. // Add param to save lastsearch_values or not
  3143. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  3144. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  3145. $add_save_lastsearch_values = 1;
  3146. }
  3147. if ($add_save_lastsearch_values) {
  3148. $url .= '&save_lastsearch_values=1';
  3149. }
  3150. }
  3151. }
  3152. $linkclose = '';
  3153. if (empty($notooltip) && $user->rights->propal->lire) {
  3154. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  3155. $label = $langs->trans("Proposal");
  3156. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  3157. }
  3158. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  3159. $linkclose .= ' class="classfortooltip"';
  3160. }
  3161. $linkstart = '<a href="'.$url.'"';
  3162. $linkstart .= $linkclose.'>';
  3163. $linkend = '</a>';
  3164. $result .= $linkstart;
  3165. if ($withpicto) {
  3166. $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  3167. }
  3168. if ($withpicto != 2) {
  3169. $result .= $this->ref;
  3170. }
  3171. $result .= $linkend;
  3172. if ($addlinktonotes >= 0) {
  3173. $txttoshow = '';
  3174. if ($addlinktonotes == 0) {
  3175. if (!empty($this->note_private) || !empty($this->note_public)) {
  3176. $txttoshow = $langs->trans('ViewPrivateNote');
  3177. }
  3178. } elseif ($addlinktonotes == 1) {
  3179. if (!empty($this->note_private)) {
  3180. $txttoshow .= ($user->socid > 0 ? '' : dol_string_nohtmltag($this->note_private, 1));
  3181. }
  3182. } elseif ($addlinktonotes == 2) {
  3183. if (!empty($this->note_public)) {
  3184. $txttoshow .= dol_string_nohtmltag($this->note_public, 1);
  3185. }
  3186. } elseif ($addlinktonotes == 3) {
  3187. if ($user->socid > 0) {
  3188. if (!empty($this->note_public)) {
  3189. $txttoshow .= dol_string_nohtmltag($this->note_public, 1);
  3190. }
  3191. } else {
  3192. if (!empty($this->note_public)) {
  3193. $txttoshow .= dol_string_nohtmltag($this->note_public, 1);
  3194. }
  3195. if (!empty($this->note_private)) {
  3196. if (!empty($txttoshow)) {
  3197. $txttoshow .= '<br><br>';
  3198. }
  3199. $txttoshow .= dol_string_nohtmltag($this->note_private, 1);
  3200. }
  3201. }
  3202. }
  3203. if ($txttoshow) {
  3204. $result .= ' <span class="note inline-block">';
  3205. $result .= '<a href="'.DOL_URL_ROOT.'/comm/propal/note.php?id='.$this->id.'" class="classfortooltip" title="'.dol_escape_htmltag($txttoshow).'">';
  3206. $result .= img_picto('', 'note');
  3207. $result .= '</a>';
  3208. $result .= '</span>';
  3209. }
  3210. }
  3211. global $action;
  3212. $hookmanager->initHooks(array($this->element . 'dao'));
  3213. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  3214. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  3215. if ($reshook > 0) {
  3216. $result = $hookmanager->resPrint;
  3217. } else {
  3218. $result .= $hookmanager->resPrint;
  3219. }
  3220. return $result;
  3221. }
  3222. /**
  3223. * Retrieve an array of proposal lines
  3224. * @param string $filters Filter on other fields
  3225. *
  3226. * @return int >0 if OK, <0 if KO
  3227. */
  3228. public function getLinesArray($filters = '')
  3229. {
  3230. return $this->fetch_lines(0, 0, $filters);
  3231. }
  3232. /**
  3233. * Create a document onto disk according to template module.
  3234. *
  3235. * @param string $modele Force model to use ('' to not force)
  3236. * @param Translate $outputlangs Object langs to use for output
  3237. * @param int $hidedetails Hide details of lines
  3238. * @param int $hidedesc Hide description
  3239. * @param int $hideref Hide ref
  3240. * @param null|array $moreparams Array to provide more information
  3241. * @return int 0 if KO, 1 if OK
  3242. */
  3243. public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
  3244. {
  3245. global $conf, $langs;
  3246. $langs->load("propale");
  3247. $outputlangs->load("products");
  3248. if (!dol_strlen($modele)) {
  3249. $modele = 'azur';
  3250. if ($this->model_pdf) {
  3251. $modele = $this->model_pdf;
  3252. } elseif (!empty($conf->global->PROPALE_ADDON_PDF)) {
  3253. $modele = $conf->global->PROPALE_ADDON_PDF;
  3254. }
  3255. }
  3256. $modelpath = "core/modules/propale/doc/";
  3257. return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  3258. }
  3259. /**
  3260. * Function used to replace a thirdparty id with another one.
  3261. *
  3262. * @param DoliDB $db Database handler
  3263. * @param int $origin_id Old thirdparty id
  3264. * @param int $dest_id New thirdparty id
  3265. * @return bool
  3266. */
  3267. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  3268. {
  3269. $tables = array(
  3270. 'propal'
  3271. );
  3272. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  3273. }
  3274. }
  3275. /**
  3276. * Class to manage commercial proposal lines
  3277. */
  3278. class PropaleLigne extends CommonObjectLine
  3279. {
  3280. /**
  3281. * @var string ID to identify managed object
  3282. */
  3283. public $element = 'propaldet';
  3284. /**
  3285. * @var string Name of table without prefix where object is stored
  3286. */
  3287. public $table_element = 'propaldet';
  3288. public $oldline;
  3289. // From llx_propaldet
  3290. public $fk_propal;
  3291. public $fk_parent_line;
  3292. public $desc; // Description ligne
  3293. public $fk_product; // Id produit predefini
  3294. /**
  3295. * @deprecated
  3296. * @see $product_type
  3297. */
  3298. public $fk_product_type;
  3299. /**
  3300. * Product type.
  3301. * @var int
  3302. * @see Product::TYPE_PRODUCT, Product::TYPE_SERVICE
  3303. */
  3304. public $product_type = Product::TYPE_PRODUCT;
  3305. public $qty;
  3306. public $tva_tx;
  3307. public $vat_src_code;
  3308. public $subprice;
  3309. public $remise_percent;
  3310. public $fk_remise_except;
  3311. public $rang = 0;
  3312. public $fk_fournprice;
  3313. public $pa_ht;
  3314. public $marge_tx;
  3315. public $marque_tx;
  3316. public $special_code; // Tag for special lines (exlusive tags)
  3317. // 1: frais de port
  3318. // 2: ecotaxe
  3319. // 3: option line (when qty = 0)
  3320. public $info_bits = 0; // Some other info:
  3321. // Bit 0: 0 si TVA normal - 1 si TVA NPR
  3322. // Bit 1: 0 ligne normale - 1 si ligne de remise fixe
  3323. public $total_ht; // Total HT de la ligne toute quantite et incluant la remise ligne
  3324. public $total_tva; // Total TVA de la ligne toute quantite et incluant la remise ligne
  3325. public $total_ttc; // Total TTC de la ligne toute quantite et incluant la remise ligne
  3326. /**
  3327. * @deprecated
  3328. * @see $remise_percent, $fk_remise_except
  3329. */
  3330. public $remise;
  3331. /**
  3332. * @deprecated
  3333. * @see $subprice
  3334. */
  3335. public $price;
  3336. // From llx_product
  3337. /**
  3338. * @deprecated
  3339. * @see $product_ref
  3340. */
  3341. public $ref;
  3342. /**
  3343. * Product reference
  3344. * @var string
  3345. */
  3346. public $product_ref;
  3347. /**
  3348. * @deprecated
  3349. * @see $product_label
  3350. */
  3351. public $libelle;
  3352. /**
  3353. * @deprecated
  3354. * @see $product_label
  3355. */
  3356. public $label;
  3357. /**
  3358. * Product label
  3359. * @var string
  3360. */
  3361. public $product_label;
  3362. /**
  3363. * Product description
  3364. * @var string
  3365. */
  3366. public $product_desc;
  3367. /**
  3368. * Product use lot
  3369. * @var string
  3370. */
  3371. public $product_tobatch;
  3372. /**
  3373. * Product barcode
  3374. * @var string
  3375. */
  3376. public $product_barcode;
  3377. public $localtax1_tx; // Local tax 1
  3378. public $localtax2_tx; // Local tax 2
  3379. public $localtax1_type; // Local tax 1 type
  3380. public $localtax2_type; // Local tax 2 type
  3381. public $total_localtax1; // Line total local tax 1
  3382. public $total_localtax2; // Line total local tax 2
  3383. public $date_start;
  3384. public $date_end;
  3385. public $skip_update_total; // Skip update price total for special lines
  3386. // Multicurrency
  3387. public $fk_multicurrency;
  3388. public $multicurrency_code;
  3389. public $multicurrency_subprice;
  3390. public $multicurrency_total_ht;
  3391. public $multicurrency_total_tva;
  3392. public $multicurrency_total_ttc;
  3393. /**
  3394. * Class line Contructor
  3395. *
  3396. * @param DoliDB $db Database handler
  3397. */
  3398. public function __construct($db)
  3399. {
  3400. $this->db = $db;
  3401. }
  3402. /**
  3403. * Retrieve the propal line object
  3404. *
  3405. * @param int $rowid Propal line id
  3406. * @return int <0 if KO, >0 if OK
  3407. */
  3408. public function fetch($rowid)
  3409. {
  3410. $sql = 'SELECT pd.rowid, pd.fk_propal, pd.fk_parent_line, pd.fk_product, pd.label as custom_label, pd.description, pd.price, pd.qty, pd.vat_src_code, pd.tva_tx,';
  3411. $sql .= ' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice,';
  3412. $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,';
  3413. $sql .= ' pd.fk_unit,';
  3414. $sql .= ' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,';
  3415. $sql .= ' pd.fk_multicurrency, pd.multicurrency_code, pd.multicurrency_subprice, pd.multicurrency_total_ht, pd.multicurrency_total_tva, pd.multicurrency_total_ttc,';
  3416. $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
  3417. $sql .= ' pd.date_start, pd.date_end, pd.product_type';
  3418. $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as pd';
  3419. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pd.fk_product = p.rowid';
  3420. $sql .= ' WHERE pd.rowid = '.((int) $rowid);
  3421. $result = $this->db->query($sql);
  3422. if ($result) {
  3423. $objp = $this->db->fetch_object($result);
  3424. if ($objp) {
  3425. $this->id = $objp->rowid;
  3426. $this->rowid = $objp->rowid; // deprecated
  3427. $this->fk_propal = $objp->fk_propal;
  3428. $this->fk_parent_line = $objp->fk_parent_line;
  3429. $this->label = $objp->custom_label;
  3430. $this->desc = $objp->description;
  3431. $this->qty = $objp->qty;
  3432. $this->price = $objp->price; // deprecated
  3433. $this->subprice = $objp->subprice;
  3434. $this->vat_src_code = $objp->vat_src_code;
  3435. $this->tva_tx = $objp->tva_tx;
  3436. $this->remise = $objp->remise; // deprecated
  3437. $this->remise_percent = $objp->remise_percent;
  3438. $this->fk_remise_except = $objp->fk_remise_except;
  3439. $this->fk_product = $objp->fk_product;
  3440. $this->info_bits = $objp->info_bits;
  3441. $this->total_ht = $objp->total_ht;
  3442. $this->total_tva = $objp->total_tva;
  3443. $this->total_ttc = $objp->total_ttc;
  3444. $this->fk_fournprice = $objp->fk_fournprice;
  3445. $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
  3446. $this->pa_ht = $marginInfos[0];
  3447. $this->marge_tx = $marginInfos[1];
  3448. $this->marque_tx = $marginInfos[2];
  3449. $this->special_code = $objp->special_code;
  3450. $this->product_type = $objp->product_type;
  3451. $this->rang = $objp->rang;
  3452. $this->ref = $objp->product_ref; // deprecated
  3453. $this->product_ref = $objp->product_ref;
  3454. $this->libelle = $objp->product_label; // deprecated
  3455. $this->product_label = $objp->product_label;
  3456. $this->product_desc = $objp->product_desc;
  3457. $this->fk_unit = $objp->fk_unit;
  3458. $this->date_start = $this->db->jdate($objp->date_start);
  3459. $this->date_end = $this->db->jdate($objp->date_end);
  3460. // Multicurrency
  3461. $this->fk_multicurrency = $objp->fk_multicurrency;
  3462. $this->multicurrency_code = $objp->multicurrency_code;
  3463. $this->multicurrency_subprice = $objp->multicurrency_subprice;
  3464. $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
  3465. $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
  3466. $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
  3467. $this->fetch_optionals();
  3468. $this->db->free($result);
  3469. return 1;
  3470. } else {
  3471. return 0;
  3472. }
  3473. } else {
  3474. return -1;
  3475. }
  3476. }
  3477. /**
  3478. * Insert object line propal in database
  3479. *
  3480. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  3481. * @return int <0 if KO, >0 if OK
  3482. */
  3483. public function insert($notrigger = 0)
  3484. {
  3485. global $conf, $user;
  3486. $error = 0;
  3487. dol_syslog(get_class($this)."::insert rang=".$this->rang);
  3488. $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht == ''); // If true, we can use a default value. If this->pa_ht = '0', we must use '0'.
  3489. // Clean parameters
  3490. if (empty($this->tva_tx)) {
  3491. $this->tva_tx = 0;
  3492. }
  3493. if (empty($this->localtax1_tx)) {
  3494. $this->localtax1_tx = 0;
  3495. }
  3496. if (empty($this->localtax2_tx)) {
  3497. $this->localtax2_tx = 0;
  3498. }
  3499. if (empty($this->localtax1_type)) {
  3500. $this->localtax1_type = 0;
  3501. }
  3502. if (empty($this->localtax2_type)) {
  3503. $this->localtax2_type = 0;
  3504. }
  3505. if (empty($this->total_localtax1)) {
  3506. $this->total_localtax1 = 0;
  3507. }
  3508. if (empty($this->total_localtax2)) {
  3509. $this->total_localtax2 = 0;
  3510. }
  3511. if (empty($this->rang)) {
  3512. $this->rang = 0;
  3513. }
  3514. if (empty($this->remise)) {
  3515. $this->remise = 0;
  3516. }
  3517. if (empty($this->remise_percent) || !is_numeric($this->remise_percent)) {
  3518. $this->remise_percent = 0;
  3519. }
  3520. if (empty($this->info_bits)) {
  3521. $this->info_bits = 0;
  3522. }
  3523. if (empty($this->special_code)) {
  3524. $this->special_code = 0;
  3525. }
  3526. if (empty($this->fk_parent_line)) {
  3527. $this->fk_parent_line = 0;
  3528. }
  3529. if (empty($this->fk_fournprice)) {
  3530. $this->fk_fournprice = 0;
  3531. }
  3532. if (!is_numeric($this->qty)) {
  3533. $this->qty = 0;
  3534. }
  3535. if (empty($this->pa_ht)) {
  3536. $this->pa_ht = 0;
  3537. }
  3538. if (empty($this->multicurrency_subprice)) {
  3539. $this->multicurrency_subprice = 0;
  3540. }
  3541. if (empty($this->multicurrency_total_ht)) {
  3542. $this->multicurrency_total_ht = 0;
  3543. }
  3544. if (empty($this->multicurrency_total_tva)) {
  3545. $this->multicurrency_total_tva = 0;
  3546. }
  3547. if (empty($this->multicurrency_total_ttc)) {
  3548. $this->multicurrency_total_ttc = 0;
  3549. }
  3550. // if buy price not defined, define buyprice as configured in margin admin
  3551. if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
  3552. if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
  3553. return $result;
  3554. } else {
  3555. $this->pa_ht = $result;
  3556. }
  3557. }
  3558. // Check parameters
  3559. if ($this->product_type < 0) {
  3560. return -1;
  3561. }
  3562. $this->db->begin();
  3563. // Insert line into database
  3564. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'propaldet';
  3565. $sql .= ' (fk_propal, fk_parent_line, label, description, fk_product, product_type,';
  3566. $sql .= ' fk_remise_except, qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
  3567. $sql .= ' subprice, remise_percent, ';
  3568. $sql .= ' info_bits, ';
  3569. $sql .= ' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,';
  3570. $sql .= ' fk_unit,';
  3571. $sql .= ' date_start, date_end';
  3572. $sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc)';
  3573. $sql .= " VALUES (".$this->fk_propal.",";
  3574. $sql .= " ".($this->fk_parent_line > 0 ? "'".$this->db->escape($this->fk_parent_line)."'" : "null").",";
  3575. $sql .= " ".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
  3576. $sql .= " '".$this->db->escape($this->desc)."',";
  3577. $sql .= " ".($this->fk_product ? "'".$this->db->escape($this->fk_product)."'" : "null").",";
  3578. $sql .= " '".$this->db->escape($this->product_type)."',";
  3579. $sql .= " ".($this->fk_remise_except ? "'".$this->db->escape($this->fk_remise_except)."'" : "null").",";
  3580. $sql .= " ".price2num($this->qty, 'MS').",";
  3581. $sql .= " ".(empty($this->vat_src_code) ? "''" : "'".$this->db->escape($this->vat_src_code)."'").",";
  3582. $sql .= " ".price2num($this->tva_tx).",";
  3583. $sql .= " ".price2num($this->localtax1_tx).",";
  3584. $sql .= " ".price2num($this->localtax2_tx).",";
  3585. $sql .= " '".$this->db->escape($this->localtax1_type)."',";
  3586. $sql .= " '".$this->db->escape($this->localtax2_type)."',";
  3587. $sql .= " ".(price2num($this->subprice) !== '' ? price2num($this->subprice, 'MU') : "null").",";
  3588. $sql .= " ".price2num($this->remise_percent, 3).",";
  3589. $sql .= " ".(isset($this->info_bits) ? ((int) $this->info_bits) : "null").",";
  3590. $sql .= " ".price2num($this->total_ht, 'MT').",";
  3591. $sql .= " ".price2num($this->total_tva, 'MT').",";
  3592. $sql .= " ".price2num($this->total_localtax1, 'MT').",";
  3593. $sql .= " ".price2num($this->total_localtax2, 'MT').",";
  3594. $sql .= " ".price2num($this->total_ttc, 'MT').",";
  3595. $sql .= " ".(!empty($this->fk_fournprice) ? "'".$this->db->escape($this->fk_fournprice)."'" : "null").",";
  3596. $sql .= " ".(isset($this->pa_ht) ? "'".price2num($this->pa_ht)."'" : "null").",";
  3597. $sql .= ' '.((int) $this->special_code).',';
  3598. $sql .= ' '.((int) $this->rang).',';
  3599. $sql .= ' '.(empty($this->fk_unit) ? 'NULL' : ((int) $this->fk_unit)).',';
  3600. $sql .= " ".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null").',';
  3601. $sql .= " ".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null");
  3602. $sql .= ", ".($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) : 'null');
  3603. $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
  3604. $sql .= ", ".price2num($this->multicurrency_subprice, 'CU');
  3605. $sql .= ", ".price2num($this->multicurrency_total_ht, 'CT');
  3606. $sql .= ", ".price2num($this->multicurrency_total_tva, 'CT');
  3607. $sql .= ", ".price2num($this->multicurrency_total_ttc, 'CT');
  3608. $sql .= ')';
  3609. dol_syslog(get_class($this).'::insert', LOG_DEBUG);
  3610. $resql = $this->db->query($sql);
  3611. if ($resql) {
  3612. $this->rowid = $this->db->last_insert_id(MAIN_DB_PREFIX.'propaldet');
  3613. if (!$error) {
  3614. $this->id = $this->rowid;
  3615. $result = $this->insertExtraFields();
  3616. if ($result < 0) {
  3617. $error++;
  3618. }
  3619. }
  3620. if (!$error && !$notrigger) {
  3621. // Call trigger
  3622. $result = $this->call_trigger('LINEPROPAL_INSERT', $user);
  3623. if ($result < 0) {
  3624. $this->db->rollback();
  3625. return -1;
  3626. }
  3627. // End call triggers
  3628. }
  3629. $this->db->commit();
  3630. return 1;
  3631. } else {
  3632. $this->error = $this->db->error()." sql=".$sql;
  3633. $this->db->rollback();
  3634. return -1;
  3635. }
  3636. }
  3637. /**
  3638. * Delete line in database
  3639. *
  3640. * @param User $user Object user
  3641. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  3642. * @return int <0 if ko, >0 if ok
  3643. */
  3644. public function delete(User $user, $notrigger = 0)
  3645. {
  3646. global $conf;
  3647. $error = 0;
  3648. $this->db->begin();
  3649. $sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE rowid = ".((int) $this->rowid);
  3650. dol_syslog("PropaleLigne::delete", LOG_DEBUG);
  3651. if ($this->db->query($sql)) {
  3652. // Remove extrafields
  3653. if (!$error) {
  3654. $this->id = $this->rowid;
  3655. $result = $this->deleteExtraFields();
  3656. if ($result < 0) {
  3657. $error++;
  3658. dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
  3659. }
  3660. }
  3661. if (!$error && !$notrigger) {
  3662. // Call trigger
  3663. $result = $this->call_trigger('LINEPROPAL_DELETE', $user);
  3664. if ($result < 0) {
  3665. $this->db->rollback();
  3666. return -1;
  3667. }
  3668. }
  3669. // End call triggers
  3670. $this->db->commit();
  3671. return 1;
  3672. } else {
  3673. $this->error = $this->db->error()." sql=".$sql;
  3674. $this->db->rollback();
  3675. return -1;
  3676. }
  3677. }
  3678. /**
  3679. * Update propal line object into DB
  3680. *
  3681. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  3682. * @return int <0 if ko, >0 if ok
  3683. */
  3684. public function update($notrigger = 0)
  3685. {
  3686. global $conf, $user;
  3687. $error = 0;
  3688. $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht == ''); // If true, we can use a default value. If this->pa_ht = '0', we must use '0'.
  3689. if (empty($this->id) && !empty($this->rowid)) {
  3690. $this->id = $this->rowid;
  3691. }
  3692. // Clean parameters
  3693. if (empty($this->tva_tx)) {
  3694. $this->tva_tx = 0;
  3695. }
  3696. if (empty($this->localtax1_tx)) {
  3697. $this->localtax1_tx = 0;
  3698. }
  3699. if (empty($this->localtax2_tx)) {
  3700. $this->localtax2_tx = 0;
  3701. }
  3702. if (empty($this->total_localtax1)) {
  3703. $this->total_localtax1 = 0;
  3704. }
  3705. if (empty($this->total_localtax2)) {
  3706. $this->total_localtax2 = 0;
  3707. }
  3708. if (empty($this->localtax1_type)) {
  3709. $this->localtax1_type = 0;
  3710. }
  3711. if (empty($this->localtax2_type)) {
  3712. $this->localtax2_type = 0;
  3713. }
  3714. if (empty($this->marque_tx)) {
  3715. $this->marque_tx = 0;
  3716. }
  3717. if (empty($this->marge_tx)) {
  3718. $this->marge_tx = 0;
  3719. }
  3720. if (empty($this->price)) {
  3721. $this->price = 0; // TODO A virer
  3722. }
  3723. if (empty($this->remise)) {
  3724. $this->remise = 0; // TODO A virer
  3725. }
  3726. if (empty($this->remise_percent)) {
  3727. $this->remise_percent = 0;
  3728. }
  3729. if (empty($this->info_bits)) {
  3730. $this->info_bits = 0;
  3731. }
  3732. if (empty($this->special_code)) {
  3733. $this->special_code = 0;
  3734. }
  3735. if (empty($this->fk_parent_line)) {
  3736. $this->fk_parent_line = 0;
  3737. }
  3738. if (empty($this->fk_fournprice)) {
  3739. $this->fk_fournprice = 0;
  3740. }
  3741. if (empty($this->subprice)) {
  3742. $this->subprice = 0;
  3743. }
  3744. if (empty($this->pa_ht)) {
  3745. $this->pa_ht = 0;
  3746. }
  3747. // if buy price not defined, define buyprice as configured in margin admin
  3748. if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
  3749. if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
  3750. return $result;
  3751. } else {
  3752. $this->pa_ht = $result;
  3753. }
  3754. }
  3755. $this->db->begin();
  3756. // Mise a jour ligne en base
  3757. $sql = "UPDATE ".MAIN_DB_PREFIX."propaldet SET";
  3758. $sql .= " description='".$this->db->escape($this->desc)."'";
  3759. $sql .= ", label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
  3760. $sql .= ", product_type=".$this->product_type;
  3761. $sql .= ", vat_src_code = '".(empty($this->vat_src_code) ? '' : $this->vat_src_code)."'";
  3762. $sql .= ", tva_tx='".price2num($this->tva_tx)."'";
  3763. $sql .= ", localtax1_tx=".price2num($this->localtax1_tx);
  3764. $sql .= ", localtax2_tx=".price2num($this->localtax2_tx);
  3765. $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'";
  3766. $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'";
  3767. $sql .= ", qty='".price2num($this->qty)."'";
  3768. $sql .= ", subprice=".price2num($this->subprice)."";
  3769. $sql .= ", remise_percent=".price2num($this->remise_percent)."";
  3770. $sql .= ", price=".price2num($this->price).""; // TODO A virer
  3771. $sql .= ", remise=".price2num($this->remise).""; // TODO A virer
  3772. $sql .= ", info_bits='".$this->db->escape($this->info_bits)."'";
  3773. if (empty($this->skip_update_total)) {
  3774. $sql .= ", total_ht=".price2num($this->total_ht)."";
  3775. $sql .= ", total_tva=".price2num($this->total_tva)."";
  3776. $sql .= ", total_ttc=".price2num($this->total_ttc)."";
  3777. $sql .= ", total_localtax1=".price2num($this->total_localtax1)."";
  3778. $sql .= ", total_localtax2=".price2num($this->total_localtax2)."";
  3779. }
  3780. $sql .= ", fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? "'".$this->db->escape($this->fk_fournprice)."'" : "null");
  3781. $sql .= ", buy_price_ht=".price2num($this->pa_ht);
  3782. if (strlen($this->special_code)) {
  3783. $sql .= ", special_code=".$this->special_code;
  3784. }
  3785. $sql .= ", fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line : "null");
  3786. if (!empty($this->rang)) {
  3787. $sql .= ", rang=".((int) $this->rang);
  3788. }
  3789. $sql .= ", date_start=".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null");
  3790. $sql .= ", date_end=".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null");
  3791. $sql .= ", fk_unit=".(!$this->fk_unit ? 'NULL' : $this->fk_unit);
  3792. // Multicurrency
  3793. $sql .= ", multicurrency_subprice=".price2num($this->multicurrency_subprice)."";
  3794. $sql .= ", multicurrency_total_ht=".price2num($this->multicurrency_total_ht)."";
  3795. $sql .= ", multicurrency_total_tva=".price2num($this->multicurrency_total_tva)."";
  3796. $sql .= ", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc)."";
  3797. $sql .= " WHERE rowid = ".((int) $this->id);
  3798. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  3799. $resql = $this->db->query($sql);
  3800. if ($resql) {
  3801. if (!$error) {
  3802. $result = $this->insertExtraFields();
  3803. if ($result < 0) {
  3804. $error++;
  3805. }
  3806. }
  3807. if (!$error && !$notrigger) {
  3808. // Call trigger
  3809. $result = $this->call_trigger('LINEPROPAL_UPDATE', $user);
  3810. if ($result < 0) {
  3811. $this->db->rollback();
  3812. return -1;
  3813. }
  3814. // End call triggers
  3815. }
  3816. $this->db->commit();
  3817. return 1;
  3818. } else {
  3819. $this->error = $this->db->error();
  3820. $this->db->rollback();
  3821. return -2;
  3822. }
  3823. }
  3824. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  3825. /**
  3826. * Update DB line fields total_xxx
  3827. * Used by migration
  3828. *
  3829. * @return int <0 if KO, >0 if OK
  3830. */
  3831. public function update_total()
  3832. {
  3833. // phpcs:enable
  3834. $this->db->begin();
  3835. // Mise a jour ligne en base
  3836. $sql = "UPDATE ".MAIN_DB_PREFIX."propaldet SET";
  3837. $sql .= " total_ht=".price2num($this->total_ht, 'MT')."";
  3838. $sql .= ",total_tva=".price2num($this->total_tva, 'MT')."";
  3839. $sql .= ",total_ttc=".price2num($this->total_ttc, 'MT')."";
  3840. $sql .= " WHERE rowid = ".((int) $this->rowid);
  3841. dol_syslog("PropaleLigne::update_total", LOG_DEBUG);
  3842. $resql = $this->db->query($sql);
  3843. if ($resql) {
  3844. $this->db->commit();
  3845. return 1;
  3846. } else {
  3847. $this->error = $this->db->error();
  3848. $this->db->rollback();
  3849. return -2;
  3850. }
  3851. }
  3852. }