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