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';
26require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.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');
86 $this->invoice->getListIdAvoirFromInvoice();
88 $this->invoice->fetchObjectLinked();
112 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
114 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
115 throw new RestException(403);
121 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
125 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir")) {
126 $search_sale = DolibarrApiAccess::$user->id;
129 $sql =
"SELECT t.rowid";
130 $sql .=
" FROM " . MAIN_DB_PREFIX .
"facture_fourn AS t";
131 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"facture_fourn_extrafields AS ef ON (ef.fk_object = t.rowid)";
132 $sql .=
' WHERE t.entity IN (' .
getEntity(
'supplier_invoice') .
')';
134 $sql .=
" AND t.fk_soc IN (" . $this->db->sanitize($socids) .
")";
137 if ($status ==
'draft') {
138 $sql .=
" AND t.fk_statut IN (0)";
140 if ($status ==
'unpaid') {
141 $sql .=
" AND t.fk_statut IN (1)";
143 if ($status ==
'paid') {
144 $sql .=
" AND t.fk_statut IN (2)";
146 if ($status ==
'cancelled') {
147 $sql .=
" AND t.fk_statut IN (3)";
150 if ($search_sale && $search_sale !=
'-1') {
151 if ($search_sale == -2) {
152 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
153 } elseif ($search_sale > 0) {
154 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
162 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
167 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
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());
197 if ($pagination_data) {
198 $totalsResult = $this->db->query($sqlTotals);
199 $total = $this->db->fetch_object($totalsResult)->total;
204 $obj_ret[
'data'] = $tmp;
205 $obj_ret[
'pagination'] = [
206 'total' => (int) $total,
208 'page_count' => ceil((
int) $total / $limit),
232 public function post($request_data =
null)
234 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
235 throw new RestException(403,
"Insufficiant rights");
238 if (!is_array($request_data)) {
239 $request_data = array();
245 foreach ($request_data as $field => $value) {
246 if ($field ===
'caller') {
248 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
252 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
254 if (!array_key_exists(
'date', $request_data)) {
255 $this->invoice->date =
dol_now();
258 if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
259 throw new RestException(500,
"Error creating invoice ", array_merge(array($this->invoice->error), $this->invoice->errors));
261 return $this->invoice->id;
276 public function put(
$id, $request_data =
null)
278 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
279 throw new RestException(403);
283 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
286 $result = $this->invoice->fetch(
$id);
288 throw new RestException(404,
'Supplier invoice not found');
291 if (!is_array($request_data)) {
292 $request_data = array();
295 foreach ($request_data as $field => $value) {
296 if ($field ==
'id') {
299 if ($field ===
'caller') {
301 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
304 if ($field ==
'array_options' && is_array($value)) {
305 foreach ($value as $index => $val) {
311 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
314 if ($this->invoice->update(DolibarrApiAccess::$user)) {
315 return $this->
get(
$id);
334 public function delete(
$id)
336 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"supprimer")) {
337 throw new RestException(403);
340 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
342 $result = $this->invoice->fetch(
$id);
344 throw new RestException(404,
'Supplier invoice not found');
347 if ($this->invoice->delete(DolibarrApiAccess::$user) < 0) {
348 throw new RestException(500,
'Error when deleting invoice');
354 'message' =>
'Supplier invoice deleted'
380 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
381 throw new RestException(403);
385 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
388 $result = $this->invoice->fetch(
$id);
390 throw new RestException(404,
'Invoice not found');
393 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
395 throw new RestException(304,
'Error nothing done. The invoice is already validated');
398 throw new RestException(500,
'Error when validating Invoice: ' . $this->invoice->error);
404 'message' =>
'Invoice validated (Ref=' . $this->invoice->ref .
')'
426 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
427 throw new RestException(403);
429 $result = $this->invoice->fetch(
$id);
431 throw new RestException(404,
'Invoice not found');
435 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
438 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
440 throw new RestException(304,
'Nothing done.');
443 throw new RestException(500,
'Error : ' . $this->invoice->error);
446 $result = $this->invoice->fetch(
$id);
448 throw new RestException(404,
'Invoice not found');
472 throw new RestException(400,
'Invoice ID is mandatory');
475 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
476 throw new RestException(403);
479 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
482 $result = $this->invoice->fetch(
$id);
484 throw new RestException(404,
'Invoice not found');
487 $result = $this->invoice->getListOfPayments();
488 if ($this->invoice->error !==
'') {
489 throw new RestException(405, $this->invoice->error);
518 public function addPayment(
$id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $amount =
null)
521 throw new RestException(400,
'Invoice ID is mandatory');
524 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
525 throw new RestException(403);
528 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
531 $result = $this->invoice->fetch(
$id);
533 throw new RestException(404,
'Invoice not found');
537 if (empty($accountid)) {
538 throw new RestException(400,
'Bank account ID is mandatory');
542 if (empty($payment_mode_id)) {
543 throw new RestException(400,
'Payment mode ID is mandatory');
546 if (
null !== $amount && $amount > 0) {
548 $paymentamount = $amount;
551 $totalpaid = $this->invoice->getSommePaiement();
552 $totaldeposits = $this->invoice->getSumDepositsUsed();
553 $paymentamount =
price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits,
'MT');
559 $multicurrency_amounts = array();
561 $paymentamount = (float)
price2num($paymentamount,
'MT');
563 $amounts[
$id] = $paymentamount;
570 if (
null !== $amount && $amount > 0 && !empty($this->invoice->total_ttc)) {
571 $newvalue = (float)
price2num($paymentamount * $this->invoice->multicurrency_total_ttc / $this->invoice->total_ttc,
'MT');
573 $newvalue = (float)
price2num($this->invoice->multicurrency_total_ttc,
'MT');
575 $multicurrency_amounts[
$id] = $newvalue;
579 $paiement->datepaye = $datepaye;
580 $paiement->amounts = $amounts;
581 $paiement->multicurrency_amounts = $multicurrency_amounts;
582 $paiement->paiementid = $payment_mode_id;
583 $paiement->paiementcode = (
string)
dol_getIdFromCode($this->db, (
string) $payment_mode_id,
'c_paiement',
'id',
'code', 1);
584 $paiement->num_payment = $num_payment;
585 $paiement->note_private = $comment;
587 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
588 if ($paiement_id < 0) {
589 $this->db->rollback();
590 throw new RestException(400,
'Payment error : ' . $paiement->error);
594 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
596 $this->db->rollback();
597 throw new RestException(400,
'Add payment to bank error : ' . $paiement->error);
623 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'creer')) {
624 throw new RestException(403);
627 $result = $this->invoice->fetch(
$id);
629 throw new RestException(404,
'Supplier invoice not found');
633 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
636 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
638 throw new RestException(304,
'Error nothing done. Maybe object is already paid or not payable.');
641 throw new RestException(500,
'Error: ' . $this->invoice->error);
644 $result = $this->invoice->fetch(
$id);
646 throw new RestException(404,
'Supplier invoice not found');
667 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'creer')) {
668 throw new RestException(403);
671 $result = $this->invoice->fetch(
$id);
673 throw new RestException(404,
'Supplier invoice not found');
677 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
680 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
682 throw new RestException(304,
'Nothing done.');
685 throw new RestException(500,
'Error: ' . $this->invoice->error);
688 $result = $this->invoice->fetch(
$id);
690 throw new RestException(404,
'Supplier invoice not found');
712 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
713 throw new RestException(403);
716 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
719 $result = $this->invoice->fetch(
$id);
721 throw new RestException(404,
'Supplier invoice not found');
724 $this->invoice->fetch_lines();
726 foreach ($this->invoice->lines as $line) {
753 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
754 throw new RestException(403);
758 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
761 $result = $this->invoice->fetch(
$id);
763 throw new RestException(404,
'Supplier invoice not found');
766 $request_data = (object) $request_data;
768 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
769 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
771 $updateRes = $this->invoice->addline(
772 $request_data->description,
773 $request_data->pu_ht,
774 $request_data->tva_tx,
775 $request_data->localtax1_tx,
776 $request_data->localtax2_tx,
778 $request_data->fk_product,
779 $request_data->remise_percent,
780 $request_data->date_start,
781 $request_data->date_end,
782 $request_data->fk_code_ventilation,
783 $request_data->info_bits,
784 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
785 $request_data->product_type,
788 $request_data->array_options,
789 $request_data->fk_unit,
790 $request_data->origin_id,
791 $request_data->multicurrency_subprice,
792 $request_data->ref_supplier,
793 $request_data->special_code
796 if ($updateRes < 0) {
797 throw new RestException(400,
'Unable to insert the new line. Check your inputs. ' . $this->invoice->error);
822 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
823 throw new RestException(403);
827 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
830 $result = $this->invoice->fetch(
$id);
832 throw new RestException(404,
'Supplier invoice not found');
835 $request_data = (object) $request_data;
837 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
838 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
840 $updateRes = $this->invoice->updateline(
842 $request_data->description,
843 $request_data->pu_ht,
844 $request_data->tva_tx,
845 $request_data->localtax1_tx,
846 $request_data->localtax2_tx,
848 $request_data->fk_product,
849 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
850 $request_data->info_bits,
851 $request_data->product_type,
852 $request_data->remise_percent,
854 $request_data->date_start,
855 $request_data->date_end,
856 $request_data->array_options,
857 $request_data->fk_unit,
858 $request_data->multicurrency_subprice,
859 $request_data->ref_supplier,
863 if ($updateRes > 0) {
864 $result = $this->
get(
$id);
865 unset($result->line);
868 throw new RestException(304, $this->invoice->error);
891 if (empty($lineid)) {
892 throw new RestException(400,
'Line ID is mandatory');
895 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
896 throw new RestException(403);
899 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
902 $result = $this->invoice->fetch(
$id);
904 throw new RestException(404,
'Supplier invoice not found');
909 $updateRes = $this->invoice->deleteLine($lineid);
910 if ($updateRes > 0) {
914 'message' =>
'line '.$lineid.
' deleted'
918 throw new RestException(405, $this->invoice->error);
939 unset(
$object->barcode_type_code);
940 unset(
$object->barcode_type_label);
941 unset(
$object->barcode_type_coder);
956 if ($data ===
null) {
960 foreach (SupplierInvoices::$FIELDS as $field) {
961 if (!isset($data[$field])) {
962 throw new RestException(400,
"$field field missing");
964 $invoice[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
Class to manage suppliers invoices.
Class to manage payments for supplier invoices.
settodraft($id, $idwarehouse=-1, $notrigger=0)
Sets an invoice as draft.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an invoice.
settopaid($id, $close_code='', $close_note='')
Sets a supplier invoice as paid.
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 @phpstan-template T.
settounpaid($id)
Sets a supplier invoice as unpaid.
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_now($mode='gmt')
Return date for now.
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, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string