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(0, $ref,
'', $contact_list);
115 return $this->
_fetch(0,
'', $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);
188 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
190 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
191 throw new RestException(403);
197 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
201 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
202 $search_sale = DolibarrApiAccess::$user->id;
205 $sql =
"SELECT t.rowid";
206 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture AS t";
207 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_extrafields AS ef ON (ef.fk_object = t.rowid)";
208 $sql .=
' WHERE t.entity IN ('.getEntity(
'invoice').
')';
210 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
213 if ($search_sale && $search_sale !=
'-1') {
214 if ($search_sale == -2) {
215 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
216 } elseif ($search_sale > 0) {
217 $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).
")";
221 if ($status ==
'draft') {
222 $sql .=
" AND t.fk_statut IN (0)";
224 if ($status ==
'unpaid') {
225 $sql .=
" AND t.fk_statut IN (1)";
227 if ($status ==
'paid') {
228 $sql .=
" AND t.fk_statut IN (2)";
230 if ($status ==
'cancelled') {
231 $sql .=
" AND t.fk_statut IN (3)";
238 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
243 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
245 $sql .= $this->db->order($sortfield, $sortorder);
250 $offset = $limit * $page;
252 $sql .= $this->db->plimit($limit + 1, $offset);
255 $result = $this->db->query($sql);
258 $num = $this->db->num_rows($result);
259 $min = min($num, ($limit <= 0 ? $num : $limit));
261 $obj = $this->db->fetch_object($result);
262 $invoice_static =
new Facture($this->db);
263 if ($invoice_static->fetch($obj->rowid)) {
265 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
266 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
267 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
268 $invoice_static->remaintopay =
price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits,
'MT');
271 $tmparray = $invoice_static->liste_contact(-1,
'external', 1);
272 if (is_array($tmparray)) {
273 $invoice_static->contacts_ids = $tmparray;
276 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
277 $invoice_static->online_payment_url = getOnlinePaymentUrl(0,
'invoice', $invoice_static->ref);
284 throw new RestException(503,
'Error when retrieve invoice list : '.$this->db->lasterror());
288 if ($pagination_data) {
289 $totalsResult = $this->db->query($sqlTotals);
290 $total = $this->db->fetch_object($totalsResult)->total;
295 $obj_ret[
'data'] = $tmp;
296 $obj_ret[
'pagination'] = [
297 'total' => (int) $total,
299 'page_count' => ceil((
int) $total / $limit),
313 public function post($request_data =
null)
315 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
316 throw new RestException(403,
"Insuffisant rights");
319 $result = $this->
_validate($request_data);
321 foreach ($request_data as $field => $value) {
322 if ($field ===
'caller') {
324 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
328 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
330 if (!array_key_exists(
'date', $request_data)) {
331 $this->invoice->date =
dol_now();
342 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data[
"date_lim_reglement"]) ? 0 : $request_data[
"date_lim_reglement"])) < 0) {
343 throw new RestException(500,
"Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
345 return ((
int) $this->invoice->id);
363 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
365 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
366 throw new RestException(403);
368 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
369 throw new RestException(403);
371 if (empty($orderid)) {
372 throw new RestException(400,
'Order ID is mandatory');
376 $result = $order->fetch($orderid);
378 throw new RestException(404,
'Order not found');
381 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
383 throw new RestException(405, $this->invoice->error);
385 $this->invoice->fetchObjectLinked();
404 require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
406 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
407 throw new RestException(403);
409 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
410 throw new RestException(403);
412 if (empty($contractid)) {
413 throw new RestException(400,
'Contract ID is mandatory');
416 $contract =
new Contrat($this->db);
417 $result = $contract->fetch($contractid);
419 throw new RestException(404,
'Contract not found');
422 $result = $this->invoice->createFromContract($contract, DolibarrApiAccess::$user);
424 throw new RestException(405, $this->invoice->error);
426 $this->invoice->fetchObjectLinked();
440 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
441 throw new RestException(403);
444 $result = $this->invoice->fetch(
$id);
446 throw new RestException(404,
'Invoice not found');
450 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
452 $this->invoice->getLinesArray();
454 foreach ($this->invoice->lines as $line) {
476 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
477 throw new RestException(403);
480 $result = $this->invoice->fetch(
$id);
482 throw new RestException(404,
'Invoice not found');
486 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
489 $request_data = (object) $request_data;
491 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
492 $request_data->label =
sanitizeVal($request_data->label);
494 $updateRes = $this->invoice->updateline(
497 $request_data->subprice,
499 $request_data->remise_percent,
500 $request_data->date_start,
501 $request_data->date_end,
502 $request_data->tva_tx,
503 $request_data->localtax1_tx,
504 $request_data->localtax2_tx,
505 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
506 $request_data->info_bits,
507 $request_data->product_type,
508 $request_data->fk_parent_line,
510 $request_data->fk_fournprice,
511 $request_data->pa_ht,
512 $request_data->label,
513 $request_data->special_code,
514 $request_data->array_options,
515 $request_data->situation_percent,
516 $request_data->fk_unit,
517 $request_data->multicurrency_subprice,
519 $request_data->ref_ext,
523 if ($updateRes > 0) {
524 $result = $this->
get(
$id);
525 unset($result->line);
528 throw new RestException(304, $this->invoice->error);
547 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
548 throw new RestException(403);
551 $result = $this->invoice->fetch(
$id);
554 throw new RestException(404,
'Invoice not found');
557 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
558 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
562 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
565 $result = $this->invoice->add_contact($contactid, $type,
'external');
568 throw new RestException(500,
'Error when added the contact');
574 'message' =>
'Contact linked to the invoice'
595 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
596 throw new RestException(403);
599 $result = $this->invoice->fetch(
$id);
602 throw new RestException(404,
'Invoice not found');
606 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
609 $contacts = $this->invoice->liste_contact();
611 foreach ($contacts as $contact) {
612 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
613 $result = $this->invoice->delete_contact($contact[
'rowid']);
616 throw new RestException(500,
'Error when deleted the contact');
640 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
641 throw new RestException(403);
643 if (empty($lineid)) {
644 throw new RestException(400,
'Line ID is mandatory');
648 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
651 $result = $this->invoice->fetch(
$id);
653 throw new RestException(404,
'Invoice not found');
656 $updateRes = $this->invoice->deleteLine($lineid,
$id);
657 if ($updateRes > 0) {
658 return $this->
get(
$id);
660 throw new RestException(405, $this->invoice->error);
671 public function put(
$id, $request_data =
null)
673 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
674 throw new RestException(403);
677 $result = $this->invoice->fetch(
$id);
679 throw new RestException(404,
'Invoice not found');
683 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
686 foreach ($request_data as $field => $value) {
687 if ($field ==
'id') {
690 if ($field ===
'caller') {
692 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
695 if ($field ==
'array_options' && is_array($value)) {
696 foreach ($value as $index => $val) {
697 $this->invoice->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->invoice);
702 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
705 if ($field ==
'cond_reglement_id') {
706 $this->invoice->date_lim_reglement = $this->invoice->calculate_date_lim_reglement();
711 if (!empty($this->invoice->fk_account)) {
712 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
713 throw new RestException(400, $this->invoice->error);
717 if ($this->invoice->update(DolibarrApiAccess::$user)) {
718 return $this->
get(
$id);
730 public function delete(
$id)
732 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'supprimer')) {
733 throw new RestException(403);
735 $result = $this->invoice->fetch(
$id);
737 throw new RestException(404,
'Invoice not found');
741 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
744 $result = $this->invoice->delete(DolibarrApiAccess::$user);
746 throw new RestException(500,
'Error when deleting invoice');
747 } elseif ($result == 0) {
748 throw new RestException(403,
'Invoice not erasable');
754 'message' =>
'Invoice deleted'
784 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
785 throw new RestException(403);
788 $result = $this->invoice->fetch(
$id);
790 throw new RestException(404,
'Invoice not found');
794 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
797 $request_data = (object) $request_data;
799 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
800 $request_data->label =
sanitizeVal($request_data->label);
803 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
804 $request_data->fk_parent_line = 0;
808 $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);
809 $pa_ht = $marginInfos[0];
811 $updateRes = $this->invoice->addline(
813 $request_data->subprice,
815 $request_data->tva_tx,
816 $request_data->localtax1_tx,
817 $request_data->localtax2_tx,
818 $request_data->fk_product,
819 $request_data->remise_percent,
820 $request_data->date_start,
821 $request_data->date_end,
822 $request_data->fk_code_ventilation,
823 $request_data->info_bits,
824 $request_data->fk_remise_except,
825 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
826 $request_data->subprice,
827 $request_data->product_type,
829 $request_data->special_code,
830 $request_data->origin,
831 $request_data->origin_id,
832 $request_data->fk_parent_line,
833 empty($request_data->fk_fournprice) ? null : $request_data->fk_fournprice,
835 $request_data->label,
836 $request_data->array_options,
837 $request_data->situation_percent,
838 $request_data->fk_prev_id,
839 $request_data->fk_unit,
841 $request_data->ref_ext
844 if ($updateRes < 0) {
845 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
870 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
872 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
873 throw new RestException(403);
875 $result = $this->invoice->fetch(
$id);
877 throw new RestException(404,
'Invoice not found');
881 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
884 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
886 throw new RestException(500,
'Error : '.$this->invoice->error);
889 $result = $this->invoice->fetch(
$id);
891 throw new RestException(404,
'Invoice not found');
895 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
920 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
921 throw new RestException(403);
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);
932 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
934 throw new RestException(304,
'Nothing done.');
937 throw new RestException(500,
'Error : '.$this->invoice->error);
940 $result = $this->invoice->fetch(
$id);
942 throw new RestException(404,
'Invoice not found');
946 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
970 public function validate(
$id, $force_number =
'', $idwarehouse = 0, $notrigger = 0)
972 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
973 throw new RestException(403);
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);
984 $result = $this->invoice->validate(DolibarrApiAccess::$user, $force_number, $idwarehouse, $notrigger);
986 throw new RestException(304,
'Error nothing done. May be object is already validated');
989 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
992 $result = $this->invoice->fetch(
$id);
994 throw new RestException(404,
'Invoice not found');
998 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1002 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
1003 $this->invoice->online_payment_url = getOnlinePaymentUrl(0,
'invoice', $this->invoice->ref);
1025 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1026 throw new RestException(403);
1028 $result = $this->invoice->fetch(
$id);
1030 throw new RestException(404,
'Invoice not found');
1034 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1037 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
1039 throw new RestException(304,
'Error nothing done. May be object is already validated');
1042 throw new RestException(500,
'Error : '.$this->invoice->error);
1046 $result = $this->invoice->fetch(
$id);
1048 throw new RestException(404,
'Invoice not found');
1052 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1074 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1075 throw new RestException(403);
1077 $result = $this->invoice->fetch(
$id);
1079 throw new RestException(404,
'Invoice not found');
1083 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1086 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1088 throw new RestException(304,
'Nothing done');
1091 throw new RestException(500,
'Error : '.$this->invoice->error);
1095 $result = $this->invoice->fetch(
$id);
1097 throw new RestException(404,
'Invoice not found');
1101 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1117 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1119 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1120 throw new RestException(403);
1123 $result = $this->invoice->fetch(
$id);
1125 throw new RestException(404,
'Invoice not found');
1129 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1133 $result = $discountcheck->fetch(0, $this->invoice->id);
1136 throw new RestException(404,
'Discount not found');
1139 throw new RestException(500, $discountcheck->error);
1142 return parent::_cleanObjectDatas($discountcheck);
1160 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1162 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1163 throw new RestException(403);
1166 $result = $this->invoice->fetch(
$id);
1168 throw new RestException(404,
'Invoice not found');
1172 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1175 if ($this->invoice->paye) {
1176 throw new RestException(500,
'Alreay paid');
1179 $this->invoice->fetch(
$id);
1180 $this->invoice->fetch_thirdparty();
1184 $result = $discountcheck->fetch(0, $this->invoice->id);
1196 $amount_ht = $amount_tva = $amount_ttc = array();
1197 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1199 @phan-var-force array<string,float> $amount_ht
1200 @phan-var-force array<string,float> $amount_tva
1201 @phan-var-force array<string,float> $amount_ttc
1202 @phan-var-force array<string,float> $multicurrency_amount_ht
1203 @phan-var-force array<string,float> $multicurrency_amount_tva
1204 @phan-var-force array<string,float> $multicurrency_amount_ttc
1209 foreach ($this->invoice->lines as $line) {
1210 if ($line->product_type < 9 && $line->total_ht != 0) {
1211 if (!array_key_exists($line->tva_tx, $amount_ht)) {
1212 $amount_ht[$line->tva_tx] = 0.0;
1213 $amount_tva[$line->tva_tx] = 0.0;
1214 $amount_ttc[$line->tva_tx] = 0.0;
1215 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
1216 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
1217 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
1220 $amount_ht[$line->tva_tx] += $line->total_ht;
1221 $amount_tva[$line->tva_tx] += $line->total_tva;
1222 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1223 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1224 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1225 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1233 $discount->description =
'(CREDIT_NOTE)';
1235 $discount->description =
'(DEPOSIT)';
1237 $discount->description =
'(EXCESS RECEIVED)';
1239 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1242 $discount->fk_soc = $this->invoice->socid;
1243 $discount->socid = $this->invoice->socid;
1244 $discount->fk_facture_source = $this->invoice->id;
1252 $sql =
'SELECT SUM(pf.amount) as total_payments';
1253 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1254 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1255 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1256 $sql .=
' AND pf.fk_paiement = p.rowid';
1257 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1258 $resql = $this->db->query($sql);
1263 $res = $this->db->fetch_object($resql);
1264 $total_payments = $res->total_payments;
1267 $total_creditnote_and_deposit = 0;
1268 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1269 $sql .=
" re.description, re.fk_facture_source";
1270 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1271 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1272 $resql = $this->db->query($sql);
1273 if (!empty($resql)) {
1274 while ($obj = $this->db->fetch_object($resql)) {
1275 $total_creditnote_and_deposit += $obj->amount_ttc;
1281 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1282 $discount->amount_tva = 0;
1283 $discount->tva_tx = 0;
1285 $result = $discount->create(DolibarrApiAccess::$user);
1291 foreach ($amount_ht as $tva_tx => $xxx) {
1292 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1293 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1294 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1295 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1296 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1297 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1298 $discount->tva_tx = abs((
float) $tva_tx);
1300 $result = $discount->create(DolibarrApiAccess::$user);
1308 if (empty($error)) {
1311 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1313 $this->db->commit();
1315 $this->db->rollback();
1316 throw new RestException(500,
'Could not set paid');
1319 $this->db->commit();
1322 $this->db->rollback();
1323 throw new RestException(500,
'Discount creation error');
1348 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1349 throw new RestException(403);
1352 throw new RestException(400,
'Invoice ID is mandatory');
1354 if (empty($discountid)) {
1355 throw new RestException(400,
'Discount ID is mandatory');
1359 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1362 $result = $this->invoice->fetch(
$id);
1364 throw new RestException(404,
'Invoice not found');
1367 $result = $this->invoice->insert_discount($discountid);
1369 throw new RestException(405, $this->invoice->error);
1393 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1395 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1396 throw new RestException(403);
1399 throw new RestException(400,
'Invoice ID is mandatory');
1401 if (empty($discountid)) {
1402 throw new RestException(400,
'Credit ID is mandatory');
1406 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1409 $result = $discount->fetch($discountid);
1411 throw new RestException(404,
'Credit not found');
1414 $result = $discount->link_to_invoice(0,
$id);
1416 throw new RestException(405, $discount->error);
1437 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1438 throw new RestException(403);
1441 throw new RestException(400,
'Invoice ID is mandatory');
1445 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1448 $result = $this->invoice->fetch(
$id);
1450 throw new RestException(404,
'Invoice not found');
1453 $result = $this->invoice->getListOfPayments();
1455 throw new RestException(405, $this->invoice->error);
1483 public function addPayment(
$id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1485 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1487 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1488 throw new RestException(403);
1491 throw new RestException(400,
'Invoice ID is mandatory');
1495 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1498 if (isModEnabled(
"bank")) {
1499 if (empty($accountid)) {
1500 throw new RestException(400,
'Account ID is mandatory');
1504 if (empty($paymentid)) {
1505 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1509 $result = $this->invoice->fetch(
$id);
1511 throw new RestException(404,
'Invoice not found');
1515 $totalpaid = $this->invoice->getSommePaiement();
1516 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1517 $totaldeposits = $this->invoice->getSumDepositsUsed();
1518 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1523 $multicurrency_amounts = array();
1527 $resteapayer =
price2num($resteapayer,
'MT');
1528 $amounts[
$id] = (float)
price2num(-1 * (
float) $resteapayer,
'MT');
1530 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1531 $multicurrency_amounts[
$id] = (float)
price2num(-1 * (
float) $newvalue,
'MT');
1533 $resteapayer =
price2num($resteapayer,
'MT');
1534 $amounts[
$id] = (float) $resteapayer;
1536 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1537 $multicurrency_amounts[
$id] = (float) $newvalue;
1541 $paymentobj =
new Paiement($this->db);
1542 $paymentobj->datepaye = $datepaye;
1543 $paymentobj->amounts = $amounts;
1544 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1545 $paymentobj->paiementid = $paymentid;
1546 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $paymentid,
'c_paiement',
'id',
'code', 1);
1547 $paymentobj->num_payment = $num_payment;
1548 $paymentobj->note_private = $comment;
1550 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1551 if ($payment_id < 0) {
1552 $this->db->rollback();
1553 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1556 if (isModEnabled(
"bank")) {
1557 $label =
'(CustomerInvoicePayment)';
1559 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1560 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1563 $label =
'(CustomerInvoicePaymentBack)';
1565 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1567 $this->db->rollback();
1568 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1572 $this->db->commit();
1603 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1605 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1607 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1608 throw new RestException(403);
1610 foreach ($arrayofamounts as
$id => $amount) {
1612 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1615 throw new RestException(403,
'Access not allowed on invoice ID '.
$id.
' for login '.DolibarrApiAccess::$user->login);
1619 if (isModEnabled(
"bank")) {
1620 if (empty($accountid)) {
1621 throw new RestException(400,
'Account ID is mandatory');
1624 if (empty($paymentid)) {
1625 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1631 $multicurrency_amounts = array();
1634 foreach ($arrayofamounts as
$id => $amountarray) {
1635 $result = $this->invoice->fetch(
$id);
1637 $this->db->rollback();
1638 throw new RestException(404,
'Invoice ID '.
$id.
' not found');
1641 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1642 $this->db->rollback();
1643 throw new RestException(400,
'Payment in both currency '.
$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1646 $is_multicurrency = 0;
1647 $total_ttc = $this->invoice->total_ttc;
1649 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1650 $is_multicurrency = 1;
1651 $total_ttc = $this->invoice->multicurrency_total_ttc;
1655 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1656 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1657 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1658 $remainstopay = $amount =
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1660 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1661 $amount =
price2num($amountarray[
"amount"],
'MT');
1664 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1665 $amount =
price2num($amountarray[
"multicurrency_amount"],
'MT');
1668 if ($amount > $remainstopay && !$accepthigherpayment) {
1669 $this->db->rollback();
1670 throw new RestException(400,
'Payment amount on invoice ID '.
$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1674 $amount =
price2num(-1 * (
float) $amount,
'MT');
1677 if ($is_multicurrency) {
1678 $amounts[
$id] =
null;
1680 $multicurrency_amounts[
$id] = (float) $amount;
1682 $amounts[
$id] = (float) $amount;
1684 $multicurrency_amounts[
$id] =
null;
1689 $paymentobj =
new Paiement($this->db);
1690 $paymentobj->datepaye = $datepaye;
1691 $paymentobj->amounts = $amounts;
1692 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1693 $paymentobj->paiementid = $paymentid;
1694 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $paymentid,
'c_paiement',
'id',
'code', 1);
1695 $paymentobj->num_payment = $num_payment;
1696 $paymentobj->note_private = $comment;
1697 $paymentobj->ref_ext = $ref_ext;
1698 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1699 if ($payment_id < 0) {
1700 $this->db->rollback();
1701 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1703 if (isModEnabled(
"bank")) {
1704 $label =
'(CustomerInvoicePayment)';
1705 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1706 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1709 $label =
'(CustomerInvoicePaymentBack)';
1711 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1713 $this->db->rollback();
1714 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1718 $this->db->commit();
1739 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1741 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1742 throw new RestException(403);
1745 throw new RestException(400,
'Payment ID is mandatory');
1748 $paymentobj =
new Paiement($this->db);
1749 $result = $paymentobj->fetch(
$id);
1752 throw new RestException(404,
'Payment not found');
1755 if (!empty($num_payment)) {
1756 $result = $paymentobj->update_num($num_payment);
1758 throw new RestException(500,
'Error when updating the payment num');
1765 'message' =>
'Payment updated'
1785 unset(
$object->barcode_type_code);
1786 unset(
$object->barcode_type_label);
1787 unset(
$object->barcode_type_coder);
1804 foreach (Invoices::$FIELDS as $field) {
1805 if (!isset($data[$field])) {
1806 throw new RestException(400,
"$field field missing");
1808 $invoice[$field] = $data[$field];
1847 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1848 throw new RestException(403);
1851 $result = $this->template_invoice->fetch(
$id, $ref, $ref_ext);
1853 throw new RestException(404,
'Template invoice not found');
1857 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1861 if ($contact_list > -1) {
1862 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1863 if (is_array($tmparray)) {
1864 $this->template_invoice->contacts_ids = $tmparray;
1868 $this->template_invoice->fetchObjectLinked();
1888 unset(
$object->barcode_type_code);
1889 unset(
$object->barcode_type_label);
1890 unset(
$object->barcode_type_coder);
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 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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='', $pagination_data=false)
List invoices.
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.
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.