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'
519 if ($data ===
null) {
524 $mandatoryfields = array(
528 foreach ($mandatoryfields as $field) {
529 if (!isset($data[$field])) {
530 throw new RestException(400,
"$field field missing");
532 $member[$field] = $data[$field];
553 unset(
$object->label_incoterms);
554 unset(
$object->location_incoterms);
555 unset(
$object->fk_delivery_address);
556 unset(
$object->shipping_method_id);
561 unset(
$object->total_localtax1);
562 unset(
$object->total_localtax2);
566 unset(
$object->linkedObjectsIds);
581 unset(
$object->barcode_type_code);
582 unset(
$object->barcode_type_label);
583 unset(
$object->barcode_type_coder);
584 unset(
$object->mode_reglement_id);
585 unset(
$object->cond_reglement_id);
586 unset(
$object->cond_reglement);
587 unset(
$object->fk_delivery_address);
588 unset(
$object->shipping_method_id);
594 unset(
$object->label_incoterms);
595 unset(
$object->location_incoterms);
602 unset(
$object->total_localtax1);
603 unset(
$object->total_localtax2);
627 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
628 throw new RestException(403);
632 $result = $member->fetch(
$id);
634 throw new RestException(404,
'member not found');
638 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
642 foreach ($member->subscriptions as $subscription) {
667 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
668 throw new RestException(403);
670 if (!is_numeric($start_date) || !is_numeric($end_date) || !is_numeric($amount)) {
671 throw new RestException(422,
'Malformed data: subscription start or end date, or subscription amount, is not numeric');
673 if ($start_date > $end_date) {
674 throw new RestException(422,
'Malformed data: subscription start is not larger than end date');
678 $result = $member->fetch(
$id);
680 throw new RestException(404,
'member not found');
684 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
687 $result = $member->subscription((
int) $start_date, (
float) $amount, 0,
'', $label,
'',
'',
'', (
int) $end_date);
689 throw new RestException(500, $member->error);
712 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
714 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
715 throw new RestException(403);
719 $result = $member->fetch(
$id);
721 throw new RestException(404,
'Member not found');
725 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
730 $result = $categories->getListForItem(
$id,
'member', $sortfield, $sortorder, $limit, $page);
733 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
757 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
758 throw new RestException(403);
762 $result = $membertype->fetch(
$id);
764 throw new RestException(404,
'member type not found');
768 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
796 public function indexType($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
800 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
801 throw new RestException(403);
804 $sql =
"SELECT t.rowid";
805 $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)";
806 $sql .=
' WHERE t.entity IN ('.getEntity(
'member_type').
')';
813 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
818 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
820 $sql .= $this->db->order($sortfield, $sortorder);
825 $offset = $limit * $page;
827 $sql .= $this->db->plimit($limit + 1, $offset);
830 $result = $this->db->query($sql);
833 $num = $this->db->num_rows($result);
834 $min = min($num, ($limit <= 0 ? $num : $limit));
836 $obj = $this->db->fetch_object($result);
838 if ($membertype->fetch($obj->rowid)) {
844 throw new RestException(503,
'Error when retrieve member type list : '.$this->db->lasterror());
848 if ($pagination_data) {
849 $totalsResult = $this->db->query($sqlTotals);
850 $total = $this->db->fetch_object($totalsResult)->total;
855 $obj_ret[
'data'] = $tmp;
856 $obj_ret[
'pagination'] = [
857 'total' => (int) $total,
859 'page_count' => ceil((
int) $total / $limit),
882 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
883 throw new RestException(403);
889 foreach ($request_data as $field => $value) {
890 if ($field ===
'caller') {
892 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
896 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
898 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
899 throw new RestException(500,
'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
901 return $membertype->id;
921 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
922 throw new RestException(403);
926 $result = $membertype->fetch(
$id);
928 throw new RestException(404,
'member type not found');
932 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
935 foreach ($request_data as $field => $value) {
936 if ($field ==
'id') {
939 if ($field ===
'caller') {
941 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
944 if ($field ==
'array_options' && is_array($value)) {
945 foreach ($value as $index => $val) {
946 $membertype->array_options[$index] = $val;
952 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
957 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
958 return $this->
get(
$id);
960 throw new RestException(500,
'Error when updating member type: '.$membertype->error);
980 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
981 throw new RestException(403);
984 $result = $membertype->fetch(
$id);
986 throw new RestException(404,
'member type not found');
990 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
993 $res = $membertype->delete(DolibarrApiAccess::$user);
995 throw new RestException(500,
"Can't delete, error occurs");
1001 'message' =>
'Member type deleted'
1016 $membertype = array();
1018 $mandatoryfields = array(
'label');
1020 foreach ($mandatoryfields as $field) {
1021 if (!isset($data[$field])) {
1022 throw new RestException(400,
"$field field missing");
1024 $membertype[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
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.