19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
22 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture-rec.class.php';
37 static $FIELDS = array(
49 private $template_invoice;
74 public function get($id, $contact_list = 1)
76 return $this->
_fetch($id,
'',
'', $contact_list);
92 public function getByRef($ref, $contact_list = 1)
94 return $this->
_fetch(
'', $ref,
'', $contact_list);
112 return $this->
_fetch(
'',
'', $ref_ext, $contact_list);
128 private function _fetch($id, $ref =
'', $ref_ext =
'', $contact_list = 1)
130 if (!DolibarrApiAccess::$user->rights->facture->lire) {
131 throw new RestException(401);
134 $result = $this->invoice->fetch($id, $ref, $ref_ext);
136 throw new RestException(404,
'Invoice not found');
140 $this->invoice->totalpaid = $this->invoice->getSommePaiement();
141 $this->invoice->totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
142 $this->invoice->totaldeposits = $this->invoice->getSumDepositsUsed();
143 $this->invoice->remaintopay =
price2num($this->invoice->total_ttc - $this->invoice->totalpaid - $this->invoice->totalcreditnotes - $this->invoice->totaldeposits,
'MT');
146 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
150 if ($contact_list > -1) {
151 $tmparray = $this->invoice->liste_contact(-1,
'external', $contact_list);
152 if (is_array($tmparray)) {
153 $this->invoice->contacts_ids = $tmparray;
157 $this->invoice->fetchObjectLinked();
179 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $status =
'', $sqlfilters =
'')
183 if (!DolibarrApiAccess::$user->rights->facture->lire) {
184 throw new RestException(401);
190 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
194 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
195 $search_sale = DolibarrApiAccess::$user->id;
198 $sql =
"SELECT t.rowid";
199 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
200 $sql .=
", sc.fk_soc, sc.fk_user";
202 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as t";
204 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
205 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
208 $sql .=
' WHERE t.entity IN ('.getEntity(
'invoice').
')';
209 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
210 $sql .=
" AND t.fk_soc = sc.fk_soc";
213 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
216 if ($search_sale > 0) {
217 $sql .=
" AND t.rowid = sc.fk_soc";
221 if ($status ==
'draft') {
222 $sql .=
" AND t.fk_statut IN (0)";
224 if ($status ==
'unpaid') {
225 $sql .=
" AND t.fk_statut IN (1)";
227 if ($status ==
'paid') {
228 $sql .=
" AND t.fk_statut IN (2)";
230 if ($status ==
'cancelled') {
231 $sql .=
" AND t.fk_statut IN (3)";
234 if ($search_sale > 0) {
235 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
241 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
243 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
244 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
247 $sql .= $this->
db->order($sortfield, $sortorder);
252 $offset = $limit * $page;
254 $sql .= $this->
db->plimit($limit + 1, $offset);
257 $result = $this->
db->query($sql);
260 $num = $this->
db->num_rows($result);
261 $min = min($num, ($limit <= 0 ? $num : $limit));
263 $obj = $this->
db->fetch_object($result);
264 $invoice_static =
new Facture($this->
db);
265 if ($invoice_static->fetch($obj->rowid)) {
267 $invoice_static->totalpaid = $invoice_static->getSommePaiement();
268 $invoice_static->totalcreditnotes = $invoice_static->getSumCreditNotesUsed();
269 $invoice_static->totaldeposits = $invoice_static->getSumDepositsUsed();
270 $invoice_static->remaintopay =
price2num($invoice_static->total_ttc - $invoice_static->totalpaid - $invoice_static->totalcreditnotes - $invoice_static->totaldeposits,
'MT');
273 $tmparray = $invoice_static->liste_contact(-1,
'external', 1);
274 if (is_array($tmparray)) {
275 $invoice_static->contacts_ids = $tmparray;
282 throw new RestException(503,
'Error when retrieve invoice list : '.$this->
db->lasterror());
284 if (!count($obj_ret)) {
285 throw new RestException(404,
'No invoice found');
296 public function post($request_data =
null)
298 if (!DolibarrApiAccess::$user->rights->facture->creer) {
299 throw new RestException(401,
"Insuffisant rights");
302 $result = $this->
_validate($request_data);
304 foreach ($request_data as $field => $value) {
305 $this->invoice->$field = $value;
307 if (!array_key_exists(
'date', $request_data)) {
308 $this->invoice->date =
dol_now();
319 if ($this->invoice->create(DolibarrApiAccess::$user, 0, (empty($request_data[
"date_lim_reglement"]) ? 0 : $request_data[
"date_lim_reglement"])) < 0) {
320 throw new RestException(500,
"Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors));
322 return $this->invoice->id;
342 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
344 if (!DolibarrApiAccess::$user->rights->commande->lire) {
345 throw new RestException(401);
347 if (!DolibarrApiAccess::$user->rights->facture->creer) {
348 throw new RestException(401);
350 if (empty($orderid)) {
351 throw new RestException(400,
'Order ID is mandatory');
355 $result = $order->fetch($orderid);
357 throw new RestException(404,
'Order not found');
360 $result = $this->invoice->createFromOrder($order, DolibarrApiAccess::$user);
362 throw new RestException(405, $this->invoice->error);
364 $this->invoice->fetchObjectLinked();
379 if (!DolibarrApiAccess::$user->rights->facture->lire) {
380 throw new RestException(401);
383 $result = $this->invoice->fetch($id);
385 throw new RestException(404,
'Invoice not found');
389 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
391 $this->invoice->getLinesArray();
393 foreach ($this->invoice->lines as $line) {
414 public function putLine($id, $lineid, $request_data =
null)
416 if (!DolibarrApiAccess::$user->rights->facture->creer) {
417 throw new RestException(401);
420 $result = $this->invoice->fetch($id);
422 throw new RestException(404,
'Invoice not found');
426 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
429 $request_data = (object) $request_data;
431 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
432 $request_data->label =
sanitizeVal($request_data->label);
434 $updateRes = $this->invoice->updateline(
437 $request_data->subprice,
439 $request_data->remise_percent,
440 $request_data->date_start,
441 $request_data->date_end,
442 $request_data->tva_tx,
443 $request_data->localtax1_tx,
444 $request_data->localtax2_tx,
445 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
446 $request_data->info_bits,
447 $request_data->product_type,
448 $request_data->fk_parent_line,
450 $request_data->fk_fournprice,
451 $request_data->pa_ht,
452 $request_data->label,
453 $request_data->special_code,
454 $request_data->array_options,
455 $request_data->situation_percent,
456 $request_data->fk_unit,
457 $request_data->multicurrency_subprice,
459 $request_data->ref_ext,
463 if ($updateRes > 0) {
464 $result = $this->
get($id);
465 unset($result->line);
468 throw new RestException(304, $this->invoice->error);
488 if (!DolibarrApiAccess::$user->rights->facture->creer) {
489 throw new RestException(401);
492 $result = $this->invoice->fetch($id);
495 throw new RestException(404,
'Invoice not found');
498 if (!in_array($type, array(
'BILLING',
'SHIPPING',
'CUSTOMER'),
true)) {
499 throw new RestException(500,
'Availables types: BILLING, SHIPPING OR CUSTOMER');
503 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
506 $result = $this->invoice->add_contact($contactid, $type,
'external');
509 throw new RestException(500,
'Error when added the contact');
532 if (!DolibarrApiAccess::$user->rights->facture->creer) {
533 throw new RestException(401);
536 $result = $this->invoice->fetch($id);
539 throw new RestException(404,
'Invoice not found');
543 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
546 $contacts = $this->invoice->liste_contact();
548 foreach ($contacts as $contact) {
549 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
550 $result = $this->invoice->delete_contact($contact[
'rowid']);
553 throw new RestException(500,
'Error when deleted the contact');
579 if (!DolibarrApiAccess::$user->rights->facture->creer) {
580 throw new RestException(401);
582 if (empty($lineid)) {
583 throw new RestException(400,
'Line ID is mandatory');
587 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
590 $result = $this->invoice->fetch($id);
592 throw new RestException(404,
'Invoice not found');
597 $updateRes = $this->invoice->deleteline($lineid);
598 if ($updateRes > 0) {
599 return $this->
get($id);
601 throw new RestException(405, $this->invoice->error);
612 public function put($id, $request_data =
null)
614 if (!DolibarrApiAccess::$user->rights->facture->creer) {
615 throw new RestException(401);
618 $result = $this->invoice->fetch($id);
620 throw new RestException(404,
'Invoice not found');
624 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
627 foreach ($request_data as $field => $value) {
628 if ($field ==
'id') {
631 $this->invoice->$field = $value;
635 if (!empty($this->invoice->fk_account)) {
636 if ($this->invoice->setBankAccount($this->invoice->fk_account) == 0) {
637 throw new RestException(400, $this->invoice->error);
641 if ($this->invoice->update(DolibarrApiAccess::$user)) {
642 return $this->
get($id);
654 public function delete($id)
656 if (!DolibarrApiAccess::$user->rights->facture->supprimer) {
657 throw new RestException(401);
659 $result = $this->invoice->fetch($id);
661 throw new RestException(404,
'Invoice not found');
665 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
668 $result = $this->invoice->delete(DolibarrApiAccess::$user);
670 throw new RestException(500,
'Error when deleting invoice');
676 'message' =>
'Invoice deleted'
704 public function postLine($id, $request_data =
null)
706 if (!DolibarrApiAccess::$user->rights->facture->creer) {
707 throw new RestException(401);
710 $result = $this->invoice->fetch($id);
712 throw new RestException(404,
'Invoice not found');
716 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
719 $request_data = (object) $request_data;
721 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
722 $request_data->label =
sanitizeVal($request_data->label);
725 if (($request_data->product_type != 9 && empty($request_data->fk_parent_line)) || $request_data->product_type == 9) {
726 $request_data->fk_parent_line = 0;
730 $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);
731 $pa_ht = $marginInfos[0];
733 $updateRes = $this->invoice->addline(
735 $request_data->subprice,
737 $request_data->tva_tx,
738 $request_data->localtax1_tx,
739 $request_data->localtax2_tx,
740 $request_data->fk_product,
741 $request_data->remise_percent,
742 $request_data->date_start,
743 $request_data->date_end,
744 $request_data->fk_code_ventilation,
745 $request_data->info_bits,
746 $request_data->fk_remise_except,
747 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
748 $request_data->subprice,
749 $request_data->product_type,
751 $request_data->special_code,
752 $request_data->origin,
753 $request_data->origin_id,
754 $request_data->fk_parent_line,
755 empty($request_data->fk_fournprice) ?
null:$request_data->fk_fournprice,
757 $request_data->label,
758 $request_data->array_options,
759 $request_data->situation_percent,
760 $request_data->fk_prev_id,
761 $request_data->fk_unit,
763 $request_data->ref_ext
766 if ($updateRes < 0) {
767 throw new RestException(400,
'Unable to insert the new line. Check your inputs. '.$this->invoice->error);
792 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
794 if (!DolibarrApiAccess::$user->rights->facture->creer) {
795 throw new RestException(401);
797 $result = $this->invoice->fetch($id);
799 throw new RestException(404,
'Invoice not found');
803 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
806 $result = $this->invoice->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
808 throw new RestException(500,
'Error : '.$this->invoice->error);
811 $result = $this->invoice->fetch($id);
813 throw new RestException(404,
'Invoice not found');
817 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
843 if (!DolibarrApiAccess::$user->rights->facture->creer) {
844 throw new RestException(401);
846 $result = $this->invoice->fetch($id);
848 throw new RestException(404,
'Invoice not found');
852 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
855 $result = $this->invoice->setDraft(DolibarrApiAccess::$user, $idwarehouse);
857 throw new RestException(304,
'Nothing done.');
860 throw new RestException(500,
'Error : '.$this->invoice->error);
863 $result = $this->invoice->fetch($id);
865 throw new RestException(404,
'Invoice not found');
869 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
893 public function validate($id, $idwarehouse = 0, $notrigger = 0)
895 if (!DolibarrApiAccess::$user->rights->facture->creer) {
896 throw new RestException(401);
898 $result = $this->invoice->fetch($id);
900 throw new RestException(404,
'Invoice not found');
904 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
907 $result = $this->invoice->validate(DolibarrApiAccess::$user,
'', $idwarehouse, $notrigger);
909 throw new RestException(304,
'Error nothing done. May be object is already validated');
912 throw new RestException(500,
'Error when validating Invoice: '.$this->invoice->error);
915 $result = $this->invoice->fetch($id);
917 throw new RestException(404,
'Invoice not found');
921 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
943 public function settopaid($id, $close_code =
'', $close_note =
'')
945 if (!DolibarrApiAccess::$user->rights->facture->creer) {
946 throw new RestException(401);
948 $result = $this->invoice->fetch($id);
950 throw new RestException(404,
'Invoice not found');
954 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
957 $result = $this->invoice->setPaid(DolibarrApiAccess::$user, $close_code, $close_note);
959 throw new RestException(304,
'Error nothing done. May be object is already validated');
962 throw new RestException(500,
'Error : '.$this->invoice->error);
966 $result = $this->invoice->fetch($id);
968 throw new RestException(404,
'Invoice not found');
972 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
995 if (!DolibarrApiAccess::$user->rights->facture->creer) {
996 throw new RestException(401);
998 $result = $this->invoice->fetch($id);
1000 throw new RestException(404,
'Invoice not found');
1004 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1007 $result = $this->invoice->setUnpaid(DolibarrApiAccess::$user);
1009 throw new RestException(304,
'Nothing done');
1012 throw new RestException(500,
'Error : '.$this->invoice->error);
1016 $result = $this->invoice->fetch($id);
1018 throw new RestException(404,
'Invoice not found');
1022 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1039 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1041 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1042 throw new RestException(401);
1045 $result = $this->invoice->fetch($id);
1047 throw new RestException(404,
'Invoice not found');
1051 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1055 $result = $discountcheck->fetch(0, $this->invoice->id);
1058 throw new RestException(404,
'Discount not found');
1061 throw new RestException(500, $discountcheck->error);
1064 return parent::_cleanObjectDatas($discountcheck);
1082 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1084 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1085 throw new RestException(401);
1088 $result = $this->invoice->fetch($id);
1090 throw new RestException(404,
'Invoice not found');
1094 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1097 if ($this->invoice->paye) {
1098 throw new RestException(500,
'Alreay paid');
1101 $this->invoice->fetch($id);
1102 $this->invoice->fetch_thirdparty();
1106 $result = $discountcheck->fetch(0, $this->invoice->id);
1118 $amount_ht = $amount_tva = $amount_ttc = array();
1119 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
1123 foreach ($this->invoice->lines as $line) {
1124 if ($line->product_type < 9 && $line->total_ht != 0) {
1126 $amount_ht[$line->tva_tx] += $line->total_ht;
1127 $amount_tva[$line->tva_tx] += $line->total_tva;
1128 $amount_ttc[$line->tva_tx] += $line->total_ttc;
1129 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
1130 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
1131 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
1139 $discount->description =
'(CREDIT_NOTE)';
1141 $discount->description =
'(DEPOSIT)';
1143 $discount->description =
'(EXCESS RECEIVED)';
1145 throw new RestException(500,
'Cant convert to reduc an Invoice of this type');
1148 $discount->fk_soc = $this->invoice->socid;
1149 $discount->fk_facture_source = $this->invoice->id;
1157 $sql =
'SELECT SUM(pf.amount) as total_payments';
1158 $sql .=
' FROM '.MAIN_DB_PREFIX.
'paiement_facture as pf, '.MAIN_DB_PREFIX.
'paiement as p';
1159 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as c ON p.fk_paiement = c.id';
1160 $sql .=
' WHERE pf.fk_facture = '.((int) $this->invoice->id);
1161 $sql .=
' AND pf.fk_paiement = p.rowid';
1162 $sql .=
' AND p.entity IN ('.getEntity(
'invoice').
')';
1168 $res = $this->
db->fetch_object(
$resql);
1169 $total_payments = $res->total_payments;
1172 $total_creditnote_and_deposit = 0;
1173 $sql =
"SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
1174 $sql .=
" re.description, re.fk_facture_source";
1175 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1176 $sql .=
" WHERE fk_facture = ".((int) $this->invoice->id);
1179 while ($obj = $this->
db->fetch_object(
$resql)) {
1180 $total_creditnote_and_deposit += $obj->amount_ttc;
1186 $discount->amount_ht = $discount->amount_ttc = $total_payments + $total_creditnote_and_deposit - $this->invoice->total_ttc;
1187 $discount->amount_tva = 0;
1188 $discount->tva_tx = 0;
1190 $result = $discount->create(DolibarrApiAccess::$user);
1196 foreach ($amount_ht as $tva_tx => $xxx) {
1197 $discount->amount_ht = abs($amount_ht[$tva_tx]);
1198 $discount->amount_tva = abs($amount_tva[$tva_tx]);
1199 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
1200 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
1201 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
1202 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
1203 $discount->tva_tx = abs($tva_tx);
1205 $result = $discount->create(DolibarrApiAccess::$user);
1213 if (empty($error)) {
1216 $result = $this->invoice->setPaid(DolibarrApiAccess::$user);
1218 $this->
db->commit();
1220 $this->
db->rollback();
1221 throw new RestException(500,
'Could not set paid');
1224 $this->
db->commit();
1227 $this->
db->rollback();
1228 throw new RestException(500,
'Discount creation error');
1255 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1256 throw new RestException(401);
1259 throw new RestException(400,
'Invoice ID is mandatory');
1261 if (empty($discountid)) {
1262 throw new RestException(400,
'Discount ID is mandatory');
1266 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1269 $result = $this->invoice->fetch($id);
1271 throw new RestException(404,
'Invoice not found');
1274 $result = $this->invoice->insert_discount($discountid);
1276 throw new RestException(405, $this->invoice->error);
1302 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1304 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1305 throw new RestException(401);
1308 throw new RestException(400,
'Invoice ID is mandatory');
1310 if (empty($discountid)) {
1311 throw new RestException(400,
'Credit ID is mandatory');
1315 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1318 $result = $discount->fetch($discountid);
1320 throw new RestException(404,
'Credit not found');
1323 $result = $discount->link_to_invoice(0, $id);
1325 throw new RestException(405, $discount->error);
1348 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1349 throw new RestException(401);
1352 throw new RestException(400,
'Invoice ID is mandatory');
1356 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1359 $result = $this->invoice->fetch($id);
1361 throw new RestException(404,
'Invoice not found');
1364 $result = $this->invoice->getListOfPayments();
1366 throw new RestException(405, $this->invoice->error);
1394 public function addPayment($id, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'')
1398 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1400 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1401 throw new RestException(403);
1404 throw new RestException(400,
'Invoice ID is mandatory');
1408 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1412 if (empty($accountid)) {
1413 throw new RestException(400,
'Account ID is mandatory');
1417 if (empty($paymentid)) {
1418 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1422 $result = $this->invoice->fetch($id);
1424 throw new RestException(404,
'Invoice not found');
1428 $totalpaid = $this->invoice->getSommePaiement();
1429 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed();
1430 $totaldeposits = $this->invoice->getSumDepositsUsed();
1431 $resteapayer =
price2num($this->invoice->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1436 $multicurrency_amounts = array();
1440 $resteapayer =
price2num($resteapayer,
'MT');
1441 $amounts[$id] = -$resteapayer;
1443 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1444 $multicurrency_amounts[$id] = -$newvalue;
1446 $resteapayer =
price2num($resteapayer,
'MT');
1447 $amounts[$id] = $resteapayer;
1449 $newvalue =
price2num($this->invoice->multicurrency_total_ttc,
'MT');
1450 $multicurrency_amounts[$id] = $newvalue;
1455 $paymentobj->datepaye = $datepaye;
1456 $paymentobj->amounts = $amounts;
1457 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1458 $paymentobj->paiementid = $paymentid;
1459 $paymentobj->paiementcode =
dol_getIdFromCode($this->
db, $paymentid,
'c_paiement',
'id',
'code', 1);
1460 $paymentobj->num_payment = $num_payment;
1461 $paymentobj->note_private = $comment;
1463 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1464 if ($payment_id < 0) {
1465 $this->
db->rollback();
1466 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1470 $label =
'(CustomerInvoicePayment)';
1472 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1473 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1476 $label =
'(CustomerInvoicePaymentBack)';
1478 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1480 $this->
db->rollback();
1481 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1485 $this->
db->commit();
1515 public function addPaymentDistributed($arrayofamounts, $datepaye, $paymentid, $closepaidinvoices, $accountid, $num_payment =
'', $comment =
'', $chqemetteur =
'', $chqbank =
'', $ref_ext =
'', $accepthigherpayment =
false)
1519 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1521 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1522 throw new RestException(403);
1524 foreach ($arrayofamounts as $id => $amount) {
1526 throw new RestException(400,
'Invoice ID is mandatory. Fill the invoice id and amount into arrayofamounts parameter. For example: {"1": "99.99", "2": "10"}');
1529 throw new RestException(403,
'Access not allowed on invoice ID '.$id.
' for login '.DolibarrApiAccess::$user->login);
1534 if (empty($accountid)) {
1535 throw new RestException(400,
'Account ID is mandatory');
1538 if (empty($paymentid)) {
1539 throw new RestException(400,
'Payment ID or Payment Code is mandatory');
1545 $multicurrency_amounts = array();
1548 foreach ($arrayofamounts as $id => $amountarray) {
1549 $result = $this->invoice->fetch($id);
1551 $this->
db->rollback();
1552 throw new RestException(404,
'Invoice ID '.$id.
' not found');
1555 if (($amountarray[
"amount"] ==
"remain" || $amountarray[
"amount"] > 0) && ($amountarray[
"multicurrency_amount"] ==
"remain" || $amountarray[
"multicurrency_amount"] > 0)) {
1556 $this->
db->rollback();
1557 throw new RestException(400,
'Payment in both currency '.$id.
' ( amount: '.$amountarray[
"amount"].
', multicurrency_amount: '.$amountarray[
"multicurrency_amount"].
')');
1560 $is_multicurrency = 0;
1561 $total_ttc = $this->invoice->total_ttc;
1563 if ($amountarray[
"multicurrency_amount"] > 0 || $amountarray[
"multicurrency_amount"] ==
"remain") {
1564 $is_multicurrency = 1;
1565 $total_ttc = $this->invoice->multicurrency_total_ttc;
1569 $totalpaid = $this->invoice->getSommePaiement($is_multicurrency);
1570 $totalcreditnotes = $this->invoice->getSumCreditNotesUsed($is_multicurrency);
1571 $totaldeposits = $this->invoice->getSumDepositsUsed($is_multicurrency);
1572 $remainstopay = $amount =
price2num($total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits,
'MT');
1574 if (!$is_multicurrency && $amountarray[
"amount"] !=
'remain') {
1575 $amount =
price2num($amountarray[
"amount"],
'MT');
1578 if ($is_multicurrency && $amountarray[
"multicurrency_amount"] !=
'remain') {
1579 $amount =
price2num($amountarray[
"multicurrency_amount"],
'MT');
1582 if ($amount > $remainstopay && !$accepthigherpayment) {
1583 $this->
db->rollback();
1584 throw new RestException(400,
'Payment amount on invoice ID '.$id.
' ('.$amount.
') is higher than remain to pay ('.$remainstopay.
')');
1591 if ($is_multicurrency) {
1592 $amounts[$id] =
null;
1594 $multicurrency_amounts[$id] = $amount;
1596 $amounts[$id] = $amount;
1598 $multicurrency_amounts[$id] =
null;
1604 $paymentobj->datepaye = $datepaye;
1605 $paymentobj->amounts = $amounts;
1606 $paymentobj->multicurrency_amounts = $multicurrency_amounts;
1607 $paymentobj->paiementid = $paymentid;
1608 $paymentobj->paiementcode =
dol_getIdFromCode($this->
db, $paymentid,
'c_paiement',
'id',
'code', 1);
1609 $paymentobj->num_payment = $num_payment;
1610 $paymentobj->note_private = $comment;
1611 $paymentobj->ref_ext = $ref_ext;
1612 $payment_id = $paymentobj->create(DolibarrApiAccess::$user, ($closepaidinvoices ==
'yes' ? 1 : 0));
1613 if ($payment_id < 0) {
1614 $this->
db->rollback();
1615 throw new RestException(400,
'Payment error : '.$paymentobj->error);
1618 $label =
'(CustomerInvoicePayment)';
1619 if ($paymentobj->paiementcode ==
'CHQ' && empty($chqemetteur)) {
1620 throw new RestException(400,
'Emetteur is mandatory when payment code is '.$paymentobj->paiementcode);
1623 $label =
'(CustomerInvoicePaymentBack)';
1625 $result = $paymentobj->addPaymentToBank(DolibarrApiAccess::$user,
'payment', $label, $accountid, $chqemetteur, $chqbank);
1627 $this->
db->rollback();
1628 throw new RestException(400,
'Add payment to bank error : '.$paymentobj->error);
1632 $this->
db->commit();
1652 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
1654 if (!DolibarrApiAccess::$user->rights->facture->creer) {
1655 throw new RestException(401);
1658 throw new RestException(400,
'Payment ID is mandatory');
1662 $result = $paymentobj->fetch($id);
1665 throw new RestException(404,
'Payment not found');
1668 if (!empty($num_payment)) {
1669 $result = $paymentobj->update_num($num_payment);
1671 throw new RestException(500,
'Error when updating the payment num');
1678 'message' =>
'Payment updated'
1693 $object = parent::_cleanObjectDatas($object);
1695 unset($object->note);
1696 unset($object->address);
1697 unset($object->barcode_type);
1698 unset($object->barcode_type_code);
1699 unset($object->barcode_type_label);
1700 unset($object->barcode_type_coder);
1701 unset($object->canvas);
1717 foreach (Invoices::$FIELDS as $field) {
1718 if (!isset($data[$field])) {
1719 throw new RestException(400,
"$field field missing");
1721 $invoice[$field] = $data[$field];
1760 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1761 throw new RestException(401);
1764 $result = $this->template_invoice->fetch($id, $ref, $ref_ext);
1766 throw new RestException(404,
'Template invoice not found');
1770 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1774 if ($contact_list > -1) {
1775 $tmparray = $this->template_invoice->liste_contact(-1,
'external', $contact_list);
1776 if (is_array($tmparray)) {
1777 $this->template_invoice->contacts_ids = $tmparray;
1781 $this->template_invoice->fetchObjectLinked();
1796 $object = parent::_cleanObjectDatas($object);
1798 unset($object->note);
1799 unset($object->address);
1800 unset($object->barcode_type);
1801 unset($object->barcode_type_code);
1802 unset($object->barcode_type_label);
1803 unset($object->barcode_type_coder);
1804 unset($object->canvas);