19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/compta/tva/class/tva.class.php';
22require_once DOL_DOCUMENT_ROOT.
'/compta/tva/class/paymentvat.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
39 public static $FIELDS = array(
49 public static $PAYMENT_FIELDS = array(
73 public function get(
$id)
75 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
76 throw new RestException(403);
79 $vat =
new Tva($this->db);
80 $result = $vat->fetch(
$id);
83 if ($result <= 0 || empty($vat->id)) {
84 throw new RestException(404,
'VAT payment not found');
108 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
110 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
111 throw new RestException(403);
116 $sql =
"SELECT t.rowid";
117 $sql .=
" FROM ".MAIN_DB_PREFIX.
"tva AS t";
118 $sql .=
" WHERE t.entity IN (".getEntity(
'tva').
")";
125 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
130 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
132 $sql .= $this->db->order($sortfield, $sortorder);
137 $offset = $limit * $page;
139 $sql .= $this->db->plimit($limit + 1, $offset);
143 $result = $this->db->query($sql);
146 throw new RestException(503,
'Error when retrieving list of VAT payments: '.$this->db->lasterror());
149 $num = $this->db->num_rows($result);
150 $min = min($num, ($limit <= 0 ? $num : $limit));
151 for ($i = 0; $i < $min; $i++) {
152 $obj = $this->db->fetch_object($result);
153 $vat =
new Tva($this->db);
154 if ($vat->fetch($obj->rowid) > 0) {
161 if ($pagination_data) {
162 $totalsResult = $this->db->query($sqlTotals);
163 $total = $this->db->fetch_object($totalsResult)->total;
168 $obj_ret[
'data'] = $tmp;
169 $obj_ret[
'pagination'] = array(
170 'total' => (
int) $total,
172 'page_count' => ($limit > 0 ? (
int) ceil((
int) $total / $limit) : 1),
192 public function post($request_data =
null)
194 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'creer')) {
195 throw new RestException(403);
201 $vat =
new Tva($this->db);
202 foreach ($request_data as $field => $value) {
203 if ($field ===
'caller') {
205 $vat->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
212 if ($vat->create(DolibarrApiAccess::$user) < 0) {
213 throw new RestException(500,
'Error when creating VAT payment: '.$vat->errorsToString());
232 public function put(
$id, $request_data =
null)
234 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'creer')) {
235 throw new RestException(403);
238 $vat =
new Tva($this->db);
239 $result = $vat->fetch(
$id);
242 if ($result <= 0 || empty($vat->id)) {
243 throw new RestException(404,
'VAT payment not found');
246 foreach ($request_data as $field => $value) {
247 if ($field ==
'id') {
250 if ($field ===
'caller') {
252 $vat->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
255 if ($field ==
'array_options' && is_array($value)) {
256 foreach ($value as $index => $val) {
265 if ($vat->update(DolibarrApiAccess::$user) > 0) {
266 return $this->
get(
$id);
268 throw new RestException(500,
'Error when updating VAT payment: '.$vat->errorsToString());
284 public function delete(
$id)
286 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'supprimer')) {
287 throw new RestException(403);
290 $vat =
new Tva($this->db);
291 $result = $vat->fetch(
$id);
294 if ($result <= 0 || empty($vat->id)) {
295 throw new RestException(404,
'VAT payment not found');
299 if (!empty($vat->getSommePaiement())) {
300 throw new RestException(403,
'VAT declaration not erasable, it has payments');
303 if ($vat->delete(DolibarrApiAccess::$user) < 0) {
304 throw new RestException(500,
'Error when deleting VAT payment: '.$vat->errorsToString());
310 'message' =>
'VAT payment deleted'
331 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
333 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
334 throw new RestException(403);
339 $sql =
"SELECT t.rowid FROM ".MAIN_DB_PREFIX.
"payment_vat AS t, ".MAIN_DB_PREFIX.
"tva AS s";
340 $sql .=
" WHERE t.fk_tva = s.rowid AND s.entity IN (".getEntity(
'tva').
")";
341 $sql .= $this->db->order($sortfield, $sortorder);
346 $offset = $limit * $page;
347 $sql .= $this->db->plimit($limit + 1, $offset);
350 $result = $this->db->query($sql);
352 throw new RestException(503,
'Error when retrieving list of VAT payments: '.$this->db->lasterror());
355 $num = $this->db->num_rows($result);
356 $min = min($num, ($limit <= 0 ? $num : $limit));
357 for ($i = 0; $i < $min; $i++) {
358 $obj = $this->db->fetch_object($result);
360 if ($payment->fetch($obj->rowid) > 0) {
383 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
384 throw new RestException(403);
389 $sql =
"SELECT t.rowid FROM ".MAIN_DB_PREFIX.
"payment_vat AS t";
390 $sql .=
" WHERE t.fk_tva = ".((int)
$id);
391 $sql .= $this->db->order(
't.rowid',
'ASC');
393 $result = $this->db->query($sql);
395 throw new RestException(503,
'Error when retrieving list of VAT payments: '.$this->db->lasterror());
398 $num = $this->db->num_rows($result);
399 for ($i = 0; $i < $num; $i++) {
400 $obj = $this->db->fetch_object($result);
402 if ($payment->fetch($obj->rowid) > 0) {
423 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
424 throw new RestException(403);
428 $result = $payment->fetch($pid);
429 if ($result <= 0 || empty($payment->id)) {
430 throw new RestException(404,
'VAT payment not found');
453 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'creer')) {
454 throw new RestException(403);
461 $payment->chid =
$id;
462 $payment->datepaye = $request_data[
'datepaye'];
463 $payment->amounts = array(
$id => (
float) $request_data[
'amount']);
464 $payment->paiementtype = $request_data[
'paiementtype'];
465 if (isset($request_data[
'num_payment'])) {
466 $payment->num_payment = $request_data[
'num_payment'];
468 if (isset($request_data[
'note'])) {
469 $payment->note = $request_data[
'note'];
472 if ($payment->create(DolibarrApiAccess::$user) < 0) {
473 throw new RestException(500,
'Error when creating VAT payment', array_merge(array($payment->error), $payment->errors));
476 if (
isModEnabled(
"bank") && !empty($request_data[
'accountid'])) {
477 $payment->addPaymentToBank(DolibarrApiAccess::$user,
'payment_vat',
'(VATPayment)', (
int) $request_data[
'accountid'],
'',
'');
499 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'supprimer')) {
500 throw new RestException(403);
504 $result = $payment->fetch($pid);
505 if ($result <= 0 || empty($payment->id)) {
506 throw new RestException(404,
'VAT payment not found');
509 if ($payment->delete(DolibarrApiAccess::$user) < 0) {
510 throw new RestException(500,
'Error when deleting VAT payment: '.$payment->errorsToString());
516 'message' =>
'VAT payment deleted'
531 if ($data ===
null) {
535 foreach (VatPayments::$FIELDS as $field) {
536 if (!isset($data[$field])) {
537 throw new RestException(400,
"$field field missing");
539 $vat[$field] = $data[$field];
554 if ($data ===
null) {
558 foreach (VatPayments::$PAYMENT_FIELDS as $field) {
559 if (!isset($data[$field])) {
560 throw new RestException(400,
"$field field missing");
562 $payment[$field] = $data[$field];
581 unset(
$object->labelStatusShort);
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_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.
Class to manage payments of social contributions.
Class to manage VAT - Value-added tax (also known in French as TVA)
_cleanObjectDatas($object)
Clean sensitive object data.
addPayment($id, $request_data=null)
Add a payment to a VAT declaration.
__construct()
Constructor.
put($id, $request_data=null)
Update a VAT payment (declaration).
deletePayment($pid)
Delete a VAT payment.
getPayments($id)
Get the list of payments of a given VAT declaration.
_validatePayment($data)
Validate fields before creating a VAT payment.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List VAT payments (declarations).
getPayment($pid)
Get a given VAT payment.
getAllPayments($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0)
List all VAT payments.
post($request_data=null)
Create a VAT payment (declaration).
_validate($data)
Validate fields before creating a VAT declaration.
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.