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

$req_item_type=$_REQUEST['item_type'];



 // Common Information

$page_title="Products/Services Information";
$page="34_product_service.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='item_info';	
$unique='item_id';

$request_id=$_REQUEST['item_id'];

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

// End Common Information

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

{		

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

$item_type = $_POST['item_type'];

$cy_id=mysqli_fetch_object(mysqli_query($conn,'select max(item_sl) as item_sl from '.$table.' where item_type="'.$item_type.'"'));
$item_sl_id=$cy_id->item_sl+1;
$item_sl_no = sprintf("%07d", $item_sl_id);

$item_id = $item_type.''.$item_sl_no;

$item_category = $_POST['item_category'];
$sku = $_POST['sku_code'];
$item_name = $_POST['item_name'];
$unit_name = $_POST['unit_name'];
$sale_price = $_POST['sale_price'];
$cost_price = $_POST['cost_price'];
$inventory_ledger = $_POST['inventory_ledger'];
$income_ledger = $_POST['income_ledger'];
$cost_ledger = $_POST['cost_ledger'];

if($sku==""){
$sku_code=$item_id;
}else{
$sku_code=$sku;
}


//$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  item_info (item_id, item_sl, item_type, item_category, sku_code, item_name, unit_name, sale_price, cost_price, inventory_ledger, income_ledger, cost_ledger, entry_by, entry_at) VALUES
("'.$item_id.'", "'.$item_sl_no.'", "'.$item_type.'", "'.$item_category.'", "'.$sku_code.'", "'.$item_name.'", "'.$unit_name.'", "'.$sale_price.'", "'.$cost_price.'", "'.$inventory_ledger.'", "'.$income_ledger.'", "'.$cost_ledger.'", "'.$entry_by.'", "'.$entry_at.'")';
	
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');

$item_name = $_POST['item_name'];
$sku_code = $_POST['sku_code'];
$sale_price = $_POST['sale_price'];
$cost_price = $_POST['cost_price'];

$item_id=$request_id;

 $up_sql = 'update item_info set item_name="'.$item_name.'", sku_code="'.$sku_code.'", sale_price="'.$sale_price.'", cost_price="'.$cost_price.'",  edit_by="'.$entry_by.'", edit_at="'.$entry_at.'" 
 where item_id='.$item_id;
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">Products &amp; Services</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"> SKU </span></th>
                    <th scope="col"><span class="card-title">Item Name </span></th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
						$sql = 'select * from '.$table.' where item_type=2';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                 <tr>
       <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->item_id;?>" style="color:#000000"><?php echo $data->sku_code;?></a></td>
      <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->item_id;?>" style="color:#000000"><?php echo $data->item_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()">
			  
			  <div class="row mb-3">
                  <label for="inputEmail3" class="col-sm-3 col-form-label">Item Type</label>
                  <div class="col-sm-9">
              <?php if($request_id>0){?>      
			<select id="item_type" name="item_type"  class="form-select"  required >
		
			<?php foreign_relation($conn,'item_type','id','item_type',$_POST['item_type'],'id="'.$ms_data->item_type.'" order by id');?>
		
        	</select>
			<?php } else {?>
			<select id="item_type" name="item_type"  class="form-select"  required  >
	
			<?php foreign_relation($conn,'item_type','id','item_type',$_POST['item_type'],'id="'.$req_item_type.'" order by id');?>
		
        	</select>
			
			<?php }?>
			
					
                  </div>
                </div>
			  
			  
                <div class="row mb-3">
                  <label for="inputEmail3" class="col-sm-3 col-form-label">Item Name</label>
                  <div class="col-sm-9">
                    <input type="text" class="form-control" id="item_name" name="item_name" value="<?php if($request_id>0){ echo $ms_data->item_name; } ?>" required>
                  </div>
                </div>
				
				<div class="row mb-3">
                  <label for="inputEmail3" class="col-sm-3 col-form-label">SKU</label>
                  <div class="col-sm-9">
                    <input type="text" class="form-control" id="sku_code" name="sku_code" value="<?php if($request_id>0){ echo $ms_data->sku_code; } ?>" >
                  </div>
                </div>
				
				
				<div class="row mb-3">
                  <label for="inputEmail3" class="col-sm-3 col-form-label">Unit Type</label>
                  <div class="col-sm-9">
              <?php if($request_id>0){?>    
			<select id="unit_name" name="unit_name"  class="form-select"  required  >
			
			<?php foreign_relation($conn,'unit_management','unit_name','unit_name',$_POST['unit_name'],'unit_name="'.$ms_data->unit_name.'" order by id');?>
		
        	</select>
				<?php } else {?>
				
			<select id="unit_name" name="unit_name"  class="form-select"  required  >
			
			<option></option>
		
			<?php foreign_relation($conn,'unit_management','unit_name','unit_name',$_POST['unit_name'],'1 order by id');?>
		
        	</select>	
				
				<?php }?>
				
					
                  </div>
                </div>
				
				
				<div class="row mb-3">
                  <label for="inputEmail3" class="col-sm-3 col-form-label">Category</label>
                  <div class="col-sm-9">
                 <?php if($request_id>0){?>       
			<select id="item_category" name="item_category"  class="form-select"  required  >
			
		
			
	
			<?php foreign_relation($conn,'item_category','id','item_category',$_POST['item_category'],'id="'.$ms_data->item_category.'" order by id');?>
		
        	</select>
				<?php } else {?>
				
				<select id="item_category" name="item_category"  class="form-select"  required  >
			
			<option></option>
			
	
			<?php foreign_relation($conn,'item_category','id','item_category',$_POST['item_category'],'1 order by id');?>
		
        	</select>
				
				<?php }?>	
                  </div>
                </div>
				
		
				
				<hr>
				
				
				<div class="col-md-5">
                 <label for="inputEmail5" class="form-label">Sales Price/Rate</label>
                  <input type="text" class="form-control" id="sale_price" name="sale_price" value="<?php if($request_id>0){ echo $ms_data->sale_price; } ?>" >
                </div>
                <div class="col-md-7">
                  <label for="inputPassword5" class="form-label">Income Account</label>
			<?php if($request_id>0){?> 
            <select id="income_ledger" name="income_ledger"  class="form-select"  required  >
			
			<?php foreign_relation($conn,'accounts_ledger','ledger_id','ledger_name',$_POST['income_ledger'],'ledger_id="'.$ms_data->income_ledger.'" order by ledger_id');?>
        	</select>
			<?php } else {?>
			 <select id="income_ledger" name="income_ledger"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'accounts_ledger','ledger_id','ledger_name',$_POST['income_ledger'],'acc_class=3 order by ledger_id');?>
        	</select>
			
			<?php }?>	
                </div>
				
				
				
				<?php /*?><div class="col-md-5">
                 <label for="inputEmail5" class="form-label">Cost Price/Rate</label>
                  <input type="text" class="form-control" id="cost_price" name="cost_price" value="<?php if($request_id>0){ echo $ms_data->cost_price; } ?>">
                </div>
                <div class="col-md-7">
                  <label for="inputPassword5" class="form-label">Expense Account</label>
			<?php if($request_id>0){?> 
            <select id="cost_ledger" name="cost_ledger"  class="form-select"  required  >
			<?php foreign_relation($conn,'accounts_ledger','ledger_id','ledger_name',$_POST['cost_ledger'],'ledger_id="'.$ms_data->cost_ledger.'" order by ledger_id');?>
        	</select>
			<?php } else {?>
			<select id="cost_ledger" name="cost_ledger"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'accounts_ledger','ledger_id','ledger_name',$_POST['cost_ledger'],'ledger_group_id=41001 order by ledger_id');?>
        	</select>
			<?php }?>
			
                </div><?php */?>
				
				
				
				<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 ======= -->
<?php 
 selected_two("#income_ledger");
 ?>
</body>

</html>