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';
62$version = DOL_VERSION;
65$hookmanager->initHooks(array(
'cli'));
73print
"***** ".$script_file.
" (".$version.
") pid=".dol_getmypid().
" *****\n";
74dol_syslog($script_file.
" launched with arg ".implode(
',', $argv));
77if (!isset($argv[1])) {
82if (!empty($dolibarr_main_db_readonly)) {
83 print
"Error: instance in read-onyl mode\n";
92$fileprefix =
'mergedpdf';
98$paymentdateafter =
'';
99$paymentdatebefore =
'';
101$thirdpartiesid = array();
103foreach ($argv as $key => $value) {
107 if (preg_match(
'/^lang=/i', $value)) {
109 $valarray = explode(
'=', $value);
110 $newlangid = $valarray[1];
111 print
'Use language '.$newlangid.
".\n";
113 if (preg_match(
'/^prefix=/i', $value)) {
115 $valarray = explode(
'=', $value);
116 $fileprefix = $valarray[1];
117 print
'Use prefix for filename '.$fileprefix.
".\n";
120 if (preg_match(
'/^mode=/i', $value)) {
122 $valarray = explode(
'=', $value);
123 $mode = $valarray[1];
124 print
'Use mode '.$fileprefix.
".\n";
128 if (preg_match(
'/^regenerate=(.*)/i', $value, $reg)) {
129 if (!in_array($reg[1], array(
'',
'0',
'no'))) {
131 $regenerate = $reg[1];
132 print
'Regeneration of PDF is requested with template '.$regenerate.
"\n";
135 if (preg_match(
'/^regeneratenomerge=(.*)/i', $value, $reg)) {
136 if (!in_array($reg[1], array(
'',
'0',
'no'))) {
138 $regenerate = $reg[1];
140 print
'Regeneration (without merge) of PDF is requested with template '.$regenerate.
"\n";
144 if ($value ==
'filter=all') {
146 $option .= (empty($option) ?
'' :
'_').
'all';
149 print
'Rebuild PDF for all invoices'.
"\n";
152 if ($value ==
'filter=date') {
154 $option .= (empty($option) ?
'' :
'_').
'date_'.$argv[$key + 1].
'_'.$argv[$key + 2];
159 print
'Rebuild PDF for documents validated between '.dol_print_date($dateafterdate,
'day',
'gmt').
" and ".
dol_print_date($datebeforedate,
'day',
'gmt').
".\n";
162 if ($value ==
'filter=payments') {
164 $option .= (empty($option) ?
'' :
'_').
'payments_'.$argv[$key + 1].
'_'.$argv[$key + 2];
165 $filter[] =
'payments';
169 if (empty($paymentdateafter) || empty($paymentdatebefore)) {
170 print
'Error: Bad date format or value'.
"\n";
173 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";
176 if ($value ==
'filter=nopayment') {
178 $option .= (empty($option) ?
'' :
'_').
'nopayment';
179 $filter[] =
'nopayment';
181 print
'Rebuild PDF for ivoices with no payment done yet.'.
"\n";
184 if ($value ==
'filter=bank') {
186 $option .= (empty($option) ?
'' :
'_').
'bank_'.$argv[$key + 1];
189 $paymentonbankref = $argv[$key + 1];
190 $bankaccount =
new Account($db);
191 $result = $bankaccount->fetch(0, $paymentonbankref);
193 print
'Error: Bank account with ref "'.$paymentonbankref.
'" not found'.
"\n";
196 $paymentonbankid = $bankaccount->id;
197 print
'Rebuild PDF for invoices with at least one payment on financial account '.$bankaccount->ref.
"\n";
200 if ($value ==
'filter=nodeposit') {
202 $option .= (empty($option) ?
'' :
'_').
'nodeposit';
203 $filter[] =
'nodeposit';
205 print
'Exclude deposit invoices'.
"\n";
207 if ($value ==
'filter=noreplacement') {
209 $option .= (empty($option) ?
'' :
'_').
'noreplacement';
210 $filter[] =
'noreplacement';
212 print
'Exclude replacement invoices'.
"\n";
214 if ($value ==
'filter=nocreditnote') {
216 $option .= (empty($option) ?
'' :
'_').
'nocreditnote';
217 $filter[] =
'nocreditnote';
219 print
'Exclude credit note invoices'.
"\n";
222 if ($value ==
'filter=excludethirdparties') {
224 $filter[] =
'excludethirdparties';
226 $thirdpartiesid = explode(
',', $argv[$key + 1]);
227 print
'Exclude thirdparties with id in list ('.implode(
',', $thirdpartiesid).
").\n";
229 $option .= (empty($option) ?
'' :
'_').
'excludethirdparties'.implode(
'-', $thirdpartiesid);
231 if ($value ==
'filter=onlythirdparties') {
233 $filter[] =
'onlythirdparties';
235 $thirdpartiesid = explode(
',', $argv[$key + 1]);
236 print
'Only thirdparties with id in list ('.implode(
',', $thirdpartiesid).
").\n";
238 $option .= (empty($option) ?
'' :
'_').
'onlythirdparty'.implode(
'-', $thirdpartiesid);
241 if (!$found && preg_match(
'/filter=/i', $value)) {
246 $thirdpartiesid = array_map(
'intval', $thirdpartiesid);
247 '@phan-var-force int[] $thirdpartiesid';
251if (empty($option) && count($filter) <= 0) {
256if (in_array(
'payments', $filter) && in_array(
'nopayment', $filter)) {
260if (in_array(
'bank', $filter) && in_array(
'nopayment', $filter)) {
265$diroutputpdf =
'auto';
269$result =
rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, 1, $regenerate, $option, (
string) $paymentonbankid, $thirdpartiesid, $fileprefix, $nomerge, $mode);
275 print
'--- end ok'.
"\n";
278 print
'--- end error code='.$error.
"\n";
294 print
"Rebuild PDF files for some invoices/orders/proposals and/or merge PDF files into one.\n";
296 print
"To build/merge PDF for all documents, use filter=all\n";
297 print
"Usage: ".$script_file.
" mode=invoice filter=all\n";
298 print
"To build/merge PDF for documents in a date range:\n";
299 print
"Usage: ".$script_file.
" mode=invoice filter=date dateafter datebefore\n";
300 print
"To build/merge PDF for invoices with at least one payment in a date range:\n";
301 print
"Usage: ".$script_file.
" mode=invoice filter=payments dateafter datebefore\n";
302 print
"To build/merge PDF for invoices with at least one payment onto a bank account:\n";
303 print
"Usage: ".$script_file.
" mode=invoice filter=bank bankref\n";
304 print
"To build/merge PDF for invoices with no payments, use filter=nopayment\n";
305 print
"Usage: ".$script_file.
" mode=invoice filter=nopayment\n";
306 print
"To exclude credit notes, use filter=nocreditnote\n";
307 print
"To exclude replacement invoices, use filter=noreplacement\n";
308 print
"To exclude deposit invoices, use filter=nodeposit\n";
309 print
"To exclude some thirdparties, use filter=excludethirdparties id1,id2...\n";
310 print
"To limit to some thirdparties, use filter=onlythirdparties id1,id2...\n";
312 print
"To regenerate existing PDF before merge, use regenerate=templatename\n";
313 print
"To regenerate existing PDF with no merge (regenerate only), use regeneratenomerge=templatename\n";
314 print
"To merge by reusing existing PDF (generation only for non already existing PDF), use regenerate=no or nothing\n";
316 print
"To generate documents in a given language, use lang=xx_XX\n";
317 print
"To set prefix of generated file name, use prefix=myfileprefix\n";
319 print
"Example: ".$script_file.
" mode=invoice filter=payments 20080101 20081231 lang=fr_FR regenerate=crabe\n";
320 print
"Example: ".$script_file.
" mode=invoice filter=all lang=en_US\n";
322 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_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
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.
rebuild_merge_pdf_usage()
Show usage of script.