dolibarr 25.0.0-alpha
rebuild_merge_pdf.php
Go to the documentation of this file.
1#!/usr/bin/env php
2<?php
3/*
4 * Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28if (!defined('NOSESSION')) {
29 define('NOSESSION', '1');
30}
31
32$sapi_type = php_sapi_name();
33$script_file = basename(__FILE__);
34$path = __DIR__.'/';
35
36// Test if batch mode
37if (substr($sapi_type, 0, 3) == 'cgi') {
38 echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
39 exit(1);
40}
41
42// Include Dolibarr environment
43require_once $path."../../htdocs/master.inc.php";
44require_once DOL_DOCUMENT_ROOT.'/core/lib/functionscli.lib.php';
45// After this $db is an opened handler to database. We close it at end of file.
46require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
47require_once DOL_DOCUMENT_ROOT.'/core/lib/invoice2.lib.php';
48
58// Load main language strings
59$langs->load("main");
60
61// Global variables
62$version = DOL_VERSION;
63$error = 0;
64
65$hookmanager->initHooks(array('cli'));
66
67
68/*
69 * Main
70 */
71
72@set_time_limit(0);
73print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
74dol_syslog($script_file." launched with arg ".implode(',', $argv));
75
76// Check parameters
77if (!isset($argv[1])) {
79 exit(1);
80}
81
82if (!empty($dolibarr_main_db_readonly)) {
83 print "Error: instance in read-onyl mode\n";
84 exit(1);
85}
86
87
88$newlangid = 'en_EN'; // To force a new lang id
89$filter = array();
90$regenerate = ''; // Ask regenerate (contains name of model to use)
91$option = '';
92$fileprefix = 'mergedpdf';
93$nomerge = 0;
94$mode = 'invoice';
95
96$dateafterdate = '';
97$datebeforedate = '';
98$paymentdateafter = '';
99$paymentdatebefore = '';
100$paymentonbankid = 0;
101$thirdpartiesid = array();
102
103foreach ($argv as $key => $value) {
104 $found = false;
105
106 // Define options
107 if (preg_match('/^lang=/i', $value)) {
108 $found = true;
109 $valarray = explode('=', $value);
110 $newlangid = $valarray[1];
111 print 'Use language '.$newlangid.".\n";
112 }
113 if (preg_match('/^prefix=/i', $value)) {
114 $found = true;
115 $valarray = explode('=', $value);
116 $fileprefix = $valarray[1];
117 print 'Use prefix for filename '.$fileprefix.".\n";
118 }
119
120 if (preg_match('/^mode=/i', $value)) {
121 $found = true;
122 $valarray = explode('=', $value);
123 $mode = $valarray[1];
124 print 'Use mode '.$fileprefix.".\n";
125 }
126
127 $reg = array();
128 if (preg_match('/^regenerate=(.*)/i', $value, $reg)) {
129 if (!in_array($reg[1], array('', '0', 'no'))) {
130 $found = true;
131 $regenerate = $reg[1];
132 print 'Regeneration of PDF is requested with template '.$regenerate."\n";
133 }
134 }
135 if (preg_match('/^regeneratenomerge=(.*)/i', $value, $reg)) {
136 if (!in_array($reg[1], array('', '0', 'no'))) {
137 $found = true;
138 $regenerate = $reg[1];
139 $nomerge = 1;
140 print 'Regeneration (without merge) of PDF is requested with template '.$regenerate."\n";
141 }
142 }
143
144 if ($value == 'filter=all') {
145 $found = true;
146 $option .= (empty($option) ? '' : '_').'all';
147 $filter[] = 'all';
148
149 print 'Rebuild PDF for all invoices'."\n";
150 }
151
152 if ($value == 'filter=date') {
153 $found = true;
154 $option .= (empty($option) ? '' : '_').'date_'.$argv[$key + 1].'_'.$argv[$key + 2];
155 $filter[] = 'date';
156
157 $dateafterdate = dol_stringtotime($argv[$key + 1]);
158 $datebeforedate = dol_stringtotime($argv[$key + 2]);
159 print 'Rebuild PDF for documents validated between '.dol_print_date($dateafterdate, 'day', 'gmt')." and ".dol_print_date($datebeforedate, 'day', 'gmt').".\n";
160 }
161
162 if ($value == 'filter=payments') {
163 $found = true;
164 $option .= (empty($option) ? '' : '_').'payments_'.$argv[$key + 1].'_'.$argv[$key + 2];
165 $filter[] = 'payments';
166
167 $paymentdateafter = dol_stringtotime($argv[$key + 1].'000000');
168 $paymentdatebefore = dol_stringtotime($argv[$key + 2].'235959');
169 if (empty($paymentdateafter) || empty($paymentdatebefore)) {
170 print 'Error: Bad date format or value'."\n";
171 exit(1);
172 }
173 print 'Rebuild PDF for invoices with at least one payment between '.dol_print_date($paymentdateafter, 'day', 'gmt')." and ".dol_print_date($paymentdatebefore, 'day', 'gmt').".\n";
174 }
175
176 if ($value == 'filter=nopayment') {
177 $found = true;
178 $option .= (empty($option) ? '' : '_').'nopayment';
179 $filter[] = 'nopayment';
180
181 print 'Rebuild PDF for invoices with no payment done yet.'."\n";
182 }
183
184 if ($value == 'filter=bank') {
185 $found = true;
186 $option .= (empty($option) ? '' : '_').'bank_'.$argv[$key + 1];
187 $filter[] = 'bank';
188
189 $paymentonbankref = $argv[$key + 1];
190 $bankaccount = new Account($db);
191 $result = $bankaccount->fetch(0, $paymentonbankref);
192 if ($result <= 0) {
193 print 'Error: Bank account with ref "'.$paymentonbankref.'" not found'."\n";
194 exit(1);
195 }
196 $paymentonbankid = $bankaccount->id;
197 print 'Rebuild PDF for invoices with at least one payment on financial account '.$bankaccount->ref."\n";
198 }
199
200 if ($value == 'filter=nodeposit') {
201 $found = true;
202 $option .= (empty($option) ? '' : '_').'nodeposit';
203 $filter[] = 'nodeposit';
204
205 print 'Exclude deposit invoices'."\n";
206 }
207 if ($value == 'filter=noreplacement') {
208 $found = true;
209 $option .= (empty($option) ? '' : '_').'noreplacement';
210 $filter[] = 'noreplacement';
211
212 print 'Exclude replacement invoices'."\n";
213 }
214 if ($value == 'filter=nocreditnote') {
215 $found = true;
216 $option .= (empty($option) ? '' : '_').'nocreditnote';
217 $filter[] = 'nocreditnote';
218
219 print 'Exclude credit note invoices'."\n";
220 }
221
222 if ($value == 'filter=excludethirdparties') {
223 $found = true;
224 $filter[] = 'excludethirdparties';
225
226 $thirdpartiesid = explode(',', $argv[$key + 1]);
227 print 'Exclude thirdparties with id in list ('.implode(',', $thirdpartiesid).").\n";
228
229 $option .= (empty($option) ? '' : '_').'excludethirdparties'.implode('-', $thirdpartiesid);
230 }
231 if ($value == 'filter=onlythirdparties') {
232 $found = true;
233 $filter[] = 'onlythirdparties';
234
235 $thirdpartiesid = explode(',', $argv[$key + 1]);
236 print 'Only thirdparties with id in list ('.implode(',', $thirdpartiesid).").\n";
237
238 $option .= (empty($option) ? '' : '_').'onlythirdparty'.implode('-', $thirdpartiesid);
239 }
240
241 if (!$found && preg_match('/filter=/i', $value)) {
243 exit(1);
244 }
245
246 $thirdpartiesid = array_map('intval', $thirdpartiesid);
247 '@phan-var-force int[] $thirdpartiesid';
248}
249
250// Check if an option and a filter has been provided
251if (empty($option) && count($filter) <= 0) {
253 exit(1);
254}
255// Check if there is no incompatible choice
256if (in_array('payments', $filter) && in_array('nopayment', $filter)) {
258 exit(1);
259}
260if (in_array('bank', $filter) && in_array('nopayment', $filter)) {
262 exit(1);
263}
264
265$diroutputpdf = 'auto';
266
267// Define SQL and SQL request to select invoices
268// Use $filter, $dateafterdate, datebeforedate, $paymentdateafter, $paymentdatebefore
269$result = rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, 1, $regenerate, $option, (string) $paymentonbankid, $thirdpartiesid, $fileprefix, $nomerge, $mode);
270
271// -------------------- END OF YOUR CODE --------------------
272
273if ($result >= 0) {
274 $error = 0;
275 print '--- end ok'."\n";
276} else {
277 $error = $result;
278 print '--- end error code='.$error."\n";
279}
280
281$db->close();
282
283exit($error);
284
291{
292 global $script_file;
293
294 print "Rebuild PDF files for some invoices/orders/proposals and/or merge PDF files into one.\n";
295 print "\n";
296 print "To build/merge PDF for all documents, use filter=all\n";
297 print "Usage: ".$script_file." mode=invoice filter=all\n";
298 print "To build/merge PDF for documents in a date range:\n";
299 print "Usage: ".$script_file." mode=invoice filter=date dateafter datebefore\n";
300 print "To build/merge PDF for invoices with at least one payment in a date range:\n";
301 print "Usage: ".$script_file." mode=invoice filter=payments dateafter datebefore\n";
302 print "To build/merge PDF for invoices with at least one payment onto a bank account:\n";
303 print "Usage: ".$script_file." mode=invoice filter=bank bankref\n";
304 print "To build/merge PDF for invoices with no payments, use filter=nopayment\n";
305 print "Usage: ".$script_file." mode=invoice filter=nopayment\n";
306 print "To exclude credit notes, use filter=nocreditnote\n";
307 print "To exclude replacement invoices, use filter=noreplacement\n";
308 print "To exclude deposit invoices, use filter=nodeposit\n";
309 print "To exclude some thirdparties, use filter=excludethirdparties id1,id2...\n";
310 print "To limit to some thirdparties, use filter=onlythirdparties id1,id2...\n";
311 print "\n";
312 print "To regenerate existing PDF before merge, use regenerate=templatename\n";
313 print "To regenerate existing PDF with no merge (regenerate only), use regeneratenomerge=templatename\n";
314 print "To merge by reusing existing PDF (generation only for non already existing PDF), use regenerate=no or nothing\n";
315 print "\n";
316 print "To generate documents in a given language, use lang=xx_XX\n";
317 print "To set prefix of generated file name, use prefix=myfileprefix\n";
318 print "\n";
319 print "Example: ".$script_file." mode=invoice filter=payments 20080101 20081231 lang=fr_FR regenerate=crabe\n";
320 print "Example: ".$script_file." mode=invoice filter=all lang=en_US\n";
321 print "\n";
322 print "Note that some filters can be cumulated.\n";
323}
Class to manage bank accounts.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition date.lib.php:436
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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.
rebuild_merge_pdf_usage()
Show usage of script.