/** * Display a listing of the resource. * * @return Response */ public function index() { $input = Input::all(); $types = Types::dropdownList(); $suppliers = Suppliers::dropdownList(); $supplierTable = (new Suppliers())->getTable(); $productTable = (new Products())->getTable(); $stocks = null; if ($this->loggedUser()->outlet_id) { $outletStockTable = (new OutletsStocks())->getTable(); $stocks = OutletsStocks::join($productTable, $productTable . '.id', '=', $outletStockTable . '.product_id')->where(function ($q) use($input, $productTable, $outletStockTable) { if (array_key_exists('name', $input) && strlen($input['name'])) { $q->where($productTable . '.name', 'LIKE', "%" . $input['name'] . "%"); } if (array_key_exists('type', $input) && strlen($input['type'])) { $q->where('type_id', '=', $input['type']); } if (array_key_exists('entry_from', $input) && strlen($input['entry_from'])) { $q->where(DB::raw('DATE(' . $outletStockTable . '.created_at)'), '>=', date('Y-m-d', strtotime($input['entry_from']))); } if (array_key_exists('entry_to', $input) && strlen($input['entry_to'])) { $q->where(DB::raw('DATE(' . $outletStockTable . '.created_at)'), '<=', date('Y-m-d', strtotime($input['entry_to']))); } if (array_key_exists('barcode', $input) && strlen($input['barcode'])) { $q->where('product_code', 'LIKE', "%" . trim($input['barcode']) . "%"); } })->select($outletStockTable . '.*')->where('outlet_id', '=', $this->loggedUser()->outlet_id)->paginate(20); } else { $stockTable = (new Stocks())->getTable(); $stocks = Stocks::join($productTable, $productTable . '.id', '=', $stockTable . '.product_id')->join($supplierTable, $supplierTable . '.id', '=', $stockTable . '.supplier_id')->where(function ($q) use($input, $productTable, $stockTable) { if (array_key_exists('name', $input) && strlen($input['name'])) { $q->where($productTable . '.name', 'LIKE', "%" . $input['name'] . "%"); } if (array_key_exists('type', $input) && strlen($input['type'])) { $q->where('type_id', '=', $input['type']); } if (array_key_exists('supplier', $input) && strlen($input['supplier'])) { $q->where('supplier_id', '=', $input['supplier']); } if (array_key_exists('entry_from', $input) && strlen($input['entry_from'])) { $q->where(DB::raw('DATE(' . $stockTable . '.created_at)'), '>=', date('Y-m-d', strtotime($input['entry_from']))); } if (array_key_exists('entry_to', $input) && strlen($input['entry_to'])) { $q->where(DB::raw('DATE(' . $stockTable . '.created_at)'), '<=', date('Y-m-d', strtotime($input['entry_to']))); } if (array_key_exists('barcode', $input) && strlen($input['barcode'])) { $q->where('product_code', 'LIKE', "%" . trim($input['barcode']) . "%"); } })->select($stockTable . '.*')->paginate(20); } $index = $stocks->getPerPage() * ($stocks->getCurrentPage() - 1) + 1; return View::make('stocks.index', compact('stocks', 'index', 'suppliers', 'types', 'input')); }
/** * Store a newly created resource in storage. * * @return Response */ public function store() { $validator = Validator::make(Input::all(), OutletsStocksReturns::$returnrules); $outletsStocks = OutletsStocks::where('product_id', '=', Input::get('product_id'))->where('outlet_id', '=', Input::get('outlet_id'))->first(); $stockId = Input::get('stock_id'); if (Input::get('quantity') <= $outletsStocks->quantity) { if ($validator->passes()) { $stockreturn = new OutletsStocksReturns(); $stockreturn->product_id = Input::get('product_id'); $stockreturn->outlet_id = Input::get('outlet_id'); $stockreturn->quantity = Input::get('quantity'); $stockreturn->status = "Pending..."; $stockreturn->comment = Input::get('comments'); $stockreturn->save(); return Redirect::route('stocks.index')->with('success', 'Stock Return created successfully'); } else { return Redirect::route('stockreturns.return', $stockId)->withErrors($validator)->withInput(Input::all()); } } else { return Redirect::route('stocks.index')->with('error', 'RETURN ERROR : Return Quantity is more than Stock Quantity'); } }
public function returnitem($id) { $sales = SalesItems::where('sales_id', '=', $id)->get(); if ($sales) { foreach ($sales as $sale) { if ($this->user->outlet_id != 0) { $outletstock = OutletsStocks::where('product_id', '=', $sale->product_id)->where('outlet_id', '=', $this->user->outlet_id)->first(); if ($outletstock) { $outletstock->quantity = $outletstock->quantity + $sale->quantity; $outletstock->save(); SalesItems::destroy($sale->id); } } else { $product = Products::find($sale->product_id); if ($product) { // var_dump($product);exit(); $product->quantity = $product->quantity + $sale->quantity; $product->save(); SalesItems::destroy($sale->id); } } } Sales::destroy($id); return Redirect::route('sales.index')->with('success', 'Item successfully return'); } }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { if (!$id) { return Redirect::route('distributions.index')->with('error', 'Please provide distribution id'); } $distribution = Distributions::find($id); if (empty($distribution)) { return Redirect::route('distributions.index')->with('error', 'Distribution not found'); } $outletStock = OutletsStocks::whereProductId($distribution->product_id)->first(); if ($distribution->quantity <= $outletStock->quantity) { $product = Products::find($distribution->product_id); $product->quantity += $distribution->quantity; $product->save(); $outletStock->quantity -= $distribution->quantity; $outletStock->save(); Distributions::destroy($id); return Redirect::route('distributions.index')->with('success', 'Distribution deleted successfully'); } else { return Redirect::route('distributions.index')->with('error', 'Distribution cannot be deleted as the items from this distribution are already sold.'); } }