Removed classes instantiations and fetch to avoid multiple DB queries per invoice

This commit is contained in:
David Pareja Rodriguez 2023-08-11 14:05:56 +02:00 committed by Laurent Destailleur
parent 5c7a042413
commit ad140ea4bc

View file

@ -895,20 +895,17 @@ class BonPrelevement extends CommonObject
$sql .= ", pfd.code_banque, pfd.code_guichet, pfd.number, pfd.cle_rib";
$sql .= ", pfd.amount";
$sql .= ", s.nom as name";
$sql .= ", f.ref, sr.bic, sr.iban_prefix, sr.frstrecur";
if ($type != 'bank-transfer') {
$sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "prelevement_facture_demande as pfd ON f.rowid = pfd.fk_facture";
} else {
$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "prelevement_facture_demande as pfd ON f.rowid = pfd.fk_facture_fourn";
}
$sql .= ", ".MAIN_DB_PREFIX."societe as s";
$sql .= ", ".MAIN_DB_PREFIX."prelevement_demande as pfd";
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as s ON s.rowid = f.fk_soc";
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_rib as sr ON s.rowid = sr.fk_soc AND sr.default_rib = 1";
$sql .= " WHERE f.entity IN (".getEntity('invoice').')';
if ($type != 'bank-transfer') {
$sql .= " AND f.rowid = pfd.fk_facture";
} else {
$sql .= " AND f.rowid = pfd.fk_facture_fourn";
}
$sql .= " AND s.rowid = f.fk_soc";
$sql .= " AND f.fk_statut = 1"; // Invoice validated
$sql .= " AND f.paye = 0";
$sql .= " AND pfd.traite = 0";
@ -953,6 +950,7 @@ class BonPrelevement extends CommonObject
if (count($factures) > 0) {
foreach ($factures as $key => $fac) {
/*
if ($type != 'bank-transfer') {
$tmpinvoice = new Facture($this->db);
} else {
@ -960,36 +958,60 @@ class BonPrelevement extends CommonObject
}
$resfetch = $tmpinvoice->fetch($fac[0]);
if ($resfetch >= 0) { // Field 0 of $fac is rowid of invoice
if ($soc->fetch($tmpinvoice->socid) >= 0) {
$bac = new CompanyBankAccount($this->db);
$bac->fetch(0, $soc->id);
*/
// Check if $fac[8] s.nom is null
if ($fac[8] != null) {
//$bac = new CompanyBankAccount($this->db);
//$bac->fetch(0, $soc->id);
if ($type != 'bank-transfer') {
if ($format == 'FRST' && $bac->frstrecur != 'FRST') {
if ($format == 'FRST' && $fac[12] != 'FRST') {
continue;
}
if ($format == 'RCUR' && $bac->frstrecur != 'RCUR') {
if ($format == 'RCUR' && $fac[12] != 'RCUR') {
continue;
}
}
$verif = false;
require_once DOL_DOCUMENT_ROOT.'/includes/php-iban/oophp-iban.php';
if ($bac->verif() >= 1) {
$swift = $fac[10];
if (preg_match("/^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$/", $swift)) {
$verif = true;
} else {
$verif = false;
}
if ($verif) {
// if correct, also verify IBAN
$iban = new PHP_IBAN\IBAN($fac[11]);
$verif = $iban->Verify();
}
if ($verif) {
$factures_prev[$i] = $fac;
/* second array necessary for BonPrelevement */
$factures_prev_id[$i] = $fac[0];
$i++;
//dol_syslog(__METHOD__."::RIB is ok", LOG_DEBUG);
} else {
dol_syslog(__METHOD__." Check BAN Error on default bank number IBAN/BIC for thirdparty reported by verif() ".$tmpinvoice->socid." ".$soc->name, LOG_WARNING);
$this->invoice_in_error[$fac[0]] = "Error on default bank number IBAN/BIC for invoice ".$tmpinvoice->getNomUrl(0)." for thirdparty ".$soc->getNomUrl(0);
$this->thirdparty_in_error[$soc->id] = "Error on default bank number IBAN/BIC for invoice ".$tmpinvoice->getNomUrl(0)." for thirdparty ".$soc->getNomUrl(0);
if ($type != 'bank-transfer') {
$invoice_url = "<a href='".DOL_URL_ROOT.'/compta/facture/card.php?facid='.$fac[0]."'>".$fac[9]."</a>";
} else {
$invoice_url = "<a href='".DOL_URL_ROOT.'/fourn/facture/card.php?facid='.$fac[0]."'>".$fac[9]."</a>";
}
dol_syslog(__METHOD__ . " Check BAN Error on default bank number IBAN/BIC for thirdparty reported by verif() " . $fac->fk_soc . " " . $soc->name, LOG_WARNING);
$this->invoice_in_error[$fac[0]] = "Error on default bank number IBAN/BIC for invoice " . $invoice_url . " for thirdparty " . $soc->getNomUrl(0);
$this->thirdparty_in_error[$soc->id] = "Error on default bank number IBAN/BIC for invoice " . $invoice_url . " for thirdparty " . $soc->getNomUrl(0);
}
} else {
dol_syslog(__METHOD__." Check BAN Failed to read company", LOG_WARNING);
dol_syslog(__METHOD__ . " Check BAN Failed to read company", LOG_WARNING);
}
/*
} else {
dol_syslog(__METHOD__." Check BAN Failed to read invoice", LOG_WARNING);
}
*/
}
} else {
dol_syslog(__METHOD__." Check BAN No invoice to process", LOG_WARNING);
@ -1099,21 +1121,25 @@ class BonPrelevement extends CommonObject
}
if (!$error) {
/*
if ($type != 'bank-transfer') {
$fact = new Facture($this->db);
} else {
$fact = new FactureFournisseur($this->db);
}
*/
// Add lines for the bon
if (count($factures_prev) > 0) {
foreach ($factures_prev as $fac) { // Add a link in database for each invoice
// Fetch invoice
/*
$result = $fact->fetch($fac[0]);
if ($result < 0) {
$this->error = 'ERRORBONPRELEVEMENT Failed to load invoice with id '.$fac[0];
break;
}
*/
/*
* Add standing order. This add record into llx_prelevement_lignes and llx_prelevement
@ -1127,6 +1153,10 @@ class BonPrelevement extends CommonObject
* $fac[6] : cle rib
* $fac[7] : amount
* $fac[8] : client nom
* $fac[9] : Invoice ref
* $fac[10] : BIC
* $fac[11] : IBAN
* $fac[12] : frstrcur
*/
$ri = $this->AddFacture($fac[0], $fac[2], $fac[8], $fac[7], $fac[3], $fac[4], $fac[5], $fac[6], $type);
if ($ri <> 0) {