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

$today_date = date('Y-m-d');
$today_time = date('H:i:s');

$timeA = ('04:00:00');
$timeB = ('11:59:00');

$timeC = ('14:00:00');
$timeD = ('20:00:00');

//$timeE = ('18:00:00');
//$timeF = ('23:59:00');

//$timeG = ('02:00:00');
//$timeH = ('04:30:00');


 if ($today_time  > $timeA  && $today_time < $timeB) {
 
  $default_milk_schedule = 1;
 
 } elseif ($today_time  > $timeC  && $today_time < $timeD) {
 
  $default_milk_schedule = 2;
 
 } 
 

//do_calander('#jv_date');
// Common Information

$page_title="Milk Record";
$page="1002_cattle_milk_record.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='milk_record';	
$unique='id';


// End Common Information

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

{
//$_POST['entry_by'] = $_SESSION['user']['id'];
$status='MANUAL';
$entry_by =  $_SESSION['user']['id'];
$entry_at = $now=date('Y-m-d H:i:s');
$group_for = $_SESSION['user']['group'];
$invoice_date=$_POST['invoice_date'];
$milk_schedule=$_POST['milk_schedule'];
$cattle_id=$_POST['cattle_id'];
$total_unit=$_POST['total_unit'];
$remarks=$_POST['remarks'];
$unit_name='KG';
$tr_no=0;


		//$YR = date('Y',strtotime($invoice_date));
 		//$year = date('y',strtotime($invoice_date));
  		//$month = date('m',strtotime($invoice_date));
 		//$inv_cy_id = find_a_field($conn,'milk_record','max(jv_id)','year="'.$YR.'" and jv_type="'.$jv_type.'"')+1;
  		//$cy_id = sprintf("%06d", $inv_cy_id);
   		//$invoice_no=''.$year.''.$month.''.$jv_type.''.$cy_id;
		//$view_invoice_no=$jv_title.''.$year.''.$month.''.$jv_type.''.$cy_id;
		

$warehouse_id= $_SESSION['user']['depot'];

  
$cattle_sql="SELECT * FROM cattle_info  WHERE cattle_id='".$cattle_id."'";
$cattle_data = find_all_field_sql($conn,$cattle_sql);


$ins_sql = 'INSERT INTO  milk_record (tr_no, tr_date, cattle_id, cattle_no, shed_no, unit_name, total_unit, entry_by, entry_at, remarks, milk_schedule, edit_by, edit_at) VALUES

("'.$tr_no.'", "'.$invoice_date.'", "'.$cattle_id.'", "'.$cattle_data->cattle_no.'", "'.$cattle_data->shed_no.'", "'.$unit_name.'", "'.$total_unit.'", "'.$entry_by.'", "'.$entry_at.'", "'.$remarks.'", "'.$milk_schedule.'","0","'.$entry_at.'")';
	
mysqli_query($conn,$ins_sql);


header("location:".$page);

?>


<?php

}







?>




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

<!-- ======= <head> ======= -->
 <?php require_once('inc_files/inc_head.php'); ?>
<!-- ======= </head> ======= -->


  <?php
  do_calander('#invoice_date');
  create_combobox('master_ledger');
  ?>
 
 
 <script language="javascript">

function count(){



var total_unit=(document.getElementById('total_unit').value)*1; 

var unit_price=(document.getElementById('unit_price').value)*1;

document.getElementById('total_amt').value=(total_unit*unit_price);



}


function count_total_cal(){
var sub_total_amt=(document.getElementById('sub_total_amt').value)*1; 
var vat_amt=(document.getElementById('vat_amt').value)*1; 
var advance_amt=(document.getElementById('advance_amt').value)*1; 
document.getElementById('invoice_amt').value=(sub_total_amt+vat_amt)-advance_amt;

}




//window.onload = function() {
//  document.getElementById("itemin").focus();
//};






</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">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-7">

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

              <!-- General Form Elements -->
                <form class="row g-3" action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:12px;">
				
					<div class="col-md-6">
                 <label for="first_name" class="form-label"><strong>TR Date</strong></label>
    			<input type="text" class="form-control" id="invoice_date" name="invoice_date" value="<?php echo isset($_REQUEST['invoice_date'])?$_REQUEST['invoice_date']:date('Y-m-d');?>" >
			   
                </div>
				
               	<div class="col-md-6">
                 <label for="last_name" class="form-label"><strong>Time</strong></label>
            <select id="milk_schedule" name="milk_schedule"  class="form-select"  required  >
			<option></option>
			
			<?php foreign_relation($conn,'milk_schedule','id','milk_schedule',$default_milk_schedule,'1 order by id');?>
		
        	</select>
				 
                </div>
				
			
				
				
				
				
				
				
			<div class="col-md-6">
           <label for="first_name" class="form-label"><strong>Cattle No</strong></label>	
				 
			<select id="cattle_id" name="cattle_id"  class="form-select"  required  >
			<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-6">
                 <label for="last_name" class="form-label"><strong>Quantity</strong></label>
            	<input type="text" class="form-control" id="total_unit" name="total_unit" value="" required>
				 
                </div>
				
				
				
				
				<div class="col-md-12">
                 <label for="last_name" class="form-label"><strong>Remarks</strong></label>
                   <textarea class="form-control" style="height: 60px" id="remarks" name="remarks" ></textarea>
                </div>
				
				
				
                
				
                   

                <div class="text-center">
				
				
                  <button type="submit" class="btn btn-success btn-sm" name="confirm_data" id="confirm_data" ><i class="bi bi-plus-lg me-1"></i> Save &amp; Publish</button> &nbsp;&nbsp;
				
				 
                
                </div>

              </form><!-- End General Form Elements -->

            </div>
          </div>
</div>


<div class="col-lg-5">

          <div class="card">
            <?php /*?><div class="card-body">
		   
			<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="22%" scope="col"><span class="card-title" style="font-size:10px; text-transform:uppercase;">JV No </span></th>
                    <th width="20%" scope="col"><span class="card-title" style="font-size:10px;  text-transform:uppercase;"><div style="text-align:center">JV Date</div> </span></th>
                    <th width="20%" scope="col"><span class="card-title" style="font-size:10px; text-transform:uppercase;">
                    <div style="text-align:right">Amount</div> </span></th>
                    <th width="38%" scope="col"><span class="card-title" style="font-size:10px; text-transform:uppercase;">
                    <div style="text-align:right">Entry By </div>
                    </span></th>
                  </tr>
                </thead>
                <tbody>
		<?php
			
					
		 $sql = "select user_id, fname from user_management where 1 group by user_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $fname[$info->user_id]=$info->fname;
		}
					
					    $sql = 'select a.jv_no, a.view_jv_no, a.jv_date, sum(j.dr_amt) as jv_amt, a.entry_by from '.$table.' a, journal j 
						 where a.jv_no=j.jv_no and a.tr_from="Sales" and a.status="CHECKED" and j.item_id=0 group by j.jv_no order by a.jv_no DESC LIMIT 8';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
			
      <tr>
      <td><a href="view_file/journal_print_view.php?jv_no=<?php echo $data->jv_no;?>" target="_blank"><?php echo $data->view_jv_no;?></a></td>
      <td align="center"><?php echo date('d-M-y',strtotime($data->jv_date));?></td>
      <td align="right"><?php echo number_format($data->jv_amt,2); ?></td>
      <td align="right"><?php echo $fname[$data->entry_by];?></td>
      </tr>
				  <?php }?>
				  
     
                </tbody>
              </table>
			</div><?php */?>
		</div>
</div>






 
 
 
          

      </div>
    </section>

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

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

</html>