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 $this->invoice->$field = $value;
629 if (!empty($this->invoice->fk_account)) {
630 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
631 throw new RestException(400, $this->invoice->error);
635 if ($this->invoice->update(DolibarrApiAccess::$user)) {
636 return $this->
get($id);
648 public function delete($id)
650 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'supprimer')) {
651 throw new RestException(401);
653 $result = $this->invoice->fetch($id);
655 throw new RestException(404,
'Invoice not found');
659 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
662 $result = $this->invoice->delete(DolibarrApiAccess::$user);
664 throw new RestException(500,
'Error when deleting invoice');
665 } elseif ($result == 0) {
666 throw new RestException(403,
'Invoice not erasable');
672 'message' =>
'Invoice deleted'
700 public function postLine($id, $request_data =
null)
702 if (!DolibarrApiAccess::$user->rights->facture->creer) {
703 throw new RestException(401);
706 $result = $this->invoice->fetch($id);
708 throw new RestException(404,
'Invoice not found');
712 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
715 $request_data = (object) $request_data;
717 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
718 $request_data->label =
sanitizeVal($request_data->label);
721 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
722 $request_data->fk_parent_line = 0;
726 $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);
727 $pa_ht = $marginInfos[0];
729 $updateRes = $this->invoice->addline(
731 $request_data->subprice,
733 $request_data->tva_tx,
734 $request_data->localtax1_tx,
735 $request_data->localtax2_tx,
736 $request_data->fk_product,
737 $request_data->remise_percent,
738 $request_data->date_start,
739 $request_data->date_end,
740 $request_data->fk_code_ventilation,
741 $request_data->info_bits,
742 $request_data->fk_remise_except,
743 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
744 $request_data->subprice,
745 $request_data->product_type,
747 $request_data->special_code,
748 $request_data->origin,
749 $request_data->origin_id,
750 $request_data->fk_parent_line,
751 empty($request_data->fk_fournprice) ?
null:$request_data->fk_fournprice,
753 $request_data->label,
754 $request_data->array_options,
755 $request_data->situation_percent,
756 $request_data->fk_prev_id,
757 $request_data->fk_unit,
759 $request_data->ref_ext
762 if ($updateRes < 0) {
763 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
788 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
790 if (!DolibarrApiAccess::$user->rights->facture->creer) {
791 throw new RestException(401);
793 $result = $this->invoice->fetch($id);
795 throw new RestException(404,
'Invoice not found');
799 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
802 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
804 throw new RestException(500,
'Error : '.$this->invoice->error);
807 $result = $this->invoice->fetch($id);
809 throw new RestException(404,
'Invoice not found');
813 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
838 if (!DolibarrApiAccess::$user->rights->facture->creer) {
839 throw new RestException(401);
841 $result = $this->invoice->fetch($id);
843 throw new RestException(404,
'Invoice not found');
847 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
850 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
852 throw new RestException(304,
'Nothing done.');
855 throw new RestException(500,
'Error : '.$this->invoice->error);
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);
887 public function validate($id, $idwarehouse = 0, $notrigger = 0)
889 if (!DolibarrApiAccess::$user->rights->facture->creer) {
890 throw new RestException(401);
892 $result = $this->invoice->fetch($id);
894 throw new RestException(404,
'Invoice not found');
898 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
901 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
903 throw new RestException(304,
'Error nothing done. May be object is already validated');
906 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
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);
936 public function settopaid($id, $close_code =
'', $close_note =
'')
938 if (!DolibarrApiAccess::$user->rights->facture->creer) {
939 throw new RestException(401);
941 $result = $this->invoice->fetch($id);
943 throw new RestException(404,
'Invoice not found');
947 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
950 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
952 throw new RestException(304,
'Error nothing done. May be object is already validated');
955 throw new RestException(500,
'Error : '.$this->invoice->error);
959 $result = $this->invoice->fetch($id);
961 throw new RestException(404,
'Invoice not found');
965 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
987 if (!DolibarrApiAccess::$user->rights->facture->creer) {
988 throw new RestException(401);
990 $result = $this->invoice->fetch($id);
992 throw new RestException(404,
'Invoice not found');
996 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
999 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1001 throw new RestException(304,
'Nothing done');
1004 throw new RestException(500,
'Error : '.$this->invoice->error);
1008 $result = $this->invoice->fetch($id);
1010 throw new RestException(404,
'Invoice not found');
1014 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1030 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1032 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1033 throw new RestException(401);
1036 $result = $this->invoice->fetch($id);
1038 throw new RestException(404,
'Invoice not found');
1042 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1046 $result = $discountcheck->fetch(0, $this->invoice->id);
1049 throw new RestException(404,
'Discount not found');
1052 throw new RestException(500, $discountcheck->error);
1055 return parent::_cleanObjectDatas($discountcheck);
1073 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1075 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1076 throw new RestException(401);
1079 $result = $this->invoice->fetch($id);
1081 throw new RestException(404,
'Invoice not found');
1085 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1088 if ($this->invoice->paye) {
1089 throw new RestException(500,
'Alreay paid');
1092 $this->invoice->fetch($id);
1093 $this->invoice->fetch_thirdparty();
1097 $result = $discountcheck->fetch(0, $this->invoice->id);
1109 $amount_ht = $amount_tva = $amount_ttc = array();
1110 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1114 foreach ($this->invoice->lines as $line) {
1115 if ($line->product_type < 9 && $line->total_ht != 0) {
1117 $amount_ht[$line->tva_tx] += $line->total_ht;
1118 $amount_tva[$line->tva_tx] += $line->total_tva;
1119 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1120 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1121 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1122 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1130 $discount->description =
'(CREDIT_NOTE)';
1132 $discount->description =
'(DEPOSIT)';
1134 $discount->description =
'(EXCESS RECEIVED)';
1136 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1139 $discount->fk_soc = $this->invoice->socid;
1140 $discount->fk_facture_source = $this->invoice->id;
1148 $sql =
'SELECT SUM(pf.amount) as total_payments';
1149 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1150 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1151 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1152 $sql .=
' AND pf.fk_paiement = p.rowid';
1153 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1154 $resql = $this->db->query($sql);
1159 $res = $this->db->fetch_object($resql);
1160 $total_payments = $res->total_payments;
1163 $total_creditnote_and_deposit = 0;
1164 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1165 $sql .=
" re.description, re.fk_facture_source";
1166 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1167 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1168 $resql = $this->db->query($sql);
1169 if (!empty($resql)) {
1170 while ($obj = $this->db->fetch_object($resql)) {
1171 $total_creditnote_and_deposit += $obj->amount_ttc;
1177 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1178 $discount->amount_tva = 0;
1179 $discount->tva_tx = 0;
1181 $result = $discount->create(DolibarrApiAccess::$user);
1187 foreach ($amount_ht as $tva_tx => $xxx) {
1188 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1189 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1190 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1191 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1192 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1193 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1194 $discount->tva_tx = abs($tva_tx);
1196 $result = $discount->create(DolibarrApiAccess::$user);
1204 if (empty($error)) {
1207 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1209 $this->db->commit();
1211 $this->db->rollback();
1212 throw new RestException(500,
'Could not set paid');
1215 $this->db->commit();
1218 $this->db->rollback();
1219 throw new RestException(500,
'Discount creation error');
1244 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1245 throw new RestException(401);
1248 throw new RestException(400,
'Invoice ID is mandatory');
1250 if (empty($discountid)) {
1251 throw new RestException(400,
'Discount ID is mandatory');
1255 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1258 $result = $this->invoice->fetch($id);
1260 throw new RestException(404,
'Invoice not found');
1263 $result = $this->invoice->insert_discount($discountid);
1265 throw new RestException(405, $this->invoice->error);
1289 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1291 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1292 throw new RestException(401);
1295 throw new RestException(400,
'Invoice ID is mandatory');
1297 if (empty($discountid)) {
1298 throw new RestException(400,
'Credit ID is mandatory');
1302 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1305 $result = $discount->fetch($discountid);
1307 throw new RestException(404,
'Credit not found');
1310 $result = $discount->link_to_invoice(0, $id);
1312 throw new RestException(405, $discount->error);
1334 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1335 throw new RestException(401);
1338 throw new RestException(400,
'Invoice ID is mandatory');
1342 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1345 $result = $this->invoice->fetch($id);
1347 throw new RestException(404,
'Invoice not found');
1350 $result = $this->invoice->getListOfPayments();
1352 throw new RestException(405, $this->invoice->error);
1380 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1382 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1384 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1385 throw new RestException(403);
1388 throw new RestException(400,
'Invoice ID is mandatory');
1392 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1395 if (isModEnabled(
"banque")) {
1396 if (empty($accountid)) {
1397 throw new RestException(400,
'Account ID is mandatory');
1401 if (empty($paymentid)) {
1402 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1406 $result = $this->invoice->fetch($id);
1408 throw new RestException(404,
'Invoice not found');
1412 $totalpaid = $this->invoice->getSommePaiement();
1413 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1414 $totaldeposits = $this->invoice->getSumDepositsUsed();
1415 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1420 $multicurrency_amounts = array();
1424 $resteapayer =
price2num($resteapayer,
'MT');
1425 $amounts[$id] = -$resteapayer;
1427 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1428 $multicurrency_amounts[$id] = -$newvalue;
1430 $resteapayer =
price2num($resteapayer,
'MT');
1431 $amounts[$id] = $resteapayer;
1433 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1434 $multicurrency_amounts[$id] = $newvalue;
1438 $paymentobj =
new Paiement($this->db);
1439 $paymentobj->datepaye = $datepaye;
1440 $paymentobj->amounts = $amounts;
1441 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1442 $paymentobj->paiementid = $paymentid;
1443 $paymentobj->paiementcode =
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1444 $paymentobj->num_payment = $num_payment;
1445 $paymentobj->note_private = $comment;
1447 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1448 if ($payment_id < 0) {
1449 $this->db->rollback();
1450 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1453 if (isModEnabled(
"banque")) {
1454 $label =
'(CustomerInvoicePayment)';
1456 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1457 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1460 $label =
'(CustomerInvoicePaymentBack)';
1462 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1464 $this->db->rollback();
1465 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1469 $this->db->commit();
1500 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1502 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1504 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1505 throw new RestException(403);
1507 foreach ($arrayofamounts as $id => $amount) {
1509 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1512 throw new RestException(403,
'Access not allowed on invoice ID '.$id.
' for login '.DolibarrApiAccess::$user->login);
1516 if (isModEnabled(
"banque")) {
1517 if (empty($accountid)) {
1518 throw new RestException(400,
'Account ID is mandatory');
1521 if (empty($paymentid)) {
1522 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1528 $multicurrency_amounts = array();
1531 foreach ($arrayofamounts as $id => $amountarray) {
1532 $result = $this->invoice->fetch($id);
1534 $this->db->rollback();
1535 throw new RestException(404,
'Invoice ID '.$id.
' not found');
1538 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1539 $this->db->rollback();
1540 throw new RestException(400,
'Payment in both currency '.$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1543 $is_multicurrency = 0;
1544 $total_ttc = $this->invoice->total_ttc;
1546 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1547 $is_multicurrency = 1;
1548 $total_ttc = $this->invoice->multicurrency_total_ttc;
1552 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1553 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1554 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1555 $remainstopay = $amount =
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1557 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1558 $amount =
price2num($amountarray[
"amount"],
'MT');
1561 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1562 $amount =
price2num($amountarray[
"multicurrency_amount"],
'MT');
1565 if ($amount > $remainstopay && !$accepthigherpayment) {
1566 $this->db->rollback();
1567 throw new RestException(400,
'Payment amount on invoice ID '.$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1574 if ($is_multicurrency) {
1575 $amounts[$id] =
null;
1577 $multicurrency_amounts[$id] = $amount;
1579 $amounts[$id] = $amount;
1581 $multicurrency_amounts[$id] =
null;
1586 $paymentobj =
new Paiement($this->db);
1587 $paymentobj->datepaye = $datepaye;
1588 $paymentobj->amounts = $amounts;
1589 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1590 $paymentobj->paiementid = $paymentid;
1591 $paymentobj->paiementcode =
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1592 $paymentobj->num_payment = $num_payment;
1593 $paymentobj->note_private = $comment;
1594 $paymentobj->ref_ext = $ref_ext;
1595 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1596 if ($payment_id < 0) {
1597 $this->db->rollback();
1598 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1600 if (isModEnabled(
"banque")) {
1601 $label =
'(CustomerInvoicePayment)';
1602 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1603 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1606 $label =
'(CustomerInvoicePaymentBack)';
1608 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1610 $this->db->rollback();
1611 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1615 $this->db->commit();
1636 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1638 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1639 throw new RestException(401);
1642 throw new RestException(400,
'Payment ID is mandatory');
1645 $paymentobj =
new Paiement($this->db);
1646 $result = $paymentobj->fetch($id);
1649 throw new RestException(404,
'Payment not found');
1652 if (!empty($num_payment)) {
1653 $result = $paymentobj->update_num($num_payment);
1655 throw new RestException(500,
'Error when updating the payment num');
1662 'message' =>
'Payment updated'
1677 $object = parent::_cleanObjectDatas($object);
1679 unset($object->note);
1680 unset($object->address);
1681 unset($object->barcode_type);
1682 unset($object->barcode_type_code);
1683 unset($object->barcode_type_label);
1684 unset($object->barcode_type_coder);
1685 unset($object->canvas);
1701 foreach (Invoices::$FIELDS as $field) {
1702 if (!isset($data[$field])) {
1703 throw new RestException(400,
"$field field missing");
1705 $invoice[$field] = $data[$field];
1744 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1745 throw new RestException(401);
1748 $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1750 throw new RestException(404,
'Template invoice not found');
1754 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1758 if ($contact_list > -1) {
1759 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1760 if (is_array($tmparray)) {
1761 $this->template_invoice->contacts_ids = $tmparray;
1765 $this->template_invoice->fetchObjectLinked();
1780 $object = parent::_cleanObjectDatas($object);
1782 unset($object->note);
1783 unset($object->address);
1784 unset($object->barcode_type);
1785 unset($object->barcode_type_code);
1786 unset($object->barcode_type_label);
1787 unset($object->barcode_type_coder);
1788 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.
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.
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.