<?php 
include 'connection.php';
session_start();
if (!isset($_SESSION['is_logged_in'])) {
    header("Location:login.php");
    die();
    }
function randomPassword() {
    $alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 5; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}

$unique_id = randomPassword();	

date_default_timezone_set('Asia/Calcutta');
$today = date("d/m/y");
$today1 = date("d-m-Y");
$today2 = date("Y-m-d");
$received_time = date("Y-m-d H:i:s");

$currentyear = date("Y");
$currentmonth = date("m");

if($currentmonth>03) { 
$cuyear = date("Y");
$cuyear1 = date("y")+1;
} 
else { 
$cuyear = date("Y")-1;
$cuyear1 =  date("y");
} 
$f_year = "$cuyear"."-"."$cuyear1";



$querylogin = "select * from  mst_users where user_name = '". $_SESSION["LOGIN_NAME"]."' AND password = '".$_SESSION["EMPLOYEE_PASSWORD"]."'";
$resultlogin = mysqli_query($conn,$querylogin);
while($row=mysqli_fetch_array($resultlogin))
{
$user_name = $row['user_name'];
$user_id09 = $row['user_id'];
$login_type = $row['login_type'];
}	
?>



<?php


	$queryls = "select * from invoice_series where auto_no!='' and year='$f_year'  ORDER BY auto_id";
//echo $queryls;
$resultls = mysqli_query($conn,$queryls);    
 while($row=mysqli_fetch_array($resultls))
                                                    {
                                                    $auto_no = $row['auto_no'];
													 $company_name = $row['company_name'];
                                                    }
    
                                                     
$auto_no_new=$auto_no+1;
$cont_seriesa= sprintf("%04d", $auto_no_new);
$invoice_no1="$cont_seriesa"."-"."$f_year";


$invoice_date=$_POST['invoice_date'];
$delivery_challan_id=$_REQUEST['delivery_challan_id'];
$customer_id=$_REQUEST['customer_id'];
$from_date=$_REQUEST['datepicker'];
$to_date=$_REQUEST['datepicker1'];

$queryls = "select * from mst_customer where customer_id = '".$_REQUEST['customer_id']."'";
$resultls = mysqli_query($conn,$queryls);	

while($row=mysqli_fetch_array($resultls))

{

$customer_id=$row['customer_id'];
 $customer_name=$row['customer_name'];

$customer_address=$row['billing_address'];
$customer_state=$row['state'];
$customer_state_code=$row['state_code'];
$customer_email_id=$row['email_id'];
$customer_GST_no=$row['billing_GST_no'];
$customer_PAN_no = $row['billing_PAN_no'];
$customer_pincode = $row['pincode'];
$customer_tel_no=$row['tel_no'];

$vendor_code=$row['vendor_code'];
$buyer_address=$row['buyer_address'];
$buyer_GST_no=$row['buyer_GST_no'];
$buyer_PAN_no=$row['buyer_PAN_no'];
$buyer_state=$row['buyer_state'];
$buyer_state_code=$row['buyer_state_code'];
$buyer_name = $row['buyer_name'];
$customer_cell_no = $row['cell_no'];
}

  $value=$_POST['check_list'];
			   $invoice_done_challan_id=implode(',',$value);
if($_REQUEST['tax_id']==''){ 									

$qryauto="insert into tax_invoice(customer_id,customer_name,customer_address,customer_state,customer_state_code,customer_email_id,customer_GST_no,customer_PAN_no,customer_pincode,customer_tel_no,vendor_code,buyer_address,buyer_GST_no,buyer_PAN_no,buyer_state,buyer_state_code,buyer_name,customer_cell_no,entry_date,entry_by,unique_id,invoice_done_challan_id,from_date,to_date) values ('$customer_id','$customer_name','$customer_address','$customer_state','$customer_state_code','$customer_email_id','$customer_GST_no','$customer_PAN_no','$customer_pincode','$customer_tel_no','$vendor_code','$buyer_address','$buyer_GST_no','$buyer_PAN_no','$buyer_state','$buyer_state_code','$buyer_name','$customer_cell_no','$today2','$user_name','$unique_id','$invoice_done_challan_id','$from_date','$to_date')";
$rauto=mysqli_query($conn,$qryauto);
//echo $rauto;
//exit();
  $qry3z = "select * from tax_invoice where  unique_id='$unique_id' and customer_id='$customer_id' and entry_date='$today2'and invoice_done_challan_id='$invoice_done_challan_id'";
$re3z= mysqli_query($conn,$qry3z);
while($row=mysqli_fetch_array($re3z))
		{  
		$tax_id = $row['tax_id'];
		}
}else {

$tax_id=$_REQUEST['tax_id'];
}
 $tax_id;
//exit();
													foreach($_POST['check_list'] as $selected) { 
													
													
?>				<?php  $queryaapr = "SELECT * FROM  delivery_challan_more_product_02 where delivery_challan_more_product_id='$selected'";
$result1hsn = mysqli_query($conn,$queryaapr);
$num_rows2 = mysqli_num_rows($result1hsn);
while($row=mysqli_fetch_array($result1hsn))
{
$material_name = $row['material_name'];
$inward_challan_no = $row['inward_challan_no'];
$cust_more_row_id = $row['cust_more_row_id'];
$row_id = $row['row_id'];
$category_id = $row['category_id'];
$product_id=$row['product_id'];
$qry97 = "select * from finish_product_master where product_id='$product_id'";
$re97 = mysqli_query($conn,$qry97);
while($row=mysqli_fetch_array($re97))
{
$finish_HSN_code=$row['HSN_code'];
}
$query2hsn="SELECT * FROM HSN_code_master WHERE HSN_code = '$finish_HSN_code'";
//echo $query2hsn;
$result1hsn = mysqli_query($conn,$query2hsn);
while($query3hsn=mysqli_fetch_array($result1hsn))
{
$CGST_finish = $query3hsn['CGST'];
$SGST_finish = $query3hsn['SGST'];
$IGST_finish= $query3hsn['IGST'];
}

$queryls = "select * from category_master where category_id = '$category_id'";
$resultls = mysqli_query($conn,$queryls);	
while($row=mysqli_fetch_array($resultls))
{
$category_id=$row['category_id'];
$category_name=$row['category_name'];
$category_HSN_code=$row['HSN_code'];
}
$query2hsn="SELECT * FROM HSN_code_master WHERE HSN_code = '$category_HSN_code'";
//echo $query2hsn;
$result1hsn = mysqli_query($conn,$query2hsn);
while($query3hsn=mysqli_fetch_array($result1hsn))
{
$CGST_category = $query3hsn['CGST'];
$SGST_category = $query3hsn['SGST'];
$IGST_category= $query3hsn['IGST'];
}

$delivery_challan_no = $row['delivery_challan_no'];
$componant = $row['componant'];
$cust_po = $row['cust_po'];
$qty = $row['qty'];
$rate_of_po = $row['rate_of_po'];
$total = $row['total'];
$delivery_challan_more_product_id=$row['delivery_challan_more_product_id'];
 $sumoftotal+=$total;
$sumqty+=$qty;

$query1 = "update delivery_challan_more_product_02 set tax_id='$tax_id',invoice_no='$invoice_no1',invoice_date='$invoice_date',finish_HSN_code='$finish_HSN_code',CGST_finish='$CGST_finish',SGST_finish='$SGST_finish',IGST_finish='$IGST_finish',category_HSN_code='$category_HSN_code',CGST_category='$CGST_category',SGST_category='$SGST_category',IGST_category='$IGST_category' where delivery_challan_more_product_id='$selected'";
$r1=mysqli_query($conn,$query1);


}
												
 }								




//$qryauto="insert into invoice_more_02 (qty,amount,description,tax_id,customer_id,customer_name,entry_by,entry_date,invoice_done_challan_id) values ('$sumqty','$sumoftotal','$description','$tax_id','$customer_id','$customer_name','$user_name','$today2','$invoice_done_challan_id')";
//$re97 = mysqli_query($conn,$qryauto);




//$query1 = "update invoice_more_02 set invoice_no='$invoice_no1',status='SAVE'  where customer_id = '".$_REQUEST['customer_id']."' and tax_id = '".$_REQUEST['tax_id']."' " ;
//$r1=mysqli_query($conn,$query1);
	
	
$query1 = "update tax_invoice set invoice_no='$invoice_no1',invoice_date='$invoice_date',status='SAVE' where customer_id = '".$_REQUEST['customer_id']."' and tax_id = '$tax_id'" ;
$r1=mysqli_query($conn,$query1);

$query16cha = "update invoice_series set auto_no='$auto_no_new',invoice_no='$invoice_no1',year='$f_year' ";
$res16cha=mysqli_query($conn,$query16cha);

//exit();			


//exit();
?>


<script>
javascript:window.location.href="view_all_invoice.php";
</script>


