22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture-rec.class.php';
39 public static $FIELDS = array(
51 private $template_invoice;
61 $this->invoice =
new Facture($this->db);
62 $this->template_invoice =
new FactureRec($this->db);
76 public function get(
$id, $contact_list = 1)
78 return $this->
_fetch(
$id,
'',
'', $contact_list);
94 public function getByRef($ref, $contact_list = 1)
96 return $this->
_fetch(0, $ref,
'', $contact_list);
114 return $this->
_fetch(0,
'', $ref_ext, $contact_list);
130 private function _fetch(
$id, $ref =
'', $ref_ext =
'', $contact_list = 1)
132 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
133 throw new RestException(403);
136 $result = $this->invoice->fetch(
$id, $ref, $ref_ext);
138 throw new RestException(404,
'Invoice not found');
142 $this->invoice->totalpaid = $this->invoice->getSommePaiement();
143 $this->invoice->totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
144 $this->invoice->totaldeposits = $this->invoice->getSumDepositsUsed();
145 $this->invoice->remaintopay =
price2num($this->invoice->total_ttc - $this->invoice->totalpaid - $this->invoice->totalcreditnotes - $this->invoice->totaldeposits,
'MT');
148 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
152 $this->invoice->getListIdAvoirFromInvoice();
155 if ($contact_list > -1) {
156 $tmparray = $this->invoice->liste_contact(-1,
'external', $contact_list);
157 if (is_array($tmparray)) {
158 $this->invoice->contacts_ids = $tmparray;
160 $tmparray = $this->invoice->liste_contact(-1,
'internal', $contact_list);
161 if (is_array($tmparray)) {
162 $this->invoice->contacts_ids = $tmparray;
166 $this->invoice->fetchObjectLinked();
169 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
170 $this->invoice->online_payment_url = getOnlinePaymentUrl(0,
'invoice', $this->invoice->ref);
197 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false, $loadlinkedobjects = 0)
199 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
200 throw new RestException(403);
206 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
210 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
211 $search_sale = DolibarrApiAccess::$user->id;
214 $sql =
"SELECT t.rowid";
215 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture AS t";
216 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_extrafields AS ef ON (ef.fk_object = t.rowid)";
217 $sql .=
' WHERE t.entity IN ('.getEntity(
'invoice').
')';
219 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
222 if ($search_sale && $search_sale !=
'-1') {
223 if ($search_sale == -2) {
224 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
225 } elseif ($search_sale > 0) {
226 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
230 if ($status ==
'draft') {
231 $sql .=
" AND t.fk_statut IN (0)";
233 if ($status ==
'unpaid') {
234 $sql .=
" AND t.fk_statut IN (1)";
236 if ($status ==
'paid') {
237 $sql .=
" AND t.fk_statut IN (2)";
239 if ($status ==
'cancelled') {
240 $sql .=
" AND t.fk_statut IN (3)";
247 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
252 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
254 $sql .= $this->db->order($sortfield, $sortorder);
259 $offset = $limit * $page;
261 $sql .= $this->db->plimit($limit + 1, $offset);
264 $result = $this->db->query($sql);
267 $num = $this->db->num_rows($result);
268 $min = min($num, ($limit <= 0 ? $num : $limit));
270 $obj = $this->db->fetch_object($result);
271 $invoice_static =
new Facture($this->db);
272 if ($invoice_static->fetch($obj->rowid) > 0) {
274 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
275 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
276 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
277 $invoice_static->remaintopay =
price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits,
'MT');
280 $invoice_static->getListIdAvoirFromInvoice();
283 $tmparray = $invoice_static->liste_contact(-1,
'external', 1);
284 if (is_array($tmparray)) {
285 $invoice_static->contacts_ids = $tmparray;
288 if ($loadlinkedobjects) {
290 $invoice_static->fetchObjectLinked();
294 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
295 $invoice_static->online_payment_url = getOnlinePaymentUrl(0,
'invoice', $invoice_static->ref);
302 throw new RestException(503,
'Error when retrieve invoice list : '.$this->db->lasterror());
306 if ($pagination_data) {
307 $totalsResult = $this->db->query($sqlTotals);
308 $total = $this->db->fetch_object($totalsResult)->total;
313 $obj_ret[
'data'] = $tmp;
314 $obj_ret[
'pagination'] = [
315 'total' => (int) $total,
317 'page_count' => ceil((
int) $total / $limit),
333 public function post($request_data =
null)
335 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
336 throw new RestException(403,
"Insuffisant rights");
339 if (!is_array($request_data)) {
340 $request_data = array();
346 foreach ($request_data as $field => $value) {
347 if ($field ===
'caller') {
349 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
353 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
355 if (!array_key_exists(
'date', $request_data)) {
356 $this->invoice->date =
dol_now();
367 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data[
"date_lim_reglement"]) ? 0 : $request_data[
"date_lim_reglement"])) < 0) {
368 throw new RestException(500,
"Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
370 return ((
int) $this->invoice->id);
389 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
391 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
392 throw new RestException(403);
394 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
395 throw new RestException(403);
397 if (empty($orderid)) {
398 throw new RestException(400,
'Order ID is mandatory');
401 throw new RestException(403,
'Access not allowed on order for login '.DolibarrApiAccess::$user->login);
405 $result = $order->fetch($orderid);
407 throw new RestException(404,
'Order not found');
410 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
412 throw new RestException(405, $this->invoice->error);
414 $this->invoice->fetchObjectLinked();
433 require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
435 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
436 throw new RestException(403);
438 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
439 throw new RestException(403);
441 if (empty($contractid)) {
442 throw new RestException(400,
'Contract ID is mandatory');
445 $contract =
new Contrat($this->db);
446 $result = $contract->fetch($contractid);
448 throw new RestException(404,
'Contract not found');
451 $result = $this->invoice->createFromContract($contract, DolibarrApiAccess::$user);
453 throw new RestException(405, $this->invoice->error);
455 $this->invoice->fetchObjectLinked();
471 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
472 throw new RestException(403);
475 $result = $this->invoice->fetch(
$id);
477 throw new RestException(404,
'Invoice not found');
481 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
483 $this->invoice->getLinesArray();
485 foreach ($this->invoice->lines as $line) {
509 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
510 throw new RestException(403);
513 $result = $this->invoice->fetch(
$id);
515 throw new RestException(404,
'Invoice not found');
519 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
522 $request_data = (object) $request_data;
524 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
525 $request_data->label =
sanitizeVal($request_data->label);
528 $result = $invoiceline->fetch($lineid);
530 throw new RestException(404,
'Invoice line not found');
533 if ($invoiceline->fk_facture !=
$id) {
534 throw new RestException(403,
'Line does not belong to this invoice');
537 $updateRes = $this->invoice->updateline(
540 $request_data->subprice,
542 $request_data->remise_percent,
543 $request_data->date_start,
544 $request_data->date_end,
545 $request_data->tva_tx,
546 $request_data->localtax1_tx,
547 $request_data->localtax2_tx,
548 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
549 $request_data->info_bits,
550 $request_data->product_type,
551 $request_data->fk_parent_line,
553 $request_data->fk_fournprice,
554 $request_data->pa_ht,
555 $request_data->label,
556 $request_data->special_code,
557 $request_data->array_options,
558 $request_data->situation_percent,
559 $request_data->fk_unit,
560 $request_data->multicurrency_subprice,
562 $request_data->ref_ext,
566 if ($updateRes > 0) {
567 $result = $this->
get(
$id);
568 unset($result->line);
571 throw new RestException(304, $this->invoice->error);
592 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
593 throw new RestException(403);
596 $result = $this->invoice->fetch(
$id);
599 throw new RestException(404,
'Invoice not found');
602 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
603 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
607 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
610 $result = $this->invoice->add_contact($contactid, $type,
'external');
613 throw new RestException(500,
'Error when added the contact');
619 'message' =>
'Contact linked to the invoice'
640 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
641 throw new RestException(403);
644 $result = $this->invoice->fetch(
$id);
647 throw new RestException(404,
'Invoice not found');
651 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
654 $contacts = $this->invoice->liste_contact();
656 foreach ($contacts as $contact) {
657 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
658 $result = $this->invoice->delete_contact($contact[
'rowid']);
661 throw new RestException(500,
'Error when deleted the contact');
685 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
686 throw new RestException(403);
688 if (empty($lineid)) {
689 throw new RestException(400,
'Line ID is mandatory');
693 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
696 $result = $this->invoice->fetch(
$id);
698 throw new RestException(404,
'Invoice not found');
701 $updateRes = $this->invoice->deleteLine($lineid,
$id);
702 if ($updateRes > 0) {
703 return $this->
get(
$id);
705 throw new RestException(405, $this->invoice->error);
718 public function put(
$id, $request_data =
null)
720 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
721 throw new RestException(403);
724 $result = $this->invoice->fetch(
$id);
726 throw new RestException(404,
'Invoice not found');
730 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
733 foreach ($request_data as $field => $value) {
734 if ($field ==
'id') {
737 if ($field ===
'caller') {
739 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
742 if ($field ==
'array_options' && is_array($value)) {
743 foreach ($value as $index => $val) {
744 $this->invoice->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->invoice);
749 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
752 if ($field ==
'cond_reglement_id') {
753 $this->invoice->date_lim_reglement = $this->invoice->calculate_date_lim_reglement();
758 if (!empty($this->invoice->fk_account)) {
759 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
760 throw new RestException(400, $this->invoice->error);
764 if ($this->invoice->update(DolibarrApiAccess::$user) > 0) {
765 return $this->
get(
$id);
767 throw new RestException(500, $this->invoice->error);
779 public function delete(
$id)
781 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'supprimer')) {
782 throw new RestException(403);
784 $result = $this->invoice->fetch(
$id);
786 throw new RestException(404,
'Invoice not found');
790 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
793 $result = $this->invoice->delete(DolibarrApiAccess::$user);
795 throw new RestException(500,
'Error when deleting invoice');
796 } elseif ($result == 0) {
797 throw new RestException(403,
'Invoice not erasable');
803 'message' =>
'Invoice deleted'
835 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
836 throw new RestException(403);
839 $result = $this->invoice->fetch(
$id);
841 throw new RestException(404,
'Invoice not found');
845 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
848 $request_data = (object) $request_data;
850 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
851 $request_data->label =
sanitizeVal($request_data->label);
854 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
855 $request_data->fk_parent_line = 0;
859 $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);
860 $pa_ht = $marginInfos[0];
862 $updateRes = $this->invoice->addline(
864 $request_data->subprice,
866 $request_data->tva_tx,
867 $request_data->localtax1_tx,
868 $request_data->localtax2_tx,
869 $request_data->fk_product,
870 $request_data->remise_percent,
871 $request_data->date_start,
872 $request_data->date_end,
873 $request_data->fk_code_ventilation,
874 $request_data->info_bits,
875 $request_data->fk_remise_except,
876 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
877 $request_data->subprice,
878 $request_data->product_type,
880 $request_data->special_code,
881 $request_data->origin,
882 $request_data->origin_id,
883 $request_data->fk_parent_line,
884 empty($request_data->fk_fournprice) ? null : $request_data->fk_fournprice,
886 $request_data->label,
887 $request_data->array_options,
888 $request_data->situation_percent,
889 $request_data->fk_prev_id,
890 $request_data->fk_unit,
892 $request_data->ref_ext
895 if ($updateRes < 0) {
896 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
920 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
922 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
923 throw new RestException(403);
925 $result = $this->invoice->fetch(
$id);
927 throw new RestException(404,
'Invoice not found');
931 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
934 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
936 throw new RestException(500,
'Error : '.$this->invoice->error);
939 $result = $this->invoice->fetch(
$id);
941 throw new RestException(404,
'Invoice not found');
945 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
969 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
970 throw new RestException(403);
972 $result = $this->invoice->fetch(
$id);
974 throw new RestException(404,
'Invoice not found');
978 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
981 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
983 throw new RestException(304,
'Nothing done.');
986 throw new RestException(500,
'Error : '.$this->invoice->error);
989 $result = $this->invoice->fetch(
$id);
991 throw new RestException(404,
'Invoice not found');
1015 public function validate(
$id, $force_number =
'', $idwarehouse = 0, $notrigger = 0)
1017 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1018 throw new RestException(403);
1020 $result = $this->invoice->fetch(
$id);
1022 throw new RestException(404,
'Invoice not found');
1026 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1029 $result = $this->invoice->validate(DolibarrApiAccess::$user, $force_number, $idwarehouse, $notrigger);
1031 throw new RestException(304,
'Error nothing done. May be object is already validated');
1034 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
1037 $result = $this->invoice->fetch(
$id);
1039 throw new RestException(404,
'Invoice not found');
1043 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1047 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
1048 $this->invoice->online_payment_url = getOnlinePaymentUrl(0,
'invoice', $this->invoice->ref);
1070 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1071 throw new RestException(403);
1073 $result = $this->invoice->fetch(
$id);
1075 throw new RestException(404,
'Invoice not found');
1079 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1082 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
1084 throw new RestException(304,
'Error nothing done. May be object is already validated');
1087 throw new RestException(500,
'Error : '.$this->invoice->error);
1091 $result = $this->invoice->fetch(
$id);
1093 throw new RestException(404,
'Invoice not found');
1097 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1119 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1120 throw new RestException(403);
1122 $result = $this->invoice->fetch(
$id);
1124 throw new RestException(404,
'Invoice not found');
1128 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1131 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1133 throw new RestException(304,
'Nothing done');
1136 throw new RestException(500,
'Error : '.$this->invoice->error);
1140 $result = $this->invoice->fetch(
$id);
1142 throw new RestException(404,
'Invoice not found');
1146 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1162 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1164 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1165 throw new RestException(403);
1168 $result = $this->invoice->fetch(
$id);
1170 throw new RestException(404,
'Invoice not found');
1174 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1178 $result = $discountcheck->fetch(0, $this->invoice->id);
1181 throw new RestException(404,
'Discount not found');
1184 throw new RestException(500, $discountcheck->error);
1187 return parent::_cleanObjectDatas($discountcheck);
1205 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1207 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1208 throw new RestException(403);
1211 $result = $this->invoice->fetch(
$id);
1213 throw new RestException(404,
'Invoice not found');
1217 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1220 if ($this->invoice->paye) {
1221 throw new RestException(500,
'Alreay paid');
1224 $this->invoice->fetch(
$id);
1225 $this->invoice->fetch_thirdparty();
1229 $result = $discountcheck->fetch(0, $this->invoice->id);
1241 $amount_ht = $amount_tva = $amount_ttc = array();
1242 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1244 @phan-var-force array<string,float> $amount_ht
1245 @phan-var-force array<string,float> $amount_tva
1246 @phan-var-force array<string,float> $amount_ttc
1247 @phan-var-force array<string,float> $multicurrency_amount_ht
1248 @phan-var-force array<string,float> $multicurrency_amount_tva
1249 @phan-var-force array<string,float> $multicurrency_amount_ttc
1254 foreach ($this->invoice->lines as $line) {
1255 if ($line->product_type < 9 && $line->total_ht != 0) {
1256 if (!array_key_exists($line->tva_tx, $amount_ht)) {
1257 $amount_ht[$line->tva_tx] = 0.0;
1258 $amount_tva[$line->tva_tx] = 0.0;
1259 $amount_ttc[$line->tva_tx] = 0.0;
1260 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
1261 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
1262 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
1265 $amount_ht[$line->tva_tx] += $line->total_ht;
1266 $amount_tva[$line->tva_tx] += $line->total_tva;
1267 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1268 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1269 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1270 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1278 $discount->description =
'(CREDIT_NOTE)';
1280 $discount->description =
'(DEPOSIT)';
1282 $discount->description =
'(EXCESS RECEIVED)';
1284 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1287 $discount->fk_soc = $this->invoice->socid;
1288 $discount->socid = $this->invoice->socid;
1289 $discount->fk_facture_source = $this->invoice->id;
1297 $sql =
'SELECT SUM(pf.amount) as total_payments';
1298 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1299 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1300 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1301 $sql .=
' AND pf.fk_paiement = p.rowid';
1302 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1303 $resql = $this->db->query($sql);
1308 $res = $this->db->fetch_object($resql);
1309 $total_payments = $res->total_payments;
1312 $total_creditnote_and_deposit = 0;
1313 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1314 $sql .=
" re.description, re.fk_facture_source";
1315 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1316 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1317 $resql = $this->db->query($sql);
1318 if (!empty($resql)) {
1319 while ($obj = $this->db->fetch_object($resql)) {
1320 $total_creditnote_and_deposit += $obj->amount_ttc;
1326 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1327 $discount->amount_tva = 0;
1328 $discount->tva_tx = 0;
1330 $result = $discount->create(DolibarrApiAccess::$user);
1336 foreach ($amount_ht as $tva_tx => $xxx) {
1337 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1338 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1339 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1340 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1341 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1342 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1343 $discount->tva_tx = abs((
float) $tva_tx);
1345 $result = $discount->create(DolibarrApiAccess::$user);
1353 if (empty($error)) {
1356 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1358 $this->db->commit();
1360 $this->db->rollback();
1361 throw new RestException(500,
'Could not set paid');
1364 $this->db->commit();
1367 $this->db->rollback();
1368 throw new RestException(500,
'Discount creation error');
1393 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1394 throw new RestException(403);
1397 throw new RestException(400,
'Invoice ID is mandatory');
1399 if (empty($discountid)) {
1400 throw new RestException(400,
'Discount ID is mandatory');
1404 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1407 $result = $this->invoice->fetch(
$id);
1409 throw new RestException(404,
'Invoice not found');
1412 $result = $this->invoice->insert_discount($discountid);
1414 throw new RestException(405, $this->invoice->error);
1438 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1440 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1441 throw new RestException(403);
1444 throw new RestException(400,
'Invoice ID is mandatory');
1446 if (empty($discountid)) {
1447 throw new RestException(400,
'Credit ID is mandatory');
1451 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1454 $result = $discount->fetch($discountid);
1456 throw new RestException(404,
'Credit not found');
1459 $result = $discount->link_to_invoice(0,
$id);
1461 throw new RestException(405, $discount->error);
1484 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1485 throw new RestException(403);
1488 throw new RestException(400,
'Invoice ID is mandatory');
1492 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1495 $result = $this->invoice->fetch(
$id);
1497 throw new RestException(404,
'Invoice not found');
1500 $result = $this->invoice->getListOfPayments();
1501 if (!is_array($result) && $result < 0) {
1502 throw new RestException(405, $this->invoice->error);
1530 public function addPayment(
$id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1532 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1534 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1535 throw new RestException(403);
1538 throw new RestException(400,
'Invoice ID is mandatory');
1542 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1545 if (isModEnabled(
"bank")) {
1546 if (empty($accountid)) {
1547 throw new RestException(400,
'Account ID is mandatory');
1551 if (empty($paymentid)) {
1552 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1556 $result = $this->invoice->fetch(
$id);
1558 throw new RestException(404,
'Invoice not found');
1562 $totalpaid = $this->invoice->getSommePaiement();
1563 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1564 $totaldeposits = $this->invoice->getSumDepositsUsed();
1565 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1570 $multicurrency_amounts = array();
1574 $resteapayer =
price2num($resteapayer,
'MT');
1575 $amounts[
$id] = (float)
price2num(-1 * abs((
float) $resteapayer),
'MT');
1577 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1578 $multicurrency_amounts[
$id] = (float)
price2num(-1 * (
float) $newvalue,
'MT');
1580 $resteapayer =
price2num($resteapayer,
'MT');
1581 $amounts[
$id] = (float) $resteapayer;
1583 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1584 $multicurrency_amounts[
$id] = (float) $newvalue;
1588 $paymentobj =
new Paiement($this->db);
1589 if (is_numeric($datepaye)) {
1590 $paymentobj->datepaye = $datepaye;
1594 $paymentobj->amounts = $amounts;
1595 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1596 $paymentobj->paiementid = $paymentid;
1597 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $paymentid,
'c_paiement',
'id',
'code', 1);
1598 $paymentobj->num_payment = $num_payment;
1599 $paymentobj->note_private = $comment;
1601 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1602 if ($payment_id < 0) {
1603 $this->db->rollback();
1604 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1607 if (isModEnabled(
"bank")) {
1608 $label =
'(CustomerInvoicePayment)';
1610 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1611 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1614 $label =
'(CustomerInvoicePaymentBack)';
1616 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1618 $this->db->rollback();
1619 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1623 $this->db->commit();
1656 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1658 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1660 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1661 throw new RestException(403);
1663 foreach ($arrayofamounts as
$id => $amount) {
1665 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1668 throw new RestException(403,
'Access not allowed on invoice ID '.
$id.
' for login '.DolibarrApiAccess::$user->login);
1672 if (isModEnabled(
"bank")) {
1673 if (empty($accountid)) {
1674 throw new RestException(400,
'Account ID is mandatory');
1677 if (empty($paymentid)) {
1678 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1684 $multicurrency_amounts = array();
1687 foreach ($arrayofamounts as
$id => $amountarray) {
1688 $result = $this->invoice->fetch((
int)
$id);
1690 $this->db->rollback();
1691 throw new RestException(404,
'Invoice ID '.
$id.
' not found');
1694 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1695 $this->db->rollback();
1696 throw new RestException(400,
'Payment in both currency '.
$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1699 $is_multicurrency = 0;
1700 $total_ttc = $this->invoice->total_ttc;
1702 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1703 $is_multicurrency = 1;
1704 $total_ttc = $this->invoice->multicurrency_total_ttc;
1708 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1709 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1710 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1711 $remainstopay = $amount = (float)
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1713 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1714 $amount = (float)
price2num($amountarray[
"amount"],
'MT');
1717 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1718 $amount = (float)
price2num($amountarray[
"multicurrency_amount"],
'MT');
1721 if (abs($amount) > abs($remainstopay) && !$accepthigherpayment) {
1722 $this->db->rollback();
1723 throw new RestException(400,
'Payment amount on invoice ID '.
$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1727 $amount = (float)
price2num(-1 * abs((
float) $amount),
'MT');
1730 if ($is_multicurrency) {
1731 $amounts[
$id] =
null;
1733 $multicurrency_amounts[
$id] = (float) $amount;
1735 $amounts[
$id] = (float) $amount;
1737 $multicurrency_amounts[
$id] =
null;
1742 $paymentobj =
new Paiement($this->db);
1743 if (is_numeric($datepaye)) {
1744 $paymentobj->datepaye = $datepaye;
1748 $paymentobj->amounts = $amounts;
1749 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1750 $paymentobj->paiementid = $paymentid;
1751 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $paymentid,
'c_paiement',
'id',
'code', 1);
1752 $paymentobj->num_payment = $num_payment;
1753 $paymentobj->note_private = $comment;
1754 $paymentobj->ref_ext = $ref_ext;
1755 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1756 if ($payment_id < 0) {
1757 $this->db->rollback();
1758 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1760 if (isModEnabled(
"bank")) {
1761 $label =
'(CustomerInvoicePayment)';
1762 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1763 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1766 $label =
'(CustomerInvoicePaymentBack)';
1768 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1770 $this->db->rollback();
1771 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1775 $this->db->commit();
1798 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1800 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1801 throw new RestException(403);
1804 throw new RestException(400,
'Payment ID is mandatory');
1807 $paymentobj =
new Paiement($this->db);
1808 $result = $paymentobj->fetch(
$id);
1811 throw new RestException(404,
'Payment not found');
1814 if (!empty($num_payment)) {
1815 $result = $paymentobj->update_num($num_payment);
1817 throw new RestException(500,
'Error when updating the payment num');
1824 'message' =>
'Payment updated'
1844 unset(
$object->barcode_type_code);
1845 unset(
$object->barcode_type_label);
1846 unset(
$object->barcode_type_coder);
1862 if ($data ===
null) {
1866 foreach (Invoices::$FIELDS as $field) {
1867 if (!isset($data[$field])) {
1868 throw new RestException(400,
"$field field missing");
1870 $invoice[$field] = $data[$field];
1909 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1910 throw new RestException(403);
1913 $result = $this->template_invoice->fetch(
$id, $ref, $ref_ext);
1915 throw new RestException(404,
'Template invoice not found');
1919 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1923 if ($contact_list > -1) {
1924 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1925 if (is_array($tmparray)) {
1926 $this->template_invoice->contacts_ids = $tmparray;
1930 $this->template_invoice->fetchObjectLinked();
1950 unset(
$object->barcode_type_code);
1951 unset(
$object->barcode_type_label);
1952 unset(
$object->barcode_type_coder);
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to manage customers orders.
Class to manage absolute discounts.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
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 lines.
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.
validate($id, $force_number='', $idwarehouse=0, $notrigger=0)
Validate an 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.
useCreditNote($id, $discountid)
Add an available credit note discount to payments of an existing invoice.
createInvoiceFromContract($contractid)
Create an invoice using a contract.
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.
_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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='', $pagination_data=false, $loadlinkedobjects=0)
List invoices.
deleteLine($id, $lineid)
Deletes a line of a given invoice.
Class to manage payments of customer invoices.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
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 '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.