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

$ms_sql="SELECT * FROM journal_master  WHERE tr_no='".$request_date."'";
$ms_data = find_all_field_sql($conn,$ms_sql);

 // Common Information

$page_title="Sales Invoice";
$page="1001_pos_sales_entry.php?active_id=".$active_id."&menu_id=".$menu_id."";
$status_page="47_customer_invoice_status.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');
$status='CHECKED';
$tr_no=$_POST['tr_no'];
$tr_date=$_POST['tr_date'];
$item_id=$_POST['item_id'];
$total_unit=$_POST['total_unit'];

$ms_sql="SELECT * FROM journal_master  WHERE tr_no='".$tr_no."'";
$ms_data = find_all_field_sql($conn,$ms_sql);

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

$cust_price_sql="SELECT sales_price FROM customer_sales_price  WHERE item_id='".$item_id."' and ref_no='".$ms_data->ref_no."' and sales_price>0 group by item_id";
$cust_price_data = find_all_field_sql($conn,$cust_price_sql);

if($cust_price_data->sales_price>0){

$item_inv_price=$cust_price_data->sales_price;
$total_amt=$total_unit*$cust_price_data->sales_price;

}else{

$item_inv_price=$item_data->sale_price;
$total_amt=$total_unit*$item_data->sale_price;

}


//if($dr_amt>0){}
 $ins_sql = 'INSERT INTO  sales_invoice (jv_no, jv_date, tr_no, ref_no, item_id, unit_name, total_unit, unit_price, total_amt,  entry_at, entry_by, edit_by, edit_at) VALUES

("'.$tr_no.'", "'.$ms_data->jv_date.'", "'.$tr_no.'", "'.$ms_data->ref_no.'", "'.$item_id.'", "'.$item_data->unit_name.'", "'.$total_unit.'", "'.$item_inv_price.'", "'.$total_amt.'", "'.$entry_at.'", "'.$entry_by.'", "0", "0000-00-00 00:00:00")';
	
mysqli_query($conn,$ins_sql);


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

?>

<?php }


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

{

$tr_no=$_POST['tr_no'];
$mobile_no=$_POST['mobile_no'];

if($_POST['coupon_no']>0){
$coupon_no=$_POST['coupon_no'];
}else{
$coupon_no=0;
}


  $up_sql = 'update journal_master  set mobile_no="'.$mobile_no.'", coupon_no="'.$coupon_no.'"  where tr_no='.$tr_no;
mysqli_query($conn,$up_sql);


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

?>

<?php } 

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

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

$ms_sql="SELECT * FROM journal_master  WHERE tr_no='".$tr_no."'";
$ms_data = find_all_field_sql($conn,$ms_sql);

$jv_date=$ms_data->jv_date;
$tr_from=$ms_data->tr_from;
$jv_title=find_a_field($conn,'journal_type','jv_title','id="'.$ms_data->jv_type.'"');

		$YR = date('Y',strtotime($jv_date));
 		$year = date('y',strtotime($jv_date));
  		$month = date('m',strtotime($jv_date));
 		$inv_cy_id = find_a_field($conn,'journal_master','max(jv_id)','year="'.$YR.'" and jv_type="'.$ms_data->jv_type.'"')+1;
  		$cy_id = sprintf("%06d", $inv_cy_id);
   		$jv_no=''.$year.''.$month.''.$ms_data->jv_type.''.$cy_id;
		$view_jv_no=$jv_title.''.$jv_no;

 $sql = 'select * from  cash_bank_ledger where status="Active" and id!=11 order by id';
$query = mysqli_query($conn,$sql);
while ($data = mysqli_fetch_object($query)) {

if($_POST['receive_amt_'.$data->id]>0)
{
$receive_amt= $_POST['receive_amt_'.$data->id];

   $ins_sqljv = 'INSERT INTO sales_receive_data (tr_no, jv_no, ledger_id, order_no, ledger_name, receive_amt, entry_at, entry_by) VALUES

("'.$tr_no.'", "'.$jv_no.'", "'.$data->ledger_id.'", "'.$data->id.'", "'.$data->ledger_name.'", "'.$receive_amt.'", "'.$entry_at.'", "'.$entry_by.'")';
	
mysqli_query($conn,$ins_sqljv);
}
}

$customer_cash=$_POST['customer_cash'];


if($customer_cash>0) {
 $cust_sql = 'INSERT INTO sales_receive_data (tr_no, jv_no, ledger_id, order_no, ledger_name, receive_amt, entry_at, entry_by) VALUES

("'.$tr_no.'", "'.$jv_no.'", "0", "11", "Cusotomer Cash", "'.$customer_cash.'", "'.$entry_at.'", "'.$entry_by.'")';
	
mysqli_query($conn,$cust_sql);

}


$up_jm='update journal_master set jv_no="'.$jv_no.'",  year="'.$YR.'", jv_id="'.$inv_cy_id.'", view_jv_no="'.$view_jv_no.'", remarks="'.$special_nots.'", 
status="'.$status.'", checked_by="'.$entry_by.'", checked_at="'.$entry_at.'"
 where tr_no="'.$tr_no.'"';
mysqli_query($conn,$up_jm);

$up_sd='update sales_invoice set jv_no="'.$jv_no.'" where tr_no="'.$tr_no.'"';
mysqli_query($conn,$up_sd);

if($jv_no>0){
auto_insert_invoice_secoundary_journal($conn,$jv_no);
}

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

?>

<?php } ?>




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

<!-- ======= <head> ======= -->
 <?php require_once('inc_files/inc_head.php'); ?>
<!-- ======= </head> ======= -->
 <?php
  do_calander('#tr_date');
  do_calander('#delivery_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; 
var discount_amt=(document.getElementById('discount_amt_'+id).value)*1; 

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

document.getElementById('amt_after_discount_'+id).value=(total_amt-discount_amt);

}


function TR_discount_cal(id){

var discount=(document.getElementById('discount_'+id).value)*1; 
var total_amt=(document.getElementById('total_amt_'+id).value)*1; 
var discount_amt=document.getElementById('discount_amt_'+id).value=(total_amt*discount)/100; 

document.getElementById('amt_after_discount_'+id).value=(total_amt-discount_amt);

}


function TR_discount_amt(id){

var discount_amt=(document.getElementById('discount_amt_'+id).value)*1; 
var total_amt=(document.getElementById('total_amt_'+id).value)*1; 


document.getElementById('amt_after_discount_'+id).value=(total_amt-discount_amt);

}




function SUMcalculation(id){

var tot_total_amt = 0;
var tot_discount_amt = 0;
var tot_amt_after_discount = 0;

<?php
	$sql = 'select  c.*, i.item_name from  sales_invoice c, item_info i
	where c.item_id=i.item_id and c.tr_no="'.$_REQUEST['tr_no'].'" group by id order by id desc';
	
$query = mysqli_query($conn,$sql);
while ($data = mysqli_fetch_object($query)) {

?>
tot_total_amt = tot_total_amt  + document.getElementById('total_amt_<?php echo $data->id?>').value*1;
tot_discount_amt = tot_discount_amt  + document.getElementById('discount_amt_<?php echo $data->id?>').value*1;
tot_amt_after_discount = tot_amt_after_discount  + document.getElementById('amt_after_discount_<?php echo $data->id?>').value*1;
<?php }?>

document.getElementById('tot_total_amt').value = tot_total_amt;
document.getElementById('tot_discount_amt').value = tot_discount_amt;
document.getElementById('tot_amt_after_discount').value = tot_amt_after_discount;
}




function SUMreceiveamt(id){

var tot_receive_amt = 0;

<?php
$sql = 'select * from  cash_bank_ledger where status="Active" and id!=11 order by id';
	
$query = mysqli_query($conn,$sql);
while ($data = mysqli_fetch_object($query)) {

?>
tot_receive_amt = tot_receive_amt  + document.getElementById('receive_amt_<?php echo $data->id?>').value*1;

<?php }?>

document.getElementById('tot_receive_amt').value = tot_receive_amt;

}


function customer_cash_cal(){

var tot_amt_after_discount=(document.getElementById('tot_amt_after_discount').value)*1; 
var tot_receive_amt=(document.getElementById('tot_receive_amt').value)*1; 
var customer_cash=(document.getElementById('customer_cash').value)*1;

document.getElementById('return_cash').value=(customer_cash-tot_receive_amt);

}





	function update_value(id)
	{
var order_no=id;
var unit_price=(document.getElementById('unit_price_'+id).value); 
var total_unit=(document.getElementById('total_unit_'+id).value); 
var total_amt=(document.getElementById('total_amt_'+id).value); 
var discount=(document.getElementById('discount_'+id).value); 
var discount_amt=(document.getElementById('discount_amt_'+id).value); 
var amt_after_discount=(document.getElementById('amt_after_discount_'+id).value); 
var remarks=(document.getElementById('remarks_'+id).value); 

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

var strURL="1001_pos_sales_entry_ajax.php?order_no="+order_no+"&unit_price="+unit_price+"&total_unit="+total_unit+"&total_amt="+total_amt+"&discount="+discount+"&discount_amt="+discount_amt+"&amt_after_discount="+amt_after_discount+"&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">Sales 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>Sales Org Information</strong></h5>
			 
              <form class="row g-3" action="" method="post" name="codz" id="codz" onSubmit="return check()">
	 
                
				
				
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong> Company Name</strong></label>
           <select id="group_for" name="group_for"  class="form-select" >
                  
                   <?php foreign_relation($conn,'user_group','id','group_name',$_POST['group_for'],'1');?>
                 </select>
			    
                </div>
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong>Country</strong></label>
     		 <input type="text" placeholder="Country"  class="form-control" id="country" name="country" value="Bangladesh" >
			   
                </div>
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong>Delivery Plan</strong></label>
     		 <input type="text" placeholder="Delivery Plan"  class="form-control" id="delivery_plan" name="delivery_plan" value="" >
			   
                </div>
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong> Warehouse</strong></label>
           <select id="warehouse_id" name="warehouse_id"  class="form-select" >
                   <option></option>
                   <?php foreign_relation($conn,'warehouse','warehouse_id','warehouse_name',$_POST['warehouse_id'],'1');?>
                 </select>
			    
                </div>
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong>Ship of Pickup</strong></label>
     		  <select id="warehouse_id" name="warehouse_id"  class="form-select" >
                   <option></option>
                   <?php foreign_relation($conn,'warehouse','warehouse_id','warehouse_name',$_POST['warehouse_id'],'1');?>
                 </select>
			   
                </div>
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong>Business Model</strong></label>
     		 <input type="text" placeholder="Business Model"  class="form-control" id="delivery_plan" name="delivery_plan" value="" >
			   
                </div>
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong> Currency </strong></label>
           <select id="currency_type" name="currency_type"  class="form-select" >
                   <option></option>
                   <?php foreign_relation($conn,'currency_type','id','currency_type',$_POST['currency_type'],'1');?>
                 </select>
			    
                </div>
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong>Order Type</strong></label>
     		  <select id="order_type" name="order_type"  class="form-select" >
                   <option></option>
                   <?php foreign_relation($conn,'sales_type','id','sales_type',$_POST['warehouse_id'],'1');?>
                 </select>
			   
                </div>
				
				<div class="col-md-4">
                
                </div>
				
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong> Customer Name</strong></label>
           <select id="ref_no" name="ref_no"  class="form-select" >
                 
                   <?php foreign_relation($conn,'accounts_reference','ref_no','display_name',$_POST['ref_no'],'ref_no="'.$ms_data->ref_no.'"');?>
                 </select>
			    
                </div>
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong>Delivery Address</strong></label>
     		   <input type="text" placeholder="Delivery Address"  class="form-control" id="delivery_plan" name="delivery_plan" value="" >
			   
                </div>
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong>Payment Term</strong></label>
     		 <input type="text" placeholder="Payment Term"  class="form-control" id="delivery_plan" name="delivery_plan" value="" >
			   
                </div>
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong>Required  Delivery Date</strong></label>
         <input type="text"   class="form-control" id="delivery_date" name="delivery_date" value="" >
			    
                </div>
				
				<div class="col-md-4">
                 <label for="first_name" class="form-label"><strong>Remarks</strong></label>
         <input type="text" placeholder="Remarks"  class="form-control" id="remarks" name="remarks" value="" >
			    
                </div>
				
				<div class="col-md-4" style="text-align:center">
                 <label for="first_name" class="form-label"><strong>&nbsp;</strong></label><br />
     	 <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> MS Data&nbsp;&nbsp;</button>
			   
                </div>
				
			
				
				
               
  </form>
			  
			  
			   <?php if($ms_data->coupon_no>0) {?>
			  <table class="table table-bordered">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th width="28%" scope="col">Coupon#</th>
                    <th width="28%" scope="col">Total Invoice </th>
                    <th width="28%" scope="col">Discount</th>
                  </tr>
                </thead>
				<?php
				
		
		
		$sql = 'select m.coupon_no, count(DISTINCT m.jv_no) as inv_qty, sum(s.discount_amt) as discount_amt from journal_master m, sales_invoice s
		 where m.jv_no=s.jv_no and m.coupon_no="'.$ms_data->coupon_no.'" and s.discount_amt>0 and m.year>0 group by  m.coupon_no';
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $inv_qty[$info->coupon_no]=$info->inv_qty;
		 $discount_amt[$info->coupon_no]=$info->discount_amt;
		}
		
				?>
                <tbody>
                  <tr>
                    <th scope="row">
		<select id="coupon_no" name="coupon_no"  class="form-select"  style="width: 100%;" >
		<?php foreign_relation($conn,'coupon_info','coupon_no','coupon_no_view',$ms_data->coupon_no,'coupon_no="'.$ms_data->coupon_no.'" order by year, coupon_id');?>
        </select>					</th>
                    <th scope="row">
				<input type="number" class="form-control" name="inv_qty" id="inv_qty" value="<?=$inv_qty[$ms_data->coupon_no]?>"  style="width:95%;" readonly="">
					</th>
                    <th scope="row">
				<input type="text" class="form-control" name="dis_amt" id="dis_amt" value="<?=$discount_amt[$ms_data->coupon_no]?>"  style="width:95%;"readonly="">
					</th>
                  </tr>
                </tbody>
              </table>
			  <?php }?>

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

          <div class="card">
            <div class="card-body">
			
			
			<br>
			
			
			<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="8%" scope="col">Unit Price</th>
                    <th width="8%" scope="col">Quantity</th>
                    <th width="8%" scope="col">Amount</th>
                    <th width="8%" scope="col"><div align="center">%</div></th>
                    <th width="8%" scope="col">Discount</th>
                    <th width="8%" scope="col">Net Amt </th>
                    <th width="8%" scope="col">ACTION</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <th scope="row">
					<input type="hidden" class="form-control" id="tr_no" name="tr_no" required value="<?php echo $_REQUEST['tr_no'];?>" style=" width: 95%" >
					
					<select id="item_id" name="item_id"  class="form-select"  style="width: 100%;" required>
					<option></option>
                     
						<?php foreign_relation($conn,'item_info i, item_category s','i.item_id','concat(i.sku_code," :: ",i.item_name)',$item_id,
						'i.item_category=s.id and s.item_group=1 group by i.item_id order by i.item_category, i.sku_code');?>
                    </select>					</th>
                    <td><input type="text" class="form-control" id="unit_price" name="unit_price" required value="" style=" width: 95%" ></td>
                    <td><input type="text" class="form-control" id="total_unit" name="total_unit" required value="" style=" width: 95%" ></td>
                    <td><input type="text" class="form-control" id="total_amt" name="total_amt" required value="" style=" width: 95%" ></td>
                    <td><input type="text" class="form-control" name="discount_<?php echo $data->id; ?>" id="discount_<?php echo $data->id; ?>" value="<?php echo $data->discount; ?>"  style="width:70px;"  onKeyUp="TR_discount_cal(<?php echo $data->id?>), SUMcalculation(<?php echo $data->id?>)"></td>
                    <td><input type="text" class="form-control" name="discount_amt_<?php echo $data->id; ?>" id="discount_amt_<?php echo $data->id; ?>" value="<?php echo $data->discount_amt; $tot_discount_amt +=$data->discount_amt;?>" required style="width:100px;" onKeyUp="TR_discount_amt(<?php echo $data->id?>), SUMcalculation(<?php echo $data->id?>)"></td>
                    <td><input type="text" class="form-control" name="amt_after_discount_<?php echo $data->id; ?>" id="amt_after_discount_<?php echo $data->id; ?>" value="<?php echo $amt_after_discount=$data->total_amt-$data->discount_amt;  $tot_amt_after_discount +=$data->total_amt-$data->discount_amt;?>"  style="width:100px;" readonly=""></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 Item&nbsp;&nbsp;</button>					</td>
                  </tr>
                </tbody>
              </table>
                
<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="2%" scope="col">SL</th>
                    <th width="10%" scope="col">Item</th>
                    <th width="11%" scope="col">Price</th>
                    <th width="9%" scope="col" style="white-space:nowrap;">Quantity</th>
                    <th width="12%" scope="col">Amount</th>
                    <th width="5%" scope="col"><div align="center">%</div></th>
                    <th width="5%" scope="col">Discount</th>
                    <th width="12%" scope="col" style="white-space:nowrap">Net Amt </th>
                    <th width="14%" scope="col">Remarks</th>
                    <th width="8%" scope="col">Action</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['calf_status_src']==''){
						$status_id_con=" and c.status_id=1";
					}elseif($_REQUEST['calf_status_src']>0){
						$status_id_con=" and c.status_id='".$_REQUEST['calf_status_src']."'";
					}
					
					
					
					if($_REQUEST['cattle_id_src']!='')
					$cattle_id=" and c.mother_id='".$_REQUEST['cattle_id_src']."'";
					
		
					
						  $sql = 'select  c.*, i.item_name from  sales_invoice c, item_info i
						where c.item_id=i.item_id and c.tr_no="'.$_REQUEST['tr_no'].'" group by id order by id desc';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                 <tr>
                   <td style="white-space: nowrap;"><?php echo $sl++;?></td>
                   <td style="white-space: nowrap;"><?php echo $data->item_name; ?></td>
                   <td style="white-space: nowrap;"><?php echo $data->unit_price; ?>
				  <input type="hidden" class="form-control" name="unit_price_<?php echo $data->id; ?>" id="unit_price_<?php echo $data->id; ?>"
				   value="<?php echo $data->unit_price; ?>"  style="width:70px;"> </td>
                <td style="white-space: nowrap;">
<input type="text" class="form-control" name="total_unit_<?php echo $data->id; ?>" id="total_unit_<?php echo $data->id; ?>" value="<?php echo $data->total_unit; $tot_total_unit +=$data->total_unit; ?>" required style="width:70px;" onKeyUp="TR_calculation(<?php echo $data->id?>), SUMcalculation(<?php echo $data->id?>)" ></td>
                   <td><input type="text" class="form-control" name="total_amt_<?php echo $data->id; ?>" id="total_amt_<?php echo $data->id; ?>" value="<?php echo $data->total_amt; $tot_total_amt +=$data->total_amt;?>" required style="width:100px;" readonly=""></td>
                   <td bgcolor="#00FF00"><input type="text" class="form-control" name="discount_<?php echo $data->id; ?>" id="discount_<?php echo $data->id; ?>" value="<?php echo $data->discount; ?>"  style="width:70px;"  onKeyUp="TR_discount_cal(<?php echo $data->id?>), SUMcalculation(<?php echo $data->id?>)"></td>
                   <td bgcolor="#00FF00"><input type="text" class="form-control" name="discount_amt_<?php echo $data->id; ?>" id="discount_amt_<?php echo $data->id; ?>" value="<?php echo $data->discount_amt; $tot_discount_amt +=$data->discount_amt;?>" required style="width:100px;" onKeyUp="TR_discount_amt(<?php echo $data->id?>), SUMcalculation(<?php echo $data->id?>)"></td>
                   <td><input type="text" class="form-control" name="amt_after_discount_<?php echo $data->id; ?>" id="amt_after_discount_<?php echo $data->id; ?>" value="<?php echo $amt_after_discount=$data->total_amt-$data->discount_amt;  $tot_amt_after_discount +=$data->total_amt-$data->discount_amt;?>"  style="width:100px;" readonly=""></td>
                   <td>
				   <div align="center">
				     <input name="remarks_<?php echo $data->id; ?>" type="text" id="remarks_<?php echo $data->id; ?>"  value="<?php echo $data->remarks; ?>" style="width:150px" class="form-control"/>
				   </div></td>
                   <td><span id="divi_<?php echo $data->id?>">
				   <?php if($ms_data->status!='CHECKED') {?>
			  <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:60px; height:30px; background-color: #FF3366; font-weight:700; font-size:12px;"/>
			   <?php } ?>
			  
          </span></td>
                 </tr>
                 
				  <?php } ?>
				  
				  <tr>
                   <td colspan="3" style="white-space: nowrap;"><div align="center"><strong>TOTAL</strong></div></td>
                   <td style="white-space: nowrap;"><input type="text" class="form-control" name="tot_total_unit" id="tot_total_unit" value="<?php echo $tot_total_unit; ?>"  style="width:70px; height:32px;"  readonly="" ></td>
                   <td><input type="text" class="form-control" name="tot_total_amt" id="tot_total_amt" value="<?php echo $tot_total_amt; ?>"  style="width:100px; height:32px;"  readonly="" ></td>
                   <td bgcolor="#00FF00">&nbsp;</td>
                   <td bgcolor="#00FF00"><input type="text" class="form-control" name="tot_discount_amt" id="tot_discount_amt" value="<?php echo $tot_discount_amt; ?>"  style="width:100px; height:32px;"  readonly="" ></td>
                   <td><input type="text" class="form-control" name="tot_amt_after_discount" id="tot_amt_after_discount" value="<?php echo $tot_amt_after_discount; ?>"  style="width:100px; height:32px;"  readonly="" ></td>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                 </tr>
                </tbody>
              </table>
              </form>
			

              <!-- Table with stripped rows id="example" -->
              
			  
			
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        <?php /*?><div class="col-lg-4"> 

          <div class="card">
            <div class="card-body">
			
			<br>
			
		
			
             <table class="table table-bordered">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th colspan="2" scope="col">Sales Summari </th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <th width="70%" scope="row">MRP Total</th>
                    <td width="30%"><input type="text" class="form-control" name="invoice_amt" id="invoice_amt" value="" required style="width:100px;">					</td>
                  </tr>
                  <tr>
                    <th scope="row">Discount (-)</th>
                    <td><input type="text" class="form-control" name="invoice_amt" id="invoice_amt" value="" required style="width:100px;"></td>
                  </tr>
                  <tr>
                    <th scope="row">VAT (+)</th>
                    <td><input type="text" class="form-control" name="invoice_amt" id="invoice_amt" value="" required style="width:100px;"></td>
                  </tr>
                  <tr>
                    <th scope="row">TOTAL</th>
                    <td><input type="text" class="form-control" name="invoice_amt" id="invoice_amt" value="" required style="width:100px;"></td>
                  </tr>
                </tbody>
              </table>
			  
			 
			
			</div>
		</div>
	</div><?php */?>
	<form  action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:12px;">
	
	 <div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
			<br>
			
				<table class="table table-bordered">
				<tr>
					<td width="60%" valign="top">
					
					<table class="table table-bordered">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th scope="col">Special Nots</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <th scope="row">
                      <textarea id="special_nots" name="special_nots" rows="4" cols="50" style="width:100%"><?php echo $ms_data->remarks?></textarea>				</th>
                    </tr>
                </tbody>
              </table>
					
				  </td>
					<td width="40%">
						<table class="table table-bordered">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th scope="col">Payment By </th>
                    <th scope="col">Amount</th>
                  </tr>
                </thead>
                <tbody>
				<?php
				
							 $sql = 'select order_no, receive_amt as sales_rec_amt  from sales_receive_data where tr_no="'.$_REQUEST['tr_no'].'" group by order_no ';
							 $query = mysqli_query($conn,$sql);
							 while($info=mysqli_fetch_object($query)){
							 $sales_rec_amt[$info->order_no]=$info->sales_rec_amt;
							}
							
				  		 $sql = 'select * from  cash_bank_ledger where status="Active" and id!=11 order by id';
						$query = mysqli_query($conn,$sql);
						while ($bank_data = mysqli_fetch_object($query)) {

					?>
                  <tr>
                    <th width="70%" ><?php echo $bank_data->ledger_name;?></th>
                    <td width="30%"><input type="text" class="form-control" name="receive_amt_<?php echo $bank_data->id; ?>" id="receive_amt_<?php echo $bank_data->id; ?>" value="<?php echo $sales_rec_amt[$bank_data->id]; $tot_receive_amt +=$sales_rec_amt[$bank_data->id];?>"  onKeyUp="SUMreceiveamt(<?php echo $data->id?>)" style="width:100px;"  autocomplete="off">					</td>
                  </tr>
                  
				  
				  
				  <?php }?>
				  
				  
				  <tr  bgcolor="#EDE9FE">
                    <th>Received Amt</th>
                    <td> 
					<input type="hidden" class="form-control" name="tot_receive_amt" id="tot_receive_amt" value="<?php echo $tot_receive_amt; ?>"  style="width:100px; height:32px;"  readonly="" >
					<input type="text" class="form-control" name="customer_cash" id="customer_cash" value="<?php echo $sales_rec_amt[11]; ?>"  style="width:100px; height:32px; font-weight:700;"  onKeyUp="customer_cash_cal()" ></td>
                  </tr>
				  
				  <tr  bgcolor="#EDE9FE">
                    <th>Return Amt</th>
                    <td> <input type="text" class="form-control" name="return_cash" id="return_cash" 
					value="<?php echo $return_cash=$sales_rec_amt[11]-$tot_receive_amt; ?>"  style="width:100px; height:32px; font-weight:700;"  readonly="" ></td>
                  </tr>
				  
				  <tr>
                    <td colspan="2" scope="row" align="left">
						 <br><br>
			
 <input type="hidden" class="form-control" id="tr_no" name="tr_no" required value="<?php echo $_REQUEST['tr_no'];?>" style=" width: 95%" >

           <?php if($ms_data->status!='CHECKED') {?><button type="submit" class="btn btn-success btn-sm" name="confirm_data" id="confirm_data" >Save &amp; Publish</button><?php }?>&nbsp;&nbsp;
				<?php if($ms_data->status=='CHECKED') {?>
				<a href="view_file/sales_pos_print_view.php?jv_no=<?php echo $ms_data->jv_no;?>" target="_blank">
				<button type="button" class="btn btn-primary btn-sm" >Print</button></a><?php }?>
				<button type="reset" class="btn btn-danger btn-sm" onClick="parent.location='<?php echo $status_page?>'" >EXIT&nbsp;</button>				</td>
                  </tr>
                </tbody>
              </table>
					</td>
				</tr>
			</table>
			
			
				
			</div>
		</div>
	</div>
	</form>		
			
      </div>
    </section>

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

 <!-- ======= Footer ======= -->
 <?php require_once('inc_files/inc_footer.php'); ?>
<!-- ======= Footer ======= -->
<?php selected_two("#item_id"); ?>
<?php selected_two("#coupon_no"); ?>
<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>