21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
39 public static $FIELDS = array(
48 public $stockmovement;
77 public function get(
$id)
79 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'mouvement',
'lire')) {
80 throw new RestException(403);
83 throw new RestException(400,
'No stock movement with id 0 can exist');
85 $result = $this->stockmovement->fetch(
$id);
87 throw new RestException(404,
'stock movement not found');
111 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
115 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
116 throw new RestException(403);
119 $sql =
"SELECT t.rowid";
120 $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)";
123 $sql .=
' WHERE 1 = 1';
129 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
134 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
136 $sql .= $this->db->order($sortfield, $sortorder);
141 $offset = $limit * $page;
143 $sql .= $this->db->plimit($limit + 1, $offset);
146 $result = $this->db->query($sql);
149 $num = $this->db->num_rows($result);
150 $min = min($num, ($limit <= 0 ? $num : $limit));
152 $obj = $this->db->fetch_object($result);
154 if ($stockmovement_static->fetch($obj->rowid)) {
160 throw new RestException(503,
'Error when retrieve stock movement list : '.$this->db->lasterror());
164 if ($pagination_data) {
165 $totalsResult = $this->db->query($sqlTotals);
166 $total = $this->db->fetch_object($totalsResult)->total;
171 $obj_ret[
'data'] = $tmp;
172 $obj_ret[
'pagination'] = [
173 'total' => (int) $total,
175 'page_count' => ceil((
int) $total / $limit),
212 public function post($product_id, $warehouse_id, $qty, $type = 2, $batch =
'', $movementcode =
'', $label =
'', $price =
'', $datem =
'', $sellBy =
'', $eatBy =
'', $origin_type =
'', $origin_id = 0)
214 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'creer')) {
215 throw new RestException(403);
219 throw new RestException(503,
"Making a stock movement with a quantity of 0 is not possible");
223 if ($type == 1 && $qty >= 0) {
226 if ($type == 2 && $qty >= 0) {
230 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
235 $this->stockmovement->setOrigin($origin_type, $origin_id);
236 if ($this->stockmovement->_create(DolibarrApiAccess::$user, $product_id, $warehouse_id, $qty, $type, (
float) $price, $label, $movementcode, $dateMvt, $dluo, $dlc, $batch) <= 0) {
237 $errormessage = $this->stockmovement->error;
238 if (empty($errormessage)) {
239 $errormessage = implode(
',', $this->stockmovement->errors);
241 throw new RestException(503,
'Error when create stock movement : '.$errormessage);
244 return $this->stockmovement->id;
336 unset(
$object->location_incoterms);
337 unset(
$object->label_incoterms);
343 unset(
$object->total_localtax1);
344 unset(
$object->total_localtax2);
348 unset(
$object->shipping_method_id);
351 unset(
$object->fk_delivery_address);
352 unset(
$object->cond_reglement);
353 unset(
$object->cond_reglement_id);
354 unset(
$object->mode_reglement_id);
355 unset(
$object->barcode_type_coder);
356 unset(
$object->barcode_type_label);
357 unset(
$object->barcode_type_code);
386 if ($data ===
null) {
389 $stockmovement = array();
390 foreach (self::$FIELDS as $field) {
391 if (!isset($data[$field])) {
392 throw new RestException(400,
"$field field missing");
394 $stockmovement[$field] = $data[$field];
396 return $stockmovement;
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_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.
post($product_id, $warehouse_id, $qty, $type=2, $batch='', $movementcode='', $label='', $price='', $datem='', $sellBy='', $eatBy='', $origin_type='', $origin_id=0)
Create a stock movement.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
Get a list of stock movements.
_cleanObjectDatas($object)
Update stock movement.
__construct()
Constructor.
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...
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria