impayees.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  4. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  6. * Copyright (C) 2012 Vinicius Nogueira <viniciusvgn@gmail.com>
  7. * Copyright (C) 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/impayees.php
  24. * \ingroup facture
  25. * \brief Page to list all unpaid invoices
  26. */
  27. require '../../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  31. if (! $user->rights->fournisseur->facture->lire) accessforbidden();
  32. $langs->load("companies");
  33. $langs->load("bills");
  34. $socid=GETPOST('socid','int');
  35. $option = GETPOST('option');
  36. // Security check
  37. if ($user->societe_id > 0)
  38. {
  39. $action = '';
  40. $socid = $user->societe_id;
  41. }
  42. $sortfield = GETPOST("sortfield",'alpha');
  43. $sortorder = GETPOST("sortorder",'alpha');
  44. $search_ref = GETPOST('search_ref','alpha');
  45. $search_ref_supplier = GETPOST('search_ref_supplier','alpha');
  46. $search_company = GETPOST('search_company','alpha');
  47. $search_amount_no_tax = GETPOST('search_amount_no_tax','alpha');
  48. $search_amount_all_tax = GETPOST('search_amount_all_tax','alpha');
  49. $page = GETPOST("page",'int');
  50. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  51. $offset = $conf->liste_limit * $page;
  52. $pageprev = $page - 1;
  53. $pagenext = $page + 1;
  54. if (! $sortfield) $sortfield="f.date_lim_reglement";
  55. if (! $sortorder) $sortorder="ASC";
  56. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter")) // Both test are required to be compatible with all browsers
  57. {
  58. $search_ref="";
  59. $search_ref_supplier="";
  60. $search_company="";
  61. $search_amount_no_tax="";
  62. $search_amount_all_tax="";
  63. }
  64. /*
  65. * View
  66. */
  67. $now=dol_now();
  68. llxHeader('',$langs->trans("BillsSuppliersUnpaid"));
  69. $title=$langs->trans("BillsSuppliersUnpaid");
  70. $facturestatic=new FactureFournisseur($db);
  71. $companystatic=new Societe($db);
  72. if ($user->rights->fournisseur->facture->lire)
  73. {
  74. $sql = "SELECT s.rowid as socid, s.nom as name,";
  75. $sql.= " f.rowid, f.ref, f.ref_supplier, f.total_ht, f.total_ttc,";
  76. $sql.= " f.datef as df, f.date_lim_reglement as datelimite, ";
  77. $sql.= " f.paye as paye, f.rowid as facid, f.fk_statut";
  78. $sql.= " ,sum(pf.amount) as am";
  79. if (! $user->rights->societe->client->voir && ! $socid) $sql .= ", sc.fk_soc, sc.fk_user ";
  80. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  81. if (! $user->rights->societe->client->voir && ! $socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  82. $sql.= ",".MAIN_DB_PREFIX."facture_fourn as f";
  83. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf ON f.rowid=pf.fk_facturefourn ";
  84. $sql.= " WHERE f.entity = ".$conf->entity;
  85. $sql.= " AND f.fk_soc = s.rowid";
  86. $sql.= " AND f.paye = 0 AND f.fk_statut = 1";
  87. if ($option == 'late') $sql.=" AND f.date_lim_reglement < '".$db->idate(dol_now() - $conf->facture->fournisseur->warning_delay)."'";
  88. if (! $user->rights->societe->client->voir && ! $socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
  89. if ($socid) $sql .= " AND s.rowid = ".$socid;
  90. if (GETPOST('filtre'))
  91. {
  92. $filtrearr = explode(",", GETPOST('filtre'));
  93. foreach ($filtrearr as $fil)
  94. {
  95. $filt = explode(":", $fil);
  96. $sql .= " AND " . $filt[0] . " = " . $filt[1];
  97. }
  98. }
  99. if ($search_ref)
  100. {
  101. $sql .= " AND f.ref LIKE '%".$search_ref."%'";
  102. }
  103. if ($search_ref_supplier)
  104. {
  105. $sql .= " AND f.ref_supplier LIKE '%".$search_ref_supplier."%'";
  106. }
  107. if ($search_company)
  108. {
  109. $sql .= " AND s.nom LIKE '%".$search_company."%'";
  110. }
  111. if ($search_amount_no_tax)
  112. {
  113. $sql .= " AND f.total_ht = '".$search_amount_no_tax."'";
  114. }
  115. if ($search_amount_all_tax)
  116. {
  117. $sql .= " AND f.total_ttc = '".$search_amount_all_tax."'";
  118. }
  119. if (dol_strlen(GETPOST('sf_re')) > 0)
  120. {
  121. $sql .= " AND f.ref_supplier LIKE '%".GETPOST('sf_re')."%'";
  122. }
  123. $sql.= " GROUP BY s.rowid, s.nom, f.rowid, f.ref, f.ref_supplier, f.total_ht, f.total_ttc, f.datef, f.date_lim_reglement, f.paye, f.fk_statut";
  124. if (! $user->rights->societe->client->voir && ! $socid) $sql .= ", sc.fk_soc, sc.fk_user ";
  125. $sql.=$db->order($sortfield,$sortorder);
  126. if (! in_array("f.ref_supplier",explode(',',$sortfield))) $sql.= ", f.ref_supplier DESC";
  127. $resql = $db->query($sql);
  128. if ($resql)
  129. {
  130. $num = $db->num_rows($resql);
  131. if ($socid)
  132. {
  133. $soc = new Societe($db);
  134. $soc->fetch($socid);
  135. }
  136. $param ='';
  137. if ($socid) $param.="&socid=".$socid;
  138. if ($search_ref) $param.='&amp;search_ref='.urlencode($search_ref);
  139. if ($search_ref_supplier) $param.='&amp;search_ref_supplier='.urlencode($search_ref_supplier);
  140. if ($search_company) $param.='&amp;search_company='.urlencode($search_company);
  141. if ($search_amount_no_tax) $param.='&amp;search_amount_no_tax='.urlencode($search_amount_no_tax);
  142. if ($search_amount_all_tax) $param.='&amp;search_amount_all_tax='.urlencode($search_amount_all_tax);
  143. $param.=($option?"&option=".$option:"");
  144. if (! empty($late)) $param.='&late='.urlencode($late);
  145. $urlsource=str_replace('&amp;','&',$param);
  146. $titre=($socid?$langs->trans("BillsSuppliersUnpaidForCompany",$soc->name):$langs->trans("BillsSuppliersUnpaid"));
  147. if ($option == 'late') $titre.=' ('.$langs->trans("Late").')';
  148. else $titre.=' ('.$langs->trans("All").')';
  149. $link='';
  150. if (empty($option)) $link='<a href="'.$_SERVER["PHP_SELF"].'?option=late'.($socid?'&socid='.$socid:'').'">'.$langs->trans("ShowUnpaidLateOnly").'</a>';
  151. elseif ($option == 'late') $link='<a href="'.$_SERVER["PHP_SELF"].'?'.($socid?'&socid='.$socid:'').'">'.$langs->trans("ShowUnpaidAll").'</a>';
  152. print load_fiche_titre($titre,$link);
  153. print_barre_liste('','',$_SERVER["PHP_SELF"],$param,$sortfield,$sortorder,'',0); // We don't want pagination on this page
  154. $i = 0;
  155. print '<form method="get" action="'.$_SERVER["PHP_SELF"].'">';
  156. print '<table class="liste" width="100%">';
  157. print '<tr class="liste_titre">';
  158. print_liste_field_titre($langs->trans("Ref"),$_SERVER["PHP_SELF"],"f.rowid","",$param,"",$sortfield,$sortorder);
  159. print_liste_field_titre($langs->trans("RefSupplier"),$_SERVER["PHP_SELF"],"f.ref_supplier","",$param,"",$sortfield,$sortorder);
  160. print_liste_field_titre($langs->trans("Date"),$_SERVER["PHP_SELF"],"f.datef","",$param,'align="center"',$sortfield,$sortorder);
  161. print_liste_field_titre($langs->trans("DateDue"),$_SERVER["PHP_SELF"],"f.date_lim_reglement","",$param,'align="center"',$sortfield,$sortorder);
  162. print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","",$param,"",$sortfield,$sortorder);
  163. print_liste_field_titre($langs->trans("AmountHT"),$_SERVER["PHP_SELF"],"f.total_ht","",$param,'align="right"',$sortfield,$sortorder);
  164. print_liste_field_titre($langs->trans("AmountTTC"),$_SERVER["PHP_SELF"],"f.total_ttc","",$param,'align="right"',$sortfield,$sortorder);
  165. print_liste_field_titre($langs->trans("AlreadyPaid"),$_SERVER["PHP_SELF"],"am","",$param,'align="right"',$sortfield,$sortorder);
  166. print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"fk_statut,paye,am","",$param,'align="right"',$sortfield,$sortorder);
  167. print "</tr>\n";
  168. // Lines with filter fields
  169. print '<tr class="liste_titre">';
  170. print '<td class="liste_titre">';
  171. print '<input class="flat" size="8" type="text" name="search_ref" value="'.$search_ref.'"></td>';
  172. print '<td class="liste_titre">';
  173. print '<input class="flat" size="8" type="text" name="search_ref_supplier" value="'.$search_ref_supplier.'"></td>';
  174. print '<td class="liste_titre">&nbsp;</td>';
  175. print '<td class="liste_titre">&nbsp;</td>';
  176. print '<td class="liste_titre" align="left">';
  177. print '<input class="flat" type="text" size="6" name="search_company" value="'.$search_company.'">';
  178. print '</td><td class="liste_titre" align="right">';
  179. print '<input class="flat" type="text" size="8" name="search_amount_no_tax" value="'.$search_amount_no_tax.'">';
  180. print '</td><td class="liste_titre" align="right">';
  181. print '<input class="flat" type="text" size="8" name="search_amount_all_tax" value="'.$search_amount_all_tax.'">';
  182. print '</td>';
  183. print '<td class="liste_titre" align="right">';
  184. $searchpicto=$form->showFilterAndCheckAddButtons(0);
  185. print $searchpicto;
  186. print '</td>';
  187. print "</tr>\n";
  188. if ($num > 0)
  189. {
  190. $var=True;
  191. $total_ht=0;
  192. $total_ttc=0;
  193. $total_paid=0;
  194. while ($i < $num)
  195. {
  196. $objp = $db->fetch_object($resql);
  197. $facturestatic->statut = $objp->fk_statut;
  198. $facturestatic->date_echeance = $db->jdate($objp->datelimite);
  199. print '<tr class="oddeven">';
  200. $classname = "impayee";
  201. print '<td class="nowrap">';
  202. $facturestatic->id=$objp->facid;
  203. $facturestatic->ref=$objp->ref;
  204. print $facturestatic->getNomUrl(1);
  205. print "</td>\n";
  206. print '<td class="nowrap">'.dol_trunc($objp->ref_supplier,12).'</td>';
  207. print '<td class="nowrap" align="center">'.dol_print_date($db->jdate($objp->df),'day')."</td>\n";
  208. print '<td class="nowrap" align="center">'.dol_print_date($db->jdate($objp->datelimite),'day');
  209. if ($facturestatic->hasDelay()) {
  210. print img_warning($langs->trans("Late"));
  211. }
  212. print "</td>\n";
  213. print '<td>';
  214. $companystatic->id=$objp->socid;
  215. $companystatic->name=$objp->name;
  216. print $companystatic->getNomUrl(1,'supplier',32);
  217. print '</td>';
  218. print "<td align=\"right\">".price($objp->total_ht)."</td>";
  219. print "<td align=\"right\">".price($objp->total_ttc)."</td>";
  220. print "<td align=\"right\">".price($objp->am)."</td>";
  221. // Show invoice status
  222. print '<td align="right" class="nowrap">';
  223. print $facturestatic->LibStatut($objp->paye,$objp->fk_statut,5,$objp->am);
  224. print '</td>';
  225. print "</tr>\n";
  226. $total_ht+=$objp->total_ht;
  227. $total_ttc+=$objp->total_ttc;
  228. $total_paid+=$objp->am;
  229. $i++;
  230. }
  231. print '<tr class="liste_total">';
  232. print "<td colspan=\"5\" align=\"left\">".$langs->trans("Total").": </td>";
  233. print "<td align=\"right\"><b>".price($total_ht)."</b></td>";
  234. print "<td align=\"right\"><b>".price($total_ttc)."</b></td>";
  235. print "<td align=\"right\"><b>".price($total_paid)."</b></td>";
  236. print '<td align="center">&nbsp;</td>';
  237. print "</tr>\n";
  238. }
  239. print "</table>";
  240. print '</form>';
  241. $db->free($resql);
  242. }
  243. else
  244. {
  245. dol_print_error($db);
  246. }
  247. }
  248. // End of page
  249. llxFooter();
  250. $db->close();