card.php 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  1. <?php
  2. /* Copyright (C) 2003-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2019 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2014 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
  6. * Copyright (C) 2010-2017 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2013 Christophe Battarel <christophe.battarel@altairis.fr>
  8. * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
  9. * Copyright (C) 2014-2020 Ferran Marcet <fmarcet@2byte.es>
  10. * Copyright (C) 2014-2016 Marcos García <marcosgdf@gmail.com>
  11. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  12. * Copyright (C) 2018-2021 Frédéric France <frederic.france@netlogic.fr>
  13. * Copyright (C) 2023 Charlene Benke <charlene@patas-monkey.com>
  14. * Copyright (C) 2023 Nick Fragoulis
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  28. */
  29. /**
  30. * \file htdocs/contrat/card.php
  31. * \ingroup contrat
  32. * \brief Page of a contract
  33. */
  34. require "../main.inc.php";
  35. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/lib/contract.lib.php';
  38. require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  39. require_once DOL_DOCUMENT_ROOT.'/core/modules/contract/modules_contract.php';
  40. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  42. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  43. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  44. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  45. if (isModEnabled("propal")) {
  46. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  47. }
  48. if (isModEnabled('project')) {
  49. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  50. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
  51. }
  52. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  53. // Load translation files required by the page
  54. $langs->loadLangs(array("contracts", "orders", "companies", "bills", "products", 'compta'));
  55. $action = GETPOST('action', 'aZ09');
  56. $confirm = GETPOST('confirm', 'alpha');
  57. $cancel = GETPOST('cancel', 'alpha');
  58. $backtopage = GETPOST('backtopage', 'alpha');
  59. $socid = GETPOST('socid', 'int');
  60. $id = GETPOST('id', 'int');
  61. $ref = GETPOST('ref', 'alpha');
  62. $origin = GETPOST('origin', 'alpha');
  63. $originid = GETPOST('originid', 'int');
  64. // PDF
  65. $hidedetails = (GETPOST('hidedetails', 'int') ? GETPOST('hidedetails', 'int') : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS') ? 1 : 0));
  66. $hidedesc = (GETPOST('hidedesc', 'int') ? GETPOST('hidedesc', 'int') : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DESC') ? 1 : 0));
  67. $hideref = (GETPOST('hideref', 'int') ? GETPOST('hideref', 'int') : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_REF') ? 1 : 0));
  68. $datecontrat = '';
  69. $usehm = (getDolGlobalString('MAIN_USE_HOURMIN_IN_DATE_RANGE') ? $conf->global->MAIN_USE_HOURMIN_IN_DATE_RANGE : 0);
  70. // Security check
  71. if ($user->socid) {
  72. $socid = $user->socid;
  73. }
  74. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  75. $hookmanager->initHooks(array('contractcard', 'globalcard'));
  76. $object = new Contrat($db);
  77. $extrafields = new ExtraFields($db);
  78. // Load object
  79. if ($id > 0 || !empty($ref) && $action != 'add') {
  80. $ret = $object->fetch($id, $ref);
  81. if ($ret > 0) {
  82. $ret = $object->fetch_thirdparty();
  83. }
  84. if ($ret < 0) {
  85. dol_print_error('', $object->error);
  86. }
  87. }
  88. // fetch optionals attributes and labels
  89. $extrafields->fetch_name_optionals_label($object->table_element);
  90. // fetch optionals attributes lines and labels
  91. $extralabelslines = $extrafields->fetch_name_optionals_label($object->table_element_line);
  92. $permissionnote = $user->hasRight('contrat', 'creer'); // Used by the include of actions_setnotes.inc.php
  93. $permissiondellink = $user->hasRight('contrat', 'creer'); // Used by the include of actions_dellink.inc.php
  94. $permissiontodelete = ($user->hasRight('contrat', 'creer') && $object->statut == $object::STATUS_DRAFT) || $user->hasRight('contrat', 'supprimer');
  95. $permissiontoadd = $user->hasRight('contrat', 'creer'); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
  96. $permissiontoedit = $permissiontoadd;
  97. $permissiontoactivate = $user->hasRight('contrat', 'activer');
  98. $error = 0;
  99. $result = restrictedArea($user, 'contrat', $object->id);
  100. /*
  101. * Actions
  102. */
  103. $parameters = array('socid' => $socid);
  104. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  105. if ($reshook < 0) {
  106. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  107. }
  108. if (empty($reshook)) {
  109. $backurlforlist = DOL_URL_ROOT.'/contrat/list.php';
  110. if (empty($backtopage) || ($cancel && empty($id))) {
  111. if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
  112. if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
  113. $backtopage = $backurlforlist;
  114. } else {
  115. $backtopage = DOL_URL_ROOT.'/contrat/card.php?id='.((!empty($id) && $id > 0) ? $id : '__ID__');
  116. }
  117. }
  118. }
  119. if ($cancel) {
  120. if (!empty($backtopageforcancel)) {
  121. header("Location: ".$backtopageforcancel);
  122. exit;
  123. } elseif (!empty($backtopage)) {
  124. header("Location: ".$backtopage);
  125. exit;
  126. }
  127. $action = '';
  128. }
  129. include DOL_DOCUMENT_ROOT.'/core/actions_setnotes.inc.php'; // Must be include, not includ_once
  130. include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php'; // Must be include, not include_once
  131. include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; // Must be include, not include_once
  132. if ($action == 'confirm_active' && $confirm == 'yes' && $permissiontoactivate) {
  133. $date_start = '';
  134. $date_end = '';
  135. if (GETPOST('startmonth') && GETPOST('startday') && GETPOST('startyear')) {
  136. $date_start = dol_mktime(GETPOST('starthour'), GETPOST('startmin'), 0, GETPOST('startmonth'), GETPOST('startday'), GETPOST('startyear'));
  137. }
  138. if (GETPOST('endmonth') && GETPOST('endday') && GETPOST('endyear')) {
  139. $date_end = dol_mktime(GETPOST('endhour'), GETPOST('endmin'), 0, GETPOST('endmonth'), GETPOST('endday'), GETPOST('endyear'));
  140. }
  141. $result = $object->active_line($user, GETPOST('ligne', 'int'), $date_start, $date_end, GETPOST('comment'));
  142. if ($result > 0) {
  143. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  144. exit;
  145. } else {
  146. setEventMessages($object->error, $object->errors, 'errors');
  147. }
  148. } elseif ($action == 'confirm_closeline' && $confirm == 'yes' && $permissiontoactivate) {
  149. $date_end = '';
  150. if (GETPOST('endmonth') && GETPOST('endday') && GETPOST('endyear')) {
  151. $date_end = dol_mktime(GETPOST('endhour'), GETPOST('endmin'), 0, GETPOST('endmonth'), GETPOST('endday'), GETPOST('endyear'));
  152. }
  153. if (!$date_end) {
  154. $error++;
  155. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("DateEnd")), null, 'errors');
  156. }
  157. if (!$error) {
  158. $result = $object->close_line($user, GETPOST('ligne', 'int'), $date_end, urldecode(GETPOST('comment')));
  159. if ($result > 0) {
  160. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  161. exit;
  162. } else {
  163. setEventMessages($object->error, $object->errors, 'errors');
  164. }
  165. }
  166. }
  167. if (GETPOST('mode') == 'predefined') {
  168. $date_start = '';
  169. $date_end = '';
  170. if (GETPOST('date_startmonth') && GETPOST('date_startday') && GETPOST('date_startyear')) {
  171. $date_start = dol_mktime(GETPOST('date_starthour'), GETPOST('date_startmin'), 0, GETPOST('date_startmonth'), GETPOST('date_startday'), GETPOST('date_startyear'));
  172. }
  173. if (GETPOST('date_endmonth') && GETPOST('date_endday') && GETPOST('date_endyear')) {
  174. $date_end = dol_mktime(GETPOST('date_endhour'), GETPOST('date_endmin'), 0, GETPOST('date_endmonth'), GETPOST('date_endday'), GETPOST('date_endyear'));
  175. }
  176. }
  177. // Param dates
  178. $date_start_update = '';
  179. $date_end_update = '';
  180. $date_start_real_update = '';
  181. $date_end_real_update = '';
  182. if (GETPOST('date_start_updatemonth') && GETPOST('date_start_updateday') && GETPOST('date_start_updateyear')) {
  183. $date_start_update = dol_mktime(GETPOST('date_start_updatehour'), GETPOST('date_start_updatemin'), 0, GETPOST('date_start_updatemonth'), GETPOST('date_start_updateday'), GETPOST('date_start_updateyear'));
  184. }
  185. if (GETPOST('date_end_updatemonth') && GETPOST('date_end_updateday') && GETPOST('date_end_updateyear')) {
  186. $date_end_update = dol_mktime(GETPOST('date_end_updatehour'), GETPOST('date_end_updatemin'), 0, GETPOST('date_end_updatemonth'), GETPOST('date_end_updateday'), GETPOST('date_end_updateyear'));
  187. }
  188. if (GETPOST('date_start_real_updatemonth') && GETPOST('date_start_real_updateday') && GETPOST('date_start_real_updateyear')) {
  189. $date_start_real_update = dol_mktime(GETPOST('date_start_real_updatehour'), GETPOST('date_start_real_updatemin'), 0, GETPOST('date_start_real_updatemonth'), GETPOST('date_start_real_updateday'), GETPOST('date_start_real_updateyear'));
  190. }
  191. if (GETPOST('date_end_real_updatemonth') && GETPOST('date_end_real_updateday') && GETPOST('date_end_real_updateyear')) {
  192. $date_end_real_update = dol_mktime(GETPOST('date_end_real_updatehour'), GETPOST('date_end_real_updatemin'), 0, GETPOST('date_end_real_updatemonth'), GETPOST('date_end_real_updateday'), GETPOST('date_end_real_updateyear'));
  193. }
  194. if (GETPOST('remonth') && GETPOST('reday') && GETPOST('reyear')) {
  195. $datecontrat = dol_mktime(GETPOST('rehour'), GETPOST('remin'), 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear'));
  196. }
  197. // Add contract
  198. if ($action == 'add' && $user->hasRight('contrat', 'creer')) {
  199. // Check
  200. if (empty($datecontrat)) {
  201. $error++;
  202. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors');
  203. $action = 'create';
  204. }
  205. if ($socid < 1) {
  206. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ThirdParty")), null, 'errors');
  207. $action = 'create';
  208. $error++;
  209. }
  210. // Fill array 'array_options' with data from add form
  211. $ret = $extrafields->setOptionalsFromPost(null, $object);
  212. if ($ret < 0) {
  213. $error++;
  214. $action = 'create';
  215. }
  216. if (!$error) {
  217. $object->socid = $socid;
  218. $object->date_contrat = $datecontrat;
  219. $object->commercial_suivi_id = GETPOST('commercial_suivi_id', 'int');
  220. $object->commercial_signature_id = GETPOST('commercial_signature_id', 'int');
  221. $object->note_private = GETPOST('note_private', 'alpha');
  222. $object->note_public = GETPOST('note_public', 'alpha');
  223. $object->fk_project = GETPOST('projectid', 'int');
  224. $object->remise_percent = price2num(GETPOST('remise_percent'), '', 2);
  225. $object->ref = GETPOST('ref', 'alpha');
  226. $object->ref_customer = GETPOST('ref_customer', 'alpha');
  227. $object->ref_supplier = GETPOST('ref_supplier', 'alpha');
  228. // If creation from another object of another module (Example: origin=propal, originid=1)
  229. if (!empty($origin) && !empty($originid)) {
  230. // Parse element/subelement (ex: project_task)
  231. $element = $subelement = $origin;
  232. if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
  233. $element = $regs[1];
  234. $subelement = $regs[2];
  235. }
  236. // For compatibility
  237. if ($element == 'order') {
  238. $element = $subelement = 'commande';
  239. }
  240. if ($element == 'propal') {
  241. $element = 'comm/propal';
  242. $subelement = 'propal';
  243. }
  244. if ($element == 'invoice' || $element == 'facture') {
  245. $element = 'compta/facture';
  246. $subelement = 'facture';
  247. }
  248. $object->origin = $origin;
  249. $object->origin_id = $originid;
  250. // Possibility to add external linked objects with hooks
  251. $object->linked_objects[$object->origin] = $object->origin_id;
  252. if (is_array($_POST['other_linked_objects']) && !empty($_POST['other_linked_objects'])) {
  253. $object->linked_objects = array_merge($object->linked_objects, $_POST['other_linked_objects']);
  254. }
  255. $id = $object->create($user);
  256. if ($id > 0) {
  257. dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
  258. $classname = ucfirst($subelement);
  259. $srcobject = new $classname($db);
  260. dol_syslog("Try to find source object origin=".$object->origin." originid=".$object->origin_id." to add lines");
  261. $result = $srcobject->fetch($object->origin_id);
  262. if ($result > 0) {
  263. $srcobject->fetch_thirdparty();
  264. $lines = $srcobject->lines;
  265. if (empty($lines) && method_exists($srcobject, 'fetch_lines')) {
  266. $srcobject->fetch_lines();
  267. $lines = $srcobject->lines;
  268. }
  269. $fk_parent_line = 0;
  270. $num = count($lines);
  271. for ($i = 0; $i < $num; $i++) {
  272. $product_type = ($lines[$i]->product_type ? $lines[$i]->product_type : 0);
  273. if ($product_type == 1 || (getDolGlobalString('CONTRACT_SUPPORT_PRODUCTS') && in_array($product_type, array(0, 1)))) { // TODO Exclude also deee
  274. // service prédéfini
  275. if ($lines[$i]->fk_product > 0) {
  276. $product_static = new Product($db);
  277. // Define output language
  278. if (getDolGlobalInt('MAIN_MULTILANGS') && getDolGlobalString('PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE')) {
  279. $prod = new Product($db);
  280. $prod->id = $lines[$i]->fk_product;
  281. $prod->getMultiLangs();
  282. $outputlangs = $langs;
  283. $newlang = '';
  284. if (empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  285. $newlang = GETPOST('lang_id', 'aZ09');
  286. }
  287. if (empty($newlang)) {
  288. $newlang = $srcobject->thirdparty->default_lang;
  289. }
  290. if (!empty($newlang)) {
  291. $outputlangs = new Translate("", $conf);
  292. $outputlangs->setDefaultLang($newlang);
  293. }
  294. $label = (!empty($prod->multilangs[$outputlangs->defaultlang]["libelle"])) ? $prod->multilangs[$outputlangs->defaultlang]["libelle"] : $lines[$i]->product_label;
  295. } else {
  296. $label = $lines[$i]->product_label;
  297. }
  298. $desc = ($lines[$i]->desc && $lines[$i]->desc != $lines[$i]->label) ? dol_htmlentitiesbr($lines[$i]->desc) : '';
  299. } else {
  300. $desc = dol_htmlentitiesbr($lines[$i]->desc);
  301. }
  302. // Extrafields
  303. $array_options = array();
  304. // For avoid conflicts if trigger used
  305. if (method_exists($lines[$i], 'fetch_optionals')) {
  306. $lines[$i]->fetch_optionals();
  307. $array_options = $lines[$i]->array_options;
  308. }
  309. $txtva = $lines[$i]->vat_src_code ? $lines[$i]->tva_tx.' ('.$lines[$i]->vat_src_code.')' : $lines[$i]->tva_tx;
  310. // View third's localtaxes for now
  311. $localtax1_tx = get_localtax($txtva, 1, $object->thirdparty);
  312. $localtax2_tx = get_localtax($txtva, 2, $object->thirdparty);
  313. $result = $object->addline(
  314. $desc,
  315. $lines[$i]->subprice,
  316. $lines[$i]->qty,
  317. $txtva,
  318. $localtax1_tx,
  319. $localtax2_tx,
  320. $lines[$i]->fk_product,
  321. $lines[$i]->remise_percent,
  322. $lines[$i]->date_start,
  323. $lines[$i]->date_end,
  324. 'HT',
  325. 0,
  326. $lines[$i]->info_bits,
  327. $lines[$i]->fk_fournprice,
  328. $lines[$i]->pa_ht,
  329. $array_options,
  330. $lines[$i]->fk_unit,
  331. $num+1
  332. );
  333. if ($result < 0) {
  334. $error++;
  335. break;
  336. }
  337. }
  338. }
  339. } else {
  340. setEventMessages($srcobject->error, $srcobject->errors, 'errors');
  341. $error++;
  342. }
  343. // Hooks
  344. $parameters = array('objFrom' => $srcobject);
  345. $reshook = $hookmanager->executeHooks('createFrom', $parameters, $object, $action); // Note that $action and $object may have been
  346. // modified by hook
  347. if ($reshook < 0) {
  348. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  349. $error++;
  350. }
  351. } else {
  352. setEventMessages($object->error, $object->errors, 'errors');
  353. $error++;
  354. }
  355. if ($error) {
  356. $action = 'create';
  357. }
  358. } else {
  359. $result = $object->create($user);
  360. if ($result > 0) {
  361. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  362. exit;
  363. } else {
  364. setEventMessages($object->error, $object->errors, 'errors');
  365. }
  366. $action = 'create';
  367. }
  368. }
  369. } elseif ($action == 'classin' && $user->hasRight('contrat', 'creer')) {
  370. $object->setProject(GETPOST('projectid'));
  371. } elseif ($action == 'addline' && $user->hasRight('contrat', 'creer')) {
  372. // Add a new line
  373. // Set if we used free entry or predefined product
  374. $predef = '';
  375. $product_desc = (GETPOSTISSET('dp_desc') ? GETPOST('dp_desc', 'restricthtml') : '');
  376. $price_ht = '';
  377. $price_ht_devise = '';
  378. $price_ttc = '';
  379. $price_ttc_devise = '';
  380. $rang = count($object->lines) + 1;
  381. if (GETPOST('price_ht') !== '') {
  382. $price_ht = price2num(GETPOST('price_ht'), 'MU', 2);
  383. }
  384. if (GETPOST('multicurrency_price_ht') !== '') {
  385. $price_ht_devise = price2num(GETPOST('multicurrency_price_ht'), 'CU', 2);
  386. }
  387. if (GETPOST('price_ttc') !== '') {
  388. $price_ttc = price2num(GETPOST('price_ttc'), 'MU', 2);
  389. }
  390. if (GETPOST('multicurrency_price_ttc') !== '') {
  391. $price_ttc_devise = price2num(GETPOST('multicurrency_price_ttc'), 'CU', 2);
  392. }
  393. if (GETPOST('prod_entry_mode', 'alpha') == 'free') {
  394. $idprod = 0;
  395. } else {
  396. $idprod = GETPOST('idprod', 'int');
  397. if (getDolGlobalString('MAIN_DISABLE_FREE_LINES') && $idprod <= 0) {
  398. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ProductOrService")), null, 'errors');
  399. $error++;
  400. }
  401. }
  402. $tva_tx = GETPOST('tva_tx', 'alpha');
  403. $qty = price2num(GETPOST('qty'.$predef, 'alpha'), 'MS');
  404. $remise_percent = (GETPOSTISSET('remise_percent'.$predef) ? price2num(GETPOST('remise_percent'.$predef), '', 2) : 0);
  405. if (empty($remise_percent)) {
  406. $remise_percent = 0;
  407. }
  408. if ($qty == '') {
  409. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Qty")), null, 'errors');
  410. $error++;
  411. }
  412. if (GETPOST('prod_entry_mode', 'alpha') == 'free' && (empty($idprod) || $idprod < 0) && empty($product_desc)) {
  413. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Description")), null, 'errors');
  414. $error++;
  415. }
  416. $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'));
  417. $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'));
  418. if (!empty($date_start) && !empty($date_end) && $date_start > $date_end) {
  419. setEventMessages($langs->trans("Error").': '.$langs->trans("DateStartPlanned").' > '.$langs->trans("DateEndPlanned"), null, 'errors');
  420. $error++;
  421. }
  422. // Extrafields
  423. $extralabelsline = $extrafields->fetch_name_optionals_label($object->table_element_line);
  424. $array_options = $extrafields->getOptionalsFromPost($object->table_element_line, $predef);
  425. // Unset extrafield
  426. if (is_array($extralabelsline)) {
  427. // Get extra fields
  428. foreach ($extralabelsline as $key => $value) {
  429. unset($_POST["options_".$key]);
  430. }
  431. }
  432. if (!$error) {
  433. // Clean parameters
  434. $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'));
  435. $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'));
  436. // Ecrase $tva_tx par celui du produit. TODO Remove this once vat selection is open
  437. // Get and check minimum price
  438. if ($idprod > 0) {
  439. $prod = new Product($db);
  440. $prod->fetch($idprod);
  441. // Update if prices fields are defined
  442. /*$tva_tx = get_default_tva($mysoc, $object->thirdparty, $prod->id);
  443. $tva_npr = get_default_npr($mysoc, $object->thirdparty, $prod->id);
  444. if (empty($tva_tx)) {
  445. }*/
  446. $tva_npr = 0;
  447. $price_min = $prod->price_min;
  448. $price_min_ttc = $prod->price_min_ttc;
  449. // On defini prix unitaire
  450. if (getDolGlobalString('PRODUIT_MULTIPRICES') && $object->thirdparty->price_level) {
  451. $price_min = $prod->multiprices_min[$object->thirdparty->price_level];
  452. $price_min_ttc = $prod->multiprices_min_ttc[$object->thirdparty->price_level];
  453. } elseif (getDolGlobalString('PRODUIT_CUSTOMER_PRICES')) {
  454. // If price per customer
  455. require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
  456. $prodcustprice = new ProductCustomerPrice($db);
  457. $filter = array('t.fk_product' => $prod->id, 't.fk_soc' => $object->thirdparty->id);
  458. $result = $prodcustprice->fetchAll('', '', 0, 0, $filter);
  459. if ($result) {
  460. if (count($prodcustprice->lines) > 0) {
  461. $price_min = price($prodcustprice->lines[0]->price_min);
  462. $price_min_ttc = price($prodcustprice->lines[0]->price_min_ttc);
  463. /*$tva_tx = $prodcustprice->lines[0]->tva_tx;
  464. if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) {
  465. $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')';
  466. }
  467. $tva_npr = $prodcustprice->lines[0]->recuperableonly;
  468. if (empty($tva_tx)) {
  469. $tva_npr = 0;
  470. }*/
  471. }
  472. }
  473. }
  474. $tmpvat = price2num(preg_replace('/\s*\(.*\)/', '', $tva_tx));
  475. $tmpprodvat = price2num(preg_replace('/\s*\(.*\)/', '', $prod->tva_tx));
  476. // Set unit price to use
  477. if (!empty($price_ht) || $price_ht === '0') {
  478. $pu_ht = price2num($price_ht, 'MU');
  479. $pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU');
  480. $price_base_type = 'HT';
  481. } elseif (!empty($price_ttc) || $price_ttc === '0') {
  482. $pu_ttc = price2num($price_ttc, 'MU');
  483. $pu_ht = price2num($pu_ttc / (1 + ($tmpvat / 100)), 'MU');
  484. $price_base_type = 'TTC';
  485. }
  486. $desc = $prod->description;
  487. //If text set in desc is the same as product descpription (as now it's preloaded) whe add it only one time
  488. if ($product_desc == $desc && getDolGlobalString('PRODUIT_AUTOFILL_DESC')) {
  489. $product_desc = '';
  490. }
  491. if (!empty($product_desc) && getDolGlobalString('MAIN_NO_CONCAT_DESCRIPTION')) {
  492. $desc = $product_desc;
  493. } else {
  494. $desc = dol_concatdesc($desc, $product_desc, '', getDolGlobalString('MAIN_CHANGE_ORDER_CONCAT_DESCRIPTION'));
  495. }
  496. $fk_unit = $prod->fk_unit;
  497. } else {
  498. $pu_ht = price2num($price_ht, 'MU');
  499. $pu_ttc = price2num($price_ttc, 'MU');
  500. $tva_npr = (preg_match('/\*/', $tva_tx) ? 1 : 0);
  501. if (empty($tva_tx)) {
  502. $tva_npr = 0;
  503. }
  504. $tva_tx = str_replace('*', '', $tva_tx);
  505. $desc = $product_desc;
  506. $fk_unit = GETPOST('units', 'alpha');
  507. $pu_ht_devise = price2num($price_ht_devise, 'MU');
  508. $pu_ttc_devise = price2num($price_ttc_devise, 'MU');
  509. $tmpvat = price2num(preg_replace('/\s*\(.*\)/', '', $tva_tx));
  510. // Set unit price to use
  511. if (!empty($price_ht) || $price_ht === '0') {
  512. $pu_ht = price2num($price_ht, 'MU');
  513. $pu_ttc = price2num($pu_ht * (1 + ((float) $tmpvat / 100)), 'MU');
  514. $price_base_type = 'HT';
  515. } elseif (!empty($price_ttc) || $price_ttc === '0') {
  516. $pu_ttc = price2num($price_ttc, 'MU');
  517. $pu_ht = price2num($pu_ttc / (1 + ((float) $tmpvat / 100)), 'MU');
  518. $price_base_type = 'TTC';
  519. }
  520. }
  521. $localtax1_tx = get_localtax($tva_tx, 1, $object->thirdparty, $mysoc, $tva_npr);
  522. $localtax2_tx = get_localtax($tva_tx, 2, $object->thirdparty, $mysoc, $tva_npr);
  523. // ajout prix achat
  524. $fk_fournprice = GETPOST('fournprice');
  525. if (GETPOST('buying_price')) {
  526. $pa_ht = GETPOST('buying_price');
  527. } else {
  528. $pa_ht = null;
  529. }
  530. $info_bits = 0;
  531. if ($tva_npr) {
  532. $info_bits |= 0x01;
  533. }
  534. if (((getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !$user->hasRight('produit', 'ignore_price_min_advance'))
  535. || !getDolGlobalString('MAIN_USE_ADVANCED_PERMS')) && ($price_min && (price2num($pu_ht) * (1 - price2num($remise_percent) / 100) < price2num($price_min)))) {
  536. $object->error = $langs->trans("CantBeLessThanMinPrice", price(price2num($price_min, 'MU'), 0, $langs, 0, 0, -1, $conf->currency));
  537. $result = -1;
  538. } else {
  539. // Insert line
  540. $result = $object->addline(
  541. $desc,
  542. $pu_ht,
  543. $qty,
  544. $tva_tx,
  545. $localtax1_tx,
  546. $localtax2_tx,
  547. $idprod,
  548. $remise_percent,
  549. $date_start,
  550. $date_end,
  551. $price_base_type,
  552. $pu_ttc,
  553. $info_bits,
  554. $fk_fournprice,
  555. $pa_ht,
  556. $array_options,
  557. $fk_unit,
  558. $rang
  559. );
  560. }
  561. if ($result > 0) {
  562. // Define output language
  563. if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE') && getDolGlobalString('CONTRACT_ADDON_PDF')) { // No generation if default type not defined
  564. $outputlangs = $langs;
  565. $newlang = '';
  566. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  567. $newlang = GETPOST('lang_id', 'aZ09');
  568. }
  569. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
  570. $newlang = $object->thirdparty->default_lang;
  571. }
  572. if (!empty($newlang)) {
  573. $outputlangs = new Translate("", $conf);
  574. $outputlangs->setDefaultLang($newlang);
  575. }
  576. $ret = $object->fetch($id); // Reload to get new records
  577. $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  578. }
  579. unset($_POST['prod_entry_mode']);
  580. unset($_POST['qty']);
  581. unset($_POST['type']);
  582. unset($_POST['remise_percent']);
  583. unset($_POST['price_ht']);
  584. unset($_POST['multicurrency_price_ht']);
  585. unset($_POST['price_ttc']);
  586. unset($_POST['tva_tx']);
  587. unset($_POST['product_ref']);
  588. unset($_POST['product_label']);
  589. unset($_POST['product_desc']);
  590. unset($_POST['fournprice']);
  591. unset($_POST['buying_price']);
  592. unset($_POST['np_marginRate']);
  593. unset($_POST['np_markRate']);
  594. unset($_POST['dp_desc']);
  595. unset($_POST['idprod']);
  596. unset($_POST['date_starthour']);
  597. unset($_POST['date_startmin']);
  598. unset($_POST['date_startsec']);
  599. unset($_POST['date_startday']);
  600. unset($_POST['date_startmonth']);
  601. unset($_POST['date_startyear']);
  602. unset($_POST['date_endhour']);
  603. unset($_POST['date_endmin']);
  604. unset($_POST['date_endsec']);
  605. unset($_POST['date_endday']);
  606. unset($_POST['date_endmonth']);
  607. unset($_POST['date_endyear']);
  608. } else {
  609. setEventMessages($object->error, $object->errors, 'errors');
  610. }
  611. }
  612. } elseif ($action == 'updateline' && $user->hasRight('contrat', 'creer') && !GETPOST('cancel', 'alpha')) {
  613. $error = 0;
  614. $predef = '';
  615. if (!empty($date_start_update) && !empty($date_end_update) && $date_start_update > $date_end_update) {
  616. setEventMessages($langs->trans("Error").': '.$langs->trans("DateStartPlanned").' > '.$langs->trans("DateEndPlanned"), null, 'errors');
  617. $action = 'editline';
  618. $_GET['rowid'] = GETPOST('elrowid');
  619. $error++;
  620. }
  621. if (!$error) {
  622. $objectline = new ContratLigne($db);
  623. if ($objectline->fetch(GETPOST('elrowid', 'int')) < 0) {
  624. setEventMessages($objectline->error, $objectline->errors, 'errors');
  625. $error++;
  626. }
  627. $objectline->fetch_optionals();
  628. $objectline->oldcopy = dol_clone($objectline);
  629. }
  630. $db->begin();
  631. if (!$error) {
  632. if ($date_start_real_update == '') {
  633. $date_start_real_update = $objectline->date_start_real;
  634. }
  635. if ($date_end_real_update == '') {
  636. $date_end_real_update = $objectline->date_end_real;
  637. }
  638. $vat_rate = GETPOST('eltva_tx', 'alpha');
  639. // Define info_bits
  640. $info_bits = 0;
  641. if (preg_match('/\*/', $vat_rate)) {
  642. $info_bits |= 0x01;
  643. }
  644. // Define vat_rate
  645. $vat_rate = str_replace('*', '', $vat_rate);
  646. $localtax1_tx = get_localtax($vat_rate, 1, $object->thirdparty, $mysoc);
  647. $localtax2_tx = get_localtax($vat_rate, 2, $object->thirdparty, $mysoc);
  648. $txtva = $vat_rate;
  649. // Clean vat code
  650. $reg = array();
  651. $vat_src_code = '';
  652. if (preg_match('/\((.*)\)/', $txtva, $reg)) {
  653. $vat_src_code = $reg[1];
  654. $txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate.
  655. }
  656. // ajout prix d'achat
  657. if (GETPOST('buying_price')) {
  658. $pa_ht = price2num(GETPOST('buying_price'), '', 2);
  659. } else {
  660. $pa_ht = null;
  661. }
  662. $fk_unit = GETPOST('unit', 'alpha');
  663. // update price_ht with discount
  664. // TODO Use object->updateline instead objedtline->update
  665. $price_ht = price2num(GETPOST('elprice'), 'MU');
  666. $remise_percent = price2num(GETPOST('elremise_percent'), '', 2);
  667. if ($remise_percent > 0) {
  668. $remise = round(($price_ht * $remise_percent / 100), 2);
  669. }
  670. $objectline->fk_product = GETPOST('idprod', 'int');
  671. $objectline->description = GETPOST('product_desc', 'restricthtml');
  672. $objectline->price_ht = $price_ht;
  673. $objectline->subprice = price2num(GETPOST('elprice'), 'MU');
  674. $objectline->qty = price2num(GETPOST('elqty'), 'MS');
  675. $objectline->remise_percent = $remise_percent;
  676. $objectline->tva_tx = ($txtva ? $txtva : 0); // Field may be disabled, so we use vat rate 0
  677. $objectline->vat_src_code = $vat_src_code;
  678. $objectline->localtax1_tx = is_numeric($localtax1_tx) ? $localtax1_tx : 0;
  679. $objectline->localtax2_tx = is_numeric($localtax2_tx) ? $localtax2_tx : 0;
  680. $objectline->date_start = $date_start_update;
  681. $objectline->date_start_real = $date_start_real_update;
  682. $objectline->date_end = $date_end_update;
  683. $objectline->date_end_real = $date_end_real_update;
  684. $objectline->user_closing_id = $user->id;
  685. //$objectline->fk_fournprice = $fk_fournprice;
  686. $objectline->pa_ht = $pa_ht;
  687. $objectline->rang = $objectline->rang;
  688. if ($fk_unit > 0) {
  689. $objectline->fk_unit = GETPOST('unit');
  690. } else {
  691. $objectline->fk_unit = null;
  692. }
  693. // Extrafields
  694. $extralabelsline = $extrafields->fetch_name_optionals_label($objectline->table_element);
  695. $array_options = $extrafields->getOptionalsFromPost($object->table_element_line, $predef);
  696. if (is_array($array_options) && count($array_options) > 0) {
  697. // We replace values in this->line->array_options only for entries defined into $array_options
  698. foreach ($array_options as $key => $value) {
  699. $objectline->array_options[$key] = $array_options[$key];
  700. }
  701. }
  702. // TODO verifier price_min si fk_product et multiprix
  703. $result = $objectline->update($user);
  704. if ($result < 0) {
  705. $error++;
  706. setEventMessages($objectline->error, $objectline->errors, 'errors');
  707. }
  708. }
  709. if (!$error) {
  710. $db->commit();
  711. } else {
  712. $db->rollback();
  713. }
  714. } elseif ($action == 'confirm_deleteline' && $confirm == 'yes' && $user->hasRight('contrat', 'creer')) {
  715. $result = $object->deleteline(GETPOST('lineid', 'int'), $user);
  716. if ($result >= 0) {
  717. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  718. exit;
  719. } else {
  720. setEventMessages($object->error, $object->errors, 'errors');
  721. }
  722. } elseif ($action == 'confirm_valid' && $confirm == 'yes' && $user->hasRight('contrat', 'creer')) {
  723. $result = $object->validate($user);
  724. if ($result > 0) {
  725. // Define output language
  726. if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
  727. $outputlangs = $langs;
  728. $newlang = '';
  729. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  730. $newlang = GETPOST('lang_id', 'aZ09');
  731. }
  732. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
  733. $newlang = $object->thirdparty->default_lang;
  734. }
  735. if (!empty($newlang)) {
  736. $outputlangs = new Translate("", $conf);
  737. $outputlangs->setDefaultLang($newlang);
  738. }
  739. $model = $object->model_pdf;
  740. $ret = $object->fetch($id); // Reload to get new records
  741. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  742. }
  743. } else {
  744. setEventMessages($object->error, $object->errors, 'errors');
  745. }
  746. } elseif ($action == 'reopen' && $user->hasRight('contrat', 'creer')) {
  747. $result = $object->reopen($user);
  748. if ($result < 0) {
  749. setEventMessages($object->error, $object->errors, 'errors');
  750. }
  751. } elseif ($action == 'confirm_close' && $confirm == 'yes' && $user->hasRight('contrat', 'creer')) {
  752. // Close all lines
  753. $result = $object->closeAll($user);
  754. if ($result < 0) {
  755. setEventMessages($object->error, $object->errors, 'errors');
  756. }
  757. } elseif ($action == 'confirm_activate' && $confirm == 'yes' && $user->hasRight('contrat', 'creer')) {
  758. $date_start = dol_mktime(12, 0, 0, GETPOST('d_startmonth'), GETPOST('d_startday'), GETPOST('d_startyear'));
  759. $date_end = dol_mktime(12, 0, 0, GETPOST('d_endmonth'), GETPOST('d_endday'), GETPOST('d_endyear'));
  760. $comment = GETPOST('comment', 'alpha');
  761. $result = $object->activateAll($user, $date_start, 0, $comment, $date_end);
  762. if ($result < 0) {
  763. setEventMessages($object->error, $object->errors, 'errors');
  764. }
  765. } elseif ($action == 'confirm_delete' && $confirm == 'yes' && $user->hasRight('contrat', 'supprimer')) {
  766. $result = $object->delete($user);
  767. if ($result >= 0) {
  768. header("Location: list.php?restore_lastsearch_values=1");
  769. return;
  770. } else {
  771. setEventMessages($object->error, $object->errors, 'errors');
  772. }
  773. } elseif ($action == 'confirm_move' && $confirm == 'yes' && $user->hasRight('contrat', 'creer')) {
  774. if (GETPOST('newcid') > 0) {
  775. $contractline = new ContratLigne($db);
  776. $result = $contractline->fetch(GETPOSTINT('lineid'));
  777. $contractline->fk_contrat = GETPOSTINT('newcid');
  778. $result = $contractline->update($user, 1);
  779. if ($result >= 0) {
  780. header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
  781. return;
  782. } else {
  783. setEventMessages($object->error, $object->errors, 'errors');
  784. }
  785. } else {
  786. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("RefNewContract")), null, 'errors');
  787. }
  788. } elseif ($action == 'update_extras' && $permissiontoadd) {
  789. $object->oldcopy = dol_clone($object, 2);
  790. $attribute = GETPOST('attribute', 'alphanohtml');
  791. // Fill array 'array_options' with data from update form
  792. $ret = $extrafields->setOptionalsFromPost(null, $object, $attribute);
  793. if ($ret < 0) {
  794. setEventMessages($extrafields->error, $object->errors, 'errors');
  795. $error++;
  796. }
  797. if (!$error) {
  798. $result = $object->updateExtraField($attribute, 'CONTRACT_MODIFY');
  799. if ($result < 0) {
  800. setEventMessages($object->error, $object->errors, 'errors');
  801. $error++;
  802. }
  803. }
  804. if ($error) {
  805. $action = 'edit_extras';
  806. }
  807. } elseif ($action == 'setref_supplier' && $permissiontoadd) {
  808. if (!$cancel) {
  809. $object->oldcopy = dol_clone($object, 2);
  810. $result = $object->setValueFrom('ref_supplier', GETPOST('ref_supplier', 'alpha'), '', null, 'text', '', $user, 'CONTRACT_MODIFY');
  811. if ($result < 0) {
  812. setEventMessages($object->error, $object->errors, 'errors');
  813. $action = 'editref_supplier';
  814. } else {
  815. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  816. exit;
  817. }
  818. } else {
  819. header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
  820. exit;
  821. }
  822. } elseif ($action == 'setref_customer' && $permissiontoadd) {
  823. if (!$cancel) {
  824. $object->oldcopy = dol_clone($object, 2);
  825. $result = $object->setValueFrom('ref_customer', GETPOST('ref_customer', 'alpha'), '', null, 'text', '', $user, 'CONTRACT_MODIFY');
  826. if ($result < 0) {
  827. setEventMessages($object->error, $object->errors, 'errors');
  828. $action = 'editref_customer';
  829. } else {
  830. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  831. exit;
  832. }
  833. } else {
  834. header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
  835. exit;
  836. }
  837. } elseif ($action == 'setref' && $permissiontoadd) {
  838. if (!$cancel) {
  839. $result = $object->fetch($id);
  840. if ($result < 0) {
  841. setEventMessages($object->error, $object->errors, 'errors');
  842. }
  843. $old_ref = $object->ref;
  844. $result = $object->setValueFrom('ref', GETPOST('ref', 'alpha'), '', null, 'text', '', $user, 'CONTRACT_MODIFY');
  845. if ($result < 0) {
  846. setEventMessages($object->error, $object->errors, 'errors');
  847. $action = 'editref';
  848. } else {
  849. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  850. $old_filedir = $conf->contrat->multidir_output[$object->entity].'/'.dol_sanitizeFileName($old_ref);
  851. $new_filedir = $conf->contrat->multidir_output[$object->entity].'/'.dol_sanitizeFileName($object->ref);
  852. // Rename directory of contract with new name
  853. dol_move_dir($old_filedir, $new_filedir);
  854. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  855. exit;
  856. }
  857. } else {
  858. header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
  859. exit;
  860. }
  861. } elseif ($action == 'setdate_contrat' && $permissiontoadd) {
  862. if (!$cancel) {
  863. $result = $object->fetch($id);
  864. if ($result < 0) {
  865. setEventMessages($object->error, $object->errors, 'errors');
  866. }
  867. $datacontrat = dol_mktime(GETPOST('date_contrathour'), GETPOST('date_contratmin'), 0, GETPOST('date_contratmonth'), GETPOST('date_contratday'), GETPOST('date_contratyear'));
  868. $result = $object->setValueFrom('date_contrat', $datacontrat, '', null, 'date', '', $user, 'CONTRACT_MODIFY');
  869. if ($result < 0) {
  870. setEventMessages($object->error, $object->errors, 'errors');
  871. $action = 'editdate_contrat';
  872. } else {
  873. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  874. exit;
  875. }
  876. } else {
  877. header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
  878. exit;
  879. }
  880. }
  881. // Actions when printing a doc from card
  882. include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
  883. // Actions to build doc
  884. $upload_dir = $conf->contrat->multidir_output[!empty($object->entity) ? $object->entity : $conf->entity];
  885. include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
  886. // Actions to send emails
  887. $triggersendname = 'CONTRACT_SENTBYMAIL';
  888. $paramname = 'id';
  889. $mode = 'emailfromcontract';
  890. $trackid = 'con'.$object->id;
  891. include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
  892. if (getDolGlobalString('MAIN_DISABLE_CONTACTS_TAB') && $user->hasRight('contrat', 'creer')) {
  893. if ($action == 'addcontact') {
  894. $contactid = (GETPOST('userid') ? GETPOST('userid') : GETPOST('contactid'));
  895. $typeid = (GETPOST('typecontact') ? GETPOST('typecontact') : GETPOST('type'));
  896. $result = $object->add_contact($contactid, $typeid, GETPOST("source", 'aZ09'));
  897. if ($result >= 0) {
  898. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  899. exit;
  900. } else {
  901. if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  902. $langs->load("errors");
  903. setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
  904. } else {
  905. setEventMessages($object->error, $object->errors, 'errors');
  906. }
  907. }
  908. } elseif ($action == 'swapstatut') {
  909. // bascule du statut d'un contact
  910. $result = $object->swapContactStatus(GETPOST('ligne', 'int'));
  911. } elseif ($action == 'deletecontact') {
  912. // Efface un contact
  913. $result = $object->delete_contact(GETPOST('lineid', 'int'));
  914. if ($result >= 0) {
  915. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  916. exit;
  917. } else {
  918. setEventMessages($object->error, $object->errors, 'errors');
  919. }
  920. }
  921. }
  922. // Action clone object
  923. if ($action == 'confirm_clone' && $confirm == 'yes') {
  924. if (!GETPOST('socid', 3)) {
  925. setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
  926. } else {
  927. if ($object->id > 0) {
  928. $result = $object->createFromClone($user, $socid);
  929. if ($result > 0) {
  930. header("Location: ".$_SERVER['PHP_SELF'].'?id='.$result);
  931. exit();
  932. } else {
  933. if (count($object->errors) > 0) {
  934. setEventMessages($object->error, $object->errors, 'errors');
  935. }
  936. $action = '';
  937. }
  938. }
  939. }
  940. }
  941. }
  942. /*
  943. * View
  944. */
  945. $help_url = 'EN:Module_Contracts|FR:Module_Contrat';
  946. $title = $object->ref." - ".$langs->trans('Contract');
  947. if ($action == 'create') {
  948. $title = $langs->trans("NewContract");
  949. }
  950. llxHeader('', $title, $help_url);
  951. $form = new Form($db);
  952. $formfile = new FormFile($db);
  953. if (isModEnabled('project')) {
  954. $formproject = new FormProjets($db);
  955. }
  956. // Load object modContract
  957. $module = (getDolGlobalString('CONTRACT_ADDON') ? $conf->global->CONTRACT_ADDON : 'mod_contract_serpis');
  958. if (substr($module, 0, 13) == 'mod_contract_' && substr($module, -3) == 'php') {
  959. $module = substr($module, 0, dol_strlen($module) - 4);
  960. }
  961. $result = dol_include_once('/core/modules/contract/'.$module.'.php');
  962. if ($result > 0) {
  963. $modCodeContract = new $module();
  964. }
  965. // Create
  966. if ($action == 'create') {
  967. $objectsrc = null;
  968. print load_fiche_titre($langs->trans('NewContract'), '', 'contract');
  969. $soc = new Societe($db);
  970. if ($socid > 0) {
  971. $soc->fetch($socid);
  972. }
  973. if (GETPOST('origin') && GETPOST('originid', 'int')) {
  974. // Parse element/subelement (ex: project_task)
  975. $regs = array();
  976. $element = $subelement = GETPOST('origin');
  977. if (preg_match('/^([^_]+)_([^_]+)/i', GETPOST('origin'), $regs)) {
  978. $element = $regs[1];
  979. $subelement = $regs[2];
  980. }
  981. if ($element == 'project') {
  982. $projectid = GETPOST('originid', 'int');
  983. } else {
  984. // For compatibility
  985. if ($element == 'order' || $element == 'commande') {
  986. $element = $subelement = 'commande';
  987. }
  988. if ($element == 'propal') {
  989. $element = 'comm/propal';
  990. $subelement = 'propal';
  991. }
  992. if ($element == 'invoice' || $element == 'facture') {
  993. $element = 'compta/facture';
  994. $subelement = 'facture';
  995. }
  996. dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
  997. $classname = ucfirst($subelement);
  998. $objectsrc = new $classname($db);
  999. $objectsrc->fetch($originid);
  1000. if (empty($objectsrc->lines) && method_exists($objectsrc, 'fetch_lines')) {
  1001. $objectsrc->fetch_lines();
  1002. }
  1003. $objectsrc->fetch_thirdparty();
  1004. // Replicate extrafields
  1005. $objectsrc->fetch_optionals();
  1006. $object->array_options = $objectsrc->array_options;
  1007. $projectid = (!empty($objectsrc->fk_project) ? $objectsrc->fk_project : '');
  1008. $soc = $objectsrc->thirdparty;
  1009. $note_private = (!empty($objectsrc->note_private) ? $objectsrc->note_private : '');
  1010. $note_public = (!empty($objectsrc->note_public) ? $objectsrc->note_public : '');
  1011. // Object source contacts list
  1012. $srccontactslist = $objectsrc->liste_contact(-1, 'external', 1);
  1013. }
  1014. } else {
  1015. $projectid = GETPOST('projectid', 'int');
  1016. $note_private = GETPOST("note_private");
  1017. $note_public = GETPOST("note_public");
  1018. }
  1019. $object->date_contrat = dol_now();
  1020. print '<form name="form_contract" action="'.$_SERVER["PHP_SELF"].'" method="post">';
  1021. print '<input type="hidden" name="token" value="'.newToken().'">';
  1022. print '<input type="hidden" name="action" value="add">';
  1023. print '<input type="hidden" name="socid" value="'.$soc->id.'">'."\n";
  1024. print '<input type="hidden" name="remise_percent" value="0">';
  1025. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  1026. print dol_get_fiche_head();
  1027. print '<table class="border centpercent">';
  1028. // Ref
  1029. print '<tr><td class="titlefieldcreate fieldrequired">'.$langs->trans('Ref').'</td><td>';
  1030. if (!empty($modCodeContract->code_auto)) {
  1031. $tmpcode = $langs->trans("Draft");
  1032. } else {
  1033. $tmpcode = '<input name="ref" class="maxwidth100" maxlength="128" value="'.dol_escape_htmltag(GETPOST('ref') ? GETPOST('ref') : $tmpcode).'">';
  1034. }
  1035. print $tmpcode;
  1036. print '</td></tr>';
  1037. // Ref customer
  1038. print '<tr><td>'.$langs->trans('RefCustomer').'</td>';
  1039. print '<td><input type="text" class="maxwidth150" name="ref_customer" id="ref_customer" value="'.dol_escape_htmltag(GETPOST('ref_customer', 'alpha')).'"></td></tr>';
  1040. // Ref supplier
  1041. print '<tr><td>'.$langs->trans('RefSupplier').'</td>';
  1042. print '<td><input type="text" class="maxwidth150" name="ref_supplier" id="ref_supplier" value="'.dol_escape_htmltag(GETPOST('ref_supplier', 'alpha')).'"></td></tr>';
  1043. // Thirdparty
  1044. print '<tr>';
  1045. print '<td class="fieldrequired">'.$langs->trans('ThirdParty').'</td>';
  1046. if ($socid > 0) {
  1047. print '<td>';
  1048. print $soc->getNomUrl(1);
  1049. print '<input type="hidden" name="socid" value="'.$soc->id.'">';
  1050. print '</td>';
  1051. } else {
  1052. print '<td>';
  1053. print img_picto('', 'company', 'class="pictofixedwidth"');
  1054. print $form->select_company('', 'socid', '', 'SelectThirdParty', 1, 0, null, 0, 'minwidth300 widthcentpercentminusxx maxwidth500');
  1055. print ' <a href="'.DOL_URL_ROOT.'/societe/card.php?action=create&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=create').'"><span class="fa fa-plus-circle valignmiddle paddingleft" title="'.$langs->trans("AddThirdParty").'"></span></a>';
  1056. print '</td>';
  1057. }
  1058. print '</tr>'."\n";
  1059. if ($socid > 0) {
  1060. // Ligne info remises tiers
  1061. print '<tr><td>'.$langs->trans('Discounts').'</td><td>';
  1062. if ($soc->remise_percent) {
  1063. print $langs->trans("CompanyHasRelativeDiscount", $soc->remise_percent).' ';
  1064. } else {
  1065. print '<span class="hideonsmartphone">'.$langs->trans("CompanyHasNoRelativeDiscount").'. </span>';
  1066. }
  1067. $absolute_discount = $soc->getAvailableDiscounts();
  1068. if ($absolute_discount) {
  1069. print $langs->trans("CompanyHasAbsoluteDiscount", price($absolute_discount), $langs->trans("Currency".$conf->currency)).'.';
  1070. } else {
  1071. print '<span class="hideonsmartphone">'.$langs->trans("CompanyHasNoAbsoluteDiscount").'.</span>';
  1072. }
  1073. print '</td></tr>';
  1074. }
  1075. // Commercial suivi
  1076. print '<tr><td class="nowrap"><span class="fieldrequired">'.$langs->trans("TypeContact_contrat_internal_SALESREPFOLL").'</span></td><td>';
  1077. print img_picto('', 'user', 'class="pictofixedwidth"');
  1078. print $form->select_dolusers(GETPOST("commercial_suivi_id") ? GETPOST("commercial_suivi_id") : $user->id, 'commercial_suivi_id', 1, '');
  1079. print '</td></tr>';
  1080. // Commercial signature
  1081. print '<tr><td class="nowrap"><span class="fieldrequired">'.$langs->trans("TypeContact_contrat_internal_SALESREPSIGN").'</span></td><td>';
  1082. print img_picto('', 'user', 'class="pictofixedwidth"');
  1083. print $form->select_dolusers(GETPOST("commercial_signature_id") ? GETPOST("commercial_signature_id") : $user->id, 'commercial_signature_id', 1, '');
  1084. print '</td></tr>';
  1085. print '<tr><td><span class="fieldrequired">'.$langs->trans("Date").'</span></td><td>';
  1086. print img_picto('', 'action', 'class="pictofixedwidth"');
  1087. print $form->selectDate($datecontrat, '', 0, 0, '', "contrat");
  1088. print "</td></tr>";
  1089. // Project
  1090. if (isModEnabled('project')) {
  1091. $langs->load('projects');
  1092. $formproject = new FormProjets($db);
  1093. print '<tr><td>'.$langs->trans("Project").'</td><td>';
  1094. print img_picto('', 'project', 'class="pictofixedwidth"');
  1095. $formproject->select_projects(($soc->id > 0 ? $soc->id : -1), $projectid, "projectid", 0, 0, 1, 1);
  1096. print ' &nbsp; <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" title="'.$langs->trans("AddProject").'"></span></a>';
  1097. print "</td></tr>";
  1098. }
  1099. print '<tr><td>'.$langs->trans("NotePublic").'</td><td class="tdtop">';
  1100. $doleditor = new DolEditor('note_public', $note_public, '', '100', 'dolibarr_notes', 'In', 1, true, !getDolGlobalString('FCKEDITOR_ENABLE_NOTE_PUBLIC') ? 0 : 1, ROWS_3, '90%');
  1101. print $doleditor->Create(1);
  1102. print '</td></tr>';
  1103. if (empty($user->socid)) {
  1104. print '<tr><td>'.$langs->trans("NotePrivate").'</td><td class="tdtop">';
  1105. $doleditor = new DolEditor('note_private', $note_private, '', '100', 'dolibarr_notes', 'In', 1, true, !getDolGlobalString('FCKEDITOR_ENABLE_NOTE_PRIVATE') ? 0 : 1, ROWS_3, '90%');
  1106. print $doleditor->Create(1);
  1107. print '</td></tr>';
  1108. }
  1109. // Other attributes
  1110. $parameters = array('objectsrc' => $objectsrc, 'colspan' => ' colspan="3"', 'cols' => '3');
  1111. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  1112. print $hookmanager->resPrint;
  1113. // Other attributes
  1114. if (empty($reshook)) {
  1115. print $object->showOptionals($extrafields, 'create', $parameters);
  1116. }
  1117. print "</table>\n";
  1118. print dol_get_fiche_end();
  1119. print $form->buttonsSaveCancel("Create");
  1120. if (is_object($objectsrc)) {
  1121. print '<input type="hidden" name="origin" value="'.$objectsrc->element.'">';
  1122. print '<input type="hidden" name="originid" value="'.$objectsrc->id.'">';
  1123. if (!getDolGlobalString('CONTRACT_SUPPORT_PRODUCTS')) {
  1124. print '<br>'.$langs->trans("Note").': '.$langs->trans("OnlyLinesWithTypeServiceAreUsed");
  1125. }
  1126. }
  1127. print "</form>\n";
  1128. } else {
  1129. // View and edit mode
  1130. $now = dol_now();
  1131. if ($object->id > 0) {
  1132. $object->fetch_thirdparty();
  1133. $soc = $object->thirdparty; // $soc is used later
  1134. $result = $object->fetch_lines(); // This also init $this->nbofserviceswait, $this->nbofservicesopened, $this->nbofservicesexpired=, $this->nbofservicesclosed
  1135. if ($result < 0) {
  1136. dol_print_error($db, $object->error);
  1137. }
  1138. $nbofservices = count($object->lines);
  1139. $author = new User($db);
  1140. $author->fetch($object->user_author_id);
  1141. $commercial_signature = new User($db);
  1142. $commercial_signature->fetch($object->commercial_signature_id);
  1143. $commercial_suivi = new User($db);
  1144. $commercial_suivi->fetch($object->commercial_suivi_id);
  1145. $head = contract_prepare_head($object);
  1146. $hselected = 0;
  1147. $formconfirm = '';
  1148. print dol_get_fiche_head($head, $hselected, $langs->trans("Contract"), -1, 'contract');
  1149. if ($action == 'delete') {
  1150. //Confirmation de la suppression du contrat
  1151. $formconfirm = $form->formconfirm($_SERVER['PHP_SELF']."?id=".$object->id, $langs->trans("DeleteAContract"), $langs->trans("ConfirmDeleteAContract"), "confirm_delete", '', 0, 1);
  1152. } elseif ($action == 'valid') {
  1153. //Confirmation de la validation
  1154. $ref = substr($object->ref, 1, 4);
  1155. if ($ref == 'PROV' && !empty($modCodeContract->code_auto)) {
  1156. $numref = $object->getNextNumRef($object->thirdparty);
  1157. } else {
  1158. $numref = $object->ref;
  1159. }
  1160. $text = $langs->trans('ConfirmValidateContract', $numref);
  1161. $formconfirm = $form->formconfirm($_SERVER['PHP_SELF']."?id=".$object->id, $langs->trans("ValidateAContract"), $text, "confirm_valid", '', 0, 1);
  1162. } elseif ($action == 'close') {
  1163. // Confirmation de la fermeture
  1164. $formconfirm = $form->formconfirm($_SERVER['PHP_SELF']."?id=".$object->id, $langs->trans("CloseAContract"), $langs->trans("ConfirmCloseContract"), "confirm_close", '', 0, 1);
  1165. } elseif ($action == 'activate') {
  1166. $formquestion = array(
  1167. array('type' => 'date', 'name' => 'd_start', 'label' => $langs->trans("DateServiceActivate"), 'value' => dol_now()),
  1168. array('type' => 'date', 'name' => 'd_end', 'label' => $langs->trans("DateEndPlanned"), /*'value' => $form->selectDate('', "end", $usehm, $usehm, '', "active", 1, 0),*/ '', ''),
  1169. array('type' => 'text', 'name' => 'comment', 'label' => $langs->trans("Comment"), 'value' => '', '', '', 'class' => 'minwidth300', 'moreattr'=>'autofocus')
  1170. );
  1171. $formconfirm = $form->formconfirm($_SERVER['PHP_SELF']."?id=".$object->id, $langs->trans("ActivateAllOnContract"), $langs->trans("ConfirmActivateAllOnContract"), "confirm_activate", $formquestion, 'yes', 1, 280);
  1172. } elseif ($action == 'clone') {
  1173. $filter = '(s.client:IN:1,2,3)';
  1174. // Clone confirmation
  1175. $formquestion = array(array('type' => 'other', 'name' => 'socid', 'label' => $langs->trans("SelectThirdParty"), 'value' => $form->select_company(GETPOST('socid', 'int'), 'socid', $filter)));
  1176. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneContract', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
  1177. }
  1178. // Call Hook formConfirm
  1179. $parameters = array(
  1180. 'formConfirm' => $formconfirm,
  1181. 'id' => $id,
  1182. //'lineid' => $lineid,
  1183. );
  1184. // Note that $action and $object may have been modified by hook
  1185. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action);
  1186. if (empty($reshook)) {
  1187. $formconfirm .= $hookmanager->resPrint;
  1188. } elseif ($reshook > 0) {
  1189. $formconfirm = $hookmanager->resPrint;
  1190. }
  1191. // Print form confirm
  1192. print $formconfirm;
  1193. // Contract
  1194. if ($object->status == $object::STATUS_DRAFT && $user->hasRight('contrat', 'creer')) {
  1195. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" method="POST">';
  1196. print '<input type="hidden" name="token" value="'.newToken().'">';
  1197. print '<input type="hidden" name="action" value="setremise">';
  1198. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  1199. }
  1200. // Contract card
  1201. $linkback = '<a href="'.DOL_URL_ROOT.'/contrat/list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  1202. $morehtmlref = '';
  1203. if (!empty($modCodeContract->code_auto)) {
  1204. $morehtmlref .= $object->ref;
  1205. } else {
  1206. $morehtmlref .= $form->editfieldkey("", 'ref', $object->ref, $object, $user->hasRight('contrat', 'creer'), 'string', '', 0, 3);
  1207. $morehtmlref .= $form->editfieldval("", 'ref', $object->ref, $object, $user->hasRight('contrat', 'creer'), 'string', '', 0, 2);
  1208. }
  1209. $morehtmlref .= '<div class="refidno">';
  1210. // Ref customer
  1211. $morehtmlref .= $form->editfieldkey("RefCustomer", 'ref_customer', $object->ref_customer, $object, $user->hasRight('contrat', 'creer'), 'string', '', 0, 1);
  1212. $morehtmlref .= $form->editfieldval("RefCustomer", 'ref_customer', $object->ref_customer, $object, $user->hasRight('contrat', 'creer'), 'string'.(isset($conf->global->THIRDPARTY_REF_INPUT_SIZE) ? ':' . getDolGlobalString('THIRDPARTY_REF_INPUT_SIZE') : ''), '', null, null, '', 1, 'getFormatedCustomerRef');
  1213. // Ref supplier
  1214. $morehtmlref .= '<br>';
  1215. $morehtmlref .= $form->editfieldkey("RefSupplier", 'ref_supplier', $object->ref_supplier, $object, $user->hasRight('contrat', 'creer'), 'string', '', 0, 1);
  1216. $morehtmlref .= $form->editfieldval("RefSupplier", 'ref_supplier', $object->ref_supplier, $object, $user->hasRight('contrat', 'creer'), 'string', '', null, null, '', 1, 'getFormatedSupplierRef');
  1217. // Thirdparty
  1218. $morehtmlref .= '<br>'.$object->thirdparty->getNomUrl(1);
  1219. if (!getDolGlobalString('MAIN_DISABLE_OTHER_LINK') && $object->thirdparty->id > 0) {
  1220. $morehtmlref .= ' (<a href="'.DOL_URL_ROOT.'/contrat/list.php?socid='.$object->thirdparty->id.'&search_name='.urlencode($object->thirdparty->name).'">'.$langs->trans("OtherContracts").'</a>)';
  1221. }
  1222. // Project
  1223. if (isModEnabled('project')) {
  1224. $langs->load("projects");
  1225. $morehtmlref .= '<br>';
  1226. if ($permissiontoadd) {
  1227. $morehtmlref .= img_picto($langs->trans("Project"), 'project', 'class="pictofixedwidth"');
  1228. if ($action != 'classify') {
  1229. $morehtmlref .= '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?action=classify&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetProject')).'</a> ';
  1230. }
  1231. $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, $object->socid, $object->fk_project, ($action == 'classify' ? 'projectid' : 'none'), 0, 0, 0, 1, '', 'maxwidth300');
  1232. } else {
  1233. if (!empty($object->fk_project)) {
  1234. $proj = new Project($db);
  1235. $proj->fetch($object->fk_project);
  1236. $morehtmlref .= $proj->getNomUrl(1);
  1237. if ($proj->title) {
  1238. $morehtmlref .= '<span class="opacitymedium"> - '.dol_escape_htmltag($proj->title).'</span>';
  1239. }
  1240. }
  1241. }
  1242. }
  1243. $morehtmlref .= '</div>';
  1244. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'none', $morehtmlref);
  1245. print '<div class="fichecenter">';
  1246. print '<div class="underbanner clearboth"></div>';
  1247. print '<table class="border tableforfield" width="100%">';
  1248. // Line info of thirdparty discounts
  1249. print '<tr><td class="titlefield">'.$langs->trans('Discount').'</td><td colspan="3">';
  1250. if ($object->thirdparty->remise_percent) {
  1251. print $langs->trans("CompanyHasRelativeDiscount", $object->thirdparty->remise_percent).'. ';
  1252. } else {
  1253. print '<span class="hideonsmartphone">'.$langs->trans("CompanyHasNoRelativeDiscount").'. </span>';
  1254. }
  1255. $absolute_discount = $object->thirdparty->getAvailableDiscounts();
  1256. if ($absolute_discount) {
  1257. print $langs->trans("CompanyHasAbsoluteDiscount", price($absolute_discount), $langs->trans("Currency".$conf->currency)).'.';
  1258. } else {
  1259. print '<span class="hideonsmartphone">'.$langs->trans("CompanyHasNoAbsoluteDiscount").'.</span>';
  1260. }
  1261. print '</td></tr>';
  1262. // Date
  1263. print '<tr>';
  1264. print '<td class="titlefield">';
  1265. print $form->editfieldkey("Date", 'date_contrat', $object->date_contrat, $object, $user->hasRight('contrat', 'creer'));
  1266. print '</td><td>';
  1267. print $form->editfieldval("Date", 'date_contrat', $object->date_contrat, $object, $user->hasRight('contrat', 'creer'), 'datehourpicker');
  1268. print '</td>';
  1269. print '</tr>';
  1270. // Other attributes
  1271. $cols = 3;
  1272. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  1273. print "</table>";
  1274. print '</div>';
  1275. if ($object->status == $object::STATUS_DRAFT && $user->hasRight('contrat', 'creer')) {
  1276. print '</form>';
  1277. }
  1278. echo '<br>';
  1279. if (getDolGlobalString('MAIN_DISABLE_CONTACTS_TAB')) {
  1280. $blocname = 'contacts';
  1281. $title = $langs->trans('ContactsAddresses');
  1282. include DOL_DOCUMENT_ROOT.'/core/tpl/bloc_showhide.tpl.php';
  1283. }
  1284. if (getDolGlobalString('MAIN_DISABLE_NOTES_TAB')) {
  1285. $blocname = 'notes';
  1286. $title = $langs->trans('Notes');
  1287. include DOL_DOCUMENT_ROOT.'/core/tpl/bloc_showhide.tpl.php';
  1288. }
  1289. $arrayothercontracts = $object->getListOfContracts('others'); // array or -1 if technical error
  1290. /*
  1291. * Lines of contracts
  1292. */
  1293. // Add products/services form
  1294. //$forceall = 1;
  1295. global $inputalsopricewithtax;
  1296. $inputalsopricewithtax = 1;
  1297. $productstatic = new Product($db);
  1298. $usemargins = 0;
  1299. if (isModEnabled('margin') && !empty($object->element) && in_array($object->element, array('facture', 'propal', 'commande'))) {
  1300. $usemargins = 1;
  1301. }
  1302. // Title line for service
  1303. $cursorline = 1;
  1304. print '<div id="contrat-lines-container" id="contractlines" data-contractid="'.$object->id.'" data-element="'.$object->element.'" >';
  1305. while ($cursorline <= $nbofservices) {
  1306. print '<div id="contrat-line-container'.$object->lines[$cursorline - 1]->id.'" data-contratlineid = "'.$object->lines[$cursorline - 1]->id.'" data-element="'.$object->lines[$cursorline - 1]->element.'" >';
  1307. print '<form name="update" id="addproduct" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'" method="post">';
  1308. print '<input type="hidden" name="token" value="'.newToken().'">';
  1309. print '<input type="hidden" name="action" value="updateline">';
  1310. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  1311. print '<input type="hidden" name="elrowid" value="'.$object->lines[$cursorline - 1]->id.'">';
  1312. print '<input type="hidden" name="fournprice" value="'.(!empty($object->lines[$cursorline - 1]->fk_fournprice) ? $object->lines[$cursorline - 1]->fk_fournprice : 0).'">';
  1313. // Area with common detail of line
  1314. print '<div class="div-table-responsive-no-min">';
  1315. print '<table class="notopnoleftnoright allwidth tableforservicepart1 centpercent">';
  1316. $sql = "SELECT cd.rowid, cd.statut, cd.label as label_det, cd.fk_product, cd.product_type, cd.description, cd.price_ht, cd.qty,";
  1317. $sql .= " cd.tva_tx, cd.vat_src_code, cd.remise_percent, cd.info_bits, cd.subprice, cd.multicurrency_subprice,";
  1318. $sql .= " cd.date_ouverture_prevue as date_start, cd.date_ouverture as date_start_real,";
  1319. $sql .= " cd.date_fin_validite as date_end, cd.date_cloture as date_end_real,";
  1320. $sql .= " cd.commentaire as comment, cd.fk_product_fournisseur_price as fk_fournprice, cd.buy_price_ht as pa_ht,";
  1321. $sql .= " cd.fk_unit,";
  1322. $sql .= " p.rowid as pid, p.ref as pref, p.label as plabel, p.fk_product_type as ptype, p.entity as pentity, p.tosell, p.tobuy, p.tobatch";
  1323. $sql .= " ,cd.rang";
  1324. $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as cd";
  1325. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON cd.fk_product = p.rowid";
  1326. $sql .= " WHERE cd.rowid = ".((int) $object->lines[$cursorline - 1]->id);
  1327. $result = $db->query($sql);
  1328. if ($result) {
  1329. $total = 0;
  1330. $objp = $db->fetch_object($result);
  1331. // Line title
  1332. print '<tr class="liste_titre'.($cursorline ? ' liste_titre_add' : '').'">';
  1333. print '<td>'.$langs->trans("ServiceNb", $cursorline).'</td>';
  1334. print '<td width="80" class="center">'.$langs->trans("VAT").'</td>';
  1335. print '<td width="80" class="right">'.$langs->trans("PriceUHT").'</td>';
  1336. //if (isModEnabled("multicurrency")) {
  1337. // print '<td width="80" class="right">'.$langs->trans("PriceUHTCurrency").'</td>';
  1338. //}
  1339. print '<td width="30" class="center">'.$langs->trans("Qty").'</td>';
  1340. if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
  1341. print '<td width="30" class="left">'.$langs->trans("Unit").'</td>';
  1342. }
  1343. print '<td width="50" class="right">'.$langs->trans("ReductionShort").'</td>';
  1344. if (isModEnabled('margin') && getDolGlobalString('MARGIN_SHOW_ON_CONTRACT')) {
  1345. print '<td width="50" class="right">'.$langs->trans("BuyingPrice").'</td>';
  1346. }
  1347. //
  1348. if ($nbofservices > 1 && $conf->browser->layout != 'phone' && $user->hasRight('contrat', 'creer')) {
  1349. print '<td width="30" class="linecolmove tdlineupdown center">';
  1350. if ($cursorline > 1) {
  1351. print '<a class="lineupdown reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=up&token='.newToken().'&rowid='.$objp->rowid.'">';
  1352. echo img_up('default', 0, 'imgupforline');
  1353. print '</a>';
  1354. }
  1355. if ($cursorline < $nbofservices) {
  1356. print '<a class="lineupdown reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=down&token='.newToken().'&rowid='.$objp->rowid.'">';
  1357. echo img_down('default', 0, 'imgdownforline');
  1358. print '</a>';
  1359. }
  1360. print '</td>';
  1361. } else {
  1362. print '<td width="30">&nbsp;</td>';
  1363. }
  1364. print "</tr>\n";
  1365. // Line in view mode
  1366. if ($action != 'editline' || GETPOST('rowid') != $objp->rowid) {
  1367. $moreparam = '';
  1368. if (getDolGlobalString('CONTRACT_HIDE_CLOSED_SERVICES_BY_DEFAULT') && $objp->statut == ContratLigne::STATUS_CLOSED && $action != 'showclosedlines') {
  1369. $moreparam = 'style="display: none;"';
  1370. }
  1371. print '<tr class="tdtop oddeven" '.$moreparam.'>';
  1372. // Label
  1373. if ($objp->fk_product > 0) {
  1374. $productstatic->id = $objp->fk_product;
  1375. $productstatic->type = $objp->ptype;
  1376. $productstatic->ref = $objp->pref;
  1377. $productstatic->entity = $objp->pentity;
  1378. $productstatic->label = $objp->plabel;
  1379. $productstatic->status = $objp->tosell;
  1380. $productstatic->status_buy = $objp->tobuy;
  1381. $productstatic->status_batch = $objp->tobatch;
  1382. print '<td>';
  1383. $text = $productstatic->getNomUrl(1, '', 32);
  1384. if ($objp->plabel) {
  1385. $text .= ' - ';
  1386. $text .= $objp->plabel;
  1387. }
  1388. $description = $objp->description;
  1389. // Add description in form
  1390. if (getDolGlobalInt('PRODUIT_DESC_IN_FORM_ACCORDING_TO_DEVICE')) {
  1391. $text .= (!empty($objp->description) && $objp->description != $objp->plabel) ? '<br>'.dol_htmlentitiesbr($objp->description) : '';
  1392. $description = ''; // Already added into main visible desc
  1393. }
  1394. print $form->textwithtooltip($text, $description, 3, '', '', $cursorline, 3, (!empty($line->fk_parent_line) ? img_picto('', 'rightarrow') : ''));
  1395. print '</td>';
  1396. } else {
  1397. print '<td>'.img_object($langs->trans("ShowProductOrService"), ($objp->product_type ? 'service' : 'product')).' '.dol_htmlentitiesbr($objp->description)."</td>\n";
  1398. }
  1399. // VAT
  1400. print '<td class="center">';
  1401. print vatrate($objp->tva_tx.($objp->vat_src_code ? (' ('.$objp->vat_src_code.')') : ''), '%', $objp->info_bits);
  1402. print '</td>';
  1403. // Price
  1404. print '<td class="right">'.($objp->subprice != '' ? price($objp->subprice) : '')."</td>\n";
  1405. // Price multicurrency
  1406. /*if (isModEnabled("multicurrency")) {
  1407. print '<td class="linecoluht_currency nowrap right">'.price($objp->multicurrency_subprice).'</td>';
  1408. }*/
  1409. // Quantity
  1410. print '<td class="center">'.$objp->qty.'</td>';
  1411. // Unit
  1412. if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
  1413. print '<td class="left">'.$langs->trans($object->lines[$cursorline - 1]->getLabelOfUnit()).'</td>';
  1414. }
  1415. // Discount
  1416. if ($objp->remise_percent > 0) {
  1417. print '<td class="right">'.$objp->remise_percent."%</td>\n";
  1418. } else {
  1419. print '<td>&nbsp;</td>';
  1420. }
  1421. // Margin
  1422. if (isModEnabled('margin') && getDolGlobalString('MARGIN_SHOW_ON_CONTRACT')) {
  1423. print '<td class="right nowraponall">'.price($objp->pa_ht).'</td>';
  1424. }
  1425. // Icon move, update et delete (status contract 0=draft,1=validated,2=closed)
  1426. print '<td class="nowraponall right">';
  1427. if ($user->hasRight('contrat', 'creer') && is_array($arrayothercontracts) && count($arrayothercontracts) && ($object->statut >= 0)) {
  1428. print '<!-- link to move service line into another contract -->';
  1429. print '<a class="reposition marginrightonly" style="padding-left: 5px;" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=move&token='.newToken().'&rowid='.$objp->rowid.'">';
  1430. print img_picto($langs->trans("MoveToAnotherContract"), 'uparrow');
  1431. print '</a>';
  1432. }
  1433. if ($user->hasRight('contrat', 'creer') && ($object->statut >= 0)) {
  1434. print '<a class="reposition marginrightonly editfielda" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=editline&token='.newToken().'&rowid='.$objp->rowid.'">';
  1435. print img_edit();
  1436. print '</a>';
  1437. }
  1438. if ($user->hasRight('contrat', 'creer') && ($object->statut >= 0)) {
  1439. print '<a class="reposition marginrightonly" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=deleteline&token='.newToken().'&rowid='.$objp->rowid.'">';
  1440. print img_delete();
  1441. print '</a>';
  1442. }
  1443. print '</td>';
  1444. print "</tr>\n";
  1445. $colspan = 6;
  1446. if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
  1447. $colspan++;
  1448. }
  1449. if (isModEnabled('margin') && getDolGlobalString('MARGIN_SHOW_ON_CONTRACT')) {
  1450. $colspan++;
  1451. }
  1452. // Dates of service planed and real
  1453. if ($objp->subprice >= 0) {
  1454. print '<tr class="oddeven" '.$moreparam.'>';
  1455. print '<td colspan="'.$colspan.'">';
  1456. // Date planned
  1457. print $langs->trans("DateStartPlanned").': ';
  1458. if ($objp->date_start) {
  1459. print dol_print_date($db->jdate($objp->date_start), 'day');
  1460. // Warning si date prevu passee et pas en service
  1461. if ($objp->statut == 0 && $db->jdate($objp->date_start) < ($now - $conf->contrat->services->inactifs->warning_delay)) {
  1462. $warning_delay = $conf->contrat->services->inactifs->warning_delay / 3600 / 24;
  1463. $textlate = $langs->trans("Late").' = '.$langs->trans("DateReference").' > '.$langs->trans("DateToday").' '.(ceil($warning_delay) >= 0 ? '+' : '').ceil($warning_delay).' '.$langs->trans("days");
  1464. print " ".img_warning($textlate);
  1465. }
  1466. } else {
  1467. print $langs->trans("Unknown");
  1468. }
  1469. print ' &nbsp;-&nbsp; ';
  1470. print $langs->trans("DateEndPlanned").': ';
  1471. if ($objp->date_end) {
  1472. print dol_print_date($db->jdate($objp->date_end), 'day');
  1473. if ($objp->statut == 4 && $db->jdate($objp->date_end) < ($now - $conf->contrat->services->expires->warning_delay)) {
  1474. $warning_delay = $conf->contrat->services->expires->warning_delay / 3600 / 24;
  1475. $textlate = $langs->trans("Late").' = '.$langs->trans("DateReference").' > '.$langs->trans("DateToday").' '.(ceil($warning_delay) >= 0 ? '+' : '').ceil($warning_delay).' '.$langs->trans("days");
  1476. print " ".img_warning($textlate);
  1477. }
  1478. } else {
  1479. print $langs->trans("Unknown");
  1480. }
  1481. print '</td>';
  1482. print '</tr>';
  1483. }
  1484. // Display lines extrafields
  1485. if (is_array($extralabelslines) && count($extralabelslines) > 0) {
  1486. $line = new ContratLigne($db);
  1487. $line->id = $objp->rowid;
  1488. $line->fetch_optionals();
  1489. print $line->showOptionals($extrafields, 'view', array('class'=>'oddeven', 'style'=>$moreparam, 'colspan'=>$colspan), '', '', 1);
  1490. }
  1491. } else {
  1492. // Line in mode update
  1493. // Ligne carac
  1494. print '<tr class="oddeven">';
  1495. print '<td>';
  1496. if ($objp->fk_product > 0) {
  1497. $canchangeproduct = 1;
  1498. if (empty($canchangeproduct)) {
  1499. $productstatic->id = $objp->fk_product;
  1500. $productstatic->type = $objp->ptype;
  1501. $productstatic->ref = $objp->pref;
  1502. $productstatic->entity = $objp->pentity;
  1503. print $productstatic->getNomUrl(1, '', 32);
  1504. print $objp->label ? ' - '.dol_trunc($objp->label, 32) : '';
  1505. print '<input type="hidden" name="idprod" value="'.(!empty($object->lines[$cursorline - 1]->fk_product) ? $object->lines[$cursorline - 1]->fk_product : 0).'">';
  1506. } else {
  1507. $senderissupplier = 0;
  1508. if (empty($senderissupplier)) {
  1509. print $form->select_produits((!empty($object->lines[$cursorline - 1]->fk_product) ? $object->lines[$cursorline - 1]->fk_product : 0), 'idprod');
  1510. } else {
  1511. $form->select_produits_fournisseurs((!empty($object->lines[$cursorline - 1]->fk_product) ? $object->lines[$cursorline - 1]->fk_product : 0), 'idprod');
  1512. }
  1513. }
  1514. print '<br>';
  1515. } else {
  1516. print $objp->label ? $objp->label.'<br>' : '';
  1517. print '<input type="hidden" name="idprod" value="'.(!empty($object->lines[$cursorline - 1]->fk_product) ? $object->lines[$cursorline - 1]->fk_product : 0).'">';
  1518. }
  1519. // editeur wysiwyg
  1520. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1521. $nbrows = ROWS_2;
  1522. if (getDolGlobalString('MAIN_INPUT_DESC_HEIGHT')) {
  1523. $nbrows = $conf->global->MAIN_INPUT_DESC_HEIGHT;
  1524. }
  1525. $enable = (isset($conf->global->FCKEDITOR_ENABLE_DETAILS) ? $conf->global->FCKEDITOR_ENABLE_DETAILS : 0);
  1526. $doleditor = new DolEditor('product_desc', $objp->description, '', 92, 'dolibarr_details', '', false, true, $enable, $nbrows, '90%');
  1527. $doleditor->Create();
  1528. print '</td>';
  1529. // VAT
  1530. print '<td class="right">';
  1531. print $form->load_tva("eltva_tx", $objp->tva_tx.($objp->vat_src_code ? (' ('.$objp->vat_src_code.')') : ''), $mysoc, $object->thirdparty, $objp->fk_product, $objp->info_bits, $objp->product_type, 0, 1);
  1532. print '</td>';
  1533. // Price
  1534. print '<td class="right"><input size="5" type="text" name="elprice" value="'.price($objp->subprice).'"></td>';
  1535. // Price multicurrency
  1536. /*if (isModEnabled("multicurrency")) {
  1537. print '<td class="linecoluht_currency nowrap right">'.price($objp->multicurrency_subprice).'</td>';
  1538. }*/
  1539. // Quantity
  1540. print '<td class="center"><input size="2" type="text" name="elqty" value="'.$objp->qty.'"></td>';
  1541. // Unit
  1542. if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
  1543. print '<td class="left">';
  1544. print $form->selectUnits($objp->fk_unit, "unit");
  1545. print '</td>';
  1546. }
  1547. // Discount
  1548. print '<td class="nowrap right"><input size="1" type="text" name="elremise_percent" value="'.$objp->remise_percent.'">%</td>';
  1549. if (!empty($usemargins)) {
  1550. print '<td class="right">';
  1551. if ($objp->fk_product) {
  1552. print '<select id="fournprice" name="fournprice"></select>';
  1553. }
  1554. print '<input id="buying_price" type="text" size="5" name="buying_price" value="'.price($objp->pa_ht, 0, '', 0).'"></td>';
  1555. }
  1556. print '<td class="center">';
  1557. print '<input type="submit" class="button margintoponly marginbottomonly" name="save" value="'.$langs->trans("Modify").'">';
  1558. print '<br><input type="submit" class="button margintoponly marginbottomonly button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
  1559. print '</td>';
  1560. print '</tr>';
  1561. $colspan = 6;
  1562. if (isModEnabled('margin') && getDolGlobalString('MARGIN_SHOW_ON_CONTRACT')) {
  1563. $colspan++;
  1564. }
  1565. if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
  1566. $colspan++;
  1567. }
  1568. // Line dates planed
  1569. print '<tr class="oddeven">';
  1570. print '<td colspan="'.$colspan.'">';
  1571. print $langs->trans("DateStartPlanned").' ';
  1572. print $form->selectDate($db->jdate($objp->date_start), "date_start_update", $usehm, $usehm, ($db->jdate($objp->date_start) > 0 ? 0 : 1), "update");
  1573. print ' &nbsp;&nbsp;'.$langs->trans("DateEndPlanned").' ';
  1574. print $form->selectDate($db->jdate($objp->date_end), "date_end_update", $usehm, $usehm, ($db->jdate($objp->date_end) > 0 ? 0 : 1), "update");
  1575. print '</td>';
  1576. print '</tr>';
  1577. if (is_array($extralabelslines) && count($extralabelslines) > 0) {
  1578. $line = new ContratLigne($db);
  1579. $line->id = $objp->rowid;
  1580. $line->fetch_optionals();
  1581. print $line->showOptionals($extrafields, 'edit', array('style'=>'class="oddeven"', 'colspan'=>$colspan), '', '', 1);
  1582. }
  1583. }
  1584. $db->free($result);
  1585. } else {
  1586. dol_print_error($db);
  1587. }
  1588. if ($object->statut > 0) {
  1589. $moreparam = '';
  1590. if (getDolGlobalString('CONTRACT_HIDE_CLOSED_SERVICES_BY_DEFAULT') && $object->lines[$cursorline - 1]->statut == ContratLigne::STATUS_CLOSED && $action != 'showclosedlines') {
  1591. $moreparam = 'style="display: none;"';
  1592. }
  1593. $colspan = 6;
  1594. if (getDolGlobalInt('PRODUCT_USE_UNITS')) {
  1595. $colspan++;
  1596. }
  1597. if (isModEnabled('margin') && getDolGlobalString('MARGIN_SHOW_ON_CONTRACT')) {
  1598. $colspan++;
  1599. }
  1600. print '<tr class="oddeven" '.$moreparam.'>';
  1601. print '<td class="tdhrthin" colspan="'.$colspan.'"><hr class="opacitymedium tdhrthin"></td>';
  1602. print "</tr>\n";
  1603. }
  1604. print "</table>";
  1605. print '</div>';
  1606. print "</form>\n";
  1607. /*
  1608. * Confirmation to delete service line of contract
  1609. */
  1610. if ($action == 'deleteline' && !$_REQUEST["cancel"] && $user->hasRight('contrat', 'creer') && $object->lines[$cursorline - 1]->id == GETPOST('rowid')) {
  1611. print $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$object->id."&lineid=".GETPOST('rowid'), $langs->trans("DeleteContractLine"), $langs->trans("ConfirmDeleteContractLine"), "confirm_deleteline", '', 0, 1);
  1612. if ($ret == 'html') {
  1613. print '<table class="notopnoleftnoright" width="100%"><tr class="oddeven" height="6"><td></td></tr></table>';
  1614. }
  1615. }
  1616. /*
  1617. * Confirmation to move service toward another contract
  1618. */
  1619. if ($action == 'move' && !$_REQUEST["cancel"] && $user->hasRight('contrat', 'creer') && $object->lines[$cursorline - 1]->id == GETPOST('rowid')) {
  1620. $arraycontractid = array();
  1621. foreach ($arrayothercontracts as $contractcursor) {
  1622. $arraycontractid[$contractcursor->id] = $contractcursor->ref;
  1623. }
  1624. //var_dump($arraycontractid);
  1625. // Cree un tableau formulaire
  1626. $formquestion = array(
  1627. 'text' => $langs->trans("ConfirmMoveToAnotherContractQuestion"),
  1628. array('type' => 'select', 'name' => 'newcid', 'values' => $arraycontractid));
  1629. print $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$object->id."&lineid=".GETPOST('rowid', 'int'), $langs->trans("MoveToAnotherContract"), $langs->trans("ConfirmMoveToAnotherContract"), "confirm_move", $formquestion);
  1630. print '<table class="notopnoleftnoright" width="100%"><tr class="oddeven" height="6"><td></td></tr></table>';
  1631. }
  1632. // Area with status and activation info of line
  1633. if ($object->statut > 0) {
  1634. print '<table class="notopnoleftnoright tableforservicepart2'.($cursorline < $nbofservices ? ' boxtablenobottom' : '').' centpercent">';
  1635. print '<tr class="oddeven" '.$moreparam.'>';
  1636. print '<td><span class="valignmiddle hideonsmartphone">'.$langs->trans("ServiceStatus").':</span> '.$object->lines[$cursorline - 1]->getLibStatut(4).'</td>';
  1637. print '<td width="30" class="right">';
  1638. if ($user->socid == 0) {
  1639. if ($object->statut > 0 && $action != 'activateline' && $action != 'unactivateline') {
  1640. $tmpaction = 'activateline';
  1641. $tmpactionpicto = 'play';
  1642. $tmpactiontext = $langs->trans("Activate");
  1643. if ($objp->statut == 4) {
  1644. $tmpaction = 'unactivateline';
  1645. $tmpactionpicto = 'playstop';
  1646. $tmpactiontext = $langs->trans("Disable");
  1647. }
  1648. if (($tmpaction == 'activateline' && $user->hasRight('contrat', 'activer')) || ($tmpaction == 'unactivateline' && $user->hasRight('contrat', 'desactiver'))) {
  1649. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;ligne='.$object->lines[$cursorline - 1]->id.'&amp;action='.$tmpaction.'">';
  1650. print img_picto($tmpactiontext, $tmpactionpicto);
  1651. print '</a>';
  1652. }
  1653. }
  1654. }
  1655. print '</td>';
  1656. print "</tr>\n";
  1657. print '<tr class="oddeven" '.$moreparam.'>';
  1658. print '<td>';
  1659. // Si pas encore active
  1660. if (!$objp->date_start_real) {
  1661. print $langs->trans("DateStartReal").': ';
  1662. if ($objp->date_start_real) {
  1663. print dol_print_date($db->jdate($objp->date_start_real), 'day');
  1664. } else {
  1665. print $langs->trans("ContractStatusNotRunning");
  1666. }
  1667. }
  1668. // Si active et en cours
  1669. if ($objp->date_start_real && !$objp->date_end_real) {
  1670. print $langs->trans("DateStartReal").': ';
  1671. print dol_print_date($db->jdate($objp->date_start_real), 'day');
  1672. }
  1673. // Si desactive
  1674. if ($objp->date_start_real && $objp->date_end_real) {
  1675. print $langs->trans("DateStartReal").': ';
  1676. print dol_print_date($db->jdate($objp->date_start_real), 'day');
  1677. print ' &nbsp;-&nbsp; ';
  1678. print $langs->trans("DateEndReal").': ';
  1679. print dol_print_date($db->jdate($objp->date_end_real), 'day');
  1680. }
  1681. if (!empty($objp->comment)) {
  1682. print " &nbsp;-&nbsp; ".$objp->comment;
  1683. }
  1684. print '</td>';
  1685. print '<td class="center">&nbsp;</td>';
  1686. print '</tr>';
  1687. print '</table>';
  1688. }
  1689. // Form to activate line
  1690. if ($user->hasRight('contrat', 'activer') && $action == 'activateline' && $object->lines[$cursorline - 1]->id == GETPOST('ligne', 'int')) {
  1691. print '<form name="active" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  1692. print '<input type="hidden" name="token" value="'.newToken().'">';
  1693. print '<input type="hidden" name="action" value="confirm_active">';
  1694. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  1695. print '<input type="hidden" name="id" value="'.$object->id.'">';
  1696. print '<input type="hidden" name="ligne" value="'.GETPOST('ligne', 'int').'">';
  1697. print '<input type="hidden" name="confirm" value="yes">';
  1698. print '<table class="noborder tableforservicepart2'.($cursorline < $nbofservices ? ' boxtablenobottom' : '').' centpercent">';
  1699. // Definie date debut et fin par defaut
  1700. $dateactstart = $objp->date_start;
  1701. if (GETPOST('remonth')) {
  1702. $dateactstart = dol_mktime(12, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear'));
  1703. } elseif (!$dateactstart) {
  1704. $dateactstart = time();
  1705. }
  1706. $dateactend = $objp->date_end;
  1707. if (GETPOST('endmonth')) {
  1708. $dateactend = dol_mktime(12, 0, 0, GETPOST('endmonth'), GETPOST('endday'), GETPOST('endyear'));
  1709. } elseif (!$dateactend) {
  1710. if ($objp->fk_product > 0) {
  1711. $product = new Product($db);
  1712. $product->fetch($objp->fk_product);
  1713. $dateactend = dol_time_plus_duree(time(), $product->duration_value, $product->duration_unit);
  1714. }
  1715. }
  1716. print '<tr class="oddeven">';
  1717. print '<td class="nohover">'.$langs->trans("DateServiceActivate").'</td><td class="nohover">';
  1718. print $form->selectDate($dateactstart, 'start', $usehm, $usehm, '', "active", 1, 0);
  1719. print '</td>';
  1720. print '<td class="nohover">'.$langs->trans("DateEndPlanned").'</td><td class="nohover">';
  1721. print $form->selectDate($dateactend, "end", $usehm, $usehm, '', "active", 1, 0);
  1722. print '</td>';
  1723. print '<td class="center nohover">';
  1724. print '</td>';
  1725. print '</tr>';
  1726. print '<tr class="oddeven">';
  1727. print '<td class="nohover">'.$langs->trans("Comment").'</td><td colspan="3" class="nohover" colspan="'.(isModEnabled('margin') ? 4 : 3).'"><input type="text" class="minwidth300" name="comment" value="'.dol_escape_htmltag(GETPOST("comment", 'alphanohtml')).'"></td>';
  1728. print '<td class="nohover right">';
  1729. print '<input type="submit" class="button" name="activate" value="'.$langs->trans("Activate").'"> &nbsp; ';
  1730. print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
  1731. print '</td>';
  1732. print '</tr>';
  1733. print '</table>';
  1734. print '</form>';
  1735. }
  1736. if ($user->hasRight('contrat', 'activer') && $action == 'unactivateline' && $object->lines[$cursorline - 1]->id == GETPOST('ligne', 'int')) {
  1737. /**
  1738. * Disable a contract line
  1739. */
  1740. print '<!-- Form to disabled a line -->'."\n";
  1741. print '<form name="confirm_closeline" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;ligne='.$object->lines[$cursorline - 1]->id.'" method="post">';
  1742. print '<input type="hidden" name="token" value="'.newToken().'">';
  1743. print '<input type="hidden" name="confirm" value="yes">';
  1744. print '<input type="hidden" name="action" value="confirm_closeline">';
  1745. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  1746. print '<table class="noborder tableforservicepart2'.($cursorline < $nbofservices ? ' boxtablenobottom' : '').' centpercent">';
  1747. // Definie date debut et fin par defaut
  1748. $dateactstart = $objp->date_start_real;
  1749. if (GETPOST('remonth')) {
  1750. $dateactstart = dol_mktime(12, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear'));
  1751. } elseif (!$dateactstart) {
  1752. $dateactstart = time();
  1753. }
  1754. $dateactend = $objp->date_end_real;
  1755. if (GETPOST('endmonth')) {
  1756. $dateactend = dol_mktime(12, 0, 0, GETPOST('endmonth'), GETPOST('endday'), GETPOST('endyear'));
  1757. } elseif (!$dateactend) {
  1758. if ($objp->fk_product > 0) {
  1759. $product = new Product($db);
  1760. $product->fetch($objp->fk_product);
  1761. $dateactend = dol_time_plus_duree(time(), $product->duration_value, $product->duration_unit);
  1762. }
  1763. }
  1764. $now = dol_now();
  1765. if ($dateactend > $now) {
  1766. $dateactend = $now;
  1767. }
  1768. print '<tr class="oddeven"><td colspan="2" class="nohover">';
  1769. if ($objp->statut >= 4) {
  1770. if ($objp->statut == 4) {
  1771. print $langs->trans("DateEndReal").' ';
  1772. print $form->selectDate($dateactend, "end", $usehm, $usehm, ($objp->date_end_real > 0 ? 0 : 1), "closeline", 1, 1);
  1773. }
  1774. }
  1775. print '</td>';
  1776. print '<td class="center nohover">';
  1777. print '</td></tr>';
  1778. print '<tr class="oddeven">';
  1779. print '<td class="nohover">'.$langs->trans("Comment").'</td><td class="nohover"><input class="quatrevingtpercent" type="text" class="flat" name="comment" value="'.dol_escape_htmltag(GETPOST('comment', 'alpha')).'"></td>';
  1780. print '<td class="nohover right">';
  1781. print '<input type="submit" class="button" name="close" value="'.$langs->trans("Disable").'"> &nbsp; ';
  1782. print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
  1783. print '</td>';
  1784. print '</tr>';
  1785. print '</table>';
  1786. print '</form>';
  1787. }
  1788. print '</div>';
  1789. $cursorline++;
  1790. }
  1791. print '</div>';
  1792. // Form to add new line
  1793. if ($user->hasRight('contrat', 'creer') && ($object->statut == 0)) {
  1794. $dateSelector = 1;
  1795. print "\n";
  1796. print ' <form name="addproduct" id="addproduct" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.(($action != 'editline') ? '' : '#line_'.GETPOST('lineid', 'int')).'" method="POST">
  1797. <input type="hidden" name="token" value="'.newToken().'">
  1798. <input type="hidden" name="action" value="'.(($action != 'editline') ? 'addline' : 'updateline').'">
  1799. <input type="hidden" name="mode" value="">
  1800. <input type="hidden" name="id" value="'.$object->id.'">
  1801. <input type="hidden" name="page_y" value="">
  1802. <input type="hidden" name="backtopage" value="'.$backtopage.'">
  1803. ';
  1804. print '<div class="div-table-responsive-no-min">';
  1805. print '<table id="tablelines" class="noborder noshadow" width="100%">'; // Array with (n*2)+1 lines
  1806. // Form to add new line
  1807. if ($action != 'editline') {
  1808. $forcetoshowtitlelines = 1;
  1809. if (empty($object->multicurrency_code)) {
  1810. $object->multicurrency_code = $conf->currency; // TODO Remove this when multicurrency supported on contracts
  1811. }
  1812. // Add free products/services
  1813. $parameters = array();
  1814. $reshook = $hookmanager->executeHooks('formAddObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  1815. if ($reshook < 0) {
  1816. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  1817. }
  1818. if (empty($reshook)) {
  1819. $object->formAddObjectLine(1, $mysoc, $soc);
  1820. }
  1821. }
  1822. print '</table>';
  1823. print '</div>';
  1824. print '</form>';
  1825. }
  1826. print dol_get_fiche_end();
  1827. // Select mail models is same action as presend
  1828. if (GETPOST('modelselected')) {
  1829. $action = 'presend';
  1830. }
  1831. /*
  1832. * Buttons
  1833. */
  1834. if ($user->socid == 0 && $action != 'presend' && $action != 'editline') {
  1835. print '<div class="tabsAction">';
  1836. $parameters = array();
  1837. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  1838. if (empty($reshook)) {
  1839. $params = array(
  1840. 'attr' => array(
  1841. 'title' => '',
  1842. 'class' => 'classfortooltip'
  1843. )
  1844. );
  1845. // Send
  1846. if (empty($user->socid)) {
  1847. if ($object->status == $object::STATUS_VALIDATED) {
  1848. if ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') || $user->hasRight('contrat', 'creer'))) {
  1849. print dolGetButtonAction('', $langs->trans('SendMail'), 'default', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=presend&token='.newToken().'&mode=init#formmailbeforetitle', '', true, $params);
  1850. } else {
  1851. print dolGetButtonAction('', $langs->trans('SendMail'), 'default', '#', '', false, $params);
  1852. }
  1853. }
  1854. }
  1855. if ($object->status == $object::STATUS_DRAFT && $nbofservices) {
  1856. if ($user->hasRight('contrat', 'creer')) {
  1857. unset($params['attr']['title']);
  1858. print dolGetButtonAction($langs->trans('Validate'), '', 'default', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=valid&token='.newToken(), '', true, $params);
  1859. } else {
  1860. $params['attr']['title'] = $langs->trans("NotEnoughPermissions");
  1861. print dolGetButtonAction($langs->trans('Validate'), '', 'default', '#', '', false, $params);
  1862. }
  1863. }
  1864. if ($object->status == $object::STATUS_VALIDATED) {
  1865. if ($user->hasRight('contrat', 'creer')) {
  1866. unset($params['attr']['title']);
  1867. print dolGetButtonAction($langs->trans('Modify'), '', 'default', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=reopen&token='.newToken(), '', true, $params);
  1868. } else {
  1869. $params['attr']['title'] = $langs->trans("NotEnoughPermissions");
  1870. print dolGetButtonAction($langs->trans('Modify'), '', 'default', '#', '', false, $params);
  1871. }
  1872. }
  1873. // Create ... buttons
  1874. $arrayofcreatebutton = array();
  1875. if (isModEnabled('commande') && $object->status > 0 && $object->nbofservicesclosed < $nbofservices) {
  1876. $arrayofcreatebutton[] = array(
  1877. 'url' => '/commande/card.php?action=create&token='.newToken().'&origin='.$object->element.'&originid='.$object->id.'&socid='.$object->thirdparty->id,
  1878. 'label' => $langs->trans('AddOrder'),
  1879. 'lang' => 'orders',
  1880. 'perm' => $user->hasRight('commande', 'creer')
  1881. );
  1882. }
  1883. if (isModEnabled('facture') && $object->status > 0 && $soc->client > 0) {
  1884. $arrayofcreatebutton[] = array(
  1885. 'url' => '/compta/facture/card.php?action=create&origin='.$object->element.'&originid='.$object->id.'&socid='.$object->thirdparty->id,
  1886. 'label' => $langs->trans('CreateBill'),
  1887. 'lang' => 'bills',
  1888. 'perm' => $user->hasRight('facture', 'creer')
  1889. );
  1890. }
  1891. if (isModEnabled('supplier_invoice') && $object->status > 0 && $soc->fournisseur == 1) {
  1892. $arrayofcreatebutton[] = array(
  1893. 'url' => '/fourn/facture/card.php?action=create&origin='.$object->element.'&originid='.$object->id.'&socid='.$object->thirdparty->id,
  1894. 'label' => $langs->trans('AddSupplierInvoice'),
  1895. 'lang' => 'bills',
  1896. 'perm' => $user->hasRight('fournisseur', 'facture', 'creer')
  1897. );
  1898. }
  1899. if (count($arrayofcreatebutton)) {
  1900. unset($params['attr']['title']);
  1901. print dolGetButtonAction('', $langs->trans("Create"), 'default', $arrayofcreatebutton, '', true, $params);
  1902. }
  1903. if ($object->nbofservicesclosed > 0 || $object->nbofserviceswait > 0) {
  1904. if ($user->hasRight('contrat', 'activer')) {
  1905. unset($params['attr']['title']);
  1906. print dolGetButtonAction($langs->trans('ActivateAllContracts'), '', 'default', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=activate&token='.newToken(), '', true, $params);
  1907. } else {
  1908. unset($params['attr']['title']);
  1909. print dolGetButtonAction($langs->trans('ActivateAllContracts'), '', 'default', '#', '', false, $params);
  1910. }
  1911. }
  1912. if ($object->nbofservicesclosed < $nbofservices) {
  1913. if ($user->hasRight('contrat', 'desactiver')) {
  1914. unset($params['attr']['title']);
  1915. print dolGetButtonAction($langs->trans('CloseAllContracts'), '', 'default', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=close&token='.newToken(), '', true, $params);
  1916. } else {
  1917. unset($params['attr']['title']);
  1918. print dolGetButtonAction($langs->trans('CloseAllContracts'), '', 'default', '#', '', false, $params);
  1919. }
  1920. //if (! $numactive)
  1921. //{
  1922. //}
  1923. //else
  1924. //{
  1925. // print '<div class="inline-block divButAction"><a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("CloseRefusedBecauseOneServiceActive").'">'.$langs->trans("Close").'</a></div>';
  1926. //}
  1927. }
  1928. if (getDolGlobalString('CONTRACT_HIDE_CLOSED_SERVICES_BY_DEFAULT') && $object->nbofservicesclosed > 0) {
  1929. if ($action == 'showclosedlines') {
  1930. print '<div class="inline-block divButAction"><a class="butAction" id="btnhideclosedlines" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=hideclosedlines">'.$langs->trans("HideClosedServices").'</a></div>';
  1931. } else {
  1932. print '<div class="inline-block divButAction"><a class="butAction" id="btnshowclosedlines" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=showclosedlines">'.$langs->trans("ShowClosedServices").'</a></div>';
  1933. }
  1934. }
  1935. // Clone
  1936. if ($user->hasRight('contrat', 'creer')) {
  1937. unset($params['attr']['title']);
  1938. print dolGetButtonAction($langs->trans('ToClone'), '', 'default', $_SERVER['PHP_SELF'].'?id='.$object->id.'&socid='.$object->socid.'&action=clone&token='.newToken(), '', true, $params);
  1939. }
  1940. // Delete
  1941. unset($params['attr']['title']);
  1942. print dolGetButtonAction($langs->trans('Delete'), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), '', $permissiontodelete, $params);
  1943. }
  1944. print "</div>";
  1945. }
  1946. if ($action != 'presend') {
  1947. print '<div class="fichecenter"><div class="fichehalfleft">';
  1948. /*
  1949. * Generated documents
  1950. */
  1951. $filename = dol_sanitizeFileName($object->ref);
  1952. $filedir = $conf->contrat->multidir_output[$object->entity]."/".dol_sanitizeFileName($object->ref);
  1953. $urlsource = $_SERVER["PHP_SELF"]."?id=".$object->id;
  1954. $genallowed = $user->hasRight('contrat', 'lire');
  1955. $delallowed = $user->hasRight('contrat', 'creer');
  1956. print $formfile->showdocuments('contract', $filename, $filedir, $urlsource, $genallowed, $delallowed, ($object->model_pdf ? $object->model_pdf : getDolGlobalString('CONTRACT_ADDON_PDF')), 1, 0, 0, 28, 0, '', 0, '', $soc->default_lang, '', $object);
  1957. // Show links to link elements
  1958. $linktoelem = $form->showLinkToObjectBlock($object, null, array('contrat'));
  1959. $somethingshown = $form->showLinkedObjectBlock($object, $linktoelem);
  1960. // Show online signature link
  1961. if ($object->statut != Contrat::STATUS_DRAFT && getDolGlobalString('CONTRACT_ALLOW_ONLINESIGN')) {
  1962. print '<br><!-- Link to sign -->';
  1963. require_once DOL_DOCUMENT_ROOT.'/core/lib/signature.lib.php';
  1964. print showOnlineSignatureUrl('contract', $object->ref).'<br>';
  1965. }
  1966. print '</div><div class="fichehalfright">';
  1967. $MAXEVENT = 10;
  1968. $morehtmlcenter = dolGetButtonTitle($langs->trans('SeeAll'), '', 'fa fa-bars imgforviewmode', DOL_URL_ROOT.'/contrat/agenda.php?id='.$object->id);
  1969. // List of actions on element
  1970. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
  1971. $formactions = new FormActions($db);
  1972. $somethingshown = $formactions->showactions($object, 'contract', $socid, 1, 'listactions', $MAXEVENT, '', $morehtmlcenter);
  1973. print '</div></div>';
  1974. }
  1975. // Presend form
  1976. $modelmail = 'contract';
  1977. $defaulttopic = 'SendContractRef';
  1978. $diroutput = $conf->contrat->multidir_output[$object->entity];
  1979. $trackid = 'con'.$object->id;
  1980. include DOL_DOCUMENT_ROOT.'/core/tpl/card_presend.tpl.php';
  1981. }
  1982. }
  1983. llxFooter();
  1984. $db->close();
  1985. ?>
  1986. <?php
  1987. if (isModEnabled('margin') && $action == 'editline') {
  1988. // TODO Why this ? To manage margin on contracts ??>
  1989. <script type="text/javascript">
  1990. $(document).ready(function() {
  1991. var idprod = $("input[name='idprod']").val();
  1992. var fournprice = $("input[name='fournprice']").val();
  1993. var token = '<?php echo currentToken(); ?>'; // For AJAX Call we use old 'token' and not 'newtoken'
  1994. if (idprod > 0) {
  1995. $.post('<?php echo DOL_URL_ROOT; ?>/fourn/ajax/getSupplierPrices.php', {
  1996. 'idprod': idprod,
  1997. 'token': token
  1998. }, function(data) {
  1999. if (data.length > 0) {
  2000. var options = '';
  2001. var trouve=false;
  2002. $(data).each(function() {
  2003. options += '<option value="'+this.id+'" price="'+this.price+'"';
  2004. if (fournprice > 0) {
  2005. if (this.id == fournprice) {
  2006. options += ' selected';
  2007. $("#buying_price").val(this.price);
  2008. trouve = true;
  2009. }
  2010. }
  2011. options += '>'+this.label+'</option>';
  2012. });
  2013. options += '<option value=null'+(trouve?'':' selected')+'><?php echo $langs->trans("InputPrice"); ?></option>';
  2014. $("#fournprice").html(options);
  2015. if (trouve) {
  2016. $("#buying_price").hide();
  2017. $("#fournprice").show();
  2018. }
  2019. else {
  2020. $("#buying_price").show();
  2021. }
  2022. $("#fournprice").change(function() {
  2023. var selval = $(this).find('option:selected').attr("price");
  2024. if (selval)
  2025. $("#buying_price").val(selval).hide();
  2026. else
  2027. $('#buying_price').show();
  2028. });
  2029. }
  2030. else {
  2031. $("#fournprice").hide();
  2032. $('#buying_price').show();
  2033. }
  2034. },
  2035. 'json');
  2036. }
  2037. else {
  2038. $("#fournprice").hide();
  2039. $('#buying_price').show();
  2040. }
  2041. });
  2042. </script>
  2043. <?php
  2044. }