dolibarr  13.0.2
api_mos.class.php
1 <?php
2 /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3  * Copyright (C) 2019 Maxime Kohlhaas <maxime@atm-consulting.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/mrp/class/mo.class.php';
22 
23 
36 class Mos extends DolibarrApi
37 {
41  public $mo;
42 
46  public function __construct()
47  {
48  global $db, $conf;
49  $this->db = $db;
50  $this->mo = new Mo($this->db);
51  }
52 
64  public function get($id)
65  {
66  if (!DolibarrApiAccess::$user->rights->mrp->read) {
67  throw new RestException(401);
68  }
69 
70  $result = $this->mo->fetch($id);
71  if (!$result) {
72  throw new RestException(404, 'MO not found');
73  }
74 
75  if (!DolibarrApi::_checkAccessToResource('mrp', $this->mo->id, 'mrp_mo')) {
76  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
77  }
78 
79  return $this->_cleanObjectDatas($this->mo);
80  }
81 
82 
97  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
98  {
99  global $db, $conf;
100 
101  $obj_ret = array();
102  $tmpobject = new Mo($this->db);
103 
104  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
105 
106  $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
107 
108  // If the internal user must only see his customers, force searching by him
109  $search_sale = 0;
110  if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
111  $search_sale = DolibarrApiAccess::$user->id;
112  }
113 
114  $sql = "SELECT t.rowid";
115  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
116  $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
117  }
118  $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." as t";
119 
120  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
121  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
122  }
123  $sql .= " WHERE 1 = 1";
124 
125  // Example of use $mode
126  //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
127  //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
128 
129  if ($tmpobject->ismultientitymanaged) {
130  $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
131  }
132  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
133  $sql .= " AND t.fk_soc = sc.fk_soc";
134  }
135  if ($restrictonsocid && $socid) {
136  $sql .= " AND t.fk_soc = ".$socid;
137  }
138  if ($restrictonsocid && $search_sale > 0) {
139  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
140  }
141  // Insert sale filter
142  if ($restrictonsocid && $search_sale > 0) {
143  $sql .= " AND sc.fk_user = ".$search_sale;
144  }
145  if ($sqlfilters) {
146  if (!DolibarrApi::_checkFilters($sqlfilters)) {
147  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
148  }
149  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
150  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
151  }
152 
153  $sql .= $this->db->order($sortfield, $sortorder);
154  if ($limit) {
155  if ($page < 0) {
156  $page = 0;
157  }
158  $offset = $limit * $page;
159 
160  $sql .= $this->db->plimit($limit + 1, $offset);
161  }
162 
163  $result = $this->db->query($sql);
164  if ($result) {
165  $num = $this->db->num_rows($result);
166  $i = 0;
167  while ($i < $num) {
168  $obj = $this->db->fetch_object($result);
169  $tmp_object = new Mo($this->db);
170  if ($tmp_object->fetch($obj->rowid)) {
171  $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
172  }
173  $i++;
174  }
175  } else {
176  throw new RestException(503, 'Error when retrieve MO list');
177  }
178  if (!count($obj_ret)) {
179  throw new RestException(404, 'No MO found');
180  }
181  return $obj_ret;
182  }
183 
190  public function post($request_data = null)
191  {
192  if (!DolibarrApiAccess::$user->rights->mrp->write) {
193  throw new RestException(401);
194  }
195  // Check mandatory fields
196  $result = $this->_validate($request_data);
197 
198  foreach ($request_data as $field => $value) {
199  $this->mo->$field = $value;
200  }
201  if (!$this->mo->create(DolibarrApiAccess::$user)) {
202  throw new RestException(500, "Error creating MO", array_merge(array($this->mo->error), $this->mo->errors));
203  }
204  return $this->mo->id;
205  }
206 
215  public function put($id, $request_data = null)
216  {
217  if (!DolibarrApiAccess::$user->rights->mrp->write) {
218  throw new RestException(401);
219  }
220 
221  $result = $this->mo->fetch($id);
222  if (!$result) {
223  throw new RestException(404, 'MO not found');
224  }
225 
226  if (!DolibarrApi::_checkAccessToResource('mrp', $this->mo->id, 'mrp_mo')) {
227  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
228  }
229 
230  foreach ($request_data as $field => $value) {
231  if ($field == 'id') {
232  continue;
233  }
234  $this->mo->$field = $value;
235  }
236 
237  if ($this->mo->update(DolibarrApiAccess::$user) > 0) {
238  return $this->get($id);
239  } else {
240  throw new RestException(500, $this->mo->error);
241  }
242  }
243 
250  public function delete($id)
251  {
252  if (!DolibarrApiAccess::$user->rights->mrp->delete) {
253  throw new RestException(401);
254  }
255  $result = $this->mo->fetch($id);
256  if (!$result) {
257  throw new RestException(404, 'MO not found');
258  }
259 
260  if (!DolibarrApi::_checkAccessToResource('mrp', $this->mo->id, 'mrp_mo')) {
261  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
262  }
263 
264  if (!$this->mo->delete(DolibarrApiAccess::$user)) {
265  throw new RestException(500, 'Error when deleting MO : '.$this->mo->error);
266  }
267 
268  return array(
269  'success' => array(
270  'code' => 200,
271  'message' => 'MO deleted'
272  )
273  );
274  }
275 
276 
277  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
284  protected function _cleanObjectDatas($object)
285  {
286  // phpcs:enable
287  $object = parent::_cleanObjectDatas($object);
288 
289  unset($object->rowid);
290  unset($object->canvas);
291 
292  unset($object->name);
293  unset($object->lastname);
294  unset($object->firstname);
295  unset($object->civility_id);
296  unset($object->statut);
297  unset($object->state);
298  unset($object->state_id);
299  unset($object->state_code);
300  unset($object->region);
301  unset($object->region_code);
302  unset($object->country);
303  unset($object->country_id);
304  unset($object->country_code);
305  unset($object->barcode_type);
306  unset($object->barcode_type_code);
307  unset($object->barcode_type_label);
308  unset($object->barcode_type_coder);
309  unset($object->total_ht);
310  unset($object->total_tva);
311  unset($object->total_localtax1);
312  unset($object->total_localtax2);
313  unset($object->total_ttc);
314  unset($object->fk_account);
315  unset($object->comments);
316  unset($object->note);
317  unset($object->mode_reglement_id);
318  unset($object->cond_reglement_id);
319  unset($object->cond_reglement);
320  unset($object->shipping_method_id);
321  unset($object->fk_incoterms);
322  unset($object->label_incoterms);
323  unset($object->location_incoterms);
324 
325  // If object has lines, remove $db property
326  if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
327  $nboflines = count($object->lines);
328  for ($i = 0; $i < $nboflines; $i++) {
329  $this->_cleanObjectDatas($object->lines[$i]);
330 
331  unset($object->lines[$i]->lines);
332  unset($object->lines[$i]->note);
333  }
334  }
335 
336  return $object;
337  }
338 
347  private function _validate($data)
348  {
349  $myobject = array();
350  foreach ($this->mo->fields as $field => $propfield) {
351  if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
352  continue; // Not a mandatory field
353  }
354  if (!isset($data[$field])) {
355  throw new RestException(400, "$field field missing");
356  }
357  $myobject[$field] = $data[$field];
358  }
359  return $myobject;
360  }
361 }
Class for Mo.
Definition: mo.class.php:34
put($id, $request_data=null)
Update MO.
$conf db
API class for accounts.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
List Mos.
Class for API REST v1.
Definition: api.class.php:30
__construct()
Constructor.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
Definition: api.class.php:252
post($request_data=null)
Create MO object.
_validate($data)
Validate fields before create or update object.
_cleanObjectDatas($object)
Clean sensible object datas.