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