<?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'];
// Common Information

$page_title="Supplier Information";
$page="51_supplier_add.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='accounts_reference';	
$unique='ref_no';

$request_id=$_REQUEST['ref_no'];

$ms_data=mysqli_fetch_object(mysqli_query($conn,'select * from '.$table.' where ref_no="'.$request_id.'"'));

// End Common Information

if(isset($_POST['insert']))

{		

$entry_by = $_SESSION['user']['id'];
		 
$entry_at = $now=date('Y-m-d H:i:s');

$ref_type =3;

$cy_id=mysqli_fetch_object(mysqli_query($conn,'select max(ref_id) as ref_id from '.$table.' where ref_type="'.$ref_type.'"'));
$ref_sl_id=$cy_id->ref_id+1;
$ref_sl_no = sprintf("%05d", $ref_sl_id);

$ref_no = $ref_type.''.$ref_sl_no;

$full_name=$_POST['title_name'].' '.$_POST['first_name'].' '.$_POST['last_name'];

$display_name=$_POST['title_name'].' '.$_POST['first_name'].' '.$_POST['last_name'].' - [Code: '.$ref_no.']';

//$grp_sql="SELECT * FROM ledger_group  WHERE group_id='".$ledger_group_id."'";
//$grp_data = find_all_field_sql($conn,$grp_sql);


$ins_sql = 'INSERT INTO  accounts_reference (ref_no, ref_id, ref_type, title_name, first_name, last_name, full_name, phone_no, mobile_no, fax_no, email, company_name, ledger_id, emp_code, address, city, state, postal_code, country, entry_by, entry_at, display_name) VALUES
("'.$ref_no.'", "'.$ref_sl_no.'", "'.$ref_type.'", "'.$_POST['title_name'].'", "'.$_POST['first_name'].'", "'.$_POST['last_name'].'", "'.$full_name.'", "'.$_POST['phone_no'].'", "'.$_POST['mobile_no'].'", "'.$_POST['fax_no'].'", "'.$_POST['email'].'", "'.$_POST['company_name'].'", "'.$_POST['ledger_id'].'", "'.$_POST['emp_code'].'", "'.$_POST['address'].'", "'.$_POST['city'].'", "'.$_POST['state'].'", "'.$_POST['postal_code'].'", "'.$_POST['country'].'", "'.$entry_by.'", "'.$entry_at.'", "'.$display_name.'")';
	
mysqli_query($conn,$ins_sql);

?>

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

<?php

}



if(isset($_POST['update']))

{		

$entry_by = $_SESSION['user']['id'];
		 
$entry_at = $now=date('Y-m-d H:i:s');

$ref_no=$request_id;

$full_name=$_POST['title_name'].' '.$_POST['first_name'].' '.$_POST['last_name'];
$display_name=$_POST['title_name'].' '.$_POST['first_name'].' '.$_POST['last_name'].' - [Code: '.$ref_no.']';

 $up_sql = 'update accounts_reference set title_name="'.$_POST['title_name'].'", first_name="'.$_POST['first_name'].'", last_name="'.$_POST['last_name'].'", full_name="'.$full_name.'", 
phone_no="'.$_POST['phone_no'].'", mobile_no="'.$_POST['mobile_no'].'", fax_no="'.$_POST['fax_no'].'", email="'.$_POST['email'].'", company_name="'.$_POST['company_name'].'", emp_code="'.$_POST['emp_code'].'", address="'.$_POST['address'].'", city="'.$_POST['city'].'", state="'.$_POST['state'].'", postal_code="'.$_POST['postal_code'].'", country="'.$_POST['country'].'",  edit_by="'.$entry_by.'", 
edit_at="'.$entry_at.'" , display_name="'.$display_name.'" 
 where ref_no='.$ref_no;
mysqli_query($conn,$up_sql);


?>

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

<?php

}

?>






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

<!-- ======= <head> ======= -->
 <?php require_once('inc_files/inc_head.php'); ?>
<!-- ======= </head> ======= -->

<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">Purchase Management</li>
          <li class="breadcrumb-item active"><?php echo $page_title?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

   

    <section class="section">
      <div class="row">
        <div class="col-lg-5"> 

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

              <!-- Table with stripped rows -->
              <table class="table datatable" style="font-size:14px;">
                <thead>
                  <tr>
                    <th scope="col"><span class="card-title"> Code </span></th>
                    <th scope="col"><span class="card-title">Supplier Name </span></th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
						$sql = 'select * from '.$table.' where ref_type=3';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                 <tr>
       <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->ref_no;?>" style="color:#000000"><?php echo $data->ref_no;?></a></td>
      <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->ref_no;?>" style="color:#000000"><?php echo $data->full_name;?></a></td>
 </tr>
				  <?php }?>
                </tbody>
              </table>
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        <div class="col-lg-7">

          <div class="card">
            <div class="card-body">
              <h5 class="card-title"><strong><?php echo $page_title;?></strong></h5>
			  
			

              <!-- 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-2">
                 <label for="title_name" class="form-label"><strong>Title</strong></label>
                  <input type="text" class="form-control" id="title_name" name="title_name" value="<?php if($request_id>0){ echo $ms_data->title_name; } ?>" >
                </div>
                <div class="col-md-5">
                 <label for="first_name" class="form-label"><strong>First Name</strong></label>
                  <input type="text" class="form-control" id="first_name" name="first_name" value="<?php if($request_id>0){ echo $ms_data->first_name; } ?>" >
                </div>
				
				<div class="col-md-5">
                 <label for="last_name" class="form-label"><strong>Last Name</strong></label>
                  <input type="text" class="form-control" id="last_name" name="last_name" value="<?php if($request_id>0){ echo $ms_data->last_name; } ?>" >
                </div>
			  
			 <div class="col-md-4">
                 <label for="phone_no" class="form-label"><strong>Phone</strong></label>
                  <input type="text" class="form-control" id="phone_no" name="phone_no" value="<?php if($request_id>0){ echo $ms_data->phone_no; } ?>" >
                </div>
                <div class="col-md-4">
                 <label for="mobile_no" class="form-label"><strong>Mobile</strong></label>
                  <input type="text" class="form-control" id="mobile_no" name="mobile_no" value="<?php if($request_id>0){ echo $ms_data->mobile_no; } ?>" >
                </div>
				
				<div class="col-md-4">
                 <label for="fax_no" class="form-label"><strong>Fax</strong></label>
                  <input type="text" class="form-control" id="fax_no" name="fax_no" value="<?php if($request_id>0){ echo $ms_data->fax_no; } ?>" >
                </div>
			   
				<div class="col-md-12">
                 
                </div>
			  
                <div class="row mb-3" >
                  <label for="email" class="col-sm-3 col-form-label"><strong>Email</strong></label>
                  <div class="col-sm-9">
                    <input type="text" class="form-control" id="email" name="email" value="<?php if($request_id>0){ echo $ms_data->email; } ?>" required>
                  </div>
                </div>
				
				<div class="row mb-3">
                  <label for="company_name" class="col-sm-3 col-form-label"><strong>Company</strong></label>
                  <div class="col-sm-9">
                    <input type="text" class="form-control" id="company_name" name="company_name" value="<?php if($request_id>0){ echo $ms_data->company_name; } ?>" required>
                  </div>
                </div>
				
				
				<div class="row mb-3">
                  <label for="ledger_id" class="col-sm-3 col-form-label"><strong>GL Config</strong></label>
                  <div class="col-sm-9">
                    <?php if($request_id>0){?>  
                    
			<select id="ledger_id" name="ledger_id"  class="form-select"  required  >
			
			<?php foreign_relation($conn,'accounts_ledger','ledger_id','ledger_name',$_POST['ledger_id'],'ledger_id="'.$ms_data->ledger_id.'" order by ledger_id');?>
		
        	</select>
			
			<?php } else {?>
			
			<select id="ledger_id" name="ledger_id"  class="form-select"  required  >
			
			<option></option>
			
			<?php foreign_relation($conn,'accounts_ledger','ledger_id','ledger_name',$_POST['ledger_id'],'ledger_group_id=22001 order by ledger_id');?>
		
        	</select>
			<?php }?>	
                  </div>
                </div>
				
				
				
				
				
				
				<div class="row mb-3">
                  <label for="emp_code" class="col-sm-3 col-form-label"><strong>Employee</strong></label>
                  <div class="col-sm-9">
              <?php if($request_id>0){?>    
			<select id="emp_code" name="emp_code"  class="form-select"  required  >
			
			<?php foreign_relation($conn,'employee_info','emp_code','emp_name',$_POST['emp_code'],'emp_code="'.$ms_data->emp_code.'" order by emp_code');?>
		
        	</select>
				<?php } else {?>
				
			<select id="emp_code" name="emp_code"  class="form-select"  required  >
			
			<option></option>
		
			<?php foreign_relation($conn,'employee_info','emp_code','emp_name',$_POST['emp_code'],'1 order by emp_code');?>
		
        	</select>	
				
				<?php }?>
				
					
                  </div>
                </div>
				
				
				<div class="row mb-3">
                  <label for="address" class="col-sm-3 col-form-label"><strong>Address</strong></label>
                  <div class="col-sm-9">
                 <textarea class="form-control" style="height: 80px" id="address" name="address" ><?php if($request_id>0){ echo $ms_data->address; } ?></textarea>	
                  </div>
                </div>
				
			
				
				<div class="col-md-6">
                 <label for="city" class="form-label"><strong>City/Town</strong></label>
                  <input type="text" class="form-control" id="city" name="city" value="<?php if($request_id>0){ echo $ms_data->city; } ?>" >
                </div>
                <div class="col-md-6">
                 <label for="state" class="form-label"><strong>State/Province</strong></label>
                  <input type="text" class="form-control" id="state" name="state" value="<?php if($request_id>0){ echo $ms_data->state; } ?>" >
                </div>
				
				
				
				<div class="col-md-6">
                 <label for="postal_code" class="form-label"><strong>Postal Code</strong></label>
                  <input type="text" class="form-control" id="postal_code" name="postal_code" value="<?php if($request_id>0){ echo $ms_data->postal_code; } ?>">
                </div>
                <div class="col-md-6">
                 <label for="country" class="form-label"><strong>Country</strong></label>
                  <input type="text" class="form-control" id="country" name="country" value="<?php if($request_id>0){ echo $ms_data->country; } ?>">
                </div>
				
				
				
				<br>
				<br>
				<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" id="insert" ><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>
      </div>
    </section>

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

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

</body>

</html>