dolibarr 22.0.5
api_stockmovements.class.php
1<?php
2/* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
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
19use Luracast\Restler\RestException;
20
21require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
22
23
31{
35 public static $FIELDS = array(
36 'product_id',
37 'warehouse_id',
38 'qty'
39 );
40
44 public $stockmovement;
45
49 public function __construct()
50 {
51 global $db;
52 $this->db = $db;
53 $this->stockmovement = new MouvementStock($this->db);
54 }
55
66 /*
67 public function get($id)
68 {
69 if (!DolibarrApiAccess::$user->hasRight('stock', 'lire')) {
70 throw new RestException(403);
71 }
72
73 $result = $this->stockmovement->fetch($id);
74 if (!$result ) {
75 throw new RestException(404, 'warehouse not found');
76 }
77
78 if (!DolibarrApi::_checkAccessToResource('warehouse',$this->stockmovement->id)) {
79 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
80 }
81
82 return $this->_cleanObjectDatas($this->stockmovement);
83 }*/
84
100 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
101 {
102 $obj_ret = array();
103
104 if (!DolibarrApiAccess::$user->hasRight('stock', 'lire')) {
105 throw new RestException(403);
106 }
107
108 $sql = "SELECT t.rowid";
109 $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
110
111 //$sql.= ' WHERE t.entity IN ('.getEntity('stock').')';
112 $sql .= ' WHERE 1 = 1';
113 // Add sql filters
114 if ($sqlfilters) {
115 $errormessage = '';
116 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
117 if ($errormessage) {
118 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
119 }
120 }
121
122 $sql .= $this->db->order($sortfield, $sortorder);
123 if ($limit) {
124 if ($page < 0) {
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 $i = 0;
135 $num = $this->db->num_rows($result);
136 $min = min($num, ($limit <= 0 ? $num : $limit));
137 while ($i < $min) {
138 $obj = $this->db->fetch_object($result);
139 $stockmovement_static = new MouvementStock($this->db);
140 if ($stockmovement_static->fetch($obj->rowid)) {
141 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($stockmovement_static), $properties);
142 }
143 $i++;
144 }
145 } else {
146 throw new RestException(503, 'Error when retrieve stock movement list : '.$this->db->lasterror());
147 }
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->hasRight('stock', 'creer')) {
180 throw new RestException(403);
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, (float) $price, $movementlabel, $movementcode, $dateMvt, $eatBy, $sellBy, $lot) <= 0) {
202 $errormessage = $this->stockmovement->error;
203 if (empty($errormessage)) {
204 $errormessage = implode(',', $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->hasRight('stock', 'creer')) {
223 throw new RestException(403);
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(403, '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->hasRight('stock', 'supprimer')) {
256 throw new RestException(403);
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(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
265 }
266
267 if (! $this->stockmovement->delete(DolibarrApiAccess::$user)) {
268 throw new RestException(403,'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) // @phpstan-ignore-line
347 {
348 if ($data === null) {
349 $data = array();
350 }
351 $stockmovement = array();
352 foreach (self::$FIELDS as $field) {
353 if (!isset($data[$field])) {
354 throw new RestException(400, "$field field missing");
355 }
356 $stockmovement[$field] = $data[$field];
357 }
358 return $stockmovement;
359 }
360}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
Class for API REST v1.
Definition api.class.php:33
_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:431
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria