18 use Luracast\Restler\RestException;
20 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/subscription.class.php';
33 public static $FIELDS = array(
59 public function get($id)
61 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
62 throw new RestException(401);
66 $result = $subscription->fetch($id);
68 throw new RestException(404,
'Subscription not found');
88 public function index($sortfield =
"dateadh", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
94 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
95 throw new RestException(401);
98 $sql =
"SELECT rowid";
99 $sql .=
" FROM ".MAIN_DB_PREFIX.
"subscription as t";
100 $sql .=
' WHERE 1 = 1';
106 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
110 $sql .= $this->db->order($sortfield, $sortorder);
115 $offset = $limit * $page;
117 $sql .= $this->db->plimit($limit + 1, $offset);
120 $result = $this->db->query(
$sql);
123 $num = $this->db->num_rows($result);
124 while ($i < min($limit, $num)) {
125 $obj = $this->db->fetch_object($result);
127 if ($subscription->fetch($obj->rowid)) {
133 throw new RestException(503,
'Error when retrieve subscription list : '.$this->db->lasterror());
135 if (!count($obj_ret)) {
136 throw new RestException(404,
'No Subscription found');
148 public function post($request_data =
null)
150 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
151 throw new RestException(401);
154 $result = $this->
_validate($request_data);
157 foreach ($request_data as $field => $value) {
158 $subscription->$field = $value;
160 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
161 throw new RestException(500,
'Error when creating contribution', array_merge(array($subscription->error), $subscription->errors));
163 return $subscription->id;
173 public function put($id, $request_data =
null)
175 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
176 throw new RestException(401);
180 $result = $subscription->fetch($id);
182 throw new RestException(404,
'Subscription not found');
185 foreach ($request_data as $field => $value) {
186 if ($field ==
'id') {
189 $subscription->$field = $value;
192 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
193 return $this->
get($id);
195 throw new RestException(500,
'Error when updating contribution: '.$subscription->error);
205 public function delete($id)
208 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
209 throw new RestException(401);
212 $result = $subscription->fetch($id);
214 throw new RestException(404,
'Subscription not found');
217 $res = $subscription->delete(DolibarrApiAccess::$user);
219 throw new RestException(500,
"Can't delete, error occurs");
220 } elseif ($res == 0) {
221 throw new RestException(409,
"Can't delete, that product is probably used");
227 'message' =>
'Subscription deleted'
242 $subscription = array();
243 foreach (Subscriptions::$FIELDS as $field) {
244 if (!isset($data[$field])) {
245 throw new RestException(400,
"$field field missing");
247 $subscription[$field] = $data[$field];
249 return $subscription;