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

$maindb = "item";
$trandb = "transacn";
$formname = "Stock New";
$file = "stock_new.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];


$zyear = right($zdb, 4);
$mfrdate = getzdates($zyear, 'S');

//$mtodate = getzdates($conn,$zyear, 'E');

mysqli_select_db($conn, $zdb);

$name = mysqli_real_escape_string($conn, $_POST["name"]);
$asdt = $_POST["asdt"];
$brand = $_POST["brand"];
$supp = $_POST["supplier"];
$type = $_POST["type"];
$smode = $_POST["smode"];
$godown = $_POST["godown"];
$quality = $_REQUEST['quality'];

$namecond = "";
$qualitycond = "";
$suppcond = "";
$brandcond = "";
$typecond = "";
$smodecond = "";
$godowncond = "";

if (!empty($supp)) {
    $suppcond = " where b.`supplier` = '$supp' ";
}

if (!empty($name)) {
    $namecond = " AND b.`code` LIKE '$name' ";
}
if (!empty($quality)) {
    //        $qualitycond = " AND `quality` LIKE '$quality' ";
    $qualitycond = " AND b.`quality` = '$quality' ";
}
if (!empty($brand)) {
    $brandcond = " AND b.`brand`='$brand' ";
}
if (!empty($type)) {
    $typecond = " AND a.`type`='$type' ";
}

if (!empty($smode)) {
    $smodecond = " AND a.`salemode`='$smode' ";
}

if (!empty($godown)) {
    $godowncond = " AND a.`godown`='$godown' ";
}


mysqli_query($conn, "drop temporary table if existS itemdbf ") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table itemdbf 
                                SELECT a.godown, a.itemcode, a.baleqty, sum(a.qty) as qty,b.barcode, b.code, a.cost, b.name as itemname, a.brand 
                                       FROM `transacn` a, `item` b WHERE a.`itemcode` =  b.`barcode` AND a.`vouchdt` <= '$asdt' 
                                       $namecond $brandcond $qualitycond $yarncond $typecond $shadecond $godowncond
                                       GROUP BY a.godown, a.brand, a.itemcode, cost having sum(a.qty) != 0  ") or die(mysqli_error($conn));

$getitem = mysqli_query($conn, "SELECT a.*, b.name as brandname FROM itemdbf a LEFT OUTER JOIN brand b ON a.brand = b.code $suppcond 
                                       ORDER BY a.code ") or die(mysqli_error($conn));
//print table($getitem) ;

if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    ?>
    <script src="MyScript/item.js" type="text/javascript"></script>
    </head>

    <body>
        <div class="container">
            <div class="row">
                <?php include("includes/head_rep.php"); ?>
                <div class="col-lg-12 pad">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <div class="col-lg-12 pad">
                                <table class="table table-condensed table-bordered">
                                    <tr style="background-color: #1ABC9C;">
                                        <th style="width:3%;" class="text-center">SRL.</th>
                                        <th style="width:7%;" class="text-center">GODOWN</th>
                                        <th style="width:7%;" class="text-center">CODE</th>
                                        <th style="width:32%;" class="text-center">ITEMNAME</th>
                                        <th style="width:8%;" class="text-center">LOGO</th>
                                        <th style="width:7%;" class="text-center">QTY.</th>
                                        <th style="width:7%;" class="text-center">KGS.</th>
                                        <th style="width:7%;" class="text-center">TOTAL KGS.</th>
                                        <th style="width:7%;" class="text-right">COST</th>
                                        <th style="width:7%;" class="text-right">AMOUNT</th>
                                    </tr>
                                    <?php
                                    $ctr = 0;
                                    while ($item = mysqli_fetch_array($getitem)) {
                                        $icode = $item['code'];
                                        $igodown = $item['godown'];
                                        $ibarcode = $item['barcode'];
                                        $bcode = $item['brand'];
                                        $bale = $item['baleqty'];
                                        $ttotqty = $item['qty'];

                                        $cost = $item['cost'];
                                        $itemname = $item['itemname'];
                                        $brandname = $item['brandname'];

                                        $gdgodown = mysqli_query($conn, "SELECT `code`,`gocode` FROM `godown` WHERE `code`='$igodown'") or die(mysqli_error($conn));
                                        $gtgodown = mysqli_fetch_array($gdgodown);

                                        if ($cost > 0) {
                                            
                                        } else {
                                            $getcost = mysqli_query($conn, "SELECT cost from $trandb where cost > 0 and brand = '$bcode' and itemcode = '$icode' 
                                                                              and godown = '$igodown' and baleqty = '$bale' ORDER BY vouchdt desc") or die(mysqli_error($conn));
                                            $fetcost = mysqli_fetch_array($getcost);
                                            $cost = $fetcost['cost'];
                                        }
                                        $qtykg = $bale * $ttotqty;

                                        $amt = $cost * $ttotqty;
                                        $tamt = $tamt + $amt;
                                        $totqty = $ttotqty + $totqty;
                                        $totkg = $totkg + $qtykg;
                                        $ctr++;

                                        if ($ttotqty <= 0) {
                                            $bcolr = 'pink';
                                        } else {
                                            $bcolr = 'white';
                                        }
                                        $mcurdt = date("Y-m-d");
                                        $url = "inwoutrep_fr.php?view=view&brand=$brandname&itemname=$ibarcode&fdt=$mfrdate&tdt=$mcurdt";
                                        echo "<tr style='background-color: $bcolr' class='clickable-row2' data-href='$url' data-target='_blank'>";
                                        ?>
                                        <td class="text-center"><label><?php echo $ctr; ?></label></td>
                                        <td class="text-center"><label><?php echo $gtgodown['gocode']; ?></label></td>
                                        <td class="text-center"><label><?php echo $icode; ?></label></td>
                                        <td class="text-center"><label><?php echo $itemname; ?></label></td>
                                        <td class="text-center"><label><?php echo $brandname; ?></label></td>

                                        <td class="text-right"><label class="label-control"><?php echo number_format($ttotqty); ?></label></td>
                                        <td class="text-center"><label><?php echo number_format($bale); ?></label></td>
                                        <td class="text-right"><label class="label-control"><?php echo number_format($qtykg); ?></label></td>
                                        <td class="text-right"><label class="label-control"><?php echo number_format($cost, 2); ?></label></td>
                                        <td class="text-right"><label class="label-control"><?php echo number_format($amt, 2); ?></label></td>
                                        <?php
                                        echo "</tr>";
                                    }
                                    ?>
                                    <tr>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td class="text-center"><label class="label-control" style="color: red;">TOTAL</label></td>
                                        <td class="text-right"><label class="label-control" style="color: red;"><? echo $totqty; ?></label></td>
                                        <td><label class="label-control" style="color: red;"></label></td>
                                        <td class="text-right"><label class="label-control" style="color: red;"><? echo $totkg; ?></label></td>
                                        <td><label class="label-control" style="color: red;"></label></td>
                                        <td class="text-right"><label class="label-control" style="color: red;"><? echo number_format($tamt, 2); ?></label></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <?php
    }


    if (isset($_POST["excel"])) {

        $contents .= "CODE, ITEMNAME, LOGO, QTY., KGS., COST, AMOUNT \n";

        while ($item = mysqli_fetch_array($getitem)) {
            $icode = $item['code'];
            $bcode = $item['brand'];
            $bale = $item['baleqty'];
            $ttotqty = $item['qty'];
            $cost = $item['cost'];
            $itemname = $item['itemname'];
            $brandname = $item['brandname'];

            $amt = $cost * $ttotqty;
            $tamt = $tamt + $amt;
            $totqty = $ttotqty + $totqty;

            $contents .= "$icode" . ",";
            $contents .= "$itemname" . ",";
            $contents .= "$brandname" . ",";
            $contents .= "$ttotqty" . ",";
            $contents .= "$bale" . ",";
            $contents .= "$cost" . ",";
            $contents .= "$amt" . "\n";
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "TOTAL" . ",";
        $contents .= "$totqty" . ",";
        $contents .= "" . ",";
        $contents .= "$tamt" . "\n";

        $contents = strip_tags($contents);

        if ($_POST["dtype"] == 1) {
            header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
            print $contents;
        } else {
            header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
            print $contents;
        }
    }
    ?>