dolibarr 21.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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27if (!defined('NOSESSION')) {
28 define('NOSESSION', '1');
29}
30
31$sapi_type = php_sapi_name();
32$script_file = basename(__FILE__);
33$path = __DIR__.'/';
34
35// Test if batch mode
36if (substr($sapi_type, 0, 3) == 'cgi') {
37 echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
38 exit(1);
39}
40
41// Include Dolibarr environment
42require_once $path."../../htdocs/master.inc.php";
43require_once DOL_DOCUMENT_ROOT.'/core/lib/functionscli.lib.php';
44// After this $db is an opened handler to database. We close it at end of file.
45require_once DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php";
46require_once DOL_DOCUMENT_ROOT."/core/modules/facture/modules_facture.php";
47require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
48require_once DOL_DOCUMENT_ROOT.'/core/lib/invoice2.lib.php';
49
50// Load main language strings
51$langs->load("main");
52
53// Global variables
54$version = DOL_VERSION;
55$error = 0;
56
57$hookmanager->initHooks(array('cli'));
58
59
60/*
61 * Main
62 */
63
64@set_time_limit(0);
65print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
66dol_syslog($script_file." launched with arg ".join(',', $argv));
67
68// Check parameters
69if (!isset($argv[1])) {
71 exit(1);
72}
73
74if (!empty($dolibarr_main_db_readonly)) {
75 print "Error: instance in read-onyl mode\n";
76 exit(1);
77}
78
79$diroutputpdf = $conf->facture->dir_output.'/temp';
80$newlangid = 'en_EN'; // To force a new lang id
81$filter = array();
82$regenerate = ''; // Ask regenerate (contains name of model to use)
83$option = '';
84$fileprefix = 'mergedpdf';
85$dateafterdate = '';
86$datebeforedate = '';
87$paymentdateafter = '';
88$paymentdatebefore = '';
89$paymentonbankid = 0;
90$thirdpartiesid = 0;
91
92foreach ($argv as $key => $value) {
93 $found = false;
94
95 // Define options
96 if (preg_match('/^lang=/i', $value)) {
97 $found = true;
98 $valarray = explode('=', $value);
99 $newlangid = $valarray[1];
100 print 'Use language '.$newlangid.".\n";
101 }
102 if (preg_match('/^prefix=/i', $value)) {
103 $found = true;
104 $valarray = explode('=', $value);
105 $fileprefix = $valarray[1];
106 print 'Use prefix for filename '.$fileprefix.".\n";
107 }
108
109 if (preg_match('/^regenerate=(.*)/i', $value, $reg)) {
110 if (!in_array($reg[1], array('', '0', 'no'))) {
111 $found = true;
112 $regenerate = $reg[1];
113 print 'Regeneration of PDF is requested with template '.$regenerate."\n";
114 }
115 }
116
117 if ($value == 'filter=all') {
118 $found = true;
119 $option .= (empty($option) ? '' : '_').'all';
120 $filter[] = 'all';
121
122 print 'Rebuild PDF for all invoices'."\n";
123 }
124
125 if ($value == 'filter=date') {
126 $found = true;
127 $option .= (empty($option) ? '' : '_').'date_'.$argv[$key + 1].'_'.$argv[$key + 2];
128 $filter[] = 'date';
129
130 $dateafterdate = dol_stringtotime($argv[$key + 1]);
131 $datebeforedate = dol_stringtotime($argv[$key + 2]);
132 print 'Rebuild PDF for invoices validated between '.dol_print_date($dateafterdate, 'day', 'gmt')." and ".dol_print_date($datebeforedate, 'day', 'gmt').".\n";
133 }
134
135 if ($value == 'filter=payments') {
136 $found = true;
137 $option .= (empty($option) ? '' : '_').'payments_'.$argv[$key + 1].'_'.$argv[$key + 2];
138 $filter[] = 'payments';
139
140 $paymentdateafter = dol_stringtotime($argv[$key + 1].'000000');
141 $paymentdatebefore = dol_stringtotime($argv[$key + 2].'235959');
142 if (empty($paymentdateafter) || empty($paymentdatebefore)) {
143 print 'Error: Bad date format or value'."\n";
144 exit(1);
145 }
146 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";
147 }
148
149 if ($value == 'filter=nopayment') {
150 $found = true;
151 $option .= (empty($option) ? '' : '_').'nopayment';
152 $filter[] = 'nopayment';
153
154 print 'Rebuild PDF for invoices with no payment done yet.'."\n";
155 }
156
157 if ($value == 'filter=bank') {
158 $found = true;
159 $option .= (empty($option) ? '' : '_').'bank_'.$argv[$key + 1];
160 $filter[] = 'bank';
161
162 $paymentonbankref = $argv[$key + 1];
163 $bankaccount = new Account($db);
164 $result = $bankaccount->fetch(0, $paymentonbankref);
165 if ($result <= 0) {
166 print 'Error: Bank account with ref "'.$paymentonbankref.'" not found'."\n";
167 exit(1);
168 }
169 $paymentonbankid = $bankaccount->id;
170 print 'Rebuild PDF for invoices with at least one payment on financial account '.$bankaccount->ref."\n";
171 }
172
173 if ($value == 'filter=nodeposit') {
174 $found = true;
175 $option .= (empty($option) ? '' : '_').'nodeposit';
176 $filter[] = 'nodeposit';
177
178 print 'Exclude deposit invoices'."\n";
179 }
180 if ($value == 'filter=noreplacement') {
181 $found = true;
182 $option .= (empty($option) ? '' : '_').'noreplacement';
183 $filter[] = 'noreplacement';
184
185 print 'Exclude replacement invoices'."\n";
186 }
187 if ($value == 'filter=nocreditnote') {
188 $found = true;
189 $option .= (empty($option) ? '' : '_').'nocreditnote';
190 $filter[] = 'nocreditnote';
191
192 print 'Exclude credit note invoices'."\n";
193 }
194
195 if ($value == 'filter=excludethirdparties') {
196 $found = true;
197 $filter[] = 'excludethirdparties';
198
199 $thirdpartiesid = explode(',', $argv[$key + 1]);
200 print 'Exclude thirdparties with id in list ('.join(',', $thirdpartiesid).").\n";
201
202 $option .= (empty($option) ? '' : '_').'excludethirdparties'.join('-', $thirdpartiesid);
203 }
204 if ($value == 'filter=onlythirdparties') {
205 $found = true;
206 $filter[] = 'onlythirdparties';
207
208 $thirdpartiesid = explode(',', $argv[$key + 1]);
209 print 'Only thirdparties with id in list ('.join(',', $thirdpartiesid).").\n";
210
211 $option .= (empty($option) ? '' : '_').'onlythirdparty'.join('-', $thirdpartiesid);
212 }
213
214 if (!$found && preg_match('/filter=/i', $value)) {
216 exit(1);
217 }
218}
219
220// Check if an option and a filter has been provided
221if (empty($option) && count($filter) <= 0) {
223 exit(1);
224}
225// Check if there is no incompatible choice
226if (in_array('payments', $filter) && in_array('nopayment', $filter)) {
228 exit(1);
229}
230if (in_array('bank', $filter) && in_array('nopayment', $filter)) {
232 exit(1);
233}
234
235// Define SQL and SQL request to select invoices
236// Use $filter, $dateafterdate, datebeforedate, $paymentdateafter, $paymentdatebefore
237$result = rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, 1, $regenerate, $option, (string) $paymentonbankid, $thirdpartiesid, $fileprefix);
238
239// -------------------- END OF YOUR CODE --------------------
240
241if ($result >= 0) {
242 $error = 0;
243 print '--- end ok'."\n";
244} else {
245 $error = $result;
246 print '--- end error code='.$error."\n";
247}
248
249$db->close();
250
251exit($error);
252
259{
260 global $script_file;
261
262 print "Rebuild PDF files for some invoices and merge PDF files into one.\n";
263 print "\n";
264 print "To build/merge PDF for invoices in a date range:\n";
265 print "Usage: ".$script_file." filter=date dateafter datebefore\n";
266 print "To build/merge PDF for invoices with at least one payment in a date range:\n";
267 print "Usage: ".$script_file." filter=payments dateafter datebefore\n";
268 print "To build/merge PDF for invoices with at least one payment onto a bank account:\n";
269 print "Usage: ".$script_file." filter=bank bankref\n";
270 print "To build/merge PDF for all invoices, use filter=all\n";
271 print "Usage: ".$script_file." filter=all\n";
272 print "To build/merge PDF for invoices with no payments, use filter=nopayment\n";
273 print "Usage: ".$script_file." filter=nopayment\n";
274 print "To exclude credit notes, use filter=nocreditnote\n";
275 print "To exclude replacement invoices, use filter=noreplacement\n";
276 print "To exclude deposit invoices, use filter=nodeposit\n";
277 print "To exclude some thirdparties, use filter=excludethirdparties id1,id2...\n";
278 print "To limit to some thirdparties, use filter=onlythirdparties id1,id2...\n";
279 print "To regenerate existing PDF, use regenerate=templatename\n";
280 print "To generate invoices in a language, use lang=xx_XX\n";
281 print "To set prefix of generated file name, use prefix=myfileprefix\n";
282 print "\n";
283 print "Example: ".$script_file." filter=payments 20080101 20081231 lang=fr_FR regenerate=crabe\n";
284 print "Example: ".$script_file." filter=all lang=en_US\n";
285 print "\n";
286 print "Note that some filters can be cumulated.\n";
287}
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:427
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')
Function to build a compiled PDF.
rebuild_merge_pdf_usage()
Show usage of script.