card.php 85 KB

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