card.php 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017
  1. <?php
  2. /* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2022 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  5. * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
  6. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  7. * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
  8. * Copyright (C) 2010-2016 Juanjo Menent <jmenent@2byte.es>
  9. * Copyright (C) 2010-2022 Philippe Grand <philippe.grand@atoo-net.com>
  10. * Copyright (C) 2012-2013 Christophe Battarel <christophe.battarel@altairis.fr>
  11. * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
  12. * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
  13. * Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es>
  14. * Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
  15. * Copyright (C) 2018-2021 Frédéric France <frederic.france@netlogic.fr>
  16. * Copyright (C) 2020 Nicolas ZABOURI <info@inovea-conseil.com>
  17. * Copyright (C) 2022 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 3 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  31. */
  32. /**
  33. * \file htdocs/comm/propal/card.php
  34. * \ingroup propale
  35. * \brief Page of commercial proposals card and list
  36. */
  37. // Load Dolibarr environment
  38. require '../../main.inc.php';
  39. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  40. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formpropal.class.php';
  42. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmargin.class.php';
  43. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  44. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  45. require_once DOL_DOCUMENT_ROOT.'/core/modules/propale/modules_propale.php';
  46. require_once DOL_DOCUMENT_ROOT.'/core/lib/propal.lib.php';
  47. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  48. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  49. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  50. if (isModEnabled('project')) {
  51. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  52. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
  53. }
  54. if (isModEnabled('variants')) {
  55. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
  56. }
  57. // Load translation files required by the page
  58. $langs->loadLangs(array('companies', 'propal', 'compta', 'bills', 'orders', 'products', 'deliveries', 'sendings', 'other'));
  59. if (isModEnabled('incoterm')) {
  60. $langs->load('incoterm');
  61. }
  62. if (isModEnabled('margin')) {
  63. $langs->load('margins');
  64. }
  65. $error = 0;
  66. $id = GETPOST('id', 'int');
  67. $ref = GETPOST('ref', 'alpha');
  68. $socid = GETPOST('socid', 'int');
  69. $action = GETPOST('action', 'aZ09');
  70. $cancel = GETPOST('cancel', 'alpha');
  71. $origin = GETPOST('origin', 'alpha');
  72. $originid = GETPOST('originid', 'int');
  73. $confirm = GETPOST('confirm', 'alpha');
  74. $lineid = GETPOST('lineid', 'int');
  75. $contactid = GETPOST('contactid', 'int');
  76. $projectid = GETPOST('projectid', 'int');
  77. $rank = (GETPOST('rank', 'int') > 0) ? GETPOST('rank', 'int') : -1;
  78. // PDF
  79. $hidedetails = (GETPOST('hidedetails', 'int') ? GETPOST('hidedetails', 'int') : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0));
  80. $hidedesc = (GETPOST('hidedesc', 'int') ? GETPOST('hidedesc', 'int') : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0));
  81. $hideref = (GETPOST('hideref', 'int') ? GETPOST('hideref', 'int') : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0));
  82. $object = new Propal($db);
  83. $extrafields = new ExtraFields($db);
  84. // fetch optionals attributes and labels
  85. $extrafields->fetch_name_optionals_label($object->table_element);
  86. // Load object
  87. if ($id > 0 || !empty($ref)) {
  88. $ret = $object->fetch($id, $ref);
  89. if ($ret > 0) {
  90. $ret = $object->fetch_thirdparty();
  91. }
  92. if ($ret <= 0) {
  93. setEventMessages($object->error, $object->errors, 'errors');
  94. $action = '';
  95. }
  96. }
  97. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  98. $hookmanager->initHooks(array('propalcard', 'globalcard'));
  99. $usercanread = $user->hasRight("propal", "lire");
  100. $usercancreate = $user->hasRight("propal", "creer");
  101. $usercandelete = $user->hasRight("propal", "supprimer");
  102. $usercanclose = ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && $usercancreate) || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->propal->propal_advance->close)));
  103. $usercanvalidate = ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && $usercancreate) || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->propal->propal_advance->validate)));
  104. $usercansend = (empty($conf->global->MAIN_USE_ADVANCED_PERMS) || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->propal->propal_advance->send)));
  105. $usermustrespectpricemin = ((!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->produit->ignore_price_min_advance)) || empty($conf->global->MAIN_USE_ADVANCED_PERMS));
  106. $usercancreateorder = $user->hasRight('commande', 'creer');
  107. $usercancreateinvoice = $user->hasRight('facture', 'creer');
  108. $usercancreatecontract = $user->hasRight('contrat', 'creer');
  109. $usercancreateintervention = $user->hasRight('ficheinter', 'creer');
  110. $usercancreatepurchaseorder = ($user->hasRight('fournisseur', 'commande', 'creer') || $user->hasRight('supplier_order', 'creer'));
  111. $permissionnote = $usercancreate; // Used by the include of actions_setnotes.inc.php
  112. $permissiondellink = $usercancreate; // Used by the include of actions_dellink.inc.php
  113. $permissiontoedit = $usercancreate; // Used by the include of actions_lineupdown.inc.php
  114. // Security check
  115. if (!empty($user->socid)) {
  116. $socid = $user->socid;
  117. }
  118. restrictedArea($user, 'propal', $object->id);
  119. /*
  120. * Actions
  121. */
  122. $parameters = array('socid' => $socid);
  123. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  124. if ($reshook < 0) {
  125. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  126. }
  127. if (empty($reshook)) {
  128. $backurlforlist = DOL_URL_ROOT.'/comm/propal/list.php';
  129. if (empty($backtopage) || ($cancel && empty($id))) {
  130. if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
  131. if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
  132. $backtopage = $backurlforlist;
  133. } else {
  134. $backtopage = DOL_URL_ROOT.'/comm/propal/card.php?id='.((!empty($id) && $id > 0) ? $id : '__ID__');
  135. }
  136. }
  137. }
  138. if ($cancel) {
  139. if (!empty($backtopageforcancel)) {
  140. header("Location: ".$backtopageforcancel);
  141. exit;
  142. } elseif (!empty($backtopage)) {
  143. header("Location: ".$backtopage);
  144. exit;
  145. }
  146. $action = '';
  147. }
  148. include DOL_DOCUMENT_ROOT.'/core/actions_setnotes.inc.php'; // Must be include, not includ_once
  149. include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php'; // Must be include, not include_once
  150. include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; // Must be include, not include_once
  151. // Action clone object
  152. if ($action == 'confirm_clone' && $confirm == 'yes' && $usercancreate) {
  153. if (!GETPOST('socid', 3)) {
  154. setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
  155. } else {
  156. if ($object->id > 0) {
  157. if (!empty($conf->global->PROPAL_CLONE_DATE_DELIVERY)) {
  158. //Get difference between old and new delivery date and change lines according to difference
  159. $date_delivery = dol_mktime(
  160. 12,
  161. 0,
  162. 0,
  163. GETPOST('date_deliverymonth', 'int'),
  164. GETPOST('date_deliveryday', 'int'),
  165. GETPOST('date_deliveryyear', 'int')
  166. );
  167. $date_delivery_old = (empty($object->delivery_date) ? $object->date_livraison : $object->delivery_date);
  168. if (!empty($date_delivery_old) && !empty($date_delivery)) {
  169. //Attempt to get the date without possible hour rounding errors
  170. $old_date_delivery = dol_mktime(
  171. 12,
  172. 0,
  173. 0,
  174. dol_print_date($date_delivery_old, '%m'),
  175. dol_print_date($date_delivery_old, '%d'),
  176. dol_print_date($date_delivery_old, '%Y')
  177. );
  178. //Calculate the difference and apply if necessary
  179. $difference = $date_delivery - $old_date_delivery;
  180. if ($difference != 0) {
  181. $object->date_livraison = $date_delivery;
  182. $object->delivery_date = $date_delivery;
  183. foreach ($object->lines as $line) {
  184. if (isset($line->date_start)) {
  185. $line->date_start = $line->date_start + $difference;
  186. }
  187. if (isset($line->date_end)) {
  188. $line->date_end = $line->date_end + $difference;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. $result = $object->createFromClone($user, $socid, (GETPOSTISSET('entity') ? GETPOST('entity', 'int') : null), (GETPOST('update_prices', 'aZ') ? true : false));
  195. if ($result > 0) {
  196. header("Location: ".$_SERVER['PHP_SELF'].'?id='.$result);
  197. exit();
  198. } else {
  199. if (count($object->errors) > 0) {
  200. setEventMessages($object->error, $object->errors, 'errors');
  201. }
  202. $action = '';
  203. }
  204. }
  205. }
  206. } elseif ($action == 'confirm_delete' && $confirm == 'yes' && $usercandelete) {
  207. // Delete proposal
  208. $result = $object->delete($user);
  209. if ($result > 0) {
  210. header('Location: '.DOL_URL_ROOT.'/comm/propal/list.php?restore_lastsearch_values=1');
  211. exit();
  212. } else {
  213. $langs->load("errors");
  214. setEventMessages($object->error, $object->errors, 'errors');
  215. }
  216. } elseif ($action == 'confirm_deleteline' && $confirm == 'yes' && $usercancreate) {
  217. // Remove line
  218. $result = $object->deleteline($lineid);
  219. // reorder lines
  220. if ($result > 0) {
  221. $object->line_order(true);
  222. } else {
  223. $langs->load("errors");
  224. setEventMessages($object->error, $object->errors, 'errors');
  225. }
  226. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  227. // Define output language
  228. $outputlangs = $langs;
  229. if (getDolGlobalInt('MAIN_MULTILANGS')) {
  230. $outputlangs = new Translate("", $conf);
  231. $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $object->thirdparty->default_lang);
  232. $outputlangs->setDefaultLang($newlang);
  233. }
  234. $ret = $object->fetch($id); // Reload to get new records
  235. if ($ret > 0) {
  236. $object->fetch_thirdparty();
  237. }
  238. $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  239. }
  240. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id);
  241. exit();
  242. } elseif ($action == 'confirm_validate' && $confirm == 'yes' && $usercanvalidate) {
  243. // Validation
  244. $idwarehouse = GETPOST('idwarehouse', 'int');
  245. $result = $object->valid($user);
  246. if ( $result > 0 && ! empty($conf->global->PROPAL_SKIP_ACCEPT_REFUSE) ) {
  247. $result = $object->closeProposal($user, $object::STATUS_SIGNED);
  248. }
  249. if ($result >= 0) {
  250. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  251. $outputlangs = $langs;
  252. $newlang = '';
  253. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  254. $newlang = GETPOST('lang_id', 'aZ09');
  255. }
  256. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
  257. $newlang = $object->thirdparty->default_lang;
  258. }
  259. if (!empty($newlang)) {
  260. $outputlangs = new Translate("", $conf);
  261. $outputlangs->setDefaultLang($newlang);
  262. }
  263. $model = $object->model_pdf;
  264. $ret = $object->fetch($id); // Reload to get new records
  265. if ($ret > 0) {
  266. $object->fetch_thirdparty();
  267. }
  268. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  269. }
  270. } else {
  271. $langs->load("errors");
  272. if (count($object->errors) > 0) {
  273. setEventMessages($object->error, $object->errors, 'errors');
  274. } else {
  275. setEventMessages($langs->trans($object->error), null, 'errors');
  276. }
  277. }
  278. } elseif ($action == 'setdate' && $usercancreate) {
  279. $datep = dol_mktime(12, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
  280. if (empty($datep)) {
  281. $error++;
  282. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors');
  283. }
  284. if (!$error) {
  285. $result = $object->set_date($user, $datep);
  286. if ($result > 0 && !empty($object->duree_validite) && !empty($object->fin_validite)) {
  287. $datev = $datep + ($object->duree_validite * 24 * 3600);
  288. $result = $object->set_echeance($user, $datev, 1);
  289. }
  290. if ($result < 0) {
  291. dol_print_error($db, $object->error);
  292. } elseif (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  293. $outputlangs = $langs;
  294. $newlang = '';
  295. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
  296. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  297. if (!empty($newlang)) {
  298. $outputlangs = new Translate("", $conf);
  299. $outputlangs->setDefaultLang($newlang);
  300. }
  301. $model = $object->model_pdf;
  302. $ret = $object->fetch($id); // Reload to get new records
  303. if ($ret > 0) {
  304. $object->fetch_thirdparty();
  305. }
  306. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  307. }
  308. }
  309. } elseif ($action == 'setecheance' && $usercancreate) {
  310. $result = $object->set_echeance($user, dol_mktime(12, 0, 0, GETPOST('echmonth', 'int'), GETPOST('echday', 'int'), GETPOST('echyear', 'int')));
  311. if ($result >= 0) {
  312. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  313. $outputlangs = $langs;
  314. $newlang = '';
  315. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
  316. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  317. if (!empty($newlang)) {
  318. $outputlangs = new Translate("", $conf);
  319. $outputlangs->setDefaultLang($newlang);
  320. }
  321. $model = $object->model_pdf;
  322. $ret = $object->fetch($id); // Reload to get new records
  323. if ($ret > 0) {
  324. $object->fetch_thirdparty();
  325. }
  326. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  327. }
  328. } else {
  329. setEventMessages($object->error, $object->errors, 'errors');
  330. }
  331. } elseif ($action == 'setdate_livraison' && $usercancreate) {
  332. $result = $object->setDeliveryDate($user, dol_mktime(12, 0, 0, GETPOST('date_livraisonmonth', 'int'), GETPOST('date_livraisonday', 'int'), GETPOST('date_livraisonyear', 'int')));
  333. if ($result < 0) {
  334. dol_print_error($db, $object->error);
  335. }
  336. } elseif ($action == 'setref_client' && $usercancreate) {
  337. // Positionne ref client
  338. $result = $object->set_ref_client($user, GETPOST('ref_client'));
  339. if ($result < 0) {
  340. setEventMessages($object->error, $object->errors, 'errors');
  341. }
  342. } elseif ($action == 'set_incoterms' && isModEnabled('incoterm') && $usercancreate) {
  343. // Set incoterm
  344. $result = $object->setIncoterms(GETPOST('incoterm_id', 'int'), GETPOST('location_incoterms', 'alpha'));
  345. } elseif ($action == 'add' && $usercancreate) {
  346. // Create proposal
  347. $object->socid = $socid;
  348. $object->fetch_thirdparty();
  349. $datep = dol_mktime(12, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear'));
  350. $date_delivery = dol_mktime(12, 0, 0, GETPOST('date_livraisonmonth'), GETPOST('date_livraisonday'), GETPOST('date_livraisonyear'));
  351. $duration = GETPOST('duree_validite', 'int');
  352. if (empty($datep)) {
  353. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("DatePropal")), null, 'errors');
  354. $action = 'create';
  355. $error++;
  356. }
  357. if (empty($duration)) {
  358. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ValidityDuration")), null, 'errors');
  359. $action = 'create';
  360. $error++;
  361. }
  362. if ($socid < 1) {
  363. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Customer")), null, 'errors');
  364. $action = 'create';
  365. $error++;
  366. }
  367. if (!$error) {
  368. $db->begin();
  369. // If we select proposal to clone during creation (when option PROPAL_CLONE_ON_CREATE_PAGE is on)
  370. if (GETPOST('createmode') == 'copy' && GETPOST('copie_propal')) {
  371. if ($object->fetch(GETPOST('copie_propal', 'int')) > 0) {
  372. $object->ref = GETPOST('ref');
  373. $object->datep = $datep;
  374. $object->date = $datep;
  375. $object->date_livraison = $date_delivery; // deprecated
  376. $object->delivery_date = $date_delivery;
  377. $object->availability_id = GETPOST('availability_id');
  378. $object->demand_reason_id = GETPOST('demand_reason_id');
  379. $object->fk_delivery_address = GETPOST('fk_address', 'int');
  380. $object->shipping_method_id = GETPOST('shipping_method_id', 'int');
  381. $object->warehouse_id = GETPOST('warehouse_id', 'int');
  382. $object->duree_validite = $duration;
  383. $object->cond_reglement_id = GETPOST('cond_reglement_id');
  384. $object->deposit_percent = GETPOST('cond_reglement_id_deposit_percent', 'alpha');
  385. $object->mode_reglement_id = GETPOST('mode_reglement_id');
  386. $object->fk_account = GETPOST('fk_account', 'int');
  387. $object->remise_absolue = price2num(GETPOST('remise_absolue'), 'MU', 2); // deprecated
  388. $object->remise_percent = price2num(GETPOST('remise_percent'), '', 2);
  389. $object->socid = GETPOST('socid', 'int');
  390. $object->contact_id = GETPOST('contactid', 'int');
  391. $object->fk_project = GETPOST('projectid', 'int');
  392. $object->model_pdf = GETPOST('model', 'alphanohtml');
  393. $object->author = $user->id; // deprecated
  394. $object->user_author_id = $user->id;
  395. $object->note_private = GETPOST('note_private', 'restricthtml');
  396. $object->note_public = GETPOST('note_public', 'restricthtml');
  397. $object->statut = Propal::STATUS_DRAFT;
  398. $object->fk_incoterms = GETPOST('incoterm_id', 'int');
  399. $object->location_incoterms = GETPOST('location_incoterms', 'alpha');
  400. } else {
  401. setEventMessages($langs->trans("ErrorFailedToCopyProposal", GETPOST('copie_propal')), null, 'errors');
  402. }
  403. } else {
  404. $object->ref = GETPOST('ref');
  405. $object->ref_client = GETPOST('ref_client');
  406. $object->datep = $datep;
  407. $object->date = $datep;
  408. $object->date_livraison = $date_delivery;
  409. $object->delivery_date = $date_delivery;
  410. $object->availability_id = GETPOST('availability_id', 'int');
  411. $object->demand_reason_id = GETPOST('demand_reason_id', 'int');
  412. $object->fk_delivery_address = GETPOST('fk_address', 'int');
  413. $object->shipping_method_id = GETPOST('shipping_method_id', 'int');
  414. $object->warehouse_id = GETPOST('warehouse_id', 'int');
  415. $object->duree_validite = price2num(GETPOST('duree_validite', 'alpha'));
  416. $object->cond_reglement_id = GETPOST('cond_reglement_id', 'int');
  417. $object->deposit_percent = GETPOST('cond_reglement_id_deposit_percent', 'alpha');
  418. $object->mode_reglement_id = GETPOST('mode_reglement_id', 'int');
  419. $object->fk_account = GETPOST('fk_account', 'int');
  420. $object->contact_id = GETPOST('contactid', 'int');
  421. $object->fk_project = GETPOST('projectid', 'int');
  422. $object->model_pdf = GETPOST('model');
  423. $object->author = $user->id; // deprecated
  424. $object->note_private = GETPOST('note_private', 'restricthtml');
  425. $object->note_public = GETPOST('note_public', 'restricthtml');
  426. $object->fk_incoterms = GETPOST('incoterm_id', 'int');
  427. $object->location_incoterms = GETPOST('location_incoterms', 'alpha');
  428. $object->origin = GETPOST('origin');
  429. $object->origin_id = GETPOST('originid');
  430. // Multicurrency
  431. if (isModEnabled("multicurrency")) {
  432. $object->multicurrency_code = GETPOST('multicurrency_code', 'alpha');
  433. }
  434. // Fill array 'array_options' with data from add form
  435. $ret = $extrafields->setOptionalsFromPost(null, $object);
  436. if ($ret < 0) {
  437. $error++;
  438. $action = 'create';
  439. }
  440. }
  441. if (!$error) {
  442. if ($origin && $originid) {
  443. // Parse element/subelement (ex: project_task)
  444. $element = $subelement = $origin;
  445. if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
  446. $element = $regs [1];
  447. $subelement = $regs [2];
  448. }
  449. // For compatibility
  450. if ($element == 'order') {
  451. $element = $subelement = 'commande';
  452. }
  453. if ($element == 'propal') {
  454. $element = 'comm/propal';
  455. $subelement = 'propal';
  456. }
  457. if ($element == 'contract') {
  458. $element = $subelement = 'contrat';
  459. }
  460. if ($element == 'inter') {
  461. $element = $subelement = 'ficheinter';
  462. }
  463. if ($element == 'shipping') {
  464. $element = $subelement = 'expedition';
  465. }
  466. $object->origin = $origin;
  467. $object->origin_id = $originid;
  468. // Possibility to add external linked objects with hooks
  469. $object->linked_objects [$object->origin] = $object->origin_id;
  470. if (is_array($_POST['other_linked_objects']) && !empty($_POST['other_linked_objects'])) {
  471. $object->linked_objects = array_merge($object->linked_objects, $_POST['other_linked_objects']);
  472. }
  473. $id = $object->create($user);
  474. if ($id > 0) {
  475. dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
  476. $classname = ucfirst($subelement);
  477. $srcobject = new $classname($db);
  478. dol_syslog("Try to find source object origin=".$object->origin." originid=".$object->origin_id." to add lines");
  479. $result = $srcobject->fetch($object->origin_id);
  480. if ($result > 0) {
  481. $lines = $srcobject->lines;
  482. if (empty($lines) && method_exists($srcobject, 'fetch_lines')) {
  483. $srcobject->fetch_lines();
  484. $lines = $srcobject->lines;
  485. }
  486. $fk_parent_line = 0;
  487. $num = count($lines);
  488. for ($i = 0; $i < $num; $i++) {
  489. $label = (!empty($lines[$i]->label) ? $lines[$i]->label : '');
  490. $desc = (!empty($lines[$i]->desc) ? $lines[$i]->desc : $lines[$i]->libelle);
  491. // Positive line
  492. $product_type = ($lines[$i]->product_type ? $lines[$i]->product_type : 0);
  493. // Date start
  494. $date_start = false;
  495. if ($lines[$i]->date_debut_prevue) {
  496. $date_start = $lines[$i]->date_debut_prevue;
  497. }
  498. if ($lines[$i]->date_debut_reel) {
  499. $date_start = $lines[$i]->date_debut_reel;
  500. }
  501. if ($lines[$i]->date_start) {
  502. $date_start = $lines[$i]->date_start;
  503. }
  504. // Date end
  505. $date_end = false;
  506. if ($lines[$i]->date_fin_prevue) {
  507. $date_end = $lines[$i]->date_fin_prevue;
  508. }
  509. if ($lines[$i]->date_fin_reel) {
  510. $date_end = $lines[$i]->date_fin_reel;
  511. }
  512. if ($lines[$i]->date_end) {
  513. $date_end = $lines[$i]->date_end;
  514. }
  515. // Reset fk_parent_line for no child products and special product
  516. if (($lines[$i]->product_type != 9 && empty($lines[$i]->fk_parent_line)) || $lines[$i]->product_type == 9) {
  517. $fk_parent_line = 0;
  518. }
  519. // Extrafields
  520. if (method_exists($lines[$i], 'fetch_optionals')) {
  521. $lines[$i]->fetch_optionals();
  522. $array_options = $lines[$i]->array_options;
  523. }
  524. $tva_tx = $lines[$i]->tva_tx;
  525. if (!empty($lines[$i]->vat_src_code) && !preg_match('/\(/', $tva_tx)) {
  526. $tva_tx .= ' ('.$lines[$i]->vat_src_code.')';
  527. }
  528. $result = $object->addline($desc, $lines[$i]->subprice, $lines[$i]->qty, $tva_tx, $lines[$i]->localtax1_tx, $lines[$i]->localtax2_tx, $lines[$i]->fk_product, $lines[$i]->remise_percent, 'HT', 0, $lines[$i]->info_bits, $product_type, $lines[$i]->rang, $lines[$i]->special_code, $fk_parent_line, $lines[$i]->fk_fournprice, $lines[$i]->pa_ht, $label, $date_start, $date_end, $array_options, $lines[$i]->fk_unit);
  529. if ($result > 0) {
  530. $lineid = $result;
  531. } else {
  532. $lineid = 0;
  533. $error++;
  534. break;
  535. }
  536. // Defined the new fk_parent_line
  537. if ($result > 0 && $lines[$i]->product_type == 9) {
  538. $fk_parent_line = $result;
  539. }
  540. }
  541. // Hooks
  542. $parameters = array('objFrom' => $srcobject);
  543. $reshook = $hookmanager->executeHooks('createFrom', $parameters, $object, $action); // Note that $action and $object may have been
  544. // modified by hook
  545. if ($reshook < 0) {
  546. $error++;
  547. }
  548. } else {
  549. setEventMessages($srcobject->error, $srcobject->errors, 'errors');
  550. $error++;
  551. }
  552. } else {
  553. setEventMessages($object->error, $object->errors, 'errors');
  554. $error++;
  555. }
  556. } else {
  557. // Standard creation
  558. $id = $object->create($user);
  559. }
  560. if ($id > 0) {
  561. // Insert default contacts if defined
  562. if (GETPOST('contactid') > 0) {
  563. $result = $object->add_contact(GETPOST('contactid'), 'CUSTOMER', 'external');
  564. if ($result < 0) {
  565. $error++;
  566. setEventMessages($langs->trans("ErrorFailedToAddContact"), null, 'errors');
  567. }
  568. }
  569. if (!empty($conf->global->PROPOSAL_AUTO_ADD_AUTHOR_AS_CONTACT)) {
  570. $result = $object->add_contact($user->id, 'SALESREPFOLL', 'internal');
  571. if ($result < 0) {
  572. $error++;
  573. setEventMessages($langs->trans("ErrorFailedToAddUserAsContact"), null, 'errors');
  574. }
  575. }
  576. if (!$error) {
  577. $db->commit();
  578. // Define output language
  579. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  580. $outputlangs = $langs;
  581. $newlang = '';
  582. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  583. $newlang = GETPOST('lang_id', 'aZ09');
  584. }
  585. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
  586. $newlang = $object->thirdparty->default_lang;
  587. }
  588. if (!empty($newlang)) {
  589. $outputlangs = new Translate("", $conf);
  590. $outputlangs->setDefaultLang($newlang);
  591. }
  592. $model = $object->model_pdf;
  593. $ret = $object->fetch($id); // Reload to get new records
  594. $result = $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  595. if ($result < 0) {
  596. dol_print_error($db, $result);
  597. }
  598. }
  599. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$id);
  600. exit();
  601. } else {
  602. $db->rollback();
  603. $action = 'create';
  604. }
  605. } else {
  606. setEventMessages($object->error, $object->errors, 'errors');
  607. $db->rollback();
  608. $action = 'create';
  609. }
  610. }
  611. }
  612. } elseif ($action == 'classifybilled' && $usercanclose) {
  613. // Classify billed
  614. $db->begin();
  615. $result = $object->classifyBilled($user, 0, '');
  616. if ($result < 0) {
  617. setEventMessages($object->error, $object->errors, 'errors');
  618. $error++;
  619. }
  620. if (!$error) {
  621. $db->commit();
  622. } else {
  623. $db->rollback();
  624. }
  625. } elseif ($action == 'confirm_closeas' && $usercanclose && !GETPOST('cancel', 'alpha')) {
  626. // Close proposal
  627. if (!(GETPOST('statut', 'int') > 0)) {
  628. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("CloseAs")), null, 'errors');
  629. $action = 'closeas';
  630. } elseif (GETPOST('statut', 'int') == $object::STATUS_SIGNED || GETPOST('statut', 'int') == $object::STATUS_NOTSIGNED) {
  631. // prevent browser refresh from closing proposal several times
  632. if ($object->statut == $object::STATUS_VALIDATED || ( ! empty($conf->global->PROPAL_SKIP_ACCEPT_REFUSE) && $object->statut == $object::STATUS_DRAFT)) {
  633. $db->begin();
  634. $result = $object->closeProposal($user, GETPOST('statut', 'int'), GETPOST('note_private', 'restricthtml'));
  635. if ($result < 0) {
  636. setEventMessages($object->error, $object->errors, 'errors');
  637. $error++;
  638. }
  639. $deposit = null;
  640. $locationTarget = '';
  641. $deposit_percent_from_payment_terms = getDictionaryValue('c_payment_term', 'deposit_percent', $object->cond_reglement_id);
  642. if (
  643. !$error && GETPOST('statut', 'int') == $object::STATUS_SIGNED && GETPOST('generate_deposit', 'alpha') == 'on'
  644. && !empty($deposit_percent_from_payment_terms) && isModEnabled('facture') && !empty($user->rights->facture->creer)
  645. ) {
  646. require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
  647. $date = dol_mktime(0, 0, 0, GETPOST('datefmonth', 'int'), GETPOST('datefday', 'int'), GETPOST('datefyear', 'int'));
  648. $forceFields = array();
  649. if (GETPOSTISSET('date_pointoftax')) {
  650. $forceFields['date_pointoftax'] = dol_mktime(0, 0, 0, GETPOST('date_pointoftaxmonth', 'int'), GETPOST('date_pointoftaxday', 'int'), GETPOST('date_pointoftaxyear', 'int'));
  651. }
  652. $deposit = Facture::createDepositFromOrigin($object, $date, GETPOST('cond_reglement_id', 'int'), $user, 0, GETPOST('validate_generated_deposit', 'alpha') == 'on', $forceFields);
  653. if ($deposit) {
  654. setEventMessage('DepositGenerated');
  655. $locationTarget = DOL_URL_ROOT . '/compta/facture/card.php?id=' . $deposit->id;
  656. } else {
  657. $error++;
  658. setEventMessages($object->error, $object->errors, 'errors');
  659. }
  660. }
  661. if (!$error) {
  662. $db->commit();
  663. if ($deposit && empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  664. $ret = $deposit->fetch($deposit->id); // Reload to get new records
  665. $outputlangs = $langs;
  666. if (getDolGlobalInt('MAIN_MULTILANGS')) {
  667. $outputlangs = new Translate('', $conf);
  668. $outputlangs->setDefaultLang($deposit->thirdparty->default_lang);
  669. $outputlangs->load('products');
  670. }
  671. $result = $deposit->generateDocument($deposit->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  672. if ($result < 0) {
  673. setEventMessages($deposit->error, $deposit->errors, 'errors');
  674. }
  675. }
  676. if ($locationTarget) {
  677. header('Location: ' . $locationTarget);
  678. exit;
  679. }
  680. } else {
  681. $db->rollback();
  682. $action = '';
  683. }
  684. }
  685. }
  686. } elseif ($action == 'confirm_reopen' && $usercanclose && !GETPOST('cancel', 'alpha')) {
  687. // Reopen proposal
  688. // prevent browser refresh from reopening proposal several times
  689. if ($object->statut == Propal::STATUS_SIGNED || $object->statut == Propal::STATUS_NOTSIGNED || $object->statut == Propal::STATUS_BILLED) {
  690. $db->begin();
  691. $result = $object->reopen($user, empty($conf->global->PROPAL_SKIP_ACCEPT_REFUSE));
  692. if ($result < 0) {
  693. setEventMessages($object->error, $object->errors, 'errors');
  694. $error++;
  695. }
  696. if (!$error) {
  697. $db->commit();
  698. } else {
  699. $db->rollback();
  700. }
  701. }
  702. } elseif ($action == 'import_lines_from_object'
  703. && $user->rights->propal->creer
  704. && $object->statut == Propal::STATUS_DRAFT
  705. ) {
  706. // add lines from objectlinked
  707. $fromElement = GETPOST('fromelement');
  708. $fromElementid = GETPOST('fromelementid');
  709. $importLines = GETPOST('line_checkbox');
  710. if (!empty($importLines) && is_array($importLines) && !empty($fromElement) && ctype_alpha($fromElement) && !empty($fromElementid)) {
  711. if ($fromElement == 'commande') {
  712. dol_include_once('/'.$fromElement.'/class/'.$fromElement.'.class.php');
  713. $lineClassName = 'OrderLine';
  714. } elseif ($fromElement == 'propal') {
  715. dol_include_once('/comm/'.$fromElement.'/class/'.$fromElement.'.class.php');
  716. $lineClassName = 'PropaleLigne';
  717. }
  718. $nextRang = count($object->lines) + 1;
  719. $importCount = 0;
  720. $error = 0;
  721. foreach ($importLines as $lineId) {
  722. $lineId = intval($lineId);
  723. $originLine = new $lineClassName($db);
  724. if (intval($fromElementid) > 0 && $originLine->fetch($lineId) > 0) {
  725. $originLine->fetch_optionals();
  726. $desc = $originLine->desc;
  727. $pu_ht = $originLine->subprice;
  728. $qty = $originLine->qty;
  729. $txtva = $originLine->tva_tx;
  730. $txlocaltax1 = $originLine->localtax1_tx;
  731. $txlocaltax2 = $originLine->localtax2_tx;
  732. $fk_product = $originLine->fk_product;
  733. $remise_percent = $originLine->remise_percent;
  734. $date_start = $originLine->date_start;
  735. $date_end = $originLine->date_end;
  736. $ventil = 0;
  737. $info_bits = $originLine->info_bits;
  738. $fk_remise_except = $originLine->fk_remise_except;
  739. $price_base_type = 'HT';
  740. $pu_ttc = 0;
  741. $type = $originLine->product_type;
  742. $rang = $nextRang++;
  743. $special_code = $originLine->special_code;
  744. $origin = $originLine->element;
  745. $origin_id = $originLine->id;
  746. $fk_parent_line = 0;
  747. $fk_fournprice = $originLine->fk_fournprice;
  748. $pa_ht = $originLine->pa_ht;
  749. $label = $originLine->label;
  750. $array_options = $originLine->array_options;
  751. $situation_percent = 100;
  752. $fk_prev_id = '';
  753. $fk_unit = $originLine->fk_unit;
  754. $pu_ht_devise = $originLine->multicurrency_subprice;
  755. $res = $object->addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $price_base_type, $pu_ttc, $info_bits, $type, $rang, $special_code, $fk_parent_line, $fk_fournprice, $pa_ht, $label, $date_start, $date_end, $array_options, $fk_unit, $origin, $origin_id, $pu_ht_devise, $fk_remise_except);
  756. if ($res > 0) {
  757. $importCount++;
  758. } else {
  759. $error++;
  760. }
  761. } else {
  762. $error++;
  763. }
  764. }
  765. if ($error) {
  766. setEventMessages($langs->trans('ErrorsOnXLines', $error), null, 'errors');
  767. }
  768. }
  769. }
  770. include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
  771. // Actions to send emails
  772. $actiontypecode = 'AC_OTH_AUTO';
  773. $triggersendname = 'PROPAL_SENTBYMAIL';
  774. $autocopy = 'MAIN_MAIL_AUTOCOPY_PROPOSAL_TO';
  775. $trackid = 'pro'.$object->id;
  776. include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
  777. // Go back to draft
  778. if ($action == 'modif' && $usercancreate) {
  779. $object->setDraft($user);
  780. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  781. // Define output language
  782. $outputlangs = $langs;
  783. if (getDolGlobalInt('MAIN_MULTILANGS')) {
  784. $outputlangs = new Translate("", $conf);
  785. $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $object->thirdparty->default_lang);
  786. $outputlangs->setDefaultLang($newlang);
  787. }
  788. $ret = $object->fetch($id); // Reload to get new records
  789. if ($ret > 0) {
  790. $object->fetch_thirdparty();
  791. }
  792. $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  793. }
  794. } elseif ($action == "setabsolutediscount" && $usercancreate) {
  795. if (GETPOST("remise_id", "int")) {
  796. if ($object->id > 0) {
  797. $result = $object->insert_discount(GETPOST("remise_id", "int"));
  798. if ($result < 0) {
  799. setEventMessages($object->error, $object->errors, 'errors');
  800. }
  801. }
  802. }
  803. } elseif ($action == 'addline' && GETPOST('submitforalllines', 'alpha') && GETPOST('vatforalllines', 'alpha') !== '' && $usercancreate) {
  804. // Define a vat_rate for all lines
  805. $vat_rate = (GETPOST('vatforalllines') ? GETPOST('vatforalllines') : 0);
  806. $vat_rate = str_replace('*', '', $vat_rate);
  807. $localtax1_rate = get_localtax($vat_rate, 1, $object->thirdparty, $mysoc);
  808. $localtax2_rate = get_localtax($vat_rate, 2, $object->thirdparty, $mysoc);
  809. foreach ($object->lines as $line) {
  810. $result = $object->updateline($line->id, $line->subprice, $line->qty, $line->remise_percent, $vat_rate, $localtax1_rate, $localtax2_rate, $line->desc, 'HT', $line->info_bits, $line->special_code, $line->fk_parent_line, 0, $line->fk_fournprice, $line->pa_ht, $line->label, $line->product_type, $line->date_start, $line->date_end, $line->array_options, $line->fk_unit, $line->multicurrency_subprice);
  811. }
  812. } elseif ($action == 'addline' && GETPOST('submitforalllines', 'alpha') && GETPOST('remiseforalllines', 'alpha') !== '' && $usercancreate) {
  813. // Define a discount for all lines
  814. $remise_percent = (GETPOST('remiseforalllines') ? GETPOST('remiseforalllines') : 0);
  815. $remise_percent = str_replace('*', '', $remise_percent);
  816. foreach ($object->lines as $line) {
  817. $result = $object->updateline($line->id, $line->subprice, $line->qty, $remise_percent, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, $line->desc, 'HT', $line->info_bits, $line->special_code, $line->fk_parent_line, 0, $line->fk_fournprice, $line->pa_ht, $line->label, $line->product_type, $line->date_start, $line->date_end, $line->array_options, $line->fk_unit, $line->multicurrency_subprice);
  818. }
  819. } elseif ($action == 'addline' && $usercancreate) { // Add line
  820. // Set if we used free entry or predefined product
  821. $predef = '';
  822. $product_desc = (GETPOSTISSET('dp_desc') ? GETPOST('dp_desc', 'restricthtml') : '');
  823. $price_ht = '';
  824. $price_ht_devise = '';
  825. $price_ttc = '';
  826. $price_ttc_devise = '';
  827. if (GETPOST('price_ht') !== '') {
  828. $price_ht = price2num(GETPOST('price_ht'), 'MU', 2);
  829. }
  830. if (GETPOST('multicurrency_price_ht') !== '') {
  831. $price_ht_devise = price2num(GETPOST('multicurrency_price_ht'), 'CU', 2);
  832. }
  833. if (GETPOST('price_ttc') !== '') {
  834. $price_ttc = price2num(GETPOST('price_ttc'), 'MU', 2);
  835. }
  836. if (GETPOST('multicurrency_price_ttc') !== '') {
  837. $price_ttc_devise = price2num(GETPOST('multicurrency_price_ttc'), 'CU', 2);
  838. }
  839. $prod_entry_mode = GETPOST('prod_entry_mode', 'aZ09');
  840. if ($prod_entry_mode == 'free') {
  841. $idprod = 0;
  842. $tva_tx = (GETPOST('tva_tx', 'alpha') ? price2num(preg_replace('/\s*\(.*\)/', '', GETPOST('tva_tx', 'alpha'))) : 0);
  843. } else {
  844. $idprod = GETPOST('idprod', 'int');
  845. $tva_tx = '';
  846. }
  847. $qty = price2num(GETPOST('qty'.$predef, 'alpha'), 'MS', 2);
  848. $remise_percent = (GETPOSTISSET('remise_percent'.$predef) ? price2num(GETPOST('remise_percent'.$predef, 'alpha'), '', 2) : 0);
  849. if (empty($remise_percent)) {
  850. $remise_percent = 0;
  851. }
  852. // Extrafields
  853. $extralabelsline = $extrafields->fetch_name_optionals_label($object->table_element_line);
  854. $array_options = $extrafields->getOptionalsFromPost($object->table_element_line, $predef);
  855. // Unset extrafield
  856. if (is_array($extralabelsline)) {
  857. // Get extra fields
  858. foreach ($extralabelsline as $key => $value) {
  859. unset($_POST["options_".$key]);
  860. }
  861. }
  862. if ($prod_entry_mode == 'free' && (empty($idprod) || $idprod < 0) && GETPOST('type') < 0) {
  863. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
  864. $error++;
  865. }
  866. if ($prod_entry_mode == 'free' && (empty($idprod) || $idprod < 0) && $price_ht === '' && $price_ht_devise === '' && $price_ttc === '' && $price_ttc_devise === '') { // Unit price can be 0 but not ''. Also price can be negative for proposal.
  867. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("UnitPriceHT")), null, 'errors');
  868. $error++;
  869. }
  870. if ($prod_entry_mode == 'free' && (empty($idprod) || $idprod < 0) && empty($product_desc)) {
  871. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Description")), null, 'errors');
  872. $error++;
  873. }
  874. if (!$error && isModEnabled('variants') && $prod_entry_mode != 'free') {
  875. if ($combinations = GETPOST('combinations', 'array')) {
  876. //Check if there is a product with the given combination
  877. $prodcomb = new ProductCombination($db);
  878. if ($res = $prodcomb->fetchByProductCombination2ValuePairs($idprod, $combinations)) {
  879. $idprod = $res->fk_product_child;
  880. } else {
  881. setEventMessages($langs->trans('ErrorProductCombinationNotFound'), null, 'errors');
  882. $error++;
  883. }
  884. }
  885. }
  886. if (!$error && ($qty >= 0) && (!empty($product_desc) || (!empty($idprod) && $idprod > 0))) {
  887. $pu_ht = 0;
  888. $pu_ttc = 0;
  889. $price_min = 0;
  890. $price_min_ttc = 0;
  891. $price_base_type = (GETPOST('price_base_type', 'alpha') ? GETPOST('price_base_type', 'alpha') : 'HT');
  892. $db->begin();
  893. // $tva_tx can be 'x.x (XXX)'
  894. // Ecrase $pu par celui du produit
  895. // Ecrase $desc par celui du produit
  896. // Ecrase $tva_tx par celui du produit
  897. // Replaces $fk_unit with the product unit
  898. if (!empty($idprod) && $idprod > 0) {
  899. $prod = new Product($db);
  900. $prod->fetch($idprod);
  901. $label = ((GETPOST('product_label') && GETPOST('product_label') != $prod->label) ? GETPOST('product_label') : '');
  902. // Update if prices fields are defined
  903. $tva_tx = get_default_tva($mysoc, $object->thirdparty, $prod->id);
  904. $tva_npr = get_default_npr($mysoc, $object->thirdparty, $prod->id);
  905. if (empty($tva_tx)) {
  906. $tva_npr = 0;
  907. }
  908. // Price unique per product
  909. $pu_ht = $prod->price;
  910. $pu_ttc = $prod->price_ttc;
  911. $price_min = $prod->price_min;
  912. $price_min_ttc = $prod->price_min_ttc;
  913. $price_base_type = $prod->price_base_type;
  914. // If price per segment
  915. if (!empty($conf->global->PRODUIT_MULTIPRICES) && $object->thirdparty->price_level) {
  916. $pu_ht = $prod->multiprices[$object->thirdparty->price_level];
  917. $pu_ttc = $prod->multiprices_ttc[$object->thirdparty->price_level];
  918. $price_min = $prod->multiprices_min[$object->thirdparty->price_level];
  919. $price_min_ttc = $prod->multiprices_min_ttc[$object->thirdparty->price_level];
  920. $price_base_type = $prod->multiprices_base_type[$object->thirdparty->price_level];
  921. if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) { // using this option is a bug. kept for backward compatibility
  922. if (isset($prod->multiprices_tva_tx[$object->thirdparty->price_level])) {
  923. $tva_tx = $prod->multiprices_tva_tx[$object->thirdparty->price_level];
  924. }
  925. if (isset($prod->multiprices_recuperableonly[$object->thirdparty->price_level])) {
  926. $tva_npr = $prod->multiprices_recuperableonly[$object->thirdparty->price_level];
  927. }
  928. }
  929. } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
  930. // If price per customer
  931. require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
  932. $prodcustprice = new Productcustomerprice($db);
  933. $filter = array('t.fk_product' => $prod->id, 't.fk_soc' => $object->thirdparty->id);
  934. $result = $prodcustprice->fetchAll('', '', 0, 0, $filter);
  935. if ($result) {
  936. // If there is some prices specific to the customer
  937. if (count($prodcustprice->lines) > 0) {
  938. $pu_ht = price($prodcustprice->lines[0]->price);
  939. $pu_ttc = price($prodcustprice->lines[0]->price_ttc);
  940. $price_min = price($prodcustprice->lines[0]->price_min);
  941. $price_min_ttc = price($prodcustprice->lines[0]->price_min_ttc);
  942. $price_base_type = $prodcustprice->lines[0]->price_base_type;
  943. $tva_tx = ($prodcustprice->lines[0]->default_vat_code ? $prodcustprice->lines[0]->tva_tx.' ('.$prodcustprice->lines[0]->default_vat_code.' )' : $prodcustprice->lines[0]->tva_tx);
  944. if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) {
  945. $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')';
  946. }
  947. $tva_npr = $prodcustprice->lines[0]->recuperableonly;
  948. if (empty($tva_tx)) {
  949. $tva_npr = 0;
  950. }
  951. }
  952. }
  953. } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) {
  954. // If price per quantity
  955. if ($prod->prices_by_qty[0]) { // yes, this product has some prices per quantity
  956. // Search the correct price into loaded array product_price_by_qty using id of array retrieved into POST['pqp'].
  957. $pqp = GETPOST('pbq', 'int');
  958. // Search price into product_price_by_qty from $prod->id
  959. foreach ($prod->prices_by_qty_list[0] as $priceforthequantityarray) {
  960. if ($priceforthequantityarray['rowid'] != $pqp) {
  961. continue;
  962. }
  963. // We found the price
  964. if ($priceforthequantityarray['price_base_type'] == 'HT') {
  965. $pu_ht = $priceforthequantityarray['unitprice'];
  966. } else {
  967. $pu_ttc = $priceforthequantityarray['unitprice'];
  968. }
  969. // Note: the remise_percent or price by qty is used to set data on form, so we will use value from POST.
  970. break;
  971. }
  972. }
  973. } elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY_MULTIPRICES)) {
  974. // If price per quantity and customer
  975. if ($prod->prices_by_qty[$object->thirdparty->price_level]) { // yes, this product has some prices per quantity
  976. // Search the correct price into loaded array product_price_by_qty using id of array retrieved into POST['pqp'].
  977. $pqp = GETPOST('pbq', 'int');
  978. // Search price into product_price_by_qty from $prod->id
  979. foreach ($prod->prices_by_qty_list[$object->thirdparty->price_level] as $priceforthequantityarray) {
  980. if ($priceforthequantityarray['rowid'] != $pqp) {
  981. continue;
  982. }
  983. // We found the price
  984. if ($priceforthequantityarray['price_base_type'] == 'HT') {
  985. $pu_ht = $priceforthequantityarray['unitprice'];
  986. } else {
  987. $pu_ttc = $priceforthequantityarray['unitprice'];
  988. }
  989. // Note: the remise_percent or price by qty is used to set data on form, so we will use value from POST.
  990. break;
  991. }
  992. }
  993. }
  994. $tmpvat = price2num(preg_replace('/\s*\(.*\)/', '', $tva_tx));
  995. $tmpprodvat = price2num(preg_replace('/\s*\(.*\)/', '', $prod->tva_tx));
  996. // Set unit price to use
  997. if (!empty($price_ht) || $price_ht === '0') {
  998. $pu_ht = price2num($price_ht, 'MU');
  999. $pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU');
  1000. } elseif (!empty($price_ttc) || $price_ttc === '0') {
  1001. $pu_ttc = price2num($price_ttc, 'MU');
  1002. $pu_ht = price2num($pu_ttc / (1 + ($tmpvat / 100)), 'MU');
  1003. } elseif ($tmpvat != $tmpprodvat) {
  1004. // Is this still used ?
  1005. if ($price_base_type != 'HT') {
  1006. $pu_ht = price2num($pu_ttc / (1 + ($tmpvat / 100)), 'MU');
  1007. } else {
  1008. $pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU');
  1009. }
  1010. }
  1011. $desc = '';
  1012. // Define output language
  1013. if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) {
  1014. $outputlangs = $langs;
  1015. $newlang = '';
  1016. if (empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  1017. $newlang = GETPOST('lang_id', 'aZ09');
  1018. }
  1019. if (empty($newlang)) {
  1020. $newlang = $object->thirdparty->default_lang;
  1021. }
  1022. if (!empty($newlang)) {
  1023. $outputlangs = new Translate("", $conf);
  1024. $outputlangs->setDefaultLang($newlang);
  1025. }
  1026. $desc = (!empty($prod->multilangs[$outputlangs->defaultlang]["description"])) ? $prod->multilangs[$outputlangs->defaultlang]["description"] : $prod->description;
  1027. } else {
  1028. $desc = $prod->description;
  1029. }
  1030. //If text set in desc is the same as product description (as now it's preloaded) whe add it only one time
  1031. if ($product_desc==$desc && !empty($conf->global->PRODUIT_AUTOFILL_DESC)) {
  1032. $product_desc='';
  1033. }
  1034. if (!empty($product_desc) && !empty($conf->global->MAIN_NO_CONCAT_DESCRIPTION)) {
  1035. $desc = $product_desc;
  1036. } else {
  1037. $desc = dol_concatdesc($desc, $product_desc, '', !empty($conf->global->MAIN_CHANGE_ORDER_CONCAT_DESCRIPTION));
  1038. }
  1039. // Add custom code and origin country into description
  1040. if (empty($conf->global->MAIN_PRODUCT_DISABLE_CUSTOMCOUNTRYCODE) && (!empty($prod->customcode) || !empty($prod->country_code))) {
  1041. $tmptxt = '(';
  1042. // Define output language
  1043. if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE)) {
  1044. $outputlangs = $langs;
  1045. $newlang = '';
  1046. if (empty($newlang) && GETPOST('lang_id', 'alpha')) {
  1047. $newlang = GETPOST('lang_id', 'alpha');
  1048. }
  1049. if (empty($newlang)) {
  1050. $newlang = $object->thirdparty->default_lang;
  1051. }
  1052. if (!empty($newlang)) {
  1053. $outputlangs = new Translate("", $conf);
  1054. $outputlangs->setDefaultLang($newlang);
  1055. $outputlangs->load('products');
  1056. }
  1057. if (!empty($prod->customcode)) {
  1058. $tmptxt .= $outputlangs->transnoentitiesnoconv("CustomCode").': '.$prod->customcode;
  1059. }
  1060. if (!empty($prod->customcode) && !empty($prod->country_code)) {
  1061. $tmptxt .= ' - ';
  1062. }
  1063. if (!empty($prod->country_code)) {
  1064. $tmptxt .= $outputlangs->transnoentitiesnoconv("CountryOrigin").': '.getCountry($prod->country_code, 0, $db, $outputlangs, 0);
  1065. }
  1066. } else {
  1067. if (!empty($prod->customcode)) {
  1068. $tmptxt .= $langs->transnoentitiesnoconv("CustomCode").': '.$prod->customcode;
  1069. }
  1070. if (!empty($prod->customcode) && !empty($prod->country_code)) {
  1071. $tmptxt .= ' - ';
  1072. }
  1073. if (!empty($prod->country_code)) {
  1074. $tmptxt .= $langs->transnoentitiesnoconv("CountryOrigin").': '.getCountry($prod->country_code, 0, $db, $langs, 0);
  1075. }
  1076. }
  1077. $tmptxt .= ')';
  1078. $desc = dol_concatdesc($desc, $tmptxt);
  1079. }
  1080. $type = $prod->type;
  1081. $fk_unit = $prod->fk_unit;
  1082. } else {
  1083. $pu_ht = price2num($price_ht, 'MU');
  1084. $pu_ttc = price2num($price_ttc, 'MU');
  1085. $tva_npr = (preg_match('/\*/', $tva_tx) ? 1 : 0);
  1086. if (empty($tva_tx)) {
  1087. $tva_npr = 0;
  1088. }
  1089. $tva_tx = str_replace('*', '', $tva_tx);
  1090. $label = (GETPOST('product_label') ? GETPOST('product_label') : '');
  1091. $desc = $product_desc;
  1092. $type = GETPOST('type');
  1093. $fk_unit = GETPOST('units', 'alpha');
  1094. $pu_ht_devise = price2num($price_ht_devise, 'MU');
  1095. $pu_ttc_devise = price2num($price_ttc_devise, 'MU');
  1096. if ($pu_ttc && !$pu_ht) {
  1097. $price_base_type = 'TTC';
  1098. }
  1099. }
  1100. // Margin
  1101. $fournprice = price2num(GETPOST('fournprice'.$predef) ? GETPOST('fournprice'.$predef) : '');
  1102. $buyingprice = price2num(GETPOST('buying_price'.$predef) != '' ? GETPOST('buying_price'.$predef) : ''); // If buying_price is '0', we muste keep this value
  1103. $date_start = dol_mktime(GETPOST('date_start'.$predef.'hour'), GETPOST('date_start'.$predef.'min'), GETPOST('date_start'.$predef.'sec'), GETPOST('date_start'.$predef.'month'), GETPOST('date_start'.$predef.'day'), GETPOST('date_start'.$predef.'year'));
  1104. $date_end = dol_mktime(GETPOST('date_end'.$predef.'hour'), GETPOST('date_end'.$predef.'min'), GETPOST('date_end'.$predef.'sec'), GETPOST('date_end'.$predef.'month'), GETPOST('date_end'.$predef.'day'), GETPOST('date_end'.$predef.'year'));
  1105. // Prepare a price equivalent for minimum price check
  1106. $pu_equivalent = $pu_ht;
  1107. $pu_equivalent_ttc = $pu_ttc;
  1108. $currency_tx = $object->multicurrency_tx;
  1109. // Check if we have a foreing currency
  1110. // If so, we update the pu_equiv as the equivalent price in base currency
  1111. if ($pu_ht == '' && $pu_ht_devise != '' && $currency_tx != '') {
  1112. $pu_equivalent = $pu_ht_devise * $currency_tx;
  1113. }
  1114. if ($pu_ttc == '' && $pu_ttc_devise != '' && $currency_tx != '') {
  1115. $pu_equivalent_ttc = $pu_ttc_devise * $currency_tx;
  1116. }
  1117. // Local Taxes
  1118. $localtax1_tx = get_localtax($tva_tx, 1, $object->thirdparty, $tva_npr);
  1119. $localtax2_tx = get_localtax($tva_tx, 2, $object->thirdparty, $tva_npr);
  1120. $info_bits = 0;
  1121. if ($tva_npr) {
  1122. $info_bits |= 0x01;
  1123. }
  1124. //var_dump(price2num($price_min)); var_dump(price2num($pu_ht)); var_dump($remise_percent);
  1125. //var_dump(price2num($price_min_ttc)); var_dump(price2num($pu_ttc)); var_dump($remise_percent);exit;
  1126. if ($usermustrespectpricemin) {
  1127. if ($pu_equivalent && $price_min && ((price2num($pu_equivalent) * (1 - $remise_percent / 100)) < price2num($price_min))) {
  1128. $mesg = $langs->trans("CantBeLessThanMinPrice", price(price2num($price_min, 'MU'), 0, $langs, 0, 0, -1, $conf->currency));
  1129. setEventMessages($mesg, null, 'errors');
  1130. $error++;
  1131. } elseif ($pu_equivalent_ttc && $price_min_ttc && ((price2num($pu_equivalent_ttc) * (1 - $remise_percent / 100)) < price2num($price_min_ttc))) {
  1132. $mesg = $langs->trans("CantBeLessThanMinPrice", price(price2num($price_min_ttc, 'MU'), 0, $langs, 0, 0, -1, $conf->currency));
  1133. setEventMessages($mesg, null, 'errors');
  1134. $error++;
  1135. }
  1136. }
  1137. if (!$error) {
  1138. // Insert line
  1139. $result = $object->addline($desc, $pu_ht, $qty, $tva_tx, $localtax1_tx, $localtax2_tx, $idprod, $remise_percent, $price_base_type, $pu_ttc, $info_bits, $type, min($rank, count($object->lines) + 1), 0, GETPOST('fk_parent_line'), $fournprice, $buyingprice, $label, $date_start, $date_end, $array_options, $fk_unit, '', 0, $pu_ht_devise);
  1140. if ($result > 0) {
  1141. $db->commit();
  1142. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  1143. // Define output language
  1144. $outputlangs = $langs;
  1145. if (getDolGlobalInt('MAIN_MULTILANGS')) {
  1146. $outputlangs = new Translate("", $conf);
  1147. $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $object->thirdparty->default_lang);
  1148. $outputlangs->setDefaultLang($newlang);
  1149. }
  1150. $ret = $object->fetch($id); // Reload to get new records
  1151. if ($ret > 0) {
  1152. $object->fetch_thirdparty();
  1153. }
  1154. $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  1155. }
  1156. unset($_POST['prod_entry_mode']);
  1157. unset($_POST['qty']);
  1158. unset($_POST['type']);
  1159. unset($_POST['remise_percent']);
  1160. unset($_POST['price_ht']);
  1161. unset($_POST['multicurrency_price_ht']);
  1162. unset($_POST['price_ttc']);
  1163. unset($_POST['tva_tx']);
  1164. unset($_POST['product_ref']);
  1165. unset($_POST['product_label']);
  1166. unset($_POST['product_desc']);
  1167. unset($_POST['fournprice']);
  1168. unset($_POST['buying_price']);
  1169. unset($_POST['np_marginRate']);
  1170. unset($_POST['np_markRate']);
  1171. unset($_POST['dp_desc']);
  1172. unset($_POST['idprod']);
  1173. unset($_POST['units']);
  1174. unset($_POST['date_starthour']);
  1175. unset($_POST['date_startmin']);
  1176. unset($_POST['date_startsec']);
  1177. unset($_POST['date_startday']);
  1178. unset($_POST['date_startmonth']);
  1179. unset($_POST['date_startyear']);
  1180. unset($_POST['date_endhour']);
  1181. unset($_POST['date_endmin']);
  1182. unset($_POST['date_endsec']);
  1183. unset($_POST['date_endday']);
  1184. unset($_POST['date_endmonth']);
  1185. unset($_POST['date_endyear']);
  1186. } else {
  1187. $db->rollback();
  1188. setEventMessages($object->error, $object->errors, 'errors');
  1189. }
  1190. }
  1191. }
  1192. } elseif ($action == 'updateline' && $usercancreate && GETPOST('save')) {
  1193. // Update a line within proposal
  1194. // Define info_bits
  1195. $info_bits = 0;
  1196. if (preg_match('/\*/', GETPOST('tva_tx'))) {
  1197. $info_bits |= 0x01;
  1198. }
  1199. // Clean parameters
  1200. $description = dol_htmlcleanlastbr(GETPOST('product_desc', 'restricthtml'));
  1201. // Define vat_rate
  1202. $vat_rate = (GETPOST('tva_tx') ? GETPOST('tva_tx') : 0);
  1203. $vat_rate = str_replace('*', '', $vat_rate);
  1204. $localtax1_rate = get_localtax($vat_rate, 1, $object->thirdparty, $mysoc);
  1205. $localtax2_rate = get_localtax($vat_rate, 2, $object->thirdparty, $mysoc);
  1206. $pu_ht = price2num(GETPOST('price_ht'), '', 2);
  1207. $pu_ttc = price2num(GETPOST('price_ttc'), '', 2);
  1208. // Add buying price
  1209. $fournprice = price2num(GETPOST('fournprice') ? GETPOST('fournprice') : '');
  1210. $buyingprice = price2num(GETPOST('buying_price') != '' ? GETPOST('buying_price') : ''); // If buying_price is '0', we muste keep this value
  1211. $pu_ht_devise = price2num(GETPOST('multicurrency_subprice'), '', 2);
  1212. $pu_ttc_devise = price2num(GETPOST('multicurrency_subprice_ttc'), '', 2);
  1213. $date_start = dol_mktime(GETPOST('date_starthour'), GETPOST('date_startmin'), GETPOST('date_startsec'), GETPOST('date_startmonth'), GETPOST('date_startday'), GETPOST('date_startyear'));
  1214. $date_end = dol_mktime(GETPOST('date_endhour'), GETPOST('date_endmin'), GETPOST('date_endsec'), GETPOST('date_endmonth'), GETPOST('date_endday'), GETPOST('date_endyear'));
  1215. $remise_percent = price2num(GETPOST('remise_percent'), '', 2);
  1216. // Prepare a price equivalent for minimum price check
  1217. $pu_equivalent = $pu_ht;
  1218. $pu_equivalent_ttc = $pu_ttc;
  1219. $currency_tx = $object->multicurrency_tx;
  1220. // Check if we have a foreing currency
  1221. // If so, we update the pu_equiv as the equivalent price in base currency
  1222. if ($pu_ht == '' && $pu_ht_devise != '' && $currency_tx != '') {
  1223. $pu_equivalent = $pu_ht_devise * $currency_tx;
  1224. }
  1225. if ($pu_ttc == '' && $pu_ttc_devise != '' && $currency_tx != '') {
  1226. $pu_equivalent_ttc = $pu_ttc_devise * $currency_tx;
  1227. }
  1228. // Extrafields
  1229. $extralabelsline = $extrafields->fetch_name_optionals_label($object->table_element_line);
  1230. $array_options = $extrafields->getOptionalsFromPost($object->table_element_line);
  1231. // Unset extrafield
  1232. if (is_array($extralabelsline)) {
  1233. // Get extra fields
  1234. foreach ($extralabelsline as $key => $value) {
  1235. unset($_POST["options_".$key]);
  1236. }
  1237. }
  1238. // Define special_code for special lines
  1239. $special_code = GETPOST('special_code', 'int');
  1240. if (!GETPOST('qty')) {
  1241. $special_code = 3;
  1242. }
  1243. // Check minimum price
  1244. $productid = GETPOST('productid', 'int');
  1245. if (!empty($productid)) {
  1246. $product = new Product($db);
  1247. $res = $product->fetch($productid);
  1248. $type = $product->type;
  1249. $label = ((GETPOST('update_label') && GETPOST('product_label')) ? GETPOST('product_label') : '');
  1250. $price_min = $product->price_min;
  1251. if (!empty($conf->global->PRODUIT_MULTIPRICES) && !empty($object->thirdparty->price_level)) {
  1252. $price_min = $product->multiprices_min[$object->thirdparty->price_level];
  1253. }
  1254. $price_min_ttc = $product->price_min_ttc;
  1255. if (!empty($conf->global->PRODUIT_MULTIPRICES) && !empty($object->thirdparty->price_level)) {
  1256. $price_min_ttc = $product->multiprices_min_ttc[$object->thirdparty->price_level];
  1257. }
  1258. //var_dump(price2num($price_min)); var_dump(price2num($pu_ht)); var_dump($remise_percent);
  1259. //var_dump(price2num($price_min_ttc)); var_dump(price2num($pu_ttc)); var_dump($remise_percent);exit;
  1260. if ($usermustrespectpricemin) {
  1261. if ($pu_equivalent && $price_min && ((price2num($pu_equivalent) * (1 - $remise_percent / 100)) < price2num($price_min))) {
  1262. $mesg = $langs->trans("CantBeLessThanMinPrice", price(price2num($price_min, 'MU'), 0, $langs, 0, 0, -1, $conf->currency));
  1263. setEventMessages($mesg, null, 'errors');
  1264. $error++;
  1265. $action = 'editline';
  1266. } elseif ($pu_equivalent_ttc && $price_min_ttc && ((price2num($pu_equivalent_ttc) * (1 - $remise_percent / 100)) < price2num($price_min_ttc))) {
  1267. $mesg = $langs->trans("CantBeLessThanMinPrice", price(price2num($price_min_ttc, 'MU'), 0, $langs, 0, 0, -1, $conf->currency));
  1268. setEventMessages($mesg, null, 'errors');
  1269. $error++;
  1270. $action = 'editline';
  1271. }
  1272. }
  1273. } else {
  1274. $type = GETPOST('type');
  1275. $label = (GETPOST('product_label') ? GETPOST('product_label') : '');
  1276. // Check parameters
  1277. if (GETPOST('type') < 0) {
  1278. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
  1279. $error++;
  1280. }
  1281. }
  1282. if (!$error) {
  1283. $db->begin();
  1284. if (empty($user->rights->margins->creer)) {
  1285. foreach ($object->lines as &$line) {
  1286. if ($line->id == GETPOST('lineid', 'int')) {
  1287. $fournprice = $line->fk_fournprice;
  1288. $buyingprice = $line->pa_ht;
  1289. break;
  1290. }
  1291. }
  1292. }
  1293. $qty = price2num(GETPOST('qty', 'alpha'), 'MS');
  1294. $pu = $pu_ht;
  1295. $price_base_type = 'HT';
  1296. if (empty($pu) && !empty($pu_ttc)) {
  1297. $pu = $pu_ttc;
  1298. $price_base_type = 'TTC';
  1299. }
  1300. $result = $object->updateline(GETPOST('lineid', 'int'), $pu, $qty, $remise_percent, $vat_rate, $localtax1_rate, $localtax2_rate, $description, $price_base_type, $info_bits, $special_code, GETPOST('fk_parent_line'), 0, $fournprice, $buyingprice, $label, $type, $date_start, $date_end, $array_options, GETPOST("units"), $pu_ht_devise);
  1301. if ($result >= 0) {
  1302. $db->commit();
  1303. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  1304. // Define output language
  1305. $outputlangs = $langs;
  1306. if (getDolGlobalInt('MAIN_MULTILANGS')) {
  1307. $outputlangs = new Translate("", $conf);
  1308. $newlang = (GETPOST('lang_id', 'aZ09') ? GETPOST('lang_id', 'aZ09') : $object->thirdparty->default_lang);
  1309. $outputlangs->setDefaultLang($newlang);
  1310. }
  1311. $ret = $object->fetch($id); // Reload to get new records
  1312. if ($ret > 0) {
  1313. $object->fetch_thirdparty();
  1314. }
  1315. $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  1316. }
  1317. unset($_POST['qty']);
  1318. unset($_POST['type']);
  1319. unset($_POST['productid']);
  1320. unset($_POST['remise_percent']);
  1321. unset($_POST['price_ht']);
  1322. unset($_POST['multicurrency_price_ht']);
  1323. unset($_POST['price_ttc']);
  1324. unset($_POST['tva_tx']);
  1325. unset($_POST['product_ref']);
  1326. unset($_POST['product_label']);
  1327. unset($_POST['product_desc']);
  1328. unset($_POST['fournprice']);
  1329. unset($_POST['buying_price']);
  1330. unset($_POST['date_starthour']);
  1331. unset($_POST['date_startmin']);
  1332. unset($_POST['date_startsec']);
  1333. unset($_POST['date_startday']);
  1334. unset($_POST['date_startmonth']);
  1335. unset($_POST['date_startyear']);
  1336. unset($_POST['date_endhour']);
  1337. unset($_POST['date_endmin']);
  1338. unset($_POST['date_endsec']);
  1339. unset($_POST['date_endday']);
  1340. unset($_POST['date_endmonth']);
  1341. unset($_POST['date_endyear']);
  1342. } else {
  1343. $db->rollback();
  1344. setEventMessages($object->error, $object->errors, 'errors');
  1345. }
  1346. }
  1347. } elseif ($action == 'updateline' && $usercancreate && GETPOST('cancel', 'alpha')) {
  1348. header('Location: '.$_SERVER['PHP_SELF'].'?id='.$object->id); // Pour reaffichage de la fiche en cours d'edition
  1349. exit();
  1350. } elseif ($action == 'classin' && $usercancreate) {
  1351. // Set project
  1352. $object->setProject(GETPOST('projectid', 'int'));
  1353. } elseif ($action == 'setavailability' && $usercancreate) {
  1354. // Delivery time
  1355. $result = $object->set_availability($user, GETPOST('availability_id', 'int'));
  1356. } elseif ($action == 'setdemandreason' && $usercancreate) {
  1357. // Origin of the commercial proposal
  1358. $result = $object->set_demand_reason($user, GETPOST('demand_reason_id', 'int'));
  1359. } elseif ($action == 'setconditions' && $usercancreate) {
  1360. // Terms of payment
  1361. $result = $object->setPaymentTerms(GETPOST('cond_reglement_id', 'int'), GETPOST('cond_reglement_id_deposit_percent', 'alpha'));
  1362. } elseif ($action == 'setremisepercent' && $usercancreate) {
  1363. $result = $object->set_remise_percent($user, price2num(GETPOST('remise_percent'), '', 2));
  1364. } elseif ($action == 'setremiseabsolue' && $usercancreate) {
  1365. $result = $object->set_remise_absolue($user, price2num(GETPOST('remise_absolue'), 'MU', 2));
  1366. } elseif ($action == 'setmode' && $usercancreate) {
  1367. // Payment choice
  1368. $result = $object->setPaymentMethods(GETPOST('mode_reglement_id', 'int'));
  1369. } elseif ($action == 'setmulticurrencycode' && $usercancreate) {
  1370. // Multicurrency Code
  1371. $result = $object->setMulticurrencyCode(GETPOST('multicurrency_code', 'alpha'));
  1372. } elseif ($action == 'setmulticurrencyrate' && $usercancreate) {
  1373. // Multicurrency rate
  1374. $result = $object->setMulticurrencyRate(price2num(GETPOST('multicurrency_tx')), GETPOST('calculation_mode', 'int'));
  1375. } elseif ($action == 'setbankaccount' && $usercancreate) {
  1376. // bank account
  1377. $result = $object->setBankAccount(GETPOST('fk_account', 'int'));
  1378. } elseif ($action == 'setshippingmethod' && $usercancreate) {
  1379. // shipping method
  1380. $result = $object->setShippingMethod(GETPOST('shipping_method_id', 'int'));
  1381. } elseif ($action == 'setwarehouse' && $usercancreate) {
  1382. // warehouse
  1383. $result = $object->setWarehouse(GETPOST('warehouse_id', 'int'));
  1384. } elseif ($action == 'update_extras') {
  1385. $object->oldcopy = dol_clone($object);
  1386. // Fill array 'array_options' with data from update form
  1387. $ret = $extrafields->setOptionalsFromPost(null, $object, GETPOST('attribute', 'restricthtml'));
  1388. if ($ret < 0) {
  1389. $error++;
  1390. }
  1391. if (!$error) {
  1392. $result = $object->updateExtraField(GETPOST('attribute', 'restricthtml'), 'PROPAL_MODIFY', $user);
  1393. if ($result < 0) {
  1394. setEventMessages($object->error, $object->errors, 'errors');
  1395. $error++;
  1396. }
  1397. }
  1398. if ($error) {
  1399. $action = 'edit_extras';
  1400. }
  1401. }
  1402. if (!empty($conf->global->MAIN_DISABLE_CONTACTS_TAB) && $usercancreate) {
  1403. if ($action == 'addcontact') {
  1404. if ($object->id > 0) {
  1405. $contactid = (GETPOST('userid') ? GETPOST('userid') : GETPOST('contactid'));
  1406. $typeid = (GETPOST('typecontact') ? GETPOST('typecontact') : GETPOST('type'));
  1407. $result = $object->add_contact($contactid, $typeid, GETPOST("source", 'aZ09'));
  1408. }
  1409. if ($result >= 0) {
  1410. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  1411. exit();
  1412. } else {
  1413. if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  1414. $langs->load("errors");
  1415. setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
  1416. } else {
  1417. setEventMessages($object->error, $object->errors, 'errors');
  1418. }
  1419. }
  1420. } elseif ($action == 'swapstatut') {
  1421. // Toggle the status of a contact
  1422. if ($object->fetch($id) > 0) {
  1423. $result = $object->swapContactStatus(GETPOST('ligne', 'int'));
  1424. } else {
  1425. dol_print_error($db);
  1426. }
  1427. } elseif ($action == 'deletecontact') {
  1428. // Delete a contact
  1429. $object->fetch($id);
  1430. $result = $object->delete_contact($lineid);
  1431. if ($result >= 0) {
  1432. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  1433. exit();
  1434. } else {
  1435. dol_print_error($db);
  1436. }
  1437. }
  1438. }
  1439. // Actions to build doc
  1440. $upload_dir = !empty($conf->propal->multidir_output[$object->entity])?$conf->propal->multidir_output[$object->entity]:$conf->propal->dir_output;
  1441. $permissiontoadd = $usercancreate;
  1442. include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
  1443. }
  1444. /*
  1445. * View
  1446. */
  1447. $form = new Form($db);
  1448. $formfile = new FormFile($db);
  1449. $formpropal = new FormPropal($db);
  1450. $formmargin = new FormMargin($db);
  1451. if (isModEnabled('project')) {
  1452. $formproject = new FormProjets($db);
  1453. }
  1454. $title = $object->ref." - ".$langs->trans('Card');
  1455. if ($action == 'create') {
  1456. $title = $langs->trans("NewPropal");
  1457. }
  1458. $help_url = 'EN:Commercial_Proposals|FR:Proposition_commerciale|ES:Presupuestos|DE:Modul_Angebote';
  1459. llxHeader('', $title, $help_url);
  1460. $now = dol_now();
  1461. // Add new proposal
  1462. if ($action == 'create') {
  1463. $currency_code = $conf->currency;
  1464. print load_fiche_titre($langs->trans("NewProp"), '', 'propal');
  1465. $soc = new Societe($db);
  1466. if ($socid > 0) {
  1467. $res = $soc->fetch($socid);
  1468. }
  1469. // Load objectsrc
  1470. if (!empty($origin) && !empty($originid)) {
  1471. // Parse element/subelement (ex: project_task)
  1472. $element = $subelement = $origin;
  1473. $regs = array();
  1474. if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
  1475. $element = $regs[1];
  1476. $subelement = $regs[2];
  1477. }
  1478. if ($element == 'project') {
  1479. $projectid = $originid;
  1480. } else {
  1481. // For compatibility
  1482. if ($element == 'order' || $element == 'commande') {
  1483. $element = $subelement = 'commande';
  1484. }
  1485. if ($element == 'propal') {
  1486. $element = 'comm/propal';
  1487. $subelement = 'propal';
  1488. }
  1489. if ($element == 'contract') {
  1490. $element = $subelement = 'contrat';
  1491. }
  1492. if ($element == 'shipping') {
  1493. $element = $subelement = 'expedition';
  1494. }
  1495. dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
  1496. $classname = ucfirst($subelement);
  1497. $objectsrc = new $classname($db);
  1498. $objectsrc->fetch($originid);
  1499. if (empty($objectsrc->lines) && method_exists($objectsrc, 'fetch_lines')) {
  1500. $objectsrc->fetch_lines();
  1501. }
  1502. $objectsrc->fetch_thirdparty();
  1503. $projectid = (!empty($objectsrc->fk_project) ? $objectsrc->fk_project : 0);
  1504. $ref_client = (!empty($objectsrc->ref_client) ? $objectsrc->ref_client : '');
  1505. $soc = $objectsrc->thirdparty;
  1506. $cond_reglement_id = (!empty($objectsrc->cond_reglement_id) ? $objectsrc->cond_reglement_id : (!empty($soc->cond_reglement_id) ? $soc->cond_reglement_id : 0)); // TODO maybe add default value option
  1507. $mode_reglement_id = (!empty($objectsrc->mode_reglement_id) ? $objectsrc->mode_reglement_id : (!empty($soc->mode_reglement_id) ? $soc->mode_reglement_id : 0));
  1508. $remise_absolue = (!empty($objectsrc->remise_absolue) ? $objectsrc->remise_absolue : (!empty($soc->remise_absolue) ? $soc->remise_absolue : 0)); // deprecated
  1509. $remise_percent = (!empty($objectsrc->remise_percent) ? $objectsrc->remise_percent : (!empty($soc->remise_percent) ? $soc->remise_percent : 0));
  1510. $warehouse_id = (!empty($objectsrc->warehouse_id) ? $objectsrc->warehouse_id : (!empty($soc->warehouse_id) ? $soc->warehouse_id : 0));
  1511. $dateinvoice = (empty($dateinvoice) ? (empty($conf->global->MAIN_AUTOFILL_DATE) ?-1 : '') : $dateinvoice);
  1512. // Replicate extrafields
  1513. $objectsrc->fetch_optionals();
  1514. $object->array_options = $objectsrc->array_options;
  1515. if (isModEnabled("multicurrency")) {
  1516. if (!empty($objectsrc->multicurrency_code)) {
  1517. $currency_code = $objectsrc->multicurrency_code;
  1518. }
  1519. if (!empty($conf->global->MULTICURRENCY_USE_ORIGIN_TX) && !empty($objectsrc->multicurrency_tx)) {
  1520. $currency_tx = $objectsrc->multicurrency_tx;
  1521. }
  1522. }
  1523. }
  1524. } else {
  1525. if (isModEnabled("multicurrency") && !empty($soc->multicurrency_code)) {
  1526. $currency_code = $soc->multicurrency_code;
  1527. }
  1528. }
  1529. //Warehouse default if null
  1530. if ($soc->fk_warehouse > 0) {
  1531. $warehouse_id = $soc->fk_warehouse;
  1532. }
  1533. if (isModEnabled('stock') && empty($warehouse_id) && !empty($conf->global->WAREHOUSE_ASK_WAREHOUSE_DURING_ORDER)) {
  1534. if (empty($object->warehouse_id) && !empty($conf->global->MAIN_DEFAULT_WAREHOUSE)) {
  1535. $warehouse_id = $conf->global->MAIN_DEFAULT_WAREHOUSE;
  1536. }
  1537. if (empty($object->warehouse_id) && !empty($conf->global->MAIN_DEFAULT_WAREHOUSE_USER)) {
  1538. $warehouse_id = $user->fk_warehouse;
  1539. }
  1540. }
  1541. print '<form name="addprop" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  1542. print '<input type="hidden" name="token" value="'.newToken().'">';
  1543. print '<input type="hidden" name="action" value="add">';
  1544. if ($origin != 'project' && $originid) {
  1545. print '<input type="hidden" name="origin" value="'.$origin.'">';
  1546. print '<input type="hidden" name="originid" value="'.$originid.'">';
  1547. } elseif ($origin == 'project' && !empty($projectid)) {
  1548. print '<input type="hidden" name="projectid" value="'.$projectid.'">';
  1549. }
  1550. print dol_get_fiche_head();
  1551. print '<table class="border centpercent">';
  1552. // Reference
  1553. print '<tr class="field_ref"><td class="titlefieldcreate fieldrequired">'.$langs->trans('Ref').'</td><td class="valuefieldcreate">'.$langs->trans("Draft").'</td></tr>';
  1554. // Ref customer
  1555. print '<tr class="field_ref_client"><td class="titlefieldcreate">'.$langs->trans('RefCustomer').'</td><td class="valuefieldcreate">';
  1556. print '<input type="text" name="ref_client" value="'.(!empty($ref_client)?$ref_client:GETPOST('ref_client')).'"></td>';
  1557. print '</tr>';
  1558. // Third party
  1559. print '<tr class="field_socid">';
  1560. print '<td class="titlefieldcreate fieldrequired">'.$langs->trans('Customer').'</td>';
  1561. $shipping_method_id = 0;
  1562. if ($socid > 0) {
  1563. print '<td class="valuefieldcreate">';
  1564. print $soc->getNomUrl(1, 'customer');
  1565. print '<input type="hidden" name="socid" value="'.$soc->id.'">';
  1566. print '</td>';
  1567. if (!empty($conf->global->SOCIETE_ASK_FOR_SHIPPING_METHOD) && !empty($soc->shipping_method_id)) {
  1568. $shipping_method_id = $soc->shipping_method_id;
  1569. }
  1570. //$warehouse_id = $soc->warehouse_id;
  1571. } else {
  1572. print '<td class="valuefieldcreate">';
  1573. print img_picto('', 'company').$form->select_company('', 'socid', '((s.client = 1 OR s.client = 2 OR s.client = 3) AND status=1)', 'SelectThirdParty', 1, 0, null, 0, 'minwidth300 maxwidth500 widthcentpercentminusxx');
  1574. // reload page to retrieve customer informations
  1575. if (empty($conf->global->RELOAD_PAGE_ON_CUSTOMER_CHANGE_DISABLED)) {
  1576. print '<script type="text/javascript">
  1577. $(document).ready(function() {
  1578. $("#socid").change(function() {
  1579. console.log("We have changed the company - Reload page");
  1580. var socid = $(this).val();
  1581. // reload page
  1582. $("input[name=action]").val("create");
  1583. $("form[name=addprop]").submit();
  1584. });
  1585. });
  1586. </script>';
  1587. }
  1588. print ' <a href="'.DOL_URL_ROOT.'/societe/card.php?action=create&client=3&fournisseur=0&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=create').'"><span class="fa fa-plus-circle valignmiddle paddingleft" title="'.$langs->trans("AddThirdParty").'"></span></a>';
  1589. print '</td>';
  1590. }
  1591. print '</tr>'."\n";
  1592. if ($socid > 0) {
  1593. // Contacts (ask contact only if thirdparty already defined).
  1594. print '<tr class="field_contactid"><td class="titlefieldcreate">'.$langs->trans("DefaultContact").'</td><td class="valuefieldcreate">';
  1595. print img_picto('', 'contact');
  1596. print $form->selectcontacts($soc->id, $contactid, 'contactid', 1, '', '', 0, 'minwidth300');
  1597. print '</td></tr>';
  1598. // Third party discounts info line
  1599. print '<tr class="field_discount_info"><td class="titlefieldcreate">'.$langs->trans('Discounts').'</td><td class="valuefieldcreate">';
  1600. $absolute_discount = $soc->getAvailableDiscounts();
  1601. $thirdparty = $soc;
  1602. $discount_type = 0;
  1603. $backtopage = urlencode($_SERVER["PHP_SELF"].'?socid='.$thirdparty->id.'&action='.$action.'&origin='.GETPOST('origin').'&originid='.GETPOST('originid'));
  1604. include DOL_DOCUMENT_ROOT.'/core/tpl/object_discounts.tpl.php';
  1605. print '</td></tr>';
  1606. }
  1607. // Date
  1608. print '<tr class="field_addprop"><td class="titlefieldcreate fieldrequired">'.$langs->trans('DatePropal').'</td><td class="valuefieldcreate">';
  1609. print $form->selectDate('', '', '', '', '', "addprop", 1, 1);
  1610. print '</td></tr>';
  1611. // Validaty duration
  1612. print '<tr class="field_duree_validitee"><td class="titlefieldcreate fieldrequired">'.$langs->trans("ValidityDuration").'</td><td class="valuefieldcreate">'.img_picto('', 'clock', 'class="paddingright"').'<input name="duree_validite" class="width50" value="'.(GETPOSTISSET('duree_validite') ? GETPOST('duree_validite', 'alphanohtml') : $conf->global->PROPALE_VALIDITY_DURATION).'"> '.$langs->trans("days").'</td></tr>';
  1613. // Terms of payment
  1614. print '<tr class="field_cond_reglement_id"><td class="nowrap">'.$langs->trans('PaymentConditionsShort').'</td><td>';
  1615. print img_picto('', 'paiment');
  1616. print $form->getSelectConditionsPaiements((GETPOSTISSET('cond_reglement_id') && GETPOST('cond_reglement_id', 'int') != 0) ? GETPOST('cond_reglement_id', 'int') : $soc->cond_reglement_id, 'cond_reglement_id', 1, 1, 0, '', (GETPOSTISSET('cond_reglement_id_deposit_percent') ? GETPOST('cond_reglement_id_deposit_percent', 'alpha') : $soc->deposit_percent));
  1617. print '</td></tr>';
  1618. // Mode of payment
  1619. print '<tr class="field_mode_reglement_id"><td class="titlefieldcreate">'.$langs->trans('PaymentMode').'</td><td class="valuefieldcreate">';
  1620. print img_picto('', 'bank', 'class="pictofixedwidth"');
  1621. print $form->select_types_paiements((GETPOSTISSET('mode_reglement_id') && GETPOST('mode_reglement_id', 'int') != 0) ? GETPOST('mode_reglement_id', 'int') : $soc->mode_reglement_id, 'mode_reglement_id', 'CRDT', 0, 1, 0, 0, 1, 'maxwidth200 widthcentpercentminusx', 1);
  1622. print '</td></tr>';
  1623. // Bank Account
  1624. if (!empty($conf->global->BANK_ASK_PAYMENT_BANK_DURING_PROPOSAL) && isModEnabled("banque")) {
  1625. print '<tr class="field_fk_account"><td class="titlefieldcreate">'.$langs->trans('BankAccount').'</td><td class="valuefieldcreate">';
  1626. print img_picto('', 'bank_account', 'class="pictofixedwidth"').$form->select_comptes($soc->fk_account, 'fk_account', 0, '', 1, '', 0, 'maxwidth200 widthcentpercentminusx', 1);
  1627. print '</td></tr>';
  1628. }
  1629. // Source / Channel - What trigger creation
  1630. print '<tr class="field_demand_reason_id"><td class="titlefieldcreate">'.$langs->trans('Source').'</td><td class="valuefieldcreate">';
  1631. print img_picto('', 'question', 'class="pictofixedwidth"');
  1632. $form->selectInputReason('', 'demand_reason_id', "SRC_PROP", 1, 'maxwidth200 widthcentpercentminusx');
  1633. print '</td></tr>';
  1634. // Delivery delay
  1635. print '<tr class="field_availability_id"><td class="titlefieldcreate">'.$langs->trans('AvailabilityPeriod');
  1636. if (isModEnabled('commande')) {
  1637. print ' ('.$langs->trans('AfterOrder').')';
  1638. }
  1639. print '</td><td class="valuefieldcreate">';
  1640. print img_picto('', 'clock', 'class="pictofixedwidth"');
  1641. $form->selectAvailabilityDelay('', 'availability_id', '', 1, 'maxwidth200 widthcentpercentminusx');
  1642. print '</td></tr>';
  1643. // Shipping Method
  1644. if (isModEnabled("expedition")) {
  1645. if (!empty($conf->global->SOCIETE_ASK_FOR_SHIPPING_METHOD) && !empty($soc->shipping_method_id)) {
  1646. $shipping_method_id = $soc->shipping_method_id;
  1647. }
  1648. print '<tr class="field_shipping_method_id"><td class="titlefieldcreate">'.$langs->trans('SendingMethod').'</td><td class="valuefieldcreate">';
  1649. print img_picto('', 'object_dollyrevert', 'class="pictofixedwidth"');
  1650. $form->selectShippingMethod($shipping_method_id, 'shipping_method_id', '', 1, '', 0, 'maxwidth200 widthcentpercentminusx');
  1651. print '</td></tr>';
  1652. }
  1653. // Warehouse
  1654. if (isModEnabled('stock') && !empty($conf->global->WAREHOUSE_ASK_WAREHOUSE_DURING_PROPAL)) {
  1655. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  1656. $formproduct = new FormProduct($db);
  1657. print '<tr class="field_warehouse_id"><td class="titlefieldcreate">'.$langs->trans('Warehouse').'</td><td class="valuefieldcreate">';
  1658. print img_picto('', 'stock', 'class="pictofixedwidth"').$formproduct->selectWarehouses($warehouse_id, 'warehouse_id', '', 1, 0, 0, '', 0, 0, array(), 'maxwidth500 widthcentpercentminusxx');
  1659. print '</td></tr>';
  1660. }
  1661. // Delivery date (or manufacturing)
  1662. print '<tr class="field_date_livraison"><td class="titlefieldcreate">'.$langs->trans("DeliveryDate").'</td>';
  1663. print '<td class="valuefieldcreate">';
  1664. if (isset($conf->global->DATE_LIVRAISON_WEEK_DELAY) && is_numeric($conf->global->DATE_LIVRAISON_WEEK_DELAY)) {
  1665. $tmpdte = time() + ((7 * $conf->global->DATE_LIVRAISON_WEEK_DELAY) * 24 * 60 * 60);
  1666. $syear = date("Y", $tmpdte);
  1667. $smonth = date("m", $tmpdte);
  1668. $sday = date("d", $tmpdte);
  1669. print $form->selectDate($syear."-".$smonth."-".$sday, 'date_livraison', '', '', '', "addprop");
  1670. } else {
  1671. print $form->selectDate(-1, 'date_livraison', '', '', '', "addprop", 1, 1);
  1672. }
  1673. print '</td></tr>';
  1674. // Project
  1675. if (isModEnabled('project')) {
  1676. $langs->load("projects");
  1677. print '<tr class="field_projectid">';
  1678. print '<td class="titlefieldcreate">'.$langs->trans("Project").'</td><td class="valuefieldcreate">';
  1679. print img_picto('', 'project', 'class="pictofixedwidth"').$formproject->select_projects(($soc->id > 0 ? $soc->id : -1), $projectid, 'projectid', 0, 0, 1, 1, 0, 0, 0, '', 1, 0, 'maxwidth500 widthcentpercentminusxx');
  1680. print ' <a href="'.DOL_URL_ROOT.'/projet/card.php?socid='.$soc->id.'&action=create&status=1&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=create&socid='.$soc->id).'"><span class="fa fa-plus-circle valignmiddle paddingleft" title="'.$langs->trans("AddProject").'"></span></a>';
  1681. print '</td>';
  1682. print '</tr>';
  1683. }
  1684. // Incoterms
  1685. if (isModEnabled('incoterm')) {
  1686. print '<tr class="field_incoterm_id">';
  1687. print '<td class="titlefieldcreate"><label for="incoterm_id">'.$form->textwithpicto($langs->trans("IncotermLabel"), $soc->label_incoterms, 1).'</label></td>';
  1688. print '<td class="valuefieldcreate maxwidthonsmartphone">';
  1689. print $form->select_incoterms((!empty($soc->fk_incoterms) ? $soc->fk_incoterms : ''), (!empty($soc->location_incoterms) ? $soc->location_incoterms : ''));
  1690. print '</td></tr>';
  1691. }
  1692. // Template to use by default
  1693. print '<tr class="field_model">';
  1694. print '<td class="titlefieldcreate">'.$langs->trans("DefaultModel").'</td>';
  1695. print '<td class="valuefieldcreate">';
  1696. print img_picto('', 'pdf', 'class="pictofixedwidth"');
  1697. $liste = ModelePDFPropales::liste_modeles($db);
  1698. $preselected = (!empty($conf->global->PROPALE_ADDON_PDF_ODT_DEFAULT) ? $conf->global->PROPALE_ADDON_PDF_ODT_DEFAULT : getDolGlobalString("PROPALE_ADDON_PDF"));
  1699. print $form->selectarray('model', $liste, $preselected, 0, 0, 0, '', 0, 0, 0, '', 'maxwidth200 widthcentpercentminusx', 1);
  1700. print "</td></tr>";
  1701. // Multicurrency
  1702. if (isModEnabled("multicurrency")) {
  1703. print '<tr class="field_currency">';
  1704. print '<td class="titlefieldcreate">'.$form->editfieldkey('Currency', 'multicurrency_code', '', $object, 0).'</td>';
  1705. print '<td class="valuefieldcreate maxwidthonsmartphone">';
  1706. print img_picto('', 'currency', 'class="pictofixedwidth"').$form->selectMultiCurrency($currency_code, 'multicurrency_code', 0);
  1707. print '</td></tr>';
  1708. }
  1709. // Public note
  1710. print '<tr class="field_note_public">';
  1711. print '<td class="titlefieldcreate tdtop">'.$langs->trans('NotePublic').'</td>';
  1712. print '<td class="valuefieldcreate">';
  1713. $note_public = $object->getDefaultCreateValueFor('note_public', (!empty($objectsrc) ? $objectsrc->note_public : null));
  1714. $doleditor = new DolEditor('note_public', $note_public, '', 80, 'dolibarr_notes', 'In', 0, false, empty($conf->global->FCKEDITOR_ENABLE_NOTE_PUBLIC) ? 0 : 1, ROWS_3, '90%');
  1715. print $doleditor->Create(1);
  1716. // Private note
  1717. if (empty($user->socid)) {
  1718. print '<tr class="field_note_private">';
  1719. print '<td class="titlefieldcreate tdtop">'.$langs->trans('NotePrivate').'</td>';
  1720. print '<td class="valuefieldcreate">';
  1721. $note_private = $object->getDefaultCreateValueFor('note_private', ((!empty($origin) && !empty($originid) && is_object($objectsrc)) ? $objectsrc->note_private : null));
  1722. $doleditor = new DolEditor('note_private', $note_private, '', 80, 'dolibarr_notes', 'In', 0, false, empty($conf->global->FCKEDITOR_ENABLE_NOTE_PRIVATE) ? 0 : 1, ROWS_3, '90%');
  1723. print $doleditor->Create(1);
  1724. // print '<textarea name="note_private" wrap="soft" cols="70" rows="'.ROWS_3.'">'.$note_private.'.</textarea>
  1725. print '</td></tr>';
  1726. }
  1727. // Other attributes
  1728. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
  1729. // Lines from source
  1730. if (!empty($origin) && !empty($originid) && is_object($objectsrc)) {
  1731. // TODO for compatibility
  1732. if ($origin == 'contrat') {
  1733. // Calcul contrat->price (HT), contrat->total (TTC), contrat->tva
  1734. $objectsrc->remise_absolue = $remise_absolue; // deprecated
  1735. $objectsrc->remise_percent = $remise_percent;
  1736. $objectsrc->update_price(1, 'auto', 1);
  1737. }
  1738. print "\n<!-- ".$classname." info -->";
  1739. print "\n";
  1740. print '<input type="hidden" name="amount" value="'.$objectsrc->total_ht.'">'."\n";
  1741. print '<input type="hidden" name="total" value="'.$objectsrc->total_ttc.'">'."\n";
  1742. print '<input type="hidden" name="tva" value="'.$objectsrc->total_tva.'">'."\n";
  1743. print '<input type="hidden" name="origin" value="'.$objectsrc->element.'">';
  1744. print '<input type="hidden" name="originid" value="'.$objectsrc->id.'">';
  1745. $newclassname = $classname;
  1746. if ($newclassname == 'Propal') {
  1747. $newclassname = 'CommercialProposal';
  1748. } elseif ($newclassname == 'Commande') {
  1749. $newclassname = 'Order';
  1750. } elseif ($newclassname == 'Expedition') {
  1751. $newclassname = 'Sending';
  1752. } elseif ($newclassname == 'Fichinter') {
  1753. $newclassname = 'Intervention';
  1754. }
  1755. print '<tr><td>'.$langs->trans($newclassname).'</td><td>'.$objectsrc->getNomUrl(1).'</td></tr>';
  1756. print '<tr><td>'.$langs->trans('AmountHT').'</td><td>'.price($objectsrc->total_ht, 0, $langs, 1, -1, -1, $conf->currency).'</td></tr>';
  1757. print '<tr><td>'.$langs->trans('AmountVAT').'</td><td>'.price($objectsrc->total_tva, 0, $langs, 1, -1, -1, $conf->currency)."</td></tr>";
  1758. if ($mysoc->localtax1_assuj == "1" || $objectsrc->total_localtax1 != 0) { // Localtax1
  1759. print '<tr><td>'.$langs->transcountry("AmountLT1", $mysoc->country_code).'</td><td>'.price($objectsrc->total_localtax1, 0, $langs, 1, -1, -1, $conf->currency)."</td></tr>";
  1760. }
  1761. if ($mysoc->localtax2_assuj == "1" || $objectsrc->total_localtax2 != 0) { // Localtax2
  1762. print '<tr><td>'.$langs->transcountry("AmountLT2", $mysoc->country_code).'</td><td>'.price($objectsrc->total_localtax2, 0, $langs, 1, -1, -1, $conf->currency)."</td></tr>";
  1763. }
  1764. print '<tr><td>'.$langs->trans('AmountTTC').'</td><td>'.price($objectsrc->total_ttc, 0, $langs, 1, -1, -1, $conf->currency)."</td></tr>";
  1765. if (isModEnabled("multicurrency")) {
  1766. print '<tr><td>'.$langs->trans('MulticurrencyAmountHT').'</td><td>'.price($objectsrc->multicurrency_total_ht).'</td></tr>';
  1767. print '<tr><td>'.$langs->trans('MulticurrencyAmountVAT').'</td><td>'.price($objectsrc->multicurrency_total_tva)."</td></tr>";
  1768. print '<tr><td>'.$langs->trans('MulticurrencyAmountTTC').'</td><td>'.price($objectsrc->multicurrency_total_ttc)."</td></tr>";
  1769. }
  1770. }
  1771. print "</table>\n";
  1772. /*
  1773. * Combobox for copy function
  1774. */
  1775. if (empty($conf->global->PROPAL_CLONE_ON_CREATE_PAGE)) {
  1776. print '<input type="hidden" name="createmode" value="empty">';
  1777. }
  1778. if (!empty($conf->global->PROPAL_CLONE_ON_CREATE_PAGE)) {
  1779. print '<br><table>';
  1780. // For backward compatibility
  1781. print '<tr>';
  1782. print '<td><input type="radio" name="createmode" value="copy"></td>';
  1783. print '<td>'.$langs->trans("CopyPropalFrom").' </td>';
  1784. print '<td>';
  1785. $liste_propal = array();
  1786. $liste_propal [0] = '';
  1787. $sql = "SELECT p.rowid as id, p.ref, s.nom";
  1788. $sql .= " FROM ".MAIN_DB_PREFIX."propal p";
  1789. $sql .= ", ".MAIN_DB_PREFIX."societe s";
  1790. $sql .= " WHERE s.rowid = p.fk_soc";
  1791. $sql .= " AND p.entity IN (".getEntity('propal').")";
  1792. $sql .= " AND p.fk_statut <> 0";
  1793. $sql .= " ORDER BY Id";
  1794. $resql = $db->query($sql);
  1795. if ($resql) {
  1796. $num = $db->num_rows($resql);
  1797. $i = 0;
  1798. while ($i < $num) {
  1799. $row = $db->fetch_row($resql);
  1800. $propalRefAndSocName = $row[1]." - ".$row[2];
  1801. $liste_propal[$row[0]] = $propalRefAndSocName;
  1802. $i++;
  1803. }
  1804. print $form->selectarray("copie_propal", $liste_propal, 0);
  1805. } else {
  1806. dol_print_error($db);
  1807. }
  1808. print '</td></tr>';
  1809. print '<tr><td class="tdtop"><input type="radio" name="createmode" value="empty" checked></td>';
  1810. print '<td valign="top" colspan="2">'.$langs->trans("CreateEmptyPropal").'</td></tr>';
  1811. print '</table>';
  1812. }
  1813. print dol_get_fiche_end();
  1814. $langs->load("bills");
  1815. print $form->buttonsSaveCancel("CreateDraft");
  1816. print "</form>";
  1817. // Show origin lines
  1818. if (!empty($origin) && !empty($originid) && is_object($objectsrc)) {
  1819. print '<br>';
  1820. $title = $langs->trans('ProductsAndServices');
  1821. print load_fiche_titre($title);
  1822. print '<div class="div-table-responsive-no-min">';
  1823. print '<table class="noborder centpercent">';
  1824. $objectsrc->printOriginLinesList();
  1825. print '</table>';
  1826. print '</div>';
  1827. }
  1828. } elseif ($object->id > 0) {
  1829. /*
  1830. * Show object in view mode
  1831. */
  1832. $soc = new Societe($db);
  1833. $soc->fetch($object->socid);
  1834. $head = propal_prepare_head($object);
  1835. print dol_get_fiche_head($head, 'comm', $langs->trans('Proposal'), -1, 'propal');
  1836. $formconfirm = '';
  1837. // Clone confirmation
  1838. if ($action == 'clone') {
  1839. // Create an array for form
  1840. $formquestion = array(
  1841. // 'text' => $langs->trans("ConfirmClone"),
  1842. // array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneMainAttributes"), 'value' => 1),
  1843. array('type' => 'other', 'name' => 'socid', 'label' => $langs->trans("SelectThirdParty"), 'value' => $form->select_company(GETPOST('socid', 'int'), 'socid', '(s.client=1 OR s.client=2 OR s.client=3)', '', 0, 0, null, 0, 'maxwidth300')),
  1844. array('type' => 'checkbox', 'name' => 'update_prices', 'label' => $langs->trans('PuttingPricesUpToDate'), 'value' => (!empty($conf->global->PROPOSAL_CLONE_UPDATE_PRICES) ? 1 : 0)),
  1845. );
  1846. if (!empty($conf->global->PROPAL_CLONE_DATE_DELIVERY) && !empty($object->delivery_date)) {
  1847. $formquestion[] = array('type' => 'date', 'name' => 'date_delivery', 'label' => $langs->trans("DeliveryDate"), 'value' => $object->delivery_date);
  1848. }
  1849. // Incomplete payment. We ask if reason = discount or other
  1850. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmClonePropal', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
  1851. }
  1852. if ($action == 'closeas') {
  1853. //Form to close proposal (signed or not)
  1854. $formquestion = array();
  1855. if (empty($conf->global->PROPAL_SKIP_ACCEPT_REFUSE)) {
  1856. $formquestion[] = array('type' => 'select', 'name' => 'statut', 'label' => '<span class="fieldrequired">'.$langs->trans("CloseAs").'</span>', 'values' => array($object::STATUS_SIGNED => $object->LibStatut($object::STATUS_SIGNED), $object::STATUS_NOTSIGNED => $object->LibStatut($object::STATUS_NOTSIGNED)));
  1857. }
  1858. $formquestion[] = array('type' => 'text', 'name' => 'note_private', 'label' => $langs->trans("Note"), 'value' => ''); // Field to complete private note (not replace)
  1859. if (getDolGlobalInt('PROPOSAL_SUGGEST_DOWN_PAYMENT_INVOICE_CREATION')) {
  1860. // This is a hidden option:
  1861. // Suggestion to create invoice during proposal signature is not enabled by default.
  1862. // Such choice should be managed by the workflow module and trigger. This option generates conflicts with some setup.
  1863. // It may also break step of creating an order when invoicing must be done from orders and not from proposal
  1864. $deposit_percent_from_payment_terms = getDictionaryValue('c_payment_term', 'deposit_percent', $object->cond_reglement_id);
  1865. if (!empty($deposit_percent_from_payment_terms) && isModEnabled('facture') && !empty($user->rights->facture->creer)) {
  1866. require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
  1867. $object->fetchObjectLinked();
  1868. $eligibleForDepositGeneration = true;
  1869. if (array_key_exists('facture', $object->linkedObjects)) {
  1870. foreach ($object->linkedObjects['facture'] as $invoice) {
  1871. if ($invoice->type == Facture::TYPE_DEPOSIT) {
  1872. $eligibleForDepositGeneration = false;
  1873. break;
  1874. }
  1875. }
  1876. }
  1877. if ($eligibleForDepositGeneration && array_key_exists('commande', $object->linkedObjects)) {
  1878. foreach ($object->linkedObjects['commande'] as $order) {
  1879. $order->fetchObjectLinked();
  1880. if (array_key_exists('facture', $order->linkedObjects)) {
  1881. foreach ($order->linkedObjects['facture'] as $invoice) {
  1882. if ($invoice->type == Facture::TYPE_DEPOSIT) {
  1883. $eligibleForDepositGeneration = false;
  1884. break 2;
  1885. }
  1886. }
  1887. }
  1888. }
  1889. }
  1890. if ($eligibleForDepositGeneration) {
  1891. $formquestion[] = array(
  1892. 'type' => 'checkbox',
  1893. 'tdclass' => 'showonlyifsigned',
  1894. 'name' => 'generate_deposit',
  1895. 'morecss' => 'margintoponly marginbottomonly',
  1896. 'label' => $form->textwithpicto($langs->trans('GenerateDeposit', $object->deposit_percent), $langs->trans('DepositGenerationPermittedByThePaymentTermsSelected'))
  1897. );
  1898. $formquestion[] = array(
  1899. 'type' => 'date',
  1900. 'tdclass' => 'fieldrequired showonlyifgeneratedeposit',
  1901. 'name' => 'datef',
  1902. 'label' => $langs->trans('DateInvoice'),
  1903. 'value' => dol_now(),
  1904. 'datenow' => true
  1905. );
  1906. if (!empty($conf->global->INVOICE_POINTOFTAX_DATE)) {
  1907. $formquestion[] = array(
  1908. 'type' => 'date',
  1909. 'tdclass' => 'fieldrequired showonlyifgeneratedeposit',
  1910. 'name' => 'date_pointoftax',
  1911. 'label' => $langs->trans('DatePointOfTax'),
  1912. 'value' => dol_now(),
  1913. 'datenow' => true
  1914. );
  1915. }
  1916. $paymentTermsSelect = $form->getSelectConditionsPaiements(0, 'cond_reglement_id', -1, 0, 1, 'minwidth200');
  1917. $formquestion[] = array(
  1918. 'type' => 'other',
  1919. 'tdclass' => 'fieldrequired showonlyifgeneratedeposit',
  1920. 'name' => 'cond_reglement_id',
  1921. 'label' => $langs->trans('PaymentTerm'),
  1922. 'value' => $paymentTermsSelect
  1923. );
  1924. $formquestion[] = array(
  1925. 'type' => 'checkbox',
  1926. 'tdclass' => 'showonlyifgeneratedeposit',
  1927. 'name' => 'validate_generated_deposit',
  1928. 'morecss' => 'margintoponly marginbottomonly',
  1929. 'label' => $langs->trans('ValidateGeneratedDeposit')
  1930. );
  1931. $formquestion[] = array(
  1932. 'type' => 'onecolumn',
  1933. 'value' => '
  1934. <script>
  1935. let signedValue = ' . $object::STATUS_SIGNED . ';
  1936. $(document).ready(function() {
  1937. $("[name=generate_deposit]").change(function () {
  1938. let $self = $(this);
  1939. let $target = $(".showonlyifgeneratedeposit").parent(".tagtr");
  1940. if (! $self.parents(".tagtr").is(":hidden") && $self.is(":checked")) {
  1941. $target.show();
  1942. } else {
  1943. $target.hide();
  1944. }
  1945. return true;
  1946. });
  1947. $("#statut").change(function() {
  1948. let $target = $(".showonlyifsigned").parent(".tagtr");
  1949. if ($(this).val() == signedValue) {
  1950. $target.show();
  1951. } else {
  1952. $target.hide();
  1953. }
  1954. $("[name=generate_deposit]").trigger("change");
  1955. return true;
  1956. });
  1957. $("#statut").trigger("change");
  1958. });
  1959. </script>
  1960. '
  1961. );
  1962. }
  1963. }
  1964. }
  1965. if (isModEnabled('notification')) {
  1966. require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
  1967. $notify = new Notify($db);
  1968. $formquestion = array_merge($formquestion, array(
  1969. array('type' => 'onecolumn', 'value' => $notify->confirmMessage('PROPAL_CLOSE_SIGNED', $object->socid, $object)),
  1970. ));
  1971. }
  1972. if (empty($conf->global->PROPAL_SKIP_ACCEPT_REFUSE)) {
  1973. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('SetAcceptedRefused'), $text, 'confirm_closeas', $formquestion, '', 1, 250);
  1974. } else {
  1975. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?statut=3&id=' . $object->id, $langs->trans('Close'), $text, 'confirm_closeas', $formquestion, '', 1, 250);
  1976. }
  1977. } elseif ($action == 'delete') {
  1978. // Confirm delete
  1979. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteProp'), $langs->trans('ConfirmDeleteProp', $object->ref), 'confirm_delete', '', 0, 1);
  1980. } elseif ($action == 'reopen') {
  1981. // Confirm reopen
  1982. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ReOpen'), $langs->trans('ConfirmReOpenProp', $object->ref), 'confirm_reopen', '', 0, 1);
  1983. } elseif ($action == 'ask_deleteline') {
  1984. // Confirmation delete product/service line
  1985. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid, $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteline', '', 0, 1);
  1986. } elseif ($action == 'validate') {
  1987. // Confirm validate proposal
  1988. $error = 0;
  1989. // We verify whether the object is provisionally numbering
  1990. $ref = substr($object->ref, 1, 4);
  1991. if ($ref == 'PROV' || $ref == '') {
  1992. $numref = $object->getNextNumRef($soc);
  1993. if (empty($numref)) {
  1994. $error++;
  1995. setEventMessages($object->error, $object->errors, 'errors');
  1996. }
  1997. } else {
  1998. $numref = $object->ref;
  1999. }
  2000. $text = $langs->trans('ConfirmValidateProp', $numref);
  2001. if (isModEnabled('notification')) {
  2002. require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
  2003. $notify = new Notify($db);
  2004. $text .= '<br>';
  2005. $text .= $notify->confirmMessage('PROPAL_VALIDATE', $object->socid, $object);
  2006. }
  2007. // mandatoryPeriod
  2008. $nbMandated = 0;
  2009. foreach ($object->lines as $line) {
  2010. $res = $line->fetch_product();
  2011. if ($res > 0 ) {
  2012. if ($line->product->isService() && $line->product->isMandatoryPeriod() && (empty($line->date_start) || empty($line->date_end) )) {
  2013. $nbMandated++;
  2014. break;
  2015. }
  2016. }
  2017. }
  2018. if ($nbMandated > 0) {
  2019. $text .= '<div><span class="clearboth nowraponall warning">'.$langs->trans("mandatoryPeriodNeedTobeSetMsgValidate").'</span></div>';
  2020. }
  2021. if (!$error) {
  2022. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ValidateProp'), $text, 'confirm_validate', '', 0, 1);
  2023. }
  2024. }
  2025. // Call Hook formConfirm
  2026. $parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
  2027. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  2028. if (empty($reshook)) {
  2029. $formconfirm .= $hookmanager->resPrint;
  2030. } elseif ($reshook > 0) {
  2031. $formconfirm = $hookmanager->resPrint;
  2032. }
  2033. // Print form confirm
  2034. print $formconfirm;
  2035. // Proposal card
  2036. $linkback = '<a href="'.DOL_URL_ROOT.'/comm/propal/list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  2037. $morehtmlref = '<div class="refidno">';
  2038. // Ref customer
  2039. $morehtmlref .= $form->editfieldkey("RefCustomer", 'ref_client', $object->ref_client, $object, $usercancreate, 'string', '', 0, 1);
  2040. $morehtmlref .= $form->editfieldval("RefCustomer", 'ref_client', $object->ref_client, $object, $usercancreate, 'string'.(isset($conf->global->THIRDPARTY_REF_INPUT_SIZE) ? ':'.$conf->global->THIRDPARTY_REF_INPUT_SIZE : ''), '', null, null, '', 1);
  2041. // Thirdparty
  2042. $morehtmlref .= '<br>'.$object->thirdparty->getNomUrl(1, 'customer');
  2043. if (empty($conf->global->MAIN_DISABLE_OTHER_LINK) && $object->thirdparty->id > 0) {
  2044. $morehtmlref .= ' (<a href="'.DOL_URL_ROOT.'/comm/propal/list.php?socid='.$object->thirdparty->id.'&search_societe='.urlencode($object->thirdparty->name).'">'.$langs->trans("OtherProposals").'</a>)';
  2045. }
  2046. // Project
  2047. if (isModEnabled('project')) {
  2048. $langs->load("projects");
  2049. $morehtmlref .= '<br>';
  2050. if ($usercancreate) {
  2051. $morehtmlref .= img_picto($langs->trans("Project"), 'project', 'class="pictofixedwidth"');
  2052. if ($action != 'classify') {
  2053. $morehtmlref .= '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?action=classify&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetProject')).'</a> ';
  2054. }
  2055. $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, $object->socid, $object->fk_project, ($action == 'classify' ? 'projectid' : 'none'), 0, ($action == 'classify' ? 1 : 0), 0, 1, '');
  2056. } else {
  2057. if (!empty($object->fk_project)) {
  2058. $proj = new Project($db);
  2059. $proj->fetch($object->fk_project);
  2060. $morehtmlref .= $proj->getNomUrl(1);
  2061. if ($proj->title) {
  2062. $morehtmlref .= '<span class="opacitymedium"> - '.dol_escape_htmltag($proj->title).'</span>';
  2063. }
  2064. }
  2065. }
  2066. }
  2067. $morehtmlref .= '</div>';
  2068. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
  2069. print '<div class="fichecenter">';
  2070. print '<div class="fichehalfleft">';
  2071. print '<div class="underbanner clearboth"></div>';
  2072. print '<table class="border tableforfield centpercent">';
  2073. // Link for thirdparty discounts
  2074. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  2075. $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
  2076. $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
  2077. } else {
  2078. $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
  2079. $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
  2080. }
  2081. print '<tr><td class="titlefield">'.$langs->trans('Discounts').'</td><td>';
  2082. $absolute_discount = $soc->getAvailableDiscounts('', $filterabsolutediscount);
  2083. $absolute_creditnote = $soc->getAvailableDiscounts('', $filtercreditnote);
  2084. $absolute_discount = price2num($absolute_discount, 'MT');
  2085. $absolute_creditnote = price2num($absolute_creditnote, 'MT');
  2086. $caneditfield = ($object->statut != Propal::STATUS_SIGNED && $object->statut != Propal::STATUS_BILLED);
  2087. $thirdparty = $soc;
  2088. $discount_type = 0;
  2089. $backtopage = urlencode($_SERVER["PHP_SELF"].'?id='.$object->id);
  2090. include DOL_DOCUMENT_ROOT.'/core/tpl/object_discounts.tpl.php';
  2091. print '</td></tr>';
  2092. // Date of proposal
  2093. print '<tr>';
  2094. print '<td>';
  2095. // print '<table class="nobordernopadding" width="100%"><tr><td>';
  2096. // print $langs->trans('DatePropal');
  2097. // print '</td>';
  2098. // if ($action != 'editdate' && $usercancreate && $caneditfield) {
  2099. // print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editdate&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->trans('SetDate'), 1).'</a></td>';
  2100. // }
  2101. // print '</tr></table>';
  2102. $editenable = $usercancreate && $caneditfield && $object->statut == Propal::STATUS_DRAFT;
  2103. print $form->editfieldkey("DatePropal", 'date', '', $object, $editenable);
  2104. print '</td><td class="valuefield">';
  2105. if ($action == 'editdate' && $usercancreate && $caneditfield) {
  2106. print '<form name="editdate" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="post">';
  2107. print '<input type="hidden" name="token" value="'.newToken().'">';
  2108. print '<input type="hidden" name="action" value="setdate">';
  2109. print $form->selectDate($object->date, 're', '', '', 0, "editdate");
  2110. print '<input type="submit" class="button button-edit" value="'.$langs->trans('Modify').'">';
  2111. print '</form>';
  2112. } else {
  2113. if ($object->date) {
  2114. print dol_print_date($object->date, 'day');
  2115. } else {
  2116. print '&nbsp;';
  2117. }
  2118. }
  2119. print '</td>';
  2120. // Date end proposal
  2121. print '<tr>';
  2122. print '<td>';
  2123. print '<table class="nobordernopadding centpercent"><tr><td>';
  2124. print $langs->trans('DateEndPropal');
  2125. print '</td>';
  2126. if ($action != 'editecheance' && $usercancreate && $caneditfield) {
  2127. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editecheance&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->trans('SetConditions'), 1).'</a></td>';
  2128. }
  2129. print '</tr></table>';
  2130. print '</td><td class="valuefield">';
  2131. if ($action == 'editecheance' && $usercancreate && $caneditfield) {
  2132. print '<form name="editecheance" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="post">';
  2133. print '<input type="hidden" name="token" value="'.newToken().'">';
  2134. print '<input type="hidden" name="action" value="setecheance">';
  2135. print $form->selectDate($object->fin_validite, 'ech', '', '', '', "editecheance");
  2136. print '<input type="submit" class="button button-edit" value="'.$langs->trans('Modify').'">';
  2137. print '</form>';
  2138. } else {
  2139. if (!empty($object->fin_validite)) {
  2140. print dol_print_date($object->fin_validite, 'day');
  2141. if ($object->statut == Propal::STATUS_VALIDATED && $object->fin_validite < ($now - $conf->propal->cloture->warning_delay)) {
  2142. print img_warning($langs->trans("Late"));
  2143. }
  2144. } else {
  2145. print '&nbsp;';
  2146. }
  2147. }
  2148. print '</td>';
  2149. print '</tr>';
  2150. // Payment term
  2151. print '<tr><td>';
  2152. print '<table class="nobordernopadding" width="100%"><tr><td>';
  2153. print $langs->trans('PaymentConditionsShort');
  2154. print '</td>';
  2155. if ($action != 'editconditions' && $usercancreate && $caneditfield) {
  2156. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editconditions&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetConditions'), 1).'</a></td>';
  2157. }
  2158. print '</tr></table>';
  2159. print '</td><td class="valuefield">';
  2160. if ($action == 'editconditions' && $usercancreate && $caneditfield) {
  2161. $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->cond_reglement_id, 'cond_reglement_id', 0, '', 1, $object->deposit_percent);
  2162. } else {
  2163. $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->cond_reglement_id, 'none', 0, '', 1, $object->deposit_percent);
  2164. }
  2165. print '</td>';
  2166. print '</tr>';
  2167. // Payment mode
  2168. print '<tr class="field_mode_reglement_id">';
  2169. print '<td class="titlefieldcreate">';
  2170. print '<table class="nobordernopadding centpercent"><tr><td>';
  2171. print $langs->trans('PaymentMode');
  2172. print '</td>';
  2173. if ($action != 'editmode' && $usercancreate && $caneditfield) {
  2174. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editmode&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetMode'), 1).'</a></td>';
  2175. }
  2176. print '</tr></table>';
  2177. print '</td><td class="valuefieldcreate">';
  2178. if ($action == 'editmode' && $usercancreate && $caneditfield) {
  2179. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id, 'mode_reglement_id', 'CRDT', 1, 1);
  2180. } else {
  2181. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id, 'none');
  2182. }
  2183. print '</td></tr>';
  2184. // Delivery date
  2185. $langs->load('deliveries');
  2186. print '<tr><td>';
  2187. print $form->editfieldkey($langs->trans('DeliveryDate'), 'date_livraison', $object->delivery_date, $object, $usercancreate && $caneditfield, 'datepicker');
  2188. print '</td><td class="valuefieldedit">';
  2189. print $form->editfieldval($langs->trans('DeliveryDate'), 'date_livraison', $object->delivery_date, $object, $usercancreate && $caneditfield, 'datepicker');
  2190. print '</td>';
  2191. print '</tr>';
  2192. // Delivery delay
  2193. print '<tr class="fielddeliverydelay"><td>';
  2194. print '<table class="nobordernopadding" width="100%"><tr><td>';
  2195. if (isModEnabled('commande')) {
  2196. print $form->textwithpicto($langs->trans('AvailabilityPeriod'), $langs->trans('AvailabilityPeriod').' ('.$langs->trans('AfterOrder').')');
  2197. } else {
  2198. print $langs->trans('AvailabilityPeriod');
  2199. }
  2200. print '</td>';
  2201. if ($action != 'editavailability' && $usercancreate && $caneditfield) {
  2202. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editavailability&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetAvailability'), 1).'</a></td>';
  2203. }
  2204. print '</tr></table>';
  2205. print '</td><td class="valuefield">';
  2206. if ($action == 'editavailability' && $usercancreate && $caneditfield) {
  2207. $form->form_availability($_SERVER['PHP_SELF'].'?id='.$object->id, $object->availability_id, 'availability_id', 1);
  2208. } else {
  2209. $form->form_availability($_SERVER['PHP_SELF'].'?id='.$object->id, $object->availability_id, 'none', 1);
  2210. }
  2211. print '</td>';
  2212. print '</tr>';
  2213. // Shipping Method
  2214. if (isModEnabled("expedition")) {
  2215. print '<tr><td>';
  2216. print '<table class="nobordernopadding centpercent"><tr><td>';
  2217. print $langs->trans('SendingMethod');
  2218. print '</td>';
  2219. if ($action != 'editshippingmethod' && $usercancreate && $caneditfield) {
  2220. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editshippingmethod&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->trans('SetShippingMode'), 1).'</a></td>';
  2221. }
  2222. print '</tr></table>';
  2223. print '</td><td class="valuefield">';
  2224. if ($action == 'editshippingmethod' && $usercancreate && $caneditfield) {
  2225. $form->formSelectShippingMethod($_SERVER['PHP_SELF'].'?id='.$object->id, $object->shipping_method_id, 'shipping_method_id', 1);
  2226. } else {
  2227. $form->formSelectShippingMethod($_SERVER['PHP_SELF'].'?id='.$object->id, $object->shipping_method_id, 'none');
  2228. }
  2229. print '</td>';
  2230. print '</tr>';
  2231. }
  2232. // Warehouse
  2233. if (isModEnabled('stock') && !empty($conf->global->WAREHOUSE_ASK_WAREHOUSE_DURING_PROPAL)) {
  2234. $langs->load('stocks');
  2235. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  2236. $formproduct = new FormProduct($db);
  2237. print '<tr class="field_warehouse_id"><td class="titlefieldcreate">';
  2238. $editenable = $usercancreate;
  2239. print $form->editfieldkey("Warehouse", 'warehouse', '', $object, $editenable);
  2240. print '</td><td class="valuefieldcreate">';
  2241. if ($action == 'editwarehouse') {
  2242. $formproduct->formSelectWarehouses($_SERVER['PHP_SELF'].'?id='.$object->id, $object->warehouse_id, 'warehouse_id', 1);
  2243. } else {
  2244. $formproduct->formSelectWarehouses($_SERVER['PHP_SELF'].'?id='.$object->id, $object->warehouse_id, 'none');
  2245. }
  2246. print '</td>';
  2247. print '</tr>';
  2248. }
  2249. // Origin of demand
  2250. print '<tr><td>';
  2251. print '<table class="nobordernopadding centpercent"><tr><td>';
  2252. print $langs->trans('Source');
  2253. print '</td>';
  2254. if ($action != 'editdemandreason' && $usercancreate) {
  2255. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editdemandreason&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetDemandReason'), 1).'</a></td>';
  2256. }
  2257. print '</tr></table>';
  2258. print '</td><td class="valuefield">';
  2259. if ($action == 'editdemandreason' && $usercancreate) {
  2260. $form->formInputReason($_SERVER['PHP_SELF'].'?id='.$object->id, $object->demand_reason_id, 'demand_reason_id', 1);
  2261. } else {
  2262. $form->formInputReason($_SERVER['PHP_SELF'].'?id='.$object->id, $object->demand_reason_id, 'none');
  2263. }
  2264. print '</td>';
  2265. print '</tr>';
  2266. // Multicurrency
  2267. if (isModEnabled("multicurrency")) {
  2268. // Multicurrency code
  2269. print '<tr>';
  2270. print '<td>';
  2271. print '<table class="nobordernopadding" width="100%"><tr><td>';
  2272. print $form->editfieldkey('Currency', 'multicurrency_code', '', $object, 0);
  2273. print '</td>';
  2274. if ($action != 'editmulticurrencycode' && $object->statut == $object::STATUS_DRAFT && $usercancreate) {
  2275. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editmulticurrencycode&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1).'</a></td>';
  2276. }
  2277. print '</tr></table>';
  2278. print '</td><td class="valuefield">';
  2279. if ($object->statut == $object::STATUS_DRAFT && $action == 'editmulticurrencycode' && $usercancreate) {
  2280. $form->form_multicurrency_code($_SERVER['PHP_SELF'].'?id='.$object->id, $object->multicurrency_code, 'multicurrency_code');
  2281. } else {
  2282. $form->form_multicurrency_code($_SERVER['PHP_SELF'].'?id='.$object->id, $object->multicurrency_code, 'none');
  2283. }
  2284. print '</td></tr>';
  2285. // Multicurrency rate
  2286. if ($object->multicurrency_code != $conf->currency || $object->multicurrency_tx != 1) {
  2287. print '<tr>';
  2288. print '<td>';
  2289. print '<table class="nobordernopadding" width="100%"><tr>';
  2290. print '<td>';
  2291. print $form->editfieldkey('CurrencyRate', 'multicurrency_tx', '', $object, 0);
  2292. print '</td>';
  2293. if ($action != 'editmulticurrencyrate' && $object->statut == $object::STATUS_DRAFT && $object->multicurrency_code && $object->multicurrency_code != $conf->currency && $usercancreate) {
  2294. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editmulticurrencyrate&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1).'</a></td>';
  2295. }
  2296. print '</tr></table>';
  2297. print '</td><td class="valuefield">';
  2298. if ($object->statut == $object::STATUS_DRAFT && ($action == 'editmulticurrencyrate' || $action == 'actualizemulticurrencyrate') && $usercancreate) {
  2299. if ($action == 'actualizemulticurrencyrate') {
  2300. list($object->fk_multicurrency, $object->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($object->db, $object->multicurrency_code);
  2301. }
  2302. $form->form_multicurrency_rate($_SERVER['PHP_SELF'].'?id='.$object->id, $object->multicurrency_tx, 'multicurrency_tx', $object->multicurrency_code);
  2303. } else {
  2304. $form->form_multicurrency_rate($_SERVER['PHP_SELF'].'?id='.$object->id, $object->multicurrency_tx, 'none', $object->multicurrency_code);
  2305. if ($object->statut == $object::STATUS_DRAFT && $object->multicurrency_code && $object->multicurrency_code != $conf->currency) {
  2306. print '<div class="inline-block"> &nbsp; &nbsp; &nbsp; &nbsp; ';
  2307. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=actualizemulticurrencyrate">'.$langs->trans("ActualizeCurrency").'</a>';
  2308. print '</div>';
  2309. }
  2310. }
  2311. print '</td></tr>';
  2312. }
  2313. }
  2314. if ($soc->outstanding_limit) {
  2315. // Outstanding Bill
  2316. print '<tr><td>';
  2317. print $langs->trans('OutstandingBill');
  2318. print '</td><td class="valuefield">';
  2319. $arrayoutstandingbills = $soc->getOutstandingBills();
  2320. print ($arrayoutstandingbills['opened'] > $soc->outstanding_limit ? img_warning() : '');
  2321. print price($arrayoutstandingbills['opened']).' / ';
  2322. print price($soc->outstanding_limit, 0, $langs, 1, - 1, - 1, $conf->currency);
  2323. print '</td>';
  2324. print '</tr>';
  2325. }
  2326. if (!empty($conf->global->BANK_ASK_PAYMENT_BANK_DURING_PROPOSAL) && isModEnabled("banque")) {
  2327. // Bank Account
  2328. print '<tr><td>';
  2329. print '<table width="100%" class="nobordernopadding"><tr><td>';
  2330. print $langs->trans('BankAccount');
  2331. print '</td>';
  2332. if ($action != 'editbankaccount' && $usercancreate) {
  2333. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editbankaccount&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->trans('SetBankAccount'), 1).'</a></td>';
  2334. }
  2335. print '</tr></table>';
  2336. print '</td><td class="valuefield">';
  2337. if ($action == 'editbankaccount') {
  2338. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'fk_account', 1);
  2339. } else {
  2340. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'none');
  2341. }
  2342. print '</td>';
  2343. print '</tr>';
  2344. }
  2345. $tmparray = $object->getTotalWeightVolume();
  2346. $totalWeight = $tmparray['weight'];
  2347. $totalVolume = $tmparray['volume'];
  2348. if ($totalWeight) {
  2349. print '<tr><td>'.$langs->trans("CalculatedWeight").'</td>';
  2350. print '<td class="valuefield">';
  2351. print showDimensionInBestUnit($totalWeight, 0, "weight", $langs, isset($conf->global->MAIN_WEIGHT_DEFAULT_ROUND) ? $conf->global->MAIN_WEIGHT_DEFAULT_ROUND : -1, isset($conf->global->MAIN_WEIGHT_DEFAULT_UNIT) ? $conf->global->MAIN_WEIGHT_DEFAULT_UNIT : 'no', 0);
  2352. print '</td></tr>';
  2353. }
  2354. if ($totalVolume) {
  2355. print '<tr><td>'.$langs->trans("CalculatedVolume").'</td>';
  2356. print '<td class="valuefield">';
  2357. print showDimensionInBestUnit($totalVolume, 0, "volume", $langs, isset($conf->global->MAIN_VOLUME_DEFAULT_ROUND) ? $conf->global->MAIN_VOLUME_DEFAULT_ROUND : -1, isset($conf->global->MAIN_VOLUME_DEFAULT_UNIT) ? $conf->global->MAIN_VOLUME_DEFAULT_UNIT : 'no', 0);
  2358. print '</td></tr>';
  2359. }
  2360. // Incoterms
  2361. if (isModEnabled('incoterm')) {
  2362. print '<tr><td>';
  2363. print '<table width="100%" class="nobordernopadding"><tr><td>';
  2364. print $langs->trans('IncotermLabel');
  2365. print '<td><td class="right">';
  2366. if ($action != 'editincoterm' && $usercancreate && $caneditfield) {
  2367. print '<a class="editfielda" href="'.DOL_URL_ROOT.'/comm/propal/card.php?id='.$object->id.'&action=editincoterm&token='.newToken().'">'.img_edit().'</a>';
  2368. } else {
  2369. print '&nbsp;';
  2370. }
  2371. print '</td></tr></table>';
  2372. print '</td>';
  2373. print '<td class="valuefield">';
  2374. if ($action == 'editincoterm' && $usercancreate && $caneditfield) {
  2375. print $form->select_incoterms((!empty($object->fk_incoterms) ? $object->fk_incoterms : ''), (!empty($object->location_incoterms) ? $object->location_incoterms : ''), $_SERVER['PHP_SELF'].'?id='.$object->id);
  2376. } else {
  2377. print $form->textwithpicto($object->display_incoterms(), $object->label_incoterms, 1);
  2378. }
  2379. print '</td></tr>';
  2380. }
  2381. // Other attributes
  2382. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  2383. print '</table>';
  2384. print '</div>';
  2385. print '<div class="fichehalfright">';
  2386. print '<div class="underbanner clearboth"></div>';
  2387. print '<table class="border tableforfield centpercent">';
  2388. if (isModEnabled("multicurrency") && ($object->multicurrency_code && $object->multicurrency_code != $conf->currency)) {
  2389. // Multicurrency Amount HT
  2390. print '<tr><td class="titlefieldmiddle">'.$form->editfieldkey('MulticurrencyAmountHT', 'multicurrency_total_ht', '', $object, 0).'</td>';
  2391. print '<td class="nowrap right amountcard">'.price($object->multicurrency_total_ht, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).'</td>';
  2392. print '</tr>';
  2393. // Multicurrency Amount VAT
  2394. print '<tr><td>'.$form->editfieldkey('MulticurrencyAmountVAT', 'multicurrency_total_tva', '', $object, 0).'</td>';
  2395. print '<td class="nowrap right amountcard">'.price($object->multicurrency_total_tva, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).'</td>';
  2396. print '</tr>';
  2397. // Multicurrency Amount TTC
  2398. print '<tr><td>'.$form->editfieldkey('MulticurrencyAmountTTC', 'multicurrency_total_ttc', '', $object, 0).'</td>';
  2399. print '<td class="nowrap right amountcard">'.price($object->multicurrency_total_ttc, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).'</td>';
  2400. print '</tr>';
  2401. }
  2402. // Amount HT
  2403. print '<tr><td class="titlefieldmiddle">'.$langs->trans('AmountHT').'</td>';
  2404. print '<td class="nowrap right amountcard">'.price($object->total_ht, '', $langs, 0, - 1, - 1, $conf->currency).'</td>';
  2405. print '</tr>';
  2406. // Amount VAT
  2407. print '<tr><td>'.$langs->trans('AmountVAT').'</td>';
  2408. print '<td class="nowrap right amountcard">'.price($object->total_tva, '', $langs, 0, - 1, - 1, $conf->currency).'</td>';
  2409. print '</tr>';
  2410. // Amount Local Taxes
  2411. if ($mysoc->localtax1_assuj == "1" || $object->total_localtax1 != 0) { // Localtax1
  2412. print '<tr><td>'.$langs->transcountry("AmountLT1", $mysoc->country_code).'</td>';
  2413. print '<td class="nowrap right amountcard">'.price($object->total_localtax1, '', $langs, 0, - 1, - 1, $conf->currency).'</td>';
  2414. print '</tr>';
  2415. }
  2416. if ($mysoc->localtax2_assuj == "1" || $object->total_localtax2 != 0) { // Localtax2
  2417. print '<tr><td>'.$langs->transcountry("AmountLT2", $mysoc->country_code).'</td>';
  2418. print '<td class="nowrap right amountcard">'.price($object->total_localtax2, '', $langs, 0, - 1, - 1, $conf->currency).'</td>';
  2419. print '</tr>';
  2420. }
  2421. // Amount TTC
  2422. print '<tr><td>'.$langs->trans('AmountTTC').'</td>';
  2423. print '<td class="nowrap right amountcard">'.price($object->total_ttc, '', $langs, 0, - 1, - 1, $conf->currency).'</td>';
  2424. print '</tr>';
  2425. // Statut
  2426. //print '<tr><td height="10">' . $langs->trans('Status') . '</td><td class="left" colspan="2">' . $object->getLibStatut(4) . '</td></tr>';
  2427. print '</table>';
  2428. // Margin Infos
  2429. if (isModEnabled('margin')) {
  2430. $formmargin->displayMarginInfos($object);
  2431. }
  2432. print '</div>';
  2433. print '</div>';
  2434. print '<div class="clearboth"></div><br>';
  2435. if (!empty($conf->global->MAIN_DISABLE_CONTACTS_TAB)) {
  2436. $blocname = 'contacts';
  2437. $title = $langs->trans('ContactsAddresses');
  2438. include DOL_DOCUMENT_ROOT.'/core/tpl/bloc_showhide.tpl.php';
  2439. }
  2440. if (!empty($conf->global->MAIN_DISABLE_NOTES_TAB)) {
  2441. $blocname = 'notes';
  2442. $title = $langs->trans('Notes');
  2443. include DOL_DOCUMENT_ROOT.'/core/tpl/bloc_showhide.tpl.php';
  2444. }
  2445. /*
  2446. * Lines
  2447. */
  2448. // Get object lines
  2449. $result = $object->getLinesArray();
  2450. // Add products/services form
  2451. //$forceall = 1;
  2452. global $inputalsopricewithtax;
  2453. $inputalsopricewithtax = 1;
  2454. print ' <form name="addproduct" id="addproduct" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">
  2455. <input type="hidden" name="token" value="' . newToken().'">
  2456. <input type="hidden" name="action" value="' . (($action != 'editline') ? 'addline' : 'updateline').'">
  2457. <input type="hidden" name="mode" value="">
  2458. <input type="hidden" name="page_y" value="">
  2459. <input type="hidden" name="id" value="' . $object->id.'">
  2460. ';
  2461. if (!empty($conf->use_javascript_ajax) && $object->statut == Propal::STATUS_DRAFT) {
  2462. include DOL_DOCUMENT_ROOT.'/core/tpl/ajaxrow.tpl.php';
  2463. }
  2464. print '<div class="div-table-responsive-no-min">';
  2465. if (!empty($object->lines) || ($object->statut == Propal::STATUS_DRAFT && $usercancreate && $action != 'selectlines' && $action != 'editline')) {
  2466. print '<table id="tablelines" class="noborder noshadow" width="100%">';
  2467. }
  2468. if (!empty($object->lines)) {
  2469. $object->printObjectLines($action, $mysoc, $object->thirdparty, $lineid, 1);
  2470. }
  2471. // Form to add new line
  2472. if ($object->statut == Propal::STATUS_DRAFT && $usercancreate && $action != 'selectlines') {
  2473. if ($action != 'editline') {
  2474. $parameters = array();
  2475. $reshook = $hookmanager->executeHooks('formAddObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  2476. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  2477. if (empty($reshook))
  2478. $object->formAddObjectLine(1, $mysoc, $soc);
  2479. } else {
  2480. $parameters = array();
  2481. $reshook = $hookmanager->executeHooks('formEditObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  2482. }
  2483. }
  2484. if (!empty($object->lines) || ($object->statut == Propal::STATUS_DRAFT && $usercancreate && $action != 'selectlines' && $action != 'editline')) {
  2485. print '</table>';
  2486. }
  2487. print '</div>';
  2488. print "</form>\n";
  2489. print dol_get_fiche_end();
  2490. /*
  2491. * Button Actions
  2492. */
  2493. if ($action != 'presend') {
  2494. print '<div class="tabsAction">';
  2495. $parameters = array();
  2496. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
  2497. // modified by hook
  2498. if (empty($reshook)) {
  2499. if ($action != 'editline') {
  2500. // Validate
  2501. if (($object->statut == Propal::STATUS_DRAFT && $object->total_ttc >= 0 && count($object->lines) > 0)
  2502. || ($object->statut == Propal::STATUS_DRAFT && !empty($conf->global->PROPAL_ENABLE_NEGATIVE) && count($object->lines) > 0)) {
  2503. if ($usercanvalidate) {
  2504. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=validate&token='.newToken().'">'.(empty($conf->global->PROPAL_SKIP_ACCEPT_REFUSE) ? $langs->trans('Validate') : $langs->trans('ValidateAndSign')).'</a>';
  2505. } else {
  2506. print '<a class="butActionRefused classfortooltip" href="#">'.$langs->trans('Validate').'</a>';
  2507. }
  2508. }
  2509. // Create event
  2510. /*if ($conf->agenda->enabled && !empty($conf->global->MAIN_ADD_EVENT_ON_ELEMENT_CARD)) // Add hidden condition because this is not a "workflow" action so should appears somewhere else on page.
  2511. {
  2512. print '<a class="butAction" href="' . DOL_URL_ROOT . '/comm/action/card.php?action=create&amp;origin=' . $object->element . '&amp;originid=' . $object->id . '&amp;socid=' . $object->socid . '">' . $langs->trans("AddAction") . '</a></div>';
  2513. }*/
  2514. // Edit
  2515. if ($object->statut == Propal::STATUS_VALIDATED && $usercancreate) {
  2516. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=modif&token='.newToken().'">'.$langs->trans('Modify').'</a>';
  2517. }
  2518. // ReOpen
  2519. if ( (( !empty($conf->global->PROPAL_REOPEN_UNSIGNED_ONLY) && $object->statut == Propal::STATUS_NOTSIGNED) || (empty($conf->global->PROPAL_REOPEN_UNSIGNED_ONLY) && ($object->statut == Propal::STATUS_SIGNED || $object->statut == Propal::STATUS_NOTSIGNED || $object->statut == Propal::STATUS_BILLED))) && $usercanclose) {
  2520. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=reopen&token='.newToken().(empty($conf->global->MAIN_JUMP_TAG) ? '' : '#reopen').'"';
  2521. print '>'.$langs->trans('ReOpen').'</a>';
  2522. }
  2523. // Send
  2524. if (empty($user->socid)) {
  2525. if ($object->statut == Propal::STATUS_VALIDATED || $object->statut == Propal::STATUS_SIGNED || !empty($conf->global->PROPOSAL_SENDBYEMAIL_FOR_ALL_STATUS)) {
  2526. print dolGetButtonAction('', $langs->trans('SendMail'), 'default', $_SERVER["PHP_SELF"].'?action=presend&token='.newToken().'&id='.$object->id.'&mode=init#formmailbeforetitle', '', $usercansend);
  2527. }
  2528. }
  2529. // Create a sale order
  2530. if (isModEnabled('commande') && $object->statut == Propal::STATUS_SIGNED) {
  2531. if ($usercancreateorder) {
  2532. print '<a class="butAction" href="'.DOL_URL_ROOT.'/commande/card.php?action=create&origin='.$object->element.'&originid='.$object->id.'&socid='.$object->socid.'">'.$langs->trans("AddOrder").'</a>';
  2533. }
  2534. }
  2535. // Create a purchase order
  2536. if (!empty($conf->global->WORKFLOW_CAN_CREATE_PURCHASE_ORDER_FROM_PROPOSAL)) {
  2537. if ($object->statut == Propal::STATUS_SIGNED && ((isModEnabled("fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || isModEnabled("supplier_order"))) {
  2538. if ($usercancreatepurchaseorder) {
  2539. print '<a class="butAction" href="'.DOL_URL_ROOT.'/fourn/commande/card.php?action=create&origin='.$object->element.'&originid='.$object->id.'&socid='.$object->socid.'">'.$langs->trans("AddPurchaseOrder").'</a>';
  2540. }
  2541. }
  2542. }
  2543. // Create an intervention
  2544. if (isModEnabled("service") && isModEnabled('ficheinter') && $object->statut == Propal::STATUS_SIGNED) {
  2545. if ($usercancreateintervention) {
  2546. $langs->load("interventions");
  2547. print '<a class="butAction" href="'.DOL_URL_ROOT.'/fichinter/card.php?action=create&origin='.$object->element.'&originid='.$object->id.'&socid='.$object->socid.'">'.$langs->trans("AddIntervention").'</a>';
  2548. }
  2549. }
  2550. // Create contract
  2551. if (isModEnabled('contrat') && $object->statut == Propal::STATUS_SIGNED) {
  2552. $langs->load("contracts");
  2553. if ($usercancreatecontract) {
  2554. print '<a class="butAction" href="'.DOL_URL_ROOT.'/contrat/card.php?action=create&origin='.$object->element.'&originid='.$object->id.'&socid='.$object->socid.'">'.$langs->trans('AddContract').'</a>';
  2555. }
  2556. }
  2557. // Create an invoice and classify billed
  2558. if ($object->statut == Propal::STATUS_SIGNED && empty($conf->global->PROPOSAL_ARE_NOT_BILLABLE)) {
  2559. if (isModEnabled('facture') && $usercancreateinvoice) {
  2560. print '<a class="butAction" href="'.DOL_URL_ROOT.'/compta/facture/card.php?action=create&origin='.$object->element.'&originid='.$object->id.'&socid='.$object->socid.'">'.$langs->trans("CreateBill").'</a>';
  2561. }
  2562. $arrayofinvoiceforpropal = $object->getInvoiceArrayList();
  2563. if ((is_array($arrayofinvoiceforpropal) && count($arrayofinvoiceforpropal) > 0) || empty($conf->global->WORKFLOW_PROPAL_NEED_INVOICE_TO_BE_CLASSIFIED_BILLED)) {
  2564. if ($usercanclose) {
  2565. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=classifybilled&token='.newToken().'&socid='.$object->socid.'">'.$langs->trans("ClassifyBilled").'</a>';
  2566. } else {
  2567. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("NotEnoughPermissions").'">'.$langs->trans("ClassifyBilled").'</a>';
  2568. }
  2569. }
  2570. }
  2571. if (empty($conf->global->PROPAL_SKIP_ACCEPT_REFUSE)) {
  2572. // Close as accepted/refused
  2573. if ($object->statut == Propal::STATUS_VALIDATED) {
  2574. if ($usercanclose) {
  2575. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=closeas&token='.newToken().(empty($conf->global->MAIN_JUMP_TAG) ? '' : '#close').'"';
  2576. print '>'.$langs->trans('SetAcceptedRefused').'</a>';
  2577. } else {
  2578. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("NotEnoughPermissions").'"';
  2579. print '>'.$langs->trans('SetAcceptedRefused').'</a>';
  2580. }
  2581. }
  2582. } else {
  2583. // Set not signed (close)
  2584. if ($object->statut == Propal::STATUS_DRAFT && $usercanclose) {
  2585. print '<a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&token='.newToken().'&action=closeas&token='.newToken() . (empty($conf->global->MAIN_JUMP_TAG) ? '' : '#close') . '"';
  2586. print '>' . $langs->trans('SetRefusedAndClose') . '</a>';
  2587. }
  2588. }
  2589. // Clone
  2590. if ($usercancreate) {
  2591. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&socid='.$object->socid.'&action=clone&token='.newToken().'&object='.$object->element.'">'.$langs->trans("ToClone").'</a>';
  2592. }
  2593. // Delete
  2594. print dolGetButtonAction($langs->trans("Delete"), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), 'delete', $usercandelete);
  2595. }
  2596. }
  2597. print '</div>';
  2598. }
  2599. //Select mail models is same action as presend
  2600. if (GETPOST('modelselected')) {
  2601. $action = 'presend';
  2602. }
  2603. if ($action != 'presend') {
  2604. print '<div class="fichecenter"><div class="fichehalfleft">';
  2605. print '<a name="builddoc"></a>'; // ancre
  2606. /*
  2607. * Generated documents
  2608. */
  2609. $objref = dol_sanitizeFileName($object->ref);
  2610. $filedir = $conf->propal->multidir_output[$object->entity]."/".dol_sanitizeFileName($object->ref);
  2611. $urlsource = $_SERVER["PHP_SELF"]."?id=".$object->id;
  2612. $genallowed = $usercanread;
  2613. $delallowed = $usercancreate;
  2614. print $formfile->showdocuments('propal', $objref, $filedir, $urlsource, $genallowed, $delallowed, $object->model_pdf, 1, 0, 0, 28, 0, '', 0, '', $soc->default_lang, '', $object);
  2615. // Show links to link elements
  2616. $linktoelem = $form->showLinkToObjectBlock($object, null, array('propal'));
  2617. $compatibleImportElementsList = false;
  2618. if ($user->rights->propal->creer && $object->statut == Propal::STATUS_DRAFT) {
  2619. $compatibleImportElementsList = array('commande', 'propal'); // import from linked elements
  2620. }
  2621. $somethingshown = $form->showLinkedObjectBlock($object, $linktoelem, $compatibleImportElementsList);
  2622. // Show online signature link
  2623. $useonlinesignature = 1;
  2624. if ($object->statut != Propal::STATUS_DRAFT && $useonlinesignature) {
  2625. print '<br><!-- Link to sign -->';
  2626. require_once DOL_DOCUMENT_ROOT.'/core/lib/signature.lib.php';
  2627. print showOnlineSignatureUrl('proposal', $object->ref).'<br>';
  2628. }
  2629. print '</div><div class="fichehalfright">';
  2630. // List of actions on element
  2631. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
  2632. $formactions = new FormActions($db);
  2633. $somethingshown = $formactions->showactions($object, 'propal', $socid, 1);
  2634. print '</div></div>';
  2635. }
  2636. // Presend form
  2637. $modelmail = 'propal_send';
  2638. $defaulttopic = 'SendPropalRef';
  2639. $diroutput = $conf->propal->multidir_output[$object->entity];
  2640. $trackid = 'pro'.$object->id;
  2641. include DOL_DOCUMENT_ROOT.'/core/tpl/card_presend.tpl.php';
  2642. }
  2643. // End of page
  2644. llxFooter();
  2645. $db->close();