19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/compta/sociales/class/chargesociales.class.php';
22require_once DOL_DOCUMENT_ROOT.
'/compta/sociales/class/paymentsocialcontribution.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(
51 public static $PAYMENT_FIELDS = array(
75 public function get(
$id)
77 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
78 throw new RestException(403);
82 $result = $contrib->fetch(
$id);
83 if ($result <= 0 || empty($contrib->id)) {
84 throw new RestException(404,
'Social contribution 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.
"chargesociales AS t";
118 $sql .=
" WHERE t.entity IN (".getEntity(
'chargesociales').
")";
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 social contributions: '.$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);
154 if ($contrib->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);
202 foreach ($request_data as $field => $value) {
203 if ($field ===
'caller') {
205 $contrib->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
209 if ($field ==
'fk_type') {
210 $contrib->type = (int) $value;
217 if ($contrib->create(DolibarrApiAccess::$user) < 0) {
218 throw new RestException(500,
'Error when creating social contribution: '.$contrib->errorsToString());
237 public function put(
$id, $request_data =
null)
239 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'creer')) {
240 throw new RestException(403);
244 $result = $contrib->fetch(
$id);
245 if ($result <= 0 || empty($contrib->id)) {
246 throw new RestException(404,
'Social contribution not found');
249 foreach ($request_data as $field => $value) {
250 if ($field ==
'id') {
253 if ($field ===
'caller') {
255 $contrib->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
258 if ($field ==
'array_options' && is_array($value)) {
259 foreach ($value as $index => $val) {
265 if ($field ==
'fk_type') {
266 $contrib->type = (int) $value;
273 if ($contrib->update(DolibarrApiAccess::$user) > 0) {
274 return $this->
get(
$id);
276 throw new RestException(500,
'Error when updating social contribution: '.$contrib->errorsToString());
292 public function delete(
$id)
294 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'supprimer')) {
295 throw new RestException(403);
299 $result = $contrib->fetch(
$id);
300 if ($result <= 0 || empty($contrib->id)) {
301 throw new RestException(404,
'Social contribution not found');
305 if (!empty($contrib->getSommePaiement())) {
306 throw new RestException(403,
'Social contribution not erasable, it has payments');
309 if ($contrib->delete(DolibarrApiAccess::$user) < 0) {
310 throw new RestException(500,
'Error when deleting social contribution: '.$contrib->errorsToString());
316 'message' =>
'Social contribution deleted'
337 public function getAllPayments($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0)
339 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
340 throw new RestException(403);
345 $sql =
"SELECT t.rowid FROM ".MAIN_DB_PREFIX.
"paiementcharge AS t, ".MAIN_DB_PREFIX.
"chargesociales AS s";
346 $sql .=
" WHERE t.fk_charge = s.rowid AND s.entity IN (".getEntity(
'chargesociales').
")";
347 $sql .= $this->db->order($sortfield, $sortorder);
352 $offset = $limit * $page;
353 $sql .= $this->db->plimit($limit + 1, $offset);
356 $result = $this->db->query($sql);
358 throw new RestException(503,
'Error when retrieving list of payments: '.$this->db->lasterror());
361 $num = $this->db->num_rows($result);
362 $min = min($num, ($limit <= 0 ? $num : $limit));
363 for ($i = 0; $i < $min; $i++) {
364 $obj = $this->db->fetch_object($result);
366 if ($payment->fetch($obj->rowid) > 0) {
389 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
390 throw new RestException(403);
395 $sql =
"SELECT t.rowid FROM ".MAIN_DB_PREFIX.
"paiementcharge AS t";
396 $sql .=
" WHERE t.fk_charge = ".((int)
$id);
397 $sql .= $this->db->order(
't.rowid',
'ASC');
399 $result = $this->db->query($sql);
401 throw new RestException(503,
'Error when retrieving list of payments: '.$this->db->lasterror());
404 $num = $this->db->num_rows($result);
405 for ($i = 0; $i < $num; $i++) {
406 $obj = $this->db->fetch_object($result);
408 if ($payment->fetch($obj->rowid) > 0) {
429 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
430 throw new RestException(403);
434 $result = $payment->fetch($pid);
435 if ($result <= 0 || empty($payment->id)) {
436 throw new RestException(404,
'Payment not found');
459 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'creer')) {
460 throw new RestException(403);
465 if (
isModEnabled(
"bank") && !((
int) ($request_data[
'accountid'] ?? 0) > 0)) {
466 throw new RestException(400,
'accountid field missing');
469 $datepaye = is_numeric($request_data[
'datepaye']) ? (int) $request_data[
'datepaye'] :
dol_stringtotime((
string) $request_data[
'datepaye'], 1, 1);
470 if (empty($datepaye)) {
471 throw new RestException(400,
'datepaye must be a timestamp or a date YYYY-MM-DD');
475 if ($contrib->fetch(
$id) <= 0 || empty($contrib->id)) {
476 throw new RestException(404,
'Social contribution not found');
480 $payment->chid = $contrib->id;
481 $payment->datepaye = $datepaye;
482 $payment->amounts = array($contrib->id => (
float) $request_data[
'amount']);
483 $payment->paiementtype = $request_data[
'paiementtype'];
484 if (isset($request_data[
'num_payment'])) {
485 $payment->num_payment = $request_data[
'num_payment'];
487 if (isset($request_data[
'note'])) {
488 $payment->note = $request_data[
'note'];
491 $closepaidcontrib = isset($request_data[
'closepaidcontrib']) ? (int) $request_data[
'closepaidcontrib'] : 1;
496 if ($payment->create(DolibarrApiAccess::$user, $closepaidcontrib) < 0) {
497 $this->db->rollback();
498 throw new RestException(400,
'Payment error : '.$payment->errorsToString());
502 if ($payment->addPaymentToBank(DolibarrApiAccess::$user,
'payment_sc',
'(SocialContributionPayment)', (
int) $request_data[
'accountid'],
'',
'') <= 0) {
503 $this->db->rollback();
504 throw new RestException(400,
'Add payment to bank error : '.$payment->errorsToString());
529 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'supprimer')) {
530 throw new RestException(403);
534 $result = $payment->fetch($pid);
535 if ($result <= 0 || empty($payment->id)) {
536 throw new RestException(404,
'Payment not found');
539 if ($payment->delete(DolibarrApiAccess::$user) < 0) {
540 throw new RestException(500,
'Error when deleting payment: '.$payment->errorsToString());
546 'message' =>
'Payment deleted'
561 if ($data ===
null) {
565 foreach (SocialContributions::$FIELDS as $field) {
566 if (!isset($data[$field])) {
567 throw new RestException(400,
"$field field missing");
569 $contrib[$field] = $data[$field];
584 if ($data ===
null) {
588 foreach (SocialContributions::$PAYMENT_FIELDS as $field) {
589 if (!isset($data[$field])) {
590 throw new RestException(400,
"$field field missing");
592 $payment[$field] = $data[$field];
611 unset(
$object->labelStatusShort);
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Class for managing the social charges.
_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.
_validate($data)
Validate fields before creating a social contribution.
put($id, $request_data=null)
Update a social contribution.
__construct()
Constructor.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List social contributions.
post($request_data=null)
Create a social contribution.
_cleanObjectDatas($object)
Clean sensitive object data.
getPayment($pid)
Get a given social contribution payment.
_validatePayment($data)
Validate fields before creating a payment.
addPayment($id, $request_data=null)
Add a payment to a social contribution.
getAllPayments($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0)
List all social contribution payments.
deletePayment($pid)
Delete a social contribution payment.
getPayments($id)
Get the list of payments of a given social contribution.
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.