<?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\JamathModel;
use File;

class JamathController extends Controller
{
	public function __construct()
	{
		$this->JamathMod= new JamathModel;
	}
	public function LoadNewApplicant()
    {
		$delMemberstatus = $this->JamathMod->DeletePendingMemberRecords();
		$delPendingstatus = $this->JamathMod->DeletePendingDocRecords();
		return view('Mosque.newapplicantion');
    }
	//#########Import Loan data starts here
	public function importloandatactrl(Request $req)
	{
		$data['lacc_id'] = $this->JamathMod->LoadNextSerialNo();
		$data['lacc_no'] = 0;
		return view('importloandata',compact('data'));
	}	
	public function importloandataFindctrl(Request $req)
	{
		$data['lacc_oldrefno'] = strtoupper($req->input('lacc_oldrefno'));
		$lacc_oldrefno=$this->JamathMod->importloandataFindMod($data);
		$data['reference']=$lacc_oldrefno;
		return json_encode($data);	
	}
	//##########Import Loan data ends here
	public function EditJamathCtrl(Request $req)
	{
		$data['cst_id'] = $req->input('cst_id');
		//$data['view_family_record'] = $this->JamathMod->ViewFamilyMember($data);
		return view('Mosque.newapplicantion',compact('data'));
	}
	public function PrintMemberAppCtrl(Request $req)
	{
		$data['cst_id'] = $req->input('cst_id');
		return view('Mosque.printmemberapplicantion',compact('data'));
	}
	public function Applicantioninsertctrl(Request $req)
	{
		$data['Username'] = $req->input('Username');
		$data['cst_id'] = $req->input('cst_id');
		$data['cst_doj'] = $req->input('cst_doj');
		$data['cst_name'] = $req->input('cst_name');
		$data['cst_dob'] = $req->input('cst_dob');
		$data['grd_id'] = $req->input('grd_id');
		$data['cst_grd_code'] = $req->input('cst_grd_code');
		$data['cst_code'] = $req->input('cst_code');
		$data['lacc_age'] = $req->input('lacc_age');
		$data['cst_maritalstatus'] = $req->input('cst_maritalstatus');
		$data['cst_qualification'] = $req->input('cst_qualification');
		$data['cst_bloodgroup'] = $req->input('cst_bloodgroup');
		$data['cst_gender'] = $req->input('cst_gender');
		$data['cst_occupation'] = $req->input('cst_occupation');
		$data['cst_income'] = $req->input('cst_income');
		$data['cst_note'] = $req->input('cst_note');		
		/***************************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');
		$data['lacc_resaddr_email'] = $req->input('lacc_resaddr_email');		
		/***************************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_mob'] = $req->input('lacc_permaddr_mob');
		$data['lacc_permaddr_tel'] = $req->input('lacc_permaddr_tel');
		$data['lacc_permaddr_email'] = $req->input('lacc_permaddr_email');	
		$data['dp'] = $req->input('dp');
		
		$loanapplicant=$this->JamathMod->insertLoanApplicant($data);
		return json_encode($loanapplicant);
	}
	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->JamathMod->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 LoadViewjamathmembers(Request $req)
	{
		$data['rectype'] = $req->input('rectype');
		$data['fiscalyear'] = DB::table('fyr_fiscalyear')->whereRaw('? between fyr_startdate and fyr_enddate', [date('Y-m-d')])->where([["fyr_status","=",'Active']])->get();
		$data['LoadJamathRecords']=$this->JamathMod->viewjamathapplicant($data['rectype']);
		return view('Mosque.viewjamathmembers',compact('data'));
	}
	public function printjamathmemrptCtrl(Request $req)
	{
		$data['rectype'] = $req->input('rectype');
		$data['fiscalyear'] = DB::table('fyr_fiscalyear')->whereRaw('? between fyr_startdate and fyr_enddate', [date('Y-m-d')])->where([["fyr_status","=",'Active']])->get();
		$data['LoadJamathRecords']=$this->JamathMod->viewjamathapplicant($data['rectype']);
		return view('Mosque.printjamathmembersreport',compact('data'));
	}
	public function loadcreateloanapp(Request $request)
	{
		$id = $request->input('lacc_id');
		$data['LoanApplicantRecords']=$this->JamathMod->viewloanapplicantion($id);
		return view('LoanApp.loanapplication',compact('data'));
	}
	public function loanprocessingfee(Request $request)
	{
		$lacc_loanamt = $request->input('lacc_loanamt');
		$lpf=$this->JamathMod->viewloanprocessingfee($lacc_loanamt);
		return json_encode($lpf);
	}
	public function sureDeleteloanApp(Request $req)
	{
		$data['lacc_id'] = $req->input('lacc_id');
		return $sureDeleteloanApp=$this->JamathMod->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,ltyp_loantype FROM ltyp_loantypes where ltyp_status != '$status' order by ltyp_maxmonth"));
		return response()->json($ltyp_loantypes);
	}
	public function loaddelayfeetypes()
	{
		$status	= 'Deleted';
		$dpmt_delaypayment = DB::select( DB::raw("SELECT dpmt_id,dpmt_charge FROM dpmt_delaypayment where dpmt_status != '$status' order by dpmt_charge"));
		return response()->json($dpmt_delaypayment);
	}
	public function loantypeFindFind(Request $req)
	{
		$data['ltyp_id'] = strtoupper($req->input('ltyp_id'));
		$data['editId'] = $req->input('editId');
		$loantypedet=$this->JamathMod->loantypeFindFindMod($data);
		$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['dpmt_charge']=$request->input('dpmt_charge');
		$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');
		$sub['lacc_remarks']=$request->input('lacc_remarks');
		return $loanac=$this->JamathMod->createloanac($sub);
	}
	//This will load Next Grade serial number
	public function LoadGradeSerialNumberCtrl(Request $request)
	{
		$Username=$request->input('Username');
		$gradeid=$request->input('gradeid');
		$doj=$request->input('doj');
		$dt = date("Y-m-d");
		$data['fiscalyear'] = DB::table('fyr_fiscalyear')->whereRaw('? between fyr_startdate and fyr_enddate', [date('Y-m-d')])->where([["fyr_status","=",'Active']])->get();
		$fyrid = $data['fiscalyear'][0]->fyr_id;
		$data['gradefundlist'] = DB::select( DB::raw("SELECT * FROM grfn_gradefund LEFT JOIN grd_grade ON grfn_gradefund.grd_id=grd_grade.grd_id where grfn_gradefund.grfn_status = 'Active' AND  grfn_gradefund.grd_id = '$gradeid' AND grfn_gradefund.fyr_id='$fyrid' AND grfn_gradefund.grfn_paycycle != 'One time' "));
		//Delete Billrec table and transaction table
		DB::table('bil_billrec')->where('bil_status', 'Inactive')->where('usr_name', $Username)->delete();
		DB::table('trn_transaction')->where('trn_status', 'Inactive')->where('usr_name', $Username)->delete();
		//Auto Increment updation Starts here
		$data['maxbillid'] = DB::select( DB::raw("SELECT ifnull(max(bil_id),0)+1 as maxid FROM bil_billrec"));
		$data['maxtransid'] = DB::select( DB::raw("SELECT ifnull(max(trn_id),0)+1 as maxid FROM trn_transaction"));
		$maxbillid = $data['maxbillid'][0]->maxid;
		$maxtransid = $data['maxtransid'][0]->maxid;
		$data['altermaxbillid'] = DB::select( DB::raw("ALTER TABLE bil_billrec AUTO_INCREMENT = $maxbillid"));
		$data['altermaxtransid'] = DB::select( DB::raw("ALTER TABLE trn_transaction AUTO_INCREMENT = $maxtransid"));
		//Auto Increment updation ends here
		$grfn_amt= 0;
		$trinsid=0;
		foreach($data['gradefundlist'] as $gradefundlistval)
		{
			$doj1 = $doj;
			//#!!-	Transaction Record Starts here	-!!# 
			$trinsid = DB::table('trn_transaction')->insertGetId(['cst_id'=>0,'fy_id'=>$gradefundlistval->fyr_id,'grd_id'=>$gradefundlistval->grd_id,'grfn_id'=>$gradefundlistval->grfn_id,'ftyp_id'=>$gradefundlistval->ftyp_id,'trn_type'=>$gradefundlistval->grfn_paycycle,'trn_billno'=>0,'trn_amt'=>$grfn_amt,'trn_date'=>$dt,'trn_note'=>$gradefundlistval->grfn_description,'trn_paytype'=>'','usr_name'=>$Username,'trn_status'=>'Inactive']);
			//#!!-	Transaction Record Ends here	-!!#			
			//###Daily AMOUNT Calculation STARTS HERE ###
			if($gradefundlistval->grfn_paycycle == "Daily")
			{
				while(strtotime($doj1) <= strtotime($dt))
				{
					$data = array('bil_type'=>$gradefundlistval->grfn_paycycle,'fy_id'=>$gradefundlistval->fyr_id,'grd_id'=>$gradefundlistval->grd_id,'ftyp_id'=>$gradefundlistval->ftyp_id,'grfn_id'=>$gradefundlistval->grfn_id,'bil_amt'=>$gradefundlistval->grfn_amt,'bil_date'=>$doj1,'bil_note'=>$gradefundlistval->grfn_description,'usr_name'=>$Username,'bil_status'=>'Inactive','trn_id'=>$trinsid);
					$insertdetails = DB::table('bil_billrec')->insert($data);
					$doj1 = date ("Y-m-d", strtotime("+1 day", strtotime($doj1)));
				}
			}
			//###Daily AMOUNT Calculation ENDS HERE ###			
			//###Weekly AMOUNT Calculation STARTS HERE ###
			if($gradefundlistval->grfn_paycycle == "Weekly")
			{
				while(strtotime($doj1) <= strtotime($dt))
				{
					$data = array('bil_type'=>$gradefundlistval->grfn_paycycle,'fy_id'=>$gradefundlistval->fyr_id,'grd_id'=>$gradefundlistval->grd_id,'ftyp_id'=>$gradefundlistval->ftyp_id,'grfn_id'=>$gradefundlistval->grfn_id,'bil_amt'=>$gradefundlistval->grfn_amt,'bil_date'=>$doj1,'bil_note'=>$gradefundlistval->grfn_description,'usr_name'=>$Username,'bil_status'=>'Inactive','trn_id'=>$trinsid);
					$insertdetails = DB::table('bil_billrec')->insert($data);
					$doj1 = date ("Y-m-d", strtotime("+7 day", strtotime($doj1)));
				}
			}
			//###Weekly AMOUNT Calculation ENDS HERE ###			
			//###Monthly AMOUNT Calculation STARTS HERE ###
			if($gradefundlistval->grfn_paycycle == "Monthly")
			{
				while(strtotime($doj1) <= strtotime($dt))
				{
					$data = array('bil_type'=>$gradefundlistval->grfn_paycycle,'fy_id'=>$gradefundlistval->fyr_id,'grd_id'=>$gradefundlistval->grd_id,'ftyp_id'=>$gradefundlistval->ftyp_id,'grfn_id'=>$gradefundlistval->grfn_id,'bil_amt'=>$gradefundlistval->grfn_amt,'bil_date'=>$doj1,'bil_note'=>$gradefundlistval->grfn_description,'usr_name'=>$Username,'bil_status'=>'Inactive','trn_id'=>$trinsid);
					$insertdetails = DB::table('bil_billrec')->insert($data);
					$doj1 = date ("Y-m-d", strtotime("+1 month", strtotime($doj1)));
				}
			}
			//###Monthly AMOUNT Calculation ENDS HERE ###
			//###Quarterly AMOUNT Calculation STARTS HERE ###
			if($gradefundlistval->grfn_paycycle == "Quarterly")
			{
				while(strtotime($doj1) <= strtotime($dt))
				{
					$data = array('bil_type'=>$gradefundlistval->grfn_paycycle,'fy_id'=>$gradefundlistval->fyr_id,'grd_id'=>$gradefundlistval->grd_id,'ftyp_id'=>$gradefundlistval->ftyp_id,'grfn_id'=>$gradefundlistval->grfn_id,'bil_amt'=>$gradefundlistval->grfn_amt,'bil_date'=>$doj1,'bil_note'=>$gradefundlistval->grfn_description,'usr_name'=>$Username,'bil_status'=>'Inactive','trn_id'=>$trinsid);
					$insertdetails = DB::table('bil_billrec')->insert($data);
					$doj1 = date ("Y-m-d", strtotime("+3 month", strtotime($doj1)));
				}
			}
			//###Quarterly AMOUNT Calculation STARTS HERE ###
			//###Semiannually AMOUNT Calculation STARTS HERE ###
			if($gradefundlistval->grfn_paycycle == "Semiannually")
			{
				while(strtotime($doj1) <= strtotime($dt))
				{
					$data = array('bil_type'=>$gradefundlistval->grfn_paycycle,'fy_id'=>$gradefundlistval->fyr_id,'grd_id'=>$gradefundlistval->grd_id,'ftyp_id'=>$gradefundlistval->ftyp_id,'grfn_id'=>$gradefundlistval->grfn_id,'bil_amt'=>$gradefundlistval->grfn_amt,'bil_date'=>$doj1,'bil_note'=>$gradefundlistval->grfn_description,'usr_name'=>$Username,'bil_status'=>'Inactive','trn_id'=>$trinsid);
					$insertdetails = DB::table('bil_billrec')->insert($data);
					$doj1 = date ("Y-m-d", strtotime("+6 month", strtotime($doj1)));
				}
			}
			//###Semiannually AMOUNT Calculation ENDS HERE ###
			//###Yearly AMOUNT Calculation STARTS HERE ###
			if($gradefundlistval->grfn_paycycle == "Yearly")
			{
				while(strtotime($doj1) <= strtotime($dt))
				{
					$data = array('bil_type'=>$gradefundlistval->grfn_paycycle,'fy_id'=>$gradefundlistval->fyr_id,'grd_id'=>$gradefundlistval->grd_id,'ftyp_id'=>$gradefundlistval->ftyp_id,'grfn_id'=>$gradefundlistval->grfn_id,'bil_amt'=>$gradefundlistval->grfn_amt,'bil_date'=>$doj1,'bil_note'=>$gradefundlistval->grfn_description,'usr_name'=>$Username,'bil_status'=>'Inactive','trn_id'=>$trinsid);
					$insertdetails = DB::table('bil_billrec')->insert($data);
					$doj1 = date ("Y-m-d", strtotime("+1 year", strtotime($doj1)));
				}
			}
			//###Yearly AMOUNT Calculation ENDS HERE ###
			$grfn_amt=0;
		}
		$data['reclist'] = DB::select( DB::raw("SELECT ifnull(max(cst_code),0)+1 as custcode,(SELECT grd_prefix FROM grd_grade where grd_id = '$gradeid') as gradeprefix FROM cst_customer where cst_status != 'Deleted' AND cst_type='Member'") );
		//#####################	BILLING STARTS HERE
		$trn_transaction = DB::select( DB::raw("SELECT grd_grade.grd_grade,grd_grade.grd_prefix, ftyp_fundtype.ftyp_fundtype as ftype, grfn_gradefund.grfn_paycycle as paycycle,(SELECT ifnull(sum(bil_amt),0) FROM `bil_billrec` WHERE trn_id=trn_transaction.trn_id ) AS totalamt,(SELECT ifnull(sum(bil_amt),0) FROM `bil_billrec` WHERE paid_trn_id=trn_transaction.trn_id ) AS paidamt FROM trn_transaction LEFT JOIN grfn_gradefund ON trn_transaction.grfn_id=grfn_gradefund.grfn_id LEFT JOIN grd_grade ON grd_grade.grd_id=trn_transaction.grd_id LEFT JOIN ftyp_fundtype ON ftyp_fundtype.ftyp_id=trn_transaction.ftyp_id WHERE trn_transaction.usr_name='$Username' AND trn_transaction.trn_status='Inactive' AND trn_transaction.trn_type != 'Payment' ") );
		$slno=1;
		$bal = 0;
		$totamt = 0;
		$paidamt = 0;
		$balamt = 0;
		$transrecord="";
		$transrecord = "<table  class='table table-striped table-bordered' id='tblipb'><thead><tr><th>" . __('mylanguage.slno') ."</th><th>" . __('mylanguage.type') ."</th><th>" . __('mylanguage.Payment_cycle') ."</th><th style='text-align: right;'>" . __('mylanguage.Total_amount') ."</th><th style='text-align: right;'>" . __('mylanguage.Paid_amount') ."</th><th style='text-align: right;'> " . __('mylanguage.balance_amount') ."</th><th>" . __('mylanguage.action') ."</th></tr></thead><tbody>";
		foreach($trn_transaction as $trn_val)
		{
			$bal = $trn_val->totalamt - $trn_val->paidamt;
			$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $trn_val->ftype  . "</td><td>" .$trn_val->paycycle . "</td><td style='text-align: right;'>" .$trn_val->totalamt . "</td><td style='text-align: right;'>" .$trn_val->paidamt . "</td>  <td style='text-align: right;'>" . $bal ."</td><td><button class='btn btn-default btn-xs' ><i class='fa fa-eye' aria-hidden='true'></i></button></td></tr>";
			$slno++;
			$totamt = $totamt + $trn_val->totalamt;
			$paidamt = $paidamt + $trn_val->paidamt;
			$balamt = $balamt + $bal;
		}
		$transrecord = $transrecord. "</tbody><tfoot><tr><th></th><th></th><th>". __('mylanguage.grand_total') ."</th><th style='text-align: right;'> $totamt</th><th style='text-align: right;'> $paidamt</th><th style='text-align: right;'> $balamt</th><th></th></tr></tfoot></table>";
		$data['transrecord'] = $transrecord;
		$data['totamt'] =$totamt;
		$data['paidamt'] =$paidamt;
		$data['balamt'] =$balamt;
		//#####################	BILLING ENDS HERE
		//#####################	REPORT STARTS HERE 
		$rpt_transaction = DB::select( DB::raw("SELECT grd_grade.grd_grade,grd_grade.grd_prefix, ftyp_fundtype.ftyp_fundtype as ftype, grfn_gradefund.grfn_paycycle as paycycle, trn_transaction.trn_billno as billno, trn_transaction.trn_date as dt, (SELECT ifnull(sum(bil_amt),0) FROM `bil_billrec` WHERE trn_id=trn_transaction.trn_id ) AS totalamt,trn_transaction.trn_type FROM trn_transaction LEFT JOIN grfn_gradefund ON trn_transaction.grfn_id=grfn_gradefund.grfn_id LEFT JOIN grd_grade ON grd_grade.grd_id=trn_transaction.grd_id LEFT JOIN ftyp_fundtype ON ftyp_fundtype.ftyp_id=trn_transaction.ftyp_id WHERE trn_transaction.usr_name='$Username' AND trn_transaction.trn_status='Inactive' ORDER BY trn_transaction.trn_date ") );
		$rpt_slno=1;
		$rpt_bal = 0;
		$rpt_totamt = 0;
		$rpt_paidamt = 0;
		$rpt_balamt = 0;
		$rpt_transrecord="";
		$rpt_transrecord = "<table  class='table table-striped table-bordered' id='tbltransaction'><thead><tr><th>". __('mylanguage.slno')  ."</th><th>". __('mylanguage.transaction_type')  ."</th><th>". __('mylanguage.billno')  ."</th><th>".  __('mylanguage.date')  ."</th><th>". __('mylanguage.paymenttype')  ."</th><th style='text-align: right;'>". __('mylanguage.credit')  ."</th><th style='text-align: right;'>". __('mylanguage.debit')  ."</th><th style='text-align: right;'>". __('mylanguage.balance')  ."</th><th>". __('mylanguage.action')  ."</th></tr></thead><tbody>";
		$debit=0;
		$credit=0;
		$totcrdr=0;
		foreach($rpt_transaction as $rpt_val)
		{
			if($rpt_val->trn_type == "Payment")
			{
				$trntype= __('mylanguage.debit');
				$debit = $rpt_val->totalamt;
				$totcrdr= $totcrdr-$debit;
			}
			else
			{
				$trntype= __('mylanguage.credit');
				$credit=$rpt_val->totalamt;
				$totcrdr= $totcrdr+$credit;
			}
			$rpt_transrecord = $rpt_transrecord .  "<tr><td>" . $rpt_slno . "</td><td>" . $rpt_val->ftype . "(" . $rpt_val->paycycle . ")</td><td>" . $rpt_val->billno  . "</td><td>" . date("d-m-Y",strtotime($rpt_val->dt)) . "</td><td>$trntype</td><td style='text-align: right;'>" .$credit . "</td><td style='text-align: right;'>" .$debit . "</td><td style='text-align: right;'>$totcrdr</td><td><button class='btn btn-default btn-xs' ><i class='fa fa-eye' aria-hidden='true'></i></button></td></tr>";
			$rpt_slno++;
			$rpt_totamt= $rpt_totamt + $credit;
			$rpt_paidamt= $rpt_paidamt + $debit;
		}
		$rpt_transrecord = $rpt_transrecord. "</tbody><tfoot><tr><th></th><th></th><th></th><th></th><th>". __('mylanguage.grand_total') ."</th><th style='text-align: right;'> $rpt_totamt</th><th style='text-align: right;'>$rpt_paidamt</th><th style='text-align: right;'> $totcrdr</th><th></th></tr></tfoot></table>";
		$data['rpt_transrecord'] = $rpt_transrecord;
		//#####################	REPORT ENDS HERE
		return $data;
	}
	//#################Fund insert starts
	public function inscstgradefundCtrl(Request $req)
	{
		$user = auth()->user();
		$Username =  $user->usr_id;
		$grfn_id = $req->input('grfn_id');
		$ftyp_id = $req->input('ftyp_id');
		$grd_id = $req->input('grd_id');
		$fyr_id = $req->input('fyr_id');
		$fund_cust_id = $req->input('fund_cust_id');
		$grfn_amt = $req->input('grfn_amt');
		$grfn_paycycle = $req->input('grfn_paycycle');
		$grfn_description = $req->input('grfn_description');
		$grfn_status = $req->input('grfn_status');
		$dt= date("Y-m-d");
		$date_from = $req->input('date_from');
		$cst_customer=$this->JamathMod->LoadSingleCustMod($fund_cust_id);
		$qinsert = DB::table('grfn_gradefund')->insertGetId(['ftyp_id' => $ftyp_id,'grd_id' => $cst_customer->grd_id,'fyr_id' => $fyr_id,'grfn_amt' => $grfn_amt,'grfn_paycycle' => $grfn_paycycle,'grfn_description' => $grfn_description,'grfn_status' => $grfn_status]);
		//#!!-	Transaction Record Starts here--!!#
		//$trnlist = DB::table('trn_transaction')->where('ftyp_id',$ftyp_id)->where('trn_status','=','Active')->first();
		//if(is_null($trnlist))
		{
			$trinsid = DB::table('trn_transaction')->insertGetId(['cst_id'=>$fund_cust_id,'fy_id'=>$fyr_id,'grd_id'=>$cst_customer->grd_id,'grfn_id'=>$qinsert,'ftyp_id'=>$ftyp_id,'trn_type'=>$grfn_paycycle,'trn_billno'=>0,'trn_amt'=>$grfn_amt,'trn_date'=>$date_from,'trn_note'=>$grfn_description,'trn_paytype'=>'','usr_name'=>$Username,'trn_status'=>'Active']);
		}
		//else
		{
		//	$trinsid = $trnlist->trn_id;
		}
		//#!!-	Transaction Record Ends here--!!#
		$data = array('bil_type'=>$grfn_paycycle,'fy_id'=>$fyr_id,'grd_id'=>$cst_customer->grd_id,'cst_id'=>$fund_cust_id,'ftyp_id'=>$ftyp_id,'grfn_id'=>$qinsert,'bil_amt'=>$grfn_amt,'bil_date'=>$date_from,'bil_note'=>$grfn_description,'usr_name'=>$Username,'bil_status'=>'Active','trn_id'=>$trinsid);
		$insertdetails = DB::table('bil_billrec')->insert($data);		
//###################################################################################
//#########################-AUTO BALANCE ADJUSTMENT STARTS HERE-#####################
//###################################################################################
		$data['cst_id'] =$fund_cust_id;
		$bil_billrec = $this->JamathMod->CheckCustomerBal($data['cst_id']);
		$data['totalamt'] = $bil_billrec[0]->totalamt;
		$data['paidamt'] = $bil_billrec[0]->paidamt;
		$data['balamt'] = $data['totalamt'] -  ($data['paidamt'] + $bil_billrec[0]->advamt + $bil_billrec[0]->prepayamt );		
		$data['advamt'] = $bil_billrec[0]->advamt;
		$data['prepayamt'] = $bil_billrec[0]->prepayamt;
		//if($data['balamt'] >0)
		{
			if($data['advamt'] > 0)
			{
				$data['move_adv_bal'] = $this->JamathMod->moveAdvBalMod($bil_billrec);
			}
		}
//###################################################################################
//#########################-AUTO BALANCE ADJUSTMENT ENDS HERE-#######################
//###################################################################################		
		$bil_billrec = $this->JamathMod->CheckCustomerBal($fund_cust_id);
		return response()->json(['status' => '1', 'message' => trans('Fund added successfully...'), 'bil_billrec' => $bil_billrec]);
	}
	//#################Fund insert ends
	//###Select and Load SINGLE Customer Record Starts here	###
	public function LoadSingleCustCtrl(Request $req)
	{
		$cst_id=$req->cst_id;
		$cst_customer=$this->JamathMod->LoadSingleCustMod($cst_id);
		return response()->json($cst_customer);
	}
	//###Select and Load SINGLE Customer Record Ends here ###	
	//###Payment Calculation Starts here###
	public function CalculatePaymentCtrl(Request $req)
	{
		$cst_id=$req->cst_id;
		$cst_customer=$this->JamathMod->LoadSingleCustMod($cst_id);
		$payRecords = $this->JamathMod->CheckCustomerBal($cst_id);
		//#####################	BILLING TABLE STARTS HERE
		$trn_transaction = $this->JamathMod->TotPaidBalMod($cst_id);
		$slno=1;
		$bal = 0;
			$transrecord="";
			$transrecord = "<table  class='table table-sorting table-striped table-hover table-bordered datatable dataTable no-footer' id='tblipb'><thead><tr><th style='width: 30px;'>" . __('mylanguage.slno') ."</th><th>" . __('mylanguage.type') ."</th><th>" . __('mylanguage.Payment_cycle') ."</th><th style='width: 75px;'>" . __('mylanguage.total_bal_amt') ."</th><th style='width: 75px;'>" . __('mylanguage.amt_payable') ."</th><th style='width: 75px;'> " . __('mylanguage.balance_amount') ."</th><th  style='padding: 0 !important;margin: 0 !important;text-align: center;vertical-align: middle;width: 30px;'><i class='fa fa-eye' aria-hidden='true'></i></th></tr></thead><tbody>";
		foreach($trn_transaction as $trn_val)
		{
				if($trn_val->paycycle == "Daily")
				{
					$bilr_paycycle = "ಪ್ರತಿದಿನ";
				}
				else if($trn_val->paycycle == "Weekly")
				{
					$bilr_paycycle = "ವಾರಕ್ಕೊಮ್ಮೆ";
				}
				else if($trn_val->paycycle == "Monthly")
				{
					$bilr_paycycle = "ಮಾಸಿಕ";
				}
				else if($trn_val->paycycle == "Quarterly")
				{
					$bilr_paycycle = "ಮೂರು ತಿಂಗಳಿಗೊಮ್ಮೆ";
				}
				else if($trn_val->paycycle == "Semiannually")
				{
					$bilr_paycycle = "ಅರ್ಧ ವಾರ್ಷಿಕ";
				}
				else if($trn_val->paycycle == "Yearly")
				{
					$bilr_paycycle = "ವಾರ್ಷಿಕ";
				}
				else if($trn_val->paycycle == "One time")
				{
					$bilr_paycycle = "ಒಂದು ಬಾರಿ";
				}
			$bal = $trn_val->totalamt - $trn_val->paidamt;
			$transrecord = $transrecord .  "<tr><td><input type='hidden' name='trn_id[]' id='trn_id[]' value='" . $trn_val->trn_id . "' ><input type='hidden' name='slno[]' id='slno[]' value='" . $slno. "' ><input type='hidden' name='fy_id[]' id='fy_id[]' value='" . $trn_val->fy_id . "' ><input type='hidden' name='grd_id[]' id='grd_id[]' value='" . $trn_val->grd_id . "' ><input type='hidden' name='grfn_id[]' id='grfn_id[]' value='" . $trn_val->grfn_id . "' ><input type='hidden' name='ftyp_id[]' id='ftyp_id[]' value='" . $trn_val->ftyp_id . "' >" . $slno . "</td><td><input type='hidden' name='ftype[]' id='ftype[]' value='" . $trn_val->ftype . "' >" . $trn_val->ftype  . "</td><td><input type='hidden' name='paycycle[]' id='paycycle[]' value='" . $trn_val->paycycle . "' >" . $bilr_paycycle . "</td><td style='text-align: right;padding: 0 !important;  margin: 0 !important;'><input type='text' class='form-control' readonly style='text-align: right;' name='totbalamt[]' id='totbalamt[]' value='$bal' ></td><td style='text-align: right;padding: 0 !important;  margin: 0 !important;'><input type='text' class='form-control' style='text-align: right;' name='paidamt[]' id='paidamt[]' placeholder='0' onkeyup='calculatetotalamt()' ></td><td style='text-align: right;padding: 0 !important;  margin: 0 !important;'><input type='text' readonly class='form-control'  style='text-align: right;' name='balamt[]' id='balamt[]' value='" . $bal . "'></td><td style='padding: 0 !important;margin: 0 !important;text-align: center;vertical-align: middle;'><button class='btn btn-default btn-xs' title='View'><i class='fa fa-eye' aria-hidden='true'></i></button></td></tr>";
			$slno++;
		}
			$transrecord = $transrecord. "</tbody><tfoot><tr><th></th><th></th><th>". __('mylanguage.grand_total') ."</th><th style='text-align: right;padding: 0 !important;  margin: 0 !important;'><input type='text' readonly class='form-control'  style='text-align: right;' name='grandtotbal' id='grandtotbal' ></th><th style='text-align: right;padding: 0 !important;  margin: 0 !important;'><input type='text' readonly class='form-control'  style='text-align: right;' name='grandpaidbal' id='grandpaidbal' value='" . $bal . "'></th><th style='text-align: right;padding: 0 !important;  margin: 0 !important;'><input type='text' readonly class='form-control'  style='text-align: right;' name='grandbal' id='grandbal' value='" . $bal . "'></th><th style='text-align: right;padding: 0 !important;  margin: 0 !important;'></th></tr><tr><th></th><th></th><th>". __('mylanguage.adv_amt') ."</th><th style='text-align: right;padding: 0 !important;  margin: 0 !important;'></th><th style='text-align: right;padding: 0 !important;  margin: 0 !important;'><input type='text' readonly class='form-control'  style='text-align: right;' name='adv_amt' id='adv_amt' value='0'></th><th style='text-align: right;padding: 0 !important;  margin: 0 !important;'></th><th style='text-align: right;padding: 0 !important;  margin: 0 !important;'></th></tr></tfoot></table>";
		//#####################	BILLING TABLE ENDS HERE
		return response()->json(['cst_customer' =>$cst_customer,'payrecord' => $payRecords,'billingtable' => $transrecord]);
	}
	//###Payment Calculation Ends here###
	//### Make Billing payment starts here
	public function makebillingpaymentCtrl(Request $request)
	{
		$sub['pay_cust_id']=$request->input('pay_cust_id');
		$sub['paycstname']=$request->input('paycstname');
		$sub['payTotal_amount']=$request->input('payTotal_amount');
		$sub['payPaid_amount']=$request->input('payPaid_amount');
		$sub['paybalance_amount']=$request->input('paybalance_amount');
		$sub['amt_payable']=$request->input('amt_payable');
		
		$sub['arrtrn_id']=$request->input('arrtrn_id');
		$sub['arrslno']=$request->input('arrslno');
		$sub['arrftype']=$request->input('arrftype');
		$sub['arrpaycycle']=$request->input('arrpaycycle');	
		$sub['arrtotbalamt']=$request->input('arrtotbalamt');
		$sub['arrpaidamt']=$request->input('arrpaidamt');
		$sub['arrbalamt']=$request->input('arrbalamt');
		
		$sub['arrfy_id'] = $request->input('arrfy_id');
		$sub['arrgrd_id'] = $request->input('arrgrd_id');
		$sub['arrgrfn_id'] = $request->input('arrgrfn_id');
		$sub['arrftyp_id'] = $request->input('arrftyp_id');
		
		$sub['grandtotbal']=$request->input('grandtotbal');
		$sub['grandpaidbal']=$request->input('grandpaidbal');
		$sub['grandbal']=$request->input('grandbal');
		$sub['adv_amt']=$request->input('adv_amt');
		
		return $bill=$this->JamathMod->makebillingpaymentMod($sub);
	}
	//### Make Billing payment ends here
	public function PrintPaymentBillCtrl(Request $request)
	{
		$id=$request->input('transaction_id');
		$data['trn_transaction']=$this->JamathMod->transactionrpt($id);
		$data['trn_billno'] = $data['trn_transaction']->trn_billno;		
		$data['trn_date'] = date("d-m-Y",strtotime($data['trn_transaction']->trn_date));
		$cst_id=$data['trn_transaction']->cst_id;
		$data['cst_customer']=$this->JamathMod->LoadSingleCustMod($cst_id);
		$data['cst_name'] = $data['cst_customer']->cst_name;		
		$data['cst_code'] = $data['cst_customer']->cst_code;
		$data['grd_id'] =  $data['cst_customer']->grd_id;
		$data['cst_grade']=$this->JamathMod->LoadCustGrade($data['grd_id']);
		$data['cst_grade_prefix']=$data['cst_grade']->grd_prefix;
		$data['bilr_bilreceipt']=$this->JamathMod->billreceiptmod($id);		
		$data['bil_billrec'] = $this->JamathMod->CheckCustomerBal($cst_id);
		$transrecord="";
		$transrecord = "<input type='hidden' name='transaction_ids' id='transaction_ids' value='$id'><table  class='table table-bordered' id='tblipb'><thead><tr><th>" . __('mylanguage.slno') ."</th><th>" . __('mylanguage.type') ."</th><th>" . __('mylanguage.Payment_cycle') ."</th><th>" . __('mylanguage.total_bal_amt') ."</th><th>" . __('mylanguage.amt_payable') ."</th><th > " . __('mylanguage.balance_amount') ."</th></tr></thead><tbody>";
		$bilr_totbalamt=0;
		$bilr_paidamt=0;
		$bilr_balamt=0;
		$bilr_paycycle ="";
		$advamt = 0;
		foreach($data['bilr_bilreceipt'] as $trn_val)
		{
			if($trn_val->bilr_ftype == "Advance")
			{
				$advamt = $trn_val->bilr_paidamt;
			}
			else
			{
				if($trn_val->bilr_paycycle == "Daily")
				{
					$bilr_paycycle = "ಪ್ರತಿದಿನ";
				}
				else if($trn_val->bilr_paycycle == "Weekly")
				{
					$bilr_paycycle = "ವಾರಕ್ಕೊಮ್ಮೆ";
				}
				else if($trn_val->bilr_paycycle == "Monthly")
				{
					$bilr_paycycle = "ಮಾಸಿಕ";
				}
				else if($trn_val->bilr_paycycle == "Quarterly")
				{
					$bilr_paycycle = "ಮೂರು ತಿಂಗಳಿಗೊಮ್ಮೆ";
				}
				else if($trn_val->bilr_paycycle == "Semiannually")
				{
					$bilr_paycycle = "ಅರ್ಧ ವಾರ್ಷಿಕ";
				}
				else if($trn_val->bilr_paycycle == "Yearly")
				{
					$bilr_paycycle = "ವಾರ್ಷಿಕ";
				}
				else if($trn_val->bilr_paycycle == "One time")
				{
					$bilr_paycycle = "ಒಂದು ಬಾರಿ";
				}
				$transrecord = $transrecord .  "<tr><td>" . $trn_val->bilr_slno . "</td><td>" . $trn_val->bilr_ftype  . "</td><td>" . $bilr_paycycle . "</td><td style='text-align: right;'>" . $trn_val->bilr_totbalamt  . "</td><td style='text-align: right;'>" . $trn_val->bilr_paidamt  . "</td><td style='text-align: right;'>" . $trn_val->bilr_balamt  . "</td></tr>";
				$bilr_totbalamt=$bilr_totbalamt+$trn_val->bilr_totbalamt;
				$bilr_paidamt=$bilr_paidamt+$trn_val->bilr_paidamt;
				$bilr_balamt=$bilr_balamt+$trn_val->bilr_balamt;
				$bilr_paycycle ="";
			}
		}
		$transrecord = $transrecord. "</tbody><tfoot><tr><th></th><th></th><th>". __('mylanguage.grand_total') ."</th><th style='text-align: right;'> $bilr_totbalamt</th><th style='text-align: right;'> $bilr_paidamt</th><th style='text-align: right;'> $bilr_balamt</th></tr>";
		$transrecord = $transrecord. "<tr><th></th><th></th><th>". __('mylanguage.adv_amt') ."</th><th style='text-align: right;'></th><th style='text-align: right;'> $advamt</th><th style='text-align: right;'></th></tr>";
		$transrecord = $transrecord. "</tfoot></table>";
		$data['transrecord']=$transrecord;
		$data['bilr_paidamt'] = $bilr_paidamt;
		$data['bilr_gtotal'] = $bilr_paidamt + $advamt ;
		return $data;
	}
	
	public function viewsingleinvoiceCtrl(Request $request)
	{
		$id=$request->input('bill_id');
		$data['trn_transaction']=$this->JamathMod->billingsinglerecrpt($id);
		$trn_id = $data['trn_transaction'][0]->trn_id;
		$data['trn_billno'] = $data['trn_transaction'][0]->bil_id;		
		$data['trn_date'] = date("d-m-Y",strtotime($data['trn_transaction'][0]->bil_date));
		$cst_id=$data['trn_transaction'][0]->cst_id;
		$data['cst_customer']=$this->JamathMod->LoadSingleCustMod($cst_id);
		$data['cst_name'] = $data['cst_customer']->cst_name;		
		$data['cst_code'] = $data['cst_customer']->cst_code;
		$data['grd_id'] =  $data['cst_customer']->grd_id;
		$data['cst_grade']=$this->JamathMod->LoadCustGrade($data['grd_id']);
		$data['cst_grade_prefix']=$data['cst_grade']->grd_prefix;
		$data['bilr_bilreceipt']=$this->JamathMod->billreceiptmod($id);	
		$data['bil_billrec'] = $this->JamathMod->CheckCustomerBal($cst_id);
		$transrecord="";
		$transrecord = "<input type='hidden' name='transaction_idsa' id='transaction_idsa' value='$id'>";
		$transrecord = $transrecord . "<input type='hidden' name='trn_ida' id='trn_ida' value='$trn_id'>";
		$data['transrecord']=$transrecord;
		$data['bilr_paidamt'] = $data['trn_transaction'][0]->bil_amt;
		return $data;
	}
	
	public function viewinvoicebillCtrl(Request $request)
	{
		$id=$request->input('transaction_id');
		$data['trn_transaction']=$this->JamathMod->transactionrpt($id);
		$data['trn_billno'] = $data['trn_transaction']->trn_id;		
		$data['trn_date'] = date("d-m-Y",strtotime($data['trn_transaction']->trn_date));
		$cst_id=$data['trn_transaction']->cst_id;
		$data['cst_customer']=$this->JamathMod->LoadSingleCustMod($cst_id);
		$data['cst_name'] = $data['cst_customer']->cst_name;		
		$data['cst_code'] = $data['cst_customer']->cst_code;
		$data['grd_id'] =  $data['cst_customer']->grd_id;
		$data['cst_grade']=$this->JamathMod->LoadCustGrade($data['grd_id']);
		$data['cst_grade_prefix']=$data['cst_grade']->grd_prefix;
		//$data['bilr_bilreceipt']=$this->JamathMod->billreceiptmod($id);		
		//$data['bil_billrec'] = $this->JamathMod->CheckCustomerBal($cst_id);
		$data['bil_billrec'] = $this->JamathMod->CheckCustomerBal($id);
		$transrecord="";
		$transrecord = "<input type='hidden' name='transaction_ids' id='transaction_ids' value='$id'><table  class='table table-bordered' id='tblipb'><thead><tr><th>" . __('mylanguage.slno') ."</th><th>" . __('mylanguage.type') ."</th><th>" . __('mylanguage.Payment_cycle') ."</th><th>" . __('mylanguage.total_bal_amt') ."</th><th>" . __('mylanguage.amt_payable') ."</th><th > " . __('mylanguage.balance_amount') ."</th></tr></thead><tbody>";
		$bilr_totbalamt=0;
		$bilr_paidamt=0;
		$bilr_balamt=0;
		$bilr_paycycle ="";
		$advamt = 0;
		foreach($data['bilr_bilreceipt'] as $trn_val)
		{
			if($trn_val->bilr_ftype == "Advance")
			{
				$advamt = $trn_val->bilr_paidamt;
			}
			else
			{
				if($trn_val->bilr_paycycle == "Daily")
				{
					$bilr_paycycle = "ಪ್ರತಿದಿನ";
				}
				else if($trn_val->bilr_paycycle == "Weekly")
				{
					$bilr_paycycle = "ವಾರಕ್ಕೊಮ್ಮೆ";
				}
				else if($trn_val->bilr_paycycle == "Monthly")
				{
					$bilr_paycycle = "ಮಾಸಿಕ";
				}
				else if($trn_val->bilr_paycycle == "Quarterly")
				{
					$bilr_paycycle = "ಮೂರು ತಿಂಗಳಿಗೊಮ್ಮೆ";
				}
				else if($trn_val->bilr_paycycle == "Semiannually")
				{
					$bilr_paycycle = "ಅರ್ಧ ವಾರ್ಷಿಕ";
				}
				else if($trn_val->bilr_paycycle == "Yearly")
				{
					$bilr_paycycle = "ವಾರ್ಷಿಕ";
				}
				else if($trn_val->bilr_paycycle == "One time")
				{
					$bilr_paycycle = "ಒಂದು ಬಾರಿ";
				}
				$transrecord = $transrecord .  "<tr><td>" . $trn_val->bilr_slno . "</td><td>" . $trn_val->bilr_ftype  . "</td><td>" . $bilr_paycycle . "</td><td style='text-align: right;'>" . $trn_val->bilr_totbalamt  . "</td><td style='text-align: right;'>" . $trn_val->bilr_paidamt  . "</td><td style='text-align: right;'>" . $trn_val->bilr_balamt  . "</td></tr>";
				$bilr_totbalamt=$bilr_totbalamt+$trn_val->bilr_totbalamt;
				$bilr_paidamt=$bilr_paidamt+$trn_val->bilr_paidamt;
				$bilr_balamt=$bilr_balamt+$trn_val->bilr_balamt;
				$bilr_paycycle ="";
			}
		}
		$transrecord = $transrecord. "</tbody><tfoot><tr><th></th><th></th><th>". __('mylanguage.grand_total') ."</th><th style='text-align: right;'> $bilr_totbalamt</th><th style='text-align: right;'> $bilr_paidamt</th><th style='text-align: right;'> $bilr_balamt</th></tr>";
		$transrecord = $transrecord. "<tr><th></th><th></th><th>". __('mylanguage.adv_amt') ."</th><th style='text-align: right;'></th><th style='text-align: right;'> $advamt</th><th style='text-align: right;'></th></tr>";
		$transrecord = $transrecord. "</tfoot></table>";
		$data['transrecord']=$transrecord;
		$data['bilr_paidamt'] = $bilr_paidamt;
		$data['bilr_gtotal'] = $bilr_paidamt + $advamt ;
		return $data;
	}
	
	public function printprintbillCtrl(Request $request)
	{
		$advamt =0;
		$data['transaction_id'] = $request->input('transaction_id');		
		$id=$request->input('transaction_id');
		$data['trn_transaction']=$this->JamathMod->transactionrpt($id);
		$data['trn_billno'] = $data['trn_transaction']->trn_billno;		
		$data['trn_date'] = $data['trn_transaction']->trn_date;
		$cst_id=$data['trn_transaction']->cst_id;
		$data['cst_customer']=$this->JamathMod->LoadSingleCustMod($cst_id);
		$data['cst_name'] = $data['cst_customer']->cst_name;		
		$data['cst_code'] = $data['cst_customer']->cst_code;
		$data['grd_id'] =  $data['cst_customer']->grd_id;
		$data['cst_grade']=$this->JamathMod->LoadCustGrade($data['grd_id']);
		$data['cst_grade_prefix']=$data['cst_grade']->grd_prefix;
		$data['bilr_bilreceipt']=$this->JamathMod->billreceiptmod($id);
		$transrecord="";
		$transrecord = "<table  class='table table-condensed table-bordered' ><thead><tr><th>" . __('mylanguage.slno') ."</th><th>" . __('mylanguage.type') ."</th><th style='text-align: right;' >" . __('mylanguage.total_bal_amt') ."</th><th style='text-align: right;' >" . __('mylanguage.amt_payable') ."</th><th style='text-align: right;' > " . __('mylanguage.balance_amount') ."</th></tr></thead><tbody>";
		$bilr_totbalamt=0;
		$bilr_paidamt=0;
		$bilr_balamt=0;
		foreach($data['bilr_bilreceipt'] as $trn_val)
		{			
			if($trn_val->bilr_ftype == "Advance")
			{
				$advamt = $trn_val->bilr_paidamt;
			}
			else
			{
				if($trn_val->bilr_paycycle == "Daily")
				{
					$bilr_paycycle = "ಪ್ರತಿದಿನ";
				}
				else if($trn_val->bilr_paycycle == "Weekly")
				{
					$bilr_paycycle = "ವಾರಕ್ಕೊಮ್ಮೆ";
				}
				else if($trn_val->bilr_paycycle == "Monthly")
				{
					$bilr_paycycle = "ಮಾಸಿಕ";
				}
				else if($trn_val->bilr_paycycle == "Quarterly")
				{
					$bilr_paycycle = "ಮೂರು ತಿಂಗಳಿಗೊಮ್ಮೆ";
				}
				else if($trn_val->bilr_paycycle == "Semiannually")
				{
					$bilr_paycycle = "ಅರ್ಧ ವಾರ್ಷಿಕ";
				}
				else if($trn_val->bilr_paycycle == "Yearly")
				{
					$bilr_paycycle = "ವಾರ್ಷಿಕ";
				}
				else if($trn_val->bilr_paycycle == "One time")
				{
					$bilr_paycycle = "ಒಂದು ಬಾರಿ";
				}
			
				$transrecord = $transrecord .  "<tr><td>" . $trn_val->bilr_slno . "</td><td>" . $trn_val->bilr_ftype  . "</td><td style='text-align: right;'>" . $trn_val->bilr_totbalamt  . "</td><td style='text-align: right;'>" . $trn_val->bilr_paidamt  . "</td><td style='text-align: right;'>" . $trn_val->bilr_balamt  . "</td></tr>";
				$bilr_totbalamt=$bilr_totbalamt+$trn_val->bilr_totbalamt;
				$bilr_paidamt=$bilr_paidamt+$trn_val->bilr_paidamt;
				$bilr_balamt=$bilr_balamt+$trn_val->bilr_balamt;
			}
		}
		$transrecord = $transrecord. "<tr><th></th><th>". __('mylanguage.grand_total') ."</th><th style='text-align: right;'> $bilr_totbalamt</th><th style='text-align: right;'> $bilr_paidamt</th><th style='text-align: right;'> $bilr_balamt</th></tr>";
		if($advamt > 0 )
		{
		$transrecord = $transrecord. "<tr><th></th><th>". __('mylanguage.adv_amt') ."</th><th style='text-align: right;'></th><th style='text-align: right;'> $advamt</th><th style='text-align: right;'></th></tr>";
		}
		$transrecord = $transrecord. "</tbody></table>";
		$data['transrecord']=$transrecord;
		$data['bilr_paidamt'] = $bilr_paidamt;
		$data['bilr_gtotal'] = $bilr_paidamt + $advamt;
		//#############################
		return view('Mosque.printbill',compact('data'));
	}
	//Edit Customer Record starts here	
	public function LoadEditingAppCtrl(Request $request)
	{
		$cst_id = $request->input('edit_cst_id');
		$data['cst_id'] = $request->input('edit_cst_id');
		if($cst_id != 0)
		{
			$data['cst_customer']=$this->JamathMod->LoadSingleCustMod($cst_id);
			$data['cst_grade']=$this->JamathMod->LoadCustGrade($data['cst_customer']->grd_id);
			$data['resaddress']=unserialize($data['cst_customer']->cst_resaddress);
			$data['permaddress']=unserialize($data['cst_customer']->cst_permaddress);
			//###############################################
				$data['view_family_record'] = $this->JamathMod->ViewFamilyMember($data);
				$transrecord = "";
				$transrecord = "<table  class='table table-striped table-bordered' id='tabloan'><thead><tr><th>" .  __('mylanguage.slno')  . "</th><th>" .  __('mylanguage.relationship')  ."</th><th>" .  __('mylanguage.name')  ."</th><th>" .  __('mylanguage.gender')  ."</th><th>" .  __('mylanguage.dob')  . "</th><th>" .   __('mylanguage.marital_status')  . "</th><th>" .  __('mylanguage.qualification')   . "</th><th>" .  __('mylanguage.Mobno') . "</th><th style='width: 100px;'>" .  __('mylanguage.action')  ."</th></tr>	</thead><tbody>";
				$slno =1;
			foreach($data['view_family_record'] as $view_family_record)
			{
				$contact = unserialize($view_family_record->cst_resaddress);
				$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $view_family_record->cst_relationship  . "</td><td style='text-align: left;'>" . $view_family_record->cst_name  . "</td><td style='text-align: left;'>" . $view_family_record->cst_gender  . "</td><td style='text-align: left;'>" . date("d-m-Y",strtotime($view_family_record->cst_dob))  . "</td><td>" . $view_family_record->cst_maritalstatus  . "</td><td>" . $view_family_record->cst_qualification  . "</td><td style='text-align: left;'>" . $contact[0] . "</td><td style='text-align: right;'><button type='button' class='btn btn-default btn-xs edit'  onclick='funeditfamilymembers($view_family_record->cst_id)'><i class='fa fa-pencil-square-o' aria-hidden='true'></i></button> <button type='button' class='btn btn-default btn-xs print'   onclick='DeleteFamilyMembers($view_family_record->cst_id)'><i class='fa fa-trash' aria-hidden='true'></i></button></td></tr>";
				$slno++;
			}
				$transrecord = $transrecord. "</tbody></table>";
				$data['transrecord']=$transrecord;
			//###############################################
			
			//###############################################
			$data['view_member_docs'] = $this->JamathMod->ViewMemberDocs($data);	
			$transrecord = "";
			$transrecord = "<table  class='table table-striped table-bordered' id='tabloan'><thead><tr><th>" .  __('mylanguage.slno')  . "</th><th>" .  __('mylanguage.document_type')  ."</th><th>" .  __('mylanguage.idnocode')  ."</th><th>" .  __('mylanguage.document_file')  ."</th><th>" .  __('mylanguage.more_detail')  . "</th><th style='width: 100px;'>" .  __('mylanguage.action')  ."</th></tr>	</thead><tbody>";
			$slno =1;
			$filename="";
			foreach($data['view_member_docs'] as $view_member_docs)
			{
				if(\File::extension($view_member_docs->doc_docfile) == "")
				{
					$filename = "img/defaultimg.png";
				}
				else
				{
					$filename = $this->JamathMod->fileexists($view_member_docs->doc_docfile);
				}
				$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $view_member_docs->doc_doctype  . "</td><td style='text-align: left;'>" . $view_member_docs->doc_docno  . "</td><td style='text-align: left;'>";
				if($filename == "img/defaultimg.png")
				{
					$transrecord = $transrecord .  "";
				}
				else
				{
					$transrecord = $transrecord .  "<a href='" . $view_member_docs->doc_docfile . "' download>Download</a>";
				}
				$transrecord = $transrecord .  "</td><td>" . $view_member_docs->doc_detail  . "</td><td><button type='button'  class='btn btn-default btn-xs print' onclick='DeleteMemberDocuments($view_member_docs->doc_id)' ><i class='fa fa-trash' aria-hidden='true' ></i></button></td></tr>";
				$slno++;
			}
			$transrecord = $transrecord. "</tbody></table>";
			$data['docrecord']=$transrecord;
			//###############################################
			//#####################	BILLING TABLE STARTS HERE
			$bil_billrec = $this->JamathMod->CheckCustomerBal($cst_id);			
			$data['totalamt'] = $bil_billrec[0]->totalamt;
			$data['paidamt'] = $bil_billrec[0]->paidamt;
			$data['balamt'] = ($data['paidamt'] + $bil_billrec[0]->advamt + $bil_billrec[0]->prepayamt ) - $data['totalamt'];		
			$data['advamt'] = $bil_billrec[0]->advamt;
			$data['prepayamt'] = $bil_billrec[0]->prepayamt;
			if($data['balamt'] < 0 )
			{
				$data['balamt'] = 0;
			}
			$trn_transaction = $this->JamathMod->TotPaidBalMod($cst_id);
			$slno=1;
			$bal = 0;
			$totamt = 0; $paidamt=0; $balamt=0; $advbal=0;
			$transrecord="";
			$transrecord = "<table  class='table table-sorting table-striped table-hover table-bordered datatable dataTable no-footer' id='tblipb'><thead><tr><th>" . __('mylanguage.slno') ."</th><th>" . __('mylanguage.type') ."</th><th>" . __('mylanguage.Payment_cycle') ."</th><th style='width: 100px;text-align: right;''>" . __('mylanguage.total_bal_amt') ."</th><th style='width: 100px;text-align: right;'>" . __('mylanguage.amt_payable') ."</th><th style='width: 100px;text-align: right;'> " . __('mylanguage.balance_amount') ."</th></tr></thead><tbody>";
			foreach($trn_transaction as $trn_val)
			{
				$bal = $trn_val->totalamt - $trn_val->paidamt;
				$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $trn_val->ftype  . "</td><td>" . $trn_val->paycycle . "</td><th style='text-align: right;'>$trn_val->totalamt</th><th style='text-align: right;'>$trn_val->paidamt</th><th style='text-align: right;'>$bal</th></tr>";
				$slno++;
				$totamt = $totamt + $trn_val->totalamt; 
				$paidamt= $paidamt + $trn_val->paidamt; 
				$balamt=$balamt + $bal;
			}
			$advbal= $paidamt - $totamt;
			if($advbal <0)
			{
				$advbal=0;
			}
			$transrecord = $transrecord. "</tbody><tfoot><tr><th></th><th></th><th>". __('mylanguage.grand_total') ."</th><th style='text-align: right;'>$totamt</th><th style='text-align: right;'>$paidamt</th><th style='text-align: right;'>$balamt</th></tr>";
			$transrecord = $transrecord. "<tr><th></th><th></th><th>". __('mylanguage.adv_amt') ."</th><th style='text-align: right;'></th><th style='text-align: right;'>"  . $data['balamt']  . "</th><th style='text-align: right;'></th></tr>";
			$transrecord = $transrecord. "</tfoot></table>";
			$transrecord = $transrecord. "<center><a class='form-control' style='width: 100px;' target='_blank' href='loadprintcrdr?cst_id=$cst_id' >Print</a></center><hr>";
			$data['crdrrecord']=$transrecord;
			//#####################	BILLING TABLE ENDS HERE
			//####################REPORT STARTS HERE
			$trn_transaction = $this->JamathMod->TransactionReport($cst_id);
			$transrecord="";
			$transrecord = "<table id='datatable-data-export' class='table table-sorting table-striped table-hover table-bordered datatable' id='tblipb'><thead><tr><th style='width: 75px;'>" . __('report.slno') ."</th><th style='width: 175px;'>" . __('report.transaction_report') ."</th><th style='width: 100px;'>" . __('report.transaction_date') ."</th><th>" . __('report.transaction_particular') ."</th><th style='width: 100px;text-align: right;'>" . __('report.transaction_bill') ."</th><th style='width: 100px;text-align: right;'> " . __('report.transaction_payment') ."</th><th style='width: 100px;text-align: right;'> " . __('report.transaction_sum') ."</th><th style='width: 25px;text-align: right;'> " . __('mylanguage.action') ."</th></tr></thead><tbody>";
			$payable = 0;
			$paid = 0;
			$totamt = 0;
			foreach($trn_transaction as $trn_val)
			{
				if($trn_val->trn_type == "Daily")
				{
					$bilr_paycycle = "ಪ್ರತಿದಿನ";
				}
				else if($trn_val->trn_type == "Weekly")
				{
					$bilr_paycycle = "ವಾರಕ್ಕೊಮ್ಮೆ";
				}
				else if($trn_val->trn_type == "Monthly")
				{
					$bilr_paycycle = "ಮಾಸಿಕ";
				}
				else if($trn_val->trn_type == "Quarterly")
				{
					$bilr_paycycle = "ಮೂರು ತಿಂಗಳಿಗೊಮ್ಮೆ";
				}
				else if($trn_val->trn_type == "Semiannually")
				{
					$bilr_paycycle = "ಅರ್ಧ ವಾರ್ಷಿಕ";
				}
				else if($trn_val->trn_type == "Yearly")
				{
					$bilr_paycycle = "ವಾರ್ಷಿಕ";
				}
				else if($trn_val->trn_type == "One time")
				{
					$bilr_paycycle = "ಒಂದು ಬಾರಿ";
				}
if($trn_val->trn_type == "Daily" || $trn_val->trn_type == "Weekly" || $trn_val->trn_type == "Monthly" ||  $trn_val->trn_type == "Quarterly" ||  $trn_val->trn_type == "Semiannually" ||  $trn_val->trn_type == "Yearly" ||  $trn_val->trn_type == "One time" || $trn_val->trn_type == "Payment" )
{
	if($trn_val->trn_type == "Daily" || $trn_val->trn_type == "Weekly" || $trn_val->trn_type == "Monthly" ||  $trn_val->trn_type == "Quarterly" ||  $trn_val->trn_type == "Semiannually" ||  $trn_val->trn_type == "Yearly" ||  $trn_val->trn_type == "One time")
	{
		$transrecord = $transrecord .  "<tr style='color: #2b2121;'>";
		$transrecord = $transrecord .  "<td>" . $trn_val->trn_id . "</td>";
	}
	if($trn_val->trn_type == "Payment")
	{
		$transrecord = $transrecord .  "<tr style='color: green;'>";
		$transrecord = $transrecord .  "<th>" . $trn_val->trn_billno . "</th>";
	}	
		$transrecord = $transrecord .  "<td>";
		if($trn_val->trn_type == "Payment")
		{
			$transrecord = $transrecord .   $trn_val->trn_type;
		}
		else
		{
			$transrecord = $transrecord .  $trn_val->ftyp_fundtype . " (" . $bilr_paycycle .")";
		}
	$transrecord = $transrecord .  "</td><td>" . date("d-m-Y",strtotime($trn_val->trn_date))  . "</td>";
	$transrecord = $transrecord .  "<td>" . $trn_val->trn_note . "</td>";	
	if($trn_val->trn_type == "Daily" || $trn_val->trn_type == "Weekly" || $trn_val->trn_type == "Monthly" ||  $trn_val->trn_type == "Quarterly" ||  $trn_val->trn_type == "Semiannually" ||  $trn_val->trn_type == "Yearly" ||  $trn_val->trn_type == "One time")
	{
		$payable = $payable + $trn_val->billamt;
		$totamt = $totamt + $trn_val->billamt;
		$transrecord = $transrecord .  "<th style='text-align: right;'>"  . $trn_val->billamt  ."</th>";
	}
	else
	{
		$transrecord = $transrecord .  "<th style='text-align: right;'></th>";
	}
	if($trn_val->trn_type == "Payment")
	{
		$totamt = $totamt - $trn_val->billamt;
		$paid = $paid + $trn_val->billamt;
		$transrecord = $transrecord .  "<th style='text-align: right;'>"  . $trn_val->billamt  ."</th>";
	}
	else
	{
		$transrecord = $transrecord .  "<th style='text-align: right;'></th>";
	}
	$transrecord = $transrecord .  "<th style='text-align: right;'>"  . $totamt  ."</th>";
	$transrecord = $transrecord .  "<td style='text-align: right;'>";
	
	if($trn_val->trn_type == "Payment")
	{
		$transrecord = $transrecord . "<button class='btn btn-default btn-xs'  type='button' onclick='BillPrint(" . $trn_val->trn_id .  ")' title='View Bill'><i class='fa fa-eye' aria-hidden='true'></i></button>";
	}
	else
	{
		$transrecord = $transrecord . "<button class='btn btn-danger btn-xs'  type='button' onclick='ViewInvoice(" . $trn_val->trn_id .  ")' title='View Invoice'><i class='fa fa-eye' aria-hidden='true'></i></button>";
	}
	$transrecord = $transrecord . "</td>";
	$transrecord = $transrecord .  "</tr>";
}
			}	
			$transrecord = $transrecord. "</tbody>";		 
			$transrecord = $transrecord. "<tfoot><tr><th style='width: 75px;'></th><th style='width: 100px;'></th><th style='width: 100px;'></th><th style='text-align: right;'>Total :</th><th style='width: 100px;text-align: right;'>$payable</th><th style='width: 100px;text-align: right;'>$paid</th><th style='width: 100px;text-align: right;'>$totamt</th><th style='width: 25px;text-align: right;'></th></tr></tfoot>";			
			$transrecord = $transrecord. "</table>";
			$transrecord = $transrecord. "<center><a class='form-control' style='width: 100px;' target='_blank' href='loadprinttrans?cst_id=$cst_id' >Print</a></center><hr>";
			$data['cstrpt']=$transrecord;
			//####################REPORT ENDS HERE			
			$data['totamt']=$totamt;
			$data['dp']=$this->JamathMod->LoadDP($cst_id);
			if ($data['dp'] === null)
			{
				$data['dpname'] = "img/dpimg.jpg";
			}
			else
			{
				$data['dpname']	= $data['dp']->doc_docfile;	
			}			
			return response()->json($data);
		}
	}
	//Edit Customer Record ends here
	//Print Customer Record starts here	
	public function LoadprintingAppCtrl(Request $request)
	{
		$cst_id = $request->input('edit_cst_id');
		$data['cst_id'] = $request->input('edit_cst_id');
		if($cst_id != 0)
		{
			$data['cst_customer']=$this->JamathMod->LoadSingleCustMod($cst_id);
			$data['cst_grade']=$this->JamathMod->LoadCustGrade($data['cst_customer']->grd_id);
			$data['resaddress']=unserialize($data['cst_customer']->cst_resaddress);
			$data['permaddress']=unserialize($data['cst_customer']->cst_permaddress);
			//###############################################
				$data['view_family_record'] = $this->JamathMod->ViewFamilyMember($data);
				$transrecord = "";
				$transrecord = "<table  class='table table-striped table-bordered' id='tabloan'><thead><tr><th>" .  __('mylanguage.slno')  . "</th><th>" .  __('mylanguage.relationship')  ."</th><th>" .  __('mylanguage.name')  ."</th><th>" .  __('mylanguage.gender')  ."</th><th>" .  __('mylanguage.dob')  . "</th><th>" .   __('mylanguage.marital_status')  . "</th><th>" .  __('mylanguage.qualification')   . "</th><th>" .  __('mylanguage.Mobno') . "</th></tr>	</thead><tbody>";
				$slno =1;
			foreach($data['view_family_record'] as $view_family_record)
			{
				$contact = unserialize($view_family_record->cst_resaddress);
				$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $view_family_record->cst_relationship  . "</td><td style='text-align: left;'>" . $view_family_record->cst_name  . "</td><td style='text-align: left;'>" . $view_family_record->cst_gender  . "</td><td style='text-align: left;'>" . date("d-m-Y",strtotime($view_family_record->cst_dob))  . "</td><td>" . $view_family_record->cst_maritalstatus  . "</td><td>" . $view_family_record->cst_qualification  . "</td><td style='text-align: left;'>" . $contact[0] . "</td></tr>";
				$slno++;
			}
				$transrecord = $transrecord. "</tbody></table>";
				$data['transrecord']=$transrecord;
			//###############################################
			
			//###############################################
			$data['view_member_docs'] = $this->JamathMod->ViewMemberDocs($data);	
			$transrecord = "";
			$transrecord = "<table  class='table table-striped table-bordered' id='tabloan'><thead><tr><th>" .  __('mylanguage.slno')  . "</th><th>" .  __('mylanguage.document_type')  ."</th><th>" .  __('mylanguage.idnocode')  ."</th></tr>	</thead><tbody>";
			$slno =1;
			$filename="";
			foreach($data['view_member_docs'] as $view_member_docs)
			{
				if(\File::extension($view_member_docs->doc_docfile) == "")
				{
					$filename = "img/defaultimg.png";
				}
				else
				{
					$filename = $this->JamathMod->fileexists($view_member_docs->doc_docfile);
				}
				$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $view_member_docs->doc_doctype  . "</td><td style='text-align: left;'>" . $view_member_docs->doc_docno  . "</td></tr>";
				$slno++;
			}
			$transrecord = $transrecord. "</tbody></table>";
			$data['docrecord']=$transrecord;
			//###############################################
			//#####################	BILLING TABLE STARTS HERE
			$bil_billrec = $this->JamathMod->CheckCustomerBal($cst_id);			
			$data['totalamt'] = $bil_billrec[0]->totalamt;
			$data['paidamt'] = $bil_billrec[0]->paidamt;
			$data['balamt'] = ($data['paidamt'] + $bil_billrec[0]->advamt + $bil_billrec[0]->prepayamt ) - $data['totalamt'];		
			$data['advamt'] = $bil_billrec[0]->advamt;
			$data['prepayamt'] = $bil_billrec[0]->prepayamt;
			if($data['balamt'] < 0 )
			{
				$data['balamt'] = 0;
			}
			$trn_transaction = $this->JamathMod->TotPaidBalMod($cst_id);
			$slno=1;
			$bal = 0;
			$totamt = 0; $paidamt=0; $balamt=0; $advbal=0;
			$transrecord="";
			$transrecord = "<table  class='table table-sorting table-striped table-hover table-bordered datatable dataTable no-footer' id='tblipb'><thead><tr><th>" . __('mylanguage.slno') ."</th><th>" . __('mylanguage.type') ."</th><th>" . __('mylanguage.Payment_cycle') ."</th><th style='width: 100px;text-align: right;''>" . __('mylanguage.total_bal_amt') ."</th><th style='width: 100px;text-align: right;'>" . __('mylanguage.amt_payable') ."</th><th style='width: 100px;text-align: right;'> " . __('mylanguage.balance_amount') ."</th></tr></thead><tbody>";
			foreach($trn_transaction as $trn_val)
			{
				$bal = $trn_val->totalamt - $trn_val->paidamt;
				$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $trn_val->ftype  . "</td><td>" . $trn_val->paycycle . "</td><th style='text-align: right;'>$trn_val->totalamt</th><th style='text-align: right;'>$trn_val->paidamt</th><th style='text-align: right;'>$bal</th></tr>";
				$slno++;
				$totamt = $totamt + $trn_val->totalamt; 
				$paidamt= $paidamt + $trn_val->paidamt; 
				$balamt=$balamt + $bal;
			}
			$advbal= $paidamt - $totamt;
			if($advbal <0)
			{
				$advbal=0;
			}
			$transrecord = $transrecord. "</tbody><tfoot><tr><th></th><th></th><th>". __('mylanguage.grand_total') ."</th><th style='text-align: right;'>$totamt</th><th style='text-align: right;'>$paidamt</th><th style='text-align: right;'>$balamt</th></tr>";
			$transrecord = $transrecord. "<tr><th></th><th></th><th>". __('mylanguage.adv_amt') ."</th><th style='text-align: right;'></th><th style='text-align: right;'>"  . $data['balamt']  . "</th><th style='text-align: right;'></th></tr>";
			$transrecord = $transrecord. "</tfoot></table>";
			$data['crdrrecord']=$transrecord;
			//#####################	BILLING TABLE ENDS HERE
			//####################REPORT STARTS HERE
			$trn_transaction = $this->JamathMod->TransactionReport($cst_id);
			$transrecord="";
			$transrecord = "<table id='datatable-data-export' class='table table-sorting table-striped table-hover table-bordered datatable' id='tblipb'><thead><tr><th style='width: 75px;'>" . __('report.slno') ."</th><th style='width: 175px;'>" . __('report.transaction_report') ."</th><th style='width: 100px;'>" . __('report.transaction_date') ."</th><th>" . __('report.transaction_particular') ."</th><th style='width: 100px;text-align: right;'>" . __('report.transaction_bill') ."</th><th style='width: 100px;text-align: right;'> " . __('report.transaction_payment') ."</th><th style='width: 100px;text-align: right;'> " . __('report.transaction_sum') ."</th></tr></thead><tbody>";
			$payable = 0;
			$paid = 0;
			$totamt = 0;
			foreach($trn_transaction as $trn_val)
			{
				if($trn_val->trn_type == "Daily")
				{
					$bilr_paycycle = "ಪ್ರತಿದಿನ";
				}
				else if($trn_val->trn_type == "Weekly")
				{
					$bilr_paycycle = "ವಾರಕ್ಕೊಮ್ಮೆ";
				}
				else if($trn_val->trn_type == "Monthly")
				{
					$bilr_paycycle = "ಮಾಸಿಕ";
				}
				else if($trn_val->trn_type == "Quarterly")
				{
					$bilr_paycycle = "ಮೂರು ತಿಂಗಳಿಗೊಮ್ಮೆ";
				}
				else if($trn_val->trn_type == "Semiannually")
				{
					$bilr_paycycle = "ಅರ್ಧ ವಾರ್ಷಿಕ";
				}
				else if($trn_val->trn_type == "Yearly")
				{
					$bilr_paycycle = "ವಾರ್ಷಿಕ";
				}
				else if($trn_val->trn_type == "One time")
				{
					$bilr_paycycle = "ಒಂದು ಬಾರಿ";
				}
if($trn_val->trn_type == "Daily" || $trn_val->trn_type == "Weekly" || $trn_val->trn_type == "Monthly" ||  $trn_val->trn_type == "Quarterly" ||  $trn_val->trn_type == "Semiannually" ||  $trn_val->trn_type == "Yearly" ||  $trn_val->trn_type == "One time" || $trn_val->trn_type == "Payment" )
{
	if($trn_val->trn_type == "Daily" || $trn_val->trn_type == "Weekly" || $trn_val->trn_type == "Monthly" ||  $trn_val->trn_type == "Quarterly" ||  $trn_val->trn_type == "Semiannually" ||  $trn_val->trn_type == "Yearly" ||  $trn_val->trn_type == "One time")
	{
		$transrecord = $transrecord .  "<tr style='color: #2b2121;'>";
		$transrecord = $transrecord .  "<td>" . $trn_val->trn_id . "</td>";
	}
	if($trn_val->trn_type == "Payment")
	{
		$transrecord = $transrecord .  "<tr style='color: green;'>";
		$transrecord = $transrecord .  "<th>" . $trn_val->trn_billno . "</th>";
	}	
		$transrecord = $transrecord .  "<td>";
		if($trn_val->trn_type == "Payment")
		{
			$transrecord = $transrecord .   $trn_val->trn_type;
		}
		else
		{
			$transrecord = $transrecord .  $trn_val->ftyp_fundtype . " (" . $bilr_paycycle .")";
		}
	$transrecord = $transrecord .  "</td><td>" . date("d-m-Y",strtotime($trn_val->trn_date))  . "</td>";
	$transrecord = $transrecord .  "<td>" . $trn_val->trn_note . "</td>";	
	if($trn_val->trn_type == "Daily" || $trn_val->trn_type == "Weekly" || $trn_val->trn_type == "Monthly" ||  $trn_val->trn_type == "Quarterly" ||  $trn_val->trn_type == "Semiannually" ||  $trn_val->trn_type == "Yearly" ||  $trn_val->trn_type == "One time")
	{
		$payable = $payable + $trn_val->billamt;
		$totamt = $totamt + $trn_val->billamt;
		$transrecord = $transrecord .  "<th style='text-align: right;'>"  . $trn_val->billamt  ."</th>";
	}
	else
	{
		$transrecord = $transrecord .  "<th style='text-align: right;'></th>";
	}
	if($trn_val->trn_type == "Payment")
	{
		$totamt = $totamt - $trn_val->billamt;
		$paid = $paid + $trn_val->billamt;
		$transrecord = $transrecord .  "<th style='text-align: right;'>"  . $trn_val->billamt  ."</th>";
	}
	else
	{
		$transrecord = $transrecord .  "<th style='text-align: right;'></th>";
	}
	$transrecord = $transrecord .  "<th style='text-align: right;'>"  . $totamt  ."</th>";
	/*
	$transrecord = $transrecord .  "<td style='text-align: right;'>";
	if($trn_val->trn_type == "Payment")
	{
		$transrecord = $transrecord . "<button class='btn btn-default btn-xs'  type='button' onclick='BillPrint(" . $trn_val->trn_id .  ")' title='View Bill'><i class='fa fa-eye' aria-hidden='true'></i></button>";
	}
	else
	{
		$transrecord = $transrecord . "<button class='btn btn-danger btn-xs'  type='button' onclick='ViewInvoice(" . $trn_val->trn_id .  ")' title='View Invoice'><i class='fa fa-eye' aria-hidden='true'></i></button>";
	}
	$transrecord = $transrecord . "</td>";
	*/
	$transrecord = $transrecord .  "</tr>";
}
			}	
			$transrecord = $transrecord. "</tbody>";		 
			$transrecord = $transrecord. "<tfoot><tr><th style='width: 75px;'></th><th style='width: 100px;'></th><th style='width: 100px;'></th><th style='text-align: right;'>Total :</th><th style='width: 100px;text-align: right;'>$payable</th><th style='width: 100px;text-align: right;'>$paid</th><th style='width: 100px;text-align: right;'>$totamt</th></tr></tfoot>";			
			$transrecord = $transrecord. "</table>";
			//$transrecord = $transrecord. "<center><a class='form-control' style='width: 100px;' target='_blank' href='loadprinttrans?cst_id=$cst_id' >Print</a></center><hr>";
			$data['cstrpt']=$transrecord;
			//####################REPORT ENDS HERE			
			$data['totamt']=$totamt;
			$data['dp']=$this->JamathMod->LoadDP($cst_id);
			if ($data['dp'] === null)
			{
				$data['dpname'] = "img/dpimg.jpg";
			}
			else
			{
				$data['dpname']	= $data['dp']->doc_docfile;	
			}			
			return response()->json($data);
		}
	}
	//Print Customer Record ends here
	//###MOVE Advance BALANCE to clear dues starts here
	public function moveAdvBalCtrl(Request $request) 
	{
		$data['cst_id'] =$request->input('cst_id');
		$bil_billrec = $this->JamathMod->CheckCustomerBal($data['cst_id']);
		$data['totalamt'] = $bil_billrec[0]->totalamt;
		$data['paidamt'] = $bil_billrec[0]->paidamt;
		$data['balamt'] = $data['totalamt'] -  $data['paidamt'];		
		$data['advamt'] = $bil_billrec[0]->advamt;
		if($data['balamt'] >0)
		{
			if($data['advamt'] > 0)
			{
				$data['move_adv_bal'] = $this->JamathMod->moveAdvBalMod($bil_billrec);
			}
		}
		echo print_r($data);
	}
	//###MOVE Advance BALANCE to clear dues starts here
	//### Add Family Member detail starts here
	public function AddFamilyMemberCtrl(Request $request)
	{
		$data['cst_id'] = $request->input('cst_id'); 
		$data['cst_family_relationship'] = $request->input('cst_family_relationship'); 
		$data['cst_family_name'] = $request->input('cst_family_name');
		$data['cst_family_gender'] = $request->input('cst_family_gender'); 
		$data['cst_family_maritalstatus'] = $request->input('cst_family_maritalstatus');
		$data['cst_family_qualification'] = $request->input('cst_family_qualification');
		$data['cst_family_dob'] = $request->input('cst_family_dob'); 
		$data['cst_family_age'] = $request->input('cst_family_age'); 
		$data['cst_family_bloodgroup'] = $request->input('cst_family_bloodgroup');
		$data['cst_family_mobileno'] = $request->input('cst_family_mobileno');
		$data['cst_family_note'] = $request->input('cst_family_note');
		$data['family_cst_id'] = $request->input('family_cst_id');
		$data['family_status'] = $request->input('family_status');
		$data['family_record'] = $this->JamathMod->insertFamilyMember($data);
		$data['view_family_record'] = $this->JamathMod->ViewFamilyMember($data);		
			$transrecord = "";
			$transrecord = "<table  class='table table-striped table-bordered' id='tabloan'><thead><tr><th>" .  __('mylanguage.slno')  . "</th><th>" .  __('mylanguage.relationship')  ."</th><th>" .  __('mylanguage.name')  ."</th><th>" .  __('mylanguage.gender')  ."</th><th>" .  __('mylanguage.dob')  . "</th><th>" .   __('mylanguage.marital_status')  . "</th><th>" .  __('mylanguage.qualification')   . "</th><th>" .  __('mylanguage.Mobno') . "</th><th style='width: 100px;'>" .  __('mylanguage.action')  ."</th></tr>	</thead><tbody>";
			$slno =1;
		foreach($data['view_family_record'] as $view_family_record)
		{
			$contact = unserialize($view_family_record->cst_resaddress);
			$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $view_family_record->cst_relationship  . "</td><td style='text-align: left;'>" . $view_family_record->cst_name  . "</td><td style='text-align: left;'>" . $view_family_record->cst_gender  . "</td><td style='text-align: left;'>" . date("d-m-Y",strtotime($view_family_record->cst_dob))  . "</td><td>" . $view_family_record->cst_maritalstatus  . "</td><td>" . $view_family_record->cst_qualification  . "</td><td style='text-align: left;'>" . $contact[0] . "</td><td style='text-align: right;'><button type='button' class='btn btn-default btn-xs edit'><i class='fa fa-pencil-square-o' aria-hidden='true' onclick='funeditfamilymembers($view_family_record->cst_id)'></i></button> <button type='button'  class='btn btn-default btn-xs print' ><i class='fa fa-trash' aria-hidden='true' onclick='DeleteFamilyMembers($view_family_record->cst_id)'></i></button></td></tr>";
			$slno++;
		}
			$transrecord = $transrecord. "</tbody></table>";
		$data['transrecord']=$transrecord;
		return response()->json($data);
	}
	//### Add Family Member detail Ends here
	//Edit Family Member  record starts here
	public function EditFamilyMemberCtrl(Request $request)
	{
		$id = $request->customerid;
		$sqlstatement = DB::table('cst_customer')->where(['cst_id'=>$id])->get();
		$resaddress=unserialize($sqlstatement[0]->cst_resaddress);
		return response()->json(['sqlstatement' =>$sqlstatement,'resaddress' => $resaddress,'contactno'=>$resaddress[0]]);
	}
	//Edit Family Member  record ends here
	//Delete Family Member record starts here
	public function delfamilymemberCtrl(Request $request)
	{
		$id = $request->customerid;
		$data['cst_id'] = $request->cst_id; 
		$sqlstatement = DB::table('cst_customer')->where('cst_id','=',$id)->delete();
		$data['view_family_record'] = $this->JamathMod->ViewFamilyMember($data);
			$transrecord = "";
			$transrecord = "<table  class='table table-striped table-bordered' id='tabloan'><thead><tr><th>" .  __('mylanguage.slno')  . "</th><th>" .  __('mylanguage.relationship')  ."</th><th>" .  __('mylanguage.name')  ."</th><th>" .  __('mylanguage.gender')  ."</th><th>" .  __('mylanguage.dob')  . "</th><th>" .   __('mylanguage.marital_status')  . "</th><th>" .  __('mylanguage.qualification')   . "</th><th>" .  __('mylanguage.Mobno') . "</th><th style='width: 100px;'>" .  __('mylanguage.action')  ."</th></tr>	</thead><tbody>";
			$slno =1;
		foreach($data['view_family_record'] as $view_family_record)
		{
			$contact = unserialize($view_family_record->cst_resaddress);
			$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $view_family_record->cst_relationship  . "</td><td style='text-align: left;'>" . $view_family_record->cst_name  . "</td><td style='text-align: left;'>" . $view_family_record->cst_gender  . "</td><td style='text-align: left;'>" . date("d-m-Y",strtotime($view_family_record->cst_dob))  . "</td><td>" . $view_family_record->cst_maritalstatus  . "</td><td>" . $view_family_record->cst_qualification  . "</td><td style='text-align: left;'>" . $contact[0] . "</td><td style='text-align: right;'><button type='button' class='btn btn-default btn-xs edit'  onclick='funeditfamilymembers($view_family_record->cst_id)'><i class='fa fa-pencil-square-o' aria-hidden='true'></i></button> <button type='button' class='btn btn-default btn-xs print'   onclick='DeleteFamilyMembers($view_family_record->cst_id)'><i class='fa fa-trash' aria-hidden='true'></i></button></td></tr>";
			$slno++;
		}
			$transrecord = $transrecord. "</tbody></table>";
			$data['transrecord']=$transrecord;
		return response()->json($data);
	}
	//Delete Family Member record ends here
	//### Add Member docs starts here
	public function uploadmemberdocsCtrl(Request $request)
	{
		$data['cst_id'] = $request->input('cst_id'); 
		$data['document_cst_id'] = $request->input('document_cst_id'); 
		$data['doc_doctype'] = $request->input('doc_doctype');
		$data['doc_docfile'] = $request->input('doc_docfile'); 
		$data['doc_docno'] = $request->input('doc_docno');
		$data['doc_detail'] = $request->input('doc_detail');
		$data['doc_module'] = $request->input('doc_module');
		if($data['cst_id'] == 0)
		{
			$data['doc_status'] = "Pending";
		}
		else
		{
			$data['doc_status'] = "Active";
		}		
		//Upload Image starts here
		$filenm="";
		$filename="";
		if ($request->hasFile('doc_docfile')) 
		{
			$filenm = $request->file('doc_docfile');
			$filename = time().'.'.$filenm->getClientOriginalExtension();
			$destinationPath = public_path('/uploadmember');
			$filenm->move($destinationPath, $filename);
		}
		$data['docfilename'] = 'uploadmember/' . $filename;
		//Upload Image ends here
		$data['insert_record'] = $this->JamathMod->insMemberDocsMod($data);
		$data['view_member_docs'] = $this->JamathMod->ViewMemberDocs($data);		
			$transrecord = "";
			$transrecord = "<table  class='table table-striped table-bordered' id='tabloan'><thead><tr><th>" .  __('mylanguage.slno')  . "</th><th>" .  __('mylanguage.document_type')  ."</th><th>" .  __('mylanguage.idnocode')  ."</th><th>" .  __('mylanguage.document_file')  ."</th><th>" .  __('mylanguage.more_detail')  . "</th><th style='width: 100px;'>" .  __('mylanguage.action')  ."</th></tr>	</thead><tbody>";
			$slno =1;
		foreach($data['view_member_docs'] as $view_member_docs)
		{
			if(\File::extension($view_member_docs->doc_docfile) == "")
			{
				$filename = "img/defaultimg.png";
			}
			else
			{
				$filename = $this->JamathMod->fileexists($view_member_docs->doc_docfile);
			}
			$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $view_member_docs->doc_doctype  . "</td><td style='text-align: left;'>" . $view_member_docs->doc_docno  . "</td><td style='text-align: left;'>";
			if($filename == "img/defaultimg.png")
			{
				$transrecord = $transrecord .  "";
			}
			else
			{
				$transrecord = $transrecord .  "<a href='" . $view_member_docs->doc_docfile . "' download>Download</a>";
			}
			$transrecord = $transrecord .  "</td><td>" . $view_member_docs->doc_detail  . "</td><td><button type='button'  class='btn btn-default btn-xs print' ><i class='fa fa-trash' aria-hidden='true' onclick='DeleteMemberDocuments($view_member_docs->doc_id)'></i></button></td></tr>";
			$slno++;
		}
			$transrecord = $transrecord. "</tbody></table>";
		$data['transrecord']=$transrecord;
		return response()->json($data);
	}
	//### Add Member docs ends here
	public function loadprintcrdrCtrl(Request $request)
	{
			$cst_id= $request->cst_id;
			//#####################	BILLING TABLE STARTS HERE			
		$data['cst_customer']=$this->JamathMod->LoadSingleCustMod($cst_id);
		$data['cst_name'] = $data['cst_customer']->cst_name;		
		$data['cst_code'] = $data['cst_customer']->cst_code;
		$data['grd_id'] =  $data['cst_customer']->grd_id;
		$data['cst_grade']=$this->JamathMod->LoadCustGrade($data['grd_id']);
		$data['cst_grade_prefix']=$data['cst_grade']->grd_prefix;			
			$bil_billrec = $this->JamathMod->CheckCustomerBal($cst_id);			
			$data['totalamt'] = $bil_billrec[0]->totalamt;
			$data['paidamt'] = $bil_billrec[0]->paidamt;
			$data['balamt'] = ($data['paidamt'] + $bil_billrec[0]->advamt + $bil_billrec[0]->prepayamt ) - $data['totalamt'];		
			$data['advamt'] = $bil_billrec[0]->advamt;
			$data['prepayamt'] = $bil_billrec[0]->prepayamt;
			if($data['balamt'] < 0 )
			{
				$data['balamt'] = 0;
			}
			$trn_transaction = $this->JamathMod->TotPaidBalMod($cst_id);
			$slno=1;
			$bal = 0;
			$totamt = 0; $paidamt=0; $balamt=0; $advbal=0;
			$transrecord="";
			$transrecord = "<table  class='table table-sorting table-striped table-hover table-bordered datatable dataTable no-footer' id='tblipb'><thead><tr><th>" . __('mylanguage.slno') ."</th><th>" . __('mylanguage.type') ."</th><th>" . __('mylanguage.Payment_cycle') ."</th><th style='width: 100px;text-align: right;''>" . __('mylanguage.total_bal_amt') ."</th><th style='width: 100px;text-align: right;'>" . __('mylanguage.amt_payable') ."</th><th style='width: 100px;text-align: right;'> " . __('mylanguage.balance_amount') ."</th></tr></thead><tbody>";
			foreach($trn_transaction as $trn_val)
			{
				$bal = $trn_val->totalamt - $trn_val->paidamt;
				$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $trn_val->ftype  . "</td><td>" . $trn_val->paycycle . "</td><th style='text-align: right;'>$trn_val->totalamt</th><th style='text-align: right;'>$trn_val->paidamt</th><th style='text-align: right;'>$bal</th></tr>";
				$slno++;
				$totamt = $totamt + $trn_val->totalamt; 
				$paidamt= $paidamt + $trn_val->paidamt; 
				$balamt=$balamt + $bal;
			}
			$advbal= $paidamt - $totamt;
			if($advbal <0)
			{
				$advbal=0;
			}
			$transrecord = $transrecord. "</tbody><tfoot><tr><th></th><th></th><th>". __('mylanguage.grand_total') ."</th><th style='text-align: right;'>$totamt</th><th style='text-align: right;'>$paidamt</th><th style='text-align: right;'>$balamt</th></tr>";
			$transrecord = $transrecord. "<tr><th></th><th></th><th>". __('mylanguage.adv_amt') ."</th><th style='text-align: right;'></th><th style='text-align: right;'>"  . $data['balamt']  . "</th><th style='text-align: right;'></th></tr>";
			$transrecord = $transrecord. "</tfoot></table>";
			$data['crdrrecord']=$transrecord;
			//#####################	BILLING TABLE ENDS HERE
		return view('Mosque.printcrdr',compact('data'));
	}
	public function loadprinttransCtrl(Request $request)
	{
			$cst_id= $request->cst_id;
			//#####################	BILLING TABLE STARTS HERE			
		$data['cst_customer']=$this->JamathMod->LoadSingleCustMod($cst_id);
		$data['cst_name'] = $data['cst_customer']->cst_name;		
		$data['cst_code'] = $data['cst_customer']->cst_code;
		$data['grd_id'] =  $data['cst_customer']->grd_id;
		$data['cst_grade']=$this->JamathMod->LoadCustGrade($data['grd_id']);
		$data['cst_grade_prefix']=$data['cst_grade']->grd_prefix;			
			$bil_billrec = $this->JamathMod->CheckCustomerBal($cst_id);			
			$data['totalamt'] = $bil_billrec[0]->totalamt;
			$data['paidamt'] = $bil_billrec[0]->paidamt;
			$data['balamt'] = ($data['paidamt'] + $bil_billrec[0]->advamt + $bil_billrec[0]->prepayamt ) - $data['totalamt'];		
			$data['advamt'] = $bil_billrec[0]->advamt;
			$data['prepayamt'] = $bil_billrec[0]->prepayamt;
			if($data['balamt'] < 0 )
			{
				$data['balamt'] = 0;
			}
//####################REPORT STARTS HERE
			$trn_transaction = $this->JamathMod->TransactionReport($cst_id);
			$transrecord="";
			$transrecord = "<table id='datatable-data-export' class='table table-sorting table-striped table-hover table-bordered datatable' id='tblipb'><thead><tr><th style='width: 75px;'>" . __('report.slno') ."</th><th style='width: 175px;'>" . __('report.transaction_report') ."</th><th style='width: 100px;'>" . __('report.transaction_date') ."</th><th>" . __('report.transaction_particular') ."</th><th style='width: 100px;text-align: right;'>" . __('report.transaction_bill') ."</th><th style='width: 100px;text-align: right;'> " . __('report.transaction_payment') ."</th><th style='width: 100px;text-align: right;'> " . __('report.transaction_sum') ."</th></tr></thead><tbody>";
			$payable = 0;
			$paid = 0;
			$totamt = 0;
			$bilr_paycycle="";
			foreach($trn_transaction as $trn_val)
			{
				if($trn_val->trn_type == "Daily")
				{
					$bilr_paycycle = "ಪ್ರತಿದಿನ";
				}
				else if($trn_val->trn_type == "Weekly")
				{
					$bilr_paycycle = "ವಾರಕ್ಕೊಮ್ಮೆ";
				}
				else if($trn_val->trn_type == "Monthly")
				{
					$bilr_paycycle = "ಮಾಸಿಕ";
				}
				else if($trn_val->trn_type == "Quarterly")
				{
					$bilr_paycycle = "ಮೂರು ತಿಂಗಳಿಗೊಮ್ಮೆ";
				}
				else if($trn_val->trn_type == "Semiannually")
				{
					$bilr_paycycle = "ಅರ್ಧ ವಾರ್ಷಿಕ";
				}
				else if($trn_val->trn_type == "Yearly")
				{
					$bilr_paycycle = "ವಾರ್ಷಿಕ";
				}
				else if($trn_val->trn_type == "One time")
				{
					$bilr_paycycle = "ಒಂದು ಬಾರಿ";
				}
if($trn_val->trn_type == "Daily" || $trn_val->trn_type == "Weekly" || $trn_val->trn_type == "Monthly" ||  $trn_val->trn_type == "Quarterly" ||  $trn_val->trn_type == "Semiannually" ||  $trn_val->trn_type == "Yearly" ||  $trn_val->trn_type == "One time" || $trn_val->trn_type == "Payment" )
{
	if($trn_val->trn_type == "Daily" || $trn_val->trn_type == "Weekly" || $trn_val->trn_type == "Monthly" ||  $trn_val->trn_type == "Quarterly" ||  $trn_val->trn_type == "Semiannually" ||  $trn_val->trn_type == "Yearly" ||  $trn_val->trn_type == "One time")
	{
		$transrecord = $transrecord .  "<tr style='color: #2b2121;'>";
		$transrecord = $transrecord .  "<td>" . $trn_val->trn_id . "</td>";
	}
	if($trn_val->trn_type == "Payment")
	{
		$transrecord = $transrecord .  "<tr style='color: green;'>";
		$transrecord = $transrecord .  "<th>" . $trn_val->trn_billno . "</th>";
	}
	$transrecord = $transrecord .  "<td>";
		if($trn_val->trn_type == "Payment")
		{
			$transrecord = $transrecord .   $trn_val->trn_type;
		}
		else
		{
			//$transrecord = $transrecord .  $trn_val->ftyp_fundtype . " (" . $bilr_paycycle .")";
			$transrecord = $transrecord .  $trn_val->ftyp_fundtype ;
		}
	$transrecord = $transrecord .  "</td><td>" . date("d-m-Y",strtotime($trn_val->trn_date))  . "</td>";
	$transrecord = $transrecord .  "<td>" . $trn_val->trn_note . "</td>";	
	if($trn_val->trn_type == "Daily" || $trn_val->trn_type == "Weekly" || $trn_val->trn_type == "Monthly" ||  $trn_val->trn_type == "Quarterly" ||  $trn_val->trn_type == "Semiannually" ||  $trn_val->trn_type == "Yearly" ||  $trn_val->trn_type == "One time")
	{
		$payable = $payable + $trn_val->billamt;
		$totamt = $totamt + $trn_val->billamt;
		$transrecord = $transrecord .  "<th style='text-align: right;'>"  . $trn_val->billamt  ."</th>";
	}
	else
	{
		$transrecord = $transrecord .  "<th style='text-align: right;'></th>";
	}
	if($trn_val->trn_type == "Payment")
	{
		$totamt = $totamt - $trn_val->billamt;
		$paid = $paid + $trn_val->billamt;
		$transrecord = $transrecord .  "<th style='text-align: right;'>"  . $trn_val->billamt  ."</th>";
	}
	else
	{
		$transrecord = $transrecord .  "<th style='text-align: right;'></th>";
	}
	$transrecord = $transrecord .  "<th style='text-align: right;'>"  . $totamt  ."</th>";
	$transrecord = $transrecord .  "</tr>";
}
			}
			//$transrecord = $transrecord. "</tbody>";		 
			$transrecord = $transrecord. "<tr><th style='width: 75px;'></th><th style='width: 100px;'></th><th style='width: 100px;'></th><th style='text-align: right;'>Total :</th><th style='width: 100px;text-align: right;'>$payable</th><th style='width: 100px;text-align: right;'>$paid</th><th style='width: 100px;text-align: right;'>$totamt</th></tr></tbody>";			
			$transrecord = $transrecord. "</table>";
			$data['cstrpt']=$transrecord;
			//####################REPORT ENDS HERE	
		return view('Mosque.printtransaction',compact('data'));
	}
	//Delete payment record starts here
	public function delpayctrl(Request $request)
	{
		$transaction_ids = $request->transaction_ids;
		$deltrn_transaction=DB::table('trn_transaction')->where('trn_id','=',$transaction_ids)->update(['trn_status'=>'Deleted']);
		$delbil_billrec=DB::table('bil_billrec')->where('trn_id','=',$transaction_ids)->update(['bil_status'=>'Deleted']);
		return response()->json(['rec' =>'Deleted']);
	}
	//Delete payment record ends here
	//Delete billing record starts here
	public function delbillingrecordctrl(Request $request)
	{
		$transaction_ids = $request->transaction_ids;
		$trn_ida = $request->trn_ida;
		$deltrn_transaction=DB::table('bil_billrec')->where('bil_id','=',$transaction_ids)->update(['bil_status'=>'Deleted']);
		$deltrn_transaction=DB::table('trn_transaction')->where('trn_id','=',$trn_ida)->update(['trn_status'=>'Deleted']);
		return response()->json(['rec' =>'Deleted']);
	}
	//Delete billing record ends here
	public function uploaddpCtrl(Request $request)
	{
		//##############CODING TO UPLOAD IMAGE STARTS HERE#############
		$validator = Validator::make($request->all(),
			[
				'file' => 'image',
			],
			[
				'file.image' => 'The file must be an image (jpeg, png, bmp, gif, or svg)'
			]);
		if ($validator->fails())
			return array(
				'fail' => true,
				'errors' => $validator->errors()
			);
		$extension = $request->file('dp')->getClientOriginalExtension();
		$dir = 'dox/';
		$filename = uniqid() . '_' . time() . '.' . $extension;
		$request->file('dp')->move($dir, $filename);
		$data['docfilename'] ='dox/'.$filename;
		$data['cst_id'] = $request->cst_id;
		$data['insert_record'] = $this->JamathMod->insMemberDP($data);//Image insert code
		return $filename;
	}
	public function delmemberdocsCtrl(Request $request)
	{
		$doc_id = $request->doc_id;
		$deltrn_transaction=DB::table('doc_document')->where('doc_id','=',$doc_id)->update(['doc_status'=>'Deleted']);
		$data['cst_id'] = $request->cst_id;
		$data['view_member_docs'] = $this->JamathMod->ViewMemberDocs($data);		
			$transrecord = "";
			$transrecord = "<table  class='table table-striped table-bordered' id='tabloan'><thead><tr><th>" .  __('mylanguage.slno')  . "</th><th>" .  __('mylanguage.document_type')  ."</th><th>" .  __('mylanguage.idnocode')  ."</th><th>" .  __('mylanguage.document_file')  ."</th><th>" .  __('mylanguage.more_detail')  . "</th><th style='width: 100px;'>" .  __('mylanguage.action')  ."</th></tr>	</thead><tbody>";
			$slno =1;
		foreach($data['view_member_docs'] as $view_member_docs)
		{
			if(\File::extension($view_member_docs->doc_docfile) == "")
			{
				$filename = "img/defaultimg.png";
			}
			else
			{
				$filename = $this->JamathMod->fileexists($view_member_docs->doc_docfile);
			}
			$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $view_member_docs->doc_doctype  . "</td><td style='text-align: left;'>" . $view_member_docs->doc_docno  . "</td><td style='text-align: left;'>";
			if($filename == "img/defaultimg.png")
			{
				$transrecord = $transrecord .  "";
			}
			else
			{
				$transrecord = $transrecord .  "<a href='" . $view_member_docs->doc_docfile . "' download>Download</a>";
			}
			$transrecord = $transrecord .  "</td><td>" . $view_member_docs->doc_detail  . "</td><td><button type='button'  class='btn btn-default btn-xs print' ><i class='fa fa-trash' aria-hidden='true' onclick='DeleteMemberDocuments($view_member_docs->doc_id)'></i></button></td></tr>";
			$slno++;
		}
			$transrecord = $transrecord. "</tbody></table>";
		$data['transrecord']=$transrecord;
		return response()->json($data);
	}
	public function LoadjamathxlsPmt(Request $req)
	{
		$data['rectype'] = $req->input('rectype');
		$data['fiscalyear'] = DB::table('fyr_fiscalyear')->whereRaw('? between fyr_startdate and fyr_enddate', [date('Y-m-d')])->where([["fyr_status","=",'Active']])->get();
		$status= 'Deleted';
		$data['fundtype']=DB::table('ftyp_fundtype')->where([["ftyp_module","Jamath"]])->where([["ftyp_status","<>",$status]])->orderBy('ftyp_id')->get();
		$data['LoadJamathRecords']=$this->JamathMod->viewxlsjamathapplicant($data);
		return view('Mosque.viewjamathxlsPmt',compact('data'));
	}
	public function LoadjamathxlsPmtPrint(Request $req)
	{
		$data['rectype'] = $req->input('rectype');
		$data['fiscalyear'] = DB::table('fyr_fiscalyear')->whereRaw('? between fyr_startdate and fyr_enddate', [date('Y-m-d')])->where([["fyr_status","=",'Active']])->get();
		$status= 'Deleted';
		$data['fundtype']=DB::table('ftyp_fundtype')->where([["ftyp_module","Jamath"]])->where([["ftyp_status","<>",$status]])->orderBy('ftyp_id')->get();
		$data['LoadJamathRecords']=$this->JamathMod->viewxlsjamathapplicant($data);
		return view('Mosque.viewjamathxlsPmtPrint',compact('data'));
	}
}