dolibarr  17.0.4
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  public 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 $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 ".$this->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  $errormessage = '';
113  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
114  if ($errormessage) {
115  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
116  }
117  }
118 
119  $sql .= $this->db->order($sortfield, $sortorder);
120  if ($limit) {
121  if ($page < 0) {
122  $page = 0;
123  }
124  $offset = $limit * $page;
125 
126  $sql .= $this->db->plimit($limit + 1, $offset);
127  }
128 
129  $result = $this->db->query($sql);
130  if ($result) {
131  $i = 0;
132  $num = $this->db->num_rows($result);
133  $min = min($num, ($limit <= 0 ? $num : $limit));
134  while ($i < $min) {
135  $obj = $this->db->fetch_object($result);
136  $stockmovement_static = new MouvementStock($this->db);
137  if ($stockmovement_static->fetch($obj->rowid)) {
138  $obj_ret[] = $this->_cleanObjectDatas($stockmovement_static);
139  }
140  $i++;
141  }
142  } else {
143  throw new RestException(503, 'Error when retrieve stock movement list : '.$this->db->lasterror());
144  }
145  if (!count($obj_ret)) {
146  throw new RestException(404, 'No stock movement found');
147  }
148  return $obj_ret;
149  }
150 
176  public function post($product_id, $warehouse_id, $qty, $type = 2, $lot = '', $movementcode = '', $movementlabel = '', $price = '', $datem = '', $dlc = '', $dluo = '', $origin_type = '', $origin_id = 0)
177  {
178  if (!DolibarrApiAccess::$user->rights->stock->creer) {
179  throw new RestException(401);
180  }
181 
182  if ($qty == 0) {
183  throw new RestException(503, "Making a stock movement with a quantity of 0 is not possible");
184  }
185 
186  // Type increase or decrease
187  if ($type == 1 && $qty >= 0) {
188  $type = 0;
189  }
190  if ($type == 2 && $qty >= 0) {
191  $type = 3;
192  }
193 
194  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
195  $eatBy = empty($dluo) ? '' : dol_stringtotime($dluo);
196  $sellBy = empty($dlc) ? '' : dol_stringtotime($dlc);
197  $dateMvt = empty($datem) ? '' : dol_stringtotime($datem);
198 
199  $this->stockmovement->setOrigin($origin_type, $origin_id);
200  if ($this->stockmovement->_create(DolibarrApiAccess::$user, $product_id, $warehouse_id, $qty, $type, $price, $movementlabel, $movementcode, $dateMvt, $eatBy, $sellBy, $lot) <= 0) {
201  $errormessage = $this->stockmovement->error;
202  if (empty($errormessage)) {
203  $errormessage = join(',', $this->stockmovement->errors);
204  }
205  throw new RestException(503, 'Error when create stock movement : '.$errormessage);
206  }
207 
208  return $this->stockmovement->id;
209  }
210 
218  /*
219  public function put($id, $request_data = null)
220  {
221  if(! DolibarrApiAccess::$user->rights->stock->creer) {
222  throw new RestException(401);
223  }
224 
225  $result = $this->stockmovement->fetch($id);
226  if( ! $result ) {
227  throw new RestException(404, 'stock movement not found');
228  }
229 
230  if( ! DolibarrApi::_checkAccessToResource('stock',$this->stockmovement->id)) {
231  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
232  }
233 
234  foreach($request_data as $field => $value) {
235  if ($field == 'id') continue;
236  $this->stockmovement->$field = $value;
237  }
238 
239  if($this->stockmovement->update($id, DolibarrApiAccess::$user))
240  return $this->get ($id);
241 
242  return false;
243  }*/
244 
251  /*
252  public function delete($id)
253  {
254  if(! DolibarrApiAccess::$user->rights->stock->supprimer) {
255  throw new RestException(401);
256  }
257  $result = $this->stockmovement->fetch($id);
258  if( ! $result ) {
259  throw new RestException(404, 'stock movement not found');
260  }
261 
262  if( ! DolibarrApi::_checkAccessToResource('stock',$this->stockmovement->id)) {
263  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
264  }
265 
266  if (! $this->stockmovement->delete(DolibarrApiAccess::$user)) {
267  throw new RestException(401,'error when delete stock movement');
268  }
269 
270  return array(
271  'success' => array(
272  'code' => 200,
273  'message' => 'Warehouse deleted'
274  )
275  );
276  }*/
277 
278 
279 
280  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
287  protected function _cleanObjectDatas($object)
288  {
289  // phpcs:enable
290  $object = parent::_cleanObjectDatas($object);
291 
292  // Remove useless data
293  unset($object->civility_id);
294  unset($object->firstname);
295  unset($object->lastname);
296  unset($object->name);
297  unset($object->location_incoterms);
298  unset($object->label_incoterms);
299  unset($object->fk_incoterms);
300  unset($object->lines);
301  unset($object->total_ht);
302  unset($object->total_ttc);
303  unset($object->total_tva);
304  unset($object->total_localtax1);
305  unset($object->total_localtax2);
306  unset($object->note);
307  unset($object->note_private);
308  unset($object->note_public);
309  unset($object->shipping_method_id);
310  unset($object->fk_account);
311  unset($object->model_pdf);
312  unset($object->fk_delivery_address);
313  unset($object->cond_reglement);
314  unset($object->cond_reglement_id);
315  unset($object->mode_reglement_id);
316  unset($object->barcode_type_coder);
317  unset($object->barcode_type_label);
318  unset($object->barcode_type_code);
319  unset($object->barcode_type);
320  unset($object->country_code);
321  unset($object->country_id);
322  unset($object->country);
323  unset($object->thirdparty);
324  unset($object->contact);
325  unset($object->contact_id);
326  unset($object->user);
327  unset($object->fk_project);
328  unset($object->project);
329  unset($object->canvas);
330 
331  //unset($object->eatby); Filled correctly in read mode
332  //unset($object->sellby); Filled correctly in read mode
333 
334  return $object;
335  }
336 
345  private function _validate($data)
346  {
347  $stockmovement = array();
348  foreach (self::$FIELDS as $field) {
349  if (!isset($data[$field])) {
350  throw new RestException(400, "$field field missing");
351  }
352  $stockmovement[$field] = $data[$field];
353  }
354  return $stockmovement;
355  }
356 }
Class for API REST v1.
Definition: api.class.php:31
Class to manage stock movements.
_validate($data)
Validate fields before create or update object.
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:407
forgeSQLFromUniversalSearchCriteria($filter, &$error='')
forgeSQLFromUniversalSearchCriteria
$conf db
API class for accounts.
Definition: inc.php:41