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

class IncExpReportController extends Controller
{
	public function __construct()
	{
		$this->IncExpRptMod= new IncExpReportModel;
	}
	public function loadTransRPTCtrl(Request $request)
	{
		$membercode = (int) filter_var($request->input('member_id'), FILTER_SANITIZE_NUMBER_INT);
		if($membercode == 0)
		{
			$data['cst_id']  = 0;
		}
		else
		{
			if($membercode <0)
			{
				$membercode = -1 * $membercode;
			}
			$cst_customer=$this->IncExpRptMod->LoadCstRecMod($membercode);
			$data['cst_id']  = $cst_customer->cst_id;
		}
		$data['submit']  = $request->input('submit');
		$data['member_id']  = $request->input('member_id');
		$data['grd_id']  = $request->input('grd_id');
		$data['ftyp_id']  = $request->input('ftyp_id');
		$data['grfn_paycycle']  = $request->input('grfn_paycycle');
		$data['fdate']  = $request->input('fdate');
		$data['tdate']  = $request->input('tdate');
		$data['trn_transaction'] = $this->IncExpRptMod->TransactionReport($data);
		return view('Mosque.viewincomeexpense',compact('data'));
	}
	public function printTransRPTCtrl(Request $request)
	{
		$membercode = (int) filter_var($request->input('member_id'), FILTER_SANITIZE_NUMBER_INT);
		if($membercode == 0)
		{
			$data['cst_id']  = 0;
		}
		else
		{
			if($membercode <0)
			{
				$membercode = -1 * $membercode;
			}
			$cst_customer=$this->IncExpRptMod->LoadCstRecMod($membercode);
			$data['cst_id']  = $cst_customer->cst_id;
		}
		$data['member_id']  = $request->input('member_id');
		$data['grd_id']  = $request->input('grd_id');
		$data['ftyp_id']  = $request->input('ftyp_id');
		$data['grfn_paycycle']  = $request->input('grfn_paycycle');
		$data['fdate']  = $request->input('fdate');
		$data['tdate']  = $request->input('tdate');
		$data['btnsubmitall']  = $request->input('btnsubmitall');
		$data['trn_transaction'] = $this->IncExpRptMod->TransactionReport($data);
		return view('Mosque.printincomeexpensereport',compact('data'));
	}
	public function fundtypetransreportCtrl(Request $request)
	{
		$membercode = (int) filter_var($request->input('member_id'), FILTER_SANITIZE_NUMBER_INT);
		if($membercode == 0)
		{
			$data['cst_id']  = 0;
		}
		else
		{
			if($membercode <0)
			{
				$membercode = -1 * $membercode;
			}
			$cst_customer=$this->IncExpRptMod->LoadCstRecMod($membercode);
			$data['cst_id']  = $cst_customer->cst_id;
		}

		$data['submit']  = $request->input('submit');
		$data['member_id']  = $request->input('member_id');
		$data['grd_id']  = $request->input('grd_id');
		$data['ftyp_id']  = $request->input('ftyp_id');
		$data['grfn_paycycle']  = $request->input('grfn_paycycle');
		$data['fdate']  = $request->input('fdate');
		$data['tdate']  = $request->input('tdate');
		
		//#####################	BILLING TABLE STARTS HERE
		$trn_transaction = $this->IncExpRptMod->fundtypewiserpt($data);
		$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: 50px;'>" . __('mylanguage.slno') ."</th><th>" . __('fundtype.fundtype') ."</th><th style='width: 100px;text-align: right;'>" . __('mylanguage.Total_amount') ."</th><th style='width: 100px;text-align: right;'>" . __('mylanguage.Paid_amount') ."</th><th style='width: 100px; text-align: right;'> " . __('mylanguage.balance_amount') ."</th></tr></thead><tbody>";
			$totamt = 0;
			$paidamt = 0;
		foreach($trn_transaction as $trn_val)
		{
			$bal = $trn_val->totamt - $trn_val->paidamt;
			$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $trn_val->ftyp_fundtype  . "</td><td style='width: 100px;text-align: right;'>" . $trn_val->totamt . "</td><td style='width: 100px;text-align: right;'>" . $trn_val->paidamt . "</td><td style='width: 100px;text-align: right;'>" . $bal . "</td></tr>";
			$totamt = $totamt + $trn_val->totamt;
			$paidamt = $paidamt + $trn_val->paidamt;
			$slno++;
		}
			$bal = $totamt - $paidamt;
			$transrecord = $transrecord. "</tbody><tfoot><tr><th></th><th>Total </th><th style='width: 100px;text-align: right;'>" . $totamt . "</th><th style='width: 100px;text-align: right;'>" . $paidamt . "</th><th style='width: 100px;text-align: right;'>" . $bal . "</th></tr></table>";
		//#####################	BILLING TABLE ENDS HERE
		$data['fundtypwiserpt'] = $transrecord;
		return view('Mosque.fundtypetransreport',compact('data'));
	}
	public function ftypetransRPTCtrl(Request $request)
	{
				$membercode = (int) filter_var($request->input('member_id'), FILTER_SANITIZE_NUMBER_INT);
		if($membercode == 0)
		{
			$data['cst_id']  = 0;
		}
		else
		{
			if($membercode <0)
			{
				$membercode = -1 * $membercode;
			}
			$cst_customer=$this->IncExpRptMod->LoadCstRecMod($membercode);
			$data['cst_id']  = $cst_customer->cst_id;
		}
		$data['member_id']  = $request->input('member_id');
		$data['grd_id']  = $request->input('grd_id');
		$data['ftyp_id']  = $request->input('ftyp_id');
		$data['grfn_paycycle']  = $request->input('grfn_paycycle');
		$data['fdate']  = $request->input('fdate');
		$data['tdate']  = $request->input('tdate');
		
		//#####################	BILLING TABLE STARTS HERE
		$trn_transaction = $this->IncExpRptMod->fundtypewiserpt($data);
		$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: 50px;'>" . __('mylanguage.slno') ."</th><th>" . __('fundtype.fundtype') ."</th><th style='width: 100px;text-align: right;'>" . __('mylanguage.Total_amount') ."</th><th style='width: 100px;text-align: right;'>" . __('mylanguage.Paid_amount') ."</th><th style='width: 100px; text-align: right;'> " . __('mylanguage.balance_amount') ."</th></tr></thead><tbody>";
			$totamt = 0;
			$paidamt = 0;
		foreach($trn_transaction as $trn_val)
		{
			$bal = $trn_val->totamt - $trn_val->paidamt;
			$transrecord = $transrecord .  "<tr><td>" . $slno . "</td><td>" . $trn_val->ftyp_fundtype  . "</td><td style='width: 100px;text-align: right;'>" . $trn_val->totamt . "</td><td style='width: 100px;text-align: right;'>" . $trn_val->paidamt . "</td><td style='width: 100px;text-align: right;'>" . $bal . "</td></tr>";
			$totamt = $totamt + $trn_val->totamt;
			$paidamt = $paidamt + $trn_val->paidamt;
			$slno++;
		}
			$bal = $totamt - $paidamt;
			$transrecord = $transrecord. "</tbody><tfoot><tr><th></th><th>Total </th><th style='width: 100px;text-align: right;'>" . $totamt . "</th><th style='width: 100px;text-align: right;'>" . $paidamt . "</th><th style='width: 100px;text-align: right;'>" . $bal . "</th></tr></table>";
		//#####################	BILLING TABLE ENDS HERE
		$data['fundtypwiserpt'] = $transrecord;
		return view('Mosque.printfundwisetransrpt',compact('data'));
	}	
	public function LoadIncomeExpense(Request $req)
    {
		$data['url']='viewincomeexpense?ftyp_module='.$req->input('ftyp_module');
		$data['fiscalyear'] = DB::table('fyr_fiscalyear')->whereRaw('? between fyr_startdate and fyr_enddate', [date('Y-m-d')])->where([["fyr_status","=",'Active']])->get();
		$Mid=$this->IncExpRptMod->QPermission($data);
		$data['delete']= $Mid->Delete;
		$data['edit']= $Mid->Edit;
		$data['create']= $Mid->create;
		$status= 'Deleted';
		$data['ftyp_module'] = $req->input('ftyp_module');
		$data['fundtype']=DB::select( DB::raw("select *,(SELECT ifnull(sum(bil_amt),0) FROM `bil_billrec` WHERE ftyp_id=ftyp_fundtype.ftyp_id  AND bil_status='Active' ) AS totalamt from `ftyp_fundtype` where (`ftyp_module` = '" . $data['ftyp_module'] . "') and (`ftyp_status` <> 'Deleted')"));
		return view('Mosque.viewincomeexpense',compact('data'));
    }
	public function loadincexpCtrl(Request $req)
	{
		$ftyp_id = $req->input('ftyp_id');
		$data['fundtypes']= $this->IncExpRptMod->loadfundtypesingle($ftyp_id);
		return response()->json($data);
	}
	public function InsertIncExpCtrl(Request $req)
	{
		$user = auth()->user();
		$Username =  $user->usr_id;
		$ftyp_id = $req->input('ftyp_id');
		$entry_date = $req->input('entry_date');
		$fyr_id = $req->input('fyr_id');
		$grfn_amt = $req->input('grfn_amt');
		$trn_type = $req->input('trn_type');
		$grfn_description = $req->input('grfn_description');
		//#!!-	Transaction Record Starts here--!!#
		$trinsid = DB::table('trn_transaction')->insertGetId(['cst_id'=>0,'fy_id'=>$fyr_id,'grd_id'=>0,'grfn_id'=>0,'ftyp_id'=>$ftyp_id,'trn_type'=>$trn_type,'trn_billno'=>0,'trn_amt'=>$grfn_amt,'trn_date'=>$entry_date,'trn_note'=>$grfn_description,'trn_paytype'=>'','usr_name'=>$Username,'trn_status'=>'Active']);
		//#!!-	Transaction Record Ends here--!!#
		$data = array('bil_type'=>$trn_type,'fy_id'=>$fyr_id,'grd_id'=>1,'cst_id'=>0,'ftyp_id'=>$ftyp_id,'grfn_id'=>0,'bil_amt'=>$grfn_amt,'bil_date'=>$entry_date,'bil_note'=>$grfn_description,'usr_name'=>$Username,'bil_status'=>'Active','trn_id'=>$trinsid);
		$insertdetails = DB::table('bil_billrec')->insert($data);
		$bil_billrec = $this->IncExpRptMod->CheckBal($ftyp_id,$trn_type);
		return response()->json(['status' => '1', 'message' => trans('Fund added successfully...'), 'bil_billrec' => $bil_billrec]);
	}
	public function LoadIncExpRecCtrl(Request $req)
	{
		$data['ftyp_id'] = $req->input('ftyp_id');
		$data['ftyp_module'] = $req->input('ftyp_module');
		$data['cst_id'] = 0;
		$data['btnsubmitall']  = "";//$request->input('submit');
		$data['grd_id']  = "";
		$data['grfn_paycycle']  ="";
		$data['fdate']  = "";//$request->input('fdate');
		$data['tdate']  = "";//$request->input('tdate');
		$data['trn_transaction'] = $this->IncExpRptMod->TransactionReport($data);
		return view('Mosque.viewincexprec',compact('data'));	
	}
	public function LoadIncExpRecsearchCtrl(Request $req)
	{
		$data['ftyp_id'] = $req->input('ftyp_id');
		$data['ftyp_module'] = $req->input('ftyp_module');
		$data['cst_id'] = 0;
		$data['btnsubmitall']  = $req->input('btnsubmitall');
		$data['grd_id']  = "";
		$data['grfn_paycycle']  ="";
		$data['fdate']  = $req->input('fdate');
		$data['tdate']  = $req->input('tdate');
		$data['trn_transaction'] = $this->IncExpRptMod->TransactionReport($data);
		return view('Mosque.viewincexprec',compact('data'));	
	}
	public function DelIncExpRecCtrl(Request $req)
	{
		$trn_id = $req->input('trn_id');
		$data = array('trn_status'=>'Deleted');
		$updatedetails = DB::table('trn_transaction')->where('trn_id',$trn_id)->update($data);
		$data = array('bil_status'=>'Deleted');
		$updatedetails = DB::table('bil_billrec')->where('trn_id',$trn_id)->update($data);
		//-----------------
		$data['ftyp_id'] = $req->input('ftyp_id');
		$data['ftyp_module'] = $req->input('ftyp_module');
		$data['cst_id'] = 0;
		$data['btnsubmitall']  = $req->input('btnsubmitall');
		$data['grd_id']  = "";
		$data['grfn_paycycle']  ="";
		$data['fdate']  = $req->input('fdate');
		$data['tdate']  = $req->input('tdate');
		$data['trn_transaction'] = $this->IncExpRptMod->TransactionReport($data);
		return view('Mosque.viewincexprec',compact('data'));	
	}
}