<?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="Cattle Feed Issue";



?>




<!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(cattle_id){

var total_unit_morning=(document.getElementById('total_unit_morning_'+cattle_id).value)*1; 
var total_unit_afternoon=(document.getElementById('total_unit_afternoon_'+cattle_id).value)*1; 

document.getElementById('total_unit_'+cattle_id).value=(total_unit_morning+total_unit_afternoon);

}




	function update_value(item_id)
	{
	
var cattle_type=(document.getElementById('cattle_type').value); 
var no_of_cattle=(document.getElementById('no_of_cattle').value); 
var total_feed_qty=(document.getElementById('total_feed_qty').value); 

var tr_date=(document.getElementById('tr_date_'+item_id).value); 
var item_id=(document.getElementById('item_id_'+item_id).value); 
var unit_price=(document.getElementById('unit_price_'+item_id).value); 
var percent=(document.getElementById('percent_'+item_id).value)*1; 
var total_unit_cal=(document.getElementById('total_unit_cal_'+item_id).value)*1; 
var total_unit=(document.getElementById('total_unit_'+item_id).value)*1; 

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

var strURL="1002_cattle_feed_issue_ajax.php?cattle_type="+cattle_type+"&no_of_cattle="+no_of_cattle+"&total_feed_qty="+total_feed_qty+"&tr_date="+tr_date+"&item_id="+item_id+"&unit_price="+unit_price+"&percent="+percent+"&total_unit_cal="+total_unit_cal+"&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_'+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>

<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">Cattle 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-2">
           <label for="first_name" class="form-label"><strong> Date</strong></label> 
			<input type="text" class="form-control" id="tr_date" name="tr_date" required value="<?php echo isset($_REQUEST['tr_date'])?$_REQUEST['tr_date']:date('Y-m-d');?>" >
	
                </div>
                <div class="col-md-2">
			<label for="first_name" class="form-label"><strong> Cattle Type</strong></label> 
           <select id="cattle_type" name="cattle_type"  class="form-select"  required  >
			<option></option>
			
			<?php foreign_relation($conn,'cattle_feed_type','id','cattle_type',$_POST['cattle_type'],'1 order by id');?>
		
        	</select>
		
                </div>
                <div class="col-md-3">
				<label for="first_name" class="form-label"><strong> Number of Cattle</strong></label> 
         <input type="text" class="form-control" id="no_of_cattle" name="no_of_cattle" required value="<?php echo $_POST['no_of_cattle'];?>" >
                </div>
				<div class="col-md-2">
				<label for="first_name" class="form-label"><strong> Total Qty</strong></label> 
         <input type="text" class="form-control" id="total_feed_qty" name="total_feed_qty" required value="<?php echo $_POST['total_feed_qty'];?>" >
                </div>
				<div class="col-md-3" 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>
				<!--<div class="col-md-2" align="right">
				<a href="1002_milk_receive.php?active_id=1002&menu_id=10"><button type="button" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg me-1"></i> New Entry&nbsp;&nbsp;&nbsp;</button></a>
				</div>-->
                
              </form><!-- End No Labels Form -->

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

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

              <!-- 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="15%" scope="col">DATE</th>
                    <th width="15%" scope="col">Product</th>
                    <th width="20%" scope="col" style="white-space:nowrap" ><div align="center">Price</div></th>
                    <th width="14%" scope="col" style="white-space:nowrap" ><div align="center">Percentage (%)</div></th>
                    <th width="20%" scope="col"><div align="center">Quantity </div></th>
                    <th width="16%" 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 tr_date, cattle_type, item_id, total_unit as issue_qty from cattle_feed_issue where 1 group by  tr_date, cattle_type, item_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $issue_qty[$info->tr_date][$info->cattle_type][$info->item_id]=$info->issue_qty;
		}
		
		
		
					
					if($_POST['cattle_id']!='')
					$cattle_id_con=" and c.cattle_id='".$_POST['cattle_id']."'";
					
					if($_POST['shed_no']!='')
					$shed_no_con=" and c.shed_no='".$_POST['shed_no']."'";
					
		$sql = "select item_id, percent from cattle_feed_ratio where cattle_type='".$_POST['cattle_type']."' group by  item_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $percent[$info->item_id]=$info->percent;
		}
					
						$sql = 'select  i.* from  item_info i 
						where item_category in (2,3)  group by i.item_id order by i.item_category, i.item_id ';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				<?php if(isset($_POST['run_data'])){?>	
                 <tr>
                   <td style="white-space: nowrap;"><?php echo date('d-M-y',strtotime($tr_date));?>
				   <input name="tr_date_<?php echo $data->item_id?>" type="hidden" id="tr_date_<?php echo $data->item_id?>"
				   value="<?php echo $tr_date;?>"  style="width:120px" class="form-control"/>				   </td>
                   <td><?php echo $data->bangla_name;?>
				   <input name="item_id_<?php echo $data->item_id?>" type="hidden" id="item_id_<?php echo $data->item_id?>"
				   value="<?php echo $data->item_id;?>"  style="width:120px" class="form-control"/>				   </td>
                   <td style="white-space: nowrap;"><div align="center">
				   <input name="unit_price_<?php echo $data->item_id?>" type="text" id="unit_price_<?php echo $data->item_id?>"
				   value="<?php echo $data->cost_price;?>"  style="width:100px" class="form-control"/>
				   </div></td>
                   <td style="white-space: nowrap;"><div align="center">
                     <input name="percent_<?php echo $data->item_id?>" type="text" id="percent_<?php echo $data->item_id?>"
				   value="<?php echo $percent[$data->item_id];?>"  style="width:70px" class="form-control"/>
                   </div></td>
                   <td>
				   <div align="center">
				   <input name="total_unit_cal_<?php echo $data->item_id?>" type="hidden" id="total_unit_cal_<?php echo $data->item_id?>"
				   value="<?php echo $total_unit_cal=round(($_POST['total_feed_qty']*$percent[$data->item_id])/100,0);?>" style="width:120px" class="form-control"/>
				   
				   
				  <input name="total_unit_<?php echo $data->item_id?>" type="text" id="total_unit_<?php echo $data->item_id?>"
				   value="<?php if($issue_qty[$tr_date][$_POST['cattle_type']][$data->item_id]>0)
				    {echo $issue_qty[$tr_date][$_POST['cattle_type']][$data->item_id];}else{
					echo $total_unit_cal;
					}?>" style="width:120px" class="form-control"/>
		           </div></td>
                   <td><span id="divi_<?php echo $data->item_id?>">
			 <?php if($issue_qty[$tr_date][$_POST['cattle_type']][$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:30px; background-color: #FF3366; font-weight:700; font-size:12px;"/>
			  <? }else{?>
			  <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="Save"  onclick="update_value(<?php echo $data->item_id?>)" style="width:80px; height:30px; background-color: #28a745; font-weight:700; font-size:12px;"/>
			  <?php }?>
          </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("#cattle_id"); ?>
<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>