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';
29require_once DOL_DOCUMENT_ROOT .
'/adherents/class/adherentstats.class.php';
43 public static $FIELDS = array(
60 $this->memberstats =
new AdherentStats($this->db, DolibarrApiAccess::$user->socid, DolibarrApiAccess::$user->
id);
74 public function get(
$id)
76 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
77 throw new RestException(403);
82 $result = $member->initAsSpecimen();
84 $result = $member->fetch(
$id);
87 throw new RestException(404,
'member not found');
91 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
113 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
114 throw new RestException(403);
118 $result = $member->fetch(0,
'', $thirdparty);
120 throw new RestException(404,
'member not found');
124 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
144 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
145 throw new RestException(403);
148 $sql =
"SELECT rowid, fk_soc as socid, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
149 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
150 $sql .=
" AND entity IN (".getEntity(
'adherent').
")";
152 $result = $this->db->query($sql);
154 if ($result && $this->db->num_rows($result) == 1) {
155 $obj = $this->db->fetch_object($result);
156 $thirdparty =
new Societe($this->db);
157 $result = $thirdparty->fetch($obj->socid);
160 throw new RestException(404,
'thirdparty not found');
164 $result = $member->fetch(0,
'', $thirdparty->id);
166 throw new RestException(404,
'member not found');
169 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
173 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
195 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
196 throw new RestException(403);
199 $thirdparty =
new Societe($this->db);
200 $result = $thirdparty->fetch(0,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
202 throw new RestException(404,
'thirdparty not found');
206 $result = $member->fetch(0,
'', $thirdparty->id);
208 throw new RestException(404,
'member not found');
212 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
234 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
235 throw new RestException(403);
238 $thirdparty =
new Societe($this->db);
239 $result = $thirdparty->fetch(0,
'',
'', $barcode);
241 throw new RestException(404,
'thirdparty not found');
245 $result = $member->fetch(0,
'', $thirdparty->id);
247 throw new RestException(404,
'member not found');
251 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
281 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $typeid =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
285 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
286 throw new RestException(403);
289 $sql =
"SELECT t.rowid";
290 $sql .=
" FROM ".MAIN_DB_PREFIX.
"adherent AS t LEFT JOIN ".MAIN_DB_PREFIX.
"adherent_extrafields AS ef ON (ef.fk_object = t.rowid)";
292 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_member as c";
294 $sql .=
' WHERE t.entity IN ('.getEntity(
'adherent').
')';
295 if (!empty($typeid)) {
296 $sql .=
' AND t.fk_adherent_type='.((int) $typeid);
300 $sql .=
" AND c.fk_categorie = ".((int) $category);
301 $sql .=
" AND c.fk_member = t.rowid";
308 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
313 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
315 $sql .= $this->db->order($sortfield, $sortorder);
320 $offset = $limit * $page;
322 $sql .= $this->db->plimit($limit + 1, $offset);
325 $result = $this->db->query($sql);
328 $num = $this->db->num_rows($result);
329 $min = min($num, ($limit <= 0 ? $num : $limit));
331 $obj = $this->db->fetch_object($result);
333 if ($member->fetch($obj->rowid)) {
339 throw new RestException(503,
'Error when retrieve member list : '.$this->db->lasterror());
343 if ($pagination_data) {
344 $totalsResult = $this->db->query($sqlTotals);
345 $total = $this->db->fetch_object($totalsResult)->total;
350 $obj_ret[
'data'] = $tmp;
351 $obj_ret[
'pagination'] = [
352 'total' => (int) $total,
354 'page_count' => ceil((
int) $total / $limit),
373 public function post($request_data =
null)
375 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
376 throw new RestException(403);
379 $result = $this->
_validate($request_data);
382 foreach ($request_data as $field => $value) {
383 if ($field ===
'caller') {
385 $member->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
391 if ($member->create(DolibarrApiAccess::$user) < 0) {
392 throw new RestException(500,
'Error creating member', array_merge(array($member->error), $member->errors));
410 public function put(
$id, $request_data =
null)
412 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
413 throw new RestException(403);
417 $result = $member->fetch(
$id);
419 throw new RestException(404,
'member not found');
423 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
426 foreach ($request_data as $field => $value) {
427 if ($field ==
'id') {
430 if ($field ===
'caller') {
432 $member->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
435 if ($field ==
'array_options' && is_array($value)) {
436 foreach ($value as $index => $val) {
437 $member->array_options[$index] = $this->
_checkValForAPI($field, $val, $member);
443 if ($field ==
'statut') {
445 $result = $member->resiliate(DolibarrApiAccess::$user);
447 throw new RestException(500,
'Error when resiliating member: '.$member->error);
449 } elseif ($value ==
'1') {
450 $result = $member->validate(DolibarrApiAccess::$user);
452 throw new RestException(500,
'Error when validating member: '.$member->error);
454 } elseif ($value ==
'-2') {
455 $result = $member->exclude(DolibarrApiAccess::$user);
457 throw new RestException(500,
'Error when excluding member: '.$member->error);
467 if ($member->update(DolibarrApiAccess::$user) >= 0) {
468 return $this->
get(
$id);
470 throw new RestException(500,
'Error when updating member: '.$member->error);
486 public function delete(
$id)
488 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'supprimer')) {
489 throw new RestException(403);
492 $result = $member->fetch(
$id);
494 throw new RestException(404,
'member not found');
498 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
502 $res = $member->delete(DolibarrApiAccess::$user);
504 throw new RestException(500,
"Can't delete, error occurs");
510 'message' =>
'Member deleted'
526 if ($data ===
null) {
531 $mandatoryfields = array(
535 foreach ($mandatoryfields as $field) {
536 if (!isset($data[$field])) {
537 throw new RestException(400,
"$field field missing");
539 $member[$field] = $data[$field];
563 unset(
$object->label_incoterms);
564 unset(
$object->location_incoterms);
565 unset(
$object->fk_delivery_address);
566 unset(
$object->shipping_method_id);
571 unset(
$object->total_localtax1);
572 unset(
$object->total_localtax2);
576 unset(
$object->linkedObjectsIds);
591 unset(
$object->barcode_type_code);
592 unset(
$object->barcode_type_label);
593 unset(
$object->barcode_type_coder);
594 unset(
$object->mode_reglement_id);
595 unset(
$object->cond_reglement_id);
596 unset(
$object->cond_reglement);
597 unset(
$object->fk_delivery_address);
598 unset(
$object->shipping_method_id);
604 unset(
$object->label_incoterms);
605 unset(
$object->location_incoterms);
612 unset(
$object->total_localtax1);
613 unset(
$object->total_localtax2);
637 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
638 throw new RestException(403);
642 $result = $member->fetch(
$id);
644 throw new RestException(404,
'member not found');
648 foreach ($member->subscriptions as $subscription) {
673 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
674 throw new RestException(403);
676 if (!is_numeric($start_date) || !is_numeric($end_date) || !is_numeric($amount)) {
677 throw new RestException(422,
'Malformed data: subscription start or end date, or subscription amount, is not numeric');
679 if ($start_date > $end_date) {
680 throw new RestException(422,
'Malformed data: subscription start is not larger than end date');
684 $result = $member->fetch(
$id);
686 throw new RestException(404,
'member not found');
689 $result = $member->subscription((
int) $start_date, (
float) $amount, 0,
'', $label,
'',
'',
'', (
int) $end_date);
691 throw new RestException(500, $member->error);
714 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
716 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
717 throw new RestException(403);
721 $result = $member->fetch(
$id);
723 throw new RestException(404,
'Member not found');
728 $result = $categories->getListForItem(
$id,
'member', $sortfield, $sortorder, $limit, $page);
731 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
755 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
756 throw new RestException(403);
760 $result = $membertype->fetch(
$id);
762 throw new RestException(404,
'member type not found');
766 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
794 public function indexType($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
798 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
799 throw new RestException(403);
802 $sql =
"SELECT t.rowid";
803 $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)";
804 $sql .=
' WHERE t.entity IN ('.getEntity(
'member_type').
')';
811 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
816 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
818 $sql .= $this->db->order($sortfield, $sortorder);
823 $offset = $limit * $page;
825 $sql .= $this->db->plimit($limit + 1, $offset);
828 $result = $this->db->query($sql);
831 $num = $this->db->num_rows($result);
832 $min = min($num, ($limit <= 0 ? $num : $limit));
834 $obj = $this->db->fetch_object($result);
836 if ($membertype->fetch($obj->rowid)) {
842 throw new RestException(503,
'Error when retrieve member type list : '.$this->db->lasterror());
846 if ($pagination_data) {
847 $totalsResult = $this->db->query($sqlTotals);
848 $total = $this->db->fetch_object($totalsResult)->total;
853 $obj_ret[
'data'] = $tmp;
854 $obj_ret[
'pagination'] = [
855 'total' => (int) $total,
857 'page_count' => ceil((
int) $total / $limit),
880 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
881 throw new RestException(403);
887 foreach ($request_data as $field => $value) {
888 if ($field ===
'caller') {
890 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
894 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
896 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
897 throw new RestException(500,
'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
899 return $membertype->id;
919 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
920 throw new RestException(403);
924 $result = $membertype->fetch(
$id);
926 throw new RestException(404,
'member type not found');
930 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
933 foreach ($request_data as $field => $value) {
934 if ($field ==
'id') {
937 if ($field ===
'caller') {
939 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
942 if ($field ==
'array_options' && is_array($value)) {
943 foreach ($value as $index => $val) {
944 $membertype->array_options[$index] = $val;
950 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
955 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
956 return $this->
get(
$id);
958 throw new RestException(500,
'Error when updating member type: '.$membertype->error);
978 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
979 throw new RestException(403);
982 $result = $membertype->fetch(
$id);
984 throw new RestException(404,
'member type not found');
988 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
991 $res = $membertype->delete(DolibarrApiAccess::$user);
993 throw new RestException(500,
"Can't delete, error occurs");
999 'message' =>
'Member type deleted'
1020 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
1021 throw new RestException(403);
1024 return $this->memberstats->getNbByMonth($year, $format);
1039 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
1040 throw new RestException(403);
1043 return $this->memberstats->getNbByYear();
1060 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
1061 throw new RestException(403);
1064 return $this->memberstats->getAmountByMonth($year, $format);
1082 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
1083 throw new RestException(403);
1086 return $this->memberstats->getLastModifiedMembers($max);
1099 $membertype = array();
1101 $mandatoryfields = array(
'label');
1103 foreach ($mandatoryfields as $field) {
1104 if (!isset($data[$field])) {
1105 throw new RestException(400,
"$field field missing");
1107 $membertype[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Class to manage members of a foundation.
Class to manage statistics of members.
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.
getLastModifiedMembers($max)
Last Modified Members.
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 @phpstan-template T.
getAmountByMonth($year, $format=0)
Return the number of subscriptions by month for a given year.
_validateType($data)
Validate fields before creating an object.
putType($id, $request_data=null)
Update member type.
getNbByYear()
Return an array with the number of subscriptions by year.
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.
getNbByMonth($year, $format=0)
Return an array with the number of members by month for a given year.
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.