<?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="Customer Management";
$page="41_customer_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 =2;

$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['company_name'];

//$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, customer_type, zone, area, vat_no, nid_no, tin_no) 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.'", "'.$_POST['customer_type'].'", "'.$_POST['zone'].'", "'.$_POST['area'].'", "'.$_POST['vat_no'].'", "'.$_POST['nid_no'].'" ,
 "'.$_POST['tin_no'].'")';
	
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['company_name'];

  $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.'", zone="'.$_POST['zone'].'",
 area="'.$_POST['area'].'", customer_type="'.$_POST['customer_type'].'", vat_no="'.$_POST['vat_no'].'", nid_no="'.$_POST['nid_no'].'", tin_no="'.$_POST['tin_no'].'"
 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">Sales 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-7"> 

          <div class="card">
            <div class="card-body">
            <h5 class="card-title"><strong>Customer List</strong></h5>
              

              <!-- Table with stripped rows -->
              <table class="table" id="data_table"style="font-size:12px;">
                <thead>
                  <tr>
                    <th scope="col"><div align="left"><span class="card-title"> Code </span></div></th>
                    <th scope="col"><span class="card-title">Customer  </span></th>
                    <th scope="col" style="white-space:nowrap"><span class="card-title">Proprietor  Name</span></th>
                    <th scope="col"><span class="card-title">Mobile   </span></th>
                    <th scope="col"><span class="card-title">Action </span></th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
						$sql = 'select * from '.$table.' where ref_type=2';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                 <tr>
       <td><div align="left"><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->ref_no;?>" style="color:#000000"><?php echo $data->ref_no;?></a></div></td>
      <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->ref_no;?>" style="color:#000000"><?php echo $data->display_name;?></a></td>
      <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->ref_no;?>" style="color:#000000"><?php echo $data->proprietor_name;?></a></td>
                 <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->ref_no;?>" style="color:#000000"><?php echo $data->mobile_no;?></a></td>
                 <td>
				 <a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->ref_no;?>"  style="color: #FFFFFF">
				  <button type="submit" class="btn btn-danger btn-sm" name="edit" id="edit" ><i class="bi bi-pen"></i> Edit &nbsp;</button></a>
				 </td>
                 </tr>
				  <?php }?>
                </tbody>
              </table>
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

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

          <div class="card">
            <div class="card-body">
              <h5 class="card-title"><strong>Add / Edit Customer</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-6">
                 <label for="first_name" class="form-label"><strong>Customer Code</strong></label>
                  <input type="text" placeholder="Customer Code" class="form-control" id="ref_no_view" name="ref_no_view" value="<?php if($request_id>0){ echo $ms_data->ref_no_view; } ?>" >
                </div>
				
				<div class="col-md-6">
                 <label for="mobile_no" class="form-label"><strong>Customer Type</strong></label>
            <select id="customer_type" name="customer_type"  class="form-select"  >
			
			<option></option>
			
			<?php foreign_relation($conn,'customer_type','id','customer_type',$ms_data->customer_type,'1 order by id');?>
		
        	</select>
                </div>
			  
			  
                
				
				<div class="col-md-12">
                 <label for="last_name" class="form-label"><strong>Customer Name</strong></label>
                  <input type="text" placeholder="Customer Name"  class="form-control" id="display_name" name="display_name" value="<?php if($request_id>0){ echo $ms_data->display_name; } ?>" >
                </div>
				
				<div class="col-md-12">
                 <label for="last_name" class="form-label"><strong>Proprietor Name</strong></label>
                  <input type="text" placeholder="Proprietor Name"  class="form-control" id="proprietor_name" name="proprietor_name" value="<?php if($request_id>0){ echo $ms_data->proprietor_name; } ?>" >
                </div>
			  
			 
                <div class="col-md-6">
                 <label for="mobile_no" class="form-label"><strong>Mobile</strong></label>
                  <input type="text" placeholder="Mobile"  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-6">
                 <label for="fax_no" class="form-label"><strong>Email</strong></label>
                  <input type="text" placeholder="Email"  class="form-control" id="email" name="email" value="<?php if($request_id>0){ echo $ms_data->email; } ?>" >
                </div>
			   
				
				
				
				
				
				
				<div class="col-md-6">
                 <label for="phone_no" class="form-label"><strong>BIN No</strong></label>
          <input type="text" placeholder="BIN No"  class="form-control" id="vat_no" name="vat_no" value="<?php if($request_id>0){ echo $ms_data->vat_no; } ?>" >
				 
                </div>
               <div class="col-md-6">
                 <label for="phone_no" class="form-label"><strong>TIN No</strong></label>
          
			
			   <input type="text" placeholder="TIN No"  class="form-control" id="tin_no" name="tin_no" value="<?php if($request_id>0){ echo $ms_data->tin_no; } ?>" >
				 
                </div>
				
				
				<div class="col-md-6">
                 <label for="mobile_no" class="form-label"><strong>NID No</strong></label>
                  <input type="text" placeholder="NID No"  class="form-control" id="nid_no" name="nid_no" value="<?php if($request_id>0){ echo $ms_data->nid_no; } ?>" >
                </div>
				
				<div class="col-md-6">
                 <label for="mobile_no" class="form-label"><strong>GL Config</strong></label>
                  <?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=11003 order by ledger_id');?>
		
        	</select>
			<?php }?>	
                </div>
				
				
				<div class="col-md-4">
                 <label for="mobile_no" class="form-label"><strong>Territory</strong></label>
                  <input type="text" placeholder="Territory"  class="form-control" id="territory" name="territory" value="<?php if($request_id>0){ echo $ms_data->territory; } ?>" >
                </div>
				
				 <div class="col-md-4">
                 <label for="mobile_no" class="form-label"><strong>Area </strong></label>
                  <input type="text" placeholder="Area"   class="form-control" id="area" name="area" value="<?php if($request_id>0){ echo $ms_data->area; } ?>" >
                </div>
				
				<div class="col-md-4">
                 <label for="phone_no" class="form-label"><strong>Zone</strong></label>
          
			
			   <input type="text" placeholder="Zone"  class="form-control" id="zone" name="zone" value="<?php if($request_id>0){ echo $ms_data->zone; } ?>" >
				 
                </div>
               
				
				<div class="col-md-12">
                 <label for="last_name" class="form-label"><strong>Address</strong></label>
                  <textarea class="form-control" style="height: 80px" id="address" name="address" ><?php if($request_id>0){ echo $ms_data->address; } ?></textarea>
                </div>
				
			<div class="col-md-12">
                 <label for="last_name" class="form-label"><strong>Shipping Address</strong></label>
                  <textarea class="form-control" style="height: 80px" id="shipping_address" name="shipping_address" ><?php if($request_id>0){ echo $ms_data->shipping_address; } ?></textarea>
                </div>
				
			
				
				<div class="col-md-6">
                 <label for="city" class="form-label"><strong>Nominee Name</strong></label>
                  <input type="text" placeholder="Nominee Name"  class="form-control" id="nominee_name" name="nominee_name" value="<?php if($request_id>0){ echo $ms_data->nominee_name; } ?>" >
                </div>
                <div class="col-md-6">
                 <label for="state" class="form-label"><strong>Nominee NID</strong></label>
                  <input type="text" placeholder="Nominee NID"  class="form-control" id="nominee_nid" name="nominee_nid" value="<?php if($request_id>0){ echo $ms_data->nominee_nid; } ?>" >
                </div>
				
				
				
				<div class="col-md-6">
                 <label for="postal_code" class="form-label"><strong>Relationship With Nominee</strong></label>
                  <input type="text" placeholder="Relationship With Nominee" class="form-control" id="relationship_nominee" name="relationship_nominee" value="<?php if($request_id>0){ echo $ms_data->relationship_nominee; } ?>">
                </div>
                <div class="col-md-6">
                 <label for="country" class="form-label"><strong>Status</strong></label>
                  <select id="status" name="status"  class="form-select"  required  >
			
			<option></option>
			
			<?php foreign_relation($conn,'active_status','status','status',$_POST['status'],'1 order by status');?>
		
        	</select>
                </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 ======= -->


<script>
var printCounter = 0;
 
new DataTable('#data_table', {
    caption: "&copy; Copyright <?php echo $grp_data->group_name?> All Rights Reserved",
    layout: {
        topStart: {
            buttons: [
                'copy',
                {
                    extend: 'excel',
messageTop: 'For the period from <?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?>',
                },
                {
                    extend: 'pdf',
					
messageTop: 'For the period from <?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?>',			                    
                    //messageBottom: null
					messageBottom: "Copyright <?php echo $grp_data->group_name?> All Rights Reserved"
                },
                {
                    extend: 'print',
									
messageTop: 'For the period from <?php echo "<strong>" ?><?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?><?php echo "</strong>" ?>',
                    //messageTop: function () {
//                        printCounter++;
// 
//                        if (printCounter === 1) {
//					
//                            return 'This is the first time you have printed this document. ttt';
//                        }
//                        else {
//                            return (
//                                'You have printed this document ' + printCounter + ' times'
//                            );
//                        }
//                    },
                    messageBottom: "&copy; Copyright <?php echo $grp_data->group_name?> All Rights Reserved"
                }
            ]
        }
    }
});
</script>



<script type="text/javascript">
$(document).ready(function() {
    // Check if DataTable is already initialized
    if ($.fn.DataTable.isDataTable('#data_table')) {
        // If DataTable is already initialized, destroy it
        $('#data_table').DataTable().destroy();
    }
    
    // Reinitialize DataTable with new options
    $('#data_table').dataTable({
        'iDisplayLength': 25
    });
});
</script>

</body>

</html>