<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_$dbname"];
mysqli_select_db($conn, $zdb);

$maindb = 'salemain';
$trandb = 'saletran';
$myyear = $_SESSION[$myyear];

$result = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`!= '' order by srno") or die(mysqli_error($conn));

$getnum = mysqli_num_rows($result);
browse($getnum);
while ($get = mysqli_fetch_array($result)) {
    $cgstamt = 0;
    $igstamt = 0;

    $vouchno = $get["vouchno"];
    $srno = $get["srno"];
    $amount = $get["amount"];
    $cgstrate = $get["cgstrate"];
    $sgstrate = $get["sgstrate"];
    $igstrate = $get["igstrate"];
    $discperc = $get["discperc"];
    
    $gstperc = $cgstrate + $sgstrate + $igstrate;

    $discount = round(( $amount * $discperc ) / 100,2);
    $taxval = $amount - $discount;

    if ($igstrate > 0) {
        $igstamt = round(( $taxval * $igstrate ) / 100,2);
    } else {
        $cgstamt = round(( $taxval * $cgstrate ) / 100,2);
    }
    
    $total = round($taxval + $cgstamt + $cgstamt + $igstamt,0);


    mysqli_query($conn, "update `$trandb` set `cgstamt`='$cgstamt',`sgstamt`='$cgstamt',`igstamt`='$igstamt', `gstperc`='$gstperc',"
            . " `discount`='$discount', subtot = '$taxval', `total`='$total' where vouchno = '$vouchno' and srno = '$srno'") or die(mysqli_error($conn));
}
browse("Success");
?>
