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);
340 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
342 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
343 throw new RestException(401);
345 if (!DolibarrApiAccess::$user->rights->facture->creer) {
346 throw new RestException(401);
348 if (empty($orderid)) {
349 throw new RestException(400,
'Order ID is mandatory');
352 throw new RestException(403,
'Access not allowed on order for login '.DolibarrApiAccess::$user->login);
356 $result = $order->fetch($orderid);
358 throw new RestException(404,
'Order not found');
361 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
363 throw new RestException(405, $this->invoice->error);
365 $this->invoice->fetchObjectLinked();
379 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
380 throw new RestException(401);
383 $result = $this->invoice->fetch($id);
385 throw new RestException(404,
'Invoice not found');
389 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
391 $this->invoice->getLinesArray();
393 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);
486 if (!DolibarrApiAccess::$user->rights->facture->creer) {
487 throw new RestException(401);
490 $result = $this->invoice->fetch($id);
493 throw new RestException(404,
'Invoice not found');
496 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
497 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
501 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
504 $result = $this->invoice->add_contact($contactid, $type,
'external');
507 throw new RestException(500,
'Error when added the contact');
513 'message' =>
'Contact linked to the invoice'
534 if (!DolibarrApiAccess::$user->rights->facture->creer) {
535 throw new RestException(401);
538 $result = $this->invoice->fetch($id);
541 throw new RestException(404,
'Invoice not found');
545 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
548 $contacts = $this->invoice->liste_contact();
550 foreach ($contacts as $contact) {
551 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
552 $result = $this->invoice->delete_contact($contact[
'rowid']);
555 throw new RestException(500,
'Error when deleted the contact');
579 if (!DolibarrApiAccess::$user->rights->facture->creer) {
580 throw new RestException(401);
582 if (empty($lineid)) {
583 throw new RestException(400,
'Line ID is mandatory');
587 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
590 $result = $this->invoice->fetch($id);
592 throw new RestException(404,
'Invoice not found');
595 $updateRes = $this->invoice->deleteline($lineid, $id);
596 if ($updateRes > 0) {
597 return $this->
get($id);
599 throw new RestException(405, $this->invoice->error);
610 public function put($id, $request_data =
null)
612 if (!DolibarrApiAccess::$user->rights->facture->creer) {
613 throw new RestException(401);
616 $result = $this->invoice->fetch($id);
618 throw new RestException(404,
'Invoice not found');
622 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
625 foreach ($request_data as $field => $value) {
626 if ($field ==
'id') {
629 if ($field ==
'array_options' && is_array($value)) {
630 foreach ($value as $index => $val) {
631 $this->invoice->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->invoice);
635 $this->invoice->$field = $value;
639 if (!empty($this->invoice->fk_account)) {
640 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
641 throw new RestException(400, $this->invoice->error);
645 if ($this->invoice->update(DolibarrApiAccess::$user) > 0) {
646 return $this->
get($id);
648 throw new RestException(500, $this->invoice->error);
658 public function delete($id)
660 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'supprimer')) {
661 throw new RestException(401);
663 $result = $this->invoice->fetch($id);
665 throw new RestException(404,
'Invoice not found');
669 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
672 $result = $this->invoice->delete(DolibarrApiAccess::$user);
674 throw new RestException(500,
'Error when deleting invoice');
675 } elseif ($result == 0) {
676 throw new RestException(403,
'Invoice not erasable');
682 'message' =>
'Invoice deleted'
710 public function postLine($id, $request_data =
null)
712 if (!DolibarrApiAccess::$user->rights->facture->creer) {
713 throw new RestException(401);
716 $result = $this->invoice->fetch($id);
718 throw new RestException(404,
'Invoice not found');
722 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
725 $request_data = (object) $request_data;
727 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
728 $request_data->label =
sanitizeVal($request_data->label);
731 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
732 $request_data->fk_parent_line = 0;
736 $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);
737 $pa_ht = $marginInfos[0];
739 $updateRes = $this->invoice->addline(
741 $request_data->subprice,
743 $request_data->tva_tx,
744 $request_data->localtax1_tx,
745 $request_data->localtax2_tx,
746 $request_data->fk_product,
747 $request_data->remise_percent,
748 $request_data->date_start,
749 $request_data->date_end,
750 $request_data->fk_code_ventilation,
751 $request_data->info_bits,
752 $request_data->fk_remise_except,
753 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
754 $request_data->subprice,
755 $request_data->product_type,
757 $request_data->special_code,
758 $request_data->origin,
759 $request_data->origin_id,
760 $request_data->fk_parent_line,
761 empty($request_data->fk_fournprice) ?
null:$request_data->fk_fournprice,
763 $request_data->label,
764 $request_data->array_options,
765 $request_data->situation_percent,
766 $request_data->fk_prev_id,
767 $request_data->fk_unit,
769 $request_data->ref_ext
772 if ($updateRes < 0) {
773 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
798 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
800 if (!DolibarrApiAccess::$user->rights->facture->creer) {
801 throw new RestException(401);
803 $result = $this->invoice->fetch($id);
805 throw new RestException(404,
'Invoice not found');
809 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
812 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
814 throw new RestException(500,
'Error : '.$this->invoice->error);
817 $result = $this->invoice->fetch($id);
819 throw new RestException(404,
'Invoice not found');
823 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
848 if (!DolibarrApiAccess::$user->rights->facture->creer) {
849 throw new RestException(401);
851 $result = $this->invoice->fetch($id);
853 throw new RestException(404,
'Invoice not found');
857 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
860 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
862 throw new RestException(304,
'Nothing done.');
865 throw new RestException(500,
'Error : '.$this->invoice->error);
868 $result = $this->invoice->fetch($id);
870 throw new RestException(404,
'Invoice not found');
874 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
897 public function validate($id, $idwarehouse = 0, $notrigger = 0)
899 if (!DolibarrApiAccess::$user->rights->facture->creer) {
900 throw new RestException(401);
902 $result = $this->invoice->fetch($id);
904 throw new RestException(404,
'Invoice not found');
908 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
911 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
913 throw new RestException(304,
'Error nothing done. May be object is already validated');
916 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
919 $result = $this->invoice->fetch($id);
921 throw new RestException(404,
'Invoice not found');
925 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
946 public function settopaid($id, $close_code =
'', $close_note =
'')
948 if (!DolibarrApiAccess::$user->rights->facture->creer) {
949 throw new RestException(401);
951 $result = $this->invoice->fetch($id);
953 throw new RestException(404,
'Invoice not found');
957 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
960 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
962 throw new RestException(304,
'Error nothing done. May be object is already validated');
965 throw new RestException(500,
'Error : '.$this->invoice->error);
969 $result = $this->invoice->fetch($id);
971 throw new RestException(404,
'Invoice not found');
975 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
997 if (!DolibarrApiAccess::$user->rights->facture->creer) {
998 throw new RestException(401);
1000 $result = $this->invoice->fetch($id);
1002 throw new RestException(404,
'Invoice not found');
1006 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1009 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1011 throw new RestException(304,
'Nothing done');
1014 throw new RestException(500,
'Error : '.$this->invoice->error);
1018 $result = $this->invoice->fetch($id);
1020 throw new RestException(404,
'Invoice not found');
1024 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->hasRight(
'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').
')';
1164 $resql = $this->db->query($sql);
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);
1178 $resql = $this->db->query($sql);
1179 if (!empty($resql)) {
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');
1254 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1255 throw new RestException(401);
1258 throw new RestException(400,
'Invoice ID is mandatory');
1260 if (empty($discountid)) {
1261 throw new RestException(400,
'Discount ID is mandatory');
1265 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1268 $result = $this->invoice->fetch($id);
1270 throw new RestException(404,
'Invoice not found');
1273 $result = $this->invoice->insert_discount($discountid);
1275 throw new RestException(405, $this->invoice->error);
1299 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1301 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1302 throw new RestException(401);
1305 throw new RestException(400,
'Invoice ID is mandatory');
1307 if (empty($discountid)) {
1308 throw new RestException(400,
'Credit ID is mandatory');
1312 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1315 $result = $discount->fetch($discountid);
1317 throw new RestException(404,
'Credit not found');
1320 $result = $discount->link_to_invoice(0, $id);
1322 throw new RestException(405, $discount->error);
1344 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1345 throw new RestException(401);
1348 throw new RestException(400,
'Invoice ID is mandatory');
1352 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1355 $result = $this->invoice->fetch($id);
1357 throw new RestException(404,
'Invoice not found');
1360 $result = $this->invoice->getListOfPayments();
1362 throw new RestException(405, $this->invoice->error);
1390 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1392 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1394 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1395 throw new RestException(403);
1398 throw new RestException(400,
'Invoice ID is mandatory');
1402 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1405 if (isModEnabled(
"banque")) {
1406 if (empty($accountid)) {
1407 throw new RestException(400,
'Account ID is mandatory');
1411 if (empty($paymentid)) {
1412 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1416 $result = $this->invoice->fetch($id);
1418 throw new RestException(404,
'Invoice not found');
1422 $totalpaid = $this->invoice->getSommePaiement();
1423 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1424 $totaldeposits = $this->invoice->getSumDepositsUsed();
1425 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1430 $multicurrency_amounts = array();
1434 $resteapayer =
price2num($resteapayer,
'MT');
1435 $amounts[$id] = -$resteapayer;
1437 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1438 $multicurrency_amounts[$id] = -$newvalue;
1440 $resteapayer =
price2num($resteapayer,
'MT');
1441 $amounts[$id] = $resteapayer;
1443 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1444 $multicurrency_amounts[$id] = $newvalue;
1448 $paymentobj =
new Paiement($this->db);
1449 $paymentobj->datepaye = $datepaye;
1450 $paymentobj->amounts = $amounts;
1451 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1452 $paymentobj->paiementid = $paymentid;
1453 $paymentobj->paiementcode =
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1454 $paymentobj->num_payment = $num_payment;
1455 $paymentobj->note_private = $comment;
1457 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1458 if ($payment_id < 0) {
1459 $this->db->rollback();
1460 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1463 if (isModEnabled(
"banque")) {
1464 $label =
'(CustomerInvoicePayment)';
1466 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1467 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1470 $label =
'(CustomerInvoicePaymentBack)';
1472 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1474 $this->db->rollback();
1475 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1479 $this->db->commit();
1510 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1512 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1514 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1515 throw new RestException(403);
1517 foreach ($arrayofamounts as $id => $amount) {
1519 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1522 throw new RestException(403,
'Access not allowed on invoice ID '.$id.
' for login '.DolibarrApiAccess::$user->login);
1526 if (isModEnabled(
"banque")) {
1527 if (empty($accountid)) {
1528 throw new RestException(400,
'Account ID is mandatory');
1531 if (empty($paymentid)) {
1532 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1538 $multicurrency_amounts = array();
1541 foreach ($arrayofamounts as $id => $amountarray) {
1542 $result = $this->invoice->fetch($id);
1544 $this->db->rollback();
1545 throw new RestException(404,
'Invoice ID '.$id.
' not found');
1548 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1549 $this->db->rollback();
1550 throw new RestException(400,
'Payment in both currency '.$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1553 $is_multicurrency = 0;
1554 $total_ttc = $this->invoice->total_ttc;
1556 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1557 $is_multicurrency = 1;
1558 $total_ttc = $this->invoice->multicurrency_total_ttc;
1562 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1563 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1564 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1565 $remainstopay = $amount =
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1567 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1568 $amount =
price2num($amountarray[
"amount"],
'MT');
1571 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1572 $amount =
price2num($amountarray[
"multicurrency_amount"],
'MT');
1575 if (abs($amount) > abs($remainstopay) && !$accepthigherpayment) {
1576 $this->db->rollback();
1577 throw new RestException(400,
'Payment amount on invoice ID '.$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1581 $amount = - abs($amount);
1584 if ($is_multicurrency) {
1585 $amounts[$id] =
null;
1587 $multicurrency_amounts[$id] = $amount;
1589 $amounts[$id] = $amount;
1591 $multicurrency_amounts[$id] =
null;
1596 $paymentobj =
new Paiement($this->db);
1597 $paymentobj->datepaye = $datepaye;
1598 $paymentobj->amounts = $amounts;
1599 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1600 $paymentobj->paiementid = $paymentid;
1601 $paymentobj->paiementcode =
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1602 $paymentobj->num_payment = $num_payment;
1603 $paymentobj->note_private = $comment;
1604 $paymentobj->ref_ext = $ref_ext;
1605 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1606 if ($payment_id < 0) {
1607 $this->db->rollback();
1608 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1610 if (isModEnabled(
"banque")) {
1611 $label =
'(CustomerInvoicePayment)';
1612 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1613 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1616 $label =
'(CustomerInvoicePaymentBack)';
1618 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1620 $this->db->rollback();
1621 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1625 $this->db->commit();
1646 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1648 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1649 throw new RestException(401);
1652 throw new RestException(400,
'Payment ID is mandatory');
1655 $paymentobj =
new Paiement($this->db);
1656 $result = $paymentobj->fetch($id);
1659 throw new RestException(404,
'Payment not found');
1662 if (!empty($num_payment)) {
1663 $result = $paymentobj->update_num($num_payment);
1665 throw new RestException(500,
'Error when updating the payment num');
1672 'message' =>
'Payment updated'
1687 $object = parent::_cleanObjectDatas($object);
1689 unset($object->note);
1690 unset($object->address);
1691 unset($object->barcode_type);
1692 unset($object->barcode_type_code);
1693 unset($object->barcode_type_label);
1694 unset($object->barcode_type_coder);
1695 unset($object->canvas);
1711 foreach (Invoices::$FIELDS as $field) {
1712 if (!isset($data[$field])) {
1713 throw new RestException(400,
"$field field missing");
1715 $invoice[$field] = $data[$field];
1754 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1755 throw new RestException(401);
1758 $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1760 throw new RestException(404,
'Template invoice not found');
1764 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1768 if ($contact_list > -1) {
1769 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1770 if (is_array($tmparray)) {
1771 $this->template_invoice->contacts_ids = $tmparray;
1775 $this->template_invoice->fetchObjectLinked();
1790 $object = parent::_cleanObjectDatas($object);
1792 unset($object->note);
1793 unset($object->address);
1794 unset($object->barcode_type);
1795 unset($object->barcode_type_code);
1796 unset($object->barcode_type_label);
1797 unset($object->barcode_type_coder);
1798 unset($object->canvas);
Class to manage customers orders.
Class to manage absolute discounts.
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.