2015-05-05 04:40:28 +02:00
< ? php
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
2017-10-19 12:35:15 +02:00
use Luracast\Restler\RestException ;
2015-05-05 04:40:28 +02:00
2017-10-19 12:35:15 +02:00
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php' ;
2015-05-05 04:40:28 +02:00
/**
2016-06-14 19:55:49 +02:00
* API class for invoices
2015-05-05 04:40:28 +02:00
*
2017-07-28 10:16:17 +02:00
* @ access protected
2015-05-05 04:40:28 +02:00
* @ class DolibarrApiAccess { @ requires user , external }
*/
2016-06-14 19:55:49 +02:00
class Invoices extends DolibarrApi
2015-05-06 00:55:42 +02:00
{
2015-05-05 04:40:28 +02:00
/**
*
2017-07-28 10:16:17 +02:00
* @ var array $FIELDS Mandatory fields , checked when create and update object
2015-05-05 04:40:28 +02:00
*/
static $FIELDS = array (
2018-08-15 14:28:34 +02:00
'socid' ,
2015-05-05 04:40:28 +02:00
);
/**
* @ var Facture $invoice { @ type Facture }
*/
public $invoice ;
/**
* Constructor
*/
function __construct ()
{
global $db , $conf ;
$this -> db = $db ;
$this -> invoice = new Facture ( $this -> db );
}
/**
* Get properties of a invoice object
*
* Return an array with invoice informations
2017-07-28 10:16:17 +02:00
*
2015-05-05 04:40:28 +02:00
* @ param int $id ID of invoice
* @ return array | mixed data without useless information
2015-05-06 00:55:42 +02:00
*
2015-05-05 04:40:28 +02:00
* @ throws RestException
*/
2018-02-01 02:14:17 +01:00
function get ( $id )
{
2015-05-05 04:40:28 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> lire ) {
throw new RestException ( 401 );
}
2017-07-28 10:16:17 +02:00
2018-02-01 02:14:17 +01:00
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
2017-07-28 10:16:17 +02:00
2018-02-01 02:14:17 +01:00
// Get payment details
$this -> invoice -> totalpaye = $this -> invoice -> getSommePaiement ();
$this -> invoice -> totalcreditnotes = $this -> invoice -> getSumCreditNotesUsed ();
$this -> invoice -> totaldeposits = $this -> invoice -> getSumDepositsUsed ();
$this -> invoice -> resteapayer = price2num ( $this -> invoice -> total_ttc - $this -> invoice -> totalpaye - $this -> invoice -> totalcreditnotes - $this -> invoice -> totaldeposits , 'MT' );
2017-10-23 16:45:49 +02:00
2018-02-01 02:14:17 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
2015-05-05 04:40:28 +02:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2018-05-29 10:04:20 +02:00
// Add external contacts ids
$this -> invoice -> contacts_ids = $this -> invoice -> liste_contact ( - 1 , 'external' , 1 );
2017-10-19 17:57:16 +02:00
$this -> invoice -> fetchObjectLinked ();
2015-05-05 04:40:28 +02:00
return $this -> _cleanObjectDatas ( $this -> invoice );
2018-02-01 02:14:17 +01:00
}
2015-05-05 04:40:28 +02:00
/**
* List invoices
2017-07-28 10:16:17 +02:00
*
2015-05-05 04:40:28 +02:00
* Get a list of invoices
2017-07-28 10:16:17 +02:00
*
2016-12-05 13:31:29 +01:00
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
* @ param string $thirdparty_ids Thirdparty ids to filter orders of . { @ example '1' or '1,2,3' } { @ pattern /^ [ 0 - 9 ,] * $ / i }
* @ param string $status Filter by invoice status : draft | unpaid | paid | cancelled
* @ param string $sqlfilters Other criteria to filter answers separated by a comma . Syntax example " (t.ref:like:'SO-%') and (t.date_creation:<:'20160101') "
* @ return array Array of invoice objects
2015-05-06 00:55:42 +02:00
*
2016-06-14 19:55:49 +02:00
* @ throws RestException
2015-05-05 04:40:28 +02:00
*/
2018-08-15 14:28:34 +02:00
function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $thirdparty_ids = '' , $status = '' , $sqlfilters = '' )
{
2015-05-05 04:40:28 +02:00
global $db , $conf ;
2017-07-28 10:16:17 +02:00
2015-05-05 04:40:28 +02:00
$obj_ret = array ();
2016-12-07 19:02:39 +01:00
// case of external user, $thirdparty_ids param is ignored and replaced by user's socid
2016-12-05 13:31:29 +01:00
$socids = DolibarrApiAccess :: $user -> societe_id ? DolibarrApiAccess :: $user -> societe_id : $thirdparty_ids ;
2017-07-28 10:16:17 +02:00
2015-05-05 04:40:28 +02:00
// If the internal user must only see his customers, force searching by him
2016-12-07 19:02:39 +01:00
$search_sale = 0 ;
if ( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socids ) $search_sale = DolibarrApiAccess :: $user -> id ;
2015-05-05 04:40:28 +02:00
2016-10-25 18:33:45 +02:00
$sql = " SELECT t.rowid " ;
2016-12-07 19:02:39 +01:00
if (( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socids ) || $search_sale > 0 ) $sql .= " , sc.fk_soc, sc.fk_user " ; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
2016-10-25 18:33:45 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " facture as t " ;
2017-07-28 10:16:17 +02:00
2016-12-07 19:02:39 +01:00
if (( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socids ) || $search_sale > 0 ) $sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ; // We need this table joined to the select in order to filter by sale
2015-05-05 04:40:28 +02:00
2018-11-15 17:27:49 +01:00
$sql .= ' WHERE t.entity IN (' . getEntity ( 'invoice' ) . ')' ;
2016-12-07 19:02:39 +01:00
if (( ! DolibarrApiAccess :: $user -> rights -> societe -> client -> voir && ! $socids ) || $search_sale > 0 ) $sql .= " AND t.fk_soc = sc.fk_soc " ;
2016-12-05 13:31:29 +01:00
if ( $socids ) $sql .= " AND t.fk_soc IN ( " . $socids . " ) " ;
2016-12-07 19:02:39 +01:00
2016-10-25 18:33:45 +02:00
if ( $search_sale > 0 ) $sql .= " AND t.rowid = sc.fk_soc " ; // Join for the needed table to filter by sale
2017-07-28 10:16:17 +02:00
2016-06-14 19:55:49 +02:00
// Filter by status
2016-10-25 18:33:45 +02:00
if ( $status == 'draft' ) $sql .= " AND t.fk_statut IN (0) " ;
if ( $status == 'unpaid' ) $sql .= " AND t.fk_statut IN (1) " ;
if ( $status == 'paid' ) $sql .= " AND t.fk_statut IN (2) " ;
if ( $status == 'cancelled' ) $sql .= " AND t.fk_statut IN (3) " ;
2015-05-05 04:40:28 +02:00
// Insert sale filter
if ( $search_sale > 0 )
{
$sql .= " AND sc.fk_user = " . $search_sale ;
}
2016-10-25 18:33:45 +02:00
// Add sql filters
2017-07-28 10:16:17 +02:00
if ( $sqlfilters )
2015-05-05 04:40:28 +02:00
{
2016-10-25 18:33:45 +02:00
if ( ! DolibarrApi :: _checkFilters ( $sqlfilters ))
{
throw new RestException ( 503 , 'Error when validating parameter sqlfilters ' . $sqlfilters );
}
$regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)' ;
$sql .= " AND ( " . preg_replace_callback ( '/' . $regexstring . '/' , 'DolibarrApi::_forge_criteria_callback' , $sqlfilters ) . " ) " ;
2015-05-05 04:40:28 +02:00
}
2017-07-28 10:16:17 +02:00
2015-05-05 04:40:28 +02:00
$sql .= $db -> order ( $sortfield , $sortorder );
2018-01-04 21:14:45 +01:00
if ( $limit )
{
2015-05-05 04:40:28 +02:00
if ( $page < 0 )
{
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $db -> plimit ( $limit + 1 , $offset );
}
$result = $db -> query ( $sql );
if ( $result )
{
2016-12-07 19:12:25 +01:00
$i = 0 ;
2015-05-05 04:40:28 +02:00
$num = $db -> num_rows ( $result );
2017-05-22 10:45:02 +02:00
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
while ( $i < $min )
2015-05-05 04:40:28 +02:00
{
$obj = $db -> fetch_object ( $result );
$invoice_static = new Facture ( $db );
2018-01-04 21:14:45 +01:00
if ( $invoice_static -> fetch ( $obj -> rowid ))
{
// Get payment details
$invoice_static -> totalpaid = $invoice_static -> getSommePaiement ();
$invoice_static -> totalcreditnotes = $invoice_static -> getSumCreditNotesUsed ();
$invoice_static -> totaldeposits = $invoice_static -> getSumDepositsUsed ();
$invoice_static -> remaintopay = price2num ( $invoice_static -> total_ttc - $invoice_static -> totalpaid - $invoice_static -> totalcreditnotes - $invoice_static -> totaldeposits , 'MT' );
2018-05-29 10:04:20 +02:00
// Add external contacts ids
$invoice_static -> contacts_ids = $invoice_static -> liste_contact ( - 1 , 'external' , 1 );
2018-05-22 11:40:32 +02:00
2018-01-04 21:14:45 +01:00
$obj_ret [] = $this -> _cleanObjectDatas ( $invoice_static );
2015-05-05 04:40:28 +02:00
}
$i ++ ;
}
}
else {
2016-12-05 13:31:29 +01:00
throw new RestException ( 503 , 'Error when retrieve invoice list : ' . $db -> lasterror ());
2015-05-05 04:40:28 +02:00
}
if ( ! count ( $obj_ret )) {
throw new RestException ( 404 , 'No invoice found' );
}
return $obj_ret ;
}
2017-07-28 10:16:17 +02:00
2015-05-05 04:40:28 +02:00
/**
* Create invoice object
2017-07-28 10:16:17 +02:00
*
2015-05-06 00:55:42 +02:00
* @ param array $request_data Request datas
2016-12-07 19:12:25 +01:00
* @ return int ID of invoice
2015-05-05 04:40:28 +02:00
*/
2018-04-16 13:51:16 +02:00
function post ( $request_data = null )
2015-05-05 04:40:28 +02:00
{
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
2016-07-29 02:28:51 +02:00
throw new RestException ( 401 , " Insuffisant rights " );
2015-05-05 04:40:28 +02:00
}
// Check mandatory fields
$result = $this -> _validate ( $request_data );
2017-07-28 10:16:17 +02:00
2015-05-05 04:40:28 +02:00
foreach ( $request_data as $field => $value ) {
$this -> invoice -> $field = $value ;
}
2017-09-21 17:48:12 +02:00
if ( ! array_key_exists ( 'date' , $request_data )) {
2015-05-05 04:40:28 +02:00
$this -> invoice -> date = dol_now ();
}
2016-07-29 02:28:51 +02:00
/* We keep lines as an array
if ( isset ( $request_data [ " lines " ])) {
$lines = array ();
foreach ( $request_data [ " lines " ] as $line ) {
array_push ( $lines , ( object ) $line );
}
$this -> invoice -> lines = $lines ;
} */
2017-07-28 10:16:17 +02:00
2016-12-23 02:08:22 +01:00
if ( $this -> invoice -> create ( DolibarrApiAccess :: $user ) < 0 ) {
throw new RestException ( 500 , " Error creating invoice " , array_merge ( array ( $this -> invoice -> error ), $this -> invoice -> errors ));
2015-05-05 04:40:28 +02:00
}
return $this -> invoice -> id ;
}
2017-11-14 15:49:09 +01:00
/**
* Create an invoice using an existing order .
*
*
* @ param int $orderid Id of the order
*
* @ url POST / createfromorder / { orderid }
*
* @ return int
* @ throws 400
* @ throws 401
* @ throws 404
* @ throws 405
*/
2018-08-15 14:28:34 +02:00
function createInvoiceFromOrder ( $orderid )
{
2017-11-14 15:49:09 +01:00
require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php' ;
if ( ! DolibarrApiAccess :: $user -> rights -> commande -> lire ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 );
2017-11-14 15:49:09 +01:00
}
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 );
2017-12-21 16:50:18 +01:00
}
2017-11-14 15:49:09 +01:00
if ( empty ( $orderid )) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 400 , 'Order ID is mandatory' );
2017-11-14 15:49:09 +01:00
}
$order = new Commande ( $this -> db );
$result = $order -> fetch ( $orderid );
if ( ! $result ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 404 , 'Order not found' );
2017-11-14 15:49:09 +01:00
}
$result = $this -> invoice -> createFromOrder ( $order , DolibarrApiAccess :: $user );
if ( $result < 0 ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 405 , $this -> invoice -> error );
2017-11-14 15:49:09 +01:00
}
$this -> invoice -> fetchObjectLinked ();
return $this -> _cleanObjectDatas ( $this -> invoice );
}
2017-10-19 12:30:36 +02:00
/**
* Get lines of an invoice
*
* @ param int $id Id of invoice
*
* @ url GET { id } / lines
*
* @ return int
*/
2018-08-15 14:28:34 +02:00
function getLines ( $id )
{
2017-10-19 12:30:36 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> lire ) {
throw new RestException ( 401 );
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$this -> invoice -> getLinesArray ();
$result = array ();
foreach ( $this -> invoice -> lines as $line ) {
array_push ( $result , $this -> _cleanObjectDatas ( $line ));
}
return $result ;
}
/**
2017-10-19 12:35:15 +02:00
* Update a line to a given invoice
2017-10-19 12:30:36 +02:00
*
* @ param int $id Id of invoice to update
* @ param int $lineid Id of line to update
* @ param array $request_data InvoiceLine data
*
* @ url PUT { id } / lines / { lineid }
*
* @ return object
2018-01-04 23:44:21 +01:00
*
* @ throws 200
* @ throws 304
* @ throws 401
* @ throws 404
2017-10-19 12:30:36 +02:00
*/
2018-08-15 14:28:34 +02:00
function putLine ( $id , $lineid , $request_data = null )
{
2017-10-19 12:30:36 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
throw new RestException ( 401 );
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$request_data = ( object ) $request_data ;
$updateRes = $this -> invoice -> updateline (
$lineid ,
$request_data -> desc ,
$request_data -> subprice ,
$request_data -> qty ,
$request_data -> remise_percent ,
$request_data -> date_start ,
$request_data -> date_end ,
$request_data -> tva_tx ,
$request_data -> localtax1_tx ,
$request_data -> localtax2_tx ,
'HT' ,
$request_data -> info_bits ,
$request_data -> product_type ,
$request_data -> fk_parent_line ,
0 ,
$request_data -> fk_fournprice ,
$request_data -> pa_ht ,
$request_data -> label ,
$request_data -> special_code ,
$request_data -> array_options ,
$request_data -> situation_percent ,
$request_data -> fk_unit ,
$request_data -> multicurrency_subprice
);
if ( $updateRes > 0 ) {
$result = $this -> get ( $id );
unset ( $result -> line );
return $this -> _cleanObjectDatas ( $result );
2018-01-04 23:44:21 +01:00
} else {
throw new RestException ( 304 , $this -> invoice -> error );
2017-10-19 12:30:36 +02:00
}
}
/**
2017-10-22 12:21:44 +02:00
* Deletes a line of a given invoice
2017-10-19 12:30:36 +02:00
*
2017-10-22 12:21:44 +02:00
* @ param int $id Id of invoice
* @ param int $lineid Id of the line to delete
2017-10-19 12:30:36 +02:00
*
2017-10-22 12:21:44 +02:00
* @ url DELETE { id } / lines / { lineid }
2017-10-19 12:30:36 +02:00
*
2017-10-22 12:21:44 +02:00
* @ return array
2018-01-04 23:44:21 +01:00
*
2017-10-22 12:21:44 +02:00
* @ throws 400
* @ throws 401
* @ throws 404
* @ throws 405
2017-10-19 12:30:36 +02:00
*/
2018-08-15 14:28:34 +02:00
function deleteLine ( $id , $lineid )
{
2017-10-22 12:21:44 +02:00
2017-10-19 12:30:36 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
throw new RestException ( 401 );
}
2017-10-22 12:21:44 +02:00
if ( empty ( $lineid )) {
throw new RestException ( 400 , 'Line ID is mandatory' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2017-10-19 12:30:36 +02:00
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
2017-12-21 16:50:18 +01:00
// TODO Check the lineid $lineid is a line of ojbect
$updateRes = $this -> invoice -> deleteline ( $lineid );
if ( $updateRes > 0 ) {
return $this -> get ( $id );
}
else
{
2017-10-22 12:21:44 +02:00
throw new RestException ( 405 , $this -> invoice -> error );
2017-10-19 12:30:36 +02:00
}
}
2015-05-05 04:40:28 +02:00
/**
* Update invoice
*
* @ param int $id Id of invoice to update
2017-07-28 10:16:17 +02:00
* @ param array $request_data Datas
* @ return int
2015-05-05 04:40:28 +02:00
*/
2018-04-16 13:51:16 +02:00
function put ( $id , $request_data = null )
2015-05-05 04:40:28 +02:00
{
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
throw new RestException ( 401 );
}
2017-07-28 10:16:17 +02:00
2015-05-05 04:40:28 +02:00
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
2016-07-29 02:28:51 +02:00
throw new RestException ( 404 , 'Invoice not found' );
2015-05-05 04:40:28 +02:00
}
2017-07-28 10:16:17 +02:00
2015-05-05 04:40:28 +02:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
foreach ( $request_data as $field => $value ) {
2016-11-19 16:08:27 +01:00
if ( $field == 'id' ) continue ;
2015-05-05 04:40:28 +02:00
$this -> invoice -> $field = $value ;
}
2017-07-28 10:16:17 +02:00
2017-12-28 12:43:26 +01:00
// update bank account
if ( ! empty ( $this -> invoice -> fk_account ))
{
if ( $this -> invoice -> setBankAccount ( $this -> invoice -> fk_account ) == 0 )
{
throw new RestException ( 400 , $this -> invoice -> error );
}
}
2017-12-19 14:14:26 +01:00
2015-05-05 04:40:28 +02:00
if ( $this -> invoice -> update ( $id , DolibarrApiAccess :: $user ))
2018-01-12 15:38:53 +01:00
return $this -> get ( $id );
2017-07-28 10:16:17 +02:00
2015-05-05 04:40:28 +02:00
return false ;
}
2017-07-28 10:16:17 +02:00
2015-05-05 04:40:28 +02:00
/**
* Delete invoice
*
2015-05-06 00:55:42 +02:00
* @ param int $id Invoice ID
2017-12-28 12:43:26 +01:00
* @ return array
2015-05-05 04:40:28 +02:00
*/
function delete ( $id )
{
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> supprimer ) {
throw new RestException ( 401 );
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
2016-07-29 02:28:51 +02:00
throw new RestException ( 404 , 'Invoice not found' );
2015-05-05 04:40:28 +02:00
}
2017-07-28 10:16:17 +02:00
2016-12-05 13:31:29 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
2015-05-05 04:40:28 +02:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
2017-07-28 10:16:17 +02:00
2016-06-14 19:55:49 +02:00
if ( $this -> invoice -> delete ( $id ) < 0 )
2015-05-05 04:40:28 +02:00
{
throw new RestException ( 500 );
}
2017-07-28 10:16:17 +02:00
2015-05-05 04:40:28 +02:00
return array (
'success' => array (
'code' => 200 ,
2016-12-05 13:31:29 +01:00
'message' => 'Invoice deleted'
2015-05-05 04:40:28 +02:00
)
);
}
2017-07-28 10:16:17 +02:00
2017-09-22 12:18:11 +02:00
/**
2017-09-22 12:24:42 +02:00
* Add a line to a given invoice
2017-10-07 14:11:01 +02:00
*
2018-05-27 15:04:12 +02:00
* Exemple of POST query :
* {
* " desc " : " Desc " , " subprice " : " 1.00000000 " , " qty " : " 1 " , " tva_tx " : " 20.000 " , " localtax1_tx " : " 0.000 " , " localtax2_tx " : " 0.000 " ,
* " fk_product " : " 1 " , " remise_percent " : " 0 " , " date_start " : " " , " date_end " : " " , " fk_code_ventilation " : 0 , " info_bits " : " 0 " ,
* " fk_remise_except " : null , " product_type " : " 1 " , " rang " : " -1 " , " special_code " : " 0 " , " fk_parent_line " : null , " fk_fournprice " : null ,
* " pa_ht " : " 0.00000000 " , " label " : " " , " array_options " : [], " situation_percent " : " 100 " , " fk_prev_id " : null , " fk_unit " : null
* }
2017-09-22 12:08:11 +02:00
*
2017-09-22 12:18:11 +02:00
* @ param int $id Id of invoice
2017-10-22 12:21:44 +02:00
* @ param array $request_data InvoiceLine data
2017-09-22 12:08:11 +02:00
*
2017-10-24 12:49:37 +02:00
* @ url POST { id } / lines
2017-09-22 12:08:11 +02:00
*
2017-09-22 12:18:11 +02:00
* @ return int
2017-12-15 10:00:50 +01:00
*
* @ throws 200
* @ throws 401
* @ throws 404
* @ throws 400
2017-09-22 12:08:11 +02:00
*/
2018-08-15 14:28:34 +02:00
function postLine ( $id , $request_data = null )
{
2018-08-09 11:31:48 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
throw new RestException ( 401 );
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$request_data = ( object ) $request_data ;
// Reset fk_parent_line for no child products and special product
if (( $request_data -> product_type != 9 && empty ( $request_data -> fk_parent_line )) || $request_data -> product_type == 9 ) {
$request_data -> fk_parent_line = 0 ;
}
// calculate pa_ht
$marginInfos = getMarginInfos ( $request_data -> subprice , $request_data -> remise_percent , $request_data -> tva_tx , $request_data -> localtax1_tx , $request_data -> localtax2_tx , $request_data -> fk_fournprice , $request_data -> pa_ht );
$pa_ht = $marginInfos [ 0 ];
$updateRes = $this -> invoice -> addline (
$request_data -> desc ,
$request_data -> subprice ,
$request_data -> qty ,
$request_data -> tva_tx ,
$request_data -> localtax1_tx ,
$request_data -> localtax2_tx ,
$request_data -> fk_product ,
$request_data -> remise_percent ,
$request_data -> date_start ,
$request_data -> date_end ,
$request_data -> fk_code_ventilation ,
$request_data -> info_bits ,
$request_data -> fk_remise_except ,
'HT' ,
0 ,
$request_data -> product_type ,
$request_data -> rang ,
$request_data -> special_code ,
$request_data -> origin ,
$request_data -> origin_id ,
$request_data -> fk_parent_line ,
empty ( $request_data -> fk_fournprice ) ? null : $request_data -> fk_fournprice ,
$pa_ht ,
$request_data -> label ,
$request_data -> array_options ,
$request_data -> situation_percent ,
$request_data -> fk_prev_id ,
$request_data -> fk_unit
);
if ( $updateRes < 0 ) {
2017-12-28 12:43:26 +01:00
throw new RestException ( 400 , 'Unable to insert the new line. Check your inputs. ' . $this -> invoice -> error );
2018-08-09 11:31:48 +02:00
}
2017-12-15 10:00:50 +01:00
2018-08-09 11:31:48 +02:00
return $updateRes ;
2017-09-22 12:08:11 +02:00
}
2017-09-22 12:00:54 +02:00
2018-04-27 10:46:09 +02:00
/**
* Adds a contact to an invoice
*
* @ param int $id Order ID
* @ param int $fk_socpeople Id of thirdparty contact ( if source = 'external' ) or id of user ( if souce = 'internal' ) to link
* @ param string $type_contact Type of contact ( code ) . Must a code found into table llx_c_type_contact . For example : BILLING
* @ param string $source external = Contact extern ( llx_socpeople ), internal = Contact intern ( llx_user )
2018-04-27 11:38:31 +02:00
* @ param int $notrigger Disable all triggers
2018-04-27 10:46:09 +02:00
*
* @ url POST { id } / contacts
*
* @ return array
*
* @ throws 200
* @ throws 304
* @ throws 401
* @ throws 404
* @ throws 500
*
*/
function addContact ( $id , $fk_socpeople , $type_contact , $source , $notrigger = 0 )
{
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
throw new RestException ( 401 );
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$result = $this -> invoice -> add_contact ( $fk_socpeople , $type_contact , $source , $notrigger );
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error : ' . $this -> invoice -> error );
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
return $this -> _cleanObjectDatas ( $this -> invoice );
}
2017-07-28 10:16:17 +02:00
/**
2017-10-02 11:17:46 +02:00
* Sets an invoice as draft
2017-10-02 10:55:34 +02:00
*
* @ param int $id Order ID
* @ param int $idwarehouse Warehouse ID
*
2017-10-02 11:51:54 +02:00
* @ url POST { id } / settodraft
2017-10-02 10:55:34 +02:00
*
* @ return array
2017-10-07 14:11:01 +02:00
*
2017-10-02 10:55:34 +02:00
* @ throws 200
* @ throws 304
* @ throws 401
* @ throws 404
* @ throws 500
2017-10-07 14:11:01 +02:00
*
2017-10-02 10:55:34 +02:00
*/
2017-10-02 11:51:54 +02:00
function settodraft ( $id , $idwarehouse =- 1 )
2017-10-02 10:55:34 +02:00
{
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 );
2017-10-02 10:55:34 +02:00
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 404 , 'Invoice not found' );
2017-10-02 10:55:34 +02:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2017-10-02 10:55:34 +02:00
}
$result = $this -> invoice -> set_draft ( DolibarrApiAccess :: $user , $idwarehouse );
if ( $result == 0 ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 304 , 'Nothing done.' );
2017-10-02 10:55:34 +02:00
}
if ( $result < 0 ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 500 , 'Error : ' . $this -> invoice -> error );
2017-10-02 10:55:34 +02:00
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
return $this -> _cleanObjectDatas ( $this -> invoice );
}
/**
* Validate an invoice
2017-12-28 12:43:26 +01:00
*
* If you get a bad value for param notrigger check that ou provide this in body
* {
* " idwarehouse " : 0 ,
* " notrigger " : 0
* }
2017-07-28 10:16:17 +02:00
*
2017-10-19 12:35:15 +02:00
* @ param int $id Invoice ID
2017-07-28 10:16:17 +02:00
* @ param int $idwarehouse Warehouse ID
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
*
* @ url POST { id } / validate
*
* @ return array
*/
function validate ( $id , $idwarehouse = 0 , $notrigger = 0 )
{
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
throw new RestException ( 401 );
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$result = $this -> invoice -> validate ( DolibarrApiAccess :: $user , '' , $idwarehouse , $notrigger );
if ( $result == 0 ) {
2017-10-02 10:55:34 +02:00
throw new RestException ( 304 , 'Error nothing done. May be object is already validated' );
2017-07-28 10:16:17 +02:00
}
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error when validating Invoice: ' . $this -> invoice -> error );
}
2017-10-02 10:55:34 +02:00
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
return $this -> _cleanObjectDatas ( $this -> invoice );
2017-07-28 10:16:17 +02:00
}
2017-10-02 11:17:46 +02:00
/**
* Sets an invoice as paid
*
* @ param int $id Order ID
* @ param string $close_code Code renseigne si on classe a payee completement alors que paiement incomplet ( cas escompte par exemple )
* @ param string $close_note Commentaire renseigne si on classe a payee alors que paiement incomplet ( cas escompte par exemple )
*
2017-10-02 11:51:54 +02:00
* @ url POST { id } / settopaid
2017-10-02 11:17:46 +02:00
*
* @ return array An invoice object
*
* @ throws 200
* @ throws 304
* @ throws 401
* @ throws 404
* @ throws 500
*/
2017-10-02 11:51:54 +02:00
function settopaid ( $id , $close_code = '' , $close_note = '' )
2017-10-02 11:17:46 +02:00
{
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 );
2017-10-02 11:17:46 +02:00
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 404 , 'Invoice not found' );
2017-10-02 11:17:46 +02:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2017-10-02 11:17:46 +02:00
}
$result = $this -> invoice -> set_paid ( DolibarrApiAccess :: $user , $close_code , $close_note );
if ( $result == 0 ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 304 , 'Error nothing done. May be object is already validated' );
2017-10-02 11:17:46 +02:00
}
if ( $result < 0 ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 500 , 'Error : ' . $this -> invoice -> error );
2017-10-02 11:17:46 +02:00
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
return $this -> _cleanObjectDatas ( $this -> invoice );
2017-12-28 12:43:26 +01:00
}
2017-10-02 11:17:46 +02:00
2017-12-28 12:43:26 +01:00
/**
* Sets an invoice as unpaid
*
* @ param int $id Order ID
*
* @ url POST { id } / settounpaid
*
* @ return array An invoice object
*
* @ throws 200
* @ throws 304
* @ throws 401
* @ throws 404
* @ throws 500
*/
function settounpaid ( $id )
{
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 );
2017-12-28 12:43:26 +01:00
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 404 , 'Invoice not found' );
2017-12-28 12:43:26 +01:00
}
2017-10-02 11:17:46 +02:00
2017-12-28 12:43:26 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2017-12-28 12:43:26 +01:00
}
2017-10-02 11:17:46 +02:00
2017-12-28 12:43:26 +01:00
$result = $this -> invoice -> set_unpaid ( DolibarrApiAccess :: $user );
if ( $result == 0 ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 304 , 'Nothing done' );
2017-12-28 12:43:26 +01:00
}
if ( $result < 0 ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 500 , 'Error : ' . $this -> invoice -> error );
2017-12-28 12:43:26 +01:00
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $this -> invoice -> id )) {
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
return $this -> _cleanObjectDatas ( $this -> invoice );
}
2018-01-04 21:14:45 +01:00
2017-10-25 14:41:12 +02:00
/**
2017-11-10 10:15:50 +01:00
* Add a discount line into an invoice ( as an invoice line ) using an existing absolute discount
*
* Note that this consume the discount .
2017-10-25 14:41:12 +02:00
*
* @ param int $id Id of invoice
2017-10-27 00:36:06 +02:00
* @ param int $discountid Id of discount
2017-10-25 14:41:12 +02:00
*
2017-10-27 00:36:06 +02:00
* @ url POST { id } / usediscount / { discountid }
2017-10-25 14:41:12 +02:00
*
* @ return int
* @ throws 400
* @ throws 401
* @ throws 404
* @ throws 405
*/
2018-08-15 14:28:34 +02:00
function useDiscount ( $id , $discountid )
{
2017-10-25 14:41:12 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 );
2017-10-25 14:41:12 +02:00
}
if ( empty ( $id )) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 400 , 'Invoice ID is mandatory' );
2017-10-25 14:41:12 +02:00
}
2017-10-27 00:36:06 +02:00
if ( empty ( $discountid )) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 400 , 'Discount ID is mandatory' );
2017-10-25 14:41:12 +02:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $id )) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2017-10-25 14:41:12 +02:00
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 404 , 'Invoice not found' );
2017-10-25 14:41:12 +02:00
}
2017-10-27 00:36:06 +02:00
$result = $this -> invoice -> insert_discount ( $discountid );
2017-10-25 14:41:12 +02:00
if ( $result < 0 ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 405 , $this -> invoice -> error );
2017-10-25 14:41:12 +02:00
}
return $result ;
}
2017-10-25 17:09:37 +02:00
/**
2017-11-10 10:15:50 +01:00
* Add an available credit note discount to payments of an existing invoice .
*
* Note that this consume the credit note .
2017-10-25 17:09:37 +02:00
*
* @ param int $id Id of invoice
2017-10-27 00:36:06 +02:00
* @ param int $discountid Id of a discount coming from a credit note
2017-10-25 17:09:37 +02:00
*
2017-10-27 10:02:17 +02:00
* @ url POST { id } / usecreditnote / { discountid }
2017-10-25 17:09:37 +02:00
*
* @ return int
* @ throws 400
* @ throws 401
* @ throws 404
* @ throws 405
*/
2018-08-15 14:28:34 +02:00
function useCreditNote ( $id , $discountid )
{
2017-11-02 16:01:13 +01:00
2017-10-25 17:09:37 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/discount.class.php' ;
2017-11-02 16:01:13 +01:00
2017-10-25 17:09:37 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 );
2017-10-25 17:09:37 +02:00
}
if ( empty ( $id )) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 400 , 'Invoice ID is mandatory' );
2017-10-25 17:09:37 +02:00
}
2017-10-27 10:02:17 +02:00
if ( empty ( $discountid )) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 400 , 'Credit ID is mandatory' );
2017-10-25 17:09:37 +02:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $id )) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2017-10-25 17:09:37 +02:00
}
$discount = new DiscountAbsolute ( $this -> db );
2017-10-27 00:36:06 +02:00
$result = $discount -> fetch ( $discountid );
2017-10-25 17:09:37 +02:00
if ( ! $result ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 404 , 'Credit not found' );
2017-10-25 17:09:37 +02:00
}
$result = $discount -> link_to_invoice ( 0 , $id );
if ( $result < 0 ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 405 , $discount -> error );
2017-10-25 17:09:37 +02:00
}
return $result ;
}
2017-10-24 10:50:38 +02:00
/**
2017-10-27 00:36:06 +02:00
* Get list of payments of a given invoice
2017-10-24 10:50:38 +02:00
*
* @ param int $id Id of invoice
*
2017-11-02 16:01:13 +01:00
* @ url GET { id } / payments
2017-10-24 10:50:38 +02:00
*
* @ return array
* @ throws 400
* @ throws 401
* @ throws 404
* @ throws 405
*/
2018-08-15 14:28:34 +02:00
function getPayments ( $id )
{
2017-10-24 10:50:38 +02:00
2017-10-24 11:25:31 +02:00
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> lire ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 );
2017-10-24 10:50:38 +02:00
}
if ( empty ( $id )) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 400 , 'Invoice ID is mandatory' );
2017-10-24 10:50:38 +02:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $id )) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 401 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2017-10-24 10:50:38 +02:00
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 404 , 'Invoice not found' );
2017-10-24 10:50:38 +02:00
}
$result = $this -> invoice -> getListOfPayments ();
if ( $result < 0 ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 405 , $this -> invoice -> error );
2017-10-24 10:50:38 +02:00
}
2017-11-02 16:01:13 +01:00
2017-10-24 10:50:38 +02:00
return $result ;
}
2018-04-27 11:38:31 +02:00
/**
* Add payment line to a specific invoice with the remain to pay as amount .
*
* @ param int $id Id of invoice
* @ param string $datepaye { @ from body } Payment date { @ type timestamp }
* @ param int $paiementid { @ from body } Payment mode Id { @ min 1 }
* @ param string $closepaidinvoices { @ from body } Close paid invoices { @ choice yes , no }
* @ param int $accountid { @ from body } Account Id { @ min 1 }
* @ param string $num_paiement { @ from body } Payment number ( optional )
* @ param string $comment { @ from body } Note ( optional )
* @ param string $chqemetteur { @ from body } Payment issuer ( mandatory if paiementcode = 'CHQ' )
* @ param string $chqbank { @ from body } Issuer bank name ( optional )
2017-11-02 15:01:38 +01:00
*
2018-04-27 11:38:31 +02:00
* @ url POST { id } / payments
2017-11-02 15:01:38 +01:00
*
2018-04-27 11:38:31 +02:00
* @ return int Payment ID
* @ throws 400
* @ throws 401
* @ throws 404
*/
2018-08-15 14:28:34 +02:00
function addPayment ( $id , $datepaye , $paiementid , $closepaidinvoices , $accountid , $num_paiement = '' , $comment = '' , $chqemetteur = '' , $chqbank = '' )
{
global $conf ;
2018-04-27 11:38:31 +02:00
require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php' ;
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
throw new RestException ( 403 );
}
if ( empty ( $id )) {
throw new RestException ( 400 , 'Invoice ID is mandatory' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $id )) {
throw new RestException ( 403 , '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 ( empty ( $paiementid )) {
throw new RestException ( 400 , 'Paiement ID or Paiement Code is mandatory' );
}
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Invoice not found' );
}
// 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' );
$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
$paiement = new Paiement ( $this -> db );
$paiement -> datepaye = $datepaye ;
$paiement -> amounts = $amounts ; // Array with all payments dispatching with invoice id
$paiement -> multicurrency_amounts = $multicurrency_amounts ; // Array with all payments dispatching
$paiement -> paiementid = $paiementid ;
$paiement -> paiementcode = dol_getIdFromCode ( $this -> db , $paiementid , 'c_paiement' , 'id' , 'code' , 1 );
$paiement -> num_paiement = $num_paiement ;
$paiement -> note = $comment ;
$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 );
}
if ( ! empty ( $conf -> banque -> enabled )) {
$label = '(CustomerInvoicePayment)' ;
if ( $paiement -> paiementcode == 'CHQ' && empty ( $chqemetteur )) {
throw new RestException ( 400 , 'Emetteur is mandatory when payment code is ' . $paiement -> paiementcode );
}
if ( $this -> invoice -> type == Facture :: TYPE_CREDIT_NOTE ) $label = '(CustomerInvoicePaymentBack)' ; // Refund of a credit note
$result = $paiement -> addPaymentToBank ( DolibarrApiAccess :: $user , 'payment' , $label , $accountid , $chqemetteur , $chqbank );
if ( $result < 0 )
{
$this -> db -> rollback ();
throw new RestException ( 400 , 'Add payment to bank error : ' . $paiement -> error );
}
}
$this -> db -> commit ();
return $paiement_id ;
}
/**
* Add a payment to pay partially or completely one or several invoices .
* Warning : Take care that all invoices are owned by the same customer .
2018-04-29 18:06:31 +02:00
* Example of value for parameter arrayofamounts : { " 1 " : " 99.99 " , " 2 " : " 10 " }
2018-04-27 11:38:31 +02:00
*
2018-05-16 15:23:52 +02:00
* @ param array $arrayofamounts { @ from body } Array with id of invoices with amount to pay for each invoice
2018-04-27 11:38:31 +02:00
* @ param string $datepaye { @ from body } Payment date { @ type timestamp }
* @ param int $paiementid { @ from body } Payment mode Id { @ min 1 }
2017-11-02 15:01:38 +01:00
* @ param string $closepaidinvoices { @ from body } Close paid invoices { @ choice yes , no }
* @ param int $accountid { @ from body } Account Id { @ min 1 }
* @ param string $num_paiement { @ from body } Payment number ( optional )
* @ param string $comment { @ from body } Note ( optional )
* @ param string $chqemetteur { @ from body } Payment issuer ( mandatory if paiementcode = 'CHQ' )
* @ param string $chqbank { @ from body } Issuer bank name ( optional )
*
2018-04-27 11:38:31 +02:00
* @ url POST / paymentsdistributed
2017-11-02 15:01:38 +01:00
*
2018-04-26 12:03:55 +02:00
* @ return int Payment ID
2017-11-02 15:01:38 +01:00
* @ throws 400
* @ throws 401
2018-04-27 11:38:31 +02:00
* @ throws 403
2017-11-02 15:01:38 +01:00
* @ throws 404
*/
2018-04-27 11:38:31 +02:00
function addPaymentDistributed ( $arrayofamounts , $datepaye , $paiementid , $closepaidinvoices , $accountid , $num_paiement = '' , $comment = '' , $chqemetteur = '' , $chqbank = '' )
{
2017-11-02 15:01:38 +01:00
global $conf ;
2018-04-27 11:38:31 +02:00
2017-11-02 15:01:38 +01:00
require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php' ;
2018-04-27 11:38:31 +02:00
2017-11-02 15:01:38 +01:00
if ( ! DolibarrApiAccess :: $user -> rights -> facture -> creer ) {
2018-08-09 11:31:48 +02:00
throw new RestException ( 403 );
2017-11-02 15:01:38 +01:00
}
2018-04-27 11:38:31 +02:00
foreach ( $arrayofamounts as $id => $amount ) {
if ( empty ( $id )) {
throw new RestException ( 400 , 'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'facture' , $id )) {
throw new RestException ( 403 , 'Access not allowed on invoice ID ' . $id . ' for login ' . DolibarrApiAccess :: $user -> login );
}
}
if ( ! empty ( $conf -> banque -> enabled )) {
if ( empty ( $accountid )) {
throw new RestException ( 400 , 'Account ID is mandatory' );
}
}
if ( empty ( $paiementid )) {
throw new RestException ( 400 , 'Paiement ID or Paiement Code is mandatory' );
}
$this -> db -> begin ();
$amounts = array ();
$multicurrency_amounts = array ();
// Loop on each invoice to pay
foreach ( $arrayofamounts as $id => $amount )
{
$result = $this -> invoice -> fetch ( $id );
if ( ! $result ) {
$this -> db -> rollback ();
throw new RestException ( 404 , 'Invoice ID ' . $id . ' not found' );
}
// 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' );
if ( $amount != 'remain' )
{
if ( $amount > $resteapayer )
{
$this -> db -> rollback ();
throw new RestException ( 400 , 'Payment amount on invoice ID ' . $id . ' (' . $amount . ') is higher than remain to pay (' . $resteapayer . ')' );
}
$resteapayer = $amount ;
}
2018-04-27 10:46:09 +02:00
// 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 ;
}
2018-04-27 11:38:31 +02:00
}
2017-11-02 15:01:38 +01:00
// Creation of payment line
$paiement = new Paiement ( $this -> db );
$paiement -> datepaye = $datepaye ;
$paiement -> amounts = $amounts ; // Array with all payments dispatching with invoice id
$paiement -> multicurrency_amounts = $multicurrency_amounts ; // Array with all payments dispatching
$paiement -> paiementid = $paiementid ;
$paiement -> paiementcode = dol_getIdFromCode ( $this -> db , $paiementid , 'c_paiement' , 'id' , 'code' , 1 );
$paiement -> num_paiement = $num_paiement ;
$paiement -> note = $comment ;
$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 );
}
if ( ! empty ( $conf -> banque -> enabled )) {
$label = '(CustomerInvoicePayment)' ;
if ( $paiement -> paiementcode == 'CHQ' && empty ( $chqemetteur )) {
throw new RestException ( 400 , 'Emetteur is mandatory when payment code is ' . $paiement -> paiementcode );
}
if ( $this -> invoice -> type == Facture :: TYPE_CREDIT_NOTE ) $label = '(CustomerInvoicePaymentBack)' ; // Refund of a credit note
$result = $paiement -> addPaymentToBank ( DolibarrApiAccess :: $user , 'payment' , $label , $accountid , $chqemetteur , $chqbank );
if ( $result < 0 )
{
$this -> db -> rollback ();
throw new RestException ( 400 , 'Add payment to bank error : ' . $paiement -> error );
}
}
2018-04-27 11:38:31 +02:00
2017-11-02 15:01:38 +01:00
$this -> db -> commit ();
2018-04-27 11:38:31 +02:00
2017-11-02 15:01:38 +01:00
return $paiement_id ;
}
2017-07-28 10:16:17 +02:00
/**
* Clean sensible object datas
*
* @ param object $object Object to clean
* @ return array Array of cleaned object properties
*/
2018-08-15 14:28:34 +02:00
function _cleanObjectDatas ( $object )
{
2017-07-28 10:16:17 +02:00
2018-08-15 14:28:34 +02:00
$object = parent :: _cleanObjectDatas ( $object );
2017-07-28 10:16:17 +02:00
2017-11-06 11:06:31 +01:00
unset ( $object -> note );
2018-08-15 14:28:34 +02:00
unset ( $object -> address );
unset ( $object -> barcode_type );
unset ( $object -> barcode_type_code );
unset ( $object -> barcode_type_label );
unset ( $object -> barcode_type_coder );
2017-07-28 10:16:17 +02:00
2018-08-15 14:28:34 +02:00
return $object ;
2017-07-28 10:16:17 +02:00
}
2015-05-05 04:40:28 +02:00
/**
* Validate fields before create or update object
2017-07-28 10:16:17 +02:00
*
2016-12-07 19:12:25 +01:00
* @ param array | null $data Datas to validate
2015-05-05 04:40:28 +02:00
* @ return array
2017-07-28 10:16:17 +02:00
*
2015-05-05 04:40:28 +02:00
* @ throws RestException
*/
function _validate ( $data )
{
$invoice = array ();
2016-06-14 19:55:49 +02:00
foreach ( Invoices :: $FIELDS as $field ) {
2018-08-15 14:28:34 +02:00
if ( ! isset ( $data [ $field ])) {
2015-05-05 04:40:28 +02:00
throw new RestException ( 400 , " $field field missing " );
2018-08-15 14:28:34 +02:00
}
2015-05-05 04:40:28 +02:00
$invoice [ $field ] = $data [ $field ];
}
return $invoice ;
}
}