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

class LoanApplicantionController extends Controller
{
	public function __construct()
	{
		$this->LoanApplicantMod= new LoanApplicantionModel;
	}
	public function LoadLoanApplicant()
    {
		$data['lacc_no'] = $this->LoanApplicantMod->LoadNextSerialNo();
		$data['lacc_id'] = 0;
		return view('loanapplicant',compact('data'));
    }
	public function EditloanApp(Request $req)
	{
		$data['lacc_id'] = $req->input('lacc_id');
		$data['lacc_no'] = $req->input('acno');
		return view('loanapplicant',compact('data'));
	}
	public function loanApplicantinsert(Request $req)
	{
		$data['editId'] = $req->input('editId');
		$data['lacc_no'] = $req->input('lacc_no');
		$data['br_id'] = $req->input('br_id');
		$data['ltyp_id'] = $req->input('ltyp_id');
		$data['lacc_custname'] = $req->input('lacc_custname');
		$data['lacc_swdof'] = $req->input('lacc_swdof');
		$data['lacc_pan'] = $req->input('lacc_pan');
		$data['lacc_chitno'] = $req->input('lacc_chitno');
		$data['lacc_agrno'] = $req->input('lacc_agrno');
		$data['lacc_dob'] = $req->input('lacc_dob');
		$data['lacc_age'] = $req->input('lacc_age');
		$data['lacc_oldrefno'] = $req->input('lacc_oldrefno');
		$data['lacc_securityentry'] = $req->input('lacc_securityentry');
		
		/***************************resaddr*************************/
		$data['lacc_resaddr_addr'] = $req->input('lacc_resaddr_addr');
		$data['lacc_resaddr_city'] = $req->input('lacc_resaddr_city');
		$data['lacc_resaddr_pin'] = $req->input('lacc_resaddr_pin');
		$data['lacc_resaddr_landmark'] = $req->input('lacc_resaddr_landmark');
		$data['lacc_resaddr_fax'] = $req->input('lacc_resaddr_fax');
		$data['lacc_resaddr_mob'] = $req->input('lacc_resaddr_mob');
		$data['lacc_resaddr_tel'] = $req->input('lacc_resaddr_tel');
		
		/***************************compaddr*************************/
		$data['lacc_compaddr_addr'] = $req->input('lacc_compaddr_addr');
		$data['lacc_compaddr_city'] = $req->input('lacc_compaddr_city');
		$data['lacc_compaddr_pin'] = $req->input('lacc_compaddr_pin');
		$data['lacc_compaddr_landmark'] = $req->input('lacc_compaddr_landmark');
		$data['lacc_compaddr_fax'] = $req->input('lacc_compaddr_fax');
		$data['lacc_compaddr_mob'] = $req->input('lacc_compaddr_mob');
		$data['lacc_compaddr_tel'] = $req->input('lacc_compaddr_tel');
		
		/***************************permaddr*************************/
		$data['lacc_permaddr_addr'] = $req->input('lacc_permaddr_addr');
		$data['lacc_permaddr_city'] = $req->input('lacc_permaddr_city');
		$data['lacc_permaddr_pin'] = $req->input('lacc_permaddr_pin');
		$data['lacc_permaddr_landmark'] = $req->input('lacc_permaddr_landmark');
		$data['lacc_permaddr_fax'] = $req->input('lacc_permaddr_fax');
		$data['lacc_permaddr_mob'] = $req->input('lacc_permaddr_mob');
		$data['lacc_permaddr_tel'] = $req->input('lacc_permaddr_tel');
		$data['lacc_permaddr_chngresyesno'] = $req->input('lacc_permaddr_chngresyesno');		
		$data['lacc_gender'] = $req->input('lacc_gender');
		
		/***************************jobprofile*************************/
		$data['lacc_jobprofile_yrsatcurrentjob'] = $req->input('lacc_jobprofile_yrsatcurrentjob');
		$data['lacc_jobprofile_department'] = $req->input('lacc_jobprofile_department');
		$data['lacc_jobprofile_designation'] = $req->input('lacc_jobprofile_designation');
		$data['lacc_jobprofile_grossincomepm'] = $req->input('lacc_jobprofile_grossincomepm');
		$data['lacc_jobprofile_netincomepm'] = $req->input('lacc_jobprofile_netincomepm');
		$data['lacc_jobprofile_otherincsrc'] = $req->input('lacc_jobprofile_otherincsrc');
		$data['lacc_profession_other'] = $req->input('lacc_profession_other');
		
		/***************************bankac*************************/
		$data['lacc_monthlyrent'] = $req->input('lacc_monthlyrent');
		$data['lacc_bankac_bankname'] = $req->input('lacc_bankac_bankname');
		$data['lacc_bankac_branch'] = $req->input('lacc_bankac_branch');
		$data['lacc_bankac_accountno'] = $req->input('lacc_bankac_accountno');
		$data['lacc_bankac_accountsince'] = $req->input('lacc_bankac_accountsince');
		$data['lacc_bankac_accounttype'] = $req->input('lacc_bankac_accounttype');
		
		/***************************coapplicant*************************/
		$data['lacc_coapplicantprofile_name'] = $req->input('lacc_coapplicantprofile_name');
		$data['lacc_coapplicantprofile_compname'] = $req->input('lacc_coapplicantprofile_compname');
		$data['lacc_coapplicantprofile_monthlyincome'] = $req->input('lacc_coapplicantprofile_monthlyincome');
		$data['lacc_coapplicantprofile_netincome'] = $req->input('lacc_coapplicantprofile_netincome');
		$data['lacc_coapplicantprofile_officetel'] = $req->input('lacc_coapplicantprofile_officetel');
		$data['lacc_coapplicantprofile_relationship'] = $req->input('lacc_coapplicantprofile_relationship');
		$data['lacc_coapplicantprofile_qualification'] = $req->input('lacc_coapplicantprofile_qualification');
		$data['lacc_coapplicantprofile_occupation'] = $req->input('lacc_coapplicantprofile_occupation');
		
		/**************************loan*****************************/
		$data['lacc_loanamt'] = $req->input('lacc_loanamt');
		$data['lacc_tenor'] = $req->input('lacc_tenor');
		$data['lacc_intrate'] = $req->input('lacc_intrate');
		$data['lacc_ufc'] = $req->input('lacc_ufc');
		
		/***************************Reference*************************/
		$data['lacc_reference1_name'] = $req->input('lacc_reference1_name');
		$data['lacc_reference1_relationship'] = $req->input('lacc_reference1_relationship');
		$data['lacc_reference1_address'] = $req->input('lacc_reference1_address');
		$data['lacc_reference1_tel'] = $req->input('lacc_reference1_tel');
		$data['lacc_reference1_res'] = $req->input('lacc_reference1_res');
		$data['lacc_reference2_name'] = $req->input('lacc_reference2_name');
		$data['lacc_reference2_relationship'] = $req->input('lacc_reference2_relationship');
		$data['lacc_reference2_address'] = $req->input('lacc_reference2_address');
		$data['lacc_reference2_tel'] = $req->input('lacc_reference2_tel');
		$data['lacc_reference2_res'] = $req->input('lacc_reference2_res');
		/***************************Reference*************************/
		
		/***************************guarantor*************************/
		$data['lacc_guarantor1_name'] = $req->input('lacc_guarantor1_name');
		$data['lacc_guarantor1_relationship'] = $req->input('lacc_guarantor1_relationship');
		$data['lacc_guarantor1_address'] = $req->input('lacc_guarantor1_address');
		$data['lacc_guarantor1_tel'] = $req->input('lacc_guarantor1_tel');
		$data['lacc_guarantor1_res'] = $req->input('lacc_guarantor1_res');
		$data['lacc_guarantor2_name'] = $req->input('lacc_guarantor2_name');
		$data['lacc_guarantor2_relationship'] = $req->input('lacc_guarantor2_relationship');
		$data['lacc_guarantor2_address'] = $req->input('lacc_guarantor2_address');
		$data['lacc_guarantor2_tel'] = $req->input('lacc_guarantor2_tel');
		$data['lacc_guarantor2_res'] = $req->input('lacc_guarantor2_res');
		$data['lacc_guarantor3_name'] = $req->input('lacc_guarantor3_name');
		$data['lacc_guarantor3_relationship'] = $req->input('lacc_guarantor3_relationship');
		$data['lacc_guarantor3_address'] = $req->input('lacc_guarantor3_address');
		$data['lacc_guarantor3_tel'] = $req->input('lacc_guarantor3_tel');
		$data['lacc_guarantor3_res'] = $req->input('lacc_guarantor3_res');
		/***************************guarantor*************************/
		
		/***************************Self emp det starts here*************************/
		$data['lacc_selfempdet_natureofbusiness'] = $req->input('lacc_selfempdet_natureofbusiness');
		$data['lacc_selfempdet_incomepa'] = $req->input('lacc_selfempdet_incomepa');
		$data['lacc_selfempdet_yearsatpresentbusiness'] = $req->input('lacc_selfempdet_yearsatpresentbusiness');
		/***************************Self emp det ends here*************************/
		
		$data['lacc_jobcategory'] = $req->input('lacc_jobcategory');
		$data['lacc_gender'] = $req->input('lacc_gender');
		$data['lacc_maritalst'] = $req->input('lacc_maritalst');
		$data['lacc_education'] = $req->input('lacc_education');
		
		if(trim($req->input('lacc_profession_other')) != "")
		{
		$data['lacc_profession'] = $req->input('lacc_profession_other');
		}
		else
		{
		$data['lacc_profession'] = $req->input('lacc_profession');
		}
		
		$data['lacc_residencestatus'] = $req->input('lacc_residencestatus');
		//$data['lacc_monthlyrent'] = $req->input('lacc_monthlyrent');
		//$data['lacc_ihave'] = $req->input('lacc_ihave');				
		
		$data['lacc_ihave'] = implode(",", $req->input('lacc_ihave'));
		
		$data['lacc_chitrefno'] = $req->input('lacc_chitrefno');
		$data['lacc_chitbrid'] = $req->input('lacc_chitbrid');
		$data['lacc_paidupamt'] = $req->input('lacc_paidupamt');
		
		return $loanapplicant=$this->LoanApplicantMod->insertLoanApplicant($data);
	}
	public function referencenumberFind(Request $req)
	{
		$data['lacc_oldrefno'] = strtoupper($req->input('lacc_oldrefno'));
		//$data['lacc_oldrefno'] = "NFCPL1001";
		$data['editId'] = $req->input('editId');
		$lacc_oldrefno=$this->LoanApplicantMod->referencenumberFindMod($data);
		//print_r($lacc_oldrefno);
		if(count($lacc_oldrefno) ==1)
		{
		$data['reference']=$lacc_oldrefno;
		$data['res1']=unserialize($lacc_oldrefno->lacc_resaddr);
		$data['comp']=unserialize($lacc_oldrefno->lacc_compaddr);
		$data['perm']=unserialize($lacc_oldrefno->lacc_permaddr);
		$data['jobprofile']=unserialize($lacc_oldrefno->lacc_jobprofile);
		$data['coapplicant']=unserialize($lacc_oldrefno->lacc_coapplicantprofile);
		$data['self']=unserialize($lacc_oldrefno->lacc_selfempdet);
		//$data['educate']=unserialize($lacc_oldrefno->lacc_education);
		//$data['lacc_ihave']=unserialize($lacc_oldrefno->lacc_ihave);
		$data['bank']=unserialize($lacc_oldrefno->lacc_bankac);
		$data['ref1']=unserialize($lacc_oldrefno->lacc_reference1);
		$data['ref2']=unserialize($lacc_oldrefno->lacc_reference2);
		$data['Gurr1']=unserialize($lacc_oldrefno->lacc_guarantor1);
		$data['Gurr2']=unserialize($lacc_oldrefno->lacc_guarantor2);
		$data['Gurr3']=unserialize($lacc_oldrefno->lacc_guarantor3);
		return json_encode($data);
		}		
	}
	public function LoadViewLoanApplicantion()
	{
		$data['LoanApplicantRecords']=$this->LoanApplicantMod->viewloanapplicantionrecord('Active');
		return view('LoanApp.viewloanapplicantion',compact('data'));
	}
	public function loadcreateloanapp(Request $request)
	{
		$id = $request->input('lacc_id');
		$data['LoanApplicantRecords']=$this->LoanApplicantMod->viewloanapplicantion($id);
		return view('LoanApp.loanapplication',compact('data'));
	}
	public function loanprocessingfee(Request $request)
	{
		$lacc_loanamt = $request->input('lacc_loanamt');
		$lpf=$this->LoanApplicantMod->viewloanprocessingfee($lacc_loanamt);
		return json_encode($lpf);
	}
	public function sureDeleteloanApp(Request $req)
	{
		$data['lacc_id'] = $req->input('lacc_id');
		return $sureDeleteloanApp=$this->LoanApplicantMod->sureDeleteloanApp($data);
	}	
	public function loadloantype()
	{
		$status	= 'Deleted';
		$ltyp_loantypes = DB::select( DB::raw("SELECT ltyp_id,CONCAT(ltyp_loantype, ' | ', ltyp_maxmonth , ' month | ',ltyp_interestperc ,'%') as loantypes FROM ltyp_loantypes where ltyp_status != '$status' order by ltyp_maxmonth"));
		return response()->json($ltyp_loantypes);
	}
	public function loantypeFindFind(Request $req)
	{
		$data['ltyp_id'] = strtoupper($req->input('ltyp_id'));
		$data['editId'] = $req->input('editId');
		$loantypedet=$this->LoanApplicantMod->loantypeFindFindMod($data);
		if(count($loantypedet) ==1)
		{
			$data['loantypedet']=$loantypedet;
			return json_encode($data);
		}		
	}
	public function loaninstallmentdetailctrl(Request $req)
	{
		$payAmt=0;
		$paytotAmt=0;
		$payperAmt=0;
		$tbl="";
		$lacc_tenor = $req->input('lacc_tenor');
		$lacc_opendt = $req->input('lacc_opendt');
		$lacc_loanamt = $req->input('lacc_loanamt');
		$lacc_intrate = $req->input('lacc_intrate');

		for($i=0; $i<$lacc_tenor; $i++)
		{
			//LOAN Amount
			$InstallmentAmount=$lacc_loanamt/$lacc_tenor;
			//Intrest percentage per month
			$Intrest=$lacc_intrate/12;
			//Intrest Amount 
			$IntrestAdd=$lacc_loanamt*$Intrest/100;
			//payable Amount 
			$IntAmt=$InstallmentAmount+$IntrestAdd;
			//Total Amount
			$payAmt+= $InstallmentAmount;
			$paytotAmt+=$IntAmt;
			$payperAmt+=$IntrestAdd;
			//slno
			$slno=$i+1;
			//month add
			$effectiveDate = date('Y-m-d', strtotime("+$slno months", strtotime($lacc_opendt)));
			$tbl =$tbl . '<tr><td>' . $slno . '<input type="hidden" name="lins_no[]" class="form-control" value="'  . $slno . '"></td><td><input type="date" name="lins_date[]" class="form-control" value="' . $effectiveDate. '" readonly></td><td><input type="text" name="lins_amt[]" class="form-control" value="' . round($InstallmentAmount) . '" readonly></td><td><input type="text" name="lins_iperc[]" class="form-control" value="' . round($Intrest,2) . '" readonly></td><td><input type="text" name="lins_iamt[]" class="form-control" value="' . round($IntrestAdd) . '" readonly></td><td><input type="text" name="totalpayable[]" class="form-control" value="' . round($IntAmt) . '" readonly></td><td><input type="text" name="lins_chqno[]" class="form-control" ></td><td><input type="text" name="lins_note[]" class="form-control" ></td></tr>';
		}
			$tbl =$tbl . '<th></th><th></th><th><input type="text" readonly class="form-control" id="totalloanamt" value="'. $lacc_loanamt .'" ></th><th></th><th><input type="text" readonly class="form-control" id="totalloanper" value="' . round($payperAmt) . '"  ></th><th><input type="text" readonly class="form-control" id="totalloanpayamt" value="' . round($paytotAmt) . '"></th><th></th><th></th>';
		
		return $tbl;
	}	
	public function loancreateloanacc(Request $request)
	{
		$sub['lacc_id']=$request->input('lacc_id');	 
		$sub['lacc_loanamt']=$request->input('lacc_loanamt');	 
		$sub['lacc_tenor']=$request->input('lacc_tenor');	 
		$sub['lacc_intrate']=$request->input('lacc_intrate');	 
		$sub['lacc_opendt']=$request->input('lacc_opendt');	 
		$sub['lpf_id']=$request->input('lpf_id');	 
		$sub['lpf_amttype']=$request->input('lpf_amttype');	 
		$sub['lpf_amt']=$request->input('lpf_amt');	 
		$sub['lacc_lpfprocessingfee']=$request->input('lacc_lpfprocessingfee');	 
		$sub['arrlins_no']=$request->input('arrlins_no');	 
		$sub['arrlins_date']=$request->input('arrlins_date');	 
		$sub['arrlins_amt']=$request->input('arrlins_amt');	 
		$sub['arrlins_iperc']=$request->input('arrlins_iperc');	 
		$sub['arrlins_iamt']=$request->input('arrlins_iamt');	 
		$sub['arrlins_chqno']=$request->input('arrlins_chqno');	 
		$sub['arrlins_note']=$request->input('arrlins_note');		
		return $loanac=$this->LoanApplicantMod->createloanac($sub);
	}
}