dolibarr 21.0.0-beta
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 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
56// Load main language strings
57$langs->load("main");
58
59// Global variables
60$version = DOL_VERSION;
61$error = 0;
62
63$hookmanager->initHooks(array('cli'));
64
65
66/*
67 * Main
68 */
69
70@set_time_limit(0);
71print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
72dol_syslog($script_file." launched with arg ".join(',', $argv));
73
74// Check parameters
75if (!isset($argv[1])) {
77 exit(1);
78}
79
80if (!empty($dolibarr_main_db_readonly)) {
81 print "Error: instance in read-onyl mode\n";
82 exit(1);
83}
84
85
86$newlangid = 'en_EN'; // To force a new lang id
87$filter = array();
88$regenerate = ''; // Ask regenerate (contains name of model to use)
89$option = '';
90$fileprefix = 'mergedpdf';
91$nomerge = 0;
92$mode = 'invoice';
93
94$dateafterdate = '';
95$datebeforedate = '';
96$paymentdateafter = '';
97$paymentdatebefore = '';
98$paymentonbankid = 0;
99$thirdpartiesid = array();
100
101foreach ($argv as $key => $value) {
102 $found = false;
103
104 // Define options
105 if (preg_match('/^lang=/i', $value)) {
106 $found = true;
107 $valarray = explode('=', $value);
108 $newlangid = $valarray[1];
109 print 'Use language '.$newlangid.".\n";
110 }
111 if (preg_match('/^prefix=/i', $value)) {
112 $found = true;
113 $valarray = explode('=', $value);
114 $fileprefix = $valarray[1];
115 print 'Use prefix for filename '.$fileprefix.".\n";
116 }
117
118 if (preg_match('/^mode=/i', $value)) {
119 $found = true;
120 $valarray = explode('=', $value);
121 $mode = $valarray[1];
122 print 'Use mode '.$fileprefix.".\n";
123 }
124
125 $reg = array();
126 if (preg_match('/^regenerate=(.*)/i', $value, $reg)) {
127 if (!in_array($reg[1], array('', '0', 'no'))) {
128 $found = true;
129 $regenerate = $reg[1];
130 print 'Regeneration of PDF is requested with template '.$regenerate."\n";
131 }
132 }
133 if (preg_match('/^regeneratenomerge=(.*)/i', $value, $reg)) {
134 if (!in_array($reg[1], array('', '0', 'no'))) {
135 $found = true;
136 $regenerate = $reg[1];
137 $nomerge = 1;
138 print 'Regeneration (without merge) of PDF is requested with template '.$regenerate."\n";
139 }
140 }
141
142 if ($value == 'filter=all') {
143 $found = true;
144 $option .= (empty($option) ? '' : '_').'all';
145 $filter[] = 'all';
146
147 print 'Rebuild PDF for all invoices'."\n";
148 }
149
150 if ($value == 'filter=date') {
151 $found = true;
152 $option .= (empty($option) ? '' : '_').'date_'.$argv[$key + 1].'_'.$argv[$key + 2];
153 $filter[] = 'date';
154
155 $dateafterdate = dol_stringtotime($argv[$key + 1]);
156 $datebeforedate = dol_stringtotime($argv[$key + 2]);
157 print 'Rebuild PDF for documents validated between '.dol_print_date($dateafterdate, 'day', 'gmt')." and ".dol_print_date($datebeforedate, 'day', 'gmt').".\n";
158 }
159
160 if ($value == 'filter=payments') {
161 $found = true;
162 $option .= (empty($option) ? '' : '_').'payments_'.$argv[$key + 1].'_'.$argv[$key + 2];
163 $filter[] = 'payments';
164
165 $paymentdateafter = dol_stringtotime($argv[$key + 1].'000000');
166 $paymentdatebefore = dol_stringtotime($argv[$key + 2].'235959');
167 if (empty($paymentdateafter) || empty($paymentdatebefore)) {
168 print 'Error: Bad date format or value'."\n";
169 exit(1);
170 }
171 print 'Rebuild PDF for ivoices with at least one payment between '.dol_print_date($paymentdateafter, 'day', 'gmt')." and ".dol_print_date($paymentdatebefore, 'day', 'gmt').".\n";
172 }
173
174 if ($value == 'filter=nopayment') {
175 $found = true;
176 $option .= (empty($option) ? '' : '_').'nopayment';
177 $filter[] = 'nopayment';
178
179 print 'Rebuild PDF for ivoices with no payment done yet.'."\n";
180 }
181
182 if ($value == 'filter=bank') {
183 $found = true;
184 $option .= (empty($option) ? '' : '_').'bank_'.$argv[$key + 1];
185 $filter[] = 'bank';
186
187 $paymentonbankref = $argv[$key + 1];
188 $bankaccount = new Account($db);
189 $result = $bankaccount->fetch(0, $paymentonbankref);
190 if ($result <= 0) {
191 print 'Error: Bank account with ref "'.$paymentonbankref.'" not found'."\n";
192 exit(1);
193 }
194 $paymentonbankid = $bankaccount->id;
195 print 'Rebuild PDF for invoices with at least one payment on financial account '.$bankaccount->ref."\n";
196 }
197
198 if ($value == 'filter=nodeposit') {
199 $found = true;
200 $option .= (empty($option) ? '' : '_').'nodeposit';
201 $filter[] = 'nodeposit';
202
203 print 'Exclude deposit invoices'."\n";
204 }
205 if ($value == 'filter=noreplacement') {
206 $found = true;
207 $option .= (empty($option) ? '' : '_').'noreplacement';
208 $filter[] = 'noreplacement';
209
210 print 'Exclude replacement invoices'."\n";
211 }
212 if ($value == 'filter=nocreditnote') {
213 $found = true;
214 $option .= (empty($option) ? '' : '_').'nocreditnote';
215 $filter[] = 'nocreditnote';
216
217 print 'Exclude credit note invoices'."\n";
218 }
219
220 if ($value == 'filter=excludethirdparties') {
221 $found = true;
222 $filter[] = 'excludethirdparties';
223
224 $thirdpartiesid = explode(',', $argv[$key + 1]);
225 print 'Exclude thirdparties with id in list ('.join(',', $thirdpartiesid).").\n";
226
227 $option .= (empty($option) ? '' : '_').'excludethirdparties'.join('-', $thirdpartiesid);
228 }
229 if ($value == 'filter=onlythirdparties') {
230 $found = true;
231 $filter[] = 'onlythirdparties';
232
233 $thirdpartiesid = explode(',', $argv[$key + 1]);
234 print 'Only thirdparties with id in list ('.join(',', $thirdpartiesid).").\n";
235
236 $option .= (empty($option) ? '' : '_').'onlythirdparty'.join('-', $thirdpartiesid);
237 }
238
239 if (!$found && preg_match('/filter=/i', $value)) {
241 exit(1);
242 }
243
244 $thirdpartiesid = array_map('intval', $thirdpartiesid);
245 '@phan-var-force int[] $thirdpartiesid';
246}
247
248// Check if an option and a filter has been provided
249if (empty($option) && count($filter) <= 0) {
251 exit(1);
252}
253// Check if there is no incompatible choice
254if (in_array('payments', $filter) && in_array('nopayment', $filter)) {
256 exit(1);
257}
258if (in_array('bank', $filter) && in_array('nopayment', $filter)) {
260 exit(1);
261}
262
263$diroutputpdf = 'auto';
264
265// Define SQL and SQL request to select invoices
266// Use $filter, $dateafterdate, datebeforedate, $paymentdateafter, $paymentdatebefore
267$result = rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, 1, $regenerate, $option, (string) $paymentonbankid, $thirdpartiesid, $fileprefix, $nomerge, $mode);
268
269// -------------------- END OF YOUR CODE --------------------
270
271if ($result >= 0) {
272 $error = 0;
273 print '--- end ok'."\n";
274} else {
275 $error = $result;
276 print '--- end error code='.$error."\n";
277}
278
279$db->close();
280
281exit($error);
282
289{
290 global $script_file;
291
292 print "Rebuild PDF files for some invoices/orders/proposals and/or merge PDF files into one.\n";
293 print "\n";
294 print "To build/merge PDF for all documents, use filter=all\n";
295 print "Usage: ".$script_file." mode=invoice filter=all\n";
296 print "To build/merge PDF for documents in a date range:\n";
297 print "Usage: ".$script_file." mode=invoice filter=date dateafter datebefore\n";
298 print "To build/merge PDF for invoices with at least one payment in a date range:\n";
299 print "Usage: ".$script_file." mode=invoice filter=payments dateafter datebefore\n";
300 print "To build/merge PDF for invoices with at least one payment onto a bank account:\n";
301 print "Usage: ".$script_file." mode=invoice filter=bank bankref\n";
302 print "To build/merge PDF for invoices with no payments, use filter=nopayment\n";
303 print "Usage: ".$script_file." mode=invoice filter=nopayment\n";
304 print "To exclude credit notes, use filter=nocreditnote\n";
305 print "To exclude replacement invoices, use filter=noreplacement\n";
306 print "To exclude deposit invoices, use filter=nodeposit\n";
307 print "To exclude some thirdparties, use filter=excludethirdparties id1,id2...\n";
308 print "To limit to some thirdparties, use filter=onlythirdparties id1,id2...\n";
309 print "To regenerate existing PDF, use regenerate=templatename or regeneratenomerge=templatename\n";
310 print "To generate documents in a given language, use lang=xx_XX\n";
311 print "To set prefix of generated file name, use prefix=myfileprefix\n";
312 print "\n";
313 print "Example: ".$script_file." mode=invoice filter=payments 20080101 20081231 lang=fr_FR regenerate=crabe\n";
314 print "Example: ".$script_file." mode=invoice filter=all lang=en_US\n";
315 print "\n";
316 print "Note that some filters can be cumulated.\n";
317}
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:431
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
rebuild_merge_pdf_usage()
Show usage of script.