propal.class.php 125 KB

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