<?php
namespace App\Http\Controllers;
use AizPackages\CombinationGenerate\Services\CombinationService;
use App\Http\Requests\ProductRequest;
use App\Models\AttributeBrands;
use App\Models\AttributeValue;
use App\Models\Cart;
use App\Models\Category;
use App\Models\Product;
use App\Models\ProductTax;
use App\Models\ProductTranslation;
use App\Models\User;
use App\Models\Wishlist;
use App\Notifications\ShopProductNotification;
use App\Services\ProductFlashDealService;
use App\Services\ProductService;
use App\Services\ProductStockService;
use App\Services\ProductTaxService;
use Artisan;
use Cache;
use Carbon\Carbon;
use Combinations;
use CoreComponentRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
class ProductController extends Controller {
protected $productService;
protected $productTaxService;
protected $productFlashDealService;
protected $productStockService;
public function __construct(
ProductService $productService,
ProductTaxService $productTaxService,
ProductFlashDealService $productFlashDealService,
ProductStockService $productStockService
) {
$this->productService = $productService;
$this->productTaxService = $productTaxService;
$this->productFlashDealService = $productFlashDealService;
$this->productStockService = $productStockService;
// Staff Permission Check
$this->middleware(['permission:add_new_product'])->only('create');
$this->middleware(['permission:show_all_products'])->only('all_products');
$this->middleware(['permission:show_in_house_products'])->only('admin_products');
$this->middleware(['permission:show_seller_products'])->only('seller_products');
$this->middleware(['permission:product_edit'])->only('admin_product_edit', 'seller_product_edit');
$this->middleware(['permission:product_duplicate'])->only('duplicate');
$this->middleware(['permission:product_delete'])->only('destroy');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function admin_products(Request $request) {
CoreComponentRepository::instantiateShopRepository();
$type = 'In House';
$col_name = null;
$query = null;
$sort_search = null;
$products = Product::where('added_by', 'admin')->where('auction_product', 0)->where('wholesale_product', 0);
if ($request->type != null) {
$var = explode(",", $request->type);
$col_name = $var[0];
$query = $var[1];
$products = $products->orderBy($col_name, $query);
$sort_type = $request->type;
}
if ($request->search != null) {
$sort_search = $request->search;
$products = $products
->where('name', 'like', '%' . $sort_search . '%')
->orWhereHas('stocks', function ($q) use ($sort_search) {
$q->where('sku', 'like', '%' . $sort_search . '%');
});
}
$products = $products->where('digital', 0)->orderBy('created_at', 'desc')->paginate(15);
return view('backend.product.products.index', compact('products', 'type', 'col_name', 'query', 'sort_search'));
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function seller_products(Request $request, $product_type) {
$col_name = null;
$query = null;
$seller_id = null;
$sort_search = null;
$products = Product::where('added_by', 'seller')->where('auction_product', 0)->where('wholesale_product', 0);
if ($request->has('user_id') && $request->user_id != null) {
$products = $products->where('user_id', $request->user_id);
$seller_id = $request->user_id;
}
if ($request->search != null) {
$products = $products
->where('name', 'like', '%' . $request->search . '%');
$sort_search = $request->search;
}
if ($request->type != null) {
$var = explode(",", $request->type);
$col_name = $var[0];
$query = $var[1];
$products = $products->orderBy($col_name, $query);
$sort_type = $request->type;
}
$products = $product_type == 'physical' ? $products->where('digital', 0) : $products->where('digital', 1);
$products = $products->orderBy('created_at', 'desc')->paginate(15);
$type = 'Seller';
if ($product_type == 'digital') {
return view('backend.product.digital_products.index', compact('products', 'sort_search', 'type'));
}
return view('backend.product.products.index', compact('products', 'type', 'col_name', 'query', 'seller_id', 'sort_search'));
}
public function all_products(Request $request) {
$col_name = null;
$query = null;
$seller_id = null;
$sort_search = null;
$products = Product::where('auction_product', 0)->where('wholesale_product', 0);
if ($request->has('user_id') && $request->user_id != null) {
$products = $products->where('user_id', $request->user_id);
$seller_id = $request->user_id;
}
if ($request->search != null) {
$sort_search = $request->search;
$products = $products
->where('name', 'like', '%' . $sort_search . '%')
->orWhereHas('stocks', function ($q) use ($sort_search) {
$q->where('sku', 'like', '%' . $sort_search . '%');
});
}
if ($request->type != null) {
$var = explode(",", $request->type);
$col_name = $var[0];
$query = $var[1];
$products = $products->orderBy($col_name, $query);
$sort_type = $request->type;
}
$products = $products->orderBy('created_at', 'desc')->paginate(15);
$type = 'All';
return view('backend.product.products.index', compact('products', 'type', 'col_name', 'query', 'seller_id', 'sort_search'));
}
public function add_more_choice_option(Request $request) {
$all_attribute_values = AttributeValue::with('attribute')->where('attribute_id', $request->attribute_id)->get();
$html = '';
foreach ($all_attribute_values as $row) {
$html .= '<option value="' . $row->value . '">' . $row->value . '</option>';
}
echo json_encode($html);
}
public function add_brands(Request $request) {
$all_brands_value = AttributeBrands::where('parent_id', $request->parent_id)->get();
$html = '';
$html .= '<option value="">Seleccionar Marca</option>';
foreach ($all_brands_value as $row) {
$html .= '<option value="' . $row->parent_id . '">' . $row->name . '</option>';
}
echo json_encode($html);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function admin_product_edit(Request $request, $id) {
CoreComponentRepository::initializeCache();
$product = Product::findOrFail($id);
if ($product->digital == 1) {
return redirect('admin/digitalproducts/' . $id . '/edit');
}
$lang = $request->lang;
$tags = json_decode($product->tags);
$categories = Category::where('parent_id', 0)
->where('digital', 0)
->with('childrenCategories')
->get();
return view('backend.product.products.edit', compact('product', 'categories', 'tags', 'lang'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function seller_product_edit(Request $request, $id) {
$product = Product::findOrFail($id);
if ($product->digital == 1) {
return redirect('digitalproducts/' . $id . '/edit');
}
$lang = $request->lang;
$tags = json_decode($product->tags);
// $categories = Category::all();
$categories = Category::where('parent_id', 0)
->where('digital', 0)
->with('childrenCategories')
->get();
return view('backend.product.products.edit', compact('product', 'categories', 'tags', 'lang'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(ProductRequest $request, Product $product) {
//Product
$product = $this->productService->update($request->except([
'_token', 'sku', 'choice', 'tax_id', 'tax', 'tax_type', 'flash_deal_id', 'flash_discount', 'flash_discount_type'
]), $product);
//Product Stock
foreach ($product->stocks as $key => $stock) {
$stock->delete();
}
$request->merge(['product_id' => $product->id]);
$this->productStockService->store($request->only([
'colors_active', 'colors', 'choice_no', 'unit_price', 'sku', 'current_stock', 'product_id'
]), $product);
//Flash Deal
$this->productFlashDealService->store($request->only([
'flash_deal_id', 'flash_discount', 'flash_discount_type'
]), $product);
//VAT & Tax
if ($request->tax_id) {
ProductTax::where('product_id', $product->id)->delete();
$this->productTaxService->store($request->only([
'tax_id', 'tax', 'tax_type', 'product_id'
]));
}
// Product Translations
ProductTranslation::updateOrCreate(
$request->only([
'lang', 'product_id'
]),
$request->only([
'name', 'unit', 'description'
])
);
flash(translate('Product has been updated successfully'))->success();
Artisan::call('view:clear');
Artisan::call('cache:clear');
return back();
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(ProductRequest $request) {
$product = $this->productService->store($request->except([
'_token', 'sku', 'choice', 'tax_id', 'tax', 'tax_type', 'flash_deal_id', 'flash_discount', 'flash_discount_type'
]));
$request->merge(['product_id' => $product->id]);
//VAT & Tax
if ($request->tax_id) {
$this->productTaxService->store($request->only([
'tax_id', 'tax', 'tax_type', 'product_id'
]));
}
//Flash Deal
$this->productFlashDealService->store($request->only([
'flash_deal_id', 'flash_discount', 'flash_discount_type'
]), $product);
//Product Stock
$this->productStockService->store($request->only([
'colors_active', 'colors', 'choice_no', 'unit_price', 'sku', 'current_stock', 'product_id'
]), $product);
// Product Translations
$request->merge(['lang' => env('DEFAULT_LANGUAGE')]);
ProductTranslation::create($request->only([
'lang', 'name', 'unit', 'description', 'product_id'
]));
flash(translate('Product has been inserted successfully'))->success();
Artisan::call('view:clear');
Artisan::call('cache:clear');
return redirect()->route('products.admin');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
CoreComponentRepository::initializeCache();
$categories = Category::where('parent_id', 0)
->where('digital', 0)
->with('childrenCategories')
->get();
return view('backend.product.products.create', compact('categories'));
}
public function bulk_product_delete(Request $request) {
if ($request->id) {
foreach ($request->id as $product_id) {
$this->destroy($product_id);
}
}
return 1;
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$product = Product::findOrFail($id);
$product->product_translations()->delete();
$product->stocks()->delete();
$product->taxes()->delete();
if (Product::destroy($id)) {
Cart::where('product_id', $id)->delete();
Wishlist::where('product_id', $id)->delete();
flash(translate('Product has been deleted successfully'))->success();
Artisan::call('view:clear');
Artisan::call('cache:clear');
return back();
} else {
flash(translate('Something went wrong'))->error();
return back();
}
}
/**
* Duplicates the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function duplicate(Request $request, $id) {
$product = Product::find($id);
//Product
$product_new = $this->productService->product_duplicate_store($product);
//Product Stock
$this->productStockService->product_duplicate_store($product->stocks, $product_new);
//VAT & Tax
$this->productTaxService->product_duplicate_store($product->taxes, $product_new);
flash(translate('Product has been duplicated successfully'))->success();
if ($request->type == 'In House')
return redirect()->route('products.admin');
elseif ($request->type == 'Seller')
return redirect()->route('products.seller');
elseif ($request->type == 'All')
return redirect()->route('products.all');
}
public function get_products_by_brand(Request $request) {
$products = Product::where('brand_id', $request->brand_id)->get();
return view('partials.product_select', compact('products'));
}
public function updateTodaysDeal(Request $request) {
$product = Product::findOrFail($request->id);
$product->todays_deal = $request->status;
$product->save();
Cache::forget('todays_deal_products');
return 1;
}
public function updatePublished(Request $request) {
$product = Product::findOrFail($request->id);
$product->published = $request->status;
if ($product->added_by == 'seller' && addon_is_activated('seller_subscription') && $request->status == 1) {
$shop = $product->user->shop;
if (
$shop->package_invalid_at == null
|| Carbon::now()->diffInDays(Carbon::parse($shop->package_invalid_at), false) < 0
|| $shop->seller_package->product_upload_limit <= $shop->user->products()->where('published', 1)->count()
) {
return 0;
}
}
$product->save();
Artisan::call('view:clear');
Artisan::call('cache:clear');
return 1;
}
public function updateProductApproval(Request $request) {
$product = Product::findOrFail($request->id);
$product->approved = $request->approved;
if ($product->added_by == 'seller' && addon_is_activated('seller_subscription')) {
$shop = $product->user->shop;
if (
$shop->package_invalid_at == null
|| Carbon::now()->diffInDays(Carbon::parse($shop->package_invalid_at), false) < 0
|| $shop->seller_package->product_upload_limit <= $shop->user->products()->where('published', 1)->count()
) {
return 0;
}
}
$product->save();
$product_type = $product->digital == 0 ? 'physical' : 'digital';
$status = $request->approved == 1 ? 'approved' : 'rejected';
$users = User::findMany([User::where('user_type', 'admin')->first()->id, $product->user_id]);
Notification::send($users, new ShopProductNotification($product_type, $product, $status));
Artisan::call('view:clear');
Artisan::call('cache:clear');
return 1;
}
public function updateFeatured(Request $request) {
$product = Product::findOrFail($request->id);
$product->featured = $request->status;
if ($product->save()) {
Artisan::call('view:clear');
Artisan::call('cache:clear');
return 1;
}
return 0;
}
public function sku_combination(Request $request) {
$options = array();
if ($request->has('colors_active') && $request->has('colors') && count($request->colors) > 0) {
$colors_active = 1;
array_push($options, $request->colors);
} else {
$colors_active = 0;
}
$unit_price = $request->unit_price;
$product_name = $request->name;
if ($request->has('choice_no')) {
foreach ($request->choice_no as $key => $no) {
$name = 'choice_options_' . $no;
// foreach (json_decode($request[$name][0]) as $key => $item) {
if (isset($request[$name])) {
$data = array();
foreach ($request[$name] as $key => $item) {
// array_push($data, $item->value);
array_push($data, $item);
}
array_push($options, $data);
}
}
}
$combinations = (new CombinationService())->generate_combination($options);
return view('backend.product.products.sku_combinations', compact('combinations', 'unit_price', 'colors_active', 'product_name'));
}
public function sku_combination_edit(Request $request) {
$product = Product::findOrFail($request->id);
$options = array();
if ($request->has('colors_active') && $request->has('colors') && count($request->colors) > 0) {
$colors_active = 1;
array_push($options, $request->colors);
} else {
$colors_active = 0;
}
$product_name = $request->name;
$unit_price = $request->unit_price;
if ($request->has('choice_no')) {
foreach ($request->choice_no as $key => $no) {
$name = 'choice_options_' . $no;
// foreach (json_decode($request[$name][0]) as $key => $item) {
if (isset($request[$name])) {
$data = array();
foreach ($request[$name] as $key => $item) {
// array_push($data, $item->value);
array_push($data, $item);
}
array_push($options, $data);
}
}
}
$combinations = (new CombinationService())->generate_combination($options);
return view('backend.product.products.sku_combinations_edit', compact('combinations', 'unit_price', 'colors_active', 'product_name', 'product'));
}
}
|