<?php
session_start();
include 'dbc.php';

$userid=$_SESSION['userid'];

$id=$_REQUEST["id"];
$sqlst = "SELECT * FROM addst where user_id = '$userid'";
$resultst=$conn->query($sqlst);
$rowst = $resultst->fetch_assoc();
$st=$rowst['cname'] ;


  $sqlcus = "select * from dorder where id='$id' and user_id='$userid'";
   $result=$conn->query($sqlcus);
$ppp = $result->fetch_assoc();
    $date=$ppp["date"];
	$pname=$ppp["pname"];
	$cname=$ppp["cname"];
	$cell=$ppp["cell"];
	$month_name=$ppp["month_name"];
	$username=$ppp["username"];
	$netamount=$ppp["netamount"];
	$discount=$ppp["discount"];
	$gamount=$ppp["gamount"];
	$cashrec=$ppp["cashrec"];
	$balret=$ppp["balret"];
	$disc=$ppp["disc"];
	
	$pno=$ppp["pno"];
	
	
	$stamount=$netamount * $st / 100;
$sttotalamount=$netamount + $stamount;
	$sql = "Insert into invoice_st (id,date,month_name,cell,username,status,user_id,netamount,cname,disc,st,stamount,dno,pno,tnetamount,pname) values (null,'$date','$month_name','$cell','$username','Open','$userid','$netamount','$cname','$disc','$st','$stamount','$id','$pno','$sttotalamount','$pname')";
$conn->query($sql);
$last_id = $conn->insert_id;
	 $sqlcus = "select * from dorder_details where inv_no='$id' and user_id='$userid'";
   $result1=$conn->query($sqlcus);
  while($row = $result1->fetch_assoc())

  {
    $date=$row["date"];
	$inv_no=$row["inv_no"];
	$item_no=$row["item_no"];
	$barcode=$row["barcode"];
	$type_name=$row["type_name"];
	$item_name=$row["item_name"];
	$srate=$row["srate"];
	$qty=$row["qty"];
	$amount=$row["amount"];
	$username=$row["username"];
	$itemcode=$row["itemcode"];
	$prate=$row["prate"];
	$pamount=$row["pamount"];
	$diff=$row["diff"];
	$stamount1=$amount * $st / 100;
$sttotalamount1=$amount + $stamount1;
	 $sql2 = "Insert into invoice_details_st (id,date,inv_no,item_no,barcode,type_name,item_name,srate,qty,amount,username,user_id,itemcode,prate,pamount,diff,st,stamount,netamount)values (null,'$date','$last_id','$sno','$barcode','$type_name','$item_name','$srate','$qty','$amount','$username','$userid','$itemcode','$prate','$pamount','$diff','$st','$stamount1','$sttotalamount1')";
$conn->query($sql2);
	}
	
	$Message = "$inv_no";
    header("Location:Open_Invoice.php?Message={$Message}");
 
?>



