19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
35 static $FIELDS = array(
65 public function get($id, $contact_list = 1)
67 return $this->
_fetch($id,
'',
'', $contact_list);
83 public function getByRef($ref, $contact_list = 1)
85 return $this->
_fetch(
'', $ref,
'', $contact_list);
103 return $this->
_fetch(
'',
'', $ref_ext, $contact_list);
119 private function _fetch($id, $ref =
'', $ref_ext =
'', $contact_list = 1)
121 if (!DolibarrApiAccess::$user->rights->facture->lire) {
122 throw new RestException(401);
125 $result = $this->invoice->fetch($id, $ref, $ref_ext);
127 throw new RestException(404,
'Invoice not found');
131 $this->invoice->totalpaid = $this->invoice->getSommePaiement();
132 $this->invoice->totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
133 $this->invoice->totaldeposits = $this->invoice->getSumDepositsUsed();
134 $this->invoice->remaintopay =
price2num($this->invoice->total_ttc - $this->invoice->totalpaid - $this->invoice->totalcreditnotes - $this->invoice->totaldeposits,
'MT');
137 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
141 $this->invoice->contacts_ids = $this->invoice->liste_contact(-1,
'external', $contact_list);
143 $this->invoice->fetchObjectLinked();
164 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'')
171 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
175 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
177 $sql =
"SELECT t.rowid";
178 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", sc.fk_soc, sc.fk_user";
179 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as t";
181 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
183 $sql .=
' WHERE t.entity IN ('.getEntity(
'invoice').
')';
184 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
" AND t.fk_soc = sc.fk_soc";
185 if ($socids) $sql .=
" AND t.fk_soc IN (".$socids.
")";
187 if ($search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
190 if ($status ==
'draft') $sql .=
" AND t.fk_statut IN (0)";
191 if ($status ==
'unpaid') $sql .=
" AND t.fk_statut IN (1)";
192 if ($status ==
'paid') $sql .=
" AND t.fk_statut IN (2)";
193 if ($status ==
'cancelled') $sql .=
" AND t.fk_statut IN (3)";
195 if ($search_sale > 0)
197 $sql .=
" AND sc.fk_user = ".$search_sale;
204 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
206 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
207 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
210 $sql .= $this->
db->order($sortfield, $sortorder);
217 $offset = $limit * $page;
219 $sql .= $this->
db->plimit($limit + 1, $offset);
222 $result = $this->
db->query($sql);
226 $num = $this->
db->num_rows($result);
227 $min = min($num, ($limit <= 0 ? $num : $limit));
230 $obj = $this->
db->fetch_object($result);
231 $invoice_static =
new Facture($this->
db);
232 if ($invoice_static->fetch($obj->rowid))
235 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
236 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
237 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
238 $invoice_static->remaintopay =
price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits,
'MT');
241 $invoice_static->contacts_ids = $invoice_static->liste_contact(-1,
'external', 1);
248 throw new RestException(503,
'Error when retrieve invoice list : '.$this->
db->lasterror());
250 if (!count($obj_ret)) {
251 throw new RestException(404,
'No invoice found');
262 public function post($request_data = null)
264 if (!DolibarrApiAccess::$user->rights->facture->creer) {
265 throw new RestException(401,
"Insuffisant rights");
268 $result = $this->
_validate($request_data);
270 foreach ($request_data as $field => $value) {
271 $this->invoice->$field = $value;
273 if (!array_key_exists(
'date', $request_data)) {
274 $this->invoice->date =
dol_now();
285 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data[
"date_lim_reglement"]) ? 0 : $request_data[
"date_lim_reglement"])) < 0) {
286 throw new RestException(500,
"Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
288 return $this->invoice->id;
308 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
310 if (!DolibarrApiAccess::$user->rights->commande->lire) {
311 throw new RestException(401);
313 if (!DolibarrApiAccess::$user->rights->facture->creer) {
314 throw new RestException(401);
316 if (empty($orderid)) {
317 throw new RestException(400,
'Order ID is mandatory');
321 $result = $order->fetch($orderid);
323 throw new RestException(404,
'Order not found');
326 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
328 throw new RestException(405, $this->invoice->error);
330 $this->invoice->fetchObjectLinked();
345 if (!DolibarrApiAccess::$user->rights->facture->lire) {
346 throw new RestException(401);
349 $result = $this->invoice->fetch($id);
351 throw new RestException(404,
'Invoice not found');
355 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
357 $this->invoice->getLinesArray();
359 foreach ($this->invoice->lines as $line) {
380 public function putLine($id, $lineid, $request_data = null)
382 if (!DolibarrApiAccess::$user->rights->facture->creer) {
383 throw new RestException(401);
386 $result = $this->invoice->fetch($id);
388 throw new RestException(404,
'Invoice not found');
392 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
394 $request_data = (object) $request_data;
395 $updateRes = $this->invoice->updateline(
398 $request_data->subprice,
400 $request_data->remise_percent,
401 $request_data->date_start,
402 $request_data->date_end,
403 $request_data->tva_tx,
404 $request_data->localtax1_tx,
405 $request_data->localtax2_tx,
407 $request_data->info_bits,
408 $request_data->product_type,
409 $request_data->fk_parent_line,
411 $request_data->fk_fournprice,
412 $request_data->pa_ht,
413 $request_data->label,
414 $request_data->special_code,
415 $request_data->array_options,
416 $request_data->situation_percent,
417 $request_data->fk_unit,
418 $request_data->multicurrency_subprice,
420 $request_data->ref_ext
423 if ($updateRes > 0) {
424 $result = $this->
get($id);
425 unset($result->line);
428 throw new RestException(304, $this->invoice->error);
448 if (!DolibarrApiAccess::$user->rights->facture->creer) {
449 throw new RestException(401);
452 $result = $this->invoice->fetch($id);
455 throw new RestException(404,
'Invoice not found');
458 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
459 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
463 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
466 $result = $this->invoice->add_contact($contactid, $type,
'external');
469 throw new RestException(500,
'Error when added the contact');
492 if (!DolibarrApiAccess::$user->rights->facture->creer) {
493 throw new RestException(401);
496 $result = $this->invoice->fetch($id);
499 throw new RestException(404,
'Invoice not found');
503 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
506 $contacts = $this->invoice->liste_contact();
508 foreach ($contacts as $contact) {
509 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
510 $result = $this->invoice->delete_contact($contact[
'rowid']);
513 throw new RestException(500,
'Error when deleted the contact');
539 if (!DolibarrApiAccess::$user->rights->facture->creer) {
540 throw new RestException(401);
542 if (empty($lineid)) {
543 throw new RestException(400,
'Line ID is mandatory');
547 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
550 $result = $this->invoice->fetch($id);
552 throw new RestException(404,
'Invoice not found');
557 $updateRes = $this->invoice->deleteline($lineid);
558 if ($updateRes > 0) {
559 return $this->
get($id);
561 throw new RestException(405, $this->invoice->error);
572 public function put($id, $request_data = null)
574 if (!DolibarrApiAccess::$user->rights->facture->creer) {
575 throw new RestException(401);
578 $result = $this->invoice->fetch($id);
580 throw new RestException(404,
'Invoice not found');
584 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
587 foreach ($request_data as $field => $value) {
588 if ($field ==
'id')
continue;
589 $this->invoice->$field = $value;
593 if (!empty($this->invoice->fk_account))
595 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
596 throw new RestException(400, $this->invoice->error);
600 if ($this->invoice->update(DolibarrApiAccess::$user))
601 return $this->
get($id);
612 public function delete($id)
614 if (!DolibarrApiAccess::$user->rights->facture->supprimer) {
615 throw new RestException(401);
617 $result = $this->invoice->fetch($id);
619 throw new RestException(404,
'Invoice not found');
623 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
626 $result = $this->invoice->delete(DolibarrApiAccess::$user);
629 throw new RestException(500);
635 'message' =>
'Invoice deleted'
663 public function postLine($id, $request_data = null)
665 if (!DolibarrApiAccess::$user->rights->facture->creer) {
666 throw new RestException(401);
669 $result = $this->invoice->fetch($id);
671 throw new RestException(404,
'Invoice not found');
675 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
678 $request_data = (object) $request_data;
681 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
682 $request_data->fk_parent_line = 0;
686 $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);
687 $pa_ht = $marginInfos[0];
689 $updateRes = $this->invoice->addline(
691 $request_data->subprice,
693 $request_data->tva_tx,
694 $request_data->localtax1_tx,
695 $request_data->localtax2_tx,
696 $request_data->fk_product,
697 $request_data->remise_percent,
698 $request_data->date_start,
699 $request_data->date_end,
700 $request_data->fk_code_ventilation,
701 $request_data->info_bits,
702 $request_data->fk_remise_except,
705 $request_data->product_type,
707 $request_data->special_code,
708 $request_data->origin,
709 $request_data->origin_id,
710 $request_data->fk_parent_line,
711 empty($request_data->fk_fournprice) ?null:$request_data->fk_fournprice,
713 $request_data->label,
714 $request_data->array_options,
715 $request_data->situation_percent,
716 $request_data->fk_prev_id,
717 $request_data->fk_unit,
719 $request_data->ref_ext
722 if ($updateRes < 0) {
723 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
748 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
750 if (!DolibarrApiAccess::$user->rights->facture->creer) {
751 throw new RestException(401);
753 $result = $this->invoice->fetch($id);
755 throw new RestException(404,
'Invoice not found');
759 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
762 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
764 throw new RestException(500,
'Error : '.$this->invoice->error);
767 $result = $this->invoice->fetch($id);
769 throw new RestException(404,
'Invoice not found');
773 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
799 if (!DolibarrApiAccess::$user->rights->facture->creer) {
800 throw new RestException(401);
802 $result = $this->invoice->fetch($id);
804 throw new RestException(404,
'Invoice not found');
808 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
811 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
813 throw new RestException(304,
'Nothing done.');
816 throw new RestException(500,
'Error : '.$this->invoice->error);
819 $result = $this->invoice->fetch($id);
821 throw new RestException(404,
'Invoice not found');
825 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
849 public function validate($id, $idwarehouse = 0, $notrigger = 0)
851 if (!DolibarrApiAccess::$user->rights->facture->creer) {
852 throw new RestException(401);
854 $result = $this->invoice->fetch($id);
856 throw new RestException(404,
'Invoice not found');
860 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
863 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
865 throw new RestException(304,
'Error nothing done. May be object is already validated');
868 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
871 $result = $this->invoice->fetch($id);
873 throw new RestException(404,
'Invoice not found');
877 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
899 public function settopaid($id, $close_code =
'', $close_note =
'')
901 if (!DolibarrApiAccess::$user->rights->facture->creer) {
902 throw new RestException(401);
904 $result = $this->invoice->fetch($id);
906 throw new RestException(404,
'Invoice not found');
910 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
913 $result = $this->invoice->set_paid(DolibarrApiAccess::$user, $close_code, $close_note);
915 throw new RestException(304,
'Error nothing done. May be object is already validated');
918 throw new RestException(500,
'Error : '.$this->invoice->error);
922 $result = $this->invoice->fetch($id);
924 throw new RestException(404,
'Invoice not found');
928 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
951 if (!DolibarrApiAccess::$user->rights->facture->creer) {
952 throw new RestException(401);
954 $result = $this->invoice->fetch($id);
956 throw new RestException(404,
'Invoice not found');
960 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
963 $result = $this->invoice->set_unpaid(DolibarrApiAccess::$user);
965 throw new RestException(304,
'Nothing done');
968 throw new RestException(500,
'Error : '.$this->invoice->error);
972 $result = $this->invoice->fetch($id);
974 throw new RestException(404,
'Invoice not found');
978 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
995 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
997 if (!DolibarrApiAccess::$user->rights->facture->lire) {
998 throw new RestException(401);
1001 $result = $this->invoice->fetch($id);
1003 throw new RestException(404,
'Invoice not found');
1007 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1011 $result = $discountcheck->fetch(0, $this->invoice->id);
1014 throw new RestException(404,
'Discount not found');
1017 throw new RestException(500, $discountcheck->error);
1020 return parent::_cleanObjectDatas($discountcheck);
1038 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1040 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1041 throw new RestException(401);
1044 $result = $this->invoice->fetch($id);
1046 throw new RestException(404,
'Invoice not found');
1050 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1053 if ($this->invoice->paye) {
1054 throw new RestException(500,
'Alreay paid');
1057 $this->invoice->fetch($id);
1058 $this->invoice->fetch_thirdparty();
1062 $result = $discountcheck->fetch(0, $this->invoice->id);
1071 $amount_ht = $amount_tva = $amount_ttc = array();
1072 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1076 foreach ($this->invoice->lines as $line)
1078 if ($line->product_type < 9 && $line->total_ht != 0)
1080 $amount_ht[$line->tva_tx] += $line->total_ht;
1081 $amount_tva[$line->tva_tx] += $line->total_tva;
1082 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1083 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1084 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1085 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1093 $discount->description =
'(CREDIT_NOTE)';
1095 $discount->description =
'(DEPOSIT)';
1097 $discount->description =
'(EXCESS RECEIVED)';
1099 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1102 $discount->fk_soc = $this->invoice->socid;
1103 $discount->fk_facture_source = $this->invoice->id;
1112 $sql =
'SELECT SUM(pf.amount) as total_payments';
1113 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1114 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1115 $sql .=
' WHERE pf.fk_facture = '.$this->invoice->id;
1116 $sql .=
' AND pf.fk_paiement = p.rowid';
1117 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1121 $res = $this->
db->fetch_object(
$resql);
1122 $total_payments = $res->total_payments;
1125 $total_creditnote_and_deposit = 0;
1126 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1127 $sql .=
" re.description, re.fk_facture_source";
1128 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1129 $sql .=
" WHERE fk_facture = ".$this->invoice->id;
1132 while ($obj = $this->
db->fetch_object(
$resql)) $total_creditnote_and_deposit += $obj->amount_ttc;
1135 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1136 $discount->amount_tva = 0;
1137 $discount->tva_tx = 0;
1139 $result = $discount->create(DolibarrApiAccess::$user);
1147 foreach ($amount_ht as $tva_tx => $xxx)
1149 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1150 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1151 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1152 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1153 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1154 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1155 $discount->tva_tx = abs($tva_tx);
1157 $result = $discount->create(DolibarrApiAccess::$user);
1170 $result = $this->invoice->set_paid(DolibarrApiAccess::$user);
1173 $this->
db->commit();
1175 $this->
db->rollback();
1176 throw new RestException(500,
'Could not set paid');
1179 $this->
db->commit();
1182 $this->
db->rollback();
1183 throw new RestException(500,
'Discount creation error');
1210 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1211 throw new RestException(401);
1214 throw new RestException(400,
'Invoice ID is mandatory');
1216 if (empty($discountid)) {
1217 throw new RestException(400,
'Discount ID is mandatory');
1221 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1224 $result = $this->invoice->fetch($id);
1226 throw new RestException(404,
'Invoice not found');
1229 $result = $this->invoice->insert_discount($discountid);
1231 throw new RestException(405, $this->invoice->error);
1257 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1259 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1260 throw new RestException(401);
1263 throw new RestException(400,
'Invoice ID is mandatory');
1265 if (empty($discountid)) {
1266 throw new RestException(400,
'Credit ID is mandatory');
1270 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1273 $result = $discount->fetch($discountid);
1275 throw new RestException(404,
'Credit not found');
1278 $result = $discount->link_to_invoice(0, $id);
1280 throw new RestException(405, $discount->error);
1303 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1304 throw new RestException(401);
1307 throw new RestException(400,
'Invoice ID is mandatory');
1311 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1314 $result = $this->invoice->fetch($id);
1316 throw new RestException(404,
'Invoice not found');
1319 $result = $this->invoice->getListOfPayments();
1321 throw new RestException(405, $this->invoice->error);
1349 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1353 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1355 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1356 throw new RestException(403);
1359 throw new RestException(400,
'Invoice ID is mandatory');
1363 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1366 if (!empty($conf->banque->enabled)) {
1367 if (empty($accountid)) {
1368 throw new RestException(400,
'Account ID is mandatory');
1372 if (empty($paymentid)) {
1373 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1377 $result = $this->invoice->fetch($id);
1379 throw new RestException(404,
'Invoice not found');
1383 $totalpaye = $this->invoice->getSommePaiement();
1384 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1385 $totaldeposits = $this->invoice->getSumDepositsUsed();
1386 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits,
'MT');
1391 $multicurrency_amounts = array();
1395 $resteapayer =
price2num($resteapayer,
'MT');
1396 $amounts[$id] = -$resteapayer;
1398 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1399 $multicurrency_amounts[$id] = -$newvalue;
1401 $resteapayer =
price2num($resteapayer,
'MT');
1402 $amounts[$id] = $resteapayer;
1404 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1405 $multicurrency_amounts[$id] = $newvalue;
1411 $paymentobj->datepaye = $datepaye;
1412 $paymentobj->amounts = $amounts;
1413 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1414 $paymentobj->paiementid = $paymentid;
1415 $paymentobj->paiementcode =
dol_getIdFromCode($this->
db, $paymentid,
'c_paiement',
'id',
'code', 1);
1416 $paymentobj->num_payment = $num_payment;
1417 $paymentobj->note_private = $comment;
1419 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1420 if ($payment_id < 0)
1422 $this->
db->rollback();
1423 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1426 if (!empty($conf->banque->enabled)) {
1427 $label =
'(CustomerInvoicePayment)';
1429 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1430 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1433 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1436 $this->
db->rollback();
1437 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1441 $this->
db->commit();
1471 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1475 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1477 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1478 throw new RestException(403);
1480 foreach ($arrayofamounts as $id => $amount) {
1482 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1485 throw new RestException(403,
'Access not allowed on invoice ID '.$id.
' for login '.DolibarrApiAccess::$user->login);
1489 if (!empty($conf->banque->enabled)) {
1490 if (empty($accountid)) {
1491 throw new RestException(400,
'Account ID is mandatory');
1494 if (empty($paymentid)) {
1495 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1501 $multicurrency_amounts = array();
1504 foreach ($arrayofamounts as $id => $amountarray)
1506 $result = $this->invoice->fetch($id);
1508 $this->
db->rollback();
1509 throw new RestException(404,
'Invoice ID '.$id.
' not found');
1512 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1513 $this->
db->rollback();
1514 throw new RestException(400,
'Payment in both currency '.$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1517 $is_multicurrency = 0;
1518 $total_ttc = $this->invoice->total_ttc;
1520 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1521 $is_multicurrency = 1;
1522 $total_ttc = $this->invoice->multicurrency_total_ttc;
1526 $totalpaye = $this->invoice->getSommePaiement($is_multicurrency);
1527 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1528 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1529 $remainstopay = $amount =
price2num($total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits,
'MT');
1531 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain')
1533 $amount =
price2num($amountarray[
"amount"],
'MT');
1536 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain')
1538 $amount =
price2num($amountarray[
"multicurrency_amount"],
'MT');
1541 if ($amount > $remainstopay && !$accepthigherpayment) {
1542 $this->
db->rollback();
1543 throw new RestException(400,
'Payment amount on invoice ID '.$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1550 if ($is_multicurrency) {
1551 $amounts[$id] = null;
1553 $multicurrency_amounts[$id] = $amount;
1555 $amounts[$id] = $amount;
1557 $multicurrency_amounts[$id] = null;
1563 $paymentobj->datepaye = $datepaye;
1564 $paymentobj->amounts = $amounts;
1565 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1566 $paymentobj->paiementid = $paymentid;
1567 $paymentobj->paiementcode =
dol_getIdFromCode($this->
db, $paymentid,
'c_paiement',
'id',
'code', 1);
1568 $paymentobj->num_payment = $num_payment;
1569 $paymentobj->note_private = $comment;
1570 $paymentobj->ref_ext = $ref_ext;
1571 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1572 if ($payment_id < 0)
1574 $this->
db->rollback();
1575 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1577 if (!empty($conf->banque->enabled)) {
1578 $label =
'(CustomerInvoicePayment)';
1579 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1580 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1583 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1586 $this->
db->rollback();
1587 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1591 $this->
db->commit();
1611 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1613 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1614 throw new RestException(401);
1617 throw new RestException(400,
'Payment ID is mandatory');
1621 $result = $paymentobj->fetch($id);
1624 throw new RestException(404,
'Payment not found');
1627 if (!empty($num_payment)) {
1628 $result = $paymentobj->update_num($num_payment);
1630 throw new RestException(500,
'Error when updating the payment num');
1637 'message' =>
'Payment updated'
1652 $object = parent::_cleanObjectDatas($object);
1654 unset($object->note);
1655 unset($object->address);
1656 unset($object->barcode_type);
1657 unset($object->barcode_type_code);
1658 unset($object->barcode_type_label);
1659 unset($object->barcode_type_coder);
1675 foreach (Invoices::$FIELDS as $field) {
1676 if (!isset($data[$field])) {
1677 throw new RestException(400,
"$field field missing");
1679 $invoice[$field] = $data[$field];
const TYPE_STANDARD
Standard invoice.
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.
put($id, $request_data=null)
Update invoice.
useCreditNote($id, $discountid)
Add an available credit note discount to payments of an existing invoice.
settodraft($id, $idwarehouse=-1)
Sets an invoice as draft.
postLine($id, $request_data=null)
Add a line to a given invoice.
_validate($data)
Validate fields before create or update object.
getPayments($id)
Get list of payments of a given invoice.
addContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0)
Adds a contact to an invoice.
dol_now($mode= 'auto')
Return date for now.
useDiscount($id, $discountid)
Add a discount line into an invoice (as an invoice line) using an existing absolute discount...
createInvoiceFromOrder($orderid)
Create an invoice using an existing order.
const TYPE_REPLACEMENT
Replacement invoice.
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.
settounpaid($id)
Sets an invoice as unpaid.
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.
const TYPE_SITUATION
Situation invoice.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
__construct()
Constructor.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $thirdparty_ids= '', $status= '', $sqlfilters= '')
List invoices.
post($request_data=null)
Create invoice object.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
Class to manage customers orders.
Class to manage payments of customer invoices.
deleteContact($id, $contactid, $type)
Delete a contact type of given invoice.
putLine($id, $lineid, $request_data=null)
Update a line to a given invoice.
settopaid($id, $close_code= '', $close_note= '')
Sets an invoice as paid.
markAsCreditAvailable($id)
Create a discount (credit available) for a credit note or a deposit.
postContact($id, $contactid, $type)
Add a contact type of given invoice.
getByRef($ref, $contact_list=1)
Get properties of an invoice object by ref.
_fetch($id, $ref= '', $ref_ext= '', $contact_list=1)
Get properties of an invoice object.
const TYPE_DEPOSIT
Deposit invoice.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
dol_getIdFromCode($db, $key, $tablename, $fieldkey= 'code', $fieldid= 'id', $entityfilter=0)
Return an id or code from a code or id.
_cleanObjectDatas($object)
Clean sensible object datas.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Class to manage absolute discounts.
getLines($id)
Get lines of an invoice.
Class to manage invoices.
deleteLine($id, $lineid)
Deletes a line of a given invoice.
putPayment($id, $num_payment= '')
Update a payment.
getDiscount($id)
Get discount from invoice.
getByRefExt($ref_ext, $contact_list=1)
Get properties of an invoice object by ref_ext.