card.php 83 KB

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