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();
101 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'')
105 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
106 throw new RestException(401);
112 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
116 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir")) {
117 $search_sale = DolibarrApiAccess::$user->id;
120 $sql =
"SELECT t.rowid";
122 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir") || $search_sale > 0) {
123 $sql .=
", sc.fk_soc, sc.fk_user";
125 $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)";
128 if (!DolibarrApiAccess::$user->hasRight(
"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->hasRight(
"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);
165 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
169 $sql .= $this->db->order($sortfield, $sortorder);
174 $offset = $limit * $page;
176 $sql .= $this->db->plimit($limit + 1, $offset);
179 $result = $this->db->query($sql);
182 $num = $this->db->num_rows($result);
183 $min = min($num, ($limit <= 0 ? $num : $limit));
185 $obj = $this->db->fetch_object($result);
187 if ($invoice_static->fetch($obj->rowid)) {
193 throw new RestException(503,
'Error when retrieve supplier invoice list : '.$this->db->lasterror());
195 if (!count($obj_ret)) {
196 throw new RestException(404,
'No supplier invoice found');
215 public function post($request_data =
null)
217 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
218 throw new RestException(401,
"Insuffisant rights");
221 $result = $this->
_validate($request_data);
223 foreach ($request_data as $field => $value) {
224 $this->invoice->$field = $value;
226 if (!array_key_exists(
'date', $request_data)) {
227 $this->invoice->date =
dol_now();
230 if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
231 throw new RestException(500,
"Error creating invoice ", array_merge(array($this->invoice->error), $this->invoice->errors));
233 return $this->invoice->id;
247 public function put($id, $request_data =
null)
249 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
250 throw new RestException(401);
253 $result = $this->invoice->fetch($id);
255 throw new RestException(404,
'Supplier invoice not found');
259 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
262 foreach ($request_data as $field => $value) {
263 if ($field ==
'id') {
266 $this->invoice->$field = $value;
269 if ($this->invoice->update($id, DolibarrApiAccess::$user)) {
270 return $this->
get($id);
287 public function delete($id)
289 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"supprimer")) {
290 throw new RestException(401);
292 $result = $this->invoice->fetch($id);
294 throw new RestException(404,
'Supplier invoice not found');
298 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
301 if ($this->invoice->delete(DolibarrApiAccess::$user) < 0) {
302 throw new RestException(500,
'Error when deleting invoice');
308 'message' =>
'Supplier invoice deleted'
330 public function validate($id, $idwarehouse = 0, $notrigger = 0)
332 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
333 throw new RestException(401);
335 $result = $this->invoice->fetch($id);
337 throw new RestException(404,
'Invoice not found');
341 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
344 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
346 throw new RestException(304,
'Error nothing done. The invoice is already validated');
349 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
355 'message' =>
'Invoice validated (Ref='.$this->invoice->ref.
')'
375 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
376 throw new RestException(401);
379 throw new RestException(400,
'Invoice ID is mandatory');
382 $result = $this->invoice->fetch($id);
384 throw new RestException(404,
'Invoice not found');
388 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
391 $result = $this->invoice->getListOfPayments();
393 throw new RestException(405, $this->invoice->error);
420 public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
424 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
425 throw new RestException(403);
428 throw new RestException(400,
'Invoice ID is mandatory');
431 $result = $this->invoice->fetch($id);
433 throw new RestException(404,
'Invoice not found');
437 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
440 if (isModEnabled(
"banque")) {
441 if (empty($accountid)) {
442 throw new RestException(400,
'Bank account ID is mandatory');
446 if (empty($payment_mode_id)) {
447 throw new RestException(400,
'Payment mode ID is mandatory');
451 $totalpaid = $this->invoice->getSommePaiement();
452 $totaldeposits = $this->invoice->getSumDepositsUsed();
453 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits,
'MT');
458 $multicurrency_amounts = array();
460 $resteapayer =
price2num($resteapayer,
'MT');
461 $amounts[$id] = $resteapayer;
464 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
465 $multicurrency_amounts[$id] = $newvalue;
469 $paiement->datepaye = $datepaye;
470 $paiement->amounts = $amounts;
471 $paiement->multicurrency_amounts = $multicurrency_amounts;
472 $paiement->paiementid = $payment_mode_id;
473 $paiement->paiementcode =
dol_getIdFromCode($this->db, $payment_mode_id,
'c_paiement',
'id',
'code', 1);
474 $paiement->num_payment = $num_payment;
475 $paiement->note_public = $comment;
477 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
478 if ($paiement_id < 0) {
479 $this->db->rollback();
480 throw new RestException(400,
'Payment error : '.$paiement->error);
483 if (isModEnabled(
"banque")) {
484 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
486 $this->db->rollback();
487 throw new RestException(400,
'Add payment to bank error : '.$paiement->error);
507 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
508 throw new RestException(401);
511 $result = $this->invoice->fetch($id);
513 throw new RestException(404,
'Supplier invoice not found');
517 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
519 $this->invoice->fetch_lines();
521 foreach ($this->invoice->lines as $line) {
541 public function postLine($id, $request_data =
null)
543 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
544 throw new RestException(401);
547 $result = $this->invoice->fetch($id);
549 throw new RestException(404,
'Supplier invoice not found');
553 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
556 $request_data = (object) $request_data;
558 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
559 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
561 $updateRes = $this->invoice->addline(
562 $request_data->description,
563 $request_data->pu_ht,
564 $request_data->tva_tx,
565 $request_data->localtax1_tx,
566 $request_data->localtax2_tx,
568 $request_data->fk_product,
569 $request_data->remise_percent,
570 $request_data->date_start,
571 $request_data->date_end,
572 $request_data->ventil,
573 $request_data->info_bits,
574 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
575 $request_data->product_type,
578 $request_data->array_options,
579 $request_data->fk_unit,
580 $request_data->origin_id,
581 $request_data->multicurrency_subprice,
582 $request_data->ref_supplier,
583 $request_data->special_code
586 if ($updateRes < 0) {
587 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
608 public function putLine($id, $lineid, $request_data =
null)
610 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
611 throw new RestException(401);
614 $result = $this->invoice->fetch($id);
616 throw new RestException(404,
'Supplier invoice not found');
620 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
623 $request_data = (object) $request_data;
625 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
626 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
628 $updateRes = $this->invoice->updateline(
630 $request_data->description,
631 $request_data->pu_ht,
632 $request_data->tva_tx,
633 $request_data->localtax1_tx,
634 $request_data->localtax2_tx,
636 $request_data->fk_product,
637 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
638 $request_data->info_bits,
639 $request_data->product_type,
640 $request_data->remise_percent,
642 $request_data->date_start,
643 $request_data->date_end,
644 $request_data->array_options,
645 $request_data->fk_unit,
646 $request_data->multicurrency_subprice,
647 $request_data->ref_supplier,
651 if ($updateRes > 0) {
652 $result = $this->
get($id);
653 unset($result->line);
656 throw new RestException(304, $this->invoice->error);
677 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
678 throw new RestException(401);
681 $result = $this->invoice->fetch($id);
683 throw new RestException(404,
'Supplier invoice not found');
686 if (empty($lineid)) {
687 throw new RestException(400,
'Line ID is mandatory');
691 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
696 $updateRes = $this->invoice->deleteline($lineid);
697 if ($updateRes > 0) {
698 return $this->
get($id);
700 throw new RestException(405, $this->invoice->error);
714 $object = parent::_cleanObjectDatas($object);
716 unset($object->rowid);
717 unset($object->barcode_type);
718 unset($object->barcode_type_code);
719 unset($object->barcode_type_label);
720 unset($object->barcode_type_coder);
736 foreach (SupplierInvoices::$FIELDS as $field) {
737 if (!isset($data[$field])) {
738 throw new RestException(400,
"$field field missing");
740 $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.
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 '.
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.