Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #25230 Supplier invoice API partial payment #26063

Merged
merged 2 commits into from Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 38 additions & 31 deletions htdocs/fourn/class/api_supplier_invoices.class.php
Expand Up @@ -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
Expand Down Expand Up @@ -75,7 +75,7 @@ public function get($id)
}

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();
Expand Down Expand Up @@ -123,19 +123,19 @@ public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100,
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
Expand All @@ -156,14 +156,14 @@ public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100,
}
// 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);
}
}

Expand Down Expand Up @@ -191,7 +191,7 @@ public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100,
$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');
Expand Down Expand Up @@ -257,7 +257,7 @@ public function put($id, $request_data = null)
}

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) {
Expand Down Expand Up @@ -296,7 +296,7 @@ public function delete($id)
}

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) {
Expand Down Expand Up @@ -339,21 +339,21 @@ public function validate($id, $idwarehouse = 0, $notrigger = 0)
}

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);
if ($result == 0) {
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 . ')'
)
);
}
Expand Down Expand Up @@ -381,7 +381,7 @@ public function getPayments($id)
}

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);
Expand Down Expand Up @@ -410,6 +410,7 @@ public function getPayments($id)
* @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 {@from body} Amount of payment if we don't want to use the remain to pay
*
* @url POST {id}/payments
*
Expand All @@ -418,7 +419,7 @@ public function getPayments($id)
* @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;

Expand All @@ -430,7 +431,7 @@ public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices,
}

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")) {
Expand All @@ -449,18 +450,24 @@ public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices,
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');
Expand All @@ -479,14 +486,14 @@ public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices,
$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);
}
}

Expand Down Expand Up @@ -516,7 +523,7 @@ public function getLines($id)
}

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();
Expand Down Expand Up @@ -552,7 +559,7 @@ public function postLine($id, $request_data = null)
}

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;
Expand Down Expand Up @@ -586,7 +593,7 @@ public function postLine($id, $request_data = null)
);

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;
Expand Down Expand Up @@ -619,7 +626,7 @@ public function putLine($id, $lineid, $request_data = null)
}

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;
Expand Down Expand Up @@ -690,7 +697,7 @@ public function deleteLine($id, $lineid)
}

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
Expand Down