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);
189 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false, $loadlinkedobjects = 0)
191 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
192 throw new RestException(403);
198 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
202 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
203 $search_sale = DolibarrApiAccess::$user->id;
206 $sql =
"SELECT t.rowid";
207 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture AS t";
208 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_extrafields AS ef ON (ef.fk_object = t.rowid)";
209 $sql .=
' WHERE t.entity IN ('.getEntity(
'invoice').
')';
211 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
214 if ($search_sale && $search_sale !=
'-1') {
215 if ($search_sale == -2) {
216 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
217 } elseif ($search_sale > 0) {
218 $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).
")";
222 if ($status ==
'draft') {
223 $sql .=
" AND t.fk_statut IN (0)";
225 if ($status ==
'unpaid') {
226 $sql .=
" AND t.fk_statut IN (1)";
228 if ($status ==
'paid') {
229 $sql .=
" AND t.fk_statut IN (2)";
231 if ($status ==
'cancelled') {
232 $sql .=
" AND t.fk_statut IN (3)";
239 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
244 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
246 $sql .= $this->db->order($sortfield, $sortorder);
251 $offset = $limit * $page;
253 $sql .= $this->db->plimit($limit + 1, $offset);
256 $result = $this->db->query($sql);
259 $num = $this->db->num_rows($result);
260 $min = min($num, ($limit <= 0 ? $num : $limit));
262 $obj = $this->db->fetch_object($result);
263 $invoice_static =
new Facture($this->db);
264 if ($invoice_static->fetch($obj->rowid) > 0) {
266 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
267 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
268 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
269 $invoice_static->remaintopay =
price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits,
'MT');
272 $tmparray = $invoice_static->liste_contact(-1,
'external', 1);
273 if (is_array($tmparray)) {
274 $invoice_static->contacts_ids = $tmparray;
277 if ($loadlinkedobjects) {
279 $invoice_static->fetchObjectLinked();
283 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
284 $invoice_static->online_payment_url = getOnlinePaymentUrl(0,
'invoice', $invoice_static->ref);
291 throw new RestException(503,
'Error when retrieve invoice list : '.$this->db->lasterror());
295 if ($pagination_data) {
296 $totalsResult = $this->db->query($sqlTotals);
297 $total = $this->db->fetch_object($totalsResult)->total;
302 $obj_ret[
'data'] = $tmp;
303 $obj_ret[
'pagination'] = [
304 'total' => (int) $total,
306 'page_count' => ceil((
int) $total / $limit),
320 public function post($request_data =
null)
322 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
323 throw new RestException(403,
"Insuffisant rights");
326 $result = $this->
_validate($request_data);
328 foreach ($request_data as $field => $value) {
329 if ($field ===
'caller') {
331 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
335 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
337 if (!array_key_exists(
'date', $request_data)) {
338 $this->invoice->date =
dol_now();
349 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data[
"date_lim_reglement"]) ? 0 : $request_data[
"date_lim_reglement"])) < 0) {
350 throw new RestException(500,
"Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
352 return ((
int) $this->invoice->id);
370 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
372 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
373 throw new RestException(403);
375 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
376 throw new RestException(403);
378 if (empty($orderid)) {
379 throw new RestException(400,
'Order ID is mandatory');
383 $result = $order->fetch($orderid);
385 throw new RestException(404,
'Order not found');
388 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
390 throw new RestException(405, $this->invoice->error);
392 $this->invoice->fetchObjectLinked();
411 require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
413 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
414 throw new RestException(403);
416 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
417 throw new RestException(403);
419 if (empty($contractid)) {
420 throw new RestException(400,
'Contract ID is mandatory');
423 $contract =
new Contrat($this->db);
424 $result = $contract->fetch($contractid);
426 throw new RestException(404,
'Contract not found');
429 $result = $this->invoice->createFromContract($contract, DolibarrApiAccess::$user);
431 throw new RestException(405, $this->invoice->error);
433 $this->invoice->fetchObjectLinked();
447 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
448 throw new RestException(403);
451 $result = $this->invoice->fetch(
$id);
453 throw new RestException(404,
'Invoice not found');
457 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
459 $this->invoice->getLinesArray();
461 foreach ($this->invoice->lines as $line) {
483 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
484 throw new RestException(403);
487 $result = $this->invoice->fetch(
$id);
489 throw new RestException(404,
'Invoice not found');
493 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
496 $request_data = (object) $request_data;
498 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
499 $request_data->label =
sanitizeVal($request_data->label);
501 $updateRes = $this->invoice->updateline(
504 $request_data->subprice,
506 $request_data->remise_percent,
507 $request_data->date_start,
508 $request_data->date_end,
509 $request_data->tva_tx,
510 $request_data->localtax1_tx,
511 $request_data->localtax2_tx,
512 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
513 $request_data->info_bits,
514 $request_data->product_type,
515 $request_data->fk_parent_line,
517 $request_data->fk_fournprice,
518 $request_data->pa_ht,
519 $request_data->label,
520 $request_data->special_code,
521 $request_data->array_options,
522 $request_data->situation_percent,
523 $request_data->fk_unit,
524 $request_data->multicurrency_subprice,
526 $request_data->ref_ext,
530 if ($updateRes > 0) {
531 $result = $this->
get(
$id);
532 unset($result->line);
535 throw new RestException(304, $this->invoice->error);
554 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
555 throw new RestException(403);
558 $result = $this->invoice->fetch(
$id);
561 throw new RestException(404,
'Invoice not found');
564 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
565 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
569 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
572 $result = $this->invoice->add_contact($contactid, $type,
'external');
575 throw new RestException(500,
'Error when added the contact');
581 'message' =>
'Contact linked to the invoice'
602 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
603 throw new RestException(403);
606 $result = $this->invoice->fetch(
$id);
609 throw new RestException(404,
'Invoice not found');
613 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
616 $contacts = $this->invoice->liste_contact();
618 foreach ($contacts as $contact) {
619 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
620 $result = $this->invoice->delete_contact($contact[
'rowid']);
623 throw new RestException(500,
'Error when deleted the contact');
647 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
648 throw new RestException(403);
650 if (empty($lineid)) {
651 throw new RestException(400,
'Line ID is mandatory');
655 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
658 $result = $this->invoice->fetch(
$id);
660 throw new RestException(404,
'Invoice not found');
663 $updateRes = $this->invoice->deleteLine($lineid,
$id);
664 if ($updateRes > 0) {
665 return $this->
get(
$id);
667 throw new RestException(405, $this->invoice->error);
678 public function put(
$id, $request_data =
null)
680 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
681 throw new RestException(403);
684 $result = $this->invoice->fetch(
$id);
686 throw new RestException(404,
'Invoice not found');
690 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
693 foreach ($request_data as $field => $value) {
694 if ($field ==
'id') {
697 if ($field ===
'caller') {
699 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
702 if ($field ==
'array_options' && is_array($value)) {
703 foreach ($value as $index => $val) {
704 $this->invoice->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->invoice);
709 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
712 if ($field ==
'cond_reglement_id') {
713 $this->invoice->date_lim_reglement = $this->invoice->calculate_date_lim_reglement();
718 if (!empty($this->invoice->fk_account)) {
719 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
720 throw new RestException(400, $this->invoice->error);
724 if ($this->invoice->update(DolibarrApiAccess::$user) > 0) {
725 return $this->
get(
$id);
727 throw new RestException(500, $this->invoice->error);
737 public function delete(
$id)
739 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'supprimer')) {
740 throw new RestException(403);
742 $result = $this->invoice->fetch(
$id);
744 throw new RestException(404,
'Invoice not found');
748 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
751 $result = $this->invoice->delete(DolibarrApiAccess::$user);
753 throw new RestException(500,
'Error when deleting invoice');
754 } elseif ($result == 0) {
755 throw new RestException(403,
'Invoice not erasable');
761 'message' =>
'Invoice deleted'
791 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
792 throw new RestException(403);
795 $result = $this->invoice->fetch(
$id);
797 throw new RestException(404,
'Invoice not found');
801 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
804 $request_data = (object) $request_data;
806 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
807 $request_data->label =
sanitizeVal($request_data->label);
810 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
811 $request_data->fk_parent_line = 0;
815 $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);
816 $pa_ht = $marginInfos[0];
818 $updateRes = $this->invoice->addline(
820 $request_data->subprice,
822 $request_data->tva_tx,
823 $request_data->localtax1_tx,
824 $request_data->localtax2_tx,
825 $request_data->fk_product,
826 $request_data->remise_percent,
827 $request_data->date_start,
828 $request_data->date_end,
829 $request_data->fk_code_ventilation,
830 $request_data->info_bits,
831 $request_data->fk_remise_except,
832 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
833 $request_data->subprice,
834 $request_data->product_type,
836 $request_data->special_code,
837 $request_data->origin,
838 $request_data->origin_id,
839 $request_data->fk_parent_line,
840 empty($request_data->fk_fournprice) ? null : $request_data->fk_fournprice,
842 $request_data->label,
843 $request_data->array_options,
844 $request_data->situation_percent,
845 $request_data->fk_prev_id,
846 $request_data->fk_unit,
848 $request_data->ref_ext
851 if ($updateRes < 0) {
852 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
877 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
879 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
880 throw new RestException(403);
882 $result = $this->invoice->fetch(
$id);
884 throw new RestException(404,
'Invoice not found');
888 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
891 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
893 throw new RestException(500,
'Error : '.$this->invoice->error);
896 $result = $this->invoice->fetch(
$id);
898 throw new RestException(404,
'Invoice not found');
902 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
927 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
928 throw new RestException(403);
930 $result = $this->invoice->fetch(
$id);
932 throw new RestException(404,
'Invoice not found');
936 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
939 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
941 throw new RestException(304,
'Nothing done.');
944 throw new RestException(500,
'Error : '.$this->invoice->error);
947 $result = $this->invoice->fetch(
$id);
949 throw new RestException(404,
'Invoice not found');
953 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
977 public function validate(
$id, $force_number =
'', $idwarehouse = 0, $notrigger = 0)
979 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
980 throw new RestException(403);
982 $result = $this->invoice->fetch(
$id);
984 throw new RestException(404,
'Invoice not found');
988 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
991 $result = $this->invoice->validate(DolibarrApiAccess::$user, $force_number, $idwarehouse, $notrigger);
993 throw new RestException(304,
'Error nothing done. May be object is already validated');
996 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
999 $result = $this->invoice->fetch(
$id);
1001 throw new RestException(404,
'Invoice not found');
1005 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1009 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
1010 $this->invoice->online_payment_url = getOnlinePaymentUrl(0,
'invoice', $this->invoice->ref);
1032 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1033 throw new RestException(403);
1035 $result = $this->invoice->fetch(
$id);
1037 throw new RestException(404,
'Invoice not found');
1041 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1044 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
1046 throw new RestException(304,
'Error nothing done. May be object is already validated');
1049 throw new RestException(500,
'Error : '.$this->invoice->error);
1053 $result = $this->invoice->fetch(
$id);
1055 throw new RestException(404,
'Invoice not found');
1059 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1081 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1082 throw new RestException(403);
1084 $result = $this->invoice->fetch(
$id);
1086 throw new RestException(404,
'Invoice not found');
1090 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1093 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1095 throw new RestException(304,
'Nothing done');
1098 throw new RestException(500,
'Error : '.$this->invoice->error);
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);
1124 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1126 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1127 throw new RestException(403);
1130 $result = $this->invoice->fetch(
$id);
1132 throw new RestException(404,
'Invoice not found');
1136 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1140 $result = $discountcheck->fetch(0, $this->invoice->id);
1143 throw new RestException(404,
'Discount not found');
1146 throw new RestException(500, $discountcheck->error);
1149 return parent::_cleanObjectDatas($discountcheck);
1167 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1169 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1170 throw new RestException(403);
1173 $result = $this->invoice->fetch(
$id);
1175 throw new RestException(404,
'Invoice not found');
1179 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1182 if ($this->invoice->paye) {
1183 throw new RestException(500,
'Alreay paid');
1186 $this->invoice->fetch(
$id);
1187 $this->invoice->fetch_thirdparty();
1191 $result = $discountcheck->fetch(0, $this->invoice->id);
1203 $amount_ht = $amount_tva = $amount_ttc = array();
1204 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1206 @phan-var-force array<string,float> $amount_ht
1207 @phan-var-force array<string,float> $amount_tva
1208 @phan-var-force array<string,float> $amount_ttc
1209 @phan-var-force array<string,float> $multicurrency_amount_ht
1210 @phan-var-force array<string,float> $multicurrency_amount_tva
1211 @phan-var-force array<string,float> $multicurrency_amount_ttc
1216 foreach ($this->invoice->lines as $line) {
1217 if ($line->product_type < 9 && $line->total_ht != 0) {
1218 if (!array_key_exists($line->tva_tx, $amount_ht)) {
1219 $amount_ht[$line->tva_tx] = 0.0;
1220 $amount_tva[$line->tva_tx] = 0.0;
1221 $amount_ttc[$line->tva_tx] = 0.0;
1222 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
1223 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
1224 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
1227 $amount_ht[$line->tva_tx] += $line->total_ht;
1228 $amount_tva[$line->tva_tx] += $line->total_tva;
1229 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1230 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1231 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1232 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1240 $discount->description =
'(CREDIT_NOTE)';
1242 $discount->description =
'(DEPOSIT)';
1244 $discount->description =
'(EXCESS RECEIVED)';
1246 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1249 $discount->fk_soc = $this->invoice->socid;
1250 $discount->socid = $this->invoice->socid;
1251 $discount->fk_facture_source = $this->invoice->id;
1259 $sql =
'SELECT SUM(pf.amount) as total_payments';
1260 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1261 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1262 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1263 $sql .=
' AND pf.fk_paiement = p.rowid';
1264 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1265 $resql = $this->db->query($sql);
1270 $res = $this->db->fetch_object($resql);
1271 $total_payments = $res->total_payments;
1274 $total_creditnote_and_deposit = 0;
1275 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1276 $sql .=
" re.description, re.fk_facture_source";
1277 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1278 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1279 $resql = $this->db->query($sql);
1280 if (!empty($resql)) {
1281 while ($obj = $this->db->fetch_object($resql)) {
1282 $total_creditnote_and_deposit += $obj->amount_ttc;
1288 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1289 $discount->amount_tva = 0;
1290 $discount->tva_tx = 0;
1292 $result = $discount->create(DolibarrApiAccess::$user);
1298 foreach ($amount_ht as $tva_tx => $xxx) {
1299 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1300 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1301 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1302 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1303 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1304 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1305 $discount->tva_tx = abs((
float) $tva_tx);
1307 $result = $discount->create(DolibarrApiAccess::$user);
1315 if (empty($error)) {
1318 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1320 $this->db->commit();
1322 $this->db->rollback();
1323 throw new RestException(500,
'Could not set paid');
1326 $this->db->commit();
1329 $this->db->rollback();
1330 throw new RestException(500,
'Discount creation error');
1355 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1356 throw new RestException(403);
1359 throw new RestException(400,
'Invoice ID is mandatory');
1361 if (empty($discountid)) {
1362 throw new RestException(400,
'Discount ID is mandatory');
1366 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1369 $result = $this->invoice->fetch(
$id);
1371 throw new RestException(404,
'Invoice not found');
1374 $result = $this->invoice->insert_discount($discountid);
1376 throw new RestException(405, $this->invoice->error);
1400 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1402 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1403 throw new RestException(403);
1406 throw new RestException(400,
'Invoice ID is mandatory');
1408 if (empty($discountid)) {
1409 throw new RestException(400,
'Credit ID is mandatory');
1413 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1416 $result = $discount->fetch($discountid);
1418 throw new RestException(404,
'Credit not found');
1421 $result = $discount->link_to_invoice(0,
$id);
1423 throw new RestException(405, $discount->error);
1444 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1445 throw new RestException(403);
1448 throw new RestException(400,
'Invoice ID is mandatory');
1452 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1455 $result = $this->invoice->fetch(
$id);
1457 throw new RestException(404,
'Invoice not found');
1460 $result = $this->invoice->getListOfPayments();
1462 throw new RestException(405, $this->invoice->error);
1490 public function addPayment(
$id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1492 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1494 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1495 throw new RestException(403);
1498 throw new RestException(400,
'Invoice ID is mandatory');
1502 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1505 if (isModEnabled(
"bank")) {
1506 if (empty($accountid)) {
1507 throw new RestException(400,
'Account ID is mandatory');
1511 if (empty($paymentid)) {
1512 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1516 $result = $this->invoice->fetch(
$id);
1518 throw new RestException(404,
'Invoice not found');
1522 $totalpaid = $this->invoice->getSommePaiement();
1523 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1524 $totaldeposits = $this->invoice->getSumDepositsUsed();
1525 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1530 $multicurrency_amounts = array();
1534 $resteapayer =
price2num($resteapayer,
'MT');
1535 $amounts[
$id] = (float)
price2num(-1 * (
float) $resteapayer,
'MT');
1537 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1538 $multicurrency_amounts[
$id] = (float)
price2num(-1 * (
float) $newvalue,
'MT');
1540 $resteapayer =
price2num($resteapayer,
'MT');
1541 $amounts[
$id] = (float) $resteapayer;
1543 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1544 $multicurrency_amounts[
$id] = (float) $newvalue;
1548 $paymentobj =
new Paiement($this->db);
1549 if (is_numeric($datepaye)) {
1550 $paymentobj->datepaye = $datepaye;
1554 $paymentobj->amounts = $amounts;
1555 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1556 $paymentobj->paiementid = $paymentid;
1557 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $paymentid,
'c_paiement',
'id',
'code', 1);
1558 $paymentobj->num_payment = $num_payment;
1559 $paymentobj->note_private = $comment;
1561 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1562 if ($payment_id < 0) {
1563 $this->db->rollback();
1564 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1567 if (isModEnabled(
"bank")) {
1568 $label =
'(CustomerInvoicePayment)';
1570 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1571 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1574 $label =
'(CustomerInvoicePaymentBack)';
1576 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1578 $this->db->rollback();
1579 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1583 $this->db->commit();
1614 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1616 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1618 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1619 throw new RestException(403);
1621 foreach ($arrayofamounts as
$id => $amount) {
1623 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1626 throw new RestException(403,
'Access not allowed on invoice ID '.
$id.
' for login '.DolibarrApiAccess::$user->login);
1630 if (isModEnabled(
"bank")) {
1631 if (empty($accountid)) {
1632 throw new RestException(400,
'Account ID is mandatory');
1635 if (empty($paymentid)) {
1636 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1642 $multicurrency_amounts = array();
1645 foreach ($arrayofamounts as
$id => $amountarray) {
1646 $result = $this->invoice->fetch(
$id);
1648 $this->db->rollback();
1649 throw new RestException(404,
'Invoice ID '.
$id.
' not found');
1652 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1653 $this->db->rollback();
1654 throw new RestException(400,
'Payment in both currency '.
$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1657 $is_multicurrency = 0;
1658 $total_ttc = $this->invoice->total_ttc;
1660 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1661 $is_multicurrency = 1;
1662 $total_ttc = $this->invoice->multicurrency_total_ttc;
1666 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1667 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1668 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1669 $remainstopay = $amount =
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1671 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1672 $amount =
price2num($amountarray[
"amount"],
'MT');
1675 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1676 $amount =
price2num($amountarray[
"multicurrency_amount"],
'MT');
1679 if ($amount > $remainstopay && !$accepthigherpayment) {
1680 $this->db->rollback();
1681 throw new RestException(400,
'Payment amount on invoice ID '.
$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1685 $amount =
price2num(-1 * (
float) $amount,
'MT');
1688 if ($is_multicurrency) {
1689 $amounts[
$id] =
null;
1691 $multicurrency_amounts[
$id] = (float) $amount;
1693 $amounts[
$id] = (float) $amount;
1695 $multicurrency_amounts[
$id] =
null;
1700 $paymentobj =
new Paiement($this->db);
1701 if (is_numeric($datepaye)) {
1702 $paymentobj->datepaye = $datepaye;
1706 $paymentobj->amounts = $amounts;
1707 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1708 $paymentobj->paiementid = $paymentid;
1709 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $paymentid,
'c_paiement',
'id',
'code', 1);
1710 $paymentobj->num_payment = $num_payment;
1711 $paymentobj->note_private = $comment;
1712 $paymentobj->ref_ext = $ref_ext;
1713 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1714 if ($payment_id < 0) {
1715 $this->db->rollback();
1716 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1718 if (isModEnabled(
"bank")) {
1719 $label =
'(CustomerInvoicePayment)';
1720 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1721 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1724 $label =
'(CustomerInvoicePaymentBack)';
1726 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1728 $this->db->rollback();
1729 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1733 $this->db->commit();
1754 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1756 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1757 throw new RestException(403);
1760 throw new RestException(400,
'Payment ID is mandatory');
1763 $paymentobj =
new Paiement($this->db);
1764 $result = $paymentobj->fetch(
$id);
1767 throw new RestException(404,
'Payment not found');
1770 if (!empty($num_payment)) {
1771 $result = $paymentobj->update_num($num_payment);
1773 throw new RestException(500,
'Error when updating the payment num');
1780 'message' =>
'Payment updated'
1800 unset(
$object->barcode_type_code);
1801 unset(
$object->barcode_type_label);
1802 unset(
$object->barcode_type_coder);
1819 foreach (Invoices::$FIELDS as $field) {
1820 if (!isset($data[$field])) {
1821 throw new RestException(400,
"$field field missing");
1823 $invoice[$field] = $data[$field];
1862 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1863 throw new RestException(403);
1866 $result = $this->template_invoice->fetch(
$id, $ref, $ref_ext);
1868 throw new RestException(404,
'Template invoice not found');
1872 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1876 if ($contact_list > -1) {
1877 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1878 if (is_array($tmparray)) {
1879 $this->template_invoice->contacts_ids = $tmparray;
1883 $this->template_invoice->fetchObjectLinked();
1903 unset(
$object->barcode_type_code);
1904 unset(
$object->barcode_type_label);
1905 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.
addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment='', $comment='', $chqemetteur='', $chqbank='', $ref_ext='', $accepthigherpayment=false)
Add a payment to pay partially or completely one or several invoices.
_cleanTemplateObjectDatas($object)
Clean sensible object datas.
deleteContact($id, $contactid, $type)
Delete a contact type of given invoice.
postLine($id, $request_data=null)
Add a line to a given invoice.
_fetchTemplateInvoice($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an invoice object.
_validate($data)
Validate fields before create or update object.
addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment='', $comment='', $chqemetteur='', $chqbank='')
Add payment line to a specific invoice with the remain to pay as amount.
settopaid($id, $close_code='', $close_note='')
Sets an invoice as paid.
settodraft($id, $idwarehouse=-1)
Sets an invoice as draft.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='', $pagination_data=false, $loadlinkedobjects=0)
List invoices.
deleteLine($id, $lineid)
Deletes a line of a given invoice.
Class to manage payments of customer invoices.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
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.