21 use Luracast\Restler\RestException;
23 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
24 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
25 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/subscription.class.php';
26 require_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);
214 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $typeid =
'', $category = 0, $sqlfilters =
'')
220 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
221 throw new RestException(401);
224 $sql =
"SELECT t.rowid";
225 $sql .=
" FROM ".MAIN_DB_PREFIX.
"adherent AS t LEFT JOIN ".MAIN_DB_PREFIX.
"adherent_extrafields AS ef ON (ef.fk_object = t.rowid)";
227 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_member as c";
229 $sql .=
' WHERE t.entity IN ('.getEntity(
'adherent').
')';
230 if (!empty($typeid)) {
231 $sql .=
' AND t.fk_adherent_type='.((int) $typeid);
235 $sql .=
" AND c.fk_categorie = ".((int) $category);
236 $sql .=
" AND c.fk_member = t.rowid";
243 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
247 $sql .= $this->db->order($sortfield, $sortorder);
252 $offset = $limit * $page;
254 $sql .= $this->db->plimit($limit + 1, $offset);
257 $result = $this->db->query(
$sql);
260 $num = $this->db->num_rows($result);
261 $min = min($num, ($limit <= 0 ? $num : $limit));
263 $obj = $this->db->fetch_object($result);
265 if ($member->fetch($obj->rowid)) {
271 throw new RestException(503,
'Error when retrieve member list : '.$this->db->lasterror());
273 if (!count($obj_ret)) {
274 throw new RestException(404,
'No member found');
286 public function post($request_data =
null)
288 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
289 throw new RestException(401);
292 $result = $this->
_validate($request_data);
295 foreach ($request_data as $field => $value) {
296 $member->$field = $value;
298 if ($member->create(DolibarrApiAccess::$user) < 0) {
299 throw new RestException(500,
'Error creating member', array_merge(array($member->error), $member->errors));
311 public function put($id, $request_data =
null)
313 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
314 throw new RestException(401);
318 $result = $member->fetch($id);
320 throw new RestException(404,
'member not found');
324 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
327 foreach ($request_data as $field => $value) {
328 if ($field ==
'id') {
333 if ($field ==
'statut') {
335 $result = $member->resiliate(DolibarrApiAccess::$user);
337 throw new RestException(500,
'Error when resiliating member: '.$member->error);
339 } elseif ($value ==
'1') {
340 $result = $member->validate(DolibarrApiAccess::$user);
342 throw new RestException(500,
'Error when validating member: '.$member->error);
344 } elseif ($value ==
'-2') {
345 $result = $member->exclude(DolibarrApiAccess::$user);
347 throw new RestException(500,
'Error when excluding member: '.$member->error);
351 $member->$field = $value;
357 if ($member->update(DolibarrApiAccess::$user) >= 0) {
358 return $this->
get($id);
360 throw new RestException(500,
'Error when updating member: '.$member->error);
370 public function delete($id)
372 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'supprimer')) {
373 throw new RestException(401);
376 $result = $member->fetch($id);
378 throw new RestException(404,
'member not found');
382 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
386 $res = $member->delete($member->id, DolibarrApiAccess::$user);
388 throw new RestException(500,
"Can't delete, error occurs");
389 } elseif ($res == 0) {
390 throw new RestException(409,
"Can't delete, that product is probably used");
396 'message' =>
'Member deleted'
412 foreach (Members::$FIELDS as $field) {
413 if (!isset($data[$field])) {
414 throw new RestException(400,
"$field field missing");
416 $member[$field] = $data[$field];
431 $object = parent::_cleanObjectDatas($object);
434 unset($object->subscriptions);
435 unset($object->fk_incoterms);
436 unset($object->label_incoterms);
437 unset($object->location_incoterms);
438 unset($object->fk_delivery_address);
439 unset($object->shipping_method_id);
441 unset($object->total_ht);
442 unset($object->total_ttc);
443 unset($object->total_tva);
444 unset($object->total_localtax1);
445 unset($object->total_localtax2);
466 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
467 throw new RestException(401);
471 $result = $member->fetch($id);
473 throw new RestException(404,
'member not found');
477 foreach ($member->subscriptions as $subscription) {
497 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
498 throw new RestException(401);
502 $result = $member->fetch($id);
504 throw new RestException(404,
'member not found');
507 return $member->subscription($start_date, $amount, 0,
'', $label,
'',
'',
'', $end_date);
523 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
525 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
526 throw new RestException(401);
531 $result = $categories->getListForItem($id,
'member', $sortfield, $sortorder, $limit, $page);
533 if (empty($result)) {
534 throw new RestException(404,
'No category found');
538 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);