PageRenderTime 62ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/commande/fiche.old.php

https://bitbucket.org/speedealing/speedealing
PHP | 2426 lines | 1804 code | 294 blank | 328 comment | 575 complexity | 371ab1ca721387cc7c0c04a681616465 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  6. * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
  7. * Copyright (C) 2010-2012 Juanjo Menent <jmenent@2byte.es>
  8. * Copyright (C) 2011 Philippe Grand <philippe.grand@atoo-net.com>
  9. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  10. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
  11. * Copyright (C) 2012 David Moothen <dmoothen@gmail.com>
  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. require '../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formorder.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/commande/core/modules/commande/modules_commande.php';
  30. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/order.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  33. if (! empty($conf->propal->enabled))
  34. require DOL_DOCUMENT_ROOT.'/propal/class/propal.class.php';
  35. if (! empty($conf->projet->enabled)) {
  36. require DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  37. require DOL_DOCUMENT_ROOT.'/core/lib/project.lib.php';
  38. }
  39. $langs->load('orders');
  40. $langs->load('sendings');
  41. $langs->load('companies');
  42. $langs->load('bills');
  43. $langs->load('propal');
  44. $langs->load('deliveries');
  45. $langs->load('products');
  46. if (! empty($conf->margin->enabled))
  47. $langs->load('margins');
  48. $id=(GETPOST('id','alpha')?GETPOST('id','alpha'):GETPOST('orderid','alpha'));
  49. $ref=GETPOST('ref','alpha');
  50. $socid=GETPOST('socid','alpha');
  51. $action=GETPOST('action','alpha');
  52. $confirm=GETPOST('confirm','alpha');
  53. $lineid=GETPOST('lineid','alpha');
  54. $origin=GETPOST('origin','alpha');
  55. $originid=(GETPOST('originid','alpha')?GETPOST('originid','alpha'):GETPOST('origin_id','alpha')); // For backward compatibility
  56. $mesg = GETPOST('mesg');
  57. //PDF
  58. $hidedetails = (GETPOST('hidedetails','int') ? GETPOST('hidedetails','int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0));
  59. $hidedesc = (GETPOST('hidedesc','int') ? GETPOST('hidedesc','int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0));
  60. $hideref = (GETPOST('hideref','int') ? GETPOST('hideref','int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0));
  61. // Security check
  62. if (! empty($user->societe_id)) $socid=$user->societe_id;
  63. $result=restrictedArea($user,'commande',$id);
  64. $object = new Commande($db);
  65. // Load object
  66. if (!empty($id))
  67. {
  68. $ret=$object->fetch($id, $ref);
  69. $ret=$object->fetch_thirdparty();
  70. }
  71. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  72. $hookmanager->initHooks(array('ordercard'));
  73. /******************************************************************************/
  74. /* Actions */
  75. /******************************************************************************/
  76. $parameters=array('socid'=>$socid);
  77. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  78. // Action clone object
  79. if ($action == 'confirm_clone' && $confirm == 'yes' && $user->rights->commande->creer)
  80. {
  81. if (1==0 && ! GETPOST('clone_content') && ! GETPOST('clone_receivers'))
  82. {
  83. $mesg='<div class="error">'.$langs->trans("NoCloneOptionsSpecified").'</div>';
  84. }
  85. else
  86. {
  87. if ($object->id > 0)
  88. {
  89. $result=$object->createFromClone($socid);
  90. if ($result > 0)
  91. {
  92. header("Location: ".$_SERVER['PHP_SELF'].'?id='.$result);
  93. exit;
  94. }
  95. else
  96. {
  97. $mesg='<div class="error">'.$object->error.'</div>';
  98. $action='';
  99. }
  100. }
  101. }
  102. }
  103. // Reopen a closed order
  104. else if ($action == 'reopen' && $user->rights->commande->creer)
  105. {
  106. if ($object->statut == 3)
  107. {
  108. $result = $object->set_reopen($user);
  109. if ($result > 0)
  110. {
  111. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id);
  112. exit;
  113. }
  114. else
  115. {
  116. $mesg='<div class="error">'.$object->error.'</div>';
  117. }
  118. }
  119. }
  120. // Suppression de la commande
  121. else if ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->commande->supprimer)
  122. {
  123. $result=$object->delete($user);
  124. if ($result > 0)
  125. {
  126. header('Location: index.php');
  127. exit;
  128. }
  129. else
  130. {
  131. $mesg='<div class="error">'.$object->error.'</div>';
  132. }
  133. }
  134. // Remove a product line
  135. else if ($action == 'confirm_deleteline' && $confirm == 'yes' && $user->rights->commande->creer)
  136. {
  137. $result = $object->deleteline($lineid);
  138. if ($result > 0)
  139. {
  140. // Define output language
  141. $outputlangs = $langs;
  142. $newlang='';
  143. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang=GETPOST('lang_id');
  144. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
  145. if (! empty($newlang))
  146. {
  147. $outputlangs = new Translate();
  148. $outputlangs->setDefaultLang($newlang);
  149. }
  150. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  151. {
  152. $ret=$object->fetch($object->id); // Reload to get new records
  153. commande_pdf_create($db, $object, $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  154. }
  155. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id);
  156. exit;
  157. }
  158. else
  159. {
  160. $mesg='<div class="error">'.$object->error.'</div>';
  161. }
  162. }
  163. // Categorisation dans projet
  164. else if ($action == 'classin' && $user->rights->commande->creer)
  165. {
  166. $object->setProject(GETPOST('projectid'));
  167. }
  168. // Add order
  169. else if ($action == 'add' && $user->rights->commande->creer)
  170. {
  171. $datecommande = dol_mktime(12, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear'));
  172. $datelivraison = dol_mktime(12, 0, 0, GETPOST('liv_month'),GETPOST('liv_day'),GETPOST('liv_year'));
  173. $object->socid=$socid;
  174. $object->date_commande = $datecommande;
  175. $object->note = GETPOST('note');
  176. $object->note_public = GETPOST('note_public');
  177. $object->source = GETPOST('source_id');
  178. $object->fk_project = GETPOST('projectid');
  179. $object->ref_client = GETPOST('ref_client');
  180. $object->modelpdf = GETPOST('model');
  181. $object->cond_reglement_code = GETPOST('cond_reglement_code');
  182. $object->mode_reglement_code = GETPOST('mode_reglement_code');
  183. $object->availability_code = GETPOST('availability_code');
  184. $object->demand_reason_code = GETPOST('demand_reason_code');
  185. $object->date_livraison = $datelivraison;
  186. $object->fk_delivery_address = GETPOST('fk_address');
  187. $object->contactid = GETPOST('contactidp');
  188. // If creation from another object of another module (Example: origin=propal, originid=1)
  189. if (! empty($origin) && ! empty($originid))
  190. {
  191. // Parse element/subelement (ex: project_task)
  192. $element = $subelement = $origin;
  193. if (preg_match('/^([^_]+)_([^_]+)/i',$origin,$regs))
  194. {
  195. $element = $regs[1];
  196. $subelement = $regs[2];
  197. }
  198. // For compatibility
  199. if ($element == 'order') {
  200. $element = $subelement = 'commande';
  201. }
  202. if ($element == 'propal') {
  203. $element = 'comm/propal'; $subelement = 'propal';
  204. }
  205. if ($element == 'contract') {
  206. $element = $subelement = 'contrat';
  207. }
  208. $object->origin = $origin;
  209. $object->origin_id = $originid;
  210. // Possibility to add external linked objects with hooks
  211. $object->linked_objects[$object->origin] = $object->origin_id;
  212. $other_linked_objects=GETPOST('other_linked_objects','array');
  213. if (! empty($other_linked_objects))
  214. {
  215. $object->linked_objects = array_merge($object->linked_objects, $other_linked_objects);
  216. }
  217. $object_id = $object->create($user);
  218. if ($object_id > 0)
  219. {
  220. dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
  221. $classname = ucfirst($subelement);
  222. $srcobject = new $classname($db);
  223. dol_syslog("Try to find source object origin=".$object->origin." originid=".$object->origin_id." to add lines");
  224. $result=$srcobject->fetch($object->origin_id);
  225. if ($result > 0)
  226. {
  227. $lines = $srcobject->lines;
  228. if (empty($lines) && method_exists($srcobject,'fetch_lines')) $lines = $srcobject->fetch_lines();
  229. $fk_parent_line=0;
  230. $num=count($lines);
  231. for ($i=0;$i<$num;$i++)
  232. {
  233. $label=(! empty($lines[$i]->label)?$lines[$i]->label:'');
  234. $desc=(! empty($lines[$i]->desc)?$lines[$i]->desc:$lines[$i]->libelle);
  235. $product_type=(! empty($lines[$i]->product_type)?$lines[$i]->product_type:0);
  236. // Dates
  237. // TODO mutualiser
  238. $date_start=$lines[$i]->date_debut_prevue;
  239. if ($lines[$i]->date_debut_reel) $date_start=$lines[$i]->date_debut_reel;
  240. if ($lines[$i]->date_start) $date_start=$lines[$i]->date_start;
  241. $date_end=$lines[$i]->date_fin_prevue;
  242. if ($lines[$i]->date_fin_reel) $date_end=$lines[$i]->date_fin_reel;
  243. if ($lines[$i]->date_end) $date_end=$lines[$i]->date_end;
  244. // Reset fk_parent_line for no child products and special product
  245. if (($lines[$i]->product_type != 9 && empty($lines[$i]->fk_parent_line)) || $lines[$i]->product_type == 9) {
  246. $fk_parent_line = 0;
  247. }
  248. $result = $object->addline(
  249. $object_id,
  250. $desc,
  251. $lines[$i]->subprice,
  252. $lines[$i]->qty,
  253. $lines[$i]->tva_tx,
  254. $lines[$i]->localtax1_tx,
  255. $lines[$i]->localtax2_tx,
  256. $lines[$i]->fk_product,
  257. $lines[$i]->remise_percent,
  258. $lines[$i]->info_bits,
  259. $lines[$i]->fk_remise_except,
  260. 'HT',
  261. 0,
  262. $datestart,
  263. $dateend,
  264. $product_type,
  265. $lines[$i]->rang,
  266. $lines[$i]->special_code,
  267. $fk_parent_line,
  268. $lines[$i]->fk_fournprice,
  269. $lines[$i]->pa_ht,
  270. $label
  271. );
  272. if ($result < 0)
  273. {
  274. $error++;
  275. break;
  276. }
  277. // Defined the new fk_parent_line
  278. if ($result > 0 && $lines[$i]->product_type == 9) {
  279. $fk_parent_line = $result;
  280. }
  281. }
  282. // Hooks
  283. $parameters=array('objFrom'=>$srcobject);
  284. $reshook=$hookmanager->executeHooks('createFrom',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
  285. if ($reshook < 0) $error++;
  286. }
  287. else
  288. {
  289. $mesg=$srcobject->error;
  290. $error++;
  291. }
  292. }
  293. else
  294. {
  295. $mesg=$object->error;
  296. $error++;
  297. }
  298. }
  299. else
  300. {
  301. $object_id = $object->create($user);
  302. // If some invoice's lines already known
  303. $NBLINES=8;
  304. for ($i = 1 ; $i <= $NBLINES ; $i++)
  305. {
  306. if ($_POST['idprod'.$i])
  307. {
  308. $xid = 'idprod'.$i;
  309. $xqty = 'qty'.$i;
  310. $xremise = 'remise_percent'.$i;
  311. $object->add_product($_POST[$xid],$_POST[$xqty],$_POST[$xremise]);
  312. }
  313. }
  314. }
  315. // Insert default contacts if defined
  316. if (!empty($object_id))
  317. {
  318. if (GETPOST('contactidp'))
  319. {
  320. $result=$object->add_contact(GETPOST('contactidp'),'CUSTOMER','external');
  321. if ($result < 0)
  322. {
  323. $mesg = '<div class="error">'.$langs->trans("ErrorFailedToAddContact").'</div>';
  324. $error++;
  325. }
  326. }
  327. $id = $object_id;
  328. $action = '';
  329. }
  330. // End of object creation, we show it
  331. if (!empty($object_id) && ! $error)
  332. {
  333. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object_id);
  334. exit;
  335. }
  336. else
  337. {
  338. $db->rollback();
  339. $action='create';
  340. if (! $mesg) $mesg='<div class="error">'.$object->error.'</div>';
  341. }
  342. }
  343. else if ($action == 'classifybilled' && $user->rights->commande->creer)
  344. {
  345. $ret=$object->classifyBilled();
  346. }
  347. // Positionne ref commande client
  348. else if ($action == 'set_ref_client' && $user->rights->commande->creer)
  349. {
  350. $object->set_ref_client($user, GETPOST('ref_client'));
  351. }
  352. else if ($action == 'setremise' && $user->rights->commande->creer)
  353. {
  354. $object->set_remise($user, GETPOST('remise'));
  355. }
  356. else if ($action == 'setabsolutediscount' && $user->rights->commande->creer)
  357. {
  358. if (GETPOST('remise_id'))
  359. {
  360. if ($object->id > 0)
  361. {
  362. $object->insert_discount(GETPOST('remise_id'));
  363. }
  364. else
  365. {
  366. dol_print_error($db,$object->error);
  367. }
  368. }
  369. }
  370. else if ($action == 'setdate' && $user->rights->commande->creer)
  371. {
  372. //print "x ".$_POST['liv_month'].", ".$_POST['liv_day'].", ".$_POST['liv_year'];
  373. $date=dol_mktime(0, 0, 0, GETPOST('order_month'), GETPOST('order_day'), GETPOST('order_year'));
  374. $result=$object->set_date($user,$date);
  375. if ($result < 0)
  376. {
  377. $mesg='<div class="error">'.$object->error.'</div>';
  378. }
  379. }
  380. else if ($action == 'setdate_livraison' && $user->rights->commande->creer)
  381. {
  382. //print "x ".$_POST['liv_month'].", ".$_POST['liv_day'].", ".$_POST['liv_year'];
  383. $datelivraison=dol_mktime(0, 0, 0, GETPOST('liv_month'), GETPOST('liv_day'), GETPOST('liv_year'));
  384. $result=$object->set_date_livraison($user,$datelivraison);
  385. if ($result < 0)
  386. {
  387. $mesg='<div class="error">'.$object->error.'</div>';
  388. }
  389. }
  390. else if ($action == 'setconditions' && $user->rights->commande->creer)
  391. {
  392. $result = $object->setPaymentTerms(GETPOST('cond_reglement_code','alpha'));
  393. if ($result < 0) dol_print_error($db,$object->error);
  394. }
  395. else if ($action == 'setavailability' && $user->rights->commande->creer)
  396. {
  397. $result=$object->setAvailability(GETPOST('availability_code'));
  398. if ($result < 0) dol_print_error($db,$object->error);
  399. }
  400. else if ($action == 'setdemandreason' && $user->rights->commande->creer)
  401. {
  402. $result=$object->setDemandReason(GETPOST('demand_reason_code', 'alpha'));
  403. if ($result < 0) dol_print_error($db,$object->error);
  404. }
  405. else if ($action == 'setmode' && $user->rights->commande->creer)
  406. {
  407. $result=$object->setPaymentMethods(GETPOST('mode_reglement_code','alpha'));
  408. if ($result < 0)
  409. {
  410. dol_print_error($db,$object->error);
  411. }
  412. else
  413. {
  414. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  415. {
  416. // Define output language
  417. $outputlangs = $langs;
  418. $newlang=GETPOST('lang_id','alpha');
  419. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
  420. if (! empty($newlang))
  421. {
  422. $outputlangs = new Translate();
  423. $outputlangs->setDefaultLang($newlang);
  424. }
  425. $ret=$object->fetch($object->id); // Reload to get new records
  426. commande_pdf_create($db, $object, $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  427. }
  428. }
  429. }
  430. else if ($action == 'setremisepercent' && $user->rights->commande->creer)
  431. {
  432. $result = $object->set_remise($user, GETPOST('remise_percent'));
  433. }
  434. else if ($action == 'setremiseabsolue' && $user->rights->commande->creer)
  435. {
  436. $result = $object->set_remise_absolue($user, GETPOST('remise_absolue'));
  437. }
  438. else if ($action == 'setnote_public' && $user->rights->commande->creer)
  439. {
  440. $result=$object->update_note_public(dol_html_entity_decode(GETPOST('note_public'), ENT_QUOTES));
  441. if ($result < 0) dol_print_error($db,$object->error);
  442. }
  443. else if ($action == 'setnote' && $user->rights->commande->creer)
  444. {
  445. $result=$object->update_note(dol_html_entity_decode(GETPOST('note'), ENT_QUOTES));
  446. if ($result < 0) dol_print_error($db,$object->error);
  447. }
  448. /*
  449. * Ajout d'une ligne produit dans la commande
  450. */
  451. else if ($action == 'addline' && $user->rights->commande->creer)
  452. {
  453. $langs->load('errors');
  454. $error = false;
  455. $idprod=GETPOST('idprod', 'int');
  456. $product_desc = (GETPOST('product_desc')?GETPOST('product_desc'):(GETPOST('np_desc')?GETPOST('np_desc'):(GETPOST('dp_desc')?GETPOST('dp_desc'):'')));
  457. $price_ht = GETPOST('price_ht');
  458. $tva_tx = GETPOST('tva_tx');
  459. if ((empty($idprod) || GETPOST('usenewaddlineform')) && ($price_ht < 0) && (GETPOST('qty') < 0))
  460. {
  461. setEventMessage($langs->trans('ErrorBothFieldCantBeNegative', $langs->transnoentitiesnoconv('UnitPriceHT'), $langs->transnoentitiesnoconv('Qty')), 'errors');
  462. $error = true;
  463. }
  464. if (empty($idprod) && GETPOST('type') < 0)
  465. {
  466. setEventMessage($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Type')), 'errors');
  467. $error = true;
  468. }
  469. if ((empty($idprod) || GETPOST('usenewaddlineform')) && (!($price_ht >= 0) || $price_ht == '')) // Unit price can be 0 but not ''
  470. {
  471. setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("UnitPriceHT")), 'errors');
  472. $error++;
  473. }
  474. if (! GETPOST('qty') && GETPOST('qty') == '')
  475. {
  476. setEventMessage($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Qty')), 'errors');
  477. $error = true;
  478. }
  479. if (empty($idprod) && empty($product_desc))
  480. {
  481. setEventMessage($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Description')), 'errors');
  482. $error = true;
  483. }
  484. if (! $error && (GETPOST('qty') >= 0) && (! empty($product_desc) || ! empty($idprod)))
  485. {
  486. // Clean parameters
  487. $predef=((! empty($idprod) && $conf->global->MAIN_FEATURES_LEVEL < 2) ? '_predef' : '');
  488. $date_start=dol_mktime(0, 0, 0, GETPOST('date_start'.$predef.'month'), GETPOST('date_start'.$predef.'day'), GETPOST('date_start'.$predef.'year'));
  489. $date_end=dol_mktime(0, 0, 0, GETPOST('date_end'.$predef.'month'), GETPOST('date_end'.$predef.'day'), GETPOST('date_end'.$predef.'year'));
  490. $price_base_type = (GETPOST('price_base_type', 'alpha')?GETPOST('price_base_type', 'alpha'):'HT');
  491. // Ecrase $pu par celui du produit
  492. // Ecrase $desc par celui du produit
  493. // Ecrase $txtva par celui du produit
  494. // Ecrase $base_price_type par celui du produit
  495. if (! empty($idprod))
  496. {
  497. $prod = new Product($db);
  498. $prod->fetch($idprod);
  499. $label = ((GETPOST('product_label') && GETPOST('product_label')!=$prod->label)?GETPOST('product_label'):'');
  500. // Update if prices fields are defined
  501. if (GETPOST('usenewaddlineform'))
  502. {
  503. $pu_ht=price2num($price_ht, 'MU');
  504. $pu_ttc=price2num(GETPOST('price_ttc'), 'MU');
  505. $tva_npr=(preg_match('/\*/', $tva_tx)?1:0);
  506. $tva_tx=str_replace('*','', $tva_tx);
  507. $desc = $product_desc;
  508. }
  509. else
  510. {
  511. $tva_tx = get_default_tva($mysoc,$object->client,$prod->id);
  512. $tva_npr = get_default_npr($mysoc,$object->client,$prod->id);
  513. // multiprix
  514. if (! empty($conf->global->PRODUIT_MULTIPRICES) && ! empty($object->client->price_level))
  515. {
  516. $pu_ht = $prod->multiprices[$object->client->price_level];
  517. $pu_ttc = $prod->multiprices_ttc[$object->client->price_level];
  518. $price_min = $prod->multiprices_min[$object->client->price_level];
  519. $price_base_type = $prod->multiprices_base_type[$object->client->price_level];
  520. }
  521. else
  522. {
  523. $pu_ht = $prod->price;
  524. $pu_ttc = $prod->price_ttc;
  525. $price_min = $prod->price_min;
  526. $price_base_type = $prod->price_base_type;
  527. }
  528. // On reevalue prix selon taux tva car taux tva transaction peut etre different
  529. // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur).
  530. if ($tva_tx != $prod->tva_tx)
  531. {
  532. if ($price_base_type != 'HT')
  533. {
  534. $pu_ht = price2num($pu_ttc / (1 + ($tva_tx/100)), 'MU');
  535. }
  536. else
  537. {
  538. $pu_ttc = price2num($pu_ht * (1 + ($tva_tx/100)), 'MU');
  539. }
  540. }
  541. $desc='';
  542. // Define output language
  543. if (! empty($conf->global->MAIN_MULTILANGS) && ! empty($conf->global->PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE))
  544. {
  545. $outputlangs = $langs;
  546. $newlang='';
  547. if (empty($newlang) && GETPOST('lang_id')) $newlang=GETPOST('lang_id');
  548. if (empty($newlang)) $newlang=$object->client->default_lang;
  549. if (! empty($newlang))
  550. {
  551. $outputlangs = new Translate();
  552. $outputlangs->setDefaultLang($newlang);
  553. }
  554. $desc = (! empty($prod->multilangs[$outputlangs->defaultlang]["description"])) ? $prod->multilangs[$outputlangs->defaultlang]["description"] : $prod->description;
  555. }
  556. else
  557. {
  558. $desc = $prod->description;
  559. }
  560. $desc=dol_concatdesc($desc,$product_desc);
  561. }
  562. $type = $prod->type;
  563. }
  564. else
  565. {
  566. $pu_ht = price2num($price_ht, 'MU');
  567. $pu_ttc = price2num(GETPOST('price_ttc'), 'MU');
  568. $tva_npr = (preg_match('/\*/', $tva_tx)?1:0);
  569. $tva_tx = str_replace('*', '', $tva_tx);
  570. $label = (GETPOST('product_label')?GETPOST('product_label'):'');
  571. $desc = $product_desc;
  572. $type = GETPOST('type');
  573. }
  574. // Margin
  575. $fournprice=(GETPOST('fournprice')?GETPOST('fournprice'):'');
  576. $buyingprice=(GETPOST('buying_price')?GETPOST('buying_price'):'');
  577. // Local Taxes
  578. $localtax1_tx= get_localtax($tva_tx, 1, $object->client);
  579. $localtax2_tx= get_localtax($tva_tx, 2, $object->client);
  580. $desc=dol_htmlcleanlastbr($desc);
  581. $info_bits=0;
  582. if ($tva_npr) $info_bits |= 0x01;
  583. if (! empty($price_min) && (price2num($pu_ht)*(1-price2num(GETPOST('remise_percent'))/100) < price2num($price_min)))
  584. {
  585. $mesg = $langs->trans("CantBeLessThanMinPrice",price2num($price_min,'MU') . $langs->getCurrencySymbol($conf->currency));
  586. setEventMessage($mesg, 'errors');
  587. }
  588. else
  589. {
  590. // Insert line
  591. $result = $object->addline(
  592. $object->id,
  593. $desc,
  594. $pu_ht,
  595. GETPOST('qty'),
  596. $tva_tx,
  597. $localtax1_tx,
  598. $localtax2_tx,
  599. $idprod,
  600. GETPOST('remise_percent'),
  601. $info_bits,
  602. 0,
  603. $price_base_type,
  604. $pu_ttc,
  605. $date_start,
  606. $date_end,
  607. $type,
  608. -1,
  609. 0,
  610. GETPOST('fk_parent_line'),
  611. $fournprice,
  612. $buyingprice,
  613. $label
  614. );
  615. if ($result > 0)
  616. {
  617. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  618. {
  619. // Define output language
  620. $outputlangs = $langs;
  621. $newlang=GETPOST('lang_id','alpha');
  622. if (! empty($conf->global->MAIN_MULTILANGS) && empty($newlang)) $newlang=$object->client->default_lang;
  623. if (! empty($newlang))
  624. {
  625. $outputlangs = new Translate();
  626. $outputlangs->setDefaultLang($newlang);
  627. }
  628. $ret=$object->fetch($object->id); // Reload to get new records
  629. commande_pdf_create($db, $object, $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  630. }
  631. unset($_POST['qty']);
  632. unset($_POST['type']);
  633. unset($_POST['idprod']);
  634. unset($_POST['remise_percent']);
  635. unset($_POST['price_ht']);
  636. unset($_POST['price_ttc']);
  637. unset($_POST['tva_tx']);
  638. unset($_POST['product_ref']);
  639. unset($_POST['product_label']);
  640. unset($_POST['product_desc']);
  641. unset($_POST['fournprice']);
  642. unset($_POST['buying_price']);
  643. // old method
  644. unset($_POST['np_desc']);
  645. unset($_POST['dp_desc']);
  646. }
  647. else
  648. {
  649. setEventMessage($object->error, 'errors');
  650. }
  651. }
  652. }
  653. }
  654. /*
  655. * Mise a jour d'une ligne dans la commande
  656. */
  657. else if ($action == 'updateligne' && $user->rights->commande->creer && GETPOST('save') == $langs->trans('Save'))
  658. {
  659. // Clean parameters
  660. $date_start='';
  661. $date_end='';
  662. $date_start=dol_mktime(0, 0, 0, GETPOST('date_startmonth'), GETPOST('date_startday'), GETPOST('date_startyear'));
  663. $date_end=dol_mktime(0, 0, 0, GETPOST('date_endmonth'), GETPOST('date_endday'), GETPOST('date_endyear'));
  664. $description=dol_htmlcleanlastbr(GETPOST('product_desc'));
  665. $pu_ht=GETPOST('price_ht');
  666. // Define info_bits
  667. $info_bits=0;
  668. if (preg_match('/\*/', GETPOST('tva_tx'))) $info_bits |= 0x01;
  669. // Define vat_rate
  670. $vat_rate=GETPOST('tva_tx');
  671. $vat_rate=str_replace('*','',$vat_rate);
  672. $localtax1_rate=get_localtax($vat_rate,1,$object->client);
  673. $localtax2_rate=get_localtax($vat_rate,2,$object->client);
  674. // Add buying price
  675. $fournprice=(GETPOST('fournprice')?GETPOST('fournprice'):'');
  676. $buyingprice=(GETPOST('buying_price')?GETPOST('buying_price'):'');
  677. // Check minimum price
  678. $productid = GETPOST('productid', 'int');
  679. if (! empty($productid))
  680. {
  681. $product = new Product($db);
  682. $product->fetch($productid);
  683. $type=$product->type;
  684. $price_min = $product->price_min;
  685. if (! empty($conf->global->PRODUIT_MULTIPRICES) && ! empty($object->client->price_level))
  686. $price_min = $product->multiprices_min[$object->client->price_level];
  687. $label = ((GETPOST('update_label') && GETPOST('product_label')) ? GETPOST('product_label'):'');
  688. if ($price_min && (price2num($pu_ht)*(1-price2num(GETPOST('remise_percent'))/100) < price2num($price_min)))
  689. {
  690. setEventMessage($langs->trans("CantBeLessThanMinPrice", price2num($price_min,'MU')) . $langs->getCurrencySymbol($conf->currency), 'errors');
  691. $error++;
  692. }
  693. }
  694. else
  695. {
  696. $type = GETPOST('type');
  697. $label = (GETPOST('product_label') ? GETPOST('product_label'):'');
  698. // Check parameters
  699. if (GETPOST('type') < 0) {
  700. setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Type")), 'errors');
  701. $error++;
  702. }
  703. }
  704. if (! $error)
  705. {
  706. $result = $object->updateline(
  707. GETPOST('lineid'),
  708. $description,
  709. $pu_ht,
  710. GETPOST('qty'),
  711. GETPOST('remise_percent'),
  712. $vat_rate,
  713. $localtax1_rate,
  714. $localtax2_rate,
  715. 'HT',
  716. $info_bits,
  717. $date_start,
  718. $date_end,
  719. $type,
  720. GETPOST('fk_parent_line'),
  721. 0,
  722. $fournprice,
  723. $buyingprice,
  724. $label
  725. );
  726. if ($result >= 0)
  727. {
  728. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  729. {
  730. // Define output language
  731. $outputlangs = $langs;
  732. $newlang='';
  733. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang=GETPOST('lang_id');
  734. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
  735. if (! empty($newlang))
  736. {
  737. $outputlangs = new Translate();
  738. $outputlangs->setDefaultLang($newlang);
  739. }
  740. $ret=$object->fetch($object->id); // Reload to get new records
  741. commande_pdf_create($db, $object, $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  742. }
  743. unset($_POST['qty']);
  744. unset($_POST['type']);
  745. unset($_POST['productid']);
  746. unset($_POST['remise_percent']);
  747. unset($_POST['price_ht']);
  748. unset($_POST['price_ttc']);
  749. unset($_POST['tva_tx']);
  750. unset($_POST['product_ref']);
  751. unset($_POST['product_label']);
  752. unset($_POST['product_desc']);
  753. unset($_POST['fournprice']);
  754. unset($_POST['buying_price']);
  755. }
  756. else
  757. {
  758. setEventMessage($object->error, 'errors');
  759. }
  760. }
  761. }
  762. else if ($action == 'updateligne' && $user->rights->commande->creer && GETPOST('cancel') == $langs->trans('Cancel'))
  763. {
  764. header('Location: '.$_SERVER['PHP_SELF'].'?id='.$object->id); // Pour reaffichage de la fiche en cours d'edition
  765. exit;
  766. }
  767. else if ($action == 'confirm_validate' && $confirm == 'yes' && $user->rights->commande->valider)
  768. {
  769. $idwarehouse=GETPOST('idwarehouse');
  770. // Check parameters
  771. if (! empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $object->hasProductsOrServices(1))
  772. {
  773. if (! $idwarehouse || $idwarehouse == -1)
  774. {
  775. $error++;
  776. $mesgs[]='<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Warehouse")).'</div>';
  777. $action='';
  778. }
  779. }
  780. if (! $error)
  781. {
  782. $result=$object->valid($user,$idwarehouse);
  783. if ($result >= 0)
  784. {
  785. // Define output language
  786. $outputlangs = $langs;
  787. $newlang='';
  788. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id'])) $newlang=$_REQUEST['lang_id'];
  789. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
  790. if (! empty($newlang))
  791. {
  792. $outputlangs = new Translate();
  793. $outputlangs->setDefaultLang($newlang);
  794. }
  795. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) commande_pdf_create($db, $object, $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  796. }
  797. }
  798. }
  799. // Go back to draft status
  800. else if ($action == 'confirm_modif' && $user->rights->commande->creer)
  801. {
  802. $idwarehouse=GETPOST('idwarehouse');
  803. // Check parameters
  804. if (! empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $object->hasProductsOrServices(1))
  805. {
  806. if (! $idwarehouse || $idwarehouse == -1)
  807. {
  808. $error++;
  809. $mesgs[]='<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Warehouse")).'</div>';
  810. $action='';
  811. }
  812. }
  813. if (! $error)
  814. {
  815. $result = $object->set_draft($user,$idwarehouse);
  816. if ($result >= 0)
  817. {
  818. // Define output language
  819. $outputlangs = $langs;
  820. $newlang='';
  821. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id'])) $newlang=$_REQUEST['lang_id'];
  822. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
  823. if (! empty($newlang))
  824. {
  825. $outputlangs = new Translate();
  826. $outputlangs->setDefaultLang($newlang);
  827. }
  828. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  829. {
  830. $ret=$object->fetch($object->id); // Reload to get new records
  831. commande_pdf_create($db, $object, $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  832. }
  833. }
  834. }
  835. }
  836. else if ($action == 'confirm_shipped' && $confirm == 'yes' && $user->rights->commande->cloturer)
  837. {
  838. $result = $object->cloture($user);
  839. if ($result < 0) $mesgs=$object->errors;
  840. }
  841. else if ($action == 'confirm_cancel' && $confirm == 'yes' && $user->rights->commande->valider)
  842. {
  843. $idwarehouse=GETPOST('idwarehouse');
  844. // Check parameters
  845. if (! empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $object->hasProductsOrServices(1))
  846. {
  847. if (! $idwarehouse || $idwarehouse == -1)
  848. {
  849. $error++;
  850. $mesgs[]='<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Warehouse")).'</div>';
  851. $action='';
  852. }
  853. }
  854. if (! $error)
  855. {
  856. $result = $object->cancel($idwarehouse);
  857. }
  858. }
  859. /*
  860. * Ordonnancement des lignes
  861. */
  862. else if ($action == 'up' && $user->rights->commande->creer)
  863. {
  864. $object->line_up(GETPOST('rowid'));
  865. // Define output language
  866. $outputlangs = $langs;
  867. $newlang='';
  868. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id'])) $newlang=$_REQUEST['lang_id'];
  869. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
  870. if (! empty($newlang))
  871. {
  872. $outputlangs = new Translate();
  873. $outputlangs->setDefaultLang($newlang);
  874. }
  875. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) commande_pdf_create($db, $object, $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  876. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.'#'.GETPOST('rowid'));
  877. exit;
  878. }
  879. else if ($action == 'down' && $user->rights->commande->creer)
  880. {
  881. $object->line_down(GETPOST('rowid'));
  882. // Define output language
  883. $outputlangs = $langs;
  884. $newlang='';
  885. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id'])) $newlang=$_REQUEST['lang_id'];
  886. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
  887. if (! empty($newlang))
  888. {
  889. $outputlangs = new Translate();
  890. $outputlangs->setDefaultLang($newlang);
  891. }
  892. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) commande_pdf_create($db, $object, $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  893. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.'#'.GETPOST('rowid'));
  894. exit;
  895. }
  896. else if ($action == 'builddoc') // In get or post
  897. {
  898. /*
  899. * Generate order document
  900. * define into /core/modules/commande/modules_commande.php
  901. */
  902. // Sauvegarde le dernier modele choisi pour generer un document
  903. if ($_REQUEST['model'])
  904. {
  905. $object->setDocModel($user, $_REQUEST['model']);
  906. }
  907. // Define output language
  908. $outputlangs = $langs;
  909. $newlang='';
  910. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id'])) $newlang=$_REQUEST['lang_id'];
  911. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
  912. if (! empty($newlang))
  913. {
  914. $outputlangs = new Translate();
  915. $outputlangs->setDefaultLang($newlang);
  916. }
  917. $result=commande_pdf_create($db, $object, $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  918. if ($result <= 0)
  919. {
  920. dol_print_error($db,$result);
  921. exit;
  922. }
  923. else
  924. {
  925. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.(empty($conf->global->MAIN_JUMP_TAG)?'':'#builddoc'));
  926. exit;
  927. }
  928. }
  929. // Remove file in doc form
  930. else if ($action == 'remove_file')
  931. {
  932. if ($object->id > 0)
  933. {
  934. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  935. $langs->load("other");
  936. $upload_dir = $conf->commande->dir_output;
  937. $file = $upload_dir . '/' . GETPOST('file');
  938. $ret=dol_delete_file($file,0,0,0,$object);
  939. if ($ret) setEventMessage($langs->trans("FileWasRemoved", GETPOST('urlfile')));
  940. else setEventMessage($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), 'errors');
  941. $action='';
  942. }
  943. }
  944. /*
  945. * Add file in email form
  946. */
  947. if (GETPOST('addfile'))
  948. {
  949. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  950. // Set tmp user directory TODO Use a dedicated directory for temp mails files
  951. $vardir=$conf->user->dir_output."/".$user->id;
  952. $upload_dir_tmp = $vardir.'/temp';
  953. dol_add_file_process($upload_dir_tmp,0,0);
  954. $action ='presend';
  955. }
  956. /*
  957. * Remove file in email form
  958. */
  959. if (GETPOST('removedfile'))
  960. {
  961. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  962. // Set tmp user directory
  963. $vardir=$conf->user->dir_output."/".$user->id;
  964. $upload_dir_tmp = $vardir.'/temp';
  965. // TODO Delete only files that was uploaded from email form
  966. dol_remove_file_process(GETPOST('removedfile'),0);
  967. $action ='presend';
  968. }
  969. /*
  970. * Send mail
  971. */
  972. if ($action == 'send' && ! GETPOST('addfile') && ! GETPOST('removedfile') && ! GETPOST('cancel'))
  973. {
  974. $langs->load('mails');
  975. if ($object->id > 0)
  976. {
  977. // $ref = dol_sanitizeFileName($object->ref);
  978. // $file = $conf->commande->dir_output . '/' . $ref . '/' . $ref . '.pdf';
  979. // if (is_readable($file))
  980. // {
  981. if (GETPOST('sendto'))
  982. {
  983. // Le destinataire a ete fourni via le champ libre
  984. $sendto = GETPOST('sendto');
  985. $sendtoid = 0;
  986. }
  987. elseif (GETPOST('receiver') != '-1')
  988. {
  989. // Recipient was provided from combo list
  990. if (GETPOST('receiver') == 'thirdparty') // Id of third party
  991. {
  992. $sendto = $object->client->email;
  993. $sendtoid = 0;
  994. }
  995. else // Id du contact
  996. {
  997. $sendto = $object->client->contact_get_property(GETPOST('receiver'),'email');
  998. $sendtoid = GETPOST('receiver');
  999. }
  1000. }
  1001. if (dol_strlen($sendto))
  1002. {
  1003. $langs->load("commercial");
  1004. $from = GETPOST('fromname') . ' <' . GETPOST('frommail') .'>';
  1005. $replyto = GETPOST('replytoname'). ' <' . GETPOST('replytomail').'>';
  1006. $message = GETPOST('message');
  1007. $sendtocc = GETPOST('sendtocc');
  1008. $deliveryreceipt = GETPOST('deliveryreceipt');
  1009. if ($action == 'send')
  1010. {
  1011. if (dol_strlen(GETPOST('subject'))) $subject=GETPOST('subject');
  1012. else $subject = $langs->transnoentities('Order').' '.$object->ref;
  1013. $actiontypecode='AC_COM';
  1014. $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto.".\n";
  1015. if ($message)
  1016. {
  1017. $actionmsg.=$langs->transnoentities('MailTopic').": ".$subject."\n";
  1018. $actionmsg.=$langs->transnoentities('TextUsedInTheMessageBody').":\n";
  1019. $actionmsg.=$message;
  1020. }
  1021. $actionmsg2=$langs->transnoentities('Action'.$actiontypecode);
  1022. }
  1023. // Create form object
  1024. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  1025. $formmail = new FormMail($db);
  1026. $attachedfiles=$formmail->get_attached_files();
  1027. $filepath = $attachedfiles['paths'];
  1028. $filename = $attachedfiles['names'];
  1029. $mimetype = $attachedfiles['mimes'];
  1030. // Send mail
  1031. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  1032. $mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,'',$deliveryreceipt);
  1033. if ($mailfile->error)
  1034. {
  1035. $mesg='<div class="error">'.$mailfile->error.'</div>';
  1036. }
  1037. else
  1038. {
  1039. $result=$mailfile->sendfile();
  1040. if ($result)
  1041. {
  1042. $mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($from,2),$mailfile->getValidAddress($sendto,2)); // Must not contains "
  1043. $error=0;
  1044. // Initialisation donnees
  1045. $object->sendtoid = $sendtoid;
  1046. $object->actiontypecode = $actiontypecode;
  1047. $object->actionmsg = $actionmsg;
  1048. $object->actionmsg2 = $actionmsg2;
  1049. $object->fk_element = $object->id;
  1050. $object->elementtype = $object->element;
  1051. // Appel des triggers
  1052. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  1053. $interface=new Interfaces($db);
  1054. $result=$interface->run_triggers('ORDER_SENTBYMAIL',$object,$user,$langs,$conf);
  1055. if ($result < 0) {
  1056. $error++; $this->errors=$interface->errors;
  1057. }
  1058. // Fin appel triggers
  1059. if ($error)
  1060. {
  1061. dol_print_error($db);
  1062. }
  1063. else
  1064. {
  1065. // Redirect here
  1066. // This avoid sending mail twice if going out and then back to page
  1067. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.'&mesg='.urlencode($mesg));
  1068. exit;
  1069. }
  1070. }
  1071. else
  1072. {
  1073. $langs->load("other");
  1074. $mesg='<div class="error">';
  1075. if ($mailfile->error)
  1076. {
  1077. $mesg.=$langs->trans('ErrorFailedToSendMail',$from,$sendto);
  1078. $mesg.='<br>'.$mailfile->error;
  1079. }
  1080. else
  1081. {
  1082. $mesg.='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
  1083. }
  1084. $mesg.='</div>';
  1085. }
  1086. }
  1087. /* }
  1088. else
  1089. {
  1090. $langs->load("other");
  1091. $mesg='<div class="error">'.$langs->trans('ErrorMailRecipientIsEmpty').' !</div>';
  1092. $action='presend';
  1093. dol_syslog('Recipient email is empty');
  1094. }*/
  1095. }
  1096. else
  1097. {
  1098. $langs->load("errors");
  1099. $mesg='<div class="error">'.$langs->trans('ErrorCantReadFile',$file).'</div>';
  1100. dol_syslog('Failed to read file: '.$file);
  1101. }
  1102. }
  1103. else
  1104. {
  1105. $langs->load("other");
  1106. $mesg='<div class="error">'.$langs->trans('ErrorFailedToReadEntity',$langs->trans("Order")).'</div>';
  1107. dol_syslog($langs->trans('ErrorFailedToReadEntity', $langs->trans("Order")));
  1108. }
  1109. }
  1110. if (! empty($conf->global->MAIN_DISABLE_CONTACTS_TAB) && $user->rights->commande->creer)
  1111. {
  1112. if ($action == 'addcontact')
  1113. {
  1114. if ($object->id > 0)
  1115. {
  1116. $contactid = (GETPOST('userid') ? GETPOST('userid') : GETPOST('contactid'));
  1117. $result = $object->add_contact($contactid, GETPOST('type'), GETPOST('source'));
  1118. }
  1119. if ($result >= 0)
  1120. {
  1121. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  1122. exit;
  1123. }
  1124. else
  1125. {
  1126. if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  1127. {
  1128. $langs->load("errors");
  1129. $mesg = '<div class="error">'.$langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType").'</div>';
  1130. }
  1131. else
  1132. {
  1133. $mesg = '<div class="error">'.$object->error.'</div>';
  1134. }
  1135. }
  1136. }
  1137. // bascule du statut d'un contact
  1138. else if ($action == 'swapstatut')
  1139. {
  1140. if ($object->id > 0)
  1141. {
  1142. $result=$object->swapContactStatus(GETPOST('ligne'));
  1143. }
  1144. else
  1145. {
  1146. dol_print_error($db);
  1147. }
  1148. }
  1149. // Efface un contact
  1150. else if ($action == 'deletecontact')
  1151. {
  1152. $result = $object->delete_contact($lineid);
  1153. if ($result >= 0)
  1154. {
  1155. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  1156. exit;
  1157. }
  1158. else {
  1159. dol_print_error($db);
  1160. }
  1161. }
  1162. }
  1163. /*
  1164. * View
  1165. */
  1166. llxHeader('',$langs->trans('Order'),'EN:Customers_Orders|FR:Commandes_Clients|ES:Pedidos de clientes');
  1167. $form = new Form($db);
  1168. $formfile = new FormFile($db);
  1169. $formorder = new FormOrder($db);
  1170. /*********************************************************************
  1171. *
  1172. * Mode creation
  1173. *
  1174. *********************************************************************/
  1175. if ($action == 'create' && $user->rights->commande->creer)
  1176. {
  1177. //WYSIWYG Editor
  1178. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1179. print_fiche_titre($langs->trans('CreateOrder'));
  1180. dol_htmloutput_mesg($mesg,$mesgs,'error');
  1181. $soc = new Societe($db);
  1182. if ($socid) $res=$soc->fetch($socid);
  1183. if (! empty($origin) && ! empty($originid))
  1184. {
  1185. // Parse element/subelement (ex: project_task)
  1186. $element = $subelement = $origin;
  1187. if (preg_match('/^([^_]+)_([^_]+)/i',$origin,$regs))
  1188. {
  1189. $element = $regs[1];
  1190. $subelement = $regs[2];
  1191. }
  1192. if ($element == 'project')
  1193. {
  1194. $projectid=$originid;
  1195. }
  1196. else
  1197. {
  1198. // For compatibility
  1199. if ($element == 'order' || $element == 'commande') {
  1200. $element = $subelement = 'commande';
  1201. }
  1202. if ($element == 'propal') {
  1203. $element = 'comm/propal'; $subelement = 'propal';
  1204. }
  1205. if ($element == 'contract') {
  1206. $element = $subelement = 'contrat';
  1207. }
  1208. dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
  1209. $classname = ucfirst($subelement);
  1210. $objectsrc = new $classname($db);
  1211. $objectsrc->fetch($originid);
  1212. if (empty($objectsrc->lines) && method_exists($objectsrc,'fetch_lines')) $objectsrc->fetch_lines();
  1213. $objectsrc->fetch_thirdparty();
  1214. $projectid = (!empty($objectsrc->fk_project)?$object->fk_project:'');
  1215. $ref_client = (!empty($objectsrc->ref_client)?$object->ref_client:'');
  1216. $soc = $objectsrc->client;
  1217. $cond_reglement_id = (!empty($objectsrc->cond_reglement_id)?$objectsrc->cond_reglement_id:(!empty($soc->cond_reglement_id)?$soc->cond_reglement_id:1));
  1218. $mode_reglement_id = (!empty($objectsrc->mode_reglement_id)?$objectsrc->mode_reglement_id:(!empty($soc->mode_reglement_id)?$soc->mode_reglement_id:0));
  1219. $availability_id = (!empty($objectsrc->availability_id)?$objectsrc->availability_id:(!empty($soc->availability_id)?$soc->availability_id:0));
  1220. $demand_reason_id = (!empty($objectsrc->demand_reason_id)?$objectsrc->demand_reason_id:(!empty($soc->demand_reason_id)?$soc->demand_reason_id:0));
  1221. $remise_percent = (!empty($objectsrc->remise_percent)?$objectsrc->remise_percent:(!empty($soc->remise_percent)?$soc->remise_percent:0));
  1222. $remise_absolue = (!empty($objectsrc->remise_absolue)?$objectsrc->remise_absolue:(!empty($soc->remise_absolue)?$soc->remise_absolue:0));
  1223. $dateinvoice = empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0;
  1224. $note_private = (! empty($objectsrc->note) ? $objectsrc->note : (! empty($objectsrc->note_private) ? $objectsrc->note_private : ''));
  1225. $note_public = (! empty($objectsrc->note_public) ? $objectsrc->note_public : '');
  1226. // Object source contacts list
  1227. $srccontactslist = $objectsrc->liste_contact(-1,'external',1);
  1228. }
  1229. }
  1230. else
  1231. {
  1232. $cond_reglement_id = $soc->cond_reglement_id;
  1233. $mode_reglement_id = $soc->mode_reglement_id;
  1234. $availability_id = $soc->availability_id;
  1235. $demand_reason_id = $soc->demand_reason_id;
  1236. $remise_percent = $soc->remise_percent;
  1237. $remise_absolue = 0;
  1238. $dateinvoice = empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0;
  1239. }
  1240. $absolute_discount=$soc->getAvailableDiscounts();
  1241. $nbrow=10;
  1242. print '<form name="crea_commande" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  1243. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1244. print '<input type="hidden" name="action" value="add">';
  1245. print '<input type="hidden" name="remise_percent" value="'.$soc->remise_client.'">';
  1246. print '<input type="hidden" name="origin" value="'.$origin.'">';
  1247. print '<input type="hidden" name="originid" value="'.$originid.'">';
  1248. print '<table class="border" width="100%">';
  1249. // Reference
  1250. print '<tr><td class="fieldrequired">'.$langs->trans('Ref').'</td><td colspan="2">'.$langs->trans("Draft").'</td></tr>';
  1251. // Reference client
  1252. print '<tr><td>'.$langs->trans('RefCustomer').'</td><td colspan="2">';
  1253. print '<input type="text" name="ref_client" value=""></td>';
  1254. print '</tr>';
  1255. // Client
  1256. print '<tr><td class="fieldrequired">'.$langs->trans('Customer').'</td><td colspan="2">'.$form->select_company($socid, "socid").'</td></tr>';
  1257. /*
  1258. * Contact de la commande
  1259. */
  1260. print "<tr><td>".$langs->trans("DefaultContact").'</td><td colspan="2">';
  1261. $form->select_contacts($soc->id,$setcontact,'contactidp',1,$srccontactslist);
  1262. print '</td></tr>';
  1263. // Ligne info remises tiers
  1264. print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="2">';
  1265. if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client);
  1266. else print $langs->trans("CompanyHasNoRelativeDiscount");
  1267. print '. ';
  1268. $absolute_discount=$soc->getAvailableDiscounts();
  1269. if ($absolute_discount) print $langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->trans("Currency".$conf->currency));
  1270. else print $langs->trans("CompanyHasNoAbsoluteDiscount");
  1271. print '.';
  1272. print '</td></tr>';
  1273. // Date
  1274. print '<tr><td class="fieldrequired">'.$langs->trans('Date').'</td><td colspan="2">';
  1275. $form->select_date('','re','','','',"crea_commande",1,1);
  1276. print '</td></tr>';
  1277. // Date de livraison
  1278. print "<tr><td>".$langs->trans("DeliveryDate").'</td><td colspan="2">';
  1279. if (! empty($conf->global->DATE_LIVRAISON_WEEK_DELAY))
  1280. {
  1281. $datedelivery = time() + ((7*$conf->global->DATE_LIVRAISON_WEEK_DELAY) * 24 * 60 * 60);
  1282. }
  1283. else
  1284. {
  1285. $datedelivery=empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0;
  1286. }
  1287. $form->select_date($datedelivery,'liv_','','','',"crea_commande",1,1);
  1288. print "</td></tr>";
  1289. // Conditions de reglement
  1290. print '<tr><td nowrap="nowrap">'.$langs->trans('PaymentConditionsShort').'</td><td colspan="2">';
  1291. // $form->select_conditions_paiements($soc->cond_reglement,'cond_reglement_id',-1,1);
  1292. print $object->select_fk_extrafields('cond_reglement_code', 'cond_reglement_code');
  1293. print '</td></tr>';
  1294. // Mode de reglement
  1295. print '<tr><td>'.$langs->trans('PaymentMode').'</td><td colspan="2">';
  1296. // $form->select_types_paiements($soc->mode_reglement,'mode_reglement_id');
  1297. print $object->select_fk_extrafields('mode_reglement_code', 'mode_reglement_code');
  1298. print '</td></tr>';
  1299. // Delivery delay
  1300. print '<tr><td>'.$langs->trans('AvailabilityPeriod').'</td><td colspan="2">';
  1301. // $form->select_availability($propal->availability,'availability_id','',1);
  1302. print $object->select_fk_extrafields('availability_code', 'availability_code');
  1303. print '</td></tr>';
  1304. // What trigger creation
  1305. print '<tr><td>'.$langs->trans('Source').'</td><td colspan="2">';
  1306. // $form->select_demand_reason(($origin=='propal'?'SRC_COMM':''),'demand_reason_id','',1);
  1307. print $object->select_fk_extrafields('demand_reason_code', 'demand_reason_code');
  1308. print '</td></tr>';
  1309. // Project
  1310. if (! empty($conf->projet->enabled))
  1311. {
  1312. $projectid = 0;
  1313. if ($origin == 'project') $projectid = ($originid?$originid:0);
  1314. print '<tr><td>'.$langs->trans('Project').'</td><td colspan="2">';
  1315. $numprojet=select_projects($soc->id,$projectid);
  1316. if ($numprojet==0)
  1317. {
  1318. print ' &nbsp; <a href="'.DOL_URL_ROOT.'/projet/fiche.php?socid='.$soc->id.'&action=create">'.$langs->trans("AddProject").'</a>';
  1319. }
  1320. print '</td></tr>';
  1321. }
  1322. // Other attributes
  1323. $parameters=array('objectsrc' => $objectsrc, 'colspan' => ' colspan="3"');
  1324. $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
  1325. if (empty($reshook) && ! empty($extrafields->attribute_label))
  1326. {
  1327. foreach($extrafields->attribute_label as $key=>$label)
  1328. {
  1329. $value=(isset($_POST["options_".$key])?$_POST["options_".$key]:$object->array_options["options_".$key]);
  1330. print "<tr><td>".$label.'</td><td colspan="3">';
  1331. print $extrafields->showInputField($key,$value);
  1332. print '</td></tr>'."\n";
  1333. }
  1334. }
  1335. // Template to use by default
  1336. print '<tr><td>'.$langs->trans('Model').'</td>';
  1337. print '<td colspan="2">';
  1338. include_once DOL_DOCUMENT_ROOT.'/commande/core/modules/commande/modules_commande.php';
  1339. $liste=ModelePDFCommandes::liste_modeles($db);
  1340. print $form->selectarray('model',$liste,$conf->global->COMMANDE_ADDON_PDF);
  1341. print "</td></tr>";
  1342. // Note publique
  1343. print '<tr>';
  1344. print '<td class="border" valign="top">'.$langs->trans('NotePublic').'</td>';
  1345. print '<td valign="top" colspan="2">';
  1346. $doleditor = new DolEditor('note_public', $note_public, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, 70);
  1347. print $doleditor->Create(1);
  1348. //print '<textarea name="note_public" wrap="soft" cols="70" rows="'.ROWS_3.'">'.$note_public.'</textarea>';
  1349. print '</td></tr>';
  1350. // Note privee
  1351. if (! $user->societe_id)
  1352. {
  1353. print '<tr>';
  1354. print '<td class="border" valign="top">'.$langs->trans('NotePrivate').'</td>';
  1355. print '<td valign="top" colspan="2">';
  1356. $doleditor=new DolEditor('note', $note_private, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, 70);
  1357. print $doleditor->Create(1);
  1358. //print '<textarea name="note" wrap="soft" cols="70" rows="'.ROWS_3.'">'.$note_private.'</textarea>';
  1359. print '</td></tr>';
  1360. }
  1361. if (! empty($origin) && ! empty($originid) && is_object($objectsrc))
  1362. {
  1363. // TODO for compatibility
  1364. if ($origin == 'contrat')
  1365. {
  1366. // Calcul contrat->price (HT), contrat->total (TTC), contrat->tva
  1367. $objectsrc->remise_absolue=$remise_absolue;
  1368. $objectsrc->remise_percent=$remise_percent;
  1369. $objectsrc->update_price(1);
  1370. }
  1371. print "\n<!-- ".$classname." info -->";
  1372. print "\n";
  1373. print '<input type="hidden" name="amount" value="'.$objectsrc->total_ht.'">'."\n";
  1374. print '<input type="hidden" name="total" value="'.$objectsrc->total_ttc.'">'."\n";
  1375. print '<input type="hidden" name="tva" value="'.$objectsrc->total_tva.'">'."\n";
  1376. print '<input type="hidden" name="origin" value="'.$objectsrc->element.'">';
  1377. print '<input type="hidden" name="originid" value="'.$objectsrc->id.'">';
  1378. $newclassname=$classname;
  1379. if ($newclassname=='Propal') $newclassname='CommercialProposal';
  1380. print '<tr><td>'.$langs->trans($newclassname).'</td><td colspan="2">'.$objectsrc->getNomUrl(1).'</td></tr>';
  1381. print '<tr><td>'.$langs->tra

Large files files are truncated, but you can click here to view the full file