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