Skip to content

Commit

Permalink
Fix nomerge mode
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Dec 20, 2024
1 parent 07a2c1f commit 3fc800b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
45 changes: 36 additions & 9 deletions htdocs/core/lib/invoice2.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,30 @@
* @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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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 ';
Expand All @@ -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 ';
Expand All @@ -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 ';
Expand Down Expand Up @@ -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
Expand All @@ -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);

Check warning on line 210 in htdocs/core/lib/invoice2.lib.php

View workflow job for this annotation

GitHub Actions / phan / Run phan

invoice2.lib.php: PhanPossiblyUndeclaredVariable: Variable $fac is possibly undeclared
if ($result > 0) {
$outputlangs = $langs;
Expand All @@ -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';

Check warning on line 219 in htdocs/core/lib/invoice2.lib.php

View workflow job for this annotation

GitHub Actions / phan / Run phan

invoice2.lib.php: PhanPossiblyUndeclaredVariable: Variable $fac is possibly undeclared
if ($regenerate || !dol_is_file($filename)) {
if ($usestdout) {
print "Build PDF for invoice ".$obj->ref." - Lang = ".$outputlangs->defaultlang."\n";
Expand Down
25 changes: 19 additions & 6 deletions scripts/invoices/rebuild_merge_pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,15 @@
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 = '';
Expand All @@ -113,13 +116,22 @@
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;
$regenerate = $reg[1];
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;
Expand All @@ -136,7 +148,7 @@

$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') {
Expand All @@ -150,15 +162,15 @@
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') {
$found = true;
$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') {
Expand Down Expand Up @@ -244,7 +256,8 @@

// 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 --------------------

Expand All @@ -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";
Expand All @@ -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";
Expand Down

0 comments on commit 3fc800b

Please sign in to comment.