PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/compta/facture/fiche-rec.php

https://github.com/asterix14/dolibarr
PHP | 572 lines | 399 code | 108 blank | 65 comment | 64 complexity | 11ff95aeb6a1cadc8dd3d78bcbbefd31 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2010 Regis Houssin <regis@dolibarr.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/compta/facture/fiche-rec.php
  21. * \ingroup facture
  22. * \brief Page to show predefined invoice
  23. */
  24. require("../../main.inc.php");
  25. require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/facture-rec.class.php");
  26. require_once(DOL_DOCUMENT_ROOT."/projet/class/project.class.php");
  27. require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php");
  28. $langs->load('bills');
  29. // Security check
  30. $facid=GETPOST("facid");
  31. $action=GETPOST("action");
  32. if ($user->societe_id) $socid=$user->societe_id;
  33. $objecttype = 'facture_rec';
  34. if ($action == "create" || $action == "add") $objecttype = '';
  35. $result = restrictedArea($user, 'facture', $facid, $objecttype);
  36. if ($page == -1)
  37. {
  38. $page = 0 ;
  39. }
  40. $limit = $conf->liste_limit;
  41. $offset = $limit * $page ;
  42. if ($sortorder == "")
  43. $sortorder="DESC";
  44. if ($sortfield == "")
  45. $sortfield="f.datef";
  46. /*
  47. * Actions
  48. */
  49. // Create predefined invoice
  50. if ($_POST["action"] == 'add')
  51. {
  52. $facturerec = new FactureRec($db);
  53. $facturerec->titre = $_POST["titre"];
  54. $facturerec->note = $_POST["comment"];
  55. if ($facturerec->create($user,$facid) > 0)
  56. {
  57. $facid = $facturerec->id;
  58. $action = '';
  59. }
  60. else
  61. {
  62. $_GET["action"] = "create";
  63. $_GET["facid"] = $_POST["facid"];
  64. $mesg = '<div class="error">'.$facturerec->error.'</div>';
  65. }
  66. }
  67. // Suppression
  68. if ($_REQUEST["action"] == 'delete' && $user->rights->facture->supprimer)
  69. {
  70. $facrec = new FactureRec($db);
  71. $facrec->fetch(GETPOST("facid"));
  72. $facrec->delete();
  73. $facid = 0 ;
  74. }
  75. /*
  76. * View
  77. */
  78. llxHeader('',$langs->trans("RepeatableInvoices"),'ch-facture.html#s-fac-facture-rec');
  79. $form = new Form($db);
  80. /*
  81. * Create mode
  82. */
  83. if ($_GET["action"] == 'create')
  84. {
  85. print_fiche_titre($langs->trans("CreateRepeatableInvoice"));
  86. if ($mesg) print $mesg.'<br>';
  87. $facture = new Facture($db); // Source invoice
  88. $product_static=new Product($db);
  89. if ($facture->fetch($_GET["facid"]) > 0)
  90. {
  91. print '<form action="fiche-rec.php" method="post">';
  92. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  93. print '<input type="hidden" name="action" value="add">';
  94. print '<input type="hidden" name="facid" value="'.$facture->id.'">';
  95. print '<table class="border" width="100%">';
  96. $facture->fetch_thirdparty();
  97. print '<tr><td>'.$langs->trans("Customer").'</td><td>'.$facture->client->getNomUrl(1).'</td>';
  98. print '<td>';
  99. //print $langs->trans("NotePrivate");
  100. print '</td></tr>';
  101. print '<tr><td>'.$langs->trans("Title").'</td><td>';
  102. print '<input class="flat" type="text" name="titre" size="16" value="'.$_POST["titre"].'">';
  103. print '</td>';
  104. print '<td rowspan="4" valign="top">';
  105. print '<textarea class="flat" name="note" wrap="soft" cols="60" rows="'.ROWS_4.'"></textarea>';
  106. print '</td></tr>';
  107. print "<tr><td>".$langs->trans("Author")."</td><td>".$user->getFullName($langs)."</td></tr>";
  108. print "<tr><td>".$langs->trans("PaymentConditions")."</td><td>";
  109. $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?facid='.$facture->id,$facture->cond_reglement_id,'none');
  110. print "</td></tr>";
  111. print "<tr><td>".$langs->trans("PaymentMode")."</td><td>";
  112. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$facture->id,$facture->mode_reglement_id,'none');
  113. print "</td></tr>";
  114. if ($conf->projet->enabled)
  115. {
  116. print "<tr><td>".$langs->trans("Project")."</td><td>";
  117. if ($facture->fk_project > 0)
  118. {
  119. $project = new Project($db);
  120. $project->fetch($facture->fk_project);
  121. print $project->title;
  122. }
  123. print "</td></tr>";
  124. }
  125. print "</table>";
  126. print '<br>';
  127. if ($conf->service->enabled) {
  128. print_titre($langs->trans("ProductsAndServices"));
  129. } else {
  130. print_titre($langs->trans("Products"));
  131. }
  132. /*
  133. * Invoice lines
  134. */
  135. print '<table class="notopnoleftnoright" width="100%">';
  136. print '<tr><td colspan="3">';
  137. $sql = 'SELECT l.fk_product, l.product_type, l.description, l.qty, l.rowid, l.tva_tx,';
  138. $sql.= ' l.fk_remise_except,';
  139. $sql.= ' l.remise_percent, l.subprice, l.info_bits,';
  140. $sql.= ' l.total_ht, l.total_tva, l.total_ttc,';
  141. $sql.= ' l.date_start,';
  142. $sql.= ' l.date_end,';
  143. $sql.= ' l.product_type,';
  144. $sql.= ' p.ref, p.fk_product_type, p.label as product_label,';
  145. $sql.= ' p.description as product_desc';
  146. $sql.= " FROM ".MAIN_DB_PREFIX."facturedet as l";
  147. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON l.fk_product = p.rowid";
  148. $sql.= " WHERE l.fk_facture = ".$facture->id;
  149. $sql.= " ORDER BY l.rowid";
  150. $result = $db->query($sql);
  151. if ($result)
  152. {
  153. $num = $db->num_rows($result);
  154. $i = 0; $total = 0;
  155. echo '<table class="notopnoleftnoright" width="100%">';
  156. if ($num)
  157. {
  158. print "<tr class=\"liste_titre\">";
  159. print '<td width="54%">'.$langs->trans("Description").'</td>';
  160. print '<td width="8%" align="center">'.$langs->trans("VAT").'</td>';
  161. print '<td width="8%" align="center">'.$langs->trans("Qty").'</td>';
  162. print '<td width="8%" align="right">'.$langs->trans("ReductionShort").'</td>';
  163. print '<td width="12%" align="right">'.$langs->trans("PriceU").'</td>';
  164. print '<td width="12%" align="right">N.P.</td>';
  165. print "</tr>\n";
  166. }
  167. $var=True;
  168. while ($i < $num)
  169. {
  170. $objp = $db->fetch_object($result);
  171. if ($objp->fk_product > 0)
  172. {
  173. $product = New Product($db);
  174. $product->fetch($objp->fk_product);
  175. }
  176. $var=!$var;
  177. print "<tr $bc[$var]>";
  178. // Show product and description
  179. $type=$objp->product_type?$objp->product_type:$objp->fk_product_type;
  180. if ($objp->fk_product)
  181. {
  182. print '<td>';
  183. print '<a name="'.$objp->rowid.'"></a>'; // ancre pour retourner sur la ligne
  184. // Show product and description
  185. $product_static->type=$objp->fk_product_type;
  186. $product_static->id=$objp->fk_product;
  187. $product_static->ref=$objp->ref;
  188. $product_static->libelle=$objp->product_label;
  189. $text=$product_static->getNomUrl(1);
  190. $text.= ' - '.$objp->product_label;
  191. $description=($conf->global->PRODUIT_DESC_IN_FORM?'':dol_htmlentitiesbr($objp->description));
  192. print $form->textwithtooltip($text,$description,3,'','',$i);
  193. // Show range
  194. print_date_range($db->jdate($objp->date_start),$db->jdate($objp->date_end));
  195. // Add description in form
  196. if ($conf->global->PRODUIT_DESC_IN_FORM) print ($objp->description && $objp->description!=$objp->product_label)?'<br>'.dol_htmlentitiesbr($objp->description):'';
  197. print '</td>';
  198. }
  199. else
  200. {
  201. print '<td>';
  202. print '<a name="'.$objp->rowid.'"></a>'; // ancre pour retourner sur la ligne
  203. if ($type==1) $text = img_object($langs->trans('Service'),'service');
  204. else $text = img_object($langs->trans('Product'),'product');
  205. print $text.' '.nl2br($objp->description);
  206. // Show range
  207. print_date_range($db->jdate($objp->date_start),$db->jdate($objp->date_end));
  208. print "</td>\n";
  209. }
  210. print '<TD align="center">'.$objp->tva_tx.' %</TD>';
  211. print '<TD align="center">'.$objp->qty.'</TD>';
  212. if ($objp->remise_percent > 0)
  213. {
  214. print '<td align="right">'.$objp->remise_percent." %</td>\n";
  215. }
  216. else
  217. {
  218. print '<td>&nbsp;</td>';
  219. }
  220. print '<TD align="right">'.price($objp->subprice)."</td>\n";
  221. if ($objp->fk_product > 0 && $objp->subprice <> $product->price)
  222. {
  223. print '<td align="right">'.price($product->price)."</td>\n";
  224. $flag_different_price++;
  225. }
  226. else
  227. {
  228. print '<td>&nbsp;</td>';
  229. }
  230. print "</tr>";
  231. $i++;
  232. }
  233. $db->free($result);
  234. }
  235. else
  236. {
  237. print $db->error();
  238. }
  239. print "</table>";
  240. print '</td></tr>';
  241. if ($flag_different_price)
  242. {
  243. print '<tr><td colspan="3" align="left">';
  244. print '<select name="deal_price">';
  245. if ($flag_different_price>1)
  246. {
  247. print '<option value="new">Prendre en compte les nouveaux prix</option>';
  248. print '<option value="old">Utiliser les anciens prix</option>';
  249. }
  250. else
  251. {
  252. print '<option value="new">Prendre en compte le nouveau prix</option>';
  253. print '<option value="old">Utiliser l\'ancien prix</option>';
  254. }
  255. print '</select>';
  256. print '</td></tr>';
  257. }
  258. print '<tr><td colspan="3" align="center"><br><input type="submit" class="button" value="'.$langs->trans("Create").'"></td></tr>';
  259. print "</form>\n";
  260. print "</table>\n";
  261. }
  262. else
  263. {
  264. print "Erreur facture $facture->id inexistante";
  265. }
  266. }
  267. else
  268. {
  269. /*
  270. * View mode
  271. */
  272. if ($facid > 0)
  273. {
  274. $fac = new FactureRec($db);
  275. if ($fac->fetch($facid, $user->societe_id) > 0)
  276. {
  277. $soc = new Societe($db);
  278. $soc->fetch($fac->socid);
  279. $author = new User($db);
  280. $author->fetch($fac->user_author);
  281. dol_fiche_head($head, 'compta', $langs->trans("PredefinedInvoices"),0,'company'); // Add a div
  282. print '<table class="border" width="100%">';
  283. print '<tr><td>'.$langs->trans("Ref").'</td>';
  284. print '<td colspan="4">'.$fac->titre.'</td>';
  285. print '<tr><td>'.$langs->trans("Customer").'</td>';
  286. print '<td colspan="3">'.$soc->getNomUrl(1).'</td>';
  287. print "<td>". $langs->trans("PaymentConditions") ." : ";
  288. $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->cond_reglement_id,'none');
  289. print "</td></tr>";
  290. print "<tr><td>".$langs->trans("Author")."</td><td colspan=\"3\">".$author->getFullName($langs)."</td>";
  291. if ($fac->remise_percent > 0)
  292. {
  293. print '<td rowspan="5" valign="top">';
  294. }
  295. else
  296. {
  297. print '<td rowspan="4" valign="top">';
  298. }
  299. print $langs->trans("PaymentMode") ." : ";
  300. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->mode_reglement_id,'none');
  301. print "</td></tr>";
  302. print '<tr><td>'.$langs->trans("AmountHT").'</td>';
  303. print '<td align="right" colspan="2"><b>'.price($fac->total_ht).'</b></td>';
  304. print '<td>'.$langs->trans("Currency".$conf->monnaie).'</td></tr>';
  305. print '<tr><td>'.$langs->trans("AmountVAT").'</td><td align="right" colspan="2">'.price($fac->total_tva).'</td>';
  306. print '<td>'.$langs->trans("Currency".$conf->monnaie).'</td></tr>';
  307. print '<tr><td>'.$langs->trans("AmountTTC").'</td><td align="right" colspan="2">'.price($fac->total_ttc).'</td>';
  308. print '<td>'.$langs->trans("Currency".$conf->monnaie).'</td></tr>';
  309. if ($fac->note)
  310. {
  311. print '<tr><td colspan="5">'.$langs->trans("Note").' : '.nl2br($fac->note)."</td></tr>";
  312. }
  313. print "</table>";
  314. print '</div>';
  315. /*
  316. * Lines
  317. */
  318. if ($conf->service->enabled) {
  319. print_titre($langs->trans("ProductsAndServices"));
  320. } else {
  321. print_titre($langs->trans("Products"));
  322. }
  323. print '<table class="noborder" width="100%">';
  324. print '<tr class="liste_titre">';
  325. print '<td>'.$langs->trans("Description").'</td>';
  326. print '<td align="right">'.$langs->trans("Price").'</td>';
  327. print '<td align="center">'.$langs->trans("ReductionShort").'</td>';
  328. print '<td align="center">'.$langs->trans("Qty").'</td></tr>';
  329. $num = count($fac->lines);
  330. $i = 0;
  331. $var=True;
  332. while ($i < $num)
  333. {
  334. $var=!$var;
  335. $product_static=new Product($db);
  336. // Show product and description
  337. $type=$fac->lines[$i]->product_type?$fac->lines[$i]->product_type:$fac->lines[$i]->fk_product_type;
  338. // Try to enhance type detection using date_start and date_end for free lines when type
  339. // was not saved.
  340. if (! empty($objp->date_start)) $type=1;
  341. if (! empty($objp->date_end)) $type=1;
  342. // Show line
  343. print "<tr $bc[$var]>";
  344. if ($fac->lines[$i]->fk_product > 0)
  345. {
  346. print '<td>';
  347. print '<a name="'.$fac->lines[$i]->id.'"></a>'; // ancre pour retourner sur la ligne
  348. // Show product and description
  349. $product_static->type=$fac->lines[$i]->fk_product_type;
  350. $product_static->id=$fac->lines[$i]->fk_product;
  351. $product_static->ref=$fac->lines[$i]->product_ref;
  352. $product_static->libelle=$fac->lines[$i]->libelle;
  353. $text=$product_static->getNomUrl(1);
  354. $text.= ' - '.$fac->lines[$i]->libelle;
  355. $description=($conf->global->PRODUIT_DESC_IN_FORM?'':dol_htmlentitiesbr($fac->lines[$i]->desc));
  356. print $form->textwithtooltip($text,$description,3,'','',$i);
  357. // Show range
  358. print_date_range($fac->lines[$i]->date_start,$fac->lines[$i]->date_end);
  359. // Add description in form
  360. if ($conf->global->PRODUIT_DESC_IN_FORM) print ($fac->lines[$i]->desc && $fac->lines[$i]->desc!=$fac->lines[$i]->libelle)?'<br>'.dol_htmlentitiesbr($fac->lines[$i]->desc):'';
  361. print '</td>';
  362. }
  363. else
  364. {
  365. print '<td>';
  366. if ($type==1) $text = img_object($langs->trans('Service'),'service');
  367. else $text = img_object($langs->trans('Product'),'product');
  368. print $text.' '.nl2br($fac->lines[$i]->desc);
  369. // Show range
  370. print_date_range($fac->lines[$i]->date_start,$fac->lines[$i]->date_end);
  371. print '</td>';
  372. }
  373. print "<td align=\"right\">".price($fac->lines[$i]->price)."</td>";
  374. print '<td align="center">'.$fac->lines[$i]->remise_percent.' %</td>';
  375. print "<td align=\"center\">".$fac->lines[$i]->qty."</td></tr>\n";
  376. $i++;
  377. }
  378. print '</table>';
  379. /**
  380. * Barre d'actions
  381. */
  382. print '<div class="tabsAction">';
  383. if ($fac->statut == 0 && $user->rights->facture->supprimer)
  384. {
  385. print '<a class="butActionDelete" href="fiche-rec.php?action=delete&facid='.$fac->id.'">'.$langs->trans('Delete').'</a>';
  386. }
  387. print '</div>';
  388. }
  389. else
  390. {
  391. print $langs->trans("ErrorRecordNotFound");
  392. }
  393. }
  394. else
  395. {
  396. /*
  397. * List mode
  398. */
  399. if ($user->rights->facture->lire)
  400. {
  401. $sql = "SELECT s.nom, s.rowid as socid, f.titre, f.total, f.rowid as facid";
  402. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture_rec as f";
  403. $sql.= " WHERE f.fk_soc = s.rowid";
  404. $sql.= " AND s.entity = ".$conf->entity;
  405. if ($socid) $sql .= " AND s.rowid = ".$socid;
  406. //$sql .= " ORDER BY $sortfield $sortorder, rowid DESC ";
  407. // $sql .= $db->plimit($limit + 1,$offset);
  408. $result = $db->query($sql);
  409. }
  410. if ($result)
  411. {
  412. $num = $db->num_rows($result);
  413. print_barre_liste($langs->trans("RepeatableInvoices"),$page,"fiche-rec.php","&socid=$socid",$sortfield,$sortorder,'',$num);
  414. $i = 0;
  415. print "<table class=\"noborder\" width=\"100%\">";
  416. print '<tr class="liste_titre">';
  417. print '<td>'.$langs->trans("Ref").'</td>';
  418. print_liste_field_titre($langs->trans("Company"),"fiche-rec.php","s.nom","","&socid=$socid","",$sortfiled,$sortorder);
  419. print '</td><td align="right">'.$langs->trans("Amount").'</td>';
  420. print '<td>&nbsp;</td>';
  421. print "</td>\n";
  422. if ($num > 0)
  423. {
  424. $var=True;
  425. while ($i < min($num,$limit))
  426. {
  427. $objp = $db->fetch_object($result);
  428. $var=!$var;
  429. print "<tr $bc[$var]>";
  430. print '<td><a href="fiche-rec.php?facid='.$objp->facid.'">'.img_object($langs->trans("ShowBill"),"bill").' '.$objp->titre;
  431. print "</a></td>\n";
  432. print '<td><a href="../fiche.php?socid='.$objp->socid.'">'.$objp->nom.'</a></td>';
  433. print "<td align=\"right\">".price($objp->total)."</td>\n";
  434. if (! $objp->paye)
  435. {
  436. if ($objp->fk_statut == 0)
  437. {
  438. print '<td align="right">'.$langs->trans("Draft").'</td>';
  439. }
  440. else
  441. {
  442. print '<td align="right"><a href="facture.php?filtre=paye:0,fk_statut:1">'.$langs->trans("Validated").'</a></td>';
  443. }
  444. }
  445. else
  446. {
  447. print '<td>&nbsp;</td>';
  448. }
  449. print "</tr>\n";
  450. $i++;
  451. }
  452. }
  453. print "</table>";
  454. $db->free();
  455. }
  456. else
  457. {
  458. dol_print_error($db);
  459. }
  460. }
  461. }
  462. $db->close();
  463. llxFooter();
  464. ?>