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

$request_date=$_REQUEST['recipe_type'];

 // Common Information

$page_title="Bill of Materials";
$page="1002_bill_of_materials.php?active_id=".$active_id."&menu_id=".$menu_id."";


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

{


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

$tr_date=$_REQUEST['tr_date'];
$recipe_type=$_REQUEST['recipe_type'];
$item_id=$_POST['item_id'];
$description=$_POST['description'];
$total_unit=$_POST['total_unit'];
$percent=0;

$item_sql="SELECT * FROM item_info  WHERE item_id='".$item_id."'";
$item_data = find_all_field_sql($conn,$item_sql);


//if($dr_amt>0){}
 $ins_sql = 'INSERT INTO  bill_of_materials (item_id, recipe_type, unit_name, total_unit, percent, entry_by, entry_at,  edit_by, edit_at) VALUES

("'.$item_id.'", "'.$recipe_type.'", "'.$item_data->unit_name.'", "'.$total_unit.'", "'.$percent.'",  "'.$entry_by.'", "'.$entry_at.'", "0", "0000-00-00")';
	
mysqli_query($conn,$ins_sql);


header("location:".$page.'&tr_date='.$tr_date.'&recipe_type='.$recipe_type."");

?>

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

<?php }

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

{

$tr_date=$_POST['tr_date'];
$recipe_type=$_POST['recipe_type'];

header("location:".$page.'&tr_date='.$tr_date.'&recipe_type='.$recipe_type."");

?>

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

<?php } ?>




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

<!-- ======= <head> ======= -->
 <?php require_once('inc_files/inc_head.php'); ?>
<!-- ======= </head> ======= -->
 <?php
  do_calander('#tr_date');
  ?>
  
<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 TR_calculation(id){

var unit_price=(document.getElementById('unit_price_'+id).value)*1; 
var total_unit=(document.getElementById('total_unit_'+id).value)*1; 

document.getElementById('total_amt_'+id).value=(unit_price*total_unit);

}




	function update_value(id)
	{

 
var total_unit=(document.getElementById('total_unit_'+id).value);  

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

var strURL="1002_bill_of_materials_ajax.php?id="+id+"&total_unit="+total_unit+"&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>

<style>
		
		/*.fixed_head{
		 / Important /
  background-color: red;
  position: sticky;
  z-index: 100;
  top: 0;
		}	
	
	table thead  {
  / Important /
  background-color: red;
  position: sticky;
  z-index: 100;
  top: 0;
}*/
	
		</style>
  
<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">Inventory 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">
             <!-- <h5 class="card-title"><strong><?php echo $page_title;?></strong></h5>-->
			 <br />

              <!-- No Labels Form -->
              <form class="row g-3" action="" method="post" name="codz" id="codz">
                
       
          
                <div class="col-md-4">
			<label for="first_name" class="form-label"><strong> Recipe</strong></label> 
        <select id="recipe_type" name="recipe_type" required class="form-select"  style="width: 95%;">
                      <option></option>
 <?php foreign_relation($conn,'recipe_type','id','recipe_type',$_REQUEST['recipe_type'],'id group by id');?>
                    </select>
		
                </div>
                <div class="col-md-2">
				<label for="first_name" class="form-label"><strong> </strong></label> 
        
                </div>
				<div class="col-md-2">
				<label for="first_name" class="form-label"><strong> </strong></label> 
       
                </div>
				<div class="col-md-4" align="center">
				
				 <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>
				
                
              </form><!-- End No Labels Form -->

            </div>
          </div>
		
		</div>
		
		
	  
	  
        <div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
			
			<br>
			
			<?php if($request_date!=""){?>	
			<form  action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:12px;">
             <table class="table table-bordered ">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th width="30%" scope="col">Item Name</th>
                    <th width="12%" scope="col">Quantity</th>
                    <th width="8%" scope="col">ACTION</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <th scope="row">
					<input type="hidden" class="form-control" name="tr_date" id="tr_date" value="<?php echo $request_date;?>">
					<select id="item_id" name="item_id"  class="form-select" required style="width: 95%;">
                      <option></option>
 <?php foreign_relation($conn,'item_info i, item_category c','i.item_id','concat(i.item_name," - ",i.unit_name)',$ms_data->item_id,'i.item_category=c.id and c.item_group!=1 group by i.item_id');?>
                    </select>					</th>
                    <td><input type="text" class="form-control" name="total_unit" required id="total_unit"  autocomplete="off" ></td>
                    <td> 
		<button type="submit" class="btn btn-primary btn-sm" name="ins_primary_data" id="ins_primary_data" style="white-space:nowrap" ><i class="bi bi-plus"></i>ADD&nbsp;&nbsp;</button>					</td>
                  </tr>
                </tbody>
              </table>
                

              </form>
			<?php }?>

              <!-- Table with stripped rows id="example" -->
              <table class="table table-sm" style="font-size:12px;"  >
                <thead class="fixed_head">
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; border-top: 1px solid #CCCCCC; background:#f6f9ff; height:32px;">
                    <th width="28%" scope="col">Item Name</th>
                    <th width="37%" scope="col"><div align="center">Quantity </div></th>
                    <th width="10%" scope="col"><div align="left">UNIT</div></th>
                    <th width="25%" scope="col">Action</th>
                  </tr>
                </thead>
                <tbody>
		<?php
		
		
		//if($_POST['tr_date']!=''){
//		$tr_date =$_REQUEST['tr_date'];
//		}else{
//		$tr_date =date('Y-m-d');
//		}
		
		
		
		
		 $sql = "select cattle_id, cattle_no from cattle_info where 1 group by  cattle_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $cattle_no[$info->cattle_id]=$info->cattle_no;
		}
		
		
		
					
					if($_REQUEST['item_id_src']!='')
					$item_id_con=" and m.item_id='".$_REQUEST['item_id_src']."'";
					
					if($_REQUEST['cattle_id_src']!='')
					$cattle_id=" and m.cattle_id='".$_REQUEST['cattle_id_src']."'";
					
		
					
						 $sql = 'select  m.*, i.item_name from  bill_of_materials m, item_info i 
						where m.item_id=i.item_id and m.recipe_type="'.$request_date.'"   group by m.id order by m.id desc';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				<?php if($request_date!=""){?>	
                 <tr>
                   <td><?php echo $data->item_name;?>				  			   </td>
                   <td>
				   <div align="center">
				   
				  <input name="total_unit_<?php echo $data->id;?>" type="text" id="total_unit_<?php echo $data->id;?>"
				   value="<?php echo $data->total_unit; ?>"  onKeyUp="TR_calculation(<?php echo $data->id?>)"  style="width:120px" class="form-control"/>
		           </div></td>
                   <td><?php echo $data->unit_name;?></td>
                   <td><span id="divi_<?php echo $data->id?>">
			  <input name="flag_<?php echo $data->id?>" type="hidden" id="flag_<?php echo $data->id?>" value="1" />
			  <input class="form-control" type="button" name="Button" value="Edit"  onclick="update_value(<?php echo $data->id?>)" style="width:80px; height:30px; background-color: #FF3366; font-weight:700; font-size:12px;"/>
			  
          </span></td>
                 </tr>
                 
                 
				  <?php } }?>
				  
				  <!--<tr style="font-size:16px;">
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td><div align="center"><strong>Total</strong></div></td>
                   <td>&nbsp;</td>
                   <td><div align="center"><strong><?php echo number_format($tot_morning_qty,2);?></strong></div></td>
                   <td>&nbsp;</td>
                 </tr>-->
                </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("#item_id"); ?>
<?php selected_two("#cattle_id"); ?>
<?php selected_two("#item_id_src"); ?>
<?php selected_two("#cattle_id_src"); ?>
<script>
var printCounter = 0;
 
new DataTable('#example', {
    caption: "&copy; Copyright Five Star Farm House Limited. All Rights Reserved",
    layout: {
        topStart: {
            buttons: [
                'copy',
                {
                    extend: 'excel',
                    messageTop:
                        'The information in this table is copyright to Five Star Farm House Limited.'
                },
                {
                    extend: 'pdf',
                    //messageBottom: null
					messageBottom: "Copyright Five Star Farm House Limited. All Rights Reserved"
                },
                {
                    extend: 'print',
                    messageTop: function () {
                        printCounter++;
 
                        if (printCounter === 1) {
                            return 'This is the first time you have printed this document.';
                        }
                        else {
                            return (
                                'You have printed this document ' + printCounter + ' times'
                            );
                        }
                    },
                    messageBottom: "&copy; Copyright Five Star Farm House Limited. All Rights Reserved"
                }
            ]
        }
    }
});
</script>

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


</body>

</html>