PageRenderTime 66ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/contrat/card.php

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