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');
91 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
115 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
119 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'lire')) {
120 throw new RestException(403);
123 $sql =
"SELECT t.rowid";
124 $sql .=
" FROM ".MAIN_DB_PREFIX.
"stock_mouvement AS t";
125 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"entrepot as e ON t.fk_entrepot = e.rowid";
126 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"stock_mouvement_extrafields AS ef ON (ef.fk_object = t.rowid)";
127 $sql .=
" WHERE e.entity IN (".getEntity(
'stock').
")";
134 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
139 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
141 $sql .= $this->db->order($sortfield, $sortorder);
146 $offset = $limit * $page;
148 $sql .= $this->db->plimit($limit + 1, $offset);
151 $result = $this->db->query($sql);
154 $num = $this->db->num_rows($result);
155 $min = min($num, ($limit <= 0 ? $num : $limit));
157 $obj = $this->db->fetch_object($result);
159 if ($stockmovement_static->fetch($obj->rowid)) {
165 throw new RestException(503,
'Error when retrieve stock movement list : '.$this->db->lasterror());
169 if ($pagination_data) {
170 $totalsResult = $this->db->query($sqlTotals);
171 $total = $this->db->fetch_object($totalsResult)->total;
176 $obj_ret[
'data'] = $tmp;
177 $obj_ret[
'pagination'] = [
178 'total' => (int) $total,
180 'page_count' => ceil((
int) $total / $limit),
217 public function post($product_id, $warehouse_id, $qty, $type = 2, $batch =
'', $movementcode =
'', $label =
'', $price =
'', $datem =
'', $sellBy =
'', $eatBy =
'', $origin_type =
'', $origin_id = 0)
219 if (!DolibarrApiAccess::$user->hasRight(
'stock',
'creer')) {
220 throw new RestException(403);
224 throw new RestException(503,
"Making a stock movement with a quantity of 0 is not possible");
228 if ($type == 1 && $qty >= 0) {
231 if ($type == 2 && $qty >= 0) {
235 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
240 $this->stockmovement->setOrigin($origin_type, $origin_id);
241 if ($this->stockmovement->_create(DolibarrApiAccess::$user, $product_id, $warehouse_id, $qty, $type, (
float) $price, $label, $movementcode, $dateMvt, $dluo, $dlc, $batch) <= 0) {
242 $errormessage = $this->stockmovement->error;
243 if (empty($errormessage)) {
244 $errormessage = implode(
',', $this->stockmovement->errors);
246 throw new RestException(503,
'Error when create stock movement : '.$errormessage);
249 return $this->stockmovement->id;
342 unset(
$object->location_incoterms);
343 unset(
$object->label_incoterms);
349 unset(
$object->total_localtax1);
350 unset(
$object->total_localtax2);
354 unset(
$object->shipping_method_id);
357 unset(
$object->fk_delivery_address);
358 unset(
$object->cond_reglement);
359 unset(
$object->cond_reglement_id);
360 unset(
$object->mode_reglement_id);
361 unset(
$object->barcode_type_coder);
362 unset(
$object->barcode_type_label);
363 unset(
$object->barcode_type_code);
392 if ($data ===
null) {
395 $stockmovement = array();
396 foreach (self::$FIELDS as $field) {
397 if (!isset($data[$field])) {
398 throw new RestException(400,
"$field field missing");
400 $stockmovement[$field] = $data[$field];
402 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.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
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, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria