invoice2.lib.php 9.2 KB

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