<?php

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


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

if (isset($_REQUEST['name'])) {
    $name = mysqli_real_escape_string($conn, $_REQUEST["name"]);
    
    $result = mysqli_query($conn, "SELECT code, cost , rate, mrp, msp, unit  FROM  `item` WHERE `code`='$name'") or die(mysqli_error($conn));
//    $result = mysqli_query($conn, "SELECT a.code, a.cost , a.rate, a.mrp, a.msp, b.name as unit FROM  `item` a, `unit` b 
//                           WHERE a.`code`='$name' and a.unit=b.code") or die(mysqli_error($conn));

    if (mysqli_num_rows($result) > 0) {
        $array = mysqli_fetch_array($result);
        $getunit = mysqli_query($conn, "SELECT code, name  FROM  `unit` WHERE `code`='$array[unit]'") or die(mysqli_error($conn));
        $fetunit = mysqli_fetch_array($getunit);
        
        $code = $array["code"];
        $cost = $array["cost"];
        $rate = $array["rate"];
        $msp = $array["msp"];
        $mrp = $array["mrp"];
        $unit = $fetunit["name"];
    }

    header("Content-type: application\json");
    $data = array("code" => $code, "cost" => $cost, "rate" => $rate,"msp" => $msp, "mrp" => $mrp, "unit" => $unit);
    echo json_encode($data);
}