<?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
}


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


$unique_id = randomPassword();	

$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
$invoice_no=$_POST['invoice_no'];
$delivery_challan_date=$_REQUEST['tax_id'];



$name_of_goods=$_POST['name_of_goods'];
$delivery_note=$_POST['delivery_note'];
$mode_of_payment=$_POST['mode_of_payment'];
$suppelier_ref=$_POST['suppelier_ref'];
$other_ref=$_POST['other_ref'];
$terms_of_delivery=$_POST['terms_of_delivery'];
$mode_transport=$_POST['mode_transport'];
//$invoice_no=$_POST['invoice_no'];
 $invoice_date=$_POST['invoice_date'];






date_default_timezone_set('Asia/Calcutta');
$today = date("Y-m-d");
$time = date("h:i A"); 
$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";


$query2="select * from invoice_more_02 where tax_id = '".$_REQUEST['tax_id']."' AND customer_id = '".$_REQUEST['customer_id']."'  ORDER BY tax_more_product_id desc";//echo $query2;
$result=mysqli_query($conn,$query2);
$num_rows = mysqli_num_rows($result);
while($row=mysqli_fetch_array($result))
{  
$tax_more_product_id = $row['tax_more_product_id'];
$amount =  $_POST['amount'.$tax_more_product_id];
$description = mysqli_real_escape_string($conn,$_POST['description'.$tax_more_product_id]);

$qty = $_POST['qty'.$tax_more_product_id];
$rate = $_POST['rate'.$tax_more_product_id];
$HSN_code = $_POST['HSN_code'.$tax_more_product_id];


$query2hsn="SELECT * FROM HSN_code_master WHERE HSN_code = '$HSN_code'";
//echo $query2hsn;
$result1hsn = mysqli_query($conn,$query2hsn);
while($query3hsn=mysqli_fetch_array($result1hsn))
{
$CGST = $query3hsn['CGST'];
$SGST = $query3hsn['SGST'];
$IGST = $query3hsn['IGST'];
}





 $query1 = "update invoice_more_02 set invoice_no='$invoice_no',invoice_date='$invoice_date',amount='$amount',description='$description',rate='$rate', qty = '$qty',HSN_code ='$HSN_code',CGST_rate='$CGST',SGST_rate='$SGST',IGST_rate='$IGST'  where customer_id = '".$_REQUEST['customer_id']."' and tax_id = '".$_REQUEST['tax_id']."' and tax_more_product_id='$tax_more_product_id'" ;
//echo $query1;
$r1=mysqli_query($conn,$query1);

$i++; 
}



 $query1 = "update tax_invoice set invoice_no='$invoice_no',invoice_date='$invoice_date',delivery_note='$delivery_note',name_of_goods='$name_of_goods',mode_of_payment='$mode_of_payment',suppelier_ref='$suppelier_ref',other_ref='$other_ref' ,terms_of_delivery='$terms_of_delivery',mode_transport='$mode_transport' where customer_id = '".$_REQUEST['customer_id']."' and tax_id = '".$_REQUEST['tax_id']."' " ;
$r1=mysqli_query($conn,$query1);



	
if (isset($_POST['Submit'])=="Save")
{

$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";

$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',status='SAVE' where customer_id = '".$_REQUEST['customer_id']."' and tax_id = '".$_REQUEST['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();			
?>

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



}/////submit slosed
/*?>if (isset($_POST[PRO_ADD])=="ADD")
{


$delivery_challan_no=$_POST['delivery_challan_no'];
$delivery_challan_date=$_POST['delivery_challan_date'];




$delivery_challan_id=$_REQUEST['delivery_challan_id'];


//exit();

$product_id=$_REQUEST['product_id'];
if (is_numeric($product_id)) 
 {

$qry97 = "select * from finish_product_master where product_id='$product_id'";
$re97 = mysqli_query($conn,$qry97);
while($row=mysqli_fetch_array($re97))
{
$product_id = $row['product_id'];
$componant = $row['item_code'];
$description = $row['description'];
$bar_size = $row['bar_size'];
$material_name = $row['material_name'];
$row_id=$row['row_id'];
$finish_shaft_wt_kg=$row['finish_shaft_wt_kg'];
$revised_labour_rate_rs=$row['revised_labour_rate_rs'];

}
   $qryauto="insert into delivery_challan_more_product_02 (product_id,componant,description,bar_size,row_id,material_name,customer_id,customer_name,entry_by,entry_date,delivery_challan_id,finish_shaft_wt_kg,revised_labour_rate_rs) values ('$product_id','$componant','$description','$bar_size','$row_id','$material_name','$customer_id','$customer_name','$user_name','$today','$delivery_challan_id','$finish_shaft_wt_kg','$revised_labour_rate_rs')";
$re97 = mysqli_query($conn,$qryauto);




}

//exit();
?>


<script>
javascript:window.location.href="delivery_challan_01.php?customer_id=<?php echo $_REQUEST['customer_id']; ?>&&customer_name=<?php echo $_REQUEST['customer_name']?>&&delivery_challan_id=<?php echo $delivery_challan_id?>&&company_name=<? echo $_REQUEST['company_name']?>";
</script>
<?php } ?><?php */?>


