52function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, $usestdout, $regenerate =
'', $filesuffix =
'', $paymentbankid =
'', $thirdpartiesid = [], $fileprefix =
'mergedpdf')
54 $sql =
"SELECT DISTINCT f.rowid, f.ref";
55 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
58 if (in_array(
'all', $filter)) {
59 $sqlorder =
" ORDER BY f.ref ASC";
61 if (in_array(
'date', $filter)) {
62 if (empty($sqlwhere)) {
63 $sqlwhere =
' WHERE ';
67 $sqlwhere .=
" f.fk_statut > 0";
68 $sqlwhere .=
" AND f.datef >= '".$db->idate($dateafterdate).
"'";
69 $sqlwhere .=
" AND f.datef <= '".$db->idate($datebeforedate).
"'";
70 $sqlorder =
" ORDER BY f.datef ASC";
72 if (in_array(
'nopayment', $filter)) {
73 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON f.rowid = pf.fk_facture";
74 if (empty($sqlwhere)) {
75 $sqlwhere =
' WHERE ';
79 $sqlwhere .=
" f.fk_statut > 0";
80 $sqlwhere .=
" AND pf.fk_paiement IS NULL";
82 if (in_array(
'payments', $filter) || in_array(
'bank', $filter)) {
83 $sql .=
", ".MAIN_DB_PREFIX.
"paiement_facture as pf, ".MAIN_DB_PREFIX.
"paiement as p";
84 if (in_array(
'bank', $filter)) {
85 $sql .=
", ".MAIN_DB_PREFIX.
"bank as b";
87 if (empty($sqlwhere)) {
88 $sqlwhere =
' WHERE ';
92 $sqlwhere .=
" f.fk_statut > 0";
93 $sqlwhere .=
" AND f.rowid = pf.fk_facture";
94 $sqlwhere .=
" AND pf.fk_paiement = p.rowid";
95 if (in_array(
'payments', $filter)) {
96 $sqlwhere .=
" AND p.datep >= '".$db->idate($paymentdateafter).
"'";
97 $sqlwhere .=
" AND p.datep <= '".$db->idate($paymentdatebefore).
"'";
99 if (in_array(
'bank', $filter)) {
100 $sqlwhere .=
" AND p.fk_bank = b.rowid";
101 $sqlwhere .=
" AND b.fk_account = ".((int) $paymentbankid);
103 $sqlorder =
" ORDER BY p.datep ASC";
105 if (in_array(
'nodeposit', $filter)) {
106 if (empty($sqlwhere)) {
107 $sqlwhere =
' WHERE ';
111 $sqlwhere .=
' type <> 3';
113 if (in_array(
'noreplacement', $filter)) {
114 if (empty($sqlwhere)) {
115 $sqlwhere =
' WHERE ';
119 $sqlwhere .=
' type <> 1';
121 if (in_array(
'nocreditnote', $filter)) {
122 if (empty($sqlwhere)) {
123 $sqlwhere =
' WHERE ';
127 $sqlwhere .=
' type <> 2';
129 if (in_array(
'excludethirdparties', $filter) && is_array($thirdpartiesid)) {
130 if (empty($sqlwhere)) {
131 $sqlwhere =
' WHERE ';
135 $sqlwhere .=
' f.fk_soc NOT IN ('.$db->sanitize(implode(
',', $thirdpartiesid)).
')';
137 if (in_array(
'onlythirdparties', $filter) && is_array($thirdpartiesid)) {
138 if (empty($sqlwhere)) {
139 $sqlwhere =
' WHERE ';
143 $sqlwhere .=
' f.fk_soc IN ('.$db->sanitize(implode(
',', $thirdpartiesid)).
')';
153 dol_syslog(
"scripts/invoices/rebuild_merge.php:", LOG_DEBUG);
156 print
'--- start'.
"\n";
166 dol_syslog(
"scripts/invoices/rebuild_merge.php", LOG_DEBUG);
167 if ($resql = $db->query($sql)) {
168 $num = $db->num_rows($resql);
178 while ($cpt < $num) {
179 $obj = $db->fetch_object($resql);
182 $result = $fac->fetch($obj->rowid);
184 $outputlangs = $langs;
185 if (!empty($newlangid)) {
186 if ($outputlangs->defaultlang != $newlangid) {
188 $outputlangs->setDefaultLang($newlangid);
191 $filename = $conf->facture->dir_output.
'/'.$fac->ref.
'/'.$fac->ref.
'.pdf';
194 print
"Build PDF for invoice ".$obj->ref.
" - Lang = ".$outputlangs->defaultlang.
"\n";
196 $result = $fac->generateDocument($regenerate ? $regenerate : $fac->model_pdf, $outputlangs);
199 print
"PDF for invoice ".$obj->ref.
" already exists\n";
204 $files[] = $filename;
210 print
"Error: Failed to build PDF for invoice ".($fac->ref ? $fac->ref :
' id '.$obj->rowid).
"\n";
212 dol_syslog(
"Failed to build PDF for invoice ".($fac->ref ? $fac->ref :
' id '.$obj->rowid), LOG_ERR);
222 $page_largeur = $formatarray[
'width'];
223 $page_hauteur = $formatarray[
'height'];
224 $format = array($page_largeur, $page_hauteur);
227 print
"Using output PDF format ".implode(
'x', $format).
"\n";
229 dol_syslog(
"Using output PDF format ".implode(
'x', $format), LOG_ERR);
238 if (class_exists(
'TCPDF')) {
239 $pdf->setPrintHeader(
false);
240 $pdf->setPrintFooter(
false);
245 $pdf->SetCompression(
false);
250 foreach ($files as $file) {
252 print
"Merge PDF file for invoice ".$file.
"\n";
254 dol_syslog(
"Merge PDF file for invoice ".$file);
258 $pagecount = $pdf->setSourceFile($file);
259 for ($i = 1; $i <= $pagecount; $i++) {
260 $tplidx = $pdf->importPage($i);
261 $s = $pdf->getTemplatesize($tplidx);
262 $pdf->AddPage($s[
'h'] > $s[
'w'] ?
'P' :
'L');
263 $pdf->useTemplate($tplidx);
271 $filename = $fileprefix;
272 if (empty($filename)) {
273 $filename =
'mergedpdf';
275 if (!empty($filesuffix)) {
276 $filename .=
'_'.$filesuffix;
278 $file = $diroutputpdf.
'/'.$filename.
'.pdf';
280 if (!$error && $pagecount) {
281 $pdf->Output($file,
'F');
287 print
"Merged PDF has been built in ".$file.
"\n";
289 print
"Can't build PDF ".$file.
"\n";
296 print
"No invoices found for criteria.\n";
304 dol_syslog(
"scripts/invoices/rebuild_merge.php: Error");
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, $usestdout, $regenerate='', $filesuffix='', $paymentbankid='', $thirdpartiesid=[], $fileprefix='mergedpdf')
Function to build a compiled PDF.