NEW : Adds a payment for the list of invoices given as parameter

Adds a payment for the list of invoices given as parameter
ie : 1 payment for x invoice
This commit is contained in:
Neil Orley 2018-04-26 11:59:59 +02:00
parent 2fa69551de
commit bf3b99163d

View File

@ -957,7 +957,7 @@ class Invoices extends DolibarrApi
* *
* The model schema is defined by the PaymentData class. * The model schema is defined by the PaymentData class.
* *
* @param int $id Id of invoice * @param array $ids {@from body} Id of invoice {@type int}
* @param string $datepaye {@from body} Payment date {@type timestamp} * @param string $datepaye {@from body} Payment date {@type timestamp}
* @param int $paiementid {@from body} Payment mode Id {@min 1} * @param int $paiementid {@from body} Payment mode Id {@min 1}
* @param string $closepaidinvoices {@from body} Close paid invoices {@choice yes,no} * @param string $closepaidinvoices {@from body} Close paid invoices {@choice yes,no}
@ -967,72 +967,60 @@ class Invoices extends DolibarrApi
* @param string $chqemetteur {@from body} Payment issuer (mandatory if paiementcode = 'CHQ') * @param string $chqemetteur {@from body} Payment issuer (mandatory if paiementcode = 'CHQ')
* @param string $chqbank {@from body} Issuer bank name (optional) * @param string $chqbank {@from body} Issuer bank name (optional)
* *
* @url POST {id}/payments * @url POST /payments
* *
* @return int Payment ID * @return array Payment ID
* @throws 400 * @throws 400
* @throws 401 * @throws 401
* @throws 404 * @throws 404
*/ */
function addPayment($id, $datepaye, $paiementid, $closepaidinvoices, $accountid, $num_paiement='', $comment='', $chqemetteur='', $chqbank='') { function addPayment($ids, $datepaye, $paiementid, $closepaidinvoices, $accountid, $num_paiement='', $comment='', $chqemetteur='', $chqbank='') {
global $conf; global $conf;
require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php'; require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php';
if(! DolibarrApiAccess::$user->rights->facture->creer) { if(! DolibarrApiAccess::$user->rights->facture->creer) {
throw new RestException(401); throw new RestException(401);
} }
if(empty($id)) { foreach($ids as $id) {
if(empty($id)) {
throw new RestException(400, 'Invoice ID is mandatory'); throw new RestException(400, 'Invoice ID is mandatory');
}
if( ! DolibarrApi::_checkAccessToResource('facture',$id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
if (! empty($conf->banque->enabled)) {
if(empty($accountid)) {
throw new RestException(400, 'Account ID is mandatory');
} }
} if( ! DolibarrApi::_checkAccessToResource('facture',$id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
if(empty($paiementid)) { }
throw new RestException(400, 'Paiement ID or Paiement Code is mandatory'); $request_data = (object) $payment_data;
} if (! empty($conf->banque->enabled)) {
if(empty($accountid)) {
throw new RestException(400, 'Account ID is mandatory');
$result = $this->invoice->fetch($id); }
if( ! $result ) { }
throw new RestException(404, 'Invoice not found'); if(empty($paiementid)) {
} throw new RestException(400, 'Paiement ID or Paiement Code is mandatory');
}
// Calculate amount to pay $result = $this->invoice->fetch($id);
$totalpaye = $this->invoice->getSommePaiement(); if( ! $result ) {
$totalcreditnotes = $this->invoice->getSumCreditNotesUsed(); throw new RestException(404, 'Invoice not found');
$totaldeposits = $this->invoice->getSumDepositsUsed(); }
$resteapayer = price2num($this->invoice->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits, 'MT'); // Calculate amount to pay
$totalpaye = $this->invoice->getSommePaiement();
$totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
$totaldeposits = $this->invoice->getSumDepositsUsed();
$resteapayer = price2num($this->invoice->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits, 'MT');
// Clean parameters amount if payment is for a credit note
if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
$resteapayer = price2num($resteapayer,'MT');
$amounts[$id] = -$resteapayer;
// Multicurrency
$newvalue = price2num($this->invoice->multicurrency_total_ttc,'MT');
$multicurrency_amounts[$id] = -$newvalue;
} else {
$resteapayer = price2num($resteapayer,'MT');
$amounts[$id] = $resteapayer;
// Multicurrency
$newvalue = price2num($this->invoice->multicurrency_total_ttc,'MT');
$multicurrency_amounts[$id] = $newvalue;
}
}
$this->db->begin(); $this->db->begin();
$amounts = array();
$multicurrency_amounts = array();
// Clean parameters amount if payment is for a credit note
if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
$resteapayer = price2num($resteapayer,'MT');
$amounts[$id] = -$resteapayer;
// Multicurrency
$newvalue = price2num($this->invoice->multicurrency_total_ttc,'MT');
$multicurrency_amounts[$id] = -$newvalue;
} else {
$resteapayer = price2num($resteapayer,'MT');
$amounts[$id] = $resteapayer;
// Multicurrency
$newvalue = price2num($this->invoice->multicurrency_total_ttc,'MT');
$multicurrency_amounts[$id] = $newvalue;
}
// Creation of payment line // Creation of payment line
$paiement = new Paiement($this->db); $paiement = new Paiement($this->db);
$paiement->datepaye = $datepaye; $paiement->datepaye = $datepaye;
@ -1042,17 +1030,14 @@ class Invoices extends DolibarrApi
$paiement->paiementcode = dol_getIdFromCode($this->db,$paiementid,'c_paiement','id','code',1); $paiement->paiementcode = dol_getIdFromCode($this->db,$paiementid,'c_paiement','id','code',1);
$paiement->num_paiement = $num_paiement; $paiement->num_paiement = $num_paiement;
$paiement->note = $comment; $paiement->note = $comment;
$paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices=='yes'?1:0)); // This include closing invoices $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices=='yes'?1:0)); // This include closing invoices
if ($paiement_id < 0) if ($paiement_id < 0)
{ {
$this->db->rollback(); $this->db->rollback();
throw new RestException(400, 'Payment error : '.$paiement->error); throw new RestException(400, 'Payment error : '.$paiement->error);
} }
if (! empty($conf->banque->enabled)) { if (! empty($conf->banque->enabled)) {
$label='(CustomerInvoicePayment)'; $label='(CustomerInvoicePayment)';
if($paiement->paiementcode == 'CHQ' && empty($chqemetteur)) { if($paiement->paiementcode == 'CHQ' && empty($chqemetteur)) {
throw new RestException(400, 'Emetteur is mandatory when payment code is '.$paiement->paiementcode); throw new RestException(400, 'Emetteur is mandatory when payment code is '.$paiement->paiementcode);
} }