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),
364 public function post($request_data =
null)
366 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
367 throw new RestException(403);
370 $result = $this->
_validate($request_data);
373 foreach ($request_data as $field => $value) {
374 if ($field ===
'caller') {
376 $member->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
382 if ($member->create(DolibarrApiAccess::$user) < 0) {
383 throw new RestException(500,
'Error creating member', array_merge(array($member->error), $member->errors));
401 public function put(
$id, $request_data =
null)
403 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
404 throw new RestException(403);
408 $result = $member->fetch(
$id);
410 throw new RestException(404,
'member not found');
414 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
417 foreach ($request_data as $field => $value) {
418 if ($field ==
'id') {
421 if ($field ===
'caller') {
423 $member->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
426 if ($field ==
'array_options' && is_array($value)) {
427 foreach ($value as $index => $val) {
428 $member->array_options[$index] = $val;
434 if ($field ==
'statut') {
436 $result = $member->resiliate(DolibarrApiAccess::$user);
438 throw new RestException(500,
'Error when resiliating member: '.$member->error);
440 } elseif ($value ==
'1') {
441 $result = $member->validate(DolibarrApiAccess::$user);
443 throw new RestException(500,
'Error when validating member: '.$member->error);
445 } elseif ($value ==
'-2') {
446 $result = $member->exclude(DolibarrApiAccess::$user);
448 throw new RestException(500,
'Error when excluding member: '.$member->error);
458 if ($member->update(DolibarrApiAccess::$user) >= 0) {
459 return $this->
get(
$id);
461 throw new RestException(500,
'Error when updating member: '.$member->error);
477 public function delete(
$id)
479 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'supprimer')) {
480 throw new RestException(403);
483 $result = $member->fetch(
$id);
485 throw new RestException(404,
'member not found');
489 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
493 $res = $member->delete(DolibarrApiAccess::$user);
495 throw new RestException(500,
"Can't delete, error occurs");
501 'message' =>
'Member deleted'
519 $mandatoryfields = array(
523 foreach ($mandatoryfields as $field) {
524 if (!isset($data[$field])) {
525 throw new RestException(400,
"$field field missing");
527 $member[$field] = $data[$field];
548 unset(
$object->label_incoterms);
549 unset(
$object->location_incoterms);
550 unset(
$object->fk_delivery_address);
551 unset(
$object->shipping_method_id);
556 unset(
$object->total_localtax1);
557 unset(
$object->total_localtax2);
561 unset(
$object->linkedObjectsIds);
576 unset(
$object->barcode_type_code);
577 unset(
$object->barcode_type_label);
578 unset(
$object->barcode_type_coder);
579 unset(
$object->mode_reglement_id);
580 unset(
$object->cond_reglement_id);
581 unset(
$object->cond_reglement);
582 unset(
$object->fk_delivery_address);
583 unset(
$object->shipping_method_id);
589 unset(
$object->label_incoterms);
590 unset(
$object->location_incoterms);
597 unset(
$object->total_localtax1);
598 unset(
$object->total_localtax2);
622 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
623 throw new RestException(403);
627 $result = $member->fetch(
$id);
629 throw new RestException(404,
'member not found');
633 foreach ($member->subscriptions as $subscription) {
656 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
657 throw new RestException(403);
661 $result = $member->fetch(
$id);
663 throw new RestException(404,
'member not found');
666 return $member->subscription($start_date, $amount, 0,
'', $label,
'',
'',
'', $end_date);
686 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
688 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
689 throw new RestException(403);
694 $result = $categories->getListForItem(
$id,
'member', $sortfield, $sortorder, $limit, $page);
697 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
721 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
722 throw new RestException(403);
726 $result = $membertype->fetch(
$id);
728 throw new RestException(404,
'member type not found');
732 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
760 public function indexType($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
764 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
765 throw new RestException(403);
768 $sql =
"SELECT t.rowid";
769 $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)";
770 $sql .=
' WHERE t.entity IN ('.getEntity(
'member_type').
')';
777 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
782 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
784 $sql .= $this->db->order($sortfield, $sortorder);
789 $offset = $limit * $page;
791 $sql .= $this->db->plimit($limit + 1, $offset);
794 $result = $this->db->query($sql);
797 $num = $this->db->num_rows($result);
798 $min = min($num, ($limit <= 0 ? $num : $limit));
800 $obj = $this->db->fetch_object($result);
802 if ($membertype->fetch($obj->rowid)) {
808 throw new RestException(503,
'Error when retrieve member type list : '.$this->db->lasterror());
812 if ($pagination_data) {
813 $totalsResult = $this->db->query($sqlTotals);
814 $total = $this->db->fetch_object($totalsResult)->total;
819 $obj_ret[
'data'] = $tmp;
820 $obj_ret[
'pagination'] = [
821 'total' => (int) $total,
823 'page_count' => ceil((
int) $total / $limit),
846 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
847 throw new RestException(403);
853 foreach ($request_data as $field => $value) {
854 if ($field ===
'caller') {
856 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
860 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
862 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
863 throw new RestException(500,
'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
865 return $membertype->id;
885 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
886 throw new RestException(403);
890 $result = $membertype->fetch(
$id);
892 throw new RestException(404,
'member type not found');
896 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
899 foreach ($request_data as $field => $value) {
900 if ($field ==
'id') {
903 if ($field ===
'caller') {
905 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
908 if ($field ==
'array_options' && is_array($value)) {
909 foreach ($value as $index => $val) {
910 $membertype->array_options[$index] = $val;
916 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
921 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
922 return $this->
get(
$id);
924 throw new RestException(500,
'Error when updating member type: '.$membertype->error);
944 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
945 throw new RestException(403);
948 $result = $membertype->fetch(
$id);
950 throw new RestException(404,
'member type not found');
954 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
957 $res = $membertype->delete(DolibarrApiAccess::$user);
959 throw new RestException(500,
"Can't delete, error occurs");
965 'message' =>
'Member type deleted'
980 $membertype = array();
982 $mandatoryfields = array(
'label');
984 foreach ($mandatoryfields as $field) {
985 if (!isset($data[$field])) {
986 throw new RestException(400,
"$field field missing");
988 $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.