<?php

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

$maindb = 'salemain';
$trandb = 'saletran';

$accdb = 'actran';
$stkdb = 'transacn';

$trandbexist = 1;
$source = "SL  ";

$keyfld = "vouchno";
$myyear = $_SESSION["tit_" . $dbname];

date_default_timezone_set('Asia/Kolkata');
$time = date('H:i:s');

$usertype = $_SESSION["usertype"];
$username = $_SESSION["username_" . $dbname];

//browse($username);
$formname = 'Sale';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];

mysqli_select_db($conn, $zdb);

$getmain = mysqli_query($conn, "Select * from `$maindb` order by vouchno") or die(mysqli_error($conn));
while ($fetmain = mysqli_fetch_array($getmain)) {
    $voucher = $fetmain["vouchno"];

    $mpart = mysqli_query($conn, "SELECT sum(subtot) as taxable, gstperc FROM `saletran` WHERE vouchno='$voucher' group by gstperc order by gstperc") or die(mysqli_error($conn));
    
    $t0 = $t5 = $t12 = $t18 = $t28 = 0 ;
    while ($ipart = mysqli_fetch_array($mpart)) {
           $gstp = $ipart['gstperc'];
           $gsta = $ipart['taxable'];
           
           if ($gstp == 0) {
               $t0 = $gsta ; 
           }
           if ($gstp == 5) {
               $t5 = $gsta ; 
           }
           if ($gstp == 12) {
               $t12 = $gsta ; 
           }
           if ($gstp == 18) {
               $t18 = $gsta ; 
           }
           if ($gstp == 28) {
               $t28 = $gsta ; 
           }
           
    }

    mysqli_query($conn, "update `salemain` set tottaxable0 = '$t0', tottaxble5 = '$t5', tottaxble12 = '$t12', tottaxble18 = '$t18', 
                                 tottaxble28 = '$t28' where vouchno = '$voucher'") or die(mysqli_error($conn));
}
echo "Success";
