dolibarr 21.0.0-alpha
invoice2.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2009-2013 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
27require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
29
30
52function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, $usestdout, $regenerate = '', $filesuffix = '', $paymentbankid = '', $thirdpartiesid = [], $fileprefix = 'mergedpdf')
53{
54 $sql = "SELECT DISTINCT f.rowid, f.ref";
55 $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
56 $sqlwhere = '';
57 $sqlorder = '';
58 if (in_array('all', $filter)) {
59 $sqlorder = " ORDER BY f.ref ASC";
60 }
61 if (in_array('date', $filter)) {
62 if (empty($sqlwhere)) {
63 $sqlwhere = ' WHERE ';
64 } else {
65 $sqlwhere .= " AND";
66 }
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";
71 }
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 ';
76 } else {
77 $sqlwhere .= " AND";
78 }
79 $sqlwhere .= " f.fk_statut > 0";
80 $sqlwhere .= " AND pf.fk_paiement IS NULL";
81 }
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";
86 }
87 if (empty($sqlwhere)) {
88 $sqlwhere = ' WHERE ';
89 } else {
90 $sqlwhere .= " AND";
91 }
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)."'";
98 }
99 if (in_array('bank', $filter)) {
100 $sqlwhere .= " AND p.fk_bank = b.rowid";
101 $sqlwhere .= " AND b.fk_account = ".((int) $paymentbankid);
102 }
103 $sqlorder = " ORDER BY p.datep ASC";
104 }
105 if (in_array('nodeposit', $filter)) {
106 if (empty($sqlwhere)) {
107 $sqlwhere = ' WHERE ';
108 } else {
109 $sqlwhere .= " AND";
110 }
111 $sqlwhere .= ' type <> 3';
112 }
113 if (in_array('noreplacement', $filter)) {
114 if (empty($sqlwhere)) {
115 $sqlwhere = ' WHERE ';
116 } else {
117 $sqlwhere .= " AND";
118 }
119 $sqlwhere .= ' type <> 1';
120 }
121 if (in_array('nocreditnote', $filter)) {
122 if (empty($sqlwhere)) {
123 $sqlwhere = ' WHERE ';
124 } else {
125 $sqlwhere .= " AND";
126 }
127 $sqlwhere .= ' type <> 2';
128 }
129 if (in_array('excludethirdparties', $filter) && is_array($thirdpartiesid)) {
130 if (empty($sqlwhere)) {
131 $sqlwhere = ' WHERE ';
132 } else {
133 $sqlwhere .= " AND";
134 }
135 $sqlwhere .= ' f.fk_soc NOT IN ('.$db->sanitize(implode(',', $thirdpartiesid)).')';
136 }
137 if (in_array('onlythirdparties', $filter) && is_array($thirdpartiesid)) {
138 if (empty($sqlwhere)) {
139 $sqlwhere = ' WHERE ';
140 } else {
141 $sqlwhere .= " AND";
142 }
143 $sqlwhere .= ' f.fk_soc IN ('.$db->sanitize(implode(',', $thirdpartiesid)).')';
144 }
145 if ($sqlwhere) {
146 $sql .= $sqlwhere;
147 }
148 if ($sqlorder) {
149 $sql .= $sqlorder;
150 }
151
152 //print $sql; exit;
153 dol_syslog("scripts/invoices/rebuild_merge.php:", LOG_DEBUG);
154
155 if ($usestdout) {
156 print '--- start'."\n";
157 }
158
159 // Start of transaction
160 //$db->begin();
161
162 $error = 0;
163 $result = 0;
164 $files = array(); // liste les fichiers
165
166 dol_syslog("scripts/invoices/rebuild_merge.php", LOG_DEBUG);
167 if ($resql = $db->query($sql)) {
168 $num = $db->num_rows($resql);
169 $cpt = 0;
170 $oldemail = '';
171 $message = '';
172 $total = '';
173
174 if ($num) {
175 // First loop on each resultset to build PDF
176 // -----------------------------------------
177
178 while ($cpt < $num) {
179 $obj = $db->fetch_object($resql);
180
181 $fac = new Facture($db);
182 $result = $fac->fetch($obj->rowid);
183 if ($result > 0) {
184 $outputlangs = $langs;
185 if (!empty($newlangid)) {
186 if ($outputlangs->defaultlang != $newlangid) {
187 $outputlangs = new Translate("", $conf);
188 $outputlangs->setDefaultLang($newlangid);
189 }
190 }
191 $filename = $conf->facture->dir_output.'/'.$fac->ref.'/'.$fac->ref.'.pdf';
192 if ($regenerate || !dol_is_file($filename)) {
193 if ($usestdout) {
194 print "Build PDF for invoice ".$obj->ref." - Lang = ".$outputlangs->defaultlang."\n";
195 }
196 $result = $fac->generateDocument($regenerate ? $regenerate : $fac->model_pdf, $outputlangs);
197 } else {
198 if ($usestdout) {
199 print "PDF for invoice ".$obj->ref." already exists\n";
200 }
201 }
202
203 // Add file into files array
204 $files[] = $filename;
205 }
206
207 if ($result <= 0) {
208 $error++;
209 if ($usestdout) {
210 print "Error: Failed to build PDF for invoice ".($fac->ref ? $fac->ref : ' id '.$obj->rowid)."\n";
211 } else {
212 dol_syslog("Failed to build PDF for invoice ".($fac->ref ? $fac->ref : ' id '.$obj->rowid), LOG_ERR);
213 }
214 }
215
216 $cpt++;
217 }
218
219
220 // Define format of output PDF
221 $formatarray = pdf_getFormat($langs);
222 $page_largeur = $formatarray['width'];
223 $page_hauteur = $formatarray['height'];
224 $format = array($page_largeur, $page_hauteur);
225
226 if ($usestdout) {
227 print "Using output PDF format ".implode('x', $format)."\n";
228 } else {
229 dol_syslog("Using output PDF format ".implode('x', $format), LOG_ERR);
230 }
231
232
233 // Now, build a merged files with all files in $files array
234 //---------------------------------------------------------
235
236 // Create empty PDF
237 $pdf = pdf_getInstance($format);
238 if (class_exists('TCPDF')) {
239 $pdf->setPrintHeader(false);
240 $pdf->setPrintFooter(false);
241 }
242 $pdf->SetFont(pdf_getPDFFont($langs));
243
244 if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
245 $pdf->SetCompression(false);
246 }
247 //$pdf->SetCompression(false);
248
249 // Add all others
250 foreach ($files as $file) {
251 if ($usestdout) {
252 print "Merge PDF file for invoice ".$file."\n";
253 } else {
254 dol_syslog("Merge PDF file for invoice ".$file);
255 }
256
257 // Charge un document PDF depuis un fichier.
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);
264 }
265 }
266
267 // Create output dir if not exists
268 dol_mkdir($diroutputpdf);
269
270 // Save merged file
271 $filename = $fileprefix;
272 if (empty($filename)) {
273 $filename = 'mergedpdf';
274 }
275 if (!empty($filesuffix)) {
276 $filename .= '_'.$filesuffix;
277 }
278 $file = $diroutputpdf.'/'.$filename.'.pdf';
279
280 if (!$error && $pagecount) {
281 $pdf->Output($file, 'F');
282 dolChmod($file);
283 }
284
285 if ($usestdout) {
286 if (!$error) {
287 print "Merged PDF has been built in ".$file."\n";
288 } else {
289 print "Can't build PDF ".$file."\n";
290 }
291 }
292
293 $result = 1;
294 } else {
295 if ($usestdout) {
296 print "No invoices found for criteria.\n";
297 } else {
298 dol_syslog("No invoices found for criteria");
299 }
300 $result = 0;
301 }
302 } else {
303 dol_print_error($db);
304 dol_syslog("scripts/invoices/rebuild_merge.php: Error");
305 $error++;
306 }
307
308 if ($error) {
309 return -1;
310 } else {
311 return $result;
312 }
313}
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 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.
pdf_getFormat(Translate $outputlangs=null, $mode='setup')
Return array with format properties of default PDF format.
Definition pdf.lib.php:86
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition pdf.lib.php:267
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition pdf.lib.php:128