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');
383 $result = $this->invoice->fetch($id);
385 throw new RestException(404,
'Invoice not found');
389 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
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');
432 $result = $this->invoice->fetch($id);
434 throw new RestException(404,
'Invoice not found');
438 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
441 if (!empty($conf->banque->enabled)) {
442 if (empty($accountid)) {
443 throw new RestException(400,
'Bank account ID is mandatory');
447 if (empty($payment_mode_id)) {
448 throw new RestException(400,
'Payment mode ID is mandatory');
452 $totalpaid = $this->invoice->getSommePaiement();
453 $totaldeposits = $this->invoice->getSumDepositsUsed();
454 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits,
'MT');
459 $multicurrency_amounts = array();
461 $resteapayer =
price2num($resteapayer,
'MT');
462 $amounts[$id] = $resteapayer;
465 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
466 $multicurrency_amounts[$id] = $newvalue;
470 $paiement->datepaye = $datepaye;
471 $paiement->amounts = $amounts;
472 $paiement->multicurrency_amounts = $multicurrency_amounts;
473 $paiement->paiementid = $payment_mode_id;
474 $paiement->paiementcode =
dol_getIdFromCode($this->
db, $payment_mode_id,
'c_paiement',
'id',
'code', 1);
475 $paiement->num_payment = $num_payment;
476 $paiement->note_public = $comment;
478 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
479 if ($paiement_id < 0) {
480 $this->
db->rollback();
481 throw new RestException(400,
'Payment error : '.$paiement->error);
484 if (!empty($conf->banque->enabled)) {
485 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
487 $this->
db->rollback();
488 throw new RestException(400,
'Add payment to bank error : '.$paiement->error);
508 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
509 throw new RestException(401);
512 $result = $this->invoice->fetch($id);
514 throw new RestException(404,
'Supplier invoice not found');
518 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
520 $this->invoice->fetch_lines();
522 foreach ($this->invoice->lines as $line) {
542 public function postLine($id, $request_data =
null)
544 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
545 throw new RestException(401);
548 $result = $this->invoice->fetch($id);
550 throw new RestException(404,
'Supplier invoice not found');
554 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
557 $request_data = (object) $request_data;
559 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
560 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
562 $updateRes = $this->invoice->addline(
563 $request_data->description,
564 $request_data->pu_ht,
565 $request_data->tva_tx,
566 $request_data->localtax1_tx,
567 $request_data->localtax2_tx,
569 $request_data->fk_product,
570 $request_data->remise_percent,
571 $request_data->date_start,
572 $request_data->date_end,
573 $request_data->ventil,
574 $request_data->info_bits,
575 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
576 $request_data->product_type,
579 $request_data->array_options,
580 $request_data->fk_unit,
581 $request_data->origin_id,
582 $request_data->multicurrency_subprice,
583 $request_data->ref_supplier,
584 $request_data->special_code
587 if ($updateRes < 0) {
588 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
609 public function putLine($id, $lineid, $request_data =
null)
611 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
612 throw new RestException(401);
615 $result = $this->invoice->fetch($id);
617 throw new RestException(404,
'Supplier invoice not found');
621 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
624 $request_data = (object) $request_data;
626 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
627 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
629 $updateRes = $this->invoice->updateline(
631 $request_data->description,
632 $request_data->pu_ht,
633 $request_data->tva_tx,
634 $request_data->localtax1_tx,
635 $request_data->localtax2_tx,
637 $request_data->fk_product,
638 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
639 $request_data->info_bits,
640 $request_data->product_type,
641 $request_data->remise_percent,
643 $request_data->date_start,
644 $request_data->date_end,
645 $request_data->array_options,
646 $request_data->fk_unit,
647 $request_data->multicurrency_subprice,
648 $request_data->ref_supplier,
652 if ($updateRes > 0) {
653 $result = $this->
get($id);
654 unset($result->line);
657 throw new RestException(304, $this->invoice->error);
678 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
679 throw new RestException(401);
682 $result = $this->invoice->fetch($id);
684 throw new RestException(404,
'Supplier invoice not found');
687 if (empty($lineid)) {
688 throw new RestException(400,
'Line ID is mandatory');
692 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
697 $updateRes = $this->invoice->deleteline($lineid);
698 if ($updateRes > 0) {
699 return $this->
get($id);
701 throw new RestException(405, $this->invoice->error);
715 $object = parent::_cleanObjectDatas($object);
717 unset($object->rowid);
718 unset($object->barcode_type);
719 unset($object->barcode_type_code);
720 unset($object->barcode_type_label);
721 unset($object->barcode_type_coder);
737 foreach (SupplierInvoices::$FIELDS as $field) {
738 if (!isset($data[$field])) {
739 throw new RestException(400,
"$field field missing");
741 $invoice[$field] = $data[$field];