<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');

$active_id=$_REQUEST['active_id'];
$menu_id=$_REQUEST['menu_id'];

$data_found=$_REQUEST['emp_code'];
$tr_no=$_REQUEST['tr_no'];
// Common Information

$page_title="Employee Advance Payment";
$page="71_employee_05.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='employee_advance';	
$unique='id';

// End Common Information


if(isset($_POST['run_data']))
{
$employee_name = $_REQUEST['employee_name'];
$employee=explode(' :: ',$employee_name);
$emp_code=$employee[0];
?>

<script language="javascript">
window.location.href = "<?php echo $page;?>&emp_code=<?=$emp_code?>";
</script>

<?php }

if(isset($_POST['insert_data']))
{
$entry_by = $_SESSION['user']['id'];
$entry_at = date('Y-m-d H:i:s');
$ledger_id = $_POST['ledger_id'];
$emp_code = $_POST['emp_code'];
$tr_date = $_POST['tr_date'];
$advance_amt = $_POST['advance_amt'];
$monthly_amt = $_POST['monthly_amt'];
$start_mon = $_POST['start_mon'];
$year = date('Y',strtotime($start_mon));
$mon = date('m',strtotime($start_mon));
$total_installment = $advance_amt/$monthly_amt;
$jv_special=1;
$jv_type=2;
$jv_title=find_a_field($conn,'journal_type','jv_title','id="'.$jv_type.'"');
$jv_date=$tr_date;
$group_for = $_SESSION['user']['group'];
$status='MANUAL';

		$YR = date('Y',strtotime($jv_date));
 		$year = date('y',strtotime($jv_date));
  		$month = date('m',strtotime($jv_date));
 		$inv_cy_id = find_a_field($conn,'journal_master','max(jv_id)','year="'.$YR.'" and jv_type="'.$jv_type.'"')+1;
  		$cy_id = sprintf("%06d", $inv_cy_id);
   		$invoice_no=''.$year.''.$month.''.$jv_type.''.$cy_id;
		$view_jv_no=$jv_title.''.$year.''.$month.''.$jv_type.''.$cy_id;
		$jv_no=$invoice_no;
		
$master_ledger_id=$ledger_id;
$tr_from='Payment';

$ins_sql_ms = 'INSERT INTO  journal_master (jv_no, year, jv_id, view_jv_no, jv_type, jv_date, group_for, ref_no, master_ledger_id, tr_from, remarks, status, entry_at, entry_by, jv_special) VALUES

("'.$invoice_no.'", "'.$YR.'", "'.$cy_id.'", "'.$view_jv_no.'", "'.$jv_type.'", "'.$jv_date.'", "'.$group_for.'", "'.$emp_code.'",  "'.$master_ledger_id.'", "'.$tr_from.'", "'.$remarks.'", "'.$status.'", "'.$entry_at.'", "'.$entry_by.'", "'.$jv_special.'")';
	
mysqli_query($conn,$ins_sql_ms);




for($i=0;$i<$total_installment;$i++)
{
$smon=$mon+$i;
$syear=$year;
$current_mon = date('m',mktime(1,1,1,$smon,1,$syear));
$current_year = date('Y',mktime(1,1,1,$smon,1,$syear));
$installment_no = $i+1;

 $ins_sql = 'INSERT INTO  employee_advance (emp_code, ledger_id, tr_no, tr_date, start_mon, installment_no, mon, year, advance_amt, monthly_amt, entry_at, entry_by) VALUES

("'.$emp_code.'",  "'.$ledger_id.'", "'.$jv_no.'", "'.$tr_date.'", "'.$start_mon.'", "'.$installment_no.'", "'.$current_mon.'", "'.$current_year.'", "'.$advance_amt.'", "'.$monthly_amt.'", "'.$entry_at.'", "'.$entry_by.'")';
	
mysqli_query($conn,$ins_sql);
}



if($emp_code>0) {
auto_insert_salary_advance_secoundary_journal($conn,$jv_no);
}

?>

<script language="javascript">
window.location.href = "<?php echo $page;?>&tr_no=<?php echo $jv_no;?>";
</script>

<?php }?>

<!DOCTYPE html>
<html lang="en">

 <?php require_once('inc_files/inc_head.php'); ?>
 
 <?php 
 do_calander('#tr_date'); 
 do_calander('#start_mon'); ?>
 
 
 <script>



function getXMLHTTP() { //fuction to return the xml http object



		var xmlhttp=false;	



		try{



			xmlhttp=new XMLHttpRequest();



		}



		catch(e)	{		



			try{			



				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");

			}

			catch(e){



				try{



				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");



				}



				catch(e1){



					xmlhttp=false;



				}



			}



		}



		 	



		return xmlhttp;



    }



	function update_value(id)
	{

var emp_code=id; // Rent
var att_date=(document.getElementById('att_date').value); 

var hour=(document.getElementById('hour_'+id).value);

var minute=(document.getElementById('minute_'+id).value); 

var flag=(document.getElementById('flag_'+id).value); 

var strURL="71_employee_04_ajax.php?emp_code="+emp_code+"&att_date="+att_date+"&hour="+hour+"&minute="+minute+"&flag="+flag;



		var req = getXMLHTTP();



		if (req) {



			req.onreadystatechange = function() {

				if (req.readyState == 4) {

					// only if "OK"

					if (req.status == 200) {						

						document.getElementById('divi_'+id).style.display='inline';

						document.getElementById('divi_'+id).innerHTML=req.responseText;						

					} else {

						alert("There was a problem while using XMLHTTP:\n" + req.statusText);

					}

				}				

			}

			req.open("GET", strURL, true);

			req.send(null);

		}	



}




</script>

<body>

  <!-- ======= Header ======= -->
  <?php 
  require_once('inc_files/inc_header.php');
  ?><!-- End Header -->

  <!-- ======= Sidebar ======= -->
  <?php  require_once('inc_files/inc_menu.php'); ?><!-- End Sidebar-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1><?php echo $page_title?></h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="home.php">Home</a></li>
          <li class="breadcrumb-item">HR Management</li>
          <li class="breadcrumb-item active"><?php echo $page_title?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

    <section class="section">
      <div class="row">
<?php if($data_found=="" && $tr_no=="") {?>
<?php 
   $emp_sql="SELECT * FROM employee_info  WHERE emp_code='".$_REQUEST['emp_code']."'";
   $emp_data = find_all_field_sql($conn,$emp_sql);
   ?>
        <div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
			<br>

 <form class="row g-3" action="" method="post" name="codz" id="codz">		
	<table width="80%" border="0" align="center">
        <tr height="50">
          <td width="14%" align="right"><span style="color:#FF0000;"><strong>Employee Name:</strong></span></td>
          <td width="15%" align="left">
		  <?php 
		   auto_complete_from_db_sql($conn,'SELECT concat(emp_code," :: ",emp_name), concat(emp_code," :: ",emp_name) FROM employee_info
			WHERE 1 order by emp_code','employee_name');	
			?>
       <input type="text" class="form-control" id="employee_name" name="employee_name" value="<?php echo $_REQUEST['employee_name']?>" style="width:100%" >	  </td>
          <td width="15%" rowspan="5" align="center" ><strong>
            &nbsp;&nbsp;&nbsp;
            <button type="submit" name="run_data" id="run_data" class="btn btn-success rounded-pill btn-sm">&nbsp;<i class="bi bi-filter me-1"></i> View Data&nbsp;&nbsp;</button>
          </strong></td>
        </tr>
    </table>
               
  </form>
  <br>
 

            </div>
          </div>
        </div>
		
		 <?php }?>

     <section class="section profile">
      <div class="row">
	  
	<?php 
    $emp_sql="SELECT * FROM employee_info  WHERE emp_code='".$_REQUEST['emp_code']."'";
   $emp_data = find_all_field_sql($conn,$emp_sql);
   ?>
  <?php if($data_found>0) {?>

        <div class="col-xl-9">

          <div class="card">
            <div class="card-body">
            <br>
              <!-- Multi Columns Form -->
              <form class="row g-3" action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:14px;">
			  
			  <div class="col-md-4">
                 <label for="title_name" class="form-label"><strong>EMP Code</strong></label>
                  <input type="text" class="form-control" id="emp_code" name="emp_code" value="<?php echo $emp_data->emp_code; ?>" readonly="" required >
                </div>
                <div class="col-md-8">
                 <label for="first_name" class="form-label"><strong>Employee Name</strong></label>
                  <input type="text" class="form-control" id="first_name" name="first_name" value="<?php echo $emp_data->emp_name; ?>" readonly="" required >
                </div>
			
			  
			 	<div class="col-md-4">
                 <label for="mobile_no" class="form-label"><strong>Advance Taken Date</strong></label>
                  <input type="text" class="form-control" id="tr_date" name="tr_date" value="" required >
                </div>
                <div class="col-md-4">
                 <label for="alternative_mobile" class="form-label"><strong>Advance Amount</strong></label>
                  <input type="text" class="form-control" id="advance_amt" name="advance_amt" value="" required >
                </div>
				
				<div class="col-md-4">
                 <label for="email" class="form-label"><strong>Monthly Installment Amt </strong></label>
                  <input type="text" class="form-control" id="monthly_amt" name="monthly_amt" value="" required >
                </div>
				
				<div class="col-md-4">
                 <label for="father_name" class="form-label"><strong>Start Month</strong></label>
                  <input type="text" class="form-control" id="start_mon" name="start_mon" value="" required >
                </div>
                <div class="col-md-8">
                 <label for="mother_name" class="form-label"><strong><span style="color:#FF0000;">Cash/Bank</span></strong></label>
                 <select id="ledger_id" name="ledger_id"  class="form-select"   required>
                      <option></option>
 <?php foreign_relation($conn,'accounts_ledger','ledger_id','ledger_name',$ledger_id, 'ledger_type!="General" group by ledger_id');?>
                    </select>
                </div>
				
			
				<br>
			
                
                <div class="text-center">
				<?php if($request_id>0) {?>
                  <button type="submit" class="btn btn-success btn-sm" name="update" id="update" ><i class="bi bi-check-all"></i> Update &nbsp;</button>
				 <?php }else {?>
				  <button type="submit" class="btn btn-primary btn-sm" name="insert_data" id="insert_data" ><i class="bi bi-plus"></i> Save &amp; New &nbsp;</button>
				 <?php }?>
                  <button type="reset" class="btn btn-secondary btn-sm" onClick="parent.location='<?php echo $page?>'" >Reset</button>
                </div>
              </form><!-- End Multi Columns Form -->

            </div>
          </div>

        </div>
		<?php }?>
		
		
		  <?php if($data_found>0) {?>
        <div class="col-xl-3">

          <div class="card"> 
		  
            <div class="card-body profile-card pt-4 d-flex flex-column align-items-center">
              <img src="uploads/<?php echo $data_found?>.jpg" alt="Profile" class="rounded-circle" style="border:1px solid #999999;">
              
			  
			   <h2><?php echo $emp_data->emp_name?></h2>
              <h3>EMP Code: <?php echo $emp_data->emp_code?></h3>
			 
            </div>
			
			
          </div>
        </div>
		<?php }?>
		
		
      </div>
    </section>
	
	 <?php if($tr_no>0) {?>
	
	<section class="section profile">
      <div class="row">
	  
	<?php 
   $adv_sql="SELECT * FROM employee_advance  WHERE tr_no='".$tr_no."'";
   $adv_data = find_all_field_sql($conn,$adv_sql);
   $emp_sql="SELECT * FROM employee_info  WHERE emp_code='".$adv_data->emp_code."'";
   $emp_data = find_all_field_sql($conn,$emp_sql);
   ?>
  

        <div class="col-xl-9">

          <div class="card">
            <div class="card-body">
            <br>
              <!-- Multi Columns Form -->
              <form class="row g-3" action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:14px;">
			  
			  <div class="col-md-4">
                 <label for="title_name" class="form-label"><strong>EMP Code</strong></label>
                  <input type="text" class="form-control" id="emp_code" name="emp_code" value="<?php echo $adv_data->emp_code; ?>" readonly="" required >
                </div>
                <div class="col-md-8">
                 <label for="first_name" class="form-label"><strong>Employee Name</strong></label>
                  <input type="text" class="form-control" id="first_name" name="first_name" value="<?php echo $emp_data->emp_name; ?>" readonly="" required >
                </div>
			 	<div class="col-md-4">
                 <label for="mobile_no" class="form-label"><strong>Advance Taken Date</strong></label>
                  <input type="text" class="form-control" id="tr_date2" name="tr_date2" value="<?php echo $adv_data->tr_date; ?>" required readonly="" >
                </div>
                <div class="col-md-4">
                 <label for="alternative_mobile" class="form-label"><strong>Advance Amount</strong></label>
                  <input type="text" class="form-control" id="advance_amt" name="advance_amt" value="<?php echo $adv_data->advance_amt; ?>" required readonly="" >
                </div>
				
				<div class="col-md-4">
                 <label for="email" class="form-label"><strong>Monthly Installment Amt </strong></label>
                  <input type="text" class="form-control" id="monthly_amt" name="monthly_amt" value="<?php echo $adv_data->monthly_amt; ?>" required readonly="" >
                </div>
				
				<div class="col-md-4">
                 <label for="father_name" class="form-label"><strong>Start Month</strong></label>
                  <input type="text" class="form-control" id="start_mon2" name="start_mon2" value="<?php echo $adv_data->start_mon; ?>" required readonly="" >
                </div>
                <div class="col-md-8">
                 <label for="mother_name" class="form-label"><strong><span style="color:#FF0000;">Cash/Bank</span></strong></label>
                 <select id="ledger_id" name="ledger_id"  class="form-select"   required>
                     
 <?php foreign_relation($conn,'accounts_ledger','ledger_id','ledger_name',$ledger_id, 'ledger_id="'.$adv_data->ledger_id.'" group by ledger_id');?>
                    </select>
                </div>
				
			
				<br>
			<div class="col-md-12">
			<table width="100%" class="table table-sm" style="font-size:12px;">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th width="17%" scope="col">TR No </th>
                    <th width="21%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;"> installment no </span></th>
                    <th width="21%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Month</span></th>
                    <th width="20%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">Year</span></th>
                    <th width="21%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;" >installment Amount </span></th>
                    </tr>
                </thead>
                <tbody>
		<?php
	

	 
		
		 $sql = "select month_id, month_name from month where 1 group by month_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
		 $month_name[$info->month_id]=$info->month_name;
		 
		}
		
		
			
					     $sql = 'select * from '.$table.' where tr_no="'.$tr_no.'" ';
						$query = mysqli_query($conn,$sql);
						while ($inst_data = mysqli_fetch_object($query)) {

					?>
		
      <tr>
      <td><?php echo $inst_data->tr_no;?></td>
      <td><?php echo $inst_data->installment_no;?></td>
      <td><?php echo $month_name[$inst_data->mon];?></td>
      <td><?php echo $inst_data->year;?></td>
	  <td><?php echo $inst_data->monthly_amt;?></td>
      </tr>
				  <?php }?>
                </tbody>
              </table>
			</div>
                
                
              </form><!-- End Multi Columns Form -->

            </div>
          </div>

        </div>
		
		
		
		 
        <div class="col-xl-3">

          <div class="card"> 
		  
            <div class="card-body profile-card pt-4 d-flex flex-column align-items-center">
              <img src="uploads/<?php echo $emp_data->emp_code?>.jpg" alt="Profile" class="rounded-circle" style="border:1px solid #999999;">
              
			  
			   <h2><?php echo $emp_data->emp_name?></h2>
              <h3>EMP Code: <?php echo $emp_data->emp_code?></h3>
			 
            </div>
			
			
          </div>
        </div>
		
		
		
      </div>
    </section>
		<?php }?>
		
      </div>
    </section>

  </main><!-- End #main -->

  <!-- ======= Footer ======= -->
  <?php require_once('inc_files/inc_footer.php'); ?>
 <!-- ======= Footer ======= -->

</body>

</html>