dolibarr  19.0.0-dev
api_subscriptions.class.php
1 <?php
2 /* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 use Luracast\Restler\RestException;
19 
20 require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
21 
29 {
33  public static $FIELDS = array(
34  'fk_adherent',
35  'dateh',
36  'datef',
37  'amount',
38  );
39 
43  public function __construct()
44  {
45  global $db, $conf;
46  $this->db = $db;
47  }
48 
59  public function get($id)
60  {
61  if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
62  throw new RestException(401);
63  }
64 
65  $subscription = new Subscription($this->db);
66  $result = $subscription->fetch($id);
67  if (!$result) {
68  throw new RestException(404, 'Subscription not found');
69  }
70 
71  return $this->_cleanObjectDatas($subscription);
72  }
73 
88  public function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
89  {
90  global $conf;
91 
92  $obj_ret = array();
93 
94  if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
95  throw new RestException(401);
96  }
97 
98  $sql = "SELECT rowid";
99  $sql .= " FROM ".MAIN_DB_PREFIX."subscription as t";
100  $sql .= ' WHERE 1 = 1';
101  // Add sql filters
102  if ($sqlfilters) {
103  $errormessage = '';
104  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
105  if ($errormessage) {
106  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
107  }
108  }
109 
110  $sql .= $this->db->order($sortfield, $sortorder);
111  if ($limit) {
112  if ($page < 0) {
113  $page = 0;
114  }
115  $offset = $limit * $page;
116 
117  $sql .= $this->db->plimit($limit + 1, $offset);
118  }
119 
120  $result = $this->db->query($sql);
121  if ($result) {
122  $i = 0;
123  $num = $this->db->num_rows($result);
124  while ($i < min($limit, $num)) {
125  $obj = $this->db->fetch_object($result);
126  $subscription = new Subscription($this->db);
127  if ($subscription->fetch($obj->rowid)) {
128  $obj_ret[] = $this->_cleanObjectDatas($subscription);
129  }
130  $i++;
131  }
132  } else {
133  throw new RestException(503, 'Error when retrieve subscription list : '.$this->db->lasterror());
134  }
135  if (!count($obj_ret)) {
136  throw new RestException(404, 'No Subscription found');
137  }
138 
139  return $obj_ret;
140  }
141 
148  public function post($request_data = null)
149  {
150  if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
151  throw new RestException(401);
152  }
153  // Check mandatory fields
154  $result = $this->_validate($request_data);
155 
156  $subscription = new Subscription($this->db);
157  foreach ($request_data as $field => $value) {
158  $subscription->$field = $value;
159  }
160  if ($subscription->create(DolibarrApiAccess::$user) < 0) {
161  throw new RestException(500, 'Error when creating contribution', array_merge(array($subscription->error), $subscription->errors));
162  }
163  return $subscription->id;
164  }
165 
173  public function put($id, $request_data = null)
174  {
175  if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
176  throw new RestException(401);
177  }
178 
179  $subscription = new Subscription($this->db);
180  $result = $subscription->fetch($id);
181  if (!$result) {
182  throw new RestException(404, 'Subscription not found');
183  }
184 
185  foreach ($request_data as $field => $value) {
186  if ($field == 'id') {
187  continue;
188  }
189  $subscription->$field = $value;
190  }
191 
192  if ($subscription->update(DolibarrApiAccess::$user) > 0) {
193  return $this->get($id);
194  } else {
195  throw new RestException(500, 'Error when updating contribution: '.$subscription->error);
196  }
197  }
198 
205  public function delete($id)
206  {
207  // The right to delete a subscription comes with the right to create one.
208  if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
209  throw new RestException(401);
210  }
211  $subscription = new Subscription($this->db);
212  $result = $subscription->fetch($id);
213  if (!$result) {
214  throw new RestException(404, 'Subscription not found');
215  }
216 
217  $res = $subscription->delete(DolibarrApiAccess::$user);
218  if ($res < 0) {
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");
222  }
223 
224  return array(
225  'success' => array(
226  'code' => 200,
227  'message' => 'Subscription deleted'
228  )
229  );
230  }
231 
240  private function _validate($data)
241  {
242  $subscription = array();
243  foreach (Subscriptions::$FIELDS as $field) {
244  if (!isset($data[$field])) {
245  throw new RestException(400, "$field field missing");
246  }
247  $subscription[$field] = $data[$field];
248  }
249  return $subscription;
250  }
251 }
Class for API REST v1.
Definition: api.class.php:31
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api.class.php:104
Class to manage subscriptions of foundation members.
index($sortfield="dateadh", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List subscriptions.
_validate($data)
Validate fields before creating an object.
post($request_data=null)
Create subscription object.
put($id, $request_data=null)
Update subscription.
__construct()
Constructor.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria