<?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="Milk Record";



?>




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

<!-- ======= <head> ======= -->
 <?php require_once('inc_files/inc_head.php'); ?>
<!-- ======= </head> ======= -->
 <?php
  do_calander('#tr_date');
  ?>
<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-3">
			<label for="first_name" class="form-label"><strong> Cattle No</strong></label> 
           <select id="cattle_id" name="cattle_id"  class="form-select"    >
			<option></option>
			
			<?php foreign_relation($conn,'cattle_info','cattle_id','cattle_no',$_POST['cattle_id'],'1 order by cattle_sl');?>
		
        	</select>
		
                </div>
                <div class="col-md-3">
				<label for="first_name" class="form-label"><strong> Shed No</strong></label> 
          <select id="shed_no" name="shed_no"  class="form-select"    >
			<option></option>
			<?php foreign_relation($conn,'shed_info','id','shed_name',$_POST['shed_no'],'1');?>
		
        	</select>
                </div>
				<div class="col-md-2">
				 <button type="submit" 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>
                  <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">Shed</th>
                    <th width="10%" scope="col" style="white-space:nowrap" ><div align="center">Cattle No </div></th>
                    <th width="20%" scope="col"><div align="right">Morning </div></th>
                    <th width="20%" scope="col"><div align="right">Afternoon</div></th>
                    <th width="20%" scope="col"><div align="right">Total</div></th>
                  </tr>
                </thead>
                <tbody>
		<?php
		
		
		$sql = "select id, shed_name from shed_info where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $shed_name[$info->id]=$info->shed_name;
		}
		
		$sql = "select tr_date, cattle_id, milk_schedule, total_unit from milk_record where 1 group by  tr_date, cattle_id, milk_schedule";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $total_unit[$info->tr_date][$info->cattle_id][$info->milk_schedule]=$info->total_unit;
		}
		
		
		if($_POST['tr_date']!=''){
		$date_con .= ' and m.tr_date="'.$_REQUEST['tr_date'].'"';
		}else{
		$date_con .= ' and m.tr_date="'.date('Y-m-d').'"';
		}
					
					if($_POST['cattle_id']!='')
					$cattle_id_con=" and m.cattle_id='".$_POST['cattle_id']."'";
					
					if($_POST['shed_no']!='')
					$shed_no_con=" and m.shed_no='".$_POST['shed_no']."'";
					
					
					
						 $sql = 'select m.*, c.cattle_no from milk_record m, cattle_info c 
						where m.cattle_id=c.cattle_id '.$date_con.$cattle_id_con.$shed_no_con.' group by m.tr_date, m.cattle_id order by m.id desc';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                 <tr>
                   <td style="white-space: nowrap;"><?php echo date('d-M-y',strtotime($data->tr_date));?></td>
                   <td><?php echo $shed_name[$data->shed_no];?></td>
                   <td><div align="center"><?php echo $data->cattle_no;?></div></td>
                   <td><div align="right"><?php echo $total_unit[$data->tr_date][$data->cattle_id][1]; $tot_morning_qty +=$total_unit[$data->tr_date][$data->cattle_id][1];?></div></td>
                   <td><div align="right"><?php echo $total_unit[$data->tr_date][$data->cattle_id][2]; $tot_afternoon_qty +=$total_unit[$data->tr_date][$data->cattle_id][2];?></div></td>
   			       <td>
	                 <div align="right"><?php echo number_format($total_milk_receive=$total_unit[$data->tr_date][$data->cattle_id][1]+$total_unit[$data->tr_date][$data->cattle_id][2],2);
	 $tot_total_milk_receive +=$total_milk_receive; ?></div></td>
                 </tr>
                 
				  <?php }?>
				  <tr style="font-size:16px;">
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td><div align="center"><strong>Total</strong></div></td>
                   <td><div align="right"><strong><?php echo number_format($tot_morning_qty,2);?></strong></div></td>
                   <td><div align="right"><strong><?php echo number_format($tot_afternoon_qty,2);?></strong></div></td>
                   <td><div align="right"><strong><?php echo number_format($tot_total_milk_receive,2);?></strong></div></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>