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 ($status ==
'draft') {
254 $sql .=
" AND t.fk_statut IN (0)";
256 if ($status ==
'unpaid') {
257 $sql .=
" AND t.fk_statut IN (1)";
259 if ($status ==
'paid') {
260 $sql .=
" AND t.fk_statut IN (2)";
262 if ($status ==
'cancelled') {
263 $sql .=
" AND t.fk_statut IN (3)";
270 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
275 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
277 $sql .= $this->db->order($sortfield, $sortorder);
282 $offset = $limit * $page;
284 $sql .= $this->db->plimit($limit + 1, $offset);
287 $result = $this->db->query($sql);
290 $num = $this->db->num_rows($result);
291 $min = min($num, ($limit <= 0 ? $num : $limit));
293 $obj = $this->db->fetch_object($result);
294 $invoice_static =
new Facture($this->db);
295 if ($invoice_static->fetch($obj->rowid) > 0) {
297 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
298 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
299 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
300 $invoice_static->remaintopay =
price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits,
'MT');
303 $invoice_static->getListIdAvoirFromInvoice();
306 $tmparray = $invoice_static->liste_contact(-1,
'external', 1);
307 if (is_array($tmparray)) {
308 $invoice_static->contacts_ids = $tmparray;
311 if ($loadlinkedobjects) {
313 $invoice_static->fetchObjectLinked();
317 unset($invoice_static->lines);
321 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
322 $invoice_static->online_payment_url = getOnlinePaymentUrl(0,
'invoice', (
string) $invoice_static->ref);
329 throw new RestException(503,
'Error when retrieve invoice list : '.$this->db->lasterror());
333 if ($pagination_data) {
334 $totalsResult = $this->db->query($sqlTotals);
335 $total = $this->db->fetch_object($totalsResult)->total;
340 $obj_ret[
'data'] = $tmp;
341 $obj_ret[
'pagination'] = [
342 'total' => (int) $total,
344 'page_count' => ceil((
int) $total / $limit),
362 public function post($request_data =
null)
365 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
366 throw new RestException(403,
"Insufficiant rights");
369 if (!is_array($request_data)) {
370 $request_data = array();
377 $socid = (int) $request_data[
'socid'];
378 $thirdpartytmp =
new Societe($this->db);
379 $thirdparty_result = $thirdpartytmp->fetch($socid);
380 if ($thirdparty_result < 1) {
381 throw new RestException(404,
'Thirdparty with id='.$socid.
' not found or not allowed');
384 throw new RestException(404,
'Thirdparty with id='.$thirdpartytmp->id.
' not found or not allowed');
387 foreach ($request_data as $field => $value) {
388 if ($field ===
'caller') {
390 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
393 if ($field ==
'id') {
394 throw new RestException(400,
'Creating with id field is forbidden');
396 if ($field ==
'entity' && ((
int) $value) != ((
int)
$conf->entity)) {
397 throw new RestException(403,
'Creating with entity='.((
int) $value).
' MUST be the same entity='.((
int)
$conf->entity).
' as your API user/key belongs to');
400 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
402 if (!array_key_exists(
'date', $request_data)) {
403 $this->invoice->date =
dol_now();
414 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data[
"date_lim_reglement"]) ? 0 : $request_data[
"date_lim_reglement"])) < 0) {
415 throw new RestException(500,
"Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
417 return ((
int) $this->invoice->id);
438 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
440 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
441 throw new RestException(403);
443 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
444 throw new RestException(403);
446 if (empty($orderid)) {
447 throw new RestException(400,
'Order ID is mandatory');
450 throw new RestException(403,
'Access not allowed on order for login '.DolibarrApiAccess::$user->login);
454 $result = $order->fetch($orderid);
456 throw new RestException(404,
'Order not found');
462 throw new RestException(405,
'Order '.$order->ref.
' is not eligible for invoicing: its status does not allow creating an invoice');
465 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
467 throw new RestException(405, $this->invoice->errorsToString());
469 $this->invoice->fetchObjectLinked();
490 require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
492 if (!DolibarrApiAccess::$user->hasRight(
'contrat',
'lire')) {
493 throw new RestException(403);
495 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
496 throw new RestException(403);
498 if (empty($contractid)) {
499 throw new RestException(400,
'Contract ID is mandatory');
502 $contract =
new Contrat($this->db);
503 $result = $contract->fetch($contractid);
505 throw new RestException(404,
'Contract not found');
508 throw new RestException(403,
'Access to contract '.$contract->id.
' not allowed for login '.DolibarrApiAccess::$user->login);
511 $result = $this->invoice->createFromContract($contract, DolibarrApiAccess::$user);
513 throw new RestException(405, $this->invoice->errorsToString());
515 $this->invoice->fetchObjectLinked();
533 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
534 throw new RestException(403);
537 $result = $this->invoice->fetch(
$id);
539 throw new RestException(404,
'Invoice not found');
543 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
545 $this->invoice->getLinesArray();
547 foreach ($this->invoice->lines as $line) {
573 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
574 throw new RestException(403);
577 $result = $this->invoice->fetch(
$id);
579 throw new RestException(404,
'Invoice not found');
583 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
586 $request_data = (object) $request_data;
588 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
589 $request_data->label =
sanitizeVal($request_data->label);
592 $result = $invoiceline->fetch($lineid);
594 throw new RestException(404,
'Invoice line not found');
597 if ($invoiceline->fk_facture !=
$id) {
598 throw new RestException(403,
'Line does not belong to this invoice');
601 $updateRes = $this->invoice->updateline(
604 $request_data->subprice,
606 $request_data->remise_percent,
607 $request_data->date_start,
608 $request_data->date_end,
609 $request_data->tva_tx,
610 $request_data->localtax1_tx,
611 $request_data->localtax2_tx,
612 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
613 $request_data->info_bits,
614 $request_data->product_type,
615 $request_data->fk_parent_line,
617 $request_data->fk_fournprice,
618 $request_data->pa_ht,
619 $request_data->label,
620 $request_data->special_code,
621 $request_data->array_options,
622 $request_data->situation_percent,
623 $request_data->fk_unit,
624 $request_data->multicurrency_subprice,
626 $request_data->ref_ext,
630 if ($updateRes > 0) {
631 $result = $this->
get(
$id);
632 unset($result->line);
635 throw new RestException(304, $this->invoice->errorsToString());
658 public function postContact(
$id, $contactid, $type, $source =
'external', $notrigger = 0)
660 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
661 throw new RestException(403);
665 if (empty($source)) {
666 throw new RestException(400,
'Source can not be empty');
668 $sql_distinct_source =
"SELECT DISTINCT source";
669 $sql_distinct_source .=
" FROM ".MAIN_DB_PREFIX.
"c_type_contact";
670 $sql_distinct_source .=
" WHERE element LIKE 'facture'";
671 $sql_distinct_source .=
" AND source is NOT NULL";
672 $sql_distinct_source .=
" AND active != 0";
673 $source_result = $this->db->query($sql_distinct_source);
674 $source_array = array();
676 if ($source_result) {
677 $num = $this->db->num_rows($source_result);
680 $obj = $this->db->fetch_object($source_result);
681 $source_kind = (string) $obj->source;
682 array_push($source_array, $source_kind);
687 throw new RestException(503,
'Error when retrieving a list of invoice contact sources: '.$this->db->lasterror());
689 if (!in_array($source, (array) $source_array,
true)) {
690 throw new RestException(400,
'Combo of Source='.$source.
' and Type='.$type.
' not found in dictionary with active invoice contact types');
695 throw new RestException(400,
'type can not be empty');
698 $sql_distinct_type =
"SELECT DISTINCT code";
699 $sql_distinct_type .=
" FROM ".MAIN_DB_PREFIX.
"c_type_contact";
700 $sql_distinct_type .=
" WHERE element LIKE 'facture'";
701 $sql_distinct_type .=
" AND source='".$this->db->escape($source).
"'";
702 $sql_distinct_type .=
" AND code is NOT NULL";
703 $sql_distinct_type .=
" AND active != 0";
704 $type_result = $this->db->query($sql_distinct_type);
705 $type_array = array();
708 $num = $this->db->num_rows($type_result);
711 $obj = $this->db->fetch_object($type_result);
713 $type_kind = (string) $obj->code;
714 array_push($type_array, $type_kind);
719 throw new RestException(503,
'Error when retrieving a list of invoice contact types: '.$this->db->lasterror());
721 if (!in_array($type, (array) $type_array,
true)) {
722 throw new RestException(400,
'Combo of Type='.$type.
' and Source='.$source.
' not found in dictionary with active invoice contact types');
726 $result = $this->invoice->fetch(
$id);
728 throw new RestException(404,
'Invoice not found');
731 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
734 $result = $this->invoice->add_contact($contactid, $type, $source, $notrigger);
737 throw new RestException(400,
'Already exists: Contact='.$contactid.
' is already linked to the invoice='.
$id.
' as source='.$source.
' and type='.$type);
738 } elseif ($result == -1) {
739 throw new RestException(400,
'Wrong contact='.$contactid);
740 } elseif ($result == -2) {
741 throw new RestException(400,
'Wrong type='.$type);
742 } elseif ($result == -3) {
743 throw new RestException(400,
'Not allowed contacts');
744 } elseif ($result == -4) {
745 throw new RestException(400,
'ErrorCommercialNotAllowedForThirdparty');
746 } elseif ($result == -5) {
747 throw new RestException(400,
'Trigger failed');
748 } elseif ($result == -6) {
749 throw new RestException(400,
'DB_ERROR_RECORD_ALREADY_EXISTS');
750 } elseif ($result == -7) {
751 throw new RestException(400,
'Some other error');
755 throw new RestException(500,
'Error when added the contact');
761 'message' =>
'Contact='.$contactid.
' linked to the invoice='.
$id.
' as '.$source.
' '.$type
783 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
784 throw new RestException(403);
787 $result = $this->invoice->fetch(
$id);
789 throw new RestException(404,
'Invoice not found');
793 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
796 $contacts = $this->invoice->liste_contact(-1,
'external', 0, $type);
797 $socpeoples = $this->invoice->liste_contact(-1,
'internal', 0, $type);
799 $contacts = array_merge($contacts, $socpeoples);
822 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
823 throw new RestException(403);
826 $result = $this->invoice->fetch(
$id);
829 throw new RestException(404,
'Invoice not found');
833 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
836 $contacts = $this->invoice->liste_contact();
838 foreach ($contacts as $contact) {
839 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
840 $result = $this->invoice->delete_contact($contact[
'rowid']);
843 throw new RestException(500,
'Error when deleted the contact');
869 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
870 throw new RestException(403);
872 if (empty($lineid)) {
873 throw new RestException(400,
'Line ID is mandatory');
877 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
880 $result = $this->invoice->fetch(
$id);
882 throw new RestException(404,
'Invoice not found');
884 if ($this->invoice->status != 0) {
885 throw new RestException(403,
'Invoice not in Draft Status : '.$this->invoice->getLibStatut(1));
888 $updateRes = $this->invoice->deleteLine($lineid,
$id);
889 if ($updateRes > 0) {
890 return $this->
get(
$id);
892 throw new RestException(405, $this->invoice->errorsToString());
907 public function put(
$id, $request_data =
null)
909 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
910 throw new RestException(403);
913 throw new RestException(400,
'No invoice with id=0 can exist');
915 $result = $this->invoice->fetch(
$id);
917 throw new RestException(404,
'Invoice not found');
921 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
924 foreach ($request_data as $field => $value) {
925 if ($field ==
'id') {
928 if ($field ===
'caller') {
930 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
933 if ($field ==
'array_options' && is_array($value)) {
934 foreach ($value as $index => $val) {
940 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
943 if ($field ==
'cond_reglement_id') {
944 $this->invoice->date_lim_reglement = $this->invoice->calculate_date_lim_reglement();
949 if (!empty($this->invoice->fk_account)) {
950 if ($this->invoice->setBankAccount((
int) $this->invoice->fk_account) == 0) {
951 throw new RestException(400, $this->invoice->errorsToString());
955 if ($this->invoice->update(DolibarrApiAccess::$user) > 0) {
956 return $this->
get(
$id);
958 throw new RestException(500, $this->invoice->errorsToString());
972 public function delete(
$id)
974 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'supprimer')) {
975 throw new RestException(403);
978 throw new RestException(400,
'No invoice with id=0 can exist');
980 $result = $this->invoice->fetch(
$id);
982 throw new RestException(404,
'Invoice not found');
986 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
989 $result = $this->invoice->delete(DolibarrApiAccess::$user);
991 throw new RestException(500,
'Error when deleting invoice');
992 } elseif ($result == 0) {
993 throw new RestException(403,
'Invoice not erasable');
999 'message' =>
'Invoice deleted'
1033 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1034 throw new RestException(403);
1037 $result = $this->invoice->fetch(
$id);
1039 throw new RestException(404,
'Invoice not found');
1043 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1046 $request_data = (object) $request_data;
1048 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
1049 $request_data->label =
sanitizeVal($request_data->label);
1052 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
1053 $request_data->fk_parent_line = 0;
1057 $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);
1058 $pa_ht = $marginInfos[0];
1060 $updateRes = $this->invoice->addline(
1061 $request_data->desc,
1062 $request_data->subprice,
1064 $request_data->tva_tx,
1065 $request_data->localtax1_tx,
1066 $request_data->localtax2_tx,
1067 $request_data->fk_product,
1068 $request_data->remise_percent,
1069 $request_data->date_start,
1070 $request_data->date_end,
1071 $request_data->fk_code_ventilation,
1072 $request_data->info_bits,
1073 $request_data->fk_remise_except,
1074 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
1075 $request_data->subprice,
1076 $request_data->product_type,
1077 $request_data->rang,
1078 $request_data->special_code,
1079 $request_data->origin,
1080 $request_data->origin_id,
1081 $request_data->fk_parent_line,
1082 empty($request_data->fk_fournprice) ? null : $request_data->fk_fournprice,
1084 $request_data->label,
1085 $request_data->array_options,
1086 $request_data->situation_percent,
1087 $request_data->fk_prev_id,
1088 $request_data->fk_unit,
1090 $request_data->ref_ext
1093 if ($updateRes < 0) {
1094 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->errorsToString());
1120 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
1122 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1123 throw new RestException(403);
1125 $result = $this->invoice->fetch(
$id);
1127 throw new RestException(404,
'Invoice not found');
1131 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1134 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1136 throw new RestException(500,
'Error : '.$this->invoice->errorsToString());
1139 $result = $this->invoice->fetch(
$id);
1141 throw new RestException(404,
'Invoice not found');
1172 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1173 throw new RestException(403);
1175 $result = $this->invoice->fetch(
$id);
1177 throw new RestException(404,
'Invoice not found');
1181 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1184 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
1186 throw new RestException(304,
'Nothing done.');
1189 throw new RestException(500,
'Error : '.$this->invoice->errorsToString());
1192 $result = $this->invoice->fetch(
$id);
1194 throw new RestException(404,
'Invoice not found');
1220 public function validate(
$id, $force_number =
'', $idwarehouse = 0, $notrigger = 0)
1222 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1223 throw new RestException(403);
1225 $result = $this->invoice->fetch(
$id);
1227 throw new RestException(404,
'Invoice not found');
1231 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1234 $result = $this->invoice->validate(DolibarrApiAccess::$user, $force_number, $idwarehouse, $notrigger);
1236 throw new RestException(304,
'Error nothing done. May be object is already validated');
1239 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->errorsToString());
1242 $result = $this->invoice->fetch(
$id);
1244 throw new RestException(404,
'Invoice not found');
1253 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
1254 $this->invoice->online_payment_url = getOnlinePaymentUrl(0,
'invoice', (
string) $this->invoice->ref);
1278 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1279 throw new RestException(403);
1281 $result = $this->invoice->fetch(
$id);
1283 throw new RestException(404,
'Invoice not found');
1287 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1290 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
1292 throw new RestException(304,
'Error nothing done. May be object is already validated');
1295 throw new RestException(500,
'Error : '.$this->invoice->errorsToString());
1299 $result = $this->invoice->fetch(
$id);
1301 throw new RestException(404,
'Invoice not found');
1330 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1331 throw new RestException(403);
1333 $result = $this->invoice->fetch(
$id);
1335 throw new RestException(404,
'Invoice not found');
1339 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1342 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1344 throw new RestException(304,
'Nothing done');
1347 throw new RestException(500,
'Error : '.$this->invoice->errorsToString());
1351 $result = $this->invoice->fetch(
$id);
1353 throw new RestException(404,
'Invoice not found');
1376 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1378 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1379 throw new RestException(403);
1382 $result = $this->invoice->fetch(
$id);
1384 throw new RestException(404,
'Invoice not found');
1388 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1392 $result = $discountcheck->fetch(0, $this->invoice->id);
1395 throw new RestException(404,
'Discount not found');
1398 throw new RestException(500, $discountcheck->errorsToString());
1401 return parent::_cleanObjectDatas($discountcheck);
1421 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1423 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1424 throw new RestException(403);
1427 $result = $this->invoice->fetch(
$id);
1429 throw new RestException(404,
'Invoice not found');
1433 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1436 if ($this->invoice->paye) {
1437 throw new RestException(500,
'Alreay paid');
1440 $this->invoice->fetch(
$id);
1441 $this->invoice->fetch_thirdparty();
1445 $result = $discountcheck->fetch(0, $this->invoice->id);
1457 $amount_ht = $amount_tva = $amount_ttc = array();
1458 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1460 @phan-var-force array<string,float> $amount_ht
1461 @phan-var-force array<string,float> $amount_tva
1462 @phan-var-force array<string,float> $amount_ttc
1463 @phan-var-force array<string,float> $multicurrency_amount_ht
1464 @phan-var-force array<string,float> $multicurrency_amount_tva
1465 @phan-var-force array<string,float> $multicurrency_amount_ttc
1470 foreach ($this->invoice->lines as $line) {
1471 if ($line->product_type < 9 && $line->total_ht != 0) {
1472 if (!array_key_exists($line->tva_tx, $amount_ht)) {
1473 $amount_ht[$line->tva_tx] = 0.0;
1474 $amount_tva[$line->tva_tx] = 0.0;
1475 $amount_ttc[$line->tva_tx] = 0.0;
1476 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
1477 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
1478 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
1481 $amount_ht[$line->tva_tx] += $line->total_ht;
1482 $amount_tva[$line->tva_tx] += $line->total_tva;
1483 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1484 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1485 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1486 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1494 $discount->description =
'(CREDIT_NOTE)';
1496 $discount->description =
'(DEPOSIT)';
1498 $discount->description =
'(EXCESS RECEIVED)';
1500 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1503 $discount->fk_soc = $this->invoice->socid;
1504 $discount->socid = $this->invoice->socid;
1505 $discount->fk_facture_source = $this->invoice->id;
1513 $sql =
'SELECT SUM(pf.amount) as total_payments';
1514 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1515 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1516 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1517 $sql .=
' AND pf.fk_paiement = p.rowid';
1518 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1519 $resql = $this->db->query($sql);
1521 dol_print_error($this->db);
1524 $res = $this->db->fetch_object($resql);
1525 $total_payments = $res->total_payments;
1528 $total_creditnote_and_deposit = 0;
1529 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1530 $sql .=
" re.description, re.fk_facture_source";
1531 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1532 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1533 $resql = $this->db->query($sql);
1534 if (!empty($resql)) {
1535 while ($obj = $this->db->fetch_object($resql)) {
1536 $total_creditnote_and_deposit += $obj->amount_ttc;
1539 dol_print_error($this->db);
1542 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1543 $discount->total_ht = $discount->total_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1544 $discount->amount_tva = 0;
1545 $discount->total_tva = 0;
1546 $discount->tva_tx = 0;
1548 $result = $discount->create(DolibarrApiAccess::$user);
1554 foreach ($amount_ht as $tva_tx => $xxx) {
1555 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1556 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1557 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1558 $discount->total_ht = abs($amount_ht[$tva_tx]);
1559 $discount->total_tva = abs($amount_tva[$tva_tx]);
1560 $discount->total_ttc = abs($amount_ttc[$tva_tx]);
1561 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1562 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1563 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1564 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$tva_tx]);
1565 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$tva_tx]);
1566 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1567 $discount->tva_tx = abs((
float) $tva_tx);
1569 $result = $discount->create(DolibarrApiAccess::$user);
1577 if (empty($error)) {
1580 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1582 $this->db->commit();
1584 $this->db->rollback();
1585 throw new RestException(500,
'Could not set paid');
1588 $this->db->commit();
1591 $this->db->rollback();
1592 throw new RestException(500,
'Discount creation error');
1617 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1619 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1620 throw new RestException(403);
1624 $result = $this->invoice->fetch(
$id);
1626 throw new RestException(404,
'Invoice not found');
1630 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1635 $result = $discountcheck->fetch(0, $this->invoice->id);
1638 throw new RestException(404,
'Discount not found');
1641 throw new RestException(500, $discountcheck->errorsToString());
1644 if (!empty($discountcheck->fk_facture) || !empty($discountcheck->fk_facture_line)) {
1645 throw new RestException(409,
'Credit used');
1650 $result = $discountcheck->delete(DolibarrApiAccess::$user);
1652 $this->db->rollback();
1653 throw new RestException(500,
'Discount deletion error');
1657 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1659 $this->db->rollback();
1660 throw new RestException(500,
'Could not set unpaid');
1664 $this->db->commit();
1689 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1690 throw new RestException(403);
1693 throw new RestException(400,
'Invoice ID is mandatory');
1695 if (empty($discountid)) {
1696 throw new RestException(400,
'Discount ID is mandatory');
1700 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1703 $result = $this->invoice->fetch(
$id);
1705 throw new RestException(404,
'Invoice not found');
1708 $result = $this->invoice->insert_discount($discountid);
1710 throw new RestException(405, $this->invoice->errorsToString());
1736 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1738 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'creer')) {
1739 throw new RestException(403);
1742 throw new RestException(400,
'Invoice ID is mandatory');
1744 if (empty($discountid)) {
1745 throw new RestException(400,
'Credit ID is mandatory');
1749 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1752 $result = $discount->fetch($discountid);
1754 throw new RestException(404,
'Credit not found');
1757 $result = $discount->link_to_invoice(0,
$id);
1759 throw new RestException(405, $discount->errorsToString());
1784 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1785 throw new RestException(403);
1788 throw new RestException(400,
'Invoice ID is mandatory');
1792 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1795 $result = $this->invoice->fetch(
$id);
1797 throw new RestException(404,
'Invoice not found');
1800 $result = $this->invoice->getListOfPayments();
1801 if ($this->invoice->errorsToString() !==
'') {
1802 throw new RestException(405, $this->invoice->errorsToString());
1832 public function addPayment(
$id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1834 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1836 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'paiement')) {
1837 throw new RestException(403);
1840 throw new RestException(400,
'Invoice ID is mandatory');
1844 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1848 if (empty($accountid)) {
1849 throw new RestException(400,
'Account ID is mandatory');
1853 if (empty($paymentid)) {
1854 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1858 $result = $this->invoice->fetch(
$id);
1860 throw new RestException(404,
'Invoice not found');
1864 $totalpaid = $this->invoice->getSommePaiement();
1865 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1866 $totaldeposits = $this->invoice->getSumDepositsUsed();
1871 $multicurrency_amounts = array();
1875 $resteapayer =
price2num($this->invoice->total_ttc + $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1876 $amounts[
$id] = (float)
price2num(-1 * abs((
float) $resteapayer),
'MT');
1878 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1879 $multicurrency_amounts[
$id] = (float)
price2num(-1 * (
float) $newvalue,
'MT');
1881 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1882 $amounts[
$id] = (float) $resteapayer;
1884 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1885 $multicurrency_amounts[
$id] = (float) $newvalue;
1889 $paymentobj =
new Paiement($this->db);
1890 if (is_numeric($datepaye)) {
1891 $paymentobj->datepaye = $datepaye;
1895 $paymentobj->amounts = $amounts;
1896 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1897 $paymentobj->paiementid = $paymentid;
1898 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $paymentid,
'c_paiement',
'id',
'code', 1);
1899 $paymentobj->num_payment = $num_payment;
1900 $paymentobj->note_private = $comment;
1902 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1903 if ($payment_id < 0) {
1904 $this->db->rollback();
1905 throw new RestException(400,
'Payment error : '.$paymentobj->errorsToString());
1909 $label =
'(CustomerInvoicePayment)';
1911 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1912 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1915 $label =
'(CustomerInvoicePaymentBack)';
1917 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1919 $this->db->rollback();
1920 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->errorsToString());
1924 $this->db->commit();
1959 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1961 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1963 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'paiement')) {
1964 throw new RestException(403);
1966 foreach ($arrayofamounts as
$id => $amount) {
1968 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1971 throw new RestException(403,
'Access not allowed on invoice ID '.
$id.
' for login '.DolibarrApiAccess::$user->login);
1976 if (empty($accountid)) {
1977 throw new RestException(400,
'Account ID is mandatory');
1980 if (empty($paymentid)) {
1981 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1987 $multicurrency_amounts = array();
1990 foreach ($arrayofamounts as
$id => $amountarray) {
1992 $result = $this->invoice->fetch(
$id);
1994 $this->db->rollback();
1995 throw new RestException(404,
'Invoice ID '.
$id.
' not found');
1998 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1999 $this->db->rollback();
2000 throw new RestException(400,
'Payment in both currency '.
$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
2003 $is_multicurrency = 0;
2004 $total_ttc = $this->invoice->total_ttc;
2006 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
2007 $is_multicurrency = 1;
2008 $total_ttc = $this->invoice->multicurrency_total_ttc;
2012 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
2013 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
2014 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
2015 $remainstopay = $amount = (float)
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
2017 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
2018 $amount = (float)
price2num($amountarray[
"amount"],
'MT');
2021 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
2022 $amount = (float)
price2num($amountarray[
"multicurrency_amount"],
'MT');
2025 if (abs($amount) > abs($remainstopay) && !$accepthigherpayment) {
2026 $this->db->rollback();
2027 throw new RestException(400,
'Payment amount on invoice ID '.
$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
2031 $amount = (float)
price2num(-1 * abs((
float) $amount),
'MT');
2034 if ($is_multicurrency) {
2035 $amounts[
$id] =
null;
2037 $multicurrency_amounts[
$id] = (float) $amount;
2039 $amounts[
$id] = (float) $amount;
2041 $multicurrency_amounts[
$id] =
null;
2046 $paymentobj =
new Paiement($this->db);
2047 if (is_numeric($datepaye)) {
2048 $paymentobj->datepaye = $datepaye;
2052 $paymentobj->amounts = $amounts;
2053 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
2054 $paymentobj->paiementid = $paymentid;
2055 $paymentobj->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $paymentid,
'c_paiement',
'id',
'code', 1);
2056 $paymentobj->num_payment = $num_payment;
2057 $paymentobj->note_private = $comment;
2058 $paymentobj->ref_ext = $ref_ext;
2059 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
2060 if ($payment_id < 0) {
2061 $this->db->rollback();
2062 throw new RestException(400,
'Payment error : '.$paymentobj->errorsToString());
2065 $label =
'(CustomerInvoicePayment)';
2066 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
2067 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
2070 $label =
'(CustomerInvoicePaymentBack)';
2072 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
2074 $this->db->rollback();
2075 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->errorsToString());
2079 $this->db->commit();
2104 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
2106 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'paiement')) {
2107 throw new RestException(403);
2110 throw new RestException(400,
'Payment ID is mandatory');
2113 $paymentobj =
new Paiement($this->db);
2114 $result = $paymentobj->fetch(
$id);
2117 throw new RestException(404,
'Payment not found');
2121 $tmparray = $paymentobj->getBillsArray();
2122 foreach ($tmparray as $tmpinvoiceid) {
2124 throw new RestException(403,
'Payment is on invoices that are not all allowed for login '.DolibarrApiAccess::$user->login);
2128 if (!empty($num_payment)) {
2129 $result = $paymentobj->update_num($num_payment);
2131 throw new RestException(500,
'Error when updating the payment num');
2138 'message' =>
'Payment updated'
2161 unset(
$object->barcode_type_code);
2162 unset(
$object->barcode_type_label);
2163 unset(
$object->barcode_type_coder);
2179 if ($data ===
null) {
2183 foreach (Invoices::$FIELDS as $field) {
2184 if (!isset($data[$field])) {
2185 throw new RestException(400,
"$field field missing");
2187 $invoice[$field] = $data[$field];
2241 public function indexTemplateInvoices($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false, $loadlinkedobjects = 0, $withLines =
true)
2243 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
2244 throw new RestException(403);
2250 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
2255 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
2256 $search_sale = DolibarrApiAccess::$user->id;
2259 $sql =
"SELECT t.rowid";
2260 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_rec AS t";
2261 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe AS s ON (s.rowid = t.fk_soc)";
2262 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_rec_extrafields AS ef ON (ef.fk_object = t.rowid)";
2263 $sql .=
' WHERE t.entity IN ('.getEntity(
'invoice').
')';
2265 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
2269 if ($search_sale && $search_sale !=
'-1') {
2270 if ($search_sale == -2) {
2271 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux AS sc WHERE sc.fk_soc = t.fk_soc)";
2272 } elseif ($search_sale > 0) {
2273 $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).
")";
2278 if ($status ==
'active') {
2279 $sql .=
" AND t.suspended = 0 AND t.frequency IS NOT NULL";
2281 if ($status ==
'suspended') {
2282 $sql .=
" AND t.suspended = 1 AND t.frequency IS NOT NULL";
2284 if ($status ==
'draft') {
2285 $sql .=
" AND t.frequency IS NULL";
2291 if ($errormessage) {
2292 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
2297 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
2299 $sql .= $this->db->order($sortfield, $sortorder);
2304 $offset = $limit * $page;
2306 $sql .= $this->db->plimit($limit + 1, $offset);
2309 $result = $this->db->query($sql);
2312 $num = $this->db->num_rows($result);
2313 $min = min($num, ($limit <= 0 ? $num : $limit));
2315 $obj = $this->db->fetch_object($result);
2317 if ($factureRec->fetch($obj->rowid) > 0) {
2318 if ($loadlinkedobjects) {
2320 $factureRec->fetchObjectLinked();
2324 unset($factureRec->lines);
2332 throw new RestException(503,
'Error when retrieving recurring invoice templates: '.$this->db->lasterror());
2336 if ($pagination_data) {
2337 $totalsResult = $this->db->query($sqlTotals);
2338 $total = $this->db->fetch_object($totalsResult)->total;
2343 $obj_ret[
'data'] = $tmp;
2344 $obj_ret[
'pagination'] = array(
2345 'total' => (
int) $total,
2347 'page_count' => ceil((
int) $total / $limit),
2370 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
2371 throw new RestException(403);
2374 $result = $this->template_invoice->fetch(
$id, $ref, $ref_ext);
2376 throw new RestException(404,
'Template invoice not found');
2380 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2384 if ($contact_list > -1) {
2385 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
2386 if (is_array($tmparray)) {
2387 $this->template_invoice->contacts_ids = $tmparray;
2391 $this->template_invoice->fetchObjectLinked();
2411 unset(
$object->barcode_type_code);
2412 unset(
$object->barcode_type_label);
2413 unset(
$object->barcode_type_coder);
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
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)
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.