PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/facture/fiche-rec.php

https://bitbucket.org/speedealing/speedealing
PHP | 595 lines | 409 code | 120 blank | 66 comment | 73 complexity | f365eadcd5c41d747191103a0b21856f MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  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 3 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. $id=(GETPOST('facid','int')?GETPOST('facid','int'):GETPOST('id','int'));
  31. $action=GETPOST('action', 'alpha');
  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', $id, $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. $object = new FactureRec($db);
  47. /*
  48. * Actions
  49. */
  50. // Create predefined invoice
  51. if ($action == 'add')
  52. {
  53. if (! GETPOST('titre'))
  54. {
  55. setEventMessage($langs->trans("ErrorFieldRequired",$langs->trans("Title")), 'errors');
  56. $action = "create";
  57. $error++;
  58. }
  59. if (! $error)
  60. {
  61. $object->titre = GETPOST('titre', 'alpha');
  62. $object->note = GETPOST('comment');
  63. $object->usenewprice = GETPOST('usenewprice');
  64. if ($object->create($user, $id) > 0)
  65. {
  66. $id = $object->id;
  67. $action = '';
  68. }
  69. else
  70. {
  71. setEventMessage($object->error, 'errors');
  72. $action = "create";
  73. }
  74. }
  75. }
  76. // Suppression
  77. if ($action == 'delete' && $user->rights->facture->supprimer)
  78. {
  79. $object->fetch($id);
  80. $object->delete();
  81. $id = 0 ;
  82. }
  83. /*
  84. * View
  85. */
  86. llxHeader('',$langs->trans("RepeatableInvoices"),'ch-facture.html#s-fac-facture-rec');
  87. $form = new Form($db);
  88. /*
  89. * Create mode
  90. */
  91. if ($action == 'create')
  92. {
  93. print_fiche_titre($langs->trans("CreateRepeatableInvoice"));
  94. $invoice = new Facture($db); // Source invoice
  95. $product_static = new Product($db);
  96. if ($invoice->fetch($id) > 0)
  97. {
  98. print '<form action="fiche-rec.php" method="post">';
  99. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  100. print '<input type="hidden" name="action" value="add">';
  101. print '<input type="hidden" name="facid" value="'.$invoice->id.'">';
  102. $rowspan=4;
  103. if (! empty($conf->projet->enabled) && $invoice->fk_project > 0) $rowspan++;
  104. print '<table class="border" width="100%">';
  105. $invoice->fetch_thirdparty();
  106. print '<tr><td>'.$langs->trans("Customer").'</td><td>'.$invoice->client->getNomUrl(1,'customer').'</td>';
  107. print '<td>';
  108. //print $langs->trans("NotePrivate");
  109. print '</td></tr>';
  110. print '<tr><td>'.$langs->trans("Title").'</td><td>';
  111. print '<input class="flat" type="text" name="titre" size="16" value="'.$_POST["titre"].'">';
  112. print '</td>';
  113. print '<td rowspan="'.$rowspan.'" valign="top">';
  114. print '<textarea class="flat" name="note" wrap="soft" cols="60" rows="'.ROWS_4.'"></textarea>';
  115. print '</td></tr>';
  116. print "<tr><td>".$langs->trans("Author")."</td><td>".$user->getFullName($langs)."</td></tr>";
  117. print "<tr><td>".$langs->trans("PaymentConditions")."</td><td>";
  118. $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$invoice->id, $invoice->cond_reglement_id, 'none');
  119. print "</td></tr>";
  120. print "<tr><td>".$langs->trans("PaymentMode")."</td><td>";
  121. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$invoice->id, $facture->mode_reglement_id, 'none');
  122. print "</td></tr>";
  123. if (! empty($conf->projet->enabled) && $invoice->fk_project > 0)
  124. {
  125. print "<tr><td>".$langs->trans("Project")."</td><td>";
  126. if ($invoice->fk_project > 0)
  127. {
  128. $project = new Project($db);
  129. $project->fetch($invoice->fk_project);
  130. print $project->title;
  131. }
  132. print "</td></tr>";
  133. }
  134. print "</table>";
  135. print '<br>';
  136. $title = $langs->trans("ProductsAndServices");
  137. if (empty($conf->service->enabled))
  138. $title = $langs->trans("Products");
  139. else if (empty($conf->product->enabled))
  140. $title = $langs->trans("Services");
  141. print_titre($title);
  142. /*
  143. * Invoice lines
  144. */
  145. print '<table class="notopnoleftnoright" width="100%">';
  146. print '<tr><td colspan="3">';
  147. $sql = 'SELECT l.fk_product, l.product_type, l.label as custom_label, l.description, l.qty, l.rowid, l.tva_tx,';
  148. $sql.= ' l.fk_remise_except,';
  149. $sql.= ' l.remise_percent, l.subprice, l.info_bits,';
  150. $sql.= ' l.total_ht, l.total_tva, l.total_ttc,';
  151. $sql.= ' l.date_start,';
  152. $sql.= ' l.date_end,';
  153. $sql.= ' l.product_type,';
  154. $sql.= ' p.ref, p.fk_product_type, p.label as product_label,';
  155. $sql.= ' p.description as product_desc';
  156. $sql.= " FROM ".MAIN_DB_PREFIX."facturedet as l";
  157. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON l.fk_product = p.rowid";
  158. $sql.= " WHERE l.fk_facture = ".$invoice->id;
  159. $sql.= " ORDER BY l.rowid";
  160. $result = $db->query($sql);
  161. if ($result)
  162. {
  163. $num = $db->num_rows($result);
  164. $i = 0; $total = 0;
  165. echo '<table class="notopnoleftnoright" width="100%">';
  166. if ($num)
  167. {
  168. print '<tr class="liste_titre">';
  169. print '<td width="54%">'.$langs->trans("Description").'</td>';
  170. print '<td width="8%" align="center">'.$langs->trans("VAT").'</td>';
  171. print '<td width="8%" align="center">'.$langs->trans("Qty").'</td>';
  172. print '<td width="8%" align="right">'.$langs->trans("ReductionShort").'</td>';
  173. print '<td width="12%" align="right">'.$langs->trans("PriceU").'</td>';
  174. if (empty($conf->global->PRODUIT_MULTIPRICES)) print '<td width="12%" align="right">'.$langs->trans("CurrentProductPrice").'</td>';
  175. print "</tr>\n";
  176. }
  177. $var=True;
  178. while ($i < $num)
  179. {
  180. $objp = $db->fetch_object($result);
  181. if ($objp->fk_product > 0)
  182. {
  183. $product = New Product($db);
  184. $product->fetch($objp->fk_product);
  185. }
  186. $var=!$var;
  187. print "<tr ".$bc[$var].">";
  188. // Show product and description
  189. $type=(isset($objp->product_type)?$objp->product_type:$objp->fk_product_type);
  190. if ($objp->fk_product > 0)
  191. {
  192. print '<td>';
  193. print '<a name="'.$objp->rowid.'"></a>'; // ancre pour retourner sur la ligne
  194. // Show product and description
  195. $product_static->fetch($objp->fk_product); // We need all information later
  196. $text=$product_static->getNomUrl(1);
  197. $text.= ' - '.(! empty($objp->custom_label)?$objp->custom_label:$objp->product_label);
  198. $description=(! empty($conf->global->PRODUIT_DESC_IN_FORM)?'':dol_htmlentitiesbr($objp->description));
  199. print $form->textwithtooltip($text,$description,3,'','',$i);
  200. // Show range
  201. print_date_range($db->jdate($objp->date_start), $db->jdate($objp->date_end));
  202. // Add description in form
  203. if (! empty($conf->global->PRODUIT_DESC_IN_FORM))
  204. print (! empty($objp->description) && $objp->description!=$objp->product_label)?'<br>'.dol_htmlentitiesbr($objp->description):'';
  205. print '</td>';
  206. }
  207. else
  208. {
  209. print '<td>';
  210. print '<a name="'.$objp->rowid.'"></a>'; // ancre pour retourner sur la ligne
  211. if ($type==1) $text = img_object($langs->trans('Service'),'service');
  212. else $text = img_object($langs->trans('Product'),'product');
  213. if (! empty($objp->custom_label)) {
  214. $text.= ' <strong>'.$objp->custom_label.'</strong>';
  215. print $form->textwithtooltip($text,dol_htmlentitiesbr($objp->description),3,'','',$i);
  216. } else {
  217. print $text.' '.nl2br($objp->description);
  218. }
  219. // Show range
  220. print_date_range($db->jdate($objp->date_start), $db->jdate($objp->date_end));
  221. print "</td>\n";
  222. }
  223. print '<td align="center">'.vatrate($objp->tva_tx).'%</td>';
  224. print '<td align="center">'.$objp->qty.'</td>';
  225. if ($objp->remise_percent > 0)
  226. {
  227. print '<td align="right">'.$objp->remise_percent." %</td>\n";
  228. }
  229. else
  230. {
  231. print '<td>&nbsp;</td>';
  232. }
  233. print '<td align="right">'.price($objp->subprice)."</td>\n";
  234. // Price of product
  235. if (empty($conf->global->PRODUIT_MULTIPRICES))
  236. {
  237. if ($objp->fk_product > 0)
  238. {
  239. $flag_price_may_change++;
  240. $prodprice=$product_static->price; // price HT
  241. print '<td align="right">'.price($prodprice)."</td>\n";
  242. }
  243. else
  244. {
  245. print '<td>&nbsp;</td>';
  246. }
  247. }
  248. print "</tr>";
  249. $i++;
  250. }
  251. $db->free($result);
  252. }
  253. else
  254. {
  255. print $db->error();
  256. }
  257. print "</table>";
  258. print '</td></tr>';
  259. if ($flag_price_may_change)
  260. {
  261. print '<tr><td colspan="3" align="left">';
  262. print '<select name="usenewprice" class="flat">';
  263. print '<option value="0">'.$langs->trans("AlwaysUseFixedPrice").'</option>';
  264. print '<option value="1" disabled="disabled">'.$langs->trans("AlwaysUseNewPrice").'</option>';
  265. print '</select>';
  266. print '</td></tr>';
  267. }
  268. print '<tr><td colspan="3" align="center"><br><input type="submit" class="button" value="'.$langs->trans("Create").'"></td></tr>';
  269. print "</form>\n";
  270. print "</table>\n";
  271. }
  272. else
  273. {
  274. dol_print_error('',"Error, no invoice ".$invoice->id);
  275. }
  276. }
  277. else
  278. {
  279. /*
  280. * View mode
  281. */
  282. if ($id > 0)
  283. {
  284. if ($object->fetch($id) > 0)
  285. {
  286. $object->fetch_thirdparty();
  287. $author = new User($db);
  288. $author->fetch($object->user_author);
  289. dol_fiche_head($head, 'compta', $langs->trans("PredefinedInvoices"),0,'company'); // Add a div
  290. print '<table class="border" width="100%">';
  291. print '<tr><td>'.$langs->trans("Ref").'</td>';
  292. print '<td colspan="4">'.$object->titre.'</td>';
  293. print '<tr><td>'.$langs->trans("Customer").'</td>';
  294. print '<td colspan="3">'.$object->thirdparty->getNomUrl(1,'customer').'</td>';
  295. print "<td>". $langs->trans("PaymentConditions") ." : ";
  296. $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->cond_reglement_id,'none');
  297. print "</td></tr>";
  298. print "<tr><td>".$langs->trans("Author")."</td><td colspan=\"3\">".$author->getFullName($langs)."</td>";
  299. if ($object->remise_percent > 0)
  300. {
  301. print '<td rowspan="5" valign="top">';
  302. }
  303. else
  304. {
  305. print '<td rowspan="4" valign="top">';
  306. }
  307. print $langs->trans("PaymentMode") ." : ";
  308. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id,'none');
  309. print "</td></tr>";
  310. print '<tr><td>'.$langs->trans("AmountHT").'</td>';
  311. print '<td align="right" colspan="2"><b>'.price($object->total_ht).'</b></td>';
  312. print '<td>'.$langs->trans("Currency".$conf->currency).'</td></tr>';
  313. print '<tr><td>'.$langs->trans("AmountVAT").'</td><td align="right" colspan="2">'.price($object->total_tva).'</td>';
  314. print '<td>'.$langs->trans("Currency".$conf->currency).'</td></tr>';
  315. print '<tr><td>'.$langs->trans("AmountTTC").'</td><td align="right" colspan="2">'.price($object->total_ttc).'</td>';
  316. print '<td>'.$langs->trans("Currency".$conf->currency).'</td></tr>';
  317. if ($object->note)
  318. {
  319. print '<tr><td colspan="5">'.$langs->trans("Note").' : '.nl2br($object->note)."</td></tr>";
  320. }
  321. print "</table>";
  322. print '</div>';
  323. /*
  324. * Lines
  325. */
  326. $title = $langs->trans("ProductsAndServices");
  327. if (empty($conf->service->enabled))
  328. $title = $langs->trans("Products");
  329. else if (empty($conf->product->enabled))
  330. $title = $langs->trans("Services");
  331. print_titre($title);
  332. print '<table class="noborder" width="100%">';
  333. print '<tr class="liste_titre">';
  334. print '<td>'.$langs->trans("Description").'</td>';
  335. print '<td align="right">'.$langs->trans("Price").'</td>';
  336. print '<td align="center">'.$langs->trans("ReductionShort").'</td>';
  337. print '<td align="center">'.$langs->trans("Qty").'</td></tr>';
  338. $num = count($object->lines);
  339. $i = 0;
  340. $var=True;
  341. while ($i < $num)
  342. {
  343. $var=!$var;
  344. $product_static=new Product($db);
  345. // Show product and description
  346. $type=(isset($object->lines[$i]->product_type)?$object->lines[$i]->product_type:$object->lines[$i]->fk_product_type);
  347. // Try to enhance type detection using date_start and date_end for free lines when type
  348. // was not saved.
  349. if (! empty($objp->date_start)) $type=1;
  350. if (! empty($objp->date_end)) $type=1;
  351. // Show line
  352. print "<tr $bc[$var]>";
  353. if ($object->lines[$i]->fk_product > 0)
  354. {
  355. print '<td>';
  356. print '<a name="'.$object->lines[$i]->id.'"></a>'; // ancre pour retourner sur la ligne
  357. // Show product and description
  358. $product_static->type=$object->lines[$i]->fk_product_type;
  359. $product_static->id=$object->lines[$i]->fk_product;
  360. $product_static->ref=$object->lines[$i]->product_ref;
  361. $text=$product_static->getNomUrl(1);
  362. $text.= ' - '.(! empty($object->lines[$i]->label)?$object->lines[$i]->label:$object->lines[$i]->product_label);
  363. $description=(! empty($conf->global->PRODUIT_DESC_IN_FORM)?'':dol_htmlentitiesbr($object->lines[$i]->desc));
  364. print $form->textwithtooltip($text,$description,3,'','',$i);
  365. // Show range
  366. print_date_range($object->lines[$i]->date_start, $object->lines[$i]->date_end);
  367. // Add description in form
  368. if (! empty($conf->global->PRODUIT_DESC_IN_FORM))
  369. print (! empty($object->lines[$i]->desc) && $object->lines[$i]->desc!=$fac->lines[$i]->product_label)?'<br>'.dol_htmlentitiesbr($object->lines[$i]->desc):'';
  370. print '</td>';
  371. }
  372. else
  373. {
  374. print '<td>';
  375. if ($type==1) $text = img_object($langs->trans('Service'),'service');
  376. else $text = img_object($langs->trans('Product'),'product');
  377. if (! empty($object->lines[$i]->label)) {
  378. $text.= ' <strong>'.$object->lines[$i]->label.'</strong>';
  379. print $form->textwithtooltip($text,dol_htmlentitiesbr($object->lines[$i]->desc),3,'','',$i);
  380. } else {
  381. print $text.' '.nl2br($object->lines[$i]->desc);
  382. }
  383. // Show range
  384. print_date_range($object->lines[$i]->date_start, $object->lines[$i]->date_end);
  385. print '</td>';
  386. }
  387. print '<td align="right">'.price($object->lines[$i]->price).'</td>';
  388. print '<td align="center">'.$object->lines[$i]->remise_percent.' %</td>';
  389. print '<td align="center">'.$object->lines[$i]->qty.'</td></tr>'."\n";
  390. $i++;
  391. }
  392. print '</table>';
  393. /**
  394. * Barre d'actions
  395. */
  396. print '<div class="tabsAction">';
  397. if ($object->statut == 0 && $user->rights->facture->supprimer)
  398. {
  399. print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?action=delete&id='.$object->id.'">'.$langs->trans('Delete').'</a>';
  400. }
  401. print '</div>';
  402. }
  403. else
  404. {
  405. print $langs->trans("ErrorRecordNotFound");
  406. }
  407. }
  408. else
  409. {
  410. /*
  411. * List mode
  412. */
  413. $sql = "SELECT s.nom, s.rowid as socid, f.titre, f.total, f.rowid as facid";
  414. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture_rec as f";
  415. $sql.= " WHERE f.fk_soc = s.rowid";
  416. $sql.= " AND f.entity = ".$conf->entity;
  417. if ($socid) $sql .= " AND s.rowid = ".$socid;
  418. //$sql .= " ORDER BY $sortfield $sortorder, rowid DESC ";
  419. // $sql .= $db->plimit($limit + 1,$offset);
  420. $result = $db->query($sql);
  421. if ($result)
  422. {
  423. $num = $db->num_rows($result);
  424. print_barre_liste($langs->trans("RepeatableInvoices"),$page,$_SERVER['PHP_SELF'],"&socid=$socid",$sortfield,$sortorder,'',$num);
  425. $i = 0;
  426. print '<table class="noborder" width="100%">';
  427. print '<tr class="liste_titre">';
  428. print '<td>'.$langs->trans("Ref").'</td>';
  429. print_liste_field_titre($langs->trans("Company"),$_SERVER['PHP_SELF'],"s.nom","","&socid=$socid","",$sortfiled,$sortorder);
  430. print '</td><td align="right">'.$langs->trans("Amount").'</td>';
  431. print '<td>&nbsp;</td>';
  432. print "</td>\n";
  433. if ($num > 0)
  434. {
  435. $var=True;
  436. while ($i < min($num,$limit))
  437. {
  438. $objp = $db->fetch_object($result);
  439. $var=!$var;
  440. print "<tr $bc[$var]>";
  441. print '<td><a href="'.$_SERVER['PHP_SELF'].'?id='.$objp->facid.'">'.img_object($langs->trans("ShowBill"),"bill").' '.$objp->titre;
  442. print "</a></td>\n";
  443. print '<td><a href="../fiche.php?socid='.$objp->socid.'">'.$objp->nom.'</a></td>';
  444. print '<td align="right">'.price($objp->total).'</td>'."\n";
  445. if (! $objp->paye)
  446. {
  447. if ($objp->fk_statut == 0)
  448. {
  449. print '<td align="right">'.$langs->trans("Draft").'</td>';
  450. }
  451. else
  452. {
  453. print '<td align="right"><a href="'.DOL_URL_ROOT.'/compta/facture/list.php?filtre=paye:0,fk_statut:1">'.$langs->trans("Validated").'</a></td>';
  454. }
  455. }
  456. else
  457. {
  458. print '<td>&nbsp;</td>';
  459. }
  460. print "</tr>\n";
  461. $i++;
  462. }
  463. }
  464. print "</table>";
  465. $db->free();
  466. }
  467. else
  468. {
  469. dol_print_error($db);
  470. }
  471. }
  472. }
  473. llxFooter();
  474. $db->close();
  475. ?>