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";
43// After this $db is an opened handler to database. We close it at end of file.
44require_once DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php";
45require_once DOL_DOCUMENT_ROOT."/core/modules/facture/modules_facture.php";
46require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
47require_once DOL_DOCUMENT_ROOT.'/core/lib/invoice2.lib.php';
48
49// Load main language strings
50$langs->load("main");
51
52// Global variables
53$version = DOL_VERSION;
54$error = 0;
55
56$hookmanager->initHooks(array('cli'));
57
58
59/*
60 * Main
61 */
62
63@set_time_limit(0);
64print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
65dol_syslog($script_file." launched with arg ".join(',', $argv));
66
67// Check parameters
68if (!isset($argv[1])) {
70 exit(1);
71}
72
73if (!empty($dolibarr_main_db_readonly)) {
74 print "Error: instance in read-onyl mode\n";
75 exit(1);
76}
77
78$diroutputpdf = $conf->facture->dir_output.'/temp';
79$newlangid = 'en_EN'; // To force a new lang id
80$filter = array();
81$regenerate = ''; // Ask regenerate (contains name of model to use)
82$option = '';
83$fileprefix = 'mergedpdf';
84$dateafterdate = '';
85$datebeforedate = '';
86$paymentdateafter = '';
87$paymentdatebefore = '';
88$paymentonbankid = 0;
89$thirdpartiesid = 0;
90
91foreach ($argv as $key => $value) {
92 $found = false;
93
94 // Define options
95 if (preg_match('/^lang=/i', $value)) {
96 $found = true;
97 $valarray = explode('=', $value);
98 $newlangid = $valarray[1];
99 print 'Use language '.$newlangid.".\n";
100 }
101 if (preg_match('/^prefix=/i', $value)) {
102 $found = true;
103 $valarray = explode('=', $value);
104 $fileprefix = $valarray[1];
105 print 'Use prefix for filename '.$fileprefix.".\n";
106 }
107
108 if (preg_match('/^regenerate=(.*)/i', $value, $reg)) {
109 if (!in_array($reg[1], array('', '0', 'no'))) {
110 $found = true;
111 $regenerate = $reg[1];
112 print 'Regeneration of PDF is requested with template '.$regenerate."\n";
113 }
114 }
115
116 if ($value == 'filter=all') {
117 $found = true;
118 $option .= (empty($option) ? '' : '_').'all';
119 $filter[] = 'all';
120
121 print 'Rebuild PDF for all invoices'."\n";
122 }
123
124 if ($value == 'filter=date') {
125 $found = true;
126 $option .= (empty($option) ? '' : '_').'date_'.$argv[$key + 1].'_'.$argv[$key + 2];
127 $filter[] = 'date';
128
129 $dateafterdate = dol_stringtotime($argv[$key + 1]);
130 $datebeforedate = dol_stringtotime($argv[$key + 2]);
131 print 'Rebuild PDF for invoices validated between '.dol_print_date($dateafterdate, 'day', 'gmt')." and ".dol_print_date($datebeforedate, 'day', 'gmt').".\n";
132 }
133
134 if ($value == 'filter=payments') {
135 $found = true;
136 $option .= (empty($option) ? '' : '_').'payments_'.$argv[$key + 1].'_'.$argv[$key + 2];
137 $filter[] = 'payments';
138
139 $paymentdateafter = dol_stringtotime($argv[$key + 1].'000000');
140 $paymentdatebefore = dol_stringtotime($argv[$key + 2].'235959');
141 if (empty($paymentdateafter) || empty($paymentdatebefore)) {
142 print 'Error: Bad date format or value'."\n";
143 exit(1);
144 }
145 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";
146 }
147
148 if ($value == 'filter=nopayment') {
149 $found = true;
150 $option .= (empty($option) ? '' : '_').'nopayment';
151 $filter[] = 'nopayment';
152
153 print 'Rebuild PDF for invoices with no payment done yet.'."\n";
154 }
155
156 if ($value == 'filter=bank') {
157 $found = true;
158 $option .= (empty($option) ? '' : '_').'bank_'.$argv[$key + 1];
159 $filter[] = 'bank';
160
161 $paymentonbankref = $argv[$key + 1];
162 $bankaccount = new Account($db);
163 $result = $bankaccount->fetch(0, $paymentonbankref);
164 if ($result <= 0) {
165 print 'Error: Bank account with ref "'.$paymentonbankref.'" not found'."\n";
166 exit(1);
167 }
168 $paymentonbankid = $bankaccount->id;
169 print 'Rebuild PDF for invoices with at least one payment on financial account '.$bankaccount->ref."\n";
170 }
171
172 if ($value == 'filter=nodeposit') {
173 $found = true;
174 $option .= (empty($option) ? '' : '_').'nodeposit';
175 $filter[] = 'nodeposit';
176
177 print 'Exclude deposit invoices'."\n";
178 }
179 if ($value == 'filter=noreplacement') {
180 $found = true;
181 $option .= (empty($option) ? '' : '_').'noreplacement';
182 $filter[] = 'noreplacement';
183
184 print 'Exclude replacement invoices'."\n";
185 }
186 if ($value == 'filter=nocreditnote') {
187 $found = true;
188 $option .= (empty($option) ? '' : '_').'nocreditnote';
189 $filter[] = 'nocreditnote';
190
191 print 'Exclude credit note invoices'."\n";
192 }
193
194 if ($value == 'filter=excludethirdparties') {
195 $found = true;
196 $filter[] = 'excludethirdparties';
197
198 $thirdpartiesid = explode(',', $argv[$key + 1]);
199 print 'Exclude thirdparties with id in list ('.join(',', $thirdpartiesid).").\n";
200
201 $option .= (empty($option) ? '' : '_').'excludethirdparties'.join('-', $thirdpartiesid);
202 }
203 if ($value == 'filter=onlythirdparties') {
204 $found = true;
205 $filter[] = 'onlythirdparties';
206
207 $thirdpartiesid = explode(',', $argv[$key + 1]);
208 print 'Only thirdparties with id in list ('.join(',', $thirdpartiesid).").\n";
209
210 $option .= (empty($option) ? '' : '_').'onlythirdparty'.join('-', $thirdpartiesid);
211 }
212
213 if (!$found && preg_match('/filter=/i', $value)) {
215 exit(1);
216 }
217}
218
219// Check if an option and a filter has been provided
220if (empty($option) && count($filter) <= 0) {
222 exit(1);
223}
224// Check if there is no incompatible choice
225if (in_array('payments', $filter) && in_array('nopayment', $filter)) {
227 exit(1);
228}
229if (in_array('bank', $filter) && in_array('nopayment', $filter)) {
231 exit(1);
232}
233
234// Define SQL and SQL request to select invoices
235// Use $filter, $dateafterdate, datebeforedate, $paymentdateafter, $paymentdatebefore
236$result = rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, 1, $regenerate, $option, (string) $paymentonbankid, $thirdpartiesid, $fileprefix);
237
238// -------------------- END OF YOUR CODE --------------------
239
240if ($result >= 0) {
241 $error = 0;
242 print '--- end ok'."\n";
243} else {
244 $error = $result;
245 print '--- end error code='.$error."\n";
246}
247
248$db->close();
249
250exit($error);
251
258{
259 global $script_file;
260
261 print "Rebuild PDF files for some invoices and merge PDF files into one.\n";
262 print "\n";
263 print "To build/merge PDF for invoices in a date range:\n";
264 print "Usage: ".$script_file." filter=date dateafter datebefore\n";
265 print "To build/merge PDF for invoices with at least one payment in a date range:\n";
266 print "Usage: ".$script_file." filter=payments dateafter datebefore\n";
267 print "To build/merge PDF for invoices with at least one payment onto a bank account:\n";
268 print "Usage: ".$script_file." filter=bank bankref\n";
269 print "To build/merge PDF for all invoices, use filter=all\n";
270 print "Usage: ".$script_file." filter=all\n";
271 print "To build/merge PDF for invoices with no payments, use filter=nopayment\n";
272 print "Usage: ".$script_file." filter=nopayment\n";
273 print "To exclude credit notes, use filter=nocreditnote\n";
274 print "To exclude replacement invoices, use filter=noreplacement\n";
275 print "To exclude deposit invoices, use filter=nodeposit\n";
276 print "To exclude some thirdparties, use filter=excludethirdparties id1,id2...\n";
277 print "To limit to some thirdparties, use filter=onlythirdparties id1,id2...\n";
278 print "To regenerate existing PDF, use regenerate=templatename\n";
279 print "To generate invoices in a language, use lang=xx_XX\n";
280 print "To set prefix of generated file name, use prefix=myfileprefix\n";
281 print "\n";
282 print "Example: ".$script_file." filter=payments 20080101 20081231 lang=fr_FR regenerate=crabe\n";
283 print "Example: ".$script_file." filter=all lang=en_US\n";
284 print "\n";
285 print "Note that some filters can be cumulated.\n";
286}
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:426
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.