54function rebuild_merge_pdf($db, $langs,
$conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, $usestdout, $regenerate =
'', $filesuffix =
'', $paymentbankid =
'', $thirdpartiesid = [], $fileprefix =
'mergedpdf', $donotmerge = 0, $mode =
'invoice')
56 if ($mode ==
'invoice') {
57 require_once DOL_DOCUMENT_ROOT.
"/compta/facture/class/facture.class.php";
58 require_once DOL_DOCUMENT_ROOT.
"/core/modules/facture/modules_facture.php";
61 $dir_output =
$conf->facture->dir_output;
64 if ($diroutputpdf ==
'auto') {
65 $diroutputpdf =
$conf->invoice->dir_output.
'/temp';
67 } elseif ($mode ==
'order') {
68 require_once DOL_DOCUMENT_ROOT.
"/commande/class/commande.class.php";
69 require_once DOL_DOCUMENT_ROOT.
"/core/modules/commande/modules_commande.php";
72 $dir_output =
$conf->order->dir_output;
75 if ($diroutputpdf ==
'auto') {
76 $diroutputpdf =
$conf->order->dir_output.
'/temp';
78 } elseif ($mode ==
'proposal') {
79 require_once DOL_DOCUMENT_ROOT.
"/comm/propal/class/propal.class.php";
80 require_once DOL_DOCUMENT_ROOT.
"/core/modules/propale/modules_propale.php";
83 $dir_output =
$conf->propal->dir_output;
86 if ($diroutputpdf ==
'auto') {
87 $diroutputpdf =
$conf->propal->dir_output.
'/temp';
89 } elseif ($mode ==
'shipment') {
90 require_once DOL_DOCUMENT_ROOT.
"/expedition/class/expedition.class.php";
91 require_once DOL_DOCUMENT_ROOT.
"/core/modules/expedition/modules_expedition.php";
94 $dir_output =
$conf->shipment->dir_output;
97 if ($diroutputpdf ==
'auto') {
98 $diroutputpdf =
$conf->shipment->dir_output.
'/temp';
101 print
"Bad value for mode";
105 $sql =
"SELECT DISTINCT f.rowid, f.ref";
106 $sql .=
" FROM ".MAIN_DB_PREFIX.$table.
" as f";
109 if (in_array(
'all', $filter)) {
110 $sqlorder =
" ORDER BY f.ref ASC";
112 if (in_array(
'date', $filter)) {
113 if (empty($sqlwhere)) {
114 $sqlwhere =
' WHERE ';
118 $sqlwhere .=
" f.fk_statut > 0";
119 $sqlwhere .=
" AND f.".$db->sanitize($date).
" >= '".$db->idate($dateafterdate).
"'";
120 $sqlwhere .=
" AND f.".$db->sanitize($date).
" <= '".$db->idate($datebeforedate).
"'";
121 $sqlorder =
" ORDER BY ".$db->sanitize($date).
" ASC";
124 if (in_array(
'nopayment', $filter)) {
125 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON f.rowid = pf.fk_facture";
126 if (empty($sqlwhere)) {
127 $sqlwhere =
' WHERE ';
131 $sqlwhere .=
" f.fk_statut > 0";
132 $sqlwhere .=
" AND pf.fk_paiement IS NULL";
135 if (in_array(
'payments', $filter) || in_array(
'bank', $filter)) {
136 $sql .=
", ".MAIN_DB_PREFIX.
"paiement_facture as pf, ".MAIN_DB_PREFIX.
"paiement as p";
137 if (in_array(
'bank', $filter)) {
138 $sql .=
", ".MAIN_DB_PREFIX.
"bank as b";
140 if (empty($sqlwhere)) {
141 $sqlwhere =
' WHERE ';
145 $sqlwhere .=
" f.fk_statut > 0";
146 $sqlwhere .=
" AND f.rowid = pf.fk_facture";
147 $sqlwhere .=
" AND pf.fk_paiement = p.rowid";
148 if (in_array(
'payments', $filter)) {
149 $sqlwhere .=
" AND p.datep >= '".$db->idate($paymentdateafter).
"'";
150 $sqlwhere .=
" AND p.datep <= '".$db->idate($paymentdatebefore).
"'";
152 if (in_array(
'bank', $filter)) {
153 $sqlwhere .=
" AND p.fk_bank = b.rowid";
154 $sqlwhere .=
" AND b.fk_account = ".((int) $paymentbankid);
156 $sqlorder =
" ORDER BY p.datep ASC";
159 if (in_array(
'nodeposit', $filter)) {
160 if (empty($sqlwhere)) {
161 $sqlwhere =
' WHERE ';
165 $sqlwhere .=
' type <> 3';
168 if (in_array(
'noreplacement', $filter)) {
169 if (empty($sqlwhere)) {
170 $sqlwhere =
' WHERE ';
174 $sqlwhere .=
' type <> 1';
177 if (in_array(
'nocreditnote', $filter)) {
178 if (empty($sqlwhere)) {
179 $sqlwhere =
' WHERE ';
183 $sqlwhere .=
' type <> 2';
185 if (in_array(
'excludethirdparties', $filter) && is_array($thirdpartiesid)) {
186 if (empty($sqlwhere)) {
187 $sqlwhere =
' WHERE ';
191 $sqlwhere .=
' f.fk_soc NOT IN ('.$db->sanitize(implode(
',', $thirdpartiesid)).
')';
193 if (in_array(
'onlythirdparties', $filter) && is_array($thirdpartiesid)) {
194 if (empty($sqlwhere)) {
195 $sqlwhere =
' WHERE ';
199 $sqlwhere .=
' f.fk_soc IN ('.$db->sanitize(implode(
',', $thirdpartiesid)).
')';
209 dol_syslog(
"scripts/invoices/rebuild_merge.php:", LOG_DEBUG);
212 print
'--- start'.
"\n";
222 dol_syslog(
"scripts/invoices/rebuild_merge.php", LOG_DEBUG);
223 if ($resql = $db->query($sql)) {
224 $num = $db->num_rows($resql);
231 while ($cpt < $num) {
232 $obj = $db->fetch_object($resql);
235 if ($mode ==
'invoice') {
237 } elseif ($mode ==
'order') {
239 } elseif ($mode ==
'proposal') {
241 } elseif ($mode ==
'shipment') {
245 $result = $fac->fetch($obj->rowid);
247 $outputlangs = $langs;
248 if (!empty($newlangid)) {
249 if ($outputlangs->defaultlang != $newlangid) {
251 $outputlangs->setDefaultLang($newlangid);
254 $filename = $dir_output.
'/'.$fac->ref.
'/'.$fac->ref.
'.pdf';
257 print
"Build PDF for document ".$obj->ref.
" - Lang = ".$outputlangs->defaultlang.
"\n";
259 $result = $fac->generateDocument($regenerate ? $regenerate : $fac->model_pdf, $outputlangs);
262 print
"PDF for document ".$obj->ref.
" already exists\n";
267 $files[] = $filename;
273 print
"Error: Failed to build PDF for document ".($fac->ref ? $fac->ref :
' id '.$obj->rowid).
"\n";
275 dol_syslog(
"Failed to build PDF for document ".($fac->ref ? $fac->ref :
' id '.$obj->rowid), LOG_ERR);
288 $page_largeur = $formatarray[
'width'];
289 $page_hauteur = $formatarray[
'height'];
290 $format = array($page_largeur, $page_hauteur);
293 print
"Using output PDF format ".implode(
'x', $format).
"\n";
295 dol_syslog(
"Using output PDF format ".implode(
'x', $format), LOG_ERR);
304 if (class_exists(
'TCPDF')) {
305 $pdf->setPrintHeader(
false);
306 $pdf->setPrintFooter(
false);
311 $pdf->SetCompression(
false);
317 foreach ($files as $file) {
319 print
"Merge PDF file for invoice ".$file.
"\n";
321 dol_syslog(
"Merge PDF file for invoice ".$file);
325 $pagecount = $pdf->setSourceFile($file);
326 for ($i = 1; $i <= $pagecount; $i++) {
327 $tplidx = $pdf->importPage($i);
328 $s = $pdf->getTemplatesize($tplidx);
329 $pdf->AddPage($s[
'h'] > $s[
'w'] ?
'P' :
'L');
330 $pdf->useTemplate($tplidx);
338 $filename = $fileprefix;
339 if (empty($filename)) {
340 $filename =
'mergedpdf';
342 if (!empty($filesuffix)) {
343 $filename .=
'_'.$filesuffix;
345 $file = $diroutputpdf.
'/'.$filename.
'.pdf';
347 if (!$error && $pagecount) {
348 $pdf->Output($file,
'F');
354 print
"Merged PDF has been built in ".$file.
"\n";
356 print
"Can't build PDF ".$file.
"\n";
363 print
"No invoices found for criteria.\n";
371 dol_syslog(
"scripts/invoices/rebuild_merge.php: Error");
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', $donotmerge=0, $mode='invoice')
Function to build a compiled PDF.