22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
25require_once DOL_DOCUMENT_ROOT .
'/fourn/class/paiementfourn.class.php';
40 public static $FIELDS = array(
70 public function get(
$id)
72 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
73 throw new RestException(403);
77 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
80 $result = $this->invoice->fetch(
$id);
82 throw new RestException(404,
'Supplier invoice not found');
85 $this->invoice->fetchObjectLinked();
107 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
109 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
110 throw new RestException(403);
116 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
120 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir")) {
121 $search_sale = DolibarrApiAccess::$user->id;
124 $sql =
"SELECT t.rowid";
125 $sql .=
" FROM " . MAIN_DB_PREFIX .
"facture_fourn AS t";
126 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"facture_fourn_extrafields AS ef ON (ef.fk_object = t.rowid)";
127 $sql .=
' WHERE t.entity IN (' .
getEntity(
'supplier_invoice') .
')';
129 $sql .=
" AND t.fk_soc IN (" . $this->db->sanitize($socids) .
")";
132 if ($status ==
'draft') {
133 $sql .=
" AND t.fk_statut IN (0)";
135 if ($status ==
'unpaid') {
136 $sql .=
" AND t.fk_statut IN (1)";
138 if ($status ==
'paid') {
139 $sql .=
" AND t.fk_statut IN (2)";
141 if ($status ==
'cancelled') {
142 $sql .=
" AND t.fk_statut IN (3)";
145 if ($search_sale && $search_sale !=
'-1') {
146 if ($search_sale == -2) {
147 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
148 } elseif ($search_sale > 0) {
149 $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).
")";
157 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
162 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
164 $sql .= $this->db->order($sortfield, $sortorder);
169 $offset = $limit * $page;
171 $sql .= $this->db->plimit($limit + 1, $offset);
174 $result = $this->db->query($sql);
177 $num = $this->db->num_rows($result);
178 $min = min($num, ($limit <= 0 ? $num : $limit));
180 $obj = $this->db->fetch_object($result);
182 if ($invoice_static->fetch($obj->rowid)) {
188 throw new RestException(503,
'Error when retrieve supplier invoice list : ' . $this->db->lasterror());
192 if ($pagination_data) {
193 $totalsResult = $this->db->query($sqlTotals);
194 $total = $this->db->fetch_object($totalsResult)->total;
199 $obj_ret[
'data'] = $tmp;
200 $obj_ret[
'pagination'] = [
201 'total' => (int) $total,
203 'page_count' => ceil((
int) $total / $limit),
225 public function post($request_data =
null)
227 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
228 throw new RestException(403,
"Insuffisant rights");
231 $result = $this->
_validate($request_data);
233 foreach ($request_data as $field => $value) {
234 if ($field ===
'caller') {
236 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
240 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
242 if (!array_key_exists(
'date', $request_data)) {
243 $this->invoice->date =
dol_now();
246 if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
247 throw new RestException(500,
"Error creating invoice ", array_merge(array($this->invoice->error), $this->invoice->errors));
249 return $this->invoice->id;
262 public function put(
$id, $request_data =
null)
264 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
265 throw new RestException(403);
269 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
272 $result = $this->invoice->fetch(
$id);
274 throw new RestException(404,
'Supplier invoice not found');
277 foreach ($request_data as $field => $value) {
278 if ($field ==
'id') {
281 if ($field ===
'caller') {
283 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
286 if ($field ==
'array_options' && is_array($value)) {
287 foreach ($value as $index => $val) {
288 $this->invoice->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->invoice);
292 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
295 if ($this->invoice->update(DolibarrApiAccess::$user)) {
296 return $this->
get(
$id);
313 public function delete(
$id)
315 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"supprimer")) {
316 throw new RestException(403);
319 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
321 $result = $this->invoice->fetch(
$id);
323 throw new RestException(404,
'Supplier invoice not found');
326 if ($this->invoice->delete(DolibarrApiAccess::$user) < 0) {
327 throw new RestException(500,
'Error when deleting invoice');
333 'message' =>
'Supplier invoice deleted'
357 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
358 throw new RestException(403);
362 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
365 $result = $this->invoice->fetch(
$id);
367 throw new RestException(404,
'Invoice not found');
370 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
372 throw new RestException(304,
'Error nothing done. The invoice is already validated');
375 throw new RestException(500,
'Error when validating Invoice: ' . $this->invoice->error);
381 'message' =>
'Invoice validated (Ref=' . $this->invoice->ref .
')'
402 throw new RestException(400,
'Invoice ID is mandatory');
405 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
406 throw new RestException(403);
409 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
412 $result = $this->invoice->fetch(
$id);
414 throw new RestException(404,
'Invoice not found');
417 $result = $this->invoice->getListOfPayments();
418 if ($this->invoice->error !==
'') {
419 throw new RestException(405, $this->invoice->error);
448 public function addPayment(
$id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $amount =
null)
451 throw new RestException(400,
'Invoice ID is mandatory');
454 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
455 throw new RestException(403);
458 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
461 $result = $this->invoice->fetch(
$id);
463 throw new RestException(404,
'Invoice not found');
466 if (isModEnabled(
"bank")) {
467 if (empty($accountid)) {
468 throw new RestException(400,
'Bank account ID is mandatory');
472 if (empty($payment_mode_id)) {
473 throw new RestException(400,
'Payment mode ID is mandatory');
476 if (
null !== $amount && $amount > 0) {
478 $paymentamount = $amount;
481 $totalpaid = $this->invoice->getSommePaiement();
482 $totaldeposits = $this->invoice->getSumDepositsUsed();
483 $paymentamount =
price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits,
'MT');
489 $multicurrency_amounts = array();
491 $paymentamount = (float)
price2num($paymentamount,
'MT');
493 $amounts[
$id] = $paymentamount;
496 $newvalue = (float)
price2num($this->invoice->multicurrency_total_ttc,
'MT');
497 $multicurrency_amounts[
$id] = $newvalue;
501 $paiement->datepaye = $datepaye;
502 $paiement->amounts = $amounts;
503 $paiement->multicurrency_amounts = $multicurrency_amounts;
504 $paiement->paiementid = $payment_mode_id;
505 $paiement->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $payment_mode_id,
'c_paiement',
'id',
'code', 1);
506 $paiement->num_payment = $num_payment;
507 $paiement->note_public = $comment;
509 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
510 if ($paiement_id < 0) {
511 $this->db->rollback();
512 throw new RestException(400,
'Payment error : ' . $paiement->error);
515 if (isModEnabled(
"bank")) {
516 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
518 $this->db->rollback();
519 throw new RestException(400,
'Add payment to bank error : ' . $paiement->error);
542 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
543 throw new RestException(403);
546 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
549 $result = $this->invoice->fetch(
$id);
551 throw new RestException(404,
'Supplier invoice not found');
554 $this->invoice->fetch_lines();
556 foreach ($this->invoice->lines as $line) {
581 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
582 throw new RestException(403);
586 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
589 $result = $this->invoice->fetch(
$id);
591 throw new RestException(404,
'Supplier invoice not found');
594 $request_data = (object) $request_data;
596 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
597 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
599 $updateRes = $this->invoice->addline(
600 $request_data->description,
601 $request_data->pu_ht,
602 $request_data->tva_tx,
603 $request_data->localtax1_tx,
604 $request_data->localtax2_tx,
606 $request_data->fk_product,
607 $request_data->remise_percent,
608 $request_data->date_start,
609 $request_data->date_end,
610 $request_data->fk_code_ventilation,
611 $request_data->info_bits,
612 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
613 $request_data->product_type,
616 $request_data->array_options,
617 $request_data->fk_unit,
618 $request_data->origin_id,
619 $request_data->multicurrency_subprice,
620 $request_data->ref_supplier,
621 $request_data->special_code
624 if ($updateRes < 0) {
625 throw new RestException(400,
'Unable to insert the new line. Check your inputs. ' . $this->invoice->error);
648 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
649 throw new RestException(403);
653 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
656 $result = $this->invoice->fetch(
$id);
658 throw new RestException(404,
'Supplier invoice not found');
661 $request_data = (object) $request_data;
663 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
664 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
666 $updateRes = $this->invoice->updateline(
668 $request_data->description,
669 $request_data->pu_ht,
670 $request_data->tva_tx,
671 $request_data->localtax1_tx,
672 $request_data->localtax2_tx,
674 $request_data->fk_product,
675 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
676 $request_data->info_bits,
677 $request_data->product_type,
678 $request_data->remise_percent,
680 $request_data->date_start,
681 $request_data->date_end,
682 $request_data->array_options,
683 $request_data->fk_unit,
684 $request_data->multicurrency_subprice,
685 $request_data->ref_supplier,
689 if ($updateRes > 0) {
690 $result = $this->
get(
$id);
691 unset($result->line);
694 throw new RestException(304, $this->invoice->error);
715 if (empty($lineid)) {
716 throw new RestException(400,
'Line ID is mandatory');
719 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
720 throw new RestException(403);
723 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
726 $result = $this->invoice->fetch(
$id);
728 throw new RestException(404,
'Supplier invoice not found');
733 $updateRes = $this->invoice->deleteLine($lineid);
734 if ($updateRes > 0) {
738 'message' =>
'line '.$lineid.
' deleted'
742 throw new RestException(405, $this->invoice->error);
760 unset(
$object->barcode_type_code);
761 unset(
$object->barcode_type_label);
762 unset(
$object->barcode_type_coder);
778 foreach (SupplierInvoices::$FIELDS as $field) {
779 if (!isset($data[$field])) {
780 throw new RestException(400,
"$field field missing");
782 $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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='', $pagination_data=false)
List invoices.
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.
__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.