invoice2.lib.php 9.1 KB

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