<?
else {
    dd('3');
    $date = new Carbon($request->schedule_start_date);

    $count = 1;
    if ($request->schedule_type == CustomerProperty::inst_mon) {
        $count = 1;
    } elseif ($request->schedule_type == CustomerProperty::inst_bi_mon) {
        $count = 2;
    } elseif ($request->schedule_type == CustomerProperty::inst_quar) {
        $count = 3;
    } elseif ($request->schedule_type == CustomerProperty::inst_semi) {
        $count = 6;
    } elseif ($request->schedule_type == CustomerProperty::inst_ann) {
        $count = 12;
    }
    $date = $date->subMonth($count);
    $b_date = new Carbon($request->booking_date);
    $down_payment_date = new Carbon($request->down_date);












    if ($request->amount_schedule == CustomerStatement::percentage) {
        $downpayment = ($net_amount * $request->remaining_down_payment) / 100;
        $possession = ($net_amount * $request->remaining_possession) / 100;
        $installment = ($net_amount * $request->remaining_single_installment) / 100 * $request->remainig_installment_plan;
        $per_installment = ($net_amount * $request->remaining_single_installment) / 100;
    } else {
        $downpayment = $request->remaining_down_payment;
        $possession = $request->remaining_possession;
        $installment = $request->remaining_single_installment * $request->remainig_installment_plan;
        $per_installment = $request->remaining_single_installment;
    }
    // CustomerProperty::where('id', $property->id)->update(['down_payment' => $downpayment, 'possession_fee' => $possession, 'installment' => $installment, 'installment_plan' => $request->remainig_installment_plan,  'allotment_date' => $request->allotment_date, 'confirmation_date' => $request->confirmation_date, 'development_date' => $request->development_date, 'file_processing_date' => $request->file_processing_date, 'registration_date' => $request->registration_date, 'allotment_fee' => $request->remainig_allotment, 'confirmation_fee' => $request->remainig_confirmation, 'development_fee' => $request->remainig_development, 'file_processing_fee' => $request->remainig_file_processing, 'registration_fee' => $request->remainig_registration, 'booking_fee' => $request->remainig_booking]);

    $extra_amount = $downpayment + $possession + $installment + $request->remaining_allotment + $request->remaining_confirmation + $request->remaining_development + $request->remaining_file_processing + $request->remaining_registration + $request->remaining_booking +  $request->remaining_ballon + $request->remaining_balloting;

    //Make a  downpayment entry
    // CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::down_payment, 'inst_no' => 2, 'due_date' => $b_date, 'due' => $downpayment, 'balance' => $downpayment, 'surcharge' => 0]);
    if ($request->add_amount) {
        for ($i = 0; $i < count($request->add_amount); $i++) {
            CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::other, 'inst_no' => 3 + $i, 'due_date' => $request->add_date[$i], 'due' => $request->add_amount[$i], 'balance' => $request->add_amount[$i], 'surcharge' => 0]);
            $extra_amount += $request->add_amount[$i];
        }
    }
    $extra_amount -= $net_amount;



        $lastpaid_installemnt = CustomerStatement::where('customer_property_id', $request->property_id)
        ->where('payment_head', CustomerStatement::installment)
        ->where('received', '>', 0)
        ->latest('id')
        ->first();
   
        $last_paid_installment_no = 0;
 
   
        $total_istallment = $request->remainig_installment_plan; 
     
    if (!empty($lastpaid_installemnt)) {

        $cleanString = preg_replace('/[^0-9]/', '', $lastpaid_installemnt->inst_no);
        $last_paid_installment_no = (int) $cleanString;
        $date = new Carbon($lastpaid_installemnt->due_date);

        $date->subMonth($count - 1);

        $total_istallment =  $last_paid_installment_no +  $request->remainig_installment_plan ;
    }




    //Make a installment Entry
    if ($request->remaining_total_installment != 0) {
        for ($i = 1; $i <= $request->remainig_installment_plan; $i++) {
            if ($i == ($request->remainig_installment_plan)) {
                CustomerStatement::create([
                    'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                    'payment_head' => CustomerStatement::installment, 'inst_no' => $i, 'due_date' => $date->addMonth($count),
                    'due' => $per_installment - $extra_amount, 'balance' => $per_installment - $extra_amount, 'surcharge' => 0, 'inst_track' => 1
                ]);
            } else if ($i == ($request->remainig_installment_plan)) {
                CustomerStatement::create([
                    'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                    'payment_head' => CustomerStatement::installment, 'inst_no' => $i, 'due_date' => $date->addMonth($count), 'due' => $per_installment - $extra_amount,
                    'balance' => $per_installment - $extra_amount, 'surcharge' => 0, 'inst_track' => 1
                ]);
            } else {
                CustomerStatement::create([
                    'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                    'payment_head' => CustomerStatement::installment, 'inst_no' => $i, 'due_date' => $date->addMonth($count), 'due' => $per_installment,
                    'balance' => $per_installment, 'surcharge' => 0
                ]);
            }
        }
    }
    $possesion_date = $date;

    if (!empty($request->possession_date)) {
        $possesion_date = $request->possession_date;
    }

    if ($request->remaining_down_payment > 0) {
        //Make a  downpayment entry
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::down_payment, 'inst_no' => 2, 'due_date' => $down_payment_date, 'due' => $downpayment, 'balance' => $downpayment]);
    }
    //Make a possession Entry
    if ($request->remaining_possession > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::possession, 'inst_no' => 1, 'due_date' => $possesion_date, 'due' => $possession, 'balance' => $possession, 'received' => 0]);
    }
    //Make a allotment Entry
    if ($request->remaining_allotment > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::allotment, 'inst_no' => 1, 'due_date' => $request->allotment_date, 'due' => $request->remaining_allotment, 'balance' => $request->remaining_allotment, 'received' => 0]);
    }
    //Make a confirmation Entry
    if ($request->remaining_confirmation > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::confirmation, 'inst_no' => 1, 'due_date' => $request->confirmation_date, 'due' => $request->remaining_confirmation, 'balance' => $request->remaining_confirmation, 'received' => 0]);
    }

    //Make a booking Entry
    if ($request->remaining_booking > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::booking, 'inst_no' => 1, 'due_date' => $request->booking_date, 'due' => $request->remaining_booking, 'balance' => $request->remaining_booking, 'received' => 0]);
    }
    //Make a ballon Entry
    if ($request->remaining_ballon > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::ballon, 'inst_no' => 1, 'due_date' => $request->ballonpayment_date, 'due' => $request->remaining_ballon, 'balance' => $request->remaining_ballon, 'received' => 0]);
    }
    //Make a balloting Entry
    if ($request->remaining_balloting > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::balloting, 'inst_no' => 1, 'due_date' => $request->balloting_date, 'due' => $request->remaining_balloting, 'balance' => $request->remaining_balloting, 'received' => 0]);
    }

    //Make a file_processing Entry
    if ($request->remaining_file_processing > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::file_processing, 'inst_no' => 1, 'due_date' => $request->file_processing_date, 'due' => $request->remaining_file_processing, 'balance' => $request->remaining_file_processing, 'received' => 0]);
    }
    //Make a registration Entry
    if ($request->remaining_registration > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::registration, 'inst_no' => 1, 'due_date' => $request->registration_date, 'due' => $request->remaining_registration, 'balance' => $request->remaining_registration, 'received' => 0]);
    }
   
    $property->update(['schedular_type' => $request->schedule_type]);
}

else if ($request->schedule_type == CustomerProperty::inst_mon_w_ann || $request->schedule_type == CustomerProperty::inst_mon_w_semi || $request->schedule_type == CustomerProperty::inst_mon_w_quar || $request->schedule_type == CustomerProperty::inst_quar_w_semi) {

    $date = new Carbon($request->schedule_start_date);
    $date = $date->subMonth();
    $b_date = new Carbon($request->booking_date);
    $down_payment_date = new Carbon($request->down_date);
    $tenure = $request->total_annual_installement_count;
    $total_count = $request->total_annual_installment_count;

    if ($request->amount_schedule == CustomerStatement::percentage) {
        $downpayment = ($net_amount * $request->remaining_down_payment) / 100;
        $possession = ($net_amount * $request->remaining_possession) / 100;
        $monthy_installment = ($net_amount * $request->remaining_single_installment) / 100 * $request->remainig_installment_plan;
        $per_monthly_inst = ($net_amount * $request->remaining_single_installment) / 100;
        if ($request->schedule_type == CustomerProperty::inst_mon_w_ann) {
            $semi_installment = ($net_amount * $request->annual_installment) / 100 * $request->total_annual_installment_count;
            $tenure = 12;
            $total_count = $request->total_annual_installment_count;
        } elseif ($request->schedule_type == CustomerProperty::inst_mon_w_semi || $request->schedule_type == CustomerProperty::inst_quar_w_semi) {
            $semi_installment = ($net_amount * $request->semi_installment) / 100 * $request->total_semi_installment_count;
            $tenure = 6;
            $total_count = $request->total_semi_installment_count;
        } else {
            $semi_installment = ($net_amount * $request->quarterly_installment) / 100 * $request->total_quarterly_installment_count;
            $tenure = 3;
            $total_count = $request->total_quarterly_installment_count;
        }
        $per_semi_installment = $semi_installment;
    } else {
        $downpayment = $request->remaining_down_payment;
        $possession = $request->remaining_possession;
        $monthy_installment = $request->remaining_single_installment * $request->remainig_installment_plan;
        $per_monthly_inst = $request->remaining_single_installment;
        if ($request->schedule_type == CustomerProperty::inst_mon_w_ann) {
            $semi_installment = $request->annual_installment * $request->total_annual_installment_count;
            $tenure = 12;
            $total_count = $request->total_annual_installment_count;
            $per_semi_installment = $request->annual_installment;
        } elseif ($request->schedule_type == CustomerProperty::inst_mon_w_semi || $request->schedule_type == CustomerProperty::inst_quar_w_semi) {
            $semi_installment = $request->semi_installment * $request->total_semi_installment_count;
            $tenure = ($request->schedule_type == CustomerProperty::inst_quar_w_semi) ? 3 : 6;
            $total_count = $request->total_semi_installment_count;
            $per_semi_installment = $request->semi_installment;
        } else {
            $semi_installment = $request->quarterly_installment * $request->total_quarterly_installment_count;
            $tenure = 3;
            $total_count = $request->total_quarterly_installment_count;
            $per_semi_installment = $request->quarterly_installment;
        }
       


        //   $possession_fee = $request->possession_fee;
    }

    $extra_amount = $downpayment + $possession + $monthy_installment + $semi_installment + $request->remaining_allotment + $request->remaining_confirmation  + $request->remaining_file_processing + $request->remaining_registration + $request->remaining_booking +  $request->remaining_ballon + $request->remaining_balloting;



    if ($request->add_amount) {
        for ($i = 0; $i < count($request->add_amount); $i++) {
            CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::other, 'inst_no' => 3 + $i, 'due_date' => $request->add_date[$i], 'due' => $request->add_amount[$i], 'balance' => $request->add_amount[$i], 'surcharge' => 0]);
            $extra_amount += $request->add_amount[$i];
        }
    }


    $extra_amount -= $net_amount;

    $addMonth = ($request->schedule_type == CustomerProperty::inst_quar_w_semi) ? 3 : 1;
    $date->subMonth($addMonth - 1);
    $total_months = $total_count;
    $lastpaid_installemnt = CustomerStatement::where('customer_property_id', $request->property_id)
        ->where('payment_head', CustomerStatement::installment)
        ->where('received', '>', 0)
        ->latest('id')
        ->first();
    $last_paid_installment_no = 0;
 

        $total_istallment = $request->remainig_installment_plan; 
     
  
    if (!empty($lastpaid_installemnt)) {

        $cleanString = preg_replace('/[^0-9]/', '', $lastpaid_installemnt->inst_no);
        $last_paid_installment_no = (int) $cleanString;
        $date = new Carbon($lastpaid_installemnt->due_date);

        $date->subMonth($addMonth - 1);

        $total_istallment =  $last_paid_installment_no +  $request->remainig_installment_plan ;
    }

     
    if ($request->remaining_total_installment != 0) {
        $monthy_installment = $request->remaining_total_installment;
        
        if ($request->schedule_type == CustomerProperty::inst_mon_w_ann){
            $semi_installment = $request->total_annual_installment;
        }
        elseif ($request->schedule_type == CustomerProperty::inst_mon_w_semi || $request->schedule_type == CustomerProperty::inst_quar_w_semi) {

            $semi_installment = $request->total_semi_installment;
        }
        else{
            $semi_installment = $request->total_quarterly_installment;
        }
        $total_monthly_installemt_amount = $monthy_installment;
        $total_semi_installemt_amount = $semi_installment;

        $sum_istallment_amount =  $total_monthly_installemt_amount + $total_semi_installemt_amount;
 
        $totalcounts = $total_istallment;
        // dd($request->remainig_installment_plan);
        for ($i =  $last_paid_installment_no+1; $i <= ( $total_istallment); $i++) {
            if ($i % $tenure == 0 && $total_count > 0) {
                
                if ($i == ( $totalcounts)) {

              
                    // else{
                        $add =   $per_semi_installment + $per_monthly_inst - $extra_amount;
              
                        dd($i,$add);
                    $last_paid_installment_no =   $last_paid_installment_no + 1;
                    CustomerStatement::create([
                        'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                        'payment_head' => CustomerStatement::installment, 'inst_no' =>$last_paid_installment_no , 'due_date' => $date->addMonth($addMonth), 'due' => $add,
                        'balance' => $add, 'surcharge' => 0, 'inst_track' => 1
                    ]);
                   
                    if ($sum_istallment_amount >= $add) {
                        $sum_istallment_amount =  $sum_istallment_amount -$add;
                      }
                // }
            }
                else {
                  
                    if ($i == $totalcounts) {
                                            if ( $sum_istallment_amount > 0 ) {
                                                $last_paid_installment_no =   $last_paid_installment_no + 1;
                                                CustomerStatement::create([
                                                    'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                                    'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' =>  $sum_istallment_amount,
                                                    'balance' =>   $sum_istallment_amount, 'surcharge' => 0, 'inst_track' => 1
                                                ]);
                                                
                                                $sum_istallment_amount = 0;
                                                 
                                            }   
                                            
                                        }
                                        else{
                                         $add=   $per_semi_installment + $per_monthly_inst;
                                         
                    $last_paid_installment_no =   $last_paid_installment_no + 1;
                    CustomerStatement::create([
                        'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                        'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' => $per_semi_installment + $per_monthly_inst,
                        'balance' => $add, 'surcharge' => 0, 'inst_track' => 1
                    ]);
                    // $sum_istallment_amount =  $sum_istallment_amount -$per_semi_installment + $per_monthly_inst;
                    if ($sum_istallment_amount >= $add) {
                        $sum_istallment_amount =  $sum_istallment_amount -$add;
                      }
                }
            }
                $total_count--;
            } else if ($i == ($request->remainig_installment_plan)) {

                if($i==30){
                    dd('1',$i);
                }
                if ($i == $totalcounts) {
                    if ( $sum_istallment_amount > 0 ) {
                        $last_paid_installment_no =   $last_paid_installment_no + 1;
                        CustomerStatement::create([
                            'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                            'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' =>  $sum_istallment_amount,
                            'balance' =>   $sum_istallment_amount, 'surcharge' => 0
                        ]);
                        
                        $sum_istallment_amount = 0;
                         
                    }   
                    
                }
                else{
                $last_paid_installment_no =   $last_paid_installment_no + 1;
               $sum= $per_monthly_inst - $extra_amount;
                CustomerStatement::create([
                    'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                    'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' => $per_monthly_inst - $extra_amount,
                    'balance' => $per_monthly_inst - $extra_amount, 'surcharge' => 0
                ]);
                // $sum_istallment_amount =  $sum_istallment_amount -$per_monthly_inst - $extra_amount;
                if ($sum_istallment_amount >= $sum) {
                    $sum_istallment_amount = $sum_istallment_amount - $sum;
                  } 
            }                           
            } else {
                // if($i==30){
                //     // dd('2',$i);
                // }
                if ($i == $totalcounts) {
                    // if($i==30){
             
                    // dd('2',$i,$sum_istallment_amount,$per_monthly_inst);
                    // }
                if ( $sum_istallment_amount > 0 ) {
                    $last_paid_installment_no =   $last_paid_installment_no + 1;
                    CustomerStatement::create([
                        'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                        'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' =>  $sum_istallment_amount,
                        'balance' =>   $sum_istallment_amount, 'surcharge' => 0
                    ]);
                    
                    $sum_istallment_amount = 0;
                     
                }
            }
                else{

                    
                        $last_paid_installment_no =   $last_paid_installment_no + 1;
                        CustomerStatement::create([
                            'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                            'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' => $per_monthly_inst,
                            'balance' => $per_monthly_inst, 'surcharge' => 0
                        ]);
                        // $sum_istallment_amount =  $sum_istallment_amount -$per_monthly_inst;
                        if ($sum_istallment_amount >= $per_monthly_inst) {
                            $sum_istallment_amount = $sum_istallment_amount -  $per_monthly_inst;
                          } 
                   
                   
              
            }

        }
        }

        // dd( $sum_istallment_amount);
    }

                        // dd($total_istallment, $sum_istallment_amount,$total_monthly_installemt_amount + $total_semi_installemt_amount);
    // // dd('totla');



    //other paymentheads
    $possesion_date = $date;

    if (!empty($request->possession_date)) {
        $possesion_date = $request->possession_date;
    }
    if ($request->remaining_down_payment > 0) {
        //Make a  downpayment entry
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::down_payment, 'inst_no' => 2, 'due_date' => $b_date, 'due' => $downpayment, 'balance' => $downpayment]);
    }

    //Make a possession Entry
    if ($request->remaining_possession > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::possession, 'inst_no' => 1, 'due_date' => $possesion_date, 'due' => $possession, 'balance' => $possession, 'received' => 0]);
    }
    //Make a allotment Entry
    if ($request->remaining_allotment > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::allotment, 'inst_no' => 1, 'due_date' => $request->allotment_date, 'due' => $request->remaining_allotment, 'balance' => $request->remaining_allotment, 'received' => 0]);
    }
    //Make a confirmation Entry
    if ($request->remaining_confirmation > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::confirmation, 'inst_no' => 1, 'due_date' => $request->confirmation_date, 'due' => $request->remaining_confirmation, 'balance' => $request->remaining_confirmation, 'received' => 0]);
    }

    //Make a booking Entry
    if ($request->remaining_booking > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::booking, 'inst_no' => 1, 'due_date' => $request->booking_date, 'due' => $request->remaining_booking, 'balance' => $request->remaining_booking, 'received' => 0]);
    }
    //Make a ballon Entry
    if ($request->remaining_ballon > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::ballon, 'inst_no' => 1, 'due_date' => $request->ballonpayment_date, 'due' => $request->remaining_ballon, 'balance' => $request->remaining_ballon, 'received' => 0]);
    }

    //Make a balloting Entry
    if ($request->remaining_balloting > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::balloting, 'inst_no' => 1, 'due_date' => $request->balloting_date, 'due' => $request->remaining_balloting, 'balance' => $request->remaining_balloting, 'received' => 0]);
    }
    //Make a file_processing Entry
    if ($request->remaining_file_processing > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::file_processing, 'inst_no' => 1, 'due_date' => $request->file_processing_date, 'due' => $request->remaining_file_processing, 'balance' => $request->remaining_file_processing, 'received' => 0]);
    }
    //Make a registration Entry
    if ($request->remaining_registration > 0) {
        CustomerStatement::create(['customer_id' => $customer->id, 'customer_property_id' => $property->id, 'payment_head' => CustomerStatement::registration, 'inst_no' => 1, 'due_date' => $request->registration_date, 'due' => $request->remaining_registration, 'balance' => $request->remaining_registration, 'received' => 0]);
    }

    $property->update(['schedular_type' => $request->schedule_type]);
}



  $lastpaid_installemnt =CustomerStatement::where('customer_property_id', $request->property_id)
                   ->where('payment_head', CustomerStatement::installment)
                   ->where('received', '>', 0)
                   ->latest('id')
                   ->first();
                   $last_paid_installment_no = 0;
                   $total_istallment=$request->remainig_installment_plan + $total_months;
                   if(!empty($lastpaid_installemnt)){

                    $cleanString = preg_replace('/[^0-9]/', '', $lastpaid_installemnt->inst_no);
                    $last_paid_installment_no = (int) $cleanString;
                    $date = new Carbon($lastpaid_installemnt->due_date);

                    $date->subMonth($addMonth - 1);
                     
                    $total_istallment =  $last_paid_installment_no+  $request->remainig_installment_plan + $total_months;
                   }
                 
             
            
                    if ($request->remaining_total_installment != 0) {
                      
                        $monthy_installment=$request->remaining_total_installment;
                        $semi_installment=$request->total_semi_installment;
                        $total_monthly_installemt_amount = $monthy_installment;
                        $total_semi_installemt_amount = $semi_installment;

                         
                        $totalcounts = $total_istallment;
                     
                       
                        for ($i =  $last_paid_installment_no+1; $i <= ( $total_istallment); $i++) {
                            
                            if ($i % $tenure == 0 && $total_count > 0) {
                                if ($i == ($request->remainig_installment_plan)) {
                                    $add =   $per_semi_installment - $extra_amount;
                                    
                                    if ($i == $totalcounts) {
                                        if ($total_monthly_installemt_amount > 0 && $total_semi_installemt_amount > 0) {
                                            $last_paid_installment_no =   $last_paid_installment_no + 1;
                                            CustomerStatement::create([
                                                'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                                'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' => $total_monthly_installemt_amount + $total_semi_installemt_amount,
                                                'balance' =>  $total_monthly_installemt_amount + $total_semi_installemt_amount, 'surcharge' => 0, 'inst_track' => 1
                                            ]);
                                            
                                            $total_monthly_installemt_amount = 0;
                                            $total_semi_installemt_amount = 0;
                                        } else if ($total_monthly_installemt_amount > 0 && $total_semi_installemt_amount <= 0) {
                                            $last_paid_installment_no =   $last_paid_installment_no + 1;
                                            CustomerStatement::create([
                                                'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                                'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' =>  $total_monthly_installemt_amount,
                                                'balance' =>  $total_monthly_installemt_amount, 'surcharge' => 0, 'inst_track' => 1
                                            ]);
                                            $total_monthly_installemt_amount = 0;
                                        } else if ($total_monthly_installemt_amount <= 0 && $total_semi_installemt_amount > 0) {
                                            $last_paid_installment_no =   $last_paid_installment_no + 1;
                                            CustomerStatement::create([
                                                'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                                'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' =>  $total_semi_installemt_amount,
                                                'balance' =>  $total_semi_installemt_amount, 'surcharge' => 0, 'inst_track' => 1
                                            ]);
                                            $total_semi_installemt_amount = 0;
                                        }
                                    } else {
                                        $last_paid_installment_no =   $last_paid_installment_no + 1;
                                        CustomerStatement::create([
                                            'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                            'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' =>  $add,
                                            'balance' =>  $add, 'surcharge' => 0, 'inst_track' => 1
                                        ]);
                                        $total_semi_installemt_amount = $total_semi_installemt_amount -  $add;
                                    }
                                } else {

                                    if ($i == $totalcounts) {
                                        if ($total_monthly_installemt_amount > 0 && $total_semi_installemt_amount > 0) {
                                            $last_paid_installment_no =   $last_paid_installment_no + 1;
                                            CustomerStatement::create([
                                                'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                                'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' => $total_monthly_installemt_amount + $total_semi_installemt_amount,
                                                'balance' =>  $total_monthly_installemt_amount + $total_semi_installemt_amount, 'surcharge' => 0, 'inst_track' => 1
                                            ]);
                                            $total_monthly_installemt_amount = 0;
                                            $total_semi_installemt_amount = 0;
                                        } else if ($total_monthly_installemt_amount > 0 && $total_semi_installemt_amount <= 0) {
                                            $last_paid_installment_no =   $last_paid_installment_no + 1;
                                            CustomerStatement::create([
                                                'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                                'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' =>  $total_monthly_installemt_amount,
                                                'balance' =>  $total_monthly_installemt_amount, 'surcharge' => 0, 'inst_track' => 1
                                            ]);
                                            $total_monthly_installemt_amount = 0;
                                        } else if ($total_monthly_installemt_amount <= 0 && $total_semi_installemt_amount > 0) {
                                            $last_paid_installment_no =   $last_paid_installment_no + 1;
                                            CustomerStatement::create([
                                                'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                                'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' =>  $total_semi_installemt_amount,
                                                'balance' =>  $total_semi_installemt_amount, 'surcharge' => 0, 'inst_track' => 1
                                            ]);
                                            $total_semi_installemt_amount = 0;
                                        }
                                    } else {
                                        $last_paid_installment_no =   $last_paid_installment_no + 1;
                                        CustomerStatement::create([
                                            'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                            'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' => $per_semi_installment,
                                            'balance' => $per_semi_installment, 'surcharge' => 0, 'inst_track' => 1
                                        ]);
                                        $total_semi_installemt_amount =$total_semi_installemt_amount - $per_semi_installment;
                                    }
                                }
                                $total_count--;
                            } else if ($i == ($request->remainig_installment_plan)) {
                               
                                $add =  $per_monthly_inst - $extra_amount;
                                if ($i == $totalcounts) {
                                    if ($total_monthly_installemt_amount > 0 && $total_semi_installemt_amount > 0) {
                                        $last_paid_installment_no =   $last_paid_installment_no + 1;
                                        CustomerStatement::create([
                                            'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                            'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' => $total_monthly_installemt_amount + $total_semi_installemt_amount,
                                            'balance' =>  $total_monthly_installemt_amount + $total_semi_installemt_amount, 'surcharge' => 0, 'inst_track' => 1
                                        ]);
                                        $total_monthly_installemt_amount = 0;
                                        $total_semi_installemt_amount = 0;
                                    } else if ($total_monthly_installemt_amount > 0 && $total_semi_installemt_amount <= 0) {
                                        $last_paid_installment_no =   $last_paid_installment_no + 1;
                                        CustomerStatement::create([
                                            'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                            'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' =>  $total_monthly_installemt_amount,
                                            'balance' =>  $total_monthly_installemt_amount, 'surcharge' => 0
                                        ]);
                                        $total_monthly_installemt_amount = 0;
                                    } else if ($total_monthly_installemt_amount <= 0 && $total_semi_installemt_amount > 0) {

                                        $last_paid_installment_no =   $last_paid_installment_no + 1;
                                        CustomerStatement::create([
                                            'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                            'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' =>  $total_semi_installemt_amount,
                                            'balance' =>  $total_semi_installemt_amount, 'surcharge' => 0
                                        ]);
                                        $total_semi_installemt_amount = 0;
                                    }
                                } else {
                                    $last_paid_installment_no =   $last_paid_installment_no + 1;
                                    CustomerStatement::create([
                                        'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                        'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' => $per_monthly_inst - $extra_amount,
                                        'balance' => $add, 'surcharge' => 0
                                    ]);
                                    $total_monthly_installemt_amount =  $total_monthly_installemt_amount - $add;
                                }
                            } else {
                             
                                if ($i == $totalcounts) {
                                   
                                    if ($total_monthly_installemt_amount > 0 && $total_semi_installemt_amount > 0) {
                                        $last_paid_installment_no =   $last_paid_installment_no + 1;
                                        CustomerStatement::create([
                                            'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                            'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' => $total_monthly_installemt_amount + $total_semi_installemt_amount,
                                            'balance' =>  $total_monthly_installemt_amount + $total_semi_installemt_amount, 'surcharge' => 0
                                        ]);
                                        $total_monthly_installemt_amount = 0;
                                        $total_semi_installemt_amount = 0;
                                    } else if ($total_monthly_installemt_amount > 0 && $total_semi_installemt_amount <= 0) {
                                        $last_paid_installment_no =   $last_paid_installment_no + 1;
                                        CustomerStatement::create([
                                            'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                            'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' =>  $total_monthly_installemt_amount,
                                            'balance' =>  $total_monthly_installemt_amount, 'surcharge' => 0
                                        ]);
                                        $total_monthly_installemt_amount = 0;
                                    } else if ($total_monthly_installemt_amount <= 0 && $total_semi_installemt_amount > 0) {
                                        $last_paid_installment_no =   $last_paid_installment_no + 1;
                                        CustomerStatement::create([
                                            'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                            'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' =>  $total_semi_installemt_amount,
                                            'balance' =>  $total_semi_installemt_amount, 'surcharge' => 0
                                        ]);
                                        $total_semi_installemt_amount = 0;
                                    }
                                } else {
                                    if($total_monthly_installemt_amount >= $per_monthly_inst){
                                  
                                    $last_paid_installment_no =   $last_paid_installment_no + 1;
                                    CustomerStatement::create([
                                        'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                        'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' => $per_monthly_inst,
                                        'balance' => $per_monthly_inst, 'surcharge' => 0
                                    ]);
                                    $total_monthly_installemt_amount =   $total_monthly_installemt_amount - $per_monthly_inst;
                                }
                                else if($total_monthly_installemt_amount > 0){
                                    $last_paid_installment_no =   $last_paid_installment_no + 1;
                                $amnt =$total_monthly_installemt_amount;
                                    CustomerStatement::create([
                                        'customer_id' => $customer->id, 'customer_property_id' => $property->id,
                                        'payment_head' => CustomerStatement::installment, 'inst_no' => $last_paid_installment_no, 'due_date' => $date->addMonth($addMonth), 'due' => $amnt,
                                        'balance' =>$amnt, 'surcharge' => 0
                                    ]);
                                    $total_monthly_installemt_amount =   $total_monthly_installemt_amount -$amnt;
                                }
                            }
                            }
                        }
                    }