21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
24require_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');
84 $this->invoice->fetchObjectLinked();
105 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'')
107 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
108 throw new RestException(403);
114 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
118 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir")) {
119 $search_sale = DolibarrApiAccess::$user->id;
122 $sql =
"SELECT t.rowid";
123 $sql .=
" FROM " . MAIN_DB_PREFIX .
"facture_fourn AS t";
124 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"facture_fourn_extrafields AS ef ON (ef.fk_object = t.rowid)";
125 $sql .=
' WHERE t.entity IN (' .
getEntity(
'supplier_invoice') .
')';
127 $sql .=
" AND t.fk_soc IN (" . $this->db->sanitize($socids) .
")";
130 if ($status ==
'draft') {
131 $sql .=
" AND t.fk_statut IN (0)";
133 if ($status ==
'unpaid') {
134 $sql .=
" AND t.fk_statut IN (1)";
136 if ($status ==
'paid') {
137 $sql .=
" AND t.fk_statut IN (2)";
139 if ($status ==
'cancelled') {
140 $sql .=
" AND t.fk_statut IN (3)";
143 if ($search_sale && $search_sale !=
'-1') {
144 if ($search_sale == -2) {
145 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
146 } elseif ($search_sale > 0) {
147 $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).
")";
155 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
159 $sql .= $this->db->order($sortfield, $sortorder);
164 $offset = $limit * $page;
166 $sql .= $this->db->plimit($limit + 1, $offset);
169 $result = $this->db->query($sql);
172 $num = $this->db->num_rows($result);
173 $min = min($num, ($limit <= 0 ? $num : $limit));
175 $obj = $this->db->fetch_object($result);
177 if ($invoice_static->fetch($obj->rowid)) {
183 throw new RestException(503,
'Error when retrieve supplier invoice list : ' . $this->db->lasterror());
203 public function post($request_data =
null)
205 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
206 throw new RestException(403,
"Insuffisant rights");
209 $result = $this->
_validate($request_data);
211 foreach ($request_data as $field => $value) {
212 if ($field ===
'caller') {
214 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
218 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
220 if (!array_key_exists(
'date', $request_data)) {
221 $this->invoice->date =
dol_now();
224 if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
225 throw new RestException(500,
"Error creating invoice ", array_merge(array($this->invoice->error), $this->invoice->errors));
227 return $this->invoice->id;
240 public function put($id, $request_data =
null)
242 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
243 throw new RestException(403);
247 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
250 $result = $this->invoice->fetch($id);
252 throw new RestException(404,
'Supplier invoice not found');
255 foreach ($request_data as $field => $value) {
256 if ($field ==
'id') {
259 if ($field ===
'caller') {
261 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
264 if ($field ==
'array_options' && is_array($value)) {
265 foreach ($value as $index => $val) {
266 $this->invoice->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->invoice);
270 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
273 if ($this->invoice->update(DolibarrApiAccess::$user)) {
274 return $this->
get($id);
291 public function delete($id)
293 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"supprimer")) {
294 throw new RestException(403);
297 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
299 $result = $this->invoice->fetch($id);
301 throw new RestException(404,
'Supplier invoice not found');
304 if ($this->invoice->delete(DolibarrApiAccess::$user) < 0) {
305 throw new RestException(500,
'Error when deleting invoice');
311 'message' =>
'Supplier invoice deleted'
333 public function validate($id, $idwarehouse = 0, $notrigger = 0)
335 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
336 throw new RestException(403);
340 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
343 $result = $this->invoice->fetch($id);
345 throw new RestException(404,
'Invoice not found');
348 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
350 throw new RestException(304,
'Error nothing done. The invoice is already validated');
353 throw new RestException(500,
'Error when validating Invoice: ' . $this->invoice->error);
359 'message' =>
'Invoice validated (Ref=' . $this->invoice->ref .
')'
380 throw new RestException(400,
'Invoice ID is mandatory');
383 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
384 throw new RestException(403);
387 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
390 $result = $this->invoice->fetch($id);
392 throw new RestException(404,
'Invoice not found');
395 $result = $this->invoice->getListOfPayments();
396 if ($this->invoice->error !==
'') {
397 throw new RestException(405, $this->invoice->error);
426 public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $amount =
null)
429 throw new RestException(400,
'Invoice ID is mandatory');
432 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
433 throw new RestException(403);
436 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
439 $result = $this->invoice->fetch($id);
441 throw new RestException(404,
'Invoice not found');
444 if (isModEnabled(
"bank")) {
445 if (empty($accountid)) {
446 throw new RestException(400,
'Bank account ID is mandatory');
450 if (empty($payment_mode_id)) {
451 throw new RestException(400,
'Payment mode ID is mandatory');
454 if (
null !== $amount && $amount > 0) {
456 $paymentamount = $amount;
459 $totalpaid = $this->invoice->getSommePaiement();
460 $totaldeposits = $this->invoice->getSumDepositsUsed();
461 $paymentamount =
price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits,
'MT');
467 $multicurrency_amounts = array();
469 $paymentamount = (float)
price2num($paymentamount,
'MT');
471 $amounts[$id] = $paymentamount;
474 $newvalue = (float)
price2num($this->invoice->multicurrency_total_ttc,
'MT');
475 $multicurrency_amounts[$id] = $newvalue;
479 $paiement->datepaye = $datepaye;
480 $paiement->amounts = $amounts;
481 $paiement->multicurrency_amounts = $multicurrency_amounts;
482 $paiement->paiementid = $payment_mode_id;
483 $paiement->paiementcode = (string)
dol_getIdFromCode($this->db, $payment_mode_id,
'c_paiement',
'id',
'code', 1);
484 $paiement->num_payment = $num_payment;
485 $paiement->note_public = $comment;
487 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
488 if ($paiement_id < 0) {
489 $this->db->rollback();
490 throw new RestException(400,
'Payment error : ' . $paiement->error);
493 if (isModEnabled(
"bank")) {
494 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
496 $this->db->rollback();
497 throw new RestException(400,
'Add payment to bank error : ' . $paiement->error);
520 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
521 throw new RestException(403);
524 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
527 $result = $this->invoice->fetch($id);
529 throw new RestException(404,
'Supplier invoice not found');
532 $this->invoice->fetch_lines();
534 foreach ($this->invoice->lines as $line) {
557 public function postLine($id, $request_data =
null)
559 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
560 throw new RestException(403);
564 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
567 $result = $this->invoice->fetch($id);
569 throw new RestException(404,
'Supplier invoice not found');
572 $request_data = (object) $request_data;
574 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
575 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
577 $updateRes = $this->invoice->addline(
578 $request_data->description,
579 $request_data->pu_ht,
580 $request_data->tva_tx,
581 $request_data->localtax1_tx,
582 $request_data->localtax2_tx,
584 $request_data->fk_product,
585 $request_data->remise_percent,
586 $request_data->date_start,
587 $request_data->date_end,
588 $request_data->fk_code_ventilation,
589 $request_data->info_bits,
590 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
591 $request_data->product_type,
594 $request_data->array_options,
595 $request_data->fk_unit,
596 $request_data->origin_id,
597 $request_data->multicurrency_subprice,
598 $request_data->ref_supplier,
599 $request_data->special_code
602 if ($updateRes < 0) {
603 throw new RestException(400,
'Unable to insert the new line. Check your inputs. ' . $this->invoice->error);
624 public function putLine($id, $lineid, $request_data =
null)
626 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
627 throw new RestException(403);
631 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
634 $result = $this->invoice->fetch($id);
636 throw new RestException(404,
'Supplier invoice not found');
639 $request_data = (object) $request_data;
641 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
642 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
644 $updateRes = $this->invoice->updateline(
646 $request_data->description,
647 $request_data->pu_ht,
648 $request_data->tva_tx,
649 $request_data->localtax1_tx,
650 $request_data->localtax2_tx,
652 $request_data->fk_product,
653 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
654 $request_data->info_bits,
655 $request_data->product_type,
656 $request_data->remise_percent,
658 $request_data->date_start,
659 $request_data->date_end,
660 $request_data->array_options,
661 $request_data->fk_unit,
662 $request_data->multicurrency_subprice,
663 $request_data->ref_supplier,
667 if ($updateRes > 0) {
668 $result = $this->
get($id);
669 unset($result->line);
672 throw new RestException(304, $this->invoice->error);
693 if (empty($lineid)) {
694 throw new RestException(400,
'Line ID is mandatory');
697 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
698 throw new RestException(403);
701 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
704 $result = $this->invoice->fetch($id);
706 throw new RestException(404,
'Supplier invoice not found');
711 $updateRes = $this->invoice->deleteLine($lineid);
712 if ($updateRes > 0) {
716 'message' =>
'line '.$lineid.
' deleted'
720 throw new RestException(405, $this->invoice->error);
738 unset(
$object->barcode_type_code);
739 unset(
$object->barcode_type_label);
740 unset(
$object->barcode_type_coder);
756 foreach (SupplierInvoices::$FIELDS as $field) {
757 if (!isset($data[$field])) {
758 throw new RestException(400,
"$field field missing");
760 $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.
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='')
List invoices.
__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.