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 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();
179 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'')
183 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
184 throw new RestException(401);
190 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
194 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
195 $search_sale = DolibarrApiAccess::$user->id;
198 $sql =
"SELECT t.rowid";
199 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
200 $sql .=
", sc.fk_soc, sc.fk_user";
202 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture AS t LEFT JOIN ".MAIN_DB_PREFIX.
"facture_extrafields AS ef ON (ef.fk_object = t.rowid)";
204 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
205 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
208 $sql .=
' WHERE t.entity IN ('.getEntity(
'invoice').
')';
209 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
210 $sql .=
" AND t.fk_soc = sc.fk_soc";
213 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
216 if ($search_sale > 0) {
217 $sql .=
" AND t.rowid = sc.fk_soc";
221 if ($status ==
'draft') {
222 $sql .=
" AND t.fk_statut IN (0)";
224 if ($status ==
'unpaid') {
225 $sql .=
" AND t.fk_statut IN (1)";
227 if ($status ==
'paid') {
228 $sql .=
" AND t.fk_statut IN (2)";
230 if ($status ==
'cancelled') {
231 $sql .=
" AND t.fk_statut IN (3)";
234 if ($search_sale > 0) {
235 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
242 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
246 $sql .= $this->db->order($sortfield, $sortorder);
251 $offset = $limit * $page;
253 $sql .= $this->db->plimit($limit + 1, $offset);
256 $result = $this->db->query($sql);
259 $num = $this->db->num_rows($result);
260 $min = min($num, ($limit <= 0 ? $num : $limit));
262 $obj = $this->db->fetch_object($result);
263 $invoice_static =
new Facture($this->db);
264 if ($invoice_static->fetch($obj->rowid)) {
266 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
267 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
268 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
269 $invoice_static->remaintopay =
price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits,
'MT');
272 $tmparray = $invoice_static->liste_contact(-1,
'external', 1);
273 if (is_array($tmparray)) {
274 $invoice_static->contacts_ids = $tmparray;
281 throw new RestException(503,
'Error when retrieve invoice list : '.$this->db->lasterror());
283 if (!count($obj_ret)) {
284 throw new RestException(404,
'No invoice found');
295 public function post($request_data =
null)
297 if (!DolibarrApiAccess::$user->rights->facture->creer) {
298 throw new RestException(401,
"Insuffisant rights");
301 $result = $this->
_validate($request_data);
303 foreach ($request_data as $field => $value) {
304 $this->invoice->$field = $value;
306 if (!array_key_exists(
'date', $request_data)) {
307 $this->invoice->date =
dol_now();
318 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data[
"date_lim_reglement"]) ? 0 : $request_data[
"date_lim_reglement"])) < 0) {
319 throw new RestException(500,
"Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
321 return ((
int) $this->invoice->id);
339 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
341 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
342 throw new RestException(401);
344 if (!DolibarrApiAccess::$user->rights->facture->creer) {
345 throw new RestException(401);
347 if (empty($orderid)) {
348 throw new RestException(400,
'Order ID is mandatory');
352 $result = $order->fetch($orderid);
354 throw new RestException(404,
'Order not found');
357 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
359 throw new RestException(405, $this->invoice->error);
361 $this->invoice->fetchObjectLinked();
375 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
376 throw new RestException(401);
379 $result = $this->invoice->fetch($id);
381 throw new RestException(404,
'Invoice not found');
385 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
387 $this->invoice->getLinesArray();
389 foreach ($this->invoice->lines as $line) {
409 public function putLine($id, $lineid, $request_data =
null)
411 if (!DolibarrApiAccess::$user->rights->facture->creer) {
412 throw new RestException(401);
415 $result = $this->invoice->fetch($id);
417 throw new RestException(404,
'Invoice not found');
421 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
424 $request_data = (object) $request_data;
426 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
427 $request_data->label =
sanitizeVal($request_data->label);
429 $updateRes = $this->invoice->updateline(
432 $request_data->subprice,
434 $request_data->remise_percent,
435 $request_data->date_start,
436 $request_data->date_end,
437 $request_data->tva_tx,
438 $request_data->localtax1_tx,
439 $request_data->localtax2_tx,
440 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
441 $request_data->info_bits,
442 $request_data->product_type,
443 $request_data->fk_parent_line,
445 $request_data->fk_fournprice,
446 $request_data->pa_ht,
447 $request_data->label,
448 $request_data->special_code,
449 $request_data->array_options,
450 $request_data->situation_percent,
451 $request_data->fk_unit,
452 $request_data->multicurrency_subprice,
454 $request_data->ref_ext,
458 if ($updateRes > 0) {
459 $result = $this->
get($id);
460 unset($result->line);
463 throw new RestException(304, $this->invoice->error);
482 if (!DolibarrApiAccess::$user->rights->facture->creer) {
483 throw new RestException(401);
486 $result = $this->invoice->fetch($id);
489 throw new RestException(404,
'Invoice not found');
492 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
493 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
497 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
500 $result = $this->invoice->add_contact($contactid, $type,
'external');
503 throw new RestException(500,
'Error when added the contact');
509 'message' =>
'Contact linked to the invoice'
530 if (!DolibarrApiAccess::$user->rights->facture->creer) {
531 throw new RestException(401);
534 $result = $this->invoice->fetch($id);
537 throw new RestException(404,
'Invoice not found');
541 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
544 $contacts = $this->invoice->liste_contact();
546 foreach ($contacts as $contact) {
547 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
548 $result = $this->invoice->delete_contact($contact[
'rowid']);
551 throw new RestException(500,
'Error when deleted the contact');
575 if (!DolibarrApiAccess::$user->rights->facture->creer) {
576 throw new RestException(401);
578 if (empty($lineid)) {
579 throw new RestException(400,
'Line ID is mandatory');
583 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
586 $result = $this->invoice->fetch($id);
588 throw new RestException(404,
'Invoice not found');
591 $updateRes = $this->invoice->deleteline($lineid, $id);
592 if ($updateRes > 0) {
593 return $this->
get($id);
595 throw new RestException(405, $this->invoice->error);
606 public function put($id, $request_data =
null)
608 if (!DolibarrApiAccess::$user->rights->facture->creer) {
609 throw new RestException(401);
612 $result = $this->invoice->fetch($id);
614 throw new RestException(404,
'Invoice not found');
618 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
621 foreach ($request_data as $field => $value) {
622 if ($field ==
'id') {
625 if ($field ==
'array_options' && is_array($value)) {
626 foreach ($value as $index => $val) {
627 $this->invoice->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->invoice);
631 $this->invoice->$field = $value;
635 if (!empty($this->invoice->fk_account)) {
636 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
637 throw new RestException(400, $this->invoice->error);
641 if ($this->invoice->update(DolibarrApiAccess::$user) > 0) {
642 return $this->
get($id);
644 throw new RestException(500, $this->invoice->error);
654 public function delete($id)
656 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'supprimer')) {
657 throw new RestException(401);
659 $result = $this->invoice->fetch($id);
661 throw new RestException(404,
'Invoice not found');
665 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
668 $result = $this->invoice->delete(DolibarrApiAccess::$user);
670 throw new RestException(500,
'Error when deleting invoice');
671 } elseif ($result == 0) {
672 throw new RestException(403,
'Invoice not erasable');
678 'message' =>
'Invoice deleted'
706 public function postLine($id, $request_data =
null)
708 if (!DolibarrApiAccess::$user->rights->facture->creer) {
709 throw new RestException(401);
712 $result = $this->invoice->fetch($id);
714 throw new RestException(404,
'Invoice not found');
718 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
721 $request_data = (object) $request_data;
723 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
724 $request_data->label =
sanitizeVal($request_data->label);
727 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
728 $request_data->fk_parent_line = 0;
732 $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);
733 $pa_ht = $marginInfos[0];
735 $updateRes = $this->invoice->addline(
737 $request_data->subprice,
739 $request_data->tva_tx,
740 $request_data->localtax1_tx,
741 $request_data->localtax2_tx,
742 $request_data->fk_product,
743 $request_data->remise_percent,
744 $request_data->date_start,
745 $request_data->date_end,
746 $request_data->fk_code_ventilation,
747 $request_data->info_bits,
748 $request_data->fk_remise_except,
749 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
750 $request_data->subprice,
751 $request_data->product_type,
753 $request_data->special_code,
754 $request_data->origin,
755 $request_data->origin_id,
756 $request_data->fk_parent_line,
757 empty($request_data->fk_fournprice) ?
null:$request_data->fk_fournprice,
759 $request_data->label,
760 $request_data->array_options,
761 $request_data->situation_percent,
762 $request_data->fk_prev_id,
763 $request_data->fk_unit,
765 $request_data->ref_ext
768 if ($updateRes < 0) {
769 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
794 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
796 if (!DolibarrApiAccess::$user->rights->facture->creer) {
797 throw new RestException(401);
799 $result = $this->invoice->fetch($id);
801 throw new RestException(404,
'Invoice not found');
805 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
808 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
810 throw new RestException(500,
'Error : '.$this->invoice->error);
813 $result = $this->invoice->fetch($id);
815 throw new RestException(404,
'Invoice not found');
819 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
844 if (!DolibarrApiAccess::$user->rights->facture->creer) {
845 throw new RestException(401);
847 $result = $this->invoice->fetch($id);
849 throw new RestException(404,
'Invoice not found');
853 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
856 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
858 throw new RestException(304,
'Nothing done.');
861 throw new RestException(500,
'Error : '.$this->invoice->error);
864 $result = $this->invoice->fetch($id);
866 throw new RestException(404,
'Invoice not found');
870 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
893 public function validate($id, $idwarehouse = 0, $notrigger = 0)
895 if (!DolibarrApiAccess::$user->rights->facture->creer) {
896 throw new RestException(401);
898 $result = $this->invoice->fetch($id);
900 throw new RestException(404,
'Invoice not found');
904 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
907 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
909 throw new RestException(304,
'Error nothing done. May be object is already validated');
912 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
915 $result = $this->invoice->fetch($id);
917 throw new RestException(404,
'Invoice not found');
921 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
942 public function settopaid($id, $close_code =
'', $close_note =
'')
944 if (!DolibarrApiAccess::$user->rights->facture->creer) {
945 throw new RestException(401);
947 $result = $this->invoice->fetch($id);
949 throw new RestException(404,
'Invoice not found');
953 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
956 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
958 throw new RestException(304,
'Error nothing done. May be object is already validated');
961 throw new RestException(500,
'Error : '.$this->invoice->error);
965 $result = $this->invoice->fetch($id);
967 throw new RestException(404,
'Invoice not found');
971 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
993 if (!DolibarrApiAccess::$user->rights->facture->creer) {
994 throw new RestException(401);
996 $result = $this->invoice->fetch($id);
998 throw new RestException(404,
'Invoice not found');
1002 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1005 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1007 throw new RestException(304,
'Nothing done');
1010 throw new RestException(500,
'Error : '.$this->invoice->error);
1014 $result = $this->invoice->fetch($id);
1016 throw new RestException(404,
'Invoice not found');
1020 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1036 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1038 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1039 throw new RestException(401);
1042 $result = $this->invoice->fetch($id);
1044 throw new RestException(404,
'Invoice not found');
1048 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1052 $result = $discountcheck->fetch(0, $this->invoice->id);
1055 throw new RestException(404,
'Discount not found');
1058 throw new RestException(500, $discountcheck->error);
1061 return parent::_cleanObjectDatas($discountcheck);
1079 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1081 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1082 throw new RestException(401);
1085 $result = $this->invoice->fetch($id);
1087 throw new RestException(404,
'Invoice not found');
1091 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1094 if ($this->invoice->paye) {
1095 throw new RestException(500,
'Alreay paid');
1098 $this->invoice->fetch($id);
1099 $this->invoice->fetch_thirdparty();
1103 $result = $discountcheck->fetch(0, $this->invoice->id);
1115 $amount_ht = $amount_tva = $amount_ttc = array();
1116 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1120 foreach ($this->invoice->lines as $line) {
1121 if ($line->product_type < 9 && $line->total_ht != 0) {
1123 $amount_ht[$line->tva_tx] += $line->total_ht;
1124 $amount_tva[$line->tva_tx] += $line->total_tva;
1125 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1126 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1127 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1128 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1136 $discount->description =
'(CREDIT_NOTE)';
1138 $discount->description =
'(DEPOSIT)';
1140 $discount->description =
'(EXCESS RECEIVED)';
1142 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1145 $discount->fk_soc = $this->invoice->socid;
1146 $discount->fk_facture_source = $this->invoice->id;
1154 $sql =
'SELECT SUM(pf.amount) as total_payments';
1155 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1156 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1157 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1158 $sql .=
' AND pf.fk_paiement = p.rowid';
1159 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1160 $resql = $this->db->query($sql);
1165 $res = $this->db->fetch_object($resql);
1166 $total_payments = $res->total_payments;
1169 $total_creditnote_and_deposit = 0;
1170 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1171 $sql .=
" re.description, re.fk_facture_source";
1172 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1173 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1174 $resql = $this->db->query($sql);
1175 if (!empty($resql)) {
1176 while ($obj = $this->db->fetch_object($resql)) {
1177 $total_creditnote_and_deposit += $obj->amount_ttc;
1183 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1184 $discount->amount_tva = 0;
1185 $discount->tva_tx = 0;
1187 $result = $discount->create(DolibarrApiAccess::$user);
1193 foreach ($amount_ht as $tva_tx => $xxx) {
1194 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1195 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1196 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1197 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1198 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1199 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1200 $discount->tva_tx = abs($tva_tx);
1202 $result = $discount->create(DolibarrApiAccess::$user);
1210 if (empty($error)) {
1213 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1215 $this->db->commit();
1217 $this->db->rollback();
1218 throw new RestException(500,
'Could not set paid');
1221 $this->db->commit();
1224 $this->db->rollback();
1225 throw new RestException(500,
'Discount creation error');
1250 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1251 throw new RestException(401);
1254 throw new RestException(400,
'Invoice ID is mandatory');
1256 if (empty($discountid)) {
1257 throw new RestException(400,
'Discount ID is mandatory');
1261 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1264 $result = $this->invoice->fetch($id);
1266 throw new RestException(404,
'Invoice not found');
1269 $result = $this->invoice->insert_discount($discountid);
1271 throw new RestException(405, $this->invoice->error);
1295 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1297 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1298 throw new RestException(401);
1301 throw new RestException(400,
'Invoice ID is mandatory');
1303 if (empty($discountid)) {
1304 throw new RestException(400,
'Credit ID is mandatory');
1308 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1311 $result = $discount->fetch($discountid);
1313 throw new RestException(404,
'Credit not found');
1316 $result = $discount->link_to_invoice(0, $id);
1318 throw new RestException(405, $discount->error);
1340 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1341 throw new RestException(401);
1344 throw new RestException(400,
'Invoice ID is mandatory');
1348 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1351 $result = $this->invoice->fetch($id);
1353 throw new RestException(404,
'Invoice not found');
1356 $result = $this->invoice->getListOfPayments();
1358 throw new RestException(405, $this->invoice->error);
1386 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1388 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1390 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1391 throw new RestException(403);
1394 throw new RestException(400,
'Invoice ID is mandatory');
1398 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1401 if (isModEnabled(
"banque")) {
1402 if (empty($accountid)) {
1403 throw new RestException(400,
'Account ID is mandatory');
1407 if (empty($paymentid)) {
1408 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1412 $result = $this->invoice->fetch($id);
1414 throw new RestException(404,
'Invoice not found');
1418 $totalpaid = $this->invoice->getSommePaiement();
1419 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1420 $totaldeposits = $this->invoice->getSumDepositsUsed();
1421 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1426 $multicurrency_amounts = array();
1430 $resteapayer =
price2num($resteapayer,
'MT');
1431 $amounts[$id] = -$resteapayer;
1433 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1434 $multicurrency_amounts[$id] = -$newvalue;
1436 $resteapayer =
price2num($resteapayer,
'MT');
1437 $amounts[$id] = $resteapayer;
1439 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1440 $multicurrency_amounts[$id] = $newvalue;
1444 $paymentobj =
new Paiement($this->db);
1445 $paymentobj->datepaye = $datepaye;
1446 $paymentobj->amounts = $amounts;
1447 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1448 $paymentobj->paiementid = $paymentid;
1449 $paymentobj->paiementcode =
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1450 $paymentobj->num_payment = $num_payment;
1451 $paymentobj->note_private = $comment;
1453 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1454 if ($payment_id < 0) {
1455 $this->db->rollback();
1456 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1459 if (isModEnabled(
"banque")) {
1460 $label =
'(CustomerInvoicePayment)';
1462 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1463 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1466 $label =
'(CustomerInvoicePaymentBack)';
1468 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1470 $this->db->rollback();
1471 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1475 $this->db->commit();
1506 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1508 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1510 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1511 throw new RestException(403);
1513 foreach ($arrayofamounts as $id => $amount) {
1515 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1518 throw new RestException(403,
'Access not allowed on invoice ID '.$id.
' for login '.DolibarrApiAccess::$user->login);
1522 if (isModEnabled(
"banque")) {
1523 if (empty($accountid)) {
1524 throw new RestException(400,
'Account ID is mandatory');
1527 if (empty($paymentid)) {
1528 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1534 $multicurrency_amounts = array();
1537 foreach ($arrayofamounts as $id => $amountarray) {
1538 $result = $this->invoice->fetch($id);
1540 $this->db->rollback();
1541 throw new RestException(404,
'Invoice ID '.$id.
' not found');
1544 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1545 $this->db->rollback();
1546 throw new RestException(400,
'Payment in both currency '.$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1549 $is_multicurrency = 0;
1550 $total_ttc = $this->invoice->total_ttc;
1552 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1553 $is_multicurrency = 1;
1554 $total_ttc = $this->invoice->multicurrency_total_ttc;
1558 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1559 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1560 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1561 $remainstopay = $amount =
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1563 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1564 $amount =
price2num($amountarray[
"amount"],
'MT');
1567 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1568 $amount =
price2num($amountarray[
"multicurrency_amount"],
'MT');
1571 if ($amount > $remainstopay && !$accepthigherpayment) {
1572 $this->db->rollback();
1573 throw new RestException(400,
'Payment amount on invoice ID '.$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1580 if ($is_multicurrency) {
1581 $amounts[$id] =
null;
1583 $multicurrency_amounts[$id] = $amount;
1585 $amounts[$id] = $amount;
1587 $multicurrency_amounts[$id] =
null;
1592 $paymentobj =
new Paiement($this->db);
1593 $paymentobj->datepaye = $datepaye;
1594 $paymentobj->amounts = $amounts;
1595 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1596 $paymentobj->paiementid = $paymentid;
1597 $paymentobj->paiementcode =
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1598 $paymentobj->num_payment = $num_payment;
1599 $paymentobj->note_private = $comment;
1600 $paymentobj->ref_ext = $ref_ext;
1601 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1602 if ($payment_id < 0) {
1603 $this->db->rollback();
1604 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1606 if (isModEnabled(
"banque")) {
1607 $label =
'(CustomerInvoicePayment)';
1608 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1609 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1612 $label =
'(CustomerInvoicePaymentBack)';
1614 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1616 $this->db->rollback();
1617 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1621 $this->db->commit();
1642 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1644 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1645 throw new RestException(401);
1648 throw new RestException(400,
'Payment ID is mandatory');
1651 $paymentobj =
new Paiement($this->db);
1652 $result = $paymentobj->fetch($id);
1655 throw new RestException(404,
'Payment not found');
1658 if (!empty($num_payment)) {
1659 $result = $paymentobj->update_num($num_payment);
1661 throw new RestException(500,
'Error when updating the payment num');
1668 'message' =>
'Payment updated'
1683 $object = parent::_cleanObjectDatas($object);
1685 unset($object->note);
1686 unset($object->address);
1687 unset($object->barcode_type);
1688 unset($object->barcode_type_code);
1689 unset($object->barcode_type_label);
1690 unset($object->barcode_type_coder);
1691 unset($object->canvas);
1707 foreach (Invoices::$FIELDS as $field) {
1708 if (!isset($data[$field])) {
1709 throw new RestException(400,
"$field field missing");
1711 $invoice[$field] = $data[$field];
1750 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1751 throw new RestException(401);
1754 $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1756 throw new RestException(404,
'Template invoice not found');
1760 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1764 if ($contact_list > -1) {
1765 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1766 if (is_array($tmparray)) {
1767 $this->template_invoice->contacts_ids = $tmparray;
1771 $this->template_invoice->fetchObjectLinked();
1786 $object = parent::_cleanObjectDatas($object);
1788 unset($object->note);
1789 unset($object->address);
1790 unset($object->barcode_type);
1791 unset($object->barcode_type_code);
1792 unset($object->barcode_type_label);
1793 unset($object->barcode_type_coder);
1794 unset($object->canvas);
Class to manage customers orders.
Class to manage absolute discounts.
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.
__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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='')
List invoices.
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.