<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use PDF;
use File;
use App\LogInformation;
use Storage;
use App\BestAccidentInformation;
use App\Components\CommonComponent;
use Carbon\Carbon;

class FileController extends Controller
{   
    public function createGenerateScheduleFileIfnotExists($logId)
    {
         $logInfo = LogInformation::find($logId);

         //echeck if physically exits
         if(!empty($logInfo->schedule_pdf)){
            $isExists = Storage::disk('s3')->has($logInfo->schedule_pdf);
            if($isExists){
                return $logInfo->schedule_pdf;
            }else{

                //file name exists but not exist on disk..so create now
               $new_file_name = $this->saveEditGeneratePdf($logId);
               $logInfo->schedule_pdf = $new_file_name;
               $logInfo->save();
               return $new_file_name;
            }
         }else{
               $new_file_name = $this->saveEditGeneratePdf($logId);
               $logInfo->schedule_pdf = $new_file_name;
               $logInfo->save();
               return $new_file_name;
         }
    }

    public function saveEditGeneratePdf($log_id)
    {
        $logInfo = DB::table('log_information')->where('log_information.id', $log_id)->join('products', 'log_information.product_id', '=', 'products.id')->first();
        
        $coverLetter  = view('admin.leads.pdf.pdf-slices.cover-letter')->with('logInfo', $logInfo)->render();
        $renewContent = view('admin.leads.pdf.pdf-slices.renew-content')->with('logInfo', $logInfo)->render();

        if($logInfo->product_id==29)
             $pdf = PDF::loadView('admin.leads.pdf.astrenka_scedule', compact('logInfo', 'renewContent', 'coverLetter'))->setPaper('a4', 'portrait')->setWarnings(false)->setPaper('a4');
        else        
             $pdf = PDF::loadView('admin.leads.pdf.scedule', compact('logInfo', 'renewContent', 'coverLetter'))->setPaper('a4', 'portrait')->setWarnings(false)->setPaper('a4');
        
        if(!empty($logInfo->policy_number_full)){
            $policy_number = $logInfo->policy_number_full;    
        }else{
            $policy_number = $logInfo->policy_number;
        }
        
        $policy_number = str_replace('/', '_', $policy_number);
        
        $file_name         = "policy_schedule_" . date('dmYHis') . ".pdf";
        $file_path_to_save = 'policy_documents/' . $policy_number;
        
        // oldway
        /*$path              = public_path() . $file_path_to_save;
        $file_path_to_save = $file_path_to_save . "/" . $file_name;
        if (!File::exists($path)) {
            File::makeDirectory($path, 0755, true, true);
        }
        $path = $path . "/" . $file_name;
        $pdf->save($path);*/

        // s3
        $file_path_to_save = $file_path_to_save . "/" . $file_name;

        

        Storage::disk('s3')->put($file_path_to_save, $pdf->output()); 
       // echo $file_path_to_save."in this";
          //  exit;

     //    $pdf_path = env('S3_BUCKET_URL').$file_path_to_save;
        // echo $pdf_path;
        return $file_path_to_save;
    }

    public function createGenerateScheduleBAIFileIfnotExists($logId)
    {
         $logInfo = LogInformation::find($logId);
         //echeck if physically exits
         if(!empty($logInfo->schedule_pdf)){
            /*$file_path = public_path().$logInfo->schedule_pdf;
            $isExists = File::exists($file_path);*/

            $isExists = Storage::disk('s3')->has($logInfo->schedule_pdf);
            if($isExists){
                return $logInfo->schedule_pdf;
            }else{
                //file name exists but not exist on disk..so create now
               $new_file_name = $this->saveEditGeneratePdfBAI($logId);
               $logInfo->schedule_pdf = $new_file_name;
               $logInfo->save();
               return $new_file_name;
            }
         }else{
           $new_file_name = $this->saveEditGeneratePdfBAI($logId);
           $logInfo->schedule_pdf = $new_file_name;
           $logInfo->save();
           return $new_file_name;
         }
    }

    public function saveEditGeneratePdfBAI($log_id)
    {
        $logInfo = DB::table('log_information')
                        ->where('log_information.id', $log_id)
                        ->join('products','log_information.product_id','=','products.id')
                        ->select('log_information.*','products.*')
                        ->first();

     
     $data = BestAccidentInformation::with('Information')->with('pricingPlans')->where('information_id', $logInfo->application_id)->first();


       // dd($logInfo);

         //   $pdf = PDF::loadView('admin.leads.pdf.edit_application', compact('logInfo'))->setPaper('a4', 'portrait')->setWarnings(false);
        
        $pdf = PDF::loadView('admin.leads.pdf.policy-schedule-bai', array(
                    'monthly_cost' => $logInfo->monthlycost,
                    'lead_data' => $data,
                    'logInfo' => $logInfo,
                   // 'typeofcover' => $logInfo->type_of_cover,
                    'excess' => $logInfo->excessperiod
                ));

        if(!empty($logInfo->policy_number_full)){
            $policy_number = $logInfo->policy_number_full;    
        }else{
            $policy_number = $logInfo->policy_number;
        }
        
        $policy_number = str_replace('/', '_', $policy_number);
        
        $file_name         = "policy_schedule_" . date('dmYHis') . ".pdf";
        $file_path_to_save = 'policy_documents/' . $policy_number;
        
        // oldway
        /*$path              = public_path() . $file_path_to_save;
        $file_path_to_save = $file_path_to_save . "/" . $file_name;
        if (!File::exists($path)) {
            File::makeDirectory($path, 0755, true, true);
        }
        
        $path = $path . "/" . $file_name;
        $pdf->save($path);*/

        // s3

        $file_path_to_save = $file_path_to_save . "/" . $file_name;
        Storage::disk('s3')->put($file_path_to_save, $pdf->output()); 
        // $pdf_path = env('S3_BUCKET_URL').$file_path_to_save;
        return $file_path_to_save;
    }


    public function createRenewFileIfnotExists($logId)
    {
         $logInfo = LogInformation::find($logId);

         if(!empty($logInfo->renew_pdf)){
            // $file_path = public_path().$logInfo->cancel_pdf;
            $isExists = Storage::disk('s3')->has($logInfo->renew_pdf);
            if($isExists){
                return $logInfo->renew_pdf;
            }else{
                //file name exists but not exist on disk..so create now
               $new_file_name = $this->saveRenewPdf($logInfo->id);
               $logInfo->renew_pdf = $new_file_name;
               $logInfo->save();
               return $new_file_name;
            }
         }else{
               $new_file_name = $this->saveRenewPdf($logInfo->id);
               $logInfo->renew_pdf = $new_file_name;
               $logInfo->save();
               return $new_file_name;
         }

        
    }

    public static function saveReviewPdf($log_id,$annual_premium_old,$monthly_premium_old)
    {
        $logInfo = DB::table('log_information')->where('log_information.id', $log_id)->join('products', 'log_information.product_id', '=', 'products.id')->first();
        
        $last_year_monthly_premium = $logInfo->last_year_monthly_premium;
        $last_year_annual_premium  = $logInfo->last_year_annual_premium;
        $insurance_enddate_old     = $logInfo->insurance_enddate_old;
        $insurance_startdate_old   = $logInfo->insurance_startdate_old;

        $logInfo->last_year_monthly_premium=$monthly_premium_old;
        $logInfo->last_year_annual_premium  = $annual_premium_old;

        
      //  echo $logInfo->renewal_date;
      //  exit;
        $renew_content = view('admin.leads.pdf.pdf-slices.renew-content')->with('logInfo', $logInfo)->render();
        

        if($logInfo->product_id=="26" || $logInfo->product_id=="4" || $logInfo->product_id=="24" || $logInfo->product_id=="25")
        {
            $pdf = PDF::loadView('admin.leads.pdf.review_application_mms', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        elseif($logInfo->product_id=="32")
        {
            $pdf = PDF::loadView('admin.leads.pdf.review_application_hood_aviva', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        elseif($logInfo->product_id=="33")
        {
            $pdf = PDF::loadView('admin.leads.pdf.review_application_hood_signa', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }


        elseif($logInfo->product_id=="18" || $logInfo->product_id=="19" || $logInfo->product_id=="20" || $logInfo->product_id=="21" || $logInfo->product_id=="22" || $logInfo->product_id=="23" )
        {
            $pdf = PDF::loadView('admin.leads.pdf.review_application_covea', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        elseif($logInfo->product_id=="3" ||  $logInfo->product_id=="28")
        {
            $pdf = PDF::loadView('admin.leads.pdf.review_application_standard', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        elseif($logInfo->product_id=="30")
        {
            $pdf = PDF::loadView('admin.leads.pdf.review_application_standard_collinson', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        elseif($logInfo->product_id=="6"  || $logInfo->product_id=="31")
        {
            $pdf = PDF::loadView('admin.leads.pdf.review_application_standard_mortgage', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        elseif($logInfo->product_id=="31")
        {
            $pdf = PDF::loadView('admin.leads.pdf.review_application_standard_mortgage_collinson', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        elseif($logInfo->product_id=="29")
        {
            $pdf = PDF::loadView('admin.leads.pdf.review_application_astrenska', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        else
        {
            $pdf = PDF::loadView('admin.leads.pdf.renew_application', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }


       // $pdf = PDF::loadView('admin.leads.pdf.renew_application', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        
        if(!empty($logInfo->policy_number_full)){
            $policy_number = $logInfo->policy_number_full;    
        }else{
            $policy_number = $logInfo->policy_number;
        }
        $policy_number = str_replace('/', '_', $policy_number);
        
        $file_name         = "review_" . date('dmYHis') . ".pdf";
        $file_path_to_save = 'policy_documents/' . $policy_number;
        
        // oldway
        /*$path              = public_path() . $file_path_to_save;
        $file_path_to_save = $file_path_to_save . "/" . $file_name;
        if (!File::exists($path)) {
            File::makeDirectory($path, 0755, true, true);
        }
        
        $path = $path . "/" . $file_name;
        $pdf->save($path);*/

        // s3

        $file_path_to_save = $file_path_to_save . "/" . $file_name;
        Storage::disk('s3')->put($file_path_to_save, $pdf->output()); 
        // $pdf_path = env('S3_BUCKET_URL').$file_path_to_save;
        return $file_path_to_save;
    }

    public static function saveRenewPdf($log_id)
    {
        $logInfo = DB::table('log_information')->where('log_information.id', $log_id)->join('products', 'log_information.product_id', '=', 'products.id')->first();
        
        $last_year_monthly_premium = $logInfo->last_year_monthly_premium;
        $last_year_annual_premium  = $logInfo->last_year_annual_premium;
        $insurance_enddate_old     = $logInfo->insurance_enddate_old;
        $insurance_startdate_old   = $logInfo->insurance_startdate_old;
        
      //  echo $logInfo->renewal_date;
      //  exit;
        $renew_content = view('admin.leads.pdf.pdf-slices.renew-content')->with('logInfo', $logInfo)->render();
        

        if($logInfo->product_id=="26" || $logInfo->product_id=="4" || $logInfo->product_id=="24" || $logInfo->product_id=="25")
        {
            $pdf = PDF::loadView('admin.leads.pdf.renew_application_mms', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }


        else if($logInfo->product_id=="18" || $logInfo->product_id=="19" || $logInfo->product_id=="20" || $logInfo->product_id=="21" || $logInfo->product_id=="22" || $logInfo->product_id=="23")
        {
            $pdf = PDF::loadView('admin.leads.pdf.renew_application_covea', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        elseif($logInfo->product_id=="32")
        {
            $pdf = PDF::loadView('admin.leads.pdf.review_application_hood_aviva', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        elseif($logInfo->product_id=="33")
        {
            $pdf = PDF::loadView('admin.leads.pdf.review_application_hood_signa', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }
    
        else if($logInfo->product_id=="29")
        {
            $pdf = PDF::loadView('admin.leads.pdf.renew_application_astrenka', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        else if($logInfo->product_id=="34")
        {
            $pdf = PDF::loadView('admin.leads.pdf.renew_application_incomesecure', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        elseif($logInfo->product_id=="3" ||  $logInfo->product_id=="6" ||  $logInfo->product_id=="28")
        {
            $pdf = PDF::loadView('admin.leads.pdf.renew_application_standard', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }

        elseif($logInfo->product_id=="30" ||  $logInfo->product_id=="31" )
        {
            $pdf = PDF::loadView('admin.leads.pdf.renew_application_standard_collinson', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }


        else
        {
            $pdf = PDF::loadView('admin.leads.pdf.renew_application', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        }


       // $pdf = PDF::loadView('admin.leads.pdf.renew_application', compact('logInfo', 'renew_content', 'last_year_monthly_premium', 'last_year_annual_premium', 'insurance_startdate_old', 'insurance_enddate_old'))->setPaper('a4', 'portrait')->setWarnings(false);
        
        if(!empty($logInfo->policy_number_full)){
            $policy_number = $logInfo->policy_number_full;    
        }else{
            $policy_number = $logInfo->policy_number;
        }
        $policy_number = str_replace('/', '_', $policy_number);
        
        $file_name         = "renew_" . date('dmYHis') . ".pdf";
        $file_path_to_save = 'policy_documents/' . $policy_number;
        
        // oldway
        /*$path              = public_path() . $file_path_to_save;
        $file_path_to_save = $file_path_to_save . "/" . $file_name;
        if (!File::exists($path)) {
            File::makeDirectory($path, 0755, true, true);
        }
        
        $path = $path . "/" . $file_name;
        $pdf->save($path);*/

        // s3

        $file_path_to_save = $file_path_to_save . "/" . $file_name;
        Storage::disk('s3')->put($file_path_to_save, $pdf->output()); 
        // $pdf_path = env('S3_BUCKET_URL').$file_path_to_save;
        return $file_path_to_save;
    }


    public function createMtaFileIfnotExists($id)
    {

       // echo $id;
       // exit;
         $logInfo = LogInformation::find($id);


         if(!empty($logInfo->mta_pdf)){
            // $file_path = public_path().$logInfo->cancel_pdf;
            $isExists = Storage::disk('s3')->has($logInfo->mta_pdf);
            if($isExists){
                return $logInfo->mta_pdf;
            }else{
                //file name exists but not exist on disk..so create now
               $new_file_name = $this->saveMtaPdf($logInfo->id);
               $logInfo->mta_pdf = $new_file_name;
               $logInfo->save();
               return $new_file_name;
            }
         }else{
               $new_file_name = $this->saveMtaPdf($logInfo->id);
               $logInfo->mta_pdf = $new_file_name;
               $logInfo->save();
               return $new_file_name;
         }


    }
    
    public function saveMtaPdf($id)
    {
        $logInfo  = DB::table('log_information')
        ->where('log_information.id', $id)
        ->join('products', 'log_information.product_id', 
            '=', 'products.id')
        ->first();
        // pdf path
        
        $mta_cover_letter= view('admin.leads.pdf.pdf-slices.mta-cover-letter')->with('logInfo', $logInfo)->render();

        
        $policy_scedule = view('admin.leads.pdf.pdf-slices.policy-schedule-for-edit-lead')->with('logInfo', $logInfo)->render();
        
        if ($logInfo->product_id == Config('constant.BEST_INSURANCE_COVEA') || $logInfo->product_id == Config('constant.BEST_INSURANCE_ULTRA_COVEA') || $logInfo->product_id == Config('constant.BEST_INSURANCE') || $logInfo->product_id == Config('constant.BEST_INSURANCE_ULTRA')) {
            $policy_scedule = view('admin.leads.pdf.pdf-slices.policy-schedule-for-edit-lead')->with('logInfo', $logInfo)->render();
            
            $pdf = PDF::loadView('admin.leads.pdf.mta_appliaction', compact('logInfo', 'policy_scedule', 'mta_cover_letter'))
            ->setPaper('a4', 'portrait')
            ->setWarnings(false);
            
        } 
        else if($logInfo->product_id==29)
        {
            $pdf = PDF::loadView('admin.leads.pdf.mta_appliaction_astrenka', compact('logInfo', 'mta_cover_letter'))
            ->setPaper('a4', 'portrait')
            ->setWarnings(false);
        }

        else if($logInfo->product_id==34)
        {
            $pdf = PDF::loadView('admin.leads.pdf.mta_appliaction_incomesecure', compact('logInfo', 'mta_cover_letter'))
            ->setPaper('a4', 'portrait')
            ->setWarnings(false);
        }

        else if($logInfo->product_id==30 || $logInfo->product_id==31)
        {
            $pdf = PDF::loadView('admin.leads.pdf.mta_appliaction_collinson', compact('logInfo', 'mta_cover_letter'))
            ->setPaper('a4', 'portrait')
            ->setWarnings(false);
        }

        else if($logInfo->product_id==32 || $logInfo->product_id==33)
        {
            $pdf = PDF::loadView('admin.leads.pdf.mta_appliaction', compact('logInfo', 'mta_cover_letter'))
            ->setPaper('a4', 'portrait')
            ->setWarnings(false);
        }

        else {
            $pdf = PDF::loadView('admin.leads.pdf.mta_appliaction', compact('logInfo', 'mta_cover_letter'))
            ->setPaper('a4', 'portrait')
            ->setWarnings(false);
        }

        if(!empty($logInfo->policy_number_full)){
            $policy_number = $logInfo->policy_number_full;    
        }else{
            $policy_number = $logInfo->policy_number;
        }
        $policy_number = str_replace('/', '_', $policy_number);
        
        $file_name         = "mta_" . date('dmYHis') . ".pdf";
        $file_path_to_save = 'policy_documents/' . $policy_number;
        
        // oldway
        /*$path              = public_path() . $file_path_to_save;
        $file_path_to_save = $file_path_to_save . "/" . $file_name;
        if (!File::exists($path)) {
            File::makeDirectory($path, 0755, true, true);
        }
        
        $path = $path . "/" . $file_name;
        $pdf->save($path);*/

        $file_path_to_save = $file_path_to_save . "/" . $file_name;
        
        Storage::disk('s3')->put($file_path_to_save, $pdf->output()); 
        // $pdf_path = env('S3_BUCKET_URL').$file_path_to_save;
        return $file_path_to_save;
    }


    public function createCancelFileIfnotExists($id)
    {
         $logInfo = LogInformation::find($id);
         //echeck if physically exits
         if(!empty($logInfo->cancel_pdf)){
            // $file_path = public_path().$logInfo->cancel_pdf;
            $isExists = Storage::disk('s3')->has($logInfo->cancel_pdf);
            if($isExists){
                return $logInfo->cancel_pdf;
            }else{
                //file name exists but not exist on disk..so create now
               $new_file_name = $this->saveCancelPdf($logInfo->id);
               $logInfo->cancel_pdf = $new_file_name;
               $logInfo->save();
               return $new_file_name;
            }
         }else{
               $new_file_name = $this->saveCancelPdf($logInfo->id);
               $logInfo->cancel_pdf = $new_file_name;
               $logInfo->save();
               return $new_file_name;
         }
    }

    public function saveCancelPdf($id)
    {
        $logInfo = DB::table('log_information')
                  ->where('log_information.id', $id)
                  ->join('products','log_information.product_id','=','products.id')
                  ->first();
        $policy_cancel_schedule = view('admin.leads.pdf.pdf-slices.policy-cancel-schedule')
        ->with('logInfo',$logInfo)->render();

        $pdf = PDF::loadView('admin.leads.pdf.pdf-slices.policy-cancel-schedule',
            compact('logInfo','policy_cancel_schedule'))
            ->setPaper('a4', 'portrait')
            ->setWarnings(false);

        if(!empty($logInfo->policy_number_full)){
            $policy_number = $logInfo->policy_number_full;    
        }else{
            $policy_number = $logInfo->policy_number;
        }

        $policy_number = str_replace('/', '_', $policy_number);
        
        $file_name         = "cancel_" . date('dmYHis') . ".pdf";
        $file_path_to_save = 'policy_documents/' . $policy_number;
        
        // oldway
        /*$path              = public_path() . $file_path_to_save;
        $file_path_to_save = $file_path_to_save . "/" . $file_name;
        if (!File::exists($path)) {
            File::makeDirectory($path, 0755, true, true);
        }
        
        $path = $path . "/" . $file_name;
        $pdf->save($path);*/

        $file_path_to_save = $file_path_to_save . "/" . $file_name;
        
        Storage::disk('s3')->put($file_path_to_save, $pdf->output());
        // $pdf_path = env('S3_BUCKET_URL').$file_path_to_save;
        return $file_path_to_save;
  }


  public function createMMSCoverLetterLowerAmount($id)
    {

              //file name exists but not exist on disk..so create now
               $new_file_name = $this->saveMMSCoverLetter($id);
               
               return $new_file_name;    

    }
    
    public static function saveMMSCoverLetter($id,$action_date)
    {
        $action_date=$action_date;
        $logInfo  = DB::table('information')
        ->where('id', $id)
        ->first();
        // pdf path
        $dob = explode("/", $logInfo->dob);
		$dob_actual = $dob[2] . "-" . $dob[1] . "-" . $dob[0];
        $dob = Carbon::parse($dob_actual)->format('Y-m-d');
        $age = date_diff(date_create($dob), date_create($logInfo->insurance_startdate))->y;

        $monthly_premium = CommonComponent::fetchMonthlyCostSimpleCoveaMtaRenew($age, $logInfo->monthlybenefit, $logInfo->typeofcover, $logInfo->excessperiod, $logInfo->product_id, 0);
        $monthly_premium=number_format($monthly_premium,2);


        //echo $monthly_premium;

       
        $policy_number = str_replace('/', '_', $logInfo->policy_number_full);
        
       // echo $policy_number;
        //  $filedate=Carbon::parse($action_date)->format('dmY');

        $filedate=date('dmY',strtotime("+35 days"));
        $file_name         = "mms_coverletter_" .$policy_number."_". $filedate . ".pdf";


        $file_path_to_save = 'policy_documents/' . $policy_number;
        $file_path_to_save = $file_path_to_save . "/" . $file_name;

        //$action_date = Carbon::parse($action_date)->format('d/m/Y');
        $pdf = PDF::loadView('admin.leads.pdf.mms_coverletter_low', compact('logInfo','monthly_premium','action_date'))
        ->setPaper('a4', 'portrait')
        ->setWarnings(false)->save(public_path().'/cover_letters/' . $file_name);
  
        
        Storage::disk('s3')->put($file_path_to_save, $pdf->output()); 
        // $pdf_path = env('S3_BUCKET_URL').$file_path_to_save;
        return $file_path_to_save;
    }


    public static function saveCoverLetterBIG($id,$action_date,$letter_date,$filedate)
    {
       // $action_date=$action_date;
        $logInfo  = DB::table('information')
        ->where('id', $id)
        ->first();
        // pdf path
        $dob = explode("/", $logInfo->dob);
		$dob_actual = $dob[2] . "-" . $dob[1] . "-" . $dob[0];
        $dob = Carbon::parse($dob_actual)->format('Y-m-d');
        $age = date_diff(date_create($dob), date_create($logInfo->insurance_startdate))->y;

        $monthly_premium = CommonComponent::fetchMonthlyCostSimpleCoveaMtaStandard($age, $logInfo->monthlybenefit, $logInfo->typeofcover, $logInfo->excessperiod, $logInfo->product_id, 0);
        echo $monthly_premium;
        $monthly_premium=number_format($monthly_premium,2);


        //echo $monthly_premium;

       
        $policy_number = str_replace('/', '_', $logInfo->policy_number_full);
        
       // echo $policy_number;
       //$filedate=date('dmY',strtotime("+35 days"));

      // $filedate=Carbon::parse($action_date)->format('d/m/Y');
      // echo $filedate;
      // exit;
        $file_name         = "big_coverletter_" .$policy_number."_". $filedate . ".pdf";


        $file_path_to_save = 'policy_documents/' . $policy_number;
        $file_path_to_save = $file_path_to_save . "/" . $file_name;

        //$action_date=date('d/m/Y',$action_date);
       // $action_date = Carbon::parse($action_date)->format('d/m/Y');
        $pdf = PDF::loadView('admin.leads.pdf.big_coverletter_march', compact('logInfo','monthly_premium','action_date','letter_date'))
        ->setPaper('a4', 'portrait')
        ->setWarnings(false)->save(public_path().'/cover_letters/big/' . $file_name);
  
        
        Storage::disk('s3')->put($file_path_to_save, $pdf->output()); 
        // $pdf_path = env('S3_BUCKET_URL').$file_path_to_save;
        return $file_path_to_save;
    }

    public function createMMSCoverLetterHigherAmount($id)
    {

              //file name exists but not exist on disk..so create now
               $new_file_name = $this->saveMMSCoverLetterHigh($id);
               
               return $new_file_name;
           

    }
    
    public function saveMMSCoverLetterHigh($id)
    {
        $logInfo  = DB::table('information')
        ->where('id', $id)
        ->first();
        // pdf path
        
        $dob = explode("/", $logInfo->dob);
		$dob_actual = $dob[2] . "-" . $dob[1] . "-" . $dob[0];
        $dob = Carbon::parse($dob_actual)->format('Y-m-d');
        $age = date_diff(date_create($dob), date_create($logInfo->insurance_startdate))->y;

        $monthly_premium = CommonComponent::fetchMonthlyCostSimpleCovea($age, 2000, $logInfo->typeofcover, $logInfo->excessperiod, $logInfo->product_id, 0);
        $monthly_premium=number_format($monthly_premium,2);
        echo $monthly_premium;

        if($logInfo->policy_number_full=='MMS/SPBST/05A11945')
        {
            $old_premium="71.00";
            $monthly_premium="71.00";
            
        }

        if($logInfo->policy_number_full=='MMS/SPBST/05A10461')
        {
            $old_premium="57.75";
            $monthly_premium="58.00";
            
        }

        if($logInfo->policy_number_full=='MMS/SPBST/05A11566')
        {
            $old_premium="73.75";
            $monthly_premium="73.80";
            
        }

        if($logInfo->policy_number_full=='MMS/SPBST/05A11495')
        {
            $old_premium="95.25";
            $monthly_premium="95.20";
            
        }

        if($logInfo->policy_number_full=='MMS/SPBST/05A11242')
        {
            $old_premium="67.00";
            $monthly_premium="67.00";
            
        }

        $pdf = PDF::loadView('admin.leads.pdf.mms_coverletter_high', compact('logInfo','monthly_premium','old_premium'))
            ->setPaper('a4', 'portrait')
            ->setWarnings(false);
      
        $policy_number = str_replace('/', '_', $logInfo->policy_number_full);
        
        $file_name         = "mms_coverletter" . date('dmYHis') . ".pdf";
        $file_path_to_save = 'policy_documents/' . $policy_number;
        $file_path_to_save = $file_path_to_save . "/" . $file_name;
        echo $file_path_to_save;
        Storage::disk('s3')->put($file_path_to_save, $pdf->output()); 
        // $pdf_path = env('S3_BUCKET_URL').$file_path_to_save;
        return $file_path_to_save;
    }
}
