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/SalesTargetController.php
<?php

namespace App\Http\Controllers;

use App\Models\SalesTarget;
use App\Models\Emplyoee;
use App\Models\Product;
use App\Models\Brand;
use App\Models\SaleTargetDetail;
use App\Models\Designation;
use App\Models\Division;
use App\Models\District;
use App\Models\Region;
use App\Models\Area;
use Illuminate\Http\Request;
use DB;
use Meneses\LaravelMpdf\Facades\LaravelMpdf as mPDF;

use Maatwebsite\Excel\Facades\Excel;
use App\Exports\SaleTargetExport;
class SalesTargetController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        $this->middleware('auth');
       
    }
    public function index(Request $request)
    {
         if(!auth()->user()->canEvent('all_sales_target'))
        {
            return redirect('/');
        }
        if(isset($request->action_type)){
            $startm = $request->startyear * 100+$request->startmonth;
            $endm = $request->year * 100+$request->month;
            $con=array();
            if($request->designation)
            {
                $con['emplyoees.designation_id']=$request->designation;
            }
            if($request->employee)
            {
                $con['sales_targets.employee_id']=$request->employee;
            }
             if(auth()->user()->employee->user_type == 3){
                $sales_target = SalesTarget::join('emplyoees','emplyoees.employee_id','sales_targets.employee_id')
                ->select('sales_targets.*')
                ->where($con)
                ->where('employee_id',auth()->user()->employee->employee_id)->get();
            }else{
                $moth = \Carbon\Carbon::now('Asia/dhaka')->year * 100+\Carbon\Carbon::now('Asia/dhaka')->month;
                $sales_target = $sales_target = SalesTarget::join('emplyoees','emplyoees.employee_id','sales_targets.employee_id')
                ->select('sales_targets.*')
                ->where($con)
                ->whereBetween('month',[$startm,$endm])->get();
            }

            if($request->action_type == 'pdf'){
                 $data = [
                    'sales_target' => $sales_target,
                    'request' => $request,
                ];
                $pdf = mPDF::loadView('pdf.list.sales_target_list', $data,[],[
                  'orientation' => 'L',
                  'format' => 'Legal'
                ]);
                //->save($pdfFilePath);
                return $pdf->download('Sales-Target-List-' . \Carbon\Carbon::now()->format('yyyy-mm-dd'). '.pdf');
            }elseif($request->action_type == 'print'){
                 $data = [
                    'sales_target' => $sales_target,
                    'request' => $request,
                ];
                $pdf = mPDF::loadView('pdf.list.sales_target_list', $data,[],[
                  'orientation' => 'L',
                  'format' => 'Legal'
                ]);
                return $pdf->stream('Sales-Target-List-.pdf');
            }
            else{
                //return new SaleTargetExport($sales_target);
               // return view('exports.sales_target_list', ['sales_target' => $sales_target]);
                return Excel::download(new SaleTargetExport($sales_target), 'Sales-Target-List-.xlsx');

            }
        }
        if($request->ajax()){
            $startm = $request->startyear * 100+$request->startmonth;
            $endm = $request->year * 100+$request->month;
            $con=array();
            if($request->designation)
            {
                $con['emplyoees.designation_id']=$request->designation;
            }
            if($request->employee)
            {
                $con['sales_targets.employee_id']=$request->employee;
            }
             if(auth()->user()->employee->user_type == 3){
                $sales_target = SalesTarget::join('emplyoees','emplyoees.employee_id','sales_targets.employee_id')
                ->select('sales_targets.*')
                ->where($con)
                ->where('employee_id',auth()->user()->employee->employee_id)->get();
            }else{
                $moth = \Carbon\Carbon::now('Asia/dhaka')->year * 100+\Carbon\Carbon::now('Asia/dhaka')->month;
                $sales_target = $sales_target = SalesTarget::join('emplyoees','emplyoees.employee_id','sales_targets.employee_id')
                ->select('sales_targets.*')
                ->where($con)
                ->whereBetween('month',[$startm,$endm])->get();
            }
          // return $sales_target;
           
                return view('sales_target.table_target',['sales_target'=>$sales_target]);
           
               

           
            
        }else{
            $moth = \Carbon\Carbon::now('Asia/dhaka')->year * 100+\Carbon\Carbon::now('Asia/dhaka')->month;
            if(auth()->user()->employee->user_type == 3){
                $sales_target = SalesTarget::where('month',$moth)->where('employee_id',auth()->user()->employee->employee_id)->get();
            }else{
                
                $sales_target = SalesTarget::where('month',$moth)->get();
            }
        }
        
        
        $designations=Designation::where('status',1)->get();
     
        return view('sales_target.list',['sales_target'=>$sales_target,'designations'=>$designations]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if(!auth()->user()->canEvent('new_sales_target'))
        {
            return redirect('/');
        }
        $employees = Emplyoee::get();
        $designations = Designation::get();
        $brands = Brand::get();
        $regions=Region::get();
        $divisions = Division::where('status',1)->get();
        return view('sales_target.create',['divisions'=>$divisions,'designations'=>$designations,'employees' => $employees,'brands'=>$brands,'regions'=>$regions]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //return $request->all();
        $validator = \Validator::make($request->all(), [
            'employee' => 'required',
           
           
        ]);
        
        if ($validator->fails())
        {
            return response()->json(['errors'=>$validator->errors()->all()]);
        }else{
           //return  response()->json(['re'=> auth()->user()->user_id]);
            $moth = $request->year * 100+$request->month;
            //return $moth;
            $customers = SalesTarget::where('employee_id',$request->employee)->where('month',$moth)->first();
            if($customers){
                $salesD=SaleTargetDetail::where('sale_target_id',$customers->sale_target_id)->whereIn('product_id',$request->product_id)->get();
                //return $salesD->count();
                if($salesD->count() > 0){
                    return response()->json(['errors'=>['eroor' => 'This data already Exists']]);
                }
                try {
                    DB::beginTransaction();
                    foreach($request->product_id as $k=>$v){
                        $salesTargetDetails =  new SaleTargetDetail;
                        $salesTargetDetails->sale_target_id = $customers->sale_target_id;
                        $salesTargetDetails->product_id = $v; 
                        $salesTargetDetails->target_quantity = $request->target_qty[$k];
                        $salesTargetDetails->target_amount = $request->target_val[$k]; 
                        
                        $salesTargetDetails->created_by = auth()->user()->user_id;
                        $salesTargetDetails->save();
                    }
                    DB::commit();
                } catch (\PDOException $e) {
                    // Woopsy
                    DB::rollBack();
                    return response()->json(['errors'=>$e]);
                }
            }else{
                try {
                DB::beginTransaction();
                $salesTarget =  new SalesTarget;
                $salesTarget->employee_id = $request->employee;
                $salesTarget->designation_id = $request->designation;
                $m=str_split($moth,4);
                if(isset($m[1]) == false){
                    return response()->json(['errors'=>'Try Againg, Something is Wrong']);
                }
                $salesTarget->month = $moth;
                $salesTarget->region_id = $request->region;
                //$salesTarget->division_id = $request->division;  
                $salesTarget->area =json_encode($request->area);
                $salesTarget->district =json_encode($request->district);
                $salesTarget->division_id =json_encode($request->division);
                $salesTarget->created_by = auth()->user()->user_id;
                $salesTarget->save();
                $uid=$salesTarget->sale_target_id ;
                foreach($request->product_id as $k=>$v){
                    $salesTargetDetails =  new SaleTargetDetail;
                    $salesTargetDetails->sale_target_id = $uid;
                    $salesTargetDetails->product_id = $v; 
                    $salesTargetDetails->target_quantity = $request->target_qty[$k];
                    $salesTargetDetails->target_amount = $request->target_val[$k]; 
                    
                    $salesTargetDetails->created_by = auth()->user()->user_id;
                    $salesTargetDetails->save();
                }
                     DB::commit();
                } catch (\PDOException $e) {
                    // Woopsy
                    DB::rollBack();
                    return response()->json(['errors'=>$e]);
                }
                
            }
        }
        return response()->json(['errors'=>'0','success'=>'Record is successfully added']);
    }

    function uniquemonthemployee($emp_id,$month){
            $customers = SalesTarget::where('employee_id',$emp_id)->where('month',$month)->get()->count();
            return  $customers;
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\SalesTarget  $salesTarget
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
         if(!auth()->user()->canEvent('new_sales_target'))
        {
            return redirect('/');
        }
        $employees = Emplyoee::where('emp_status',1)->get();
        $designations = Designation::where('status',1)->get();
        $brands = Brand::where('status',1)->get();
        $regions=Region::where('status',1)->get();
        $divisions=Division::where('status',1)->get();

        $sales_target = SalesTarget::where('sale_target_id',$id)->first();
        //dd(json_decode($sales_target->division_id));
        $districts=District::whereIn('division_id',json_decode($sales_target->division_id))->where('status',1)->get();
        $areas=Area::whereIn('district_id',json_decode($sales_target->district))->get();
        //dd($districts);
        return view('sales_target.edit',['districts'=>$districts,'designations'=>$designations,'employees' => $employees,'brands'=>$brands,'regions'=>$regions,'divisions'=>$divisions,'sales_target'=>$sales_target,'areas'=>$areas]);
    }
    public function targetEnterView($id)
    {
         if(!auth()->user()->canEvent('new_sales_target'))
        {
            return redirect('/');
        }
       
        $sales_target = SalesTarget::where('sale_target_id',$id)->first();
        //dd($sales_target->area);
        return view('sales_target.target_enter',['sales_target'=>$sales_target]);
    }

    function targetEnter(Request $request){
       //return $request->sale_target_id;
         try {
                DB::beginTransaction();
        foreach($request->product_id as $k=>$v){
                $salesTargetDetails=SaleTargetDetail::where('sale_target_id',$request->sale_target_id)->where('product_id',$v)->first();

                $salesTargetDetails->sales_quantity = $request->target_qty[$v];
                $salesTargetDetails->sales_amount = $request->target_val[$v]; 
                    
                $salesTargetDetails->updated_by = auth()->user()->user_id;
                $salesTargetDetails->save();
            }
         DB::commit();
        } catch (\PDOException $e) {
            // Woopsy
            DB::rollBack();
            return response()->json(['errors'=>$e]);
        } 
        return response()->json(['errors'=>'0','success'=>'Record is successfully added']);   
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\SalesTarget  $salesTarget
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        
        //return $request;
       $validator = \Validator::make($request->all(), [
            'employee' => 'required',
           
           
        ]);
        
        if ($validator->fails())
        {
            return response()->json(['errors'=>$validator->errors()->all()]);
        }else{
           //return  response()->json(['re'=> auth()->user()->user_id]);
            $moth = $request->year * 100+$request->month;
            //return $moth;
            try {
                DB::beginTransaction();
            $salesTarget = SalesTarget::where('sale_target_id',$request->sale_target_id)->first();
            $salesTarget->month = $moth;
            $salesTarget->region_id = $request->region;
            $salesTarget->designation_id = $request->designation;
            $salesTarget->division_id = $request->division; 
            $salesTarget->area =json_encode($request->area);
            $salesTarget->district =json_encode($request->district);
            $salesTarget->created_by = auth()->user()->user_id;
            $salesTarget->save();

            $uid=$salesTarget->sale_target_id ;
           
            if(isset($request->ex_id)){
                foreach(json_decode($request->ex_id,true) as $k=>$v){
                    $salesTargetDetails=SaleTargetDetail::where('sale_target_id',$uid)->where('product_id',$v)->first();
                    $salesTargetDetails->delete();
                }
            }
            
            foreach($request->product_id as $k=>$v){
                
                $salesTargetDetails=SaleTargetDetail::where('sale_target_id',$uid)->where('product_id',$v)->first();
                //return $salesTargetDetails;
                if($salesTargetDetails){
                    $salesTargetDetails->product_id = $v; 
                    $salesTargetDetails->target_quantity = $request->target_qty[$k];
                    $salesTargetDetails->target_amount = $request->target_val[$k]; 
                    
                    $salesTargetDetails->created_by = auth()->user()->user_id;
                    $salesTargetDetails->save();
                }else{
                    $salesTargetDetails =  new SaleTargetDetail;
                    $salesTargetDetails->sale_target_id = $uid;
                    $salesTargetDetails->product_id = $v; 
                    $salesTargetDetails->target_quantity = $request->target_qty[$k];
                    $salesTargetDetails->target_amount = $request->target_val[$k]; 
                    
                    $salesTargetDetails->created_by = auth()->user()->user_id;
                    $salesTargetDetails->save();
                }
                
            }
             DB::commit();
            } catch (\PDOException $e) {
                // Woopsy
                DB::rollBack();
                return response()->json(['errors'=>$e]);
            }

            
        }
        return response()->json(['errors'=>'0','success'=>'Record is successfully added']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\SalesTarget  $salesTarget
     * @return \Illuminate\Http\Response
     */
    public function destroy(SalesTarget $salesTarget)
    {
        //
    }
}

VaKeR 2022