PageRenderTime 65ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/fourn/facture/fiche.php

https://bitbucket.org/speedealing/speedealing
PHP | 2050 lines | 1511 code | 265 blank | 274 comment | 411 complexity | 115b42541377c57293f0d9e88edc0235 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
  5. * Copyright (C) 2005 Marc Barilley <marc@ocebo.fr>
  6. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  7. * Copyright (C) 2010-2012 Juanjo Menent <jmenent@2byte.es>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/fourn/facture/fiche.php
  24. * \ingroup facture, fournisseur
  25. * \brief Page for supplier invoice card (view, edit, validate)
  26. */
  27. require '../../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php';
  31. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/fourn.lib.php';
  34. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  35. if (! empty($conf->projet->enabled)) require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  36. $langs->load('bills');
  37. $langs->load('suppliers');
  38. $langs->load('companies');
  39. $mesg='';
  40. $errors=array();
  41. $id = (GETPOST('facid','int') ? GETPOST('facid','int') : GETPOST('id','int'));
  42. $action = GETPOST("action");
  43. $confirm = GETPOST("confirm");
  44. //PDF
  45. $hidedetails = (GETPOST('hidedetails','int') ? GETPOST('hidedetails','int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0));
  46. $hidedesc = (GETPOST('hidedesc','int') ? GETPOST('hidedesc','int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0));
  47. $hideref = (GETPOST('hideref','int') ? GETPOST('hideref','int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0));
  48. // Security check
  49. $socid='';
  50. if (! empty($user->societe_id)) $socid=$user->societe_id;
  51. $result = restrictedArea($user, 'fournisseur', $id, 'facture_fourn', 'facture');
  52. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  53. $hookmanager->initHooks(array('invoicesuppliercard'));
  54. $object=new FactureFournisseur($db);
  55. /*
  56. * Actions
  57. */
  58. // Action clone object
  59. if ($action == 'confirm_clone' && $confirm == 'yes')
  60. {
  61. if (1==0 && empty($_REQUEST["clone_content"]) && empty($_REQUEST["clone_receivers"]))
  62. {
  63. $mesg='<div class="error">'.$langs->trans("NoCloneOptionsSpecified").'</div>';
  64. }
  65. else
  66. {
  67. $result=$object->createFromClone($id);
  68. if ($result > 0)
  69. {
  70. header("Location: ".$_SERVER['PHP_SELF'].'?id='.$result);
  71. exit;
  72. }
  73. else
  74. {
  75. $langs->load("errors");
  76. $mesg='<div class="error">'.$langs->trans($object->error).'</div>';
  77. $action='';
  78. }
  79. }
  80. }
  81. elseif ($action == 'confirm_valid' && $confirm == 'yes' && $user->rights->fournisseur->facture->valider)
  82. {
  83. $idwarehouse=GETPOST('idwarehouse');
  84. $object->fetch($id);
  85. $object->fetch_thirdparty();
  86. // Check parameters
  87. if (! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL) && $object->hasProductsOrServices(1))
  88. {
  89. $langs->load("stocks");
  90. if (! $idwarehouse || $idwarehouse == -1)
  91. {
  92. $error++;
  93. $errors[]=$langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Warehouse"));
  94. $action='';
  95. }
  96. }
  97. if (! $error)
  98. {
  99. $result = $object->validate($user,'',$idwarehouse);
  100. if ($result < 0)
  101. {
  102. $mesg='<div class="error">'.$object->error.'</div>';
  103. }
  104. }
  105. }
  106. elseif ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->fournisseur->facture->supprimer)
  107. {
  108. $object->fetch($id);
  109. $object->fetch_thirdparty();
  110. $result=$object->delete($id);
  111. if ($result > 0)
  112. {
  113. header('Location: index.php');
  114. exit;
  115. }
  116. else
  117. {
  118. $mesg='<div class="error">'.$object->error.'</div>';
  119. }
  120. }
  121. elseif ($action == 'confirm_deleteline' && $confirm == 'yes' && $user->rights->fournisseur->facture->creer)
  122. {
  123. $object->fetch($id);
  124. $ret = $object->deleteline(GETPOST('lineid'));
  125. if ($ret > 0)
  126. {
  127. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$id);
  128. exit;
  129. }
  130. else
  131. {
  132. $mesg='<div class="error">'.$object->error.'</div>';
  133. }
  134. }
  135. elseif ($action == 'confirm_paid' && $confirm == 'yes' && $user->rights->fournisseur->facture->creer)
  136. {
  137. $object->fetch($id);
  138. $result=$object->set_paid($user);
  139. }
  140. // Set supplier ref
  141. elseif ($action == 'setfacnumber' && $user->rights->fournisseur->facture->creer)
  142. {
  143. $object->fetch($id);
  144. $result=$object->set_ref_supplier($user, GETPOST('facnumber'));
  145. }
  146. // Set label
  147. elseif ($action == 'setlabel' && $user->rights->fournisseur->facture->creer)
  148. {
  149. $object->fetch($id);
  150. $object->label=$_POST['label'];
  151. $result=$object->update($user);
  152. if ($result < 0) dol_print_error($db);
  153. }
  154. elseif ($action == 'setdatef' && $user->rights->fournisseur->facture->creer)
  155. {
  156. $object->fetch($id);
  157. $object->date=dol_mktime(12,0,0,$_POST['datefmonth'],$_POST['datefday'],$_POST['datefyear']);
  158. if ($object->date_echeance < $object->date) $object->date_echeance=$object->date;
  159. $result=$object->update($user);
  160. if ($result < 0) dol_print_error($db,$object->error);
  161. }
  162. elseif ($action == 'setdate_lim_reglement' && $user->rights->fournisseur->facture->creer)
  163. {
  164. $object->fetch($id);
  165. $object->date_echeance=dol_mktime(12,0,0,$_POST['date_lim_reglementmonth'],$_POST['date_lim_reglementday'],$_POST['date_lim_reglementyear']);
  166. if ($object->date_echeance < $object->date) $object->date_echeance=$object->date;
  167. $result=$object->update($user);
  168. if ($result < 0) dol_print_error($db,$object->error);
  169. }
  170. elseif ($action == 'setnote_public' && $user->rights->fournisseur->facture->creer)
  171. {
  172. $object->fetch($id);
  173. $result=$object->update_note_public(dol_html_entity_decode(GETPOST('note_public'), ENT_QUOTES));
  174. if ($result < 0) dol_print_error($db,$object->error);
  175. }
  176. elseif ($action == 'setnote' && $user->rights->fournisseur->facture->creer)
  177. {
  178. $object->fetch($id);
  179. $result=$object->update_note(dol_html_entity_decode(GETPOST('note'), ENT_QUOTES));
  180. if ($result < 0) dol_print_error($db,$object->error);
  181. }
  182. // Delete payment
  183. elseif ($action == 'deletepaiement')
  184. {
  185. $object->fetch($id);
  186. if ($object->statut == 1 && $object->paye == 0 && $user->societe_id == 0)
  187. {
  188. $paiementfourn = new PaiementFourn($db);
  189. $paiementfourn->fetch(GETPOST('paiement_id'));
  190. $result=$paiementfourn->delete();
  191. if ($result < 0) $mesg='<div class="error">'.$paiementfourn->error.'</div>';
  192. }
  193. }
  194. // Create
  195. elseif ($action == 'add' && $user->rights->fournisseur->facture->creer)
  196. {
  197. $error=0;
  198. $datefacture=dol_mktime(12,0,0,$_POST['remonth'],$_POST['reday'],$_POST['reyear']);
  199. $datedue=dol_mktime(12,0,0,$_POST['echmonth'],$_POST['echday'],$_POST['echyear']);
  200. if ($datefacture == '')
  201. {
  202. $mesg='<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('DateInvoice')).'</div>';
  203. $action='create';
  204. $_GET['socid']=$_POST['socid'];
  205. $error++;
  206. }
  207. if (! GETPOST('facnumber'))
  208. {
  209. $mesg='<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('RefSupplier')).'</div>';
  210. $action='create';
  211. $_GET['socid']=$_POST['socid'];
  212. $error++;
  213. }
  214. if (! $error)
  215. {
  216. $db->begin();
  217. // Creation facture
  218. $object->ref = $_POST['facnumber'];
  219. $object->socid = $_POST['socid'];
  220. $object->libelle = $_POST['libelle'];
  221. $object->date = $datefacture;
  222. $object->date_echeance = $datedue;
  223. $object->note_public = $_POST['note'];
  224. // If creation from another object of another module
  225. if ($_POST['origin'] && $_POST['originid'])
  226. {
  227. // Parse element/subelement (ex: project_task)
  228. $element = $subelement = $_POST['origin'];
  229. /*if (preg_match('/^([^_]+)_([^_]+)/i',$_POST['origin'],$regs))
  230. {
  231. $element = $regs[1];
  232. $subelement = $regs[2];
  233. }*/
  234. // For compatibility
  235. if ($element == 'order') {
  236. $element = $subelement = 'commande';
  237. }
  238. if ($element == 'propal') {
  239. $element = 'comm/propal'; $subelement = 'propal';
  240. }
  241. if ($element == 'contract') {
  242. $element = $subelement = 'contrat';
  243. }
  244. if ($element == 'order_supplier') {
  245. $element = 'fourn'; $subelement = 'fournisseur.commande';
  246. }
  247. $object->origin = $_POST['origin'];
  248. $object->origin_id = $_POST['originid'];
  249. $id = $object->create($user);
  250. // Add lines
  251. if ($id > 0)
  252. {
  253. require_once DOL_DOCUMENT_ROOT.'/'.$element.'/class/'.$subelement.'.class.php';
  254. $classname = ucfirst($subelement);
  255. if ($classname == 'Fournisseur.commande') $classname='CommandeFournisseur';
  256. $srcobject = new $classname($db);
  257. $result=$srcobject->fetch($_POST['originid']);
  258. if ($result > 0)
  259. {
  260. $lines = $srcobject->lines;
  261. if (empty($lines) && method_exists($srcobject,'fetch_lines')) $lines = $srcobject->fetch_lines();
  262. $num=count($lines);
  263. for ($i = 0; $i < $num; $i++)
  264. {
  265. $desc=($lines[$i]->desc?$lines[$i]->desc:$lines[$i]->libelle);
  266. $product_type=($lines[$i]->product_type?$lines[$i]->product_type:0);
  267. // Dates
  268. // TODO mutualiser
  269. $date_start=$lines[$i]->date_debut_prevue;
  270. if ($lines[$i]->date_debut_reel) $date_start=$lines[$i]->date_debut_reel;
  271. if ($lines[$i]->date_start) $date_start=$lines[$i]->date_start;
  272. $date_end=$lines[$i]->date_fin_prevue;
  273. if ($lines[$i]->date_fin_reel) $date_end=$lines[$i]->date_fin_reel;
  274. if ($lines[$i]->date_end) $date_end=$lines[$i]->date_end;
  275. $result = $object->addline(
  276. $desc,
  277. $lines[$i]->subprice,
  278. $lines[$i]->tva_tx,
  279. $lines[$i]->localtax1_tx,
  280. $lines[$i]->localtax2_tx,
  281. $lines[$i]->qty,
  282. $lines[$i]->fk_product,
  283. $lines[$i]->remise_percent,
  284. $date_start,
  285. $date_end,
  286. 0,
  287. $lines[$i]->info_bits,
  288. 'HT',
  289. $product_type
  290. );
  291. if ($result < 0)
  292. {
  293. $error++;
  294. break;
  295. }
  296. }
  297. }
  298. else
  299. {
  300. $error++;
  301. }
  302. }
  303. else
  304. {
  305. $error++;
  306. }
  307. }
  308. // If some invoice's lines already known
  309. else
  310. {
  311. $id = $object->create($user);
  312. if ($id < 0)
  313. {
  314. $error++;
  315. }
  316. if (! $error)
  317. {
  318. for ($i = 1 ; $i < 9 ; $i++)
  319. {
  320. $label = $_POST['label'.$i];
  321. $amountht = price2num($_POST['amount'.$i]);
  322. $amountttc = price2num($_POST['amountttc'.$i]);
  323. $tauxtva = price2num($_POST['tauxtva'.$i]);
  324. $qty = $_POST['qty'.$i];
  325. $fk_product = $_POST['fk_product'.$i];
  326. if ($label)
  327. {
  328. if ($amountht)
  329. {
  330. $price_base='HT'; $amount=$amountht;
  331. }
  332. else
  333. {
  334. $price_base='TTC'; $amount=$amountttc;
  335. }
  336. $atleastoneline=1;
  337. $product=new Product($db);
  338. $product->fetch($_POST['idprod'.$i]);
  339. $ret=$object->addline($label, $amount, $tauxtva, $product->localtax1_tx, $product->localtax2_tx, $qty, $fk_product, $remise_percent, '', '', '', 0, $price_base);
  340. if ($ret < 0) $error++;
  341. }
  342. }
  343. }
  344. }
  345. if ($error)
  346. {
  347. $langs->load("errors");
  348. $db->rollback();
  349. $mesg='<div class="error">'.$langs->trans($object->error).'</div>';
  350. $action='create';
  351. $_GET['socid']=$_POST['socid'];
  352. }
  353. else
  354. {
  355. $db->commit();
  356. header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
  357. exit;
  358. }
  359. }
  360. }
  361. // Modification d'une ligne
  362. elseif ($action == 'update_line')
  363. {
  364. if (GETPOST('etat') == '1' && ! GETPOST('cancel')) // si on valide la modification
  365. {
  366. $object->fetch($id);
  367. $object->fetch_thirdparty();
  368. if ($_POST['puht'])
  369. {
  370. $pu=$_POST['puht'];
  371. $price_base_type='HT';
  372. }
  373. if ($_POST['puttc'])
  374. {
  375. $pu=$_POST['puttc'];
  376. $price_base_type='TTC';
  377. }
  378. if (GETPOST('idprod'))
  379. {
  380. $prod = new Product($db);
  381. $prod->fetch($_POST['idprod']);
  382. $label = $prod->description;
  383. if (trim($_POST['desc']) != trim($label)) $label=$_POST['desc'];
  384. $type = $prod->type;
  385. }
  386. else
  387. {
  388. $label = $_POST['desc'];
  389. $type = $_POST["type"]?$_POST["type"]:0;
  390. }
  391. $localtax1tx= get_localtax($_POST['tauxtva'], 1, $object->thirdparty);
  392. $localtax2tx= get_localtax($_POST['tauxtva'], 2, $object->thirdparty);
  393. $remise_percent=GETPOST('remise_percent');
  394. $result=$object->updateline(GETPOST('lineid'), $label, $pu, GETPOST('tauxtva'), $localtax1tx, $localtax2tx, GETPOST('qty'), GETPOST('idprod'), $price_base_type, 0, $type, $remise_percent);
  395. if ($result >= 0)
  396. {
  397. unset($_POST['label']);
  398. }
  399. }
  400. }
  401. elseif ($action == 'addline')
  402. {
  403. $ret=$object->fetch($id);
  404. if ($ret < 0)
  405. {
  406. dol_print_error($db,$object->error);
  407. exit;
  408. }
  409. $ret=$object->fetch_thirdparty();
  410. if ($_POST['idprodfournprice']) // > 0 or -1
  411. {
  412. $product=new Product($db);
  413. $idprod=$product->get_buyprice($_POST['idprodfournprice'], $_POST['qty']); // Just to see if a price exists for the quantity. Not used to found vat
  414. if ($idprod > 0)
  415. {
  416. $result=$product->fetch($idprod);
  417. // cas special pour lequel on a les meme reference que le fournisseur
  418. // $label = '['.$product->ref.'] - '. $product->libelle;
  419. $label = $product->description;
  420. $label.= $product->description && $_POST['np_desc'] ? "\n" : "";
  421. $label.= $_POST['np_desc'];
  422. $tvatx=get_default_tva($object->thirdparty, $mysoc, $product->id, $_POST['idprodfournprice']);
  423. $localtax1tx= get_localtax($tvatx, 1, $object->thirdparty);
  424. $localtax2tx= get_localtax($tvatx, 2, $object->thirdparty);
  425. $remise_percent=GETPOST('remise_percent');
  426. $type = $product->type;
  427. $result=$object->addline($label, $product->fourn_pu, $tvatx, $localtax1tx, $localtax2tx, $_POST['qty'], $idprod, $remise_percent);
  428. }
  429. if ($idprod == -1)
  430. {
  431. // Quantity too low
  432. $langs->load("errors");
  433. $mesg='<div class="error">'.$langs->trans("ErrorQtyTooLowForThisSupplier").'</div>';
  434. }
  435. }
  436. else
  437. {
  438. $tauxtva = price2num($_POST['tauxtva']);
  439. $localtax1tx= get_localtax($tauxtva, 1, $object->thirdparty);
  440. $localtax2tx= get_localtax($tauxtva, 2, $object->thirdparty);
  441. $remise_percent=GETPOST('remise_percent');
  442. if (! $_POST['dp_desc'])
  443. {
  444. $mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Description")).'</div>';
  445. }
  446. else
  447. {
  448. $type = $_POST["type"];
  449. if (! empty($_POST['amount']))
  450. {
  451. $ht = price2num($_POST['amount']);
  452. $price_base_type = 'HT';
  453. //$desc, $pu, $txtva, $qty, $fk_product=0, $remise_percent=0, $date_start='', $date_end='', $ventil=0, $info_bits='', $price_base_type='HT', $type=0)
  454. $result=$object->addline($_POST['dp_desc'], $ht, $tauxtva, $localtax1tx, $localtax2tx, $_POST['qty'], 0, $remise_percent, $datestart, $dateend, 0, 0, $price_base_type, $type);
  455. }
  456. else
  457. {
  458. $ttc = price2num($_POST['amountttc']);
  459. $ht = $ttc / (1 + ($tauxtva / 100));
  460. $price_base_type = 'HT';
  461. $result=$object->addline($_POST['dp_desc'], $ht, $tauxtva,$localtax1tx, $localtax2tx, $_POST['qty'], 0, $remise_percent, $datestart, $dateend, 0, 0, $price_base_type, $type);
  462. }
  463. }
  464. }
  465. //print "xx".$tva_tx; exit;
  466. if ($result > 0)
  467. {
  468. // Define output language
  469. $outputlangs = $langs;
  470. $newlang=GETPOST('lang_id','alpha');
  471. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
  472. if (! empty($newlang))
  473. {
  474. $outputlangs = new Translate();
  475. $outputlangs->setDefaultLang($newlang);
  476. }
  477. //if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) supplier_invoice_pdf_create($db, $object->id, $object->modelpdf, $outputlangs);
  478. unset($_POST['qty']);
  479. unset($_POST['type']);
  480. unset($_POST['idprodfournprice']);
  481. unset($_POST['remise_percent']);
  482. unset($_POST['dp_desc']);
  483. unset($_POST['np_desc']);
  484. unset($_POST['pu']);
  485. unset($_POST['tva_tx']);
  486. unset($_POST['label']);
  487. unset($localtax1_tx);
  488. unset($localtax2_tx);
  489. }
  490. else if (empty($mesg))
  491. {
  492. $mesg='<div class="error">'.$object->error.'</div>';
  493. }
  494. $action = '';
  495. }
  496. elseif ($action == 'classin')
  497. {
  498. $object->fetch($id);
  499. $result=$object->setProject($_POST['projectid']);
  500. }
  501. // Set invoice to draft status
  502. elseif ($action == 'edit' && $user->rights->fournisseur->facture->creer)
  503. {
  504. $object->fetch($id);
  505. $totalpaye = $object->getSommePaiement();
  506. $resteapayer = $object->total_ttc - $totalpaye;
  507. // On verifie si les lignes de factures ont ete exportees en compta et/ou ventilees
  508. //$ventilExportCompta = $object->getVentilExportCompta();
  509. // On verifie si aucun paiement n'a ete effectue
  510. if ($resteapayer == $object->total_ttc && $object->paye == 0 && $ventilExportCompta == 0)
  511. {
  512. $object->set_draft($user);
  513. $outputlangs = $langs;
  514. if (! empty($_REQUEST['lang_id']))
  515. {
  516. $outputlangs = new Translate();
  517. $outputlangs->setDefaultLang($_REQUEST['lang_id']);
  518. }
  519. //if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) supplier_invoice_pdf_create($db, $object->id, $object->modelpdf, $outputlangs);
  520. }
  521. }
  522. // Set invoice to validated/unpaid status
  523. elseif ($action == 'reopen' && $user->rights->fournisseur->facture->creer)
  524. {
  525. $result = $object->fetch($id);
  526. if ($object->statut == 2
  527. || ($object->statut == 3 && $object->close_code != 'replaced'))
  528. {
  529. $result = $object->set_unpaid($user);
  530. if ($result > 0)
  531. {
  532. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$id);
  533. exit;
  534. }
  535. else
  536. {
  537. $mesg='<div class="error">'.$object->error.'</div>';
  538. }
  539. }
  540. }
  541. // Add file in email form
  542. if (GETPOST('addfile'))
  543. {
  544. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  545. // Set tmp user directory TODO Use a dedicated directory for temp mails files
  546. $vardir=$conf->user->dir_output."/".$user->id;
  547. $upload_dir_tmp = $vardir.'/temp';
  548. dol_add_file_process($upload_dir_tmp,0,0);
  549. $action='presend';
  550. }
  551. // Remove file in email form
  552. if (! empty($_POST['removedfile']))
  553. {
  554. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  555. // Set tmp user directory
  556. $vardir=$conf->user->dir_output."/".$user->id;
  557. $upload_dir_tmp = $vardir.'/temp';
  558. // TODO Delete only files that was uploaded from email form
  559. dol_remove_file_process($_POST['removedfile'],0);
  560. $action='presend';
  561. }
  562. // Send mail
  563. if ($action == 'send' && ! $_POST['addfile'] && ! $_POST['removedfile'] && ! $_POST['cancel'])
  564. {
  565. $langs->load('mails');
  566. $object->fetch($id);
  567. $result=$object->fetch_thirdparty();
  568. if ($result > 0)
  569. {
  570. // $ref = dol_sanitizeFileName($object->ref);
  571. // $file = $conf->fournisseur->facture->dir_output.'/'.get_exdir($object->id,2).$ref.'/'.$ref.'.pdf';
  572. // if (is_readable($file))
  573. // {
  574. if ($_POST['sendto'])
  575. {
  576. // Le destinataire a ete fourni via le champ libre
  577. $sendto = $_POST['sendto'];
  578. $sendtoid = 0;
  579. }
  580. elseif ($_POST['receiver'] != '-1')
  581. {
  582. // Recipient was provided from combo list
  583. if ($_POST['receiver'] == 'thirdparty') // Id of third party
  584. {
  585. $sendto = $object->client->email;
  586. $sendtoid = 0;
  587. }
  588. else // Id du contact
  589. {
  590. $sendto = $object->client->contact_get_property($_POST['receiver'],'email');
  591. $sendtoid = $_POST['receiver'];
  592. }
  593. }
  594. if (dol_strlen($sendto))
  595. {
  596. $langs->load("commercial");
  597. $from = $_POST['fromname'] . ' <' . $_POST['frommail'] .'>';
  598. $replyto = $_POST['replytoname']. ' <' . $_POST['replytomail'].'>';
  599. $message = $_POST['message'];
  600. $sendtocc = $_POST['sendtocc'];
  601. $deliveryreceipt = $_POST['deliveryreceipt'];
  602. if ($action == 'send')
  603. {
  604. if (dol_strlen($_POST['subject'])) $subject=$_POST['subject'];
  605. else $subject = $langs->transnoentities('CustomerOrder').' '.$object->ref;
  606. $actiontypecode='AC_SUP_ORD';
  607. $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto.".\n";
  608. if ($message)
  609. {
  610. $actionmsg.=$langs->transnoentities('MailTopic').": ".$subject."\n";
  611. $actionmsg.=$langs->transnoentities('TextUsedInTheMessageBody').":\n";
  612. $actionmsg.=$message;
  613. }
  614. $actionmsg2=$langs->transnoentities('Action'.$actiontypecode);
  615. }
  616. // Create form object
  617. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  618. $formmail = new FormMail($db);
  619. $attachedfiles=$formmail->get_attached_files();
  620. $filepath = $attachedfiles['paths'];
  621. $filename = $attachedfiles['names'];
  622. $mimetype = $attachedfiles['mimes'];
  623. // Send mail
  624. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  625. $mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,'',$deliveryreceipt);
  626. if ($mailfile->error)
  627. {
  628. $mesg='<div class="error">'.$mailfile->error.'</div>';
  629. }
  630. else
  631. {
  632. $result=$mailfile->sendfile();
  633. if ($result)
  634. {
  635. $mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($from,2),$mailfile->getValidAddress($sendto,2)); // Must not contain "
  636. $error=0;
  637. // Initialisation donnees
  638. $object->sendtoid = $sendtoid;
  639. $object->actiontypecode = $actiontypecode;
  640. $object->actionmsg = $actionmsg;
  641. $object->actionmsg2 = $actionmsg2;
  642. $object->fk_element = $object->id;
  643. $object->elementtype = $object->element;
  644. // Appel des triggers
  645. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  646. $interface=new Interfaces($db);
  647. $result=$interface->run_triggers('BILL_SUPPLIER_SENTBYMAIL',$object,$user,$langs,$conf);
  648. if ($result < 0) {
  649. $error++; $this->errors=$interface->errors;
  650. }
  651. // Fin appel triggers
  652. if ($error)
  653. {
  654. dol_print_error($db);
  655. }
  656. else
  657. {
  658. // Redirect here
  659. // This avoid sending mail twice if going out and then back to page
  660. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.'&mesg='.urlencode($mesg));
  661. exit;
  662. }
  663. }
  664. else
  665. {
  666. $langs->load("other");
  667. $mesg='<div class="error">';
  668. if ($mailfile->error)
  669. {
  670. $mesg.=$langs->trans('ErrorFailedToSendMail',$from,$sendto);
  671. $mesg.='<br>'.$mailfile->error;
  672. }
  673. else
  674. {
  675. $mesg.='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
  676. }
  677. $mesg.='</div>';
  678. }
  679. }
  680. }
  681. else
  682. {
  683. $langs->load("other");
  684. $mesg='<div class="error">'.$langs->trans('ErrorMailRecipientIsEmpty').'</div>';
  685. dol_syslog('Recipient email is empty');
  686. }
  687. /* }
  688. else
  689. {
  690. $langs->load("errors");
  691. $mesg='<div class="error">'.$langs->trans('ErrorCantReadFile',$file).'</div>';
  692. dol_syslog('Failed to read file: '.$file);
  693. }*/
  694. }
  695. else
  696. {
  697. $langs->load("other");
  698. $mesg='<div class="error">'.$langs->trans('ErrorFailedToReadEntity',$langs->trans("Invoice")).'</div>';
  699. dol_syslog('Unable to read data from the invoice. The invoice file has perhaps not been generated.');
  700. }
  701. //$action = 'presend';
  702. }
  703. // Build document
  704. elseif ($action == 'builddoc')
  705. {
  706. // Save modele used
  707. $object->fetch($id);
  708. $object->fetch_thirdparty();
  709. if ($_REQUEST['model'])
  710. {
  711. $object->setDocModel($user, $_REQUEST['model']);
  712. }
  713. $outputlangs = $langs;
  714. if (! empty($_REQUEST['lang_id']))
  715. {
  716. $outputlangs = new Translate();
  717. $outputlangs->setDefaultLang($_REQUEST['lang_id']);
  718. }
  719. $result=supplier_invoice_pdf_create($db,$object,$object->modelpdf,$outputlangs);
  720. if ($result <= 0)
  721. {
  722. dol_print_error($db,$result);
  723. exit;
  724. }
  725. else
  726. {
  727. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.(empty($conf->global->MAIN_JUMP_TAG)?'':'#builddoc'));
  728. exit;
  729. }
  730. }
  731. // Delete file in doc form
  732. elseif ($action == 'remove_file')
  733. {
  734. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  735. if ($object->fetch($id))
  736. {
  737. $object->fetch_thirdparty();
  738. $upload_dir = $conf->fournisseur->facture->dir_output . "/";
  739. $file = $upload_dir . '/' . GETPOST('file');
  740. $ret=dol_delete_file($file,0,0,0,$object);
  741. if ($ret) setEventMessage($langs->trans("FileWasRemoved", GETPOST('urlfile')));
  742. else setEventMessage($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), 'errors');
  743. }
  744. }
  745. if (! empty($conf->global->MAIN_DISABLE_CONTACTS_TAB) && $user->rights->fournisseur->facture->creer)
  746. {
  747. if ($action == 'addcontact')
  748. {
  749. $result = $object->fetch($id);
  750. if ($result > 0 && $id > 0)
  751. {
  752. $contactid = (GETPOST('userid') ? GETPOST('userid') : GETPOST('contactid'));
  753. $result = $result = $object->add_contact($contactid, $_POST["type"], $_POST["source"]);
  754. }
  755. if ($result >= 0)
  756. {
  757. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  758. exit;
  759. }
  760. else
  761. {
  762. if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  763. {
  764. $langs->load("errors");
  765. $mesg = '<div class="error">'.$langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType").'</div>';
  766. }
  767. else
  768. {
  769. $mesg = '<div class="error">'.$object->error.'</div>';
  770. }
  771. }
  772. }
  773. // bascule du statut d'un contact
  774. else if ($action == 'swapstatut')
  775. {
  776. if ($object->fetch($id))
  777. {
  778. $result=$object->swapContactStatus(GETPOST('ligne'));
  779. }
  780. else
  781. {
  782. dol_print_error($db);
  783. }
  784. }
  785. // Efface un contact
  786. else if ($action == 'deletecontact')
  787. {
  788. $object->fetch($id);
  789. $result = $object->delete_contact($_GET["lineid"]);
  790. if ($result >= 0)
  791. {
  792. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  793. exit;
  794. }
  795. else {
  796. dol_print_error($db);
  797. }
  798. }
  799. }
  800. /*
  801. * View
  802. */
  803. $form = new Form($db);
  804. $formfile = new FormFile($db);
  805. $bankaccountstatic=new Account($db);
  806. llxHeader('','','');
  807. // Mode creation
  808. if ($action == 'create')
  809. {
  810. print_fiche_titre($langs->trans('NewBill'));
  811. dol_htmloutput_mesg($mesg);
  812. $societe='';
  813. if ($_GET['socid'])
  814. {
  815. $societe=new Societe($db);
  816. $societe->fetch($_GET['socid']);
  817. }
  818. if (GETPOST('origin') && GETPOST('originid'))
  819. {
  820. // Parse element/subelement (ex: project_task)
  821. $element = $subelement = GETPOST('origin');
  822. if ($element == 'project')
  823. {
  824. $projectid=GETPOST('originid');
  825. }
  826. else if (in_array($element,array('order_supplier')))
  827. {
  828. // For compatibility
  829. if ($element == 'order') {
  830. $element = $subelement = 'commande';
  831. }
  832. if ($element == 'propal') {
  833. dol_htmloutput_errors('',$errors);
  834. $element = 'comm/propal'; $subelement = 'propal';
  835. }
  836. if ($element == 'contract') {
  837. $element = $subelement = 'contrat';
  838. }
  839. if ($element == 'order_supplier') {
  840. $element = 'fourn'; $subelement = 'fournisseur.commande';
  841. }
  842. require_once DOL_DOCUMENT_ROOT.'/'.$element.'/class/'.$subelement.'.class.php';
  843. $classname = ucfirst($subelement);
  844. if ($classname == 'Fournisseur.commande') $classname='CommandeFournisseur';
  845. $objectsrc = new $classname($db);
  846. $objectsrc->fetch(GETPOST('originid'));
  847. $objectsrc->fetch_thirdparty();
  848. $projectid = (!empty($objectsrc->fk_project)?$object->fk_project:'');
  849. //$ref_client = (!empty($objectsrc->ref_client)?$object->ref_client:'');
  850. $soc = $objectsrc->client;
  851. $cond_reglement_id = (!empty($objectsrc->cond_reglement_id)?$objectsrc->cond_reglement_id:(!empty($soc->cond_reglement_id)?$soc->cond_reglement_id:1));
  852. $mode_reglement_id = (!empty($objectsrc->mode_reglement_id)?$objectsrc->mode_reglement_id:(!empty($soc->mode_reglement_id)?$soc->mode_reglement_id:0));
  853. $remise_percent = (!empty($objectsrc->remise_percent)?$objectsrc->remise_percent:(!empty($soc->remise_percent)?$soc->remise_percent:0));
  854. $remise_absolue = (!empty($objectsrc->remise_absolue)?$objectsrc->remise_absolue:(!empty($soc->remise_absolue)?$soc->remise_absolue:0));
  855. $dateinvoice = empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0;
  856. $datetmp=dol_mktime(12,0,0,$_POST['remonth'],$_POST['reday'],$_POST['reyear']);
  857. $dateinvoice=($datetmp==''?(empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0):$datetmp);
  858. $datetmp=dol_mktime(12,0,0,$_POST['echmonth'],$_POST['echday'],$_POST['echyear']);
  859. $datedue=($datetmp==''?-1:$datetmp);
  860. }
  861. }
  862. else
  863. {
  864. $datetmp=dol_mktime(12,0,0,$_POST['remonth'],$_POST['reday'],$_POST['reyear']);
  865. $dateinvoice=($datetmp==''?(empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0):$datetmp);
  866. $datetmp=dol_mktime(12,0,0,$_POST['echmonth'],$_POST['echday'],$_POST['echyear']);
  867. $datedue=($datetmp==''?-1:$datetmp);
  868. }
  869. print '<form name="add" action="'.$_SERVER["PHP_SELF"].'" method="post">';
  870. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  871. print '<input type="hidden" name="action" value="add">';
  872. print '<input type="hidden" name="origin" value="'.GETPOST('origin').'">';
  873. print '<input type="hidden" name="originid" value="'.GETPOST('originid').'">';
  874. print '<table class="border" width="100%">';
  875. // Ref
  876. print '<tr><td>'.$langs->trans('Ref').'</td><td>'.$langs->trans('Draft').'</td></tr>';
  877. // Third party
  878. print '<tr><td class="fieldrequired">'.$langs->trans('Supplier').'</td>';
  879. print '<td>';
  880. if ($_REQUEST['socid'] > 0)
  881. {
  882. print $societe->getNomUrl(1);
  883. print '<input type="hidden" name="socid" value="'.$_GET['socid'].'">';
  884. }
  885. else
  886. {
  887. print $form->select_company((empty($_GET['socid'])?'':$_GET['socid']),'socid','s.fournisseur = 1',1);
  888. }
  889. print '</td>';
  890. // Ref supplier
  891. print '<tr><td class="fieldrequired">'.$langs->trans('RefSupplier').'</td><td><input name="facnumber" value="'.(isset($_POST['facnumber'])?$_POST['facnumber']:$fac_ori->ref).'" type="text"></td>';
  892. print '</tr>';
  893. print '<tr><td valign="top" class="fieldrequired">'.$langs->trans('Type').'</td><td colspan="2">';
  894. print '<table class="nobordernopadding">'."\n";
  895. // Standard invoice
  896. print '<tr height="18"><td width="16px" valign="middle">';
  897. print '<input type="radio" name="type" value="0"'.($_POST['type']==0?' checked="checked"':'').'>';
  898. print '</td><td valign="middle">';
  899. $desc=$form->textwithpicto($langs->trans("InvoiceStandardAsk"),$langs->transnoentities("InvoiceStandardDesc"),1);
  900. print $desc;
  901. print '</td></tr>'."\n";
  902. /*
  903. // Deposit
  904. print '<tr height="18"><td width="16px" valign="middle">';
  905. print '<input type="radio" name="type" value="3"'.($_POST['type']==3?' checked="checked"':'').'>';
  906. print '</td><td valign="middle">';
  907. $desc=$form->textwithpicto($langs->trans("InvoiceDeposit"),$langs->transnoentities("InvoiceDepositDesc"),1);
  908. print $desc;
  909. print '</td></tr>'."\n";
  910. // Proforma
  911. if (! empty($conf->global->FACTURE_USE_PROFORMAT))
  912. {
  913. print '<tr height="18"><td width="16px" valign="middle">';
  914. print '<input type="radio" name="type" value="4"'.($_POST['type']==4?' checked="checked"':'').'>';
  915. print '</td><td valign="middle">';
  916. $desc=$form->textwithpicto($langs->trans("InvoiceProForma"),$langs->transnoentities("InvoiceProFormaDesc"),1);
  917. print $desc;
  918. print '</td></tr>'."\n";
  919. }
  920. // Replacement
  921. print '<tr height="18"><td valign="middle">';
  922. print '<input type="radio" name="type" value="1"'.($_POST['type']==1?' checked="checked"':'');
  923. if (! $options) print ' disabled="disabled"';
  924. print '>';
  925. print '</td><td valign="middle">';
  926. $text=$langs->trans("InvoiceReplacementAsk").' ';
  927. $text.='<select class="flat" name="fac_replacement"';
  928. if (! $options) $text.=' disabled="disabled"';
  929. $text.='>';
  930. if ($options)
  931. {
  932. $text.='<option value="-1">&nbsp;</option>';
  933. $text.=$options;
  934. }
  935. else
  936. {
  937. $text.='<option value="-1">'.$langs->trans("NoReplacableInvoice").'</option>';
  938. }
  939. $text.='</select>';
  940. $desc=$form->textwithpicto($text,$langs->transnoentities("InvoiceReplacementDesc"),1);
  941. print $desc;
  942. print '</td></tr>';
  943. // Credit note
  944. print '<tr height="18"><td valign="middle">';
  945. print '<input type="radio" name="type" value="2"'.($_POST['type']==2?' checked=true':'');
  946. if (! $optionsav) print ' disabled="disabled"';
  947. print '>';
  948. print '</td><td valign="middle">';
  949. $text=$langs->transnoentities("InvoiceAvoirAsk").' ';
  950. // $text.='<input type="text" value="">';
  951. $text.='<select class="flat" name="fac_avoir"';
  952. if (! $optionsav) $text.=' disabled="disabled"';
  953. $text.='>';
  954. if ($optionsav)
  955. {
  956. $text.='<option value="-1">&nbsp;</option>';
  957. $text.=$optionsav;
  958. }
  959. else
  960. {
  961. $text.='<option value="-1">'.$langs->trans("NoInvoiceToCorrect").'</option>';
  962. }
  963. $text.='</select>';
  964. $desc=$form->textwithpicto($text,$langs->transnoentities("InvoiceAvoirDesc"),1);
  965. print $desc;
  966. print '</td></tr>'."\n";
  967. */
  968. print '</table>';
  969. print '</td></tr>';
  970. // Label
  971. print '<tr><td>'.$langs->trans('Label').'</td><td><input size="30" name="libelle" value="'.(isset($_POST['libelle'])?$_POST['libelle']:$fac_ori->libelle).'" type="text"></td></tr>';
  972. // Date invoice
  973. print '<tr><td class="fieldrequired">'.$langs->trans('DateInvoice').'</td><td>';
  974. $form->select_date($dateinvoice,'','','','',"add",1,1);
  975. print '</td></tr>';
  976. // Due date
  977. print '<tr><td>'.$langs->trans('DateMaxPayment').'</td><td>';
  978. $form->select_date($datedue,'ech','','','',"add",1,1);
  979. print '</td></tr>';
  980. print '<tr><td>'.$langs->trans('NotePublic').'</td>';
  981. print '<td><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_5.'"></textarea></td>';
  982. print '</tr>';
  983. if (is_object($objectsrc))
  984. {
  985. print "\n<!-- ".$classname." info -->";
  986. print "\n";
  987. print '<input type="hidden" name="amount" value="'.$objectsrc->total_ht.'">'."\n";
  988. print '<input type="hidden" name="total" value="'.$objectsrc->total_ttc.'">'."\n";
  989. print '<input type="hidden" name="tva" value="'.$objectsrc->total_tva.'">'."\n";
  990. print '<input type="hidden" name="origin" value="'.$objectsrc->element.'">';
  991. print '<input type="hidden" name="originid" value="'.$objectsrc->id.'">';
  992. $txt=$langs->trans($classname);
  993. if ($classname=='CommandeFournisseur') $txt=$langs->trans("SupplierOrder");
  994. print '<tr><td>'.$txt.'</td><td colspan="2">'.$objectsrc->getNomUrl(1).'</td></tr>';
  995. print '<tr><td>'.$langs->trans('TotalHT').'</td><td colspan="2">'.price($objectsrc->total_ht).'</td></tr>';
  996. print '<tr><td>'.$langs->trans('TotalVAT').'</td><td colspan="2">'.price($objectsrc->total_tva)."</td></tr>";
  997. if ($mysoc->country_code=='ES')
  998. {
  999. if ($mysoc->localtax1_assuj=="1") //Localtax1 RE
  1000. {
  1001. print '<tr><td>'.$langs->transcountry("AmountLT1",$mysoc->country_code).'</td><td colspan="2">'.price($objectsrc->total_localtax1)."</td></tr>";
  1002. }
  1003. if ($mysoc->localtax2_assuj=="1") //Localtax2 IRPF
  1004. {
  1005. print '<tr><td>'.$langs->transcountry("AmountLT2",$mysoc->country_code).'</td><td colspan="2">'.price($objectsrc->total_localtax2)."</td></tr>";
  1006. }
  1007. }
  1008. print '<tr><td>'.$langs->trans('TotalTTC').'</td><td colspan="2">'.price($objectsrc->total_ttc)."</td></tr>";
  1009. }
  1010. else
  1011. {
  1012. // TODO more bugs
  1013. if (1==2 && ! empty($conf->global->PRODUCT_SHOW_WHEN_CREATE))
  1014. {
  1015. print '<tr class="liste_titre">';
  1016. print '<td>&nbsp;</td><td>'.$langs->trans('Label').'</td>';
  1017. print '<td align="right">'.$langs->trans('PriceUHT').'</td>';
  1018. print '<td align="right">'.$langs->trans('VAT').'</td>';
  1019. print '<td align="right">'.$langs->trans('Qty').'</td>';
  1020. print '<td align="right">'.$langs->trans('PriceUTTC').'</td>';
  1021. print '</tr>';
  1022. for ($i = 1 ; $i < 9 ; $i++)
  1023. {
  1024. $value_qty = '1';
  1025. $value_tauxtva = '';
  1026. print '<tr><td>'.$i.'</td>';
  1027. print '<td><input size="50" name="label'.$i.'" value="'.$value_label.'" type="text"></td>';
  1028. print '<td align="right"><input type="text" size="8" name="amount'.$i.'" value="'.$value_pu.'"></td>';
  1029. print '<td align="right">';
  1030. print $form->load_tva('tauxtva'.$i,$value_tauxtva,$societe,$mysoc);
  1031. print '</td>';
  1032. print '<td align="right"><input type="text" size="3" name="qty'.$i.'" value="'.$value_qty.'"></td>';
  1033. print '<td align="right"><input type="text" size="8" name="amountttc'.$i.'" value=""></td></tr>';
  1034. }
  1035. }
  1036. }
  1037. // Other options
  1038. $parameters=array();
  1039. $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
  1040. // Bouton "Create Draft"
  1041. print "</table>\n";
  1042. print '<br><center><input type="submit" class="button" name="bouton" value="'.$langs->trans('CreateDraft').'"></center>';
  1043. print "</form>\n";
  1044. // Show origin lines
  1045. if (is_object($objectsrc))
  1046. {
  1047. print '<br>';
  1048. $title=$langs->trans('ProductsAndServices');
  1049. print_titre($title);
  1050. print '<table class="noborder" width="100%">';
  1051. $objectsrc->printOriginLinesList();
  1052. print '</table>';
  1053. }
  1054. }
  1055. else
  1056. {
  1057. if ($id > 0)
  1058. {
  1059. /* *************************************************************************** */
  1060. /* */
  1061. /* Fiche en mode visu ou edition */
  1062. /* */
  1063. /* *************************************************************************** */
  1064. $now=dol_now();
  1065. $productstatic = new Product($db);
  1066. $object->fetch($id);
  1067. $societe = new Fournisseur($db);
  1068. $societe->fetch($object->socid);
  1069. /*
  1070. * View card
  1071. */
  1072. $head = facturefourn_prepare_head($object);
  1073. $titre=$langs->trans('SupplierInvoice');
  1074. dol_fiche_head($head, 'card', $titre, 0, 'bill');
  1075. dol_htmloutput_mesg($mesg);
  1076. dol_htmloutput_errors('',$errors);
  1077. // Confirmation de la suppression d'une ligne produit
  1078. if ($action == 'confirm_delete_line')
  1079. {
  1080. $ret=$form->form_confirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$_GET["lineid"], $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteline', '', 1, 1);
  1081. if ($ret == 'html') print '<br>';
  1082. }
  1083. // Clone confirmation
  1084. if ($action == 'clone')
  1085. {
  1086. // Create an array for form
  1087. $formquestion=array(
  1088. //'text' => $langs->trans("ConfirmClone"),
  1089. //array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneMainAttributes"), 'value' => 1)
  1090. );
  1091. // Paiement incomplet. On demande si motif = escompte ou autre
  1092. $ret=$form->form_confirm($_SERVER["PHP_SELF"].'?id='.$object->id,$langs->trans('CloneInvoice'),$langs->trans('ConfirmCloneInvoice',$object->ref),'confirm_clone',$formquestion,'yes', 1);
  1093. if ($ret == 'html') print '<br>';
  1094. }
  1095. // Confirmation de la validation
  1096. if ($action == 'valid')
  1097. {
  1098. $formquestion=array();
  1099. if (! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL) && $object->hasProductsOrServices(1))
  1100. {
  1101. $langs->load("stocks");
  1102. require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
  1103. $formproduct=new FormProduct($db);
  1104. $formquestion=array(
  1105. //'text' => $langs->trans("ConfirmClone"),
  1106. //array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneMainAttributes"), 'value' => 1),
  1107. //array('type' => 'checkbox', 'name' => 'update_prices', 'label' => $langs->trans("PuttingPricesUpToDate"), 'value' => 1),
  1108. array('type' => 'other', 'name' => 'idwarehouse', 'label' => $langs->trans("SelectWarehouseForStockDecrease"), 'value' => $formproduct->selectWarehouses(GETPOST('idwarehouse'),'idwarehouse','',1)));
  1109. }
  1110. $ret=$form->form_confirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ValidateBill'), $langs->trans('ConfirmValidateBill', $object->ref), 'confirm_valid', $formquestion, 1, 1, 240);
  1111. if ($ret == 'html') print '<br>';
  1112. }
  1113. // Confirmation set paid
  1114. if ($action == 'paid')
  1115. {
  1116. $ret=$form->form_confirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ClassifyPaid'), $langs->trans('ConfirmClassifyPaidBill', $object->ref), 'confirm_paid', '', 0, 1);
  1117. if ($ret == 'html') print '<br>';
  1118. }
  1119. // Confirmation de la suppression de la facture fournisseur
  1120. if ($action == 'delete')
  1121. {
  1122. $ret=$form->form_confirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteBill'), $langs->trans('ConfirmDeleteBill'), 'confirm_delete', '', 0, 1);
  1123. if ($ret == 'html') print '<br>';
  1124. }
  1125. /**
  1126. * Invoice
  1127. */
  1128. print '<table class="border" width="100%">';
  1129. $linkback = '<a href="'.DOL_URL_ROOT.'/fourn/facture/index.php'.(! empty($socid)?'?socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
  1130. // Ref
  1131. print '<tr><td nowrap="nowrap" width="20%">'.$langs->trans("Ref").'</td><td colspan="4">';
  1132. print $form->showrefnav($object, 'id', $linkback, 1, 'rowid', 'ref');
  1133. print '</td>';
  1134. print "</tr>\n";
  1135. // Ref supplier
  1136. print '<tr><td>'.$form->editfieldkey("RefSupplier",'facnumber',$object->ref_supplier,$object,($object->statut<2 && $user->rights->fournisseur->facture->creer)).'</td><td colspan="4">';
  1137. print $form->editfieldval("RefSupplier",'facnumber',$object->ref_supplier,$object,($object->statut<2 && $user->rights->fournisseur->facture->creer));
  1138. print '</td></tr>';
  1139. // Third party
  1140. print '<tr><td>'.$langs->trans('Supplier').'</td><td colspan="4">'.$societe->getNomUrl(1);
  1141. print ' &nbsp; (<a href="'.DOL_URL_ROOT.'/fourn/facture/index.php?socid='.$object->socid.'">'.$langs->trans('OtherBills').'</a>)</td>';
  1142. print '</tr>';
  1143. // Type
  1144. print '<tr><td>'.$langs->trans('Type').'</td><td colspan="4">';
  1145. print $object->getLibType();
  1146. if ($object->type == 1)
  1147. {
  1148. $facreplaced=new FactureFournisseur($db);
  1149. $facreplaced->fetch($object->fk_facture_source);
  1150. print ' ('.$langs->transnoentities("ReplaceInvoice",$facreplaced->getNomUrl(1)).')';
  1151. }
  1152. if ($object->type == 2)
  1153. {
  1154. $facusing=new FactureFournisseur($db);
  1155. $facusing->fetch($object->fk_facture_source);
  1156. print ' ('.$langs->transnoentities("CorrectInvoice",$facusing->getNomUrl(1)).')';
  1157. }
  1158. $facidavoir=$object->getListIdAvoirFromInvoice();
  1159. if (count($facidavoir) > 0)
  1160. {
  1161. print ' ('.$langs->transnoentities("InvoiceHasAvoir");
  1162. $i=0;
  1163. foreach($facidavoir as $id)
  1164. {
  1165. if ($i==0) print ' ';
  1166. else print ',';
  1167. $facavoir=new FactureFournisseur($db);
  1168. $facavoir->fetch($id);
  1169. print $facavoir->getNomUrl(1);
  1170. }
  1171. print ')';
  1172. }
  1173. if (isset($facidnext) && $facidnext > 0)
  1174. {
  1175. $facthatreplace=new FactureFournisseur($db);
  1176. $facthatreplace->fetch($facidnext);
  1177. print ' ('.$langs->transnoentities("ReplacedByInvoice",$facthatreplace->getNomUrl(1)).')';
  1178. }
  1179. print '</td></tr>';
  1180. // Label
  1181. print '<tr><td>'.$form->editfieldkey("Label",'label',$object->label,$object,($object->statut<2 && $user->rights->fournisseur->facture->creer)).'</td>';
  1182. print '<td colspan="3">'.$form->editfieldval("Label",'label',$object->label,$object,($object->statut<2 && $user->rights->fournisseur->facture->creer)).'</td>';
  1183. /*
  1184. * List of payments
  1185. */
  1186. $nbrows=7; $nbcols=2;
  1187. if (! empty($conf->projet->enabled)) $nbrows++;
  1188. if (! empty($conf->banque->enabled)) $nbcols++;
  1189. // Local taxes
  1190. if ($mysoc->country_code=='ES')
  1191. {
  1192. if($mysoc->localtax1_assuj=="1") $nbrow++;
  1193. if($mysoc->localtax2_assuj=="1") $nbrow++;
  1194. }
  1195. print '<td rowspan="'.$nbrows.'" valign="top">';
  1196. $sql = 'SELECT p.datep as dp, p.num_paiement, p.rowid, p.fk_bank,';
  1197. $sql.= ' c.libelle as paiement_type,';
  1198. $sql.= ' pf.amount,';
  1199. $sql.= ' ba.rowid as baid, ba.ref, ba.label';
  1200. $sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn as p';
  1201. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid';
  1202. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank_account as ba ON b.fk_account = ba.rowid';
  1203. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id';
  1204. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf ON pf.fk_paiementfourn = p.rowid';
  1205. $sql.= ' WHERE pf.fk_facturefourn = '.$object->id;
  1206. $sql.= ' ORDER BY p.datep, p.tms';
  1207. $result = $db->query($sql);
  1208. if ($result)
  1209. {
  1210. $num = $db->num_rows($result);
  1211. $i = 0; $totalpaye = 0;
  1212. print '<table class="nobordernopadding" width="100%">';
  1213. print '<tr class="liste_titre">';
  1214. print '<td>'.$langs->trans('Payments').'</td>';
  1215. print '<td>'.$langs->trans('Type').'</td>';
  1216. if (! empty($conf->banque->enabled)) print '<td align="right">'.$langs->trans('BankAccount').'</td>';
  1217. print '<td align="right">'.$langs->trans('Amount').'</td>';
  1218. print '<td width="18">&nbsp;</td>';
  1219. print '</tr>';
  1220. $var=true;
  1221. if ($num > 0)
  1222. {
  1223. while ($i < $num)
  1224. {
  1225. $objp = $db->fetch_object($result);
  1226. $var=!$var;
  1227. print '<tr '.$bc[$var].'>';
  1228. print '<td nowrap="nowrap"><a href="'.DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='.$objp->rowid.'">'.img_object($langs->trans('ShowPayment'),'payment').' '.dol_print_date($db->jdate($objp->dp),'day')."</a></td>\n";
  1229. print '<td>'.$objp->paiement_type.' '.$objp->num_paiement.'</td>';
  1230. if (! empty($conf->banque->enabled))
  1231. {
  1232. $bankaccountstatic->id=$objp->baid;
  1233. $bankaccountstatic->ref=$objp->ref;
  1234. $bankaccountstatic->label=$objp->ref;
  1235. print '<td align="right">';
  1236. print $bankaccountstatic->getNomUrl(1,'transactions');
  1237. print '</td>';
  1238. }
  1239. print '<td align="right">'.price($objp->amount).'</td>';
  1240. print '<td align="center">';
  1241. if ($object->statut == 1 && $object->paye == 0 && $user->societe_id == 0)
  1242. {
  1243. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=deletepaiement&paiement_id='.$objp->rowid.'">';
  1244. print img_delete();
  1245. print '</a>';
  1246. }
  1247. print '</td>';
  1248. print '</tr>';
  1249. $totalpaye += $objp->amount;
  1250. $i++;
  1251. }
  1252. }
  1253. else
  1254. {
  1255. print '<tr '.$bc[$var].'><td colspan="'.$nbcols.'">'.$langs->trans("None").'</td><td></td><td></td></tr>';
  1256. }
  1257. if ($object->paye == 0)
  1258. {
  1259. print '<tr><td colspan="'.$nbcols.'" align="right">'.$langs->trans('AlreadyPaid').' :</td><td align="right"><b>'.price($totalpaye).'</b></td><td></td></tr>';
  1260. print '<tr><td colspan="'.$nbcols.'" align="right">'.$langs->trans("Billed").' :</td><td align="right" style="border: 1px solid;">'.price($object->total_ttc).'</td><td></td></tr>';
  1261. $resteapayer = $object->total_ttc - $totalpaye;
  1262. print '<tr><td colspan="2" align="right">'.$langs->trans('RemainderToPay').' :</td>';
  1263. print '<td align="right" style="border: 1px solid;" bgcolor="#f0f0f0"><b>'.price($resteapayer).'</b></td></tr>';
  1264. }
  1265. print '</table>';
  1266. $db->free($result);
  1267. }
  1268. else
  1269. {
  1270. dol_print_error($db);
  1271. }
  1272. print '</td>';
  1273. print '</tr>';
  1274. // Date
  1275. print '<tr><td>'.$form->editfieldkey("Date",'datef',$object->datep,$object,($object->statut<2 && $user->rights->fournisseur->facture->creer && $object->getSommePaiement() <= 0),'datepicker').'</td><td colspan="3">';
  1276. print $form->editfieldval("Date",'datef',$object->datep,$object,($object->statut<2 && $user->rights->fournisseur->facture->creer && $object->getSommePaiement() <= 0),'datepicker');
  1277. print '</td>';
  1278. // Due date
  1279. print '<tr><td>'.$form->editfieldkey("DateMaxPayment",'date_lim_reglement',$object->date_echeance,$object,($object->statut<2 && $user->rights->fournisseur->facture->creer && $object->getSommePaiement() <= 0),'datepicker').'</td><td colspan="3">';
  1280. print $form->editfieldval("DateMaxPayment",'date_lim_reglement',$object->date_echeance,$object,($object->statut<2 && $user->rights->fournisseur->facture->creer && $object->getSommePaiement() <= 0),'datepicker');
  1281. if ($action != 'editdate_lim_reglement' && $object->statut < 2 && $object->date_echeance && $object->date_echeance < ($now - $conf->facture->fournisseur->warning_delay)) print img_warning($langs->trans('Late'));
  1282. print '</td>';
  1283. // Status
  1284. $alreadypaid=$object->getSommePaiement();
  1285. print '<tr><td>'.$langs->trans('Status').'</td><td colspan="3">'.$object->getLibStatut(4,$alreadypaid).'</td></tr>';
  1286. print '<tr><td>'.$langs->trans('AmountHT').'</td><td align="right">'.price($object->total_ht).'</td><td colspan="2" align="left">'.$langs->trans('Currency'.$conf->currency).'</td></tr>';
  1287. print '<tr><td>'.$langs->trans('AmountVAT').'</td><td align="right">'.price($object->total_tva).'</td><td colspan="2" align="left">'.$langs->trans('Currency'.$conf->currency).'</td></tr>';
  1288. // Amount Local Taxes
  1289. if ($mysoc->country_code=='ES')
  1290. {
  1291. if ($mysoc->localtax1_assuj=="1") //Localtax1 RE
  1292. {
  1293. print '<tr><td>'.$langs->transcountry("AmountLT1",$mysoc->country_code).'</td>';
  1294. print '<td align="right">'.price($object->total_localtax1).'</td>';
  1295. print '<td>'.$langs->trans("Currency".$conf->currency).'</td></tr>';
  1296. }
  1297. if ($mysoc->localtax2_assuj=="1") //Localtax2 IRPF
  1298. {
  1299. print '<tr><td>'.$langs->transcountry("AmountLT2",$mysoc->country_code).'</td>';
  1300. print '<td align="right">'.price($object->total_localtax2).'</td>';
  1301. print '<td>'.$langs->trans("Currency".$conf->currency).'</td></tr>';
  1302. }
  1303. }
  1304. print '<tr><td>'.$langs->trans('AmountTTC').'</td><td align="right">'.price($object->total_ttc).'</td><td colspan="2" align="left">'.$langs->trans('Currency'.$conf->currency).'</td></tr>';
  1305. // Project
  1306. if (! empty($conf->projet->enabled))
  1307. {
  1308. $langs->load('projects');
  1309. print '<tr>';
  1310. print '<td>';
  1311. print '<table class="nobordernopadding" width="100%"><tr><td>';
  1312. print $langs->trans('Project');
  1313. print '</td>';
  1314. if ($action != 'classify')
  1315. {
  1316. print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=classify&amp;id='.$object->id.'">';
  1317. print img_edit($langs->trans('SetProject'),1);
  1318. print '</a></td>';
  1319. }
  1320. print '</tr></table>';
  1321. print '</td><td colspan="3">';
  1322. if ($action == 'classify')
  1323. {
  1324. $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id,$object->socid,$object->fk_project,'projectid');
  1325. }
  1326. else
  1327. {
  1328. $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id,$object->socid,$object->fk_project,'none');
  1329. }
  1330. print '</td>';
  1331. print '</tr>';
  1332. }
  1333. // Other options
  1334. $parameters=array();
  1335. $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
  1336. print '</table>';
  1337. if (! empty($conf->global->MAIN_DISABLE_CONTACTS_TAB))
  1338. {
  1339. print '<br>';
  1340. $blocname = 'contacts';
  1341. $title = $langs->trans('ContactsAddresses');
  1342. include DOL_DOCUMENT_ROOT.'/core/tpl/bloc_showhide.tpl.php';
  1343. }
  1344. if (! empty($conf->global->MAIN_DISABLE_NOTES_TAB))
  1345. {
  1346. $colwidth=20;
  1347. $blocname = 'notes';
  1348. $title = $langs->trans('Notes');
  1349. include DOL_DOCUMENT_ROOT.'/core/tpl/bloc_showhide.tpl.php';
  1350. }
  1351. /*
  1352. * Lines
  1353. */
  1354. print '<br>';
  1355. print '<table class="noborder" width="100%">';
  1356. $var=1;
  1357. $num=count($object->lines);
  1358. for ($i = 0; $i < $num; $i++)
  1359. {
  1360. if ($i == 0)
  1361. {
  1362. print '<tr class="liste_titre"><td>'.$langs->trans('Label').'</td>';
  1363. print '<td align="right">'.$langs->trans('VAT').'</td>';
  1364. print '<td align="right">'.$langs->trans('PriceUHT').'</td>';
  1365. print '<td align="right">'.$langs->trans('PriceUTTC').'</td>';
  1366. print '<td align="right">'.$langs->trans('Qty').'</td>';
  1367. print '<td align="right">'.$langs->trans('Discount').'</td>';
  1368. print '<td align="right">'.$langs->trans('TotalHTShort').'</td>';
  1369. print '<td align="right">'.$langs->trans('TotalTTCShort').'</td>';
  1370. print '<td>&nbsp;</td>';
  1371. print '<td>&nbsp;</td>';
  1372. print '</tr>';
  1373. }
  1374. // Show product and description
  1375. $type=(! empty($object->lines[$i]->product_type)?$object->lines[$i]->product_type:(! empty($object->lines[$i]->fk_product_type)?$object->lines[$i]->fk_product_type:0));
  1376. // Try to enhance type detection using date_start and date_end for free lines where type
  1377. // was not saved.
  1378. $date_start='';
  1379. $date_end='';
  1380. if (! empty($object->lines[$i]->date_start))
  1381. {
  1382. $date_start=$object->lines[$i]->date_start;
  1383. $type=1;
  1384. }
  1385. if (! empty($object->lines[$i]->date_end))
  1386. {
  1387. $date_end=$object->lines[$i]->date_end;
  1388. $type=1;
  1389. }
  1390. $var=!$var;
  1391. // Edit line
  1392. if ($object->statut == 0 && $action == 'edit_line' && $_GET['etat'] == '0' && $_GET['lineid'] == $object->lines[$i]->rowid)
  1393. {
  1394. print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;etat=1&amp;lineid='.$object->lines[$i]->rowid.'" method="post">';
  1395. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1396. print '<input type="hidden" name="action" value="update_line">';
  1397. print '<tr '.$bc[$var].'>';
  1398. // Show product and description
  1399. print '<td>';
  1400. if ((! empty($conf->product->enabled) || ! empty($conf->service->enabled)) && $object->lines[$i]->fk_product)
  1401. {
  1402. print '<input type="hidden" name="idprod" value="'.$object->lines[$i]->fk_product.'">';
  1403. $product_static=new ProductFournisseur($db);
  1404. $product_static->fetch($object->lines[$i]->fk_product);
  1405. $text=$product_static->getNomUrl(1);
  1406. $text.= ' - '.$product_static->libelle;
  1407. print $text;
  1408. print '<br>';
  1409. }
  1410. else
  1411. {
  1412. $forceall=1; // For suppliers, we always show all types
  1413. print $form->select_type_of_lines($object->lines[$i]->product_type,'type',1);
  1414. if ($forceall || (! empty($conf->product->enabled) && ! empty($conf->service->enabled))
  1415. || (empty($conf->product->enabled) && empty($conf->service->enabled))) print '<br>';
  1416. }
  1417. if (is_object($hookmanager))
  1418. {
  1419. $parameters=array('fk_parent_line'=>$line->fk_parent_line, 'line'=>$object->lines[$i],'var'=>$var,'num'=>$num,'i'=>$i);
  1420. $reshook=$hookmanager->executeHooks('formEditProductOptions',$parameters,$object,$action);
  1421. }
  1422. // Description - Editor wysiwyg
  1423. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1424. $nbrows=ROWS_2;
  1425. if (! empty($conf->global->MAIN_INPUT_DESC_HEIGHT)) $nbrows=$conf->global->MAIN_INPUT_DESC_HEIGHT;
  1426. $doleditor=new DolEditor('desc',$object->lines[$i]->description,'',128,'dolibarr_details','',false,true,$conf->global->FCKEDITOR_ENABLE_DETAILS,$nbrows,70);
  1427. $doleditor->Create();
  1428. print '</td>';
  1429. // VAT
  1430. print '<td align="right">';
  1431. print $form->load_tva('tauxtva',$object->lines[$i]->tva_tx,$societe,$mysoc);
  1432. print '</td>';
  1433. // Unit price
  1434. print '<td align="right" nowrap="nowrap"><input size="4" name="puht" type="text" value="'.price($object->lines[$i]->pu_ht).'"></td>';
  1435. print '<td align="right" nowrap="nowrap"><input size="4" name="puttc" type="text" value=""></td>';
  1436. print '<td align="right"><input size="1" name="qty" type="text" value="'.$object->lines[$i]->qty.'"></td>';
  1437. print '<td align="right" nowrap="nowrap"><input size="1" name="remise_percent" type="text" value="'.$object->lines[$i]->remise_percent.'">%</td>';
  1438. print '<td align="right" nowrap="nowrap">&nbsp;</td>';
  1439. print '<td align="right" nowrap="nowrap">&nbsp;</td>';
  1440. print '<td align="center" colspan="2"><input type="submit" class="button" value="'.$langs->trans('Save').'">';
  1441. print '<br><input type="submit" class="button" name="cancel" value="'.$langs->trans('Cancel').'"></td>';
  1442. print '</tr>';
  1443. print '</form>';
  1444. }
  1445. else // Affichage simple de la ligne
  1446. {
  1447. print '<tr '.$bc[$var].'>';
  1448. // Show product and description
  1449. print '<td>';
  1450. if ($object->lines[$i]->fk_product)
  1451. {
  1452. print '<a name="'.$objp->rowid.'"></a>'; // ancre pour retourner sur la ligne
  1453. $product_static=new ProductFournisseur($db);
  1454. $product_static->fetch($object->lines[$i]->fk_product);
  1455. $text=$product_static->getNomUrl(1);
  1456. $text.= ' - '.$product_static->libelle;
  1457. $description=($conf->global->PRODUIT_DESC_IN_FORM?'':dol_htmlentitiesbr($object->lines[$i]->description));
  1458. print $form->textwithtooltip($text,$description,3,'','',$i);
  1459. // Show range
  1460. print_date_range($date_start,$date_end);
  1461. // Add description in form
  1462. if (! empty($conf->global->PRODUIT_DESC_IN_FORM)) print ($object->lines[$i]->description && $object->lines[$i]->description!=$product_static->libelle)?'<br>'.dol_htmlentitiesbr($object->lines[$i]->description):'';
  1463. }
  1464. // Description - Editor wysiwyg
  1465. if (! $object->lines[$i]->fk_product)
  1466. {
  1467. if ($type==1) $text = img_object($langs->trans('Service'),'service');
  1468. else $text = img_object($langs->trans('Product'),'product');
  1469. print $text.' '.nl2br($object->lines[$i]->description);
  1470. // Show range
  1471. print_date_range($date_start,$date_end);
  1472. }
  1473. print '</td>';
  1474. // VAT
  1475. print '<td align="right">'.vatrate($object->lines[$i]->tva_tx).'%</td>';
  1476. // Unit price
  1477. print '<td align="right" nowrap="nowrap">'.price($object->lines[$i]->pu_ht,'MU').'</td>';
  1478. print '<td align="right" nowrap="nowrap">'.($object->lines[$i]->pu_ttc?price($object->lines[$i]->pu_ttc,'MU'):'&nbsp;').'</td>';
  1479. print '<td align="right">'.$object->lines[$i]->qty.'</td>';
  1480. print '<td align="right">'.(($object->lines[$i]->remise_percent > 0)?$object->lines[$i]->remise_percent.'%':'').'</td>';
  1481. print '<td align="right" nowrap="nowrap">'.price($object->lines[$i]->total_ht).'</td>';
  1482. print '<td align="right" nowrap="nowrap">'.price($object->lines[$i]->total_ttc).'</td>';
  1483. print '<td align="center" width="16">';
  1484. if ($object->statut == 0) print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=edit_line&amp;etat=0&amp;lineid='.$object->lines[$i]->rowid.'">'.img_edit().'</a>';
  1485. else print '&nbsp;';
  1486. print '</td>';
  1487. print '<td align="center" width="16">';
  1488. if ($object->statut == 0) print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=confirm_delete_line&amp;lineid='.$object->lines[$i]->rowid.'">'.img_delete().'</a>';
  1489. else print '&nbsp;';
  1490. print '</td>';
  1491. print '</tr>';
  1492. }
  1493. }
  1494. /*
  1495. * Form to add new line
  1496. */
  1497. if ($object->statut == 0 && $action != 'edit_line')
  1498. {
  1499. print '<tr class="liste_titre">';
  1500. print '<td>';
  1501. print '<a name="add"></a>'; // ancre
  1502. print $langs->trans('AddNewLine').' - '.$langs->trans("FreeZone").'</td>';
  1503. print '<td align="right">'.$langs->trans('VAT').'</td>';
  1504. print '<td align="right">'.$langs->trans('PriceUHT').'</td>';
  1505. print '<td align="right">'.$langs->trans('PriceUTTC').'</td>';
  1506. print '<td align="right">'.$langs->trans('Qty').'</td>';
  1507. print '<td align="right">&nbsp;</td>';
  1508. print '<td align="right">&nbsp;</td>';
  1509. print '<td align="right">&nbsp;</td>';
  1510. print '<td>&nbsp;</td>';
  1511. print '<td>&nbsp;</td>';
  1512. print '</tr>';
  1513. // Add free products/services form
  1514. print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=addline" method="post">';
  1515. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1516. print '<input type="hidden" name="facid" value="'.$object->id.'">';
  1517. print '<input type="hidden" name="socid" value="'.$societe->id.'">';
  1518. $var=true;
  1519. print '<tr '.$bc[$var].'>';
  1520. print '<td>';
  1521. $forceall=1; // For suppliers, we always show all types
  1522. print $form->select_type_of_lines(isset($_POST["type"])?$_POST["type"]:-1,'type',1,0,$forceall);
  1523. if ($forceall || (! empty($conf->product->enabled) && ! empty($conf->service->enabled))
  1524. || (empty($conf->product->enabled) && empty($conf->service->enabled))) print '<br>';
  1525. if (is_object($hookmanager))
  1526. {
  1527. $parameters=array();
  1528. $reshook=$hookmanager->executeHooks('formCreateProductOptions',$parameters,$object,$action);
  1529. }
  1530. // Editor wysiwyg
  1531. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1532. $nbrows=ROWS_2;
  1533. if (! empty($conf->global->MAIN_INPUT_DESC_HEIGHT)) $nbrows=$conf->global->MAIN_INPUT_DESC_HEIGHT;
  1534. $doleditor=new DolEditor('dp_desc',GETPOST("dp_desc"),'',100,'dolibarr_details','',false,true,$conf->global->FCKEDITOR_ENABLE_DETAILS,$nbrows,70);
  1535. $doleditor->Create();
  1536. print '</td>';
  1537. print '<td align="right">';
  1538. print $form->load_tva('tauxtva',(GETPOST('tauxtva')?GETPOST('tauxtva'):-1),$societe,$mysoc);
  1539. print '</td>';
  1540. print '<td align="right">';
  1541. print '<input size="4" name="amount" type="text">';
  1542. print '</td>';
  1543. print '<td align="right">';
  1544. print '<input size="4" name="amountttc" type="text">';
  1545. print '</td>';
  1546. print '<td align="right">';
  1547. print '<input size="1" name="qty" type="text" value="1">';
  1548. print '</td>';
  1549. print '<td align="right" nowrap="nowrap"><input size="1" name="remise_percent" type="text" value="'.(GETPOST('remise_percent')?GETPOST('remise_percent'):'0').'">%</td>';
  1550. print '<td>&nbsp;</td>';
  1551. print '<td>&nbsp;</td>';
  1552. print '<td align="center" valign="middle" colspan="2"><input type="submit" class="button" value="'.$langs->trans('Add').'"></td></tr>';
  1553. print '</form>';
  1554. // Ajout de produits/services predefinis
  1555. if (! empty($conf->product->enabled) || ! empty($conf->service->enabled))
  1556. {
  1557. print '<tr class="liste_titre">';
  1558. print '<td colspan="4">';
  1559. print $langs->trans("AddNewLine").' - ';
  1560. if (! empty($conf->service->enabled))
  1561. {
  1562. print $langs->trans('RecordedProductsAndServices');
  1563. }
  1564. else
  1565. {
  1566. print $langs->trans('RecordedProducts');
  1567. }
  1568. print '</td>';
  1569. print '<td align="right">'.$langs->trans('Qty').'</td>';
  1570. print '<td>&nbsp;</td>';
  1571. print '<td>&nbsp;</td>';
  1572. print '<td colspan="4">&nbsp;</td>';
  1573. print '</tr>';
  1574. print '<form name="addline_predef" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=addline" method="post">';
  1575. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1576. print '<input type="hidden" name="socid" value="'. $object->socid .'">';
  1577. print '<input type="hidden" name="facid" value="'.$object->id.'">';
  1578. $var=! $var;
  1579. print '<tr '.$bc[$var].'>';
  1580. print '<td colspan="4">';
  1581. $ajaxoptions=array(
  1582. 'update' => array('pqty' => 'qty', 'p_remise_percent' => 'discount'),
  1583. 'disabled' => 'addPredefinedProductButton',
  1584. 'error' => $langs->trans("NoPriceDefinedForThisSupplier")
  1585. );
  1586. $form->select_produits_fournisseurs($object->socid, '', 'idprodfournprice', '', '', $ajaxoptions);
  1587. if (empty($conf->global->PRODUIT_USE_SEARCH_TO_SELECT)) print '<br>';
  1588. if (is_object($hookmanager))
  1589. {
  1590. $parameters=array('htmlname'=>'idprodfournprice');
  1591. $reshook=$hookmanager->executeHooks('formCreateProductSupplierOptions',$parameters,$object,$action);
  1592. }
  1593. $nbrows=ROWS_2;
  1594. if (! empty($conf->global->MAIN_INPUT_DESC_HEIGHT)) $nbrows=$conf->global->MAIN_INPUT_DESC_HEIGHT;
  1595. $doleditor = new DolEditor('np_desc', GETPOST('np_desc'), '', 100, 'dolibarr_details', '', false, true, $conf->global->FCKEDITOR_ENABLE_DETAILS, $nbrows, 70);
  1596. $doleditor->Create();
  1597. print '</td>';
  1598. print '<td align="right"><input type="text" id="pqty" name="qty" value="1" size="1"></td>';
  1599. print '<td align="right" nowrap="nowrap"><input size="1" id="p_remise_percent" name="remise_percent" type="text" value="'.(GETPOST('remise_percent')?GETPOST('remise_percent'):'0').'">%</td>';
  1600. print '<td>&nbsp;</td>';
  1601. print '<td>&nbsp;</td>';
  1602. print '<td align="center" valign="middle" colspan="2"><input type="submit" id="addPredefinedProductButton" class="button" value="'.$langs->trans("Add").'"></td>';
  1603. print '</tr>';
  1604. print '</form>';
  1605. }
  1606. }
  1607. print '</table>';
  1608. print '</div>';
  1609. if ($action != 'presend')
  1610. {
  1611. /*
  1612. * Boutons actions
  1613. */
  1614. print '<div class="tabsAction">';
  1615. // Reopen a standard paid invoice
  1616. if (($object->type == 0 || $object->type == 1) && ($object->statut == 2 || $object->statut == 3)) // A paid invoice (partially or completely)
  1617. {
  1618. if (! $facidnext && $object->close_code != 'replaced') // Not replaced by another invoice
  1619. {
  1620. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=reopen">'.$langs->trans('ReOpen').'</a>';
  1621. }
  1622. else
  1623. {
  1624. print '<span class="butActionRefused" title="'.$langs->trans("DisabledBecauseReplacedInvoice").'">'.$langs->trans('ReOpen').'</span>';
  1625. }
  1626. }
  1627. // Send by mail
  1628. if (($object->statut == 1 || $object->statut == 2))
  1629. {
  1630. if (empty($conf->global->MAIN_USE_ADVANCED_PERMS) || $user->rights->fournisseur->supplier_invoice_advance->send)
  1631. {
  1632. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=presend&amp;mode=init">'.$langs->trans('SendByMail').'</a>';
  1633. }
  1634. else print '<a class="butActionRefused" href="#">'.$langs->trans('SendByMail').'</a>';
  1635. }
  1636. //Make payments
  1637. if ($action != 'edit' && $object->statut == 1 && $object->paye == 0 && $user->societe_id == 0)
  1638. {
  1639. print '<a class="butAction" href="paiement.php?facid='.$object->id.'&amp;action=create">'.$langs->trans('DoPayment').'</a>';
  1640. }
  1641. //Classify paid
  1642. if ($action != 'edit' && $object->statut == 1 && $object->paye == 0 && $user->societe_id == 0)
  1643. {
  1644. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=paid"';
  1645. print '>'.$langs->trans('ClassifyPaid').'</a>';
  1646. //print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=paid">'.$langs->trans('ClassifyPaid').'</a>';
  1647. }
  1648. //Validate
  1649. if ($action != 'edit' && $object->statut == 0)
  1650. {
  1651. if (count($object->lines))
  1652. {
  1653. if ($user->rights->fournisseur->facture->valider)
  1654. {
  1655. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=valid"';
  1656. print '>'.$langs->trans('Validate').'</a>';
  1657. }
  1658. else
  1659. {
  1660. print '<a class="butActionRefused" href="#" title="'.dol_escape_htmltag($langs->trans("NotAllowed")).'"';
  1661. print '>'.$langs->trans('Validate').'</a>';
  1662. }
  1663. }
  1664. }
  1665. //Clone
  1666. if ($action != 'edit' && $user->rights->fournisseur->facture->creer)
  1667. {
  1668. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=clone&amp;socid='.$object->socid.'">'.$langs->trans('ToClone').'</a>';
  1669. }
  1670. //Delete
  1671. if ($action != 'edit' && $user->rights->fournisseur->facture->supprimer)
  1672. {
  1673. print '<a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delete">'.$langs->trans('Delete').'</a>';
  1674. }
  1675. print '</div>';
  1676. if ($action != 'edit')
  1677. {
  1678. print '<table width="100%"><tr><td width="50%" valign="top">';
  1679. print '<a name="builddoc"></a>'; // ancre
  1680. /*
  1681. * Documents generes
  1682. */
  1683. $ref=dol_sanitizeFileName($object->ref);
  1684. $subdir = get_exdir($object->id,2).$ref;
  1685. $filedir = $conf->fournisseur->facture->dir_output.'/'.get_exdir($object->id,2).$ref;
  1686. $urlsource=$_SERVER['PHP_SELF'].'?id='.$object->id;
  1687. $genallowed=$user->rights->fournisseur->facture->creer;
  1688. $delallowed=$user->rights->fournisseur->facture->supprimer;
  1689. $modelpdf=(! empty($object->modelpdf)?$object->modelpdf:'');
  1690. print '<br>';
  1691. print $formfile->showdocuments('facture_fournisseur',$subdir,$filedir,$urlsource,$genallowed,$delallowed,$modelpdf,1,0,0,0,0,'','','',$societe->default_lang);
  1692. $somethingshown=$formfile->numoffiles;
  1693. /*
  1694. * Linked object block
  1695. */
  1696. $somethingshown=$object->showLinkedObjectBlock();
  1697. print '</td><td valign="top" width="50%">';
  1698. print '<br>';
  1699. // List of actions on element
  1700. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
  1701. $formactions=new FormActions($db);
  1702. $somethingshown=$formactions->showactions($object,'invoice_supplier',$socid);
  1703. print '</td></tr></table>';
  1704. }
  1705. }
  1706. /*
  1707. * Show mail form
  1708. */
  1709. if ($action == 'presend')
  1710. {
  1711. $ref = dol_sanitizeFileName($object->ref);
  1712. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1713. $fileparams = dol_most_recent_file($conf->fournisseur->facture->dir_output.'/'.get_exdir($object->id,2).$ref);
  1714. $file=$fileparams['fullname'];
  1715. // Build document if it not exists
  1716. if (! $file || ! is_readable($file))
  1717. {
  1718. // Define output language
  1719. $outputlangs = $langs;
  1720. $newlang='';
  1721. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id'])) $newlang=$_REQUEST['lang_id'];
  1722. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
  1723. if (! empty($newlang))
  1724. {
  1725. $outputlangs = new Translate();
  1726. $outputlangs->setDefaultLang($newlang);
  1727. }
  1728. $result=supplier_invoice_pdf_create($db, $object, GETPOST('model')?GETPOST('model'):$object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  1729. if ($result <= 0)
  1730. {
  1731. dol_print_error($db,$result);
  1732. exit;
  1733. }
  1734. $fileparams = dol_most_recent_file($conf->fournisseur->facture->dir_output.'/'.get_exdir($object->id,2).$ref);
  1735. $file=$fileparams['fullname'];
  1736. }
  1737. print '<br>';
  1738. print_titre($langs->trans('SendBillByMail'));
  1739. // Cree l'objet formulaire mail
  1740. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  1741. $formmail = new FormMail($db);
  1742. $formmail->fromtype = 'user';
  1743. $formmail->fromid = $user->id;
  1744. $formmail->fromname = $user->getFullName($langs);
  1745. $formmail->frommail = $user->email;
  1746. $formmail->withfrom=1;
  1747. $formmail->withto=empty($_POST["sendto"])?1:$_POST["sendto"];
  1748. $formmail->withtosocid=$soc->id;
  1749. $formmail->withtocc=1;
  1750. $formmail->withtoccsocid=0;
  1751. $formmail->withtoccc=$conf->global->MAIN_EMAIL_USECCC;
  1752. $formmail->withtocccsocid=0;
  1753. $formmail->withtopic=$langs->trans('SendBillRef','__FACREF__');
  1754. $formmail->withfile=2;
  1755. $formmail->withbody=1;
  1756. $formmail->withdeliveryreceipt=1;
  1757. $formmail->withcancel=1;
  1758. // Tableau des substitutions
  1759. $formmail->substit['__FACREF__']=$object->ref;
  1760. $formmail->substit['__SIGNATURE__']=$user->signature;
  1761. $formmail->substit['__PERSONALIZED__']='';
  1762. // Tableau des parametres complementaires
  1763. $formmail->param['action']='send';
  1764. $formmail->param['models']='invoice_supplier_send';
  1765. $formmail->param['facid']=$object->id;
  1766. $formmail->param['returnurl']=$_SERVER["PHP_SELF"].'?id='.$object->id;
  1767. // Init list of files
  1768. if (GETPOST("mode")=='init')
  1769. {
  1770. $formmail->clear_attached_files();
  1771. $formmail->add_attached_files($file,basename($file),dol_mimetype($file));
  1772. }
  1773. // Show form
  1774. $formmail->show_form();
  1775. print '<br>';
  1776. }
  1777. }
  1778. }
  1779. // End of page
  1780. llxFooter();
  1781. $db->close();
  1782. ?>