<?php

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

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

if (isset($_REQUEST['qty'])) {
    $unit = $_REQUEST['unit'];
    $oldvol = $_REQUEST['vol'];
    $stkunit = $_REQUEST['stkunit'];
    $qty = $_REQUEST['qty'];
    $itemcode = $_REQUEST['itemcode'];
    $itempart = $_REQUEST['itempart'];
    $itemgrp = $_REQUEST['itemgrp'];
    $titempart = mysqli_real_escape_string($conn, $_REQUEST['itempart']);

    $getitm = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$itemcode'")or die(mysqli_error($conn));
    $count = mysqli_num_rows($getitm);


    if ($count > 0) {
        $itm = mysqli_fetch_array($getitm);
        $type = $itm['type'];

        switch ($type) {
            case "F":
                $mvol = $qty;
                $stkvol = $mvol;
                break;
            case "P":
                $stringParts = explode("/", $itempart);

                $g1 = $stringParts[0]; // 855
                $g2 = $stringParts[1]; // 21
                $g3 = $stringParts[2]; // 21


                $mp1 = $mp2 = $mp3 = 0;

                if ($g1 == 0) {
                    $mp1 = (float) $titempart;
//                    browse('in');
                } else {
//                    browse('out');

                    $mp1 = $g1;
                    $mp2 = $g2;
                    $mp3 = $g3;
                }


//                if ($itemgrp != 'TIM' && $itemgrp != 'PLY' && $itemgrp != "PL " && $itemgrp != "EB " && $itemgrp != "MDF") {
//
//                    $mvol = $mp1 * $qty;
//                    
//                } else {

                switch ($unit) {
                    case "C.F.T.":
                    case "CFT":
                        if ($itempart != '') {
                            $mvol = ($mp1 * $mp2 * $mp3) / 144;
                        }
                        break;
                    case "N.A. ":
                    case "NA":
                        $mvol = ($mp1 / 4) * ($mp2 * 0.305) * ($mp3 * 0.305) * $qty;
                        break;

                    case "R.F.T":
                    case "R.F.T.":
                    case "RFT":
                        $mvol = $mp1;
                        break;

                    case "SQRFT":
                        $mvol = ($mp2 * $mp3) * $qty;
                        break;

                    case "SQMTR":
                    case "SQRMTR":

                        if ($mp2 != '') {
                            $chkusub = mysqli_query($conn, "Select * from `unitsub` where `mm`='$mp2' ") or die(mysqli_error($conn));
                            $cn = mysqli_num_rows($chkusub);
                            if ($cn > 0) {
                                $subunit = mysqli_fetch_array($chkusub);
                                $vv1 = $subunit['value'];
                            } else {
//                                    echo "Size Not Found For SQMTR";
                                $false1 = '1';
                            }
                        } else {
                            $vv1 = 0;
                        }

                        if ($mp3 != '') {
                            $chkusub = mysqli_query($conn, "Select * from `unitsub` where `mm`='$mp3' ") or die(mysqli_error($conn));
                            $cn = mysqli_num_rows($chkusub);
                            if ($cn > 0) {
                                $subunit = mysqli_fetch_array($chkusub);
                                $vv2 = $subunit['value'];
                            } else {
                                $false2 = '1';

//                                    echo "Size Not Found For SQMTR";
                            }
                        }


                        if ($vv1 != 0 && $vv2 != 0) {
                            $mvol = $vv1 * $vv2 * $qty;
                        } else {
                            $mvol = $oldvol;
                        }
                        break;

                    default:

                        $mvol = $qty;
                        break;
                }
//                }
///////////////////////   for stock volume - by product group



                switch ($stkunit) {
                    case "C.F.T.":
                    case "CFT":
                        if ($itempart != '') {
                            $stkvol = ($mp1 * $mp2 * $mp3) / 144;
                        }
                        break;
                    case "N.A. ":
                    case "NA":
                        if ($mp2 != '') {
                            $chkusub = mysqli_query($conn, "Select * from `unitsub` where `mm`='$mp2' ") or die(mysqli_error($conn));
                            $cn = mysqli_num_rows($chkusub);
                            if ($cn > 0) {
                                $subunit = mysqli_fetch_array($chkusub);
                                $vv1 = $subunit['value'];
                            } else {
//                                echo "Size Not Found For SQMTR";
                                $false3 = '1';
                            }
                        } else {
                            $vv1 = 0;
                        }


                        if ($mp3 != '') {
                            $chkusub = mysqli_query($conn, "Select * from `unitsub` where `mm`='$mp3' ") or die(mysqli_error($conn));
                            $cn = mysqli_num_rows($chkusub);
                            if ($cn > 0) {
                                $subunit = mysqli_fetch_array($chkusub);
                                $vv2 = $subunit['value'];
                            } else {
//                                echo "Size Not Found For SQMTR";
                                $false4 = '1';
                            }
                        }


                        if ($vv1 != 0 && $vv2 != 0) {
                            $stkvol = ($mp1 / 4) * $vv1 * $vv2 * $qty;
                        } else {
                            if ($itempart != '') {
                                $stkvol = $oldvol / 4 * $mp1;
                            } else {
                                $stkvol = $mvol;
                            }
                        }


                    case "R.F.T":
                    case "R.F.T.":
                    case "RFT":
                        $stkvol = $mp1;
                        break;

                    case "SQRFT":
                        $stkvol = ($mp2 * $mp3) * $qty;
                        break;

                    case "SQMTR":
                    case "SQRMTR":
                        if ($mp2 != '') {
                            $chkusub = mysqli_query($conn, "Select * from `unitsub` where `mm`='$mp2' ") or die(mysqli_error($conn));
                            $cn = mysqli_num_rows($chkusub);
                            if ($cn > 0) {
                                $subunit = mysqli_fetch_array($chkusub);
                                $vv1 = $subunit['value'];
                            } else {
//                                    echo "Size Not Found For SQMTR";
                                $false1 = '1';
                            }
                        } else {
                            $vv1 = 0;
                        }

                        if ($mp3 != '') {
                            $chkusub = mysqli_query($conn, "Select * from `unitsub` where `mm`='$mp3' ") or die(mysqli_error($conn));
                            $cn = mysqli_num_rows($chkusub);
                            if ($cn > 0) {
                                $subunit = mysqli_fetch_array($chkusub);
                                $vv2 = $subunit['value'];
                            } else {
                                $false2 = '1';

//                                    echo "Size Not Found For SQMTR";
                            }
                        }


                        if ($vv1 != 0 && $vv2 != 0) {
                            $stkvol = $vv1 * $vv2 * $qty;
                        } else {
                            $stkvol = $oldvol;
                        }
                        break;
                    default:
                        $stkvol = $qty;
                        break;
                }
        }
    }

    $sucs = 1;
    header("Content-type: application\json");

    $data = array("vol" => $mvol, "stkvol" => $stkvol, "false1" => $false1, "false2" => $false2, "false3" => $false3, "false4" => $false4);
    echo json_encode($data);
}
?>