<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use Illuminate\Http\Request;
use DB;
use Validator;
use App\Http\Model\LoanAccPanelModel;

class LoanAccPanelController extends Controller
{
	public function __construct()
	{
		$this->LoanAccPanelMod= new LoanAccPanelModel;
	}
	public function LoanApplicantpanel(Request $request)
    {
		$id = $request->input('lacc_id');
		
		//### Coding to Calculate Delay payment starts here
		$data['LoanApplicantRecords']=$this->LoanAccPanelMod->viewloanapplicantion($id);
		$data['LoanInstallmentRecords']=$this->LoanAccPanelMod->viewLoanInstallmentRecords($id);
		$data['LoanTotalPaid']=$this->LoanAccPanelMod->LoanTotalPaid($id); //Total paid
		
		foreach($data['LoanApplicantRecords'] as $LoanApplicantRec1)
		{
			$delaypmt_charge = $LoanApplicantRec1->dpmt_charge;
		}
	
		$totinterestamt =0;
		$tototheramt = 0;
		$totemiamt = 0;
		$totpaidamt = 0;
		$totbalamt = 0;
		$btn=0;
		$paid_amt = 0;
		$bal_amt=0;
		$totdelaypayment=0;
		$totpaiddecreaseonloop = $data['LoanTotalPaid'];		
		foreach($data['LoanInstallmentRecords'] as $LoanInstallmentRec)
		{
			$lins_amt=$LoanInstallmentRec->lins_amt;						
			$lins_iamt=$LoanInstallmentRec->lins_iamt;						
			$othr_amt=$LoanInstallmentRec->penaltysum;
			$totamt = $lins_amt+$lins_iamt+$othr_amt;
			
			//Sum Total
			$totinterestamt = $totinterestamt +  $lins_iamt;
			$tototheramt = $tototheramt + $othr_amt;
			$totemiamt = $totemiamt+ ($lins_amt+$lins_iamt+$othr_amt);
			$totdelaypayment = $totdelaypayment + 0;
			
			if($totpaiddecreaseonloop > $totamt)
			{
				$paid_amt=$totamt;
				$totpaiddecreaseonloop = $totpaiddecreaseonloop - $totamt;
			}
			else
			{
				$paid_amt = $totpaiddecreaseonloop;
				$totpaiddecreaseonloop = 0;
			}
									
			$bal_amt=$totamt-$paid_amt;
			
			$totpaidamt = $totpaidamt + $paid_amt;
			$totbalamt = $totbalamt + $bal_amt;
			
			//Coding to Calculate days difference between two days start here	
			$currentdt = strtotime(date("Y-m-d")); //Current day
			$installmentdt = strtotime($LoanInstallmentRec->lins_date); //Installment day
			$datediff = $currentdt - $installmentdt;	
			$daysdifference =  round($datediff / (60 * 60 * 24)) ;
			if($daysdifference >=0)
			{
				if($bal_amt > 0)
				{
					//$daysdifference=$daysdifference;
					$delaydet['lacc_id'] = $id;
					$delaydet['lins_id'] = $LoanInstallmentRec->lins_id;
					$delaydet['daysdifference'] = $daysdifference;
					$delaydet['ltr_amt'] = $bal_amt;
					$delaydet['delaypmt_charge'] = $delaypmt_charge;
					$delaypay = $this->LoanAccPanelMod->AddDelayPaymentPenalty($delaydet);
				}
			}
			//Coding to Calculate days difference between two days ends here
		}
		//### Coding to Calculate Delay payment ends here
			
		$data['LoanApplicantRecords']=$this->LoanAccPanelMod->viewloanapplicantion($id);
		$data['LoanInstallmentRecords']=$this->LoanAccPanelMod->viewLoanInstallmentRecords($id);
		$data['LoanTotalPaid']=$this->LoanAccPanelMod->LoanTotalPaid($id); //Total paid
		return view('LoanApp.loanacpanel',compact('data'));
    }
	public function LoanPaymentCalculationCtrl(Request $request)
	{
		$id = $request->input('lins_id');
		$prevpaid= 0;
		$currpay=$this->LoanAccPanelMod->LoancurrentPayable($id); //current payable amount
		$Currentpaid=$this->LoanAccPanelMod->LoanCurrentPaid($id); //Current paid amount
		$prevpay=$this->LoanAccPanelMod->LoanpreviousPayable($id); //previous payable amount
		$Previouspaid=$this->LoanAccPanelMod->LoanPreviousPaid($id); //Previous paid amount
		$data['CurrentPayableAmt'] = ($currpay) - ($Currentpaid);
		$prevpaid = ($prevpay) - ($Currentpaid+$Previouspaid);
		if($prevpaid < 0)
		{
			$prevpaid = 0;
			$data['CurrentPayableAmt'] =  round(($currpay+$prevpay) - ($Currentpaid+$Previouspaid));
		}
		$data['PreviousPayableAmt'] = round($prevpaid);
		$data['TotalPayableAmt'] = round($data['CurrentPayableAmt']+$data['PreviousPayableAmt']);
		return json_encode($data);
	}	
	public function LoanEditPaymentCtrl(Request $request)
	{
		$id = $request->input('lins_id');
		$data = $this->LoanAccPanelMod->ModEditPaymentCtrl($id);
		return json_encode($data);
	}	
	public function LoanDelayPmtCalcCtrl(Request $request)
	{
		$lacc_id = $request->input('lacc_id');
		$id = $request->input('lins_id');
		$prevpaid= 0;
		//##########
		$data['LoanApplicantRecords']=$this->LoanAccPanelMod->viewloanapplicantion($lacc_id);
		foreach($data['LoanApplicantRecords'] as $LoanApplicantRec1)
		{
			$data['delaypmt_charge'] = $LoanApplicantRec1->dpmt_charge;
		}
		//##########
		//##########
		$data['LoanSingleInsRecord']=$this->LoanAccPanelMod->viewloansingleinstallmentdetail($id);
		foreach($data['LoanSingleInsRecord'] as $LoanSingleInsRecord1)
		{
			$currentdt = date("Y-m-d");
			$data['lins_date'] = $LoanSingleInsRecord1->lins_date;
				
			$date1_ts = strtotime($currentdt);
			$date2_ts = strtotime($data['lins_date']);
			$diff = $date1_ts - $date2_ts ;
			$data['nodays'] = round($diff / 86400);
		}
		//##########
		$currpay=$this->LoanAccPanelMod->LoancurrentPayable($id); //current payable amount
		$Currentpaid=$this->LoanAccPanelMod->LoanCurrentPaid($id); //Current paid amount
		$prevpay=$this->LoanAccPanelMod->LoanpreviousPayable($id); //previous payable amount
		$Previouspaid=$this->LoanAccPanelMod->LoanPreviousPaid($id); //Previous paid amount
		$data['CurrentPayableAmt'] = ($currpay) - ($Currentpaid);
		$prevpaid = ($prevpay) - ($Currentpaid+$Previouspaid);
		if($prevpaid < 0)
		{
			$prevpaid = 0;
			$data['CurrentPayableAmt'] =  ($currpay+$prevpay) - ($Currentpaid+$Previouspaid);
		}
		$data['PreviousPayableAmt'] = $prevpaid;
		$data['TotalPayableAmt'] = $data['CurrentPayableAmt']+$data['PreviousPayableAmt'];
			
		//Coding to Calculate Delay payment charge starts here
		$delaysum = $data['CurrentPayableAmt'] * $data['delaypmt_charge']/100;
		$data['totaldelaysum'] = $delaysum /365 * $data['nodays'];
		//Coding to Calculate Delay payment charge ends here
		return json_encode($data);
	}
	
	public function LoanDelayPmtreCalcCtrl(Request $request)
	{
		$lacc_id = $request->input('lacc_id');
		$id = $request->input('lins_id');
		$prevpaid= 0;
		//##########
		$data['delaypmt_charge'] = $request->input('dpmtdelayinterest');
		//##########
		//##########
		$currentdt = $request->input('dpmttdt');
		$data['lins_date'] = $request->input('dpmtfdt');
		
		$date1_ts = strtotime($currentdt);
		$date2_ts = strtotime($data['lins_date']);
		$diff = $date1_ts - $date2_ts ;
		$data['nodays'] = round($diff / 86400);
		//##########
		$currpay=$this->LoanAccPanelMod->LoancurrentPayable($id); //current payable amount
		$Currentpaid=$this->LoanAccPanelMod->LoanCurrentPaid($id); //Current paid amount
		$prevpay=$this->LoanAccPanelMod->LoanpreviousPayable($id); //previous payable amount
		$Previouspaid=$this->LoanAccPanelMod->LoanPreviousPaid($id); //Previous paid amount
		$data['CurrentPayableAmt'] = $request->input('dpmtemiamt');;
		$prevpaid = ($prevpay) - ($Currentpaid+$Previouspaid);
		if($prevpaid < 0)
		{
			$prevpaid = 0;
			$data['CurrentPayableAmt'] =  $request->input('dpmtemiamt');;
		}
		$data['PreviousPayableAmt'] = $prevpaid;
		$data['TotalPayableAmt'] = $data['CurrentPayableAmt']+$data['PreviousPayableAmt'];
		//Coding to Calculate Delay payment charge starts here
		$delaysum = $data['CurrentPayableAmt'] * $data['delaypmt_charge']/100;
		$data['totaldelaysum'] = $delaysum /365 * $data['nodays'];
		//Coding to Calculate Delay payment charge ends here
		return json_encode($data);
	}
	
	public function LoanPaymentInsertCtrl(Request $req)
	{
		$data['lacc_id'] = $req->input('lacc_id');
		$data['lins_id'] = $req->input('lins_id');
		$data['ltr_transdt'] = $req->input('ltr_transdt');
		$data['ltr_transtype'] = $req->input('ltr_transtype');
		$data['ltr_billno'] = $req->input('ltr_billno');
		$data['ltr_amt'] = $req->input('ltr_amt');
		$data['ltr_paymenttype'] = $req->input('ltr_paymenttype');
		$data['ltr_chqno'] = $req->input('ltr_chqno');
		$data['ltr_bank'] = $req->input('ltr_bank');
		$data['ltr_note'] = $req->input('ltr_note');
		$data['ltr_status'] = $req->input('ltr_status');
		$data['ltr_chq_bounce_id'] = 0;
		$rec=$this->LoanAccPanelMod->LoanPaymentInsertMod($data); //Previous paid amount
		return json_encode($rec);
	}	
	public function LoanPaymentUpdateCtrl(Request $req)
	{
		$data['ltr_id'] = $req->input('ltr_id');
		$data['lacc_id'] = $req->input('lacc_id');
		$data['lins_id'] = $req->input('lins_id');
		$data['ltr_transdt'] = $req->input('ltr_transdt');
		$data['ltr_transtype'] = $req->input('ltr_transtype');
		$data['ltr_billno'] = $req->input('ltr_billno');
		$data['ltr_amt'] = $req->input('ltr_amt');
		$data['ltr_paymenttype'] = $req->input('ltr_paymenttype');
		$data['ltr_chqno'] = $req->input('ltr_chqno');
		$data['ltr_bank'] = $req->input('ltr_bank');
		$data['ltr_note'] = $req->input('ltr_note');
		$data['ltr_status'] = $req->input('ltr_status');
		$data['ltr_chq_bounce_id'] = 0;
		$rec=$this->LoanAccPanelMod->LoanPaymentUpdateMod($data); //Previous paid amount
		return json_encode($rec);
	}	
	public function loanAccountPreclosureCtrl(Request $request)
    {
		$id = $request->input('lacc_id');
		$data['LoanApplicantRecords']=$this->LoanAccPanelMod->viewloanapplicantion($id);
		$data['LoanInstallmentRecords']=$this->LoanAccPanelMod->viewLoanInstallmentRecords($id);
		$data['LoanTotalPaid']=$this->LoanAccPanelMod->LoanTotalPaid($id); //Total paid
		return view('LoanApp.loanpreclosure',compact('data'));
    }
	
	public function loanrptsingleCtrl(Request $req)
	{
		$id = $req->input('lins_id');
		$datarec="";
		
		//###Loan Single Report###
		$LoanRpt=$this->LoanAccPanelMod->viewLoanRptSingle($id);
		$LoanSingleEMIrpt=$this->LoanAccPanelMod->viewloansingleEMIdetail($id);
		$datarec .= "<table id='datatable-data-export' class='table table-sorting table-striped table-hover table-bordered datatable'>
					<thead>
						<tr>
							<th>Dates</th>
							<th>Transaction type</th>
							<th>Particulars</th>
							<th>Credit</th>
							<th>Paid</th>		
							<th><span class='fa fa-unlink'></span></th>
						</tr>
					</thead>";
					$datarec .= "<tbody>";
		foreach($LoanRpt as $LoanApplicantRec)
		{
			$dt = date("d-m-Y",strtotime($LoanApplicantRec->ltr_transdt));
			if($LoanApplicantRec->ltr_status=="Cancelled")
			{
				$datarec .= "<tr style='text-decoration: line-through;'>";
			}
			else
			{
				$datarec .= "<tr>";
			}
					$datarec .= "<td>$dt</td>";
					//#######Transaction Type Starts here #######
					$datarec .= "<td>";
					if($LoanApplicantRec->ltr_transtype=="Principle" || $LoanApplicantRec->ltr_transtype=="Interest" )
					{
					$datarec .= "<b style='color: blue;' >" . $LoanApplicantRec->ltr_transtype . "</b>";
					}
					if($LoanApplicantRec->ltr_transtype=="Cheque Bounce" || $LoanApplicantRec->ltr_transtype=="Case" || $LoanApplicantRec->ltr_transtype=="Delay payment" || $LoanApplicantRec->ltr_transtype=="Delay-Payment" || $LoanApplicantRec->ltr_transtype=="Others")
					{
					$datarec .= "<b style='color: red;' >" . $LoanApplicantRec->ltr_transtype . "</b>";
					}
					if($LoanApplicantRec->ltr_transtype=="Payment")
					{
					$txtamt = $this->convertToIndianCurrency($LoanApplicantRec->ltr_amt);
					$lins_no = $this->addOrdinalNumberSuffix($LoanSingleEMIrpt->lins_no)." EMI of Personal Loan..";
					//$datarec .=$lins_no;
					$datarec .= "<b style='color: green;'>" . $LoanApplicantRec->ltr_transtype . "</b> &nbsp; ";
					$datarec .= "<button class='btn btn-info btn-xs' onclick='funPaymentReceipt($LoanApplicantRec->ltr_billno,`$dt`,`$LoanApplicantRec->ltr_amt`,`$txtamt`,`$LoanApplicantRec->ltr_paymenttype`,`$LoanSingleEMIrpt->lins_no`,`$lins_no`)' ><i class='fa fa-print' aria-hidden='true' style='cursor: pointer' ></button>";
					}
					$datarec .= "</td>";
					//#######Transaction Type Ends here #######
					//#######Transaction Note starts here #######
					$datarec .= "<td width='350'>";
					if($LoanApplicantRec->ltr_transtype=="Payment")
					{
					$datarec .= "<b>Bill No : </b>".$LoanApplicantRec->ltr_billno."<br>";
					$datarec .= "<b>Payment Type : </b>".$LoanApplicantRec->ltr_paymenttype."<br>";
					}
					if($LoanApplicantRec->ltr_note != "")
					{
					$datarec .= "<b>Note:</b>".$LoanApplicantRec->ltr_note;
					}
					//###Cheque and Bank details starts here
					if($LoanApplicantRec->ltr_chqno != "")
					{
					$datarec .= "<br><b>Cheque:</b>".$LoanApplicantRec->ltr_chqno;
					}
					if($LoanApplicantRec->ltr_bank != "")
					{
					$datarec .= "<br><b>Bank detail:</b>".$LoanApplicantRec->ltr_bank;
					}
					//###Cheque and Bank details ends here
					if($LoanApplicantRec->ltr_status=="Cancelled")
					{
					$datarec .= "<br><button class='alert alert-danger'><b>Title:</b>". $LoanApplicantRec->ltr_cancellationtype ."<br><b>Note:</b>". $LoanApplicantRec->ltr_cancellationreason."</button>";
					}
					$datarec .= "</td>";
					//#######Transaction Note ends here #######
					if($LoanApplicantRec->ltr_transtype=="Principle" || $LoanApplicantRec->ltr_transtype=="Interest" || $LoanApplicantRec->ltr_transtype=="Cheque Bounce" || $LoanApplicantRec->ltr_transtype=="Case" || $LoanApplicantRec->ltr_transtype=="Delay payment" || $LoanApplicantRec->ltr_transtype=="Delay-Payment" || $LoanApplicantRec->ltr_transtype=="Others")
					{
					$datarec .= "<td>$LoanApplicantRec->ltr_amt</td>";
					}
					else
					{
						$datarec .= "<td>0</td>";
					}
					if($LoanApplicantRec->ltr_transtype=="Payment")
					{
					$datarec .= "<td>$LoanApplicantRec->ltr_amt</td>";
					}
					else
					{
						$datarec .= "<td>0</td>";
					}
					$datarec .= "<td>";
					if($LoanApplicantRec->ltr_transtype=="Cheque Bounce" || $LoanApplicantRec->ltr_transtype=="Case" || $LoanApplicantRec->ltr_transtype=="Delay payment"  || $LoanApplicantRec->ltr_transtype=="Delay-Payment" || $LoanApplicantRec->ltr_transtype=="Others" || $LoanApplicantRec->ltr_transtype=="Payment")
					{
						if($LoanApplicantRec->ltr_status=="Active")
						{
							//Edit Payment detail starts here..
							if($LoanApplicantRec->ltr_transtype=="Payment")
							{
						$datarec .= "<button class='btn btn-info btn-xs'  data-toggle='modal'  id='" . $LoanApplicantRec->ltr_id . "' data-target='#myeditpayment' ><span class='fa fa-pencil-square-o'></span></button><hr>";
							}
							//Edit Payment detail ends here..
							if($LoanApplicantRec->ltr_transtype=="Payment" && $LoanApplicantRec->ltr_paymenttype == "Cheque")
							{
						$datarec .= "<button class='btn btn-info btn-xs'  data-toggle='modal'  id='" . $LoanApplicantRec->ltr_id . "' data-target='#modchequecancellation' ><span class='fa fa-bomb'></span></button>";
							}
							else
							{
						$datarec .= "<button class='btn btn-info btn-xs'  data-toggle='modal'  id='" . $LoanApplicantRec->ltr_id . "' data-target='#modcancellation' ><span class='fa fa-unlink'></span></button>";
							}
						}
					}
					$datarec .= "</td>";
					$datarec .= "</tr>";
		}
					$datarec .= "</tbody>";
					$datarec .= "</table>";
		return $datarec;
	}
	
function convertToIndianCurrency($number) 
{
    $no = round($number);
    $decimal = round($number - ($no = floor($number)), 2) * 100;    
    $digits_length = strlen($no);    
    $i = 0;
    $str = array();
    $words = array(
        0 => '',
        1 => 'One',
        2 => 'Two',
        3 => 'Three',
        4 => 'Four',
        5 => 'Five',
        6 => 'Six',
        7 => 'Seven',
        8 => 'Eight',
        9 => 'Nine',
        10 => 'Ten',
        11 => 'Eleven',
        12 => 'Twelve',
        13 => 'Thirteen',
        14 => 'Fourteen',
        15 => 'Fifteen',
        16 => 'Sixteen',
        17 => 'Seventeen',
        18 => 'Eighteen',
        19 => 'Nineteen',
        20 => 'Twenty',
        30 => 'Thirty',
        40 => 'Forty',
        50 => 'Fifty',
        60 => 'Sixty',
        70 => 'Seventy',
        80 => 'Eighty',
        90 => 'Ninety');
    $digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');
    while ($i < $digits_length) {
        $divider = ($i == 2) ? 10 : 100;
        $number = floor($no % $divider);
        $no = floor($no / $divider);
        $i += $divider == 10 ? 1 : 2;
        if ($number) {
            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;            
            $str [] = ($number < 21) ? $words[$number] . ' ' . $digits[$counter] . $plural : $words[floor($number / 10) * 10] . ' ' . $words[$number % 10] . ' ' . $digits[$counter] . $plural;
        } else {
            $str [] = null;
        }  
    }
    
    $Rupees = implode(' ', array_reverse($str));
    $paise = ($decimal) ? "And Paise " . ($words[$decimal - $decimal%10]) ." " .($words[$decimal%10])  : '';
    return ($Rupees ? 'Rupees ' . $Rupees : '') . $paise . " Only";
}

function addOrdinalNumberSuffix($num) 
{
    if (!in_array(($num % 100),array(11,12,13))){
      switch ($num % 10) {
        // Handle 1st, 2nd, 3rd
        case 1:  return $num.'st';
        case 2:  return $num.'nd';
        case 3:  return $num.'rd';
      }
    }
    return $num.'th';
}
	
	//## Add Manual Penalty control starts here...
	public function AddManualPenaltyCtrl(Request $req)
	{
		$data['lacc_id'] = $req->input('lacc_id');
		$data['lins_id'] = $req->input('lins_id');
		$data['dpmtemiamt'] = $req->input('dpmtemiamt');
		$data['dpmtfdt'] = $req->input('dpmtfdt');
		$data['dpmttdt'] = $req->input('dpmttdt');
		$data['dpmtnodays'] = $req->input('dpmtnodays');
		$data['dpmtdelayinterest'] = $req->input('dpmtdelayinterest');
		$data['dpmttotpenaltyamt'] = $req->input('dpmttotpenaltyamt');
		$rec=$this->LoanAccPanelMod->InsDelayManualPaymentMod($data); //Previous paid amount
		return json_encode($rec);
	}
	//## Add manual penalty control ends here...
	
	//Load Cancellation record starts here
	public function loanCancellationCtrl(Request $req)
	{
		$id = $req->input('ltr_id');
		$datarec="";
		
		//###Loan Single Report###
		$LoanRpt=$this->LoanAccPanelMod->viewLoanTransid($id);
		
					$datarec .= "<table id='datatable-data-export' class='table table-sorting table-striped table-hover table-bordered datatable'>
					<thead>
						<tr>
							<th>Date</th>
							<th>Transaction type</th>
							<th>Particulars</th>
							<th>Credit</th>
							<th>Paid</th>
						</tr>
					</thead>";
					$datarec .= "<tbody>";
		foreach($LoanRpt as $LoanApplicantRec)
		{
					$datarec .= "<tr>";
					$datarec .= "<input type='hidden' name='ltr_id' value='" . $LoanApplicantRec->ltr_id . "'>";
					$datarec .= "<input type='hidden' name='lacc_id' value='" . $LoanApplicantRec->lacc_id . "'>";
					$datarec .= "<input type='hidden' name='lins_id' value='" . $LoanApplicantRec->lins_id . "'>";
					$datarec .= "<td>" . date("d-m-Y",strtotime($LoanApplicantRec->ltr_transdt)) . "</td>";
					//#######Transaction Type Starts here #######
					$datarec .= "<td>";
					if($LoanApplicantRec->ltr_transtype=="Principle" || $LoanApplicantRec->ltr_transtype=="Interest" )
					{
					$datarec .= "<b style='color: blue;' >" . $LoanApplicantRec->ltr_transtype . "</b>";
					}
					if($LoanApplicantRec->ltr_transtype=="Cheque Bounce" || $LoanApplicantRec->ltr_transtype=="Case" || $LoanApplicantRec->ltr_transtype=="Delay payment" || $LoanApplicantRec->ltr_transtype=="Delay-Payment" || $LoanApplicantRec->ltr_transtype=="Others")
					{
					$datarec .= "<b style='color: red;' >" . $LoanApplicantRec->ltr_transtype . "</b>";
					}
					if($LoanApplicantRec->ltr_transtype=="Payment")
					{
					$datarec .= "<b style='color: green;'>" . $LoanApplicantRec->ltr_transtype . "</b>";
					}
					$datarec .= "</td>";
					//#######Transaction Type Ends here #######
					//#######Transaction Note starts here #######
					$datarec .= "<td width='350'>";
					if($LoanApplicantRec->ltr_transtype=="Payment")
					{
					$datarec .= "<b>Bill No : </b>".$LoanApplicantRec->ltr_billno."<br>";
					}
					if($LoanApplicantRec->ltr_note != "")
					{
					$datarec .= "<b>Note:</b>".$LoanApplicantRec->ltr_note;
					}
						//###Cheque and Bank details starts here
						if($LoanApplicantRec->ltr_chqno != "")
						{
						$datarec .= "<br><b>Cheque:</b>".$LoanApplicantRec->ltr_chqno;
						}
						if($LoanApplicantRec->ltr_bank != "")
						{
						$datarec .= "<br><b>Bank detail:</b>".$LoanApplicantRec->ltr_bank;
						}
						//###Cheque and Bank details ends here
					$datarec .= "</td>";
					//#######Transaction Note ends here #######
					if($LoanApplicantRec->ltr_transtype=="Principle" || $LoanApplicantRec->ltr_transtype=="Interest" || $LoanApplicantRec->ltr_transtype=="Cheque Bounce" || $LoanApplicantRec->ltr_transtype=="Case" || $LoanApplicantRec->ltr_transtype=="Delay payment"  || $LoanApplicantRec->ltr_transtype=="Delay-Payment" || $LoanApplicantRec->ltr_transtype=="Others")
					{
					$datarec .= "<td> $LoanApplicantRec->ltr_amt</td>";
					}
					else
					{
						$datarec .= "<td>0</td>";
					}
					if($LoanApplicantRec->ltr_transtype=="Payment")
					{
					$datarec .= "<td> $LoanApplicantRec->ltr_amt</td>";
					}
					else
					{
						$datarec .= "<td>0</td>";
					}
					$datarec .= "</tr>";
		}
					$datarec .= "</tbody>";
					$datarec .= "</table>";
		return $datarec;
	}
	//Loan Cancellation record ends here
	
	//Load Cheque Cancellation record starts here
	public function loanChqCancelCtrl(Request $req)
	{
		$id = $req->input('ltr_id');
		$datarec="";
		
		//###Loan Single Report###
		$LoanRpt=$this->LoanAccPanelMod->viewLoanTransid($id);
		
					$datarec .= "<table id='datatable-data-export' class='table table-sorting table-striped table-hover table-bordered datatable'>
					<thead>
						<tr>
							<th>Receipt No.</th>
							<th>Date</th>
							<th>Transaction type</th>
							<th>Particulars</th>
							<th>Paid Amount</th>
						</tr>
					</thead>";
					$datarec .= "<tbody>";
		foreach($LoanRpt as $LoanApplicantRec)
		{
					$datarec .= "<tr>";
					$datarec .= "<input type='hidden' name='chq_ltr_id' value='" . $LoanApplicantRec->ltr_id . "'>";
					$datarec .= "<input type='hidden' name='chq_lacc_id' value='" . $LoanApplicantRec->lacc_id . "'>";
					$datarec .= "<input type='hidden' name='chq_lins_id' value='" . $LoanApplicantRec->lins_id . "'>";
					$datarec .= "<td>$LoanApplicantRec->ltr_billno</td>";
					$datarec .= "<td>" . date("d-m-Y",strtotime($LoanApplicantRec->ltr_transdt)) . "</td>";
					//#######Transaction Type Starts here #######
					$datarec .= "<td>";
					if($LoanApplicantRec->ltr_transtype=="Principle" || $LoanApplicantRec->ltr_transtype=="Interest" )
					{
					$datarec .= "<b style='color: blue;' >" . $LoanApplicantRec->ltr_transtype . "</b>";
					}
					if($LoanApplicantRec->ltr_transtype=="Cheque Bounce" || $LoanApplicantRec->ltr_transtype=="Case" || $LoanApplicantRec->ltr_transtype=="Delay payment" || $LoanApplicantRec->ltr_transtype=="Delay-Payment" || $LoanApplicantRec->ltr_transtype=="Others")
					{
					$datarec .= "<b style='color: red;' >" . $LoanApplicantRec->ltr_transtype . "</b>";
					}
					if($LoanApplicantRec->ltr_transtype=="Payment")
					{
					$datarec .= "<b style='color: green;'>" . $LoanApplicantRec->ltr_transtype . "</b>";
					}
					$datarec .= "</td>";
					//#######Transaction Type Ends here #######
					//#######Transaction Note starts here #######
					$datarec .= "<td width='350'>";
					if($LoanApplicantRec->ltr_note != "")
					{
					$datarec .= "<b>Note:</b>".$LoanApplicantRec->ltr_note;
					}
						//###Cheque and Bank details starts here
						if($LoanApplicantRec->ltr_chqno != "")
						{
						$datarec .= "<br><b>Cheque:</b>".$LoanApplicantRec->ltr_chqno;
						}
						if($LoanApplicantRec->ltr_bank != "")
						{
						$datarec .= "<br><b>Bank detail:</b>".$LoanApplicantRec->ltr_bank;
						}
						//###Cheque and Bank details ends here
					$datarec .= "</td>";
					//#######Transaction Note ends here #######
					if($LoanApplicantRec->ltr_transtype=="Payment")
					{
					$datarec .= "<td>$LoanApplicantRec->ltr_amt</td>";
					}
					else
					{
						$datarec .= "<td>0</td>";
					}
					$datarec .= "</tr>";
		}
					$datarec .= "</tbody>";
					$datarec .= "</table>";
		return $datarec;
	}
	//Loan Cheque Cancellation record ends here
	
	public function loninstcancelinsCtrl(Request $req)
	{
		$data['ltr_id'] = $req->input('ltr_id');
		$data['lacc_id'] = $req->input('lacc_id');
		$data['lins_id'] = $req->input('lins_id');
		$data['ltr_cancellationtype'] = $req->input('ltr_cancellationtype');
		$data['ltr_cancellationreason'] = $req->input('ltr_cancellationreason');
		$rec=$this->LoanAccPanelMod->LoanPaymentInsertCancelMod($data); //Previous paid amount
		return json_encode($rec);
	}
	public function chqinstcancelinsCtrl(Request $req)
	{	
		$data['ltr_id'] = $req->input('chq_ltr_id');
		$data['lacc_id'] = $req->input('chq_lacc_id');
		$data['lins_id'] = $req->input('chq_lins_id');	
		$data['ltr_cancellationtype'] = $req->input('loan_chqcancellation_type') . $req->input('ltr_cancellation_title');
		$data['ltr_cancellationreason'] = $req->input('ltr_chqcancellationreason');
		
		if($req->input('loan_chqcancellation_type') == "Cheque Bounce")
		{
			$data['ltr_cancellationcharge'] = $req->input('ltr_cancellationcharge');
			$data['ltr_cancellation_title'] = $req->input('ltr_cancellation_title');			
			//Insert cheque bounce payment starts here
				$data['ltr_transdt'] = $req->input('ltr_chqbouncedate');
				$data['ltr_transtype'] = $req->input('loan_chqcancellation_type');
				$data['ltr_amt'] = $req->input('ltr_cancellationcharge');
				$data['ltr_note'] = $req->input('ltr_chqcancellationreason');
				$data['ltr_status'] = "Active";
				$data['ltr_chq_bounce_id'] = $req->input('chq_ltr_id');
				$data['ltr_billno'] = 0;
				$data['ltr_paymenttype'] = "";				
				$data['ltr_chqno'] = "";				
				$data['ltr_bank'] = "";				
				$rec=$this->LoanAccPanelMod->LoanPaymentInsertMod($data);
			//Insert cheque bounce payment ends here
			$rec=$this->LoanAccPanelMod->LoanPaymentInsertCancelMod($data);
			return json_encode($rec);
		}
		if($req->input('loan_chqcancellation_type') == "Wrong Entry")
		{
			$rec=$this->LoanAccPanelMod->LoanPaymentInsertCancelMod($data);
			return json_encode($rec);
		}
	}
	public function RPTSingleTransCtrl(Request $req)
	{
		$data['lacc_id'] = $req->input('lacc_id');
		$data['acno'] = $req->input('acno');
		
		$id = 0;
		//$id = $req->input('lins_id');
		$datarec="";
		
		//###Loan Single Report###
		$data['LoanRpt']=$this->LoanAccPanelMod->viewRPTSingleTrans($data['lacc_id']);
		$data['LoanApplicantRecords']=$this->LoanAccPanelMod->viewloanapplicantion($data['lacc_id']);
		$data['LoanTotalPaid']=$this->LoanAccPanelMod->LoanTotalPaid($data['lacc_id']); //Total paid

		return view('LoanApp.rptsingletransaction',compact('data'));
	}
	public function WelcomeKitReport(Request $request)
	{
		$id = $request->input('lacc_id');
		//### Coding to Calculate Delay payment starts here
		$data['LoanApplicantRecords']=$this->LoanAccPanelMod->viewloanapplicantion($id);
		$data['LoanInstallmentRecords']=$this->LoanAccPanelMod->viewLoanInstallmentRecords($id);
		$data['LoanTotalPaid']=$this->LoanAccPanelMod->LoanTotalPaid($id); //Total paid
		
		foreach($data['LoanApplicantRecords'] as $LoanApplicantRec1)
		{
			$delaypmt_charge = $LoanApplicantRec1->dpmt_charge;
		}	
	
		$totinterestamt =0;
		$tototheramt = 0;
		$totemiamt = 0;
		$totpaidamt = 0;
		$totbalamt = 0;
		$btn=0;
		$paid_amt = 0;
		$bal_amt=0;
		$totdelaypayment=0;
		$totpaiddecreaseonloop = $data['LoanTotalPaid'];		
		foreach($data['LoanInstallmentRecords'] as $LoanInstallmentRec)
		{
			$lins_amt=$LoanInstallmentRec->lins_amt;						
			$lins_iamt=$LoanInstallmentRec->lins_iamt;						
			$othr_amt=$LoanInstallmentRec->penaltysum;
			$totamt = $lins_amt+$lins_iamt+$othr_amt;
			
			//Sum Total
			$totinterestamt = $totinterestamt +  $lins_iamt;
			$tototheramt = $tototheramt + $othr_amt;
			$totemiamt = $totemiamt+ ($lins_amt+$lins_iamt+$othr_amt);
			$totdelaypayment = $totdelaypayment + 0;
			
			if($totpaiddecreaseonloop > $totamt)
			{
				$paid_amt=$totamt;
				$totpaiddecreaseonloop = $totpaiddecreaseonloop - $totamt;
			}
			else
			{
				$paid_amt = $totpaiddecreaseonloop;
				$totpaiddecreaseonloop = 0;
			}
									
			$bal_amt=$totamt-$paid_amt;
			
			$totpaidamt = $totpaidamt + $paid_amt;
			$totbalamt = $totbalamt + $bal_amt;
			
				//Coding to Calculate days difference between two days start here	
				$currentdt = strtotime(date("Y-m-d")); //Current day
				$installmentdt = strtotime($LoanInstallmentRec->lins_date); //Installment day
				$datediff = $currentdt - $installmentdt;				
				$daysdifference =  round($datediff / (60 * 60 * 24)) ;
				if($daysdifference >=0)
				{
					if($bal_amt > 0)
					{
						//$daysdifference=$daysdifference;
						$delaydet['lacc_id'] = $id;
						$delaydet['lins_id'] = $LoanInstallmentRec->lins_id;
						$delaydet['daysdifference'] = $daysdifference;
						$delaydet['ltr_amt'] = $bal_amt;
						$delaydet['delaypmt_charge'] = $delaypmt_charge;
						$delaypay = $this->LoanAccPanelMod->AddDelayPaymentPenalty($delaydet);
					}
				}
				//Coding to Calculate days difference between two days ends here
		}
		//### Coding to Calculate Delay payment ends here
		$data['LoanApplicantRecords']=$this->LoanAccPanelMod->viewloanapplicantion($id);
		$data['LoanInstallmentRecords']=$this->LoanAccPanelMod->viewLoanInstallmentRecords($id);
		$data['LoanTotalPaid']=$this->LoanAccPanelMod->LoanTotalPaid($id); //Total paid
		$data['totemiamt'] = $totinterestamt;
		return view('LoanApp.rptwelcomekit',compact('data'));		
	}
	
	//Coding to update delay payment starts here
	public function DelayPaymentUpdateCtrl(Request $req)
	{
		$data['lacc_id'] = $req->input('lacc_id');
		$data['dpmt_charge'] = $req->input('dpmt_charge');		
		$rec=$this->LoanAccPanelMod->DelayPaymentUpdateMod($data); //Previous paid amount
		return json_encode($rec);
	}
	//Coding to update delay payment ends here
	
	//Coding to update delay payment starts here
	public function LoadCreditDebitReport(Request $req)
	{
		$data['lacc_id'] = $req->input('lacc_id');
		return view('LoanApp.loanrptcreditdebit',compact('data'));
	}
	//Coding to update delay payment ends here
	public function Loadarrear(Request $req)
	{
		return view('LoanApp.arrear',compact('data'));
	}
}