dolibarr 19.0.4
api_invoices.class.php
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2020 Thibault FOUCART <support@ptibogxiv.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use Luracast\Restler\RestException;
20
21require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
22require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture-rec.class.php';
23
24
31class Invoices extends DolibarrApi
32{
37 public static $FIELDS = array(
38 'socid',
39 );
40
44 private $invoice;
45
49 private $template_invoice;
50
51
55 public function __construct()
56 {
57 global $db, $conf;
58 $this->db = $db;
59 $this->invoice = new Facture($this->db);
60 $this->template_invoice = new FactureRec($this->db);
61 }
62
74 public function get($id, $contact_list = 1)
75 {
76 return $this->_fetch($id, '', '', $contact_list);
77 }
78
92 public function getByRef($ref, $contact_list = 1)
93 {
94 return $this->_fetch('', $ref, '', $contact_list);
95 }
96
110 public function getByRefExt($ref_ext, $contact_list = 1)
111 {
112 return $this->_fetch('', '', $ref_ext, $contact_list);
113 }
114
128 private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
129 {
130 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
131 throw new RestException(401);
132 }
133
134 $result = $this->invoice->fetch($id, $ref, $ref_ext);
135 if (!$result) {
136 throw new RestException(404, 'Invoice not found');
137 }
138
139 // Get payment details
140 $this->invoice->totalpaid = $this->invoice->getSommePaiement();
141 $this->invoice->totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
142 $this->invoice->totaldeposits = $this->invoice->getSumDepositsUsed();
143 $this->invoice->remaintopay = price2num($this->invoice->total_ttc - $this->invoice->totalpaid - $this->invoice->totalcreditnotes - $this->invoice->totaldeposits, 'MT');
144
145 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
146 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
147 }
148
149 // Add external contacts ids
150 if ($contact_list > -1) {
151 $tmparray = $this->invoice->liste_contact(-1, 'external', $contact_list);
152 if (is_array($tmparray)) {
153 $this->invoice->contacts_ids = $tmparray;
154 }
155 }
156
157 $this->invoice->fetchObjectLinked();
158
159 return $this->_cleanObjectDatas($this->invoice);
160 }
161
180 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $status = '', $sqlfilters = '', $properties = '')
181 {
182 global $db, $conf;
183
184 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
185 throw new RestException(401);
186 }
187
188 $obj_ret = array();
189
190 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
191 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
192
193 // If the internal user must only see his customers, force searching by him
194 $search_sale = 0;
195 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
196 $search_sale = DolibarrApiAccess::$user->id;
197 }
198
199 $sql = "SELECT t.rowid";
200 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
201 $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
202 }
203 $sql .= " FROM ".MAIN_DB_PREFIX."facture AS t LEFT JOIN ".MAIN_DB_PREFIX."facture_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
204
205 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
206 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
207 }
208
209 $sql .= ' WHERE t.entity IN ('.getEntity('invoice').')';
210 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
211 $sql .= " AND t.fk_soc = sc.fk_soc";
212 }
213 if ($socids) {
214 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
215 }
216
217 if ($search_sale > 0) {
218 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
219 }
220
221 // Filter by status
222 if ($status == 'draft') {
223 $sql .= " AND t.fk_statut IN (0)";
224 }
225 if ($status == 'unpaid') {
226 $sql .= " AND t.fk_statut IN (1)";
227 }
228 if ($status == 'paid') {
229 $sql .= " AND t.fk_statut IN (2)";
230 }
231 if ($status == 'cancelled') {
232 $sql .= " AND t.fk_statut IN (3)";
233 }
234 // Insert sale filter
235 if ($search_sale > 0) {
236 $sql .= " AND sc.fk_user = ".((int) $search_sale);
237 }
238 // Add sql filters
239 if ($sqlfilters) {
240 $errormessage = '';
241 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
242 if ($errormessage) {
243 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
244 }
245 }
246
247 $sql .= $this->db->order($sortfield, $sortorder);
248 if ($limit) {
249 if ($page < 0) {
250 $page = 0;
251 }
252 $offset = $limit * $page;
253
254 $sql .= $this->db->plimit($limit + 1, $offset);
255 }
256
257 $result = $this->db->query($sql);
258 if ($result) {
259 $i = 0;
260 $num = $this->db->num_rows($result);
261 $min = min($num, ($limit <= 0 ? $num : $limit));
262 while ($i < $min) {
263 $obj = $this->db->fetch_object($result);
264 $invoice_static = new Facture($this->db);
265 if ($invoice_static->fetch($obj->rowid)) {
266 // Get payment details
267 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
268 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
269 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
270 $invoice_static->remaintopay = price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits, 'MT');
271
272 // Add external contacts ids
273 $tmparray = $invoice_static->liste_contact(-1, 'external', 1);
274 if (is_array($tmparray)) {
275 $invoice_static->contacts_ids = $tmparray;
276 }
277 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($invoice_static), $properties);
278 }
279 $i++;
280 }
281 } else {
282 throw new RestException(503, 'Error when retrieve invoice list : '.$this->db->lasterror());
283 }
284
285 return $obj_ret;
286 }
287
294 public function post($request_data = null)
295 {
296 if (!DolibarrApiAccess::$user->rights->facture->creer) {
297 throw new RestException(401, "Insuffisant rights");
298 }
299 // Check mandatory fields
300 $result = $this->_validate($request_data);
301
302 foreach ($request_data as $field => $value) {
303 if ($field === 'caller') {
304 // 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 whith the caller
305 $this->invoice->context['caller'] = $request_data['caller'];
306 continue;
307 }
308
309 $this->invoice->$field = $value;
310 }
311 if (!array_key_exists('date', $request_data)) {
312 $this->invoice->date = dol_now();
313 }
314 /* We keep lines as an array
315 if (isset($request_data["lines"])) {
316 $lines = array();
317 foreach ($request_data["lines"] as $line) {
318 array_push($lines, (object) $line);
319 }
320 $this->invoice->lines = $lines;
321 }*/
322
323 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data["date_lim_reglement"]) ? 0 : $request_data["date_lim_reglement"])) < 0) {
324 throw new RestException(500, "Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
325 }
326 return ((int) $this->invoice->id);
327 }
328
343 public function createInvoiceFromOrder($orderid)
344 {
345 require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
346
347 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
348 throw new RestException(401);
349 }
350 if (!DolibarrApiAccess::$user->rights->facture->creer) {
351 throw new RestException(401);
352 }
353 if (empty($orderid)) {
354 throw new RestException(400, 'Order ID is mandatory');
355 }
356 if (!DolibarrApi::_checkAccessToResource('commande', $orderid)) {
357 throw new RestException(403, 'Access not allowed on order for login '.DolibarrApiAccess::$user->login);
358 }
359
360 $order = new Commande($this->db);
361 $result = $order->fetch($orderid);
362 if (!$result) {
363 throw new RestException(404, 'Order not found');
364 }
365
366 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
367 if ($result < 0) {
368 throw new RestException(405, $this->invoice->error);
369 }
370 $this->invoice->fetchObjectLinked();
371 return $this->_cleanObjectDatas($this->invoice);
372 }
373
382 public function getLines($id)
383 {
384 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
385 throw new RestException(401);
386 }
387
388 $result = $this->invoice->fetch($id);
389 if (!$result) {
390 throw new RestException(404, 'Invoice not found');
391 }
392
393 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
394 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
395 }
396 $this->invoice->getLinesArray();
397 $result = array();
398 foreach ($this->invoice->lines as $line) {
399 array_push($result, $this->_cleanObjectDatas($line));
400 }
401 return $result;
402 }
403
418 public function putLine($id, $lineid, $request_data = null)
419 {
420 if (!DolibarrApiAccess::$user->rights->facture->creer) {
421 throw new RestException(401);
422 }
423
424 $result = $this->invoice->fetch($id);
425 if (!$result) {
426 throw new RestException(404, 'Invoice not found');
427 }
428
429 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
430 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
431 }
432
433 $request_data = (object) $request_data;
434
435 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
436 $request_data->label = sanitizeVal($request_data->label);
437
438 $updateRes = $this->invoice->updateline(
439 $lineid,
440 $request_data->desc,
441 $request_data->subprice,
442 $request_data->qty,
443 $request_data->remise_percent,
444 $request_data->date_start,
445 $request_data->date_end,
446 $request_data->tva_tx,
447 $request_data->localtax1_tx,
448 $request_data->localtax2_tx,
449 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
450 $request_data->info_bits,
451 $request_data->product_type,
452 $request_data->fk_parent_line,
453 0,
454 $request_data->fk_fournprice,
455 $request_data->pa_ht,
456 $request_data->label,
457 $request_data->special_code,
458 $request_data->array_options,
459 $request_data->situation_percent,
460 $request_data->fk_unit,
461 $request_data->multicurrency_subprice,
462 0,
463 $request_data->ref_ext,
464 $request_data->rang
465 );
466
467 if ($updateRes > 0) {
468 $result = $this->get($id);
469 unset($result->line);
470 return $this->_cleanObjectDatas($result);
471 } else {
472 throw new RestException(304, $this->invoice->error);
473 }
474 }
475
489 public function postContact($id, $contactid, $type)
490 {
491 if (!DolibarrApiAccess::$user->rights->facture->creer) {
492 throw new RestException(401);
493 }
494
495 $result = $this->invoice->fetch($id);
496
497 if (!$result) {
498 throw new RestException(404, 'Invoice not found');
499 }
500
501 if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
502 throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
503 }
504
505 if (!DolibarrApi::_checkAccessToResource('invoice', $this->invoice->id)) {
506 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
507 }
508
509 $result = $this->invoice->add_contact($contactid, $type, 'external');
510
511 if (!$result) {
512 throw new RestException(500, 'Error when added the contact');
513 }
514
515 return array(
516 'success' => array(
517 'code' => 200,
518 'message' => 'Contact linked to the invoice'
519 )
520 );
521 }
522
537 public function deleteContact($id, $contactid, $type)
538 {
539 if (!DolibarrApiAccess::$user->rights->facture->creer) {
540 throw new RestException(401);
541 }
542
543 $result = $this->invoice->fetch($id);
544
545 if (!$result) {
546 throw new RestException(404, 'Invoice not found');
547 }
548
549 if (!DolibarrApi::_checkAccessToResource('invoice', $this->invoice->id)) {
550 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
551 }
552
553 $contacts = $this->invoice->liste_contact();
554
555 foreach ($contacts as $contact) {
556 if ($contact['id'] == $contactid && $contact['code'] == $type) {
557 $result = $this->invoice->delete_contact($contact['rowid']);
558
559 if (!$result) {
560 throw new RestException(500, 'Error when deleted the contact');
561 }
562 }
563 }
564
565 return $this->_cleanObjectDatas($this->invoice);
566 }
567
582 public function deleteLine($id, $lineid)
583 {
584 if (!DolibarrApiAccess::$user->rights->facture->creer) {
585 throw new RestException(401);
586 }
587 if (empty($lineid)) {
588 throw new RestException(400, 'Line ID is mandatory');
589 }
590
591 if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
592 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
593 }
594
595 $result = $this->invoice->fetch($id);
596 if (!$result) {
597 throw new RestException(404, 'Invoice not found');
598 }
599
600 $updateRes = $this->invoice->deleteline($lineid, $id);
601 if ($updateRes > 0) {
602 return $this->get($id);
603 } else {
604 throw new RestException(405, $this->invoice->error);
605 }
606 }
607
615 public function put($id, $request_data = null)
616 {
617 if (!DolibarrApiAccess::$user->rights->facture->creer) {
618 throw new RestException(401);
619 }
620
621 $result = $this->invoice->fetch($id);
622 if (!$result) {
623 throw new RestException(404, 'Invoice not found');
624 }
625
626 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
627 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
628 }
629
630 foreach ($request_data as $field => $value) {
631 if ($field == 'id') {
632 continue;
633 }
634 if ($field === 'caller') {
635 // 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 whith the caller
636 $this->invoice->context['caller'] = $request_data['caller'];
637 continue;
638 }
639
640 if ($field == 'array_options' && is_array($value)) {
641 foreach ($value as $index => $val) {
642 $this->invoice->array_options[$index] = $this->_checkValForAPI($field, $val, $this->invoice);
643 }
644 continue;
645 }
646 $this->invoice->$field = $this->_checkValForAPI($field, $value, $this->invoice);
647 }
648
649 // update bank account
650 if (!empty($this->invoice->fk_account)) {
651 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
652 throw new RestException(400, $this->invoice->error);
653 }
654 }
655
656 if ($this->invoice->update(DolibarrApiAccess::$user) > 0) {
657 return $this->get($id);
658 } else {
659 throw new RestException(500, $this->invoice->error);
660 }
661 }
662
669 public function delete($id)
670 {
671 if (!DolibarrApiAccess::$user->hasRight('facture', 'supprimer')) {
672 throw new RestException(401);
673 }
674 $result = $this->invoice->fetch($id);
675 if (!$result) {
676 throw new RestException(404, 'Invoice not found');
677 }
678
679 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
680 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
681 }
682
683 $result = $this->invoice->delete(DolibarrApiAccess::$user);
684 if ($result < 0) {
685 throw new RestException(500, 'Error when deleting invoice');
686 } elseif ($result == 0) {
687 throw new RestException(403, 'Invoice not erasable');
688 }
689
690 return array(
691 'success' => array(
692 'code' => 200,
693 'message' => 'Invoice deleted'
694 )
695 );
696 }
697
721 public function postLine($id, $request_data = null)
722 {
723 if (!DolibarrApiAccess::$user->rights->facture->creer) {
724 throw new RestException(401);
725 }
726
727 $result = $this->invoice->fetch($id);
728 if (!$result) {
729 throw new RestException(404, 'Invoice not found');
730 }
731
732 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
733 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
734 }
735
736 $request_data = (object) $request_data;
737
738 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
739 $request_data->label = sanitizeVal($request_data->label);
740
741 // Reset fk_parent_line for no child products and special product
742 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
743 $request_data->fk_parent_line = 0;
744 }
745
746 // calculate pa_ht
747 $marginInfos = getMarginInfos($request_data->subprice, $request_data->remise_percent, $request_data->tva_tx, $request_data->localtax1_tx, $request_data->localtax2_tx, $request_data->fk_fournprice, $request_data->pa_ht);
748 $pa_ht = $marginInfos[0];
749
750 $updateRes = $this->invoice->addline(
751 $request_data->desc,
752 $request_data->subprice,
753 $request_data->qty,
754 $request_data->tva_tx,
755 $request_data->localtax1_tx,
756 $request_data->localtax2_tx,
757 $request_data->fk_product,
758 $request_data->remise_percent,
759 $request_data->date_start,
760 $request_data->date_end,
761 $request_data->fk_code_ventilation,
762 $request_data->info_bits,
763 $request_data->fk_remise_except,
764 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
765 $request_data->subprice,
766 $request_data->product_type,
767 $request_data->rang,
768 $request_data->special_code,
769 $request_data->origin,
770 $request_data->origin_id,
771 $request_data->fk_parent_line,
772 empty($request_data->fk_fournprice) ? null : $request_data->fk_fournprice,
773 $pa_ht,
774 $request_data->label,
775 $request_data->array_options,
776 $request_data->situation_percent,
777 $request_data->fk_prev_id,
778 $request_data->fk_unit,
779 0,
780 $request_data->ref_ext
781 );
782
783 if ($updateRes < 0) {
784 throw new RestException(400, 'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
785 }
786
787 return $updateRes;
788 }
789
809 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
810 {
811 if (!DolibarrApiAccess::$user->rights->facture->creer) {
812 throw new RestException(401);
813 }
814 $result = $this->invoice->fetch($id);
815 if (!$result) {
816 throw new RestException(404, 'Invoice not found');
817 }
818
819 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
820 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
821 }
822
823 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
824 if ($result < 0) {
825 throw new RestException(500, 'Error : '.$this->invoice->error);
826 }
827
828 $result = $this->invoice->fetch($id);
829 if (!$result) {
830 throw new RestException(404, 'Invoice not found');
831 }
832
833 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
834 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
835 }
836
837 return $this->_cleanObjectDatas($this->invoice);
838 }
839
840
841
857 public function settodraft($id, $idwarehouse = -1)
858 {
859 if (!DolibarrApiAccess::$user->rights->facture->creer) {
860 throw new RestException(401);
861 }
862 $result = $this->invoice->fetch($id);
863 if (!$result) {
864 throw new RestException(404, 'Invoice not found');
865 }
866
867 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
868 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
869 }
870
871 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
872 if ($result == 0) {
873 throw new RestException(304, 'Nothing done.');
874 }
875 if ($result < 0) {
876 throw new RestException(500, 'Error : '.$this->invoice->error);
877 }
878
879 $result = $this->invoice->fetch($id);
880 if (!$result) {
881 throw new RestException(404, 'Invoice not found');
882 }
883
884 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
885 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
886 }
887
888 return $this->_cleanObjectDatas($this->invoice);
889 }
890
891
908 public function validate($id, $idwarehouse = 0, $notrigger = 0)
909 {
910 if (!DolibarrApiAccess::$user->rights->facture->creer) {
911 throw new RestException(401);
912 }
913 $result = $this->invoice->fetch($id);
914 if (!$result) {
915 throw new RestException(404, 'Invoice not found');
916 }
917
918 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
919 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
920 }
921
922 $result = $this->invoice->validate(DolibarrApiAccess::$user, '', $idwarehouse, $notrigger);
923 if ($result == 0) {
924 throw new RestException(304, 'Error nothing done. May be object is already validated');
925 }
926 if ($result < 0) {
927 throw new RestException(500, 'Error when validating Invoice: '.$this->invoice->error);
928 }
929
930 $result = $this->invoice->fetch($id);
931 if (!$result) {
932 throw new RestException(404, 'Invoice not found');
933 }
934
935 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
936 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
937 }
938
939 return $this->_cleanObjectDatas($this->invoice);
940 }
941
957 public function settopaid($id, $close_code = '', $close_note = '')
958 {
959 if (!DolibarrApiAccess::$user->rights->facture->creer) {
960 throw new RestException(401);
961 }
962 $result = $this->invoice->fetch($id);
963 if (!$result) {
964 throw new RestException(404, 'Invoice not found');
965 }
966
967 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
968 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
969 }
970
971 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
972 if ($result == 0) {
973 throw new RestException(304, 'Error nothing done. May be object is already validated');
974 }
975 if ($result < 0) {
976 throw new RestException(500, 'Error : '.$this->invoice->error);
977 }
978
979
980 $result = $this->invoice->fetch($id);
981 if (!$result) {
982 throw new RestException(404, 'Invoice not found');
983 }
984
985 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
986 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
987 }
988
989 return $this->_cleanObjectDatas($this->invoice);
990 }
991
992
1006 public function settounpaid($id)
1007 {
1008 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1009 throw new RestException(401);
1010 }
1011 $result = $this->invoice->fetch($id);
1012 if (!$result) {
1013 throw new RestException(404, 'Invoice not found');
1014 }
1015
1016 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1017 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1018 }
1019
1020 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1021 if ($result == 0) {
1022 throw new RestException(304, 'Nothing done');
1023 }
1024 if ($result < 0) {
1025 throw new RestException(500, 'Error : '.$this->invoice->error);
1026 }
1027
1028
1029 $result = $this->invoice->fetch($id);
1030 if (!$result) {
1031 throw new RestException(404, 'Invoice not found');
1032 }
1033
1034 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1035 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1036 }
1037
1038 return $this->_cleanObjectDatas($this->invoice);
1039 }
1040
1049 public function getDiscount($id)
1050 {
1051 require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1052
1053 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1054 throw new RestException(401);
1055 }
1056
1057 $result = $this->invoice->fetch($id);
1058 if (!$result) {
1059 throw new RestException(404, 'Invoice not found');
1060 }
1061
1062 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1063 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1064 }
1065
1066 $discountcheck = new DiscountAbsolute($this->db);
1067 $result = $discountcheck->fetch(0, $this->invoice->id);
1068
1069 if ($result == 0) {
1070 throw new RestException(404, 'Discount not found');
1071 }
1072 if ($result < 0) {
1073 throw new RestException(500, $discountcheck->error);
1074 }
1075
1076 return parent::_cleanObjectDatas($discountcheck);
1077 }
1078
1092 public function markAsCreditAvailable($id)
1093 {
1094 require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1095
1096 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1097 throw new RestException(401);
1098 }
1099
1100 $result = $this->invoice->fetch($id);
1101 if (!$result) {
1102 throw new RestException(404, 'Invoice not found');
1103 }
1104
1105 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1106 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1107 }
1108
1109 if ($this->invoice->paye) {
1110 throw new RestException(500, 'Alreay paid');
1111 }
1112
1113 $this->invoice->fetch($id);
1114 $this->invoice->fetch_thirdparty();
1115
1116 // Check if there is already a discount (protection to avoid duplicate creation when resubmit post)
1117 $discountcheck = new DiscountAbsolute($this->db);
1118 $result = $discountcheck->fetch(0, $this->invoice->id);
1119
1120 $canconvert = 0;
1121 if ($this->invoice->type == Facture::TYPE_DEPOSIT && empty($discountcheck->id)) {
1122 $canconvert = 1; // we can convert deposit into discount if deposit is payed (completely, partially or not at all) and not already converted (see real condition into condition used to show button converttoreduc)
1123 }
1124 if (($this->invoice->type == Facture::TYPE_CREDIT_NOTE || $this->invoice->type == Facture::TYPE_STANDARD) && $this->invoice->paye == 0 && empty($discountcheck->id)) {
1125 $canconvert = 1; // we can convert credit note into discount if credit note is not payed back and not already converted and amount of payment is 0 (see real condition into condition used to show button converttoreduc)
1126 }
1127 if ($canconvert) {
1128 $this->db->begin();
1129
1130 $amount_ht = $amount_tva = $amount_ttc = array();
1131 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1132
1133 // Loop on each vat rate
1134 $i = 0;
1135 foreach ($this->invoice->lines as $line) {
1136 if ($line->product_type < 9 && $line->total_ht != 0) { // Remove lines with product_type greater than or equal to 9
1137 // no need to create discount if amount is null
1138 $amount_ht[$line->tva_tx] += $line->total_ht;
1139 $amount_tva[$line->tva_tx] += $line->total_tva;
1140 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1141 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1142 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1143 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1144 $i++;
1145 }
1146 }
1147
1148 // Insert one discount by VAT rate category
1149 $discount = new DiscountAbsolute($this->db);
1150 if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1151 $discount->description = '(CREDIT_NOTE)';
1152 } elseif ($this->invoice->type == Facture::TYPE_DEPOSIT) {
1153 $discount->description = '(DEPOSIT)';
1154 } elseif ($this->invoice->type == Facture::TYPE_STANDARD || $this->invoice->type == Facture::TYPE_REPLACEMENT || $this->invoice->type == Facture::TYPE_SITUATION) {
1155 $discount->description = '(EXCESS RECEIVED)';
1156 } else {
1157 throw new RestException(500, 'Cant convert to reduc an Invoice of this type');
1158 }
1159
1160 $discount->fk_soc = $this->invoice->socid;
1161 $discount->fk_facture_source = $this->invoice->id;
1162
1163 $error = 0;
1164
1165 if ($this->invoice->type == Facture::TYPE_STANDARD || $this->invoice->type == Facture::TYPE_REPLACEMENT || $this->invoice->type == Facture::TYPE_SITUATION) {
1166 // If we're on a standard invoice, we have to get excess received to create a discount in TTC without VAT
1167
1168 // Total payments
1169 $sql = 'SELECT SUM(pf.amount) as total_payments';
1170 $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf, '.MAIN_DB_PREFIX.'paiement as p';
1171 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id';
1172 $sql .= ' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1173 $sql .= ' AND pf.fk_paiement = p.rowid';
1174 $sql .= ' AND p.entity IN ('.getEntity('invoice').')';
1175 $resql = $this->db->query($sql);
1176 if (!$resql) {
1177 dol_print_error($this->db);
1178 }
1179
1180 $res = $this->db->fetch_object($resql);
1181 $total_payments = $res->total_payments;
1182
1183 // Total credit note and deposit
1184 $total_creditnote_and_deposit = 0;
1185 $sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1186 $sql .= " re.description, re.fk_facture_source";
1187 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re";
1188 $sql .= " WHERE fk_facture = ".((int) $this->invoice->id);
1189 $resql = $this->db->query($sql);
1190 if (!empty($resql)) {
1191 while ($obj = $this->db->fetch_object($resql)) {
1192 $total_creditnote_and_deposit += $obj->amount_ttc;
1193 }
1194 } else {
1195 dol_print_error($this->db);
1196 }
1197
1198 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1199 $discount->amount_tva = 0;
1200 $discount->tva_tx = 0;
1201
1202 $result = $discount->create(DolibarrApiAccess::$user);
1203 if ($result < 0) {
1204 $error++;
1205 }
1206 }
1207 if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE || $this->invoice->type == Facture::TYPE_DEPOSIT) {
1208 foreach ($amount_ht as $tva_tx => $xxx) {
1209 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1210 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1211 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1212 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1213 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1214 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1215 $discount->tva_tx = abs($tva_tx);
1216
1217 $result = $discount->create(DolibarrApiAccess::$user);
1218 if ($result < 0) {
1219 $error++;
1220 break;
1221 }
1222 }
1223 }
1224
1225 if (empty($error)) {
1226 if ($this->invoice->type != Facture::TYPE_DEPOSIT) {
1227 // Classe facture
1228 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1229 if ($result >= 0) {
1230 $this->db->commit();
1231 } else {
1232 $this->db->rollback();
1233 throw new RestException(500, 'Could not set paid');
1234 }
1235 } else {
1236 $this->db->commit();
1237 }
1238 } else {
1239 $this->db->rollback();
1240 throw new RestException(500, 'Discount creation error');
1241 }
1242 }
1243
1244 return $this->_cleanObjectDatas($this->invoice);
1245 }
1246
1263 public function useDiscount($id, $discountid)
1264 {
1265 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1266 throw new RestException(401);
1267 }
1268 if (empty($id)) {
1269 throw new RestException(400, 'Invoice ID is mandatory');
1270 }
1271 if (empty($discountid)) {
1272 throw new RestException(400, 'Discount ID is mandatory');
1273 }
1274
1275 if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1276 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1277 }
1278
1279 $result = $this->invoice->fetch($id);
1280 if (!$result) {
1281 throw new RestException(404, 'Invoice not found');
1282 }
1283
1284 $result = $this->invoice->insert_discount($discountid);
1285 if ($result < 0) {
1286 throw new RestException(405, $this->invoice->error);
1287 }
1288
1289 return $result;
1290 }
1291
1308 public function useCreditNote($id, $discountid)
1309 {
1310 require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1311
1312 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1313 throw new RestException(401);
1314 }
1315 if (empty($id)) {
1316 throw new RestException(400, 'Invoice ID is mandatory');
1317 }
1318 if (empty($discountid)) {
1319 throw new RestException(400, 'Credit ID is mandatory');
1320 }
1321
1322 if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1323 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1324 }
1325 $discount = new DiscountAbsolute($this->db);
1326 $result = $discount->fetch($discountid);
1327 if (!$result) {
1328 throw new RestException(404, 'Credit not found');
1329 }
1330
1331 $result = $discount->link_to_invoice(0, $id);
1332 if ($result < 0) {
1333 throw new RestException(405, $discount->error);
1334 }
1335
1336 return $result;
1337 }
1338
1352 public function getPayments($id)
1353 {
1354 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1355 throw new RestException(401);
1356 }
1357 if (empty($id)) {
1358 throw new RestException(400, 'Invoice ID is mandatory');
1359 }
1360
1361 if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1362 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1363 }
1364
1365 $result = $this->invoice->fetch($id);
1366 if (!$result) {
1367 throw new RestException(404, 'Invoice not found');
1368 }
1369
1370 $result = $this->invoice->getListOfPayments();
1371 if ($result < 0) {
1372 throw new RestException(405, $this->invoice->error);
1373 }
1374
1375 return $result;
1376 }
1377
1378
1400 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment = '', $comment = '', $chqemetteur = '', $chqbank = '')
1401 {
1402 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
1403
1404 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1405 throw new RestException(403);
1406 }
1407 if (empty($id)) {
1408 throw new RestException(400, 'Invoice ID is mandatory');
1409 }
1410
1411 if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1412 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1413 }
1414
1415 if (isModEnabled("banque")) {
1416 if (empty($accountid)) {
1417 throw new RestException(400, 'Account ID is mandatory');
1418 }
1419 }
1420
1421 if (empty($paymentid)) {
1422 throw new RestException(400, 'Payment ID or Payment Code is mandatory');
1423 }
1424
1425
1426 $result = $this->invoice->fetch($id);
1427 if (!$result) {
1428 throw new RestException(404, 'Invoice not found');
1429 }
1430
1431 // Calculate amount to pay
1432 $totalpaid = $this->invoice->getSommePaiement();
1433 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1434 $totaldeposits = $this->invoice->getSumDepositsUsed();
1435 $resteapayer = price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
1436
1437 $this->db->begin();
1438
1439 $amounts = array();
1440 $multicurrency_amounts = array();
1441
1442 // Clean parameters amount if payment is for a credit note
1443 if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1444 $resteapayer = price2num($resteapayer, 'MT');
1445 $amounts[$id] = price2num(-1 * $resteapayer, 'MT');
1446 // Multicurrency
1447 $newvalue = price2num($this->invoice->multicurrency_total_ttc, 'MT');
1448 $multicurrency_amounts[$id] = price2num(-1 * $newvalue, 'MT');
1449 } else {
1450 $resteapayer = price2num($resteapayer, 'MT');
1451 $amounts[$id] = $resteapayer;
1452 // Multicurrency
1453 $newvalue = price2num($this->invoice->multicurrency_total_ttc, 'MT');
1454 $multicurrency_amounts[$id] = $newvalue;
1455 }
1456
1457 // Creation of payment line
1458 $paymentobj = new Paiement($this->db);
1459 $paymentobj->datepaye = $datepaye;
1460 $paymentobj->amounts = $amounts; // Array with all payments dispatching with invoice id
1461 $paymentobj->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching
1462 $paymentobj->paiementid = $paymentid;
1463 $paymentobj->paiementcode = dol_getIdFromCode($this->db, $paymentid, 'c_paiement', 'id', 'code', 1);
1464 $paymentobj->num_payment = $num_payment;
1465 $paymentobj->note_private = $comment;
1466
1467 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices == 'yes' ? 1 : 0)); // This include closing invoices
1468 if ($payment_id < 0) {
1469 $this->db->rollback();
1470 throw new RestException(400, 'Payment error : '.$paymentobj->error);
1471 }
1472
1473 if (isModEnabled("banque")) {
1474 $label = '(CustomerInvoicePayment)';
1475
1476 if ($paymentobj->paiementcode == 'CHQ' && empty($chqemetteur)) {
1477 throw new RestException(400, 'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1478 }
1479 if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1480 $label = '(CustomerInvoicePaymentBack)'; // Refund of a credit note
1481 }
1482 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user, 'payment', $label, $accountid, $chqemetteur, $chqbank);
1483 if ($result < 0) {
1484 $this->db->rollback();
1485 throw new RestException(400, 'Add payment to bank error : '.$paymentobj->error);
1486 }
1487 }
1488
1489 $this->db->commit();
1490
1491 return $payment_id;
1492 }
1493
1520 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment = '', $comment = '', $chqemetteur = '', $chqbank = '', $ref_ext = '', $accepthigherpayment = false)
1521 {
1522 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
1523
1524 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1525 throw new RestException(403);
1526 }
1527 foreach ($arrayofamounts as $id => $amount) {
1528 if (empty($id)) {
1529 throw new RestException(400, 'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1530 }
1531 if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1532 throw new RestException(403, 'Access not allowed on invoice ID '.$id.' for login '.DolibarrApiAccess::$user->login);
1533 }
1534 }
1535
1536 if (isModEnabled("banque")) {
1537 if (empty($accountid)) {
1538 throw new RestException(400, 'Account ID is mandatory');
1539 }
1540 }
1541 if (empty($paymentid)) {
1542 throw new RestException(400, 'Payment ID or Payment Code is mandatory');
1543 }
1544
1545 $this->db->begin();
1546
1547 $amounts = array();
1548 $multicurrency_amounts = array();
1549
1550 // Loop on each invoice to pay
1551 foreach ($arrayofamounts as $id => $amountarray) {
1552 $result = $this->invoice->fetch($id);
1553 if (!$result) {
1554 $this->db->rollback();
1555 throw new RestException(404, 'Invoice ID '.$id.' not found');
1556 }
1557
1558 if (($amountarray["amount"] == "remain" || $amountarray["amount"] > 0) && ($amountarray["multicurrency_amount"] == "remain" || $amountarray["multicurrency_amount"] > 0)) {
1559 $this->db->rollback();
1560 throw new RestException(400, 'Payment in both currency '.$id.' ( amount: '.$amountarray["amount"].', multicurrency_amount: '.$amountarray["multicurrency_amount"].')');
1561 }
1562
1563 $is_multicurrency = 0;
1564 $total_ttc = $this->invoice->total_ttc;
1565
1566 if ($amountarray["multicurrency_amount"] > 0 || $amountarray["multicurrency_amount"] == "remain") {
1567 $is_multicurrency = 1;
1568 $total_ttc = $this->invoice->multicurrency_total_ttc;
1569 }
1570
1571 // Calculate amount to pay
1572 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1573 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1574 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1575 $remainstopay = $amount = price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
1576
1577 if (!$is_multicurrency && $amountarray["amount"] != 'remain') {
1578 $amount = price2num($amountarray["amount"], 'MT');
1579 }
1580
1581 if ($is_multicurrency && $amountarray["multicurrency_amount"] != 'remain') {
1582 $amount = price2num($amountarray["multicurrency_amount"], 'MT');
1583 }
1584
1585 if (abs($amount) > abs($remainstopay) && !$accepthigherpayment) {
1586 $this->db->rollback();
1587 throw new RestException(400, 'Payment amount on invoice ID '.$id.' ('.$amount.') is higher than remain to pay ('.$remainstopay.')');
1588 }
1589
1590 if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1591 $amount = price2num(-1 * abs($amount), 'MT');
1592 }
1593
1594 if ($is_multicurrency) {
1595 $amounts[$id] = null;
1596 // Multicurrency
1597 $multicurrency_amounts[$id] = $amount;
1598 } else {
1599 $amounts[$id] = $amount;
1600 // Multicurrency
1601 $multicurrency_amounts[$id] = null;
1602 }
1603 }
1604
1605 // Creation of payment line
1606 $paymentobj = new Paiement($this->db);
1607 $paymentobj->datepaye = $datepaye;
1608 $paymentobj->amounts = $amounts; // Array with all payments dispatching with invoice id
1609 $paymentobj->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching
1610 $paymentobj->paiementid = $paymentid;
1611 $paymentobj->paiementcode = dol_getIdFromCode($this->db, $paymentid, 'c_paiement', 'id', 'code', 1);
1612 $paymentobj->num_payment = $num_payment;
1613 $paymentobj->note_private = $comment;
1614 $paymentobj->ref_ext = $ref_ext;
1615 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices == 'yes' ? 1 : 0)); // This include closing invoices
1616 if ($payment_id < 0) {
1617 $this->db->rollback();
1618 throw new RestException(400, 'Payment error : '.$paymentobj->error);
1619 }
1620 if (isModEnabled("banque")) {
1621 $label = '(CustomerInvoicePayment)';
1622 if ($paymentobj->paiementcode == 'CHQ' && empty($chqemetteur)) {
1623 throw new RestException(400, 'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1624 }
1625 if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1626 $label = '(CustomerInvoicePaymentBack)'; // Refund of a credit note
1627 }
1628 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user, 'payment', $label, $accountid, $chqemetteur, $chqbank);
1629 if ($result < 0) {
1630 $this->db->rollback();
1631 throw new RestException(400, 'Add payment to bank error : '.$paymentobj->error);
1632 }
1633 }
1634
1635 $this->db->commit();
1636
1637 return $payment_id;
1638 }
1639
1654 public function putPayment($id, $num_payment = '')
1655 {
1656 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
1657
1658 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1659 throw new RestException(401);
1660 }
1661 if (empty($id)) {
1662 throw new RestException(400, 'Payment ID is mandatory');
1663 }
1664
1665 $paymentobj = new Paiement($this->db);
1666 $result = $paymentobj->fetch($id);
1667
1668 if (!$result) {
1669 throw new RestException(404, 'Payment not found');
1670 }
1671
1672 if (!empty($num_payment)) {
1673 $result = $paymentobj->update_num($num_payment);
1674 if ($result < 0) {
1675 throw new RestException(500, 'Error when updating the payment num');
1676 }
1677 }
1678
1679 return [
1680 'success' => [
1681 'code' => 200,
1682 'message' => 'Payment updated'
1683 ]
1684 ];
1685 }
1686
1687 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1694 protected function _cleanObjectDatas($object)
1695 {
1696 // phpcs:enable
1697 $object = parent::_cleanObjectDatas($object);
1698
1699 unset($object->note);
1700 unset($object->address);
1701 unset($object->barcode_type);
1702 unset($object->barcode_type_code);
1703 unset($object->barcode_type_label);
1704 unset($object->barcode_type_coder);
1705 unset($object->canvas);
1706
1707 return $object;
1708 }
1709
1718 private function _validate($data)
1719 {
1720 $invoice = array();
1721 foreach (Invoices::$FIELDS as $field) {
1722 if (!isset($data[$field])) {
1723 throw new RestException(400, "$field field missing");
1724 }
1725 $invoice[$field] = $data[$field];
1726 }
1727 return $invoice;
1728 }
1729
1730
1744 public function getTemplateInvoice($id, $contact_list = 1)
1745 {
1746 return $this->_fetchTemplateInvoice($id, '', '', $contact_list);
1747 }
1748
1762 private function _fetchTemplateInvoice($id, $ref = '', $ref_ext = '', $contact_list = 1)
1763 {
1764 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1765 throw new RestException(401);
1766 }
1767
1768 $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1769 if (!$result) {
1770 throw new RestException(404, 'Template invoice not found');
1771 }
1772
1773 if (!DolibarrApi::_checkAccessToResource('facturerec', $this->template_invoice->id)) {
1774 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1775 }
1776
1777 // Add external contacts ids
1778 if ($contact_list > -1) {
1779 $tmparray = $this->template_invoice->liste_contact(-1, 'external', $contact_list);
1780 if (is_array($tmparray)) {
1781 $this->template_invoice->contacts_ids = $tmparray;
1782 }
1783 }
1784
1785 $this->template_invoice->fetchObjectLinked();
1786 return $this->_cleanTemplateObjectDatas($this->template_invoice);
1787 }
1788
1789
1790 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1797 protected function _cleanTemplateObjectDatas($object)
1798 {
1799 // phpcs:enable
1800 $object = parent::_cleanObjectDatas($object);
1801
1802 unset($object->note);
1803 unset($object->address);
1804 unset($object->barcode_type);
1805 unset($object->barcode_type_code);
1806 unset($object->barcode_type_label);
1807 unset($object->barcode_type_coder);
1808 unset($object->canvas);
1809
1810 return $object;
1811 }
1812}
Class to manage customers orders.
Class to manage absolute discounts.
Class for API REST v1.
Definition api.class.php:32
_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.
Definition api.class.php:86
Class to manage invoices.
const TYPE_REPLACEMENT
Replacement invoice.
const TYPE_STANDARD
Standard invoice.
const TYPE_SITUATION
Situation invoice.
const TYPE_DEPOSIT
Deposit invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
Class to manage invoice templates.
postContact($id, $contactid, $type)
Add a contact type of given invoice.
putPayment($id, $num_payment='')
Update a payment.
addContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0)
Adds a contact to an invoice.
createInvoiceFromOrder($orderid)
Create an invoice using an existing order.
markAsCreditAvailable($id)
Create a discount (credit available) for a credit note or a deposit.
getDiscount($id)
Get discount from invoice.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='')
List invoices.
__construct()
Constructor.
put($id, $request_data=null)
Update invoice.
getByRefExt($ref_ext, $contact_list=1)
Get properties of an invoice object by ref_ext.
_fetch($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an invoice object.
getByRef($ref, $contact_list=1)
Get properties of an invoice object by ref.
getPayments($id)
Get list of payments of a given invoice.
_cleanObjectDatas($object)
Clean sensible object datas.
post($request_data=null)
Create invoice object.
useDiscount($id, $discountid)
Add a discount line into an invoice (as an invoice line) using an existing absolute discount.
settounpaid($id)
Sets an invoice as unpaid.
getTemplateInvoice($id, $contact_list=1)
Get properties of a template invoice object.
putLine($id, $lineid, $request_data=null)
Update a line to a given invoice.
getLines($id)
Get lines of an invoice.
useCreditNote($id, $discountid)
Add an available credit note discount to payments of an existing invoice.
addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment='', $comment='', $chqemetteur='', $chqbank='', $ref_ext='', $accepthigherpayment=false)
Add a payment to pay partially or completely one or several invoices.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an invoice.
_cleanTemplateObjectDatas($object)
Clean sensible object datas.
deleteContact($id, $contactid, $type)
Delete a contact type of given invoice.
postLine($id, $request_data=null)
Add a line to a given invoice.
_fetchTemplateInvoice($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an invoice object.
_validate($data)
Validate fields before create or update object.
addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment='', $comment='', $chqemetteur='', $chqbank='')
Add payment line to a specific invoice with the remain to pay as amount.
settopaid($id, $close_code='', $close_note='')
Sets an invoice as paid.
settodraft($id, $idwarehouse=-1)
Sets an invoice as draft.
deleteLine($id, $lineid)
Deletes a line of a given invoice.
Class to manage payments of customer invoices.
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 '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.