/** * Transfer stock to another location * @param $result * @return mixed */ public function transferStock($result) { // validate input $jsonval = new JsonValidate($this->data, '{"storeditemid":1, "locationid":1, "newlocationid":1, "amount":">=1"}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } if ($this->data->locationid == $this->data->newlocationid) { $result['error'] = "Cannot transfer stock to the same location, pick a different one."; return $result; } // check if theres enough stock at source location if (($stock = $this->stockMdl->get($this->data->storeditemid, $this->data->locationid)) === false) { $result['error'] = "Could not fetch current stock level: " . $this->stockMdl->errorInfo; return $result; } if ($stock[0]['stocklevel'] < $this->data->amount) { $result['error'] = "Not enough stock at the source location, add some first"; return $result; } // create history record for removed stock if ($this->createStockHistory($this->data->storeditemid, $this->data->locationid, 'Stock Transfer', -$this->data->amount, $this->data->newlocationid, 0) === false) { // stock history created with minus $result['error'] = "Could not create stock history record"; return $result; } // remove stock amount from current location if ($this->incrementStockLevel($this->data->storeditemid, $this->data->locationid, $this->data->amount, true) === false) { $result['error'] = "Could not decrement stock from current location"; return $result; } // create history record for added stockd if ($this->createStockHistory($this->data->storeditemid, $this->data->newlocationid, 'Stock Transfer', $this->data->amount, $this->data->locationid, 1) === false) { $result['error'] = "Could not create stock history record"; return $result; } // add stock amount to new location if ($this->incrementStockLevel($this->data->storeditemid, $this->data->newlocationid, $this->data->amount, false) === false) { $result['error'] = "Could not add stock to the new location"; return $result; } // Success; log data Logger::write("Stock Transfer", "STOCK", json_encode($this->data)); return $result; }
/** * @param $result * @return mixed Returns an array of stock. Each row is a certain item & location ID. */ public function getStock($result) { $stockMdl = new StockModel(); $stocks = $stockMdl->get(); if (is_array($stocks)) { $stockdata = []; foreach ($stocks as $stock) { $stockdata[$stock['id']] = $stock; } $result['data'] = $stockdata; } else { $result['error'] = $stockMdl->errorInfo; } return $result; }
/** * Get the current stock levels, does not take into account the current range * @param $result * @return mixed */ public function getStockLevels($result) { $stats = []; $stockMdl = new StockModel(); $stocks = $stockMdl->get(null, null, true); if ($stocks === false) { $result['error'] = "Error getting stock data: " . $stockMdl->errorInfo; } foreach ($stocks as $stock) { $stats[$stock['id']] = new stdClass(); if ($stock['locationid'] == 0) { $stats[$stock['id']]->location = "Warehouse"; } else { $stats[$stock['id']]->location = $stock['location']; } $stats[$stock['id']]->name = $stock['name']; $stats[$stock['id']]->supplier = $stock['supplier']; $stats[$stock['id']]->stocklevel = $stock['stocklevel']; $stats[$stock['id']]->stockvalue = $stock['stockvalue']; } $result['data'] = $stats; return $result; }