我在github上为laravel实现了一个名为stevebauman / inventory的库存包,问题是当我创建新库存时,它会自动将一个新记录插入具有默认字段值的stock_stock_movements表中,但我想插入一个自定义字段称为键入那些值“ ENTRADA” || “ SALIDA”,但我无法在代码的最中间部分自动使插入内容成为我代码的一部分
* Calling createStockOnLocation() will automatically create a stock
movement
*/
public function guardar(Request $request){
if ($request->method()=="POST"){
DB::transaction(function() use ($request){
if ($request->session()->exists('productos')){
$productos = $request->session()->get('productos'); //contine los productos que se agregaron al detalle
//para cada productoId obetenemos el reguistro de la tabla inventories
foreach ($productos as $producto){
$inventory = Inventory::find($producto['producto']);
$location = BaumanLocation::find($request->ubicacion);
//si no hay stock en la ubicacion
if(!$inventory->isStockOnLocation($location)){
//creamos stock en la ubicacion especificada en el movimiento
$inventory->createStockOnLocation($producto['cantidad'],$location,$request->motivo);
}else{
//agregamos stock en la ubicacion especificada
$stock = $inventory->getStockFromLocation($location);
$stock->add($producto['cantidad'],$request->motivo);
}
}
}else{
throw new Exeption("No existe la variable productos o aun no se a creado");
}
});
//eliminamos la infromacion de la variable productos que es donde se guardan los items del detalle
$request->session()->forget('productos');
return response()->redirectTo('movimientos');
}
}
/**
* Creates a stock record to the current inventory item.
*
* @param int|float|string $quantity
* @param $location
* @param string $reason
* @param int|float|string $cost
* @param null $aisle
* @param null $row
* @param null $bin
*
* @throws StockAlreadyExistsException
* @throws StockNotFoundException
* @throws \Stevebauman\Inventory\Exceptions\InvalidLocationException
* @throws \Stevebauman\Inventory\Exceptions\NoUserLoggedInException
*
* @return mixed
*/
public function createStockOnLocation($quantity, $location, $reason = '', $cost = 0, $aisle = null, $row = null, $bin = null)
{
$location = $this->getLocation($location);
try {
/*
* We want to make sure stock doesn't exist on the specified location already
*/
if ($this->getStockFromLocation($location)) {
$message = Lang::get('inventory::exceptions.StockAlreadyExistsException', [
'location' => $location->name,
]);
throw new StockAlreadyExistsException($message);
}
} catch (StockNotFoundException $e) {
/*
* A stock record wasn't found on this location, we'll create one
*/
$insert = [
'inventory_id' => $this->id,
'location_id' => $location->id,
'quantity' => 0,
'aisle' => $aisle,
'row' => $row,
'bin' => $bin,
];
/*
* We'll perform a create so a 'first' movement is generated
*/
$stock = $this->stocks()->create($insert);
/*
* Now we'll 'put' the inserted quantity onto the generated stock
* and return the results
*/
return $stock->put($quantity, $reason, $cost);
}
}