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 if ($field ==
'array_options' && is_array($value)) {
637 foreach ($value as $index => $val) {
638 $this->invoice->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->invoice);
642 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
646 if (!empty($this->invoice->fk_account)) {
647 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
648 throw new RestException(400, $this->invoice->error);
652 if ($this->invoice->update(DolibarrApiAccess::$user) > 0) {
653 return $this->
get($id);
655 throw new RestException(500, $this->invoice->error);
665 public function delete($id)
667 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'supprimer')) {
668 throw new RestException(401);
670 $result = $this->invoice->fetch($id);
672 throw new RestException(404,
'Invoice not found');
676 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
679 $result = $this->invoice->delete(DolibarrApiAccess::$user);
681 throw new RestException(500,
'Error when deleting invoice');
682 } elseif ($result == 0) {
683 throw new RestException(403,
'Invoice not erasable');
689 'message' =>
'Invoice deleted'
717 public function postLine($id, $request_data =
null)
719 if (!DolibarrApiAccess::$user->rights->facture->creer) {
720 throw new RestException(401);
723 $result = $this->invoice->fetch($id);
725 throw new RestException(404,
'Invoice not found');
729 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
732 $request_data = (object) $request_data;
734 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
735 $request_data->label =
sanitizeVal($request_data->label);
738 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
739 $request_data->fk_parent_line = 0;
743 $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);
744 $pa_ht = $marginInfos[0];
746 $updateRes = $this->invoice->addline(
748 $request_data->subprice,
750 $request_data->tva_tx,
751 $request_data->localtax1_tx,
752 $request_data->localtax2_tx,
753 $request_data->fk_product,
754 $request_data->remise_percent,
755 $request_data->date_start,
756 $request_data->date_end,
757 $request_data->fk_code_ventilation,
758 $request_data->info_bits,
759 $request_data->fk_remise_except,
760 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
761 $request_data->subprice,
762 $request_data->product_type,
764 $request_data->special_code,
765 $request_data->origin,
766 $request_data->origin_id,
767 $request_data->fk_parent_line,
768 empty($request_data->fk_fournprice) ? null : $request_data->fk_fournprice,
770 $request_data->label,
771 $request_data->array_options,
772 $request_data->situation_percent,
773 $request_data->fk_prev_id,
774 $request_data->fk_unit,
776 $request_data->ref_ext
779 if ($updateRes < 0) {
780 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
805 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
807 if (!DolibarrApiAccess::$user->rights->facture->creer) {
808 throw new RestException(401);
810 $result = $this->invoice->fetch($id);
812 throw new RestException(404,
'Invoice not found');
816 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
819 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
821 throw new RestException(500,
'Error : '.$this->invoice->error);
824 $result = $this->invoice->fetch($id);
826 throw new RestException(404,
'Invoice not found');
830 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
855 if (!DolibarrApiAccess::$user->rights->facture->creer) {
856 throw new RestException(401);
858 $result = $this->invoice->fetch($id);
860 throw new RestException(404,
'Invoice not found');
864 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
867 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
869 throw new RestException(304,
'Nothing done.');
872 throw new RestException(500,
'Error : '.$this->invoice->error);
875 $result = $this->invoice->fetch($id);
877 throw new RestException(404,
'Invoice not found');
881 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
904 public function validate($id, $idwarehouse = 0, $notrigger = 0)
906 if (!DolibarrApiAccess::$user->rights->facture->creer) {
907 throw new RestException(401);
909 $result = $this->invoice->fetch($id);
911 throw new RestException(404,
'Invoice not found');
915 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
918 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
920 throw new RestException(304,
'Error nothing done. May be object is already validated');
923 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
926 $result = $this->invoice->fetch($id);
928 throw new RestException(404,
'Invoice not found');
932 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
953 public function settopaid($id, $close_code =
'', $close_note =
'')
955 if (!DolibarrApiAccess::$user->rights->facture->creer) {
956 throw new RestException(401);
958 $result = $this->invoice->fetch($id);
960 throw new RestException(404,
'Invoice not found');
964 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
967 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
969 throw new RestException(304,
'Error nothing done. May be object is already validated');
972 throw new RestException(500,
'Error : '.$this->invoice->error);
976 $result = $this->invoice->fetch($id);
978 throw new RestException(404,
'Invoice not found');
982 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1004 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1005 throw new RestException(401);
1007 $result = $this->invoice->fetch($id);
1009 throw new RestException(404,
'Invoice not found');
1013 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1016 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1018 throw new RestException(304,
'Nothing done');
1021 throw new RestException(500,
'Error : '.$this->invoice->error);
1025 $result = $this->invoice->fetch($id);
1027 throw new RestException(404,
'Invoice not found');
1031 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1047 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1049 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1050 throw new RestException(401);
1053 $result = $this->invoice->fetch($id);
1055 throw new RestException(404,
'Invoice not found');
1059 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1063 $result = $discountcheck->fetch(0, $this->invoice->id);
1066 throw new RestException(404,
'Discount not found');
1069 throw new RestException(500, $discountcheck->error);
1072 return parent::_cleanObjectDatas($discountcheck);
1090 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1092 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1093 throw new RestException(401);
1096 $result = $this->invoice->fetch($id);
1098 throw new RestException(404,
'Invoice not found');
1102 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1105 if ($this->invoice->paye) {
1106 throw new RestException(500,
'Alreay paid');
1109 $this->invoice->fetch($id);
1110 $this->invoice->fetch_thirdparty();
1114 $result = $discountcheck->fetch(0, $this->invoice->id);
1126 $amount_ht = $amount_tva = $amount_ttc = array();
1127 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1131 foreach ($this->invoice->lines as $line) {
1132 if ($line->product_type < 9 && $line->total_ht != 0) {
1134 $amount_ht[$line->tva_tx] += $line->total_ht;
1135 $amount_tva[$line->tva_tx] += $line->total_tva;
1136 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1137 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1138 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1139 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1147 $discount->description =
'(CREDIT_NOTE)';
1149 $discount->description =
'(DEPOSIT)';
1151 $discount->description =
'(EXCESS RECEIVED)';
1153 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1156 $discount->fk_soc = $this->invoice->socid;
1157 $discount->fk_facture_source = $this->invoice->id;
1165 $sql =
'SELECT SUM(pf.amount) as total_payments';
1166 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1167 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1168 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1169 $sql .=
' AND pf.fk_paiement = p.rowid';
1170 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1171 $resql = $this->db->query($sql);
1176 $res = $this->db->fetch_object($resql);
1177 $total_payments = $res->total_payments;
1180 $total_creditnote_and_deposit = 0;
1181 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1182 $sql .=
" re.description, re.fk_facture_source";
1183 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1184 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1185 $resql = $this->db->query($sql);
1186 if (!empty($resql)) {
1187 while ($obj = $this->db->fetch_object($resql)) {
1188 $total_creditnote_and_deposit += $obj->amount_ttc;
1194 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1195 $discount->amount_tva = 0;
1196 $discount->tva_tx = 0;
1198 $result = $discount->create(DolibarrApiAccess::$user);
1204 foreach ($amount_ht as $tva_tx => $xxx) {
1205 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1206 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1207 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1208 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1209 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1210 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1211 $discount->tva_tx = abs($tva_tx);
1213 $result = $discount->create(DolibarrApiAccess::$user);
1221 if (empty($error)) {
1224 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1226 $this->db->commit();
1228 $this->db->rollback();
1229 throw new RestException(500,
'Could not set paid');
1232 $this->db->commit();
1235 $this->db->rollback();
1236 throw new RestException(500,
'Discount creation error');
1261 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1262 throw new RestException(401);
1265 throw new RestException(400,
'Invoice ID is mandatory');
1267 if (empty($discountid)) {
1268 throw new RestException(400,
'Discount ID is mandatory');
1272 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1275 $result = $this->invoice->fetch($id);
1277 throw new RestException(404,
'Invoice not found');
1280 $result = $this->invoice->insert_discount($discountid);
1282 throw new RestException(405, $this->invoice->error);
1306 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1308 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1309 throw new RestException(401);
1312 throw new RestException(400,
'Invoice ID is mandatory');
1314 if (empty($discountid)) {
1315 throw new RestException(400,
'Credit ID is mandatory');
1319 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1322 $result = $discount->fetch($discountid);
1324 throw new RestException(404,
'Credit not found');
1327 $result = $discount->link_to_invoice(0, $id);
1329 throw new RestException(405, $discount->error);
1350 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1351 throw new RestException(401);
1354 throw new RestException(400,
'Invoice ID is mandatory');
1358 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1361 $result = $this->invoice->fetch($id);
1363 throw new RestException(404,
'Invoice not found');
1366 $result = $this->invoice->getListOfPayments();
1368 throw new RestException(405, $this->invoice->error);
1396 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1398 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1400 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1401 throw new RestException(403);
1404 throw new RestException(400,
'Invoice ID is mandatory');
1408 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1411 if (isModEnabled(
"banque")) {
1412 if (empty($accountid)) {
1413 throw new RestException(400,
'Account ID is mandatory');
1417 if (empty($paymentid)) {
1418 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1422 $result = $this->invoice->fetch($id);
1424 throw new RestException(404,
'Invoice not found');
1428 $totalpaid = $this->invoice->getSommePaiement();
1429 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1430 $totaldeposits = $this->invoice->getSumDepositsUsed();
1431 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1436 $multicurrency_amounts = array();
1440 $resteapayer =
price2num($resteapayer,
'MT');
1441 $amounts[$id] =
price2num(-1 * $resteapayer,
'MT');
1443 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1444 $multicurrency_amounts[$id] =
price2num(-1 * $newvalue,
'MT');
1446 $resteapayer =
price2num($resteapayer,
'MT');
1447 $amounts[$id] = $resteapayer;
1449 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1450 $multicurrency_amounts[$id] = $newvalue;
1454 $paymentobj =
new Paiement($this->db);
1455 $paymentobj->datepaye = $datepaye;
1456 $paymentobj->amounts = $amounts;
1457 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1458 $paymentobj->paiementid = $paymentid;
1459 $paymentobj->paiementcode =
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1460 $paymentobj->num_payment = $num_payment;
1461 $paymentobj->note_private = $comment;
1463 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1464 if ($payment_id < 0) {
1465 $this->db->rollback();
1466 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1469 if (isModEnabled(
"banque")) {
1470 $label =
'(CustomerInvoicePayment)';
1472 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1473 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1476 $label =
'(CustomerInvoicePaymentBack)';
1478 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1480 $this->db->rollback();
1481 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1485 $this->db->commit();
1516 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1518 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1520 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1521 throw new RestException(403);
1523 foreach ($arrayofamounts as $id => $amount) {
1525 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 throw new RestException(403,
'Access not allowed on invoice ID '.$id.
' for login '.DolibarrApiAccess::$user->login);
1532 if (isModEnabled(
"banque")) {
1533 if (empty($accountid)) {
1534 throw new RestException(400,
'Account ID is mandatory');
1537 if (empty($paymentid)) {
1538 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1544 $multicurrency_amounts = array();
1547 foreach ($arrayofamounts as $id => $amountarray) {
1548 $result = $this->invoice->fetch($id);
1550 $this->db->rollback();
1551 throw new RestException(404,
'Invoice ID '.$id.
' not found');
1554 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1555 $this->db->rollback();
1556 throw new RestException(400,
'Payment in both currency '.$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1559 $is_multicurrency = 0;
1560 $total_ttc = $this->invoice->total_ttc;
1562 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1563 $is_multicurrency = 1;
1564 $total_ttc = $this->invoice->multicurrency_total_ttc;
1568 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1569 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1570 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1571 $remainstopay = $amount =
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1573 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1574 $amount =
price2num($amountarray[
"amount"],
'MT');
1577 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1578 $amount =
price2num($amountarray[
"multicurrency_amount"],
'MT');
1581 if ($amount > $remainstopay && !$accepthigherpayment) {
1582 $this->db->rollback();
1583 throw new RestException(400,
'Payment amount on invoice ID '.$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1587 $amount =
price2num(-1 * $amount,
'MT');
1590 if ($is_multicurrency) {
1591 $amounts[$id] =
null;
1593 $multicurrency_amounts[$id] = $amount;
1595 $amounts[$id] = $amount;
1597 $multicurrency_amounts[$id] =
null;
1602 $paymentobj =
new Paiement($this->db);
1603 $paymentobj->datepaye = $datepaye;
1604 $paymentobj->amounts = $amounts;
1605 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1606 $paymentobj->paiementid = $paymentid;
1607 $paymentobj->paiementcode =
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1608 $paymentobj->num_payment = $num_payment;
1609 $paymentobj->note_private = $comment;
1610 $paymentobj->ref_ext = $ref_ext;
1611 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1612 if ($payment_id < 0) {
1613 $this->db->rollback();
1614 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1616 if (isModEnabled(
"banque")) {
1617 $label =
'(CustomerInvoicePayment)';
1618 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1619 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1622 $label =
'(CustomerInvoicePaymentBack)';
1624 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1626 $this->db->rollback();
1627 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1631 $this->db->commit();
1652 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1654 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1655 throw new RestException(401);
1658 throw new RestException(400,
'Payment ID is mandatory');
1661 $paymentobj =
new Paiement($this->db);
1662 $result = $paymentobj->fetch($id);
1665 throw new RestException(404,
'Payment not found');
1668 if (!empty($num_payment)) {
1669 $result = $paymentobj->update_num($num_payment);
1671 throw new RestException(500,
'Error when updating the payment num');
1678 'message' =>
'Payment updated'
1693 $object = parent::_cleanObjectDatas($object);
1695 unset($object->note);
1696 unset($object->address);
1697 unset($object->barcode_type);
1698 unset($object->barcode_type_code);
1699 unset($object->barcode_type_label);
1700 unset($object->barcode_type_coder);
1701 unset($object->canvas);
1717 foreach (Invoices::$FIELDS as $field) {
1718 if (!isset($data[$field])) {
1719 throw new RestException(400,
"$field field missing");
1721 $invoice[$field] = $data[$field];
1760 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1761 throw new RestException(401);
1764 $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1766 throw new RestException(404,
'Template invoice not found');
1770 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1774 if ($contact_list > -1) {
1775 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1776 if (is_array($tmparray)) {
1777 $this->template_invoice->contacts_ids = $tmparray;
1781 $this->template_invoice->fetchObjectLinked();
1796 $object = parent::_cleanObjectDatas($object);
1798 unset($object->note);
1799 unset($object->address);
1800 unset($object->barcode_type);
1801 unset($object->barcode_type_code);
1802 unset($object->barcode_type_label);
1803 unset($object->barcode_type_coder);
1804 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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage invoices.
const TYPE_REPLACEMENT
Replacement invoice.
const TYPE_STANDARD
Standard invoice.
const TYPE_SITUATION
Situation invoice.
const TYPE_DEPOSIT
Deposit invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
Class to manage invoice templates.
postContact($id, $contactid, $type)
Add a contact type of given invoice.
putPayment($id, $num_payment='')
Update a payment.
addContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0)
Adds a contact to an invoice.
createInvoiceFromOrder($orderid)
Create an invoice using an existing order.
markAsCreditAvailable($id)
Create a discount (credit available) for a credit note or a deposit.
getDiscount($id)
Get discount from invoice.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='')
List invoices.
__construct()
Constructor.
put($id, $request_data=null)
Update invoice.
getByRefExt($ref_ext, $contact_list=1)
Get properties of an invoice object by ref_ext.
_fetch($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an invoice object.
getByRef($ref, $contact_list=1)
Get properties of an invoice object by ref.
getPayments($id)
Get list of payments of a given invoice.
_cleanObjectDatas($object)
Clean sensible object datas.
post($request_data=null)
Create invoice object.
useDiscount($id, $discountid)
Add a discount line into an invoice (as an invoice line) using an existing absolute discount.
settounpaid($id)
Sets an invoice as unpaid.
getTemplateInvoice($id, $contact_list=1)
Get properties of a template invoice object.
putLine($id, $lineid, $request_data=null)
Update a line to a given invoice.
getLines($id)
Get lines of an invoice.
useCreditNote($id, $discountid)
Add an available credit note discount to payments of an existing invoice.
addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment='', $comment='', $chqemetteur='', $chqbank='', $ref_ext='', $accepthigherpayment=false)
Add a payment to pay partially or completely one or several invoices.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an invoice.
_cleanTemplateObjectDatas($object)
Clean sensible object datas.
deleteContact($id, $contactid, $type)
Delete a contact type of given invoice.
postLine($id, $request_data=null)
Add a line to a given invoice.
_fetchTemplateInvoice($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an invoice object.
_validate($data)
Validate fields before create or update object.
addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment='', $comment='', $chqemetteur='', $chqbank='')
Add payment line to a specific invoice with the remain to pay as amount.
settopaid($id, $close_code='', $close_note='')
Sets an invoice as paid.
settodraft($id, $idwarehouse=-1)
Sets an invoice as draft.
deleteLine($id, $lineid)
Deletes a line of a given invoice.
Class to manage payments of customer invoices.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.