23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture-rec.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
42 public static $FIELDS = array(
54 private $template_invoice;
64 $this->invoice =
new Facture($this->db);
65 $this->template_invoice =
new FactureRec($this->db);
83 public function get(
$id, $contact_list = 1, $properties =
'', $withLines =
true)
85 $invoice = $this->
_fetch(
$id,
'',
'', $contact_list);
88 unset($invoice->lines);
111 return $this->
_fetch(0, $ref,
'', $contact_list);
131 return $this->
_fetch(0,
'', $ref_ext, $contact_list);
147 private function _fetch(
$id, $ref =
'', $ref_ext =
'', $contact_list = 1)
149 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
150 throw new RestException(403);
152 if (empty(
$id) && empty($ref) && empty($ref_ext)) {
153 throw new RestException(400,
'No invoice can be found with no criteria');
155 $result = $this->invoice->fetch(
$id, $ref, $ref_ext);
157 throw new RestException(404,
'Invoice not found');
161 $this->invoice->totalpaid = $this->invoice->getSommePaiement();
162 $this->invoice->totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
163 $this->invoice->totaldeposits = $this->invoice->getSumDepositsUsed();
164 $this->invoice->remaintopay =
price2num($this->invoice->total_ttc - $this->invoice->totalpaid - $this->invoice->totalcreditnotes - $this->invoice->totaldeposits,
'MT');
167 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
171 $this->invoice->getListIdAvoirFromInvoice();
174 if ($contact_list > -1) {
175 $tmparray = $this->invoice->liste_contact(-1,
'external', $contact_list);
176 if (is_array($tmparray)) {
177 $this->invoice->contacts_ids = $tmparray;
179 $tmparray = $this->invoice->liste_contact(-1,
'internal', $contact_list);
180 if (is_array($tmparray)) {
181 $this->invoice->contacts_ids = $tmparray;
185 $this->invoice->fetchObjectLinked();
188 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
189 $this->invoice->online_payment_url = getOnlinePaymentUrl(0,
'invoice', (
string) $this->invoice->ref);
219 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false, $loadlinkedobjects = 0, $withLines =
true)
221 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
222 throw new RestException(403);
228 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
232 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
233 $search_sale = DolibarrApiAccess::$user->id;
236 $sql =
"SELECT t.rowid";
237 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture AS t";
238 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = t.fk_soc)";
239 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_extrafields AS ef ON (ef.fk_object = t.rowid)";
240 $sql .=
' WHERE t.entity IN ('.getEntity(
'invoice').
')';
242 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
245 if ($search_sale && $search_sale !=
'-1') {
246 if ($search_sale == -2) {
247 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
248 } elseif ($search_sale > 0) {
249 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
253 if ((
string) $status !==
'' && !in_array($status, array(
'draft',
'unpaid',
'paid',
'cancelled'),
true)) {
254 throw new RestException(400,
"Bad value for parameter status. Expected one of: draft, unpaid, paid, cancelled");
257 if ($status ==
'draft') {
258 $sql .=
" AND t.fk_statut IN (0)";
260 if ($status ==
'unpaid') {
261 $sql .=
" AND t.fk_statut IN (1)";
263 if ($status ==
'paid') {
264 $sql .=
" AND t.fk_statut IN (2)";
266 if ($status ==
'cancelled') {
267 $sql .=
" AND t.fk_statut IN (3)";
274 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
279 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
281 $sql .= $this->db->order($sortfield, $sortorder);
286 $offset = $limit * $page;
288 $sql .= $this->db->plimit($limit + 1, $offset);
291 $result = $this->db->query($sql);
294 $num = $this->db->num_rows($result);
295 $min = min($num, ($limit <= 0 ? $num : $limit));
297 $obj = $this->db->fetch_object($result);
298 $invoice_static =
new Facture($this->db);
299 if ($invoice_static->fetch($obj->rowid) > 0) {
301 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
302 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
303 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
304 $invoice_static->remaintopay =
price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits,
'MT');
307 $invoice_static->getListIdAvoirFromInvoice();
310 $tmparray = $invoice_static->liste_contact(-1,
'external', 1);
311 if (is_array($tmparray)) {
312 $invoice_static->contacts_ids = $tmparray;
315 if ($loadlinkedobjects) {
317 $invoice_static->fetchObjectLinked();
321 unset($invoice_static->lines);
325 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
326 $invoice_static->online_payment_url = getOnlinePaymentUrl(0,
'invoice', (
string) $invoice_static->ref);
333 throw new RestException(503,
'Error when retrieve invoice list : '.$this->db->lasterror());
337 if ($pagination_data) {
338 $totalsResult = $this->db->query($sqlTotals);
339 $total = $this->db->fetch_object($totalsResult)->total;
344 $obj_ret[
'data'] = $tmp;
345 $obj_ret[
'pagination'] = [
346 'total' => (int) $total,
348 'page_count' => ceil((
int) $total / $limit),
366 public function post($request_data =
null)
369 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
370 throw new RestException(403,
"Insufficiant rights");
373 if (!is_array($request_data)) {
374 $request_data = array();
381 $socid = (int) $request_data[
'socid'];
382 $thirdpartytmp =
new Societe($this->db);
383 $thirdparty_result = $thirdpartytmp->fetch($socid);
384 if ($thirdparty_result < 1) {
385 throw new RestException(404,
'Thirdparty with id='.$socid.
' not found or not allowed');
388 throw new RestException(404,
'Thirdparty with id='.$thirdpartytmp->id.
' not found or not allowed');
391 foreach ($request_data as $field => $value) {
392 if ($field ===
'caller') {
394 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
397 if ($field ==
'id') {
398 throw new RestException(400,
'Creating with id field is forbidden');
400 if ($field ==
'entity' && ((
int) $value) != ((
int)
$conf->entity)) {
401 throw new RestException(403,
'Creating with entity='.((
int) $value).
' MUST be the same entity='.((
int)
$conf->entity).
' as your API user/key belongs to');
404 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
406 if (!array_key_exists(
'date', $request_data)) {
407 $this->invoice->date =
dol_now();
418 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data[
"date_lim_reglement"]) ? 0 : $request_data[
"date_lim_reglement"])) < 0) {
419 throw new RestException(500,
"Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
421 return ((
int) $this->invoice->id);
442 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
444 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
445 throw new RestException(403);
447 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
448 throw new RestException(403);
450 if (empty($orderid)) {
451 throw new RestException(400,
'Order ID is mandatory');
454 throw new RestException(403,
'Access not allowed on order for login '.DolibarrApiAccess::$user->login);
458 $result = $order->fetch($orderid);
460 throw new RestException(404,
'Order not found');
466 throw new RestException(405,
'Order '.$order->ref.
' is not eligible for invoicing: its status does not allow creating an invoice');
469 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
471 throw new RestException(405, $this->invoice->errorsToString());
473 $this->invoice->fetchObjectLinked();
494 require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
496 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
497 throw new RestException(403);
499 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
500 throw new RestException(403);
502 if (empty($contractid)) {
503 throw new RestException(400,
'Contract ID is mandatory');
506 $contract =
new Contrat($this->db);
507 $result = $contract->fetch($contractid);
509 throw new RestException(404,
'Contract not found');
512 throw new RestException(403,
'Access to contract '.$contract->id.
' not allowed for login '.DolibarrApiAccess::$user->login);
515 $result = $this->invoice->createFromContract($contract, DolibarrApiAccess::$user);
517 throw new RestException(405, $this->invoice->errorsToString());
519 $this->invoice->fetchObjectLinked();
537 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
538 throw new RestException(403);
541 $result = $this->invoice->fetch(
$id);
543 throw new RestException(404,
'Invoice not found');
547 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
549 $this->invoice->getLinesArray();
551 foreach ($this->invoice->lines as $line) {
577 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
578 throw new RestException(403);
581 $result = $this->invoice->fetch(
$id);
583 throw new RestException(404,
'Invoice not found');
587 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
590 $request_data = (object) $request_data;
592 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
593 $request_data->label =
sanitizeVal($request_data->label);
596 $result = $invoiceline->fetch($lineid);
598 throw new RestException(404,
'Invoice line not found');
601 if ($invoiceline->fk_facture !=
$id) {
602 throw new RestException(403,
'Line does not belong to this invoice');
605 $updateRes = $this->invoice->updateline(
608 $request_data->subprice,
610 $request_data->remise_percent,
611 $request_data->date_start,
612 $request_data->date_end,
613 $request_data->tva_tx,
614 $request_data->localtax1_tx,
615 $request_data->localtax2_tx,
616 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
617 $request_data->info_bits,
618 $request_data->product_type,
619 $request_data->fk_parent_line,
621 $request_data->fk_fournprice,
622 $request_data->pa_ht,
623 $request_data->label,
624 $request_data->special_code,
625 $request_data->array_options,
626 $request_data->situation_percent,
627 $request_data->fk_unit,
628 $request_data->multicurrency_subprice,
630 $request_data->ref_ext,
634 if ($updateRes > 0) {
635 $result = $this->
get(
$id);
636 unset($result->line);
639 throw new RestException(304, $this->invoice->errorsToString());
662 public function postContact(
$id, $contactid, $type, $source =
'external', $notrigger = 0)
664 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
665 throw new RestException(403);
669 if (empty($source)) {
670 throw new RestException(400,
'Source can not be empty');
672 $sql_distinct_source =
"SELECT DISTINCT source";
673 $sql_distinct_source .=
" FROM ".MAIN_DB_PREFIX.
"c_type_contact";
674 $sql_distinct_source .=
" WHERE element LIKE 'facture'";
675 $sql_distinct_source .=
" AND source is NOT NULL";
676 $sql_distinct_source .=
" AND active != 0";
677 $source_result = $this->db->query($sql_distinct_source);
678 $source_array = array();
680 if ($source_result) {
681 $num = $this->db->num_rows($source_result);
684 $obj = $this->db->fetch_object($source_result);
685 $source_kind = (string) $obj->source;
686 array_push($source_array, $source_kind);
691 throw new RestException(503,
'Error when retrieving a list of invoice contact sources: '.$this->db->lasterror());
693 if (!in_array($source, (array) $source_array,
true)) {
694 throw new RestException(400,
'Combo of Source='.$source.
' and Type='.$type.
' not found in dictionary with active invoice contact types');
699 throw new RestException(400,
'type can not be empty');
702 $sql_distinct_type =
"SELECT DISTINCT code";
703 $sql_distinct_type .=
" FROM ".MAIN_DB_PREFIX.
"c_type_contact";
704 $sql_distinct_type .=
" WHERE element LIKE 'facture'";
705 $sql_distinct_type .=
" AND source='".$this->db->escape($source).
"'";
706 $sql_distinct_type .=
" AND code is NOT NULL";
707 $sql_distinct_type .=
" AND active != 0";
708 $type_result = $this->db->query($sql_distinct_type);
709 $type_array = array();
712 $num = $this->db->num_rows($type_result);
715 $obj = $this->db->fetch_object($type_result);
717 $type_kind = (string) $obj->code;
718 array_push($type_array, $type_kind);
723 throw new RestException(503,
'Error when retrieving a list of invoice contact types: '.$this->db->lasterror());
725 if (!in_array($type, (array) $type_array,
true)) {
726 throw new RestException(400,
'Combo of Type='.$type.
' and Source='.$source.
' not found in dictionary with active invoice contact types');
730 $result = $this->invoice->fetch(
$id);
732 throw new RestException(404,
'Invoice not found');
735 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
738 $result = $this->invoice->add_contact($contactid, $type, $source, $notrigger);
741 throw new RestException(400,
'Already exists: Contact='.$contactid.
' is already linked to the invoice='.
$id.
' as source='.$source.
' and type='.$type);
742 } elseif ($result == -1) {
743 throw new RestException(400,
'Wrong contact='.$contactid);
744 } elseif ($result == -2) {
745 throw new RestException(400,
'Wrong type='.$type);
746 } elseif ($result == -3) {
747 throw new RestException(400,
'Not allowed contacts');
748 } elseif ($result == -4) {
749 throw new RestException(400,
'ErrorCommercialNotAllowedForThirdparty');
750 } elseif ($result == -5) {
751 throw new RestException(400,
'Trigger failed');
752 } elseif ($result == -6) {
753 throw new RestException(400,
'DB_ERROR_RECORD_ALREADY_EXISTS');
754 } elseif ($result == -7) {
755 throw new RestException(400,
'Some other error');
759 throw new RestException(500,
'Error when added the contact');
765 'message' =>
'Contact='.$contactid.
' linked to the invoice='.
$id.
' as '.$source.
' '.$type
787 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
788 throw new RestException(403);
791 $result = $this->invoice->fetch(
$id);
793 throw new RestException(404,
'Invoice not found');
797 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
800 $contacts = $this->invoice->liste_contact(-1,
'external', 0, $type);
801 $socpeoples = $this->invoice->liste_contact(-1,
'internal', 0, $type);
803 $contacts = array_merge($contacts, $socpeoples);
826 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
827 throw new RestException(403);
830 $result = $this->invoice->fetch(
$id);
833 throw new RestException(404,
'Invoice not found');
837 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
840 $contacts = $this->invoice->liste_contact();
842 foreach ($contacts as $contact) {
843 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
844 $result = $this->invoice->delete_contact($contact[
'rowid']);
847 throw new RestException(500,
'Error when deleted the contact');
873 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
874 throw new RestException(403);
876 if (empty($lineid)) {
877 throw new RestException(400,
'Line ID is mandatory');
881 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
884 $result = $this->invoice->fetch(
$id);
886 throw new RestException(404,
'Invoice not found');
888 if ($this->invoice->status != 0) {
889 throw new RestException(403,
'Invoice not in Draft Status : '.$this->invoice->getLibStatut(1));
892 $updateRes = $this->invoice->deleteLine($lineid,
$id);
893 if ($updateRes > 0) {
894 return $this->
get(
$id);
896 throw new RestException(405, $this->invoice->errorsToString());
911 public function put(
$id, $request_data =
null)
913 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
914 throw new RestException(403);
917 throw new RestException(400,
'No invoice with id=0 can exist');
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);
928 foreach ($request_data as $field => $value) {
929 if ($field ==
'id') {
932 if ($field ===
'caller') {
934 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
937 if ($field ==
'array_options' && is_array($value)) {
938 foreach ($value as $index => $val) {
944 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
947 if ($field ==
'cond_reglement_id') {
948 $this->invoice->date_lim_reglement = $this->invoice->calculate_date_lim_reglement();
953 if (!empty($this->invoice->fk_account)) {
954 if ($this->invoice->setBankAccount((
int) $this->invoice->fk_account) == 0) {
955 throw new RestException(400, $this->invoice->errorsToString());
959 if ($this->invoice->update(DolibarrApiAccess::$user) > 0) {
960 return $this->
get(
$id);
962 throw new RestException(500, $this->invoice->errorsToString());
976 public function delete(
$id)
978 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'supprimer')) {
979 throw new RestException(403);
982 throw new RestException(400,
'No invoice with id=0 can exist');
984 $result = $this->invoice->fetch(
$id);
986 throw new RestException(404,
'Invoice not found');
990 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
993 $result = $this->invoice->delete(DolibarrApiAccess::$user);
995 throw new RestException(500,
'Error when deleting invoice');
996 } elseif ($result == 0) {
997 throw new RestException(403,
'Invoice not erasable');
1003 'message' =>
'Invoice deleted'
1037 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1038 throw new RestException(403);
1041 $result = $this->invoice->fetch(
$id);
1043 throw new RestException(404,
'Invoice not found');
1047 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1050 $request_data = (object) $request_data;
1052 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
1053 $request_data->label =
sanitizeVal($request_data->label);
1056 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
1057 $request_data->fk_parent_line = 0;
1061 $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);
1062 $pa_ht = $marginInfos[0];
1064 $updateRes = $this->invoice->addline(
1065 $request_data->desc,
1066 $request_data->subprice,
1068 $request_data->tva_tx,
1069 $request_data->localtax1_tx,
1070 $request_data->localtax2_tx,
1071 $request_data->fk_product,
1072 $request_data->remise_percent,
1073 $request_data->date_start,
1074 $request_data->date_end,
1075 $request_data->fk_code_ventilation,
1076 $request_data->info_bits,
1077 $request_data->fk_remise_except,
1078 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
1079 $request_data->subprice,
1080 $request_data->product_type,
1081 $request_data->rang,
1082 $request_data->special_code,
1083 $request_data->origin,
1084 $request_data->origin_id,
1085 $request_data->fk_parent_line,
1086 empty($request_data->fk_fournprice) ? null : $request_data->fk_fournprice,
1088 $request_data->label,
1089 $request_data->array_options,
1090 $request_data->situation_percent,
1091 $request_data->fk_prev_id,
1092 $request_data->fk_unit,
1094 $request_data->ref_ext
1097 if ($updateRes < 0) {
1098 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->errorsToString());
1124 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
1126 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1127 throw new RestException(403);
1129 $result = $this->invoice->fetch(
$id);
1131 throw new RestException(404,
'Invoice not found');
1135 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1138 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1140 throw new RestException(500,
'Error : '.$this->invoice->errorsToString());
1143 $result = $this->invoice->fetch(
$id);
1145 throw new RestException(404,
'Invoice not found');
1176 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1177 throw new RestException(403);
1179 $result = $this->invoice->fetch(
$id);
1181 throw new RestException(404,
'Invoice not found');
1185 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1188 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
1190 throw new RestException(304,
'Nothing done.');
1193 throw new RestException(500,
'Error : '.$this->invoice->errorsToString());
1196 $result = $this->invoice->fetch(
$id);
1198 throw new RestException(404,
'Invoice not found');
1224 public function validate(
$id, $force_number =
'', $idwarehouse = 0, $notrigger = 0)
1226 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1227 throw new RestException(403);
1229 $result = $this->invoice->fetch(
$id);
1231 throw new RestException(404,
'Invoice not found');
1235 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1238 $result = $this->invoice->validate(DolibarrApiAccess::$user, $force_number, $idwarehouse, $notrigger);
1240 throw new RestException(304,
'Error nothing done. May be object is already validated');
1243 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->errorsToString());
1246 $result = $this->invoice->fetch(
$id);
1248 throw new RestException(404,
'Invoice not found');
1257 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
1258 $this->invoice->online_payment_url = getOnlinePaymentUrl(0,
'invoice', (
string) $this->invoice->ref);
1282 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1283 throw new RestException(403);
1285 $result = $this->invoice->fetch(
$id);
1287 throw new RestException(404,
'Invoice not found');
1291 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1294 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
1296 throw new RestException(304,
'Error nothing done. May be object is already validated');
1299 throw new RestException(500,
'Error : '.$this->invoice->errorsToString());
1303 $result = $this->invoice->fetch(
$id);
1305 throw new RestException(404,
'Invoice not found');
1334 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1335 throw new RestException(403);
1337 $result = $this->invoice->fetch(
$id);
1339 throw new RestException(404,
'Invoice not found');
1343 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1346 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1348 throw new RestException(304,
'Nothing done');
1351 throw new RestException(500,
'Error : '.$this->invoice->errorsToString());
1355 $result = $this->invoice->fetch(
$id);
1357 throw new RestException(404,
'Invoice not found');
1380 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1382 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1383 throw new RestException(403);
1386 $result = $this->invoice->fetch(
$id);
1388 throw new RestException(404,
'Invoice not found');
1392 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1396 $result = $discountcheck->fetch(0, $this->invoice->id);
1399 throw new RestException(404,
'Discount not found');
1402 throw new RestException(500, $discountcheck->errorsToString());
1405 return parent::_cleanObjectDatas($discountcheck);
1425 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1427 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1428 throw new RestException(403);
1431 $result = $this->invoice->fetch(
$id);
1433 throw new RestException(404,
'Invoice not found');
1437 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1440 if ($this->invoice->paye) {
1441 throw new RestException(500,
'Alreay paid');
1444 $this->invoice->fetch(
$id);
1445 $this->invoice->fetch_thirdparty();
1449 $result = $discountcheck->fetch(0, $this->invoice->id);
1461 $amount_ht = $amount_tva = $amount_ttc = array();
1462 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1464 @phan-var-force array<string,float> $amount_ht
1465 @phan-var-force array<string,float> $amount_tva
1466 @phan-var-force array<string,float> $amount_ttc
1467 @phan-var-force array<string,float> $multicurrency_amount_ht
1468 @phan-var-force array<string,float> $multicurrency_amount_tva
1469 @phan-var-force array<string,float> $multicurrency_amount_ttc
1474 foreach ($this->invoice->lines as $line) {
1475 if ($line->product_type < 9 && $line->total_ht != 0) {
1476 if (!array_key_exists($line->tva_tx, $amount_ht)) {
1477 $amount_ht[$line->tva_tx] = 0.0;
1478 $amount_tva[$line->tva_tx] = 0.0;
1479 $amount_ttc[$line->tva_tx] = 0.0;
1480 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
1481 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
1482 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
1485 $amount_ht[$line->tva_tx] += $line->total_ht;
1486 $amount_tva[$line->tva_tx] += $line->total_tva;
1487 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1488 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1489 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1490 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1498 $discount->description =
'(CREDIT_NOTE)';
1500 $discount->description =
'(DEPOSIT)';
1502 $discount->description =
'(EXCESS RECEIVED)';
1504 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1507 $discount->fk_soc = $this->invoice->socid;
1508 $discount->socid = $this->invoice->socid;
1509 $discount->fk_facture_source = $this->invoice->id;
1517 $sql =
'SELECT SUM(pf.amount) as total_payments';
1518 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1519 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1520 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1521 $sql .=
' AND pf.fk_paiement = p.rowid';
1522 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1523 $resql = $this->db->query($sql);
1525 dol_print_error($this->db);
1528 $res = $this->db->fetch_object($resql);
1529 $total_payments = $res->total_payments;
1532 $total_creditnote_and_deposit = 0;
1533 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1534 $sql .=
" re.description, re.fk_facture_source";
1535 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1536 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1537 $resql = $this->db->query($sql);
1538 if (!empty($resql)) {
1539 while ($obj = $this->db->fetch_object($resql)) {
1540 $total_creditnote_and_deposit += $obj->amount_ttc;
1543 dol_print_error($this->db);
1546 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1547 $discount->total_ht = $discount->total_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1548 $discount->amount_tva = 0;
1549 $discount->total_tva = 0;
1550 $discount->tva_tx = 0;
1552 $result = $discount->create(DolibarrApiAccess::$user);
1558 foreach ($amount_ht as $tva_tx => $xxx) {
1559 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1560 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1561 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1562 $discount->total_ht = abs($amount_ht[$tva_tx]);
1563 $discount->total_tva = abs($amount_tva[$tva_tx]);
1564 $discount->total_ttc = abs($amount_ttc[$tva_tx]);
1565 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1566 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1567 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1568 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$tva_tx]);
1569 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$tva_tx]);
1570 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1571 $discount->tva_tx = abs((
float) $tva_tx);
1573 $result = $discount->create(DolibarrApiAccess::$user);
1581 if (empty($error)) {
1584 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1586 $this->db->commit();
1588 $this->db->rollback();
1589 throw new RestException(500,
'Could not set paid');
1592 $this->db->commit();
1595 $this->db->rollback();
1596 throw new RestException(500,
'Discount creation error');
1621 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1623 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1624 throw new RestException(403);
1628 $result = $this->invoice->fetch(
$id);
1630 throw new RestException(404,
'Invoice not found');
1634 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1639 $result = $discountcheck->fetch(0, $this->invoice->id);
1642 throw new RestException(404,
'Discount not found');
1645 throw new RestException(500, $discountcheck->errorsToString());
1648 if (!empty($discountcheck->fk_facture) || !empty($discountcheck->fk_facture_line)) {
1649 throw new RestException(409,
'Credit used');
1654 $result = $discountcheck->delete(DolibarrApiAccess::$user);
1656 $this->db->rollback();
1657 throw new RestException(500,
'Discount deletion error');
1661 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1663 $this->db->rollback();
1664 throw new RestException(500,
'Could not set unpaid');
1668 $this->db->commit();
1693 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1694 throw new RestException(403);
1697 throw new RestException(400,
'Invoice ID is mandatory');
1699 if (empty($discountid)) {
1700 throw new RestException(400,
'Discount ID is mandatory');
1704 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1707 $result = $this->invoice->fetch(
$id);
1709 throw new RestException(404,
'Invoice not found');
1712 $result = $this->invoice->insert_discount($discountid);
1714 throw new RestException(405, $this->invoice->errorsToString());
1740 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1742 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1743 throw new RestException(403);
1746 throw new RestException(400,
'Invoice ID is mandatory');
1748 if (empty($discountid)) {
1749 throw new RestException(400,
'Credit ID is mandatory');
1753 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1756 $result = $discount->fetch($discountid);
1758 throw new RestException(404,
'Credit not found');
1761 $result = $discount->link_to_invoice(0,
$id);
1763 throw new RestException(405, $discount->errorsToString());
1788 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1789 throw new RestException(403);
1792 throw new RestException(400,
'Invoice ID is mandatory');
1796 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1799 $result = $this->invoice->fetch(
$id);
1801 throw new RestException(404,
'Invoice not found');
1804 $result = $this->invoice->getListOfPayments();
1805 if ($this->invoice->errorsToString() !==
'') {
1806 throw new RestException(405, $this->invoice->errorsToString());
1836 public function addPayment(
$id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1838 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1840 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'paiement')) {
1841 throw new RestException(403);
1844 throw new RestException(400,
'Invoice ID is mandatory');
1848 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1852 if (empty($accountid)) {
1853 throw new RestException(400,
'Account ID is mandatory');
1857 if (empty($paymentid)) {
1858 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1862 $result = $this->invoice->fetch(
$id);
1864 throw new RestException(404,
'Invoice not found');
1868 $totalpaid = $this->invoice->getSommePaiement();
1869 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1870 $totaldeposits = $this->invoice->getSumDepositsUsed();
1875 $multicurrency_amounts = array();
1879 $resteapayer =
price2num($this->invoice->total_ttc + $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1880 $amounts[
$id] = (float)
price2num(-1 * abs((
float) $resteapayer),
'MT');
1882 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1883 $multicurrency_amounts[
$id] = (float)
price2num(-1 * (
float) $newvalue,
'MT');
1885 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1886 $amounts[
$id] = (float) $resteapayer;
1888 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1889 $multicurrency_amounts[
$id] = (float) $newvalue;
1893 $paymentobj =
new Paiement($this->db);
1894 if (is_numeric($datepaye)) {
1895 $paymentobj->datepaye = $datepaye;
1899 $paymentobj->amounts = $amounts;
1900 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1901 $paymentobj->paiementid = $paymentid;
1902 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $paymentid,
'c_paiement',
'id',
'code', 1);
1903 $paymentobj->num_payment = $num_payment;
1904 $paymentobj->note_private = $comment;
1906 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1907 if ($payment_id < 0) {
1908 $this->db->rollback();
1909 throw new RestException(400,
'Payment error : '.$paymentobj->errorsToString());
1913 $label =
'(CustomerInvoicePayment)';
1915 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1916 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1919 $label =
'(CustomerInvoicePaymentBack)';
1921 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1923 $this->db->rollback();
1924 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->errorsToString());
1928 $this->db->commit();
1963 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1965 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1967 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'paiement')) {
1968 throw new RestException(403);
1970 foreach ($arrayofamounts as
$id => $amount) {
1972 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1975 throw new RestException(403,
'Access not allowed on invoice ID '.
$id.
' for login '.DolibarrApiAccess::$user->login);
1980 if (empty($accountid)) {
1981 throw new RestException(400,
'Account ID is mandatory');
1984 if (empty($paymentid)) {
1985 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1991 $multicurrency_amounts = array();
1994 foreach ($arrayofamounts as
$id => $amountarray) {
1996 $result = $this->invoice->fetch(
$id);
1998 $this->db->rollback();
1999 throw new RestException(404,
'Invoice ID '.
$id.
' not found');
2002 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
2003 $this->db->rollback();
2004 throw new RestException(400,
'Payment in both currency '.
$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
2007 $is_multicurrency = 0;
2008 $total_ttc = $this->invoice->total_ttc;
2010 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
2011 $is_multicurrency = 1;
2012 $total_ttc = $this->invoice->multicurrency_total_ttc;
2016 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
2017 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
2018 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
2019 $remainstopay = $amount = (float)
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
2021 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
2022 $amount = (float)
price2num($amountarray[
"amount"],
'MT');
2025 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
2026 $amount = (float)
price2num($amountarray[
"multicurrency_amount"],
'MT');
2029 if (abs($amount) > abs($remainstopay) && !$accepthigherpayment) {
2030 $this->db->rollback();
2031 throw new RestException(400,
'Payment amount on invoice ID '.
$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
2035 $amount = (float)
price2num(-1 * abs((
float) $amount),
'MT');
2038 if ($is_multicurrency) {
2039 $amounts[
$id] =
null;
2041 $multicurrency_amounts[
$id] = (float) $amount;
2043 $amounts[
$id] = (float) $amount;
2045 $multicurrency_amounts[
$id] =
null;
2050 $paymentobj =
new Paiement($this->db);
2051 if (is_numeric($datepaye)) {
2052 $paymentobj->datepaye = $datepaye;
2056 $paymentobj->amounts = $amounts;
2057 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
2058 $paymentobj->paiementid = $paymentid;
2059 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $paymentid,
'c_paiement',
'id',
'code', 1);
2060 $paymentobj->num_payment = $num_payment;
2061 $paymentobj->note_private = $comment;
2062 $paymentobj->ref_ext = $ref_ext;
2063 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
2064 if ($payment_id < 0) {
2065 $this->db->rollback();
2066 throw new RestException(400,
'Payment error : '.$paymentobj->errorsToString());
2069 $label =
'(CustomerInvoicePayment)';
2070 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
2071 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
2074 $label =
'(CustomerInvoicePaymentBack)';
2076 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
2078 $this->db->rollback();
2079 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->errorsToString());
2083 $this->db->commit();
2108 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
2110 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'paiement')) {
2111 throw new RestException(403);
2114 throw new RestException(400,
'Payment ID is mandatory');
2117 $paymentobj =
new Paiement($this->db);
2118 $result = $paymentobj->fetch(
$id);
2121 throw new RestException(404,
'Payment not found');
2125 $tmparray = $paymentobj->getBillsArray();
2126 foreach ($tmparray as $tmpinvoiceid) {
2128 throw new RestException(403,
'Payment is on invoices that are not all allowed for login '.DolibarrApiAccess::$user->login);
2132 if (!empty($num_payment)) {
2133 $result = $paymentobj->update_num($num_payment);
2135 throw new RestException(500,
'Error when updating the payment num');
2142 'message' =>
'Payment updated'
2165 unset(
$object->barcode_type_code);
2166 unset(
$object->barcode_type_label);
2167 unset(
$object->barcode_type_coder);
2183 if ($data ===
null) {
2187 foreach (Invoices::$FIELDS as $field) {
2188 if (!isset($data[$field])) {
2189 throw new RestException(400,
"$field field missing");
2191 $invoice[$field] = $data[$field];
2245 public function indexTemplateInvoices($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false, $loadlinkedobjects = 0, $withLines =
true)
2247 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
2248 throw new RestException(403);
2254 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
2259 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
2260 $search_sale = DolibarrApiAccess::$user->id;
2263 $sql =
"SELECT t.rowid";
2264 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_rec AS t";
2265 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = t.fk_soc)";
2266 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_rec_extrafields AS ef ON (ef.fk_object = t.rowid)";
2267 $sql .=
' WHERE t.entity IN ('.getEntity(
'invoice').
')';
2269 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
2273 if ($search_sale && $search_sale !=
'-1') {
2274 if ($search_sale == -2) {
2275 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux AS sc WHERE sc.fk_soc = t.fk_soc)";
2276 } elseif ($search_sale > 0) {
2277 $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).
")";
2282 if ((
string) $status !==
'' && !in_array($status, array(
'draft',
'active',
'suspended'),
true)) {
2283 throw new RestException(400,
"Bad value for parameter status. Expected one of: draft, active, suspended");
2286 if ($status ==
'active') {
2287 $sql .=
" AND t.suspended = 0 AND t.frequency IS NOT NULL";
2289 if ($status ==
'suspended') {
2290 $sql .=
" AND t.suspended = 1 AND t.frequency IS NOT NULL";
2292 if ($status ==
'draft') {
2293 $sql .=
" AND t.frequency IS NULL";
2299 if ($errormessage) {
2300 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
2305 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
2307 $sql .= $this->db->order($sortfield, $sortorder);
2312 $offset = $limit * $page;
2314 $sql .= $this->db->plimit($limit + 1, $offset);
2317 $result = $this->db->query($sql);
2320 $num = $this->db->num_rows($result);
2321 $min = min($num, ($limit <= 0 ? $num : $limit));
2323 $obj = $this->db->fetch_object($result);
2325 if ($factureRec->fetch($obj->rowid) > 0) {
2326 if ($loadlinkedobjects) {
2328 $factureRec->fetchObjectLinked();
2332 unset($factureRec->lines);
2340 throw new RestException(503,
'Error when retrieving recurring invoice templates: '.$this->db->lasterror());
2344 if ($pagination_data) {
2345 $totalsResult = $this->db->query($sqlTotals);
2346 $total = $this->db->fetch_object($totalsResult)->total;
2351 $obj_ret[
'data'] = $tmp;
2352 $obj_ret[
'pagination'] = array(
2353 'total' => (
int) $total,
2355 'page_count' => ceil((
int) $total / $limit),
2378 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
2379 throw new RestException(403);
2382 $result = $this->template_invoice->fetch(
$id, $ref, $ref_ext);
2384 throw new RestException(404,
'Template invoice not found');
2388 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2392 if ($contact_list > -1) {
2393 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
2394 if (is_array($tmparray)) {
2395 $this->template_invoice->contacts_ids = $tmparray;
2399 $this->template_invoice->fetchObjectLinked();
2419 unset(
$object->barcode_type_code);
2420 unset(
$object->barcode_type_label);
2421 unset(
$object->barcode_type_coder);
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage customers orders.
const STATUS_DRAFT
Draft status.
Class to manage absolute discounts.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
Class to manage invoices.
const TYPE_REPLACEMENT
Replacement invoice.
const TYPE_STANDARD
Standard invoice.
const TYPE_SITUATION
Situation invoice.
const TYPE_DEPOSIT
Deposit invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
Class to manage invoice lines.
Class to manage invoice templates.
putPayment($id, $num_payment='')
Update a payment.
addContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0)
Adds a contact to an invoice.
indexTemplateInvoices($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='', $pagination_data=false, $loadlinkedobjects=0, $withLines=true)
List template invoices.
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.
getContacts($id, $type='')
Get contacts of given 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.
unmarkAsCreditAvailable($id)
Remove a discount (credit available) for a credit note or a deposit.
getPayments($id)
Get list of payments of a given invoice.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
post($request_data=null)
Create invoice object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='', $pagination_data=false, $loadlinkedobjects=0, $withLines=true)
List invoices.
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.
postContact($id, $contactid, $type, $source='external', $notrigger=0)
Add a contact type of given invoice.
_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.
Class to manage third parties objects (customers, suppliers, prospects...)
dol_stringtotime($string, $gm=1, $processnotimeasnoon=0)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
dol_now($mode='gmt')
Return date for now.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.