19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
22require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture-rec.class.php';
37 public static $FIELDS = array(
49 private $template_invoice;
59 $this->invoice =
new Facture($this->db);
60 $this->template_invoice =
new FactureRec($this->db);
74 public function get($id, $contact_list = 1)
76 return $this->
_fetch($id,
'',
'', $contact_list);
92 public function getByRef($ref, $contact_list = 1)
94 return $this->
_fetch(
'', $ref,
'', $contact_list);
112 return $this->
_fetch(
'',
'', $ref_ext, $contact_list);
128 private function _fetch($id, $ref =
'', $ref_ext =
'', $contact_list = 1)
130 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
131 throw new RestException(401);
134 $result = $this->invoice->fetch($id, $ref, $ref_ext);
136 throw new RestException(404,
'Invoice not found');
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');
146 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
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;
157 $this->invoice->fetchObjectLinked();
180 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'')
184 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
185 throw new RestException(401);
191 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
195 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
196 $search_sale = DolibarrApiAccess::$user->id;
199 $sql =
"SELECT t.rowid";
200 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
201 $sql .=
", sc.fk_soc, sc.fk_user";
203 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture AS t LEFT JOIN ".MAIN_DB_PREFIX.
"facture_extrafields AS ef ON (ef.fk_object = t.rowid)";
205 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
206 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
209 $sql .=
' WHERE t.entity IN ('.getEntity(
'invoice').
')';
210 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
211 $sql .=
" AND t.fk_soc = sc.fk_soc";
214 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
217 if ($search_sale > 0) {
218 $sql .=
" AND t.rowid = sc.fk_soc";
222 if ($status ==
'draft') {
223 $sql .=
" AND t.fk_statut IN (0)";
225 if ($status ==
'unpaid') {
226 $sql .=
" AND t.fk_statut IN (1)";
228 if ($status ==
'paid') {
229 $sql .=
" AND t.fk_statut IN (2)";
231 if ($status ==
'cancelled') {
232 $sql .=
" AND t.fk_statut IN (3)";
235 if ($search_sale > 0) {
236 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
243 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
247 $sql .= $this->db->order($sortfield, $sortorder);
252 $offset = $limit * $page;
254 $sql .= $this->db->plimit($limit + 1, $offset);
257 $result = $this->db->query($sql);
260 $num = $this->db->num_rows($result);
261 $min = min($num, ($limit <= 0 ? $num : $limit));
263 $obj = $this->db->fetch_object($result);
264 $invoice_static =
new Facture($this->db);
265 if ($invoice_static->fetch($obj->rowid)) {
267 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
268 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
269 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
270 $invoice_static->remaintopay =
price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits,
'MT');
273 $tmparray = $invoice_static->liste_contact(-1,
'external', 1);
274 if (is_array($tmparray)) {
275 $invoice_static->contacts_ids = $tmparray;
282 throw new RestException(503,
'Error when retrieve invoice list : '.$this->db->lasterror());
294 public function post($request_data =
null)
296 if (!DolibarrApiAccess::$user->rights->facture->creer) {
297 throw new RestException(401,
"Insuffisant rights");
300 $result = $this->
_validate($request_data);
302 foreach ($request_data as $field => $value) {
303 if ($field ===
'caller') {
305 $this->invoice->context[
'caller'] = $request_data[
'caller'];
309 $this->invoice->$field = $value;
311 if (!array_key_exists(
'date', $request_data)) {
312 $this->invoice->date =
dol_now();
323 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data[
"date_lim_reglement"]) ? 0 : $request_data[
"date_lim_reglement"])) < 0) {
324 throw new RestException(500,
"Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
326 return ((
int) $this->invoice->id);
344 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
346 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
347 throw new RestException(401);
349 if (!DolibarrApiAccess::$user->rights->facture->creer) {
350 throw new RestException(401);
352 if (empty($orderid)) {
353 throw new RestException(400,
'Order ID is mandatory');
357 $result = $order->fetch($orderid);
359 throw new RestException(404,
'Order not found');
362 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
364 throw new RestException(405, $this->invoice->error);
366 $this->invoice->fetchObjectLinked();
380 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
381 throw new RestException(401);
384 $result = $this->invoice->fetch($id);
386 throw new RestException(404,
'Invoice not found');
390 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
392 $this->invoice->getLinesArray();
394 foreach ($this->invoice->lines as $line) {
414 public function putLine($id, $lineid, $request_data =
null)
416 if (!DolibarrApiAccess::$user->rights->facture->creer) {
417 throw new RestException(401);
420 $result = $this->invoice->fetch($id);
422 throw new RestException(404,
'Invoice not found');
426 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
429 $request_data = (object) $request_data;
431 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
432 $request_data->label =
sanitizeVal($request_data->label);
434 $updateRes = $this->invoice->updateline(
437 $request_data->subprice,
439 $request_data->remise_percent,
440 $request_data->date_start,
441 $request_data->date_end,
442 $request_data->tva_tx,
443 $request_data->localtax1_tx,
444 $request_data->localtax2_tx,
445 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
446 $request_data->info_bits,
447 $request_data->product_type,
448 $request_data->fk_parent_line,
450 $request_data->fk_fournprice,
451 $request_data->pa_ht,
452 $request_data->label,
453 $request_data->special_code,
454 $request_data->array_options,
455 $request_data->situation_percent,
456 $request_data->fk_unit,
457 $request_data->multicurrency_subprice,
459 $request_data->ref_ext,
463 if ($updateRes > 0) {
464 $result = $this->
get($id);
465 unset($result->line);
468 throw new RestException(304, $this->invoice->error);
487 if (!DolibarrApiAccess::$user->rights->facture->creer) {
488 throw new RestException(401);
491 $result = $this->invoice->fetch($id);
494 throw new RestException(404,
'Invoice not found');
497 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
498 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
502 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
505 $result = $this->invoice->add_contact($contactid, $type,
'external');
508 throw new RestException(500,
'Error when added the contact');
514 'message' =>
'Contact linked to the invoice'
535 if (!DolibarrApiAccess::$user->rights->facture->creer) {
536 throw new RestException(401);
539 $result = $this->invoice->fetch($id);
542 throw new RestException(404,
'Invoice not found');
546 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
549 $contacts = $this->invoice->liste_contact();
551 foreach ($contacts as $contact) {
552 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
553 $result = $this->invoice->delete_contact($contact[
'rowid']);
556 throw new RestException(500,
'Error when deleted the contact');
580 if (!DolibarrApiAccess::$user->rights->facture->creer) {
581 throw new RestException(401);
583 if (empty($lineid)) {
584 throw new RestException(400,
'Line ID is mandatory');
588 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
591 $result = $this->invoice->fetch($id);
593 throw new RestException(404,
'Invoice not found');
596 $updateRes = $this->invoice->deleteline($lineid, $id);
597 if ($updateRes > 0) {
598 return $this->
get($id);
600 throw new RestException(405, $this->invoice->error);
611 public function put($id, $request_data =
null)
613 if (!DolibarrApiAccess::$user->rights->facture->creer) {
614 throw new RestException(401);
617 $result = $this->invoice->fetch($id);
619 throw new RestException(404,
'Invoice not found');
623 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
626 foreach ($request_data as $field => $value) {
627 if ($field ==
'id') {
630 if ($field ===
'caller') {
632 $this->invoice->context[
'caller'] = $request_data[
'caller'];
636 $this->invoice->$field = $value;
640 if (!empty($this->invoice->fk_account)) {
641 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
642 throw new RestException(400, $this->invoice->error);
646 if ($this->invoice->update(DolibarrApiAccess::$user)) {
647 return $this->
get($id);
659 public function delete($id)
661 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'supprimer')) {
662 throw new RestException(401);
664 $result = $this->invoice->fetch($id);
666 throw new RestException(404,
'Invoice not found');
670 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
673 $result = $this->invoice->delete(DolibarrApiAccess::$user);
675 throw new RestException(500,
'Error when deleting invoice');
676 } elseif ($result == 0) {
677 throw new RestException(403,
'Invoice not erasable');
683 'message' =>
'Invoice deleted'
711 public function postLine($id, $request_data =
null)
713 if (!DolibarrApiAccess::$user->rights->facture->creer) {
714 throw new RestException(401);
717 $result = $this->invoice->fetch($id);
719 throw new RestException(404,
'Invoice not found');
723 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
726 $request_data = (object) $request_data;
728 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
729 $request_data->label =
sanitizeVal($request_data->label);
732 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
733 $request_data->fk_parent_line = 0;
737 $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);
738 $pa_ht = $marginInfos[0];
740 $updateRes = $this->invoice->addline(
742 $request_data->subprice,
744 $request_data->tva_tx,
745 $request_data->localtax1_tx,
746 $request_data->localtax2_tx,
747 $request_data->fk_product,
748 $request_data->remise_percent,
749 $request_data->date_start,
750 $request_data->date_end,
751 $request_data->fk_code_ventilation,
752 $request_data->info_bits,
753 $request_data->fk_remise_except,
754 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
755 $request_data->subprice,
756 $request_data->product_type,
758 $request_data->special_code,
759 $request_data->origin,
760 $request_data->origin_id,
761 $request_data->fk_parent_line,
762 empty($request_data->fk_fournprice) ? null : $request_data->fk_fournprice,
764 $request_data->label,
765 $request_data->array_options,
766 $request_data->situation_percent,
767 $request_data->fk_prev_id,
768 $request_data->fk_unit,
770 $request_data->ref_ext
773 if ($updateRes < 0) {
774 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
799 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
801 if (!DolibarrApiAccess::$user->rights->facture->creer) {
802 throw new RestException(401);
804 $result = $this->invoice->fetch($id);
806 throw new RestException(404,
'Invoice not found');
810 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
813 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
815 throw new RestException(500,
'Error : '.$this->invoice->error);
818 $result = $this->invoice->fetch($id);
820 throw new RestException(404,
'Invoice not found');
824 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
849 if (!DolibarrApiAccess::$user->rights->facture->creer) {
850 throw new RestException(401);
852 $result = $this->invoice->fetch($id);
854 throw new RestException(404,
'Invoice not found');
858 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
861 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
863 throw new RestException(304,
'Nothing done.');
866 throw new RestException(500,
'Error : '.$this->invoice->error);
869 $result = $this->invoice->fetch($id);
871 throw new RestException(404,
'Invoice not found');
875 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
898 public function validate($id, $idwarehouse = 0, $notrigger = 0)
900 if (!DolibarrApiAccess::$user->rights->facture->creer) {
901 throw new RestException(401);
903 $result = $this->invoice->fetch($id);
905 throw new RestException(404,
'Invoice not found');
909 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
912 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
914 throw new RestException(304,
'Error nothing done. May be object is already validated');
917 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
920 $result = $this->invoice->fetch($id);
922 throw new RestException(404,
'Invoice not found');
926 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
947 public function settopaid($id, $close_code =
'', $close_note =
'')
949 if (!DolibarrApiAccess::$user->rights->facture->creer) {
950 throw new RestException(401);
952 $result = $this->invoice->fetch($id);
954 throw new RestException(404,
'Invoice not found');
958 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
961 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
963 throw new RestException(304,
'Error nothing done. May be object is already validated');
966 throw new RestException(500,
'Error : '.$this->invoice->error);
970 $result = $this->invoice->fetch($id);
972 throw new RestException(404,
'Invoice not found');
976 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
998 if (!DolibarrApiAccess::$user->rights->facture->creer) {
999 throw new RestException(401);
1001 $result = $this->invoice->fetch($id);
1003 throw new RestException(404,
'Invoice not found');
1007 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1010 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1012 throw new RestException(304,
'Nothing done');
1015 throw new RestException(500,
'Error : '.$this->invoice->error);
1019 $result = $this->invoice->fetch($id);
1021 throw new RestException(404,
'Invoice not found');
1025 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1041 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1043 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1044 throw new RestException(401);
1047 $result = $this->invoice->fetch($id);
1049 throw new RestException(404,
'Invoice not found');
1053 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1057 $result = $discountcheck->fetch(0, $this->invoice->id);
1060 throw new RestException(404,
'Discount not found');
1063 throw new RestException(500, $discountcheck->error);
1066 return parent::_cleanObjectDatas($discountcheck);
1084 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1086 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1087 throw new RestException(401);
1090 $result = $this->invoice->fetch($id);
1092 throw new RestException(404,
'Invoice not found');
1096 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1099 if ($this->invoice->paye) {
1100 throw new RestException(500,
'Alreay paid');
1103 $this->invoice->fetch($id);
1104 $this->invoice->fetch_thirdparty();
1108 $result = $discountcheck->fetch(0, $this->invoice->id);
1120 $amount_ht = $amount_tva = $amount_ttc = array();
1121 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1125 foreach ($this->invoice->lines as $line) {
1126 if ($line->product_type < 9 && $line->total_ht != 0) {
1128 $amount_ht[$line->tva_tx] += $line->total_ht;
1129 $amount_tva[$line->tva_tx] += $line->total_tva;
1130 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1131 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1132 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1133 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1141 $discount->description =
'(CREDIT_NOTE)';
1143 $discount->description =
'(DEPOSIT)';
1145 $discount->description =
'(EXCESS RECEIVED)';
1147 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1150 $discount->fk_soc = $this->invoice->socid;
1151 $discount->fk_facture_source = $this->invoice->id;
1159 $sql =
'SELECT SUM(pf.amount) as total_payments';
1160 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1161 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1162 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1163 $sql .=
' AND pf.fk_paiement = p.rowid';
1164 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1165 $resql = $this->db->query($sql);
1170 $res = $this->db->fetch_object($resql);
1171 $total_payments = $res->total_payments;
1174 $total_creditnote_and_deposit = 0;
1175 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1176 $sql .=
" re.description, re.fk_facture_source";
1177 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1178 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1179 $resql = $this->db->query($sql);
1180 if (!empty($resql)) {
1181 while ($obj = $this->db->fetch_object($resql)) {
1182 $total_creditnote_and_deposit += $obj->amount_ttc;
1188 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1189 $discount->amount_tva = 0;
1190 $discount->tva_tx = 0;
1192 $result = $discount->create(DolibarrApiAccess::$user);
1198 foreach ($amount_ht as $tva_tx => $xxx) {
1199 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1200 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1201 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1202 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1203 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1204 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1205 $discount->tva_tx = abs($tva_tx);
1207 $result = $discount->create(DolibarrApiAccess::$user);
1215 if (empty($error)) {
1218 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1220 $this->db->commit();
1222 $this->db->rollback();
1223 throw new RestException(500,
'Could not set paid');
1226 $this->db->commit();
1229 $this->db->rollback();
1230 throw new RestException(500,
'Discount creation error');
1255 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1256 throw new RestException(401);
1259 throw new RestException(400,
'Invoice ID is mandatory');
1261 if (empty($discountid)) {
1262 throw new RestException(400,
'Discount ID is mandatory');
1266 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1269 $result = $this->invoice->fetch($id);
1271 throw new RestException(404,
'Invoice not found');
1274 $result = $this->invoice->insert_discount($discountid);
1276 throw new RestException(405, $this->invoice->error);
1300 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1302 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1303 throw new RestException(401);
1306 throw new RestException(400,
'Invoice ID is mandatory');
1308 if (empty($discountid)) {
1309 throw new RestException(400,
'Credit ID is mandatory');
1313 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1316 $result = $discount->fetch($discountid);
1318 throw new RestException(404,
'Credit not found');
1321 $result = $discount->link_to_invoice(0, $id);
1323 throw new RestException(405, $discount->error);
1344 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1345 throw new RestException(401);
1348 throw new RestException(400,
'Invoice ID is mandatory');
1352 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1355 $result = $this->invoice->fetch($id);
1357 throw new RestException(404,
'Invoice not found');
1360 $result = $this->invoice->getListOfPayments();
1362 throw new RestException(405, $this->invoice->error);
1390 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1392 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1394 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1395 throw new RestException(403);
1398 throw new RestException(400,
'Invoice ID is mandatory');
1402 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1405 if (isModEnabled(
"banque")) {
1406 if (empty($accountid)) {
1407 throw new RestException(400,
'Account ID is mandatory');
1411 if (empty($paymentid)) {
1412 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1416 $result = $this->invoice->fetch($id);
1418 throw new RestException(404,
'Invoice not found');
1422 $totalpaid = $this->invoice->getSommePaiement();
1423 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1424 $totaldeposits = $this->invoice->getSumDepositsUsed();
1425 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1430 $multicurrency_amounts = array();
1434 $resteapayer =
price2num($resteapayer,
'MT');
1435 $amounts[$id] =
price2num(-1 * $resteapayer,
'MT');
1437 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1438 $multicurrency_amounts[$id] =
price2num(-1 * $newvalue,
'MT');
1440 $resteapayer =
price2num($resteapayer,
'MT');
1441 $amounts[$id] = $resteapayer;
1443 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1444 $multicurrency_amounts[$id] = $newvalue;
1448 $paymentobj =
new Paiement($this->db);
1449 $paymentobj->datepaye = $datepaye;
1450 $paymentobj->amounts = $amounts;
1451 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1452 $paymentobj->paiementid = $paymentid;
1453 $paymentobj->paiementcode =
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1454 $paymentobj->num_payment = $num_payment;
1455 $paymentobj->note_private = $comment;
1457 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1458 if ($payment_id < 0) {
1459 $this->db->rollback();
1460 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1463 if (isModEnabled(
"banque")) {
1464 $label =
'(CustomerInvoicePayment)';
1466 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1467 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1470 $label =
'(CustomerInvoicePaymentBack)';
1472 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1474 $this->db->rollback();
1475 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1479 $this->db->commit();
1510 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1512 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1514 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1515 throw new RestException(403);
1517 foreach ($arrayofamounts as $id => $amount) {
1519 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1522 throw new RestException(403,
'Access not allowed on invoice ID '.$id.
' for login '.DolibarrApiAccess::$user->login);
1526 if (isModEnabled(
"banque")) {
1527 if (empty($accountid)) {
1528 throw new RestException(400,
'Account ID is mandatory');
1531 if (empty($paymentid)) {
1532 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1538 $multicurrency_amounts = array();
1541 foreach ($arrayofamounts as $id => $amountarray) {
1542 $result = $this->invoice->fetch($id);
1544 $this->db->rollback();
1545 throw new RestException(404,
'Invoice ID '.$id.
' not found');
1548 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1549 $this->db->rollback();
1550 throw new RestException(400,
'Payment in both currency '.$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1553 $is_multicurrency = 0;
1554 $total_ttc = $this->invoice->total_ttc;
1556 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1557 $is_multicurrency = 1;
1558 $total_ttc = $this->invoice->multicurrency_total_ttc;
1562 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1563 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1564 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1565 $remainstopay = $amount =
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1567 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1568 $amount =
price2num($amountarray[
"amount"],
'MT');
1571 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1572 $amount =
price2num($amountarray[
"multicurrency_amount"],
'MT');
1575 if ($amount > $remainstopay && !$accepthigherpayment) {
1576 $this->db->rollback();
1577 throw new RestException(400,
'Payment amount on invoice ID '.$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1581 $amount =
price2num(-1 * $amount,
'MT');
1584 if ($is_multicurrency) {
1585 $amounts[$id] =
null;
1587 $multicurrency_amounts[$id] = $amount;
1589 $amounts[$id] = $amount;
1591 $multicurrency_amounts[$id] =
null;
1596 $paymentobj =
new Paiement($this->db);
1597 $paymentobj->datepaye = $datepaye;
1598 $paymentobj->amounts = $amounts;
1599 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1600 $paymentobj->paiementid = $paymentid;
1601 $paymentobj->paiementcode =
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1602 $paymentobj->num_payment = $num_payment;
1603 $paymentobj->note_private = $comment;
1604 $paymentobj->ref_ext = $ref_ext;
1605 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1606 if ($payment_id < 0) {
1607 $this->db->rollback();
1608 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1610 if (isModEnabled(
"banque")) {
1611 $label =
'(CustomerInvoicePayment)';
1612 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1613 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1616 $label =
'(CustomerInvoicePaymentBack)';
1618 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1620 $this->db->rollback();
1621 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1625 $this->db->commit();
1646 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1648 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1649 throw new RestException(401);
1652 throw new RestException(400,
'Payment ID is mandatory');
1655 $paymentobj =
new Paiement($this->db);
1656 $result = $paymentobj->fetch($id);
1659 throw new RestException(404,
'Payment not found');
1662 if (!empty($num_payment)) {
1663 $result = $paymentobj->update_num($num_payment);
1665 throw new RestException(500,
'Error when updating the payment num');
1672 'message' =>
'Payment updated'
1687 $object = parent::_cleanObjectDatas($object);
1689 unset($object->note);
1690 unset($object->address);
1691 unset($object->barcode_type);
1692 unset($object->barcode_type_code);
1693 unset($object->barcode_type_label);
1694 unset($object->barcode_type_coder);
1695 unset($object->canvas);
1711 foreach (Invoices::$FIELDS as $field) {
1712 if (!isset($data[$field])) {
1713 throw new RestException(400,
"$field field missing");
1715 $invoice[$field] = $data[$field];
1754 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1755 throw new RestException(401);
1758 $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1760 throw new RestException(404,
'Template invoice not found');
1764 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1768 if ($contact_list > -1) {
1769 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1770 if (is_array($tmparray)) {
1771 $this->template_invoice->contacts_ids = $tmparray;
1775 $this->template_invoice->fetchObjectLinked();
1790 $object = parent::_cleanObjectDatas($object);
1792 unset($object->note);
1793 unset($object->address);
1794 unset($object->barcode_type);
1795 unset($object->barcode_type_code);
1796 unset($object->barcode_type_label);
1797 unset($object->barcode_type_coder);
1798 unset($object->canvas);
Class to manage customers orders.
Class to manage absolute discounts.
_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.
Class to manage invoices.
const TYPE_REPLACEMENT
Replacement invoice.
const TYPE_STANDARD
Standard invoice.
const TYPE_SITUATION
Situation invoice.
const TYPE_DEPOSIT
Deposit invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
Class to manage invoice templates.
postContact($id, $contactid, $type)
Add a contact type of given invoice.
putPayment($id, $num_payment='')
Update a payment.
addContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0)
Adds a contact to an invoice.
createInvoiceFromOrder($orderid)
Create an invoice using an existing order.
markAsCreditAvailable($id)
Create a discount (credit available) for a credit note or a deposit.
getDiscount($id)
Get discount from invoice.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='')
List invoices.
__construct()
Constructor.
put($id, $request_data=null)
Update invoice.
getByRefExt($ref_ext, $contact_list=1)
Get properties of an invoice object by ref_ext.
_fetch($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an invoice object.
getByRef($ref, $contact_list=1)
Get properties of an invoice object by ref.
getPayments($id)
Get list of payments of a given invoice.
_cleanObjectDatas($object)
Clean sensible object datas.
post($request_data=null)
Create invoice object.
useDiscount($id, $discountid)
Add a discount line into an invoice (as an invoice line) using an existing absolute discount.
settounpaid($id)
Sets an invoice as unpaid.
getTemplateInvoice($id, $contact_list=1)
Get properties of a template invoice object.
putLine($id, $lineid, $request_data=null)
Update a line to a given invoice.
getLines($id)
Get lines of an invoice.
useCreditNote($id, $discountid)
Add an available credit note discount to payments of an existing invoice.
addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment='', $comment='', $chqemetteur='', $chqbank='', $ref_ext='', $accepthigherpayment=false)
Add a payment to pay partially or completely one or several invoices.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an invoice.
_cleanTemplateObjectDatas($object)
Clean sensible object datas.
deleteContact($id, $contactid, $type)
Delete a contact type of given invoice.
postLine($id, $request_data=null)
Add a line to a given invoice.
_fetchTemplateInvoice($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an invoice object.
_validate($data)
Validate fields before create or update object.
addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment='', $comment='', $chqemetteur='', $chqbank='')
Add payment line to a specific invoice with the remain to pay as amount.
settopaid($id, $close_code='', $close_note='')
Sets an invoice as paid.
settodraft($id, $idwarehouse=-1)
Sets an invoice as draft.
deleteLine($id, $lineid)
Deletes a line of a given invoice.
Class to manage payments of customer invoices.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.