Re synchronized the UI action button "Mark as credit available" with API

At some point we will need to move this code into common location (maybe facture.class.php) and call as function from both places
This commit is contained in:
Dan Rusu 2019-04-06 20:10:11 +03:00
parent c61cbd7b15
commit 4597e78a9d

View File

@ -936,7 +936,7 @@ class Invoices extends DolibarrApi
*/ */
public function markAsCreditAvailable($id) public function markAsCreditAvailable($id)
{ {
if(! DolibarrApiAccess::$user->rights->facture->creer) { if( ! DolibarrApiAccess::$user->rights->facture->creer) {
throw new RestException(401); throw new RestException(401);
} }
@ -949,89 +949,145 @@ class Invoices extends DolibarrApi
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
$result = $this->invoice->fetch_thirdparty(); if ($this->invoice->paye) {
if( ! $result ) { throw new RestException(500, 'Alreay payed');
throw new RestException(404, 'Thirdparty not found');
} }
if (! $this->invoice->paye) // protection against multiple submit $this->invoice->fetch($id);
{ $this->invoice->fetch_thirdparty();
$this->db->begin();
$this->invoice->fetch_lines(); // Check if there is already a discount (protection to avoid duplicate creation when resubmit post)
$discountcheck=new DiscountAbsolute($this->db);
$result=$discountcheck->fetch(0, $this->invoice->id);
$amount_ht = $amount_tva = $amount_ttc = array(); $canconvert=0;
if ($this->invoice->type == Facture::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 (($this->invoice->type == Facture::TYPE_CREDIT_NOTE || $this->invoice->type == Facture::TYPE_STANDARD) && $this->invoice->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)
{
$this->db->begin();
// Loop on each vat rate $amount_ht = $amount_tva = $amount_ttc = array();
$i=0; $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
$amount_ht = array();
$amount_tva = array();
$amount_ttc = array();
foreach($this->invoice->lines as $line)
{
$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 // Loop on each vat rate
$discount = new DiscountAbsolute($this->db); $i = 0;
if ($this->invoice->type == 2) $discount->description='(CREDIT_NOTE)'; foreach ($this->invoice->lines as $line)
elseif ($this->invoice->type == 3) $discount->description='(DEPOSIT)'; {
else { if ($line->product_type < 9 && $line->total_ht != 0) // Remove lines with product_type greater than or equal to 9
$this->error="CantConvertToReducAnInvoiceOfThisType"; { // no need to create discount if amount is null
return -1; $amount_ht[$line->tva_tx] += $line->total_ht;
} $amount_tva[$line->tva_tx] += $line->total_tva;
$discount->tva_tx=abs($this->invoice->total_ttc); $amount_ttc[$line->tva_tx] += $line->total_ttc;
$discount->fk_soc=$this->invoice->socid; $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
$discount->fk_facture_source=$this->invoice->id; $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
$multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
$i++;
}
}
$error=0; // Insert one discount by VAT rate category
foreach($amount_ht as $tva_tx => $xxx) $discount = new DiscountAbsolute($this->db);
{ if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE){
$discount->amount_ht=abs($amount_ht[$tva_tx]); $discount->description = '(CREDIT_NOTE)';
$discount->amount_tva=abs($amount_tva[$tva_tx]); }
$discount->amount_ttc=abs($amount_ttc[$tva_tx]); elseif ($this->invoice->type == Facture::TYPE_DEPOSIT){
$discount->tva_tx=abs($tva_tx); $discount->description = '(DEPOSIT)';
}
elseif ($this->invoice->type == Facture::TYPE_STANDARD || $this->invoice->type == Facture::TYPE_REPLACEMENT || $this->invoice->type == Facture::TYPE_SITUATION) {
$discount->description = '(EXCESS RECEIVED)';
}
else {
throw new RestException(500, 'Cant convert to reduc an Invoice of this type');
}
$result=$discount->create(DolibarrApiAccess::$user); $discount->fk_soc = $this->invoice->socid;
if ($result < 0) $discount->fk_facture_source = $this->invoice->id;
{
$error++;
break;
}
}
if (! $error) $error = 0;
{
// Classe facture
$result=$this->invoice->set_paid(DolibarrApiAccess::$user);
if ($result > 0)
{
//$mesg='OK'.$discount->id;
$this->db->commit();
}
else
{
$this->db->rollback();
throw new RestException(500, 'Could not set paid');
}
}
else
{
$this->db->rollback();
throw new RestException(500, 'Discount creation error');
}
}
$result = $this->invoice->fetch($id); if ($this->invoice->type == Facture::TYPE_STANDARD || $this->invoice->type == Facture::TYPE_REPLACEMENT || $this->invoice->type == Facture::TYPE_SITUATION)
if( ! $result ) { {
throw new RestException(404, 'Invoice not found'); // If we're on a standard invoice, we have to get excess received to create a discount in TTC without VAT
}
if( ! DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) { // Total payments
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); $sql = 'SELECT SUM(pf.amount) as total_paiements';
$sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf, '.MAIN_DB_PREFIX.'paiement as p';
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id';
$sql.= ' WHERE pf.fk_facture = '.$this->invoice->id;
$sql.= ' AND pf.fk_paiement = p.rowid';
$sql.= ' AND p.entity IN ('.getEntity('invoice').')';
$resql = $this->db->query($sql);
if (! $resql) dol_print_error($this->db);
$res = $this->db->fetch_object($resql);
$total_paiements = $res->total_paiements;
// Total credit note and deposit
$total_creditnote_and_deposit = 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 as re";
$sql .= " WHERE fk_facture = " . $this->invoice->id;
$resql = $this->db->query($sql);
if (!empty($resql)) {
while ($obj = $this->db->fetch_object($resql)) $total_creditnote_and_deposit += $obj->amount_ttc;
} else dol_print_error($this->db);
$discount->amount_ht = $discount->amount_ttc = $total_paiements + $total_creditnote_and_deposit - $this->invoice->total_ttc;
$discount->amount_tva = 0;
$discount->tva_tx = 0;
$result = $discount->create(DolibarrApiAccess::$user);
if ($result < 0)
{
$error++;
}
}
if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE || $this->invoice->type == Facture::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->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
$discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
$discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
$discount->tva_tx = abs($tva_tx);
$result = $discount->create(DolibarrApiAccess::$user);
if ($result < 0)
{
$error++;
break;
}
}
}
if (empty($error))
{
if($this->invoice->type != Facture::TYPE_DEPOSIT) {
// Classe facture
$result = $this->invoice->set_paid(DolibarrApiAccess::$user);
if ($result >= 0)
{
$this->db->commit();
}
else
{
$this->db->rollback();
throw new RestException(500, 'Could not set paid');
}
} else {
$this->db->commit();
}
}
else
{
$this->db->rollback();
throw new RestException(500, 'Discount creation error');
}
} }
return $this->_cleanObjectDatas($this->invoice); return $this->_cleanObjectDatas($this->invoice);