<?php
session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj=new my_class();
// ============
$branchid = $_SESSION['LOGIN_BRANCH'];

?>

<link rel="stylesheet" href="assets/css/chosen.css" />
<script src="module/finance/js/voucher_credit_single.js" type="text/javascript"></script>
<style type="text/css">
i{
	cursor:pointer;
}

</style>
<style type="text/css">

/*.dbt_vouchar{
	max-width:100%;

    padding-left: 15px;
    padding-bottom: 10px;
    padding-right: 15px
}*/
/*#lgnd{
	text-align: center;
}*/

div.scrollWrapper{
  border: 2px solid #87B87F;
  height:320px;
 
  overflow:scroll;
}
#blc {
    float: right;
}
#totall_db{
	float: right;

}
#totall_cr{
	float: right;
}

</style>

	
<?php if($_SESSION['add']=='Y'){ ?>	

<div class="row-fluid" >	
<div class="row-fluid" >	
	<form class="form-vertical" >
	<legend>
		<h4 class="lighter">
		<i class="icon-hand-right icon-animated-hand-pointer blue"></i>
		<a class="pink" data-toggle="modal" href="#modal-wizard"> Credit Voucher </a>
	</h4>
	</legend>
<div>
<div class="span6">	
<div class="span4">
<div class="control-group">
<label class="control-label" for="form-field-1">Date<font color="#FF0000">*</font></label>
		<div class=" input-append">
			<input class="date-picker" style="width:100px;"  id="voucher_date"  type="text" data-date-format="dd-mm-yyyy" />
			<span class="add-on">
				<i class="icon-calendar"></i>
			</span>
		</div>
	</div>
</div>

<div class="span4">
	<div class="control-group">
		<label class="control-label" for="form-field-1">Voucher Type<font color="#FF0000">*</font></label>
		<div class="controls">
		   <select id="voucher_type" style="width:142px;" onchange="get_balance(this.value)">
				<option value="">---SELECT---</option>
				<option value="C" selected>Cash</option>
				<option value="B">Bank</option>
				
			</select>
		</div>

	</div>
</div>



     
<div class="span4">
	<div class="control-group">
		<label class="control-label" for="form-field-1">Voucher No<font color="#FF0000">*</font></label>
		<div class="controls">
			  <input type="text" style="width:128px;"  class="small" id="voucher_number"    />
		</div>

	</div>
</div>



<div class="row-fluid" >	
<div class="span12">
	<div class="control-group">
	<label class="control-label" for="form-field-1">Particulars<font color="#FF0000">*</font></label>
	<textarea class="span12" style="width:95%;" rows="3" id="particular" placeholder="Particulars "></textarea>

	</div>
</div>
</div>


 <div class="row-fluid">	
   <div class="span4">
	<div class="control-group">
    <label class="control-label"for="form-field-select-3">Select GL</label>

   
<select class="chzn-select" id="name" data-placeholder="Choose a GL...">
   <option value=""></option>
<?php 
foreach($obj->View_All_By_Cond("acc_gl_head_","_br_id='$branchid' AND _status='1' AND gl_head_has_child='0'") as $value)

{
echo "<option value='".$value['gl_head_name']."|".$value['gl_head_code']."|".$value['_id']."'>".$value['gl_head_code']."&nbsp; | ".$value['gl_head_name']."</option>";
}
?>

</select>
</div>
 </div>
 <div class="span3">
		<div class="control-group">
			<label class="control-label" for="form-field-1">Amount<font color="#FF0000">*</font></label>
			<div class="controls">
				 <input type="text" style="width:85px" class="input-mini Credit" id="credit" />
			</div>

		</div>

		
</div>

<!-- 	<div class="span3">
				<div class="control-group">
					<label class="control-label" for="form-field-1">Debit<font color="#FF0000">*</font></label>
					<div class="controls">
						 <input type="text" style="width:85px" class="input-mini Debit " id="debit" />
					</div>

				</div>
				 
	</div> -->


<div class="span2">
	<div class="control-group">
		<label class="control-label" for="form-field-1">.</label>
		<div class="controls">
			<button class="btn btn-small btn-success" type="button" id="add_field_button">
			   <i class="icon-plus bigger-100" style="hover:cursor;" ></i>Add
			</button>
		</div>

	</div>

		
</div>


</div>

</div>
<?php

$blnc=$obj->cash_in_hand_balance($branchid);
extract($blnc);

?>

<div class="balance">
<label class="control-label" id="blc" class="" for="form-field-1"> Opening Balance(Cash)=<?php echo $cash_balance_default;?></label>
</div>		
		


<div class="span6 scrollWrapper">
<table class="table" id="input_fields_wrap">
<thead>
	<tr>
	    <th>SL</th>
	    <th>Code</th>
		<th>GL Head</th>
		<!-- <th>Debit</th> -->
		<th>Amount</th>
		<th>Action</th>
	</tr>
</thead>
<tbody>

</tbody>

</table>

</div>

<!-- <div class="span9">
		<div class="control-group">
		<label class="control-label" id="totall_db" for="form-field-1">Total Amount=0 </label>	
		</div>
		
</div> -->
<div class="span9">
		<div class="control-group">
		<label class="control-label" id="totall_cr" for="form-field-1">Total Amount=0 </label>	
		</div>
		
</div>

<div class="row-fluid">
<div class="span12 ">
    <div class="control-group btn_add_re">
		<label class="control-label" for="form-field-1">&nbsp;</label>

	<div class="controls">
		<button class="btn btn-small btn-success" type="button" id="btn_save">
			<i class="icon-ok bigger-100"></i>
			Save
		</button>

		&nbsp; &nbsp; &nbsp;
		<button class="btn btn-small btn-danger" type="reset" id="btn_reset">
			<i class="icon-undo bigger-100"></i>
			Reset
		</button>
    </div>
	</div>
</div>
</div>


</form>


<?php } ?>

<script >

// ================sum_function================
function sum(){

 
	 var debit=[];		
	   var debit_div = $("#input_fields_wrap").find('tbody td.dbt');
	   
	   $(debit_div).each(function(i){

	   		debit[i] = $(this).text();
	   });

	   var credit=[];		
	   var credit_div = $("#input_fields_wrap").find('tbody td.cbt');
	   
	   $(credit_div).each(function(i){

	   		credit[i] = $(this).text();
	   }); 
	
    var n   = credit.length
    var sumcr = 0;
   while(n--){
   sumcr += parseFloat(credit[n]) || 0;
   }

    var nn   = debit.length
    var sumdbt= 0;
   while(nn--){
   sumdbt += parseFloat(debit[nn]) || 0;
   }
//alert(sumdbt);
$('#totall_cr').text('Total Amount='+sumcr);
$('#totall_db').text('Debit='+sumdbt);
}

// ================sum_function================


// ================serial======================

function serial(){
	$("td.serial").each(function(index,element){                 
	   $(element).text(index + 1); 
	});
}

function serial_sub(id){
		$("td#serial_sub_"+id+"").each(function(index,element){                 
   		$(element).text(index + 1); 
	});
}

// ================serial================

// ================sum_subgl_amount================

function sum_subgl_amount(id){

	  var amount=[];		
	   var amnt_div = $('#input_subgl_'+id+'').find('tbody tr td.amount');
	   
	   $(amnt_div).each(function(i){

	   		amount[i] = $(this).text();
	   });

		var n   = amount.length
		var tamount = 0;
		while(n--){
		tamount += parseFloat(amount[n]) || 0;
   }

     $('#totalamnt_'+id+'').text('Total Amount='+tamount);

	//alert(id);
}

// ================sum_subgl_amount================

// ================GL_Add================

$(document).ready(function() {
    var wrapper         = $("#input_fields_wrap"); 
    var add_button      = $("#add_field_button"); 
    
    var x = 1; 
    var y=1;
    //var dc=1;
//$(".Debit").hide(); 
$(add_button).click(function(){

    var name = $.trim($("#name").val());
    var debit = $.trim($(".Debit").val());
    var credit = $.trim($(".Credit").val());
  
    var result = name.split('|');
    code=result[1];
    gl_name=result[0];
    id=result[2];

 if(name==""){
	alert('Select GL');
	$('#name').focus();
	return false;
}
 else if(debit=="" && credit==""){

	alert("Enter Amount");
	return false;
}
 else if(debit!="" && credit!=""){

	alert("Enter Any One");
	return false;
}
else if(debit==""){
	debit=0;
}
else if(credit==""){
	credit=0;
}

if(debit!=0){

	var total_subgl_amnt="Debit Amount="+debit;
	var gl_amount=debit;
}
else if(credit!=0){
	var total_subgl_amnt="Credit Amount="+credit;
	var gl_amount=credit;
}
    
$(".chzn-select").val('').trigger("liszt:updated");
$('.Debit').val('');
$('.Credit').val('');


	$('#input_fields_wrap > tbody:last-child').append('<tr><td class="serial">'+y+'</td><td>'+code+' <input type="hidden" class="yval" value="'+y+'"/></td><td>'+gl_name+'<input type="hidden" id="gl_id" value="'+id+'"/></td><!--<td class="dbt" id="debit_'+y+'">'+debit+'</td>--><td class="cbt" id="credit_'+y+'">'+credit+'</td><td><a href="#" class="add_sub_gl" id='+y+'><font color="#87B87F"><i class="icon-plus icon-large" href="#modal-table_'+y+'" data-toggle="modal"  style="hover:cursor;" onclick="load_subgl('+id+')"></i></font></a>&nbsp;<a href="#" class="remove_field" id='+y+'><font color="#FF0000"><i class="icon-remove icon-large" style="hover:cursor;"></i></font></a></td></tr>'); //add input box
	$('#input_fields_wrap> tbody:last-child').append('<div id="modal-table_'+y+'" class="modal hide fade  " style="overflow-x:show;max-width:60%;min-width:52%;" tabindex="-1"><div class="modal-header no-padding"><input type="hidden" id="gl_amount_'+y+'" value="'+gl_amount+'"/><div class="table-header"><button type="button" class="close" data-dismiss="modal">&times;</button>Entry Sub GL For ' +gl_name+' On '+total_subgl_amnt+'</div></div><div class="modal-body " id="model_table_value"><div class="span4"><div class="span10"><div class="control-group"><label class="control-label" for="form-field-1">Select Parent GL<font color="#FF0000">*</font></label><div class="controls"><select class="sub_gl" id="sub_gl_'+y+'"><option value="">---SELECT---</option></select></div></div></div><div class="row-fluid"><div class="span10"><div class="control-group"><label class="control-label" for="form-field-1">Amount<font color="#FF0000">*</font></label><div class="controls"><input type="text"  class="small sub_gl_amount" id="sub_gl_amount_'+y+'"/></div></div></div><button class="btn btn-small btn-success btn_subgl_add" type="button" btn_id="'+y+'"  id="button_subgl_add_'+y+'"><i class="icon-plus bigger-100" style="hover:cursor;"></i>Add</button></div></div><div class="span7 scrollWrapper"><table class="table" id="input_subgl_'+y+'"><thead><tr><th>SL</th><th>Code</th><th>Sub GL</th><th>Amount</th><th>Action</th></tr></thead><tbody></tbody></table></div><div class="span10"><label id="totalamnt_'+y+'" class="totalamnt" style="padding-left:60%;">Total Amount=0</label></div></div><div class="modal-footer"><button class="btn btn-small btn-success pull-left" type="button" id="btn_update" data-dismiss="modal" onclick="sub_gl_add('+y+')"><i class="icon-ok bigger-100"></i>Save</button><button class="btn btn-small btn-danger pull-left" data-dismiss="modal"><i class="icon-remove"></i>Close</button></div></div>');
  	sum();
  	serial();
  	y++;
  	exit();
	});

	$("#input_fields_wrap").on("click",".btn_subgl_add", function(){ 	
		var ss = $(this).attr("btn_id"); 
		//alert(ss); 
		var sub = $.trim($('#sub_gl_'+ss+'').val());
		var result = sub.split('|');
	    sub_id=result[0];
	    subgl=result[1];
	    sub_code=result[2];
	 
        var amount = $.trim($('#sub_gl_amount_'+ss+'').val());
		var total_amount =$('#totalamnt_'+ss+'').text();
		var result_amount=total_amount.split('=');
		    total_amount=Number((result_amount[1]))+Number(amount);
		    

		var gl_amount=$.trim($('#gl_amount_'+ss+'').val());
		//alert(total_amount);
    if(sub!='' && amount!=''){
    	 if(total_amount<=gl_amount){
		$('#input_subgl_'+ss+'> tbody:last-child').append('<tr><td id="serial_sub_'+ss+'"></td><td>'+sub_code+'</td><td> '+subgl+' <input type="hidden" id="sgl_id" value="'+sub_id+'"/><input type="hidden" id="sgl_gl_id" value="'+id+'"/> </td><td class="amount" id="amount_'+y+'">'+amount+'</td><td><a href="#" class="remove_sub_gl" id='+x+'><font color="#FF0000"><i class="icon-remove icon-large" style="hover:cursor;"></i></font></a></td></tr>');
	    $('#sub_gl_amount_'+ss+'').val('');
	    $('#sub_gl_'+ss+'').val('');
	     serial_sub(ss);
	     sum_subgl_amount(ss);
	 }
	 else{
	 	alert("Your Total Sub GL Amount Exceeds your Gl Amount");
	 }
}
else{
	alert('Please Select Above Fields');
}

$('#input_subgl_'+ss+'').on("click",".remove_sub_gl", function(){ 
    $(this).parent().parent().remove();
 sum_subgl_amount(ss);
 serial_sub(ss);

})

});
   
$(wrapper).on("click",".remove_field", function(){ 
    $(this).parent().parent().remove();
    alert(this);
    sum();
   serial();
   serial_sub();
})
});

</script>

<script type="text/javascript">
$('.date-picker').datepicker().next().on(ace.click_event, function(){
	$(this).prev().focus();
});
</script>

<script type="text/javascript">
  $(".chzn-select").chosen({ width: '95%' }); 
</script>



    