Prepare database to follow supplier discounts
This commit is contained in:
parent
4804cc47bd
commit
1ef899dacc
@ -335,23 +335,25 @@ class DiscountAbsolute
|
||||
|
||||
|
||||
/**
|
||||
* Renvoie montant TTC des reductions/avoirs en cours disponibles pour une société, un user ou autre
|
||||
* Return amount (with tax) of discounts currently available for a company, user or other criteria
|
||||
*
|
||||
* @param Societe $company Object third party for filter
|
||||
* @param User $user Filtre sur un user auteur des remises
|
||||
* @param string $filter Filtre autre
|
||||
* @param int $maxvalue Filter on max value for discount
|
||||
* @param string $mode 'customer' = discounts the customer has, 'supplier' = discount i have at this supplier
|
||||
* @return int <0 if KO, amount otherwise
|
||||
*/
|
||||
function getAvailableDiscounts($company='', $user='',$filter='', $maxvalue=0)
|
||||
function getAvailableDiscounts($company='', $user='',$filter='', $maxvalue=0, $mode='customer')
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$sql = "SELECT SUM(rc.amount_ttc) as amount";
|
||||
// $sql = "SELECT rc.amount_ttc as amount";
|
||||
//$sql = "SELECT rc.amount_ttc as amount";
|
||||
$sql.= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
|
||||
$sql.= " WHERE rc.entity = " . $conf->entity;
|
||||
$sql.= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available
|
||||
if ($mode != 'supplier') $sql.= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available
|
||||
else $sql.= " AND (rc.fk_suppler_invoice IS NULL AND rc.fk_supplier_invoice IS NULL)"; // Available
|
||||
if (is_object($company)) $sql.= " AND rc.fk_soc = ".$company->id;
|
||||
if (is_object($user)) $sql.= " AND rc.fk_user = ".$user->id;
|
||||
if ($filter) $sql.=' AND ('.$filter.')';
|
||||
@ -377,18 +379,35 @@ class DiscountAbsolute
|
||||
* Return amount (with tax) of all deposits invoices used by invoice as a payment.
|
||||
* Should always be empty, except if option FACTURE_DEPOSITS_ARE_JUST_PAYMENTS is on (not recommended).
|
||||
*
|
||||
* @param Facture $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
|
||||
* @param CommonInvoice $invoice Object invoice (customer of supplier)
|
||||
* @param int $multicurrency Return multicurrency_amount instead of amount
|
||||
* @return int <0 if KO, Sum of credit notes and deposits amount otherwise
|
||||
*/
|
||||
function getSumDepositsUsed($invoice, $multicurrency=0)
|
||||
{
|
||||
$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, '.MAIN_DB_PREFIX.'facture as f';
|
||||
$sql.= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.$invoice->id;
|
||||
$sql.= ' AND f.type = 3';
|
||||
|
||||
dol_syslog(get_class($this)."::getSumDepositsUsed", 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, '.MAIN_DB_PREFIX.'facture as f';
|
||||
$sql.= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.$invoice->id;
|
||||
$sql.= ' AND f.type = 3';
|
||||
}
|
||||
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, '.MAIN_DB_PREFIX.'facture_fourn as f';
|
||||
$sql.= ' WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = '.$invoice->id;
|
||||
$sql.= ' AND f.type = 3';
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->error=get_class($this)."::getSumDepositsUsed was called with a bad object as a first parameter";
|
||||
dol_print_error($this->error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
$resql=$this->db->query($sql);
|
||||
if ($resql)
|
||||
{
|
||||
@ -398,6 +417,7 @@ class DiscountAbsolute
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->error = $this->db->lasterror();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@ -405,18 +425,35 @@ class DiscountAbsolute
|
||||
/**
|
||||
* Return amount (with tax) of all credit notes and deposits invoices used by invoice as a payment
|
||||
*
|
||||
* @param Facture $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
|
||||
* @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 getSumCreditNotesUsed($invoice, $multicurrency=0)
|
||||
{
|
||||
$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, '.MAIN_DB_PREFIX.'facture as f';
|
||||
$sql.= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.$invoice->id;
|
||||
$sql.= ' AND f.type = 2';
|
||||
|
||||
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, '.MAIN_DB_PREFIX.'facture as f';
|
||||
$sql.= ' WHERE rc.fk_facture_source=f.rowid AND rc.fk_facture = '.$invoice->id;
|
||||
$sql.= ' AND f.type = 2';
|
||||
}
|
||||
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, '.MAIN_DB_PREFIX.'facture_fourn as f';
|
||||
$sql.= ' WHERE rc.fk_invoice_supplier_source=f.rowid AND rc.fk_invoice_supplier = '.$invoice->id;
|
||||
$sql.= ' AND f.type = 2';
|
||||
}
|
||||
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)
|
||||
{
|
||||
@ -426,6 +463,7 @@ class DiscountAbsolute
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->error = $this->db->lasterror();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -100,3 +100,12 @@ CREATE TABLE llx_product_attribute_combination
|
||||
|
||||
ALTER TABLE llx_paiementfourn ADD COLUMN model_pdf varchar(255);
|
||||
|
||||
|
||||
ALTER TABLE llx_societe_remise_except ADD COLUMN fk_invoice_supplier_line integer;
|
||||
ALTER TABLE llx_societe_remise_except ADD COLUMN fk_invoice_supplier integer;
|
||||
ALTER TABLE llx_societe_remise_except ADD COLUMN fk_invoice_supplier_source integer;
|
||||
|
||||
ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_soc_remise_fk_invoice_supplier_line FOREIGN KEY (fk_invoice_supplier_line) REFERENCES llx_facture_fourn_det (rowid);
|
||||
ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_societe_remise_fk_invoice_supplier FOREIGN KEY (fk_invoice_supplier) REFERENCES llx_facture_fourn (rowid);
|
||||
ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_societe_remise_fk_invoice_supplier_source FOREIGN KEY (fk_invoice_supplier) REFERENCES llx_facture_fourn (rowid);
|
||||
|
||||
|
||||
@ -30,9 +30,13 @@ ALTER TABLE llx_societe_remise_except ADD INDEX idx_societe_remise_except_fk_fac
|
||||
|
||||
ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_societe_remise_fk_user FOREIGN KEY (fk_user) REFERENCES llx_user (rowid);
|
||||
ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_soc_remise_fk_soc FOREIGN KEY (fk_soc) REFERENCES llx_societe (rowid);
|
||||
ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_soc_remise_fk_facture_line FOREIGN KEY (fk_facture_line) REFERENCES llx_facturedet (rowid);
|
||||
|
||||
ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_soc_remise_fk_facture_line FOREIGN KEY (fk_facture_line) REFERENCES llx_facturedet (rowid);
|
||||
ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_societe_remise_fk_facture FOREIGN KEY (fk_facture) REFERENCES llx_facture (rowid);
|
||||
ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_societe_remise_fk_facture_source FOREIGN KEY (fk_facture_source) REFERENCES llx_facture (rowid);
|
||||
|
||||
ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_soc_remise_fk_invoice_supplier_line FOREIGN KEY (fk_invoice_supplier_line) REFERENCES llx_facture_fourn_det (rowid);
|
||||
ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_societe_remise_fk_invoice_supplier FOREIGN KEY (fk_invoice_supplier) REFERENCES llx_facture_fourn (rowid);
|
||||
ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_societe_remise_fk_invoice_supplier_source FOREIGN KEY (fk_invoice_supplier) REFERENCES llx_facture_fourn (rowid);
|
||||
|
||||
|
||||
|
||||
@ -33,6 +33,9 @@ create table llx_societe_remise_except
|
||||
fk_facture_line integer,
|
||||
fk_facture integer,
|
||||
fk_facture_source integer,
|
||||
fk_invoice_supplier_line integer,
|
||||
fk_invoice_supplier integer,
|
||||
fk_invoice_supplier_source integer,
|
||||
description text NOT NULL,
|
||||
multicurrency_amount_ht double(24,8) DEFAULT 0 NOT NULL,
|
||||
multicurrency_amount_tva double(24,8) DEFAULT 0 NOT NULL,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user