25require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
26require_once DOL_DOCUMENT_ROOT.
'/core/lib/pdf.lib.php';
50function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, $usestdout, $regenerate = 0, $filesuffix =
'', $paymentbankid =
'', $thirdpartiesid =
'', $fileprefix =
'mergedpdf')
52 $sql =
"SELECT DISTINCT f.rowid, f.ref";
53 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
56 if (in_array(
'all', $filter)) {
57 $sqlorder =
" ORDER BY f.ref ASC";
59 if (in_array(
'date', $filter)) {
60 if (empty($sqlwhere)) {
61 $sqlwhere =
' WHERE ';
65 $sqlwhere .=
" f.fk_statut > 0";
66 $sqlwhere .=
" AND f.datef >= '".$db->idate($dateafterdate).
"'";
67 $sqlwhere .=
" AND f.datef <= '".$db->idate($datebeforedate).
"'";
68 $sqlorder =
" ORDER BY f.datef ASC";
70 if (in_array(
'nopayment', $filter)) {
71 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON f.rowid = pf.fk_facture";
72 if (empty($sqlwhere)) {
73 $sqlwhere =
' WHERE ';
77 $sqlwhere .=
" f.fk_statut > 0";
78 $sqlwhere .=
" AND pf.fk_paiement IS NULL";
80 if (in_array(
'payments', $filter) || in_array(
'bank', $filter)) {
81 $sql .=
", ".MAIN_DB_PREFIX.
"paiement_facture as pf, ".MAIN_DB_PREFIX.
"paiement as p";
82 if (in_array(
'bank', $filter)) {
83 $sql .=
", ".MAIN_DB_PREFIX.
"bank as b";
85 if (empty($sqlwhere)) {
86 $sqlwhere =
' WHERE ';
90 $sqlwhere .=
" f.fk_statut > 0";
91 $sqlwhere .=
" AND f.rowid = pf.fk_facture";
92 $sqlwhere .=
" AND pf.fk_paiement = p.rowid";
93 if (in_array(
'payments', $filter)) {
94 $sqlwhere .=
" AND p.datep >= '".$db->idate($paymentdateafter).
"'";
95 $sqlwhere .=
" AND p.datep <= '".$db->idate($paymentdatebefore).
"'";
97 if (in_array(
'bank', $filter)) {
98 $sqlwhere .=
" AND p.fk_bank = b.rowid";
99 $sqlwhere .=
" AND b.fk_account = ".((int) $paymentbankid);
101 $sqlorder =
" ORDER BY p.datep ASC";
103 if (in_array(
'nodeposit', $filter)) {
104 if (empty($sqlwhere)) {
105 $sqlwhere =
' WHERE ';
109 $sqlwhere .=
' type <> 3';
111 if (in_array(
'noreplacement', $filter)) {
112 if (empty($sqlwhere)) {
113 $sqlwhere =
' WHERE ';
117 $sqlwhere .=
' type <> 1';
119 if (in_array(
'nocreditnote', $filter)) {
120 if (empty($sqlwhere)) {
121 $sqlwhere =
' WHERE ';
125 $sqlwhere .=
' type <> 2';
127 if (in_array(
'excludethirdparties', $filter) && is_array($thirdpartiesid)) {
128 if (empty($sqlwhere)) {
129 $sqlwhere =
' WHERE ';
133 $sqlwhere .=
' f.fk_soc NOT IN ('.$db->sanitize(join(
',', $thirdpartiesid)).
')';
135 if (in_array(
'onlythirdparties', $filter) && is_array($thirdpartiesid)) {
136 if (empty($sqlwhere)) {
137 $sqlwhere =
' WHERE ';
141 $sqlwhere .=
' f.fk_soc IN ('.$db->sanitize(join(
',', $thirdpartiesid)).
')';
151 dol_syslog(
"scripts/invoices/rebuild_merge.php:", LOG_DEBUG);
154 print
'--- start'.
"\n";
164 dol_syslog(
"scripts/invoices/rebuild_merge.php", LOG_DEBUG);
165 if ($resql = $db->query($sql)) {
166 $num = $db->num_rows($resql);
176 while ($cpt < $num) {
177 $obj = $db->fetch_object($resql);
180 $result = $fac->fetch($obj->rowid);
182 $outputlangs = $langs;
183 if (!empty($newlangid)) {
184 if ($outputlangs->defaultlang != $newlangid) {
186 $outputlangs->setDefaultLang($newlangid);
189 $filename = $conf->facture->dir_output.
'/'.$fac->ref.
'/'.$fac->ref.
'.pdf';
192 print
"Build PDF for invoice ".$obj->ref.
" - Lang = ".$outputlangs->defaultlang.
"\n";
194 $result = $fac->generateDocument($regenerate ? $regenerate : $fac->model_pdf, $outputlangs);
197 print
"PDF for invoice ".$obj->ref.
" already exists\n";
202 $files[] = $filename;
208 print
"Error: Failed to build PDF for invoice ".($fac->ref ? $fac->ref :
' id '.$obj->rowid).
"\n";
210 dol_syslog(
"Failed to build PDF for invoice ".($fac->ref ? $fac->ref :
' id '.$obj->rowid), LOG_ERR);
220 $page_largeur = $formatarray[
'width'];
221 $page_hauteur = $formatarray[
'height'];
222 $format = array($page_largeur, $page_hauteur);
225 print
"Using output PDF format ".join(
'x', $format).
"\n";
227 dol_syslog(
"Using output PDF format ".join(
'x', $format), LOG_ERR);
236 if (class_exists(
'TCPDF')) {
237 $pdf->setPrintHeader(
false);
238 $pdf->setPrintFooter(
false);
243 $pdf->SetCompression(
false);
248 foreach ($files as $file) {
250 print
"Merge PDF file for invoice ".$file.
"\n";
252 dol_syslog(
"Merge PDF file for invoice ".$file);
256 $pagecount = $pdf->setSourceFile($file);
257 for ($i = 1; $i <= $pagecount; $i++) {
258 $tplidx = $pdf->importPage($i);
259 $s = $pdf->getTemplatesize($tplidx);
260 $pdf->AddPage($s[
'h'] > $s[
'w'] ?
'P' :
'L');
261 $pdf->useTemplate($tplidx);
269 $filename = $fileprefix;
270 if (empty($filename)) {
271 $filename =
'mergedpdf';
273 if (!empty($filesuffix)) {
274 $filename .=
'_'.$filesuffix;
276 $file = $diroutputpdf.
'/'.$filename.
'.pdf';
278 if (!$error && $pagecount) {
279 $pdf->Output($file,
'F');
285 print
"Merged PDF has been built in ".$file.
"\n";
287 print
"Can't build PDF ".$file.
"\n";
294 print
"No invoices found for criteria.\n";
302 dol_syslog(
"scripts/invoices/rebuild_merge.php: Error");
Class to manage invoices.
Class to manage translations.
dol_is_file($pathoffile)
Return if path is a file.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, $usestdout, $regenerate=0, $filesuffix='', $paymentbankid='', $thirdpartiesid='', $fileprefix='mergedpdf')
Function to build a compiled PDF.
pdf_getFormat(Translate $outputlangs=null, $mode='setup')
Return array with format properties of default PDF format.
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.