public static function updateStock($productId) { $product = Products::find($productId); $stockQuantity = Stocks::whereProductId($productId)->sum('quantity'); $soldQuantity = SalesItems::where('product_id', '=', $productId)->sum('quantity'); $product->quantity = $stockQuantity - $soldQuantity; $product->save(); }
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('stocks.index')->with('error', 'Please provide Stock id'); } $stock = Stocks::find($id); $product_id = $stock->product_id; if (empty($stock)) { return Redirect::route('stocks.index')->with('error', 'Stock not found'); } $sales = SalesItems::where('stock_id', '=', $id)->count(); if ($sales) { return Redirect::route('stocks.index')->with('error', 'Stock cannot be delete, since this stock already has sale entry'); } Stocks::destroy($id); Products::updateStock($product_id); return Redirect::route('stocks.index')->with('success', 'Stock deleted successfully'); }