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