21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/adherents/class/subscription.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
39 public static $FIELDS = array(
63 public function get($id)
65 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
66 throw new RestException(401);
71 $result = $member->initAsSpecimen();
73 $result = $member->fetch($id);
76 throw new RestException(404,
'member not found');
80 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
102 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
103 throw new RestException(401);
107 $result = $member->fetch(
'',
'', $thirdparty);
109 throw new RestException(404,
'member not found');
113 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
135 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
136 throw new RestException(401);
139 $thirdparty =
new Societe($this->db);
140 $result = $thirdparty->fetch(
'',
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
142 throw new RestException(404,
'thirdparty not found');
146 $result = $member->fetch(
'',
'', $thirdparty->id);
148 throw new RestException(404,
'member not found');
152 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
174 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
175 throw new RestException(401);
178 $thirdparty =
new Societe($this->db);
179 $result = $thirdparty->fetch(
'',
'',
'', $barcode);
181 throw new RestException(404,
'thirdparty not found');
185 $result = $member->fetch(
'',
'', $thirdparty->id);
187 throw new RestException(404,
'member not found');
191 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
215 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $typeid =
'', $category = 0, $sqlfilters =
'', $properties =
'')
221 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
222 throw new RestException(401);
225 $sql =
"SELECT t.rowid";
226 $sql .=
" FROM ".MAIN_DB_PREFIX.
"adherent AS t LEFT JOIN ".MAIN_DB_PREFIX.
"adherent_extrafields AS ef ON (ef.fk_object = t.rowid)";
228 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_member as c";
230 $sql .=
' WHERE t.entity IN ('.getEntity(
'adherent').
')';
231 if (!empty($typeid)) {
232 $sql .=
' AND t.fk_adherent_type='.((int) $typeid);
236 $sql .=
" AND c.fk_categorie = ".((int) $category);
237 $sql .=
" AND c.fk_member = t.rowid";
244 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
248 $sql .= $this->db->order($sortfield, $sortorder);
253 $offset = $limit * $page;
255 $sql .= $this->db->plimit($limit + 1, $offset);
258 $result = $this->db->query($sql);
261 $num = $this->db->num_rows($result);
262 $min = min($num, ($limit <= 0 ? $num : $limit));
264 $obj = $this->db->fetch_object($result);
266 if ($member->fetch($obj->rowid)) {
272 throw new RestException(503,
'Error when retrieve member list : '.$this->db->lasterror());
284 public function post($request_data =
null)
286 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
287 throw new RestException(401);
290 $result = $this->
_validate($request_data);
293 foreach ($request_data as $field => $value) {
294 if ($field ===
'caller') {
296 $member->context[
'caller'] = $request_data[
'caller'];
300 $member->$field = $value;
302 if ($member->create(DolibarrApiAccess::$user) < 0) {
303 throw new RestException(500,
'Error creating member', array_merge(array($member->error), $member->errors));
315 public function put($id, $request_data =
null)
317 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
318 throw new RestException(401);
322 $result = $member->fetch($id);
324 throw new RestException(404,
'member not found');
328 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
331 foreach ($request_data as $field => $value) {
332 if ($field ==
'id') {
335 if ($field ===
'caller') {
337 $member->context[
'caller'] = $request_data[
'caller'];
343 if ($field ==
'statut') {
345 $result = $member->resiliate(DolibarrApiAccess::$user);
347 throw new RestException(500,
'Error when resiliating member: '.$member->error);
349 } elseif ($value ==
'1') {
350 $result = $member->validate(DolibarrApiAccess::$user);
352 throw new RestException(500,
'Error when validating member: '.$member->error);
354 } elseif ($value ==
'-2') {
355 $result = $member->exclude(DolibarrApiAccess::$user);
357 throw new RestException(500,
'Error when excluding member: '.$member->error);
361 $member->$field = $value;
367 if ($member->update(DolibarrApiAccess::$user) >= 0) {
368 return $this->
get($id);
370 throw new RestException(500,
'Error when updating member: '.$member->error);
380 public function delete($id)
382 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'supprimer')) {
383 throw new RestException(401);
386 $result = $member->fetch($id);
388 throw new RestException(404,
'member not found');
392 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
396 $res = $member->delete($member->id, DolibarrApiAccess::$user);
398 throw new RestException(500,
"Can't delete, error occurs");
399 } elseif ($res == 0) {
400 throw new RestException(409,
"Can't delete, that product is probably used");
406 'message' =>
'Member deleted'
422 foreach (Members::$FIELDS as $field) {
423 if (!isset($data[$field])) {
424 throw new RestException(400,
"$field field missing");
426 $member[$field] = $data[$field];
441 $object = parent::_cleanObjectDatas($object);
444 unset($object->subscriptions);
445 unset($object->fk_incoterms);
446 unset($object->label_incoterms);
447 unset($object->location_incoterms);
448 unset($object->fk_delivery_address);
449 unset($object->shipping_method_id);
451 unset($object->total_ht);
452 unset($object->total_ttc);
453 unset($object->total_tva);
454 unset($object->total_localtax1);
455 unset($object->total_localtax2);
476 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
477 throw new RestException(401);
481 $result = $member->fetch($id);
483 throw new RestException(404,
'member not found');
487 foreach ($member->subscriptions as $subscription) {
507 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
508 throw new RestException(401);
512 $result = $member->fetch($id);
514 throw new RestException(404,
'member not found');
517 return $member->subscription($start_date, $amount, 0,
'', $label,
'',
'',
'', $end_date);
533 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
535 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
536 throw new RestException(401);
541 $result = $categories->getListForItem($id,
'member', $sortfield, $sortorder, $limit, $page);
544 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
Class to manage members of a foundation.
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.
put($id, $request_data=null)
Update member.
createSubscription($id, $start_date, $end_date, $amount, $label='')
Add a subscription for a member.
_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.
__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.
getSubscriptions($id)
List subscriptions of a member.
post($request_data=null)
Create member object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $typeid='', $category=0, $sqlfilters='', $properties='')
List members.
Class to manage third parties objects (customers, suppliers, prospects...)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria