<?php

namespace App\Http\Controllers;

use App\Http\Requests\ProfileUpdateRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;

use Illuminate\Support\Facades\Log;
use App\Models\CarWashOwner;
use App\Models\CwoSubscription;
use App\Models\Subscription;
use App\Models\Country;
use App\Models\CurrencyMaster;
use Illuminate\Support\Facades\Http;

use Illuminate\Contracts\Auth\Authenticatable;
use App\Http\Requests\Auth\LoginRequest;
use App\Providers\RouteServiceProvider;
use Carbon\Carbon;
use Session;
use DB;
class BillingController extends Controller
{
    public function billing(Request $request)
    {
        $account_no = $request->input('account_no');
        // echo $account_no;exit;
        $getCwo = CarWashOwner::where('account_no', $account_no)->first();
        $dataBag['cwo'] = $getCwo;
        $cwo_subscription_id = $getCwo->cwo_subscription_id;
        echo 'cwo_subscription_id->' . $cwo_subscription_id . ', ';
        $dataBag['cwo_subscription_id'] = $cwo_subscription_id;
        $free_trial = $getCwo->free_trial;
        echo 'free_trial->' . $free_trial . ', ';
        $dataBag['free_trial'] = $free_trial;
        if($cwo_subscription_id > 0){
            $getSubscription = CwoSubscription::find($cwo_subscription_id);
            $type = $getSubscription->type;
            echo 'type->' . $type . '<br>';
        } elseif($free_trial == 1) {

        } else {

        }
        return view('fastpay.cwobiling', $dataBag);
    }

        public function  total_billing(Request $request){
            $account_no = $request->input('account_no');
            $invoice_id = $request->input('invoice_id');
            $total_whatsapp_amount = $request->input('total_whatsapp_amount');
           // dd($total_whatsapp_amount);
            $subscription_amount = $request->input('total_subscription_amount');
            $fetchInvoice=DB::table('invoice')->where('id',$invoice_id)->first();
           // dd($total_whatsapp_amount,$subscription_amount,$fetchInvoice);

           if(!empty($total_whatsapp_amount) && !empty($subscription_amount)){
          // dd('hii');
            
                  //check for cwo_id this is for due checking
                  $fetch=DB::table('cwo_master')->where('account_no',$account_no)->first();
                  $cwomasterid=$fetch->id;
                //  dd($cwomasterid);
                  if(!empty($cwomasterid)){
                      $checkAccessRights=DB::table('invoice')->where('is_paid',0)->where('cwo_id',$cwomasterid)->count();
                     // dd($checkAccessRights);
                      if($checkAccessRights==0){
                         abort(404);
                      }
                  }
                // dd($fetchInvoice);
                  $subscriptionAmount = number_format($subscription_amount ?? 0, 2);
                  $whatsappAmount = number_format($total_whatsapp_amount?? 0, 2);
                 // dd($subscriptionAmount,$whatsappAmount);
                  $subscriptionType=$fetchInvoice->type??'';
      
                  // echo $account_no;exit;
                  $getCwo = CarWashOwner::where('account_no', $account_no)->first();
                  $dataBag['cwo'] = $getCwo;
                  $cwomaster=DB::table('cwo_master')->select('cwo_subscription_id','start_date','end_date','account_no')->where('id',$getCwo->id)->first();
      
                  $washedDataNoMessages = DB::table('wash_details')
                  ->where('cwo_id', $getCwo->id)
                  ->whereIn('status', [3, 5])
                  ->whereNotNull('driver_phone')
                  ->whereBetween('date_washed', [
                      Carbon::parse($cwomaster->start_date)->subDays(1),
                      Carbon::parse($cwomaster->end_date)->subDays(1)
                  ])
                  ->count();
                  $totalWhatsappCharge=number_format($washedDataNoMessages*2*0.3,2);
                  $cwo_subscription_id = $getCwo->cwo_subscription_id;
                  $dataBag['cwo_subscription_id'] = $cwo_subscription_id;
                  $free_trial = $getCwo->free_trial;
                  $dataBag['free_trial'] = $free_trial;
          
                  if($cwo_subscription_id > 0){
                      $getSubscription = CwoSubscription::find($cwo_subscription_id);
                      if(empty($getSubscription)){
                        //   abort(404);
                      }
                    //  dd($whatsappAmount,$subscriptionAmount);
                      $type = $getSubscription->type;
                      $subscriptiontotal = $getSubscription->amount;
                      $dataBag['cwo_subscription_id']=$cwomaster->cwo_subscription_id;
                      $dataBag['cwo_type']=$getSubscription->type;
                      $dataBag['subscriptionTotal']=$subscriptionAmount;
                      $dataBag['whatsappAmount']=$whatsappAmount;
                      $dataBag['subscriptionType']=$subscriptionType;
                      $dataBag['whatsappCount']=0;
                      $dataBag['whatsappCharge']=0;
                      $dataBag['bothSubscriptions']=1;
                      $dataBag['whatsappSubscription']=0;
                      $dataBag['normalSubscription']=0;
                      $dataBag['total_amount'] = number_format((float)$subscriptionAmount + (float)$whatsappAmount, 2);

       
      
      
                  } elseif($free_trial == 1) {
          
                  } 
      
                  else {
          
                  }
            
                  return view('fastpay.cwototalbilling', $dataBag);
           }
            if(!empty($total_whatsapp_amount)){


        //check for cwo_id this is for due checking
        $fetch=DB::table('cwo_master')->where('account_no',$account_no)->first();
        $cwomasterid=$fetch->id;
      //  dd($cwomasterid);
        if(!empty($cwomasterid)){
            $checkAccessRights=DB::table('invoice')->where('is_paid',0)->where('cwo_id',$cwomasterid)->count();
           // dd($checkAccessRights);
            if($checkAccessRights==0){
               abort(404);
            }
        }
       
        $whatsappAmount = number_format($fetchInvoice->whatsapp_amount ?? 0, 2);

       // dd($whatsappAmount);
       // $subscriptionType=0;

        // echo $account_no;exit;
        $getCwo = CarWashOwner::where('account_no', $account_no)->first();
        $dataBag['cwo'] = $getCwo;
        $cwomaster=DB::table('cwo_master')->select('cwo_subscription_id','start_date','end_date','account_no')->where('id',$getCwo->id)->first();

    
        $free_trial = 0;
        $dataBag['free_trial'] = $free_trial;

        if(!empty($cwomaster)){
            $cwo_subscription_id= $cwomaster->cwo_subscription_id;
            $getSubscription = CwoSubscription::find($cwo_subscription_id);
            if(empty($getSubscription)){
              //   abort(404);
            }
            $type = $getSubscription->type;
            $subscriptiontotal = $getSubscription->amount;
            $dataBag['cwo_subscription_id']=$cwomaster->cwo_subscription_id;
            $dataBag['cwo_type']=$getSubscription->type;
            $dataBag['subscriptionTotal']=0;  //whatsapp Amount
            $dataBag['whatsappAmount']=$whatsappAmount;
            $dataBag['subscriptionType']=0;
            $dataBag['whatsappCount']=0;
            $dataBag['whatsappCharge']=0;
            $dataBag['total_amount']=$whatsappAmount;
            $dataBag['bothSubscriptions']=0;
            $dataBag['whatsappSubscription']=1;
            $dataBag['normalSubscription']=0;



        } elseif($free_trial == 1) {

        } 

        else {

        }
  
        return view('fastpay.cwototalbilling', $dataBag);







            }
            if(!empty($subscription_amount)){
             


                  //check for cwo_id this is for due checking
            $fetch=DB::table('cwo_master')->where('account_no',$account_no)->first();
            $cwomasterid=$fetch->id;
          //  dd($cwomasterid);
            if(!empty($cwomasterid)){
                $checkAccessRights=DB::table('invoice')->where('is_paid',0)->where('cwo_id',$cwomasterid)->count();
               // dd($checkAccessRights);
                if($checkAccessRights==0){
                   abort(404);
                }
            }
           
            $subscriptionAmount = number_format($fetchInvoice->amount ?? 0, 2);

            $subscriptionType=$fetchInvoice->type??'';

            // echo $account_no;exit;
            $getCwo = CarWashOwner::where('account_no', $account_no)->first();
            $dataBag['cwo'] = $getCwo;
            $cwomaster=DB::table('cwo_master')->select('cwo_subscription_id','start_date','end_date','account_no')->where('id',$getCwo->id)->first();

            $washedDataNoMessages = DB::table('wash_details')
            ->where('cwo_id', $getCwo->id)
            ->whereIn('status', [3, 5])
            ->whereNotNull('driver_phone')
            ->whereBetween('date_washed', [
                Carbon::parse($cwomaster->start_date)->subDays(1),
                Carbon::parse($cwomaster->end_date)->subDays(1)
            ])
            ->count();
            $totalWhatsappCharge=number_format($washedDataNoMessages*2*0.3,2);
            $cwo_subscription_id = $getCwo->cwo_subscription_id;
            $dataBag['cwo_subscription_id'] = $cwo_subscription_id;
            $free_trial = $getCwo->free_trial;
            $dataBag['free_trial'] = $free_trial;
    
            if($cwo_subscription_id > 0){
                $getSubscription = CwoSubscription::find($cwo_subscription_id);
                if(empty($getSubscription)){
                  //   abort(404);
                }
                $type = $getSubscription->type;
                $subscriptiontotal = $getSubscription->amount;
                $dataBag['cwo_subscription_id']=$cwomaster->cwo_subscription_id;
                $dataBag['cwo_type']=$getSubscription->type;
                $dataBag['subscriptionTotal']=$subscriptionAmount;
                $dataBag['whatsappAmount']=0;
                $dataBag['subscriptionType']=$subscriptionType;
                $dataBag['whatsappCount']=0;
                $dataBag['whatsappCharge']=0;
                $dataBag['total_amount']=$subscriptionAmount;
                $dataBag['bothSubscriptions']=0;
                $dataBag['whatsappSubscription']=0;
                $dataBag['normalSubscription']=1;
 


            } elseif($free_trial == 1) {
    
            } 

            else {
    
            }
      
            return view('fastpay.cwototalbilling', $dataBag);


            }
          

        }


        public function renewSubscription(Request $request){
            $bothSubscriptions=$request->bothSubscriptions;
            $whatsappSubscription=$request->whatsappSubscription;
            $normalSubscription=$request->normalSubscription;
          //  dd($request->all());
            if($normalSubscription==1){
                $cwo_id = $request->input('cwo_id');
                $subscription_detail_id = $request->input('subscription_detail_id');
                $subscription_type = $request->input('subscription_type');
                $amount = $request->input('amount');
                $whatsapp_amount = $request->input('whatsapp_amount');
                $whatsapp_count = $request->input('whatsapp_count');
                $total_amount = $request->input('total_amount');
                $dt = Carbon::now(); 
        
                if($subscription_detail_id != ''){
                    $t=time();
                    $today = date('Y-m-d');
                    $time = strtotime(date("Y-m-d"));
        
                    // $getCwo = CarWashOwner::where('id', $cwo_id)->select('end_date')->first();
                    $getCwo = CarWashOwner::where('id', $cwo_id)->select('start_date', 'end_date', 'free_trial')->first();
                    $cwo_subscription = CwoSubscription::where('cwo_id', $cwo_id)->where('status', 1)->orderBy('id', 'desc')->take(1)->get();
                    if(isset($cwo_subscription) && count($cwo_subscription)>0){ // if previous subscription exist
                        if($subscription_type == 1){
                            if($getCwo->end_date == ''){
                                $start_date = date("Y-m-d");
                                $end_date = date("Y-m-d", strtotime("+1 month", $time));
                            } else {
                                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                                $end_date = date('Y-m-d', strtotime('+1 month', strtotime($getCwo->end_date)));
                            }
                            
                        }
                        if($subscription_type == 3){
                            if($getCwo->end_date == ''){
                                $start_date = date("Y-m-d");
                                $end_date = date("Y-m-d", strtotime("+3 month", $time));
                            } else {
                                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                                $end_date = date('Y-m-d', strtotime('+3 month', strtotime($getCwo->end_date)));
                            }
                            
                        }
                        if($subscription_type == 2){
                            if($getCwo->end_date == ''){
                                $start_date = date("Y-m-d");
                                $end_date = date("Y-m-d", strtotime("+1 year", $time));
                            } else {
                                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                                $end_date = date('Y-m-d', strtotime('+1 year', strtotime($getCwo->end_date)));
                            }
                        }
                    } elseif($getCwo->free_trial == 1) { // if free trial exists
                        if($subscription_type == 1){
                            if($getCwo->end_date == ''){
                                $start_date = date("Y-m-d");
                                $end_date = date("Y-m-d", strtotime("+1 month", $time));
                            } else {
                                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                                $end_date = date('Y-m-d', strtotime('+1 month', strtotime($getCwo->end_date)));
                            }
                            
                        }
                        if($subscription_type == 3){
                            if($getCwo->end_date == ''){
                                $start_date = date("Y-m-d");
                                $end_date = date("Y-m-d", strtotime("+3 month", $time));
                            } else {
                                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                                $end_date = date('Y-m-d', strtotime('+3 month', strtotime($getCwo->end_date)));
                            }
                            
                        }
                        if($subscription_type == 2){
                            if($getCwo->end_date == ''){
                                $start_date = date("Y-m-d");
                                $end_date = date("Y-m-d", strtotime("+1 year", $time));
                            } else {
                                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                                $end_date = date('Y-m-d', strtotime('+1 year', strtotime($getCwo->end_date)));
                            }
                        }
                    } else { // if no previous subscription
                        if($subscription_type == 1){
                            $start_date = date("Y-m-d");
                            $end_date = date("Y-m-d", strtotime("+1 month", $time));
                            
                        }
                        if($subscription_type == 3){
                            $start_date = date("Y-m-d");
                            $end_date = date("Y-m-d", strtotime("+3 month", $time));
                            
                        }
                        if($subscription_type == 2){
                            $start_date = date("Y-m-d");
                            $end_date = date("Y-m-d", strtotime("+1 year", $time));
                        }
                    }
        
                    // if($subscription_type == 1){
                    //     if($getCwo->end_date == ''){
                    //         $end_date = date("Y-m-d", strtotime("+1 month", $time));
                    //     } else {
                    //         $end_date = date('Y-m-d', strtotime('+1 month', strtotime($getCwo->end_date)));
                    //     }
                        
                    // }
                    // if($subscription_type == 2){
                    //     if($getCwo->end_date == ''){
                    //         $end_date = date("Y-m-d", strtotime("+1 year", $time));
                    //     } else {
                    //         $end_date = date('Y-m-d', strtotime('+1 year', strtotime($getCwo->end_date)));
                    //     }
                    // }
                    // if($subscription_type == 3){
                    //     if($getCwo->end_date == ''){
                    //         $end_date = date("Y-m-d", strtotime("+3 month", $time));
                    //     } else {
                    //         $end_date = date('Y-m-d', strtotime('+3 month', strtotime($getCwo->end_date)));
                    //     }
                        
                    // }
                    // echo $end_date;exit;
                    $dataCwoSubscription = CwoSubscription::create([
                        'subscription_detail_id'=> $subscription_detail_id,
                        'cwo_id'=> $cwo_id,
                        // 'start_date'=> $today,
                        'start_date'=> $start_date,
                        'end_date'=> $end_date,
                        'type'=> $subscription_type,
                        'amount'=> $amount,
                        'whatsapp_amount'=> $whatsapp_amount,
                        'whatsapp_count'=> $whatsapp_count,
                        'total_amount'=> $total_amount,
                        // 'transaction_id'=> $transaction_id,
                        'transaction_id'=> $t,
                        'status'=> 0,
                        'created_at'=>$dt->toDayDateTimeString(),
                        'updated_at'=>$dt->toDayDateTimeString()
                    ]); 
                    $cwo_subscription_id = $dataCwoSubscription->id;  
        
                    // $cwoDetails = CarWashOwner::findOrFail($cwo_id);
                    // $cwoDetails->update([
                    //     'cwo_subscription_id'=> $cwo_subscription_id,
                    //     'start_date'=> $today,
                    //     'end_date'=> $end_date,
                    //     // 'is_paid'=> 0,
                    //     'updated_at'=>$dt->toDayDateTimeString()
                    //     ]);
                }
        
                return response()->json([
                    'success' => 1,
                    'message' => 'Success',
                    'cwo_id' => $cwo_id,
                    'cwo_subscription_id' => $cwo_subscription_id,
                    'transaction_id'=> $t
                ]);
            }
            if($bothSubscriptions==1){
                $cwo_id = $request->input('cwo_id');
                $subscription_detail_id = $request->input('subscription_detail_id');
                $subscription_type = $request->input('subscription_type');
                $amount = $request->input('amount');
                $whatsapp_amount = $request->input('whatsapp_amount');
                $whatsapp_count = $request->input('whatsapp_count');
                $total_amount = $request->input('total_amount');
                $dt = Carbon::now(); 
        
                if($subscription_detail_id != ''){
                    $t=time();
                    $today = date('Y-m-d');
                    $time = strtotime(date("Y-m-d"));
        
                    // $getCwo = CarWashOwner::where('id', $cwo_id)->select('end_date')->first();
                    $getCwo = CarWashOwner::where('id', $cwo_id)->select('start_date', 'end_date', 'free_trial')->first();
                    $cwo_subscription = CwoSubscription::where('cwo_id', $cwo_id)->where('status', 1)->orderBy('id', 'desc')->take(1)->get();
                    if(isset($cwo_subscription) && count($cwo_subscription)>0){ // if previous subscription exist
                        if($subscription_type == 1){
                            if($getCwo->end_date == ''){
                                $start_date = date("Y-m-d");
                                $end_date = date("Y-m-d", strtotime("+1 month", $time));
                            } else {
                                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                                $end_date = date('Y-m-d', strtotime('+1 month', strtotime($getCwo->end_date)));
                            }
                            
                        }
                        if($subscription_type == 3){
                            if($getCwo->end_date == ''){
                                $start_date = date("Y-m-d");
                                $end_date = date("Y-m-d", strtotime("+3 month", $time));
                            } else {
                                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                                $end_date = date('Y-m-d', strtotime('+3 month', strtotime($getCwo->end_date)));
                            }
                            
                        }
                        if($subscription_type == 2){
                            if($getCwo->end_date == ''){
                                $start_date = date("Y-m-d");
                                $end_date = date("Y-m-d", strtotime("+1 year", $time));
                            } else {
                                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                                $end_date = date('Y-m-d', strtotime('+1 year', strtotime($getCwo->end_date)));
                            }
                        }
                    } elseif($getCwo->free_trial == 1) { // if free trial exists
                        if($subscription_type == 1){
                            if($getCwo->end_date == ''){
                                $start_date = date("Y-m-d");
                                $end_date = date("Y-m-d", strtotime("+1 month", $time));
                            } else {
                                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                                $end_date = date('Y-m-d', strtotime('+1 month', strtotime($getCwo->end_date)));
                            }
                            
                        }
                        if($subscription_type == 3){
                            if($getCwo->end_date == ''){
                                $start_date = date("Y-m-d");
                                $end_date = date("Y-m-d", strtotime("+3 month", $time));
                            } else {
                                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                                $end_date = date('Y-m-d', strtotime('+3 month', strtotime($getCwo->end_date)));
                            }
                            
                        }
                        if($subscription_type == 2){
                            if($getCwo->end_date == ''){
                                $start_date = date("Y-m-d");
                                $end_date = date("Y-m-d", strtotime("+1 year", $time));
                            } else {
                                $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                                $end_date = date('Y-m-d', strtotime('+1 year', strtotime($getCwo->end_date)));
                            }
                        }
                    } else { // if no previous subscription
                        if($subscription_type == 1){
                            $start_date = date("Y-m-d");
                            $end_date = date("Y-m-d", strtotime("+1 month", $time));
                            
                        }
                        if($subscription_type == 3){
                            $start_date = date("Y-m-d");
                            $end_date = date("Y-m-d", strtotime("+3 month", $time));
                            
                        }
                        if($subscription_type == 2){
                            $start_date = date("Y-m-d");
                            $end_date = date("Y-m-d", strtotime("+1 year", $time));
                        }
                    }
        
                    // if($subscription_type == 1){
                    //     if($getCwo->end_date == ''){
                    //         $end_date = date("Y-m-d", strtotime("+1 month", $time));
                    //     } else {
                    //         $end_date = date('Y-m-d', strtotime('+1 month', strtotime($getCwo->end_date)));
                    //     }
                        
                    // }
                    // if($subscription_type == 2){
                    //     if($getCwo->end_date == ''){
                    //         $end_date = date("Y-m-d", strtotime("+1 year", $time));
                    //     } else {
                    //         $end_date = date('Y-m-d', strtotime('+1 year', strtotime($getCwo->end_date)));
                    //     }
                    // }
                    // if($subscription_type == 3){
                    //     if($getCwo->end_date == ''){
                    //         $end_date = date("Y-m-d", strtotime("+3 month", $time));
                    //     } else {
                    //         $end_date = date('Y-m-d', strtotime('+3 month', strtotime($getCwo->end_date)));
                    //     }
                        
                    // }
                    // echo $end_date;exit;
                    $dataCwoSubscription = CwoSubscription::create([
                        'subscription_detail_id'=> $subscription_detail_id,
                        'cwo_id'=> $cwo_id,
                        // 'start_date'=> $today,
                        'start_date'=> $start_date,
                        'end_date'=> $end_date,
                        'type'=> $subscription_type,
                        'amount'=> $amount,
                        'whatsapp_amount'=> $whatsapp_amount,
                        'whatsapp_count'=> $whatsapp_count,
                        'total_amount'=> $total_amount,
                        // 'transaction_id'=> $transaction_id,
                        'transaction_id'=> $t,
                        'status'=> 0,
                        'created_at'=>$dt->toDayDateTimeString(),
                        'updated_at'=>$dt->toDayDateTimeString()
                    ]); 
                    $cwo_subscription_id = $dataCwoSubscription->id;  
        
                    // $cwoDetails = CarWashOwner::findOrFail($cwo_id);
                    // $cwoDetails->update([
                    //     'cwo_subscription_id'=> $cwo_subscription_id,
                    //     'start_date'=> $today,
                    //     'end_date'=> $end_date,
                    //     // 'is_paid'=> 0,
                    //     'updated_at'=>$dt->toDayDateTimeString()
                    //     ]);
                }
        
                return response()->json([
                    'success' => 1,
                    'message' => 'Success',
                    'cwo_id' => $cwo_id,
                    'cwo_subscription_id' => $cwo_subscription_id,
                    'transaction_id'=> $t
                ]);
            }
            if($whatsappSubscription==1){
                $cwo_id = $request->input('cwo_id');
                $subscription_detail_id = $request->input('subscription_detail_id');
                $subscription_type = $request->input('subscription_type');
                $amount = $request->input('amount');
                $whatsapp_amount = $request->input('whatsapp_amount');
                $whatsapp_count = $request->input('whatsapp_count');
                $total_amount = $request->input('total_amount');
                $dt = Carbon::now(); 
        
                if($subscription_detail_id != ''){
                    $t=time();
                    $today = date('Y-m-d');
                    $time = strtotime(date("Y-m-d"));
                  // dd('pay no db change');
                    // $getCwo = CarWashOwner::where('id', $cwo_id)->select('end_date')->first();
                    // $getCwo = CarWashOwner::where('id', $cwo_id)->select('start_date', 'end_date', 'free_trial')->first();
                    // $cwo_subscription = CwoSubscription::where('cwo_id', $cwo_id)->where('status', 1)->orderBy('id', 'desc')->take(1)->get();
                    // if(isset($cwo_subscription) && count($cwo_subscription)>0){ // if previous subscription exist
                    //     if($subscription_type == 1){
                    //         if($getCwo->end_date == ''){
                    //             $start_date = date("Y-m-d");
                    //             $end_date = date("Y-m-d", strtotime("+1 month", $time));
                    //         } else {
                    //             $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                    //             $end_date = date('Y-m-d', strtotime('+1 month', strtotime($getCwo->end_date)));
                    //         }
                            
                    //     }
                    //     if($subscription_type == 3){
                    //         if($getCwo->end_date == ''){
                    //             $start_date = date("Y-m-d");
                    //             $end_date = date("Y-m-d", strtotime("+3 month", $time));
                    //         } else {
                    //             $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                    //             $end_date = date('Y-m-d', strtotime('+3 month', strtotime($getCwo->end_date)));
                    //         }
                            
                    //     }
                    //     if($subscription_type == 2){
                    //         if($getCwo->end_date == ''){
                    //             $start_date = date("Y-m-d");
                    //             $end_date = date("Y-m-d", strtotime("+1 year", $time));
                    //         } else {
                    //             $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                    //             $end_date = date('Y-m-d', strtotime('+1 year', strtotime($getCwo->end_date)));
                    //         }
                    //     }
                    // } elseif($getCwo->free_trial == 1) { // if free trial exists
                    //     if($subscription_type == 1){
                    //         if($getCwo->end_date == ''){
                    //             $start_date = date("Y-m-d");
                    //             $end_date = date("Y-m-d", strtotime("+1 month", $time));
                    //         } else {
                    //             $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                    //             $end_date = date('Y-m-d', strtotime('+1 month', strtotime($getCwo->end_date)));
                    //         }
                            
                    //     }
                    //     if($subscription_type == 3){
                    //         if($getCwo->end_date == ''){
                    //             $start_date = date("Y-m-d");
                    //             $end_date = date("Y-m-d", strtotime("+3 month", $time));
                    //         } else {
                    //             $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                    //             $end_date = date('Y-m-d', strtotime('+3 month', strtotime($getCwo->end_date)));
                    //         }
                            
                    //     }
                    //     if($subscription_type == 2){
                    //         if($getCwo->end_date == ''){
                    //             $start_date = date("Y-m-d");
                    //             $end_date = date("Y-m-d", strtotime("+1 year", $time));
                    //         } else {
                    //             $start_date = date("Y-m-d", strtotime("+1 day", strtotime($getCwo->end_date)));
                    //             $end_date = date('Y-m-d', strtotime('+1 year', strtotime($getCwo->end_date)));
                    //         }
                    //     }
                    // } else { // if no previous subscription
                    //     if($subscription_type == 1){
                    //         $start_date = date("Y-m-d");
                    //         $end_date = date("Y-m-d", strtotime("+1 month", $time));
                            
                    //     }
                    //     if($subscription_type == 3){
                    //         $start_date = date("Y-m-d");
                    //         $end_date = date("Y-m-d", strtotime("+3 month", $time));
                            
                    //     }
                    //     if($subscription_type == 2){
                    //         $start_date = date("Y-m-d");
                    //         $end_date = date("Y-m-d", strtotime("+1 year", $time));
                    //     }
                    // }
        
                    // if($subscription_type == 1){
                    //     if($getCwo->end_date == ''){
                    //         $end_date = date("Y-m-d", strtotime("+1 month", $time));
                    //     } else {
                    //         $end_date = date('Y-m-d', strtotime('+1 month', strtotime($getCwo->end_date)));
                    //     }
                        
                    // }
                    // if($subscription_type == 2){
                    //     if($getCwo->end_date == ''){
                    //         $end_date = date("Y-m-d", strtotime("+1 year", $time));
                    //     } else {
                    //         $end_date = date('Y-m-d', strtotime('+1 year', strtotime($getCwo->end_date)));
                    //     }
                    // }
                    // if($subscription_type == 3){
                    //     if($getCwo->end_date == ''){
                    //         $end_date = date("Y-m-d", strtotime("+3 month", $time));
                    //     } else {
                    //         $end_date = date('Y-m-d', strtotime('+3 month', strtotime($getCwo->end_date)));
                    //     }
                        
                    // }
                    // echo $end_date;exit;
                    // $dataCwoSubscription = CwoSubscription::create([
                    //     'subscription_detail_id'=> $subscription_detail_id,
                    //     'cwo_id'=> $cwo_id,
                    //     // 'start_date'=> $today,
                    //     'start_date'=> $start_date,
                    //     'end_date'=> $end_date,
                    //     'type'=> $subscription_type,
                    //     'amount'=> $amount,
                    //     'whatsapp_amount'=> $whatsapp_amount,
                    //     'whatsapp_count'=> $whatsapp_count,
                    //     'total_amount'=> $total_amount,
                    //     // 'transaction_id'=> $transaction_id,
                    //     'transaction_id'=> $t,
                    //     'status'=> 0,
                    //     'created_at'=>$dt->toDayDateTimeString(),
                    //     'updated_at'=>$dt->toDayDateTimeString()
                    // ]); 
                    // $cwo_subscription_id = $dataCwoSubscription->id;  
        
                    // $cwoDetails = CarWashOwner::findOrFail($cwo_id);
                    // $cwoDetails->update([
                    //     'cwo_subscription_id'=> $cwo_subscription_id,
                    //     'start_date'=> $today,
                    //     'end_date'=> $end_date,
                    //     // 'is_paid'=> 0,
                    //     'updated_at'=>$dt->toDayDateTimeString()
                    //     ]);
                }
        
                return response()->json([
                    'success' => 1,
                    'message' => 'Success',
                    'cwo_id' => $cwo_id,
                   // 'cwo_subscription_id' => $cwo_subscription_id,
                    'transaction_id'=> $t
                ]);
            }
           
    
        }

        public function cwoSubscription(){
            
        }
    



}
