27if (!defined(
'NOSESSION')) {
28 define(
'NOSESSION',
'1');
31$sapi_type = php_sapi_name();
32$script_file = basename(__FILE__);
36if (substr($sapi_type, 0, 3) ==
'cgi') {
37 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
42require_once $path.
"../../htdocs/master.inc.php";
44require_once DOL_DOCUMENT_ROOT.
"/compta/facture/class/facture.class.php";
45require_once DOL_DOCUMENT_ROOT.
"/core/modules/facture/modules_facture.php";
46require_once DOL_DOCUMENT_ROOT.
"/core/lib/date.lib.php";
47require_once DOL_DOCUMENT_ROOT.
'/core/lib/invoice2.lib.php';
53$version = DOL_VERSION;
56$hookmanager->initHooks(array(
'cli'));
64print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
65dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
68if (!isset($argv[1])) {
73if (!empty($dolibarr_main_db_readonly)) {
74 print
"Error: instance in read-onyl mode\n";
78$diroutputpdf = $conf->facture->dir_output.
'/temp';
83$fileprefix =
'mergedpdf';
86$paymentdateafter =
'';
87$paymentdatebefore =
'';
91foreach ($argv as $key => $value) {
95 if (preg_match(
'/^lang=/i', $value)) {
97 $valarray = explode(
'=', $value);
98 $newlangid = $valarray[1];
99 print
'Use language '.$newlangid.
".\n";
101 if (preg_match(
'/^prefix=/i', $value)) {
103 $valarray = explode(
'=', $value);
104 $fileprefix = $valarray[1];
105 print
'Use prefix for filename '.$fileprefix.
".\n";
108 if (preg_match(
'/^regenerate=(.*)/i', $value, $reg)) {
109 if (!in_array($reg[1], array(
'',
'0',
'no'))) {
111 $regenerate = $reg[1];
112 print
'Regeneration of PDF is requested with template '.$regenerate.
"\n";
116 if ($value ==
'filter=all') {
118 $option .= (empty($option) ?
'' :
'_').
'all';
121 print
'Rebuild PDF for all invoices'.
"\n";
124 if ($value ==
'filter=date') {
126 $option .= (empty($option) ?
'' :
'_').
'date_'.$argv[$key + 1].
'_'.$argv[$key + 2];
131 print
'Rebuild PDF for invoices validated between '.dol_print_date($dateafterdate,
'day',
'gmt').
" and ".
dol_print_date($datebeforedate,
'day',
'gmt').
".\n";
134 if ($value ==
'filter=payments') {
136 $option .= (empty($option) ?
'' :
'_').
'payments_'.$argv[$key + 1].
'_'.$argv[$key + 2];
137 $filter[] =
'payments';
141 if (empty($paymentdateafter) || empty($paymentdatebefore)) {
142 print
'Error: Bad date format or value'.
"\n";
145 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";
148 if ($value ==
'filter=nopayment') {
150 $option .= (empty($option) ?
'' :
'_').
'nopayment';
151 $filter[] =
'nopayment';
153 print
'Rebuild PDF for invoices with no payment done yet.'.
"\n";
156 if ($value ==
'filter=bank') {
158 $option .= (empty($option) ?
'' :
'_').
'bank_'.$argv[$key + 1];
161 $paymentonbankref = $argv[$key + 1];
162 $bankaccount =
new Account($db);
163 $result = $bankaccount->fetch(0, $paymentonbankref);
165 print
'Error: Bank account with ref "'.$paymentonbankref.
'" not found'.
"\n";
168 $paymentonbankid = $bankaccount->id;
169 print
'Rebuild PDF for invoices with at least one payment on financial account '.$bankaccount->ref.
"\n";
172 if ($value ==
'filter=nodeposit') {
174 $option .= (empty($option) ?
'' :
'_').
'nodeposit';
175 $filter[] =
'nodeposit';
177 print
'Exclude deposit invoices'.
"\n";
179 if ($value ==
'filter=noreplacement') {
181 $option .= (empty($option) ?
'' :
'_').
'noreplacement';
182 $filter[] =
'noreplacement';
184 print
'Exclude replacement invoices'.
"\n";
186 if ($value ==
'filter=nocreditnote') {
188 $option .= (empty($option) ?
'' :
'_').
'nocreditnote';
189 $filter[] =
'nocreditnote';
191 print
'Exclude credit note invoices'.
"\n";
194 if ($value ==
'filter=excludethirdparties') {
196 $filter[] =
'excludethirdparties';
198 $thirdpartiesid = explode(
',', $argv[$key + 1]);
199 print
'Exclude thirdparties with id in list ('.join(
',', $thirdpartiesid).
").\n";
201 $option .= (empty($option) ?
'' :
'_').
'excludethirdparties'.join(
'-', $thirdpartiesid);
203 if ($value ==
'filter=onlythirdparties') {
205 $filter[] =
'onlythirdparties';
207 $thirdpartiesid = explode(
',', $argv[$key + 1]);
208 print
'Only thirdparties with id in list ('.join(
',', $thirdpartiesid).
").\n";
210 $option .= (empty($option) ?
'' :
'_').
'onlythirdparty'.join(
'-', $thirdpartiesid);
213 if (!$found && preg_match(
'/filter=/i', $value)) {
220if (empty($option) && count($filter) <= 0) {
225if (in_array(
'payments', $filter) && in_array(
'nopayment', $filter)) {
229if (in_array(
'bank', $filter) && in_array(
'nopayment', $filter)) {
236$result =
rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, 1, $regenerate, $option, (
string) $paymentonbankid, $thirdpartiesid, $fileprefix);
242 print
'--- end ok'.
"\n";
245 print
'--- end error code='.$error.
"\n";
261 print
"Rebuild PDF files for some invoices and merge PDF files into one.\n";
263 print
"To build/merge PDF for invoices in a date range:\n";
264 print
"Usage: ".$script_file.
" filter=date dateafter datebefore\n";
265 print
"To build/merge PDF for invoices with at least one payment in a date range:\n";
266 print
"Usage: ".$script_file.
" filter=payments dateafter datebefore\n";
267 print
"To build/merge PDF for invoices with at least one payment onto a bank account:\n";
268 print
"Usage: ".$script_file.
" filter=bank bankref\n";
269 print
"To build/merge PDF for all invoices, use filter=all\n";
270 print
"Usage: ".$script_file.
" filter=all\n";
271 print
"To build/merge PDF for invoices with no payments, use filter=nopayment\n";
272 print
"Usage: ".$script_file.
" filter=nopayment\n";
273 print
"To exclude credit notes, use filter=nocreditnote\n";
274 print
"To exclude replacement invoices, use filter=noreplacement\n";
275 print
"To exclude deposit invoices, use filter=nodeposit\n";
276 print
"To exclude some thirdparties, use filter=excludethirdparties id1,id2...\n";
277 print
"To limit to some thirdparties, use filter=onlythirdparties id1,id2...\n";
278 print
"To regenerate existing PDF, use regenerate=templatename\n";
279 print
"To generate invoices in a language, use lang=xx_XX\n";
280 print
"To set prefix of generated file name, use prefix=myfileprefix\n";
282 print
"Example: ".$script_file.
" filter=payments 20080101 20081231 lang=fr_FR regenerate=crabe\n";
283 print
"Example: ".$script_file.
" filter=all lang=en_US\n";
285 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')
Function to build a compiled PDF.
rebuild_merge_pdf_usage()
Show usage of script.