Merge pull request #10655 from atm-quentin/FIX_credit_note_used_on_list

Fix total on list and amount opened on thirdparty card (for supplier)
This commit is contained in:
Laurent Destailleur 2019-08-15 21:14:03 +02:00 committed by GitHub
commit b8406f842d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 108 additions and 2 deletions

View File

@ -195,6 +195,29 @@ abstract class CommonInvoice extends CommonObject
}
}
/**
* Return amount (with tax) of all converted amount for this credit note
*
* @param int $multicurrency Return multicurrency_amount instead of amount
* @return int <0 if KO, Sum of credit notes and deposits amount otherwise
*/
function getSumFromThisCreditNotesNotUsed($multicurrency=0)
{
require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
$discountstatic=new DiscountAbsolute($this->db);
$result=$discountstatic->getSumFromThisCreditNotesNotUsed($this, $multicurrency);
if ($result >= 0)
{
return $result;
}
else
{
$this->error=$discountstatic->error;
return -1;
}
}
/**
* Renvoie tableau des ids de facture avoir issus de la facture
*

View File

@ -568,6 +568,49 @@ class DiscountAbsolute
return -1;
}
}
/**
* Return amount (with tax) of all converted amount for this credit note
*
* @param CommonInvoice $invoice Object invoice
* @param int $multicurrency Return multicurrency_amount instead of amount
* @return int <0 if KO, Sum of credit notes and deposits amount otherwise
*/
function getSumFromThisCreditNotesNotUsed($invoice, $multicurrency=0)
{
dol_syslog(get_class($this)."::getSumCreditNotesUsed", LOG_DEBUG);
if ($invoice->element == 'facture' || $invoice->element == 'invoice')
{
$sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount';
$sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc';
$sql.= ' WHERE rc.fk_facture IS NULL AND rc.fk_facture_source = '.$invoice->id;
}
else if ($invoice->element == 'invoice_supplier')
{
$sql = 'SELECT sum(rc.amount_ttc) as amount, sum(rc.multicurrency_amount_ttc) as multicurrency_amount';
$sql.= ' FROM '.MAIN_DB_PREFIX.'societe_remise_except as rc';
$sql.= ' WHERE rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_source = '.$invoice->id;
}
else
{
$this->error=get_class($this)."::getSumCreditNotesUsed was called with a bad object as a first parameter";
dol_print_error($this->error);
return -1;
}
$resql=$this->db->query($sql);
if ($resql)
{
$obj = $this->db->fetch_object($resql);
if ($multicurrency) return $obj->multicurrency_amount;
else return $obj->amount;
}
else
{
$this->error = $this->db->lasterror();
return -1;
}
}
/**
* Return clickable ref of object (with picto or not)

View File

@ -2581,6 +2581,27 @@ class FactureFournisseur extends CommonInvoice
return ($this->statut == self::STATUS_VALIDATED) && ($this->date_echeance < ($now - $conf->facture->fournisseur->warning_delay));
}
/**
* Is credit note used
*
* @return bool
*/
public function isCreditNoteUsed()
{
global $db;
$isUsed = false;
$sql = "SELECT fk_invoice_supplier FROM ".MAIN_DB_PREFIX."societe_remise_except WHERE fk_invoice_supplier_source=".$this->id;
$resql = $db->query($sql);
if(!empty($resql)){
$obj = $db->fetch_object($resql);
if(!empty($obj->fk_invoice_supplier))$isUsed=true;
}
return $isUsed;
}
}

View File

@ -833,6 +833,7 @@ if ($resql)
$facturestatic->date_echeance = $db->jdate($obj->datelimite);
$facturestatic->statut = $obj->fk_statut;
$thirdparty->id=$obj->socid;
$thirdparty->name=$obj->name;
$thirdparty->client=$obj->client;
@ -850,6 +851,14 @@ if ($resql)
$totalpay = $paiement + $totalcreditnotes + $totaldeposits;
$remaintopay = $obj->total_ttc - $totalpay;
//If invoice has been converted and the conversion has been used, we dont have remain to pay on invoice
if($facturestatic->type == FactureFournisseur::TYPE_CREDIT_NOTE) {
if($facturestatic->isCreditNoteUsed()){
$remaintopay=-$facturestatic->getSumFromThisCreditNotesNotUsed();
}
}
print '<tr class="oddeven">';
if (! empty($arrayfields['f.ref']['checked']))
{

View File

@ -3727,7 +3727,7 @@ class Societe extends CommonObject
$alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT');
$remaintopay=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT');
*/
if ($mode == 'supplier') $sql = "SELECT rowid, total_ht as total_ht, total_ttc, paye, fk_statut, close_code FROM ".MAIN_DB_PREFIX.$table." as f";
if ($mode == 'supplier') $sql = "SELECT rowid, total_ht as total_ht, total_ttc, paye, type, fk_statut, close_code FROM ".MAIN_DB_PREFIX.$table." as f";
else $sql = "SELECT rowid, total as total_ht, total_ttc, paye, fk_statut, close_code FROM ".MAIN_DB_PREFIX.$table." as f";
$sql .= " WHERE fk_soc = ". $this->id;
if ($mode == 'supplier') {
@ -3754,7 +3754,11 @@ class Societe extends CommonObject
$tmpobject=new Facture($this->db);
}
while($obj=$this->db->fetch_object($resql)) {
$tmpobject->id=$obj->rowid;
$tmpobject->id=$obj->rowid;
if ($obj->fk_statut != 0 // Not a draft
&& ! ($obj->fk_statut == 3 && $obj->close_code == 'replaced') // Not a replaced invoice
)
@ -3771,8 +3775,14 @@ class Societe extends CommonObject
$paiement = $tmpobject->getSommePaiement();
$creditnotes = $tmpobject->getSumCreditNotesUsed();
$deposits = $tmpobject->getSumDepositsUsed();
$outstandingOpened+=$obj->total_ttc - $paiement - $creditnotes - $deposits;
}
//if credit note is converted but not used
if($mode == 'supplier' && $obj->type == FactureFournisseur::TYPE_CREDIT_NOTE && $tmpobject->isCreditNoteUsed())$outstandingOpened-=$tmpobject->getSumFromThisCreditNotesNotUsed();
}
return array('opened'=>$outstandingOpened, 'total_ht'=>$outstandingTotal, 'total_ttc'=>$outstandingTotalIncTax); // 'opened' is 'incl taxes'
}