/** * Function that submits stocktake. * * @return Response */ public function submitStockTake() { // Validate Input. $validator = Validator::make(Input::all(), array('stockItems' => 'required')); $response = array(); if ($validator->fails()) { $response['state'] = 'Error'; $response['error'] = 'Es necesario escribir el codigo del producto'; return response()->json($response); } // Check that user is part of authorized staff. if (Auth::user()->Type != 1) { $response['state'] = 'No Autorizado'; $response['error'] = 'Usuario no autorizado!'; return response()->json($response); } // Get the branch of the current worker. $branchId = Worker::find(Auth::user()->TypeId)->BranchId; // Create Stocktake. $stocktake = StockTake::create(array('Created' => date('Y-m-d H:i:s'), 'BranchId' => $branchId)); // Now insert the breakdown of the stock. foreach (Input::get('stockItems') as $item) { // Get product. $product = Stock::find($item['id']); // Insert breakdown. StockTakeBreakdown::create(array('StockTakeId' => $stocktake->Id, 'Code' => $product->Code, 'SystemQuantity' => $product->Quantity, 'Counted' => $item['quantity'], 'Difference' => $item['quantity'] - $product->Quantity, 'State' => 0, 'ExtraData' => '')); } $response['state'] = 'Success'; $response['message'] = 'Toma de inventario agregado exitosamente!'; return response()->json($response); }