Current File : /home/tradevaly/www/app/Http/Controllers/User/ProductController.php
<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\AdvertiserOrder;
use App\Models\Award;
use App\Models\BuyOffer;
use App\Models\Category;
use App\Models\Certificate;
use App\Models\Company;
use App\Models\Country;
use App\Models\FeaturedProduct;
use App\Models\FeaturedRequest;
use App\Models\Income;
use App\Models\Message;
use App\Models\Messsage;
use App\Models\Method;
use App\Models\Package;
use App\Models\AdminNotification;
use App\Models\Pdf;
use App\Models\Product;
use App\Models\QuoteMessage;
use App\Models\Subscriber;
use App\Models\Tag;
use App\Models\Thread;
use App\Models\Thumb;
use App\Models\ProductPrice;
use App\Models\User;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use App\Http\Controllers\User\UserController;
use shurjopayv2\ShurjopayLaravelPackage8\Http\Controllers\ShurjopayController;


class ProductController extends Controller
{
    

    public function products()
    {


        $data['products'] = Product::where('supplier_id', Auth::user()->id)->where('status', 1)->paginate(50);
        return view('user.pages.product.index')->with($data);
    }
    public function pending_product()
    {


        $data['products'] = Product::where('supplier_id', Auth::user()->id)->where('status', 0)->latest()->paginate(50);

        return view('user.pages.product.index')->with($data);
    }

    public function rej_product()
    {
        $data['products'] = Product::where('supplier_id', Auth::user()->id)->where('status', 5)->paginate(50);

        return view('user.pages.product.index')->with($data);
    }

    public function trashed_product()
    {
        $data['products'] = Product::onlyTrashed()->where('supplier_id', Auth::user()->id)->paginate(50);

        return view('user.pages.product.index')->with($data);
    }

    public function delproduct($id)
    {
        $user  =  new UserController();
        $product = Product::findOrFail($id);
        $user->feature_delete_by_product($product->id);
        $user->buy_offer_delete_by_product($product->id);
        $product->delete();

        Toastr::success('Success!', 'Product deleted!');
        return redirect()->back();
    }

    public function trashed_product_destroy($id)
    {
        $user  =  new UserController();
        
        $product = Product::onlyTrashed()->where('id', $id)->where('supplier_id', Auth::id())->firstOrFail();
        $user->delete_trash_product($product);
        Toastr::success('Success!', 'Product Permanent Deleted!');
        return redirect()->back();
    }

    public function trash_product_rerurn($id)
    {
        $user  =  new UserController();
        
        $product = Product::onlyTrashed()->where('id', $id)->where('supplier_id', Auth::id())->firstOrFail();
        if (!Category::find($product->child_id)) {
            Toastr::error('Error!', "Product Category Has Been Deleted You can't Restore The Product ");
            return back();
        }
        $user->restore_trash_product($product);
        Toastr::success('Success!', 'Product Restore Successfully!');
        return back();
    }

    // Trashed Product Restore Methods

    public function restore_trash_product($product)
    {
        $product->restore();
        return true;

    }

    // Trashed Product Permanent Methods
    public function delete_trash_product($product)
    {
        $user  =  new UserController();
        
        $user->feature_delete_by_product($product->id);
        $user->buy_offer_delete_by_product($product->id);
        if ($product->image !== 'image.jpg') {
            if (Storage::disk('public')->exists('images/admin/product/' . $product->image)) {
                Storage::disk('public')->delete('images/admin/product/' . $product->image);
            }
        }
        if (Storage::disk('public')->exists('images/admin/product/' . $product->gallary1)) {
            Storage::disk('public')->delete('images/admin/product/' . $product->gallary1);
        }
        if (Storage::disk('public')->exists('images/admin/product/' . $product->gallary2)) {
            Storage::disk('public')->delete('images/admin/product/' . $product->gallary2);
        }
        if (Storage::disk('public')->exists('images/admin/product/' . $product->gallary3)) {
            Storage::disk('public')->delete('images/admin/product/' . $product->gallary3);
        }
        if (Storage::disk('public')->exists('images/admin/product/' . $product->gallary4)) {
            Storage::disk('public')->delete('images/admin/product/' . $product->gallary4);
        }
        $product->forceDelete();
        return true;
    }

    public function editproduct(Request $request, $id)
    {
        $data['currencies'] =Country::whereNotNull('currency')->groupBy('currency')->orderBy('currency', 'asc')->pluck('currency');
                        
        $product = Product::find($id);
        if ($request->isMethod("post")) {
            
            
        $request->validate([
                'category_id' => ['required'],
                'sub_id' => ['required'],
                'child_id' => ['required'],
                'title' => ['required'],
                'origin' => ['required'],
                'des' => ['required'],
                'currency' => ['required'],
                'qty' => ['nullable']
            ]);

            if ($request->hasFile('image')) {

                $image = $request->file('image');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->image)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->image);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->image = $imageName;
            }


            if ($request->hasFile('gallary1')) {

                $image = $request->file('gallary1');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary1)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary1);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary1 = $imageName;
            }


            if ($request->hasFile('gallary2')) {

                $image = $request->file('gallary2');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary2)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary2);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary2 = $imageName;
            }


            if ($request->hasFile('gallary3')) {

                $image = $request->file('gallary3');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary3)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary3);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary3 = $imageName;
            }


            if ($request->hasFile('gallary4')) {

                $image = $request->file('gallary4');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary4)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary4);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary4 = $imageName;
            }


            $product->ip = visitorip();
            $product->title = $request->title;
            $product->date = date('Y-m-d');
            $product->slug = Str::slug($request->title);
            $product->details = $request->des;
            $product->country_id = Auth::user()->country_id;
            if ($request->filled('child_id')) {
                $product->category_id = $request->category_id;
                $product->sub_id = $request->sub_id;
                $product->child_id = $request->child_id;
            } else {
                $product->category_id = $request->category_id;
                $product->sub_id = $request->sub_id;
            }
            $product->status = 0;
            $product->video = $request->video;
            $product->tags = json_encode($request->tags);
            $product->processing_time = $request->processing_time;
            $product->port = $request->port;
            $product->unit = $request->unit;
            $product->supply_ability = $request->supply_ability;
            $product->payment_method = $request->payment_method;
            $product->origin = $request->origin;
            $product->location = $request->location;
            $product->currency = $request->currency;
            $product->stock = $request->stock;
            $product->location = $request->location;
            $product->price = $request->single_price;
            $product->discount_price = $request->discount_price;
            $product->product_type = $request->product_type;
            $product->supplier_id = Auth::user()->id;

            if ($product->save()) {
                Toastr::success('Success!', 'Setting Updated!');
                return redirect()->back();
            }
        }
        $data['product'] = $product;
        $data['gallaries'] = Thumb::where('product_id', $product->id)->get();
        $data['tags'] = Tag::all();
        $data['caegories'] = Category::all();
        $data['method'] = Method::all();
        return view('user.pages.product.edit')->with($data);
    }

    public function addproduct(Request $request)
    {

        if ($request->isMethod("post")) {
            // dd($request->all());
            
            $request->validate([
                'category_id' => ['required'],
                'sub_id' => ['required'],
                'child_id' => ['required'],
                'title' => ['required'],
                'image' => ['required'],
                'origin' => ['required'],
                'currency' => ['required'],
                'price_type' => ['required'],
                'des' => ['required'],
                'qty' => ['nullable'],
                'product_type' => ['required']
            ]);
            if($request->price_type == 'negotiable'){
                $request->validate([
                    'ng_min' => 'nullable',
                    'ng_max' => 'nullable',
                    'ng_unit'=>'nullable',
                ]);
            }
                if($request->price_type == 'qbp'){
                    $request->validate([
                        'qty_min' => 'required|integer',
                        'qty_max' => 'required|integer',
                        'qty_total' => 'required|integer',
                        'qty_unit' => 'required',
                        'single_price'=>'required|integer',
                    ]);
                }
                if($request->price_type == 'pbq'){
                    $request->validate([
                    'price_min' => 'required|integer',
                    'price_max' => 'required|integer',
                    'price_total' => 'required|integer',
                    'price_unit' => 'required',
                    'single_price'=>'required|integer',
                    ]);
                }
            $product = new Product();


            if ($request->hasFile('brochure')) {

                $image = $request->file('brochure');
                $thefile = \File::get($image);
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product')) {
                    Storage::disk('public')->makeDirectory('images/admin/product');
                }
                Storage::disk('public')->put('images/admin/product/' . $imageName, $thefile);
                $product->brochure = $imageName;
            }


            if ($request->hasFile('image')) {

                $image = $request->file('image');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->image)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->image);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->image = $imageName;
            } else {
                $imageName = "product.jpg";
            }


            if ($request->hasFile('gallary1')) {

                $image = $request->file('gallary1');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary1)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary1);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary1 = $imageName;
            } else {
                $imageName = "product.jpg";
            }


            if ($request->hasFile('gallary2')) {

                $image = $request->file('gallary2');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary2)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary2);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary2 = $imageName;
            } else {
                $imageName = "product.jpg";
            }


            if ($request->hasFile('gallary3')) {

                $image = $request->file('gallary3');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary3)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary3);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary3 = $imageName;
            } else {
                $imageName = "product.jpg";
            }


            if ($request->hasFile('gallary4')) {

                $image = $request->file('gallary4');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/admin/product/' . $product->gallary4)) {
                    Storage::disk('public')->makeDirectory('images/admin/product/' . $product->gallary4);
                }
                $bannerImage = Image::make($image)->resize(500, 500)->stream();
                Storage::disk('public')->put('images/admin/product/' . $imageName, $bannerImage);
                $product->gallary4 = $imageName;
            } else {
                $imageName = "product.jpg";
            }
            if ($request->filled('child_id')) {
                $product->category_id = $request->category_id;
                $product->sub_id = $request->sub_id;
                $product->child_id = $request->child_id;
            } else {
                $product->category_id = $request->sub_id;
                $product->sub_id = $request->category_id;
            }

            $product->ip = visitorip();
            $product->title = $request->title;
            $product->date = date('Y-m-d');
            $product->slug = Str::slug($request->title);
            $product->tags = json_encode($request->tags);
            $product->model_number = $request->model_number;
            $product->brand = $request->brand;
            $product->material = $request->material;
            $product->origin = $request->origin;
            $product->price_type = $request->price_type;
            $product->product_type = $request->product_type;


            if (isset($request['outer-group'][0]['inner-group']) && is_array($request['outer-group'][0]['inner-group'])) {
                $product->attr = json_encode($request['outer-group'][0]['inner-group']);
            }

            // if ($request->price_type == 'fob') {
            //     $product->currency = $request->currency;
            //     $product->min = $request->min;
            //     $product->max = $request->max;
            //     $product->unit = $request->unit;
            //     $product->min_qty = $request->min_qty;
            // } else {

            // }
            if ($request->filled('other_payment_methods')) {
                $arr = array($request->other_payment_methods);
                $product->payment_method = json_encode(array_push($arr, $request->payment_ids));

            } else {
                $product->payment_method = json_encode($request->payment_ids);
            }
            $product->supply_type = $request->supply_type;
            $product->port = $request->port;
            $product->processing_time = $request->processing_time;
            $product->qty = $request->qty;
            $product->details = $request->des;
            $product->country_id = Auth::user()->country_id;
            $product->currency = $request->currency;

            $product->unit = $request->units;
            $product->shipping_time = $request->shipping_time;
            $product->details = $request->des;
            $product->supplier_id = Auth::user()->id;
            $product->meta_description = $request->meta_description;
            $product->meta_keyword = $request->meta_keyword;
            $product->price_type == $request->price_type;
            $product->price = $request->single_price;
            $product->discount_price = $request->discount_price;
            $product->save();
            
            Toastr::success('Success!', "Product Created Successfully!");
            
            $prod = Product::latest()->first();
            $notification = New AdminNotification();
            $notification->user_id=auth()->id();
            $notification->type="new product";
            $notification->message="Added a New Product";
            $notification->admin_reciver="yes";
            $notification->reciver=auth()->id();
            $notification->data=$prod->id;
            $notification->save();
                
            if (!($request->price_type == "negotiable")) {
                $product = Product::latest()->first();
                
                 $productPrice = new ProductPrice();
                if($request->price_type == 'qbp'){
                    $productPrice->min = $request->qty_min;
                    $productPrice->max = $request->qty_max;
                    $productPrice->total = $request->qty_total;
                    $productPrice->unit = $request->qty_unit;
                }
                if($request->price_type == 'pbq'){
                    $productPrice->min = $request->price_min;
                    $productPrice->max = $request->price_max;
                    $productPrice->total = $request->price_total;
                    $productPrice->unit = $request->price_unit;
                }
               
                $productPrice->product_id = $product->id;
                $productPrice->type = $request->price_type;
                $productPrice->save();
                
               
        

                // Toastr::error('Error!', "You cant delete last price");
                if (Auth::user()->verified == 1) {
                    return redirect()->back();
                    //   return redirect()->route('user.featured.submit', $product->id);
                }
                // Toastr::success('Success!', "Product Created Successfully!");
                return redirect()->back();
            }
        }
   $data['currencies'] =Country::whereNotNull('currency')->groupBy('currency')->orderBy('currency', 'asc')->pluck('currency');
        $data['tags'] = Tag::all();
        $data['caegories'] = Category::where('parent_id', 0)->get();
        $data['method'] = Method::all();
        return view('user.pages.product.create')->with($data);
    }
    public function addPrice($id)
    {
        
        $product = Product::where('id',$id)->first();
        $ProductPrices = ProductPrice::where('product_id',$id)->get();
        return view('user.pages.product.price',compact('product','ProductPrices'));
    }
    public function savePrice(Request $request,$id)
    {
        $productPrice = new ProductPrice();
        
        if($request->price_type == 'negotiable'){
            $product = Product::where('id',$id)->first();
            if($product->price_type ="pbq" || $product->price_type ="qbp")
            {
                $productPrices = ProductPrice::where('product_id',$product->id)->get();
                foreach($productPrices as $productPrice)
                {
                    $productPrice->delete();
                }
            }
            $product->price_type = "negotiable";
            $product->price = 0;
            $product->save();
            
        }
        if($request->price_type == 'qbp'){
            $request->validate([
                'qty_min'=>'required|integer' ,
                'qty_max'=>'required|integer'  ,
                'qty_total'=>'required|integer' , 
                'qty_unit'=>'required' , 
                'single_price'=>'required|integer' , 
            ]);
            $productPrice->min = $request->qty_min;
            $productPrice->max = $request->qty_max;
            $productPrice->total = $request->qty_total;
            $productPrice->unit = $request->qty_unit;
            $productPrice->product_id = $id;
            $productPrice->type = $request->price_type;
            
            $product = Product::where('id',$id)->first();
            $product->price_type = "qbp";
            $product->price = $request->single_price;
            $product->discount_price = $request->discount_price;
            $product->save();
            $productPrice->save();
        }
        if($request->price_type == 'pbq'){
            $request->validate([
                'price_min'=>'required|integer'  ,
                'price_max'=>'required|integer'  ,
                'price_total'=>'required|integer', 
                'price_unit'=>'required',
                'single_price'=>'required|integer' ,
            ]);
            $productPrice->min = $request->price_min;
            $productPrice->max = $request->price_max;
            $productPrice->total = $request->price_total;
            $productPrice->unit = $request->price_unit;
            $productPrice->product_id = $id;
            $productPrice->type = $request->price_type;
            $product = Product::where('id',$id)->first();
            $product->price_type = "pbq";
            $product->price = $request->single_price;
            $product->discount_price = $request->discount_price;
            $product->save();
            $productPrice->save();
        }
        
        
        
        return redirect()->back();
    }
    
    public function deletePrice($id)
    {
        $productPrice = ProductPrice::find($id);
        $productId = ProductPrice::where('product_id',$productPrice->product_id)->count();
        if($productId>1){
            $productPrice->delete();
            return redirect()->back();
        }
        else{
            return redirect()->back();
            Toastr::error('Error!', "You cant delete last price");
        }
        
    }
}