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

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

$page_title="Sales Invoice";
$page="43_customer_so.php?active_id=".$active_id."&menu_id=".$menu_id."";
$status_page="47_customer_invoice_status.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='journal_master';	
$unique='jv_no';

$request_no=$_REQUEST['tr_no'];

$ms_data=mysqli_fetch_object(mysqli_query($conn,'select * from '.$table.' where tr_no="'.$request_no.'"'));

// End Common Information





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

{
//$_POST['entry_by'] = $_SESSION['user']['id'];

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

$jv_date=$_POST['invoice_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.''.$jv_type.''.$cy_id;
		$view_jv_no=$jv_title.''.$jv_no;	
		
$status='CHECKED';
$entry_by =  $_SESSION['user']['id'];
$entry_at = date('Y-m-d H:i:s');

 $sql = 'select i.* from item_info i where i.item_category=1 order by i.item_id ';
$query = mysqli_query($conn,$sql);
while ($data = mysqli_fetch_object($query)) {

if($_POST['total_amt_'.$data->item_id]>0)
{
$item_id= $_POST['item_id_'.$data->item_id];
$unit_price= $_POST['unit_price_'.$data->item_id];
$total_unit= $_POST['total_unit_'.$data->item_id];
$total_amt= $_POST['total_amt_'.$data->item_id];

$invoice_amt +=$total_amt;

 $ins_sqljv = 'INSERT INTO  sales_invoice (jv_no, jv_date, tr_no, group_for, warehouse_id, ref_no, emp_code, item_id, unit_name, total_unit, unit_price, total_amt, tr_from,  status, entry_at, entry_by, edit_at, edit_by, checked_at, checked_by, approve_by, approve_at) VALUES

("'.$jv_no.'", "'.$jv_date.'", "'.$ms_data->tr_no.'", "'.$ms_data->group_for.'", "'.$ms_data->warehouse_id.'", "'.$ms_data->ref_no.'", "'.$ms_data->emp_code.'", "'.$item_id.'", "'.$data->unit_name.'", "'.$total_unit.'", "'.$unit_price.'", "'.$total_amt.'", "'.$ms_data->tr_from.'", "'.$status.'", "'.$entry_at.'", "'.$entry_by.'", "0000-00-00", "0", "'.$entry_at.'", "'.$entry_by.'", "0", "0000-00-00")';
	
mysqli_query($conn,$ins_sqljv);
}
}

//if($invoice_amt>0){
 $up_jm='update journal_master set jv_no="'.$jv_no.'", jv_date="'.$jv_date.'", year="'.$YR.'", jv_id="'.$inv_cy_id.'", view_jv_no="'.$view_jv_no.'", status="'.$status.'"
 where tr_no="'.$request_no.'"';
mysqli_query($conn,$up_jm);
//}

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

header("location:".$status_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 TR_calculation(item_id){

var total_unit=(document.getElementById('total_unit_'+item_id).value)*1; 
var unit_price=(document.getElementById('unit_price_'+item_id).value)*1; 

document.getElementById('total_amt_'+item_id).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>


<script language="javascript">

function SUMcalculation(item_id){
var invoice_amt = 0;

<?php

$sql = "select i.* from item_info i where i.item_category=1 order by i.item_id ";
$query = mysqli_query($conn,$sql);
while($data=mysqli_fetch_object($query)){
?>
invoice_amt = invoice_amt + document.getElementById('total_amt_<?php echo $data->item_id?>').value*1;;
<?php
}

?>
document.getElementById('invoice_amt').value = invoice_amt;



}


</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-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:14px;">
                <div class="col-md-6">
                 <label for="first_name" class="form-label"><strong>Invoice No</strong></label>
                 <input type="text" class="form-control" id="jv_no" name="jv_no" value="<?php if($request_no>0){ echo $ms_data->jv_no; } ?>" readonly="" >
			    
		<input type="hidden" class="form-control" id="warehouse_id" name="warehouse_id" value="<?php echo $_SESSION['user']['depot']; ?>" required readonly="">
                </div>
				
				<div class="col-md-6">
                 <label for="last_name" class="form-label"><strong>Invoice Date</strong></label>
                 
				<?php if($request_no>0){?> 
			    <input type="text" class="form-control" id="invoice_date" name="invoice_date" value="<?php echo $ms_data->jv_date; ?>" required >
			   <?php } else {?>
			   <input type="text" class="form-control" id="invoice_date" name="invoice_date" value="" required>
			   <?php }?>
				 
                </div>
				
				
				
				
				
				
				 <div class="col-md-12">
                 <label for="first_name" class="form-label"><strong>Customer</strong></label>
				 
				
			<?php if($request_no>0){?>
				 
			<select id="acc_reference" name="acc_reference"  class="form-select"  required  >
			
			<?php foreign_relation($conn,'accounts_reference','ref_no','concat(ref_no," :: ",company_name)',$_POST['acc_reference'],'ref_no="'.$ms_data->ref_no.'" ');?>
		
        	</select>
               
				<?php } else {?>
				
				<?php 
				 auto_complete_from_db_sql($conn,'SELECT concat(a.ref_no," :: ",a.company_name), concat(a.ref_no," :: ",a.company_name) FROM accounts_reference a
					 WHERE ref_type=2','acc_reference'); 
				 ?>
				
                  <input type="text" class="form-control" id="acc_reference" name="acc_reference" value="" required>
				<?php }?>	
				
				 </div>
				
				<?php /*?><div class="col-md-6">
                 <label for="last_name" class="form-label"><strong>Employee</strong></label>
				 
			<?php if($request_no>0){?>
				 
			<select id="emp_code" name="emp_code"  class="form-select"  required  >
			
			<?php foreign_relation($conn,'employee_info','emp_code','concat(emp_code," :: ",emp_name)',$_POST['emp_code'],'emp_code="'.$ms_data->emp_code.'" ');?>
		
        	</select>
               
				<?php } else {?>
				
				<?php
			 auto_complete_from_db_sql($conn,'SELECT concat(emp_code," :: ",emp_name), concat(emp_code," :: ",emp_name) FROM employee_info
			WHERE 1 order by emp_code','employee_name');	?>
                  <input type="text" class="form-control" id="employee_name" name="employee_name" value="<?php echo $_REQUEST['employee_name']?>" >	
				<?php }?>	
                </div>
				
				
				<div class="col-md-6">
                 <label for="last_name" class="form-label"><strong>Subject</strong></label>
                 
				<input type="text" class="form-control" id="inv_subject" name="inv_subject" value="<?php if($request_no>0){ echo $ms_data->inv_subject; }?>" required>
                </div><?php */?>
				
				
				
				<div class="col-md-12">
                 <label for="last_name" class="form-label"><strong>Remarks</strong></label>
                   <textarea class="form-control" style="height: 80px" id="inv_details" name="inv_details" ><?php if($request_no>0){ echo $ms_data->inv_details; } ?></textarea>
                </div>
				
				
		
                

             

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






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

          <div class="card">
            <div class="card-body">
              
              <br/>
              <!-- Bordered Table -->
			
              <table class="table table-bordered " style="font-size:12px;">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th width="3%" scope="col">SL</th>
                    <th width="27%" scope="col">PRODUCT Name </th>
                    <th width="10%" scope="col">UOM</th>
                    <th width="15%" scope="col">Price</th>
                    <th width="15%" scope="col">Quantity</th>
                    <th width="15%" scope="col"><center>AMOUNT</center></th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
					$sl=1;
					
		
					
						$sql = 'select i.* from item_info i where i.item_category=1 order by i.item_id ';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {
						
						

					?>
				<?php /*?>onKeyUp="SUMcalculation(<?php echo $data->item_id;?>), TR_calculation(<?php echo $data->item_id;?>)" <?php */?>
                  <tr>
                    <td scope="row"><?php echo $sl++;?></td>
                    <td><?php echo $data->item_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:95%" class="form-control"/>
					</td>
                    <td><?php echo $data->unit_name;?></td>
                    <td> <input name="unit_price_<?php echo $data->item_id?>" type="text" id="unit_price_<?php echo $data->item_id?>"
				   value="<?php echo $data->sale_price;?>" onKeyUp="TR_calculation(<?php echo $data->item_id?>)"  style="width:95%" class="form-control"/></td>
                    <td><input name="total_unit_<?php echo $data->item_id?>" type="text" id="total_unit_<?php echo $data->item_id?>"value="" 
					onKeyUp="TR_calculation(<?php echo $data->item_id;?>)"  style="width:95%" class="form-control"/></td>
                    <td align="right"><input name="total_amt_<?php echo $data->item_id?>" type="text" id="total_amt_<?php echo $data->item_id?>" value=""
					   readonly="" style="width:95%" 
					 class="form-control"/></td>
                  </tr>
				  
				   <?php }?>

                  <?php /*?><tr>
                    <td colspan="5" scope="row" valign="middle" align="right"><div align="right"><strong>Invoice Amt </strong></div></td>
                    <td align="right"><input name="invoice_amt" id="invoice_amt" type="text" class="form-control" readonly=""  value="<?php echo $invoice_amt?>"  /></td>
                  </tr><?php */?>
                  
                  
                  
               
			   
                  <tr>
                    <td colspan="6" scope="row" align="left">
						 <br><br><br><br>
			
 

                  <button type="submit" class="btn btn-success btn-sm" name="confirm_data" id="confirm_data" >Save &amp; Publish</button>&nbsp;&nbsp;
				 <button type="reset" class="btn btn-primary btn-sm" onClick="parent.location='<?php echo $status_page?>'" >Clear&nbsp;&nbsp;</button>				</td>
                  </tr>
                  
				   
                </tbody>
              </table>
			
              <!-- End Bordered Table -->
			  

            </div>
          </div>

          

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

  </main><!-- End #main -->
 </form>  
 <!-- ======= Footer ======= -->
 <?php require_once('inc_files/inc_footer.php'); ?>
<!-- ======= Footer ======= -->

</body>

</html>