<?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\LoanPreclosureModel;

class LoanPreclosureController extends Controller
{
	public function __construct()
	{
		$this->LoanPreclosureModel= new LoanPreclosureModel;
	}
	public function LoanApplicantpanel(Request $request)
    {
		$id = $request->input('lacc_id');
		
		//### Coding to Calculate Delay payment starts here
		$data['LoanApplicantRecords']=$this->LoanPreclosureModel->viewloanapplicantion($id);
		$data['LoanInstallmentRecords']=$this->LoanPreclosureModel->viewLoanInstallmentRecords($id);
		$data['LoanTotalPaid']=$this->LoanPreclosureModel->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->LoanPreclosureModel->AddDelayPaymentPenalty($delaydet);
				}
			}
			//Coding to Calculate days difference between two days ends here
		}
		//### Coding to Calculate Delay payment ends here
			
		$data['LoanApplicantRecords']=$this->LoanPreclosureModel->viewloanapplicantion($id);
		$data['LoanInstallmentRecords']=$this->LoanPreclosureModel->viewLoanInstallmentRecords($id);
		$data['LoanTotalPaid']=$this->LoanPreclosureModel->LoanTotalPaid($id); //Total paid
		return view('LoanApp.loanacpanel',compact('data'));
    }
	public function LoanPaymentCalculationCtrl(Request $request)
	{
		$lacc_id = $request->input('lacc_id');
		$id = $request->input('lins_id');
		$prevpaid= 0;
		//##########
		$data['LoanApplicantRecords']=$this->LoanPreclosureModel->viewloanapplicantion($lacc_id);
		foreach($data['LoanApplicantRecords'] as $LoanApplicantRec1)
		{
			$data['delaypmt_charge'] = $LoanApplicantRec1->dpmt_charge;
		}
		//##########
		//##########
		$data['LoanSingleInsRecord']=$this->LoanPreclosureModel->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->LoanPreclosureModel->LoancurrentPayable($id); //current payable amount
		$Currentpaid=$this->LoanPreclosureModel->LoanCurrentPaid($id); //Current paid amount
		$prevpay=$this->LoanPreclosureModel->LoanpreviousPayable($id); //previous payable amount
		$Previouspaid=$this->LoanPreclosureModel->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 LoanDelayPaymentreCalculationCtrl(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->LoanPreclosureModel->LoancurrentPayable($id); //current payable amount
		$Currentpaid=$this->LoanPreclosureModel->LoanCurrentPaid($id); //Current paid amount
		$prevpay=$this->LoanPreclosureModel->LoanpreviousPayable($id); //previous payable amount
		$Previouspaid=$this->LoanPreclosureModel->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');
		$rec=$this->LoanPreclosureModel->LoanPaymentInsertMod($data); //Previous paid amount
		return json_encode($rec);
	}
	public function loanAccountPreclosureCtrl(Request $request)
    {
		$id = $request->input('lacc_id');
		$data['LoanApplicantRecords']=$this->LoanPreclosureModel->viewloanapplicantion($id);
		$data['LoanInstallmentRecords']=$this->LoanPreclosureModel->viewLoanInstallmentRecords($id);
		$data['LoanTotalPaid']=$this->LoanPreclosureModel->LoanTotalPaid($id); //Total paid
		return view('LoanApp.loanpreclosure',compact('data'));
    }
		//## 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->LoanPreclosureModel->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->LoanPreclosureModel->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>$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
	public function loaninstcancellationinsCtrl(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->LoanPreclosureModel->LoanPaymentInsertCancelMod($data); //Previous paid amount
		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->LoanPreclosureModel->viewRPTSingleTrans($data['lacc_id']);
		$data['LoanApplicantRecords']=$this->LoanPreclosureModel->viewloanapplicantion($data['lacc_id']);
		$data['LoanTotalPaid']=$this->LoanPreclosureModel->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->LoanPreclosureModel->viewloanapplicantion($id);
		$data['LoanInstallmentRecords']=$this->LoanPreclosureModel->viewLoanInstallmentRecords($id);
		$data['LoanTotalPaid']=$this->LoanPreclosureModel->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->LoanPreclosureModel->AddDelayPaymentPenalty($delaydet);
					}
				}
				//Coding to Calculate days difference between two days ends here
		}
		//### Coding to Calculate Delay payment ends here
		$data['LoanApplicantRecords']=$this->LoanPreclosureModel->viewloanapplicantion($id);
		$data['LoanInstallmentRecords']=$this->LoanPreclosureModel->viewLoanInstallmentRecords($id);
		$data['LoanTotalPaid']=$this->LoanPreclosureModel->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->LoanPreclosureModel->DelayPaymentUpdateMod($data); //Previous paid amount
		return json_encode($rec);
	}
	//Coding to update delay payment ends here
	
	//Update Loan Preclosure Starts here
	public function LoanPreclosureUpdateCtrl(Request $request)
	{
		$sub['lacc_id']=$request->input('lacc_id');
		$sub['arrlins_id']=$request->input('arrlins_id');
		$sub['arrlins_no']=$request->input('arrlins_no');
		$sub['arrlins_amt']=$request->input('arrlins_amt');
		$sub['arrlins_iperc']=$request->input('arrlins_iperc');
		$sub['arrlins_iamt']=$request->input('arrlins_iamt');
		$sub['arrlins_date']=$request->input('arrlins_date');
		$sub['arrdelayamt']=$request->input('arrdelayamt');
		$sub['arrtotamt']=$request->input('arrtotamt');
		$sub['arrpaid_amt']=$request->input('arrpaid_amt');
		$sub['arrbal_amt']=$request->input('arrbal_amt');
		$sub['arrlins_chqno']=$request->input('arrlins_chqno');
		$sub['arrlins_note']=$request->input('arrlins_note');
		$sub['arrchequebouncecharges']=$request->input('arrchequebouncecharges');
		$sub['arrcasecharges']=$request->input('arrcasecharges');
		$sub['arrothercharges']=$request->input('arrothercharges');
		$sub['hidden_arrlins_amt']=$request->input('hidden_arrlins_amt');
		$sub['hidden_arrlins_iperc']=$request->input('hidden_arrlins_iperc');
		$sub['hidden_arrlins_iamt']=$request->input('hidden_arrlins_iamt');
		$sub['hidden_arrlins_date']=$request->input('hidden_arrlins_date');
		$sub['hidden_arrdelayamt']=$request->input('hidden_arrdelayamt');
		$sub['hidden_arrtotamt']=$request->input('hidden_arrtotamt');
		$sub['hidden_arrpaid_amt']=$request->input('hidden_arrpaid_amt');
		$sub['hidden_arrbal_amt']=$request->input('hidden_arrbal_amt');
		$sub['hidden_arrlins_chqno']=$request->input('hidden_arrlins_chqno');
		$sub['hidden_arrlins_note']=$request->input('hidden_arrlins_note');
		$sub['hidden_arrchequebouncecharges']=$request->input('hidden_arrchequebouncecharges');
		$sub['hidden_arrcasecharges']=$request->input('hidden_arrcasecharges');
		$sub['hidden_arrothercharges']=$request->input('hidden_arrothercharges');
		return $loanpreclosure=$this->LoanPreclosureModel->UpdateLoanInstallment($sub);
	}
	//Update Loan Preclosure Ends here
}