<?php

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

$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');

$maindb = 'salemain';
$trandb = 'saletran';
mysqli_select_db($conn, $zdb);

$tran = mysqli_query($conn, "select * from `$trandb`") or die(mysqli_error($conn));

$ctr= 0 ;
while ($gettran = mysqli_fetch_array($tran)) {
    $ctr++ ;
    $srl     = $gettran[srl];
    $vouchno = $gettran[vouchno];
    $mamount = $gettran[amount];

    $main = mysqli_query($conn, "select * from `$maindb` where vouchno = '$vouchno' ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($main);
    $oms = $get['oms'];
    $subtotal = $get['subtotal'];
    $discount = $get['discount'];

    if ($oms == 1) {
        $gstperc = $get['igstperc'];
    } else {
        $gstperc = $get['cgstperc'];
    }

    $disctran = ($mamount * $discount / $subtotal);
    $gsttran = ($mamount - $disctran) * ($gstperc / 100);

    mysqli_query($conn, "update `$trandb` set `gstperc`='$gstperc', `disctran`='$disctran',`gsttran`='$gsttran',`omstran`='$oms'
                 where `srl`='$srl'") or die(mysqli_error($conn));
}
browse('success');
?>
