27require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
28require_once DOL_DOCUMENT_ROOT.
'/core/lib/pdf.lib.php';
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);
251 foreach ($files as $file) {
253 print
"Merge PDF file for invoice ".$file.
"\n";
255 dol_syslog(
"Merge PDF file for invoice ".$file);
259 $pagecount = $pdf->setSourceFile($file);
260 for ($i = 1; $i <= $pagecount; $i++) {
261 $tplidx = $pdf->importPage($i);
262 $s = $pdf->getTemplatesize($tplidx);
263 $pdf->AddPage($s[
'h'] > $s[
'w'] ?
'P' :
'L');
264 $pdf->useTemplate($tplidx);
272 $filename = $fileprefix;
273 if (empty($filename)) {
274 $filename =
'mergedpdf';
276 if (!empty($filesuffix)) {
277 $filename .=
'_'.$filesuffix;
279 $file = $diroutputpdf.
'/'.$filename.
'.pdf';
281 if (!$error && $pagecount) {
282 $pdf->Output($file,
'F');
288 print
"Merged PDF has been built in ".$file.
"\n";
290 print
"Can't build PDF ".$file.
"\n";
297 print
"No invoices found for criteria.\n";
305 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.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a 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='', $filesuffix='', $paymentbankid='', $thirdpartiesid=[], $fileprefix='mergedpdf')
Function to build a compiled PDF.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
pdf_getFormat($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.