<?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 Sales Price";
$page="47_customer_sales_price.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='journal_master';	
$unique='jv_no';

// End Common Information



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

{


$sales_inv_page="1001_pos_sales_entry.php?active_id=".$active_id."&menu_id=".$menu_id."";

$status='MANUAL';
$entry_by =  $_SESSION['user']['id'];
$entry_at = date('Y-m-d H:i:s');
$group_for = $_SESSION['user']['group'];
$warehouse_id = $_SESSION['user']['depot'];
$jv_date=date('Y-m-d');
$ref_no_src=$_POST['ref_no_src'];
$jv_type=5;
$jv_special=1;
$acc_terms=1;
$tr_from='Sales';
//$tr_no=1001;
$blank=0;
$blank_date='0000-00-00';
$ms_data_table='journal_master';

$date = date('ymd');
$file = "serial_$date.txt";


if (file_exists($file)) {
    $serial = (int)file_get_contents($file);
    $serial++;
} else {
    $serial = 1;
}

file_put_contents($file, $serial);

$serialPart = str_pad($serial, 4, '0', STR_PAD_LEFT);
$number = $date . $serialPart;

 $number; 



if($ref_no_src==200001){
$sales_type=1;
}else{
$sales_type=2;
}



if($ref_no_src>0){
  $ins_sql = 'INSERT INTO  journal_master (jv_no, year, jv_id, view_jv_no, jv_type, jv_special, jv_date, group_for, warehouse_id, ref_no, master_ledger_id, emp_code, tr_from, tr_no, remarks, acc_terms, status, entry_at, entry_by, edit_at, edit_by, checked_at, checked_by, approve_by, approve_at, sales_type) VALUES

("'.$number.'", "'.$blank.'", "'.$blank.'", "'.$blank.'", "'.$jv_type.'", "'.$jv_special.'", "'.$jv_date.'", "'.$group_for.'", "'.$warehouse_id.'", "'.$ref_no_src.'", "'.$blank.'", "'.$blank.'", "'.$tr_from.'", "'.$number.'", "'.$blank.'", "'.$acc_terms.'", "'.$status.'", "'.$entry_at.'", "'.$entry_by.'", "'.$blank_date.'", "'.$blank.'", "'.$blank_date.'", "'.$blank.'", "'.$blank.'", "'.$blank_date.'", "'.$sales_type.'")';
	
mysqli_query($conn,$ins_sql);
}


header("location:".$sales_inv_page.'&tr_no='.$number."");

}




?>

<!DOCTYPE html>
<html lang="en"><style type="text/css">
<!--
.style1 {font-size: 12px}
-->
</style>

 <?php require_once('inc_files/inc_head.php'); ?>
 
 <?php
  do_calander('#fdate');
  do_calander('#tdate');
  ?>
 
 <script language="javascript">

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(item_id)
	{

var ref_no=(document.getElementById('ref_no_'+item_id).value); 
var sales_price=(document.getElementById('sales_price_'+item_id).value); 

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

var strURL="47_customer_sales_price_ajax.php?item_id="+item_id+"&ref_no="+ref_no+"&sales_price="+sales_price+"&flag="+flag;


		var req = getXMLHTTP();



		if (req) {



			req.onreadystatechange = function() {

				if (req.readyState == 4) {

					// only if "OK"

					if (req.status == 200) {						

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

						document.getElementById('divi_'+item_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">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-12"> 

          <div class="card">
            <div class="card-body">
			<br>
            <form class="row g-3" action="" method="post" name="codz" id="codz" onSubmit="return check()">
	 
                
		
				<div class="col-md-6">
                 <label for="first_name" class="form-label"><strong>Customer Name</strong></label>
     		<select id="ref_no_src" name="ref_no_src"  class="form-select"  required  >
			<option></option>
			<?php foreign_relation($conn,'accounts_reference','ref_no','display_name',$_POST['ref_no_src'],'ref_type=2 order by display_name');?>
		
        	</select>
			   
                </div>
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong>Item Category</strong></label>
            <select id="item_category_src" name="item_category_src"  class="form-select"    >
			<option></option>
			
			<?php foreign_relation($conn,'item_category','id','item_category',$_POST['item_category_src'],'item_group=1 order by id');?>
		
        	</select>
			    
                </div>
				
				<div class="col-md-2" style="text-align:center">
                 <label for="first_name" class="form-label"><strong>&nbsp;</strong></label><br />
     	 <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> Run Report&nbsp;&nbsp;</button>
			   
                </div>
				
				<div class="col-md-12">
				</div>
				<div class="col-md-12">
				</div>
			
				
				
               
  </form>
  <br>
	
              <!-- Table with stripped rows -->
              <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="10%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">  item Category </span></th>
                    <th width="12%" scope="col"><span class="card-title" style="font-size:12px; text-transform:uppercase;">SKU</span></th>
                    <th width="29%" scope="col">Item Name </th>
                    <th width="13%" scope="col">Unit Name </th>
                    <th width="11%" scope="col">Regular Price</th>
                    <th width="14%" scope="col">Sales Price </th>
                    <th width="11%" scope="col"><span class="style1">Action </span></th>
                  </tr>
                </thead>
                <tbody>
					<?php
					




if($_POST['ref_no_src']!=''){
$ref_no_con=" and ref_no='".$_POST['ref_no_src']."' ";
}



if($_POST['item_category_src']!=''){
$item_category_con=" and i.item_category='".$_POST['item_category_src']."' ";
}

	
		
		 $sql = "select ref_no, item_id, sales_price, id as data_found from customer_sales_price where 1 ".$ref_no_con." group by ref_no, item_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $sales_price[$info->ref_no][$info->item_id]=$info->sales_price;
		 $data_found[$info->ref_no][$info->item_id]=$info->data_found;
		}
		
		
		
			
					  $sql = 'select i.*, c.item_category as item_category_name from item_info i, item_category c 
					  where i.item_category=c.id and c.item_group=1 '.$item_category_con.'  order by i.item_category,  i.sku_code ';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
		<?php if($_POST['ref_no_src']>0) {?>
      <tr>
      <td><?php echo $data->item_category_name;?></td>
      <td><?php echo $data->sku_code;?></td>
      <td><?php echo $data->item_name;?></td>
      <td><?php echo $data->unit_name;?></td>
      <td><?php echo $data->sale_price;?></td>
      <td>
	  <input type="hidden" class="form-control" name="ref_no_<?php echo $data->item_id; ?>" id="ref_no_<?php echo $data->item_id; ?>" 
	  value="<?php echo $_POST['ref_no_src']?>" style="width:120px"  >
	  
	  <input type="text" class="form-control" name="sales_price_<?php echo $data->item_id; ?>" id="sales_price_<?php echo $data->item_id; ?>" 
	  value="<?php echo $sales_price[$_POST['ref_no_src']][$data->item_id]?>" style="width:120px"  >	
	  
	  <?php  $data_found[$_POST['ref_no_src']][$data->item_id]?>  </td>
	  <td><span id="divi_<?php echo $data->item_id?>">
	  <?php if($data_found[$_POST['ref_no_src']][$data->item_id]>0) {?>
			  <input name="flag_<?php echo $data->item_id?>" type="hidden" id="flag_<?php echo $data->item_id?>" value="1" />
			  <input class="form-control" type="button" name="Button" value="EDIT"  onclick="update_value(<?php echo $data->item_id?>)" style="width:80px; height:35px; background-color: #FF3366; font-weight:700; font-size:12px;"/>
	 <?php }else{?>
	 		  <input name="flag_<?php echo $data->item_id?>" type="hidden" id="flag_<?php echo $data->item_id?>" value="0" />
			  <input class="form-control" type="button" name="Button" value="SAVE"  onclick="update_value(<?php echo $data->item_id?>)" style="width:80px; height:35px; background-color: #00CC33; font-weight:700; font-size:12px;"/>
	 <?php }?>
			 
          </span></td>
      </tr>
      
				  <?php } } ?>
                </tbody>
              </table>
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        
      </div>
    </section>

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

  <!-- ======= Footer ======= -->
  <?php require_once('inc_files/inc_footer.php'); ?>
 <!-- ======= Footer ======= -->
 <?php 
 
 selected_two("#ref_no_src");
 
 ?>
</body>

</html>