21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
24require_once DOL_DOCUMENT_ROOT .
'/fourn/class/paiementfourn.class.php';
39 public static $FIELDS = array(
69 public function get($id)
71 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
72 throw new RestException(403);
76 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
79 $result = $this->invoice->fetch($id);
81 throw new RestException(404,
'Supplier invoice not found');
84 $this->invoice->fetchObjectLinked();
105 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'')
107 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
108 throw new RestException(403);
114 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
118 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir")) {
119 $search_sale = DolibarrApiAccess::$user->id;
122 $sql =
"SELECT t.rowid";
123 $sql .=
" FROM " . MAIN_DB_PREFIX .
"facture_fourn AS t";
124 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"facture_fourn_extrafields AS ef ON (ef.fk_object = t.rowid)";
125 $sql .=
' WHERE t.entity IN (' .
getEntity(
'supplier_invoice') .
')';
127 $sql .=
" AND t.fk_soc IN (" . $this->db->sanitize($socids) .
")";
130 if ($status ==
'draft') {
131 $sql .=
" AND t.fk_statut IN (0)";
133 if ($status ==
'unpaid') {
134 $sql .=
" AND t.fk_statut IN (1)";
136 if ($status ==
'paid') {
137 $sql .=
" AND t.fk_statut IN (2)";
139 if ($status ==
'cancelled') {
140 $sql .=
" AND t.fk_statut IN (3)";
143 if ($search_sale && $search_sale !=
'-1') {
144 if ($search_sale == -2) {
145 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
146 } elseif ($search_sale > 0) {
147 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
155 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
159 $sql .= $this->db->order($sortfield, $sortorder);
164 $offset = $limit * $page;
166 $sql .= $this->db->plimit($limit + 1, $offset);
169 $result = $this->db->query($sql);
172 $num = $this->db->num_rows($result);
173 $min = min($num, ($limit <= 0 ? $num : $limit));
175 $obj = $this->db->fetch_object($result);
177 if ($invoice_static->fetch($obj->rowid)) {
183 throw new RestException(503,
'Error when retrieve supplier invoice list : ' . $this->db->lasterror());
203 public function post($request_data =
null)
205 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
206 throw new RestException(403,
"Insuffisant rights");
209 $result = $this->
_validate($request_data);
211 foreach ($request_data as $field => $value) {
212 if ($field ===
'caller') {
214 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
218 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
220 if (!array_key_exists(
'date', $request_data)) {
221 $this->invoice->date =
dol_now();
224 if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
225 throw new RestException(500,
"Error creating invoice ", array_merge(array($this->invoice->error), $this->invoice->errors));
227 return $this->invoice->id;
240 public function put($id, $request_data =
null)
242 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
243 throw new RestException(403);
247 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
250 $result = $this->invoice->fetch($id);
252 throw new RestException(404,
'Supplier invoice not found');
255 foreach ($request_data as $field => $value) {
256 if ($field ==
'id') {
259 if ($field ===
'caller') {
261 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
264 if ($field ==
'array_options' && is_array($value)) {
265 foreach ($value as $index => $val) {
266 $this->invoice->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->invoice);
271 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
274 if ($this->invoice->update(DolibarrApiAccess::$user)) {
275 return $this->
get($id);
292 public function delete($id)
294 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"supprimer")) {
295 throw new RestException(403);
298 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
300 $result = $this->invoice->fetch($id);
302 throw new RestException(404,
'Supplier invoice not found');
305 if ($this->invoice->delete(DolibarrApiAccess::$user) < 0) {
306 throw new RestException(500,
'Error when deleting invoice');
312 'message' =>
'Supplier invoice deleted'
334 public function validate($id, $idwarehouse = 0, $notrigger = 0)
336 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
337 throw new RestException(403);
341 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
344 $result = $this->invoice->fetch($id);
346 throw new RestException(404,
'Invoice not found');
349 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
351 throw new RestException(304,
'Error nothing done. The invoice is already validated');
354 throw new RestException(500,
'Error when validating Invoice: ' . $this->invoice->error);
360 'message' =>
'Invoice validated (Ref=' . $this->invoice->ref .
')'
381 throw new RestException(400,
'Invoice ID is mandatory');
384 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
385 throw new RestException(403);
388 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
391 $result = $this->invoice->fetch($id);
393 throw new RestException(404,
'Invoice not found');
396 $result = $this->invoice->getListOfPayments();
397 if ($this->invoice->error !==
'') {
398 throw new RestException(405, $this->invoice->error);
427 public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $amount =
null)
430 throw new RestException(400,
'Invoice ID is mandatory');
433 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
434 throw new RestException(403);
437 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
440 $result = $this->invoice->fetch($id);
442 throw new RestException(404,
'Invoice not found');
445 if (isModEnabled(
"bank")) {
446 if (empty($accountid)) {
447 throw new RestException(400,
'Bank account ID is mandatory');
451 if (empty($payment_mode_id)) {
452 throw new RestException(400,
'Payment mode ID is mandatory');
455 if (
null !== $amount && $amount > 0) {
457 $paymentamount = $amount;
460 $totalpaid = $this->invoice->getSommePaiement();
461 $totaldeposits = $this->invoice->getSumDepositsUsed();
462 $paymentamount =
price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits,
'MT');
468 $multicurrency_amounts = array();
470 $paymentamount = (float)
price2num($paymentamount,
'MT');
472 $amounts[$id] = $paymentamount;
475 $newvalue = (float)
price2num($this->invoice->multicurrency_total_ttc,
'MT');
476 $multicurrency_amounts[$id] = $newvalue;
480 $paiement->datepaye = $datepaye;
481 $paiement->amounts = $amounts;
482 $paiement->multicurrency_amounts = $multicurrency_amounts;
483 $paiement->paiementid = $payment_mode_id;
484 $paiement->paiementcode = (string)
dol_getIdFromCode($this->db, $payment_mode_id,
'c_paiement',
'id',
'code', 1);
485 $paiement->num_payment = $num_payment;
486 $paiement->note_public = $comment;
488 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
489 if ($paiement_id < 0) {
490 $this->db->rollback();
491 throw new RestException(400,
'Payment error : ' . $paiement->error);
494 if (isModEnabled(
"bank")) {
495 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
497 $this->db->rollback();
498 throw new RestException(400,
'Add payment to bank error : ' . $paiement->error);
521 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
522 throw new RestException(403);
525 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
528 $result = $this->invoice->fetch($id);
530 throw new RestException(404,
'Supplier invoice not found');
533 $this->invoice->fetch_lines();
535 foreach ($this->invoice->lines as $line) {
558 public function postLine($id, $request_data =
null)
560 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
561 throw new RestException(403);
565 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
568 $result = $this->invoice->fetch($id);
570 throw new RestException(404,
'Supplier invoice not found');
573 $request_data = (object) $request_data;
575 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
576 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
578 $updateRes = $this->invoice->addline(
579 $request_data->description,
580 $request_data->pu_ht,
581 $request_data->tva_tx,
582 $request_data->localtax1_tx,
583 $request_data->localtax2_tx,
585 $request_data->fk_product,
586 $request_data->remise_percent,
587 $request_data->date_start,
588 $request_data->date_end,
589 $request_data->fk_code_ventilation,
590 $request_data->info_bits,
591 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
592 $request_data->product_type,
595 $request_data->array_options,
596 $request_data->fk_unit,
597 $request_data->origin_id,
598 $request_data->multicurrency_subprice,
599 $request_data->ref_supplier,
600 $request_data->special_code
603 if ($updateRes < 0) {
604 throw new RestException(400,
'Unable to insert the new line. Check your inputs. ' . $this->invoice->error);
625 public function putLine($id, $lineid, $request_data =
null)
627 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
628 throw new RestException(403);
632 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
635 $result = $this->invoice->fetch($id);
637 throw new RestException(404,
'Supplier invoice not found');
640 $request_data = (object) $request_data;
642 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
643 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
645 $updateRes = $this->invoice->updateline(
647 $request_data->description,
648 $request_data->pu_ht,
649 $request_data->tva_tx,
650 $request_data->localtax1_tx,
651 $request_data->localtax2_tx,
653 $request_data->fk_product,
654 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
655 $request_data->info_bits,
656 $request_data->product_type,
657 $request_data->remise_percent,
659 $request_data->date_start,
660 $request_data->date_end,
661 $request_data->array_options,
662 $request_data->fk_unit,
663 $request_data->multicurrency_subprice,
664 $request_data->ref_supplier,
668 if ($updateRes > 0) {
669 $result = $this->
get($id);
670 unset($result->line);
673 throw new RestException(304, $this->invoice->error);
694 if (empty($lineid)) {
695 throw new RestException(400,
'Line ID is mandatory');
698 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
699 throw new RestException(403);
702 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
705 $result = $this->invoice->fetch($id);
707 throw new RestException(404,
'Supplier invoice not found');
712 $updateRes = $this->invoice->deleteLine($lineid);
713 if ($updateRes > 0) {
717 'message' =>
'line '.$lineid.
' deleted'
721 throw new RestException(405, $this->invoice->error);
739 unset(
$object->barcode_type_code);
740 unset(
$object->barcode_type_label);
741 unset(
$object->barcode_type_coder);
757 foreach (SupplierInvoices::$FIELDS as $field) {
758 if (!isset($data[$field])) {
759 throw new RestException(400,
"$field field missing");
761 $invoice[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_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.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
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.
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.