diff --git a/htdocs/core/lib/invoice2.lib.php b/htdocs/core/lib/invoice2.lib.php index c4d4d34f727..9ee10a71cd7 100644 --- a/htdocs/core/lib/invoice2.lib.php +++ b/htdocs/core/lib/invoice2.lib.php @@ -48,12 +48,30 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php'; * @param int[] $thirdpartiesid List of thirdparties id when using filter=excludethirdpartiesid or filter=onlythirdpartiesid * @param string $fileprefix Prefix to add into filename of generated PDF * @param int $donotmerge 0=Default, 1=Disable the merge so do only the regeneration of PDFs. + * @param string $mode 'invoice' for invoices, 'proposal' for proposal, ... * @return int Error code */ -function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, $usestdout, $regenerate = '', $filesuffix = '', $paymentbankid = '', $thirdpartiesid = [], $fileprefix = 'mergedpdf', $donotmerge = 0) +function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, $usestdout, $regenerate = '', $filesuffix = '', $paymentbankid = '', $thirdpartiesid = [], $fileprefix = 'mergedpdf', $donotmerge = 0, $mode = 'invoice') { + if ($mode == 'invoice') { + $table = "facture"; + $dir_output = $conf->facture->dir_output; + $date = "datef"; + } elseif ($mode == 'order') { + $table = "commande"; + $dir_output = $conf->commande->dir_output; + $date = "date"; + } elseif ($mode == 'proposal') { + $table = "propal"; + $dir_output = $conf->propal->dir_output; + $date = "date"; + } else { + print "Bad value for mode"; + return -1; + } + $sql = "SELECT DISTINCT f.rowid, f.ref"; - $sql .= " FROM ".MAIN_DB_PREFIX."facture as f"; + $sql .= " FROM ".MAIN_DB_PREFIX.$table." as f"; $sqlwhere = ''; $sqlorder = ''; if (in_array('all', $filter)) { @@ -66,10 +84,11 @@ function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filte $sqlwhere .= " AND"; } $sqlwhere .= " f.fk_statut > 0"; - $sqlwhere .= " AND f.datef >= '".$db->idate($dateafterdate)."'"; - $sqlwhere .= " AND f.datef <= '".$db->idate($datebeforedate)."'"; + $sqlwhere .= " AND f.".$db->sanitize($date)." >= '".$db->idate($dateafterdate)."'"; + $sqlwhere .= " AND f.".$db->sanitize($date)." <= '".$db->idate($datebeforedate)."'"; $sqlorder = " ORDER BY f.datef ASC"; } + // Filter for invoices only if (in_array('nopayment', $filter)) { $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture"; if (empty($sqlwhere)) { @@ -80,6 +99,7 @@ function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filte $sqlwhere .= " f.fk_statut > 0"; $sqlwhere .= " AND pf.fk_paiement IS NULL"; } + // Filter for invoices only if (in_array('payments', $filter) || in_array('bank', $filter)) { $sql .= ", ".MAIN_DB_PREFIX."paiement_facture as pf, ".MAIN_DB_PREFIX."paiement as p"; if (in_array('bank', $filter)) { @@ -103,6 +123,7 @@ function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filte } $sqlorder = " ORDER BY p.datep ASC"; } + // Filter for invoices only if (in_array('nodeposit', $filter)) { if (empty($sqlwhere)) { $sqlwhere = ' WHERE '; @@ -111,6 +132,7 @@ function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filte } $sqlwhere .= ' type <> 3'; } + // Filter for invoices only if (in_array('noreplacement', $filter)) { if (empty($sqlwhere)) { $sqlwhere = ' WHERE '; @@ -119,6 +141,7 @@ function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filte } $sqlwhere .= ' type <> 1'; } + // Filter for invoices only if (in_array('nocreditnote', $filter)) { if (empty($sqlwhere)) { $sqlwhere = ' WHERE '; @@ -168,9 +191,6 @@ function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filte if ($resql = $db->query($sql)) { $num = $db->num_rows($resql); $cpt = 0; - $oldemail = ''; - $message = ''; - $total = ''; if ($num) { // First loop on each resultset to build PDF @@ -179,7 +199,14 @@ function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filte while ($cpt < $num) { $obj = $db->fetch_object($resql); - $fac = new Facture($db); + if ($mode == 'invoice') { + $fac = new Facture($db); + } elseif ($mode == 'order') { + $fac = new Commande($db); + } elseif ($mode == 'proposal') { + $fac = new Propal($db); + } + $result = $fac->fetch($obj->rowid); if ($result > 0) { $outputlangs = $langs; @@ -189,7 +216,7 @@ function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filte $outputlangs->setDefaultLang($newlangid); } } - $filename = $conf->facture->dir_output.'/'.$fac->ref.'/'.$fac->ref.'.pdf'; + $filename = $dir_output.'/'.$fac->ref.'/'.$fac->ref.'.pdf'; if ($regenerate || !dol_is_file($filename)) { if ($usestdout) { print "Build PDF for invoice ".$obj->ref." - Lang = ".$outputlangs->defaultlang."\n"; diff --git a/scripts/invoices/rebuild_merge_pdf.php b/scripts/invoices/rebuild_merge_pdf.php index d7ee2915281..d493ddb8aad 100755 --- a/scripts/invoices/rebuild_merge_pdf.php +++ b/scripts/invoices/rebuild_merge_pdf.php @@ -83,12 +83,15 @@ if (!empty($dolibarr_main_db_readonly)) { exit(1); } + $diroutputpdf = $conf->invoice->dir_output.'/temp'; + $newlangid = 'en_EN'; // To force a new lang id $filter = array(); $regenerate = ''; // Ask regenerate (contains name of model to use) $option = ''; $fileprefix = 'mergedpdf'; +$nomerge = 0; $dateafterdate = ''; $datebeforedate = ''; $paymentdateafter = ''; @@ -113,6 +116,7 @@ foreach ($argv as $key => $value) { print 'Use prefix for filename '.$fileprefix.".\n"; } + $reg = array(); if (preg_match('/^regenerate=(.*)/i', $value, $reg)) { if (!in_array($reg[1], array('', '0', 'no'))) { $found = true; @@ -120,6 +124,14 @@ foreach ($argv as $key => $value) { print 'Regeneration of PDF is requested with template '.$regenerate."\n"; } } + if (preg_match('/^regeneratenomerge=(.*)/i', $value, $reg)) { + if (!in_array($reg[1], array('', '0', 'no'))) { + $found = true; + $regenerate = $reg[1]; + $nomerge = 1; + print 'Regeneration (without merge) of PDF is requested with template '.$regenerate."\n"; + } + } if ($value == 'filter=all') { $found = true; @@ -136,7 +148,7 @@ foreach ($argv as $key => $value) { $dateafterdate = dol_stringtotime($argv[$key + 1]); $datebeforedate = dol_stringtotime($argv[$key + 2]); - print 'Rebuild PDF for invoices validated between '.dol_print_date($dateafterdate, 'day', 'gmt')." and ".dol_print_date($datebeforedate, 'day', 'gmt').".\n"; + print 'Rebuild PDF for documents validated between '.dol_print_date($dateafterdate, 'day', 'gmt')." and ".dol_print_date($datebeforedate, 'day', 'gmt').".\n"; } if ($value == 'filter=payments') { @@ -150,7 +162,7 @@ foreach ($argv as $key => $value) { print 'Error: Bad date format or value'."\n"; exit(1); } - 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"; + 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"; } if ($value == 'filter=nopayment') { @@ -158,7 +170,7 @@ foreach ($argv as $key => $value) { $option .= (empty($option) ? '' : '_').'nopayment'; $filter[] = 'nopayment'; - print 'Rebuild PDF for invoices with no payment done yet.'."\n"; + print 'Rebuild PDF for ivoices with no payment done yet.'."\n"; } if ($value == 'filter=bank') { @@ -244,7 +256,8 @@ if (in_array('bank', $filter) && in_array('nopayment', $filter)) { // Define SQL and SQL request to select invoices // Use $filter, $dateafterdate, datebeforedate, $paymentdateafter, $paymentdatebefore -$result = rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, 1, $regenerate, $option, (string) $paymentonbankid, $thirdpartiesid, $fileprefix); + +$result = rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, 1, $regenerate, $option, (string) $paymentonbankid, $thirdpartiesid, $fileprefix, $nomerge); // -------------------- END OF YOUR CODE -------------------- @@ -269,7 +282,7 @@ function rebuild_merge_pdf_usage() { global $script_file; - print "Rebuild PDF files for some invoices and merge PDF files into one.\n"; + print "Rebuild PDF files for some invoices and/or merge PDF files into one.\n"; print "\n"; print "To build/merge PDF for invoices in a date range:\n"; print "Usage: ".$script_file." filter=date dateafter datebefore\n"; @@ -286,7 +299,7 @@ function rebuild_merge_pdf_usage() print "To exclude deposit invoices, use filter=nodeposit\n"; print "To exclude some thirdparties, use filter=excludethirdparties id1,id2...\n"; print "To limit to some thirdparties, use filter=onlythirdparties id1,id2...\n"; - print "To regenerate existing PDF, use regenerate=templatename\n"; + print "To regenerate existing PDF, use regenerate=templatename or regeneratenomerge=templatename\n"; print "To generate invoices in a language, use lang=xx_XX\n"; print "To set prefix of generated file name, use prefix=myfileprefix\n"; print "\n";