<script language="javascript">
function validation()
{
	var val			    = parseInt(document.getElementById("min").value);
	var val2			= parseInt(document.getElementById("max").value);	
	if(val > val2)
	{
		alert("Max. Amount Is Greater From Min. Amount... !!! ");	
		return false;
	}
	else
	{
		return true;	
	}
}	
function delete_tables()
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            window.location.href = "home.php?page=multiple_ac_list";
        }
    }
    xmlhttp.open("GET", "pages/register_case/delete_tables_ac.php?table_name=tbl_register_excel", true);
    xmlhttp.send();
}	
</script>
<?php
ini_set('max_input_vars',10000);
ini_set('max_post_vars',10000);
ini_set('max_request_vars',10000); 
?>
<?php if(isset($_SESSION['msg']) )
{?>
<div class="alert alert-info"><h4>Info!</h4><?php echo $_SESSION['msg'];unset($_SESSION['msg']);  ?></div> <?php }  ?>
<style>.pendency td{background:#e5e600 !important;}</style>            
<div class="head clearfix">
                        <div class="isw-grid"></div>
                        <h1>Excel Uploaded Bank A/c List</h1>                                                     
                    </div>
<div class="block-fluid">
<form method="POST" id="frm-example" enctype="multipart/form-data" action="" onsubmit="return get_submit();">      
<table id="example" class="table select" cellspacing="0" width="100%">
   <thead>
      <tr>
        <th><input type="checkbox" name="select_all" value="1" id="example-select-all"></th>      
        <th>Employee Name</th>
        <th>State</th>
        <th>Aadhar No</th>
        <?php
        $sql    = mysql_query("SELECT min(date),max(date) FROM `tbl_attendence_excel` WHERE 1 "); 
        $rows   = mysql_fetch_array($sql);
        for($k=$rows['min(date)'];$k<=$rows['max(date)'];)
        {
            echo "<th>$k</th>";   
            $k          = date('d M,Y', strtotime($k . ' +1 day'));         
        }
        ?>
      </tr>
   </thead>
   <tfoot>
   </tfoot>
</table>   
    <div class="row-form clearfix">  
    <div class="span1">
    <label>&nbsp;</label>
    <input type="submit" name="Update" value="Case Update" /></div>
    <div class="span1">
    <label>&nbsp;</label>
    <input type="button" name="Delete Table" value="Delete Ac List"  onclick="delete_tables();"/></div>
    </div>                        
</form>
<script type="text/javascript">
$(document).ready(function (){
   var table = $('#example').DataTable({
      'ajax': {
		  url:'pages/ajax.php',
      },
	  "pageLength": 50,
	  fixedHeader: {
            header: true,
            footer: true
        },
      'columnDefs': [{
         'targets': 0,
         'searchable': false,
         'orderable': false,
         'className': 'dt-body-center',
         'render': function (data, type, full, meta){
			// alert(data);
             return '<input type="checkbox" name="id[]" value="' + $('<div/>').text(data).html() + '">';
         }
      }],
      'order': [[1, 'asc']]
   });

	   
   // Handle click on "Select all" control
   $('#example-select-all').on('click', function(){
      // Get all rows with search applied
      var rows = table.rows({ 'search': 'applied' }).nodes();
      // Check/uncheck checkboxes for all rows in the table
      $('input[type="checkbox"]', rows).prop('checked', this.checked);
   });

   // Handle click on checkbox to set state of "Select all" control
   $('#example tbody').on('change', 'input[type="checkbox"]', function(){
      // If checkbox is not checked
      if(!this.checked){
         var el = $('#example-select-all').get(0);
         // If "Select all" control is checked and has 'indeterminate' property
         if(el && el.checked && ('indeterminate' in el)){
            // Set visual state of "Select all" control 
            // as 'indeterminate'
            el.indeterminate = true;
         }
      }
   });

   // Handle form submission event
   $('#frm-example').on('submit', function(e){
      var form = this;

      // Iterate over all checkboxes in the table
      table.$('input[type="checkbox"]').each(function(){
         // If checkbox doesn't exist in DOM
         if(!$.contains(document, this)){
            // If checkbox is checked
            if(this.checked){
               // Create a hidden element 
               $(form).append(
                  $('<input>')
                     .attr('type', 'hidden')
                     .attr('name', this.name)
                     .val(this.value)
               );
            }
         } 
      });
   });
 }); 
</script>
</div>
<?php
if(isset($_POST['Update']))
{
	$post_id					= implode("','",$_POST['id']);
	$qry_1						= mysql_query(" SELECT distinct `cust_id` FROM `tbl_register_excel` where `delete`='0' and `id` IN('$post_id') ")or die(mysql_error());
	while($row_1				= mysql_fetch_array($qry_1))
	{
		$cust_id				= $row_1['cust_id'];
		$qry_2					= mysql_query(" SELECT `login_id`,`bank_id`,`branch_id`,`enf_agency`,`borrower_name`,`cust_id`,`segment_name`,`advance_type`,`borrower_address`,`mobile_no`,`city`,`tehsil`,`distict`,`state`,`act`,`status`,`allocatedon`,`total_dues`,`total_dues_date`,`ledger_amount`,`ledger_date`,`amount_guarantor`,`date_guarantor`,`date_of_npa`,`b_status`,`account_no`,`case_dt` FROM `tbl_register_excel` where `delete`='0' and `id` IN('$post_id') and `cust_id`='$cust_id' ")or die(mysql_error());
		$i						= 0;
		while($row_2			= mysql_fetch_array($qry_2))
		{
			$login_id			= $row_2['login_id'];
			$bank_id			= $row_2['bank_id'];
			$branch_id			= $row_2['branch_id'];
			$enf_agency			= $row_2['enf_agency'];
			$borrower_name		= $row_2['borrower_name'];
			$cust_id			= $row_2['cust_id'];
			$segment_name		= $row_2['segment_name'];
			$advance_type		= $row_2['advance_type'];
			$borrower_address	= $row_2['borrower_address'];
			$mobile_no			= $row_2['mobile_no'];
			$city				= $row_2['city'];
			$tehsil				= $row_2['tehsil'];
			$distict			= $row_2['distict'];
			$state				= $row_2['state'];
			$act				= $row_2['act'];
			$status				= $row_2['status'];
			$case_dt			= date("Y-m-d",strtotime(mysql_real_escape_string($row_2['case_dt'])));
			if(($case_dt=='1970-01-01') || ($case_dt=='1969-12-31') || ($case_dt=='0000-00-00') )
			{
				$case_dt	 	= "";
			}
			$allocatedon		= date("Y-m-d",strtotime(mysql_real_escape_string($row_2['allocatedon'])));
			if(($allocatedon=='1970-01-01') || ($allocatedon=='1969-12-31') || ($allocatedon=='0000-00-00') )
			{
				$allocatedon 	= "";
			}
			$total_dues			= $row_2['total_dues'];
			$total_dues_date	= date("Y-m-d",strtotime(mysql_real_escape_string($row_2['total_dues_date'])));
			if(($total_dues_date=='1970-01-01') || ($total_dues_date=='1969-12-31') || ($total_dues_date=='0000-00-00') )
			{
				$total_dues_date= "";
			}
			$ledger_amount		= $row_2['ledger_amount'];
			$ledger_date		= date("Y-m-d",strtotime(mysql_real_escape_string($row_2['ledger_date'])));
			if(($ledger_date=='1970-01-01') || ($ledger_date=='1969-12-31') || ($ledger_date=='0000-00-00') )
			{
				$ledger_date = "";
			}
			$amount_guarantor	= $row_2['amount_guarantor'];
			$date_guarantor		= date("Y-m-d",strtotime(mysql_real_escape_string($row_2['date_guarantor'])));
			if(($date_guarantor=='1970-01-01') || ($date_guarantor=='1969-12-31') || ($date_guarantor=='0000-00-00') )
			{
				$date_guarantor = "";
			}
			$date_of_npa		= date("Y-m-d",strtotime(mysql_real_escape_string($row_2['date_of_npa'])));
			if(($date_of_npa=='1970-01-01') || ($date_of_npa=='1969-12-31') || ($date_of_npa=='0000-00-00') )
			{
				$date_of_npa    = "";
			}
			$b_status			= $row_2['b_status'];
			$account_no			= $row_2['account_no'];
			if($i==0)
			{
				/////////////////Case NO
				$state_qry        			= mysql_query(" SELECT * FROM `state` where `id`='$state' ");
				$state_data		   			= mysql_fetch_array($state_qry);	
				$case_qry          			= mysql_query(" SELECT max(`id`) FROM `tbl_register_case` where `case_no` like '".$state_data['state_code']."-%' ");				
				if(mysql_num_rows($case_qry)>0)
				{
					$case_qry_data	   		= mysql_fetch_array($case_qry);							
					$case_data		   		= mysql_fetch_array(mysql_query(" SELECT `case_no` FROM `tbl_register_case` where `id`='".$case_qry_data['max(`id`)']."'"));					
					$no			       		= substr($case_data['case_no'],3)+1;
					$case_no		   		= $state_data['state_code']."-".$no;
				}
				else
				{
					$case_no				= $state_data['state_code']."-1";					
				}
				////Account Information			
				$qry_3				= mysql_query(" insert into `tbl_register_case` set `allocatedon`='$allocatedon', `act`='$act', `case_no`='$case_no', `enf_agency`='$enf_agency', `login_id`='$login_id', `bank_id`='$bank_id', `branch_id`='$branch_id', `r_branch_id`='$branch_id', `status`='$status', `borrower_name`='$borrower_name', `cust_id`='$cust_id', `mobile_no`='$mobile_no', `borrower_address`='$borrower_address', `state`='$state', `distict`='$distict', `tehsil`='$tehsil', `city_name`='$city',`case_dt`='$case_dt', `delete`='0' ")or die(mysql_error());
				$case_id			= mysql_insert_id();
			}
			//////Account Information
			$qry_4			    = mysql_query(" insert into `tbl_account_list` set `case_id`='$case_id', `account_no`='$account_no', `b_status`='$b_status', `segment_name`='$segment_name', `advance_type`='$advance_type', `amount_guarantor`='$amount_guarantor', `date_guarantor`='$date_guarantor', `ledger_amount`='$ledger_amount', `ledger_date`='$ledger_date', `total_dues`='$total_dues', `total_dues_date`='$total_dues_date' ")or die(mysql_error());
			//////Act Details		
			$qry_5			    = mysql_query(" insert into `tbl_act` set `case_id`='$case_id' ")or die(mysql_error());
	
		$i++;
		}
	}
	$qry_5						= mysql_query(" TRUNCATE `tbl_register_excel` ")or die(mysql_error()); 
	$_SESSION['msg']			= " Successfully Insert A/c !!!!!!!!";
	header("location:home.php?page=multiple_ac_list"); 	
}
?>