22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/adherents/class/subscription.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
28require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent_type.class.php';
42 public static $FIELDS = array(
67 public function get(
$id)
69 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
70 throw new RestException(403);
75 $result = $member->initAsSpecimen();
77 $result = $member->fetch(
$id);
80 throw new RestException(404,
'member not found');
84 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
106 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
107 throw new RestException(403);
111 $result = $member->fetch(0,
'', $thirdparty);
113 throw new RestException(404,
'member not found');
117 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
137 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
138 throw new RestException(403);
141 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
142 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
143 $sql .=
" AND entity IN (".getEntity(
'adherent').
")";
145 $result = $this->db->query($sql);
147 if ($result && $this->db->num_rows($result) == 1) {
148 $obj = $this->db->fetch_object($result);
149 $thirdparty =
new Societe($this->db);
150 $result = $thirdparty->fetch($obj->fk_soc);
153 throw new RestException(404,
'thirdparty not found');
157 $result = $member->fetch(0,
'', $thirdparty->id);
159 throw new RestException(404,
'member not found');
162 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
166 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
188 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
189 throw new RestException(403);
192 $thirdparty =
new Societe($this->db);
193 $result = $thirdparty->fetch(0,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
195 throw new RestException(404,
'thirdparty not found');
199 $result = $member->fetch(0,
'', $thirdparty->id);
201 throw new RestException(404,
'member not found');
205 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
227 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
228 throw new RestException(403);
231 $thirdparty =
new Societe($this->db);
232 $result = $thirdparty->fetch(0,
'',
'', $barcode);
234 throw new RestException(404,
'thirdparty not found');
238 $result = $member->fetch(0,
'', $thirdparty->id);
240 throw new RestException(404,
'member not found');
244 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
274 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $typeid =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
278 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
279 throw new RestException(403);
282 $sql =
"SELECT t.rowid";
283 $sql .=
" FROM ".MAIN_DB_PREFIX.
"adherent AS t LEFT JOIN ".MAIN_DB_PREFIX.
"adherent_extrafields AS ef ON (ef.fk_object = t.rowid)";
285 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_member as c";
287 $sql .=
' WHERE t.entity IN ('.getEntity(
'adherent').
')';
288 if (!empty($typeid)) {
289 $sql .=
' AND t.fk_adherent_type='.((int) $typeid);
293 $sql .=
" AND c.fk_categorie = ".((int) $category);
294 $sql .=
" AND c.fk_member = t.rowid";
301 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
306 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
308 $sql .= $this->db->order($sortfield, $sortorder);
313 $offset = $limit * $page;
315 $sql .= $this->db->plimit($limit + 1, $offset);
318 $result = $this->db->query($sql);
321 $num = $this->db->num_rows($result);
322 $min = min($num, ($limit <= 0 ? $num : $limit));
324 $obj = $this->db->fetch_object($result);
326 if ($member->fetch($obj->rowid)) {
332 throw new RestException(503,
'Error when retrieve member list : '.$this->db->lasterror());
336 if ($pagination_data) {
337 $totalsResult = $this->db->query($sqlTotals);
338 $total = $this->db->fetch_object($totalsResult)->total;
343 $obj_ret[
'data'] = $tmp;
344 $obj_ret[
'pagination'] = [
345 'total' => (int) $total,
347 'page_count' => ceil((
int) $total / $limit),
366 public function post($request_data =
null)
368 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
369 throw new RestException(403);
372 $result = $this->
_validate($request_data);
375 foreach ($request_data as $field => $value) {
376 if ($field ===
'caller') {
378 $member->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
384 if ($member->create(DolibarrApiAccess::$user) < 0) {
385 throw new RestException(500,
'Error creating member', array_merge(array($member->error), $member->errors));
403 public function put(
$id, $request_data =
null)
405 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
406 throw new RestException(403);
410 $result = $member->fetch(
$id);
412 throw new RestException(404,
'member not found');
416 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
419 foreach ($request_data as $field => $value) {
420 if ($field ==
'id') {
423 if ($field ===
'caller') {
425 $member->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
428 if ($field ==
'array_options' && is_array($value)) {
429 foreach ($value as $index => $val) {
430 $member->array_options[$index] = $this->
_checkValForAPI($field, $val, $member);
436 if ($field ==
'statut') {
438 $result = $member->resiliate(DolibarrApiAccess::$user);
440 throw new RestException(500,
'Error when resiliating member: '.$member->error);
442 } elseif ($value ==
'1') {
443 $result = $member->validate(DolibarrApiAccess::$user);
445 throw new RestException(500,
'Error when validating member: '.$member->error);
447 } elseif ($value ==
'-2') {
448 $result = $member->exclude(DolibarrApiAccess::$user);
450 throw new RestException(500,
'Error when excluding member: '.$member->error);
460 if ($member->update(DolibarrApiAccess::$user) >= 0) {
461 return $this->
get(
$id);
463 throw new RestException(500,
'Error when updating member: '.$member->error);
479 public function delete(
$id)
481 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'supprimer')) {
482 throw new RestException(403);
485 $result = $member->fetch(
$id);
487 throw new RestException(404,
'member not found');
491 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 $res = $member->delete(DolibarrApiAccess::$user);
497 throw new RestException(500,
"Can't delete, error occurs");
503 'message' =>
'Member deleted'
521 $mandatoryfields = array(
525 foreach ($mandatoryfields as $field) {
526 if (!isset($data[$field])) {
527 throw new RestException(400,
"$field field missing");
529 $member[$field] = $data[$field];
550 unset(
$object->label_incoterms);
551 unset(
$object->location_incoterms);
552 unset(
$object->fk_delivery_address);
553 unset(
$object->shipping_method_id);
558 unset(
$object->total_localtax1);
559 unset(
$object->total_localtax2);
563 unset(
$object->linkedObjectsIds);
578 unset(
$object->barcode_type_code);
579 unset(
$object->barcode_type_label);
580 unset(
$object->barcode_type_coder);
581 unset(
$object->mode_reglement_id);
582 unset(
$object->cond_reglement_id);
583 unset(
$object->cond_reglement);
584 unset(
$object->fk_delivery_address);
585 unset(
$object->shipping_method_id);
591 unset(
$object->label_incoterms);
592 unset(
$object->location_incoterms);
599 unset(
$object->total_localtax1);
600 unset(
$object->total_localtax2);
624 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
625 throw new RestException(403);
629 $result = $member->fetch(
$id);
631 throw new RestException(404,
'member not found');
635 foreach ($member->subscriptions as $subscription) {
658 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
659 throw new RestException(403);
663 $result = $member->fetch(
$id);
665 throw new RestException(404,
'member not found');
668 return $member->subscription($start_date, $amount, 0,
'', $label,
'',
'',
'', $end_date);
688 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
690 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
691 throw new RestException(403);
695 $result = $member->fetch(
$id);
697 throw new RestException(404,
'Member not found');
702 $result = $categories->getListForItem(
$id,
'member', $sortfield, $sortorder, $limit, $page);
705 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
729 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
730 throw new RestException(403);
734 $result = $membertype->fetch(
$id);
736 throw new RestException(404,
'member type not found');
740 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
768 public function indexType($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
772 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
773 throw new RestException(403);
776 $sql =
"SELECT t.rowid";
777 $sql .=
" FROM ".MAIN_DB_PREFIX.
"adherent_type AS t LEFT JOIN ".MAIN_DB_PREFIX.
"adherent_type_extrafields AS ef ON (ef.fk_object = t.rowid)";
778 $sql .=
' WHERE t.entity IN ('.getEntity(
'member_type').
')';
785 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
790 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
792 $sql .= $this->db->order($sortfield, $sortorder);
797 $offset = $limit * $page;
799 $sql .= $this->db->plimit($limit + 1, $offset);
802 $result = $this->db->query($sql);
805 $num = $this->db->num_rows($result);
806 $min = min($num, ($limit <= 0 ? $num : $limit));
808 $obj = $this->db->fetch_object($result);
810 if ($membertype->fetch($obj->rowid)) {
816 throw new RestException(503,
'Error when retrieve member type list : '.$this->db->lasterror());
820 if ($pagination_data) {
821 $totalsResult = $this->db->query($sqlTotals);
822 $total = $this->db->fetch_object($totalsResult)->total;
827 $obj_ret[
'data'] = $tmp;
828 $obj_ret[
'pagination'] = [
829 'total' => (int) $total,
831 'page_count' => ceil((
int) $total / $limit),
854 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
855 throw new RestException(403);
861 foreach ($request_data as $field => $value) {
862 if ($field ===
'caller') {
864 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
868 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
870 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
871 throw new RestException(500,
'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
873 return $membertype->id;
893 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
894 throw new RestException(403);
898 $result = $membertype->fetch(
$id);
900 throw new RestException(404,
'member type not found');
904 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
907 foreach ($request_data as $field => $value) {
908 if ($field ==
'id') {
911 if ($field ===
'caller') {
913 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
916 if ($field ==
'array_options' && is_array($value)) {
917 foreach ($value as $index => $val) {
918 $membertype->array_options[$index] = $val;
924 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
929 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
930 return $this->
get(
$id);
932 throw new RestException(500,
'Error when updating member type: '.$membertype->error);
952 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
953 throw new RestException(403);
956 $result = $membertype->fetch(
$id);
958 throw new RestException(404,
'member type not found');
962 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
965 $res = $membertype->delete(DolibarrApiAccess::$user);
967 throw new RestException(500,
"Can't delete, error occurs");
973 'message' =>
'Member type deleted'
988 $membertype = array();
990 $mandatoryfields = array(
'label');
992 foreach ($mandatoryfields as $field) {
993 if (!isset($data[$field])) {
994 throw new RestException(400,
"$field field missing");
996 $membertype[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to manage members of a foundation.
Class to manage members type.
Class to manage categories.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
put($id, $request_data=null)
Update member.
getType($id)
Get properties of a member type object.
createSubscription($id, $start_date, $end_date, $amount, $label='')
Add a subscription for a member.
indexType($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List members types.
_validate($data)
Validate fields before creating an object.
getByThirdparty($thirdparty)
Get properties of a member object by linked thirdparty.
getByThirdpartyBarcode($barcode)
Get properties of a member object by linked thirdparty barcode.
deleteType($id)
Delete member type.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $typeid='', $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List members.
__construct()
Constructor.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get categories for a member.
getByThirdpartyEmail($email)
Get properties of a member object by linked thirdparty email.
_cleanObjectDatas($object)
Clean sensible object datas.
_validateType($data)
Validate fields before creating an object.
putType($id, $request_data=null)
Update member type.
getSubscriptions($id)
List subscriptions of a member.
post($request_data=null)
Create member object.
getByThirdpartyAccounts($site, $key_account)
Get properties of a member object by linked thirdparty account.
postType($request_data=null)
Create member type object.
Class to manage third parties objects (customers, suppliers, prospects...)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.