diff --git a/htdocs/fourn/class/api_supplier_invoices.class.php b/htdocs/fourn/class/api_supplier_invoices.class.php index 09700c5885537..a09b8d4fbf6e8 100644 --- a/htdocs/fourn/class/api_supplier_invoices.class.php +++ b/htdocs/fourn/class/api_supplier_invoices.class.php @@ -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 @@ 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(); @@ -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 @@ -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); } } @@ -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'); @@ -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) { @@ -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) { @@ -339,7 +339,7 @@ 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); @@ -347,13 +347,13 @@ public function validate($id, $idwarehouse = 0, $notrigger = 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 . ')' ) ); } @@ -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); @@ -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 * @@ -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; @@ -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")) { @@ -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'); @@ -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); } } @@ -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(); @@ -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; @@ -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; @@ -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; @@ -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