dolibarr 20.0.0
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->hasRight('stock', 'lire')) {
69 throw new RestException(403);
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(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
79 }
80
81 return $this->_cleanObjectDatas($this->stockmovement);
82 }*/
83
97 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
98 {
99 global $conf;
100
101 $obj_ret = array();
102
103 if (!DolibarrApiAccess::$user->hasRight('stock', 'lire')) {
104 throw new RestException(403);
105 }
106
107 $sql = "SELECT t.rowid";
108 $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
109
110 //$sql.= ' WHERE t.entity IN ('.getEntity('stock').')';
111 $sql .= ' WHERE 1 = 1';
112 // Add sql filters
113 if ($sqlfilters) {
114 $errormessage = '';
115 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
116 if ($errormessage) {
117 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
118 }
119 }
120
121 $sql .= $this->db->order($sortfield, $sortorder);
122 if ($limit) {
123 if ($page < 0) {
124 $page = 0;
125 }
126 $offset = $limit * $page;
127
128 $sql .= $this->db->plimit($limit + 1, $offset);
129 }
130
131 $result = $this->db->query($sql);
132 if ($result) {
133 $i = 0;
134 $num = $this->db->num_rows($result);
135 $min = min($num, ($limit <= 0 ? $num : $limit));
136 while ($i < $min) {
137 $obj = $this->db->fetch_object($result);
138 $stockmovement_static = new MouvementStock($this->db);
139 if ($stockmovement_static->fetch($obj->rowid)) {
140 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($stockmovement_static), $properties);
141 }
142 $i++;
143 }
144 } else {
145 throw new RestException(503, 'Error when retrieve stock movement list : '.$this->db->lasterror());
146 }
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->hasRight('stock', 'creer')) {
179 throw new RestException(403);
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 = implode(',', $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->hasRight('stock', 'creer')) {
222 throw new RestException(403);
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(403, '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->hasRight('stock', 'supprimer')) {
255 throw new RestException(403);
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(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
264 }
265
266 if (! $this->stockmovement->delete(DolibarrApiAccess::$user)) {
267 throw new RestException(403,'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) // @phpstan-ignore-line
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}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class for API REST v1.
Definition api.class.php:30
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
Class to manage stock movements.
_validate($data)
Validate fields before create or update 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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
Get properties of a 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:426
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria