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

 // Common Information
 
$grp_sql="SELECT * FROM user_group  WHERE id='".$_SESSION['user']['group']."'";
$grp_data = find_all_field_sql($conn,$grp_sql);

$page_title="Coupon Details";
$page="1001_coupon_generate.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');


$coupon_date=$_POST['coupon_date'];
$full_name=$_POST['full_name'];
$card_no=$_POST['card_no'];
$mobile_no=$_POST['mobile_no'];


		$YR = date('Y',strtotime($coupon_date));
 		$year = date('y',strtotime($coupon_date));
  		$month = date('m',strtotime($coupon_date));
 		$inv_cy_id = find_a_field($conn,'coupon_info','max(coupon_id)','year="'.$YR.'"')+1;
  		$cy_id = sprintf("%06d", $inv_cy_id);
		$coupon_no=''.$YR.''.$cy_id;
   		$coupon_no_view='CPN '.$year.'/'.$inv_cy_id;



$data = [
    'coupon_no' => $coupon_no ,
    'year' => $YR,
    'coupon_id' => $inv_cy_id,
    'coupon_no_view' => $coupon_no_view,
    'coupon_date' => $coupon_date,
    'full_name' => $full_name,
	'card_no' => $card_no,
	'mobile_no' => $mobile_no,
	'entry_by' => $entry_by,
	'entry_at' => $entry_at
];

$columns = implode(", ", array_keys($data));
$values  = implode("', '", array_values($data));

if($coupon_no>0){
   $ins_sql = "INSERT INTO  coupon_info ($columns) VALUES ('$values')";
mysqli_query($conn,$ins_sql);
}



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

?>


<?php }?>




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

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

 <meta charset="UTF-8">
 <?php
  do_calander('#coupon_date');
  do_calander('#breeding_date');
  
   do_calander('#cf_date');
   do_calander('#ct_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 cattle_id=(document.getElementById('cattle_id_'+id).value); 
var breed=(document.getElementById('breed_'+id).value); 
var breed_order=(document.getElementById('breed_order_'+id).value);
var remarks=(document.getElementById('remarks_'+id).value); 
var flag=(document.getElementById('flag_'+id).value); 

var strURL="1001_cattle_breeding_ajax.php?id="+id+"&cattle_id="+cattle_id+"&breed="+breed+"&breed_order="+breed_order+"&remarks="+remarks+"&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><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-4">
           <label for="first_name" class="form-label"><strong> Coupon Date</strong></label> 
		
		<table class="table table-bordered ">
			 	<tr>
					<td><input type="text" class="form-control" id="cf_date" name="cf_date"  value="<?php echo $_REQUEST['cf_date'];?>" ></td>
					<td><input type="text" class="form-control" id="ct_date" name="ct_date"  value="<?php echo $_REQUEST['ct_date'];?>" ></td>
				</tr>
			 </table>
	
                </div>
                <div class="col-md-3">
			<label for="first_name" class="form-label"><strong> Coupon No </strong></label> 
			
			<select id="coupon_no_src" name="coupon_no_src"  class="form-select"  style="width: 95%;">
                      <option></option>
<?php foreign_relation($conn,'coupon_info','coupon_no','coupon_no_view',$_REQUEST['coupon_no_src'],'1 group by coupon_no order by coupon_no');?>
                  </select>
       
		
                </div>
				<div class="col-md-3">
				<label for="first_name" class="form-label"><strong>Mobile No </strong></label> 
				 <select id="mobile_no_src" name="mobile_no_src"  class="form-select"  style="width: 95%;">
                      <option></option>
				<?php foreign_relation($conn,'coupon_info','coupon_no','mobile_no',$_REQUEST['mobile_no_src'],'1 group by coupon_no order by coupon_no');?>
                  </select>
	   
                </div>
                
				
				
				<div class="col-md-2" align="center">
				
				 <button type="submit" name="submit_data" id="submit_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="15%" scope="col"><span style="white-space:nowrap;">Date  </span></th>
                    <th width="25%" scope="col">Name</th>
                    <th width="15%" scope="col">ID Card </th>
                    <th width="20%" scope="col">Mobile No </th>
                    <th width="5%" scope="col">ACTION</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <th scope="row">
					<input type="text" class="form-control" name="coupon_date" id="coupon_date" value="<?php echo isset($_REQUEST['coupon_date'])?$_REQUEST['coupon_date']:date('Y-m-d');?>"  required style="width:95%;"></th>
                    <td><input name="full_name" type="text" id="full_name"  value="" required style="width:95%" class="form-control"/></td>
                    <td><input name="card_no" type="text" id="card_no"  value="" required style="width:95%" class="form-control"/></td>
                    <td><input name="mobile_no" type="number" id="mobile_no"  value="" required style="width:95%" class="form-control"/></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;"   id="data_table">
                <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="2%" scope="col"><div align="center">SL</div></th>
                    <th width="14%" scope="col" style="white-space:nowrap;"><div align="left">Coupon No </div></th>
                    <th width="18%" scope="col" style="white-space:nowrap;"><div align="left">Name</div></th>
                    <th width="17%" scope="col" style="white-space:nowrap;"><div align="left">ID Card </div></th>
                    <th width="17%" scope="col"><div align="left">Mobile No </div></th>
                    <th width="17%" scope="col"><div align="left">Date</div></th>
                  </tr>
                </thead>
                <tbody>
		<?php
		$sl=1;
		
		//if($_POST['tr_date']!=''){
//		$tr_date =$_REQUEST['tr_date'];
//		}else{
//		$tr_date =date('Y-m-d');
//		}


if($_REQUEST['bf_date']!=''&&$_REQUEST['bt_date']!='') $breed_date_con .= ' and breeding_date between "'.$_REQUEST['bf_date'].'" and "'.$_REQUEST['bt_date'].'"';
if($_REQUEST['pf_date']!=''&&$_REQUEST['pt_date']!='') $pregnancy_date_con .= ' and pregnancy_checkup_date between "'.$_REQUEST['pf_date'].'" and "'.$_REQUEST['pt_date'].'"';
if($_REQUEST['df_date']!=''&&$_REQUEST['dt_date']!='') $delivery_date_con .= ' and estimated_delivery_date between "'.$_REQUEST['df_date'].'" and "'.$_REQUEST['dt_date'].'"';
		
		
		
		
		 $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;
		}
		
		 $sql = "select id, cattle_breed as cattle_breed_view from cattle_breed where 1 group by  id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $cattle_breed_view[$info->id]=$info->cattle_breed_view;
		}
		
		
					
					
					//if($_REQUEST['tr_date']!='')
					//$date_con=" and c.breeding_date='".$_REQUEST['tr_date']."'";
					
					if($_REQUEST['cattle_id_src']!='')
					$cattle_id=" and cattle_id='".$_REQUEST['cattle_id_src']."'";
					
						 //$sql  = 'SELECT cattle_id, cattle_id as get_cattle_id, breeding_date as latest_breeding_date, serial_no as latest_serial_no 
//						 FROM cattle_breeding 
//						  WHERE status = "Active" AND (cattle_id, breeding_date) IN (SELECT cattle_id, MAX(breeding_date) FROM cattle_breeding 
//						  WHERE status = "Active" GROUP BY cattle_id)';
//						  $query = mysqli_query($conn,$sql);
//						  while($data=mysqli_fetch_object($query)){++$sb;
//						  $get_cattle_id[$data->cattle_id]= $data->get_cattle_id;	
//						  $latest_breeding_date[$data->cattle_id]= $data->latest_breeding_date;	
//						  $latest_serial_no[$data->cattle_id]= $data->latest_serial_no;	
//		 				} 
					
		
					
						  //$sql = 'select  c.* from  cattle_breeding c
//						where 1 '.$cattle_id.$breed_date_con.$pregnancy_date_con.$delivery_date_con.' and c.status="Active"
//						 group by c.id order by c.breeding_date, c.cattle_id, c.id asc';
						 
						  $sql = 'SELECT * FROM coupon_info   WHERE 1 order by coupon_no';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				<?php //if($get_cattle_id[$data->cattle_id]){}?>	
                 <tr>
                   <td style="white-space: nowrap;"><div align="center"><?php echo $sl++;?></div></td>
                   <td style="white-space: nowrap;">
				     <div align="left"><?php echo $data->coupon_no_view; ?>				   </div></td>
                   <td style="white-space: nowrap;">   				   
				     <div align="left"><?php echo $data->full_name; ?></div></td>
                   <td style="white-space: nowrap;"><div align="left"><?php echo $data->card_no; ?></div></td>
                   <td>
				     <div align="left"><?php echo $data->mobile_no; ?></div></td>
                   <td><div align="left"><?php echo date('d-M-Y',strtotime($data->coupon_date));?></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>&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("#coupon_no_src"); ?>
<?php selected_two("#mobile_no_src"); ?>



<script>
var printCounter = 0;
 
new DataTable('#data_table', {
    caption: "&copy; Copyright <?php echo $grp_data->group_name?> All Rights Reserved",
    layout: {
        topStart: {
            buttons: [
                'copy',
                {
                    extend: 'excel',
                },
                {
                    extend: 'pdf',			                    
                    //messageBottom: null
					messageBottom: "Copyright <?php echo $grp_data->group_name?> All Rights Reserved"
                },
                {
                    extend: 'print',
									

                    //messageTop: function () {
//                        printCounter++;
// 
//                        if (printCounter === 1) {
//					
//                            return 'This is the first time you have printed this document. ttt';
//                        }
//                        else {
//                            return (
//                                'You have printed this document ' + printCounter + ' times'
//                            );
//                        }
//                    },
                    messageBottom: "&copy; Copyright <?php echo $grp_data->group_name?> All Rights Reserved"
                }
            ]
        }
    }
});
</script>

<!--<script>
var printCounter = 0;
 
new DataTable('#data_table', {
    caption: "&copy; Copyright <?php echo $grp_data->group_name?> All Rights Reserved",
    layout: {
        topStart: {
            buttons: [
                'copy',
                {
                    extend: 'excel',
messageTop: 'For the period from <?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?>',
                },
                {
                    extend: 'pdf',
					
messageTop: 'For the period from <?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?>',			                    
                    //messageBottom: null
					messageBottom: "Copyright <?php echo $grp_data->group_name?> All Rights Reserved"
                },
                {
                    extend: 'print',
									
messageTop: 'For the period from <?php echo "<strong>" ?><?php echo date('d-M-Y',strtotime($_REQUEST['fdate']));?> To <?php echo date('d-M-Y',strtotime($_REQUEST['tdate']));?><?php echo "</strong>" ?>',
                    //messageTop: function () {
//                        printCounter++;
// 
//                        if (printCounter === 1) {
//					
//                            return 'This is the first time you have printed this document. ttt';
//                        }
//                        else {
//                            return (
//                                'You have printed this document ' + printCounter + ' times'
//                            );
//                        }
//                    },
                    messageBottom: "&copy; Copyright <?php echo $grp_data->group_name?> All Rights Reserved"
                }
            ]
        }
    }
});
</script>-->


<!--<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>