<?php

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

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

if (isset($_REQUEST['item'])) {
    $barcode = $_REQUEST['barcode'];
    $item = $_REQUEST['item'];
    $rate = $_REQUEST['rate'];
    $vouchdt = $_REQUEST['vouchdt'];

    $getm = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$item'")or die(mysqli_error($conn));
    $m = mysqli_fetch_array($getm);

    $getn = mysqli_query($conn, "SELECT `chg` FROM `itemgrp` WHERE `code`='$m[itemgrp]'")or die(mysqli_error($conn));
    $n = mysqli_fetch_array($getn);

    if ($vouchdt <= "2025-09-22"){
        $limit = "1000";
        $skgst = "12";
    }else{
        $limit = "2500";
        $skgst = "18";
    }
        
    if ($n['chg'] == 1) {
        if ($rate >= $limit) {
            $prodperc = $skgst;
        } else {
            if ($m['gstperc'] > 0) {
                $prodperc = $m['gstperc'];
            } else {
                $prodperc = '5.00';
            }
        }
    } else {
        $prodperc = $m['gstperc'];
    }

//    $prodperc = $m['gstperc'];
//    $hsn = $m[hsncode];

    $cgst = $prodperc / 2;
    $sgst = $prodperc / 2;


    switch ($prodperc) {
        case '5' : $discperc = 4.762;
            break;
        case '12' : $discperc = 10.7143;
            break;
        case '18' : $discperc = 15.254;
            break;
        case '28' : $discperc = 21.875;
            break;
    }


    $rows = array('cgst' => "$cgst",
        'sgst' => "$sgst",
        'igst' => "$prodperc",
        'discperc' => "$discperc"
    );
    header("Content-type: application\json");
    echo json_encode($rows);
}