dolibarr  13.0.2
api_stockmovements.class.php
1 <?php
2 /* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
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.'/product/stock/class/mouvementstock.class.php';
21 
22 
30 {
34  static $FIELDS = array(
35  'product_id',
36  'warehouse_id',
37  'qty'
38  );
39 
43  public $stockmovement;
44 
48  public function __construct()
49  {
50  global $db, $conf;
51  $this->db = $db;
52  $this->stockmovement = new MouvementStock($this->db);
53  }
54 
65  /*
66  public function get($id)
67  {
68  if(! DolibarrApiAccess::$user->rights->stock->lire) {
69  throw new RestException(401);
70  }
71 
72  $result = $this->stockmovement->fetch($id);
73  if( ! $result ) {
74  throw new RestException(404, 'warehouse not found');
75  }
76 
77  if( ! DolibarrApi::_checkAccessToResource('warehouse',$this->stockmovement->id)) {
78  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
79  }
80 
81  return $this->_cleanObjectDatas($this->stockmovement);
82  }*/
83 
96  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
97  {
98  global $db, $conf;
99 
100  $obj_ret = array();
101 
102  if (!DolibarrApiAccess::$user->rights->stock->lire) {
103  throw new RestException(401);
104  }
105 
106  $sql = "SELECT t.rowid";
107  $sql .= " FROM ".MAIN_DB_PREFIX."stock_mouvement as t";
108  //$sql.= ' WHERE t.entity IN ('.getEntity('stock').')';
109  $sql .= ' WHERE 1 = 1';
110  // Add sql filters
111  if ($sqlfilters)
112  {
113  if (!DolibarrApi::_checkFilters($sqlfilters))
114  {
115  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
116  }
117  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
118  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
119  }
120 
121  $sql .= $this->db->order($sortfield, $sortorder);
122  if ($limit) {
123  if ($page < 0)
124  {
125  $page = 0;
126  }
127  $offset = $limit * $page;
128 
129  $sql .= $this->db->plimit($limit + 1, $offset);
130  }
131 
132  $result = $this->db->query($sql);
133  if ($result)
134  {
135  $i = 0;
136  $num = $this->db->num_rows($result);
137  $min = min($num, ($limit <= 0 ? $num : $limit));
138  while ($i < $min)
139  {
140  $obj = $this->db->fetch_object($result);
141  $stockmovement_static = new MouvementStock($this->db);
142  if ($stockmovement_static->fetch($obj->rowid)) {
143  $obj_ret[] = $this->_cleanObjectDatas($stockmovement_static);
144  }
145  $i++;
146  }
147  } else {
148  throw new RestException(503, 'Error when retrieve stock movement list : '.$this->db->lasterror());
149  }
150  if (!count($obj_ret)) {
151  throw new RestException(404, 'No stock movement found');
152  }
153  return $obj_ret;
154  }
155 
177  public function post($product_id, $warehouse_id, $qty, $lot = '', $movementcode = '', $movementlabel = '', $price = '', $dlc = '', $dluo = '')
178  {
179  if (!DolibarrApiAccess::$user->rights->stock->creer) {
180  throw new RestException(401);
181  }
182 
183  if ($qty == 0) {
184  throw new RestException(503, "Making a stock movement with a quentity of 0 is not possible");
185  }
186 
187  // Type increase or decrease
188  $type = 2;
189  if ($qty >= 0) {
190  $type = 3;
191  }
192 
193  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
194  $eatBy = empty($dluo) ? '' : dol_stringtotime($dluo);
195  $sellBy = empty($dlc) ? '' : dol_stringtotime($dlc);
196 
197  if ($this->stockmovement->_create(DolibarrApiAccess::$user, $product_id, $warehouse_id, $qty, $type, $price, $movementlabel, $movementcode, '', $eatBy, $sellBy, $lot) <= 0) {
198  $errormessage = $this->stockmovement->error;
199  if (empty($errormessage)) $errormessage = join(',', $this->stockmovement->errors);
200  throw new RestException(503, 'Error when create stock movement : '.$errormessage);
201  }
202 
203  return $this->stockmovement->id;
204  }
205 
213  /*
214  public function put($id, $request_data = null)
215  {
216  if(! DolibarrApiAccess::$user->rights->stock->creer) {
217  throw new RestException(401);
218  }
219 
220  $result = $this->stockmovement->fetch($id);
221  if( ! $result ) {
222  throw new RestException(404, 'stock movement not found');
223  }
224 
225  if( ! DolibarrApi::_checkAccessToResource('stock',$this->stockmovement->id)) {
226  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
227  }
228 
229  foreach($request_data as $field => $value) {
230  if ($field == 'id') continue;
231  $this->stockmovement->$field = $value;
232  }
233 
234  if($this->stockmovement->update($id, DolibarrApiAccess::$user))
235  return $this->get ($id);
236 
237  return false;
238  }*/
239 
246  /*
247  public function delete($id)
248  {
249  if(! DolibarrApiAccess::$user->rights->stock->supprimer) {
250  throw new RestException(401);
251  }
252  $result = $this->stockmovement->fetch($id);
253  if( ! $result ) {
254  throw new RestException(404, 'stock movement not found');
255  }
256 
257  if( ! DolibarrApi::_checkAccessToResource('stock',$this->stockmovement->id)) {
258  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
259  }
260 
261  if (! $this->stockmovement->delete(DolibarrApiAccess::$user)) {
262  throw new RestException(401,'error when delete stock movement');
263  }
264 
265  return array(
266  'success' => array(
267  'code' => 200,
268  'message' => 'Warehouse deleted'
269  )
270  );
271  }*/
272 
273 
274 
275  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
282  protected function _cleanObjectDatas($object)
283  {
284  // phpcs:enable
285  $object = parent::_cleanObjectDatas($object);
286 
287  // Remove useless data
288  unset($object->civility_id);
289  unset($object->firstname);
290  unset($object->lastname);
291  unset($object->name);
292  unset($object->location_incoterms);
293  unset($object->label_incoterms);
294  unset($object->fk_incoterms);
295  unset($object->lines);
296  unset($object->total_ht);
297  unset($object->total_ttc);
298  unset($object->total_tva);
299  unset($object->total_localtax1);
300  unset($object->total_localtax2);
301  unset($object->note);
302  unset($object->note_private);
303  unset($object->note_public);
304  unset($object->shipping_method_id);
305  unset($object->fk_account);
306  unset($object->model_pdf);
307  unset($object->fk_delivery_address);
308  unset($object->cond_reglement);
309  unset($object->cond_reglement_id);
310  unset($object->mode_reglement_id);
311  unset($object->barcode_type_coder);
312  unset($object->barcode_type_label);
313  unset($object->barcode_type_code);
314  unset($object->barcode_type);
315  unset($object->country_code);
316  unset($object->country_id);
317  unset($object->country);
318  unset($object->thirdparty);
319  unset($object->contact);
320  unset($object->contact_id);
321  unset($object->user);
322  unset($object->fk_project);
323  unset($object->project);
324  unset($object->canvas);
325 
326  //unset($object->eatby); Filled correctly in read mode
327  //unset($object->sellby); Filled correctly in read mode
328 
329  return $object;
330  }
331 
340  private function _validate($data)
341  {
342  $stockmovement = array();
343  foreach (self::$FIELDS as $field) {
344  if (!isset($data[$field]))
345  throw new RestException(400, "$field field missing");
346  $stockmovement[$field] = $data[$field];
347  }
348  return $stockmovement;
349  }
350 }
Class to manage stock movements.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
Get properties of a stock movement object.
_cleanObjectDatas($object)
Update stock movement.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition: date.lib.php:319
$conf db
API class for accounts.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
Class for API REST v1.
Definition: api.class.php:30
__construct()
Constructor.
_validate($data)
Validate fields before create or update object.