<?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];
$success = false;

$itemname = $_REQUEST["itemname"];
$file = $_REQUEST["file"];
$type = $_REQUEST["type"];


if ($file == 'sale_ti.php' || $file == 'crednote.php') {
    $rate = "rate";
} else {
    $rate = "cost";
}

if (isset($itemname) != "") {
    if ($type == 'P') {
        $query = "SELECT `name`,  `unit`, `hsncode`, `gstperc`, `$rate`
          FROM `packing` WHERE name != '' and name = '$itemname'";
        $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
    } else {
        $query = "SELECT `name`,  `unit`, `hsncode`, `gstperc`, `$rate`
          FROM `item` WHERE name != '' and name = '$itemname'";
        $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
    }

    $r = mysqli_fetch_assoc($result);
    $n = mysqli_num_rows($result);

    $ra = $r[$rate];


    if ($n > 0) {
        $success = true;
    }


    $rows = array('success' => $success,
        'name' => $r['name'],
//        'itemcode' => $r[prodcode],
        'unit' => $r['unit'],
        'rate' => $ra,
        'hsncode' => $r['hsncode'],
        'gstperc' => $r['gstperc']
    );
    header("Content-type: application\json");
    echo json_encode($rows);
}