23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT .
'/fourn/class/fournisseur.facture.class.php';
26require_once DOL_DOCUMENT_ROOT .
'/fourn/class/paiementfourn.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
43 public static $FIELDS = array(
75 public function get(
$id)
77 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
78 throw new RestException(403);
82 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
85 $result = $this->invoice->fetch(
$id);
87 throw new RestException(404,
'Supplier invoice not found');
91 $this->invoice->getListIdAvoirFromInvoice();
93 $this->invoice->fetchObjectLinked();
121 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
123 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
124 throw new RestException(403);
130 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
134 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir")) {
135 $search_sale = DolibarrApiAccess::$user->id;
138 $sql =
"SELECT t.rowid";
139 $sql .=
" FROM " . MAIN_DB_PREFIX .
"facture_fourn AS t";
140 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"facture_fourn_extrafields AS ef ON (ef.fk_object = t.rowid)";
141 $sql .=
' WHERE t.entity IN (' .
getEntity(
'supplier_invoice') .
')';
143 $sql .=
" AND t.fk_soc IN (" . $this->db->sanitize($socids) .
")";
146 if ((
string) $status !==
'' && !in_array($status, array(
'draft',
'unpaid',
'paid',
'cancelled'),
true)) {
147 throw new RestException(400,
"Bad value for parameter status. Expected one of: draft, unpaid, paid, cancelled");
150 if ($status ==
'draft') {
151 $sql .=
" AND t.fk_statut IN (0)";
153 if ($status ==
'unpaid') {
154 $sql .=
" AND t.fk_statut IN (1)";
156 if ($status ==
'paid') {
157 $sql .=
" AND t.fk_statut IN (2)";
159 if ($status ==
'cancelled') {
160 $sql .=
" AND t.fk_statut IN (3)";
163 if ($search_sale && $search_sale !=
'-1') {
164 if ($search_sale == -2) {
165 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
166 } elseif ($search_sale > 0) {
167 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
175 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
180 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
182 $sql .= $this->db->order($sortfield, $sortorder);
187 $offset = $limit * $page;
189 $sql .= $this->db->plimit($limit + 1, $offset);
192 $result = $this->db->query($sql);
195 $num = $this->db->num_rows($result);
196 $min = min($num, ($limit <= 0 ? $num : $limit));
198 $obj = $this->db->fetch_object($result);
200 if ($invoice_static->fetch($obj->rowid)) {
206 throw new RestException(503,
'Error when retrieve supplier invoice list : ' . $this->db->lasterror());
210 if ($pagination_data) {
211 $totalsResult = $this->db->query($sqlTotals);
212 $total = $this->db->fetch_object($totalsResult)->total;
217 $obj_ret[
'data'] = $tmp;
218 $obj_ret[
'pagination'] = [
219 'total' => (int) $total,
221 'page_count' => ceil((
int) $total / $limit),
247 public function post($request_data =
null)
249 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
250 throw new RestException(403,
"Insufficiant rights");
253 if (!is_array($request_data)) {
254 $request_data = array();
260 foreach ($request_data as $field => $value) {
261 if ($field ===
'caller') {
263 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
267 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
269 if (!array_key_exists(
'date', $request_data)) {
270 $this->invoice->date =
dol_now();
273 if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
274 throw new RestException(500,
"Error creating invoice ", array_merge(array($this->invoice->error), $this->invoice->errors));
276 return $this->invoice->id;
293 public function put(
$id, $request_data =
null)
295 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
296 throw new RestException(403);
300 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
303 $result = $this->invoice->fetch(
$id);
305 throw new RestException(404,
'Supplier invoice not found');
308 if (!is_array($request_data)) {
309 $request_data = array();
313 $this->invoice->oldcopy =
dol_clone($this->invoice, 2);
315 foreach ($request_data as $field => $value) {
316 if ($field ==
'id') {
319 if ($field ===
'caller') {
321 $this->invoice->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
324 if ($field ==
'array_options' && is_array($value)) {
325 foreach ($value as $index => $val) {
331 $this->invoice->$field = $this->
_checkValForAPI($field, $value, $this->invoice);
334 if ($this->invoice->update(DolibarrApiAccess::$user) > 0) {
335 return $this->
get(
$id);
337 throw new RestException(500, $this->invoice->errorsToString());
356 public function delete(
$id)
358 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"supprimer")) {
359 throw new RestException(403);
362 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
364 $result = $this->invoice->fetch(
$id);
366 throw new RestException(404,
'Supplier invoice not found');
369 $result = $this->invoice->delete(DolibarrApiAccess::$user);
371 throw new RestException(500,
'Error when deleting invoice');
372 } elseif ($result == 0) {
373 throw new RestException(403,
'Invoice not erasable');
379 'message' =>
'Supplier invoice deleted'
407 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
408 throw new RestException(403);
412 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
415 $result = $this->invoice->fetch(
$id);
417 throw new RestException(404,
'Invoice not found');
420 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
422 throw new RestException(304,
'Error nothing done. The invoice is already validated');
425 throw new RestException(500,
'Error when validating Invoice: ' . $this->invoice->errorsToString());
431 'message' =>
'Invoice validated (Ref=' . $this->invoice->ref .
')'
455 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
456 throw new RestException(403);
458 $result = $this->invoice->fetch(
$id);
460 throw new RestException(404,
'Invoice not found');
464 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
467 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
469 throw new RestException(304,
'Nothing done.');
472 throw new RestException(500,
'Error : ' . $this->invoice->errorsToString());
475 $result = $this->invoice->fetch(
$id);
477 throw new RestException(404,
'Invoice not found');
503 throw new RestException(400,
'Invoice ID is mandatory');
506 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"lire")) {
507 throw new RestException(403);
510 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
513 $result = $this->invoice->fetch(
$id);
515 throw new RestException(404,
'Invoice not found');
518 $result = $this->invoice->getListOfPayments();
519 if ($this->invoice->errorsToString() !==
'') {
520 throw new RestException(405, $this->invoice->errorsToString());
552 public function addPayment(
$id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $amount =
null)
555 throw new RestException(400,
'Invoice ID is mandatory');
558 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
559 throw new RestException(403);
562 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
565 $result = $this->invoice->fetch(
$id);
567 throw new RestException(404,
'Invoice not found');
571 if (empty($accountid)) {
572 throw new RestException(400,
'Bank account ID is mandatory');
576 if (empty($payment_mode_id)) {
577 throw new RestException(400,
'Payment mode ID is mandatory');
580 if (
null !== $amount && $amount > 0) {
582 $paymentamount = $amount;
585 $totalpaid = $this->invoice->getSommePaiement();
586 $totaldeposits = $this->invoice->getSumDepositsUsed();
587 $paymentamount =
price2num($this->invoice->total_ttc - $totalpaid - $totaldeposits,
'MT');
593 $multicurrency_amounts = array();
595 $paymentamount = (float)
price2num($paymentamount,
'MT');
597 $amounts[
$id] = $paymentamount;
604 if (
null !== $amount && $amount > 0 && !empty($this->invoice->total_ttc)) {
605 $newvalue = (float)
price2num($paymentamount * $this->invoice->multicurrency_total_ttc / $this->invoice->total_ttc,
'MT');
607 $newvalue = (float)
price2num($this->invoice->multicurrency_total_ttc,
'MT');
609 $multicurrency_amounts[
$id] = $newvalue;
613 $paiement->datepaye = $datepaye;
614 $paiement->amounts = $amounts;
615 $paiement->multicurrency_amounts = $multicurrency_amounts;
616 $paiement->paiementid = $payment_mode_id;
617 $paiement->paiementcode = (string)
dol_getIdFromCode($this->db, (
string) $payment_mode_id,
'c_paiement',
'id',
'code', 1);
618 $paiement->num_payment = $num_payment;
619 $paiement->note_private = $comment;
621 $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
622 if ($paiement_id < 0) {
623 $this->db->rollback();
624 throw new RestException(400,
'Payment error : ' . $paiement->errorsToString());
628 $result = $paiement->addPaymentToBank(DolibarrApiAccess::$user,
'payment_supplier',
'(SupplierInvoicePayment)', $accountid, $chqemetteur, $chqbank);
630 $this->db->rollback();
631 throw new RestException(400,
'Add payment to bank error : ' . $paiement->errorsToString());
659 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'creer')) {
660 throw new RestException(403);
663 $result = $this->invoice->fetch(
$id);
665 throw new RestException(404,
'Supplier invoice not found');
669 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
672 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
674 throw new RestException(304,
'Error nothing done. Maybe object is already paid or not payable.');
677 throw new RestException(500,
'Error: ' . $this->invoice->errorsToString());
680 $result = $this->invoice->fetch(
$id);
682 throw new RestException(404,
'Supplier invoice not found');
705 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'creer')) {
706 throw new RestException(403);
709 $result = $this->invoice->fetch(
$id);
711 throw new RestException(404,
'Supplier invoice not found');
715 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
718 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
720 throw new RestException(304,
'Nothing done.');
723 throw new RestException(500,
'Error: ' . $this->invoice->errorsToString());
726 $result = $this->invoice->fetch(
$id);
728 throw new RestException(404,
'Supplier invoice not found');
752 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
754 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'creer')) {
755 throw new RestException(403);
758 $result = $this->invoice->fetch(
$id);
760 throw new RestException(404,
'Supplier invoice not found');
764 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
769 $discountcheck->fetch(0, 0, $this->invoice->id);
770 if (!empty($discountcheck->id)) {
771 throw new RestException(409,
'A credit already exists for this supplier invoice');
775 $remaintopay = (float) $this->invoice->getRemainToPay(0);
777 throw new RestException(400,
'Supplier invoice must be validated');
780 if (!empty($this->invoice->paid) || $remaintopay >= 0) {
781 throw new RestException(400,
'No excess paid on this supplier invoice');
783 $discountdesc =
'(EXCESS PAID)';
785 if (!empty($this->invoice->paid)) {
786 throw new RestException(400,
'Supplier credit note is already refunded or converted');
788 if (!
getDolGlobalString(
'SUPPLIER_INVOICE_ALLOW_REUSE_OF_CREDIT_WHEN_PARTIALLY_REFUNDED') && $this->invoice->getSommePaiement() != 0) {
789 throw new RestException(400,
'Supplier credit note is partially refunded');
791 $discountdesc =
'(CREDIT_NOTE)';
793 $discountdesc =
'(DEPOSIT)';
795 throw new RestException(400,
'Cannot convert into credit a supplier invoice of this type');
801 $discount->description = $discountdesc;
802 $discount->discount_type = 1;
803 $discount->fk_soc = $this->invoice->socid;
804 $discount->socid = $this->invoice->socid;
805 $discount->fk_invoice_supplier_source = $this->invoice->id;
806 $discount->multicurrency_code = $this->invoice->multicurrency_code;
807 $discount->multicurrency_tx = $this->invoice->multicurrency_tx;
813 $discount->amount_ht = $discount->amount_ttc = -$remaintopay;
814 $discount->amount_tva = 0;
815 $discount->tva_tx = 0;
816 $discount->vat_src_code =
'';
817 $discount->multicurrency_total_ht = $discount->multicurrency_total_ttc = (float)
price2num((
float) $discount->amount_ttc * (float) $this->invoice->multicurrency_tx,
'MT');
818 $discount->multicurrency_total_tva = 0;
819 $discount->multicurrency_amount_ht = $discount->multicurrency_amount_ttc = $discount->multicurrency_total_ttc;
820 $discount->multicurrency_amount_tva = 0;
822 if ($discount->create(DolibarrApiAccess::$user) < 0) {
828 foreach ($this->invoice->lines as $line) {
829 if ($line->product_type < 9 && $line->total_ht != 0) {
830 $keyforvatrate = $line->tva_tx.($line->vat_src_code ?
' ('.$line->vat_src_code.
')' :
'');
831 if (!array_key_exists($keyforvatrate, $amounts)) {
832 $amounts[$keyforvatrate] = array(
'ht' => 0.0,
'tva' => 0.0,
'ttc' => 0.0,
'mht' => 0.0,
'mtva' => 0.0,
'mttc' => 0.0);
834 $amounts[$keyforvatrate][
'ht'] += (float) $line->total_ht;
835 $amounts[$keyforvatrate][
'tva'] += (float) $line->total_tva;
836 $amounts[$keyforvatrate][
'ttc'] += (float) $line->total_ttc;
837 $amounts[$keyforvatrate][
'mht'] += (float) $line->multicurrency_total_ht;
838 $amounts[$keyforvatrate][
'mtva'] += (float) $line->multicurrency_total_tva;
839 $amounts[$keyforvatrate][
'mttc'] += (float) $line->multicurrency_total_ttc;
845 $alreadypaid = $this->invoice->getSommePaiement();
846 if ($alreadypaid && abs($alreadypaid) < abs($this->invoice->total_ttc)) {
847 $ratio = abs(($this->invoice->total_ttc - $alreadypaid) / $this->invoice->total_ttc);
848 foreach ($amounts as $keyforvatrate => $vals) {
849 foreach ($vals as $k => $val) {
850 $amounts[$keyforvatrate][$k] = (float)
price2num($val * $ratio,
'MU');
856 foreach ($amounts as $keyforvatrate => $vals) {
857 $discount->amount_ht = abs($vals[
'ht']);
858 $discount->amount_tva = abs($vals[
'tva']);
859 $discount->amount_ttc = abs($vals[
'ttc']);
860 $discount->multicurrency_total_ht = $discount->multicurrency_amount_ht = abs($vals[
'mht']);
861 $discount->multicurrency_total_tva = $discount->multicurrency_amount_tva = abs($vals[
'mtva']);
862 $discount->multicurrency_total_ttc = $discount->multicurrency_amount_ttc = abs($vals[
'mttc']);
865 $discount->vat_src_code =
'';
866 if (preg_match(
'/\((.*)\)/', (
string) $keyforvatrate, $reg)) {
867 $discount->vat_src_code = $reg[1];
869 $discount->tva_tx = abs((
float) preg_replace(
'/\s*\(.*\)/',
'', (
string) $keyforvatrate));
871 if ($discount->create(DolibarrApiAccess::$user) < 0) {
879 $this->db->rollback();
880 throw new RestException(500,
'Discount creation error: '.$discount->errorsToString());
884 if ($this->invoice->setPaid(DolibarrApiAccess::$user) < 0) {
885 $this->db->rollback();
886 throw new RestException(500,
'Could not set paid: '.$this->invoice->errorsToString());
892 $this->invoice->fetch(
$id);
918 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
920 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'creer')) {
921 throw new RestException(403);
924 $result = $this->invoice->fetch(
$id);
926 throw new RestException(404,
'Supplier invoice not found');
930 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
934 $result = $discount->fetch($discountid);
935 if ($result <= 0 || empty($discount->discount_type)) {
936 throw new RestException(404,
'Supplier credit not found');
938 if ($discount->fk_soc != $this->invoice->socid) {
939 throw new RestException(400,
'Credit does not belong to the supplier of the invoice');
941 if (!empty($discount->fk_invoice_supplier) || !empty($discount->fk_invoice_supplier_line)) {
942 throw new RestException(409,
'Credit already used');
945 throw new RestException(400,
'Supplier invoice must be validated and unpaid');
947 if (
price2num($discount->amount_ttc) >
price2num($this->invoice->getRemainToPay(0))) {
948 throw new RestException(400,
'Credit is larger than remain to pay, split it before');
953 $result = $discount->link_to_invoice(0, $this->invoice->id);
955 $this->db->rollback();
956 throw new RestException(500, $discount->errorsToString());
959 if (
price2num($this->invoice->getRemainToPay(0)) == 0) {
960 if ($this->invoice->setPaid(DolibarrApiAccess::$user) < 0) {
961 $this->db->rollback();
962 throw new RestException(500,
'Could not set paid: '.$this->invoice->errorsToString());
989 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
990 throw new RestException(403);
993 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
996 $result = $this->invoice->fetch(
$id);
998 throw new RestException(404,
'Supplier invoice not found');
1001 $this->invoice->fetch_lines();
1003 foreach ($this->invoice->lines as $line) {
1032 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
1033 throw new RestException(403);
1037 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1040 $result = $this->invoice->fetch(
$id);
1042 throw new RestException(404,
'Supplier invoice not found');
1045 $request_data = (object) $request_data;
1047 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
1048 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
1050 $updateRes = $this->invoice->addline(
1051 $request_data->description,
1052 $request_data->pu_ht,
1053 $request_data->tva_tx,
1054 $request_data->localtax1_tx,
1055 $request_data->localtax2_tx,
1057 $request_data->fk_product,
1058 $request_data->remise_percent,
1059 $request_data->date_start,
1060 $request_data->date_end,
1061 $request_data->fk_code_ventilation,
1062 $request_data->info_bits,
1063 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
1064 $request_data->product_type,
1065 $request_data->rang,
1067 $request_data->array_options,
1068 $request_data->fk_unit,
1069 $request_data->origin_id,
1070 $request_data->multicurrency_subprice,
1071 $request_data->ref_supplier,
1072 $request_data->special_code
1075 if ($updateRes < 0) {
1076 throw new RestException(400,
'Unable to insert the new line. Check your inputs. ' . $this->invoice->errorsToString());
1103 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
1104 throw new RestException(403);
1108 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1111 $result = $this->invoice->fetch(
$id);
1113 throw new RestException(404,
'Supplier invoice not found');
1117 if ($invoiceline->fetch($lineid) <= 0) {
1118 throw new RestException(404,
'Supplier invoice line not found');
1120 if ($invoiceline->fk_facture_fourn != $this->invoice->id) {
1121 throw new RestException(403,
'Line does not belong to this supplier invoice');
1124 $request_data = (object) $request_data;
1126 $request_data->description =
sanitizeVal($request_data->description,
'restricthtml');
1127 $request_data->ref_supplier =
sanitizeVal($request_data->ref_supplier);
1129 $updateRes = $this->invoice->updateline(
1131 $request_data->description,
1132 $request_data->pu_ht,
1133 $request_data->tva_tx,
1134 $request_data->localtax1_tx,
1135 $request_data->localtax2_tx,
1137 $request_data->fk_product,
1138 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
1139 $request_data->info_bits,
1140 $request_data->product_type,
1141 $request_data->remise_percent,
1143 $request_data->date_start,
1144 $request_data->date_end,
1145 $request_data->array_options,
1146 $request_data->fk_unit,
1147 $request_data->multicurrency_subprice,
1148 $request_data->ref_supplier,
1152 if ($updateRes > 0) {
1153 $result = $this->
get(
$id);
1154 unset($result->line);
1157 throw new RestException(304, $this->invoice->errorsToString());
1182 if (empty($lineid)) {
1183 throw new RestException(400,
'Line ID is mandatory');
1186 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"facture",
"creer")) {
1187 throw new RestException(403);
1190 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1193 $result = $this->invoice->fetch(
$id);
1195 throw new RestException(404,
'Supplier invoice not found');
1199 if ($invoiceline->fetch($lineid) <= 0) {
1200 throw new RestException(404,
'Supplier invoice line not found');
1202 if ($invoiceline->fk_facture_fourn != $this->invoice->id) {
1203 throw new RestException(403,
'Line does not belong to this supplier invoice');
1206 $updateRes = $this->invoice->deleteLine($lineid);
1207 if ($updateRes > 0) {
1211 'message' =>
'line '.$lineid.
' deleted'
1215 throw new RestException(405, $this->invoice->errorsToString());
1240 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1242 if (!DolibarrApiAccess::$user->hasRight(
'fournisseur',
'facture',
'creer')) {
1243 throw new RestException(403);
1246 $result = $this->invoice->fetch(
$id);
1248 throw new RestException(404,
'Supplier invoice not found');
1252 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1257 throw new RestException(400,
'Supplier invoice must be a draft');
1260 throw new RestException(400,
'A discount cannot be used on a supplier credit note or deposit');
1264 $result = $discount->fetch($discountid);
1265 if ($result <= 0 || empty($discount->discount_type)) {
1266 throw new RestException(404,
'Supplier discount not found');
1268 if ($discount->fk_soc != $this->invoice->socid) {
1269 throw new RestException(400,
'Discount does not belong to the supplier of the invoice');
1271 if (!empty($discount->fk_invoice_supplier) || !empty($discount->fk_invoice_supplier_line)) {
1272 throw new RestException(409,
'Discount already used');
1275 throw new RestException(400,
'Discount is larger than the invoice, split it before');
1278 $result = $this->invoice->insert_discount($discount->id);
1280 throw new RestException(500, $this->invoice->errorsToString());
1303 unset(
$object->barcode_type_code);
1304 unset(
$object->barcode_type_label);
1305 unset(
$object->barcode_type_coder);
1320 if ($data ===
null) {
1324 foreach (SupplierInvoices::$FIELDS as $field) {
1325 if (!isset($data[$field])) {
1326 throw new RestException(400,
"$field field missing");
1328 $invoice[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
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 suppliers invoices.
const TYPE_DEPOSIT
Deposit invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
const STATUS_VALIDATED
Validated (need to be paid)
const TYPE_STANDARD
Standard invoice.
Class to manage payments for supplier invoices.
Class to manage line invoices.
settodraft($id, $idwarehouse=-1, $notrigger=0)
Sets an invoice as draft.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an invoice.
settopaid($id, $close_code='', $close_note='')
Sets a supplier invoice as paid.
deleteLine($id, $lineid)
Deletes a line of a given supplier invoice.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $status='', $sqlfilters='', $properties='', $pagination_data=false)
List invoices.
markAsCreditAvailable($id)
Create a discount (credit available) from a supplier credit note, a supplier deposit or the excess pa...
getLines($id)
Get lines of a supplier invoice.
getPayments($id)
Get list of payments of a given supplier invoice.
useDiscount($id, $discountid)
Add a discount line into a draft supplier invoice using an available supplier discount.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
settounpaid($id)
Sets a supplier invoice as unpaid.
useCreditNote($id, $discountid)
Use an available supplier credit (coming from a credit note, a deposit or an excess paid) to pay a su...
addPayment($id, $datepaye, $payment_mode_id, $closepaidinvoices, $accountid, $num_payment='', $comment='', $chqemetteur='', $chqbank='', $amount=null)
Add payment line to a specific supplier invoice with the remain to pay as amount.
post($request_data=null)
Create supplier invoice object.
postLine($id, $request_data=null)
Add a line to given supplier invoice.
put($id, $request_data=null)
Update supplier invoice.
_validate($data)
Validate fields before create or update object.
putLine($id, $lineid, $request_data=null)
Update a line to a given supplier invoice.
__construct()
Constructor.
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
dol_clone($srcobject, $native=2)
Create a clone of instance of object (new instance with same value for each properties) With native =...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.