PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/facture/impayees.php

https://bitbucket.org/speedealing/speedealing
PHP | 421 lines | 298 code | 69 blank | 54 comment | 66 complexity | 9bb05eaea82898a7c68dd0fc5c5c4b89 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 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. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/compta/facture/impayees.php
  22. * \ingroup facture
  23. * \brief Page to list and build liste of unpaid invoices
  24. */
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  31. $langs->load("bills");
  32. $id = (GETPOST('facid','int') ? GETPOST('facid','int') : GETPOST('id','int'));
  33. $action = GETPOST('action','alpha');
  34. $option = GETPOST('option');
  35. $diroutputpdf=$conf->facture->dir_output . '/unpaid/temp';
  36. // Security check
  37. if ($user->societe_id) $socid=$user->societe_id;
  38. $result = restrictedArea($user,'facture',$id,'');
  39. /*
  40. * Action
  41. */
  42. if ($action == "builddoc" && $user->rights->facture->lire)
  43. {
  44. if (is_array($_POST['toGenerate']))
  45. {
  46. $arrayofexclusion=array();
  47. foreach($_POST['toGenerate'] as $tmppdf) $arrayofexclusion[]=preg_quote($tmppdf.'.pdf','/');
  48. $factures = dol_dir_list($conf->facture->dir_output,'all',1,implode('|',$arrayofexclusion),'\.meta$|\.png','date',SORT_DESC);
  49. // liste les fichiers
  50. $files = array();
  51. $factures_bak = $factures ;
  52. foreach($_POST['toGenerate'] as $basename){
  53. foreach($factures as $facture){
  54. if(strstr($facture["name"],$basename)){
  55. $files[] = $conf->facture->dir_output.'/'.$basename.'/'.$facture["name"];
  56. }
  57. }
  58. }
  59. // Define output language (Here it is not used because we do only merging existing PDF)
  60. $outputlangs = $langs;
  61. $newlang='';
  62. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang=GETPOST('lang_id');
  63. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang=$object->client->default_lang;
  64. if (! empty($newlang))
  65. {
  66. $outputlangs = new Translate();
  67. $outputlangs->setDefaultLang($newlang);
  68. }
  69. // Create empty PDF
  70. $pdf=pdf_getInstance();
  71. if (class_exists('TCPDF'))
  72. {
  73. $pdf->setPrintHeader(false);
  74. $pdf->setPrintFooter(false);
  75. }
  76. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  77. if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
  78. // Add all others
  79. foreach($files as $file)
  80. {
  81. // Charge un document PDF depuis un fichier.
  82. $pagecount = $pdf->setSourceFile($file);
  83. for ($i = 1; $i <= $pagecount; $i++)
  84. {
  85. $tplidx = $pdf->importPage($i);
  86. $s = $pdf->getTemplatesize($tplidx);
  87. $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
  88. $pdf->useTemplate($tplidx);
  89. }
  90. }
  91. // Create output dir if not exists
  92. dol_mkdir($diroutputpdf);
  93. // Save merged file
  94. $filename=strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid")));
  95. if ($option=='late') $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Late")));
  96. if ($pagecount)
  97. {
  98. $now=dol_now();
  99. $file=$diroutputpdf.'/'.$filename.'_'.dol_print_date($now,'dayhourlog').'.pdf';
  100. $pdf->Output($file,'F');
  101. if (! empty($conf->global->MAIN_UMASK))
  102. @chmod($file, octdec($conf->global->MAIN_UMASK));
  103. }
  104. else
  105. {
  106. $mesg='<div class="error">'.$langs->trans('NoPDFAvailableForChecked').'</div>';
  107. }
  108. }
  109. else
  110. {
  111. $mesg='<div class="error">'.$langs->trans('InvoiceNotChecked').'</div>' ;
  112. }
  113. }
  114. /*
  115. * View
  116. */
  117. $title=$langs->trans("BillsCustomersUnpaid");
  118. if ($option=='late') $title=$langs->trans("BillsCustomersUnpaid");
  119. llxHeader('',$title);
  120. $form = new Form($db);
  121. $formfile = new FormFile($db);
  122. ?>
  123. <script type="text/javascript">
  124. $(document).ready(function() {
  125. $("#checkall").click(function() {
  126. $(".checkformerge").attr('checked', true);
  127. });
  128. $("#checknone").click(function() {
  129. $(".checkformerge").attr('checked', false);
  130. });
  131. });
  132. </script>
  133. <?php
  134. /***************************************************************************
  135. * *
  136. * Mode Liste *
  137. * *
  138. ***************************************************************************/
  139. $now=dol_now();
  140. $search_ref = GETPOST("search_ref");
  141. $search_societe = GETPOST("search_societe");
  142. $search_montant_ht = GETPOST("search_montant_ht");
  143. $search_montant_ttc = GETPOST("search_montant_ttc");
  144. $late = GETPOST("late");
  145. $sortfield = GETPOST("sortfield",'alpha');
  146. $sortorder = GETPOST("sortorder",'alpha');
  147. $page = GETPOST("page",'int');
  148. if ($page == -1) { $page = 0; }
  149. $offset = $conf->liste_limit * $page;
  150. $pageprev = $page - 1;
  151. $pagenext = $page + 1;
  152. if (! $sortfield) $sortfield="f.date_lim_reglement";
  153. if (! $sortorder) $sortorder="ASC";
  154. $limit = $conf->liste_limit;
  155. $sql = "SELECT s.nom, s.rowid as socid";
  156. $sql.= ", f.facnumber, f.increment, f.total as total_ht, f.tva as total_tva, f.total_ttc";
  157. $sql.= ", f.datef as df, f.date_lim_reglement as datelimite";
  158. $sql.= ", f.paye as paye, f.rowid as facid, f.fk_statut, f.type";
  159. $sql.= ", sum(pf.amount) as am";
  160. if (! $user->rights->societe->client->voir && ! $socid) $sql .= ", sc.fk_soc, sc.fk_user ";
  161. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  162. if (! $user->rights->societe->client->voir && ! $socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  163. $sql.= ",".MAIN_DB_PREFIX."facture as f";
  164. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid=pf.fk_facture ";
  165. $sql.= " WHERE f.fk_soc = s.rowid";
  166. $sql.= " AND f.entity = ".$conf->entity;
  167. $sql.= " AND f.type IN (0,1,3) AND f.fk_statut = 1";
  168. $sql.= " AND f.paye = 0";
  169. if ($option == 'late') $sql.=" AND f.date_lim_reglement < '".$db->idate(dol_now() - $conf->facture->client->warning_delay)."'";
  170. if (! $user->rights->societe->client->voir && ! $socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
  171. if (! empty($socid)) $sql .= " AND s.rowid = ".$socid;
  172. if (GETPOST('filtre'))
  173. {
  174. $filtrearr = explode(",", GETPOST('filtre'));
  175. foreach ($filtrearr as $fil)
  176. {
  177. $filt = explode(":", $fil);
  178. $sql .= " AND " . $filt[0] . " = " . $filt[1];
  179. }
  180. }
  181. if ($search_ref) $sql .= " AND f.facnumber LIKE '%".$search_ref."%'";
  182. if ($search_societe) $sql .= " AND s.nom LIKE '%".$search_societe."%'";
  183. if ($search_montant_ht) $sql .= " AND f.total = '".$search_montant_ht."'";
  184. if ($search_montant_ttc) $sql .= " AND f.total_ttc = '".$search_montant_ttc."'";
  185. if (GETPOST('sf_ref')) $sql .= " AND f.facnumber LIKE '%".GETPOST('sf_ref') . "%'";
  186. $sql.= " GROUP BY f.facnumber,f.increment,f.total,f.total_ttc,f.datef, f.date_lim_reglement,f.paye, f.rowid, f.fk_statut, f.type,s.nom, s.rowid";
  187. if (! $user->rights->societe->client->voir && ! $socid) $sql .= ", sc.fk_soc, sc.fk_user ";
  188. $sql.= " ORDER BY ";
  189. $listfield=explode(',',$sortfield);
  190. foreach ($listfield as $key => $value) $sql.=$listfield[$key]." ".$sortorder.",";
  191. $sql.= " f.facnumber DESC";
  192. //$sql .= $db->plimit($limit+1,$offset);
  193. $resql = $db->query($sql);
  194. if ($resql)
  195. {
  196. $num = $db->num_rows($resql);
  197. if (! empty($socid))
  198. {
  199. $soc = new Societe($db);
  200. $soc->fetch($socid);
  201. }
  202. $param="";
  203. $param.=(! empty($socid)?"&amp;socid=".$socid:"");
  204. $param.=(! empty($option)?"&amp;option=".$option:"");
  205. if ($search_ref) $param.='&amp;search_ref='.urlencode($search_ref);
  206. if ($search_societe) $param.='&amp;search_societe='.urlencode($search_societe);
  207. if ($search_montant_ht) $param.='&amp;search_montant_ht='.urlencode($search_montant_ht);
  208. if ($search_montant_ttc) $param.='&amp;search_montant_ttc='.urlencode($search_montant_ttc);
  209. if ($late) $param.='&amp;late='.urlencode($late);
  210. $urlsource=$_SERVER['PHP_SELF'].'?sortfield='.$sortfield.'&sortorder='.$sortorder;
  211. $urlsource.=str_replace('&amp;','&',$param);
  212. $titre=(! empty($socid)?$langs->trans("BillsCustomersUnpaidForCompany",$soc->nom):$langs->trans("BillsCustomersUnpaid"));
  213. if ($option == 'late') $titre.=' ('.$langs->trans("Late").')';
  214. else $titre.=' ('.$langs->trans("All").')';
  215. $link='';
  216. if (empty($option)) $link='<a href="'.$_SERVER["PHP_SELF"].'?option=late">'.$langs->trans("ShowUnpaidLateOnly").'</a>';
  217. elseif ($option == 'late') $link='<a href="'.$_SERVER["PHP_SELF"].'">'.$langs->trans("ShowUnpaidAll").'</a>';
  218. print_fiche_titre($titre,$link);
  219. //print_barre_liste($titre,$page,$_SERVER["PHP_SELF"],$param,$sortfield,$sortorder,'',0); // We don't want pagination on this page
  220. dol_htmloutput_mesg($mesg);
  221. $i = 0;
  222. print '<table class="liste" width="100%">';
  223. print '<tr class="liste_titre">';
  224. print_liste_field_titre($langs->trans("Ref"),$_SERVER["PHP_SELF"],"f.facnumber","",$param,"",$sortfield,$sortorder);
  225. print_liste_field_titre($langs->trans("Date"),$_SERVER["PHP_SELF"],"f.datef","",$param,'align="center"',$sortfield,$sortorder);
  226. print_liste_field_titre($langs->trans("DateDue"),$_SERVER["PHP_SELF"],"f.date_lim_reglement","",$param,'align="center"',$sortfield,$sortorder);
  227. print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","",$param,"",$sortfield,$sortorder);
  228. print_liste_field_titre($langs->trans("AmountHT"),$_SERVER["PHP_SELF"],"f.total","",$param,'align="right"',$sortfield,$sortorder);
  229. print_liste_field_titre($langs->trans("AmountVAT"),$_SERVER["PHP_SELF"],"f.tva","",$param,'align="right"',$sortfield,$sortorder);
  230. print_liste_field_titre($langs->trans("AmountTTC"),$_SERVER["PHP_SELF"],"f.total_ttc","",$param,'align="right"',$sortfield,$sortorder);
  231. print_liste_field_titre($langs->trans("Received"),$_SERVER["PHP_SELF"],"am","",$param,'align="right"',$sortfield,$sortorder);
  232. print_liste_field_titre($langs->trans("Rest"),$_SERVER["PHP_SELF"],"am","",$param,'align="right"',$sortfield,$sortorder);
  233. print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"fk_statut,paye,am","",$param,'align="right"',$sortfield,$sortorder);
  234. print_liste_field_titre($langs->trans("Merge"),$_SERVER["PHP_SELF"],"","",$param,'align="center"',$sortfield,$sortorder);
  235. print "</tr>\n";
  236. // Lignes des champs de filtre
  237. print '<form method="GET" action="'.$_SERVER["PHP_SELF"].'">';
  238. print '<tr class="liste_titre">';
  239. // Ref
  240. print '<td class="liste_titre">';
  241. print '<input class="flat" size="10" type="text" name="search_ref" value="'.$search_ref.'"></td>';
  242. print '<td class="liste_titre">&nbsp;</td>';
  243. print '<td class="liste_titre">&nbsp;</td>';
  244. print '<td class="liste_titre" align="left"><input class="flat" type="text" size="12" name="search_societe" value="'.$search_societe.'"></td>';
  245. print '<td class="liste_titre" align="right"><input class="flat" type="text" size="10" name="search_montant_ht" value="'.$search_montant_ht.'"></td>';
  246. print '<td class="liste_titre">&nbsp;</td>';
  247. print '<td class="liste_titre" align="right"><input class="flat" type="text" size="10" name="search_montant_ttc" value="'.$search_montant_ttc.'"></td>';
  248. print '<td class="liste_titre" colspan="3" align="right">';
  249. print '<input type="image" class="liste_titre" name="button_search" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/search.png" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
  250. print '</td>';
  251. print '<td class="liste_titre" align="center">';
  252. if ($conf->use_javascript_ajax) print '<a href="#" id="checkall">'.$langs->trans("All").'</a> / <a href="#" id="checknone">'.$langs->trans("None").'</a>';
  253. print '</td>';
  254. print "</tr>\n";
  255. print '</form>';
  256. if ($num > 0)
  257. {
  258. $var=True;
  259. $total_ht=0;
  260. $total_tva=0;
  261. $total_ttc=0;
  262. $total_paid=0;
  263. $facturestatic=new Facture($db);
  264. print '<form id="form_generate_pdf" method="POST" action="'.$_SERVER["PHP_SELF"].'?sortfield='. $sortfield .'&sortorder='. $sortorder .'">';
  265. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  266. while ($i < $num)
  267. {
  268. $objp = $db->fetch_object($resql);
  269. $date_limit=$db->jdate($objp->datelimite);
  270. $var=!$var;
  271. print "<tr ".$bc[$var].">";
  272. $classname = "impayee";
  273. print '<td nowrap="nowrap">';
  274. $facturestatic->id=$objp->facid;
  275. $facturestatic->ref=$objp->facnumber;
  276. $facturestatic->type=$objp->type;
  277. print '<table class="nobordernopadding"><tr class="nocellnopadd">';
  278. // Ref
  279. print '<td width="100" class="nobordernopadding" nowrap="nowrap">';
  280. print $facturestatic->getNomUrl(1);
  281. print '</td>';
  282. // Warning picto
  283. print '<td width="20" class="nobordernopadding" nowrap="nowrap">';
  284. if ($date_limit < ($now - $conf->facture->client->warning_delay) && ! $objp->paye && $objp->fk_statut == 1) print img_warning($langs->trans("Late"));
  285. print '</td>';
  286. // PDF Picto
  287. print '<td width="16" align="right" class="nobordernopadding">';
  288. $filename=dol_sanitizeFileName($objp->facnumber);
  289. $filedir=$conf->facture->dir_output . '/' . dol_sanitizeFileName($objp->facnumber);
  290. print $formfile->getDocumentsLink($facturestatic->element, $filename, $filedir);
  291. print '</td>';
  292. print '</tr></table>';
  293. print "</td>\n";
  294. print '<td nowrap align="center">'.dol_print_date($db->jdate($objp->df),'day').'</td>'."\n";
  295. print '<td nowrap align="center">'.dol_print_date($db->jdate($objp->datelimite),'day').'</td>'."\n";
  296. print '<td><a href="'.DOL_URL_ROOT.'/comm/fiche.php?socid='.$objp->socid.'">'.img_object($langs->trans("ShowCompany"),"company").' '.dol_trunc($objp->nom,28).'</a></td>';
  297. print '<td align="right">'.price($objp->total_ht).' '.$langs->getCurrencySymbol($conf->currency).'</td>';
  298. print '<td align="right">'.price($objp->total_tva).' '.$langs->getCurrencySymbol($conf->currency).'</td>';
  299. print '<td align="right">'.price($objp->total_ttc).' '.$langs->getCurrencySymbol($conf->currency).'</td>';
  300. print '<td align="right">'.(! empty($objp->am)?price($objp->am).' '.$langs->getCurrencySymbol($conf->currency):'&nbsp;').'</td>';
  301. print '<td align="right">'.(! empty($objp->am)?price($objp->total_ttc-$objp->am).' '.$langs->getCurrencySymbol($conf->currency):'&nbsp;').'</td>';
  302. // Affiche statut de la facture
  303. print '<td align="right" nowrap="nowrap">';
  304. print $facturestatic->LibStatut($objp->paye,$objp->fk_statut,5,$objp->am);
  305. print '</td>';
  306. // Checkbox
  307. print '<td align="center">';
  308. if (! empty($formfile->numoffiles))
  309. print '<input id="cb'.$objp->facid.'" class="flat checkformerge" type="checkbox" name="toGenerate[]" value="'.$objp->facnumber.'">';
  310. else
  311. print '&nbsp;';
  312. print '</td>' ;
  313. print "</tr>\n";
  314. $total_ht+=$objp->total_ht;
  315. $total_tva+=$objp->total_tva;
  316. $total_ttc+=$objp->total_ttc;
  317. $total_paid+=$objp->am;
  318. $i++;
  319. }
  320. print '<tr class="liste_total">';
  321. print '<td colspan="4" align="left">'.$langs->trans("Total").'</td>';
  322. print '<td align="right"><b>'.price($total_ht).' '.$langs->getCurrencySymbol($conf->currency).'</b></td>';
  323. print '<td align="right"><b>'.price($total_tva).' '.$langs->getCurrencySymbol($conf->currency).'</b></td>';
  324. print '<td align="right"><b>'.price($total_ttc).' '.$langs->getCurrencySymbol($conf->currency).'</b></td>';
  325. print '<td align="right"><b>'.price($total_paid).' '.$langs->getCurrencySymbol($conf->currency).'</b></td>';
  326. print '<td align="center">&nbsp;</td>';
  327. print '<td align="center">&nbsp;</td>';
  328. print '<td align="center">&nbsp;</td>';
  329. print "</tr>\n";
  330. }
  331. print "</table>";
  332. /*
  333. * Show list of available documents
  334. */
  335. $filedir=$diroutputpdf;
  336. if ($search_ref) print '<input type="hidden" name="search_ref" value="'.$search_ref.'">';
  337. if ($search_societe) print '<input type="hidden" name="search_societe" value="'.$search_societe.'">';
  338. if ($search_montant_ht) print '<input type="hidden" name="search_montant_ht" value="'.$search_montant_ht.'">';
  339. if ($search_montant_ttc) print '<input type="hidden" name="search_montant_ttc" value="'.$search_montant_ttc.'">';
  340. if ($late) print '<input type="hidden" name="late" value="'.$late.'">';
  341. $genallowed=$user->rights->facture->lire;
  342. $delallowed=$user->rights->facture->lire;
  343. print '<br>';
  344. print '<input type="hidden" name="option" value="'.$option.'">';
  345. $formfile->show_documents('unpaid','',$filedir,$urlsource,$genallowed,$delallowed,'',1,0,0,48,1,$param,$langs->trans("PDFMerge"),$langs->trans("PDFMerge"));
  346. print '</form>';
  347. $db->free($resql);
  348. }
  349. else dol_print_error($db,'');
  350. llxFooter();
  351. $db->close();
  352. ?>