<?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['barcode'])) {
    $barcode = $_REQUEST['barcode'];
    $item = $_REQUEST['item'];
    $rate = $_REQUEST['rate'];

    $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 ($n['chg'] == 1) {
        if ($rate > 999) {
            $prodperc = '12.00';
        } 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);
}