Edit File: ProductController.php
<?php namespace App\Http\Controllers\StoresDashboard; use App\Http\Controllers\Controller; use App\Http\Requests\StoreProductRequest; use App\Models\Feature; use App\Models\Product; use App\Models\ProductAdditive; use App\Models\ProductAdditiveCategory; use App\Models\Productfeature; use App\Models\Productfeatureproperity; use App\Models\ProductGroup; use App\Models\Properity; use App\Models\Store; use App\Models\StoreMenuCategory; use App\Traits\ApiTrait; use App\Traits\GeneralTrait; use App\Traits\Uploadable; use Auth; use Illuminate\Http\Request; use Redirect; use Session; class ProductController extends Controller { use ApiTrait, GeneralTrait, Uploadable; public function get_products(Request $request) { $user = Auth::user(); $store = Store::where('user_id', $user->id) ->first(); $products = Product::where('store_id', $store->id)->get(); return view('stores_dashboard.products.products', compact('store', 'products', 'user')); } public function get_add_product(Request $request) { $user = Auth::user(); $store = Store::where('user_id', $user->id) ->first(); $menus = StoreMenuCategory::where('store_id', $store->id)->get(); return view('stores_dashboard.products.new_product', compact('store', 'menus', 'user')); } public function post_product(StoreProductRequest $request) { $data = $request->validated(); $data['name'] = [ 'ar' => $data['name_ar'], 'en' => $data['name_en'], ]; $data['desc'] = [ 'ar' => $data['desc_ar'], 'en' => $data['desc_en'], ]; $product = Product::create($data); if ($product) { $group = new ProductGroup(); $group->price = $request['price']; $skuused = ProductGroup::where('in_stock_sku', $request['in_stock_sku'])->first(); if ($skuused) { $msg = Session::has('lang') && Session::get('lang') == 'en' ? 'ٌstorage unit is used before!' : 'وحدة حفظ المخزون مستخدمة من قبل.'; return response()->json([ 'key' => 'fail', 'msg' => $msg, ]); } $group->in_stock_type = $request['in_stock_type']; $group->in_stock_sku = $request['in_stock_sku']; $group->in_stock_qty = $request['in_stock_qty']; if ($request['discount_price']) { $group->discount_price = $request['discount_price']; } if ($request['from']) { $group->from = $request['from']; } if ($request['to']) { $group->to = $request['to']; } $group->product_id = $product->id; $group->save(); } if ($product->type == 'multiple') { $msg = Session::has('lang') && Session::get('lang') == 'en' ? 'ٌproduct is added successfully , add fearutes please!' : 'تم إضافه المنتج بنجاح. من فضلك قم بإضافة السمات'; $url = route('stores_dashboard.add_product_features', ['id' => $product->id]); } else { $msg = trans('dashboard.created_successfully'); $url = route('stores_dashboard.get_products'); } $store=Store::find($product->store_id); $store->updateCacheWithProducts(); return response()->json([ 'key' => 'success', 'msg' => $msg, 'url' => $url, ]); } public function updateProducts(StoreProductRequest $request) { $data = $request->validated(); $product = Product::find($request['product_id']); $product->name = [ 'ar' => $data['name_ar'], 'en' => $data['name_en'], ]; $product->desc = [ 'ar' => $data['desc_ar'], 'en' => $data['desc_en'], ]; $product->store_id = $data['store_id']; if(isset($data['image'])){ $product->image = $data['image']; } $product->store_menu_category_id = $data['store_menu_category_id']; $product->type = $data['type']; $product->update(); if ($product->update()) { $group = $product->groups()->firstOrNew(); $group->price = $request['price']; $group->in_stock_type = $request['in_stock_type']; $skuused = ProductGroup::where('in_stock_sku', $request['in_stock_sku'])->where('id', '!=', $group->id)->first(); if ($skuused) { $msg = Session::has('lang') && Session::get('lang') == 'en' ? 'ٌstorage unit is used before!' : 'وحدة حفظ المخزون مستخدمة من قبل.'; return response()->json([ 'key' => 'fail', 'msg' => $msg, ]); } $group->in_stock_sku = $request['in_stock_sku']; if ($request['discount_price']) { $group->discount_price = $request['discount_price']; } if ($request['from']) { $group->from = $request['from']; } if ($request['to']) { $group->to = $request['to']; } $group->in_stock_qty = $request['in_stock_qty']; $group->product_id = $product->id; $group->save(); } if ($product->type == 'multiple') { $msg = Session::has('lang') && Session::get('lang') == 'en' ? 'ٌproduct is added successfully , add fearutes please!' : 'تم إضافه المنتج بنجاح. من فضلك قم بإضافة السمات'; $url = route('stores_dashboard.add_product_features', ['id' => $product->id]); } else { $msg = trans('dashboard.alerts.updated_successfully'); $url = route('stores_dashboard.get_products'); } $store=Store::find($product->store_id); $store->updateCacheWithProducts(); return response()->json([ 'key' => 'success', 'value' => '1', 'url' => $url, 'msg' => $msg, ]); } public function get_add_product_features($id) { $product = Product::find($id); $features = Feature::all(); return view('stores_dashboard.products.add_product_feature', compact('product', 'features')); } public function postProductFeatures(Request $request) { $product = Product::find($request['id']); $feature = Feature::find($request['feature_id']); if (!$product || !$feature) { $msg = Session::has('lang') && Session::get('lang') == 'en' ? 'Opps! Looks like somrthing went wrong' : 'حدث خطأ ما'; return response()->json([ 'key' => 'fail', 'value' => '0', 'msg' => $msg, ]); } $pr = Productfeature::where('feature_id', $request['feature_id'])->where('product_id', $request['id'])->first(); if ($pr) { $msg = 'هذه السمه مضافه من قبل'; return response()->json([ 'key' => 'fail', 'value' => '0', 'msg' => $msg, ]); } $pr = new Productfeature(); $pr->feature_id = $request['feature_id']; $pr->product_id = $request['id']; $pr->save(); $msg = trans('dashboard.created_successfully'); $store=Store::find($product->store_id); $store->updateCacheWithProducts(); return response()->json([ 'key' => 'success', 'value' => '1', 'feature_name' => $feature->name, 'productfeature_id' => $pr->id, 'properities' => $feature->properities, 'msg' => $msg, ]); } public function removeProductFeature(Request $request) { $pr = Productfeature::find($request['id']); if (!$pr) { $msg = trans('stores_dashboard.feature_not_fount'); return response()->json([ 'key' => 'fail', 'value' => '0', 'msg' => $msg, ]); } $id = $pr->id; $pr->delete(); $store=Store::find($pr->product->store_id); $store->updateCacheWithProducts(); return response()->json([ 'key' => 'success', 'value' => '1', 'id' => $id, ]); } public function postProductFeatureProperities(Request $request) { $product = Product::find($request['id']); if (!$product) { $msg = 'wrong id.'; return response()->json([ 'key' => 'fail', 'value' => '0', 'msg' => $msg, ]); } $ids = $request['productfeature_id']; if ($ids) { foreach ($ids as $id) { $pr = Productfeature::find($id); if ($pr) { $pr->productfeatureproperities()->delete(); $properities = 'properities' . $id; if ($request[$properities]) { foreach ($request[$properities] as $p) { $properity = Properity::find($p); if ($p) { $pfr = new Productfeatureproperity(); $pfr->productfeature_id = $id; $pfr->properity_id = $p; $pfr->save(); } } $store=Store::find($pr->product->store_id); $store->updateCacheWithProducts(); } else { $msg = trans('stores_dashboard.add_feature_option'); return response()->json([ 'key' => 'fail', 'value' => '0', 'msg' => $msg, ]); } } } } else { $msg = trans('stores_dashboard.add_feature_option'); return response()->json([ 'key' => 'fail', 'value' => '0', 'msg' => $msg, ]); } $msg = trans('stores_dashboard.features_added_successfully'); $url = route('stores_dashboard.addGroups', ['id' => $product->id]); return response()->json([ 'key' => 'success', 'value' => '1', 'msg' => $msg, 'url' => $url, ]); } public function addGroups($id) { $product = Product::find($id); if (!$product || $product->type == 'simple') { return redirect()->route('store.products'); } $groups = []; foreach ($product->groups()->where('properities', '!=', null)->where('status', 'posted')->get() as $group) { $properities = []; foreach (json_decode($group->properities) as $id) { $properity = Properity::find($id); if ($properity) { array_push($properities, $properity->name); } } $groups[] = [ 'id' => $group->id, 'in_stock_qty' => $group->in_stock_qty, 'price' => $group->price, 'properities' => $properities, ]; } $store=Store::find($product->store_id); $store->updateCacheWithProducts(); return view('stores_dashboard.products.addGroups', compact('product', 'groups')); } public function identical_values($arrayA, $arrayB) { sort($arrayA); sort($arrayB); return $arrayA == $arrayB; } public function postGroups(Request $request) { $product = Product::find($request['id']); if (!$product) { $msg = Session::has('lang') && Session::get('lang') == 'en' ? 'Opps! Looks like somrthing went wrong' : 'حدث خطأ ما'; return response()->json([ 'key' => 'fail', 'value' => '0', 'msg' => $msg, ]); } $groups = ProductGroup::where('product_id', $request['id'])->where('status', 'posted')->where('properities', '!=', null)->get(); if ($groups) { foreach ($groups as $group) { if ($this->identical_values(json_decode($group->properities), $request['productfeatures_ids'])) { $msg = Session::has('lang') && Session::get('lang') == 'en' ? 'ٌYou have added this group before' : 'قمت من قبل باضافه هذه المجموعة'; return response()->json([ 'key' => 'fail', 'value' => '0', 'msg' => $msg, ]); } } } $properities = json_encode($request['productfeatures_ids']); $arr = []; foreach ($request['productfeatures_ids'] as $id) { $properity = Properity::find($id); if ($properity) { array_push($arr, $properity->name); } } $group = new ProductGroup(); $group->properities = $properities; $group->product_id = $request['id']; $first_group = $product->groups()->first(); $group->price = $first_group->price; $group->in_stock_qty = $first_group->in_stock_qty; $group->save(); $group = ProductGroup::find($group->id); $store=Store::find($product->store_id); $store->updateCacheWithProducts(); return response()->json([ 'key' => 'success', 'value' => '1', 'group_id' => $group->id, 'in_stock_qty' => $group->in_stock_qty, 'price' => $group->price, 'arr' => $arr, ]); } public function removeGroup(Request $request) { $group = ProductGroup::find($request['id']); if (!$group) { $msg = Session::has('lang') && Session::get('lang') == 'en' ? 'This group is mot available' : 'هذه المجموعة غير متاحة'; return response()->json([ 'key' => 'fail', 'value' => '0', 'msg' => $msg, ]); } $id = $group->id; $group->status = 'deleted'; $group->update(); $store=Store::find($group->product->store_id); $store->updateCacheWithProducts(); return response()->json([ 'key' => 'success', 'value' => '1', 'id' => $id, ]); } public function updateGroups(Request $request) { $group = ProductGroup::find($request['group_id']); if (!$group) { $msg = Session::has('lang') && Session::get('lang') == 'en' ? 'Opps! Looks like somrthing went wrong' : 'حدث خطأ ما'; return response()->json([ 'key' => 'fail', 'value' => '0', 'msg' => $msg, ]); } if ($request['price']) { $group->price = $request['price']; } if ($request['in_stock_qty']) { $group->in_stock_qty = $request['in_stock_qty']; } $group->update(); $group->refresh(); $store=Store::find($group->product->store_id); $store->updateCacheWithProducts(); $msg = Session::has('lang') && Session::get('lang') == 'en' ? 'Information added successfully' : 'تم إضافة المعلومات بنجاح'; return response()->json([ 'key' => 'success', 'value' => '1', 'msg' => $msg, 'id' => $group->id, 'in_stock_qty' => $group->in_stock_qty, 'price' => $group->price, ]); } public function deleteProducts(Request $request) { $product = Product::find($request['id']); if (!$product) { $msg = Session::has('lang') && Session::get('lang') == 'en' ? 'Opps! Looks like somrthing went wrong' : 'حدث خطأ ما'; return response()->json([ 'key' => 'fail', 'value' => '0', 'msg' => $msg, ]); } $product->status = "deleted"; $product->update(); $store=Store::find($product->store_id); $store->updateCacheWithProducts(); $msg = route('store.products', ['id' => $product->user_id]); return response()->json([ 'key' => 'success', 'value' => '1', 'msg' => $msg, 'id' => $product->id, ]); } public function editProducts(Request $request) { $user = Auth::user(); $store = Store::where('user_id', $user->id) ->first(); $product = Product::find($request['id']); if (!$product) { return redirect()->back()->with(['fail' => trans('stores_dashboard.product_not_found')]); } $group = $product->groups()->first(); $features = Feature::get(); $menus = StoreMenuCategory::where('store_id', $store->id)->get(); return view('stores_dashboard.products.edit', compact('product', 'group', 'features', 'menus', 'store')); } public function get_add_addition(Request $request, $id) { $user = Auth::user(); $store = Store::where('user_id', $user->id) ->first(); $product = Product::find($id); $additions = ProductAdditive::where('product_id', $product->id)->get(); $categories = ProductAdditiveCategory::where('store_id',Auth::user()->store->id)->get(); return view('stores_dashboard.products.add_additive', compact('store', 'user', 'categories', 'product', 'additions')); } public function post_addition(Request $request) { // $data = $request->validated(); $product = Product::find($request['product_id']); $additives=ProductAdditive::where('product_id',$product->id)->delete(); if($request['name_ar']){ foreach ($request['name_ar'] as $key => $ex) { if ($request['name_en'][$key] && $request['price'][$key] && $request['product_additive_category_id'][$key]) { $addition = new ProductAdditive(); $addition->name = [ 'ar' => $request['name_ar'][$key], 'en' => $request['name_en'][$key], ]; $addition->price = $request['price'][$key]; $addition->product_additive_category_id = $request['product_additive_category_id'][$key]; $addition->product_id = $product->id; $addition->save(); } } } $msg = trans('dashboard.created_successfully'); $url = route('stores_dashboard.get_products'); $store=Store::find($product->store_id); $store->updateCacheWithProducts(); return response()->json([ 'key' => 'success', 'msg' => $msg, 'url' => $url, ]); } public function delete_product(Request $request) { $product = Product::find($request['id']); $product->delete(); $msg = trans('auth.deleted_success'); $url = route('stores_dashboard.get_products'); $store=Store::find($product->store_id); $store->updateCacheWithProducts(); return response()->json([ 'key' => 'success', 'msg' => $msg, 'url' => $url, ]); } }
Back to File Manager