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

extract($_GET);
$invoice = trim($invoice);

$sec = $obj->Details_By_Cond("_fee_master_invoice","_invoice_no='$invoice'");
extract($sec);
$std = $obj->Details_By_Cond("_pf_std_personal_info","_id='".$sec['_std_id']."'");
extract($std);
$det_in = $obj->Details_By_Cond("_int_institute_setup_vw","section_id='".$sec['_section_id']."'");
extract($det_in);
//==================================================================
$dept_id = $det_in['department_id'];
//include("late_fine_date_compare.php");

//===================================================================
if($sec['_status']=='U'){
?>

<script src="module/fees/js/std_invoice.js" type="text/javascript"></script>
<!--<script src="module/fees/js/collect_fee.js" type="text/javascript"></script>-->

<div class="row-fluid">
	<div class="span2"></div>
	<div class="span8">
		<table id="sample-table-273" class="table table-striped table-bordered table-hover" style="font-size:13px;">
			<thead style="padding: 3px;">
				<tr>
					<td colspan="4" style="text-align: center; background-color:#438eb9; color:#FFFFFF; font-weight:bold; font-size:14px;"><?php echo $std['_full_name']; ?>'s Fee Information</td>
				</tr>
				<tr>
					<td width="16%" style="font-weight:bold; background-color:#FFFFFF; line-height:10px; text-align:right;">Shift : </td>
                    <td style="font-weight:bold; background-color:#FFFFFF; line-height:10px;"><span class="label label-success" style="font-weight:bold;"><?php echo $det_in['shift']; ?></span></td>
					<td width="16%" style="font-weight:bold; background-color:#FFFFFF; line-height:10px;text-align:right;">Medium : </td>
                    <td style="font-weight:bold; background-color:#FFFFFF; line-height:10px;"><span class="label label-success" style="font-weight:bold;"><?php echo $det_in['medium']; ?></span></td>
				</tr>
				<tr>
					<td style="font-weight:bold; line-height:10px;text-align:right;">Class :</td>
                    <td style="font-weight:bold; line-height:10px;"><span class="label label-warning" style="font-weight:bold;"><?php echo $det_in['class']; ?></span></td>
					<td style="font-weight:bold;line-height:10px;text-align:right;">Department </td>
                    <td style="font-weight:bold; line-height:10px;"><span class="label label-warning" style="font-weight:bold;"><?php echo $det_in['department']; ?></span></td>								
				</tr>
				<tr>
					<td style="font-weight:bold; background-color:#FFFFFF; line-height:10px;text-align:right;">Student Type : </td>
                    <td style="font-weight:bold; background-color:#FFFFFF; line-height:10px;"><span class="label label-success" style="font-weight:bold;"><?php echo $det_in['student_type']; ?></span></td>	
					<td style="font-weight:bold; background-color:#FFFFFF; line-height:10px;text-align:right;">Section : </td>
                    <td style="font-weight:bold; background-color:#FFFFFF; line-height:10px;"><span class="label label-success" style="font-weight:bold;"><?php echo $det_in['section']; ?></span></td>				
				</tr>
				<tr>				
					<td style="font-weight:bold; line-height:10px;text-align:right;">Roll : </td>
                    <td style="font-weight:bold; line-height:10px;"><span class="label label-warning" style="font-weight:bold;"><?php echo $std['_section_roll']; ?></span></td>
					<td style="font-weight:bold; line-height:10px;text-align:right;">Year : </td>
                    <td style="font-weight:bold; line-height:10px;"><span class="label label-warning" style="font-weight:bold;"><?php echo $sec['_year']; ?></span></td>
				</tr>
				<tr>				
					<td style="font-weight:bold; background-color:#FFFFFF; line-height:10px;text-align:right;">Month : </td>
                    <td style="font-weight:bold; background-color:#FFFFFF; line-height:10px;"><span class="label label-success" style="font-weight:bold;"><?php echo $obj->Month($sec['_month']); ?></span></td>
					<td style="font-weight:bold; background-color:#FFFFFF; line-height:10px;text-align:right;">Invoice : </td>
                    <td style="font-weight:bold; background-color:#FFFFFF; line-height:10px;"><span class="label label-success" style="font-weight:bold;"><?php echo $sec['_invoice_no']; ?></span></td>
				</tr>
			</thead>
		</table>
	</div>
	<div class="span2"></div>

</div>

<div class="row-fluid" id="inv_det_holder">
	<div class="span2">
		<input type="hidden" id="mod_id_1" value="<?php echo $mod_id; ?>">
        <input type="hidden" id="invoice_no_master" value="<?php echo $sec['_invoice_no']; ?>" />
        <input type="hidden" id="dept_id" value="<?php echo $dept_id; ?>" />
	</div>
	<div class="span8">
	<button class="btn btn-small btn-primary" id="add_fee_head_btn" style="float:right; margin-bottom:5px" onclick="add_more_fee_head()">
		<i class="icon-plus"></i>
			Add Fee head
	</button>
	<table id="fee_head_up_tbl" class="table table-striped table-bordered table-hover" style="font-size:12px;">
		<thead>
			<tr>
				<th>Sl</th>
				<th>Fee Head</th>
				<th>Req. Amount</th>
				<th>Action</th>
				<!--<th>Discount</th>-->
			</tr>
		</thead>
		
		<tbody>
			<?php
			$i=0;
			foreach($obj->View_All_By_Cond("_fee_details_invoice","_invoice_no='$invoice' AND _br_id='$branchid'") as $v1){
			$i++;
			?> 			
			<tr id="tr_<?php echo $i;?>">
				<td class="td_pad">
					<input type="hidden" id="sl_no_<?php echo $i;?>" name="sl_no[]" value="<?php echo $i;?>"/><?php echo $i; ?></td>
				<td class="td_pad">
				<?php echo $v1['_fee_head']; ?>
				<input type="hidden" id="fee_head_id_<?php echo $i;?>" name="fee_head_id[]" value="<?php echo $v1['_id'];?>"/>
				<input type="hidden" id="fee_head_name_<?php echo $i;?>" name="fee_head_name[]" value="<?php echo $v1['_fee_head'];?>">
				</td>
				<td class="td_pad"><?php if($v1['_type']=='C'){?> 
				<input name="fee_head_adj_amount[]" id="fee_head_adj_amount_<?php echo $i;?>" class="headamount" value="<?php echo $v1['_amount']; ?>" type="text" onchange="cal_tot_adj_amount()">
                <?php }?>
				</td>
				<td><i data-action="close" class="icon-trash bigger-170 " Style="color:red" onclick="remove_from_inv('<?php echo $i;?>')" Style="float:right;"></i>
				</td>
			</tr>
			<?php } ?>	
		</tbody>	
		<tfoot>
		<?php
			$c = $obj->View_column_details_By_Cond("_fee_details_invoice","SUM(_amount) AS credit","_invoice_no='$invoice' AND _type='C' AND _br_id='$branchid'");
			$d = $obj->View_column_details_By_Cond("_fee_details_invoice","SUM(_amount) AS debit","_invoice_no='$invoice' AND _type='D' AND _br_id='$branchid'");
		?>
		<tr>
			<th colspan="2">Due Amount:</th>
			<td colspan="2" id="total_amount" style="color: red; font-weight: bold;"><?php echo $c['credit'];?>
			
			<!--<input type="text"  value="<?php echo $c['credit'];?>" readonly />--></td>
			<!--<th>Paid</th>
			<td><input type="text" id="total_d_amount" value="<?php if($d['debit']!=''){echo $d['debit'];}else{ echo 0;}?>" /></td>-->
		</tr>	
		</tfoot>
		<input id="total_amount_input" type="hidden" value="<?php echo $c['credit'];?>"/>
	</table>
	<input type="hidden" id="fee_head_adj_inv_no" name="fee_head_adj_inv_no" value="<?php echo $invoice;?>"/>
	<button class="btn btn-small btn-success" id="update_fee_amount_btn" type="button" onclick="perform_fee_amount_update()" style="float: right; margin-bottom: 10px;">
		<i class="icon-ok bigger-100"></i>Save</button>
	</div>
</div>
<div class="modal fade" id="add_more_head_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Fee Head Search</h4>
            </div>
            <div class="modal-body">
                <!--<input type="text" class="form-control" id="fee_head_search_box" name="feeHeadSearchbox" placeholder="Fee Head Name">
					<button type="button" class="btn btn-success btn-small" style="margin: 0px; position: relative; bottom:5px; right: 3px;" onclick="feeHeadSearchboxList(<?php echo $det_in['department_id'];?>)">Search</button>-->
                <div id="FeeHeadSearchBoxResult"></div>
            </div>

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

<?php
}
elseif($sec['_status']=='P'){
?>
<!--<div class="row-fluid">This Invoice Already Paid</div>-->
<div class="alert alert-error">
<button class="close" type="button" data-dismiss="alert">
<i class="icon-remove"></i>
</button>
<strong>
<i class="icon-remove"></i>
Oh snap!
</strong>
This Invoice Already Paid
<br>
</div>
<?php
}
elseif($sec['_status']=='AD'){
?>
<!--<div class="row-fluid">This Invoice Expired</div>-->
<div class="alert alert-error">
<button class="close" type="button" data-dismiss="alert">
<i class="icon-remove"></i>
</button>
<strong>
<i class="icon-remove"></i>
Oh snap!
</strong>
This Invoice Expired
<br>
</div>
<?php
}
else{
?>
<!--<div class="row-fluid">Invalid Invoice No or Barcode</div>-->
<div class="alert alert-error">
<button class="close" type="button" data-dismiss="alert">
<i class="icon-remove"></i>
</button>
<strong>
<i class="icon-remove"></i>
Oh snap!
</strong>
Invalid Invoice No or Barcode
<br>
</div>
<?php
}
?>

<script type="text/javascript" >

/*$(document).on("change", ".headamount", function() {
    var sum = 0;
    $(".headamount").each(function(){
        sum += +$(this).val();
    });
	document.getElementById("total_amount").innerHTML = "<b>"+sum+"</b>";
    //$("#total_amount").val(sum);
});*/

$(document).on("change", ".head_amount", function() {
    var sum = 0;
    $(".head_amount").each(function(){
        sum += +$(this).val();
    });
	
    $("#total_d_amount").val(sum);
});
function perform_fee_amount_update(){
	//var i = 0;
	var fee_head_id =	 $('input[name="fee_head_id[]"]').map(function(){
					return $(this).val();}).get();
	var fee_head_name = $('input[name="fee_head_name[]"]').map(function(){
					return $(this).val();}).get();
	var j = 0;
	var adj_amounts = $('input[name="fee_head_adj_amount[]"]').map(function(){
						return $(this).val();}).get();
	
	var adj_length = adj_amounts.length;
	var alert_msg = "";
	var op = "update_insert";
	for(j;j<adj_length;j++){
		if(adj_amounts[j]=='' ||  adj_amounts[j]==0){
			alert('Please Enter a valid amount');
			return false;
		}
		
		if(fee_head_name[j]==''){
			alert('Please Enter a valid Fee name');
			return false;
		}
		
		//alert_msg = alert_msg + 'id-'+fee_head_id[j]+ ',name-'+fee_head_name[j]+',amount-'+adj_amounts[j] + ',' +'\n';
	}
	var invoice_no = $("#fee_head_adj_inv_no").val();
	var adj_tot_amount = $("#total_amount_input").val();
	alert_msg = alert_msg+adj_tot_amount;
	var datastr = 'inv_no='+invoice_no+'&adj_tot_amount='+adj_tot_amount+'&fee_head_id='+fee_head_id+'&adj_amounts='+adj_amounts+"&fee_head_name="+fee_head_name+"&operation="+op;
	//alert(alert_msg);
	//alert(datastr);
	var conf_msg_datastr = "invoice_no="+invoice_no;
	//$("#inv_det_holder").load("module/fees/fee_update_conf_msg.php",conf_msg_datastr);
	$.ajax({
	    type:"POST",
	    url:"module/fees/fee_head_adjustment_conf.php",
	    data:datastr ,
	    success:function(result){
				//alert(result);
				if(result == 1){
					alert('Fee has been adjusted successfully');
					$("#inv_det_holder").load("module/fees/fee_update_conf_msg.php",conf_msg_datastr);
				}
				else{
					alert('Error Occured during fee adjustment');
				}
	    }
	});
}
var new_fee_head_no=1;
function add_more_fee_head(){
	var mod_id = $('#mod_id_1').val();
	var inv_no = $('#invoice_no_master').val();
	var sl_no = $('input[name="sl_no[]"]').map(function(){
						return $(this).val();}).get();
	var sl_no_length = sl_no.length;
	var last_sl_no = sl_no_length + 1;
	var dept_id = $('#dept_id').val();
	var input_box_id="input_button"+new_fee_head_no;
	var datastr="id="+input_box_id+"&name="+new_fee_head_no+"&mod_id="+mod_id+"&inv_no="+inv_no+"&sl_no="+last_sl_no+"&dept_id="+dept_id;
	//alert(datastr);
	$.ajax({
				type:"post",
				url:"module/fees/add_more_fee_head_edit_inv.php",
				data:datastr ,
				success:function(st){
					//alert(st);
					$("#fee_head_up_tbl").append(st);
				}
				
			});
	new_fee_head_no++;
}
function feeHeadSearchboxList(dept_id){
		//var fee_head_name = $('#fee_head_search_box').val();
       // $("#FeeHeadSearchBoxResult").text("Searching....");
       // var datastr = "fee_head_name="+fee_head_name+"&dept_id="+dept_id;
        var datastr = "dept_id="+dept_id;
		//alert(datastr);
        $("#FeeHeadSearchBoxResult").load("module/fees/fee_head_search_load.php", datastr);
		//alert(alert_msg);
	}
</script>