28if (!defined(
'NOSESSION')) {
29 define(
'NOSESSION',
'1');
32$sapi_type = php_sapi_name();
33$script_file = basename(__FILE__);
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";
43require_once $path.
"../../htdocs/master.inc.php";
44require_once DOL_DOCUMENT_ROOT.
'/core/lib/functionscli.lib.php';
46require_once DOL_DOCUMENT_ROOT.
"/core/lib/date.lib.php";
47require_once DOL_DOCUMENT_ROOT.
'/core/lib/invoice2.lib.php';
60$version = DOL_VERSION;
63$hookmanager->initHooks(array(
'cli'));
71print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
72dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
75if (!isset($argv[1])) {
80if (!empty($dolibarr_main_db_readonly)) {
81 print
"Error: instance in read-onyl mode\n";
90$fileprefix =
'mergedpdf';
96$paymentdateafter =
'';
97$paymentdatebefore =
'';
99$thirdpartiesid = array();
101foreach ($argv as $key => $value) {
105 if (preg_match(
'/^lang=/i', $value)) {
107 $valarray = explode(
'=', $value);
108 $newlangid = $valarray[1];
109 print
'Use language '.$newlangid.
".\n";
111 if (preg_match(
'/^prefix=/i', $value)) {
113 $valarray = explode(
'=', $value);
114 $fileprefix = $valarray[1];
115 print
'Use prefix for filename '.$fileprefix.
".\n";
118 if (preg_match(
'/^mode=/i', $value)) {
120 $valarray = explode(
'=', $value);
121 $mode = $valarray[1];
122 print
'Use mode '.$fileprefix.
".\n";
126 if (preg_match(
'/^regenerate=(.*)/i', $value, $reg)) {
127 if (!in_array($reg[1], array(
'',
'0',
'no'))) {
129 $regenerate = $reg[1];
130 print
'Regeneration of PDF is requested with template '.$regenerate.
"\n";
133 if (preg_match(
'/^regeneratenomerge=(.*)/i', $value, $reg)) {
134 if (!in_array($reg[1], array(
'',
'0',
'no'))) {
136 $regenerate = $reg[1];
138 print
'Regeneration (without merge) of PDF is requested with template '.$regenerate.
"\n";
142 if ($value ==
'filter=all') {
144 $option .= (empty($option) ?
'' :
'_').
'all';
147 print
'Rebuild PDF for all invoices'.
"\n";
150 if ($value ==
'filter=date') {
152 $option .= (empty($option) ?
'' :
'_').
'date_'.$argv[$key + 1].
'_'.$argv[$key + 2];
157 print
'Rebuild PDF for documents validated between '.dol_print_date($dateafterdate,
'day',
'gmt').
" and ".
dol_print_date($datebeforedate,
'day',
'gmt').
".\n";
160 if ($value ==
'filter=payments') {
162 $option .= (empty($option) ?
'' :
'_').
'payments_'.$argv[$key + 1].
'_'.$argv[$key + 2];
163 $filter[] =
'payments';
167 if (empty($paymentdateafter) || empty($paymentdatebefore)) {
168 print
'Error: Bad date format or value'.
"\n";
171 print
'Rebuild PDF for ivoices with at least one payment between '.dol_print_date($paymentdateafter,
'day',
'gmt').
" and ".
dol_print_date($paymentdatebefore,
'day',
'gmt').
".\n";
174 if ($value ==
'filter=nopayment') {
176 $option .= (empty($option) ?
'' :
'_').
'nopayment';
177 $filter[] =
'nopayment';
179 print
'Rebuild PDF for ivoices with no payment done yet.'.
"\n";
182 if ($value ==
'filter=bank') {
184 $option .= (empty($option) ?
'' :
'_').
'bank_'.$argv[$key + 1];
187 $paymentonbankref = $argv[$key + 1];
188 $bankaccount =
new Account($db);
189 $result = $bankaccount->fetch(0, $paymentonbankref);
191 print
'Error: Bank account with ref "'.$paymentonbankref.
'" not found'.
"\n";
194 $paymentonbankid = $bankaccount->id;
195 print
'Rebuild PDF for invoices with at least one payment on financial account '.$bankaccount->ref.
"\n";
198 if ($value ==
'filter=nodeposit') {
200 $option .= (empty($option) ?
'' :
'_').
'nodeposit';
201 $filter[] =
'nodeposit';
203 print
'Exclude deposit invoices'.
"\n";
205 if ($value ==
'filter=noreplacement') {
207 $option .= (empty($option) ?
'' :
'_').
'noreplacement';
208 $filter[] =
'noreplacement';
210 print
'Exclude replacement invoices'.
"\n";
212 if ($value ==
'filter=nocreditnote') {
214 $option .= (empty($option) ?
'' :
'_').
'nocreditnote';
215 $filter[] =
'nocreditnote';
217 print
'Exclude credit note invoices'.
"\n";
220 if ($value ==
'filter=excludethirdparties') {
222 $filter[] =
'excludethirdparties';
224 $thirdpartiesid = explode(
',', $argv[$key + 1]);
225 print
'Exclude thirdparties with id in list ('.join(
',', $thirdpartiesid).
").\n";
227 $option .= (empty($option) ?
'' :
'_').
'excludethirdparties'.join(
'-', $thirdpartiesid);
229 if ($value ==
'filter=onlythirdparties') {
231 $filter[] =
'onlythirdparties';
233 $thirdpartiesid = explode(
',', $argv[$key + 1]);
234 print
'Only thirdparties with id in list ('.join(
',', $thirdpartiesid).
").\n";
236 $option .= (empty($option) ?
'' :
'_').
'onlythirdparty'.join(
'-', $thirdpartiesid);
239 if (!$found && preg_match(
'/filter=/i', $value)) {
244 $thirdpartiesid = array_map(
'intval', $thirdpartiesid);
245 '@phan-var-force int[] $thirdpartiesid';
249if (empty($option) && count($filter) <= 0) {
254if (in_array(
'payments', $filter) && in_array(
'nopayment', $filter)) {
258if (in_array(
'bank', $filter) && in_array(
'nopayment', $filter)) {
263$diroutputpdf =
'auto';
267$result =
rebuild_merge_pdf($db, $langs,
$conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, 1, $regenerate, $option, (
string) $paymentonbankid, $thirdpartiesid, $fileprefix, $nomerge, $mode);
273 print
'--- end ok'.
"\n";
276 print
'--- end error code='.$error.
"\n";
292 print
"Rebuild PDF files for some invoices/orders/proposals and/or merge PDF files into one.\n";
294 print
"To build/merge PDF for all documents, use filter=all\n";
295 print
"Usage: ".$script_file.
" mode=invoice filter=all\n";
296 print
"To build/merge PDF for documents in a date range:\n";
297 print
"Usage: ".$script_file.
" mode=invoice filter=date dateafter datebefore\n";
298 print
"To build/merge PDF for invoices with at least one payment in a date range:\n";
299 print
"Usage: ".$script_file.
" mode=invoice filter=payments dateafter datebefore\n";
300 print
"To build/merge PDF for invoices with at least one payment onto a bank account:\n";
301 print
"Usage: ".$script_file.
" mode=invoice filter=bank bankref\n";
302 print
"To build/merge PDF for invoices with no payments, use filter=nopayment\n";
303 print
"Usage: ".$script_file.
" mode=invoice filter=nopayment\n";
304 print
"To exclude credit notes, use filter=nocreditnote\n";
305 print
"To exclude replacement invoices, use filter=noreplacement\n";
306 print
"To exclude deposit invoices, use filter=nodeposit\n";
307 print
"To exclude some thirdparties, use filter=excludethirdparties id1,id2...\n";
308 print
"To limit to some thirdparties, use filter=onlythirdparties id1,id2...\n";
309 print
"To regenerate existing PDF, use regenerate=templatename or regeneratenomerge=templatename\n";
310 print
"To generate documents in a given language, use lang=xx_XX\n";
311 print
"To set prefix of generated file name, use prefix=myfileprefix\n";
313 print
"Example: ".$script_file.
" mode=invoice filter=payments 20080101 20081231 lang=fr_FR regenerate=crabe\n";
314 print
"Example: ".$script_file.
" mode=invoice filter=all lang=en_US\n";
316 print
"Note that some filters can be cumulated.\n";
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...
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', $donotmerge=0, $mode='invoice')
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...
rebuild_merge_pdf_usage()
Show usage of script.