<?php
$user = auth()->user();
?>
<script src="assets/js/king-table.js"></script>
<div class="content">
	<div class="main-content">
		<!-- DATA EXPORT -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-table"></i> {{ __('mylanguage.jamath_list') }} - {{ $data['rectype'] }}</h3>
<div class='btn-toolbar pull-right'>
    <div class='btn-group'>
		<button type='button' class='btn btn-warning' id="idreload1" onclick="loadjamathrecords('All')" title="Matrimonial Certificate" ><i class="fa fa-american-sign-language-interpreting" aria-hidden="true"></i>All Accounts</button>
		<button type='button' class='btn btn-danger' id="idreload2" onclick="loadjamathrecords('Pending')" title="Matrimonial Certificate" ><i class="fa fa-thumbs-down" aria-hidden="true"></i>Pending Accounts</button>
		<button type='button' class='btn btn-success' id="idreload3" onclick="loadjamathrecords('Paid')" title="Matrimonial Certificate" ><i class="fa fa-thumbs-up" aria-hidden="true"></i>Paid Accounts</button>
    </div>
</div>
			</div>
			<div class="widget-content">			
<table id="mydatatable" class="table table-sorting table-striped table-hover table-bordered datatable">
	<thead>
		<tr>
			<th>{{ __('mylanguage.slno') }}</th>
			<th>{{ __('mylanguage.member_id') }}</th>
			<th>{{ __('mylanguage.name') }}</th>
			<th style="text-align: right;">{{ __('mylanguage.Total_amount') }}</th>
			@foreach($data['fundtype'] as $fundtype)
				<th>{{ $fundtype->ftyp_fundtype }}</th>
			@endforeach		
			<th style="text-align: right;">{{ __('mylanguage.Paid_amount') }}</th>
			<th style="text-align: right;">{{ __('mylanguage.balance_amount') }}</th>
		</tr>
	</thead>
	<tbody>
		<?php  $i=1;
		?>
		@foreach($data['LoadJamathRecords'] as $LoadJamathRec)
		<?php
		$resaddr = unserialize($LoadJamathRec->cst_resaddress);
		$permaddr = unserialize($LoadJamathRec->cst_permaddress);
		?>
		<tr>
			<td><?php echo $i; ?></td>
			<td>{{$LoadJamathRec->grd_prefix}}{{$LoadJamathRec->cst_code}}</a></td>
			<td>{{$LoadJamathRec->cst_name}} </td>	
			<td style="text-align: right;">{{$LoadJamathRec->totalamt}}</td>
			@foreach($data['fundtype'] as $fundtype)
				<td style="text-align: right;" ><?php 
				$val = "rec".$fundtype->ftyp_id;
				echo $LoadJamathRec->$val;
				?></td>
			@endforeach		
			<td style="text-align: right;" id="paid_{{$LoadJamathRec->cst_id}}">{{$LoadJamathRec->paidamt + $LoadJamathRec->advamt + $LoadJamathRec->prepayamt}}</td>
			<td style="text-align: right;" id="bal_{{$LoadJamathRec->cst_id}}">{{ ($LoadJamathRec->totalamt) - ($LoadJamathRec->paidamt  + $LoadJamathRec->advamt + $LoadJamathRec->prepayamt) }}</td>
		</tr>
		<?php
		$i= $i+1;
		?>
		@endforeach	
	</tbody>
	<?php
	/*
	<tfoot>
		<tr>
			<th></th>
			<th></th>
			<th>Total</th>
			<th style="text-align: right;" id="tbltotalamt"><?php echo $totalamt; ?></th>		
			<th style="text-align: right;" id="tblpaidamt"><?php echo $paidamt; ?></th>	
			<th style="text-align: right;" id="tblbalamt"><?php echo $totalamt - $paidamt; ?></th>		
			<th></th>					
		</tr>
	</tfoot>
	*/
	?>
</table>
			</div>			
				<div class="widget">
					<div class="widget-header">
<button class="btn btn-info btn-default" onclick="printjamathreport('{{ $data['rectype'] }}')" ><span class="fa fa-print"></span> PRINT</button>
					</div>
				</div>
			
		</div>
		<!-- END DATA EXPORT -->
	</div>
</div>
<!-- /main -->
<script>
	$('.createMod').click(function(e)
	{   
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href'));
	});
	
	function loadloanrecord(id)
	{
		$.ajax({
			url: 'loadcreateloanapplication',
			type: 'GET',
			data:'&lacc_id='+id,
			success: function(data)
			{
				$('.content').html(data);
				//$('.loadcreateloanapplication').html(data);
				//$('#sideMenu').load('loadcreateloanapplication');
			}
		});
	}
	
	$('.edit').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href')); 
	});
	
	function DeleteloanApp(a)
	{
		$.confirm({
			title: 'Confirm!',
			content: 'Are you confirm Delete!',
			buttons: {
				confirm: function () {
					sureDeleteloanApp(a);
				},
				cancel: function () {
					$.alert('Canceled!');
				}
				
			}
		});
	}
	
	function sureDeleteloanApp(a)
	{
		$.ajax({
			url: 'sureDeleteloanApp',
			type: 'GET',
			data:'&lacc_id='+a,
			success: function(data)
			{
				$.toast({
					heading: 'Delete!',
					text: 'Sucessfully Deleted.',
					showHideTransition: 'slide',
					icon: 'success',
					loaderBg: '#d9534f'
				});
				$('#sideMenu').load('/LoadViewLoanApplicantion');
			}
		});
	}	
	function EditJamathMembers(cst_id)
	{
		$.ajax({
			url: 'EditJamathApplication',
			type: 'GET',
			data:'&cst_id='+cst_id,
			success: function(data)
			{
				$('#sideMenu').html(data);
			}
		});
	}
//Modal other payment starts here	
	$('#myotheramt').on('show.bs.modal', function(e) {
        var $modal = $(this),
            fund_cust_id = e.relatedTarget.id;
			$('#fund_cust_id').val(fund_cust_id);
			$.ajax({
				url: 'LoadSingleCust',
				type: 'GET',
				data:'&cst_id='+fund_cust_id,
				success: function(data)
				{
					$('#cstname').val(data.cst_name);
					$('#memid').val(data.cst_code);
				}
			});
    })
//Modal other payment ends here	
//Moday Make Payment Starts here
	$('#mypayment').on('show.bs.modal', function(e) 
	{
			$('#amt_payable').val('');
			amt=0;
			grandbal=0;
			var $modal = $(this),
            payment_cust_id = e.relatedTarget.id;
			$('#pay_cust_id').val(payment_cust_id);
			$.ajax({
				url: 'CalculatePayment',
				type: 'GET',
				data:'&cst_id='+payment_cust_id,
				success: function(data)
				{
					$('#paycstname').val(data.payrecord[0].cst_name);
					$('#paymemid').val(data.payrecord[0].cst_code);
					$('#payTotal_amount').val(""+data.payrecord[0].totalamt);
					paidamt= parseFloat(parseFloat(data.payrecord[0].paidamt) + parseFloat(data.payrecord[0].advamt) + parseFloat(data.payrecord[0].prepayamt));
					$('#payPaid_amount').val(""+paidamt);
					amt= parseFloat(data.payrecord[0].totalamt) - (parseFloat(data.payrecord[0].paidamt) + parseFloat(data.payrecord[0].advamt) + parseFloat(data.payrecord[0].prepayamt));
					$('#paybalance_amount').val(""+amt);
					$('#idpaymentlist').html(data.billingtable);
					$('#grandtotbal').val(amt);
					$('#grandpaidbal').val("0");
					grandbal = amt - 0;
					$('#grandbal').val(""+grandbal);
				}
			});
    })
//Modal Make Payment Ends here
//Payment Receipt starts here
	function funPaymentReceipt(billno,dt,amt,textamt,paytype,lins_no,towards)
	{
		document.getElementById("receiptbillno").innerHTML=billno;
		document.getElementById("receiptdate").innerHTML=dt;
		document.getElementById("receiptdate1").innerHTML=dt;
		document.getElementById("textsum").innerHTML=textamt;
		document.getElementById("receiptpaytype").innerHTML=paytype;
		document.getElementById("receiptname").innerHTML=document.getElementById("lacc_custname").value;
		document.getElementById("receiptlacc_no").innerHTML=document.getElementById("lacc_no").value;
		document.getElementById("receipttowards").innerHTML=towards;
		document.getElementById("receiptamt").innerHTML=amt;
		$('#modprintpaymentreceipt').modal({show:true});		 
	}
//Payment Receipt ends here
</script>
<script type="application/javascript">
	$('.submitgradefund').click(function()
	{
		i=0;
		amt=0;
			$(".alert-info").find("ul").html('<li class="fa fa-cog fa-spin fa-1x fa-fw"></li>Processing...');
			var _token = $("input[name='_token1']").val();
			var ftyp_id =  $('#ftyp_id').val();
			var grd_id =  $('#grd_id').val();
			var fyr_id =  $('#fyr_id').val();
			var grfn_amt =  $('#grfn_amt').val();
			var grfn_paycycle = $('#grfn_paycycle').val();
			var grfn_description = $("input[name='grfn_description']").val();
			var grfn_status = $("#grfn_status").val();
			var fund_cust_id = $("#fund_cust_id").val();
			document.getElementById("errftyp_id").innerHTML = "";
			document.getElementById("errgrfn_amt").innerHTML = "";
			document.getElementById("errgrfn_paycycle").innerHTML = "";
			document.getElementById("errgrfn_description").innerHTML = "";		   	
	
			if(ftyp_id == "")
			{
				document.getElementById("errftyp_id").innerHTML = "Kindly select Fund Type...";
				$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');				
			}
			else if(grfn_paycycle == "")
			{
				document.getElementById("errgrfn_paycycle").innerHTML = "Kindly select Billing Cycle...";
				$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');				
			}
			else if(grfn_amt == "")
			{
				document.getElementById("errgrfn_amt").innerHTML = "Amount Shoud not be empty...";
				$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
			}
			else
			{
			  if(i==0)
			  {
			   i++;
			   //alert(i);
					$.ajax({
						url: "/inscstgradefund",
						type:'POST',
						data: $('#frmothercharges').serialize(),
						success: function(data) {											
							if(data.status == 1)
							{
								$('#tot_'+fund_cust_id).html(""+data.bil_billrec[0].totalamt);
								paidamt= parseFloat(parseFloat(data.bil_billrec[0].paidamt) + parseFloat(data.bil_billrec[0].advamt) + parseFloat(data.bil_billrec[0].prepayamt));
								$('#paid_'+fund_cust_id).html(""+ paidamt);
								amt= parseFloat(data.bil_billrec[0].totalamt) - (parseFloat(data.bil_billrec[0].paidamt) + parseFloat(data.bil_billrec[0].advamt) + parseFloat(data.bil_billrec[0].prepayamt));
								$('#bal_'+fund_cust_id).html(""+amt);
								$('#grfn_amt').val('');
								$("input[name='grfn_description']").val('');
								$('#myotheramt').modal('toggle');
								printSuccessMsg(data.message);
							}
							else
							{
								printErrorMsg(data.message)
							}
						}
					});
				}
			}			
		return false;
	});
	function printErrorMsg (msg) {			
		$(".alert-info").css('display','none');
		$(".print-success-msg").css('display','none');
		$(".print-error-msg").find("ul").html('');
		$(".print-error-msg").css('display','block');
		$(".print-error-msg").find("ul").append('<li class="fa fa-times-circle fa-1x fa-fw"></li>'+msg);
	}	
	function printSuccessMsg (msg) {
		$(".alert-info").css('display','none');
		$(".print-error-msg").css('display','none');
		$(".print-success-msg").find("ul").html('');
		$(".print-success-msg").css('display','block');
		$(".print-success-msg").find("ul").append('<li class="fa fa-check  fa-1x fa-fw"></li>'+msg);
	}
</script>
<script>
function paymentcalculation() 
{
	var varpaid = 0;
	var gradbalamt1 = 0;
	var amt_payable=0;
	if (isNaN(parseFloat($('#amt_payable').val()))) 
	{
		amt_payable = 0;
	}
	else
	{
		amt_payable = parseFloat($('#amt_payable').val());
	}
	var trn_id = document.getElementsByName('trn_id[]');
	var totbalamt = document.getElementsByName('totbalamt[]');
	var paidamt = document.getElementsByName('paidamt[]');
	var balamt = document.getElementsByName('balamt[]');
	var grandtot = $('#grandtotbal').val();
	for (var i = 0; i <trn_id.length; i++)
	{
		//trn_id[i].value totbalamt[i].value paidamt[i].value balamt[i].value
		if(parseFloat(totbalamt[i].value) >= amt_payable)
		{
			varpaid = amt_payable;
			amt_payable = 0;
		}
		else
		{
			varpaid = totbalamt[i].value;				
			amt_payable =  amt_payable - parseFloat(totbalamt[i].value);
		}
		paidamt[i].value = varpaid;
		balamt[i].value = parseFloat(totbalamt[i].value) - parseFloat(paidamt[i].value);
		$('#grandpaidbal').val($('#amt_payable').val());
		gradbalamt1 = gradbalamt1 + parseFloat(balamt[i].value);
	}
	document.getElementById("grandbal").value = gradbalamt1;
	document.getElementById("adv_amt").value = amt_payable;
}
</script>
<script>
function calculatetotalamt()
{
	var varpaid = 0;
	var varsum = 0;
	var gradbalamt1 = 0;
	document.getElementById("amt_payable").value = 0;
	var totbalamt = document.getElementsByName('totbalamt[]');
	var paidamt = document.getElementsByName('paidamt[]');
	var balamt = document.getElementsByName('balamt[]');
	for (var i = 0; i <paidamt.length; i++)
	{ 
		if(paidamt[i].value > 0) 
		{
			varpaid = paidamt[i].value;
		}
		else
		{
			 varpaid = 0;
		}
		varsum = varsum + parseFloat(varpaid);
		balamt[i].value = parseFloat(totbalamt[i].value) - parseFloat(varpaid);
		gradbalamt1 = gradbalamt1 + parseFloat(balamt[i].value);
	}
	$('#amt_payable').val(varsum)
	$('#grandpaidbal').val(varsum)
	$('#grandbal').val(gradbalamt1)
}
</script>
<script>
	$('.submitpayment').click(function()
	{
		i=0;
		amt=0;
		//$(".alert-paymentinfo").find("ul").html('<li class="fa fa-cog fa-spin fa-1x fa-fw"></li>Processing...');
		var _token = $("input[name='_token2']").val();
		var pay_cust_id =  $('#pay_cust_id').val();
		var paycstname =  $('#paycstname').val();
		var payTotal_amount =  $('#payTotal_amount').val();
		var payPaid_amount =  $('#payPaid_amount').val();
		var paybalance_amount =  $('#paybalance_amount').val();
		if (isNaN(parseFloat($('#amt_payable').val()))) 
		{
			amt_payable = 0;
		}
		else
		{
			amt_payable = parseFloat($('#amt_payable').val());
		}
		var adv_amt = $('#adv_amt').val();
		document.getElementById("erramt_payable").innerHTML = "";
		arrtrn_id = [];
		arrslno = [];
		arrftype = [];
		arrpaycycle = [];
		arrtotbalamt = [];
		arrpaidamt = [];
		arrbalamt = [];
		arrfy_id = [];
		arrgrd_id = [];
		arrgrfn_id = [];
		arrftyp_id = [];
		var trn_id= document.getElementsByName("trn_id[]");
		var slno= document.getElementsByName("slno[]");
		var ftype= document.getElementsByName("ftype[]");
		var paycycle= document.getElementsByName("paycycle[]");
		var totbalamt= document.getElementsByName("totbalamt[]");
		var paidamt= document.getElementsByName("paidamt[]");
		var balamt= document.getElementsByName("balamt[]");
		   
		var fy_id= document.getElementsByName("fy_id[]");
		var grd_id= document.getElementsByName("grd_id[]");
		var grfn_id= document.getElementsByName("grfn_id[]");
		var ftyp_id= document.getElementsByName("ftyp_id[]");
		
		var grandtotbal= document.getElementsByName("grandtotbal");
		var grandpaidbal= document.getElementsByName("grandpaidbal");
		var grandbal= document.getElementsByName("grandbal");
		var validationbalamt = 0;
		for (index = 0; index < trn_id.length; ++index)
		{
			arrtrn_id[index] = trn_id[index].value;
			arrslno[index] = slno[index].value;
			arrftype[index] = ftype[index].value;
			arrpaycycle[index] = paycycle[index].value;
			arrtotbalamt[index] = totbalamt[index].value;
			arrpaidamt[index] = paidamt[index].value;
			arrbalamt[index] = balamt[index].value;
			arrfy_id[index] = fy_id[index].value;
			arrgrd_id[index] = grd_id[index].value;
			arrgrfn_id[index] = grfn_id[index].value;
			arrftyp_id[index] = ftyp_id[index].value;
			if(parseFloat(balamt[index].value) < 0)
			{
				validationbalamt = 1;
			}
		}

		if(amt_payable == 0)
		{
			document.getElementById("erramt_payable").innerHTML = " Mandatory Field..";
		}
		else if(validationbalamt ==1)
		{
			alert("Balance amount should not be negative");
		}
		else
		{
		  if(i==0)
		  {
				i++;		
				$.ajax({
					url: "makebillingpayment",
					type:'POST',
					dataType: 'json',
					//cache : false,
					//processData: false,
					data: {_token:_token,pay_cust_id:pay_cust_id, paycstname:paycstname, payTotal_amount:payTotal_amount, payPaid_amount:payPaid_amount, paybalance_amount:paybalance_amount, amt_payable:amt_payable,adv_amt:adv_amt, arrtrn_id:arrtrn_id,arrslno:arrslno,arrftype:arrftype,arrpaycycle:arrpaycycle,arrtotbalamt:arrtotbalamt,arrpaidamt:arrpaidamt,arrbalamt:arrbalamt,arrfy_id:arrfy_id,arrgrd_id:arrgrd_id,arrgrfn_id:arrgrfn_id,arrftyp_id:arrftyp_id},
					success: function(data)
					{
						if(data.status == "Success")
						{
							alert("Billing Record inserted Sucessfully");
							BillPrint(data.transaction_id);
						}
						else
						{
							alert("Failed to Generate Purchase Bill..");
							//printErrorMsg(data.message)
						}
					}
				});
			}
		}
		return false;
	});
		
	function BillPrint(transaction_id)
	{
		$('#mypayment').modal('toggle');
		$("#modprintpaymentreceipt").modal();
		$.ajax({
			url: 'printpaymentbill',
			type: 'get',
			data:'&transaction_id='+transaction_id,
			success: function(data)
			{
				//Update balance in table starts here
				$('#tot_'+data.trn_transaction.cst_id).html(""+data.bil_billrec[0].totalamt);
				paidamt= parseFloat(parseFloat(data.bil_billrec[0].paidamt) + parseFloat(data.bil_billrec[0].advamt) + parseFloat(data.bil_billrec[0].prepayamt));
				$('#paid_'+data.trn_transaction.cst_id).html(""+ paidamt);
				amt= parseFloat(data.bil_billrec[0].totalamt) - (parseFloat(data.bil_billrec[0].paidamt) + parseFloat(data.bil_billrec[0].advamt) + parseFloat(data.bil_billrec[0].prepayamt));
				$('#bal_'+data.trn_transaction.cst_id).html(""+amt);
				//Update balance in table ends here							
				document.getElementById("receiptbillno").innerHTML=data.trn_billno;
				document.getElementById("receiptmember_id").innerHTML= data.cst_grade_prefix + "-" + data.cst_code;
				document.getElementById("receiptdate").innerHTML=data.trn_date;
				document.getElementById("receiptname").innerHTML=data.cst_name;
				document.getElementById("receiptamt1").innerHTML=data.bilr_gtotal;
				document.getElementById("transaction_id").value=transaction_id;
				$('#receipttable').html(data.transrecord);
			}
		});
	}
	function printDiv(divName)
	{
		var transaction_id = document.getElementById("transaction_id").value;
		window.open('printprintbill?transaction_id=' + transaction_id  ,'_blank');
	}
$(document).ready( function () {
    $('#mydatatable').DataTable({
		"sDom": '<"H"lfrp>t<"F"ip>',
	});
} );
function loadjamathrecords(rectype) // Pending Accounts Paid Accounts
{
	if(rectype == "All")
	{
		document.getElementById("idreload1").innerHTML = "<i class='fa  fa-american-sign-language-interpreting fa-spin' aria-hidden='true'></i>All Accounts";
	}
	if(rectype == "Pending")
	{
		document.getElementById("idreload2").innerHTML = "<i class='fa  fa-thumbs-down fa-spin' aria-hidden='true'></i>Pending Accounts";
	}
	if(rectype == "Paid")
	{
		document.getElementById("idreload3").innerHTML = "<i class='fa  fa-thumbs-up fa-spin' aria-hidden='true'></i>Paid Accounts";
	}
	var pagename = "jamathxlspmtrpt";
	$.ajax({
		url: pagename,
		type: 'get',
		data:'&rectype='+rectype,
		success: function(data)
		{
			$('#sideMenu').html(data);
			if(rectype == "All")
			{
				document.getElementById("idreload1").innerHTML = "<i class='fa  fa-american-sign-language-interpreting' aria-hidden='true'></i>All Accounts";
			}
			if(rectype == "Pending")
			{
				document.getElementById("idreload2").innerHTML = "<i class='fa fa-thumbs-down' aria-hidden='true'></i>Pending Accounts";
			}
			if(rectype == "Paid")
			{
				document.getElementById("idreload3").innerHTML = "<i class='fa fa-thumbs-up' aria-hidden='true'></i>Paid Accounts";
			}
		}
	});
}
function printjamathreport(rectype)
{
	window.open('jamathxlspmtrptprint?rectype='+rectype,'_blank');
}
</script>