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 $result = $this->invoice->delete(DolibarrApiAccess::$user);
349 throw new RestException(500,
'Error when deleting invoice');
350 } elseif ($result == 0) {
351 throw new RestException(403,
'Invoice not erasable');
357 'message' =>
'Supplier invoice deleted'
383 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
384 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->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
398 throw new RestException(304,
'Error nothing done. The invoice is already validated');
401 throw new RestException(500,
'Error when validating Invoice: ' . $this->invoice->errorsToString());
407 'message' =>
'Invoice validated (Ref=' . $this->invoice->ref .
')'
429 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
430 throw new RestException(403);
432 $result = $this->invoice->fetch(
$id);
434 throw new RestException(404,
'Invoice not found');
438 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
441 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
443 throw new RestException(304,
'Nothing done.');
446 throw new RestException(500,
'Error : ' . $this->invoice->errorsToString());
449 $result = $this->invoice->fetch(
$id);
451 throw new RestException(404,
'Invoice not found');
475 throw new RestException(400,
'Invoice ID is mandatory');
478 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
479 throw new RestException(403);
482 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
485 $result = $this->invoice->fetch(
$id);
487 throw new RestException(404,
'Invoice not found');
490 $result = $this->invoice->getListOfPayments();
491 if ($this->invoice->errorsToString() !==
'') {
492 throw new RestException(405, $this->invoice->errorsToString());
521 public function addPayment(
$id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $amount =
null)
524 throw new RestException(400,
'Invoice ID is mandatory');
527 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
528 throw new RestException(403);
531 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
534 $result = $this->invoice->fetch(
$id);
536 throw new RestException(404,
'Invoice not found');
540 if (empty($accountid)) {
541 throw new RestException(400,
'Bank account ID is mandatory');
545 if (empty($payment_mode_id)) {
546 throw new RestException(400,
'Payment mode ID is mandatory');
549 if (
null !== $amount && $amount > 0) {
551 $paymentamount = $amount;
554 $totalpaid = $this->invoice->getSommePaiement();
555 $totaldeposits = $this->invoice->getSumDepositsUsed();
556 $paymentamount =
price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits,
'MT');
562 $multicurrency_amounts = array();
564 $paymentamount = (float)
price2num($paymentamount,
'MT');
566 $amounts[
$id] = $paymentamount;
573 if (
null !== $amount && $amount > 0 && !empty($this->invoice->total_ttc)) {
574 $newvalue = (float)
price2num($paymentamount * $this->invoice->multicurrency_total_ttc / $this->invoice->total_ttc,
'MT');
576 $newvalue = (float)
price2num($this->invoice->multicurrency_total_ttc,
'MT');
578 $multicurrency_amounts[
$id] = $newvalue;
582 $paiement->datepaye = $datepaye;
583 $paiement->amounts = $amounts;
584 $paiement->multicurrency_amounts = $multicurrency_amounts;
585 $paiement->paiementid = $payment_mode_id;
586 $paiement->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $payment_mode_id,
'c_paiement',
'id',
'code', 1);
587 $paiement->num_payment = $num_payment;
588 $paiement->note_private = $comment;
590 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
591 if ($paiement_id < 0) {
592 $this->db->rollback();
593 throw new RestException(400,
'Payment error : ' . $paiement->errorsToString());
597 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
599 $this->db->rollback();
600 throw new RestException(400,
'Add payment to bank error : ' . $paiement->errorsToString());
626 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'creer')) {
627 throw new RestException(403);
630 $result = $this->invoice->fetch(
$id);
632 throw new RestException(404,
'Supplier invoice not found');
636 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
639 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
641 throw new RestException(304,
'Error nothing done. Maybe object is already paid or not payable.');
644 throw new RestException(500,
'Error: ' . $this->invoice->errorsToString());
647 $result = $this->invoice->fetch(
$id);
649 throw new RestException(404,
'Supplier invoice not found');
670 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'creer')) {
671 throw new RestException(403);
674 $result = $this->invoice->fetch(
$id);
676 throw new RestException(404,
'Supplier invoice not found');
680 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
683 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
685 throw new RestException(304,
'Nothing done.');
688 throw new RestException(500,
'Error: ' . $this->invoice->errorsToString());
691 $result = $this->invoice->fetch(
$id);
693 throw new RestException(404,
'Supplier invoice not found');
715 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
716 throw new RestException(403);
719 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
722 $result = $this->invoice->fetch(
$id);
724 throw new RestException(404,
'Supplier invoice not found');
727 $this->invoice->fetch_lines();
729 foreach ($this->invoice->lines as $line) {
756 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
757 throw new RestException(403);
761 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
764 $result = $this->invoice->fetch(
$id);
766 throw new RestException(404,
'Supplier invoice not found');
769 $request_data = (object) $request_data;
771 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
772 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
774 $updateRes = $this->invoice->addline(
775 $request_data->description,
776 $request_data->pu_ht,
777 $request_data->tva_tx,
778 $request_data->localtax1_tx,
779 $request_data->localtax2_tx,
781 $request_data->fk_product,
782 $request_data->remise_percent,
783 $request_data->date_start,
784 $request_data->date_end,
785 $request_data->fk_code_ventilation,
786 $request_data->info_bits,
787 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
788 $request_data->product_type,
791 $request_data->array_options,
792 $request_data->fk_unit,
793 $request_data->origin_id,
794 $request_data->multicurrency_subprice,
795 $request_data->ref_supplier,
796 $request_data->special_code
799 if ($updateRes < 0) {
800 throw new RestException(400,
'Unable to insert the new line. Check your inputs. ' . $this->invoice->errorsToString());
825 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
826 throw new RestException(403);
830 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
833 $result = $this->invoice->fetch(
$id);
835 throw new RestException(404,
'Supplier invoice not found');
839 if ($invoiceline->fetch($lineid) <= 0) {
840 throw new RestException(404,
'Supplier invoice line not found');
842 if ($invoiceline->fk_facture_fourn != $this->invoice->id) {
843 throw new RestException(403,
'Line does not belong to this supplier invoice');
846 $request_data = (object) $request_data;
848 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
849 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
851 $updateRes = $this->invoice->updateline(
853 $request_data->description,
854 $request_data->pu_ht,
855 $request_data->tva_tx,
856 $request_data->localtax1_tx,
857 $request_data->localtax2_tx,
859 $request_data->fk_product,
860 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
861 $request_data->info_bits,
862 $request_data->product_type,
863 $request_data->remise_percent,
865 $request_data->date_start,
866 $request_data->date_end,
867 $request_data->array_options,
868 $request_data->fk_unit,
869 $request_data->multicurrency_subprice,
870 $request_data->ref_supplier,
874 if ($updateRes > 0) {
875 $result = $this->
get(
$id);
876 unset($result->line);
879 throw new RestException(304, $this->invoice->errorsToString());
902 if (empty($lineid)) {
903 throw new RestException(400,
'Line ID is mandatory');
906 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
907 throw new RestException(403);
910 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
913 $result = $this->invoice->fetch(
$id);
915 throw new RestException(404,
'Supplier invoice not found');
919 if ($invoiceline->fetch($lineid) <= 0) {
920 throw new RestException(404,
'Supplier invoice line not found');
922 if ($invoiceline->fk_facture_fourn != $this->invoice->id) {
923 throw new RestException(403,
'Line does not belong to this supplier invoice');
926 $updateRes = $this->invoice->deleteLine($lineid);
927 if ($updateRes > 0) {
931 'message' =>
'line '.$lineid.
' deleted'
935 throw new RestException(405, $this->invoice->errorsToString());
956 unset(
$object->barcode_type_code);
957 unset(
$object->barcode_type_label);
958 unset(
$object->barcode_type_coder);
973 if ($data ===
null) {
977 foreach (SupplierInvoices::$FIELDS as $field) {
978 if (!isset($data[$field])) {
979 throw new RestException(400,
"$field field missing");
981 $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.
Class to manage line 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.