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);
293 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
296 if ($this->invoice->update(DolibarrApiAccess::$user)) {
297 return $this->
get(
$id);
314 public function delete(
$id)
316 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"supprimer")) {
317 throw new RestException(403);
320 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
322 $result = $this->invoice->fetch(
$id);
324 throw new RestException(404,
'Supplier invoice not found');
327 if ($this->invoice->delete(DolibarrApiAccess::$user) < 0) {
328 throw new RestException(500,
'Error when deleting invoice');
334 'message' =>
'Supplier invoice deleted'
358 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
359 throw new RestException(403);
363 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
366 $result = $this->invoice->fetch(
$id);
368 throw new RestException(404,
'Invoice not found');
371 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
373 throw new RestException(304,
'Error nothing done. The invoice is already validated');
376 throw new RestException(500,
'Error when validating Invoice: ' . $this->invoice->error);
382 'message' =>
'Invoice validated (Ref=' . $this->invoice->ref .
')'
403 throw new RestException(400,
'Invoice ID is mandatory');
406 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
407 throw new RestException(403);
410 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
413 $result = $this->invoice->fetch(
$id);
415 throw new RestException(404,
'Invoice not found');
418 $result = $this->invoice->getListOfPayments();
419 if ($this->invoice->error !==
'') {
420 throw new RestException(405, $this->invoice->error);
449 public function addPayment(
$id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $amount =
null)
452 throw new RestException(400,
'Invoice ID is mandatory');
455 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
456 throw new RestException(403);
459 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
462 $result = $this->invoice->fetch(
$id);
464 throw new RestException(404,
'Invoice not found');
467 if (isModEnabled(
"bank")) {
468 if (empty($accountid)) {
469 throw new RestException(400,
'Bank account ID is mandatory');
473 if (empty($payment_mode_id)) {
474 throw new RestException(400,
'Payment mode ID is mandatory');
477 if (
null !== $amount && $amount > 0) {
479 $paymentamount = $amount;
482 $totalpaid = $this->invoice->getSommePaiement();
483 $totaldeposits = $this->invoice->getSumDepositsUsed();
484 $paymentamount =
price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits,
'MT');
490 $multicurrency_amounts = array();
492 $paymentamount = (float)
price2num($paymentamount,
'MT');
494 $amounts[
$id] = $paymentamount;
497 $newvalue = (float)
price2num($this->invoice->multicurrency_total_ttc,
'MT');
498 $multicurrency_amounts[
$id] = $newvalue;
502 $paiement->datepaye = $datepaye;
503 $paiement->amounts = $amounts;
504 $paiement->multicurrency_amounts = $multicurrency_amounts;
505 $paiement->paiementid = $payment_mode_id;
506 $paiement->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $payment_mode_id,
'c_paiement',
'id',
'code', 1);
507 $paiement->num_payment = $num_payment;
508 $paiement->note_public = $comment;
510 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
511 if ($paiement_id < 0) {
512 $this->db->rollback();
513 throw new RestException(400,
'Payment error : ' . $paiement->error);
516 if (isModEnabled(
"bank")) {
517 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
519 $this->db->rollback();
520 throw new RestException(400,
'Add payment to bank error : ' . $paiement->error);
543 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
544 throw new RestException(403);
547 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
550 $result = $this->invoice->fetch(
$id);
552 throw new RestException(404,
'Supplier invoice not found');
555 $this->invoice->fetch_lines();
557 foreach ($this->invoice->lines as $line) {
582 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
583 throw new RestException(403);
587 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
590 $result = $this->invoice->fetch(
$id);
592 throw new RestException(404,
'Supplier invoice not found');
595 $request_data = (object) $request_data;
597 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
598 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
600 $updateRes = $this->invoice->addline(
601 $request_data->description,
602 $request_data->pu_ht,
603 $request_data->tva_tx,
604 $request_data->localtax1_tx,
605 $request_data->localtax2_tx,
607 $request_data->fk_product,
608 $request_data->remise_percent,
609 $request_data->date_start,
610 $request_data->date_end,
611 $request_data->fk_code_ventilation,
612 $request_data->info_bits,
613 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
614 $request_data->product_type,
617 $request_data->array_options,
618 $request_data->fk_unit,
619 $request_data->origin_id,
620 $request_data->multicurrency_subprice,
621 $request_data->ref_supplier,
622 $request_data->special_code
625 if ($updateRes < 0) {
626 throw new RestException(400,
'Unable to insert the new line. Check your inputs. ' . $this->invoice->error);
649 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
650 throw new RestException(403);
654 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
657 $result = $this->invoice->fetch(
$id);
659 throw new RestException(404,
'Supplier invoice not found');
662 $request_data = (object) $request_data;
664 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
665 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
667 $updateRes = $this->invoice->updateline(
669 $request_data->description,
670 $request_data->pu_ht,
671 $request_data->tva_tx,
672 $request_data->localtax1_tx,
673 $request_data->localtax2_tx,
675 $request_data->fk_product,
676 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
677 $request_data->info_bits,
678 $request_data->product_type,
679 $request_data->remise_percent,
681 $request_data->date_start,
682 $request_data->date_end,
683 $request_data->array_options,
684 $request_data->fk_unit,
685 $request_data->multicurrency_subprice,
686 $request_data->ref_supplier,
690 if ($updateRes > 0) {
691 $result = $this->
get(
$id);
692 unset($result->line);
695 throw new RestException(304, $this->invoice->error);
716 if (empty($lineid)) {
717 throw new RestException(400,
'Line ID is mandatory');
720 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
721 throw new RestException(403);
724 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
727 $result = $this->invoice->fetch(
$id);
729 throw new RestException(404,
'Supplier invoice not found');
734 $updateRes = $this->invoice->deleteLine($lineid);
735 if ($updateRes > 0) {
739 'message' =>
'line '.$lineid.
' deleted'
743 throw new RestException(405, $this->invoice->error);
761 unset(
$object->barcode_type_code);
762 unset(
$object->barcode_type_label);
763 unset(
$object->barcode_type_coder);
779 foreach (SupplierInvoices::$FIELDS as $field) {
780 if (!isset($data[$field])) {
781 throw new RestException(400,
"$field field missing");
783 $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.
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.