19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
22require_once DOL_DOCUMENT_ROOT .
'/fourn/class/paiementfourn.class.php';
37 public static $FIELDS = array(
66 public function get($id)
68 if (!DolibarrApiAccess::$user->hasRight(
"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();
102 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'')
106 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
107 throw new RestException(401);
113 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
117 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir")) {
118 $search_sale = DolibarrApiAccess::$user->id;
121 $sql =
"SELECT t.rowid";
123 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir") || $search_sale > 0) {
124 $sql .=
", sc.fk_soc, sc.fk_user";
126 $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)";
129 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir") || $search_sale > 0) {
130 $sql .=
", " . MAIN_DB_PREFIX .
"societe_commerciaux as sc";
133 $sql .=
' WHERE t.entity IN (' .
getEntity(
'supplier_invoice') .
')';
134 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir") || $search_sale > 0) {
135 $sql .=
" AND t.fk_soc = sc.fk_soc";
138 $sql .=
" AND t.fk_soc IN (" . $this->db->sanitize($socids) .
")";
140 if ($search_sale > 0) {
141 $sql .=
" AND t.rowid = sc.fk_soc";
145 if ($status ==
'draft') {
146 $sql .=
" AND t.fk_statut IN (0)";
148 if ($status ==
'unpaid') {
149 $sql .=
" AND t.fk_statut IN (1)";
151 if ($status ==
'paid') {
152 $sql .=
" AND t.fk_statut IN (2)";
154 if ($status ==
'cancelled') {
155 $sql .=
" AND t.fk_statut IN (3)";
158 if ($search_sale > 0) {
159 $sql .=
" AND sc.fk_user = " . ((int) $search_sale);
166 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
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());
214 public function post($request_data =
null)
216 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
217 throw new RestException(401,
"Insuffisant rights");
220 $result = $this->
_validate($request_data);
222 foreach ($request_data as $field => $value) {
223 if ($field ===
'caller') {
225 $this->invoice->context[
'caller'] = $request_data[
'caller'];
229 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
231 if (!array_key_exists(
'date', $request_data)) {
232 $this->invoice->date =
dol_now();
235 if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
236 throw new RestException(500,
"Error creating invoice ", array_merge(array($this->invoice->error), $this->invoice->errors));
238 return $this->invoice->id;
252 public function put($id, $request_data =
null)
254 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
255 throw new RestException(401);
258 $result = $this->invoice->fetch($id);
260 throw new RestException(404,
'Supplier invoice not found');
264 throw new RestException(401,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
267 foreach ($request_data as $field => $value) {
268 if ($field ==
'id') {
271 if ($field ===
'caller') {
273 $this->invoice->context[
'caller'] = $request_data[
'caller'];
277 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
280 if ($this->invoice->update($id, DolibarrApiAccess::$user)) {
281 return $this->
get($id);
298 public function delete($id)
300 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"supprimer")) {
301 throw new RestException(401);
303 $result = $this->invoice->fetch($id);
305 throw new RestException(404,
'Supplier invoice not found');
309 throw new RestException(401,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
312 if ($this->invoice->delete(DolibarrApiAccess::$user) < 0) {
313 throw new RestException(500,
'Error when deleting invoice');
319 'message' =>
'Supplier invoice deleted'
341 public function validate($id, $idwarehouse = 0, $notrigger = 0)
343 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
344 throw new RestException(401);
346 $result = $this->invoice->fetch($id);
348 throw new RestException(404,
'Invoice not found');
352 throw new RestException(401,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
355 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
357 throw new RestException(304,
'Error nothing done. The invoice is already validated');
360 throw new RestException(500,
'Error when validating Invoice: ' . $this->invoice->error);
366 'message' =>
'Invoice validated (Ref=' . $this->invoice->ref .
')'
386 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
387 throw new RestException(401);
390 throw new RestException(400,
'Invoice ID is mandatory');
393 $result = $this->invoice->fetch($id);
395 throw new RestException(404,
'Invoice not found');
399 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
402 $result = $this->invoice->getListOfPayments();
404 throw new RestException(405, $this->invoice->error);
432 public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $amount =
null)
436 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
437 throw new RestException(403);
440 throw new RestException(400,
'Invoice ID is mandatory');
443 $result = $this->invoice->fetch($id);
445 throw new RestException(404,
'Invoice not found');
449 throw new RestException(401,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
452 if (isModEnabled(
"banque")) {
453 if (empty($accountid)) {
454 throw new RestException(400,
'Bank account ID is mandatory');
458 if (empty($payment_mode_id)) {
459 throw new RestException(400,
'Payment mode ID is mandatory');
462 if (
null !== $amount && $amount > 0) {
464 $paymentamount = $amount;
467 $totalpaid = $this->invoice->getSommePaiement();
468 $totaldeposits = $this->invoice->getSumDepositsUsed();
469 $paymentamount =
price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits,
'MT');
475 $multicurrency_amounts = array();
477 $paymentamount =
price2num($paymentamount,
'MT');
479 $amounts[$id] = $paymentamount;
482 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
483 $multicurrency_amounts[$id] = $newvalue;
487 $paiement->datepaye = $datepaye;
488 $paiement->amounts = $amounts;
489 $paiement->multicurrency_amounts = $multicurrency_amounts;
490 $paiement->paiementid = $payment_mode_id;
491 $paiement->paiementcode =
dol_getIdFromCode($this->db, $payment_mode_id,
'c_paiement',
'id',
'code', 1);
492 $paiement->num_payment = $num_payment;
493 $paiement->note_public = $comment;
495 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
496 if ($paiement_id < 0) {
497 $this->db->rollback();
498 throw new RestException(400,
'Payment error : ' . $paiement->error);
501 if (isModEnabled(
"banque")) {
502 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
504 $this->db->rollback();
505 throw new RestException(400,
'Add payment to bank error : ' . $paiement->error);
525 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
526 throw new RestException(401);
529 $result = $this->invoice->fetch($id);
531 throw new RestException(404,
'Supplier invoice not found');
535 throw new RestException(401,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
537 $this->invoice->fetch_lines();
539 foreach ($this->invoice->lines as $line) {
559 public function postLine($id, $request_data =
null)
561 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
562 throw new RestException(401);
565 $result = $this->invoice->fetch($id);
567 throw new RestException(404,
'Supplier invoice not found');
571 throw new RestException(401,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
574 $request_data = (object) $request_data;
576 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
577 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
579 $updateRes = $this->invoice->addline(
580 $request_data->description,
581 $request_data->pu_ht,
582 $request_data->tva_tx,
583 $request_data->localtax1_tx,
584 $request_data->localtax2_tx,
586 $request_data->fk_product,
587 $request_data->remise_percent,
588 $request_data->date_start,
589 $request_data->date_end,
590 $request_data->ventil,
591 $request_data->info_bits,
592 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
593 $request_data->product_type,
596 $request_data->array_options,
597 $request_data->fk_unit,
598 $request_data->origin_id,
599 $request_data->multicurrency_subprice,
600 $request_data->ref_supplier,
601 $request_data->special_code
604 if ($updateRes < 0) {
605 throw new RestException(400,
'Unable to insert the new line. Check your inputs. ' . $this->invoice->error);
626 public function putLine($id, $lineid, $request_data =
null)
628 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
629 throw new RestException(401);
632 $result = $this->invoice->fetch($id);
634 throw new RestException(404,
'Supplier invoice not found');
638 throw new RestException(401,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
641 $request_data = (object) $request_data;
643 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
644 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
646 $updateRes = $this->invoice->updateline(
648 $request_data->description,
649 $request_data->pu_ht,
650 $request_data->tva_tx,
651 $request_data->localtax1_tx,
652 $request_data->localtax2_tx,
654 $request_data->fk_product,
655 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
656 $request_data->info_bits,
657 $request_data->product_type,
658 $request_data->remise_percent,
660 $request_data->date_start,
661 $request_data->date_end,
662 $request_data->array_options,
663 $request_data->fk_unit,
664 $request_data->multicurrency_subprice,
665 $request_data->ref_supplier,
669 if ($updateRes > 0) {
670 $result = $this->
get($id);
671 unset($result->line);
674 throw new RestException(304, $this->invoice->error);
695 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
696 throw new RestException(401);
699 $result = $this->invoice->fetch($id);
701 throw new RestException(404,
'Supplier invoice not found');
704 if (empty($lineid)) {
705 throw new RestException(400,
'Line ID is mandatory');
709 throw new RestException(401,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
714 $updateRes = $this->invoice->deleteline($lineid);
715 if ($updateRes > 0) {
716 return $this->
get($id);
718 throw new RestException(405, $this->invoice->error);
732 $object = parent::_cleanObjectDatas($object);
734 unset($object->rowid);
735 unset($object->barcode_type);
736 unset($object->barcode_type_code);
737 unset($object->barcode_type_label);
738 unset($object->barcode_type_coder);
754 foreach (SupplierInvoices::$FIELDS as $field) {
755 if (!isset($data[$field])) {
756 throw new RestException(400,
"$field field missing");
758 $invoice[$field] = $data[$field];
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage suppliers invoices.
Class to manage payments for supplier invoices.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an invoice.
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.
addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment='', $comment='', $chqemetteur='', $chqbank='', $amount=null)
Add payment line to a specific supplier invoice with the remain to pay as amount.
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='')
List invoices.
__construct()
Constructor.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.