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";
306 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
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) {
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);
648 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
649 throw new RestException(403);
653 $result = $member->fetch(
$id);
655 throw new RestException(404,
'member not found');
659 foreach ($member->subscriptions as $subscription) {
684 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
685 throw new RestException(403);
687 if (!is_numeric($start_date) || !is_numeric($end_date) || !is_numeric($amount)) {
688 throw new RestException(422,
'Malformed data: subscription start or end date, or subscription amount, is not numeric');
690 if ($start_date > $end_date) {
691 throw new RestException(422,
'Malformed data: subscription start is not larger than end date');
695 $result = $member->fetch(
$id);
697 throw new RestException(404,
'member not found');
700 $result = $member->subscription((
int) $start_date, (
float) $amount, 0,
'', $label,
'',
'',
'', (
int) $end_date);
702 throw new RestException(500, $member->error);
725 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
727 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
728 throw new RestException(403);
732 $result = $member->fetch(
$id);
734 throw new RestException(404,
'Member not found');
739 $result = $categories->getListForItem(
$id,
'member', $sortfield, $sortorder, $limit, $page);
742 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
766 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
767 throw new RestException(403);
771 $result = $membertype->fetch(
$id);
773 throw new RestException(404,
'member type not found');
777 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
805 public function indexType($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
809 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
810 throw new RestException(403);
813 $sql =
"SELECT t.rowid";
814 $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)";
815 $sql .=
' WHERE t.entity IN ('.getEntity(
'member_type').
')';
820 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
822 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
827 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
829 $sql .= $this->db->order($sortfield, $sortorder);
834 $offset = $limit * $page;
836 $sql .= $this->db->plimit($limit + 1, $offset);
839 $result = $this->db->query($sql);
842 $num = $this->db->num_rows($result);
843 $min = min($num, ($limit <= 0 ? $num : $limit));
845 $obj = $this->db->fetch_object($result);
847 if ($membertype->fetch($obj->rowid)) {
853 throw new RestException(503,
'Error when retrieve member type list : '.$this->db->lasterror());
857 if ($pagination_data) {
858 $totalsResult = $this->db->query($sqlTotals);
859 $total = $this->db->fetch_object($totalsResult)->total;
864 $obj_ret[
'data'] = $tmp;
865 $obj_ret[
'pagination'] = [
866 'total' => (int) $total,
868 'page_count' => ceil((
int) $total / $limit),
891 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
892 throw new RestException(403);
898 foreach ($request_data as $field => $value) {
899 if ($field ===
'caller') {
901 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
905 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
907 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
908 throw new RestException(500,
'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
910 return $membertype->id;
930 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
931 throw new RestException(403);
935 $result = $membertype->fetch(
$id);
937 throw new RestException(404,
'member type not found');
941 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
944 foreach ($request_data as $field => $value) {
945 if ($field ==
'id') {
948 if ($field ===
'caller') {
950 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
953 if ($field ==
'array_options' && is_array($value)) {
954 foreach ($value as $index => $val) {
955 $membertype->array_options[$index] = $val;
961 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
966 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
967 return $this->
get(
$id);
969 throw new RestException(500,
'Error when updating member type: '.$membertype->error);
989 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
990 throw new RestException(403);
993 $result = $membertype->fetch(
$id);
995 throw new RestException(404,
'member type not found');
999 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1002 $res = $membertype->delete(DolibarrApiAccess::$user);
1004 throw new RestException(500,
"Can't delete, error occurs");
1010 'message' =>
'Member type deleted'
1031 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
1032 throw new RestException(403);
1035 return $this->memberstats->getNbByMonth($year, $format);
1050 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
1051 throw new RestException(403);
1054 return $this->memberstats->getNbByYear();
1071 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
1072 throw new RestException(403);
1075 return $this->memberstats->getAmountByMonth($year, $format);
1093 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
1094 throw new RestException(403);
1097 return $this->memberstats->getLastModifiedMembers($max);
1110 $membertype = array();
1112 $mandatoryfields = array(
'label');
1114 foreach ($mandatoryfields as $field) {
1115 if (!isset($data[$field])) {
1116 throw new RestException(400,
"$field field missing");
1118 $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.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_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...)
Class to manage subscriptions of foundation members.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.