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