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

$today=date("Y-m-d");
$month = date("m",strtotime($today));
		$year = date("Y",strtotime($today));
		 $month_name = date("F", mktime(0, 0, 0, $month, 10));
		
		
		 $my=$month_name . " " . $year;

$userid=$_SESSION["userid"];
$username=$_SESSION["username"];

	$inv_no=$_POST["inv_no"];
	$dis=$_POST["dis"];
	$cashrec=$_POST["cashrec"];
	$acc_name_dr=$_POST["acc_name_dr"];
	
	
 	$sql = "SELECT * FROM invoice where id = '$inv_no'";
$result=$conn->query($sql);
$row = $result->fetch_assoc();
$netamount=$row['netamount'] ;
$cname=$row['cname'] ;
$disamount=$dis;
$gamount=$netamount - $disamount;
$balret=$cashrec - $gamount;

$sql2 = "UPDATE invoice SET discount='$disamount',gamount='$gamount',cashrec='$cashrec',balret='$balret',dis='$dis' WHERE id='$inv_no'";
$conn->query($sql2);
// do the update and print out some info just to provide some visual feedback

$sql = "Insert into cashrec (id,date,month_name,ven_name,username,user_id,netamount) values (null,'$today','$my','$cname','$username','$userid','$gamount')";
$conn->query($sql);

$sqlc = "SELECT * FROM chartofaccounts where acc_name = '$acc_name_dr' and  user_id='$userid'";
$resultc=$conn->query($sqlc);
$rowc = $resultc->fetch_assoc();
$acc_symb=$rowc['acc_symb'] ;
$acc_type=$rowc['acc_type'] ;
$sql101 = "Insert into acc_ledger (id,date,month_name,tr_no,acc_name,discription,acc_type,dr_amount,cr_amount,balance,user_id,acc_symb) values (null,'$today','$my','$inv_no','$acc_name_dr','$cname','$acc_type','$gamount','0','0','$userid','$acc_symb')";
$conn->query($sql101);

header("location:Open_Invoice.php");

?>
