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 :  /proc/thread-self/root/home/vapecompany/demo.vapecompany.com.bd/app/Http/Controllers/Backend/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //proc/thread-self/root/home/vapecompany/demo.vapecompany.com.bd/app/Http/Controllers/Backend/ReportController.php
<?php

namespace App\Http\Controllers\Backend;

use App\Models\Category;
use Illuminate\Http\Request;
use App\Models\Product;
use App\Models\Brand;
use App\Models\ProductModel;
use App\Models\Supplier;
use App\Models\Chalan;
use App\Models\ChalanDetails;
use App\Models\ProductImei;
use App\Models\Stock;
use App\Models\Color;
use App\Models\Division;
use App\Models\Customer;
use App\Models\Designation;
use App\Models\PriceHistory;
use App\Models\SalesTarget;
use Barryvdh\DomPDF\Facade as PDF;
use Meneses\LaravelMpdf\Facades\LaravelMpdf as mPDF;
use Carbon\Carbon;
use DB;
use App\Exports\SaleImei;
use App\Exports\Sale;
use App\Exports\CustomerReport;
use App\Exports\SaleSummary;
use App\Exports\SaleProductWise;
use App\Exports\targetExport;
use App\Exports\priceHistoryExport;
use Maatwebsite\Excel\Facades\Excel;
use File;
use App\Http\Controllers\Controller;

class ReportController extends Controller
{
    // public function __construct()
    // {
    //     $this->middleware('auth');
    //     //ini_set("pcre.backtrack_limit", "10000000");

    // }
    function emp_target_achievement(Request $request){
        // if(!auth()->user()->canEvent('emp_target_achievement'))
        // {
        //     return redirect('/');
        // }
        $designation=$request->get('designation');
        $employee=$request->get('employee');
        if($request->ajax()){
            $con=array();
            if($request->get('employee')){
                $con['emplyoees.employee_id']=$request->get('employee');
            }
            if($request->get('designation')){
                $con['emplyoees.designation_id']=$request->get('designation');
            }
            $start_date=$request->get('startyear')*100+$request->get('startmonth');
            $end_date=$request->get('year')*100+$request->get('month');
            $sales_target= SalesTarget::leftJoin('emplyoees','sales_targets.employee_id','emplyoees.employee_id')
            ->select('sales_targets.*')
            ->whereBetween('sales_targets.month', [$start_date, $end_date])->where($con)->get();

             return view('backend.reports.tables.target',['employee'=>$employee,'designation_id'=>$designation,'sales_target'=>$sales_target,'startyear'=>$request->get('startyear'),'startmonth'=>$request->get('startmonth'),'year'=>$request->get('year'),'month'=>$request->get('month')]);

        }
        $start_date=Carbon::now()->format('Y')*100+Carbon::now()->format('m');
            //$end_date=$request->get('year')*100+$request->get('month');
        $sales_target= SalesTarget::leftJoin('emplyoees','sales_targets.employee_id','emplyoees.employee_id')
             ->select('sales_targets.*')
            ->whereBetween('sales_targets.month', [$start_date, $start_date])->get();
        $designations=Designation::where('status',1)->get();
        return  view('backend.reports.target_vs_achievement',['employee'=>$employee,'designation_id'=>$designation,'designations'=>$designations,'sales_target'=>$sales_target,'startyear'=>Carbon::now()->format('Y'),'startmonth'=>Carbon::now()->format('m'),'year'=>Carbon::now()->format('Y'),'month'=>Carbon::now()->format('m')]);
    }
    public function priceHistory(Request $request){

        $product=$request->get('product');
        $brand_id=$request->get('brand');
        if($request->ajax()){
            $con=array();
            $start_date = $request->get('s_date');
            $end_date = $request->get('e_date');
            if($request->get('product')){
                $con['products.product_id']=$request->get('product');
            }
            if($request->get('brand')){
                $con['products.brand_id']=$request->get('brand');
            }
            $priceHistorys=PriceHistory::join('products','price_histories.product_id','products.product_id')
            ->where($con)
           ->whereBetween('price_histories.history_date', [$start_date, $end_date])
            ->get();
            return  view('backend.reports.tables.price_history_table',['priceHistorys'=>$priceHistorys,'s_date'=>$start_date,'e_date'=>$end_date,'product'=>$product,'brand_id'=>$brand_id]);
        }
        $start_date=Carbon::now()->firstOfMonth()->format('Y-m-d');
        $end_date=Carbon::now()->format('Y-m-d');

        $priceHistorys=PriceHistory::join('products','price_histories.product_id','products.product_id')
           ->whereBetween('price_histories.history_date', [$start_date, $end_date])
            ->get();

        $brands=Brand::where('status',1)->get();
        $product=Product::where('status',1)->get();
       // dd($brand_id);
        return  view('backend.reports.price_history',['priceHistorys'=>$priceHistorys,'brands'=>$brands,'s_date'=>$start_date,'e_date'=>$end_date,'product'=>$product,'brand_id'=>$brand_id]);

    }
    public function sales(Request $request)
    {
//        if(!auth()->user()->canEvent('reports/sales'))
//        {
//            return redirect('/');
//        }
        $colors=Color::where('status',1)->get();
        $customer=$request->get('customer');
        $district=$request->get('district');
        $division_id=$request->get('division');
        $employee=$request->get('employee');
        $emp_status=$request->get('emp_status');
        $designation_id=$request->get('designation');
        $product=$request->get('product');
        $brand_id=$request->get('brand');
        if($request->ajax()){
            $con=array();
            $start_date = $request->get('s_date');
            $end_date = $request->get('e_date');
            if($request->get('customer')){
                $con['customers.customer_id']=$request->get('customer');
            }
            if(auth()->user()->employee->user_type != 3){
                if($request->get('district')){
                    $con['customers.district_id']=$request->get('district');
                }
                if($request->get('division')){
                    $con['customers.division_id']=$request->get('division');
                }
                if($request->get('employee')){
                    $con['emplyoees.employee_id']=$request->get('employee');
                }
                if($request->get('emp_status')){
                    $con['emplyoees.emp_status']=$request->get('emp_status');
                }
                if($request->get('designation')){
                    $con['emplyoees.designation_id']=$request->get('designation');
                }
            }
            if($request->get('product')){
                $con['products.product_id']=$request->get('product');
            }
            if($request->get('brand')){
                $con['products.brand_id']=$request->get('brand');
            }

            $end_date = $end_date ? $end_date : Chalan::latest()->pluck('chalan_date')->first();
            if(auth()->user()->employee->user_type == 3){
                  $chalans=ChalanDetails::leftJoin('chalans','chalan_details.chalan_id','chalans.chalan_id')
                ->leftJoin('customers','chalans.customer_id','customers.customer_id')
                ->leftJoin('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
                ->leftJoin('products','chalan_details.product_id','products.product_id')
                ->where('emplyoees.district_id',auth()->user()->employee->district_id)
                ->where($con)
                ->whereBetween('chalan_details.chalan_date', [$start_date, $end_date])
                ->get()->groupBy('chalan_id');
                return view('backend.reports.tables.sales_report_table',['colors'=>$colors,'chalans'=>$chalans,'s_date'=>$start_date,'e_date'=>$end_date,'customer'=>$customer,'product'=>$product,'brand_id'=>$brand_id]);
            }else{
                $chalans=ChalanDetails::leftJoin('chalans','chalan_details.chalan_id','chalans.chalan_id')
                ->leftJoin('customers','chalans.customer_id','customers.customer_id')
                ->leftJoin('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
                ->leftJoin('products','chalan_details.product_id','products.product_id')
                ->where($con)
                ->whereBetween('chalan_details.chalan_date', [$start_date, $end_date])
                ->get()->groupBy('chalan_id');
               // return $con;
                return view('backend.reports.tables.sales_report_table',['colors'=>$colors,'chalans'=>$chalans,'s_date'=>$start_date,'e_date'=>$end_date,'customer'=>$customer,'district'=>$district,'division_id'=>$division_id,'emp_status'=>$emp_status,'employee'=>$employee,'designation_id'=>$designation_id,'product'=>$product,'brand_id'=>$brand_id]);
            }
        }
        $start_date=Carbon::now()->firstOfMonth()->format('Y-m-d');
        $end_date=Carbon::now()->format('Y-m-d');
        //$chalans=ChalanDetails::whereBetween('chalan_date', [$start_date, $end_date])->get()->groupBy('chalan_id');
        if(auth()->user()->employee->user_type == 3){
             $chalans=ChalanDetails::join('chalans','chalan_details.chalan_id','chalans.chalan_id')
            ->join('customers','chalans.customer_id','customers.customer_id')
            ->join('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
            ->join('products','chalan_details.product_id','products.product_id')
            ->where('emplyoees.district_id',auth()->user()->employee->district_id)
           ->whereBetween('chalan_details.chalan_date', [$start_date, $end_date])
            ->get()->groupBy('chalan_id');
            $brands=Brand::get();
            $customers=Customer::where('district_id',auth()->user()->employee->district_id)->where('status',1)->get();
            return  view('backend.reports.sales',['customer'=>$customer,'customers'=>$customers,'brands'=>$brands,'colors'=>$colors,'chalans'=>$chalans,'s_date'=>$start_date,'e_date'=>$end_date,'product'=>$product,'brand_id'=>$brand_id]);
        }else{
        $chalans=ChalanDetails::join('chalans','chalan_details.chalan_id','chalans.chalan_id')
            ->join('customers','chalans.customer_id','customers.customer_id')
            ->join('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
            ->join('products','chalan_details.product_id','products.product_id')
           ->whereBetween('chalan_details.chalan_date', [$start_date, $end_date])
            ->get()->groupBy('chalan_id');
        //$chalan=Chalan::first();

        //dd($chalans);
        $divisions=Division::where('status',1)->get();
        $brands=Brand::where('status',1)->get();

        $designations=Designation::where('status',1)->get();
        return  view('backend.reports.sales',['brands'=>$brands,'colors'=>$colors,'designations'=>$designations,'divisions'=>$divisions,'chalans'=>$chalans,'s_date'=>$start_date,'e_date'=>$end_date,'customer'=>$customer,'district'=>$district,'division_id'=>$division_id,'emp_status'=>$emp_status,'employee'=>$employee,'designation_id'=>$designation_id,'product'=>$product,'brand_id'=>$brand_id]);
        }
    }
    public function customerReport(Request $request)
    {
//        if(!auth('admin')->user()->canEvent('reports/sales'))
//        {
//            return redirect('/');
//        }
        $colors=Color::where('status',1)->get();
        $customer=$request->get('customer');
        $district=$request->get('district');
        $division_id=$request->get('division');
        $employee=$request->get('employee');
        $emp_status=$request->get('emp_status');
        $designation_id=$request->get('designation');
        $product=$request->get('product');
        $brand_id=$request->get('brand');
        if($request->ajax()){
            $con=array();
            $start_date = $request->get('s_date');
            $end_date = $request->get('e_date');

            $end_date = $end_date ? $end_date : Chalan::latest()->pluck('chalan_date')->first();

                $chalans=chalan::whereBetween('chalan_date', [$start_date, $end_date])
                ->where('customer_id',$request->get('customer'))->get();
                // leftJoin('chalans','chalan_details.chalan_id','chalans.chalan_id')
                // ->leftJoin('customers','chalans.customer_id','customers.customer_id')
                // ->leftJoin('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
                // ->leftJoin('products','chalan_details.product_id','products.product_id')
                // ->where($con)
                // ->whereBetween('chalan_details.chalan_date', [$start_date, $end_date])
                // ->get()->groupBy('chalan_id');
               //return $chalans;
                return view('backend.reports.tables.customer_report',['colors'=>$colors,'chalans'=>$chalans,'s_date'=>$start_date,'e_date'=>$end_date,'customer'=>$customer,'district'=>$district,'division_id'=>$division_id,'emp_status'=>$emp_status,'employee'=>$employee,'designation_id'=>$designation_id,'product'=>$product,'brand_id'=>$brand_id]);

        }
        $start_date=Carbon::now()->firstOfMonth()->format('Y-m-d');
        $end_date=Carbon::now()->format('Y-m-d');
        //$chalans=ChalanDetails::whereBetween('chalan_date', [$start_date,

        //$chalan=Chalan::first();

        //dd($chalans);
        $divisions=Division::where('status',1)->get();
        $brands=Brand::where('status',1)->get();

        $designations=Designation::where('status',1)->get();
        return  view('backend.reports.cutomer_report',['brands'=>$brands,'colors'=>$colors,'designations'=>$designations,'divisions'=>$divisions,'s_date'=>$start_date,'e_date'=>$end_date,'customer'=>$customer,'district'=>$district,'division_id'=>$division_id,'emp_status'=>$emp_status,'employee'=>$employee,'designation_id'=>$designation_id,'product'=>$product,'brand_id'=>$brand_id]);

    }

    public function saleImei(Request $request)
    {
        //dd("hi");
//        if(!auth()->user()->canEvent('reports/sale_iemi'))
//        {
//            return redirect('/');
//        }
        $colors=Color::get();
        $customer=$request->get('customer');
        $district=$request->get('district');
        $division=$request->get('division');
        $employee=$request->get('employee');
        $emp_status=$request->get('emp_status');
        $designation=$request->get('designation');
        $product=$request->get('product');
        $category=$request->get('category');
        if($request->ajax()){
            $con=array();
            $start_date = $request->get('s_date');
            $end_date = $request->get('e_date');
            if($request->get('customer')){
                $con['customers.customer_id']=$request->get('customer');
            }
            if($request->get('district')){
                $con['customers.district_id']=$request->get('district');
            }
            if($request->get('division')){
                $con['customers.division_id']=$request->get('division');
            }
            if($request->get('employee')){
                $con['emplyoees.employee_id']=$request->get('employee');
            }
            if($request->get('emp_status')){
                $con['emplyoees.emp_status']=$request->get('emp_status');
            }
            if($request->get('designation')){
                $con['emplyoees.designation_id']=$request->get('designation');
            }
            if($request->get('product')){
                $con['products.product_id']=$request->get('product');
            }
            if($request->get('category')){
                $con['products.category_id']=$request->get('category');
            }

            $end_date = $end_date ? $end_date : Carbon::now()->format('Y-m-d');
            $product_imeis=ProductImei::Join('chalan_details','product_imeis.chalan_details_id','chalan_details.chalan_details_id')
            ->leftJoin('chalans','chalan_details.chalan_id','chalans.chalan_id')
            ->leftJoin('customers','chalans.customer_id','customers.customer_id')
            ->leftJoin('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
            ->leftJoin('products','chalan_details.product_id','products.product_id')
            ->where($con)
            ->whereBetween('chalan_details.chalan_date', [$start_date, $end_date])
            ->get();
            //return $product_imeis->chunk(20)[0][0]->chalan_details;
            // $chalans=ChalanDetails::leftJoin('chalans','chalan_details.chalan_id','chalans.chalan_id')
            // ->leftJoin('customers','chalans.customer_id','customers.customer_id')
            // ->leftJoin('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
            // ->leftJoin('products','chalan_details.product_id','products.product_id')
            // ->where($con)
            // ->whereBetween('chalan_details.chalan_date', [$start_date, $end_date])
            // ->get()->groupBy('chalan_id');
            //return $con;
            return view('backend.reports.tables.sale_imei_table',['colors'=>$colors,
                'product_imeis'=>$product_imeis,
                's_date'=>$start_date,
                'e_date'=>$end_date,
                'customer'=>$customer,
                'district'=>$district,
                'division_id'=>$division,
                'employee'=>$employee,
                'emp_status'=>$emp_status,
                'designation_id'=>$designation,
                'product'=>$product,
                'category_id'=>$category]);
        }
        $start_date=Carbon::now()->firstOfMonth()->format('Y-m-d');
        $end_date=Carbon::now()->format('Y-m-d');
        // $chalans=ChalanDetails::whereBetween('chalan_date', [$start_date, $end_date])->get()->groupBy('chalan_id');
         //$product_imeis=ProductImei::Join('chalan_details','product_imeis.chalan_details_id','chalan_details.chalan_details_id')
        // ->whereBetween('chalan_details.chalan_date', [$start_date, $end_date])->get();
        // dd($product_imeis);
       $product_imeis=ProductImei::Join('chalan_details','product_imeis.chalan_details_id','chalan_details.chalan_details_id')
            ->leftJoin('chalans','chalan_details.chalan_id','chalans.chalan_id')
            ->leftJoin('customers','chalans.customer_id','customers.customer_id')
            ->leftJoin('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
            ->leftJoin('products','chalan_details.product_id','products.product_id')

            ->whereBetween('chalan_details.chalan_date', [$start_date, $end_date])
            ->get();
        //$chalan=Chalan::first();

        //dd($chalans);
        $divisions=Division::where('status',1)->get();
        $categories=Category::where('status',1)->get();

        $designations=Designation::where('status',1)->get();
        return  view('backend.reports.sale_iemi',['categories'=>$categories,
            'colors'=>$colors,
            'designations'=>$designations,
            'divisions'=>$divisions,
            'product_imeis'=>$product_imeis,
            's_date'=>$start_date,
            'e_date'=>$end_date,
            'customer'=>$customer,
            'district'=>$district,
            'division_id'=>$division,
            'emp_status'=>$emp_status,
            'employee'=>$employee,
            'designation_id'=>$designation,
            'product'=>$product,
            'category_id'=>$category]);
    }

    public function salesStatement(Request $request)
    {
        $customer=$request->get('customer');
        $district=$request->get('district');
        $division=$request->get('division');
        $employee=$request->get('designation');
        $designation=$request->get('customer');
        $colors=Color::where('status',1)->get();
        if($request->ajax()){
            $con=array();
            $start_date = $request->get('s_date');
            $end_date = $request->get('e_date');
            if($request->get('customer')){
                $con['customers.customer_id']=$request->get('customer');
            }
            if($request->get('district')){
                $con['customers.district_id']=$request->get('district');
            }
            if($request->get('division')){
                $con['customers.division_id']=$request->get('division');
            }
            if($request->get('employee')){
                $con['emplyoees.employee_id']=$request->get('employee');
            }
            if($request->get('designation')){
                $con['emplyoees.designation_id']=$request->get('designation');
            }

            $end_date = $end_date ? $end_date : Chalan::latest()->pluck('chalan_date')->first();

            $chalans=Chalan::join('customers','chalans.customer_id','customers.customer_id')
            ->join('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
            ->where($con)
            ->whereBetween('chalan_date', [$start_date, $end_date])
            ->get();

            //return $input;
            return view('backend.reports.tables.sale_statement_table',['colors'=>$colors,'chalans'=>$chalans,'s_date'=>$start_date,'e_date'=>$end_date,'customer'=>$customer,'district'=>$district,'division'=>$division,'employee'=>$employee,'designation'=>$designation]);
        }
        $start_date=Carbon::now()->firstOfMonth()->format('Y-m-d');
        $end_date=Carbon::now()->format('Y-m-d');
        $chalans=ChalanDetails::whereBetween('chalan_date', [$start_date, $end_date])->get()->groupBy('chalan_id');
        //$chalan=Chalan::first();
        //dd($chalans);
        $divisions=Division::where('status',1)->get();
        $designations=Designation::where('status',1)->get();
        return  view('backend.reports.sale_statement',['colors'=>$colors,'designations'=>$designations,'divisions'=>$divisions,'chalans'=>$chalans,'s_date'=>$start_date,'e_date'=>$end_date,'customer'=>$customer,'district'=>$district,'division'=>$division,'employee'=>$employee,'designation'=>$designation]);
    }
    public function salesSummery(Request $request)
    {
//        if(!auth()->user()->canEvent('reports/sales_summery'))
//        {
//            return redirect('/');
//        }
       $customer=$request->get('customer');
        $district=$request->get('district');
        $division=$request->get('division');
        $designation=$request->get('designation');
        $employee=$request->get('employee');
        $emp_status=$request->get('emp_status');
        if($request->ajax()){
            //return $request;
            $start_date = $request->get('s_date');
            $end_date = $request->get('e_date');
            $end_date = $end_date ? $end_date : Carbon::now()->format('Y-m-d');
            $con=array();
            if($request->get('customer')){
                $con['customers.customer_id']=$request->get('customer');
            }
            if($request->get('district')){
                $con['customers.district_id']=$request->get('district');
            }
            if($request->get('division')){
                $con['customers.division_id']=$request->get('division');
            }
            if($request->get('employee')){
                $con['emplyoees.employee_id']=$request->get('employee');
            }
            if($request->get('emp_status')){
                $con['emplyoees.emp_status']=$request->get('emp_status');
            }
            if($request->get('designation')){
                $con['emplyoees.designation_id']=$request->get('designation');
            }

            $sells=Chalan::join('customers','chalans.customer_id','customers.customer_id')
            ->join('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
            ->where($con)
            ->whereBetween('chalan_date', [$start_date, $end_date])->get()->groupBy(function($data) use ($request) {
                return Carbon::parse($data->chalan_date)->format($request->by_duration);
            });
            //return $sells;
            return view('backend.reports.tables.sale_summary',['sells'=>$sells,'s_date'=>$start_date,'e_date'=>$end_date,'by_duration'=>$request->by_duration,'customer'=>$customer,'district'=>$district,'division_id'=>$division,'emp_status'=>$emp_status,'employee'=>$employee,'designation_id'=>$designation]);
        }
        $start_date=Carbon::now()->firstOfMonth()->format('Y-m-d');
        $end_date=Carbon::now()->format('Y-m-d');
        $sells=Chalan::whereBetween('chalan_date', [$start_date, $end_date])->get()->groupBy(function($data) use ($request) {
                return Carbon::parse($data->chalan_date)->format($request->by_duration);
            });
        $divisions=Division::where('status',1)->get();
        $designations=Designation::where('status',1)->get();
        return  view('backend.reports.sale_summary',['designations'=>$designations,'divisions'=>$divisions,'sells'=>$sells,'s_date'=>$start_date,'e_date'=>$end_date,'by_duration'=>$request->by_duration,'customer'=>$customer,'district'=>$district,'division_id'=>$division,'emp_status'=>$emp_status,'employee'=>$employee,'designation_id'=>$designation]);
    }
    public function productWiseSales(Request $request)
    {
//        if(!auth()->user()->canEvent('reports/product_wise_sales'))
//        {
//            return redirect('/');
//        }
       $category_id=$request->get('category');
        $product=$request->get('product');
        $model=$request->get('model_id');
        $emp_status=$request->get('emp_status');

        if($request->ajax()){
           // return $request;
            $start_date = $request->get('start_date') ? $request->get('start_date'):Carbon::now()->startOfMonth()->format('Y-m-d');
            $end_date = $request->get('end_date') ? $request->get('end_date') :Carbon::now()->format('Y-m-d');
          $con=array();
            if($request->get('category')){
                $con['category_id']=$request->get('category');
            }
            if($request->get('product')){
                $con['product_id']=$request->get('product');
            }
            if($request->get('model')){
                $con['model_id']=$request->get('model');
            }
            if($request->get('emp_status')){
                $con['emp_status']=$request->get('emp_status');
            }

             $sell_products=ChalanDetails::whereBetween('chalan_details.chalan_date', [$start_date, $end_date])
                        ->join('chalans','chalans.chalan_id','chalan_details.chalan_id')
                        ->join('emplyoees','chalans.sales_user_id' , 'emplyoees.employee_id')
                        //->where('emplyoees.emp_status',$emp_status)
                        ->where($con)
                        ->get()->groupBy('chalan_details.chalan_id');
                     //   return $sell_products;
             //ChalanDetails::where($con)->whereBetween('chalan_date', [$start_date, $end_date])->get()->groupBy('chalan_id');

            return view('backend.reports.tables.product_wise_sales_table',['sell_products'=>$sell_products,'s_date'=>$start_date,'e_date'=>$end_date,'product'=>$product,'category_id'=>$category_id,'model_id'=>$model,'emp_status'=>$emp_status]);
       }
       $start_date=Carbon::now()->firstOfMonth()->format('Y-m-d');
        $end_date=Carbon::now()->format('Y-m-d');
       $sell_products=ChalanDetails::whereBetween('chalan_details.chalan_date', [$start_date, $end_date])
                        ->join('chalans','chalans.chalan_id','chalan_details.chalan_id')
                        ->join('emplyoees','chalans.sales_user_id' , 'emplyoees.employee_id')
                        ->where('emplyoees.emp_status',1)
                        ->get()->groupBy('chalan_details.chalan_id');
      // dd($sell_products);
     $emp_status=1;
       $categories=Category::where('status',1)->get();
        return  view('backend.reports.product_wise_sales',['categories'=>$categories,'sell_products'=>$sell_products,'s_date'=>$start_date,'e_date'=>$end_date,'category_id'=>$category_id,'product'=>$product,'model_id'=>$model,'emp_status'=>$emp_status]);
    }
     public function priceHistoryPdf(Request $request)
    {

         //dd($request);
         $s_date=$request->s_date;
         $e_date=$request->e_date;
         $con=array();
        if($request->customer){
            $con['customers.customer_id']=$request->customer;
        }

        if($request->brand){
            $con['products.brand_id']=$request->brand;
        }
        if($request->product){
            $con['products.product_id']=$request->product;
        }

        $priceHistorys=PriceHistory::join('products','price_histories.product_id','products.product_id')
            ->where($con)
           ->whereBetween('price_histories.history_date', [$s_date, $e_date])
            ->get();
          // return $priceHistorys;
        $data = [
            'priceHistorys' => $priceHistorys,
            'request' => $request,
            'e_date' => $e_date,
            's_date' => $s_date,
        ];
       // dd($priceHistorys);
       //return view('pdf.reports.sells.sells', compact('chalans', 'request','e_date','s_date','colors'));
        $pdf = mPDF::loadView('backend.pdf.reports.sells.price_history', $data,[],[
          'orientation' => 'L',
          'format' => 'Legal'
        ]);

        // $pdf = PDF::loadView('pdf.reports.sells.sells', compact('chalans', 'request','e_date','s_date','colors'))->setPaper('a4');

        if ($request->action_type == 'download'){
            //->save($pdfFilePath);
            return $pdf->download('price-history-' . Carbon::now()->format('yyyy-mm-dd'). '.pdf');
        }elseif($request->action_type == 'print'){

            return $pdf->stream('price-history.pdf');
        }
        else{

            return Excel::download(new priceHistoryExport($s_date, $e_date,$priceHistorys), 'price-history.xlsx');

        }

    }
    public function targetPdf(Request $request)
    {

         //dd($request);
        $s_date=$request->get('startyear');
        $e_date=$request->get('startmonth');
        $ss_date=$request->get('year');
        $ee_date=$request->get('month');
         $con=array();
        if($request->get('employee')){
            $con['emplyoees.employee_id']=$request->get('employee');
        }
        if($request->get('designation')){
            $con['emplyoees.designation_id']=$request->get('designation');
        }
        $start_date=$request->get('startyear')*100+$request->get('startmonth');
        $end_date=$request->get('year')*100+$request->get('month');
        $sales_target= SalesTarget::leftJoin('emplyoees','sales_targets.employee_id','emplyoees.employee_id')
        ->select('sales_targets.*')
        ->whereBetween('sales_targets.month', [$start_date, $end_date])->where($con)->get();



        if ($request->action_type == 'download'){
            $data = [
                'sales_target' => $sales_target,
                'request' => $request,
                'e_date' => $e_date,
                's_date' => $s_date,
                 'ee_date' => $ee_date,
                'ss_date' => $ss_date,
            ];
            $pdf = mPDF::loadView('pdf.reports.sells.target', $data,[],[
              'orientation' => 'L',
              'format' => 'Legal'
            ]);
           // $pdf = PDF::loadView('pdf.reports.sells.target', compact('sales_target', 'request','e_date','s_date','ee_date','ss_date'))->setPaper('a4');
            return $pdf->download('target-vs-achievement-' . Carbon::now()->format('yyyy-mm-dd'). '.pdf');
        }elseif($request->action_type == 'print'){
            $data = [
                'sales_target' => $sales_target,
                'request' => $request,
                'e_date' => $e_date,
                's_date' => $s_date,
                'ee_date' => $ee_date,
                'ss_date' => $ss_date,
            ];
            $pdf = mPDF::loadView('pdf.reports.sells.target', $data,[],[
              'orientation' => 'L',
              'format' => 'Legal'
            ]);
            @unlink('pdf/reports/sales/' . 'target-vs-achievement.pdf');
            $pdf->save('pdf/reports/sales/' . 'target-vs-achievement.pdf');
            return redirect('pdf/reports/sales/' . 'target-vs-achievement.pdf');
        }
        else{

           return Excel::download(new targetExport($sales_target), 'target-vs-achievement.xlsx');

        }

    }
    public function ProductWiseSalePdf(Request $request)
    {

         //dd($request);
        $s_date=$request->s_date;
        $e_date=$request->e_date;
        $con=array();
        if($request->get('brand')){
            $con['brand_id']=$request->get('brand');
        }
        if($request->get('product')){
            $con['product_id']=$request->get('product');
        }
        if($request->get('model')){
            $con['model_id']=$request->get('model');
        }
        if($request->get('emp_status')){
            $con['emp_status']=$request->get('emp_status');
        }

        $sell_products=ChalanDetails::whereBetween('chalan_details.chalan_date', [$s_date, $e_date])
                        ->join('chalans','chalans.chalan_id','chalan_details.chalan_id')
                        ->join('emplyoees','chalans.sales_user_id' , 'emplyoees.employee_id')
                        //->where('emplyoees.emp_status',$emp_status)
                        ->where($con)
                        ->get()->groupBy('chalan_details.chalan_id');

        //ChalanDetails::where($con)->whereBetween('chalan_date', [$s_date, $e_date])->get()->groupBy('chalan_id');
        //dd($sell_products);
          //return view('pdf.reports.sells.product-wise-sell-report', compact('sell_products', 'request','e_date','s_date'));
        $pdf = PDF::loadView('pdf.reports.sells.product-wise-sell-report', compact('sell_products', 'request','e_date','s_date'))->setPaper('a4');

        if ($request->action_type == 'download'){
            return $pdf->download('product-wise-sales-report-' . Carbon::now()->format('yyyy-mm-dd'). '.pdf');
        }elseif($request->action_type == 'print'){
            @unlink('pdf/reports/sales/' . 'product-wise-sales-report.pdf');
            $pdf->save('pdf/reports/sales/' . 'product-wise-sales-report.pdf');
            return redirect('pdf/reports/sales/' . 'product-wise-sales-report.pdf');
        }
        else{

           return Excel::download(new SaleProductWise($s_date, $e_date,$con), 'ProductWise Sale Report.xlsx');

        }

    }
    public function CustomerReportPdf(Request $request)
    {

         //dd($request);
         $s_date=$request->s_date;
         $e_date=$request->e_date;
         $con=array();


        $colors=Color::get();
        $chalans=chalan::whereBetween('chalan_date', [$s_date, $e_date])
                ->where('customer_id',$request->get('customer'))->get();


        $data = [
            'chalans' => $chalans,
            'request' => $request,
            'e_date' => $e_date,
            's_date' => $s_date,
            'colors' => $colors,
        ];
       //return view('pdf.reports.sells.sells', compact('chalans', 'request','e_date','s_date','colors'));
        $pdf = mPDF::loadView('pdf.reports.sells.customer_report', $data,[],[
          'orientation' => 'L',
          'format' => 'Legal'
        ]);

        // $pdf = PDF::loadView('pdf.reports.sells.sells', compact('chalans', 'request','e_date','s_date','colors'))->setPaper('a4');

        if ($request->action_type == 'download'){
            //->save($pdfFilePath);
            return $pdf->download('customer-report-' . Carbon::now()->format('yyyy-mm-dd'). '.pdf');
        }elseif($request->action_type == 'print'){

            return $pdf->stream('customer-report.pdf');
        //$pdf->save('pdf/reports/saless/' . 'sales-report.pdf');
           // return redirect('pdf/reports/sales/' . 'sales-report.pdf');
        }
        else{
           // return new CustomerReport($s_date, $e_date,$chalans);
           // return view('exports.customer_report',['chalans'=>$chalans]);
            return Excel::download(new CustomerReport($s_date, $e_date,$chalans), 'Customer Report.xlsx');

        }

    }
    public function SalesReportPdf(Request $request)
    {

         //dd($request);
         $s_date=$request->s_date;
         $e_date=$request->e_date;
         $con=array();
        if($request->customer){
            $con['customers.customer_id']=$request->customer;
        }
        if(auth()->user()->employee->user_type != 3){
                if($request->get('district')){
                    $con['customers.district_id']=$request->get('district');
                }
                if($request->get('division')){
                    $con['customers.division_id']=$request->get('division');
                }
                if($request->get('employee')){
                    $con['emplyoees.employee_id']=$request->get('employee');
                }
                if($request->get('emp_status')){
                    $con['emplyoees.emp_status']=$request->get('emp_status');
                }
                if($request->get('designation')){
                    $con['emplyoees.designation_id']=$request->get('designation');
                }
            }
        if($request->brand){
            $con['products.brand_id']=$request->brand;
        }
        if($request->product){
            $con['products.product_id']=$request->product;
        }

        $colors=Color::get();
        if(auth()->user()->employee->user_type == 3){
             $chalans=ChalanDetails::join('chalans','chalan_details.chalan_id','chalans.chalan_id')
            ->join('customers','chalans.customer_id','customers.customer_id')
            ->join('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
            ->join('products','chalan_details.product_id','products.product_id')
             ->where('emplyoees.district_id',auth()->user()->employee->district_id)
            ->where($con)
           ->whereBetween('chalan_details.chalan_date', [$s_date, $e_date])
            ->get()->groupBy('chalan_id');


            }else{
            $chalans=ChalanDetails::join('chalans','chalan_details.chalan_id','chalans.chalan_id')
            ->join('customers','chalans.customer_id','customers.customer_id')
            ->join('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
            ->join('products','chalan_details.product_id','products.product_id')
            ->where($con)
           ->whereBetween('chalan_details.chalan_date', [$s_date, $e_date])
            ->get()->groupBy('chalan_id');
            }
        // $chalans=ChalanDetails::join('chalans','chalan_details.chalan_id','chalans.chalan_id')
        //     ->join('customers','chalans.customer_id','customers.customer_id')
        //     ->join('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
        //     ->join('products','chalan_details.product_id','products.product_id')
        //     ->where($con)
        //   ->whereBetween('chalan_details.chalan_date', [$s_date, $e_date])
        //     ->get()->groupBy('chalan_id');

        $data = [
            'chalans' => $chalans,
            'request' => $request,
            'e_date' => $e_date,
            's_date' => $s_date,
            'colors' => $colors,
        ];
       //return view('pdf.reports.sells.sells', compact('chalans', 'request','e_date','s_date','colors'));
        $pdf = mPDF::loadView('pdf.reports.sells.sells', $data,[],[
  'orientation' => 'L',
  'format' => 'Legal'
]);

        // $pdf = PDF::loadView('pdf.reports.sells.sells', compact('chalans', 'request','e_date','s_date','colors'))->setPaper('a4');

        if ($request->action_type == 'download'){
            //->save($pdfFilePath);
            return $pdf->download('sales-report-' . Carbon::now()->format('yyyy-mm-dd'). '.pdf');
        }elseif($request->action_type == 'print'){

            return $pdf->stream('sales-report.pdf');
        //$pdf->save('pdf/reports/saless/' . 'sales-report.pdf');
           // return redirect('pdf/reports/sales/' . 'sales-report.pdf');
        }
        else{
          // return new Sale($s_date, $e_date,$chalans);
            return Excel::download(new Sale($s_date, $e_date,$chalans), 'Sale Report.xlsx');

        }

    }
    public function SalesImeiReportPdf(Request $request)
    {
       // ini_set('max_execution_time', '300');
        ini_set("pcre.backtrack_limit", "5000000");
//dd(ini_get('pcre.backtrack_limit'));
         //dd($request);
         $s_date=$request->s_date;
         $e_date=$request->e_date;
         $con=array();
        if($request->customer){
            $con['customers.customer_id']=$request->customer;
        }
        if($request->district){
            $con['customers.district_id']=$request->district;
        }
        if($request->division){
            $con['customers.division_id']=$request->division;
        }
        if($request->employee){
            $con['emplyoees.employee_id']=$request->employee;
        }
        if($request->emp_status){
            $con['emplyoees.emp_status']=$request->emp_status;
        }
        if($request->designation){
            $con['emplyoees.designation_id']=$request->designation;
        }
        if($request->brand){
            $con['products.brand_id']=$request->brand;
        }
        if($request->product){
            $con['products.product_id']=$request->product;
        }

        $colors=Color::get();

        // $pdf = mPDF::loadView('pdf.reports.sells.sale_iemi', $data,[],[
        //       'orientation' => 'L',
        //       'format' => 'Legal'
        //     ]);
       // $html=view('pdf.reports.sells.sale_iemi', compact('product_imeis', 'request','e_date','s_date','colors'));
        // $pdf = PDF::loadView('pdf.reports.sells.sale_iemi', compact('product_imeis', 'request','e_date','s_date','colors'))->setPaper('a4');
        // return $pdf->download('sales-imei-report-' . Carbon::now()->format('yyyy-mm-dd'). '.pdf');

        //ini_set('max_execution_time', '300');
       //ini_set("pcre.backtrack_limit", "50000000");
//        $pdf = mPDF::chunkLoadHTML('chunk',$html,[
//   'orientation' => 'L',
//   'format' => 'Legal'
// ]);
        //return $pdf;

        // $pdf=PDF::loadHTML($html)->setPaper('a4')->setOrientation('landscape')->setOption('margin-bottom', 0);
        // return $pdf->download('sales-imei-report-' . Carbon::now()->format('yyyy-mm-dd'). '.pdf');

        if ($request->action_type == 'download'){
            $product_imeis=ProductImei::Join('chalan_details','product_imeis.chalan_details_id','chalan_details.chalan_details_id')
            ->leftJoin('chalans','chalan_details.chalan_id','chalans.chalan_id')
            ->leftJoin('customers','chalans.customer_id','customers.customer_id')
            ->leftJoin('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
            ->leftJoin('products','chalan_details.product_id','products.product_id')
            ->where($con)
            ->whereBetween('chalan_details.chalan_date', [$s_date, $e_date])
            ->get();
            $html=view('pdf.reports.sells.sale_iemi', compact('product_imeis', 'request','e_date','s_date'));
            return $html;

            $data = [
                'product_imeis' => $product_imeis,
                'request' => $request,
                'e_date' => $e_date,
                's_date' => $s_date,
                'colors' => $colors,
            ];
            $pdf = mPDF::loadView('pdf.reports.sells.sale_iemi', $data,[],[
              'orientation' => 'L',
              'format' => 'Legal'
            ]);
            return $pdf->download('sales-imei-report-' . Carbon::now()->format('yyyy-mm-dd'). '.pdf');
        }elseif($request->action_type == 'print'){
            $product_imeis=ProductImei::Join('chalan_details','product_imeis.chalan_details_id','chalan_details.chalan_details_id')
            ->leftJoin('chalans','chalan_details.chalan_id','chalans.chalan_id')
            ->leftJoin('customers','chalans.customer_id','customers.customer_id')
            ->leftJoin('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
            ->leftJoin('products','chalan_details.product_id','products.product_id')
            ->where($con)
            ->whereBetween('chalan_details.chalan_date', [$s_date, $e_date])
            ->get();

            $html=view('pdf.reports.sells.sale_iemi', compact('product_imeis', 'request','e_date','s_date'));
            return $html;
            $data = [
                'product_imeis' => $product_imeis,
                'request' => $request,
                'e_date' => $e_date,
                's_date' => $s_date,
                'colors' => $colors,
            ];
            $pdf = mPDF::loadView('pdf.reports.sells.sale_iemi', $data,[],[
              'orientation' => 'L',
              'format' => 'Legal'
            ]);
            @unlink('pdf/reports/sales/' . 'sales-imei-report.pdf');
            $pdf->save('pdf/reports/sales/' . 'sales-imei-report.pdf');
            return redirect('pdf/reports/sales/' . 'sales-imei-report.pdf');
        }
        else{

            return Excel::download(new SaleImei($s_date, $e_date,$con), 'Sale IMEI Report.xlsx');

        }

    }
    public function SaleSummaryPdf(Request $request)
    {

         //dd($request);
         $s_date=$request->s_date;
         $e_date=$request->e_date;
         $con=array();
          if($request->customer){
                $con['customers.customer_id']=$request->customer;
            }
            if($request->district){
                $con['customers.district_id']=$request->district;
            }
            if($request->division){
                $con['customers.division_id']=$request->division;
            }
            if($request->employee){
                $con['emplyoees.employee_id']=$request->employee;
            }
            if($request->emp_status){
            $con['emplyoees.emp_status']=$request->emp_status;
            }
            if($request->designation){
                $con['emplyoees.designation_id']=$request->designation;
            }
          $sells=Chalan::join('customers','chalans.customer_id','customers.customer_id')
            ->join('emplyoees','chalans.sales_user_id','emplyoees.employee_id')
            ->where($con)
            ->whereBetween('chalan_date', [$s_date, $e_date])->get()->groupBy(function($data) use ($request) {
                return Carbon::parse($data->chalan_date)->format($request->by_duration);
            });
            $by_duration=$request->by_duration;
           // dd($sells)
           // return view('pdf.reports.sells.sell-summary', compact('sells', 'request','e_date','s_date','by_duration'));
        $pdf = PDF::loadView('pdf.reports.sells.sell-summary', compact('sells', 'request','e_date','s_date','by_duration'))->setPaper('a4');

        if ($request->action_type == 'download'){
            return $pdf->download('sales-summary-report-' . Carbon::now()->format('yyyy-mm-dd'). '.pdf');
        }elseif($request->action_type == 'print'){
            @unlink('pdf/reports/sales/' . 'sales-summary-report.pdf');
            $pdf->save('pdf/reports/sales/' . 'sales-summary-report.pdf');
            return redirect('pdf/reports/sales/' . 'sales-summary-report.pdf');
        }
        else{

             return Excel::download(new SaleSummary($s_date, $e_date,$request->by_duration,$con), 'Sale Summary Report.xlsx');

        }

    }


    public function stockIn(Request $request){
         //$stocks=Stock::with('details')->get();
        // dd()
        if($request->ajax()){
            $start_date = $request->get('s_date');
            $end_date = $request->get('e_date') ? $request->get('e_date') : date('Y-m-d');
            //$end_date = $end_date ? $end_date : Stock::latest()->pluck('stock_date')->first();

            $stocks=Stock::select('stock_colors.*','stocks.*')->leftJoin('stock_colors', 'stocks.stock_id', '=', 'stock_colors.stock_id')->get()->groupby('product_id');
            //return $input;
            return view('backend.reports.tables.stock_in',['stocks'=>$stocks,'s_date'=>$start_date,'e_date'=>$end_date]);
        }
        $suppliers=Supplier::get();
        $brands=Brand::get();
        $models=ProductModel::get();
        //$products=Product::get();
        return  view('backend.reports.stock_in',['suppliers'=>$suppliers,'brands'=>$brands,'models'=>$models]);
    }
    public function StockReportPdf(Request $request)
    {
        $product = $request->roduct;
        $model = $request->model;
        $brand = $request->brand;
        $con=array();

        if($product != 'all'){
           $con['barcodes.product_id'] = $product;
        }
        if($model != 'all'){
           $con['products.model_id'] = $model;
        }
        if($brand != 'all'){
           $con['products.brand_id'] = $brand;
        }

        $chalans=DB::table('barcodes')
            ->leftJoin('products','barcodes.product_id','products.product_id')
            ->leftJoin('product_models','products.model_id','product_models.model_id')
            ->leftJoin('brands','products.brand_id','brands.brand_id');
            if(!empty($con)){
               //$chalans->where($con);
            }
        $chalans=$chalans->get();
        //return $chalans;

        $pdf = PDF::loadView('pdf.reports.stock.stock', compact('chalans', 'request','product','model'))->setPaper('a4');

        if ($request->action_type == 'download'){
            return $pdf->download('stock-report-' . Carbon::now()->format('yyyy-mm-dd'). '.pdf');
        }elseif($request->action_type == 'print'){
            @unlink('pdf/reports/stock/' . 'stock-report.pdf');
            $pdf->save('pdf/reports/stock/' . 'stock-report.pdf');
            return redirect('pdf/reports/stock/' . 'stock-report.pdf');
        }elseif($request->action_type == 'csv'){

           $headers = [
                'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0'
                , 'Content-type' => 'text/csv'
                , 'Content-Disposition' => 'attachment; filename=galleries.csv'
                , 'Expires' => '0'
                , 'Pragma' => 'public'
            ];

            $filename = 'download.csv';
            $handle = fopen($filename, 'w');
            fputcsv($handle, [
                'SL',
                'Product Code',
                'Product Name',
                'Brand',
                'Model',
                'color',
                'Purchase Price',
                'Quantity',
                'Current Stock Value',
            ]);
            foreach ($chalans as $key => $chalan) {
                $p= json_decode($chalan->purchase_quantity);
                $price='';
                $quantity='';
                $totalprice=0;
                $i=0;
                foreach($p as $k=>$v){
                   if($i > 0){
                        $price .=','.$k;
                        $quantity .=','.$v;
                        $totalprice += $k*$v;
                    }else{
                        $price .=$k;
                        $quantity .=$v;
                        $totalprice += $k*$v;
                    }
                    $i++;
                }


                fputcsv($handle, [
                    $key + 1,
                    $chalan->product_code,
                    $chalan->product_name,
                    $chalan->brand_name,
                    $chalan->model_name,
                    $chalan->color,
                    $price,
                    $quantity,
                    $totalprice,
                ]);
            }

            fclose($handle);
            return response()->download($filename, 'sales-report-' . Carbon::now()->toDateString() . '.csv', $headers);

        }
        else{

            $headers = [
                'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0'
                , 'Content-type' => 'application/vnd.ms-excel'
                , 'Content-Disposition' => 'attachment; filename=galleries.csv'
                , 'Expires' => '0'
                , 'Pragma' => 'public'
            ];

              $filename = "stock-report" . date('Y-m-d') . ".xls";
            header("Content-Disposition: attachment; filename=\"$filename\"");
            header("Content-Type: application/vnd.ms-excel");

            $row="SL\tProduct Code\tProduct Namee\Brand\tModel\tColor\tPurchase Price\tQuantity\tCurrent Stock Value\n";
            foreach ($chalans as $key => $chalan) {
                $p= json_decode($chalan->purchase_quantity);
                   $price='';
                   $quantity='';
                   $totalprice=0;
                   $i=0;
                   foreach($p as $k=>$v){
                   if($i > 0){
                        $price .=','.$k;
                        $quantity .=','.$v;
                        $totalprice += $k*$v;
                    }else{
                        $price .=$k;
                        $quantity .=$v;
                        $totalprice += $k*$v;
                    }
                       $i++;
                    }
            $row .= ($key + 1)."\t".$chalan->product_code."\t".$chalan->product_name."\t".$chalan->brand_name."\t".$chalan->model_name."\t".$chalan->color."\t".$price."\t".$quantity."\t".$totalprice."\n";


            }
            echo $row;

        }

    }
}

VaKeR 2022