VaKeR CYBER ARMY
Logo of a company Server : Apache
System : Linux host44.registrar-servers.com 4.18.0-513.18.1.lve.2.el8.x86_64 #1 SMP Sat Mar 30 15:36:11 UTC 2024 x86_64
User : vapecompany ( 2719)
PHP Version : 7.4.33
Disable Function : NONE
Directory :  /home/vapecompany/demo.vapecompany.com.bd/app/Http/Controllers/Backend/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/vapecompany/demo.vapecompany.com.bd/app/Http/Controllers/Backend/ProductController.php
<?php

namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use App\Models\Barcode;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Color;
use App\Models\PriceHistory;
use App\Models\Product;
use App\Models\ProductModel;
use Carbon\Carbon;
use DB;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        $this->middleware('admin');

    }
    public function index()
    {
        //dd(auth()->user()->getrole()->module_event);
        // if(!auth()->user()->canEvent('all_product'))
        // {
        //    return redirect('/');
        //}
        $colors=Color::get();
       // dd($products);


        return view('backend.product.list',['colors'=>$colors]);
    }
    function get_all_product(Request $request){

        $draw = $request->get('draw');
        $start = $request->get("start");
        $rowperpage = $request->get("length"); // Rows display per page

        $columnIndex_arr = $request->get('order');
        $columnName_arr = $request->get('columns');
        $order_arr = $request->get('order');
        $search_arr = $request->get('search');

        $columnIndex = $columnIndex_arr[0]['column']; // Column index
        $columnName = $columnName_arr[$columnIndex]['data']; // Column name
        $columnSortOrder = $order_arr[0]['dir']; // asc or desc
        $searchValue = $search_arr['value']; // Search value

        $totalRecords = Product::count();
        $products =Product::orderBy($columnName,$columnSortOrder);
        if($columnName_arr[0]['search']['value'] != null){
            $products=$products->Where('product_id', $columnName_arr[0]['search']['value'] );
        }
        if($columnName_arr[1]['search']['value'] != null){
            $products=$products->Where('category_name', 'like', $columnName_arr[1]['search']['value'] . '%');
        }
        if($columnName_arr[2]['search']['value'] != null){
            $products=$products->Where('brand_name', 'like', $columnName_arr[2]['search']['value'] . '%');
        }
        if($columnName_arr[3]['search']['value'] != null){
            $products=$products->Where('product_code', 'like', $columnName_arr[3]['search']['value'] . '%');
        }
        if($columnName_arr[4]['search']['value'] != null){
            $products=$products->Where('product_name', 'like', $columnName_arr[4]['search']['value'] . '%');
        }
        if($columnName_arr[5]['search']['value'] != null){
            $products=$products->Where('model_name', 'like', $columnName_arr[5]['search']['value'] . '%');
        }
        if($columnName_arr[6]['search']['value'] != null){
            $products=$products->Where('sale_price', 'like', $columnName_arr[6]['search']['value'] . '%');
        }

        if($columnName_arr[7]['search']['value'] != null){
            $products=$products->Where('products.status',  $columnName_arr[7]['search']['value']);
        }
        if($rowperpage < 0) {
            $rowperpage = 8999999999999999999;
        }
        // ->leftJoin('product_models','product_models.model_id','products.model_id')
        //     ->leftJoin('brands','brands.brand_id','products.brand_id')
        $products =$products
            ->leftJoin('categories','categories.category_id','products.category_id')


            ->skip($start)
            ->take($rowperpage)
            ->get();
        $data_arr = array();
        foreach($products as $product){
          $qty=0;
            if($product->barcodes){
               $qty=$product->barcodes->sum('quantity');
            }
            $action ='<div role="group" aria-label="Basic example" class="btn-group btn-group-sm">';
            //  if(auth()->user()->canEvent('product_edit'))
            // {
            $action ='<a href="'.url('admin/product_edit/'.$product->product_id).'" class="mr-2">
                                                        <i class="fa fa-edit text-warning"></i>
                                                    </a> ';
            // }
             $action .='<a href="#" onclick="create_price('.$product->product_id.')"  class="btn btn-default btn-rounded mb-4" data-toggle="modal" data-target="#price_setups">
                                                       Enter Price
                                                    </a> ';
            // if($qty > 0){
            //   $action .='<a href="javascript:void(0)" data-id="'.$product->product_id.'" class="download-bar-code mr-2"><i aria-hidden="true" class="fa fa-barcode text-secondary"></i></a>';
            // }


            // $action .='<a href="javascript:void(0);">
            //                                             <i class="fa fa-trash text-danger"></i>
            //                                         </a>';
            $action .=' </div>';
            $img='<img height="30px" width="50px" src="'.url('public/images/product/').'/'.$product->product_image.'">';


           $color="";
           $c=json_decode($product->product_color);
           $i=0;
        //    foreach ($c as $key => $value) {
        //         if($i == 0){

        //             $color .= Color::where('color_id',$value)->first()->color_name;
        //         }else{
        //             $color .= ',';
        //             $color .=Color::where('color_id',$value)->first()->color_name;
        //         }
        //         $i++;
        //     }





            $data_arr[] = array(
                "product_id" => $product->product_id,
                // "brand_name" => $product->brand->brand_name,
                "category_name" => $product->category ? $product->category->category_name : '',
                // "model_name" => $product->model ? $product->model->model_name:'',
                "product_code" => $product->product_code,
                "product_name" => $product->product_name,
                "sale_price" => '<span id="sale'.$product->product_id.'">'.$product->sale_price.'</span>',
                "purchase_price" => '<span id="sale'.$product->product_id.'">'.$product->purchase_price.'</span>',
                // 'intentive_price' => '<span id="incentive'.$product->product_id.'">'.$product->incentive_price.'</span>',
                // "color_id" => $color,
                "status" => $product->status == 1 ?'Active':'In Active',
                // "specification" => $product->specification ? "<div class='product_show_more' sytyle='height:50px;overflow:hidden;'>".$product->specification."</div><div>show more</div>" : '',
                "product_image" => $img,
                "qty" => $qty,
                "Action" => $action,
            );
        }
        $response = array(
            "draw" => intval($draw),
            "recordsTotal" => $totalRecords,
            "recordsFiltered" => $totalRecords,
            "data" => $data_arr
        );

        echo json_encode($response);
        exit;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // if(!auth()->user()->canEvent('new_product'))
        // {
        //     return redirect('/');
        // }
        $brands=Brand::where('status',1)->get();
        $colors=Color::where('status',1)->get();
        $categories=Category::where('status',1)->get();
        $models=ProductModel::where('status',1)->get();
        $id=1;
        if(Product::first()){
            $id=Product::latest()->first()->product_id+1;
        }

        $product_code=str_pad($id,6,0,STR_PAD_LEFT);
        return view('backend.product.create',['categories'=>$categories,'brands'=>$brands,'models'=>$models,'product_code'=>$product_code,'colors'=>$colors]);
    }
    public function print_barcode($id,Request $request){
      $quantity=request()->quantity;
        $product=Barcode::where('product_id',$id)->where('color_id',$request->color)->first();
        //dd($product->product);
        if($product){
          return view('product.product_barcode',['quantity'=>$quantity,'product'=>$product]);
        }else{
          print('Product Stock Is Empty');
        }
       // dd($quantity);
        return $product;

    }
    public function newProductCode(){
      $id=1;
        if(Product::first()){
            $id=Product::latest()->first()->product_id+1;
        }
          $product_code=str_pad($id,6,0,STR_PAD_LEFT);
        return $product_code;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
      // return $request;
         $validator = \Validator::make($request->all(), [
            'product_name' => 'required|max:200|unique:products',
            'product_code' => 'required|unique:products|max:20',

            'sale_price' => 'required',
            'purchase_price' => 'required',
           // 'wholesale_price' => 'required',

            'category' => 'required',

           // 'specification' => 'required',
            'product_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',

        ]);

       // return response()->json(['re'=> $request->all()]);
        if ($validator->fails())
        {
            return response()->json(['errors'=>$validator->errors()->all()]);
        }else{
           //return  response()->json(['re'=> auth()->user()->user_id]);
            //DB::transaction(function () {
                if ($request->hasFile('product_image')) {

                    if ($request->file('product_image')->isValid()) {
                        $image = $request->file('product_image');
                        $new_name = $request->product_name.'_'.date('_Y-m-d_H-i-s'). '.' . $image->getClientOriginalExtension();
                        $image->move(public_path('images/product/'), $new_name);
                    }else{
                        return response()->json(['errors'=>['imag'=>'image not valid']]);
                    }
                }else{
                    return response()->json(['errors'=>['imag'=>'image not found']]);
                }

                $product =  new Product;
                $product->product_name = trim($request->product_name);
                $product->product_code = trim($request->product_code);
                //$product->model_id = $request->model;
                $product->sale_price = $request->sale_price ? $request->sale_price : 0;
                $product->purchase_price = $request->purchase_price ? $request->purchase_price : 0;
                //$product->wholesale_price = $request->wholesale_price;
               // $product->brand_id = $request->brand;
                $product->category_id = $request->category;
                $product->status = $request->status ? $request->status : 0;
                //$product->is_imei = $request->is_imei ? $request->is_imei : 0;
               // $product->product_color = json_encode($request->product_color);
                $product->short_description = $request->short_description;
                $product->description = $request->description;
                $product->product_image = $new_name;
                $product->created_by = auth('admin')->user()->id;
                $product->save();

                $priceHistory = new PriceHistory;
                $priceHistory->history_date = Carbon::create('now');
                $priceHistory->product_id=$product->product_id;
                $priceHistory->sale_price = $request->sale_price ? $request->sale_price : 0;
                $priceHistory->intentive_price=  0;
                $priceHistory->created_by = auth('admin')->user()->id;
                $priceHistory->save();
           // });
        }
        return response()->json(['errors'=>'0','success'=>$request->product_name.' is successfully added']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function show(Product $product)
    {
        //
    }

    public function getProductById(Request $request)
    {
        $arr = 0;

        if (isset($request->category_id)) {
            return Product::where('category_id', $request->category_id)->where('status', 1)->get();
        }

        return $arr;
    }
    public function getPurchasePriceById(Request $request)
    {
        $arr = 0;

        if (isset($request->product_id)) {
            return Product::where('product_id', $request->product_id)->where('status', 1)->first();
        }

        return $arr;
    }
    public function get_productwithstock(Request $request)
    {
      $arr=0;
        if(isset($request->brand_id)){
            return DB::table('barcodes')
            ->select('products.product_id','product_name')
            ->leftJoin('products', 'products.product_id', '=', 'barcodes.product_id')
            ->where('barcodes.brand_id',$request->brand_id)->where('barcodes.quantity', '>',0)
            ->get();
        }

        return $arr;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // if(!auth()->user()->canEvent('product_edit'))
        // {
        //     return redirect('/');
        // }
        $product=Product::where('product_id',$id)->first();

        $brands=Brand::where('status',1)->get();
        $colors=Color::where('status',1)->get();
        $categories=Category::where('status',1)->get();
        $models=ProductModel::where('status',1)->get();
       // dd($product);
        return view('backend.product.edit',['categories'=>$categories,'brands'=>$brands,'product'=>$product,'models'=>$models,'colors'=>$colors]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Product $product)
    {
        //return $request->all();
        $code=$request->product_id;
        $validator = \Validator::make($request->all(), [
            'product_name' => 'required|max:200',
           // 'product_code' => 'required|max:20|unique:products,product_code,'.$code.',product_id',
            // 'model' => 'required',
            //'sale_price' => 'required',
            //'purchase_price' => 'required',
            //'wholesale_price' => 'required',
            // 'brand' => 'required',
           'category' => 'required',
           // 'specification' => 'required',


        ]);

       // return response()->json(['re'=> $request->all()]);
        if ($validator->fails())
        {
            return response()->json(['errors'=>$validator->errors()->all()]);
        }else{
           //return  response()->json(['re'=> auth()->user()->user_id]);
            //DB::transaction(function () {
            $new_name="";
                if ($request->hasFile('product_image')) {

                    if ($request->file('product_image')->isValid()) {
                        $image = $request->file('product_image');
                        $new_name = $request->product_name.'_'.date('_Y-m-d_H-i-s'). '.' . $image->getClientOriginalExtension();
                        $image->move(public_path('images/product'), $new_name);
                         unlink(public_path('images/product/'.$request->oldfile));
                    }else{
                        return response()->json(['errors'=>['imag'=>'image not valid']]);
                    }
                }

                $product =  Product::find($request->product_id);
                $product->product_name = trim($request->product_name);
                //$product->product_code = $request->product_code);
                // $product->model_id = $request->model;
                $product->sale_price = $request->sale_price;
                $product->purchase_price = $request->purchase_price;
                //$product->wholesale_price = $request->wholesale_price;
                // $product->brand_id = $request->brand;
                $product->category_id = $request->category;
                $product->status = $request->status ? 1 : 0;
                // $product->is_imei = $request->is_imei ? $request->is_imei : 0;
                // $product->product_color = json_encode($request->product_color);
                $product->short_description = $request->short_description;
                $product->description = $request->description;
                $product->sale_price = $request->sale_price;
                if($new_name != ""){
                    $product->product_image = $new_name;
                }

                // $product->updated_by = auth()->user()->id;
                $product->save();
           // });
        }
        //return  $request->status;
        return response()->json(['errors'=>'0','success'=>$request->product_name.' is successfully Updated']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function destroy(Product $product)
    {
        //
    }
    public function addPrice(Request $request){
       // return $request;
        $product = Product::where('product_id',$request->product_id)->first();
        if($request->sale_price != 0 && $request->sale_price != null){
            $product->sale_price = $request->sale_price;
        }

        // $product->incentive_price = $request->intentive_price ? $request->intentive_price : $product->incentive_price;
        $product->save();
        $price_date=explode('-', $request->price_date);
        $priceHistory= new PriceHistory;
        $priceHistory->history_date = $price_date[2].'-'.$price_date[1].'-'.$price_date[0];
        $priceHistory->product_id = $product->product_id;
        $priceHistory->sale_price = $request->sale_price ? $request->sale_price : 0;
        $priceHistory->intentive_price = $request->intentive_price ? $request->intentive_price : 0;
        // $priceHistory->created_by = auth()->user()->user_id;
        $priceHistory->save();
        return $product->product_id;

    }
}

VaKeR 2022