dolibarr 25.0.0-alpha
api_supplier_invoices.class.php
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2023 Joachim Kueter <git-jk@bloxera.com>
5 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22use Luracast\Restler\RestException;
23
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';
27
36{
40 public static $FIELDS = array(
41 'socid',
42 );
43
47 public $invoice;
48
52 public function __construct()
53 {
54 global $db;
55 $this->db = $db;
56 $this->invoice = new FactureFournisseur($this->db);
57 }
58
70 public function get($id)
71 {
72 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "lire")) {
73 throw new RestException(403);
74 }
75
76 if (!DolibarrApi::_checkAccessToResource('fournisseur', $id, 'facture_fourn', 'facture')) {
77 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
78 }
79
80 $result = $this->invoice->fetch($id);
81 if (!$result) {
82 throw new RestException(404, 'Supplier invoice not found');
83 }
84
85 // Retrieve credit note ids
86 $this->invoice->getListIdAvoirFromInvoice();
87
88 $this->invoice->fetchObjectLinked();
89 return $this->_cleanObjectDatas($this->invoice);
90 }
91
112 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $status = '', $sqlfilters = '', $properties = '', $pagination_data = false)
113 {
114 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "lire")) {
115 throw new RestException(403);
116 }
117
118 $obj_ret = array();
119
120 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
121 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
122
123 // If the internal user must only see his customers, force searching by him
124 $search_sale = 0;
125 if (!DolibarrApiAccess::$user->hasRight("societe", "client", "voir")) {
126 $search_sale = DolibarrApiAccess::$user->id;
127 }
128
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)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
132 $sql .= ' WHERE t.entity IN (' . getEntity('supplier_invoice') . ')';
133 if ($socids) {
134 $sql .= " AND t.fk_soc IN (" . $this->db->sanitize($socids) . ")";
135 }
136 // Filter by status
137 if ($status == 'draft') {
138 $sql .= " AND t.fk_statut IN (0)";
139 }
140 if ($status == 'unpaid') {
141 $sql .= " AND t.fk_statut IN (1)";
142 }
143 if ($status == 'paid') {
144 $sql .= " AND t.fk_statut IN (2)";
145 }
146 if ($status == 'cancelled') {
147 $sql .= " AND t.fk_statut IN (3)";
148 }
149 // Search on sale representative
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);
155 }
156 }
157 // Add sql filters
158 if ($sqlfilters) {
159 $errormessage = '';
160 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
161 if ($errormessage) {
162 throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage);
163 }
164 }
165
166 //this query will return total supplier invoices with the filters given
167 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
168
169 $sql .= $this->db->order($sortfield, $sortorder);
170 if ($limit) {
171 if ($page < 0) {
172 $page = 0;
173 }
174 $offset = $limit * $page;
175
176 $sql .= $this->db->plimit($limit + 1, $offset);
177 }
178
179 $result = $this->db->query($sql);
180 if ($result) {
181 $i = 0;
182 $num = $this->db->num_rows($result);
183 $min = min($num, ($limit <= 0 ? $num : $limit));
184 while ($i < $min) {
185 $obj = $this->db->fetch_object($result);
186 $invoice_static = new FactureFournisseur($this->db);
187 if ($invoice_static->fetch($obj->rowid)) {
188 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($invoice_static), $properties);
189 }
190 $i++;
191 }
192 } else {
193 throw new RestException(503, 'Error when retrieve supplier invoice list : ' . $this->db->lasterror());
194 }
195
196 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
197 if ($pagination_data) {
198 $totalsResult = $this->db->query($sqlTotals);
199 $total = $this->db->fetch_object($totalsResult)->total;
200
201 $tmp = $obj_ret;
202 $obj_ret = [];
203
204 $obj_ret['data'] = $tmp;
205 $obj_ret['pagination'] = [
206 'total' => (int) $total,
207 'page' => $page, //count starts from 0
208 'page_count' => ceil((int) $total / $limit),
209 'limit' => $limit
210 ];
211 }
212
213 return $obj_ret;
214 }
215
232 public function post($request_data = null)
233 {
234 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "creer")) {
235 throw new RestException(403, "Insufficiant rights");
236 }
237
238 if (!is_array($request_data)) {
239 $request_data = array();
240 }
241
242 // Check mandatory fields (not using output, only possible exception is important)
243 $this->_validate($request_data);
244
245 foreach ($request_data as $field => $value) {
246 if ($field === 'caller') {
247 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
248 $this->invoice->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
249 continue;
250 }
251
252 $this->invoice->$field = $this->_checkValForAPI($field, $value, $this->invoice);
253 }
254 if (!array_key_exists('date', $request_data)) {
255 $this->invoice->date = dol_now();
256 }
257
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));
260 }
261 return $this->invoice->id;
262 }
263
276 public function put($id, $request_data = null)
277 {
278 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "creer")) {
279 throw new RestException(403);
280 }
281
282 if (!DolibarrApi::_checkAccessToResource('fournisseur', $id, 'facture_fourn', 'facture')) {
283 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
284 }
285
286 $result = $this->invoice->fetch($id);
287 if (!$result) {
288 throw new RestException(404, 'Supplier invoice not found');
289 }
290
291 if (!is_array($request_data)) {
292 $request_data = array();
293 }
294
295 foreach ($request_data as $field => $value) {
296 if ($field == 'id') {
297 continue;
298 }
299 if ($field === 'caller') {
300 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
301 $this->invoice->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
302 continue;
303 }
304 if ($field == 'array_options' && is_array($value)) {
305 foreach ($value as $index => $val) {
306 $this->invoice->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->invoice);
307 }
308 continue;
309 }
310
311 $this->invoice->$field = $this->_checkValForAPI($field, $value, $this->invoice);
312 }
313
314 if ($this->invoice->update(DolibarrApiAccess::$user)) {
315 return $this->get($id);
316 }
317
318 return false;
319 }
320
334 public function delete($id)
335 {
336 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "supprimer")) {
337 throw new RestException(403);
338 }
339 if (!DolibarrApi::_checkAccessToResource('fournisseur', $id, 'facture_fourn', 'facture')) {
340 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
341 }
342 $result = $this->invoice->fetch($id);
343 if (!$result) {
344 throw new RestException(404, 'Supplier invoice not found');
345 }
346
347 $result = $this->invoice->delete(DolibarrApiAccess::$user);
348 if ($result < 0) {
349 throw new RestException(500, 'Error when deleting invoice');
350 } elseif ($result == 0) {
351 throw new RestException(403, 'Invoice not erasable');
352 }
353
354 return array(
355 'success' => array(
356 'code' => 200,
357 'message' => 'Supplier invoice deleted'
358 )
359 );
360 }
361
381 public function validate($id, $idwarehouse = 0, $notrigger = 0)
382 {
383 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "creer")) {
384 throw new RestException(403);
385 }
386
387 if (!DolibarrApi::_checkAccessToResource('fournisseur', $id, 'facture_fourn', 'facture')) {
388 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
389 }
390
391 $result = $this->invoice->fetch($id);
392 if (!$result) {
393 throw new RestException(404, 'Invoice not found');
394 }
395
396 $result = $this->invoice->validate(DolibarrApiAccess::$user, '', $idwarehouse, $notrigger);
397 if ($result == 0) {
398 throw new RestException(304, 'Error nothing done. The invoice is already validated');
399 }
400 if ($result < 0) {
401 throw new RestException(500, 'Error when validating Invoice: ' . $this->invoice->errorsToString());
402 }
403
404 return array(
405 'success' => array(
406 'code' => 200,
407 'message' => 'Invoice validated (Ref=' . $this->invoice->ref . ')'
408 )
409 );
410 }
411
427 public function settodraft($id, $idwarehouse = -1, $notrigger = 0)
428 {
429 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "creer")) {
430 throw new RestException(403);
431 }
432 $result = $this->invoice->fetch($id);
433 if (!$result) {
434 throw new RestException(404, 'Invoice not found');
435 }
436
437 if (!DolibarrApi::_checkAccessToResource('fournisseur', $id, 'facture_fourn', 'facture')) {
438 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
439 }
440
441 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
442 if ($result == 0) {
443 throw new RestException(304, 'Nothing done.');
444 }
445 if ($result < 0) {
446 throw new RestException(500, 'Error : ' . $this->invoice->errorsToString());
447 }
448
449 $result = $this->invoice->fetch($id);
450 if (!$result) {
451 throw new RestException(404, 'Invoice not found');
452 }
453
454 return $this->_cleanObjectDatas($this->invoice);
455 }
456
472 public function getPayments($id)
473 {
474 if (empty($id)) {
475 throw new RestException(400, 'Invoice ID is mandatory');
476 }
477
478 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "lire")) {
479 throw new RestException(403);
480 }
481 if (!DolibarrApi::_checkAccessToResource('fournisseur', $id, 'facture_fourn', 'facture')) {
482 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
483 }
484
485 $result = $this->invoice->fetch($id);
486 if (!$result) {
487 throw new RestException(404, 'Invoice not found');
488 }
489
490 $result = $this->invoice->getListOfPayments();
491 if ($this->invoice->errorsToString() !== '') {
492 throw new RestException(405, $this->invoice->errorsToString());
493 }
494
495 return $result;
496 }
497
498
521 public function addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment = '', $comment = '', $chqemetteur = '', $chqbank = '', $amount = null)
522 {
523 if (empty($id)) {
524 throw new RestException(400, 'Invoice ID is mandatory');
525 }
526
527 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "creer")) {
528 throw new RestException(403);
529 }
530 if (!DolibarrApi::_checkAccessToResource('fournisseur', $id, 'facture_fourn', 'facture')) {
531 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
532 }
533
534 $result = $this->invoice->fetch($id);
535 if (!$result) {
536 throw new RestException(404, 'Invoice not found');
537 }
538
539 if (isModEnabled("bank")) {
540 if (empty($accountid)) {
541 throw new RestException(400, 'Bank account ID is mandatory');
542 }
543 }
544
545 if (empty($payment_mode_id)) {
546 throw new RestException(400, 'Payment mode ID is mandatory');
547 }
548
549 if (null !== $amount && $amount > 0) {
550 // We use the amount given in parameter
551 $paymentamount = $amount;
552 } else {
553 // We calculate the remain to pay, and use it as amount
554 $totalpaid = $this->invoice->getSommePaiement();
555 $totaldeposits = $this->invoice->getSumDepositsUsed();
556 $paymentamount = price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits, 'MT');
557 }
558
559 $this->db->begin();
560
561 $amounts = array();
562 $multicurrency_amounts = array();
563
564 $paymentamount = (float) price2num($paymentamount, 'MT');
565
566 $amounts[$id] = $paymentamount;
567
568 // Multicurrency
569 // getWay() switches the payment to the invoice currency as soon as a multicurrency amount is set, so
570 // this value must match the partial amount, not always the full invoice TTC. When a partial amount was
571 // requested, convert it at the invoice rate (multicurrency_total_ttc / total_ttc); otherwise use the
572 // full multicurrency TTC (full payment).
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');
575 } else {
576 $newvalue = (float) price2num($this->invoice->multicurrency_total_ttc, 'MT');
577 }
578 $multicurrency_amounts[$id] = $newvalue;
579
580 // Creation of payment line
581 $paiement = new PaiementFourn($this->db);
582 $paiement->datepaye = $datepaye;
583 $paiement->amounts = $amounts; // Array with all payments dispatching with invoice id
584 $paiement->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching
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;
589
590 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices == 'yes' ? 1 : 0)); // This include closing invoices
591 if ($paiement_id < 0) {
592 $this->db->rollback();
593 throw new RestException(400, 'Payment error : ' . $paiement->errorsToString());
594 }
595
596 if (isModEnabled("bank")) {
597 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user, 'payment_supplier', '(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
598 if ($result < 0) {
599 $this->db->rollback();
600 throw new RestException(400, 'Add payment to bank error : ' . $paiement->errorsToString());
601 }
602 }
603
604 $this->db->commit();
605
606 return $paiement_id;
607 }
608
624 public function settopaid($id, $close_code = '', $close_note = '')
625 {
626 if (!DolibarrApiAccess::$user->hasRight('fournisseur', 'facture', 'creer')) {
627 throw new RestException(403);
628 }
629
630 $result = $this->invoice->fetch($id);
631 if (!$result) {
632 throw new RestException(404, 'Supplier invoice not found');
633 }
634
635 if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
636 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
637 }
638
639 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
640 if ($result == 0) {
641 throw new RestException(304, 'Error nothing done. Maybe object is already paid or not payable.');
642 }
643 if ($result < 0) {
644 throw new RestException(500, 'Error: ' . $this->invoice->errorsToString());
645 }
646
647 $result = $this->invoice->fetch($id);
648 if (!$result) {
649 throw new RestException(404, 'Supplier invoice not found');
650 }
651
652 return $this->_cleanObjectDatas($this->invoice);
653 }
654
668 public function settounpaid($id)
669 {
670 if (!DolibarrApiAccess::$user->hasRight('fournisseur', 'facture', 'creer')) {
671 throw new RestException(403);
672 }
673
674 $result = $this->invoice->fetch($id);
675 if (!$result) {
676 throw new RestException(404, 'Supplier invoice not found');
677 }
678
679 if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->invoice->id, 'facture_fourn', 'facture')) {
680 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
681 }
682
683 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
684 if ($result == 0) {
685 throw new RestException(304, 'Nothing done.');
686 }
687 if ($result < 0) {
688 throw new RestException(500, 'Error: ' . $this->invoice->errorsToString());
689 }
690
691 $result = $this->invoice->fetch($id);
692 if (!$result) {
693 throw new RestException(404, 'Supplier invoice not found');
694 }
695
696 return $this->_cleanObjectDatas($this->invoice);
697 }
698
713 public function getLines($id)
714 {
715 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "creer")) {
716 throw new RestException(403);
717 }
718 if (!DolibarrApi::_checkAccessToResource('fournisseur', $id, 'facture_fourn', 'facture')) {
719 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
720 }
721
722 $result = $this->invoice->fetch($id);
723 if (!$result) {
724 throw new RestException(404, 'Supplier invoice not found');
725 }
726
727 $this->invoice->fetch_lines();
728 $result = array();
729 foreach ($this->invoice->lines as $line) {
730 array_push($result, $this->_cleanObjectDatas($line));
731 }
732 return $result;
733 }
734
754 public function postLine($id, $request_data = null)
755 {
756 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "creer")) {
757 throw new RestException(403);
758 }
759
760 if (!DolibarrApi::_checkAccessToResource('fournisseur', $id, 'facture_fourn', 'facture')) {
761 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
762 }
763
764 $result = $this->invoice->fetch($id);
765 if (!$result) {
766 throw new RestException(404, 'Supplier invoice not found');
767 }
768
769 $request_data = (object) $request_data;
770
771 $request_data->description = sanitizeVal($request_data->description, 'restricthtml');
772 $request_data->ref_supplier = sanitizeVal($request_data->ref_supplier);
773
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,
780 $request_data->qty,
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,
789 $request_data->rang,
790 0,
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
797 );
798
799 if ($updateRes < 0) {
800 throw new RestException(400, 'Unable to insert the new line. Check your inputs. ' . $this->invoice->errorsToString());
801 }
802
803 return $updateRes;
804 }
805
823 public function putLine($id, $lineid, $request_data = null)
824 {
825 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "creer")) {
826 throw new RestException(403);
827 }
828
829 if (!DolibarrApi::_checkAccessToResource('fournisseur', $id, 'facture_fourn', 'facture')) {
830 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
831 }
832
833 $result = $this->invoice->fetch($id);
834 if (!$result) {
835 throw new RestException(404, 'Supplier invoice not found');
836 }
837
838 $invoiceline = new SupplierInvoiceLine($this->db);
839 if ($invoiceline->fetch($lineid) <= 0) {
840 throw new RestException(404, 'Supplier invoice line not found');
841 }
842 if ($invoiceline->fk_facture_fourn != $this->invoice->id) {
843 throw new RestException(403, 'Line does not belong to this supplier invoice');
844 }
845
846 $request_data = (object) $request_data;
847
848 $request_data->description = sanitizeVal($request_data->description, 'restricthtml');
849 $request_data->ref_supplier = sanitizeVal($request_data->ref_supplier);
850
851 $updateRes = $this->invoice->updateline(
852 $lineid,
853 $request_data->description,
854 $request_data->pu_ht,
855 $request_data->tva_tx,
856 $request_data->localtax1_tx,
857 $request_data->localtax2_tx,
858 $request_data->qty,
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,
864 0,
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,
871 $request_data->rang
872 );
873
874 if ($updateRes > 0) {
875 $result = $this->get($id);
876 unset($result->line);
877 return $this->_cleanObjectDatas($result);
878 } else {
879 throw new RestException(304, $this->invoice->errorsToString());
880 }
881 }
882
900 public function deleteLine($id, $lineid)
901 {
902 if (empty($lineid)) {
903 throw new RestException(400, 'Line ID is mandatory');
904 }
905
906 if (!DolibarrApiAccess::$user->hasRight("fournisseur", "facture", "creer")) {
907 throw new RestException(403);
908 }
909 if (!DolibarrApi::_checkAccessToResource('fournisseur', $id, 'facture_fourn', 'facture')) {
910 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
911 }
912
913 $result = $this->invoice->fetch($id);
914 if (!$result) {
915 throw new RestException(404, 'Supplier invoice not found');
916 }
917
918 $invoiceline = new SupplierInvoiceLine($this->db);
919 if ($invoiceline->fetch($lineid) <= 0) {
920 throw new RestException(404, 'Supplier invoice line not found');
921 }
922 if ($invoiceline->fk_facture_fourn != $this->invoice->id) {
923 throw new RestException(403, 'Line does not belong to this supplier invoice');
924 }
925
926 $updateRes = $this->invoice->deleteLine($lineid);
927 if ($updateRes > 0) {
928 return array(
929 'success' => array(
930 'code' => 200,
931 'message' => 'line '.$lineid.' deleted'
932 )
933 );
934 } else {
935 throw new RestException(405, $this->invoice->errorsToString());
936 }
937 }
938
939 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
949 protected function _cleanObjectDatas($object)
950 {
951 // phpcs:enable
952 $object = parent::_cleanObjectDatas($object);
953
954 unset($object->rowid);
955 unset($object->barcode_type);
956 unset($object->barcode_type_code);
957 unset($object->barcode_type_label);
958 unset($object->barcode_type_coder);
959
960 return $object;
961 }
962
971 private function _validate($data)
972 {
973 if ($data === null) {
974 $data = array();
975 }
976 $invoice = array();
977 foreach (SupplierInvoices::$FIELDS as $field) {
978 if (!isset($data[$field])) {
979 throw new RestException(400, "$field field missing");
980 }
981 $invoice[$field] = $data[$field];
982 }
983 return $invoice;
984 }
985}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:35
_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.
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.