19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
22 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture-rec.class.php';
37 static $FIELDS = array(
49 private $template_invoice;
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->rights->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->rights->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";
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 $this->invoice->id;
341 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
343 if (!DolibarrApiAccess::$user->rights->commande->lire) {
344 throw new RestException(401);
346 if (!DolibarrApiAccess::$user->rights->facture->creer) {
347 throw new RestException(401);
349 if (empty($orderid)) {
350 throw new RestException(400,
'Order ID is mandatory');
354 $result = $order->fetch($orderid);
356 throw new RestException(404,
'Order not found');
359 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
361 throw new RestException(405, $this->invoice->error);
363 $this->invoice->fetchObjectLinked();
378 if (!DolibarrApiAccess::$user->rights->facture->lire) {
379 throw new RestException(401);
382 $result = $this->invoice->fetch($id);
384 throw new RestException(404,
'Invoice not found');
388 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
390 $this->invoice->getLinesArray();
392 foreach ($this->invoice->lines as $line) {
413 public function putLine($id, $lineid, $request_data =
null)
415 if (!DolibarrApiAccess::$user->rights->facture->creer) {
416 throw new RestException(401);
419 $result = $this->invoice->fetch($id);
421 throw new RestException(404,
'Invoice not found');
425 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
428 $request_data = (object) $request_data;
430 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
431 $request_data->label =
sanitizeVal($request_data->label);
433 $updateRes = $this->invoice->updateline(
436 $request_data->subprice,
438 $request_data->remise_percent,
439 $request_data->date_start,
440 $request_data->date_end,
441 $request_data->tva_tx,
442 $request_data->localtax1_tx,
443 $request_data->localtax2_tx,
444 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
445 $request_data->info_bits,
446 $request_data->product_type,
447 $request_data->fk_parent_line,
449 $request_data->fk_fournprice,
450 $request_data->pa_ht,
451 $request_data->label,
452 $request_data->special_code,
453 $request_data->array_options,
454 $request_data->situation_percent,
455 $request_data->fk_unit,
456 $request_data->multicurrency_subprice,
458 $request_data->ref_ext,
462 if ($updateRes > 0) {
463 $result = $this->
get($id);
464 unset($result->line);
467 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');
531 if (!DolibarrApiAccess::$user->rights->facture->creer) {
532 throw new RestException(401);
535 $result = $this->invoice->fetch($id);
538 throw new RestException(404,
'Invoice not found');
542 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
545 $contacts = $this->invoice->liste_contact();
547 foreach ($contacts as $contact) {
548 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
549 $result = $this->invoice->delete_contact($contact[
'rowid']);
552 throw new RestException(500,
'Error when deleted the contact');
578 if (!DolibarrApiAccess::$user->rights->facture->creer) {
579 throw new RestException(401);
581 if (empty($lineid)) {
582 throw new RestException(400,
'Line ID is mandatory');
586 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
589 $result = $this->invoice->fetch($id);
591 throw new RestException(404,
'Invoice not found');
596 $updateRes = $this->invoice->deleteline($lineid);
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 $this->invoice->$field = $value;
634 if (!empty($this->invoice->fk_account)) {
635 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
636 throw new RestException(400, $this->invoice->error);
640 if ($this->invoice->update(DolibarrApiAccess::$user)) {
641 return $this->
get($id);
653 public function delete($id)
655 if (!DolibarrApiAccess::$user->rights->facture->supprimer) {
656 throw new RestException(401);
658 $result = $this->invoice->fetch($id);
660 throw new RestException(404,
'Invoice not found');
664 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
667 $result = $this->invoice->delete(DolibarrApiAccess::$user);
669 throw new RestException(500,
'Error when deleting invoice');
670 } elseif ($result == 0) {
671 throw new RestException(403,
'Invoice not erasable');
677 'message' =>
'Invoice deleted'
705 public function postLine($id, $request_data =
null)
707 if (!DolibarrApiAccess::$user->rights->facture->creer) {
708 throw new RestException(401);
711 $result = $this->invoice->fetch($id);
713 throw new RestException(404,
'Invoice not found');
717 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
720 $request_data = (object) $request_data;
722 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
723 $request_data->label =
sanitizeVal($request_data->label);
726 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
727 $request_data->fk_parent_line = 0;
731 $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);
732 $pa_ht = $marginInfos[0];
734 $updateRes = $this->invoice->addline(
736 $request_data->subprice,
738 $request_data->tva_tx,
739 $request_data->localtax1_tx,
740 $request_data->localtax2_tx,
741 $request_data->fk_product,
742 $request_data->remise_percent,
743 $request_data->date_start,
744 $request_data->date_end,
745 $request_data->fk_code_ventilation,
746 $request_data->info_bits,
747 $request_data->fk_remise_except,
748 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
749 $request_data->subprice,
750 $request_data->product_type,
752 $request_data->special_code,
753 $request_data->origin,
754 $request_data->origin_id,
755 $request_data->fk_parent_line,
756 empty($request_data->fk_fournprice) ?
null:$request_data->fk_fournprice,
758 $request_data->label,
759 $request_data->array_options,
760 $request_data->situation_percent,
761 $request_data->fk_prev_id,
762 $request_data->fk_unit,
764 $request_data->ref_ext
767 if ($updateRes < 0) {
768 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
793 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
795 if (!DolibarrApiAccess::$user->rights->facture->creer) {
796 throw new RestException(401);
798 $result = $this->invoice->fetch($id);
800 throw new RestException(404,
'Invoice not found');
804 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
807 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
809 throw new RestException(500,
'Error : '.$this->invoice->error);
812 $result = $this->invoice->fetch($id);
814 throw new RestException(404,
'Invoice not found');
818 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);
894 public function validate($id, $idwarehouse = 0, $notrigger = 0)
896 if (!DolibarrApiAccess::$user->rights->facture->creer) {
897 throw new RestException(401);
899 $result = $this->invoice->fetch($id);
901 throw new RestException(404,
'Invoice not found');
905 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
908 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
910 throw new RestException(304,
'Error nothing done. May be object is already validated');
913 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
916 $result = $this->invoice->fetch($id);
918 throw new RestException(404,
'Invoice not found');
922 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
944 public function settopaid($id, $close_code =
'', $close_note =
'')
946 if (!DolibarrApiAccess::$user->rights->facture->creer) {
947 throw new RestException(401);
949 $result = $this->invoice->fetch($id);
951 throw new RestException(404,
'Invoice not found');
955 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
958 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
960 throw new RestException(304,
'Error nothing done. May be object is already validated');
963 throw new RestException(500,
'Error : '.$this->invoice->error);
967 $result = $this->invoice->fetch($id);
969 throw new RestException(404,
'Invoice not found');
973 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
996 if (!DolibarrApiAccess::$user->rights->facture->creer) {
997 throw new RestException(401);
999 $result = $this->invoice->fetch($id);
1001 throw new RestException(404,
'Invoice not found');
1005 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1008 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1010 throw new RestException(304,
'Nothing done');
1013 throw new RestException(500,
'Error : '.$this->invoice->error);
1017 $result = $this->invoice->fetch($id);
1019 throw new RestException(404,
'Invoice not found');
1023 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1040 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1042 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1043 throw new RestException(401);
1046 $result = $this->invoice->fetch($id);
1048 throw new RestException(404,
'Invoice not found');
1052 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1056 $result = $discountcheck->fetch(0, $this->invoice->id);
1059 throw new RestException(404,
'Discount not found');
1062 throw new RestException(500, $discountcheck->error);
1065 return parent::_cleanObjectDatas($discountcheck);
1083 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1085 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1086 throw new RestException(401);
1089 $result = $this->invoice->fetch($id);
1091 throw new RestException(404,
'Invoice not found');
1095 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1098 if ($this->invoice->paye) {
1099 throw new RestException(500,
'Alreay paid');
1102 $this->invoice->fetch($id);
1103 $this->invoice->fetch_thirdparty();
1107 $result = $discountcheck->fetch(0, $this->invoice->id);
1119 $amount_ht = $amount_tva = $amount_ttc = array();
1120 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1124 foreach ($this->invoice->lines as $line) {
1125 if ($line->product_type < 9 && $line->total_ht != 0) {
1127 $amount_ht[$line->tva_tx] += $line->total_ht;
1128 $amount_tva[$line->tva_tx] += $line->total_tva;
1129 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1130 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1131 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1132 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1140 $discount->description =
'(CREDIT_NOTE)';
1142 $discount->description =
'(DEPOSIT)';
1144 $discount->description =
'(EXCESS RECEIVED)';
1146 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1149 $discount->fk_soc = $this->invoice->socid;
1150 $discount->fk_facture_source = $this->invoice->id;
1158 $sql =
'SELECT SUM(pf.amount) as total_payments';
1159 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1160 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1161 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1162 $sql .=
' AND pf.fk_paiement = p.rowid';
1163 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1169 $res = $this->
db->fetch_object(
$resql);
1170 $total_payments = $res->total_payments;
1173 $total_creditnote_and_deposit = 0;
1174 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1175 $sql .=
" re.description, re.fk_facture_source";
1176 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1177 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1180 while ($obj = $this->
db->fetch_object(
$resql)) {
1181 $total_creditnote_and_deposit += $obj->amount_ttc;
1187 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1188 $discount->amount_tva = 0;
1189 $discount->tva_tx = 0;
1191 $result = $discount->create(DolibarrApiAccess::$user);
1197 foreach ($amount_ht as $tva_tx => $xxx) {
1198 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1199 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1200 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1201 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1202 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1203 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1204 $discount->tva_tx = abs($tva_tx);
1206 $result = $discount->create(DolibarrApiAccess::$user);
1214 if (empty($error)) {
1217 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1219 $this->
db->commit();
1221 $this->
db->rollback();
1222 throw new RestException(500,
'Could not set paid');
1225 $this->
db->commit();
1228 $this->
db->rollback();
1229 throw new RestException(500,
'Discount creation error');
1256 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1257 throw new RestException(401);
1260 throw new RestException(400,
'Invoice ID is mandatory');
1262 if (empty($discountid)) {
1263 throw new RestException(400,
'Discount ID is mandatory');
1267 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1270 $result = $this->invoice->fetch($id);
1272 throw new RestException(404,
'Invoice not found');
1275 $result = $this->invoice->insert_discount($discountid);
1277 throw new RestException(405, $this->invoice->error);
1303 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1305 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1306 throw new RestException(401);
1309 throw new RestException(400,
'Invoice ID is mandatory');
1311 if (empty($discountid)) {
1312 throw new RestException(400,
'Credit ID is mandatory');
1316 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1319 $result = $discount->fetch($discountid);
1321 throw new RestException(404,
'Credit not found');
1324 $result = $discount->link_to_invoice(0, $id);
1326 throw new RestException(405, $discount->error);
1349 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1350 throw new RestException(401);
1353 throw new RestException(400,
'Invoice ID is mandatory');
1357 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1360 $result = $this->invoice->fetch($id);
1362 throw new RestException(404,
'Invoice not found');
1365 $result = $this->invoice->getListOfPayments();
1367 throw new RestException(405, $this->invoice->error);
1395 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1399 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1401 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1402 throw new RestException(403);
1405 throw new RestException(400,
'Invoice ID is mandatory');
1409 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1413 if (empty($accountid)) {
1414 throw new RestException(400,
'Account ID is mandatory');
1418 if (empty($paymentid)) {
1419 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1423 $result = $this->invoice->fetch($id);
1425 throw new RestException(404,
'Invoice not found');
1429 $totalpaid = $this->invoice->getSommePaiement();
1430 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1431 $totaldeposits = $this->invoice->getSumDepositsUsed();
1432 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1437 $multicurrency_amounts = array();
1441 $resteapayer =
price2num($resteapayer,
'MT');
1442 $amounts[$id] = -$resteapayer;
1444 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1445 $multicurrency_amounts[$id] = -$newvalue;
1447 $resteapayer =
price2num($resteapayer,
'MT');
1448 $amounts[$id] = $resteapayer;
1450 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1451 $multicurrency_amounts[$id] = $newvalue;
1456 $paymentobj->datepaye = $datepaye;
1457 $paymentobj->amounts = $amounts;
1458 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1459 $paymentobj->paiementid = $paymentid;
1460 $paymentobj->paiementcode =
dol_getIdFromCode($this->
db, $paymentid,
'c_paiement',
'id',
'code', 1);
1461 $paymentobj->num_payment = $num_payment;
1462 $paymentobj->note_private = $comment;
1464 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1465 if ($payment_id < 0) {
1466 $this->
db->rollback();
1467 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1471 $label =
'(CustomerInvoicePayment)';
1473 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1474 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1477 $label =
'(CustomerInvoicePaymentBack)';
1479 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1481 $this->
db->rollback();
1482 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1486 $this->
db->commit();
1516 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1520 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1522 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1523 throw new RestException(403);
1525 foreach ($arrayofamounts as $id => $amount) {
1527 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1530 throw new RestException(403,
'Access not allowed on invoice ID '.$id.
' for login '.DolibarrApiAccess::$user->login);
1535 if (empty($accountid)) {
1536 throw new RestException(400,
'Account ID is mandatory');
1539 if (empty($paymentid)) {
1540 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1546 $multicurrency_amounts = array();
1549 foreach ($arrayofamounts as $id => $amountarray) {
1550 $result = $this->invoice->fetch($id);
1552 $this->
db->rollback();
1553 throw new RestException(404,
'Invoice ID '.$id.
' not found');
1556 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1557 $this->
db->rollback();
1558 throw new RestException(400,
'Payment in both currency '.$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1561 $is_multicurrency = 0;
1562 $total_ttc = $this->invoice->total_ttc;
1564 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1565 $is_multicurrency = 1;
1566 $total_ttc = $this->invoice->multicurrency_total_ttc;
1570 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1571 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1572 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1573 $remainstopay = $amount =
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1575 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1576 $amount =
price2num($amountarray[
"amount"],
'MT');
1579 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1580 $amount =
price2num($amountarray[
"multicurrency_amount"],
'MT');
1583 if ($amount > $remainstopay && !$accepthigherpayment) {
1584 $this->
db->rollback();
1585 throw new RestException(400,
'Payment amount on invoice ID '.$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1592 if ($is_multicurrency) {
1593 $amounts[$id] =
null;
1595 $multicurrency_amounts[$id] = $amount;
1597 $amounts[$id] = $amount;
1599 $multicurrency_amounts[$id] =
null;
1605 $paymentobj->datepaye = $datepaye;
1606 $paymentobj->amounts = $amounts;
1607 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1608 $paymentobj->paiementid = $paymentid;
1609 $paymentobj->paiementcode =
dol_getIdFromCode($this->
db, $paymentid,
'c_paiement',
'id',
'code', 1);
1610 $paymentobj->num_payment = $num_payment;
1611 $paymentobj->note_private = $comment;
1612 $paymentobj->ref_ext = $ref_ext;
1613 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1614 if ($payment_id < 0) {
1615 $this->
db->rollback();
1616 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1619 $label =
'(CustomerInvoicePayment)';
1620 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1621 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1624 $label =
'(CustomerInvoicePaymentBack)';
1626 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1628 $this->
db->rollback();
1629 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1633 $this->
db->commit();
1653 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1655 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1656 throw new RestException(401);
1659 throw new RestException(400,
'Payment ID is mandatory');
1663 $result = $paymentobj->fetch($id);
1666 throw new RestException(404,
'Payment not found');
1669 if (!empty($num_payment)) {
1670 $result = $paymentobj->update_num($num_payment);
1672 throw new RestException(500,
'Error when updating the payment num');
1679 'message' =>
'Payment updated'
1694 $object = parent::_cleanObjectDatas($object);
1696 unset($object->note);
1697 unset($object->address);
1698 unset($object->barcode_type);
1699 unset($object->barcode_type_code);
1700 unset($object->barcode_type_label);
1701 unset($object->barcode_type_coder);
1702 unset($object->canvas);
1718 foreach (Invoices::$FIELDS as $field) {
1719 if (!isset($data[$field])) {
1720 throw new RestException(400,
"$field field missing");
1722 $invoice[$field] = $data[$field];
1761 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1762 throw new RestException(401);
1765 $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1767 throw new RestException(404,
'Template invoice not found');
1771 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1775 if ($contact_list > -1) {
1776 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1777 if (is_array($tmparray)) {
1778 $this->template_invoice->contacts_ids = $tmparray;
1782 $this->template_invoice->fetchObjectLinked();
1797 $object = parent::_cleanObjectDatas($object);
1799 unset($object->note);
1800 unset($object->address);
1801 unset($object->barcode_type);
1802 unset($object->barcode_type_code);
1803 unset($object->barcode_type_label);
1804 unset($object->barcode_type_coder);
1805 unset($object->canvas);