dolibarr 21.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 * Copyright (C) 2023 Joachim Kueter <git-jk@bloxera.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
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.'/compta/facture/class/facture.class.php';
25require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture-rec.class.php';
26
27
34class Invoices extends DolibarrApi
35{
40 public static $FIELDS = array(
41 'socid',
42 );
43
47 private $invoice;
48
52 private $template_invoice;
53
54
58 public function __construct()
59 {
60 global $db;
61 $this->db = $db;
62 $this->invoice = new Facture($this->db);
63 $this->template_invoice = new FactureRec($this->db);
64 }
65
77 public function get($id, $contact_list = 1)
78 {
79 return $this->_fetch($id, '', '', $contact_list);
80 }
81
95 public function getByRef($ref, $contact_list = 1)
96 {
97 return $this->_fetch(0, $ref, '', $contact_list);
98 }
99
113 public function getByRefExt($ref_ext, $contact_list = 1)
114 {
115 return $this->_fetch(0, '', $ref_ext, $contact_list);
116 }
117
131 private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
132 {
133 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
134 throw new RestException(403);
135 }
136
137 $result = $this->invoice->fetch($id, $ref, $ref_ext);
138 if (!$result) {
139 throw new RestException(404, 'Invoice not found');
140 }
141
142 // Get payment details
143 $this->invoice->totalpaid = $this->invoice->getSommePaiement();
144 $this->invoice->totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
145 $this->invoice->totaldeposits = $this->invoice->getSumDepositsUsed();
146 $this->invoice->remaintopay = price2num($this->invoice->total_ttc - $this->invoice->totalpaid - $this->invoice->totalcreditnotes - $this->invoice->totaldeposits, 'MT');
147
148 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
149 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
150 }
151
152 // Add external contacts ids
153 if ($contact_list > -1) {
154 $tmparray = $this->invoice->liste_contact(-1, 'external', $contact_list);
155 if (is_array($tmparray)) {
156 $this->invoice->contacts_ids = $tmparray;
157 }
158 }
159
160 $this->invoice->fetchObjectLinked();
161
162 // Add online_payment_url, copied from order
163 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
164 $this->invoice->online_payment_url = getOnlinePaymentUrl(0, 'invoice', $this->invoice->ref);
165
166 return $this->_cleanObjectDatas($this->invoice);
167 }
168
189 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $status = '', $sqlfilters = '', $properties = '', $pagination_data = false, $loadlinkedobjects = 0)
190 {
191 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
192 throw new RestException(403);
193 }
194
195 $obj_ret = array();
196
197 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
198 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
199
200 // If the internal user must only see his customers, force searching by him
201 $search_sale = 0;
202 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
203 $search_sale = DolibarrApiAccess::$user->id;
204 }
205
206 $sql = "SELECT t.rowid";
207 $sql .= " FROM ".MAIN_DB_PREFIX."facture AS t";
208 $sql .= " 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
209 $sql .= ' WHERE t.entity IN ('.getEntity('invoice').')';
210 if ($socids) {
211 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
212 }
213 // Search on sale representative
214 if ($search_sale && $search_sale != '-1') {
215 if ($search_sale == -2) {
216 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
217 } elseif ($search_sale > 0) {
218 $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).")";
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 // Add sql filters
235 if ($sqlfilters) {
236 $errormessage = '';
237 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
238 if ($errormessage) {
239 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
240 }
241 }
242
243 //this query will return total invoices with the filters given
244 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
245
246 $sql .= $this->db->order($sortfield, $sortorder);
247 if ($limit) {
248 if ($page < 0) {
249 $page = 0;
250 }
251 $offset = $limit * $page;
252
253 $sql .= $this->db->plimit($limit + 1, $offset);
254 }
255
256 $result = $this->db->query($sql);
257 if ($result) {
258 $i = 0;
259 $num = $this->db->num_rows($result);
260 $min = min($num, ($limit <= 0 ? $num : $limit));
261 while ($i < $min) {
262 $obj = $this->db->fetch_object($result);
263 $invoice_static = new Facture($this->db);
264 if ($invoice_static->fetch($obj->rowid) > 0) {
265 // Get payment details
266 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
267 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
268 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
269 $invoice_static->remaintopay = price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits, 'MT');
270
271 // Add external contacts ids
272 $tmparray = $invoice_static->liste_contact(-1, 'external', 1);
273 if (is_array($tmparray)) {
274 $invoice_static->contacts_ids = $tmparray;
275 }
276
277 if ($loadlinkedobjects) {
278 // retrieve linked objects
279 $invoice_static->fetchObjectLinked();
280 }
281
282 // Add online_payment_url, copied from order
283 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
284 $invoice_static->online_payment_url = getOnlinePaymentUrl(0, 'invoice', $invoice_static->ref);
285
286 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($invoice_static), $properties);
287 }
288 $i++;
289 }
290 } else {
291 throw new RestException(503, 'Error when retrieve invoice list : '.$this->db->lasterror());
292 }
293
294 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
295 if ($pagination_data) {
296 $totalsResult = $this->db->query($sqlTotals);
297 $total = $this->db->fetch_object($totalsResult)->total;
298
299 $tmp = $obj_ret;
300 $obj_ret = [];
301
302 $obj_ret['data'] = $tmp;
303 $obj_ret['pagination'] = [
304 'total' => (int) $total,
305 'page' => $page, //count starts from 0
306 'page_count' => ceil((int) $total / $limit),
307 'limit' => $limit
308 ];
309 }
310
311 return $obj_ret;
312 }
313
320 public function post($request_data = null)
321 {
322 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
323 throw new RestException(403, "Insuffisant rights");
324 }
325 // Check mandatory fields
326 $result = $this->_validate($request_data);
327
328 foreach ($request_data as $field => $value) {
329 if ($field === 'caller') {
330 // 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
331 $this->invoice->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
332 continue;
333 }
334
335 $this->invoice->$field = $this->_checkValForAPI($field, $value, $this->invoice);
336 }
337 if (!array_key_exists('date', $request_data)) {
338 $this->invoice->date = dol_now();
339 }
340 /* We keep lines as an array
341 if (isset($request_data["lines"])) {
342 $lines = array();
343 foreach ($request_data["lines"] as $line) {
344 array_push($lines, (object) $line);
345 }
346 $this->invoice->lines = $lines;
347 }*/
348
349 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data["date_lim_reglement"]) ? 0 : $request_data["date_lim_reglement"])) < 0) {
350 throw new RestException(500, "Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
351 }
352 return ((int) $this->invoice->id);
353 }
354
369 public function createInvoiceFromOrder($orderid)
370 {
371 require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
372
373 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
374 throw new RestException(403);
375 }
376 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
377 throw new RestException(403);
378 }
379 if (empty($orderid)) {
380 throw new RestException(400, 'Order ID is mandatory');
381 }
382 if (!DolibarrApi::_checkAccessToResource('commande', $orderid)) {
383 throw new RestException(403, 'Access not allowed on order for login '.DolibarrApiAccess::$user->login);
384 }
385
386 $order = new Commande($this->db);
387 $result = $order->fetch($orderid);
388 if (!$result) {
389 throw new RestException(404, 'Order not found');
390 }
391
392 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
393 if ($result < 0) {
394 throw new RestException(405, $this->invoice->error);
395 }
396 $this->invoice->fetchObjectLinked();
397 return $this->_cleanObjectDatas($this->invoice);
398 }
399
413 public function createInvoiceFromContract($contractid)
414 {
415 require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
416
417 if (!DolibarrApiAccess::$user->hasRight('contrat', 'lire')) {
418 throw new RestException(403);
419 }
420 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
421 throw new RestException(403);
422 }
423 if (empty($contractid)) {
424 throw new RestException(400, 'Contract ID is mandatory');
425 }
426
427 $contract = new Contrat($this->db);
428 $result = $contract->fetch($contractid);
429 if (!$result) {
430 throw new RestException(404, 'Contract not found');
431 }
432
433 $result = $this->invoice->createFromContract($contract, DolibarrApiAccess::$user);
434 if ($result < 0) {
435 throw new RestException(405, $this->invoice->error);
436 }
437 $this->invoice->fetchObjectLinked();
438 return $this->_cleanObjectDatas($this->invoice);
439 }
440
449 public function getLines($id)
450 {
451 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
452 throw new RestException(403);
453 }
454
455 $result = $this->invoice->fetch($id);
456 if (!$result) {
457 throw new RestException(404, 'Invoice not found');
458 }
459
460 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
461 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
462 }
463 $this->invoice->getLinesArray();
464 $result = array();
465 foreach ($this->invoice->lines as $line) {
466 array_push($result, $this->_cleanObjectDatas($line));
467 }
468 return $result;
469 }
470
485 public function putLine($id, $lineid, $request_data = null)
486 {
487 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
488 throw new RestException(403);
489 }
490
491 $result = $this->invoice->fetch($id);
492 if (!$result) {
493 throw new RestException(404, 'Invoice not found');
494 }
495
496 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
497 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
498 }
499
500 $request_data = (object) $request_data;
501
502 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
503 $request_data->label = sanitizeVal($request_data->label);
504
505 $updateRes = $this->invoice->updateline(
506 $lineid,
507 $request_data->desc,
508 $request_data->subprice,
509 $request_data->qty,
510 $request_data->remise_percent,
511 $request_data->date_start,
512 $request_data->date_end,
513 $request_data->tva_tx,
514 $request_data->localtax1_tx,
515 $request_data->localtax2_tx,
516 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
517 $request_data->info_bits,
518 $request_data->product_type,
519 $request_data->fk_parent_line,
520 0,
521 $request_data->fk_fournprice,
522 $request_data->pa_ht,
523 $request_data->label,
524 $request_data->special_code,
525 $request_data->array_options,
526 $request_data->situation_percent,
527 $request_data->fk_unit,
528 $request_data->multicurrency_subprice,
529 0,
530 $request_data->ref_ext,
531 $request_data->rang
532 );
533
534 if ($updateRes > 0) {
535 $result = $this->get($id);
536 unset($result->line);
537 return $this->_cleanObjectDatas($result);
538 } else {
539 throw new RestException(304, $this->invoice->error);
540 }
541 }
542
556 public function postContact($id, $contactid, $type)
557 {
558 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
559 throw new RestException(403);
560 }
561
562 $result = $this->invoice->fetch($id);
563
564 if (!$result) {
565 throw new RestException(404, 'Invoice not found');
566 }
567
568 if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
569 throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
570 }
571
572 if (!DolibarrApi::_checkAccessToResource('invoice', $this->invoice->id)) {
573 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
574 }
575
576 $result = $this->invoice->add_contact($contactid, $type, 'external');
577
578 if (!$result) {
579 throw new RestException(500, 'Error when added the contact');
580 }
581
582 return array(
583 'success' => array(
584 'code' => 200,
585 'message' => 'Contact linked to the invoice'
586 )
587 );
588 }
589
604 public function deleteContact($id, $contactid, $type)
605 {
606 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
607 throw new RestException(403);
608 }
609
610 $result = $this->invoice->fetch($id);
611
612 if (!$result) {
613 throw new RestException(404, 'Invoice not found');
614 }
615
616 if (!DolibarrApi::_checkAccessToResource('invoice', $this->invoice->id)) {
617 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
618 }
619
620 $contacts = $this->invoice->liste_contact();
621
622 foreach ($contacts as $contact) {
623 if ($contact['id'] == $contactid && $contact['code'] == $type) {
624 $result = $this->invoice->delete_contact($contact['rowid']);
625
626 if (!$result) {
627 throw new RestException(500, 'Error when deleted the contact');
628 }
629 }
630 }
631
632 return $this->_cleanObjectDatas($this->invoice);
633 }
634
649 public function deleteLine($id, $lineid)
650 {
651 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
652 throw new RestException(403);
653 }
654 if (empty($lineid)) {
655 throw new RestException(400, 'Line ID is mandatory');
656 }
657
658 if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
659 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
660 }
661
662 $result = $this->invoice->fetch($id);
663 if (!$result) {
664 throw new RestException(404, 'Invoice not found');
665 }
666
667 $updateRes = $this->invoice->deleteLine($lineid, $id);
668 if ($updateRes > 0) {
669 return $this->get($id);
670 } else {
671 throw new RestException(405, $this->invoice->error);
672 }
673 }
674
682 public function put($id, $request_data = null)
683 {
684 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
685 throw new RestException(403);
686 }
687
688 $result = $this->invoice->fetch($id);
689 if (!$result) {
690 throw new RestException(404, 'Invoice not found');
691 }
692
693 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
694 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
695 }
696
697 foreach ($request_data as $field => $value) {
698 if ($field == 'id') {
699 continue;
700 }
701 if ($field === 'caller') {
702 // 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
703 $this->invoice->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
704 continue;
705 }
706 if ($field == 'array_options' && is_array($value)) {
707 foreach ($value as $index => $val) {
708 $this->invoice->array_options[$index] = $this->_checkValForAPI($field, $val, $this->invoice);
709 }
710 continue;
711 }
712
713 $this->invoice->$field = $this->_checkValForAPI($field, $value, $this->invoice);
714
715 // If cond reglement => update date lim reglement
716 if ($field == 'cond_reglement_id') {
717 $this->invoice->date_lim_reglement = $this->invoice->calculate_date_lim_reglement();
718 }
719 }
720
721 // update bank account
722 if (!empty($this->invoice->fk_account)) {
723 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
724 throw new RestException(400, $this->invoice->error);
725 }
726 }
727
728 if ($this->invoice->update(DolibarrApiAccess::$user) > 0) {
729 return $this->get($id);
730 } else {
731 throw new RestException(500, $this->invoice->error);
732 }
733 }
734
741 public function delete($id)
742 {
743 if (!DolibarrApiAccess::$user->hasRight('facture', 'supprimer')) {
744 throw new RestException(403);
745 }
746 $result = $this->invoice->fetch($id);
747 if (!$result) {
748 throw new RestException(404, 'Invoice not found');
749 }
750
751 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
752 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
753 }
754
755 $result = $this->invoice->delete(DolibarrApiAccess::$user);
756 if ($result < 0) {
757 throw new RestException(500, 'Error when deleting invoice');
758 } elseif ($result == 0) {
759 throw new RestException(403, 'Invoice not erasable');
760 }
761
762 return array(
763 'success' => array(
764 'code' => 200,
765 'message' => 'Invoice deleted'
766 )
767 );
768 }
769
793 public function postLine($id, $request_data = null)
794 {
795 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
796 throw new RestException(403);
797 }
798
799 $result = $this->invoice->fetch($id);
800 if (!$result) {
801 throw new RestException(404, 'Invoice not found');
802 }
803
804 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
805 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
806 }
807
808 $request_data = (object) $request_data;
809
810 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
811 $request_data->label = sanitizeVal($request_data->label);
812
813 // Reset fk_parent_line for no child products and special product
814 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
815 $request_data->fk_parent_line = 0;
816 }
817
818 // calculate pa_ht
819 $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);
820 $pa_ht = $marginInfos[0];
821
822 $updateRes = $this->invoice->addline(
823 $request_data->desc,
824 $request_data->subprice,
825 $request_data->qty,
826 $request_data->tva_tx,
827 $request_data->localtax1_tx,
828 $request_data->localtax2_tx,
829 $request_data->fk_product,
830 $request_data->remise_percent,
831 $request_data->date_start,
832 $request_data->date_end,
833 $request_data->fk_code_ventilation,
834 $request_data->info_bits,
835 $request_data->fk_remise_except,
836 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
837 $request_data->subprice,
838 $request_data->product_type,
839 $request_data->rang,
840 $request_data->special_code,
841 $request_data->origin,
842 $request_data->origin_id,
843 $request_data->fk_parent_line,
844 empty($request_data->fk_fournprice) ? null : $request_data->fk_fournprice,
845 $pa_ht,
846 $request_data->label,
847 $request_data->array_options,
848 $request_data->situation_percent,
849 $request_data->fk_prev_id,
850 $request_data->fk_unit,
851 0,
852 $request_data->ref_ext
853 );
854
855 if ($updateRes < 0) {
856 throw new RestException(400, 'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
857 }
858
859 return $updateRes;
860 }
861
880 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
881 {
882 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
883 throw new RestException(403);
884 }
885 $result = $this->invoice->fetch($id);
886 if (!$result) {
887 throw new RestException(404, 'Invoice not found');
888 }
889
890 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
891 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
892 }
893
894 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
895 if ($result < 0) {
896 throw new RestException(500, 'Error : '.$this->invoice->error);
897 }
898
899 $result = $this->invoice->fetch($id);
900 if (!$result) {
901 throw new RestException(404, 'Invoice not found');
902 }
903
904 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
905 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
906 }
907
908 return $this->_cleanObjectDatas($this->invoice);
909 }
910
911
912
927 public function settodraft($id, $idwarehouse = -1)
928 {
929 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
930 throw new RestException(403);
931 }
932 $result = $this->invoice->fetch($id);
933 if (!$result) {
934 throw new RestException(404, 'Invoice not found');
935 }
936
937 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
938 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
939 }
940
941 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
942 if ($result == 0) {
943 throw new RestException(304, 'Nothing done.');
944 }
945 if ($result < 0) {
946 throw new RestException(500, 'Error : '.$this->invoice->error);
947 }
948
949 $result = $this->invoice->fetch($id);
950 if (!$result) {
951 throw new RestException(404, 'Invoice not found');
952 }
953
954 return $this->_cleanObjectDatas($this->invoice);
955 }
956
957
975 public function validate($id, $force_number = '', $idwarehouse = 0, $notrigger = 0)
976 {
977 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
978 throw new RestException(403);
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(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
987 }
988
989 $result = $this->invoice->validate(DolibarrApiAccess::$user, $force_number, $idwarehouse, $notrigger);
990 if ($result == 0) {
991 throw new RestException(304, 'Error nothing done. May be object is already validated');
992 }
993 if ($result < 0) {
994 throw new RestException(500, 'Error when validating Invoice: '.$this->invoice->error);
995 }
996
997 $result = $this->invoice->fetch($id);
998 if (!$result) {
999 throw new RestException(404, 'Invoice not found');
1000 }
1001
1002 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1003 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1004 }
1005
1006 // copy from order
1007 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
1008 $this->invoice->online_payment_url = getOnlinePaymentUrl(0, 'invoice', $this->invoice->ref);
1009
1010 return $this->_cleanObjectDatas($this->invoice);
1011 }
1012
1028 public function settopaid($id, $close_code = '', $close_note = '')
1029 {
1030 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
1031 throw new RestException(403);
1032 }
1033 $result = $this->invoice->fetch($id);
1034 if (!$result) {
1035 throw new RestException(404, 'Invoice not found');
1036 }
1037
1038 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1039 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1040 }
1041
1042 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
1043 if ($result == 0) {
1044 throw new RestException(304, 'Error nothing done. May be object is already validated');
1045 }
1046 if ($result < 0) {
1047 throw new RestException(500, 'Error : '.$this->invoice->error);
1048 }
1049
1050
1051 $result = $this->invoice->fetch($id);
1052 if (!$result) {
1053 throw new RestException(404, 'Invoice not found');
1054 }
1055
1056 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1057 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1058 }
1059
1060 return $this->_cleanObjectDatas($this->invoice);
1061 }
1062
1063
1077 public function settounpaid($id)
1078 {
1079 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
1080 throw new RestException(403);
1081 }
1082 $result = $this->invoice->fetch($id);
1083 if (!$result) {
1084 throw new RestException(404, 'Invoice not found');
1085 }
1086
1087 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1088 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1089 }
1090
1091 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1092 if ($result == 0) {
1093 throw new RestException(304, 'Nothing done');
1094 }
1095 if ($result < 0) {
1096 throw new RestException(500, 'Error : '.$this->invoice->error);
1097 }
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(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1107 }
1108
1109 return $this->_cleanObjectDatas($this->invoice);
1110 }
1111
1120 public function getDiscount($id)
1121 {
1122 require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1123
1124 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1125 throw new RestException(403);
1126 }
1127
1128 $result = $this->invoice->fetch($id);
1129 if (!$result) {
1130 throw new RestException(404, 'Invoice not found');
1131 }
1132
1133 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1134 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1135 }
1136
1137 $discountcheck = new DiscountAbsolute($this->db);
1138 $result = $discountcheck->fetch(0, $this->invoice->id);
1139
1140 if ($result == 0) {
1141 throw new RestException(404, 'Discount not found');
1142 }
1143 if ($result < 0) {
1144 throw new RestException(500, $discountcheck->error);
1145 }
1146
1147 return parent::_cleanObjectDatas($discountcheck);
1148 }
1149
1164 {
1165 require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1166
1167 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
1168 throw new RestException(403);
1169 }
1170
1171 $result = $this->invoice->fetch($id);
1172 if (!$result) {
1173 throw new RestException(404, 'Invoice not found');
1174 }
1175
1176 if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1177 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1178 }
1179
1180 if ($this->invoice->paye) {
1181 throw new RestException(500, 'Alreay paid');
1182 }
1183
1184 $this->invoice->fetch($id);
1185 $this->invoice->fetch_thirdparty();
1186
1187 // Check if there is already a discount (protection to avoid duplicate creation when resubmit post)
1188 $discountcheck = new DiscountAbsolute($this->db);
1189 $result = $discountcheck->fetch(0, $this->invoice->id);
1190
1191 $canconvert = 0;
1192 if ($this->invoice->type == Facture::TYPE_DEPOSIT && empty($discountcheck->id)) {
1193 $canconvert = 1; // we can convert deposit into discount if deposit is paid (completely, partially or not at all) and not already converted (see real condition into condition used to show button converttoreduc)
1194 }
1195 if (($this->invoice->type == Facture::TYPE_CREDIT_NOTE || $this->invoice->type == Facture::TYPE_STANDARD) && $this->invoice->paye == 0 && empty($discountcheck->id)) {
1196 $canconvert = 1; // we can convert credit note into discount if credit note is not paid back and not already converted and amount of payment is 0 (see real condition into condition used to show button converttoreduc)
1197 }
1198 if ($canconvert) {
1199 $this->db->begin();
1200
1201 $amount_ht = $amount_tva = $amount_ttc = array();
1202 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1203 '
1204 @phan-var-force array<string,float> $amount_ht
1205 @phan-var-force array<string,float> $amount_tva
1206 @phan-var-force array<string,float> $amount_ttc
1207 @phan-var-force array<string,float> $multicurrency_amount_ht
1208 @phan-var-force array<string,float> $multicurrency_amount_tva
1209 @phan-var-force array<string,float> $multicurrency_amount_ttc
1210 ';
1211
1212 // Loop on each vat rate
1213 $i = 0;
1214 foreach ($this->invoice->lines as $line) {
1215 if ($line->product_type < 9 && $line->total_ht != 0) { // Remove lines with product_type greater than or equal to 9
1216 if (!array_key_exists($line->tva_tx, $amount_ht)) {
1217 $amount_ht[$line->tva_tx] = 0.0;
1218 $amount_tva[$line->tva_tx] = 0.0;
1219 $amount_ttc[$line->tva_tx] = 0.0;
1220 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
1221 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
1222 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
1223 }
1224 // no need to create discount if amount is null
1225 $amount_ht[$line->tva_tx] += $line->total_ht;
1226 $amount_tva[$line->tva_tx] += $line->total_tva;
1227 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1228 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1229 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1230 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1231 $i++;
1232 }
1233 }
1234
1235 // Insert one discount by VAT rate category
1236 $discount = new DiscountAbsolute($this->db);
1237 if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1238 $discount->description = '(CREDIT_NOTE)';
1239 } elseif ($this->invoice->type == Facture::TYPE_DEPOSIT) {
1240 $discount->description = '(DEPOSIT)';
1241 } elseif ($this->invoice->type == Facture::TYPE_STANDARD || $this->invoice->type == Facture::TYPE_REPLACEMENT || $this->invoice->type == Facture::TYPE_SITUATION) {
1242 $discount->description = '(EXCESS RECEIVED)';
1243 } else {
1244 throw new RestException(500, 'Cant convert to reduc an Invoice of this type');
1245 }
1246
1247 $discount->fk_soc = $this->invoice->socid;
1248 $discount->socid = $this->invoice->socid;
1249 $discount->fk_facture_source = $this->invoice->id;
1250
1251 $error = 0;
1252
1253 if ($this->invoice->type == Facture::TYPE_STANDARD || $this->invoice->type == Facture::TYPE_REPLACEMENT || $this->invoice->type == Facture::TYPE_SITUATION) {
1254 // If we're on a standard invoice, we have to get excess received to create a discount in TTC without VAT
1255
1256 // Total payments
1257 $sql = 'SELECT SUM(pf.amount) as total_payments';
1258 $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf, '.MAIN_DB_PREFIX.'paiement as p';
1259 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id';
1260 $sql .= ' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1261 $sql .= ' AND pf.fk_paiement = p.rowid';
1262 $sql .= ' AND p.entity IN ('.getEntity('invoice').')';
1263 $resql = $this->db->query($sql);
1264 if (!$resql) {
1265 dol_print_error($this->db);
1266 }
1267
1268 $res = $this->db->fetch_object($resql);
1269 $total_payments = $res->total_payments;
1270
1271 // Total credit note and deposit
1272 $total_creditnote_and_deposit = 0;
1273 $sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1274 $sql .= " re.description, re.fk_facture_source";
1275 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re";
1276 $sql .= " WHERE fk_facture = ".((int) $this->invoice->id);
1277 $resql = $this->db->query($sql);
1278 if (!empty($resql)) {
1279 while ($obj = $this->db->fetch_object($resql)) {
1280 $total_creditnote_and_deposit += $obj->amount_ttc;
1281 }
1282 } else {
1283 dol_print_error($this->db);
1284 }
1285
1286 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1287 $discount->amount_tva = 0;
1288 $discount->tva_tx = 0;
1289
1290 $result = $discount->create(DolibarrApiAccess::$user);
1291 if ($result < 0) {
1292 $error++;
1293 }
1294 }
1295 if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE || $this->invoice->type == Facture::TYPE_DEPOSIT) {
1296 foreach ($amount_ht as $tva_tx => $xxx) {
1297 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1298 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1299 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1300 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1301 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1302 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1303 $discount->tva_tx = abs((float) $tva_tx);
1304
1305 $result = $discount->create(DolibarrApiAccess::$user);
1306 if ($result < 0) {
1307 $error++;
1308 break;
1309 }
1310 }
1311 }
1312
1313 if (empty($error)) {
1314 if ($this->invoice->type != Facture::TYPE_DEPOSIT) {
1315 // Set the invoice as paid
1316 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1317 if ($result >= 0) {
1318 $this->db->commit();
1319 } else {
1320 $this->db->rollback();
1321 throw new RestException(500, 'Could not set paid');
1322 }
1323 } else {
1324 $this->db->commit();
1325 }
1326 } else {
1327 $this->db->rollback();
1328 throw new RestException(500, 'Discount creation error');
1329 }
1330 }
1331
1332 return $this->_cleanObjectDatas($this->invoice);
1333 }
1334
1351 public function useDiscount($id, $discountid)
1352 {
1353 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
1354 throw new RestException(403);
1355 }
1356 if (empty($id)) {
1357 throw new RestException(400, 'Invoice ID is mandatory');
1358 }
1359 if (empty($discountid)) {
1360 throw new RestException(400, 'Discount ID is mandatory');
1361 }
1362
1363 if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1364 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1365 }
1366
1367 $result = $this->invoice->fetch($id);
1368 if (!$result) {
1369 throw new RestException(404, 'Invoice not found');
1370 }
1371
1372 $result = $this->invoice->insert_discount($discountid);
1373 if ($result < 0) {
1374 throw new RestException(405, $this->invoice->error);
1375 }
1376
1377 return $result;
1378 }
1379
1396 public function useCreditNote($id, $discountid)
1397 {
1398 require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1399
1400 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
1401 throw new RestException(403);
1402 }
1403 if (empty($id)) {
1404 throw new RestException(400, 'Invoice ID is mandatory');
1405 }
1406 if (empty($discountid)) {
1407 throw new RestException(400, 'Credit ID is mandatory');
1408 }
1409
1410 if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1411 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1412 }
1413 $discount = new DiscountAbsolute($this->db);
1414 $result = $discount->fetch($discountid);
1415 if (!$result) {
1416 throw new RestException(404, 'Credit not found');
1417 }
1418
1419 $result = $discount->link_to_invoice(0, $id);
1420 if ($result < 0) {
1421 throw new RestException(405, $discount->error);
1422 }
1423
1424 return $result;
1425 }
1426
1440 public function getPayments($id)
1441 {
1442 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1443 throw new RestException(403);
1444 }
1445 if (empty($id)) {
1446 throw new RestException(400, 'Invoice ID is mandatory');
1447 }
1448
1449 if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1450 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1451 }
1452
1453 $result = $this->invoice->fetch($id);
1454 if (!$result) {
1455 throw new RestException(404, 'Invoice not found');
1456 }
1457
1458 $result = $this->invoice->getListOfPayments();
1459 if (!is_array($result) && $result < 0) {
1460 throw new RestException(405, $this->invoice->error);
1461 }
1462
1463 return $result;
1464 }
1465
1466
1488 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment = '', $comment = '', $chqemetteur = '', $chqbank = '')
1489 {
1490 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
1491
1492 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
1493 throw new RestException(403);
1494 }
1495 if (empty($id)) {
1496 throw new RestException(400, 'Invoice ID is mandatory');
1497 }
1498
1499 if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1500 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1501 }
1502
1503 if (isModEnabled("bank")) {
1504 if (empty($accountid)) {
1505 throw new RestException(400, 'Account ID is mandatory');
1506 }
1507 }
1508
1509 if (empty($paymentid)) {
1510 throw new RestException(400, 'Payment ID or Payment Code is mandatory');
1511 }
1512
1513
1514 $result = $this->invoice->fetch($id);
1515 if (!$result) {
1516 throw new RestException(404, 'Invoice not found');
1517 }
1518
1519 // Calculate amount to pay
1520 $totalpaid = $this->invoice->getSommePaiement();
1521 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1522 $totaldeposits = $this->invoice->getSumDepositsUsed();
1523 $resteapayer = price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
1524
1525 $this->db->begin();
1526
1527 $amounts = array();
1528 $multicurrency_amounts = array();
1529
1530 // Clean parameters amount if payment is for a credit note
1531 if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1532 $resteapayer = price2num($resteapayer, 'MT');
1533 $amounts[$id] = (float) price2num(-1 * (float) $resteapayer, 'MT');
1534 // Multicurrency
1535 $newvalue = price2num($this->invoice->multicurrency_total_ttc, 'MT');
1536 $multicurrency_amounts[$id] = (float) price2num(-1 * (float) $newvalue, 'MT');
1537 } else {
1538 $resteapayer = price2num($resteapayer, 'MT');
1539 $amounts[$id] = (float) $resteapayer;
1540 // Multicurrency
1541 $newvalue = price2num($this->invoice->multicurrency_total_ttc, 'MT');
1542 $multicurrency_amounts[$id] = (float) $newvalue;
1543 }
1544
1545 // Creation of payment line
1546 $paymentobj = new Paiement($this->db);
1547 if (is_numeric($datepaye)) {
1548 $paymentobj->datepaye = $datepaye;
1549 } else {
1550 $paymentobj->datepaye = dol_stringtotime($datepaye);
1551 }
1552 $paymentobj->amounts = $amounts; // Array with all payments dispatching with invoice id
1553 $paymentobj->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching
1554 $paymentobj->paiementid = $paymentid;
1555 $paymentobj->paiementcode = (string) dol_getIdFromCode($this->db, (string) $paymentid, 'c_paiement', 'id', 'code', 1);
1556 $paymentobj->num_payment = $num_payment;
1557 $paymentobj->note_private = $comment;
1558
1559 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices == 'yes' ? 1 : 0)); // This include closing invoices
1560 if ($payment_id < 0) {
1561 $this->db->rollback();
1562 throw new RestException(400, 'Payment error : '.$paymentobj->error);
1563 }
1564
1565 if (isModEnabled("bank")) {
1566 $label = '(CustomerInvoicePayment)';
1567
1568 if ($paymentobj->paiementcode == 'CHQ' && empty($chqemetteur)) {
1569 throw new RestException(400, 'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1570 }
1571 if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1572 $label = '(CustomerInvoicePaymentBack)'; // Refund of a credit note
1573 }
1574 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user, 'payment', $label, $accountid, $chqemetteur, $chqbank);
1575 if ($result < 0) {
1576 $this->db->rollback();
1577 throw new RestException(400, 'Add payment to bank error : '.$paymentobj->error);
1578 }
1579 }
1580
1581 $this->db->commit();
1582
1583 return $payment_id;
1584 }
1585
1612 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment = '', $comment = '', $chqemetteur = '', $chqbank = '', $ref_ext = '', $accepthigherpayment = false)
1613 {
1614 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
1615
1616 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
1617 throw new RestException(403);
1618 }
1619 foreach ($arrayofamounts as $id => $amount) {
1620 if (empty($id)) {
1621 throw new RestException(400, 'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1622 }
1623 if (!DolibarrApi::_checkAccessToResource('facture', (int) $id)) {
1624 throw new RestException(403, 'Access not allowed on invoice ID '.$id.' for login '.DolibarrApiAccess::$user->login);
1625 }
1626 }
1627
1628 if (isModEnabled("bank")) {
1629 if (empty($accountid)) {
1630 throw new RestException(400, 'Account ID is mandatory');
1631 }
1632 }
1633 if (empty($paymentid)) {
1634 throw new RestException(400, 'Payment ID or Payment Code is mandatory');
1635 }
1636
1637 $this->db->begin();
1638
1639 $amounts = array();
1640 $multicurrency_amounts = array();
1641
1642 // Loop on each invoice to pay
1643 foreach ($arrayofamounts as $id => $amountarray) {
1644 $result = $this->invoice->fetch((int) $id);
1645 if (!$result) {
1646 $this->db->rollback();
1647 throw new RestException(404, 'Invoice ID '.$id.' not found');
1648 }
1649
1650 if (($amountarray["amount"] == "remain" || $amountarray["amount"] > 0) && ($amountarray["multicurrency_amount"] == "remain" || $amountarray["multicurrency_amount"] > 0)) {
1651 $this->db->rollback();
1652 throw new RestException(400, 'Payment in both currency '.$id.' ( amount: '.$amountarray["amount"].', multicurrency_amount: '.$amountarray["multicurrency_amount"].')');
1653 }
1654
1655 $is_multicurrency = 0;
1656 $total_ttc = $this->invoice->total_ttc;
1657
1658 if ($amountarray["multicurrency_amount"] > 0 || $amountarray["multicurrency_amount"] == "remain") {
1659 $is_multicurrency = 1;
1660 $total_ttc = $this->invoice->multicurrency_total_ttc;
1661 }
1662
1663 // Calculate amount to pay
1664 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1665 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1666 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1667 $remainstopay = $amount = (float) price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
1668
1669 if (!$is_multicurrency && $amountarray["amount"] != 'remain') {
1670 $amount = (float) price2num($amountarray["amount"], 'MT');
1671 }
1672
1673 if ($is_multicurrency && $amountarray["multicurrency_amount"] != 'remain') {
1674 $amount = (float) price2num($amountarray["multicurrency_amount"], 'MT');
1675 }
1676
1677 if (abs($amount) > abs($remainstopay) && !$accepthigherpayment) {
1678 $this->db->rollback();
1679 throw new RestException(400, 'Payment amount on invoice ID '.$id.' ('.$amount.') is higher than remain to pay ('.$remainstopay.')');
1680 }
1681
1682 if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1683 $amount = (float) price2num(-1 * abs((float) $amount), 'MT');
1684 }
1685
1686 if ($is_multicurrency) {
1687 $amounts[$id] = null;
1688 // Multicurrency
1689 $multicurrency_amounts[$id] = (float) $amount;
1690 } else {
1691 $amounts[$id] = (float) $amount;
1692 // Multicurrency
1693 $multicurrency_amounts[$id] = null;
1694 }
1695 }
1696
1697 // Creation of payment line
1698 $paymentobj = new Paiement($this->db);
1699 if (is_numeric($datepaye)) {
1700 $paymentobj->datepaye = $datepaye;
1701 } else {
1702 $paymentobj->datepaye = dol_stringtotime($datepaye);
1703 }
1704 $paymentobj->amounts = $amounts; // Array with all payments dispatching with invoice id
1705 $paymentobj->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching
1706 $paymentobj->paiementid = $paymentid;
1707 $paymentobj->paiementcode = (string) dol_getIdFromCode($this->db, (string) $paymentid, 'c_paiement', 'id', 'code', 1);
1708 $paymentobj->num_payment = $num_payment;
1709 $paymentobj->note_private = $comment;
1710 $paymentobj->ref_ext = $ref_ext;
1711 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices == 'yes' ? 1 : 0)); // This include closing invoices
1712 if ($payment_id < 0) {
1713 $this->db->rollback();
1714 throw new RestException(400, 'Payment error : '.$paymentobj->error);
1715 }
1716 if (isModEnabled("bank")) {
1717 $label = '(CustomerInvoicePayment)';
1718 if ($paymentobj->paiementcode == 'CHQ' && empty($chqemetteur)) {
1719 throw new RestException(400, 'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1720 }
1721 if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1722 $label = '(CustomerInvoicePaymentBack)'; // Refund of a credit note
1723 }
1724 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user, 'payment', $label, $accountid, $chqemetteur, $chqbank);
1725 if ($result < 0) {
1726 $this->db->rollback();
1727 throw new RestException(400, 'Add payment to bank error : '.$paymentobj->error);
1728 }
1729 }
1730
1731 $this->db->commit();
1732
1733 return $payment_id;
1734 }
1735
1750 public function putPayment($id, $num_payment = '')
1751 {
1752 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
1753
1754 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
1755 throw new RestException(403);
1756 }
1757 if (empty($id)) {
1758 throw new RestException(400, 'Payment ID is mandatory');
1759 }
1760
1761 $paymentobj = new Paiement($this->db);
1762 $result = $paymentobj->fetch($id);
1763
1764 if (!$result) {
1765 throw new RestException(404, 'Payment not found');
1766 }
1767
1768 if (!empty($num_payment)) {
1769 $result = $paymentobj->update_num($num_payment);
1770 if ($result < 0) {
1771 throw new RestException(500, 'Error when updating the payment num');
1772 }
1773 }
1774
1775 return [
1776 'success' => [
1777 'code' => 200,
1778 'message' => 'Payment updated'
1779 ]
1780 ];
1781 }
1782
1783 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1790 protected function _cleanObjectDatas($object)
1791 {
1792 // phpcs:enable
1793 $object = parent::_cleanObjectDatas($object);
1794
1795 unset($object->note);
1796 unset($object->address);
1797 unset($object->barcode_type);
1798 unset($object->barcode_type_code);
1799 unset($object->barcode_type_label);
1800 unset($object->barcode_type_coder);
1801 unset($object->canvas);
1802
1803 return $object;
1804 }
1805
1814 private function _validate($data)
1815 {
1816 if ($data === null) {
1817 $data = array();
1818 }
1819 $invoice = array();
1820 foreach (Invoices::$FIELDS as $field) {
1821 if (!isset($data[$field])) {
1822 throw new RestException(400, "$field field missing");
1823 }
1824 $invoice[$field] = $data[$field];
1825 }
1826 return $invoice;
1827 }
1828
1829
1843 public function getTemplateInvoice($id, $contact_list = 1)
1844 {
1845 return $this->_fetchTemplateInvoice($id, '', '', $contact_list);
1846 }
1847
1861 private function _fetchTemplateInvoice($id, $ref = '', $ref_ext = '', $contact_list = 1)
1862 {
1863 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1864 throw new RestException(403);
1865 }
1866
1867 $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1868 if (!$result) {
1869 throw new RestException(404, 'Template invoice not found');
1870 }
1871
1872 if (!DolibarrApi::_checkAccessToResource('facturerec', $this->template_invoice->id)) {
1873 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1874 }
1875
1876 // Add external contacts ids
1877 if ($contact_list > -1) {
1878 $tmparray = $this->template_invoice->liste_contact(-1, 'external', $contact_list);
1879 if (is_array($tmparray)) {
1880 $this->template_invoice->contacts_ids = $tmparray;
1881 }
1882 }
1883
1884 $this->template_invoice->fetchObjectLinked();
1885 return $this->_cleanTemplateObjectDatas($this->template_invoice);
1886 }
1887
1888
1889 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1897 {
1898 // phpcs:enable
1899 $object = parent::_cleanObjectDatas($object);
1900
1901 unset($object->note);
1902 unset($object->address);
1903 unset($object->barcode_type);
1904 unset($object->barcode_type_code);
1905 unset($object->barcode_type_label);
1906 unset($object->barcode_type_coder);
1907 unset($object->canvas);
1908
1909 return $object;
1910 }
1911}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class to manage customers orders.
Class to manage absolute discounts.
Class for API REST v1.
Definition api.class.php:31
_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:83
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.
__construct()
Constructor.
put($id, $request_data=null)
Update invoice.
validate($id, $force_number='', $idwarehouse=0, $notrigger=0)
Validate an 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.
createInvoiceFromContract($contractid)
Create an invoice using a contract.
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.
_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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='', $pagination_data=false, $loadlinkedobjects=0)
List invoices.
deleteLine($id, $lineid)
Deletes a line of a given invoice.
Class to manage payments of customer invoices.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition date.lib.php:431
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)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.