19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
22 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/paiementfourn.class.php';
37 static $FIELDS = array(
66 public function get($id)
68 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->lire) {
69 throw new RestException(401);
72 $result = $this->invoice->fetch($id);
74 throw new RestException(404,
'Supplier invoice not found');
78 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
81 $this->invoice->fetchObjectLinked();
101 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'')
108 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
112 if (!DolibarrApiAccess::$user->rights->societe->client->voir) $search_sale = DolibarrApiAccess::$user->id;
114 $sql =
"SELECT t.rowid";
116 if (!DolibarrApiAccess::$user->rights->societe->client->voir || $search_sale > 0) $sql .=
", sc.fk_soc, sc.fk_user";
117 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as t";
120 if (!DolibarrApiAccess::$user->rights->societe->client->voir || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
122 $sql .=
' WHERE t.entity IN ('.getEntity(
'supplier_invoice').
')';
123 if (!DolibarrApiAccess::$user->rights->societe->client->voir || $search_sale > 0) $sql .=
" AND t.fk_soc = sc.fk_soc";
124 if ($socids) $sql .=
" AND t.fk_soc IN (".$socids.
")";
125 if ($search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
128 if ($status ==
'draft') {
129 $sql .=
" AND t.fk_statut IN (0)";
131 if ($status ==
'unpaid') {
132 $sql .=
" AND t.fk_statut IN (1)";
134 if ($status ==
'paid') {
135 $sql .=
" AND t.fk_statut IN (2)";
137 if ($status ==
'cancelled') {
138 $sql .=
" AND t.fk_statut IN (3)";
141 if ($search_sale > 0) {
142 $sql .=
" AND sc.fk_user = ".$search_sale;
149 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
151 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
152 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
155 $sql .= $this->
db->order($sortfield, $sortorder);
161 $offset = $limit * $page;
163 $sql .= $this->
db->plimit($limit + 1, $offset);
166 $result = $this->
db->query($sql);
169 $num = $this->
db->num_rows($result);
170 $min = min($num, ($limit <= 0 ? $num : $limit));
173 $obj = $this->
db->fetch_object($result);
175 if ($invoice_static->fetch($obj->rowid)) {
181 throw new RestException(503,
'Error when retrieve supplier invoice list : '.$this->
db->lasterror());
183 if (!count($obj_ret)) {
184 throw new RestException(404,
'No supplier invoice found');
199 public function post($request_data = null)
201 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
202 throw new RestException(401,
"Insuffisant rights");
205 $result = $this->
_validate($request_data);
207 foreach ($request_data as $field => $value) {
208 $this->invoice->$field = $value;
210 if (!array_key_exists(
'date', $request_data)) {
211 $this->invoice->date =
dol_now();
214 if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
215 throw new RestException(500,
"Error creating order", array_merge(array($this->invoice->error), $this->invoice->errors));
217 return $this->invoice->id;
231 public function put($id, $request_data = null)
233 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
234 throw new RestException(401);
237 $result = $this->invoice->fetch($id);
239 throw new RestException(404,
'Supplier invoice not found');
243 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
246 foreach ($request_data as $field => $value) {
247 if ($field ==
'id')
continue;
248 $this->invoice->$field = $value;
251 if ($this->invoice->update($id, DolibarrApiAccess::$user))
252 return $this->
get($id);
268 public function delete($id)
270 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->supprimer) {
271 throw new RestException(401);
273 $result = $this->invoice->fetch($id);
275 throw new RestException(404,
'Supplier invoice not found');
279 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
282 if ($this->invoice->delete(DolibarrApiAccess::$user) < 0)
284 throw new RestException(500);
290 'message' =>
'Supplier invoice deleted'
312 public function validate($id, $idwarehouse = 0, $notrigger = 0)
314 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
315 throw new RestException(401);
317 $result = $this->invoice->fetch($id);
319 throw new RestException(404,
'Invoice not found');
323 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
326 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
328 throw new RestException(304,
'Error nothing done. The invoice is already validated');
331 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
337 'message' =>
'Invoice validated (Ref='.$this->invoice->ref.
')'
357 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->lire) {
358 throw new RestException(401);
361 throw new RestException(400,
'Invoice ID is mandatory');
365 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
368 $result = $this->invoice->fetch($id);
370 throw new RestException(404,
'Invoice not found');
373 $result = $this->invoice->getListOfPayments();
375 throw new RestException(405, $this->invoice->error);
402 public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
406 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
407 throw new RestException(403);
410 throw new RestException(400,
'Invoice ID is mandatory');
414 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
417 if (!empty($conf->banque->enabled)) {
418 if (empty($accountid)) {
419 throw new RestException(400,
'Bank account ID is mandatory');
423 if (empty($payment_mode_id)) {
424 throw new RestException(400,
'Payment mode ID is mandatory');
428 $result = $this->invoice->fetch($id);
430 throw new RestException(404,
'Invoice not found');
434 $totalpaye = $this->invoice->getSommePaiement();
435 $totaldeposits = $this->invoice->getSumDepositsUsed();
436 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaye - $totaldeposits,
'MT');
441 $multicurrency_amounts = array();
443 $resteapayer =
price2num($resteapayer,
'MT');
444 $amounts[$id] = $resteapayer;
447 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
448 $multicurrency_amounts[$id] = $newvalue;
452 $paiement->datepaye = $datepaye;
453 $paiement->amounts = $amounts;
454 $paiement->multicurrency_amounts = $multicurrency_amounts;
455 $paiement->paiementid = $payment_mode_id;
456 $paiement->paiementcode =
dol_getIdFromCode($this->
db, $payment_mode_id,
'c_paiement',
'id',
'code', 1);
457 $paiement->oper = $paiement->paiementcode;
458 $paiement->num_payment = $num_payment;
459 $paiement->note_public = $comment;
461 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
462 if ($paiement_id < 0)
464 $this->
db->rollback();
465 throw new RestException(400,
'Payment error : '.$paiement->error);
468 if (!empty($conf->banque->enabled)) {
469 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
472 $this->
db->rollback();
473 throw new RestException(400,
'Add payment to bank error : '.$paiement->error);
493 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
494 throw new RestException(401);
497 $result = $this->invoice->fetch($id);
499 throw new RestException(404,
'Supplier invoice not found');
503 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
505 $this->invoice->fetch_lines();
507 foreach ($this->invoice->lines as $line) {
523 public function postLine($id, $request_data = null)
525 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
526 throw new RestException(401);
529 $result = $this->invoice->fetch($id);
531 throw new RestException(404,
'Supplier invoice not found');
535 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
537 $request_data = (object) $request_data;
539 $updateRes = $this->invoice->addline(
540 $request_data->description,
541 $request_data->pu_ht,
542 $request_data->tva_tx,
543 $request_data->localtax1_tx,
544 $request_data->localtax2_tx,
546 $request_data->fk_product,
547 $request_data->remise_percent,
548 $request_data->date_start,
549 $request_data->date_end,
550 $request_data->ventil,
551 $request_data->info_bits,
553 $request_data->product_type,
556 $request_data->array_options,
557 $request_data->fk_unit,
558 $request_data->origin_id,
559 $request_data->multicurrency_subprice,
560 $request_data->ref_supplier,
561 $request_data->special_code
564 if ($updateRes < 0) {
565 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
586 public function putLine($id, $lineid, $request_data = null)
588 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
589 throw new RestException(401);
592 $result = $this->invoice->fetch($id);
594 throw new RestException(404,
'Supplier invoice not found');
598 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
600 $request_data = (object) $request_data;
601 $updateRes = $this->invoice->updateline(
603 $request_data->description,
604 $request_data->pu_ht,
605 $request_data->tva_tx,
606 $request_data->localtax1_tx,
607 $request_data->localtax2_tx,
609 $request_data->fk_product,
611 $request_data->info_bits,
612 $request_data->product_type,
613 $request_data->remise_percent,
615 $request_data->date_start,
616 $request_data->date_end,
617 $request_data->array_options,
618 $request_data->fk_unit,
619 $request_data->multicurrency_subprice,
620 $request_data->ref_supplier
623 if ($updateRes > 0) {
624 $result = $this->
get($id);
625 unset($result->line);
628 throw new RestException(304, $this->invoice->error);
649 if (!DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
650 throw new RestException(401);
653 $result = $this->invoice->fetch($id);
655 throw new RestException(404,
'Supplier invoice not found');
658 if (empty($lineid)) {
659 throw new RestException(400,
'Line ID is mandatory');
663 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
668 $updateRes = $this->invoice->deleteline($lineid);
669 if ($updateRes > 0) {
670 return $this->
get($id);
672 throw new RestException(405, $this->invoice->error);
686 $object = parent::_cleanObjectDatas($object);
688 unset($object->rowid);
689 unset($object->barcode_type);
690 unset($object->barcode_type_code);
691 unset($object->barcode_type_label);
692 unset($object->barcode_type_coder);
708 foreach (SupplierInvoices::$FIELDS as $field) {
709 if (!isset($data[$field]))
710 throw new RestException(400,
"$field field missing");
711 $invoice[$field] = $data[$field];
_validate($data)
Validate fields before create or update object.
putLine($id, $lineid, $request_data=null)
Update a line to a given supplier invoice.
dol_now($mode= 'auto')
Return date for now.
__construct()
Constructor.
post($request_data=null)
Create supplier invoice object.
postLine($id, $request_data=null)
Add a line to given supplier invoice.
Class to manage suppliers invoices.
getPayments($id)
Get list of payments of a given supplier invoice.
_cleanObjectDatas($object)
Clean sensible object datas.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an invoice.
deleteLine($id, $lineid)
Deletes a line of a given supplier invoice.
put($id, $request_data=null)
Update supplier invoice.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $thirdparty_ids= '', $status= '', $sqlfilters= '')
List invoices.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
dol_getIdFromCode($db, $key, $tablename, $fieldkey= 'code', $fieldid= 'id', $entityfilter=0)
Return an id or code from a code or id.
addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment= '', $comment= '', $chqemetteur= '', $chqbank= '')
Add payment line to a specific supplier invoice with the remain to pay as amount. ...
getLines($id)
Get lines of a supplier invoice.
Class to manage payments for supplier invoices.