This commit is contained in:
pascal
2023-09-28 17:53:23 +02:00
parent b9336c024a
commit dbfa676e48

View File

@@ -18,8 +18,8 @@
use Luracast\Restler\RestException;
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 . '/fourn/class/fournisseur.facture.class.php';
require_once DOL_DOCUMENT_ROOT . '/fourn/class/paiementfourn.class.php';
/**
* API class for supplier invoices
@@ -75,7 +75,7 @@ class SupplierInvoices extends DolibarrApi
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
$this->invoice->fetchObjectLinked();
@@ -123,19 +123,19 @@ class SupplierInvoices extends DolibarrApi
if (!DolibarrApiAccess::$user->hasRight("societe", "client", "voir") || $search_sale > 0) {
$sql .= ", sc.fk_soc, sc.fk_user";
}
$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn AS t LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
$sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn AS t LEFT JOIN " . MAIN_DB_PREFIX . "facture_fourn_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
// We need this table joined to the select in order to filter by sale
if (!DolibarrApiAccess::$user->hasRight("societe", "client", "voir") || $search_sale > 0) {
$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql .= ", " . MAIN_DB_PREFIX . "societe_commerciaux as sc";
}
$sql .= ' WHERE t.entity IN ('.getEntity('supplier_invoice').')';
$sql .= ' WHERE t.entity IN (' . getEntity('supplier_invoice') . ')';
if (!DolibarrApiAccess::$user->hasRight("societe", "client", "voir") || $search_sale > 0) {
$sql .= " AND t.fk_soc = sc.fk_soc";
}
if ($socids) {
$sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
$sql .= " AND t.fk_soc IN (" . $this->db->sanitize($socids) . ")";
}
if ($search_sale > 0) {
$sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
@@ -156,14 +156,14 @@ class SupplierInvoices extends DolibarrApi
}
// Insert sale filter
if ($search_sale > 0) {
$sql .= " AND sc.fk_user = ".((int) $search_sale);
$sql .= " AND sc.fk_user = " . ((int) $search_sale);
}
// Add sql filters
if ($sqlfilters) {
$errormessage = '';
$sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
if ($errormessage) {
throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage);
}
}
@@ -191,7 +191,7 @@ class SupplierInvoices extends DolibarrApi
$i++;
}
} else {
throw new RestException(503, 'Error when retrieve supplier invoice list : '.$this->db->lasterror());
throw new RestException(503, 'Error when retrieve supplier invoice list : ' . $this->db->lasterror());
}
if (!count($obj_ret)) {
throw new RestException(404, 'No supplier invoice found');
@@ -257,7 +257,7 @@ class SupplierInvoices extends DolibarrApi
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
foreach ($request_data as $field => $value) {
@@ -296,7 +296,7 @@ class SupplierInvoices extends DolibarrApi
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
if ($this->invoice->delete(DolibarrApiAccess::$user) < 0) {
@@ -339,7 +339,7 @@ class SupplierInvoices extends DolibarrApi
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
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->validate(DolibarrApiAccess::$user, '', $idwarehouse, $notrigger);
@@ -347,13 +347,13 @@ class SupplierInvoices extends DolibarrApi
throw new RestException(304, 'Error nothing done. The invoice is already validated');
}
if ($result < 0) {
throw new RestException(500, 'Error when validating Invoice: '.$this->invoice->error);
throw new RestException(500, 'Error when validating Invoice: ' . $this->invoice->error);
}
return array(
'success' => array(
'code' => 200,
'message' => 'Invoice validated (Ref='.$this->invoice->ref.')'
'message' => 'Invoice validated (Ref=' . $this->invoice->ref . ')'
)
);
}
@@ -381,7 +381,7 @@ class SupplierInvoices extends DolibarrApi
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
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($id);
@@ -410,6 +410,7 @@ class SupplierInvoices extends DolibarrApi
* @param string $comment {@from body} Note (optional)
* @param string $chqemetteur {@from body} Payment issuer (mandatory if payment_mode_id corresponds to 'CHQ'-payment type)
* @param string $chqbank {@from body} Issuer bank name (optional)
* @param float $amount Amount of payment if we don't want to use the remain to pay
*
* @url POST {id}/payments
*
@@ -418,7 +419,7 @@ class SupplierInvoices extends DolibarrApi
* @throws RestException 401
* @throws RestException 404
*/
public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment = '', $comment = '', $chqemetteur = '', $chqbank = '')
public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment = '', $comment = '', $chqemetteur = '', $chqbank = '', $amount = null)
{
global $conf;
@@ -430,7 +431,7 @@ class SupplierInvoices extends DolibarrApi
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
if (isModEnabled("banque")) {
@@ -449,18 +450,24 @@ class SupplierInvoices extends DolibarrApi
throw new RestException(404, 'Invoice not found');
}
// Calculate amount to pay
$totalpaid = $this->invoice->getSommePaiement();
$totaldeposits = $this->invoice->getSumDepositsUsed();
$resteapayer = price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits, 'MT');
if (null !== $amount && $amount > 0) {
// We use the amount given in parameter
$paymentamount = $amount;
} else {
// We calculate the remain to pay, and use it as amount
$totalpaid = $this->invoice->getSommePaiement();
$totaldeposits = $this->invoice->getSumDepositsUsed();
$paymentamount = price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits, 'MT');
}
$this->db->begin();
$amounts = array();
$multicurrency_amounts = array();
$resteapayer = price2num($resteapayer, 'MT');
$amounts[$id] = $resteapayer;
$paymentamount = price2num($paymentamount, 'MT');
$amounts[$id] = $paymentamount;
// Multicurrency
$newvalue = price2num($this->invoice->multicurrency_total_ttc, 'MT');
@@ -479,14 +486,14 @@ class SupplierInvoices extends DolibarrApi
$paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices == 'yes' ? 1 : 0)); // This include closing invoices
if ($paiement_id < 0) {
$this->db->rollback();
throw new RestException(400, 'Payment error : '.$paiement->error);
throw new RestException(400, 'Payment error : ' . $paiement->error);
}
if (isModEnabled("banque")) {
$result = $paiement->addPaymentToBank(DolibarrApiAccess::$user, 'payment_supplier', '(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
if ($result < 0) {
$this->db->rollback();
throw new RestException(400, 'Add payment to bank error : '.$paiement->error);
throw new RestException(400, 'Add payment to bank error : ' . $paiement->error);
}
}
@@ -516,7 +523,7 @@ class SupplierInvoices extends DolibarrApi
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
$this->invoice->fetch_lines();
$result = array();
@@ -552,7 +559,7 @@ class SupplierInvoices extends DolibarrApi
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
$request_data = (object) $request_data;
@@ -586,7 +593,7 @@ class SupplierInvoices extends DolibarrApi
);
if ($updateRes < 0) {
throw new RestException(400, 'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
throw new RestException(400, 'Unable to insert the new line. Check your inputs. ' . $this->invoice->error);
}
return $updateRes;
@@ -619,7 +626,7 @@ class SupplierInvoices extends DolibarrApi
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
$request_data = (object) $request_data;
@@ -690,7 +697,7 @@ class SupplierInvoices extends DolibarrApi
}
if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
// TODO Check the lineid $lineid is a line of ojbect