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