123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- namespace App\Http\Controllers\Seller;
- use App\Http\Requests\ProductRequest;
- use App\Models\SellerSpreadPackagePayment;
- use Illuminate\Http\Request;
- use App\Models\AttributeValue;
- use App\Models\Cart;
- use App\Models\Category;
- use App\Models\Product;
- use App\Models\ProductTax;
- use App\Models\User;
- use App\Models\ProductTranslation;
- use Carbon\Carbon;
- use Combinations;
- use Artisan;
- use Auth;
- use Str;
- use App\Services\ProductService;
- use App\Services\ProductTaxService;
- use App\Services\ProductFlashDealService;
- use App\Services\ProductStockService;
- use function collect;
- use function count;
- use function date;
- use function dd;
- use function flash;
- use function print_r;
- use function redirect;
- use function seller_package_validity_check;
- use function time;
- use function translate;
- 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;
- }
- public function product_seller_review_modal(Request $request){
- $product = Product::where('id',$request->product_id)->first();
- $customers = User::where('user_type', 'customer')->where('email_verified_at', '!=', null)->orderBy('created_at', 'desc')->get();
- return view('frontend.user.product_seller_review_modal', compact('product','customers'));
- }
- public function index(Request $request)
- {
-
- $search = null;
- $products = Product::where('user_id', Auth::user()->id)->where('digital', 0)->orderBy('created_at', 'desc');
- if ($request->has('search')) {
- $search = $request->search;
- $products = $products->where('name', 'like', '%' . $search . '%');
- }
- $products = $products->paginate(10);
- // dd($products);
- $seller_spread_packages_payments = collect(SellerSpreadPackagePayment::with(['products', 'seller_spread_package'])->where('user_id', Auth::user()->id)->where('expire_at', '>', time())->get())->toArray();
-
- foreach ( $seller_spread_packages_payments as $key=>$seller_spread_packages_payment )
- {
- if (count($seller_spread_packages_payment['products']) >= $seller_spread_packages_payment['product_spread_limit']) {
- unset($seller_spread_packages_payments[$key]);
- }
- }
- return view('seller.product.products.index', compact('products', 'search', 'seller_spread_packages_payments'));
- }
- public function create(Request $request)
- {
- if (addon_is_activated('seller_subscription')) {
- if (seller_package_validity_check()) {
- $categories = Category::where('parent_id', 0)
- ->where('digital', 0)
- ->with('childrenCategories')
- ->get();
- return view('seller.product.products.create', compact('categories'));
- } else {
- flash(translate('Please upgrade your package.'))->warning();
- return back();
- }
- }
- $categories = Category::where('parent_id', 0)
- ->where('digital', 0)
- ->with('childrenCategories')
- ->get();
- return view('seller.product.products.create', compact('categories'));
- }
- public function store(ProductRequest $request)
- {
- if (addon_is_activated('seller_subscription')) {
- if (!seller_package_validity_check()) {
- flash(translate('Please upgrade your package.'))->warning();
- return redirect()->route('seller.products');
- }
- }
- $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'
- ]));
- }
- //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('seller.products');
- }
- public function edit(Request $request, $id)
- {
- $product = Product::findOrFail($id);
- if (Auth::user()->id != $product->user_id) {
- flash(translate('This product is not yours.'))->warning();
- return back();
- }
- $lang = $request->lang;
- $tags = json_decode($product->tags);
- $categories = Category::where('parent_id', 0)
- ->where('digital', 0)
- ->with('childrenCategories')
- ->get();
- return view('seller.product.products.edit', compact('product', 'categories', 'tags', 'lang'));
- }
- 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);
- //VAT & Tax
- if ($request->tax_id) {
- ProductTax::where('product_id', $product->id)->delete();
- $request->merge(['product_id' => $product->id]);
- $this->productTaxService->store($request->only([
- 'tax_id', 'tax', 'tax_type', 'product_id'
- ]));
- }
- // Product Translations
- ProductTranslation::where('lang', $request->lang)
- ->where('product_id', $request->product_id)
- ->updateOrInsert($request->only([
- 'lang', 'name', 'unit', 'description', 'product_id'
- ]));
- flash(translate('Product has been updated successfully'))->success();
- Artisan::call('view:clear');
- Artisan::call('cache:clear');
- return back();
- }
- 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;
- $data = array();
- foreach ($request[$name] as $key => $item) {
- array_push($data, $item);
- }
- array_push($options, $data);
- }
- }
- $combinations = Combinations::makeCombinations($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;
- $data = array();
- foreach ($request[$name] as $key => $item) {
- array_push($data, $item);
- }
- array_push($options, $data);
- }
- }
- $combinations = Combinations::makeCombinations($options);
- return view('backend.product.products.sku_combinations_edit', compact('combinations', 'unit_price', 'colors_active', 'product_name', 'product'));
- }
- 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 updatePublished(Request $request)
- {
- $product = Product::findOrFail($request->id);
- $product->published = $request->status;
- if (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->product_upload_limit <= $shop->user->products()->where('published', 1)->count()
- ) {
- return 2;
- }
- }
- $product->save();
- return 1;
- }
- public function updateFeatured(Request $request)
- {
- $product = Product::findOrFail($request->id);
- $product->seller_featured = $request->status;
- if ($product->save()) {
- Artisan::call('view:clear');
- Artisan::call('cache:clear');
- return 1;
- }
- return 0;
- }
- public function updateSellerSpreadPackage(Request $request)
- {
- $seller_spread_packages_payment = SellerSpreadPackagePayment::find($request->seller_spread_package_payment_id);
- if (!$seller_spread_packages_payment) {
- flash('推广包不存在')->success();
- return redirect()->route('seller.products');
- }elseif ($seller_spread_packages_payment->approval!=1){
- flash('推广包不存在')->success();
- return redirect()->route('seller.products');
- }
- elseif ($seller_spread_packages_payment->expire_at < time()) {
- flash('推广包已过期')->success();
- return redirect()->route('seller.products');
- }
- elseif ($seller_spread_packages_payment->product_spread_limit <= count($seller_spread_packages_payment->products) ) {
- flash('推广包已使用完')->success();
- return redirect()->route('seller.products');
- }
- $product = Product::findOrFail($request->product_id);
- $product->seller_spread_package_payment_id = $seller_spread_packages_payment->id;
- if ($product->save()) {
- Artisan::call('view:clear');
- Artisan::call('cache:clear');
- flash(translate('Product has been spreaded successfully'))->success();
- return redirect()->route('seller.products');
- }
- flash(translate('Error'))->error();
- return redirect()->route('seller.products');
- }
- public function duplicate($id)
- {
- $product = Product::find($id);
- if (Auth::user()->id != $product->user_id) {
- dd($product->user_id);
- flash(translate('This product is not yours.'))->warning();
- return back();
- }
- // dd(seller_package_validity_check());
- // if (addon_is_activated('seller_subscription')) {
- if (!seller_package_validity_check()) {
- flash(translate('Please upgrade your package.'))->warning();
- return back();
- }
- // }
- if (Auth::user()->id == $product->user_id) {
- $product_new = $product->replicate();
- $product_new->slug = $product_new->slug . '-' . Str::random(5);
- $product_new->save();
- //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();
- return redirect()->route('seller.products');
- } else {
- flash(translate('This product is not yours.'))->warning();
- return back();
- }
- }
- public function destroy($id)
- {
- $product = Product::findOrFail($id);
- if (Auth::user()->id != $product->user_id) {
- flash(translate('This product is not yours.'))->warning();
- return back();
- }
- $product->product_translations()->delete();
- $product->stocks()->delete();
- $product->taxes()->delete();
- if (Product::destroy($id)) {
- Cart::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();
- }
- }
- }
|