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';
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');
85 $this->invoice->getListIdAvoirFromInvoice();
87 $this->invoice->fetchObjectLinked();
111 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
113 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
114 throw new RestException(403);
120 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
124 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir")) {
125 $search_sale = DolibarrApiAccess::$user->id;
128 $sql =
"SELECT t.rowid";
129 $sql .=
" FROM " . MAIN_DB_PREFIX .
"facture_fourn AS t";
130 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"facture_fourn_extrafields AS ef ON (ef.fk_object = t.rowid)";
131 $sql .=
' WHERE t.entity IN (' .
getEntity(
'supplier_invoice') .
')';
133 $sql .=
" AND t.fk_soc IN (" . $this->db->sanitize($socids) .
")";
136 if ($status ==
'draft') {
137 $sql .=
" AND t.fk_statut IN (0)";
139 if ($status ==
'unpaid') {
140 $sql .=
" AND t.fk_statut IN (1)";
142 if ($status ==
'paid') {
143 $sql .=
" AND t.fk_statut IN (2)";
145 if ($status ==
'cancelled') {
146 $sql .=
" AND t.fk_statut IN (3)";
149 if ($search_sale && $search_sale !=
'-1') {
150 if ($search_sale == -2) {
151 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
152 } elseif ($search_sale > 0) {
153 $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).
")";
159 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
161 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
166 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
168 $sql .= $this->db->order($sortfield, $sortorder);
173 $offset = $limit * $page;
175 $sql .= $this->db->plimit($limit + 1, $offset);
178 $result = $this->db->query($sql);
181 $num = $this->db->num_rows($result);
182 $min = min($num, ($limit <= 0 ? $num : $limit));
184 $obj = $this->db->fetch_object($result);
186 if ($invoice_static->fetch($obj->rowid)) {
192 throw new RestException(503,
'Error when retrieve supplier invoice list : ' . $this->db->lasterror());
196 if ($pagination_data) {
197 $totalsResult = $this->db->query($sqlTotals);
198 $total = $this->db->fetch_object($totalsResult)->total;
203 $obj_ret[
'data'] = $tmp;
204 $obj_ret[
'pagination'] = [
205 'total' => (int) $total,
207 'page_count' => ceil((
int) $total / $limit),
231 public function post($request_data =
null)
233 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
234 throw new RestException(403,
"Insufficiant rights");
237 if (!is_array($request_data)) {
238 $request_data = array();
244 foreach ($request_data as $field => $value) {
245 if ($field ===
'caller') {
247 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
251 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
253 if (!array_key_exists(
'date', $request_data)) {
254 $this->invoice->date =
dol_now();
257 if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
258 throw new RestException(500,
"Error creating invoice ", array_merge(array($this->invoice->error), $this->invoice->errors));
260 return $this->invoice->id;
275 public function put(
$id, $request_data =
null)
277 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
278 throw new RestException(403);
282 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
285 $result = $this->invoice->fetch(
$id);
287 throw new RestException(404,
'Supplier invoice not found');
290 if (!is_array($request_data)) {
291 $request_data = array();
294 foreach ($request_data as $field => $value) {
295 if ($field ==
'id') {
298 if ($field ===
'caller') {
300 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
303 if ($field ==
'array_options' && is_array($value)) {
304 foreach ($value as $index => $val) {
310 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
313 if ($this->invoice->update(DolibarrApiAccess::$user)) {
314 return $this->
get(
$id);
333 public function delete(
$id)
335 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"supprimer")) {
336 throw new RestException(403);
339 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
341 $result = $this->invoice->fetch(
$id);
343 throw new RestException(404,
'Supplier invoice not found');
346 if ($this->invoice->delete(DolibarrApiAccess::$user) < 0) {
347 throw new RestException(500,
'Error when deleting invoice');
353 'message' =>
'Supplier invoice deleted'
379 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
380 throw new RestException(403);
384 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
387 $result = $this->invoice->fetch(
$id);
389 throw new RestException(404,
'Invoice not found');
392 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
394 throw new RestException(304,
'Error nothing done. The invoice is already validated');
397 throw new RestException(500,
'Error when validating Invoice: ' . $this->invoice->error);
403 'message' =>
'Invoice validated (Ref=' . $this->invoice->ref .
')'
425 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
426 throw new RestException(403);
428 $result = $this->invoice->fetch(
$id);
430 throw new RestException(404,
'Invoice not found');
434 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
437 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
439 throw new RestException(304,
'Nothing done.');
442 throw new RestException(500,
'Error : ' . $this->invoice->error);
445 $result = $this->invoice->fetch(
$id);
447 throw new RestException(404,
'Invoice not found');
471 throw new RestException(400,
'Invoice ID is mandatory');
474 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
475 throw new RestException(403);
478 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
481 $result = $this->invoice->fetch(
$id);
483 throw new RestException(404,
'Invoice not found');
486 $result = $this->invoice->getListOfPayments();
487 if ($this->invoice->error !==
'') {
488 throw new RestException(405, $this->invoice->error);
517 public function addPayment(
$id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $amount =
null)
520 throw new RestException(400,
'Invoice ID is mandatory');
523 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
524 throw new RestException(403);
527 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
530 $result = $this->invoice->fetch(
$id);
532 throw new RestException(404,
'Invoice not found');
536 if (empty($accountid)) {
537 throw new RestException(400,
'Bank account ID is mandatory');
541 if (empty($payment_mode_id)) {
542 throw new RestException(400,
'Payment mode ID is mandatory');
545 if (
null !== $amount && $amount > 0) {
547 $paymentamount = $amount;
550 $totalpaid = $this->invoice->getSommePaiement();
551 $totaldeposits = $this->invoice->getSumDepositsUsed();
552 $paymentamount =
price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits,
'MT');
558 $multicurrency_amounts = array();
560 $paymentamount = (float)
price2num($paymentamount,
'MT');
562 $amounts[
$id] = $paymentamount;
565 $newvalue = (float)
price2num($this->invoice->multicurrency_total_ttc,
'MT');
566 $multicurrency_amounts[
$id] = $newvalue;
570 $paiement->datepaye = $datepaye;
571 $paiement->amounts = $amounts;
572 $paiement->multicurrency_amounts = $multicurrency_amounts;
573 $paiement->paiementid = $payment_mode_id;
574 $paiement->paiementcode = (
string)
dol_getIdFromCode($this->db, (
string) $payment_mode_id,
'c_paiement',
'id',
'code', 1);
575 $paiement->num_payment = $num_payment;
576 $paiement->note_private = $comment;
578 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
579 if ($paiement_id < 0) {
580 $this->db->rollback();
581 throw new RestException(400,
'Payment error : ' . $paiement->error);
585 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
587 $this->db->rollback();
588 throw new RestException(400,
'Add payment to bank error : ' . $paiement->error);
614 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'creer')) {
615 throw new RestException(403);
618 $result = $this->invoice->fetch(
$id);
620 throw new RestException(404,
'Supplier invoice not found');
624 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
627 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
629 throw new RestException(304,
'Error nothing done. Maybe object is already paid or not payable.');
632 throw new RestException(500,
'Error: ' . $this->invoice->error);
635 $result = $this->invoice->fetch(
$id);
637 throw new RestException(404,
'Supplier invoice not found');
658 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'creer')) {
659 throw new RestException(403);
662 $result = $this->invoice->fetch(
$id);
664 throw new RestException(404,
'Supplier invoice not found');
668 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
671 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
673 throw new RestException(304,
'Nothing done.');
676 throw new RestException(500,
'Error: ' . $this->invoice->error);
679 $result = $this->invoice->fetch(
$id);
681 throw new RestException(404,
'Supplier invoice not found');
703 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
704 throw new RestException(403);
707 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
710 $result = $this->invoice->fetch(
$id);
712 throw new RestException(404,
'Supplier invoice not found');
715 $this->invoice->fetch_lines();
717 foreach ($this->invoice->lines as $line) {
744 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
745 throw new RestException(403);
749 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
752 $result = $this->invoice->fetch(
$id);
754 throw new RestException(404,
'Supplier invoice not found');
757 $request_data = (object) $request_data;
759 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
760 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
762 $updateRes = $this->invoice->addline(
763 $request_data->description,
764 $request_data->pu_ht,
765 $request_data->tva_tx,
766 $request_data->localtax1_tx,
767 $request_data->localtax2_tx,
769 $request_data->fk_product,
770 $request_data->remise_percent,
771 $request_data->date_start,
772 $request_data->date_end,
773 $request_data->fk_code_ventilation,
774 $request_data->info_bits,
775 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
776 $request_data->product_type,
779 $request_data->array_options,
780 $request_data->fk_unit,
781 $request_data->origin_id,
782 $request_data->multicurrency_subprice,
783 $request_data->ref_supplier,
784 $request_data->special_code
787 if ($updateRes < 0) {
788 throw new RestException(400,
'Unable to insert the new line. Check your inputs. ' . $this->invoice->error);
813 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
814 throw new RestException(403);
818 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
821 $result = $this->invoice->fetch(
$id);
823 throw new RestException(404,
'Supplier invoice not found');
826 $request_data = (object) $request_data;
828 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
829 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
831 $updateRes = $this->invoice->updateline(
833 $request_data->description,
834 $request_data->pu_ht,
835 $request_data->tva_tx,
836 $request_data->localtax1_tx,
837 $request_data->localtax2_tx,
839 $request_data->fk_product,
840 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
841 $request_data->info_bits,
842 $request_data->product_type,
843 $request_data->remise_percent,
845 $request_data->date_start,
846 $request_data->date_end,
847 $request_data->array_options,
848 $request_data->fk_unit,
849 $request_data->multicurrency_subprice,
850 $request_data->ref_supplier,
854 if ($updateRes > 0) {
855 $result = $this->
get(
$id);
856 unset($result->line);
859 throw new RestException(304, $this->invoice->error);
882 if (empty($lineid)) {
883 throw new RestException(400,
'Line ID is mandatory');
886 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
887 throw new RestException(403);
890 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
893 $result = $this->invoice->fetch(
$id);
895 throw new RestException(404,
'Supplier invoice not found');
900 $updateRes = $this->invoice->deleteLine($lineid);
901 if ($updateRes > 0) {
905 'message' =>
'line '.$lineid.
' deleted'
909 throw new RestException(405, $this->invoice->error);
930 unset(
$object->barcode_type_code);
931 unset(
$object->barcode_type_label);
932 unset(
$object->barcode_type_coder);
947 if ($data ===
null) {
951 foreach (SupplierInvoices::$FIELDS as $field) {
952 if (!isset($data[$field])) {
953 throw new RestException(400,
"$field field missing");
955 $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.
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.
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 '.
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
buildzip.php