dolibarr  17.0.3
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 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
22 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture-rec.class.php';
23 
24 
31 class Invoices extends DolibarrApi
32 {
37  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->rights->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 
179  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $status = '', $sqlfilters = '')
180  {
181  global $db, $conf;
182 
183  if (!DolibarrApiAccess::$user->rights->facture->lire) {
184  throw new RestException(401);
185  }
186 
187  $obj_ret = array();
188 
189  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
190  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
191 
192  // If the internal user must only see his customers, force searching by him
193  $search_sale = 0;
194  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
195  $search_sale = DolibarrApiAccess::$user->id;
196  }
197 
198  $sql = "SELECT t.rowid";
199  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
200  $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)
201  }
202  $sql .= " FROM ".MAIN_DB_PREFIX."facture as t";
203 
204  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
205  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
206  }
207 
208  $sql .= ' WHERE t.entity IN ('.getEntity('invoice').')';
209  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
210  $sql .= " AND t.fk_soc = sc.fk_soc";
211  }
212  if ($socids) {
213  $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
214  }
215 
216  if ($search_sale > 0) {
217  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
218  }
219 
220  // Filter by status
221  if ($status == 'draft') {
222  $sql .= " AND t.fk_statut IN (0)";
223  }
224  if ($status == 'unpaid') {
225  $sql .= " AND t.fk_statut IN (1)";
226  }
227  if ($status == 'paid') {
228  $sql .= " AND t.fk_statut IN (2)";
229  }
230  if ($status == 'cancelled') {
231  $sql .= " AND t.fk_statut IN (3)";
232  }
233  // Insert sale filter
234  if ($search_sale > 0) {
235  $sql .= " AND sc.fk_user = ".((int) $search_sale);
236  }
237  // Add sql filters
238  if ($sqlfilters) {
239  $errormessage = '';
240  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
241  if ($errormessage) {
242  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
243  }
244  }
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)) {
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  $obj_ret[] = $this->_cleanObjectDatas($invoice_static);
277  }
278  $i++;
279  }
280  } else {
281  throw new RestException(503, 'Error when retrieve invoice list : '.$this->db->lasterror());
282  }
283  if (!count($obj_ret)) {
284  throw new RestException(404, 'No invoice found');
285  }
286  return $obj_ret;
287  }
288 
295  public function post($request_data = null)
296  {
297  if (!DolibarrApiAccess::$user->rights->facture->creer) {
298  throw new RestException(401, "Insuffisant rights");
299  }
300  // Check mandatory fields
301  $result = $this->_validate($request_data);
302 
303  foreach ($request_data as $field => $value) {
304  $this->invoice->$field = $value;
305  }
306  if (!array_key_exists('date', $request_data)) {
307  $this->invoice->date = dol_now();
308  }
309  /* We keep lines as an array
310  if (isset($request_data["lines"])) {
311  $lines = array();
312  foreach ($request_data["lines"] as $line) {
313  array_push($lines, (object) $line);
314  }
315  $this->invoice->lines = $lines;
316  }*/
317 
318  if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data["date_lim_reglement"]) ? 0 : $request_data["date_lim_reglement"])) < 0) {
319  throw new RestException(500, "Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
320  }
321  return $this->invoice->id;
322  }
323 
338  public function createInvoiceFromOrder($orderid)
339  {
340 
341  require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
342 
343  if (!DolibarrApiAccess::$user->rights->commande->lire) {
344  throw new RestException(401);
345  }
346  if (!DolibarrApiAccess::$user->rights->facture->creer) {
347  throw new RestException(401);
348  }
349  if (empty($orderid)) {
350  throw new RestException(400, 'Order ID is mandatory');
351  }
352 
353  $order = new Commande($this->db);
354  $result = $order->fetch($orderid);
355  if (!$result) {
356  throw new RestException(404, 'Order not found');
357  }
358 
359  $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
360  if ($result < 0) {
361  throw new RestException(405, $this->invoice->error);
362  }
363  $this->invoice->fetchObjectLinked();
364  return $this->_cleanObjectDatas($this->invoice);
365  }
366 
376  public function getLines($id)
377  {
378  if (!DolibarrApiAccess::$user->rights->facture->lire) {
379  throw new RestException(401);
380  }
381 
382  $result = $this->invoice->fetch($id);
383  if (!$result) {
384  throw new RestException(404, 'Invoice not found');
385  }
386 
387  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
388  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
389  }
390  $this->invoice->getLinesArray();
391  $result = array();
392  foreach ($this->invoice->lines as $line) {
393  array_push($result, $this->_cleanObjectDatas($line));
394  }
395  return $result;
396  }
397 
413  public function putLine($id, $lineid, $request_data = null)
414  {
415  if (!DolibarrApiAccess::$user->rights->facture->creer) {
416  throw new RestException(401);
417  }
418 
419  $result = $this->invoice->fetch($id);
420  if (!$result) {
421  throw new RestException(404, 'Invoice not found');
422  }
423 
424  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
425  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
426  }
427 
428  $request_data = (object) $request_data;
429 
430  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
431  $request_data->label = sanitizeVal($request_data->label);
432 
433  $updateRes = $this->invoice->updateline(
434  $lineid,
435  $request_data->desc,
436  $request_data->subprice,
437  $request_data->qty,
438  $request_data->remise_percent,
439  $request_data->date_start,
440  $request_data->date_end,
441  $request_data->tva_tx,
442  $request_data->localtax1_tx,
443  $request_data->localtax2_tx,
444  $request_data->price_base_type ? $request_data->price_base_type : 'HT',
445  $request_data->info_bits,
446  $request_data->product_type,
447  $request_data->fk_parent_line,
448  0,
449  $request_data->fk_fournprice,
450  $request_data->pa_ht,
451  $request_data->label,
452  $request_data->special_code,
453  $request_data->array_options,
454  $request_data->situation_percent,
455  $request_data->fk_unit,
456  $request_data->multicurrency_subprice,
457  0,
458  $request_data->ref_ext,
459  $request_data->rang
460  );
461 
462  if ($updateRes > 0) {
463  $result = $this->get($id);
464  unset($result->line);
465  return $this->_cleanObjectDatas($result);
466  } else {
467  throw new RestException(304, $this->invoice->error);
468  }
469  }
470 
485  public function postContact($id, $contactid, $type)
486  {
487  if (!DolibarrApiAccess::$user->rights->facture->creer) {
488  throw new RestException(401);
489  }
490 
491  $result = $this->invoice->fetch($id);
492 
493  if (!$result) {
494  throw new RestException(404, 'Invoice not found');
495  }
496 
497  if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
498  throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
499  }
500 
501  if (!DolibarrApi::_checkAccessToResource('invoice', $this->invoice->id)) {
502  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
503  }
504 
505  $result = $this->invoice->add_contact($contactid, $type, 'external');
506 
507  if (!$result) {
508  throw new RestException(500, 'Error when added the contact');
509  }
510 
511  return $this->_cleanObjectDatas($this->invoice);
512  }
513 
529  public function deleteContact($id, $contactid, $type)
530  {
531  if (!DolibarrApiAccess::$user->rights->facture->creer) {
532  throw new RestException(401);
533  }
534 
535  $result = $this->invoice->fetch($id);
536 
537  if (!$result) {
538  throw new RestException(404, 'Invoice not found');
539  }
540 
541  if (!DolibarrApi::_checkAccessToResource('invoice', $this->invoice->id)) {
542  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
543  }
544 
545  $contacts = $this->invoice->liste_contact();
546 
547  foreach ($contacts as $contact) {
548  if ($contact['id'] == $contactid && $contact['code'] == $type) {
549  $result = $this->invoice->delete_contact($contact['rowid']);
550 
551  if (!$result) {
552  throw new RestException(500, 'Error when deleted the contact');
553  }
554  }
555  }
556 
557  return $this->_cleanObjectDatas($this->invoice);
558  }
559 
575  public function deleteLine($id, $lineid)
576  {
577 
578  if (!DolibarrApiAccess::$user->rights->facture->creer) {
579  throw new RestException(401);
580  }
581  if (empty($lineid)) {
582  throw new RestException(400, 'Line ID is mandatory');
583  }
584 
585  if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
586  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
587  }
588 
589  $result = $this->invoice->fetch($id);
590  if (!$result) {
591  throw new RestException(404, 'Invoice not found');
592  }
593 
594  // TODO Check the lineid $lineid is a line of ojbect
595 
596  $updateRes = $this->invoice->deleteline($lineid);
597  if ($updateRes > 0) {
598  return $this->get($id);
599  } else {
600  throw new RestException(405, $this->invoice->error);
601  }
602  }
603 
611  public function put($id, $request_data = null)
612  {
613  if (!DolibarrApiAccess::$user->rights->facture->creer) {
614  throw new RestException(401);
615  }
616 
617  $result = $this->invoice->fetch($id);
618  if (!$result) {
619  throw new RestException(404, 'Invoice not found');
620  }
621 
622  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
623  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
624  }
625 
626  foreach ($request_data as $field => $value) {
627  if ($field == 'id') {
628  continue;
629  }
630  $this->invoice->$field = $value;
631  }
632 
633  // update bank account
634  if (!empty($this->invoice->fk_account)) {
635  if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
636  throw new RestException(400, $this->invoice->error);
637  }
638  }
639 
640  if ($this->invoice->update(DolibarrApiAccess::$user)) {
641  return $this->get($id);
642  }
643 
644  return false;
645  }
646 
653  public function delete($id)
654  {
655  if (!DolibarrApiAccess::$user->rights->facture->supprimer) {
656  throw new RestException(401);
657  }
658  $result = $this->invoice->fetch($id);
659  if (!$result) {
660  throw new RestException(404, 'Invoice not found');
661  }
662 
663  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
664  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
665  }
666 
667  $result = $this->invoice->delete(DolibarrApiAccess::$user);
668  if ($result < 0) {
669  throw new RestException(500, 'Error when deleting invoice');
670  } elseif ($result == 0) {
671  throw new RestException(403, 'Invoice not erasable');
672  }
673 
674  return array(
675  'success' => array(
676  'code' => 200,
677  'message' => 'Invoice deleted'
678  )
679  );
680  }
681 
705  public function postLine($id, $request_data = null)
706  {
707  if (!DolibarrApiAccess::$user->rights->facture->creer) {
708  throw new RestException(401);
709  }
710 
711  $result = $this->invoice->fetch($id);
712  if (!$result) {
713  throw new RestException(404, 'Invoice not found');
714  }
715 
716  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
717  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
718  }
719 
720  $request_data = (object) $request_data;
721 
722  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
723  $request_data->label = sanitizeVal($request_data->label);
724 
725  // Reset fk_parent_line for no child products and special product
726  if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
727  $request_data->fk_parent_line = 0;
728  }
729 
730  // calculate pa_ht
731  $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);
732  $pa_ht = $marginInfos[0];
733 
734  $updateRes = $this->invoice->addline(
735  $request_data->desc,
736  $request_data->subprice,
737  $request_data->qty,
738  $request_data->tva_tx,
739  $request_data->localtax1_tx,
740  $request_data->localtax2_tx,
741  $request_data->fk_product,
742  $request_data->remise_percent,
743  $request_data->date_start,
744  $request_data->date_end,
745  $request_data->fk_code_ventilation,
746  $request_data->info_bits,
747  $request_data->fk_remise_except,
748  $request_data->price_base_type ? $request_data->price_base_type : 'HT',
749  $request_data->subprice,
750  $request_data->product_type,
751  $request_data->rang,
752  $request_data->special_code,
753  $request_data->origin,
754  $request_data->origin_id,
755  $request_data->fk_parent_line,
756  empty($request_data->fk_fournprice) ?null:$request_data->fk_fournprice,
757  $pa_ht,
758  $request_data->label,
759  $request_data->array_options,
760  $request_data->situation_percent,
761  $request_data->fk_prev_id,
762  $request_data->fk_unit,
763  0,
764  $request_data->ref_ext
765  );
766 
767  if ($updateRes < 0) {
768  throw new RestException(400, 'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
769  }
770 
771  return $updateRes;
772  }
773 
793  public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
794  {
795  if (!DolibarrApiAccess::$user->rights->facture->creer) {
796  throw new RestException(401);
797  }
798  $result = $this->invoice->fetch($id);
799  if (!$result) {
800  throw new RestException(404, 'Invoice not found');
801  }
802 
803  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
804  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
805  }
806 
807  $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
808  if ($result < 0) {
809  throw new RestException(500, 'Error : '.$this->invoice->error);
810  }
811 
812  $result = $this->invoice->fetch($id);
813  if (!$result) {
814  throw new RestException(404, 'Invoice not found');
815  }
816 
817  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
818  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
819  }
820 
821  return $this->_cleanObjectDatas($this->invoice);
822  }
823 
824 
825 
842  public function settodraft($id, $idwarehouse = -1)
843  {
844  if (!DolibarrApiAccess::$user->rights->facture->creer) {
845  throw new RestException(401);
846  }
847  $result = $this->invoice->fetch($id);
848  if (!$result) {
849  throw new RestException(404, 'Invoice not found');
850  }
851 
852  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
853  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
854  }
855 
856  $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
857  if ($result == 0) {
858  throw new RestException(304, 'Nothing done.');
859  }
860  if ($result < 0) {
861  throw new RestException(500, 'Error : '.$this->invoice->error);
862  }
863 
864  $result = $this->invoice->fetch($id);
865  if (!$result) {
866  throw new RestException(404, 'Invoice not found');
867  }
868 
869  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
870  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
871  }
872 
873  return $this->_cleanObjectDatas($this->invoice);
874  }
875 
876 
894  public function validate($id, $idwarehouse = 0, $notrigger = 0)
895  {
896  if (!DolibarrApiAccess::$user->rights->facture->creer) {
897  throw new RestException(401);
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(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
906  }
907 
908  $result = $this->invoice->validate(DolibarrApiAccess::$user, '', $idwarehouse, $notrigger);
909  if ($result == 0) {
910  throw new RestException(304, 'Error nothing done. May be object is already validated');
911  }
912  if ($result < 0) {
913  throw new RestException(500, 'Error when validating Invoice: '.$this->invoice->error);
914  }
915 
916  $result = $this->invoice->fetch($id);
917  if (!$result) {
918  throw new RestException(404, 'Invoice not found');
919  }
920 
921  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
922  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
923  }
924 
925  return $this->_cleanObjectDatas($this->invoice);
926  }
927 
944  public function settopaid($id, $close_code = '', $close_note = '')
945  {
946  if (!DolibarrApiAccess::$user->rights->facture->creer) {
947  throw new RestException(401);
948  }
949  $result = $this->invoice->fetch($id);
950  if (!$result) {
951  throw new RestException(404, 'Invoice not found');
952  }
953 
954  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
955  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
956  }
957 
958  $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
959  if ($result == 0) {
960  throw new RestException(304, 'Error nothing done. May be object is already validated');
961  }
962  if ($result < 0) {
963  throw new RestException(500, 'Error : '.$this->invoice->error);
964  }
965 
966 
967  $result = $this->invoice->fetch($id);
968  if (!$result) {
969  throw new RestException(404, 'Invoice not found');
970  }
971 
972  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
973  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
974  }
975 
976  return $this->_cleanObjectDatas($this->invoice);
977  }
978 
979 
994  public function settounpaid($id)
995  {
996  if (!DolibarrApiAccess::$user->rights->facture->creer) {
997  throw new RestException(401);
998  }
999  $result = $this->invoice->fetch($id);
1000  if (!$result) {
1001  throw new RestException(404, 'Invoice not found');
1002  }
1003 
1004  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1005  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1006  }
1007 
1008  $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1009  if ($result == 0) {
1010  throw new RestException(304, 'Nothing done');
1011  }
1012  if ($result < 0) {
1013  throw new RestException(500, 'Error : '.$this->invoice->error);
1014  }
1015 
1016 
1017  $result = $this->invoice->fetch($id);
1018  if (!$result) {
1019  throw new RestException(404, 'Invoice not found');
1020  }
1021 
1022  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1023  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1024  }
1025 
1026  return $this->_cleanObjectDatas($this->invoice);
1027  }
1028 
1038  public function getDiscount($id)
1039  {
1040  require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1041 
1042  if (!DolibarrApiAccess::$user->rights->facture->lire) {
1043  throw new RestException(401);
1044  }
1045 
1046  $result = $this->invoice->fetch($id);
1047  if (!$result) {
1048  throw new RestException(404, 'Invoice not found');
1049  }
1050 
1051  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1052  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1053  }
1054 
1055  $discountcheck = new DiscountAbsolute($this->db);
1056  $result = $discountcheck->fetch(0, $this->invoice->id);
1057 
1058  if ($result == 0) {
1059  throw new RestException(404, 'Discount not found');
1060  }
1061  if ($result < 0) {
1062  throw new RestException(500, $discountcheck->error);
1063  }
1064 
1065  return parent::_cleanObjectDatas($discountcheck);
1066  }
1067 
1081  public function markAsCreditAvailable($id)
1082  {
1083  require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1084 
1085  if (!DolibarrApiAccess::$user->rights->facture->creer) {
1086  throw new RestException(401);
1087  }
1088 
1089  $result = $this->invoice->fetch($id);
1090  if (!$result) {
1091  throw new RestException(404, 'Invoice not found');
1092  }
1093 
1094  if (!DolibarrApi::_checkAccessToResource('facture', $this->invoice->id)) {
1095  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1096  }
1097 
1098  if ($this->invoice->paye) {
1099  throw new RestException(500, 'Alreay paid');
1100  }
1101 
1102  $this->invoice->fetch($id);
1103  $this->invoice->fetch_thirdparty();
1104 
1105  // Check if there is already a discount (protection to avoid duplicate creation when resubmit post)
1106  $discountcheck = new DiscountAbsolute($this->db);
1107  $result = $discountcheck->fetch(0, $this->invoice->id);
1108 
1109  $canconvert = 0;
1110  if ($this->invoice->type == Facture::TYPE_DEPOSIT && empty($discountcheck->id)) {
1111  $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)
1112  }
1113  if (($this->invoice->type == Facture::TYPE_CREDIT_NOTE || $this->invoice->type == Facture::TYPE_STANDARD) && $this->invoice->paye == 0 && empty($discountcheck->id)) {
1114  $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)
1115  }
1116  if ($canconvert) {
1117  $this->db->begin();
1118 
1119  $amount_ht = $amount_tva = $amount_ttc = array();
1120  $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1121 
1122  // Loop on each vat rate
1123  $i = 0;
1124  foreach ($this->invoice->lines as $line) {
1125  if ($line->product_type < 9 && $line->total_ht != 0) { // Remove lines with product_type greater than or equal to 9
1126  // no need to create discount if amount is null
1127  $amount_ht[$line->tva_tx] += $line->total_ht;
1128  $amount_tva[$line->tva_tx] += $line->total_tva;
1129  $amount_ttc[$line->tva_tx] += $line->total_ttc;
1130  $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1131  $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1132  $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1133  $i++;
1134  }
1135  }
1136 
1137  // Insert one discount by VAT rate category
1138  $discount = new DiscountAbsolute($this->db);
1139  if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1140  $discount->description = '(CREDIT_NOTE)';
1141  } elseif ($this->invoice->type == Facture::TYPE_DEPOSIT) {
1142  $discount->description = '(DEPOSIT)';
1143  } elseif ($this->invoice->type == Facture::TYPE_STANDARD || $this->invoice->type == Facture::TYPE_REPLACEMENT || $this->invoice->type == Facture::TYPE_SITUATION) {
1144  $discount->description = '(EXCESS RECEIVED)';
1145  } else {
1146  throw new RestException(500, 'Cant convert to reduc an Invoice of this type');
1147  }
1148 
1149  $discount->fk_soc = $this->invoice->socid;
1150  $discount->fk_facture_source = $this->invoice->id;
1151 
1152  $error = 0;
1153 
1154  if ($this->invoice->type == Facture::TYPE_STANDARD || $this->invoice->type == Facture::TYPE_REPLACEMENT || $this->invoice->type == Facture::TYPE_SITUATION) {
1155  // If we're on a standard invoice, we have to get excess received to create a discount in TTC without VAT
1156 
1157  // Total payments
1158  $sql = 'SELECT SUM(pf.amount) as total_payments';
1159  $sql .= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf, '.MAIN_DB_PREFIX.'paiement as p';
1160  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id';
1161  $sql .= ' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1162  $sql .= ' AND pf.fk_paiement = p.rowid';
1163  $sql .= ' AND p.entity IN ('.getEntity('invoice').')';
1164  $resql = $this->db->query($sql);
1165  if (!$resql) {
1166  dol_print_error($this->db);
1167  }
1168 
1169  $res = $this->db->fetch_object($resql);
1170  $total_payments = $res->total_payments;
1171 
1172  // Total credit note and deposit
1173  $total_creditnote_and_deposit = 0;
1174  $sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1175  $sql .= " re.description, re.fk_facture_source";
1176  $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re";
1177  $sql .= " WHERE fk_facture = ".((int) $this->invoice->id);
1178  $resql = $this->db->query($sql);
1179  if (!empty($resql)) {
1180  while ($obj = $this->db->fetch_object($resql)) {
1181  $total_creditnote_and_deposit += $obj->amount_ttc;
1182  }
1183  } else {
1184  dol_print_error($this->db);
1185  }
1186 
1187  $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1188  $discount->amount_tva = 0;
1189  $discount->tva_tx = 0;
1190 
1191  $result = $discount->create(DolibarrApiAccess::$user);
1192  if ($result < 0) {
1193  $error++;
1194  }
1195  }
1196  if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE || $this->invoice->type == Facture::TYPE_DEPOSIT) {
1197  foreach ($amount_ht as $tva_tx => $xxx) {
1198  $discount->amount_ht = abs($amount_ht[$tva_tx]);
1199  $discount->amount_tva = abs($amount_tva[$tva_tx]);
1200  $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1201  $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1202  $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1203  $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1204  $discount->tva_tx = abs($tva_tx);
1205 
1206  $result = $discount->create(DolibarrApiAccess::$user);
1207  if ($result < 0) {
1208  $error++;
1209  break;
1210  }
1211  }
1212  }
1213 
1214  if (empty($error)) {
1215  if ($this->invoice->type != Facture::TYPE_DEPOSIT) {
1216  // Classe facture
1217  $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1218  if ($result >= 0) {
1219  $this->db->commit();
1220  } else {
1221  $this->db->rollback();
1222  throw new RestException(500, 'Could not set paid');
1223  }
1224  } else {
1225  $this->db->commit();
1226  }
1227  } else {
1228  $this->db->rollback();
1229  throw new RestException(500, 'Discount creation error');
1230  }
1231  }
1232 
1233  return $this->_cleanObjectDatas($this->invoice);
1234  }
1235 
1253  public function useDiscount($id, $discountid)
1254  {
1255 
1256  if (!DolibarrApiAccess::$user->rights->facture->creer) {
1257  throw new RestException(401);
1258  }
1259  if (empty($id)) {
1260  throw new RestException(400, 'Invoice ID is mandatory');
1261  }
1262  if (empty($discountid)) {
1263  throw new RestException(400, 'Discount ID is mandatory');
1264  }
1265 
1266  if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1267  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1268  }
1269 
1270  $result = $this->invoice->fetch($id);
1271  if (!$result) {
1272  throw new RestException(404, 'Invoice not found');
1273  }
1274 
1275  $result = $this->invoice->insert_discount($discountid);
1276  if ($result < 0) {
1277  throw new RestException(405, $this->invoice->error);
1278  }
1279 
1280  return $result;
1281  }
1282 
1300  public function useCreditNote($id, $discountid)
1301  {
1302 
1303  require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1304 
1305  if (!DolibarrApiAccess::$user->rights->facture->creer) {
1306  throw new RestException(401);
1307  }
1308  if (empty($id)) {
1309  throw new RestException(400, 'Invoice ID is mandatory');
1310  }
1311  if (empty($discountid)) {
1312  throw new RestException(400, 'Credit ID is mandatory');
1313  }
1314 
1315  if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1316  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1317  }
1318  $discount = new DiscountAbsolute($this->db);
1319  $result = $discount->fetch($discountid);
1320  if (!$result) {
1321  throw new RestException(404, 'Credit not found');
1322  }
1323 
1324  $result = $discount->link_to_invoice(0, $id);
1325  if ($result < 0) {
1326  throw new RestException(405, $discount->error);
1327  }
1328 
1329  return $result;
1330  }
1331 
1346  public function getPayments($id)
1347  {
1348 
1349  if (!DolibarrApiAccess::$user->rights->facture->lire) {
1350  throw new RestException(401);
1351  }
1352  if (empty($id)) {
1353  throw new RestException(400, 'Invoice ID is mandatory');
1354  }
1355 
1356  if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1357  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1358  }
1359 
1360  $result = $this->invoice->fetch($id);
1361  if (!$result) {
1362  throw new RestException(404, 'Invoice not found');
1363  }
1364 
1365  $result = $this->invoice->getListOfPayments();
1366  if ($result < 0) {
1367  throw new RestException(405, $this->invoice->error);
1368  }
1369 
1370  return $result;
1371  }
1372 
1373 
1395  public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment = '', $comment = '', $chqemetteur = '', $chqbank = '')
1396  {
1397  global $conf;
1398 
1399  require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
1400 
1401  if (!DolibarrApiAccess::$user->rights->facture->creer) {
1402  throw new RestException(403);
1403  }
1404  if (empty($id)) {
1405  throw new RestException(400, 'Invoice ID is mandatory');
1406  }
1407 
1408  if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1409  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1410  }
1411 
1412  if (isModEnabled("banque")) {
1413  if (empty($accountid)) {
1414  throw new RestException(400, 'Account ID is mandatory');
1415  }
1416  }
1417 
1418  if (empty($paymentid)) {
1419  throw new RestException(400, 'Payment ID or Payment Code is mandatory');
1420  }
1421 
1422 
1423  $result = $this->invoice->fetch($id);
1424  if (!$result) {
1425  throw new RestException(404, 'Invoice not found');
1426  }
1427 
1428  // Calculate amount to pay
1429  $totalpaid = $this->invoice->getSommePaiement();
1430  $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1431  $totaldeposits = $this->invoice->getSumDepositsUsed();
1432  $resteapayer = price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
1433 
1434  $this->db->begin();
1435 
1436  $amounts = array();
1437  $multicurrency_amounts = array();
1438 
1439  // Clean parameters amount if payment is for a credit note
1440  if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1441  $resteapayer = price2num($resteapayer, 'MT');
1442  $amounts[$id] = -$resteapayer;
1443  // Multicurrency
1444  $newvalue = price2num($this->invoice->multicurrency_total_ttc, 'MT');
1445  $multicurrency_amounts[$id] = -$newvalue;
1446  } else {
1447  $resteapayer = price2num($resteapayer, 'MT');
1448  $amounts[$id] = $resteapayer;
1449  // Multicurrency
1450  $newvalue = price2num($this->invoice->multicurrency_total_ttc, 'MT');
1451  $multicurrency_amounts[$id] = $newvalue;
1452  }
1453 
1454  // Creation of payment line
1455  $paymentobj = new Paiement($this->db);
1456  $paymentobj->datepaye = $datepaye;
1457  $paymentobj->amounts = $amounts; // Array with all payments dispatching with invoice id
1458  $paymentobj->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching
1459  $paymentobj->paiementid = $paymentid;
1460  $paymentobj->paiementcode = dol_getIdFromCode($this->db, $paymentid, 'c_paiement', 'id', 'code', 1);
1461  $paymentobj->num_payment = $num_payment;
1462  $paymentobj->note_private = $comment;
1463 
1464  $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices == 'yes' ? 1 : 0)); // This include closing invoices
1465  if ($payment_id < 0) {
1466  $this->db->rollback();
1467  throw new RestException(400, 'Payment error : '.$paymentobj->error);
1468  }
1469 
1470  if (isModEnabled("banque")) {
1471  $label = '(CustomerInvoicePayment)';
1472 
1473  if ($paymentobj->paiementcode == 'CHQ' && empty($chqemetteur)) {
1474  throw new RestException(400, 'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1475  }
1476  if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1477  $label = '(CustomerInvoicePaymentBack)'; // Refund of a credit note
1478  }
1479  $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user, 'payment', $label, $accountid, $chqemetteur, $chqbank);
1480  if ($result < 0) {
1481  $this->db->rollback();
1482  throw new RestException(400, 'Add payment to bank error : '.$paymentobj->error);
1483  }
1484  }
1485 
1486  $this->db->commit();
1487 
1488  return $payment_id;
1489  }
1490 
1516  public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment = '', $comment = '', $chqemetteur = '', $chqbank = '', $ref_ext = '', $accepthigherpayment = false)
1517  {
1518  global $conf;
1519 
1520  require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
1521 
1522  if (!DolibarrApiAccess::$user->rights->facture->creer) {
1523  throw new RestException(403);
1524  }
1525  foreach ($arrayofamounts as $id => $amount) {
1526  if (empty($id)) {
1527  throw new RestException(400, 'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1528  }
1529  if (!DolibarrApi::_checkAccessToResource('facture', $id)) {
1530  throw new RestException(403, 'Access not allowed on invoice ID '.$id.' for login '.DolibarrApiAccess::$user->login);
1531  }
1532  }
1533 
1534  if (isModEnabled("banque")) {
1535  if (empty($accountid)) {
1536  throw new RestException(400, 'Account ID is mandatory');
1537  }
1538  }
1539  if (empty($paymentid)) {
1540  throw new RestException(400, 'Payment ID or Payment Code is mandatory');
1541  }
1542 
1543  $this->db->begin();
1544 
1545  $amounts = array();
1546  $multicurrency_amounts = array();
1547 
1548  // Loop on each invoice to pay
1549  foreach ($arrayofamounts as $id => $amountarray) {
1550  $result = $this->invoice->fetch($id);
1551  if (!$result) {
1552  $this->db->rollback();
1553  throw new RestException(404, 'Invoice ID '.$id.' not found');
1554  }
1555 
1556  if (($amountarray["amount"] == "remain" || $amountarray["amount"] > 0) && ($amountarray["multicurrency_amount"] == "remain" || $amountarray["multicurrency_amount"] > 0)) {
1557  $this->db->rollback();
1558  throw new RestException(400, 'Payment in both currency '.$id.' ( amount: '.$amountarray["amount"].', multicurrency_amount: '.$amountarray["multicurrency_amount"].')');
1559  }
1560 
1561  $is_multicurrency = 0;
1562  $total_ttc = $this->invoice->total_ttc;
1563 
1564  if ($amountarray["multicurrency_amount"] > 0 || $amountarray["multicurrency_amount"] == "remain") {
1565  $is_multicurrency = 1;
1566  $total_ttc = $this->invoice->multicurrency_total_ttc;
1567  }
1568 
1569  // Calculate amount to pay
1570  $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1571  $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1572  $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1573  $remainstopay = $amount = price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
1574 
1575  if (!$is_multicurrency && $amountarray["amount"] != 'remain') {
1576  $amount = price2num($amountarray["amount"], 'MT');
1577  }
1578 
1579  if ($is_multicurrency && $amountarray["multicurrency_amount"] != 'remain') {
1580  $amount = price2num($amountarray["multicurrency_amount"], 'MT');
1581  }
1582 
1583  if ($amount > $remainstopay && !$accepthigherpayment) {
1584  $this->db->rollback();
1585  throw new RestException(400, 'Payment amount on invoice ID '.$id.' ('.$amount.') is higher than remain to pay ('.$remainstopay.')');
1586  }
1587 
1588  if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1589  $amount = -$amount;
1590  }
1591 
1592  if ($is_multicurrency) {
1593  $amounts[$id] = null;
1594  // Multicurrency
1595  $multicurrency_amounts[$id] = $amount;
1596  } else {
1597  $amounts[$id] = $amount;
1598  // Multicurrency
1599  $multicurrency_amounts[$id] = null;
1600  }
1601  }
1602 
1603  // Creation of payment line
1604  $paymentobj = new Paiement($this->db);
1605  $paymentobj->datepaye = $datepaye;
1606  $paymentobj->amounts = $amounts; // Array with all payments dispatching with invoice id
1607  $paymentobj->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching
1608  $paymentobj->paiementid = $paymentid;
1609  $paymentobj->paiementcode = dol_getIdFromCode($this->db, $paymentid, 'c_paiement', 'id', 'code', 1);
1610  $paymentobj->num_payment = $num_payment;
1611  $paymentobj->note_private = $comment;
1612  $paymentobj->ref_ext = $ref_ext;
1613  $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices == 'yes' ? 1 : 0)); // This include closing invoices
1614  if ($payment_id < 0) {
1615  $this->db->rollback();
1616  throw new RestException(400, 'Payment error : '.$paymentobj->error);
1617  }
1618  if (isModEnabled("banque")) {
1619  $label = '(CustomerInvoicePayment)';
1620  if ($paymentobj->paiementcode == 'CHQ' && empty($chqemetteur)) {
1621  throw new RestException(400, 'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1622  }
1623  if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
1624  $label = '(CustomerInvoicePaymentBack)'; // Refund of a credit note
1625  }
1626  $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user, 'payment', $label, $accountid, $chqemetteur, $chqbank);
1627  if ($result < 0) {
1628  $this->db->rollback();
1629  throw new RestException(400, 'Add payment to bank error : '.$paymentobj->error);
1630  }
1631  }
1632 
1633  $this->db->commit();
1634 
1635  return $payment_id;
1636  }
1637 
1651  public function putPayment($id, $num_payment = '')
1652  {
1653  require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
1654 
1655  if (!DolibarrApiAccess::$user->rights->facture->creer) {
1656  throw new RestException(401);
1657  }
1658  if (empty($id)) {
1659  throw new RestException(400, 'Payment ID is mandatory');
1660  }
1661 
1662  $paymentobj = new Paiement($this->db);
1663  $result = $paymentobj->fetch($id);
1664 
1665  if (!$result) {
1666  throw new RestException(404, 'Payment not found');
1667  }
1668 
1669  if (!empty($num_payment)) {
1670  $result = $paymentobj->update_num($num_payment);
1671  if ($result < 0) {
1672  throw new RestException(500, 'Error when updating the payment num');
1673  }
1674  }
1675 
1676  return [
1677  'success' => [
1678  'code' => 200,
1679  'message' => 'Payment updated'
1680  ]
1681  ];
1682  }
1683 
1684  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1691  protected function _cleanObjectDatas($object)
1692  {
1693  // phpcs:enable
1694  $object = parent::_cleanObjectDatas($object);
1695 
1696  unset($object->note);
1697  unset($object->address);
1698  unset($object->barcode_type);
1699  unset($object->barcode_type_code);
1700  unset($object->barcode_type_label);
1701  unset($object->barcode_type_coder);
1702  unset($object->canvas);
1703 
1704  return $object;
1705  }
1706 
1715  private function _validate($data)
1716  {
1717  $invoice = array();
1718  foreach (Invoices::$FIELDS as $field) {
1719  if (!isset($data[$field])) {
1720  throw new RestException(400, "$field field missing");
1721  }
1722  $invoice[$field] = $data[$field];
1723  }
1724  return $invoice;
1725  }
1726 
1727 
1741  public function getTemplateInvoice($id, $contact_list = 1)
1742  {
1743  return $this->_fetchTemplateInvoice($id, '', '', $contact_list);
1744  }
1745 
1759  private function _fetchTemplateInvoice($id, $ref = '', $ref_ext = '', $contact_list = 1)
1760  {
1761  if (!DolibarrApiAccess::$user->rights->facture->lire) {
1762  throw new RestException(401);
1763  }
1764 
1765  $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1766  if (!$result) {
1767  throw new RestException(404, 'Template invoice not found');
1768  }
1769 
1770  if (!DolibarrApi::_checkAccessToResource('facturerec', $this->template_invoice->id)) {
1771  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1772  }
1773 
1774  // Add external contacts ids
1775  if ($contact_list > -1) {
1776  $tmparray = $this->template_invoice->liste_contact(-1, 'external', $contact_list);
1777  if (is_array($tmparray)) {
1778  $this->template_invoice->contacts_ids = $tmparray;
1779  }
1780  }
1781 
1782  $this->template_invoice->fetchObjectLinked();
1783  return $this->_cleanTemplateObjectDatas($this->template_invoice);
1784  }
1785 
1786 
1787  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1794  protected function _cleanTemplateObjectDatas($object)
1795  {
1796  // phpcs:enable
1797  $object = parent::_cleanObjectDatas($object);
1798 
1799  unset($object->note);
1800  unset($object->address);
1801  unset($object->barcode_type);
1802  unset($object->barcode_type_code);
1803  unset($object->barcode_type_label);
1804  unset($object->barcode_type_coder);
1805  unset($object->canvas);
1806 
1807  return $object;
1808  }
1809 }
Invoices\getTemplateInvoice
getTemplateInvoice($id, $contact_list=1)
Get properties of a template invoice object.
Definition: api_invoices.class.php:1741
Invoices\getDiscount
getDiscount($id)
Get discount from invoice.
Definition: api_invoices.class.php:1038
db
$conf db
API class for accounts.
Definition: inc.php:41
Invoices\settodraft
settodraft($id, $idwarehouse=-1)
Sets an invoice as draft.
Definition: api_invoices.class.php:842
Invoices\getLines
getLines($id)
Get lines of an invoice.
Definition: api_invoices.class.php:376
Invoices\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_invoices.class.php:1715
Invoices\putLine
putLine($id, $lineid, $request_data=null)
Update a line to a given invoice.
Definition: api_invoices.class.php:413
Invoices\put
put($id, $request_data=null)
Update invoice.
Definition: api_invoices.class.php:611
Invoices\markAsCreditAvailable
markAsCreditAvailable($id)
Create a discount (credit available) for a credit note or a deposit.
Definition: api_invoices.class.php:1081
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4993
Invoices\useDiscount
useDiscount($id, $discountid)
Add a discount line into an invoice (as an invoice line) using an existing absolute discount.
Definition: api_invoices.class.php:1253
Invoices\_fetchTemplateInvoice
_fetchTemplateInvoice($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an invoice object.
Definition: api_invoices.class.php:1759
FactureRec
Class to manage invoice templates.
Definition: facture-rec.class.php:40
Invoices\addPayment
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.
Definition: api_invoices.class.php:1395
Invoices\addPaymentDistributed
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.
Definition: api_invoices.class.php:1516
sanitizeVal
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
Definition: functions.lib.php:867
DolibarrApi\_checkAccessToResource
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:283
Facture
Class to manage invoices.
Definition: facture.class.php:60
Invoices
Definition: api_invoices.class.php:31
Invoices\createInvoiceFromOrder
createInvoiceFromOrder($orderid)
Create an invoice using an existing order.
Definition: api_invoices.class.php:338
Invoices\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_invoices.class.php:1691
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5823
Invoices\putPayment
putPayment($id, $num_payment='')
Update a payment.
Definition: api_invoices.class.php:1651
Facture\TYPE_DEPOSIT
const TYPE_DEPOSIT
Deposit invoice.
Definition: facture.class.php:392
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
Invoices\settounpaid
settounpaid($id)
Sets an invoice as unpaid.
Definition: api_invoices.class.php:994
Facture\TYPE_REPLACEMENT
const TYPE_REPLACEMENT
Replacement invoice.
Definition: facture.class.php:382
dol_getIdFromCode
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
Definition: functions.lib.php:8879
Invoices\postLine
postLine($id, $request_data=null)
Add a line to a given invoice.
Definition: api_invoices.class.php:705
Invoices\deleteLine
deleteLine($id, $lineid)
Deletes a line of a given invoice.
Definition: api_invoices.class.php:575
Invoices\useCreditNote
useCreditNote($id, $discountid)
Add an available credit note discount to payments of an existing invoice.
Definition: api_invoices.class.php:1300
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
Commande
Class to manage customers orders.
Definition: commande.class.php:46
Invoices\postContact
postContact($id, $contactid, $type)
Add a contact type of given invoice.
Definition: api_invoices.class.php:485
Invoices\getByRef
getByRef($ref, $contact_list=1)
Get properties of an invoice object by ref.
Definition: api_invoices.class.php:92
DiscountAbsolute
Class to manage absolute discounts.
Definition: discount.class.php:29
forgeSQLFromUniversalSearchCriteria
forgeSQLFromUniversalSearchCriteria($filter, &$error='')
forgeSQLFromUniversalSearchCriteria
Definition: functions.lib.php:11565
Invoices\getByRefExt
getByRefExt($ref_ext, $contact_list=1)
Get properties of an invoice object by ref_ext.
Definition: api_invoices.class.php:110
Invoices\_fetch
_fetch($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an invoice object.
Definition: api_invoices.class.php:128
Invoices\_cleanTemplateObjectDatas
_cleanTemplateObjectDatas($object)
Clean sensible object datas.
Definition: api_invoices.class.php:1794
Invoices\addContact
addContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0)
Adds a contact to an invoice.
Definition: api_invoices.class.php:793
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:137
Facture\TYPE_CREDIT_NOTE
const TYPE_CREDIT_NOTE
Credit note invoice.
Definition: facture.class.php:387
Paiement
Class to manage payments of customer invoices.
Definition: paiement.class.php:42
Facture\TYPE_STANDARD
const TYPE_STANDARD
Standard invoice.
Definition: facture.class.php:377
getMarginInfos
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.
Definition: margins.lib.php:118
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2951
Invoices\__construct
__construct()
Constructor.
Definition: api_invoices.class.php:55
Invoices\deleteContact
deleteContact($id, $contactid, $type)
Delete a contact type of given invoice.
Definition: api_invoices.class.php:529
Invoices\validate
validate($id, $idwarehouse=0, $notrigger=0)
Validate an invoice.
Definition: api_invoices.class.php:894
Facture\TYPE_SITUATION
const TYPE_SITUATION
Situation invoice.
Definition: facture.class.php:402
Invoices\post
post($request_data=null)
Create invoice object.
Definition: api_invoices.class.php:295
Invoices\getPayments
getPayments($id)
Get list of payments of a given invoice.
Definition: api_invoices.class.php:1346
Invoices\settopaid
settopaid($id, $close_code='', $close_note='')
Sets an invoice as paid.
Definition: api_invoices.class.php:944
Invoices\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='')
List invoices.
Definition: api_invoices.class.php:179