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';
26require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
40 public static $FIELDS = array(
50 public static $PAYMENT_FIELDS = array(
74 public function get(
$id)
76 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
77 throw new RestException(403);
80 $vat =
new Tva($this->db);
81 $result = $vat->fetch(
$id);
84 if ($result <= 0 || empty($vat->id)) {
85 throw new RestException(404,
'VAT payment not found');
109 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
111 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
112 throw new RestException(403);
117 $sql =
"SELECT t.rowid";
118 $sql .=
" FROM ".MAIN_DB_PREFIX.
"tva AS t";
119 $sql .=
" WHERE t.entity IN (".getEntity(
'tva').
")";
126 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
131 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
133 $sql .= $this->db->order($sortfield, $sortorder);
138 $offset = $limit * $page;
140 $sql .= $this->db->plimit($limit + 1, $offset);
144 $result = $this->db->query($sql);
147 throw new RestException(503,
'Error when retrieving list of VAT payments: '.$this->db->lasterror());
150 $num = $this->db->num_rows($result);
151 $min = min($num, ($limit <= 0 ? $num : $limit));
152 for ($i = 0; $i < $min; $i++) {
153 $obj = $this->db->fetch_object($result);
154 $vat =
new Tva($this->db);
155 if ($vat->fetch($obj->rowid) > 0) {
162 if ($pagination_data) {
163 $totalsResult = $this->db->query($sqlTotals);
164 $total = $this->db->fetch_object($totalsResult)->total;
169 $obj_ret[
'data'] = $tmp;
170 $obj_ret[
'pagination'] = array(
171 'total' => (
int) $total,
173 'page_count' => ($limit > 0 ? (
int) ceil((
int) $total / $limit) : 1),
193 public function post($request_data =
null)
195 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'creer')) {
196 throw new RestException(403);
202 $vat =
new Tva($this->db);
203 foreach ($request_data as $field => $value) {
204 if ($field ===
'caller') {
206 $vat->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
213 if ($vat->create(DolibarrApiAccess::$user) < 0) {
214 throw new RestException(500,
'Error when creating VAT payment: '.$vat->errorsToString());
233 public function put(
$id, $request_data =
null)
235 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'creer')) {
236 throw new RestException(403);
239 $vat =
new Tva($this->db);
240 $result = $vat->fetch(
$id);
243 if ($result <= 0 || empty($vat->id)) {
244 throw new RestException(404,
'VAT payment not found');
247 foreach ($request_data as $field => $value) {
248 if ($field ==
'id') {
251 if ($field ===
'caller') {
253 $vat->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
256 if ($field ==
'array_options' && is_array($value)) {
257 foreach ($value as $index => $val) {
266 if ($vat->update(DolibarrApiAccess::$user) > 0) {
267 return $this->
get(
$id);
269 throw new RestException(500,
'Error when updating VAT payment: '.$vat->errorsToString());
285 public function delete(
$id)
287 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'supprimer')) {
288 throw new RestException(403);
291 $vat =
new Tva($this->db);
292 $result = $vat->fetch(
$id);
295 if ($result <= 0 || empty($vat->id)) {
296 throw new RestException(404,
'VAT payment not found');
300 if (!empty($vat->getSommePaiement())) {
301 throw new RestException(403,
'VAT declaration not erasable, it has payments');
304 if ($vat->delete(DolibarrApiAccess::$user) < 0) {
305 throw new RestException(500,
'Error when deleting VAT payment: '.$vat->errorsToString());
311 'message' =>
'VAT payment deleted'
332 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
334 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
335 throw new RestException(403);
340 $sql =
"SELECT t.rowid FROM ".MAIN_DB_PREFIX.
"payment_vat AS t, ".MAIN_DB_PREFIX.
"tva AS s";
341 $sql .=
" WHERE t.fk_tva = s.rowid AND s.entity IN (".getEntity(
'tva').
")";
342 $sql .= $this->db->order($sortfield, $sortorder);
347 $offset = $limit * $page;
348 $sql .= $this->db->plimit($limit + 1, $offset);
351 $result = $this->db->query($sql);
353 throw new RestException(503,
'Error when retrieving list of VAT payments: '.$this->db->lasterror());
356 $num = $this->db->num_rows($result);
357 $min = min($num, ($limit <= 0 ? $num : $limit));
358 for ($i = 0; $i < $min; $i++) {
359 $obj = $this->db->fetch_object($result);
361 if ($payment->fetch($obj->rowid) > 0) {
384 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
385 throw new RestException(403);
390 $sql =
"SELECT t.rowid FROM ".MAIN_DB_PREFIX.
"payment_vat AS t";
391 $sql .=
" WHERE t.fk_tva = ".((int)
$id);
392 $sql .= $this->db->order(
't.rowid',
'ASC');
394 $result = $this->db->query($sql);
396 throw new RestException(503,
'Error when retrieving list of VAT payments: '.$this->db->lasterror());
399 $num = $this->db->num_rows($result);
400 for ($i = 0; $i < $num; $i++) {
401 $obj = $this->db->fetch_object($result);
403 if ($payment->fetch($obj->rowid) > 0) {
424 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
425 throw new RestException(403);
429 $result = $payment->fetch($pid);
430 if ($result <= 0 || empty($payment->id)) {
431 throw new RestException(404,
'VAT payment not found');
454 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'creer')) {
455 throw new RestException(403);
460 if (
isModEnabled(
"bank") && !((
int) ($request_data[
'accountid'] ?? 0) > 0)) {
461 throw new RestException(400,
'accountid field missing');
464 $datepaye = is_numeric($request_data[
'datepaye']) ? (int) $request_data[
'datepaye'] :
dol_stringtotime((
string) $request_data[
'datepaye'], 1, 1);
465 if (empty($datepaye)) {
466 throw new RestException(400,
'datepaye must be a timestamp or a date YYYY-MM-DD');
469 $vat =
new Tva($this->db);
470 if ($vat->fetch(
$id) <= 0 || empty($vat->id)) {
471 throw new RestException(404,
'VAT declaration not found');
475 $payment->chid = $vat->id;
476 $payment->datepaye = $datepaye;
477 $payment->amounts = array($vat->id => (
float) $request_data[
'amount']);
478 $payment->paiementtype = $request_data[
'paiementtype'];
479 if (isset($request_data[
'num_payment'])) {
480 $payment->num_payment = $request_data[
'num_payment'];
482 if (isset($request_data[
'note'])) {
483 $payment->note = $request_data[
'note'];
486 $closepaidvat = isset($request_data[
'closepaidvat']) ? (int) $request_data[
'closepaidvat'] : 1;
491 if ($payment->create(DolibarrApiAccess::$user, $closepaidvat) < 0) {
492 $this->db->rollback();
493 throw new RestException(400,
'Payment error : '.$payment->errorsToString());
497 if ($payment->addPaymentToBank(DolibarrApiAccess::$user,
'payment_vat',
'(VATPayment)', (
int) $request_data[
'accountid'],
'',
'') <= 0) {
498 $this->db->rollback();
499 throw new RestException(400,
'Add payment to bank error : '.$payment->errorsToString());
524 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'supprimer')) {
525 throw new RestException(403);
529 $result = $payment->fetch($pid);
530 if ($result <= 0 || empty($payment->id)) {
531 throw new RestException(404,
'VAT payment not found');
534 if ($payment->delete(DolibarrApiAccess::$user) < 0) {
535 throw new RestException(500,
'Error when deleting VAT payment: '.$payment->errorsToString());
541 'message' =>
'VAT payment deleted'
556 if ($data ===
null) {
560 foreach (VatPayments::$FIELDS as $field) {
561 if (!isset($data[$field])) {
562 throw new RestException(400,
"$field field missing");
564 $vat[$field] = $data[$field];
579 if ($data ===
null) {
583 foreach (VatPayments::$PAYMENT_FIELDS as $field) {
584 if (!isset($data[$field])) {
585 throw new RestException(400,
"$field field missing");
587 $payment[$field] = $data[$field];
606 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.
dol_stringtotime($string, $gm=1, $processnotimeasnoon=0)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
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.