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);
241 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
243 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
244 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
247 $sql .= $this->
db->order($sortfield, $sortorder);
252 $offset = $limit * $page;
254 $sql .= $this->
db->plimit($limit + 1, $offset);
257 $result = $this->
db->query($sql);
260 $num = $this->
db->num_rows($result);
261 $min = min($num, ($limit <= 0 ? $num : $limit));
263 $obj = $this->
db->fetch_object($result);
264 $invoice_static =
new Facture($this->
db);
265 if ($invoice_static->fetch($obj->rowid)) {
267 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
268 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
269 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
270 $invoice_static->remaintopay =
price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits,
'MT');
273 $tmparray = $invoice_static->liste_contact(-1,
'external', 1);
274 if (is_array($tmparray)) {
275 $invoice_static->contacts_ids = $tmparray;
282 throw new RestException(503,
'Error when retrieve invoice list : '.$this->
db->lasterror());
284 if (!count($obj_ret)) {
285 throw new RestException(404,
'No invoice found');
296 public function post($request_data =
null)
298 if (!DolibarrApiAccess::$user->rights->facture->creer) {
299 throw new RestException(401,
"Insuffisant rights");
302 $result = $this->
_validate($request_data);
304 foreach ($request_data as $field => $value) {
305 $this->invoice->$field = $value;
307 if (!array_key_exists(
'date', $request_data)) {
308 $this->invoice->date =
dol_now();
319 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data[
"date_lim_reglement"]) ? 0 : $request_data[
"date_lim_reglement"])) < 0) {
320 throw new RestException(500,
"Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
322 return $this->invoice->id;
340 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
342 if (!DolibarrApiAccess::$user->rights->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');
353 $result = $order->fetch($orderid);
355 throw new RestException(404,
'Order not found');
358 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
360 throw new RestException(405, $this->invoice->error);
362 $this->invoice->fetchObjectLinked();
376 if (!DolibarrApiAccess::$user->rights->facture->lire) {
377 throw new RestException(401);
380 $result = $this->invoice->fetch($id);
382 throw new RestException(404,
'Invoice not found');
386 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
388 $this->invoice->getLinesArray();
390 foreach ($this->invoice->lines as $line) {
410 public function putLine($id, $lineid, $request_data =
null)
412 if (!DolibarrApiAccess::$user->rights->facture->creer) {
413 throw new RestException(401);
416 $result = $this->invoice->fetch($id);
418 throw new RestException(404,
'Invoice not found');
422 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
425 $request_data = (object) $request_data;
427 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
428 $request_data->label =
sanitizeVal($request_data->label);
430 $updateRes = $this->invoice->updateline(
433 $request_data->subprice,
435 $request_data->remise_percent,
436 $request_data->date_start,
437 $request_data->date_end,
438 $request_data->tva_tx,
439 $request_data->localtax1_tx,
440 $request_data->localtax2_tx,
441 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
442 $request_data->info_bits,
443 $request_data->product_type,
444 $request_data->fk_parent_line,
446 $request_data->fk_fournprice,
447 $request_data->pa_ht,
448 $request_data->label,
449 $request_data->special_code,
450 $request_data->array_options,
451 $request_data->situation_percent,
452 $request_data->fk_unit,
453 $request_data->multicurrency_subprice,
455 $request_data->ref_ext,
459 if ($updateRes > 0) {
460 $result = $this->
get($id);
461 unset($result->line);
464 throw new RestException(304, $this->invoice->error);
483 if (!DolibarrApiAccess::$user->rights->facture->creer) {
484 throw new RestException(401);
487 $result = $this->invoice->fetch($id);
490 throw new RestException(404,
'Invoice not found');
493 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
494 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
498 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
501 $result = $this->invoice->add_contact($contactid, $type,
'external');
504 throw new RestException(500,
'Error when added the contact');
510 'message' =>
'Contact linked to the invoice'
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');
576 if (!DolibarrApiAccess::$user->rights->facture->creer) {
577 throw new RestException(401);
579 if (empty($lineid)) {
580 throw new RestException(400,
'Line ID is mandatory');
584 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
587 $result = $this->invoice->fetch($id);
589 throw new RestException(404,
'Invoice not found');
592 $updateRes = $this->invoice->deleteline($lineid, $id);
593 if ($updateRes > 0) {
594 return $this->
get($id);
596 throw new RestException(405, $this->invoice->error);
607 public function put($id, $request_data =
null)
609 if (!DolibarrApiAccess::$user->rights->facture->creer) {
610 throw new RestException(401);
613 $result = $this->invoice->fetch($id);
615 throw new RestException(404,
'Invoice not found');
619 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
622 foreach ($request_data as $field => $value) {
623 if ($field ==
'id') {
626 $this->invoice->$field = $value;
630 if (!empty($this->invoice->fk_account)) {
631 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
632 throw new RestException(400, $this->invoice->error);
636 if ($this->invoice->update(DolibarrApiAccess::$user)) {
637 return $this->
get($id);
649 public function delete($id)
651 if (!DolibarrApiAccess::$user->rights->facture->supprimer) {
652 throw new RestException(401);
654 $result = $this->invoice->fetch($id);
656 throw new RestException(404,
'Invoice not found');
660 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
663 $result = $this->invoice->delete(DolibarrApiAccess::$user);
665 throw new RestException(500,
'Error when deleting invoice');
666 } elseif ($result == 0) {
667 throw new RestException(403,
'Invoice not erasable');
673 'message' =>
'Invoice deleted'
701 public function postLine($id, $request_data =
null)
703 if (!DolibarrApiAccess::$user->rights->facture->creer) {
704 throw new RestException(401);
707 $result = $this->invoice->fetch($id);
709 throw new RestException(404,
'Invoice not found');
713 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
716 $request_data = (object) $request_data;
718 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
719 $request_data->label =
sanitizeVal($request_data->label);
722 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
723 $request_data->fk_parent_line = 0;
727 $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);
728 $pa_ht = $marginInfos[0];
730 $updateRes = $this->invoice->addline(
732 $request_data->subprice,
734 $request_data->tva_tx,
735 $request_data->localtax1_tx,
736 $request_data->localtax2_tx,
737 $request_data->fk_product,
738 $request_data->remise_percent,
739 $request_data->date_start,
740 $request_data->date_end,
741 $request_data->fk_code_ventilation,
742 $request_data->info_bits,
743 $request_data->fk_remise_except,
744 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
745 $request_data->subprice,
746 $request_data->product_type,
748 $request_data->special_code,
749 $request_data->origin,
750 $request_data->origin_id,
751 $request_data->fk_parent_line,
752 empty($request_data->fk_fournprice) ?
null:$request_data->fk_fournprice,
754 $request_data->label,
755 $request_data->array_options,
756 $request_data->situation_percent,
757 $request_data->fk_prev_id,
758 $request_data->fk_unit,
760 $request_data->ref_ext
763 if ($updateRes < 0) {
764 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
789 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
791 if (!DolibarrApiAccess::$user->rights->facture->creer) {
792 throw new RestException(401);
794 $result = $this->invoice->fetch($id);
796 throw new RestException(404,
'Invoice not found');
800 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
803 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
805 throw new RestException(500,
'Error : '.$this->invoice->error);
808 $result = $this->invoice->fetch($id);
810 throw new RestException(404,
'Invoice not found');
814 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
839 if (!DolibarrApiAccess::$user->rights->facture->creer) {
840 throw new RestException(401);
842 $result = $this->invoice->fetch($id);
844 throw new RestException(404,
'Invoice not found');
848 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
851 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
853 throw new RestException(304,
'Nothing done.');
856 throw new RestException(500,
'Error : '.$this->invoice->error);
859 $result = $this->invoice->fetch($id);
861 throw new RestException(404,
'Invoice not found');
865 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
888 public function validate($id, $idwarehouse = 0, $notrigger = 0)
890 if (!DolibarrApiAccess::$user->rights->facture->creer) {
891 throw new RestException(401);
893 $result = $this->invoice->fetch($id);
895 throw new RestException(404,
'Invoice not found');
899 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
902 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
904 throw new RestException(304,
'Error nothing done. May be object is already validated');
907 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
910 $result = $this->invoice->fetch($id);
912 throw new RestException(404,
'Invoice not found');
916 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
937 public function settopaid($id, $close_code =
'', $close_note =
'')
939 if (!DolibarrApiAccess::$user->rights->facture->creer) {
940 throw new RestException(401);
942 $result = $this->invoice->fetch($id);
944 throw new RestException(404,
'Invoice not found');
948 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
951 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
953 throw new RestException(304,
'Error nothing done. May be object is already validated');
956 throw new RestException(500,
'Error : '.$this->invoice->error);
960 $result = $this->invoice->fetch($id);
962 throw new RestException(404,
'Invoice not found');
966 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
988 if (!DolibarrApiAccess::$user->rights->facture->creer) {
989 throw new RestException(401);
991 $result = $this->invoice->fetch($id);
993 throw new RestException(404,
'Invoice not found');
997 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1000 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1002 throw new RestException(304,
'Nothing done');
1005 throw new RestException(500,
'Error : '.$this->invoice->error);
1009 $result = $this->invoice->fetch($id);
1011 throw new RestException(404,
'Invoice not found');
1015 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1031 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1033 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1034 throw new RestException(401);
1037 $result = $this->invoice->fetch($id);
1039 throw new RestException(404,
'Invoice not found');
1043 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1047 $result = $discountcheck->fetch(0, $this->invoice->id);
1050 throw new RestException(404,
'Discount not found');
1053 throw new RestException(500, $discountcheck->error);
1056 return parent::_cleanObjectDatas($discountcheck);
1074 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1076 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1077 throw new RestException(401);
1080 $result = $this->invoice->fetch($id);
1082 throw new RestException(404,
'Invoice not found');
1086 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1089 if ($this->invoice->paye) {
1090 throw new RestException(500,
'Alreay paid');
1093 $this->invoice->fetch($id);
1094 $this->invoice->fetch_thirdparty();
1098 $result = $discountcheck->fetch(0, $this->invoice->id);
1110 $amount_ht = $amount_tva = $amount_ttc = array();
1111 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1115 foreach ($this->invoice->lines as $line) {
1116 if ($line->product_type < 9 && $line->total_ht != 0) {
1118 $amount_ht[$line->tva_tx] += $line->total_ht;
1119 $amount_tva[$line->tva_tx] += $line->total_tva;
1120 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1121 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1122 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1123 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1131 $discount->description =
'(CREDIT_NOTE)';
1133 $discount->description =
'(DEPOSIT)';
1135 $discount->description =
'(EXCESS RECEIVED)';
1137 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1140 $discount->fk_soc = $this->invoice->socid;
1141 $discount->fk_facture_source = $this->invoice->id;
1149 $sql =
'SELECT SUM(pf.amount) as total_payments';
1150 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1151 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1152 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1153 $sql .=
' AND pf.fk_paiement = p.rowid';
1154 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1160 $res = $this->
db->fetch_object(
$resql);
1161 $total_payments = $res->total_payments;
1164 $total_creditnote_and_deposit = 0;
1165 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1166 $sql .=
" re.description, re.fk_facture_source";
1167 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1168 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1171 while ($obj = $this->
db->fetch_object(
$resql)) {
1172 $total_creditnote_and_deposit += $obj->amount_ttc;
1178 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1179 $discount->amount_tva = 0;
1180 $discount->tva_tx = 0;
1182 $result = $discount->create(DolibarrApiAccess::$user);
1188 foreach ($amount_ht as $tva_tx => $xxx) {
1189 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1190 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1191 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1192 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1193 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1194 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1195 $discount->tva_tx = abs($tva_tx);
1197 $result = $discount->create(DolibarrApiAccess::$user);
1205 if (empty($error)) {
1208 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1210 $this->
db->commit();
1212 $this->
db->rollback();
1213 throw new RestException(500,
'Could not set paid');
1216 $this->
db->commit();
1219 $this->
db->rollback();
1220 throw new RestException(500,
'Discount creation error');
1245 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1246 throw new RestException(401);
1249 throw new RestException(400,
'Invoice ID is mandatory');
1251 if (empty($discountid)) {
1252 throw new RestException(400,
'Discount ID is mandatory');
1256 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1259 $result = $this->invoice->fetch($id);
1261 throw new RestException(404,
'Invoice not found');
1264 $result = $this->invoice->insert_discount($discountid);
1266 throw new RestException(405, $this->invoice->error);
1290 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1292 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1293 throw new RestException(401);
1296 throw new RestException(400,
'Invoice ID is mandatory');
1298 if (empty($discountid)) {
1299 throw new RestException(400,
'Credit ID is mandatory');
1303 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1306 $result = $discount->fetch($discountid);
1308 throw new RestException(404,
'Credit not found');
1311 $result = $discount->link_to_invoice(0, $id);
1313 throw new RestException(405, $discount->error);
1335 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1336 throw new RestException(401);
1339 throw new RestException(400,
'Invoice ID is mandatory');
1343 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1346 $result = $this->invoice->fetch($id);
1348 throw new RestException(404,
'Invoice not found');
1351 $result = $this->invoice->getListOfPayments();
1353 throw new RestException(405, $this->invoice->error);
1381 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1383 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1385 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1386 throw new RestException(403);
1389 throw new RestException(400,
'Invoice ID is mandatory');
1393 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1397 if (empty($accountid)) {
1398 throw new RestException(400,
'Account ID is mandatory');
1402 if (empty($paymentid)) {
1403 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1407 $result = $this->invoice->fetch($id);
1409 throw new RestException(404,
'Invoice not found');
1413 $totalpaid = $this->invoice->getSommePaiement();
1414 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1415 $totaldeposits = $this->invoice->getSumDepositsUsed();
1416 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1421 $multicurrency_amounts = array();
1425 $resteapayer =
price2num($resteapayer,
'MT');
1426 $amounts[$id] = -$resteapayer;
1428 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1429 $multicurrency_amounts[$id] = -$newvalue;
1431 $resteapayer =
price2num($resteapayer,
'MT');
1432 $amounts[$id] = $resteapayer;
1434 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1435 $multicurrency_amounts[$id] = $newvalue;
1440 $paymentobj->datepaye = $datepaye;
1441 $paymentobj->amounts = $amounts;
1442 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1443 $paymentobj->paiementid = $paymentid;
1444 $paymentobj->paiementcode =
dol_getIdFromCode($this->
db, $paymentid,
'c_paiement',
'id',
'code', 1);
1445 $paymentobj->num_payment = $num_payment;
1446 $paymentobj->note_private = $comment;
1448 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1449 if ($payment_id < 0) {
1450 $this->
db->rollback();
1451 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1455 $label =
'(CustomerInvoicePayment)';
1457 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1458 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1461 $label =
'(CustomerInvoicePaymentBack)';
1463 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1465 $this->
db->rollback();
1466 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1470 $this->
db->commit();
1501 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1503 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1505 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1506 throw new RestException(403);
1508 foreach ($arrayofamounts as $id => $amount) {
1510 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1513 throw new RestException(403,
'Access not allowed on invoice ID '.$id.
' for login '.DolibarrApiAccess::$user->login);
1518 if (empty($accountid)) {
1519 throw new RestException(400,
'Account ID is mandatory');
1522 if (empty($paymentid)) {
1523 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1529 $multicurrency_amounts = array();
1532 foreach ($arrayofamounts as $id => $amountarray) {
1533 $result = $this->invoice->fetch($id);
1535 $this->
db->rollback();
1536 throw new RestException(404,
'Invoice ID '.$id.
' not found');
1539 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1540 $this->
db->rollback();
1541 throw new RestException(400,
'Payment in both currency '.$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1544 $is_multicurrency = 0;
1545 $total_ttc = $this->invoice->total_ttc;
1547 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1548 $is_multicurrency = 1;
1549 $total_ttc = $this->invoice->multicurrency_total_ttc;
1553 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1554 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1555 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1556 $remainstopay = $amount =
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1558 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1559 $amount =
price2num($amountarray[
"amount"],
'MT');
1562 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1563 $amount =
price2num($amountarray[
"multicurrency_amount"],
'MT');
1566 if ($amount > $remainstopay && !$accepthigherpayment) {
1567 $this->
db->rollback();
1568 throw new RestException(400,
'Payment amount on invoice ID '.$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1575 if ($is_multicurrency) {
1576 $amounts[$id] =
null;
1578 $multicurrency_amounts[$id] = $amount;
1580 $amounts[$id] = $amount;
1582 $multicurrency_amounts[$id] =
null;
1588 $paymentobj->datepaye = $datepaye;
1589 $paymentobj->amounts = $amounts;
1590 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1591 $paymentobj->paiementid = $paymentid;
1592 $paymentobj->paiementcode =
dol_getIdFromCode($this->
db, $paymentid,
'c_paiement',
'id',
'code', 1);
1593 $paymentobj->num_payment = $num_payment;
1594 $paymentobj->note_private = $comment;
1595 $paymentobj->ref_ext = $ref_ext;
1596 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1597 if ($payment_id < 0) {
1598 $this->
db->rollback();
1599 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1602 $label =
'(CustomerInvoicePayment)';
1603 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1604 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1607 $label =
'(CustomerInvoicePaymentBack)';
1609 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1611 $this->
db->rollback();
1612 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1616 $this->
db->commit();
1637 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1639 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1640 throw new RestException(401);
1643 throw new RestException(400,
'Payment ID is mandatory');
1647 $result = $paymentobj->fetch($id);
1650 throw new RestException(404,
'Payment not found');
1653 if (!empty($num_payment)) {
1654 $result = $paymentobj->update_num($num_payment);
1656 throw new RestException(500,
'Error when updating the payment num');
1663 'message' =>
'Payment updated'
1678 $object = parent::_cleanObjectDatas($object);
1680 unset($object->note);
1681 unset($object->address);
1682 unset($object->barcode_type);
1683 unset($object->barcode_type_code);
1684 unset($object->barcode_type_label);
1685 unset($object->barcode_type_coder);
1686 unset($object->canvas);
1702 foreach (Invoices::$FIELDS as $field) {
1703 if (!isset($data[$field])) {
1704 throw new RestException(400,
"$field field missing");
1706 $invoice[$field] = $data[$field];
1745 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1746 throw new RestException(401);
1749 $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1751 throw new RestException(404,
'Template invoice not found');
1755 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1759 if ($contact_list > -1) {
1760 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1761 if (is_array($tmparray)) {
1762 $this->template_invoice->contacts_ids = $tmparray;
1766 $this->template_invoice->fetchObjectLinked();
1781 $object = parent::_cleanObjectDatas($object);
1783 unset($object->note);
1784 unset($object->address);
1785 unset($object->barcode_type);
1786 unset($object->barcode_type_code);
1787 unset($object->barcode_type_label);
1788 unset($object->barcode_type_coder);
1789 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.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
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...
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.
isModEnabled($module)
Is Dolibarr module enabled.
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.
$conf db
API class for accounts.