NEW: supplier credit notes: convert to usable absolute discount + link/unlink to supplier invoice + create supplier credit note from excess paid

This commit is contained in:
Marc de Lima Lucio 2018-02-14 10:47:10 +01:00
parent 795cbaa978
commit 4f9bb33600
12 changed files with 519 additions and 70 deletions

View File

@ -2989,11 +2989,11 @@ else if ($id > 0 || ! empty($ref))
$resteapayeraffiche = $resteapayer;
if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) { // Never use this
$filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
$filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
$filterabsolutediscount = "fk_facture IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
$filtercreditnote = "fk_facture IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
} else {
$filterabsolutediscount = "fk_facture_source IS NULL OR (fk_facture_source IS NOT NULL AND (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%'))";
$filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
$filterabsolutediscount = "fk_facture IS NOT NULL AND (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
$filtercreditnote = "fk_facture IS NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
}
$absolute_discount = $soc->getAvailableDiscounts('', $filterabsolutediscount);

View File

@ -180,12 +180,6 @@ abstract class CommonInvoice extends CommonObject
*/
function getSumCreditNotesUsed($multicurrency=0)
{
if ($this->element == 'facture_fourn' || $this->element == 'invoice_supplier')
{
// TODO
return 0;
}
require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
$discountstatic=new DiscountAbsolute($this->db);

View File

@ -3955,7 +3955,7 @@ abstract class CommonObject
$discount=new DiscountAbsolute($this->db);
$discount->fetch($line->fk_remise_except);
$this->tpl['description'] = $langs->transnoentities("DiscountFromExcessReceived",$discount->getNomUrl(0));
}
} // TODO handle (EXCESS PAID)
else
{
$this->tpl['description'] = dol_trunc($line->desc,60);

View File

@ -60,16 +60,17 @@ class DiscountAbsolute
/**
* Load object from database into memory
*
* @param int $rowid id discount to load
* @param int $fk_facture_source fk_facture_source
* @return int <0 if KO, =0 if not found, >0 if OK
* @param int $rowid id discount to load
* @param int $fk_facture_source fk_facture_source
* @param int $fk_invoice_supplier_source fk_invoice_supplier_source
* @return int <0 if KO, =0 if not found, >0 if OK
*/
function fetch($rowid, $fk_facture_source=0)
function fetch($rowid, $fk_facture_source=0, $fk_invoice_supplier_source=0)
{
global $conf;
// Check parameters
if (! $rowid && ! $fk_facture_source)
if (! $rowid && ! $fk_facture_source && ! $fk_invoice_supplier_source)
{
$this->error='ErrorBadParameters';
return -1;
@ -79,14 +80,16 @@ class DiscountAbsolute
$sql.= " sr.fk_user,";
$sql.= " sr.amount_ht, sr.amount_tva, sr.amount_ttc, sr.tva_tx,";
$sql.= " sr.multicurrency_amount_ht, sr.multicurrency_amount_tva, sr.multicurrency_amount_ttc,";
$sql.= " sr.fk_facture_line, sr.fk_facture, sr.fk_facture_source, sr.description,";
$sql.= " sr.fk_facture_line, sr.fk_facture, sr.fk_facture_source, sr.fk_invoice_supplier_line, sr.fk_invoice_supplier, sr.fk_invoice_supplier_source, sr.description,";
$sql.= " sr.datec,";
$sql.= " f.facnumber as ref_facture_source";
$sql.= " f.facnumber as ref_facture_source, fsup.facnumber as ref_invoice_supplier_source";
$sql.= " FROM ".MAIN_DB_PREFIX."societe_remise_except as sr";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON sr.fk_facture_source = f.rowid";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fsup ON sr.fk_invoice_supplier_source = fsup.rowid";
$sql.= " WHERE sr.entity = " . $conf->entity;
if ($rowid) $sql.= " AND sr.rowid=".$rowid;
if ($fk_facture_source) $sql.= " AND sr.fk_facture_source=".$fk_facture_source;
if ($fk_invoice_supplier_source) $sql.= " AND sr.fk_invoice_supplier_source=".$fk_invoice_supplier_source;
dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
$resql = $this->db->query($sql);
@ -113,6 +116,10 @@ class DiscountAbsolute
$this->fk_facture = $obj->fk_facture;
$this->fk_facture_source = $obj->fk_facture_source; // Id avoir source
$this->ref_facture_source = $obj->ref_facture_source; // Ref avoir source
$this->fk_invoice_supplier_line = $obj->fk_invoice_supplier_line;
$this->fk_invoice_supplier = $obj->fk_invoice_supplier;
$this->fk_invoice_supplier_source = $obj->fk_invoice_supplier_source; // Id avoir source
$this->ref_invoice_supplier_source = $obj->ref_invoice_supplier_source; // Ref avoir source
$this->description = $obj->description;
$this->datec = $this->db->jdate($obj->datec);
@ -161,11 +168,12 @@ class DiscountAbsolute
$sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise_except";
$sql.= " (entity, datec, fk_soc, fk_user, description,";
$sql.= " amount_ht, amount_tva, amount_ttc, tva_tx,";
$sql.= " fk_facture_source";
$sql.= " fk_facture_source, fk_invoice_supplier_source";
$sql.= ")";
$sql.= " VALUES (".$conf->entity.", '".$this->db->idate($this->datec!=''?$this->datec:dol_now())."', ".$this->fk_soc.", ".$user->id.", '".$this->db->escape($this->description)."',";
$sql.= " ".$this->amount_ht.", ".$this->amount_tva.", ".$this->amount_ttc.", ".$this->tva_tx.",";
$sql.= " ".($this->fk_facture_source ? "'".$this->db->escape($this->fk_facture_source)."'":"null");
$sql.= " ".($this->fk_facture_source ? "'".$this->db->escape($this->fk_facture_source)."'":"null").",";
$sql.= " ".($this->fk_invoice_supplier_source ? "'".$this->db->escape($this->fk_invoice_supplier_source)."'":"null");
$sql.= ")";
dol_syslog(get_class($this)."::create", LOG_DEBUG);
@ -194,7 +202,7 @@ class DiscountAbsolute
global $conf, $langs;
// Check if we can remove the discount
if ($this->fk_facture_source)
if ($this->fk_facture_source) // TODO check
{
$sql="SELECT COUNT(rowid) as nb";
$sql.=" FROM ".MAIN_DB_PREFIX."societe_remise_except";
@ -278,9 +286,10 @@ class DiscountAbsolute
*
* @param int $rowidline Invoice line id (To use discount into invoice lines)
* @param int $rowidinvoice Invoice id (To use discount as a credit note to reduc payment of invoice)
* @param string $mode 'supplier' to link to supplier invoice, 'customer' instead
* @return int <0 if KO, >0 if OK
*/
function link_to_invoice($rowidline,$rowidinvoice)
function link_to_invoice($rowidline,$rowidinvoice,$mode='customer')
{
// Check parameters
if (! $rowidline && ! $rowidinvoice)
@ -295,16 +304,26 @@ class DiscountAbsolute
}
$sql ="UPDATE ".MAIN_DB_PREFIX."societe_remise_except";
if ($rowidline) $sql.=" SET fk_facture_line = ".$rowidline;
if ($rowidinvoice) $sql.=" SET fk_facture = ".$rowidinvoice;
if($mode == 'supplier') {
if ($rowidline) $sql.=" SET fk_invoice_supplier_line = ".$rowidline;
if ($rowidinvoice) $sql.=" SET fk_invoice_supplier = ".$rowidinvoice;
} else {
if ($rowidline) $sql.=" SET fk_facture_line = ".$rowidline;
if ($rowidinvoice) $sql.=" SET fk_facture = ".$rowidinvoice;
}
$sql.=" WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::link_to_invoice", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$this->fk_facture_line=$rowidline;
$this->fk_facture=$rowidinvoice;
if($mode == 'supplier') {
$this->fk_invoice_supplier_line=$rowidline;
$this->fk_invoice_supplier=$rowidinvoice;
} else {
$this->fk_facture_line=$rowidline;
$this->fk_facture=$rowidinvoice;
}
return 1;
}
else
@ -319,12 +338,17 @@ class DiscountAbsolute
* Link the discount to a particular invoice line or a particular invoice.
* Do not call this if discount is linked to a reconcialiated invoice
*
* @param string $mode 'supplier' to unlink a supplier invoice, 'customer' instead
* @return int <0 if KO, >0 if OK
*/
function unlink_invoice()
function unlink_invoice($mode = 'customer')
{
$sql ="UPDATE ".MAIN_DB_PREFIX."societe_remise_except";
$sql.=" SET fk_facture_line = NULL, fk_facture = NULL";
if($mode = 'supplier') {
$sql.=" SET fk_invoice_supplier_line = NULL, fk_invoice_supplier = NULL";
} else {
$sql.=" SET fk_facture_line = NULL, fk_facture = NULL";
}
$sql.=" WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::unlink_invoice", LOG_DEBUG);
@ -359,8 +383,8 @@ class DiscountAbsolute
//$sql = "SELECT rc.amount_ttc as amount";
$sql.= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc";
$sql.= " WHERE rc.entity = " . $conf->entity;
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 ($mode == 'supplier') $sql.= " AND (rc.fk_facture_source IS NULL AND rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; // Available
else $sql.= " AND (rc.fk_invoice_supplier_source IS NULL AND rc.fk_invoice_supplier IS NULL AND rc.fk_invoice_supplier_line 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.')';
@ -452,7 +476,7 @@ class DiscountAbsolute
$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 OR f.type = 0)'; // Find discount coming from credit note or excess received
$sql.= ' AND (f.type = 2 OR f.type = 0)'; // Find discount coming from credit note or excess paid
}
else
{

View File

@ -1236,6 +1236,7 @@ class Form
if (preg_match('/\(CREDIT_NOTE\)/', $desc)) $desc=preg_replace('/\(CREDIT_NOTE\)/', $langs->trans("CreditNote"), $desc);
if (preg_match('/\(DEPOSIT\)/', $desc)) $desc=preg_replace('/\(DEPOSIT\)/', $langs->trans("Deposit"), $desc);
if (preg_match('/\(EXCESS RECEIVED\)/', $desc)) $desc=preg_replace('/\(EXCESS RECEIVED\)/', $langs->trans("ExcessReceived"), $desc);
// TODO handle (EXCESS PAID)
$selectstring='';
if ($selected > 0 && $selected == $obj->rowid) $selectstring=' selected';
@ -4219,9 +4220,10 @@ class Form
* @param int $maxvalue Max value for lines that can be selected
* @param string $more More string to add
* @param int $hidelist 1=Hide list
* @param string $mode 'supplier' to list available discounts for suppliers, 'customer' instead
* @return void
*/
function form_remise_dispo($page, $selected, $htmlname, $socid, $amount, $filter='', $maxvalue=0, $more='', $hidelist=0)
function form_remise_dispo($page, $selected, $htmlname, $socid, $amount, $filter='', $maxvalue=0, $more='', $hidelist=0, $mode='customer')
{
global $conf,$langs;
if ($htmlname != "none")
@ -4230,28 +4232,51 @@ class Form
print '<input type="hidden" name="action" value="setabsolutediscount">';
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print '<div class="inline-block">';
if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))
{
if (! $filter || $filter=="fk_facture_source IS NULL") print $langs->trans("CompanyHasAbsoluteDiscount",price($amount,0,$langs,0,0,-1,$conf->currency)); // If we want deposit to be substracted to payments only and not to total of final invoice
else print $langs->trans("CompanyHasCreditNote",price($amount,0,$langs,0,0,-1,$conf->currency));
}
else
{
if (! $filter || $filter=="fk_facture_source IS NULL OR (fk_facture_source IS NOT NULL AND (description LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%'))") print $langs->trans("CompanyHasAbsoluteDiscount",price($amount,0,$langs,0,0,-1,$conf->currency));
else print $langs->trans("CompanyHasCreditNote",price($amount,0,$langs,0,0,-1,$conf->currency));
if($mode == 'supplier') {
if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))
{
if (! $filter || $filter=="fk_invoice_supplier IS NOT NULL") print $langs->trans("CompanyHasAbsoluteDiscount",price($amount,0,$langs,0,0,-1,$conf->currency)); // If we want deposit to be substracted to payments only and not to total of final invoice
else print $langs->trans("CompanyHasCreditNote",price($amount,0,$langs,0,0,-1,$conf->currency));
}
else
{
if (! $filter || $filter=="fk_invoice_supplier IS NOT NOT NULL AND (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS PAID)%')") print $langs->trans("CompanyHasAbsoluteDiscount",price($amount,0,$langs,0,0,-1,$conf->currency));
else print $langs->trans("CompanyHasCreditNote",price($amount,0,$langs,0,0,-1,$conf->currency));
}
} else {
if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))
{
if (! $filter || $filter=="fk_facture IS NOT NULL") print $langs->trans("CompanyHasAbsoluteDiscount",price($amount,0,$langs,0,0,-1,$conf->currency)); // If we want deposit to be substracted to payments only and not to total of final invoice
else print $langs->trans("CompanyHasCreditNote",price($amount,0,$langs,0,0,-1,$conf->currency));
}
else
{
if (! $filter || $filter=="fk_facture IS NOT NULL AND (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')") print $langs->trans("CompanyHasAbsoluteDiscount",price($amount,0,$langs,0,0,-1,$conf->currency));
else print $langs->trans("CompanyHasCreditNote",price($amount,0,$langs,0,0,-1,$conf->currency));
}
}
if (empty($hidelist)) print ': ';
print '</div>';
if (empty($hidelist))
{
print '<div class="inline-block" style="padding-right: 10px">';
$newfilter='fk_facture IS NULL AND fk_facture_line IS NULL'; // Remises disponibles
if($mode == 'supplier') {
$newfilter = 'fk_facture_source IS NULL AND fk_facture IS NULL AND fk_facture_line IS NULL'; // Exclude customer discounts
$newfilter.= ' AND fk_invoice_supplier IS NULL AND fk_invoice_supplier_line IS NULL'; // Supplier discounts available
} else {
$newfilter = 'fk_invoice_supplier_source IS NULL AND fk_invoice_supplier IS NULL AND fk_invoice_supplier_line IS NULL'; // Exclude supplier discounts
$newfilter.= ' AND fk_facture IS NULL AND fk_facture_line IS NULL'; // Customer discounts available
}
if ($filter) $newfilter.=' AND ('.$filter.')';
$nbqualifiedlines=$this->select_remises($selected,$htmlname,$newfilter,$socid,$maxvalue);
if ($nbqualifiedlines > 0)
{
print ' &nbsp; <input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("UseLine")).'"';
if ($filter && $filter != "fk_facture_source IS NULL OR (fk_facture_source IS NOT NULL AND description LIKE '(DEPOSIT)%')") print ' title="'.$langs->trans("UseCreditNoteInInvoicePayment").'"';
if($mode == 'supplier' && $filter && $filter != "fk_invoice_supplier IS NOT NOT NULL AND (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS PAID)%')")
print ' title="'.$langs->trans("UseCreditNoteInInvoicePayment").'"';
if($mode != 'supplier' && $filter && $filter != "fk_facture IS NOT NULL AND (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')")
print ' title="'.$langs->trans("UseCreditNoteInInvoicePayment").'"';
print '>';
}
print '</div>';

View File

@ -88,7 +88,7 @@ function doc_getlinedesc($line,$outputlangs,$hideref=0,$hidedesc=0,$issupplierli
$discount=new DiscountAbsolute($db);
$discount->fetch($line->fk_remise_except);
$libelleproduitservice=$outputlangs->transnoentitiesnoconv("DiscountFromExcessReceived",$discount->ref_facture_source);
}
} // TODO handle (EXCESS PAID)
else
{
if ($idprod)

View File

@ -1251,7 +1251,7 @@ function pdf_getlinedesc($object,$i,$outputlangs,$hideref=0,$hidedesc=0,$issuppl
$discount=new DiscountAbsolute($db);
$discount->fetch($object->lines[$i]->fk_remise_except);
$libelleproduitservice=$outputlangs->transnoentitiesnoconv("DiscountFromExcessReceived",$discount->ref_facture_source);
}
} // TODO handle (EXCESS PAID)
else
{
if ($idprod)

View File

@ -72,7 +72,7 @@ if (empty($outputalsopricetotalwithtax)) $outputalsopricetotalwithtax=0;
$txt='';
print img_object($langs->trans("ShowReduc"),'reduc').' ';
if ($line->description == '(DEPOSIT)') $txt=$langs->trans("Deposit");
elseif ($line->description == '(EXCESS RECEIVED)') $txt=$langs->trans("ExcessReceived");
elseif ($line->description == '(EXCESS RECEIVED)') $txt=$langs->trans("ExcessReceived"); // TODO handle (EXCESS PAID)
//else $txt=$langs->trans("Discount");
print $txt;
?>
@ -100,7 +100,7 @@ if (empty($outputalsopricetotalwithtax)) $outputalsopricetotalwithtax=0;
$discount=new DiscountAbsolute($this->db);
$discount->fetch($line->fk_remise_except);
echo ($txt?' - ':'').$langs->transnoentities("DiscountFromExcessReceived",$discount->getNomUrl(0));
}
} // TODO handle (EXCESS PAID)
else
{
echo ($txt?' - ':'').dol_htmlentitiesbr($line->description);

View File

@ -897,6 +897,106 @@ class FactureFournisseur extends CommonInvoice
}
}
/**
* Add a discount line into an invoice (as an invoice line) using an existing absolute discount (Consume the discount)
*
* @param int $idremise Id of absolute discount
* @return int >0 if OK, <0 if KO
*/
function insert_discount($idremise)
{
global $langs;
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
include_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
$this->db->begin();
$remise=new DiscountAbsolute($this->db);
$result=$remise->fetch($idremise);
if ($result > 0)
{
if ($remise->fk_invoice_supplier) // Protection against multiple submission
{
$this->error=$langs->trans("ErrorDiscountAlreadyUsed");
$this->db->rollback();
return -5;
}
$facligne=new SupplierInvoiceLine($this->db);
$facligne->fk_facture=$this->id;
$facligne->fk_remise_except=$remise->id;
$facligne->desc=$remise->description; // Description ligne
$facligne->vat_src_code=$remise->vat_src_code;
$facligne->tva_tx=$remise->tva_tx;
$facligne->subprice = -$remise->amount_ht;
$facligne->fk_product=0; // Id produit predefini
$facligne->qty=1;
$facligne->remise_percent=0;
$facligne->rang=-1;
$facligne->info_bits=2;
// Get buy/cost price of invoice that is source of discount
if ($remise->fk_invoice_supplier_source > 0)
{
$srcinvoice=new FactureFournisseur($this->db);
$srcinvoice->fetch($remise->fk_invoice_supplier_source);
$totalcostpriceofinvoice=0;
include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmargin.class.php'; // TODO Move this into commonobject
$formmargin=new FormMargin($this->db);
$arraytmp=$formmargin->getMarginInfosArray($srcinvoice, false);
$facligne->pa_ht = $arraytmp['pa_total'];
}
$facligne->total_ht = -$remise->amount_ht;
$facligne->total_tva = -$remise->amount_tva;
$facligne->total_ttc = -$remise->amount_ttc;
$facligne->multicurrency_subprice = -$remise->multicurrency_subprice;
$facligne->multicurrency_total_ht = -$remise->multicurrency_total_ht;
$facligne->multicurrency_total_tva = -$remise->multicurrency_total_tva;
$facligne->multicurrency_total_ttc = -$remise->multicurrency_total_ttc;
$lineid=$facligne->insert();
if ($lineid > 0)
{
$result=$this->update_price(1);
if ($result > 0)
{
// Create link between discount and invoice line
$result=$remise->link_to_invoice($lineid,0,'supplier');
if ($result < 0)
{
$this->error=$remise->error;
$this->db->rollback();
return -4;
}
$this->db->commit();
return 1;
}
else
{
$this->error=$facligne->error;
$this->db->rollback();
return -1;
}
}
else
{
$this->error=$facligne->error;
$this->db->rollback();
return -2;
}
}
else
{
$this->db->rollback();
return -3;
}
}
/**
* Delete invoice from database

View File

@ -36,6 +36,7 @@ require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/fourn.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
@ -265,6 +266,14 @@ if (empty($reshook))
$action='';
}
}
// Delete link of credit note to invoice
else if ($action == 'unlinkdiscount' && $user->rights->facture->creer)
{
$discount = new DiscountAbsolute($db);
$result = $discount->fetch(GETPOST("discountid"));
$discount->unlink_invoice('supplier');
}
elseif ($action == 'confirm_paid' && $confirm == 'yes' && $user->rights->fournisseur->facture->creer)
{
@ -376,6 +385,186 @@ if (empty($reshook))
$result=$object->update($user);
if ($result < 0) dol_print_error($db,$object->error);
}
elseif ($action == "setabsolutediscount" && $user->rights->facture->creer)
{
// POST[remise_id] or POST[remise_id_for_payment]
// We use the credit to reduce amount of invoice
if (! empty($_POST["remise_id"])) {
$ret = $object->fetch($id);
if ($ret > 0) {
$result = $object->insert_discount($_POST["remise_id"]);
if ($result < 0) {
setEventMessages($object->error, $object->errors, 'errors');
}
} else {
dol_print_error($db, $object->error);
}
}
// We use the credit to reduce remain to pay
if (! empty($_POST["remise_id_for_payment"]))
{
require_once DOL_DOCUMENT_ROOT . '/core/class/discount.class.php';
$discount = new DiscountAbsolute($db);
$discount->fetch($_POST["remise_id_for_payment"]);
//var_dump($object->getRemainToPay(0));
//var_dump($discount->amount_ttc);exit;
if ($discount->amount_ttc > $object->getRemainToPay(0))
{
// TODO Split the discount in 2 automatically
$error++;
setEventMessages($langs->trans("ErrorDiscountLargerThanRemainToPaySplitItBefore"), null, 'errors');
}
if (! $error)
{
$result = $discount->link_to_invoice(0, $id, 'supplier');
if ($result < 0) {
setEventMessages($discount->error, $discount->errors, 'errors');
}
}
}
if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
{
$outputlangs = $langs;
$newlang = '';
if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09');
if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
if (! empty($newlang)) {
$outputlangs = new Translate("", $conf);
$outputlangs->setDefaultLang($newlang);
}
$ret = $object->fetch($id); // Reload to get new records
$result = $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
if ($result < 0) setEventMessages($object->error, $object->errors, 'errors');
}
}
// Convertir en reduc
else if ($action == 'confirm_converttoreduc' && $confirm == 'yes' && $user->rights->facture->creer)
{
$object->fetch($id);
$object->fetch_thirdparty();
//$object->fetch_lines(); // Already done into fetch
// Check if there is already a discount (protection to avoid duplicate creation when resubmit post)
$discountcheck=new DiscountAbsolute($db);
$result=$discountcheck->fetch(0,0,$object->id);
$canconvert=0;
if ($object->type == FactureFournisseur::TYPE_DEPOSIT && empty($discountcheck->id)) $canconvert=1; // we can convert deposit into discount if deposit is payed (completely, partially or not at all) and not already converted (see real condition into condition used to show button converttoreduc)
if (($object->type == FactureFournisseur::TYPE_CREDIT_NOTE || $object->type == FactureFournisseur::TYPE_STANDARD) && $object->paye == 0 && empty($discountcheck->id)) $canconvert=1; // we can convert credit note into discount if credit note is not payed back and not already converted and amount of payment is 0 (see real condition into condition used to show button converttoreduc)
if ($canconvert)
{
$db->begin();
$amount_ht = $amount_tva = $amount_ttc = array();
// Loop on each vat rate
$i = 0;
foreach ($object->lines as $line)
{
if ($line->product_type < 9 && $line->total_ht != 0) // Remove lines with product_type greater than or equal to 9
{ // no need to create discount if amount is null
$amount_ht[$line->tva_tx] += $line->total_ht;
$amount_tva[$line->tva_tx] += $line->total_tva;
$amount_ttc[$line->tva_tx] += $line->total_ttc;
$i ++;
}
}
// Insert one discount by VAT rate category
$discount = new DiscountAbsolute($db);
if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE)
$discount->description = '(CREDIT_NOTE)';
elseif ($object->type == FactureFournisseur::TYPE_DEPOSIT)
$discount->description = '(DEPOSIT)';
elseif ($object->type == FactureFournisseur::TYPE_STANDARD || $object->type == FactureFournisseur::TYPE_REPLACEMENT || $object->type == FactureFournisseur::TYPE_SITUATION)
$discount->description = '(EXCESS PAID)';
else {
setEventMessages($langs->trans('CantConvertToReducAnInvoiceOfThisType'), null, 'errors');
}
$discount->fk_soc = $object->socid;
$discount->fk_invoice_supplier_source = $object->id;
$error = 0;
if ($object->type == FactureFournisseur::TYPE_STANDARD || $object->type == FactureFournisseur::TYPE_REPLACEMENT || $object->type == FactureFournisseur::TYPE_SITUATION)
{
// If we're on a standard invoice, we have to get excess paid to create a discount in TTC without VAT
$sql = 'SELECT SUM(pf.amount) as total_paiements';
$sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf, '.MAIN_DB_PREFIX.'paiementfourn as p';
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id AND c.entity IN (' . getEntity('c_paiement') . ')';
$sql.= ' WHERE pf.fk_facturefourn = '.$object->id;
$sql.= ' AND pf.fk_paiementfourn = p.rowid';
$sql.= ' AND p.entity IN (' . getEntity('facture').')';
$sql.= ' ORDER BY p.datep, p.tms';
$resql = $db->query($sql);
if (! $resql) dol_print_error($db);
$res = $db->fetch_object($resql);
$total_paiements = $res->total_paiements;
$discount->amount_ht = $discount->amount_ttc = $total_paiements - $object->total_ttc;
$discount->amount_tva = 0;
$discount->tva_tx = 0;
$result = $discount->create($user);
if ($result < 0)
{
$error++;
}
}
if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE || $object->type == FactureFournisseur::TYPE_DEPOSIT)
{
foreach ($amount_ht as $tva_tx => $xxx)
{
$discount->amount_ht = abs($amount_ht[$tva_tx]);
$discount->amount_tva = abs($amount_tva[$tva_tx]);
$discount->amount_ttc = abs($amount_ttc[$tva_tx]);
$discount->tva_tx = abs($tva_tx);
$result = $discount->create($user);
if ($result < 0)
{
$error++;
break;
}
}
}
if (empty($error))
{
if($object->type != FactureFournisseur::TYPE_DEPOSIT) {
// Classe facture
$result = $object->set_paid($user);
if ($result >= 0)
{
$db->commit();
}
else
{
setEventMessages($object->error, $object->errors, 'errors');
$db->rollback();
}
} else {
$db->commit();
}
}
else
{
setEventMessages($discount->error, $discount->errors, 'errors');
$db->rollback();
}
}
}
// Delete payment
elseif ($action == 'confirm_delete_paiement' && $confirm == 'yes' && $user->rights->fournisseur->facture->creer)
@ -464,6 +653,7 @@ if (empty($reshook))
// Credit note invoice
if ($_POST['type'] == FactureFournisseur::TYPE_CREDIT_NOTE)
{
$sourceinvoice = GETPOST('fac_avoir','int');
if (! ($sourceinvoice > 0) && empty($conf->global->INVOICE_CREDIT_NOTE_STANDALONE))
{
@ -519,6 +709,10 @@ if (empty($reshook))
$id = $object->create($user);
if($id <= 0) {
$error++;
}
if (GETPOST('invoiceAvoirWithLines', 'int')==1 && $id>0)
{
$facture_source = new FactureFournisseur($db); // fetch origin object
@ -677,7 +871,7 @@ if (empty($reshook))
}
$num=count($lines);
for ($i = 0; $i < $num; $i++)
for ($i = 0; $i < $num; $i++) // TODO handle subprice < 0
{
$desc=($lines[$i]->desc?$lines[$i]->desc:$lines[$i]->libelle);
$product_type=($lines[$i]->product_type?$lines[$i]->product_type:0);
@ -1485,6 +1679,7 @@ if ($action == 'create')
if ($societe->id > 0)
{
$absolute_discount = $societe->getAvailableDiscounts('', '', 0, 'supplier');
print $societe->getNomUrl(1);
print '<input type="hidden" name="socid" value="'.$societe->id.'">';
}
@ -1717,22 +1912,22 @@ if ($action == 'create')
print '</td></tr>';
if ($socid > 0)
if ($societe->id > 0)
{
// Discounts for third party
print '<tr><td>' . $langs->trans('Discounts') . '</td><td>';
if ($soc->remise_percent)
print $langs->trans("CompanyHasRelativeDiscount", '<a href="' . DOL_URL_ROOT . '/comm/remise.php?id=' . $soc->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"] . '?socid=' . $soc->id . '&action=' . $action . '&origin=' . GETPOST('origin') . '&originid=' . GETPOST('originid')) . '">' . $soc->remise_percent . '</a>');
if ($societe->remise_percent)
print $langs->trans("CompanyHasRelativeDiscount", '<a href="' . DOL_URL_ROOT . '/comm/remise.php?id=' . $societe->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"] . '?socid=' . $societe->id . '&action=' . $action . '&origin=' . GETPOST('origin') . '&originid=' . GETPOST('originid')) . '">' . $societe->remise_percent . '</a>');
else
print $langs->trans("CompanyHasNoRelativeDiscount");
print ' <a href="' . DOL_URL_ROOT . '/comm/remise.php?id=' . $soc->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"] . '?socid=' . $soc->id . '&action=' . $action . '&origin=' . GETPOST('origin') . '&originid=' . GETPOST('originid')) . '">(' . $langs->trans("EditRelativeDiscount") . ')</a>';
print ' <a href="' . DOL_URL_ROOT . '/comm/remise.php?id=' . $societe->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"] . '?socid=' . $societe->id . '&action=' . $action . '&origin=' . GETPOST('origin') . '&originid=' . GETPOST('originid')) . '">(' . $langs->trans("EditRelativeDiscount") . ')</a>';
print '. ';
print '<br>';
if ($absolute_discount)
print $langs->trans("CompanyHasAbsoluteDiscount", '<a href="' . DOL_URL_ROOT . '/comm/remx.php?id=' . $soc->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"] . '?socid=' . $soc->id . '&action=' . $action . '&origin=' . GETPOST('origin') . '&originid=' . GETPOST('originid')) . '">' . price($absolute_discount) . '</a>', $langs->trans("Currency" . $conf->currency));
print $langs->trans("CompanyHasAbsoluteDiscount", '<a href="' . DOL_URL_ROOT . '/comm/remx.php?id=' . $societe->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"] . '?socid=' . $societe->id . '&action=' . $action . '&origin=' . GETPOST('origin') . '&originid=' . GETPOST('originid')) . '">' . price($absolute_discount) . '</a>', $langs->trans("Currency" . $conf->currency));
else
print $langs->trans("CompanyHasNoAbsoluteDiscount");
print ' <a href="' . DOL_URL_ROOT . '/comm/remx.php?id=' . $soc->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"] . '?socid=' . $soc->id . '&action=' . $action . '&origin=' . GETPOST('origin') . '&originid=' . GETPOST('originid')) . '">(' . $langs->trans("EditGlobalDiscounts") . ')</a>';
print ' <a href="' . DOL_URL_ROOT . '/comm/remx.php?id=' . $societe->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"] . '?socid=' . $societe->id . '&action=' . $action . '&origin=' . GETPOST('origin') . '&originid=' . GETPOST('originid')) . '">(' . $langs->trans("EditGlobalDiscounts") . ')</a>';
print '.';
print '</td></tr>';
}
@ -1944,6 +2139,18 @@ else
}
$resteapayeraffiche = $resteapayer;
if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) { // Never use this
$filterabsolutediscount = "fk_invoice_supplier IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
$filtercreditnote = "fk_invoice_supplier IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
} else {
$filterabsolutediscount = "fk_invoice_supplier IS NOT NOT NULL AND (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS PAID)%')";
$filtercreditnote = "fk_invoice_supplier IS NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS PAID)%')";
}
$absolute_discount = $societe->getAvailableDiscounts('', $filterabsolutediscount, 0, 'supplier');
$absolute_creditnote = $societe->getAvailableDiscounts('', $filtercreditnote, 0, 'supplier');
$absolute_discount = price2num($absolute_discount, 'MT');
$absolute_creditnote = price2num($absolute_creditnote, 'MT');
/*
* View card
@ -1953,6 +2160,17 @@ else
dol_fiche_head($head, 'card', $titre, -1, 'bill');
$formconfirm = '';
// Confirmation de la conversion de l'avoir en reduc
if ($action == 'converttoreduc') {
if($object->type == FactureFournisseur::TYPE_STANDARD) $type_fac = 'ExcessReceived'; // TODO translation trop-perçu => trop-payé
elseif($object->type == FactureFournisseur::TYPE_CREDIT_NOTE) $type_fac = 'CreditNote';
elseif($object->type == FactureFournisseur::TYPE_DEPOSIT) $type_fac = 'Deposit';
$text = $langs->trans('ConfirmConvertToReduc', strtolower($langs->transnoentities($type_fac))); // TODO translation client => fournisseur
$formconfirm = $form->formconfirm($_SERVER['PHP_SELF'] . '?facid=' . $object->id, $langs->trans('ConvertToReduc'), $text, 'confirm_converttoreduc', '', "yes", 2);
}
// Clone confirmation
if ($action == 'clone')
{
@ -2189,7 +2407,88 @@ else
print ' ('.$langs->transnoentities("ReplacedByInvoice",$facthatreplace->getNomUrl(1)).')';
}
print '</td></tr>';
// Relative and absolute discounts
$addrelativediscount = '<a href="' . DOL_URL_ROOT . '/comm/remise.php?id=' . $societe->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"]) . '?facid=' . $object->id . '">' . $langs->trans("EditRelativeDiscounts") . '</a>';
$addabsolutediscount = '<a href="' . DOL_URL_ROOT . '/comm/remx.php?id=' . $societe->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"]) . '?facid=' . $object->id . '">' . $langs->trans("EditGlobalDiscounts") . '</a>';
$addcreditnote = '<a href="' . DOL_URL_ROOT . '/fourn/facture/card.php?action=create&socid=' . $societe->id . '&type=2&backtopage=' . urlencode($_SERVER["PHP_SELF"]) . '?facid=' . $object->id . '">' . $langs->trans("AddCreditNote") . '</a>';
$viewabsolutediscount = '<a href="' . DOL_URL_ROOT . '/comm/remx.php?id=' . $societe->id . '&backtopage=' . urlencode($_SERVER["PHP_SELF"]) . '?facid=' . $object->id . '">' . $langs->trans("ViewAvailableGlobalDiscounts") . '</a>';
print '<!-- Discounts --><tr><td>' . $langs->trans('Discounts');
print '</td><td>';
if ($societe->remise_percent)
print $langs->trans("CompanyHasRelativeDiscount", $societe->remise_percent);
else
print $langs->trans("CompanyHasNoRelativeDiscount");
// print ' ('.$addrelativediscount.')';
// Is there is commercial discount or down payment available ?
if ($absolute_discount > 0) {
print '. ';
if ($object->statut > 0 || $object->type == FactureFournisseur::TYPE_CREDIT_NOTE || $object->type == FactureFournisseur::TYPE_DEPOSIT) {
if ($object->statut == 0) {
print $langs->trans("CompanyHasAbsoluteDiscount", price($absolute_discount), $langs->transnoentities("Currency" . $conf->currency));
print '. ';
} else {
if ($object->statut < 1 || $object->type == FactureFournisseur::TYPE_CREDIT_NOTE || $object->type == FactureFournisseur::TYPE_DEPOSIT) {
$text = $langs->trans("CompanyHasAbsoluteDiscount", price($absolute_discount), $langs->transnoentities("Currency" . $conf->currency));
print '<br>' . $text . '.<br>';
} else {
$text = $langs->trans("CompanyHasAbsoluteDiscount", price($absolute_discount), $langs->transnoentities("Currency" . $conf->currency));
$text2 = $langs->trans("AbsoluteDiscountUse");
print $form->textwithpicto($text, $text2);
}
}
} else {
// Discount available of type fixed amount (not credit note)
print '<br>';
$form->form_remise_dispo($_SERVER["PHP_SELF"] . '?facid=' . $object->id, GETPOST('discountid'), 'remise_id', $societe->id, $absolute_discount, $filterabsolutediscount, $resteapayer, ' (' . $addabsolutediscount . ')', 0, 'supplier');
}
} else {
if ($absolute_creditnote > 0) // If not, link will be added later
{
if ($object->statut == FactureFournisseur::STATUS_DRAFT && $object->type != FactureFournisseur::TYPE_CREDIT_NOTE && $object->type != FactureFournisseur::TYPE_DEPOSIT)
print ' (' . $addabsolutediscount . ')<br>';
else
print '. ';
} else
print '. ';
}
// Is there credit notes availables ?
if ($absolute_creditnote > 0)
{
// If validated, we show link "add credit note to payment"
if ($object->statut != FactureFournisseur::STATUS_VALIDATED || $object->type == FactureFournisseur::TYPE_CREDIT_NOTE) {
if ($object->statut == 0 && $object->type != FactureFournisseur::TYPE_DEPOSIT) {
$text = $langs->trans("CompanyHasCreditNote", price($absolute_creditnote), $langs->transnoentities("Currency" . $conf->currency));
print $form->textwithpicto($text, $langs->trans("CreditNoteDepositUse"));
} else {
print $langs->trans("CompanyHasCreditNote", price($absolute_creditnote), $langs->transnoentities("Currency" . $conf->currency)) . '.';
}
} else { // We can add a credit note on a down payment or standard invoice or situation invoice
// There is credit notes discounts available
if (! $absolute_discount) print '<br>';
// $form->form_remise_dispo($_SERVER["PHP_SELF"].'?facid='.$object->id, 0, 'remise_id_for_payment', $societe->id, $absolute_creditnote, $filtercreditnote, $resteapayer, '', 0, 'supplier');
$more=' ('.$addcreditnote. (($addcreditnote && $viewabsolutediscount) ? ' - ' : '') . $viewabsolutediscount . ')';
$form->form_remise_dispo($_SERVER["PHP_SELF"] . '?facid=' . $object->id, 0, 'remise_id_for_payment', $societe->id, $absolute_creditnote, $filtercreditnote, 0, $more, 0, 'supplier'); // We allow credit note even if amount is higher
}
}
if (! $absolute_discount && ! $absolute_creditnote) {
print $langs->trans("CompanyHasNoAbsoluteDiscount");
if ($object->statut == FactureFournisseur::STATUS_DRAFT && $object->type != FactureFournisseur::TYPE_CREDIT_NOTE && $object->type != FactureFournisseur::TYPE_DEPOSIT)
print ' (' . $addabsolutediscount . ')<br>';
else
print '. ';
}
// if ($object->statut == 0 && $object->type != 2 && $object->type != 3)
// {
// if (! $absolute_discount && ! $absolute_creditnote) print '<br>';
// print ' &nbsp; - &nbsp; ';
// print $addabsolutediscount;
// print ' &nbsp; - &nbsp; '.$addcreditnote; // We disbale link to credit note
// }
print '</td></tr>';
// Label
print '<tr>';
print '<td>'.$form->editfieldkey("Label",'label',$object->label,$object,($user->rights->fournisseur->facture->creer)).'</td>';
@ -2539,18 +2838,19 @@ else
print $langs->trans('AlreadyPaid');
print ' :</td><td align="right"'.(($totalpaye > 0)?' class="amountalreadypaid"':'').'>' . price($totalpaye) . '</td><td>&nbsp;</td></tr>';
$resteapayer = $object->total_ttc - $totalpaye;
//$resteapayer = $object->total_ttc - $totalpaye;
$resteapayeraffiche = $resteapayer;
$cssforamountpaymentcomplete = 'amountpaymentcomplete';
// Loop on each credit note or deposit amount applied
$creditnoteamount = 0;
$depositamount = 0;
/*
$sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
$sql .= " re.description, re.fk_facture_source";
$sql .= " FROM " . MAIN_DB_PREFIX . "societe_remise_except_supplier as re";
$sql .= " WHERE fk_facture = " . $object->id;
$sql .= " re.description, re.fk_invoice_supplier_source";
$sql .= " FROM " . MAIN_DB_PREFIX . "societe_remise_except as re";
$sql .= " WHERE fk_invoice_supplier = " . $object->id;
$resql = $db->query($sql);
if ($resql) {
$num = $db->num_rows($resql);
@ -2558,7 +2858,7 @@ else
$invoice = new FactureFournisseur($db);
while ($i < $num) {
$obj = $db->fetch_object($resql);
$invoice->fetch($obj->fk_facture_source);
$invoice->fetch($obj->fk_invoice_supplier_source);
print '<tr><td colspan="' . $nbcols . '" align="right">';
if ($invoice->type == FactureFournisseur::TYPE_CREDIT_NOTE)
print $langs->trans("CreditNote") . ' ';
@ -2568,7 +2868,7 @@ else
print ' :</td>';
print '<td align="right">' . price($obj->amount_ttc) . '</td>';
print '<td align="right">';
print '<a href="' . $_SERVER["PHP_SELF"] . '?facid=' . $object->id . '&action=unlinkdiscount&discountid=' . $obj->rowid . '">' . img_delete() . '</a>';
print '<a href="' . $_SERVER["PHP_SELF"] . '?facid=' . $object->id . '&action=unlinkdiscount&discountid=' . $obj->rowid . '">' . img_delete() . '</a>'; // TODO unlinkdiscount
print '</td></tr>';
$i ++;
if ($invoice->type == FactureFournisseur::TYPE_CREDIT_NOTE)
@ -2579,7 +2879,7 @@ else
} else {
dol_print_error($db);
}
*/
// Paye partiellement 'escompte'
if (($object->statut == FactureFournisseur::STATUS_CLOSED || $object->statut == FactureFournisseur::STATUS_ABANDONED) && $object->close_code == 'discount_vat') {
@ -2627,7 +2927,7 @@ else
else
print $langs->trans('ExcessReceived');
print ' :</td>';
print '<td align="right"'.($resteapayeraffiche?' class="amountremaintopay"':$cssforamountpaymentcomplete).'>' . price($resteapayeraffiche) . '</td>';
print '<td align="right" class="'.($resteapayeraffiche?'amountremaintopay':$cssforamountpaymentcomplete).'">' . price($resteapayeraffiche) . '</td>';
print '<td class="nowrap">&nbsp;</td></tr>';
}
else // Credit note
@ -2649,7 +2949,7 @@ else
else
print $langs->trans('ExcessPaydBack');
print ' :</td>';
print '<td align="right"'.($resteapayeraffiche?' class="amountremaintopay"':(' class="'.$cssforamountpaymentcomplete.'"')).'>' . price($sign * $resteapayeraffiche) . '</td>';
print '<td align="right" class="'.($resteapayeraffiche?'amountremaintopay':$cssforamountpaymentcomplete).'">' . price($sign * $resteapayeraffiche) . '</td>';
print '<td class="nowrap">&nbsp;</td></tr>';
// Sold credit note
@ -2792,7 +3092,7 @@ else
}
// Reverse back money or convert to reduction
if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE || $object->type == FactureFournisseur::TYPE_DEPOSIT) {
if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE || $object->type == FactureFournisseur::TYPE_DEPOSIT || $object->type == FactureFournisseur::TYPE_STANDARD) {
// For credit note only
if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE && $object->statut == 1 && $object->paye == 0)
{
@ -2806,6 +3106,11 @@ else
}
}
// For standard invoice with excess paid
if ($object->type == FactureFournisseur::TYPE_STANDARD && empty($object->paye) && ($object->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits) < 0 && $user->rights->facture->creer && empty($discount->id))
{
print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?facid='.$object->id.'&amp;action=converttoreduc">'.$langs->trans('ConvertExcessReceivedToReduc').'</a></div>'; // TODO translation : trop-perçu => trop payé
}
// For credit note
if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE && $object->statut == 1 && $object->paye == 0 && $user->rights->fournisseur->facture->creer && $object->getSommePaiement() == 0) {
print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?facid=' . $object->id . '&amp;action=converttoreduc">' . $langs->trans('ConvertToReduc') . '</a></div>';

View File

@ -1718,14 +1718,15 @@ class Societe extends CommonObject
* @param User $user Filtre sur un user auteur des remises
* @param string $filter Filtre autre
* @param integer $maxvalue Filter on max value for discount
* @param string $mode 'supplier' to get available discounts for suppliers, 'customer' instead
* @return int <0 if KO, Credit note amount otherwise
*/
function getAvailableDiscounts($user='',$filter='',$maxvalue=0)
function getAvailableDiscounts($user='',$filter='',$maxvalue=0,$mode='customer')
{
require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
$discountstatic=new DiscountAbsolute($this->db);
$result=$discountstatic->getAvailableDiscounts($this,$user,$filter,$maxvalue);
$result=$discountstatic->getAvailableDiscounts($this,$user,$filter,$maxvalue,$mode);
if ($result >= 0)
{
return $result;

View File

@ -490,7 +490,7 @@ if ($sql_select)
$txt='';
print img_object($langs->trans("ShowReduc"),'reduc').' ';
if ($objp->description == '(DEPOSIT)') $txt=$langs->trans("Deposit");
elseif ($objp->description == '(EXCESS RECEIVED)') $txt=$langs->trans("ExcessReceived");
elseif ($objp->description == '(EXCESS RECEIVED)') $txt=$langs->trans("ExcessReceived"); // TODO handle (EXCESS PAID)
//else $txt=$langs->trans("Discount");
print $txt;
?>
@ -517,7 +517,7 @@ if ($sql_select)
echo ($txt?' - ':'').$langs->transnoentities("DiscountFromDeposit",$discount->getNomUrl(0));
// Add date of deposit
if (! empty($conf->global->INVOICE_ADD_DEPOSIT_DATE)) echo ' ('.dol_print_date($discount->datec).')';
}
} // TODO handle (EXCESS PAID)
else
{
echo ($txt?' - ':'').dol_htmlentitiesbr($objp->description);