<?php

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

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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$main = mysqli_query($conn, "select * from `$maindb` order by vouchno") or die(mysqli_error($conn));
$ctr = 0;
while ($get = mysqli_fetch_array($main)) {

    $vouchno = $get['vouchno'];
    $cgst5 = 0;
    $cgst12 = 0;
    $cgst18 = 0;
    $cgst28 = 0;
    $cgst5a = 0;
    $cgst12a = 0;
    $cgst18a = 0;
    $cgst28a = 0;
    $totgst5 = 0;
    $totgst12 = 0;
    $totgst18 = 0;
    $totgst28 = 0;
    $totqty = 0;
    $tran = mysqli_query($conn, "select * from `$trandb` where vouchno = '$vouchno'") or die(mysqli_error($conn));
    while ($gettran = mysqli_fetch_array($tran)) {
        $cperc = $gettran['cgstperc'];
        $camt = $gettran['cgstamt'];
        $qty = $gettran['qty'];
//        $taxable = $gettran['subtot'];

        if ($cperc < 3) {
            $cgst5a = $camt;
            $cgst5 += $cgst5a;
        } else if ($cperc < 7) {
            $cgst12a = $camt;
            $cgst12 += $cgst12a;
        } else if ($cperc < 10) {
            $cgst18a = $camt;
            $cgst18 += $cgst18a;
        } else if ($cperc < 15) {
            $cgst28a = $camt;
            $cgst28 += $cgst28a;
        }
        $totqty += $qty;
    }

    $totgst5 = $cgst5 + $cgst5;
    $totgst12 = $cgst12 + $cgst12;
    $totgst18 = $cgst18 + $cgst18;
    $totgst28 = $cgst28 + $cgst28;

//    mysqli_query($conn, "update `$maindb` set `cgst5`='$cgst5',`sgst5`='$cgst5', `cgst12`='$cgst12',`sgst12`='$cgst12',
//                `cgst18`='$cgst18', `sgst18`='$cgst18', `cgst28`='$cgst28', `sgst28`='$cgst28',
//                  `totgst5`='$totgst5',`totgst12`='$totgst12', `totgst18`='$totgst18',`totgst28`='$totgst28'   
//                 where `vouchno`='$vouchno'") or die(mysqli_error($conn));
    mysqli_query($conn, "update `$maindb` set totqty = '$totqty' 
                 where `vouchno`='$vouchno'") or die(mysqli_error($conn));
}
browse('success');
