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';
40 public static $FIELDS = array(
52 private $template_invoice;
62 $this->invoice =
new Facture($this->db);
63 $this->template_invoice =
new FactureRec($this->db);
77 public function get($id, $contact_list = 1)
79 return $this->
_fetch($id,
'',
'', $contact_list);
95 public function getByRef($ref, $contact_list = 1)
97 return $this->
_fetch(
'', $ref,
'', $contact_list);
115 return $this->
_fetch(
'',
'', $ref_ext, $contact_list);
131 private function _fetch($id, $ref =
'', $ref_ext =
'', $contact_list = 1)
133 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
134 throw new RestException(403);
137 $result = $this->invoice->fetch($id, $ref, $ref_ext);
139 throw new RestException(404,
'Invoice not found');
143 $this->invoice->totalpaid = $this->invoice->getSommePaiement();
144 $this->invoice->totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
145 $this->invoice->totaldeposits = $this->invoice->getSumDepositsUsed();
146 $this->invoice->remaintopay =
price2num($this->invoice->total_ttc - $this->invoice->totalpaid - $this->invoice->totalcreditnotes - $this->invoice->totaldeposits,
'MT');
149 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
153 if ($contact_list > -1) {
154 $tmparray = $this->invoice->liste_contact(-1,
'external', $contact_list);
155 if (is_array($tmparray)) {
156 $this->invoice->contacts_ids = $tmparray;
160 $this->invoice->fetchObjectLinked();
163 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
164 $this->invoice->online_payment_url = getOnlinePaymentUrl(0,
'invoice', $this->invoice->ref);
187 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'')
189 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
190 throw new RestException(403);
196 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
200 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
201 $search_sale = DolibarrApiAccess::$user->id;
204 $sql =
"SELECT t.rowid";
205 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture AS t";
206 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_extrafields AS ef ON (ef.fk_object = t.rowid)";
207 $sql .=
' WHERE t.entity IN ('.getEntity(
'invoice').
')';
209 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
212 if ($search_sale && $search_sale !=
'-1') {
213 if ($search_sale == -2) {
214 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
215 } elseif ($search_sale > 0) {
216 $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).
")";
220 if ($status ==
'draft') {
221 $sql .=
" AND t.fk_statut IN (0)";
223 if ($status ==
'unpaid') {
224 $sql .=
" AND t.fk_statut IN (1)";
226 if ($status ==
'paid') {
227 $sql .=
" AND t.fk_statut IN (2)";
229 if ($status ==
'cancelled') {
230 $sql .=
" AND t.fk_statut IN (3)";
237 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
241 $sql .= $this->db->order($sortfield, $sortorder);
246 $offset = $limit * $page;
248 $sql .= $this->db->plimit($limit + 1, $offset);
251 $result = $this->db->query($sql);
254 $num = $this->db->num_rows($result);
255 $min = min($num, ($limit <= 0 ? $num : $limit));
257 $obj = $this->db->fetch_object($result);
258 $invoice_static =
new Facture($this->db);
259 if ($invoice_static->fetch($obj->rowid)) {
261 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
262 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
263 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
264 $invoice_static->remaintopay =
price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits,
'MT');
267 $tmparray = $invoice_static->liste_contact(-1,
'external', 1);
268 if (is_array($tmparray)) {
269 $invoice_static->contacts_ids = $tmparray;
272 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
273 $invoice_static->online_payment_url = getOnlinePaymentUrl(0,
'invoice', $invoice_static->ref);
280 throw new RestException(503,
'Error when retrieve invoice list : '.$this->db->lasterror());
292 public function post($request_data =
null)
294 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
295 throw new RestException(403,
"Insuffisant rights");
298 $result = $this->
_validate($request_data);
300 foreach ($request_data as $field => $value) {
301 if ($field ===
'caller') {
303 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
307 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
309 if (!array_key_exists(
'date', $request_data)) {
310 $this->invoice->date =
dol_now();
321 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data[
"date_lim_reglement"]) ? 0 : $request_data[
"date_lim_reglement"])) < 0) {
322 throw new RestException(500,
"Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
324 return ((
int) $this->invoice->id);
343 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
345 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
346 throw new RestException(403);
348 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
349 throw new RestException(403);
351 if (empty($orderid)) {
352 throw new RestException(400,
'Order ID is mandatory');
355 throw new RestException(403,
'Access not allowed on order for login '.DolibarrApiAccess::$user->login);
359 $result = $order->fetch($orderid);
361 throw new RestException(404,
'Order not found');
364 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
366 throw new RestException(405, $this->invoice->error);
368 $this->invoice->fetchObjectLinked();
387 require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
389 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
390 throw new RestException(403);
392 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
393 throw new RestException(403);
395 if (empty($contractid)) {
396 throw new RestException(400,
'Contract ID is mandatory');
399 $contract =
new Contrat($this->db);
400 $result = $contract->fetch($contractid);
402 throw new RestException(404,
'Contract not found');
405 $result = $this->invoice->createFromContract($contract, DolibarrApiAccess::$user);
407 throw new RestException(405, $this->invoice->error);
409 $this->invoice->fetchObjectLinked();
423 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
424 throw new RestException(403);
427 $result = $this->invoice->fetch($id);
429 throw new RestException(404,
'Invoice not found');
433 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
435 $this->invoice->getLinesArray();
437 foreach ($this->invoice->lines as $line) {
457 public function putLine($id, $lineid, $request_data =
null)
459 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
460 throw new RestException(403);
463 $result = $this->invoice->fetch($id);
465 throw new RestException(404,
'Invoice not found');
469 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
472 $request_data = (object) $request_data;
474 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
475 $request_data->label =
sanitizeVal($request_data->label);
477 $updateRes = $this->invoice->updateline(
480 $request_data->subprice,
482 $request_data->remise_percent,
483 $request_data->date_start,
484 $request_data->date_end,
485 $request_data->tva_tx,
486 $request_data->localtax1_tx,
487 $request_data->localtax2_tx,
488 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
489 $request_data->info_bits,
490 $request_data->product_type,
491 $request_data->fk_parent_line,
493 $request_data->fk_fournprice,
494 $request_data->pa_ht,
495 $request_data->label,
496 $request_data->special_code,
497 $request_data->array_options,
498 $request_data->situation_percent,
499 $request_data->fk_unit,
500 $request_data->multicurrency_subprice,
502 $request_data->ref_ext,
506 if ($updateRes > 0) {
507 $result = $this->
get($id);
508 unset($result->line);
511 throw new RestException(304, $this->invoice->error);
530 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
531 throw new RestException(403);
534 $result = $this->invoice->fetch($id);
537 throw new RestException(404,
'Invoice not found');
540 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
541 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
545 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
548 $result = $this->invoice->add_contact($contactid, $type,
'external');
551 throw new RestException(500,
'Error when added the contact');
557 'message' =>
'Contact linked to the invoice'
578 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
579 throw new RestException(403);
582 $result = $this->invoice->fetch($id);
585 throw new RestException(404,
'Invoice not found');
589 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
592 $contacts = $this->invoice->liste_contact();
594 foreach ($contacts as $contact) {
595 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
596 $result = $this->invoice->delete_contact($contact[
'rowid']);
599 throw new RestException(500,
'Error when deleted the contact');
623 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
624 throw new RestException(403);
626 if (empty($lineid)) {
627 throw new RestException(400,
'Line ID is mandatory');
631 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
634 $result = $this->invoice->fetch($id);
636 throw new RestException(404,
'Invoice not found');
639 $updateRes = $this->invoice->deleteLine($lineid, $id);
640 if ($updateRes > 0) {
641 return $this->
get($id);
643 throw new RestException(405, $this->invoice->error);
654 public function put($id, $request_data =
null)
656 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
657 throw new RestException(403);
660 $result = $this->invoice->fetch($id);
662 throw new RestException(404,
'Invoice not found');
666 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
669 foreach ($request_data as $field => $value) {
670 if ($field ==
'id') {
673 if ($field ===
'caller') {
675 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
678 if ($field ==
'array_options' && is_array($value)) {
679 foreach ($value as $index => $val) {
680 $this->invoice->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->invoice);
685 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
688 if ($field ==
'cond_reglement_id') {
689 $this->invoice->date_lim_reglement = $this->invoice->calculate_date_lim_reglement();
694 if (!empty($this->invoice->fk_account)) {
695 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
696 throw new RestException(400, $this->invoice->error);
700 if ($this->invoice->update(DolibarrApiAccess::$user) > 0) {
701 return $this->
get($id);
703 throw new RestException(500, $this->invoice->error);
713 public function delete($id)
715 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'supprimer')) {
716 throw new RestException(403);
718 $result = $this->invoice->fetch($id);
720 throw new RestException(404,
'Invoice not found');
724 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
727 $result = $this->invoice->delete(DolibarrApiAccess::$user);
729 throw new RestException(500,
'Error when deleting invoice');
730 } elseif ($result == 0) {
731 throw new RestException(403,
'Invoice not erasable');
737 'message' =>
'Invoice deleted'
765 public function postLine($id, $request_data =
null)
767 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
768 throw new RestException(403);
771 $result = $this->invoice->fetch($id);
773 throw new RestException(404,
'Invoice not found');
777 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
780 $request_data = (object) $request_data;
782 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
783 $request_data->label =
sanitizeVal($request_data->label);
786 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
787 $request_data->fk_parent_line = 0;
791 $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);
792 $pa_ht = $marginInfos[0];
794 $updateRes = $this->invoice->addline(
796 $request_data->subprice,
798 $request_data->tva_tx,
799 $request_data->localtax1_tx,
800 $request_data->localtax2_tx,
801 $request_data->fk_product,
802 $request_data->remise_percent,
803 $request_data->date_start,
804 $request_data->date_end,
805 $request_data->fk_code_ventilation,
806 $request_data->info_bits,
807 $request_data->fk_remise_except,
808 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
809 $request_data->subprice,
810 $request_data->product_type,
812 $request_data->special_code,
813 $request_data->origin,
814 $request_data->origin_id,
815 $request_data->fk_parent_line,
816 empty($request_data->fk_fournprice) ? null : $request_data->fk_fournprice,
818 $request_data->label,
819 $request_data->array_options,
820 $request_data->situation_percent,
821 $request_data->fk_prev_id,
822 $request_data->fk_unit,
824 $request_data->ref_ext
827 if ($updateRes < 0) {
828 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
853 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
855 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
856 throw new RestException(403);
858 $result = $this->invoice->fetch($id);
860 throw new RestException(404,
'Invoice not found');
864 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
867 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
869 throw new RestException(500,
'Error : '.$this->invoice->error);
872 $result = $this->invoice->fetch($id);
874 throw new RestException(404,
'Invoice not found');
878 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
903 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
904 throw new RestException(403);
906 $result = $this->invoice->fetch($id);
908 throw new RestException(404,
'Invoice not found');
912 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
915 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
917 throw new RestException(304,
'Nothing done.');
920 throw new RestException(500,
'Error : '.$this->invoice->error);
923 $result = $this->invoice->fetch($id);
925 throw new RestException(404,
'Invoice not found');
929 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
953 public function validate($id, $force_number =
'', $idwarehouse = 0, $notrigger = 0)
955 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
956 throw new RestException(403);
958 $result = $this->invoice->fetch($id);
960 throw new RestException(404,
'Invoice not found');
964 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
967 $result = $this->invoice->validate(DolibarrApiAccess::$user, $force_number, $idwarehouse, $notrigger);
969 throw new RestException(304,
'Error nothing done. May be object is already validated');
972 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
975 $result = $this->invoice->fetch($id);
977 throw new RestException(404,
'Invoice not found');
981 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
985 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
986 $this->invoice->online_payment_url = getOnlinePaymentUrl(0,
'invoice', $this->invoice->ref);
1006 public function settopaid($id, $close_code =
'', $close_note =
'')
1008 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1009 throw new RestException(403);
1011 $result = $this->invoice->fetch($id);
1013 throw new RestException(404,
'Invoice not found');
1017 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1020 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
1022 throw new RestException(304,
'Error nothing done. May be object is already validated');
1025 throw new RestException(500,
'Error : '.$this->invoice->error);
1029 $result = $this->invoice->fetch($id);
1031 throw new RestException(404,
'Invoice not found');
1035 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1057 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1058 throw new RestException(403);
1060 $result = $this->invoice->fetch($id);
1062 throw new RestException(404,
'Invoice not found');
1066 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1069 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1071 throw new RestException(304,
'Nothing done');
1074 throw new RestException(500,
'Error : '.$this->invoice->error);
1078 $result = $this->invoice->fetch($id);
1080 throw new RestException(404,
'Invoice not found');
1084 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1100 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1102 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1103 throw new RestException(403);
1106 $result = $this->invoice->fetch($id);
1108 throw new RestException(404,
'Invoice not found');
1112 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1116 $result = $discountcheck->fetch(0, $this->invoice->id);
1119 throw new RestException(404,
'Discount not found');
1122 throw new RestException(500, $discountcheck->error);
1125 return parent::_cleanObjectDatas($discountcheck);
1143 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1145 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1146 throw new RestException(403);
1149 $result = $this->invoice->fetch($id);
1151 throw new RestException(404,
'Invoice not found');
1155 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1158 if ($this->invoice->paye) {
1159 throw new RestException(500,
'Alreay paid');
1162 $this->invoice->fetch($id);
1163 $this->invoice->fetch_thirdparty();
1167 $result = $discountcheck->fetch(0, $this->invoice->id);
1179 $amount_ht = $amount_tva = $amount_ttc = array();
1180 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1184 foreach ($this->invoice->lines as $line) {
1185 if ($line->product_type < 9 && $line->total_ht != 0) {
1187 $amount_ht[$line->tva_tx] += $line->total_ht;
1188 $amount_tva[$line->tva_tx] += $line->total_tva;
1189 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1190 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1191 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1192 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1200 $discount->description =
'(CREDIT_NOTE)';
1202 $discount->description =
'(DEPOSIT)';
1204 $discount->description =
'(EXCESS RECEIVED)';
1206 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1209 $discount->fk_soc = $this->invoice->socid;
1210 $discount->socid = $this->invoice->socid;
1211 $discount->fk_facture_source = $this->invoice->id;
1219 $sql =
'SELECT SUM(pf.amount) as total_payments';
1220 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1221 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1222 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1223 $sql .=
' AND pf.fk_paiement = p.rowid';
1224 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1225 $resql = $this->db->query($sql);
1230 $res = $this->db->fetch_object($resql);
1231 $total_payments = $res->total_payments;
1234 $total_creditnote_and_deposit = 0;
1235 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1236 $sql .=
" re.description, re.fk_facture_source";
1237 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1238 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1239 $resql = $this->db->query($sql);
1240 if (!empty($resql)) {
1241 while ($obj = $this->db->fetch_object($resql)) {
1242 $total_creditnote_and_deposit += $obj->amount_ttc;
1248 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1249 $discount->amount_tva = 0;
1250 $discount->tva_tx = 0;
1252 $result = $discount->create(DolibarrApiAccess::$user);
1258 foreach ($amount_ht as $tva_tx => $xxx) {
1259 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1260 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1261 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1262 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1263 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1264 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1265 $discount->tva_tx = abs($tva_tx);
1267 $result = $discount->create(DolibarrApiAccess::$user);
1275 if (empty($error)) {
1278 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1280 $this->db->commit();
1282 $this->db->rollback();
1283 throw new RestException(500,
'Could not set paid');
1286 $this->db->commit();
1289 $this->db->rollback();
1290 throw new RestException(500,
'Discount creation error');
1315 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1316 throw new RestException(403);
1319 throw new RestException(400,
'Invoice ID is mandatory');
1321 if (empty($discountid)) {
1322 throw new RestException(400,
'Discount ID is mandatory');
1326 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1329 $result = $this->invoice->fetch($id);
1331 throw new RestException(404,
'Invoice not found');
1334 $result = $this->invoice->insert_discount($discountid);
1336 throw new RestException(405, $this->invoice->error);
1360 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1362 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1363 throw new RestException(403);
1366 throw new RestException(400,
'Invoice ID is mandatory');
1368 if (empty($discountid)) {
1369 throw new RestException(400,
'Credit ID is mandatory');
1373 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1376 $result = $discount->fetch($discountid);
1378 throw new RestException(404,
'Credit not found');
1381 $result = $discount->link_to_invoice(0, $id);
1383 throw new RestException(405, $discount->error);
1404 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1405 throw new RestException(403);
1408 throw new RestException(400,
'Invoice ID is mandatory');
1412 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1415 $result = $this->invoice->fetch($id);
1417 throw new RestException(404,
'Invoice not found');
1420 $result = $this->invoice->getListOfPayments();
1422 throw new RestException(405, $this->invoice->error);
1450 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1452 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1454 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1455 throw new RestException(403);
1458 throw new RestException(400,
'Invoice ID is mandatory');
1462 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1465 if (isModEnabled(
"bank")) {
1466 if (empty($accountid)) {
1467 throw new RestException(400,
'Account ID is mandatory');
1471 if (empty($paymentid)) {
1472 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1476 $result = $this->invoice->fetch($id);
1478 throw new RestException(404,
'Invoice not found');
1482 $totalpaid = $this->invoice->getSommePaiement();
1483 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1484 $totaldeposits = $this->invoice->getSumDepositsUsed();
1485 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1490 $multicurrency_amounts = array();
1494 $resteapayer =
price2num($resteapayer,
'MT');
1495 $amounts[$id] = (float)
price2num(-1 * (
float) $resteapayer,
'MT');
1497 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1498 $multicurrency_amounts[$id] = (float)
price2num(-1 * (
float) $newvalue,
'MT');
1500 $resteapayer =
price2num($resteapayer,
'MT');
1501 $amounts[$id] = (float) $resteapayer;
1503 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1504 $multicurrency_amounts[$id] = (float) $newvalue;
1508 $paymentobj =
new Paiement($this->db);
1509 if (is_numeric($datepaye)) {
1510 $paymentobj->datepaye = $datepaye;
1514 $paymentobj->amounts = $amounts;
1515 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1516 $paymentobj->paiementid = $paymentid;
1517 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1518 $paymentobj->num_payment = $num_payment;
1519 $paymentobj->note_private = $comment;
1521 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1522 if ($payment_id < 0) {
1523 $this->db->rollback();
1524 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1527 if (isModEnabled(
"bank")) {
1528 $label =
'(CustomerInvoicePayment)';
1530 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1531 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1534 $label =
'(CustomerInvoicePaymentBack)';
1536 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1538 $this->db->rollback();
1539 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1543 $this->db->commit();
1574 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1576 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1578 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1579 throw new RestException(403);
1581 foreach ($arrayofamounts as $id => $amount) {
1583 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1586 throw new RestException(403,
'Access not allowed on invoice ID '.$id.
' for login '.DolibarrApiAccess::$user->login);
1590 if (isModEnabled(
"bank")) {
1591 if (empty($accountid)) {
1592 throw new RestException(400,
'Account ID is mandatory');
1595 if (empty($paymentid)) {
1596 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1602 $multicurrency_amounts = array();
1605 foreach ($arrayofamounts as $id => $amountarray) {
1606 $result = $this->invoice->fetch($id);
1608 $this->db->rollback();
1609 throw new RestException(404,
'Invoice ID '.$id.
' not found');
1612 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1613 $this->db->rollback();
1614 throw new RestException(400,
'Payment in both currency '.$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1617 $is_multicurrency = 0;
1618 $total_ttc = $this->invoice->total_ttc;
1620 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1621 $is_multicurrency = 1;
1622 $total_ttc = $this->invoice->multicurrency_total_ttc;
1626 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1627 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1628 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1629 $remainstopay = $amount = (float)
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1631 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1632 $amount = (float)
price2num($amountarray[
"amount"],
'MT');
1635 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1636 $amount = (float)
price2num($amountarray[
"multicurrency_amount"],
'MT');
1639 if (abs($amount) > abs($remainstopay) && !$accepthigherpayment) {
1640 $this->db->rollback();
1641 throw new RestException(400,
'Payment amount on invoice ID '.$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1645 $amount = (float)
price2num(-1 * abs((
float) $amount),
'MT');
1648 if ($is_multicurrency) {
1649 $amounts[$id] =
null;
1651 $multicurrency_amounts[$id] = (float) $amount;
1653 $amounts[$id] = (float) $amount;
1655 $multicurrency_amounts[$id] =
null;
1660 $paymentobj =
new Paiement($this->db);
1661 if (is_numeric($datepaye)) {
1662 $paymentobj->datepaye = $datepaye;
1666 $paymentobj->amounts = $amounts;
1667 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1668 $paymentobj->paiementid = $paymentid;
1669 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1670 $paymentobj->num_payment = $num_payment;
1671 $paymentobj->note_private = $comment;
1672 $paymentobj->ref_ext = $ref_ext;
1673 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1674 if ($payment_id < 0) {
1675 $this->db->rollback();
1676 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1678 if (isModEnabled(
"bank")) {
1679 $label =
'(CustomerInvoicePayment)';
1680 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1681 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1684 $label =
'(CustomerInvoicePaymentBack)';
1686 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1688 $this->db->rollback();
1689 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1693 $this->db->commit();
1714 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1716 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1717 throw new RestException(403);
1720 throw new RestException(400,
'Payment ID is mandatory');
1723 $paymentobj =
new Paiement($this->db);
1724 $result = $paymentobj->fetch($id);
1727 throw new RestException(404,
'Payment not found');
1730 if (!empty($num_payment)) {
1731 $result = $paymentobj->update_num($num_payment);
1733 throw new RestException(500,
'Error when updating the payment num');
1740 'message' =>
'Payment updated'
1760 unset(
$object->barcode_type_code);
1761 unset(
$object->barcode_type_label);
1762 unset(
$object->barcode_type_coder);
1779 foreach (Invoices::$FIELDS as $field) {
1780 if (!isset($data[$field])) {
1781 throw new RestException(400,
"$field field missing");
1783 $invoice[$field] = $data[$field];
1822 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1823 throw new RestException(403);
1826 $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1828 throw new RestException(404,
'Template invoice not found');
1832 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1836 if ($contact_list > -1) {
1837 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1838 if (is_array($tmparray)) {
1839 $this->template_invoice->contacts_ids = $tmparray;
1843 $this->template_invoice->fetchObjectLinked();
1863 unset(
$object->barcode_type_code);
1864 unset(
$object->barcode_type_label);
1865 unset(
$object->barcode_type_coder);
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to manage customers orders.
Class to manage contracts.
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 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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='')
List invoices.
__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.
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.