dolibarr 21.0.0-beta
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
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')
55{
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";
59
60 $table = "facture";
61 $dir_output = $conf->facture->dir_output;
62 $date = "datef";
63
64 if ($diroutputpdf == 'auto') {
65 $diroutputpdf = $conf->invoice->dir_output.'/temp';
66 }
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";
70
71 $table = "commande";
72 $dir_output = $conf->order->dir_output;
73 $date = "date";
74
75 if ($diroutputpdf == 'auto') {
76 $diroutputpdf = $conf->order->dir_output.'/temp';
77 }
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";
81
82 $table = "propal";
83 $dir_output = $conf->propal->dir_output;
84 $date = "datep";
85
86 if ($diroutputpdf == 'auto') {
87 $diroutputpdf = $conf->propal->dir_output.'/temp';
88 }
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";
92
93 $table = "propal";
94 $dir_output = $conf->shipment->dir_output;
95 $date = "date";
96
97 if ($diroutputpdf == 'auto') {
98 $diroutputpdf = $conf->shipment->dir_output.'/temp';
99 }
100 } else {
101 print "Bad value for mode";
102 return -1;
103 }
104
105 $sql = "SELECT DISTINCT f.rowid, f.ref";
106 $sql .= " FROM ".MAIN_DB_PREFIX.$table." as f";
107 $sqlwhere = '';
108 $sqlorder = '';
109 if (in_array('all', $filter)) {
110 $sqlorder = " ORDER BY f.ref ASC";
111 }
112 if (in_array('date', $filter)) {
113 if (empty($sqlwhere)) {
114 $sqlwhere = ' WHERE ';
115 } else {
116 $sqlwhere .= " AND";
117 }
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";
122 }
123 // Filter for invoices only
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 ';
128 } else {
129 $sqlwhere .= " AND";
130 }
131 $sqlwhere .= " f.fk_statut > 0";
132 $sqlwhere .= " AND pf.fk_paiement IS NULL";
133 }
134 // Filter for invoices only
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";
139 }
140 if (empty($sqlwhere)) {
141 $sqlwhere = ' WHERE ';
142 } else {
143 $sqlwhere .= " AND";
144 }
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)."'";
151 }
152 if (in_array('bank', $filter)) {
153 $sqlwhere .= " AND p.fk_bank = b.rowid";
154 $sqlwhere .= " AND b.fk_account = ".((int) $paymentbankid);
155 }
156 $sqlorder = " ORDER BY p.datep ASC";
157 }
158 // Filter for invoices only
159 if (in_array('nodeposit', $filter)) {
160 if (empty($sqlwhere)) {
161 $sqlwhere = ' WHERE ';
162 } else {
163 $sqlwhere .= " AND";
164 }
165 $sqlwhere .= ' type <> 3';
166 }
167 // Filter for invoices only
168 if (in_array('noreplacement', $filter)) {
169 if (empty($sqlwhere)) {
170 $sqlwhere = ' WHERE ';
171 } else {
172 $sqlwhere .= " AND";
173 }
174 $sqlwhere .= ' type <> 1';
175 }
176 // Filter for invoices only
177 if (in_array('nocreditnote', $filter)) {
178 if (empty($sqlwhere)) {
179 $sqlwhere = ' WHERE ';
180 } else {
181 $sqlwhere .= " AND";
182 }
183 $sqlwhere .= ' type <> 2';
184 }
185 if (in_array('excludethirdparties', $filter) && is_array($thirdpartiesid)) {
186 if (empty($sqlwhere)) {
187 $sqlwhere = ' WHERE ';
188 } else {
189 $sqlwhere .= " AND";
190 }
191 $sqlwhere .= ' f.fk_soc NOT IN ('.$db->sanitize(implode(',', $thirdpartiesid)).')';
192 }
193 if (in_array('onlythirdparties', $filter) && is_array($thirdpartiesid)) {
194 if (empty($sqlwhere)) {
195 $sqlwhere = ' WHERE ';
196 } else {
197 $sqlwhere .= " AND";
198 }
199 $sqlwhere .= ' f.fk_soc IN ('.$db->sanitize(implode(',', $thirdpartiesid)).')';
200 }
201 if ($sqlwhere) {
202 $sql .= $sqlwhere;
203 }
204 if ($sqlorder) {
205 $sql .= $sqlorder;
206 }
207
208 //print $sql; exit;
209 dol_syslog("scripts/invoices/rebuild_merge.php:", LOG_DEBUG);
210
211 if ($usestdout) {
212 print '--- start'."\n";
213 }
214
215 // Start of transaction
216 //$db->begin();
217
218 $error = 0;
219 $result = 0;
220 $files = array(); // liste les fichiers
221
222 dol_syslog("scripts/invoices/rebuild_merge.php", LOG_DEBUG);
223 if ($resql = $db->query($sql)) {
224 $num = $db->num_rows($resql);
225 $cpt = 0;
226
227 if ($num) {
228 // First loop on each resultset to build PDF
229 // -----------------------------------------
230
231 while ($cpt < $num) {
232 $obj = $db->fetch_object($resql);
233
234 $fac = null;
235 if ($mode == 'invoice') {
236 $fac = new Facture($db);
237 } elseif ($mode == 'order') {
238 $fac = new Commande($db);
239 } elseif ($mode == 'proposal') {
240 $fac = new Propal($db);
241 } elseif ($mode == 'shipment') {
242 $fac = new Expedition($db);
243 }
244
245 $result = $fac->fetch($obj->rowid);
246 if ($result > 0) {
247 $outputlangs = $langs;
248 if (!empty($newlangid)) {
249 if ($outputlangs->defaultlang != $newlangid) {
250 $outputlangs = new Translate("", $conf);
251 $outputlangs->setDefaultLang($newlangid);
252 }
253 }
254 $filename = $dir_output.'/'.$fac->ref.'/'.$fac->ref.'.pdf';
255 if ($regenerate || !dol_is_file($filename)) {
256 if ($usestdout) {
257 print "Build PDF for document ".$obj->ref." - Lang = ".$outputlangs->defaultlang."\n";
258 }
259 $result = $fac->generateDocument($regenerate ? $regenerate : $fac->model_pdf, $outputlangs);
260 } else {
261 if ($usestdout) {
262 print "PDF for document ".$obj->ref." already exists\n";
263 }
264 }
265
266 // Add file into files array
267 $files[] = $filename;
268 }
269
270 if ($result <= 0) {
271 $error++;
272 if ($usestdout) {
273 print "Error: Failed to build PDF for document ".($fac->ref ? $fac->ref : ' id '.$obj->rowid)."\n";
274 } else {
275 dol_syslog("Failed to build PDF for document ".($fac->ref ? $fac->ref : ' id '.$obj->rowid), LOG_ERR);
276 }
277 }
278
279 $cpt++;
280 }
281
282 if ($donotmerge) {
283 return 1;
284 }
285
286 // Define format of output PDF
287 $formatarray = pdf_getFormat($langs);
288 $page_largeur = $formatarray['width'];
289 $page_hauteur = $formatarray['height'];
290 $format = array($page_largeur, $page_hauteur);
291
292 if ($usestdout) {
293 print "Using output PDF format ".implode('x', $format)."\n";
294 } else {
295 dol_syslog("Using output PDF format ".implode('x', $format), LOG_ERR);
296 }
297
298
299 // Now, build a merged files with all files in $files array
300 //---------------------------------------------------------
301
302 // Create empty PDF
303 $pdf = pdf_getInstance($format);
304 if (class_exists('TCPDF')) {
305 $pdf->setPrintHeader(false);
306 $pdf->setPrintFooter(false);
307 }
308 $pdf->SetFont(pdf_getPDFFont($langs));
309
310 if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
311 $pdf->SetCompression(false);
312 }
313 //$pdf->SetCompression(false);
314
315 $pagecount = 0;
316 // Add all others
317 foreach ($files as $file) {
318 if ($usestdout) {
319 print "Merge PDF file for invoice ".$file."\n";
320 } else {
321 dol_syslog("Merge PDF file for invoice ".$file);
322 }
323
324 // Charge un document PDF depuis un fichier.
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);
331 }
332 }
333
334 // Create output dir if not exists
335 dol_mkdir($diroutputpdf);
336
337 // Save merged file
338 $filename = $fileprefix;
339 if (empty($filename)) {
340 $filename = 'mergedpdf';
341 }
342 if (!empty($filesuffix)) {
343 $filename .= '_'.$filesuffix;
344 }
345 $file = $diroutputpdf.'/'.$filename.'.pdf';
346
347 if (!$error && $pagecount) {
348 $pdf->Output($file, 'F');
349 dolChmod($file);
350 }
351
352 if ($usestdout) {
353 if (!$error) {
354 print "Merged PDF has been built in ".$file."\n";
355 } else {
356 print "Can't build PDF ".$file."\n";
357 }
358 }
359
360 $result = 1;
361 } else {
362 if ($usestdout) {
363 print "No invoices found for criteria.\n";
364 } else {
365 dol_syslog("No invoices found for criteria");
366 }
367 $result = 0;
368 }
369 } else {
370 dol_print_error($db);
371 dol_syslog("scripts/invoices/rebuild_merge.php: Error");
372 $error++;
373 }
374
375 if ($error) {
376 return -1;
377 } else {
378 return $result;
379 }
380}
Class to manage customers orders.
Class to manage invoices.
Class to manage proposals.
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', $donotmerge=0, $mode='invoice')
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...
Definition member.php:79
pdf_getFormat($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:265
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition pdf.lib.php:128