dolibarr  7.0.0-beta
api_invoices.class.php
1 <?php
2 /* Copyright (C) 2015 Jean-Fran├žois Ferry <jfefe@aternatik.fr>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 use Luracast\Restler\RestException;
19 
20 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
21 
28 class Invoices extends DolibarrApi
29 {
34  static $FIELDS = array(
35  'socid'
36  );
37 
41  public $invoice;
42 
46  function __construct()
47  {
48  global $db, $conf;
49  $this->db = $db;
50  $this->invoice = new Facture($this->db);
51  }
52 
63  function get($id)
64  {
65  if(! DolibarrApiAccess::$user->rights->facture->lire) {
66  throw new RestException(401);
67  }
68 
69  $result = $this->invoice->fetch($id);
70  if( ! $result ) {
71  throw new RestException(404, 'Invoice not found');
72  }
73 
74  // Get payment details
75  $this->invoice->totalpaye = $this->invoice->getSommePaiement();
76  $this->invoice->totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
77  $this->invoice->totaldeposits = $this->invoice->getSumDepositsUsed();
78  $this->invoice->resteapayer = price2num($this->invoice->total_ttc - $this->invoice->totalpaye - $this->invoice->totalcreditnotes - $this->invoice->totaldeposits, 'MT');
79 
80  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
81  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
82  }
83 
84  $this->invoice->fetchObjectLinked();
85  return $this->_cleanObjectDatas($this->invoice);
86  }
87 
104  function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids='', $status='', $sqlfilters = '') {
105  global $db, $conf;
106 
107  $obj_ret = array();
108 
109  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
110  $socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $thirdparty_ids;
111 
112  // If the internal user must only see his customers, force searching by him
113  $search_sale = 0;
114  if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
115 
116  $sql = "SELECT t.rowid";
117  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $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)
118  $sql.= " FROM ".MAIN_DB_PREFIX."facture as t";
119 
120  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
121 
122  $sql.= ' WHERE t.entity IN ('.getEntity('facture').')';
123  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc";
124  if ($socids) $sql.= " AND t.fk_soc IN (".$socids.")";
125 
126  if ($search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
127 
128  // Filter by status
129  if ($status == 'draft') $sql.= " AND t.fk_statut IN (0)";
130  if ($status == 'unpaid') $sql.= " AND t.fk_statut IN (1)";
131  if ($status == 'paid') $sql.= " AND t.fk_statut IN (2)";
132  if ($status == 'cancelled') $sql.= " AND t.fk_statut IN (3)";
133  // Insert sale filter
134  if ($search_sale > 0)
135  {
136  $sql .= " AND sc.fk_user = ".$search_sale;
137  }
138  // Add sql filters
139  if ($sqlfilters)
140  {
141  if (! DolibarrApi::_checkFilters($sqlfilters))
142  {
143  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
144  }
145  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
146  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
147  }
148 
149  $sql.= $db->order($sortfield, $sortorder);
150  if ($limit)
151  {
152  if ($page < 0)
153  {
154  $page = 0;
155  }
156  $offset = $limit * $page;
157 
158  $sql.= $db->plimit($limit + 1, $offset);
159  }
160 
161  $result = $db->query($sql);
162  if ($result)
163  {
164  $i=0;
165  $num = $db->num_rows($result);
166  $min = min($num, ($limit <= 0 ? $num : $limit));
167  while ($i < $min)
168  {
169  $obj = $db->fetch_object($result);
170  $invoice_static = new Facture($db);
171  if ($invoice_static->fetch($obj->rowid))
172  {
173  // Get payment details
174  $invoice_static->totalpaid = $invoice_static->getSommePaiement();
175  $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
176  $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
177  $invoice_static->remaintopay = price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits, 'MT');
178 
179  $obj_ret[] = $this->_cleanObjectDatas($invoice_static);
180  }
181  $i++;
182  }
183  }
184  else {
185  throw new RestException(503, 'Error when retrieve invoice list : '.$db->lasterror());
186  }
187  if( ! count($obj_ret)) {
188  throw new RestException(404, 'No invoice found');
189  }
190  return $obj_ret;
191  }
192 
199  function post($request_data = NULL)
200  {
201  if(! DolibarrApiAccess::$user->rights->facture->creer) {
202  throw new RestException(401, "Insuffisant rights");
203  }
204  // Check mandatory fields
205  $result = $this->_validate($request_data);
206 
207  foreach($request_data as $field => $value) {
208  $this->invoice->$field = $value;
209  }
210  if(! array_key_exists('date', $request_data)) {
211  $this->invoice->date = dol_now();
212  }
213  /* We keep lines as an array
214  if (isset($request_data["lines"])) {
215  $lines = array();
216  foreach ($request_data["lines"] as $line) {
217  array_push($lines, (object) $line);
218  }
219  $this->invoice->lines = $lines;
220  }*/
221 
222  if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
223  throw new RestException(500, "Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
224  }
225  return $this->invoice->id;
226  }
227 
242  function createInvoiceFromOrder($orderid) {
243 
244  require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
245 
246  if(! DolibarrApiAccess::$user->rights->commande->lire) {
247  throw new RestException(401);
248  }
249  if(! DolibarrApiAccess::$user->rights->facture->creer) {
250  throw new RestException(401);
251  }
252  if(empty($orderid)) {
253  throw new RestException(400, 'Order ID is mandatory');
254  }
255 
256  $order = new Commande($this->db);
257  $result = $order->fetch($orderid);
258  if( ! $result ) {
259  throw new RestException(404, 'Order not found');
260  }
261 
262  $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
263  if( $result < 0) {
264  throw new RestException(405, $this->invoice->error);
265  }
266  $this->invoice->fetchObjectLinked();
267  return $this->_cleanObjectDatas($this->invoice);
268  }
269 
279  function getLines($id) {
280  if(! DolibarrApiAccess::$user->rights->facture->lire) {
281  throw new RestException(401);
282  }
283 
284  $result = $this->invoice->fetch($id);
285  if( ! $result ) {
286  throw new RestException(404, 'Invoice not found');
287  }
288 
289  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
290  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
291  }
292  $this->invoice->getLinesArray();
293  $result = array();
294  foreach ($this->invoice->lines as $line) {
295  array_push($result,$this->_cleanObjectDatas($line));
296  }
297  return $result;
298  }
299 
316  function putLine($id, $lineid, $request_data = NULL) {
317  if(! DolibarrApiAccess::$user->rights->facture->creer) {
318  throw new RestException(401);
319  }
320 
321  $result = $this->invoice->fetch($id);
322  if( ! $result ) {
323  throw new RestException(404, 'Invoice not found');
324  }
325 
326  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
327  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
328  }
329  $request_data = (object) $request_data;
330  $updateRes = $this->invoice->updateline(
331  $lineid,
332  $request_data->desc,
333  $request_data->subprice,
334  $request_data->qty,
335  $request_data->remise_percent,
336  $request_data->date_start,
337  $request_data->date_end,
338  $request_data->tva_tx,
339  $request_data->localtax1_tx,
340  $request_data->localtax2_tx,
341  'HT',
342  $request_data->info_bits,
343  $request_data->product_type,
344  $request_data->fk_parent_line,
345  0,
346  $request_data->fk_fournprice,
347  $request_data->pa_ht,
348  $request_data->label,
349  $request_data->special_code,
350  $request_data->array_options,
351  $request_data->situation_percent,
352  $request_data->fk_unit,
353  $request_data->multicurrency_subprice
354  );
355 
356  if ($updateRes > 0) {
357  $result = $this->get($id);
358  unset($result->line);
359  return $this->_cleanObjectDatas($result);
360  } else {
361  throw new RestException(304, $this->invoice->error);
362  }
363  }
364 
380  function deleteLine($id, $lineid) {
381 
382  if(! DolibarrApiAccess::$user->rights->facture->creer) {
383  throw new RestException(401);
384  }
385  if(empty($lineid)) {
386  throw new RestException(400, 'Line ID is mandatory');
387  }
388 
389  if( ! DolibarrApi::_checkAccessToResource('facture',$id)) {
390  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
391  }
392 
393  $result = $this->invoice->fetch($id);
394  if( ! $result ) {
395  throw new RestException(404, 'Invoice not found');
396  }
397 
398  // TODO Check the lineid $lineid is a line of ojbect
399 
400  $updateRes = $this->invoice->deleteline($lineid);
401  if ($updateRes > 0) {
402  return $this->get($id);
403  }
404  else
405  {
406  throw new RestException(405, $this->invoice->error);
407  }
408  }
409 
417  function put($id, $request_data = NULL)
418  {
419  if(! DolibarrApiAccess::$user->rights->facture->creer) {
420  throw new RestException(401);
421  }
422 
423  $result = $this->invoice->fetch($id);
424  if( ! $result ) {
425  throw new RestException(404, 'Invoice not found');
426  }
427 
428  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
429  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
430  }
431 
432  foreach($request_data as $field => $value) {
433  if ($field == 'id') continue;
434  $this->invoice->$field = $value;
435  }
436 
437  // update bank account
438  if (!empty($this->invoice->fk_account))
439  {
440  if($this->invoice->setBankAccount($this->invoice->fk_account) == 0)
441  {
442  throw new RestException(400,$this->invoice->error);
443  }
444  }
445 
446  if($this->invoice->update($id, DolibarrApiAccess::$user))
447  return $this->get($id);
448 
449  return false;
450  }
451 
458  function delete($id)
459  {
460  if(! DolibarrApiAccess::$user->rights->facture->supprimer) {
461  throw new RestException(401);
462  }
463  $result = $this->invoice->fetch($id);
464  if( ! $result ) {
465  throw new RestException(404, 'Invoice not found');
466  }
467 
468  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
469  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
470  }
471 
472  if( $this->invoice->delete($id) < 0)
473  {
474  throw new RestException(500);
475  }
476 
477  return array(
478  'success' => array(
479  'code' => 200,
480  'message' => 'Invoice deleted'
481  )
482  );
483  }
484 
502  function postLine($id, $request_data = NULL) {
503  if(! DolibarrApiAccess::$user->rights->facture->creer) {
504  throw new RestException(401);
505  }
506 
507  $result = $this->invoice->fetch($id);
508  if( ! $result ) {
509  throw new RestException(404, 'Invoice not found');
510  }
511 
512  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
513  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
514  }
515 
516  $request_data = (object) $request_data;
517 
518  // Reset fk_parent_line for no child products and special product
519  if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
520  $request_data->fk_parent_line = 0;
521  }
522 
523  // calculate pa_ht
524  $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);
525  $pa_ht = $marginInfos[0];
526 
527  $updateRes = $this->invoice->addline(
528  $request_data->desc,
529  $request_data->subprice,
530  $request_data->qty,
531  $request_data->tva_tx,
532  $request_data->localtax1_tx,
533  $request_data->localtax2_tx,
534  $request_data->fk_product,
535  $request_data->remise_percent,
536  $request_data->date_start,
537  $request_data->date_end,
538  $request_data->fk_code_ventilation,
539  $request_data->info_bits,
540  $request_data->fk_remise_except,
541  'HT',
542  0,
543  $request_data->product_type,
544  $request_data->rang,
545  $request_data->special_code,
546  $request_data->origin,
547  $request_data->origin_id,
548  $request_data->fk_parent_line,
549  empty($request_data->fk_fournprice)?null:$request_data->fk_fournprice,
550  $pa_ht,
551  $request_data->label,
552  $request_data->array_options,
553  $request_data->situation_percent,
554  $request_data->fk_prev_id,
555  $request_data->fk_unit
556  );
557 
558  if ($updateRes < 0) {
559  throw new RestException(400, 'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
560  }
561 
562  return $updateRes;
563  }
564 
582  function settodraft($id, $idwarehouse=-1)
583  {
584  if(! DolibarrApiAccess::$user->rights->facture->creer) {
585  throw new RestException(401);
586  }
587  $result = $this->invoice->fetch($id);
588  if( ! $result ) {
589  throw new RestException(404, 'Invoice not found');
590  }
591 
592  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
593  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
594  }
595 
596  $result = $this->invoice->set_draft(DolibarrApiAccess::$user, $idwarehouse);
597  if ($result == 0) {
598  throw new RestException(304, 'Nothing done.');
599  }
600  if ($result < 0) {
601  throw new RestException(500, 'Error : '.$this->invoice->error);
602  }
603 
604  $result = $this->invoice->fetch($id);
605  if( ! $result ) {
606  throw new RestException(404, 'Invoice not found');
607  }
608 
609  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
610  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
611  }
612 
613  return $this->_cleanObjectDatas($this->invoice);
614  }
615 
616 
634  function validate($id, $idwarehouse=0, $notrigger=0)
635  {
636  if(! DolibarrApiAccess::$user->rights->facture->creer) {
637  throw new RestException(401);
638  }
639  $result = $this->invoice->fetch($id);
640  if( ! $result ) {
641  throw new RestException(404, 'Invoice not found');
642  }
643 
644  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
645  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
646  }
647 
648  $result = $this->invoice->validate(DolibarrApiAccess::$user, '', $idwarehouse, $notrigger);
649  if ($result == 0) {
650  throw new RestException(304, 'Error nothing done. May be object is already validated');
651  }
652  if ($result < 0) {
653  throw new RestException(500, 'Error when validating Invoice: '.$this->invoice->error);
654  }
655 
656  $result = $this->invoice->fetch($id);
657  if( ! $result ) {
658  throw new RestException(404, 'Invoice not found');
659  }
660 
661  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
662  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
663  }
664 
665  return $this->_cleanObjectDatas($this->invoice);
666  }
667 
685  function settopaid($id, $close_code='', $close_note='')
686  {
687  if(! DolibarrApiAccess::$user->rights->facture->creer) {
688  throw new RestException(401);
689  }
690  $result = $this->invoice->fetch($id);
691  if( ! $result ) {
692  throw new RestException(404, 'Invoice not found');
693  }
694 
695  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
696  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
697  }
698 
699  $result = $this->invoice->set_paid(DolibarrApiAccess::$user, $close_code, $close_note);
700  if ($result == 0) {
701  throw new RestException(304, 'Error nothing done. May be object is already validated');
702  }
703  if ($result < 0) {
704  throw new RestException(500, 'Error : '.$this->invoice->error);
705  }
706 
707 
708  $result = $this->invoice->fetch($id);
709  if( ! $result ) {
710  throw new RestException(404, 'Invoice not found');
711  }
712 
713  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
714  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
715  }
716 
717  return $this->_cleanObjectDatas($this->invoice);
718  }
719 
720 
736  function settounpaid($id)
737  {
738  if(! DolibarrApiAccess::$user->rights->facture->creer) {
739  throw new RestException(401);
740  }
741  $result = $this->invoice->fetch($id);
742  if( ! $result ) {
743  throw new RestException(404, 'Invoice not found');
744  }
745 
746  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
747  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
748  }
749 
750  $result = $this->invoice->set_unpaid(DolibarrApiAccess::$user);
751  if ($result == 0) {
752  throw new RestException(304, 'Nothing done');
753  }
754  if ($result < 0) {
755  throw new RestException(500, 'Error : '.$this->invoice->error);
756  }
757 
758 
759  $result = $this->invoice->fetch($id);
760  if( ! $result ) {
761  throw new RestException(404, 'Invoice not found');
762  }
763 
764  if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) {
765  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
766  }
767 
768  return $this->_cleanObjectDatas($this->invoice);
769  }
770 
787  function useDiscount($id, $discountid) {
788 
789  if(! DolibarrApiAccess::$user->rights->facture->creer) {
790  throw new RestException(401);
791  }
792  if(empty($id)) {
793  throw new RestException(400, 'Invoice ID is mandatory');
794  }
795  if(empty($discountid)) {
796  throw new RestException(400, 'Discount ID is mandatory');
797  }
798 
799  if( ! DolibarrApi::_checkAccessToResource('facture',$id)) {
800  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
801  }
802 
803  $result = $this->invoice->fetch($id);
804  if( ! $result ) {
805  throw new RestException(404, 'Invoice not found');
806  }
807 
808  $result = $this->invoice->insert_discount($discountid);
809  if( $result < 0) {
810  throw new RestException(405, $this->invoice->error);
811  }
812 
813  return $result;
814  }
815 
832  function useCreditNote($id, $discountid) {
833 
834  require_once DOL_DOCUMENT_ROOT . '/core/class/discount.class.php';
835 
836  if(! DolibarrApiAccess::$user->rights->facture->creer) {
837  throw new RestException(401);
838  }
839  if(empty($id)) {
840  throw new RestException(400, 'Invoice ID is mandatory');
841  }
842  if(empty($discountid)) {
843  throw new RestException(400, 'Credit ID is mandatory');
844  }
845 
846  if( ! DolibarrApi::_checkAccessToResource('facture',$id)) {
847  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
848  }
849  $discount = new DiscountAbsolute($this->db);
850  $result = $discount->fetch($discountid);
851  if( ! $result ) {
852  throw new RestException(404, 'Credit not found');
853  }
854 
855  $result = $discount->link_to_invoice(0, $id);
856  if( $result < 0) {
857  throw new RestException(405, $discount->error);
858  }
859 
860  return $result;
861  }
862 
876  function getPayments($id) {
877 
878  if(! DolibarrApiAccess::$user->rights->facture->lire) {
879  throw new RestException(401);
880  }
881  if(empty($id)) {
882  throw new RestException(400, 'Invoice ID is mandatory');
883  }
884 
885  if( ! DolibarrApi::_checkAccessToResource('facture',$id)) {
886  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
887  }
888 
889  $result = $this->invoice->fetch($id);
890  if( ! $result ) {
891  throw new RestException(404, 'Invoice not found');
892  }
893 
894  $result = $this->invoice->getListOfPayments();
895  if( $result < 0) {
896  throw new RestException(405, $this->invoice->error);
897  }
898 
899  return $result;
900  }
901 
924  function addPayment($id, $datepaye, $paiementid, $closepaidinvoices, $accountid, $num_paiement='', $comment='', $chqemetteur='', $chqbank='') {
925  global $conf;
926 
927  require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php';
928 
929  if(! DolibarrApiAccess::$user->rights->facture->creer) {
930  throw new RestException(401);
931  }
932  if(empty($id)) {
933  throw new RestException(400, 'Invoice ID is mandatory');
934  }
935 
936  if( ! DolibarrApi::_checkAccessToResource('facture',$id)) {
937  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
938  }
939 
940  $request_data = (object) $payment_data;
941 
942  if (! empty($conf->banque->enabled)) {
943  if(empty($accountid)) {
944  throw new RestException(400, 'Account ID is mandatory');
945  }
946  }
947 
948  if(empty($paiementid)) {
949  throw new RestException(400, 'Paiement ID or Paiement Code is mandatory');
950  }
951 
952 
953  $result = $this->invoice->fetch($id);
954  if( ! $result ) {
955  throw new RestException(404, 'Invoice not found');
956  }
957 
958  // Calculate amount to pay
959  $totalpaye = $this->invoice->getSommePaiement();
960  $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
961  $totaldeposits = $this->invoice->getSumDepositsUsed();
962  $resteapayer = price2num($this->invoice->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits, 'MT');
963 
964  $this->db->begin();
965  // Clean parameters amount if payment is for a credit note
966  if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) {
967  $resteapayer = price2num($resteapayer,'MT');
968  $amounts[$id] = -$resteapayer;
969  // Multicurrency
970  $newvalue = price2num($this->invoice->multicurrency_total_ttc,'MT');
971  $multicurrency_amounts[$id] = -$newvalue;
972  } else {
973  $resteapayer = price2num($resteapayer,'MT');
974  $amounts[$id] = $resteapayer;
975  // Multicurrency
976  $newvalue = price2num($this->invoice->multicurrency_total_ttc,'MT');
977  $multicurrency_amounts[$id] = $newvalue;
978  }
979 
980 
981  // Creation of payment line
982  $paiement = new Paiement($this->db);
983  $paiement->datepaye = $datepaye;
984  $paiement->amounts = $amounts; // Array with all payments dispatching with invoice id
985  $paiement->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching
986  $paiement->paiementid = $paiementid;
987  $paiement->paiementcode = dol_getIdFromCode($this->db,$paiementid,'c_paiement','id','code',1);
988  $paiement->num_paiement = $num_paiement;
989  $paiement->note = $comment;
990 
991  $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices=='yes'?1:0)); // This include closing invoices
992  if ($paiement_id < 0)
993  {
994  $this->db->rollback();
995  throw new RestException(400, 'Payment error : '.$paiement->error);
996  }
997 
998  if (! empty($conf->banque->enabled)) {
999  $label='(CustomerInvoicePayment)';
1000 
1001  if($paiement->paiementcode == 'CHQ' && empty($chqemetteur)) {
1002  throw new RestException(400, 'Emetteur is mandatory when payment code is '.$paiement->paiementcode);
1003  }
1004  if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) $label='(CustomerInvoicePaymentBack)'; // Refund of a credit note
1005  $result=$paiement->addPaymentToBank(DolibarrApiAccess::$user,'payment',$label,$accountid,$chqemetteur,$chqbank);
1006  if ($result < 0)
1007  {
1008  $this->db->rollback();
1009  throw new RestException(400, 'Add payment to bank error : '.$paiement->error);
1010  }
1011  }
1012  $this->db->commit();
1013  return $paiement_id;
1014  }
1015 
1022  function _cleanObjectDatas($object) {
1023 
1024  $object = parent::_cleanObjectDatas($object);
1025 
1026  unset($object->note);
1027  unset($object->address);
1028  unset($object->barcode_type);
1029  unset($object->barcode_type_code);
1030  unset($object->barcode_type_label);
1031  unset($object->barcode_type_coder);
1032 
1033  return $object;
1034  }
1035 
1044  function _validate($data)
1045  {
1046  $invoice = array();
1047  foreach (Invoices::$FIELDS as $field) {
1048  if (!isset($data[$field]))
1049  throw new RestException(400, "$field field missing");
1050  $invoice[$field] = $data[$field];
1051  }
1052  return $invoice;
1053  }
1054 
1055 }
settopaid($id, $close_code='', $close_note='')
Sets an invoice as paid.
postLine($id, $request_data=NULL)
Add a line to a given invoice.
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.
useCreditNote($id, $discountid)
Add an available credit note discount to payments of an existing invoice.
addPayment($id, $datepaye, $paiementid, $closepaidinvoices, $accountid, $num_paiement='', $comment='', $chqemetteur='', $chqbank='')
Add payment line to a specific invoice.
settodraft($id, $idwarehouse=-1)
Sets an invoice as draft.
_validate($data)
Validate fields before create or update object.
getPayments($id)
Get list of payments of a given invoice.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0)
Return an id or code from a code or id.
useDiscount($id, $discountid)
Add a discount line into an invoice (as an invoice line) using an existing absolute discount...
createInvoiceFromOrder($orderid)
Create an invoice using an existing order.
settounpaid($id)
Sets an invoice as unpaid.
putLine($id, $lineid, $request_data=NULL)
Update a line to a given invoice.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:246
validate($id, $idwarehouse=0, $notrigger=0)
Validate an invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
__construct()
Constructor.
Class for API REST v1.
Definition: api.class.php:29
Class to manage customers orders.
Class to manage payments of customer invoices.
dol_now($mode='gmt')
Return date for now.
put($id, $request_data=NULL)
Update invoice.
_cleanObjectDatas($object)
Clean sensible object datas.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check user access to a resource.
Definition: api.class.php:221
post($request_data=NULL)
Create invoice object.
Class to manage absolute discounts.
getLines($id)
Get lines of an invoice.
Class to manage invoices.
deleteLine($id, $lineid)
Deletes a line of a given invoice.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...