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);
342 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
344 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
345 throw new RestException(403);
347 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
348 throw new RestException(403);
350 if (empty($orderid)) {
351 throw new RestException(400,
'Order ID is mandatory');
355 $result = $order->fetch($orderid);
357 throw new RestException(404,
'Order not found');
360 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
362 throw new RestException(405, $this->invoice->error);
364 $this->invoice->fetchObjectLinked();
383 require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
385 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
386 throw new RestException(403);
388 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
389 throw new RestException(403);
391 if (empty($contractid)) {
392 throw new RestException(400,
'Contract ID is mandatory');
395 $contract =
new Contrat($this->db);
396 $result = $contract->fetch($contractid);
398 throw new RestException(404,
'Contract not found');
401 $result = $this->invoice->createFromContract($contract, DolibarrApiAccess::$user);
403 throw new RestException(405, $this->invoice->error);
405 $this->invoice->fetchObjectLinked();
419 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
420 throw new RestException(403);
423 $result = $this->invoice->fetch($id);
425 throw new RestException(404,
'Invoice not found');
429 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
431 $this->invoice->getLinesArray();
433 foreach ($this->invoice->lines as $line) {
453 public function putLine($id, $lineid, $request_data =
null)
455 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
456 throw new RestException(403);
459 $result = $this->invoice->fetch($id);
461 throw new RestException(404,
'Invoice not found');
465 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
468 $request_data = (object) $request_data;
470 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
471 $request_data->label =
sanitizeVal($request_data->label);
473 $updateRes = $this->invoice->updateline(
476 $request_data->subprice,
478 $request_data->remise_percent,
479 $request_data->date_start,
480 $request_data->date_end,
481 $request_data->tva_tx,
482 $request_data->localtax1_tx,
483 $request_data->localtax2_tx,
484 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
485 $request_data->info_bits,
486 $request_data->product_type,
487 $request_data->fk_parent_line,
489 $request_data->fk_fournprice,
490 $request_data->pa_ht,
491 $request_data->label,
492 $request_data->special_code,
493 $request_data->array_options,
494 $request_data->situation_percent,
495 $request_data->fk_unit,
496 $request_data->multicurrency_subprice,
498 $request_data->ref_ext,
502 if ($updateRes > 0) {
503 $result = $this->
get($id);
504 unset($result->line);
507 throw new RestException(304, $this->invoice->error);
526 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
527 throw new RestException(403);
530 $result = $this->invoice->fetch($id);
533 throw new RestException(404,
'Invoice not found');
536 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
537 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
541 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
544 $result = $this->invoice->add_contact($contactid, $type,
'external');
547 throw new RestException(500,
'Error when added the contact');
553 'message' =>
'Contact linked to the invoice'
574 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
575 throw new RestException(403);
578 $result = $this->invoice->fetch($id);
581 throw new RestException(404,
'Invoice not found');
585 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
588 $contacts = $this->invoice->liste_contact();
590 foreach ($contacts as $contact) {
591 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
592 $result = $this->invoice->delete_contact($contact[
'rowid']);
595 throw new RestException(500,
'Error when deleted the contact');
619 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
620 throw new RestException(403);
622 if (empty($lineid)) {
623 throw new RestException(400,
'Line ID is mandatory');
627 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
630 $result = $this->invoice->fetch($id);
632 throw new RestException(404,
'Invoice not found');
635 $updateRes = $this->invoice->deleteLine($lineid, $id);
636 if ($updateRes > 0) {
637 return $this->
get($id);
639 throw new RestException(405, $this->invoice->error);
650 public function put($id, $request_data =
null)
652 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
653 throw new RestException(403);
656 $result = $this->invoice->fetch($id);
658 throw new RestException(404,
'Invoice not found');
662 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
665 foreach ($request_data as $field => $value) {
666 if ($field ==
'id') {
669 if ($field ===
'caller') {
671 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
674 if ($field ==
'array_options' && is_array($value)) {
675 foreach ($value as $index => $val) {
676 $this->invoice->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->invoice);
681 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
684 if ($field ==
'cond_reglement_id') {
685 $this->invoice->date_lim_reglement = $this->invoice->calculate_date_lim_reglement();
690 if (!empty($this->invoice->fk_account)) {
691 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
692 throw new RestException(400, $this->invoice->error);
696 if ($this->invoice->update(DolibarrApiAccess::$user)) {
697 return $this->
get($id);
709 public function delete($id)
711 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'supprimer')) {
712 throw new RestException(403);
714 $result = $this->invoice->fetch($id);
716 throw new RestException(404,
'Invoice not found');
720 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
723 $result = $this->invoice->delete(DolibarrApiAccess::$user);
725 throw new RestException(500,
'Error when deleting invoice');
726 } elseif ($result == 0) {
727 throw new RestException(403,
'Invoice not erasable');
733 'message' =>
'Invoice deleted'
761 public function postLine($id, $request_data =
null)
763 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
764 throw new RestException(403);
767 $result = $this->invoice->fetch($id);
769 throw new RestException(404,
'Invoice not found');
773 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
776 $request_data = (object) $request_data;
778 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
779 $request_data->label =
sanitizeVal($request_data->label);
782 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
783 $request_data->fk_parent_line = 0;
787 $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);
788 $pa_ht = $marginInfos[0];
790 $updateRes = $this->invoice->addline(
792 $request_data->subprice,
794 $request_data->tva_tx,
795 $request_data->localtax1_tx,
796 $request_data->localtax2_tx,
797 $request_data->fk_product,
798 $request_data->remise_percent,
799 $request_data->date_start,
800 $request_data->date_end,
801 $request_data->fk_code_ventilation,
802 $request_data->info_bits,
803 $request_data->fk_remise_except,
804 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
805 $request_data->subprice,
806 $request_data->product_type,
808 $request_data->special_code,
809 $request_data->origin,
810 $request_data->origin_id,
811 $request_data->fk_parent_line,
812 empty($request_data->fk_fournprice) ? null : $request_data->fk_fournprice,
814 $request_data->label,
815 $request_data->array_options,
816 $request_data->situation_percent,
817 $request_data->fk_prev_id,
818 $request_data->fk_unit,
820 $request_data->ref_ext
823 if ($updateRes < 0) {
824 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
849 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
851 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
852 throw new RestException(403);
854 $result = $this->invoice->fetch($id);
856 throw new RestException(404,
'Invoice not found');
860 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
863 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
865 throw new RestException(500,
'Error : '.$this->invoice->error);
868 $result = $this->invoice->fetch($id);
870 throw new RestException(404,
'Invoice not found');
874 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
899 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
900 throw new RestException(403);
902 $result = $this->invoice->fetch($id);
904 throw new RestException(404,
'Invoice not found');
908 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
911 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
913 throw new RestException(304,
'Nothing done.');
916 throw new RestException(500,
'Error : '.$this->invoice->error);
919 $result = $this->invoice->fetch($id);
921 throw new RestException(404,
'Invoice not found');
925 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
949 public function validate($id, $force_number =
'', $idwarehouse = 0, $notrigger = 0)
951 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
952 throw new RestException(403);
954 $result = $this->invoice->fetch($id);
956 throw new RestException(404,
'Invoice not found');
960 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
963 $result = $this->invoice->validate(DolibarrApiAccess::$user, $force_number, $idwarehouse, $notrigger);
965 throw new RestException(304,
'Error nothing done. May be object is already validated');
968 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
971 $result = $this->invoice->fetch($id);
973 throw new RestException(404,
'Invoice not found');
977 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
981 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
982 $this->invoice->online_payment_url = getOnlinePaymentUrl(0,
'invoice', $this->invoice->ref);
1002 public function settopaid($id, $close_code =
'', $close_note =
'')
1004 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1005 throw new RestException(403);
1007 $result = $this->invoice->fetch($id);
1009 throw new RestException(404,
'Invoice not found');
1013 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1016 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
1018 throw new RestException(304,
'Error nothing done. May be object is already validated');
1021 throw new RestException(500,
'Error : '.$this->invoice->error);
1025 $result = $this->invoice->fetch($id);
1027 throw new RestException(404,
'Invoice not found');
1031 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1053 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1054 throw new RestException(403);
1056 $result = $this->invoice->fetch($id);
1058 throw new RestException(404,
'Invoice not found');
1062 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1065 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1067 throw new RestException(304,
'Nothing done');
1070 throw new RestException(500,
'Error : '.$this->invoice->error);
1074 $result = $this->invoice->fetch($id);
1076 throw new RestException(404,
'Invoice not found');
1080 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1096 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1098 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1099 throw new RestException(403);
1102 $result = $this->invoice->fetch($id);
1104 throw new RestException(404,
'Invoice not found');
1108 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1112 $result = $discountcheck->fetch(0, $this->invoice->id);
1115 throw new RestException(404,
'Discount not found');
1118 throw new RestException(500, $discountcheck->error);
1121 return parent::_cleanObjectDatas($discountcheck);
1139 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1141 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1142 throw new RestException(403);
1145 $result = $this->invoice->fetch($id);
1147 throw new RestException(404,
'Invoice not found');
1151 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1154 if ($this->invoice->paye) {
1155 throw new RestException(500,
'Alreay paid');
1158 $this->invoice->fetch($id);
1159 $this->invoice->fetch_thirdparty();
1163 $result = $discountcheck->fetch(0, $this->invoice->id);
1175 $amount_ht = $amount_tva = $amount_ttc = array();
1176 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1180 foreach ($this->invoice->lines as $line) {
1181 if ($line->product_type < 9 && $line->total_ht != 0) {
1183 $amount_ht[$line->tva_tx] += $line->total_ht;
1184 $amount_tva[$line->tva_tx] += $line->total_tva;
1185 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1186 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1187 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1188 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1196 $discount->description =
'(CREDIT_NOTE)';
1198 $discount->description =
'(DEPOSIT)';
1200 $discount->description =
'(EXCESS RECEIVED)';
1202 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1205 $discount->fk_soc = $this->invoice->socid;
1206 $discount->socid = $this->invoice->socid;
1207 $discount->fk_facture_source = $this->invoice->id;
1215 $sql =
'SELECT SUM(pf.amount) as total_payments';
1216 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1217 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1218 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1219 $sql .=
' AND pf.fk_paiement = p.rowid';
1220 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1221 $resql = $this->db->query($sql);
1226 $res = $this->db->fetch_object($resql);
1227 $total_payments = $res->total_payments;
1230 $total_creditnote_and_deposit = 0;
1231 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1232 $sql .=
" re.description, re.fk_facture_source";
1233 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1234 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1235 $resql = $this->db->query($sql);
1236 if (!empty($resql)) {
1237 while ($obj = $this->db->fetch_object($resql)) {
1238 $total_creditnote_and_deposit += $obj->amount_ttc;
1244 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1245 $discount->amount_tva = 0;
1246 $discount->tva_tx = 0;
1248 $result = $discount->create(DolibarrApiAccess::$user);
1254 foreach ($amount_ht as $tva_tx => $xxx) {
1255 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1256 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1257 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1258 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1259 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1260 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1261 $discount->tva_tx = abs($tva_tx);
1263 $result = $discount->create(DolibarrApiAccess::$user);
1271 if (empty($error)) {
1274 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1276 $this->db->commit();
1278 $this->db->rollback();
1279 throw new RestException(500,
'Could not set paid');
1282 $this->db->commit();
1285 $this->db->rollback();
1286 throw new RestException(500,
'Discount creation error');
1311 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1312 throw new RestException(403);
1315 throw new RestException(400,
'Invoice ID is mandatory');
1317 if (empty($discountid)) {
1318 throw new RestException(400,
'Discount ID is mandatory');
1322 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1325 $result = $this->invoice->fetch($id);
1327 throw new RestException(404,
'Invoice not found');
1330 $result = $this->invoice->insert_discount($discountid);
1332 throw new RestException(405, $this->invoice->error);
1356 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1358 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1359 throw new RestException(403);
1362 throw new RestException(400,
'Invoice ID is mandatory');
1364 if (empty($discountid)) {
1365 throw new RestException(400,
'Credit ID is mandatory');
1369 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1372 $result = $discount->fetch($discountid);
1374 throw new RestException(404,
'Credit not found');
1377 $result = $discount->link_to_invoice(0, $id);
1379 throw new RestException(405, $discount->error);
1400 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1401 throw new RestException(403);
1404 throw new RestException(400,
'Invoice ID is mandatory');
1408 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1411 $result = $this->invoice->fetch($id);
1413 throw new RestException(404,
'Invoice not found');
1416 $result = $this->invoice->getListOfPayments();
1418 throw new RestException(405, $this->invoice->error);
1446 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1448 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1450 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1451 throw new RestException(403);
1454 throw new RestException(400,
'Invoice ID is mandatory');
1458 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1461 if (isModEnabled(
"bank")) {
1462 if (empty($accountid)) {
1463 throw new RestException(400,
'Account ID is mandatory');
1467 if (empty($paymentid)) {
1468 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1472 $result = $this->invoice->fetch($id);
1474 throw new RestException(404,
'Invoice not found');
1478 $totalpaid = $this->invoice->getSommePaiement();
1479 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1480 $totaldeposits = $this->invoice->getSumDepositsUsed();
1481 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1486 $multicurrency_amounts = array();
1490 $resteapayer =
price2num($resteapayer,
'MT');
1491 $amounts[$id] = (float)
price2num(-1 * (
float) $resteapayer,
'MT');
1493 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1494 $multicurrency_amounts[$id] = (float)
price2num(-1 * (
float) $newvalue,
'MT');
1496 $resteapayer =
price2num($resteapayer,
'MT');
1497 $amounts[$id] = (float) $resteapayer;
1499 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1500 $multicurrency_amounts[$id] = (float) $newvalue;
1504 $paymentobj =
new Paiement($this->db);
1505 if (is_numeric($datepaye)) {
1506 $paymentobj->datepaye = $datepaye;
1510 $paymentobj->amounts = $amounts;
1511 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1512 $paymentobj->paiementid = $paymentid;
1513 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1514 $paymentobj->num_payment = $num_payment;
1515 $paymentobj->note_private = $comment;
1517 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1518 if ($payment_id < 0) {
1519 $this->db->rollback();
1520 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1523 if (isModEnabled(
"bank")) {
1524 $label =
'(CustomerInvoicePayment)';
1526 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1527 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1530 $label =
'(CustomerInvoicePaymentBack)';
1532 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1534 $this->db->rollback();
1535 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1539 $this->db->commit();
1570 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1572 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1574 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1575 throw new RestException(403);
1577 foreach ($arrayofamounts as $id => $amount) {
1579 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1582 throw new RestException(403,
'Access not allowed on invoice ID '.$id.
' for login '.DolibarrApiAccess::$user->login);
1586 if (isModEnabled(
"bank")) {
1587 if (empty($accountid)) {
1588 throw new RestException(400,
'Account ID is mandatory');
1591 if (empty($paymentid)) {
1592 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1598 $multicurrency_amounts = array();
1601 foreach ($arrayofamounts as $id => $amountarray) {
1602 $result = $this->invoice->fetch($id);
1604 $this->db->rollback();
1605 throw new RestException(404,
'Invoice ID '.$id.
' not found');
1608 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1609 $this->db->rollback();
1610 throw new RestException(400,
'Payment in both currency '.$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1613 $is_multicurrency = 0;
1614 $total_ttc = $this->invoice->total_ttc;
1616 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1617 $is_multicurrency = 1;
1618 $total_ttc = $this->invoice->multicurrency_total_ttc;
1622 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1623 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1624 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1625 $remainstopay = $amount =
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1627 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1628 $amount =
price2num($amountarray[
"amount"],
'MT');
1631 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1632 $amount =
price2num($amountarray[
"multicurrency_amount"],
'MT');
1635 if ($amount > $remainstopay && !$accepthigherpayment) {
1636 $this->db->rollback();
1637 throw new RestException(400,
'Payment amount on invoice ID '.$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1641 $amount =
price2num(-1 * (
float) $amount,
'MT');
1644 if ($is_multicurrency) {
1645 $amounts[$id] =
null;
1647 $multicurrency_amounts[$id] = (float) $amount;
1649 $amounts[$id] = (float) $amount;
1651 $multicurrency_amounts[$id] =
null;
1656 $paymentobj =
new Paiement($this->db);
1657 if (is_numeric($datepaye)) {
1658 $paymentobj->datepaye = $datepaye;
1662 $paymentobj->amounts = $amounts;
1663 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1664 $paymentobj->paiementid = $paymentid;
1665 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, $paymentid,
'c_paiement',
'id',
'code', 1);
1666 $paymentobj->num_payment = $num_payment;
1667 $paymentobj->note_private = $comment;
1668 $paymentobj->ref_ext = $ref_ext;
1669 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1670 if ($payment_id < 0) {
1671 $this->db->rollback();
1672 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1674 if (isModEnabled(
"bank")) {
1675 $label =
'(CustomerInvoicePayment)';
1676 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1677 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1680 $label =
'(CustomerInvoicePaymentBack)';
1682 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1684 $this->db->rollback();
1685 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1689 $this->db->commit();
1710 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1712 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1713 throw new RestException(403);
1716 throw new RestException(400,
'Payment ID is mandatory');
1719 $paymentobj =
new Paiement($this->db);
1720 $result = $paymentobj->fetch($id);
1723 throw new RestException(404,
'Payment not found');
1726 if (!empty($num_payment)) {
1727 $result = $paymentobj->update_num($num_payment);
1729 throw new RestException(500,
'Error when updating the payment num');
1736 'message' =>
'Payment updated'
1756 unset(
$object->barcode_type_code);
1757 unset(
$object->barcode_type_label);
1758 unset(
$object->barcode_type_coder);
1775 foreach (Invoices::$FIELDS as $field) {
1776 if (!isset($data[$field])) {
1777 throw new RestException(400,
"$field field missing");
1779 $invoice[$field] = $data[$field];
1818 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1819 throw new RestException(403);
1822 $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1824 throw new RestException(404,
'Template invoice not found');
1828 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1832 if ($contact_list > -1) {
1833 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1834 if (is_array($tmparray)) {
1835 $this->template_invoice->contacts_ids = $tmparray;
1839 $this->template_invoice->fetchObjectLinked();
1859 unset(
$object->barcode_type_code);
1860 unset(
$object->barcode_type_label);
1861 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...
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_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
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.