invoice2.lib.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /*
  3. * Copyright (C) 2009-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/lib/invoice2.lib.php
  20. * \ingroup facture
  21. * \brief Function to rebuild PDF and merge PDF files into one
  22. */
  23. require_once(DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php');
  24. require_once(DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php');
  25. /**
  26. * Function to build a compiled PDF
  27. *
  28. * @param DoliDB $db Database handler
  29. * @param Translate $langs Object langs
  30. * @param Conf $conf Object conf
  31. * @param string $diroutputpdf Dir to output file
  32. * @param string $newlangid Lang id
  33. * @param array $filter Array with filters
  34. * @param date $dateafterdate Invoice after date
  35. * @param date $datebeforedate Invoice before date
  36. * @param date $paymentdateafter Payment after date (must includes hour)
  37. * @param date $paymentdatebefore Payment before date (must includes hour)
  38. * @param int $usestdout Add information onto standard output
  39. * @param int $regenerate ''=Use existing PDF files, 'nameofpdf'=Regenerate all PDF files using the template
  40. * @param string $option Suffix to add into file name of generated PDF
  41. * @param string $paymentbankid Only if payment on this bank account id
  42. * @param array $thirdpartiesid List of thirdparties id when using filter excludethirdpartiesid or onlythirdpartiesid
  43. * @return int Error code
  44. */
  45. function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, $usestdout, $regenerate=0, $option='', $paymentbankid='', $thirdpartiesid='')
  46. {
  47. $sql = "SELECT DISTINCT f.rowid, f.facnumber";
  48. $sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
  49. $sqlwhere='';
  50. $sqlorder='';
  51. if (in_array('all',$filter))
  52. {
  53. $sqlorder = " ORDER BY f.facnumber ASC";
  54. }
  55. if (in_array('date',$filter))
  56. {
  57. if (empty($sqlwhere)) $sqlwhere=' WHERE ';
  58. else $sqlwhere.=" AND";
  59. $sqlwhere.= " f.fk_statut > 0";
  60. $sqlwhere.= " AND f.datef >= '".$db->idate($dateafterdate)."'";
  61. $sqlwhere.= " AND f.datef <= '".$db->idate($datebeforedate)."'";
  62. $sqlorder = " ORDER BY f.datef ASC";
  63. }
  64. if (in_array('nopayment',$filter))
  65. {
  66. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
  67. if (empty($sqlwhere)) $sqlwhere=' WHERE ';
  68. else $sqlwhere.=" AND";
  69. $sqlwhere.= " f.fk_statut > 0";
  70. $sqlwhere.= " AND pf.fk_paiement IS NULL";
  71. }
  72. if (in_array('payments',$filter) || in_array('bank',$filter))
  73. {
  74. $sql.= ", ".MAIN_DB_PREFIX."paiement_facture as pf, ".MAIN_DB_PREFIX."paiement as p";
  75. if (in_array('bank',$filter)) $sql.= ", ".MAIN_DB_PREFIX."bank as b";
  76. if (empty($sqlwhere)) $sqlwhere=' WHERE ';
  77. else $sqlwhere.=" AND";
  78. $sqlwhere.= " f.fk_statut > 0";
  79. $sqlwhere.= " AND f.rowid = pf.fk_facture";
  80. $sqlwhere.= " AND pf.fk_paiement = p.rowid";
  81. if (in_array('payments',$filter))
  82. {
  83. $sqlwhere.= " AND p.datep >= '".$db->idate($paymentdateafter)."'";
  84. $sqlwhere.= " AND p.datep <= '".$db->idate($paymentdatebefore)."'";
  85. }
  86. if (in_array('bank',$filter))
  87. {
  88. $sqlwhere.= " AND p.fk_bank = b.rowid";
  89. $sqlwhere.= " AND b.fk_account = ".$paymentbankid;
  90. }
  91. $sqlorder = " ORDER BY p.datep ASC";
  92. }
  93. if (in_array('nodeposit',$filter))
  94. {
  95. if (empty($sqlwhere)) $sqlwhere=' WHERE ';
  96. else $sqlwhere.=" AND";
  97. $sqlwhere.=' type <> 3';
  98. }
  99. if (in_array('noreplacement',$filter))
  100. {
  101. if (empty($sqlwhere)) $sqlwhere=' WHERE ';
  102. else $sqlwhere.=" AND";
  103. $sqlwhere.=' type <> 1';
  104. }
  105. if (in_array('nocreditnote',$filter))
  106. {
  107. if (empty($sqlwhere)) $sqlwhere=' WHERE ';
  108. else $sqlwhere.=" AND";
  109. $sqlwhere.=' type <> 2';
  110. }
  111. if (in_array('excludethirdparties',$filter) && is_array($thirdpartiesid))
  112. {
  113. if (empty($sqlwhere)) $sqlwhere=' WHERE ';
  114. else $sqlwhere.=" AND";
  115. $sqlwhere.=' f.fk_soc NOT IN ('.join(',',$thirdpartiesid).')';
  116. }
  117. if (in_array('onlythirdparties',$filter) && is_array($thirdpartiesid))
  118. {
  119. if (empty($sqlwhere)) $sqlwhere=' WHERE ';
  120. else $sqlwhere.=" AND";
  121. $sqlwhere.=' f.fk_soc IN ('.join(',',$thirdpartiesid).')';
  122. }
  123. if ($sqlwhere) $sql.=$sqlwhere;
  124. if ($sqlorder) $sql.=$sqlorder;
  125. //print $sql; exit;
  126. dol_syslog("scripts/invoices/rebuild_merge.php: sql=".$sql);
  127. if ($usestdout) print '--- start'."\n";
  128. // Start of transaction
  129. //$db->begin();
  130. $error = 0;
  131. $result = 0;
  132. $files = array() ; // liste les fichiers
  133. dol_syslog("scripts/invoices/rebuild_merge.php sql=".$sql);
  134. if ( $resql=$db->query($sql) )
  135. {
  136. $num = $db->num_rows($resql);
  137. $cpt = 0;
  138. $oldemail = '';
  139. $message = '';
  140. $total = '';
  141. if ($num)
  142. {
  143. // First loop on each resultset to build PDF
  144. // -----------------------------------------
  145. while ($cpt < $num)
  146. {
  147. $obj = $db->fetch_object($resql);
  148. $fac = new Facture($db);
  149. $result=$fac->fetch($obj->rowid);
  150. if ($result > 0)
  151. {
  152. $outputlangs = $langs;
  153. if (! empty($newlangid))
  154. {
  155. if ($outputlangs->defaultlang != $newlangid)
  156. {
  157. $outputlangs = new Translate("",$conf);
  158. $outputlangs->setDefaultLang($newlangid);
  159. }
  160. }
  161. $filename=$conf->facture->dir_output.'/'.$fac->ref.'/'.$fac->ref.'.pdf';
  162. if ($regenerate || ! dol_is_file($filename))
  163. {
  164. if ($usestdout) print "Build PDF for invoice ".$obj->facnumber." - Lang = ".$outputlangs->defaultlang."\n";
  165. $result=facture_pdf_create($db, $fac, $regenerate?$regenerate:$fac->modelpdf, $outputlangs);
  166. }
  167. else {
  168. if ($usestdout) print "PDF for invoice ".$obj->facnumber." already exists\n";
  169. }
  170. // Add file into files array
  171. $files[] = $filename;
  172. }
  173. if ($result <= 0)
  174. {
  175. $error++;
  176. if ($usestdout) print "Error: Failed to build PDF for invoice ".($fac->ref?$fac->ref:' id '.$obj->rowid)."\n";
  177. else dol_syslog("Failed to build PDF for invoice ".($fac->ref?$fac->ref:' id '.$obj->rowid), LOG_ERR);
  178. }
  179. $cpt++;
  180. }
  181. // Define format of output PDF
  182. $formatarray=pdf_getFormat($langs);
  183. $page_largeur = $formatarray['width'];
  184. $page_hauteur = $formatarray['height'];
  185. $format = array($page_largeur,$page_hauteur);
  186. if ($usestdout) print "Using output PDF format ".join('x',$format)."\n";
  187. else dol_syslog("Using output PDF format ".join('x',$format), LOG_ERR);
  188. // Now, build a merged files with all files in $files array
  189. //---------------------------------------------------------
  190. // Create empty PDF
  191. $pdf=pdf_getInstance($format);
  192. if (class_exists('TCPDF'))
  193. {
  194. $pdf->setPrintHeader(false);
  195. $pdf->setPrintFooter(false);
  196. }
  197. $pdf->SetFont(pdf_getPDFFont($langs));
  198. if ($conf->global->MAIN_DISABLE_PDF_COMPRESSION) $pdf->SetCompression(false);
  199. //$pdf->SetCompression(false);
  200. //$pdf->Open();
  201. //$pdf->AddPage();
  202. //$title=$langs->trans("BillsCustomersUnpaid");
  203. //if ($option=='late') $title=$langs->trans("BillsCustomersUnpaid");
  204. //$pdf->MultiCell(100, 3, $title, 0, 'J');
  205. // Add all others
  206. foreach($files as $file)
  207. {
  208. if ($usestdout) print "Merge PDF file for invoice ".$file."\n";
  209. else dol_syslog("Merge PDF file for invoice ".$file);
  210. // Charge un document PDF depuis un fichier.
  211. $pagecount = $pdf->setSourceFile($file);
  212. for ($i = 1; $i <= $pagecount; $i++)
  213. {
  214. $tplidx = $pdf->importPage($i);
  215. $s = $pdf->getTemplatesize($tplidx);
  216. $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
  217. $pdf->useTemplate($tplidx);
  218. }
  219. }
  220. // Create output dir if not exists
  221. dol_mkdir($diroutputpdf);
  222. // Save merged file
  223. $filename='mergedpdf';
  224. if (! empty($option)) $filename.='_'.$option;
  225. $file=$diroutputpdf.'/'.$filename.'.pdf';
  226. if (! $error && $pagecount)
  227. {
  228. $pdf->Output($file,'F');
  229. if (! empty($conf->global->MAIN_UMASK))
  230. @chmod($file, octdec($conf->global->MAIN_UMASK));
  231. }
  232. if ($usestdout)
  233. {
  234. if (! $error) print "Merged PDF has been built in ".$file."\n";
  235. else print "Can't build PDF ".$file."\n";
  236. }
  237. $result = 1;
  238. }
  239. else
  240. {
  241. if ($usestdout) print "No invoices found for criteria.\n";
  242. else dol_syslog("No invoices found for criteria");
  243. $result = 0;
  244. }
  245. }
  246. else
  247. {
  248. dol_print_error($db);
  249. dol_syslog("scripts/invoices/rebuild_merge.php: Error");
  250. $error++;
  251. }
  252. if ($error) return -1;
  253. else return $result;
  254. }