card.php 125 KB

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