<?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["containerno"])) {
    $vouchno = $_REQUEST["vouchno"];
    $srno = $_REQUEST["srno"];
    $containerno = $_REQUEST["containerno"];
    $source = $_REQUEST["source"];
    $totqty = $_REQUEST["totqty"];
    $baleqty = $_REQUEST["baleqty"];
    $qty = $_REQUEST["qty"];

    $mitemcode = $_REQUEST["itemcode"];
    $brand = $_REQUEST["brand"];
    $getbr = mysqli_query($conn, "SELECT code,name FROM `brand` WHERE `name`='$brand'") or die(mysqli_error($conn));
    $bcode = mysqli_fetch_array($getbr);
    $brcode = $bcode['code'];
    $finditem = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$mitemcode' and brand='$brcode'") or die(mysqli_error($conn));
    $fitem = mysqli_fetch_array($finditem);
    $mitem = $fitem['barcode'];





    $getexist = mysqli_query($conn, "SELECT * FROM `dumstock` WHERE `vouchno`='$vouchno' AND `srno`='$srno'")or die(mysqli_error($conn));
    $exist = mysqli_num_rows($getexist);

    if ($exist > 0) {
        mysqli_query($conn, "UPDATE `dumstock` SET `containerno`='$containerno', `itemcode`='$mitem', 
                           `source`='$source', `totqty`='$totqty', `baleqty`='$baleqty', `qty`='$qty'
                 WHERE `vouchno`='$vouchno' AND `srno`='$srno'") or die(mysqli_error($conn));
    } else {
        mysqli_query($conn, "INSERT INTO `dumstock`(`vouchno`, `srno`, `containerno`,`itemcode`,`source`, brand,
                           `totqty`, `baleqty`,`qty`) 
                    VALUES 
                           ('$vouchno', '$srno', '$containerno', '$mitem', '$source', '$brcode',
                            '$totqty', '$baleqty', '$qty' )") or die(mysqli_error($conn));
    }
}