19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
22 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/paiementfourn.class.php';
37 public static $FIELDS = array(
66 public function get($id)
68 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->lire) {
69 throw new RestException(401);
72 $result = $this->invoice->fetch($id);
74 throw new RestException(404,
'Supplier invoice not found');
78 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
81 $this->invoice->fetchObjectLinked();
101 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'')
105 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->lire) {
106 throw new RestException(401);
112 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
116 if (!DolibarrApiAccess::$user->rights->societe->client->voir) {
117 $search_sale = DolibarrApiAccess::$user->id;
120 $sql =
"SELECT t.rowid";
122 if (!DolibarrApiAccess::$user->rights->societe->client->voir || $search_sale > 0) {
123 $sql .=
", sc.fk_soc, sc.fk_user";
125 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as t";
128 if (!DolibarrApiAccess::$user->rights->societe->client->voir || $search_sale > 0) {
129 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
132 $sql .=
' WHERE t.entity IN ('.getEntity(
'supplier_invoice').
')';
133 if (!DolibarrApiAccess::$user->rights->societe->client->voir || $search_sale > 0) {
134 $sql .=
" AND t.fk_soc = sc.fk_soc";
137 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
139 if ($search_sale > 0) {
140 $sql .=
" AND t.rowid = sc.fk_soc";
144 if ($status ==
'draft') {
145 $sql .=
" AND t.fk_statut IN (0)";
147 if ($status ==
'unpaid') {
148 $sql .=
" AND t.fk_statut IN (1)";
150 if ($status ==
'paid') {
151 $sql .=
" AND t.fk_statut IN (2)";
153 if ($status ==
'cancelled') {
154 $sql .=
" AND t.fk_statut IN (3)";
157 if ($search_sale > 0) {
158 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
164 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
166 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
167 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
170 $sql .= $this->
db->order($sortfield, $sortorder);
175 $offset = $limit * $page;
177 $sql .= $this->
db->plimit($limit + 1, $offset);
180 $result = $this->
db->query($sql);
183 $num = $this->
db->num_rows($result);
184 $min = min($num, ($limit <= 0 ? $num : $limit));
186 $obj = $this->
db->fetch_object($result);
188 if ($invoice_static->fetch($obj->rowid)) {
194 throw new RestException(503,
'Error when retrieve supplier invoice list : '.$this->
db->lasterror());
196 if (!count($obj_ret)) {
197 throw new RestException(404,
'No supplier invoice found');
216 public function post($request_data =
null)
218 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
219 throw new RestException(401,
"Insuffisant rights");
222 $result = $this->
_validate($request_data);
224 foreach ($request_data as $field => $value) {
225 $this->invoice->$field = $value;
227 if (!array_key_exists(
'date', $request_data)) {
228 $this->invoice->date =
dol_now();
231 if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
232 throw new RestException(500,
"Error creating order", array_merge(array($this->invoice->error), $this->invoice->errors));
234 return $this->invoice->id;
248 public function put($id, $request_data =
null)
250 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
251 throw new RestException(401);
254 $result = $this->invoice->fetch($id);
256 throw new RestException(404,
'Supplier invoice not found');
260 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
263 foreach ($request_data as $field => $value) {
264 if ($field ==
'id') {
267 $this->invoice->$field = $value;
270 if ($this->invoice->update($id, DolibarrApiAccess::$user)) {
271 return $this->
get($id);
288 public function delete($id)
290 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->supprimer) {
291 throw new RestException(401);
293 $result = $this->invoice->fetch($id);
295 throw new RestException(404,
'Supplier invoice not found');
299 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
302 if ($this->invoice->delete(DolibarrApiAccess::$user) < 0) {
303 throw new RestException(500,
'Error when deleting invoice');
309 'message' =>
'Supplier invoice deleted'
331 public function validate($id, $idwarehouse = 0, $notrigger = 0)
333 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
334 throw new RestException(401);
336 $result = $this->invoice->fetch($id);
338 throw new RestException(404,
'Invoice not found');
342 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
345 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
347 throw new RestException(304,
'Error nothing done. The invoice is already validated');
350 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
356 'message' =>
'Invoice validated (Ref='.$this->invoice->ref.
')'
376 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->lire) {
377 throw new RestException(401);
380 throw new RestException(400,
'Invoice ID is mandatory');
384 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
387 $result = $this->invoice->fetch($id);
389 throw new RestException(404,
'Invoice not found');
392 $result = $this->invoice->getListOfPayments();
394 throw new RestException(405, $this->invoice->error);
421 public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
425 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
426 throw new RestException(403);
429 throw new RestException(400,
'Invoice ID is mandatory');
433 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
437 if (empty($accountid)) {
438 throw new RestException(400,
'Bank account ID is mandatory');
442 if (empty($payment_mode_id)) {
443 throw new RestException(400,
'Payment mode ID is mandatory');
447 $result = $this->invoice->fetch($id);
449 throw new RestException(404,
'Invoice not found');
453 $totalpaid = $this->invoice->getSommePaiement();
454 $totaldeposits = $this->invoice->getSumDepositsUsed();
455 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits,
'MT');
460 $multicurrency_amounts = array();
462 $resteapayer =
price2num($resteapayer,
'MT');
463 $amounts[$id] = $resteapayer;
466 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
467 $multicurrency_amounts[$id] = $newvalue;
471 $paiement->datepaye = $datepaye;
472 $paiement->amounts = $amounts;
473 $paiement->multicurrency_amounts = $multicurrency_amounts;
474 $paiement->paiementid = $payment_mode_id;
475 $paiement->paiementcode =
dol_getIdFromCode($this->
db, $payment_mode_id,
'c_paiement',
'id',
'code', 1);
476 $paiement->num_payment = $num_payment;
477 $paiement->note_public = $comment;
479 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
480 if ($paiement_id < 0) {
481 $this->
db->rollback();
482 throw new RestException(400,
'Payment error : '.$paiement->error);
486 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
488 $this->
db->rollback();
489 throw new RestException(400,
'Add payment to bank error : '.$paiement->error);
509 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
510 throw new RestException(401);
513 $result = $this->invoice->fetch($id);
515 throw new RestException(404,
'Supplier invoice not found');
519 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
521 $this->invoice->fetch_lines();
523 foreach ($this->invoice->lines as $line) {
543 public function postLine($id, $request_data =
null)
545 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
546 throw new RestException(401);
549 $result = $this->invoice->fetch($id);
551 throw new RestException(404,
'Supplier invoice not found');
555 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
558 $request_data = (object) $request_data;
560 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
561 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
563 $updateRes = $this->invoice->addline(
564 $request_data->description,
565 $request_data->pu_ht,
566 $request_data->tva_tx,
567 $request_data->localtax1_tx,
568 $request_data->localtax2_tx,
570 $request_data->fk_product,
571 $request_data->remise_percent,
572 $request_data->date_start,
573 $request_data->date_end,
574 $request_data->ventil,
575 $request_data->info_bits,
576 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
577 $request_data->product_type,
580 $request_data->array_options,
581 $request_data->fk_unit,
582 $request_data->origin_id,
583 $request_data->multicurrency_subprice,
584 $request_data->ref_supplier,
585 $request_data->special_code
588 if ($updateRes < 0) {
589 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
610 public function putLine($id, $lineid, $request_data =
null)
612 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
613 throw new RestException(401);
616 $result = $this->invoice->fetch($id);
618 throw new RestException(404,
'Supplier invoice not found');
622 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
625 $request_data = (object) $request_data;
627 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
628 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
630 $updateRes = $this->invoice->updateline(
632 $request_data->description,
633 $request_data->pu_ht,
634 $request_data->tva_tx,
635 $request_data->localtax1_tx,
636 $request_data->localtax2_tx,
638 $request_data->fk_product,
639 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
640 $request_data->info_bits,
641 $request_data->product_type,
642 $request_data->remise_percent,
644 $request_data->date_start,
645 $request_data->date_end,
646 $request_data->array_options,
647 $request_data->fk_unit,
648 $request_data->multicurrency_subprice,
649 $request_data->ref_supplier,
653 if ($updateRes > 0) {
654 $result = $this->
get($id);
655 unset($result->line);
658 throw new RestException(304, $this->invoice->error);
679 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
680 throw new RestException(401);
683 $result = $this->invoice->fetch($id);
685 throw new RestException(404,
'Supplier invoice not found');
688 if (empty($lineid)) {
689 throw new RestException(400,
'Line ID is mandatory');
693 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
698 $updateRes = $this->invoice->deleteline($lineid);
699 if ($updateRes > 0) {
700 return $this->
get($id);
702 throw new RestException(405, $this->invoice->error);
716 $object = parent::_cleanObjectDatas($object);
718 unset($object->rowid);
719 unset($object->barcode_type);
720 unset($object->barcode_type_code);
721 unset($object->barcode_type_label);
722 unset($object->barcode_type_coder);
738 foreach (SupplierInvoices::$FIELDS as $field) {
739 if (!isset($data[$field])) {
740 throw new RestException(400,
"$field field missing");
742 $invoice[$field] = $data[$field];
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Class to manage suppliers invoices.
Class to manage payments for supplier invoices.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an invoice.
addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment='', $comment='', $chqemetteur='', $chqbank='')
Add payment line to a specific supplier invoice with the remain to pay as amount.
deleteLine($id, $lineid)
Deletes a line of a given supplier invoice.
getLines($id)
Get lines of a supplier invoice.
getPayments($id)
Get list of payments of a given supplier invoice.
_cleanObjectDatas($object)
Clean sensible object datas.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='')
List invoices.
post($request_data=null)
Create supplier invoice object.
postLine($id, $request_data=null)
Add a line to given supplier invoice.
put($id, $request_data=null)
Update supplier invoice.
_validate($data)
Validate fields before create or update object.
putLine($id, $lineid, $request_data=null)
Update a line to a given supplier invoice.
__construct()
Constructor.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_now($mode='auto')
Return date for now.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.
$conf db
API class for accounts.