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

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

$formname = 'Stock';

$maindb = 'product';
$trandb = 'transacn';

$grid = 'stock.php';

//$getprevyear = mysqli_query($conn,"SELECT cono, finyear FROM $zroot.`compyear` where `cono`='$compcd' 
//                            and `finyear`!='$myyear' ORDER BY `lastdate` DESC LIMIT 0,1 ")or die(mysqli_error($conn));
//$showprevyear = mysqli_fetch_array($getprevyear);
//$prevyear = $showprevyear['finyear'];
//
//
//$olddb = $zroot1 . $compcd . '_' . $prevyear;

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

    $prodcond = "";
    $subtypcond = "";
    $typcond = "";
    $szecond = "";

    if (!empty($_POST["type"])) {
        $type = trim($_POST["type"]);
        $typcond = "AND a.`type` = '$type' ";
    }

    if (!empty($_POST["subtype"])) {
        $subtype = trim($_POST["subtype"]);
        $subtypcond = "AND a.`subtype` = '$subtype' ";
    }

    if (!empty($_POST["product"])) {
        $bname = trim($_POST["product"]);
        $itmcond = "AND a.`code`='$bname' ";
    }

    if (!empty($_POST["size"])) {
        $size = trim($_POST["size"]);
        $szcond = "AND b.`size` = '$size' ";
    }

    if (empty($_POST["todt"])) {
        browse("DATE CANNOT BE EMPTY");
        header("refresh:1; $grid");
    }

    $mtodate = trim($_POST["todt"]);

    $getitem = mysqli_query($conn, "SELECT a.*, b.size, b.prodcode FROM `$maindb` a, `prodtran` b WHERE a.code != '' AND a.`code`= b.`prodcode` $prodcond $subtypcond $typcond $itmcond $szcond ORDER BY code") or die(mysqli_error($conn));
    $getno = mysqli_num_rows($getitem);

    $mtdt = date("d-m-Y", strtotime($mtodate));
    if ($getno == 0) {

        browse("Record Not Found");
        echo "<script>
            window.close();
            </script>";
    }

    include("../includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>

    </head>
    <body>
        <div class="container">
            <?php
            include("includes/head_rep.php");
            ?>

            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th>Sr.</th>
                                <th>Name</th>
                                <th>Category</th>
                                <th>Sub-Category</th>
                                <th>Size</th>
                                <th>M.R.P.</th>
                                <th>Inward</th>
                                <th>Sale Return</th>
                                <th>Sale Online</th>
                                <th>Sale Offline</th>
                                <th>Give Away</th>
                                <th>Damage</th>
                                <th>Total Sale</th>
                                <th>Balance</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $gropen = 0;
                            $grsale = 0;
                            $grpur = 0;
                            $grbal = 0;
                            $grval = 0;
                            $mopen = 0;

//                            ----
                            $totinwqty = 0;
                            $totsrqty = 0;
                            $totonlinqty = 0;
                            $totofflinqty = 0;
                            $totgiveqty = 0;
                            $totdmqty = 0;
                            $totlessqty = 0;
                            $totbalqty = 0;

                            $ctr = 0;
                            mysqli_select_db($zdb);
                            while ($row = mysqli_fetch_object($getitem)) {
//                                browse($row->code);
                                // $tqry1 = mysqli_query($conn,"SELECT name,compcode FROM `compmast` WHERE `compcode` ='$row->compcode' ") or die(mysqli_error($conn));
                                // $getqty1 = mysqli_fetch_object($tqry1);
                                // $compc = $getqty1->name;
                                ########### Size ##################

                                $getcateg = mysqli_query($conn, "SELECT code, name FROM `type` WHERE `code` ='$row->type' ") or die(mysqli_error($conn));
                                $fcateg = mysqli_fetch_object($getcateg);

                                $getscateg = mysqli_query($conn, "SELECT code, name FROM `subtype` WHERE `code` ='$row->subtype'") or die(mysqli_error($conn));
                                $fscateg = mysqli_fetch_object($getscateg);

                                $getsize = mysqli_query($conn, "SELECT code, name FROM `size` WHERE `code` ='$row->size' ") or die(mysqli_error($conn));
                                $fsize = mysqli_fetch_object($getsize);

                                $getinwqty = mysqli_query($conn, "SELECT sum(qty) as inward FROM `transacn` WHERE `itemcode` ='$row->code'
                                                           AND `size` = $row->size AND source = 'IN' AND flag = 'IN'") or die(mysqli_error($conn));
                                $inwqtyf = mysqli_fetch_object($getinwqty);

                                $getsrqty = mysqli_query($conn, "SELECT sum(qty) as saleret FROM `transacn` WHERE `itemcode` ='$row->code' 
                                                         AND `size` = $row->size AND source = 'IN' AND flag = 'SR'") or die(mysqli_error($conn));
                                $srqtyf = mysqli_fetch_object($getsrqty);
//                                browse($fqty->addqty);


                                $getonslqty = mysqli_query($conn, "SELECT sum(qty) as onlinesl FROM `transacn` WHERE `itemcode` ='$row->code' 
                                                           AND `size` = $row->size AND `source` = 'SL' ") or die(mysqli_error($conn));
                                $onslqty = mysqli_fetch_object($getonslqty);

                                $getoffslqty = mysqli_query($conn, "SELECT sum(qty) as offlinesl FROM `transacn` WHERE 
                                                           `itemcode` ='$row->code' AND `size` = $row->size AND `source` = 'OFSL' AND flag = 'OF'") or die(mysqli_error($conn));
                                $offslqty = mysqli_fetch_object($getoffslqty);

                                $getgaqty = mysqli_query($conn, "SELECT sum(qty) as `giveaway` FROM `transacn` WHERE 
                                                           `itemcode` ='$row->code' AND `size` = $row->size AND `source` = 'OFSL' AND flag = 'G'") or die(mysqli_error($conn));
                                $gaqty = mysqli_fetch_object($getgaqty);

                                $getdmqty = mysqli_query($conn, "SELECT sum(qty) as `damage` FROM `transacn` WHERE 
                                                           `itemcode` ='$row->code' AND `size` = $row->size AND `source` = 'OFSL' AND flag = 'D'") or die(mysqli_error($conn));
                                $dmqty = mysqli_fetch_object($getdmqty);

                                $getlessqty = mysqli_query($conn, "SELECT sum(qty) as lessqty FROM `transacn` WHERE
                                                          `itemcode` ='$row->code' AND `size` = $row->size AND qty < 0") or die(mysqli_error($conn));
                                $lqty = mysqli_fetch_object($getlessqty);

                                $getbalqty = mysqli_query($conn, "SELECT sum(qty) as balqty FROM `transacn` WHERE 
                                                        `itemcode` ='$row->code' AND `size` = $row->size") or die(mysqli_error($conn));
                                $fbalqty = mysqli_fetch_object($getbalqty);

                                $mtype = $fcateg->name;
                                $msubtype = $fscateg->name;
                                $msize = $fsize->name;
                                $iname = $row->name;
                                $mmrp = $row->cost;


                                $inwqty = $inwqtyf->inward;
                                $srqty = $srqtyf->saleret;
                                $onlinqty = $onslqty->onlinesl;
                                $offlinqty = $offslqty->offlinesl;
                                $giveqty = $gaqty->giveaway;
                                $damageqty = $dmqty->damage;

                                $lessqty = $onlinqty + $offlinqty;
                                $balqty = $fbalqty->balqty;

                                $ctr++;
                                echo "<tr> ";
                                echo "<td align='left'>$ctr</td>";
                                echo "<td align='left'>$iname</td>";
                                echo "<td align='left'>$mtype</td>";
                                echo "<td align='left'>$msubtype</td>";
                                echo "<td align='left'>$msize</td>";
                                echo "<td align='right'>$mmrp</td>";
                                echo "<td align='right'>$inwqty</td>";
                                echo "<td align='right'>$srqty</td>";
                                echo "<td align='right'>" . abs($onlinqty) . "</td>";
                                echo "<td align='right'>$offlinqty</td>";
                                echo "<td align='right'>$giveqty</td>";
                                echo "<td align='right'>$damageqty</td>";
                                echo "<td align='right' style='color: blue'>" . abs($lessqty) . "</td>";
                                echo "<td align='right' style='font-weight: bold;'>$balqty</td>";
                                if ($prnval == 2) {

                                    echo "<td style='text-align:right;'>";
                                    echo "<font><b> " . number_format("$newcost", 0, ".", ",") . "</b>";
                                    echo "</td>";

                                    echo "<td style='text-align:right;'>";
                                    echo "<font><b> " . number_format("$itemvalue", 0, ".", ",") . "</b>";
                                    echo "</td>";
                                }
                                echo "</tr>";

                                $totinwqty = $totinwqty + $inwqty;
                                $totsrqty = $totsrqty + $srqty;
                                $totonlinqty = $totonlinqty + $onlinqty;
                                $totofflinqty = $totofflinqty + $offlinqty;
                                $totgiveqty = $totgiveqty + $giveqty;
                                $totdmqty = $totdmqty + $damageqty;
                                $totlessqty = $totlessqty + abs($lessqty);
                                $totbalqty = $totbalqty + $balqty;

                                $gropen = $gropen + $openqty;
                                $grsale = $grsale + $saleqty;
                                $grpur = $grpur + $purqty;
                                $grbal = $grbal + $balqty;
                                $grval = $grval + $itemvalue;
                            }
                            echo "<tr style ='background-color:white;font-weight:bold;color:red'> ";
                            echo "<td align='left'></td>";
                            echo "<td align='left'></td>";
                            echo "<td align='left'></td>";
                            echo "<td align='left'></td>";
                            echo "<td align='left'></td>";
                            echo "<td align='right'><font size='2px'> Total</font></td>";

                            echo "<td align='right'>$totinwqty</td>";
                            echo "<td align='right'>$totsrqty</td>";
                            echo "<td align='right'>$totonlinqty</font></td>";
                            echo "<td align='right'>$totofflinqty</td>";
                            echo "<td align='right'>$totgiveqty</td>";
                            echo "<td align='right'>$totdmqty</td>";
                            echo "<td align='right'>$totlessqty</td>";
                            echo "<td align='right'>$totbalqty</td>";
                            if ($prnval == 2) {
                                echo "<td align='right'>";
                                echo "<font><b></b>";
                                echo "</td>";
                                echo "<td align='right'>";
                                echo "<font><b> " . $grval . "</b>";
                                echo "</td>";
                            }
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>


    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {

    $bcond = "";
    $ncond = "";
    $gcond = "";
    $gflcond = "";
    $cmpcond = "";
    $artcond = "";
    $packcond = "";
    $colrcond = "";
    $szecond = "";
    $maindb = 'item';
    $trandb = 'transacn';

    if (!empty($_POST["barcode"])) {
        $bname = trim($_POST["barcode"]);
        $bcond = "AND `barcode` LIKE '%$bname%' ";
    }
    if (!empty($_POST["name"])) {
        $nname = trim($_POST["name"]);
        $ncond = " and `name` LIKE '$nname%'";
    }
    if (!empty($_POST["company"])) {
        $cmpnme = trim($_POST["company"]);
        $cmpcond = " and `compcode`='$cmpnme'";
    }
    if (!empty($_POST["article"])) {
        $artnme = trim($_POST["article"]);
        $artcond = " and `articode` LIKE '%$artnme%'";
    }
    if (!empty($_POST["size"])) {
        $sizecde = trim($_POST["size"]);
        $szecond = " and `sizecode`='$sizecde'";
    }

    if (!empty($_POST["itemgrp"])) {
        $gname = trim($_POST["itemgrp"]);
        $gcond = " and `itemgrp` = '$gname' ";
    }

    if (empty($_POST["frmdt"])) {
        browse("FROM DATE CANNOT BE EMPTY");
        header("refresh:1; $grid");
    }
    if (empty($_POST["todt"])) {
        browse("TO DATE CANNOT BE EMPTY");
        header("refresh:1; $grid");
    }

    $prnval = $_POST["prnval"];
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $getitem = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE barcode != ''  $bcond $ncond $gcond $cmpcond $artcond $szecond ORDER BY barcode") or die(mysqli_error($conn));
    $getno = mysqli_num_rows($getitem);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    if ($getno == 0) {

        browse("Record Not Found");
        echo "<script>
            window.close();
            </script>";
    }
    if ($prnval == 2) {
        $contents.="Sr.,Barcode,Name,Comp,Article,Size,Opening,Inward,Outward,Balance,Cost,Value\n";
    } else {
        $contents.="Sr.,Barcode,Name,Comp,Article,Size,Opening,Inward,Outward,Balance\n";
    }
    $gropen = 0;
    $grsale = 0;
    $grpur = 0;
    $grbal = 0;
    $grval = 0;
    $mopen = 0;

    $ctr = 0;
    mysqli_select_db($zdb);
    while ($row = mysqli_fetch_object($getitem)) {

        $tqry1 = mysqli_query($conn, "SELECT name,compcode FROM `compmast` WHERE `compcode` ='$row->compcode' ") or die(mysqli_error($conn));
        $getqty1 = mysqli_fetch_object($tqry1);
        $compc = $getqty1->name;

########### Size ##################

        $size = mysqli_query($conn, "SELECT sizecode,name FROM `size` WHERE `sizecode` ='$row->sizecode' ") or die(mysqli_error($conn));
        $gets = mysqli_fetch_object($size);
        $sizes = $gets->name;


        $icode = $row->code;
        $ibarcode = $row->barcode;
        $iarticle = $row->article;
        $iname = $row->name;

        $mopen = $row->opening;
        $itemname = $row->name;
        $cost = $row->cost;
        if ($cost > 0) {
            $actcost = $cost;
        } else {
            $getpur = mysqli_query($conn, "SELECT `rate` FROM `purtran` WHERE `itemcode`='$icode' and rate > 0 
                                                           and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));
            $chkpurrate = mysqli_fetch_array($getpur);
            $actcost = $chkpurrate[rate];
        }
        $newcost = $actcost;


        $getopen = mysqli_query($conn, "SELECT sum(qty) as sqty FROM `$trandb` WHERE barcode = '$ibarcode'  
                                                             and `vouchdt` < '$mfrmdate' ") or die(mysqli_error($conn));
        $iopen = mysqli_fetch_array($getopen);
        $openqty = $iopen['sqty'] + $mopen;

        $getpur = mysqli_query($conn, "SELECT sum(qty) as sqty FROM `$trandb` WHERE  barcode = '$ibarcode'   and
                                                          `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and qty > 0") or die(mysqli_error($conn));
        $ipur = mysqli_fetch_array($getpur);
        $purqty = $ipur['sqty'];


        $getsale = mysqli_query($conn, "SELECT sum(qty) as sqty FROM `$trandb` WHERE barcode = '$ibarcode'   and
                                                          `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and qty < 0") or die(mysqli_error($conn));
        $isale = mysqli_fetch_array($getsale);
        $saleqty1 = $isale['sqty'];


        $saleqty = $saleqty1 * -1;

        $balqty = $openqty + $purqty - $saleqty;
        $itemvalue = $newcost * $balqty;

        $ctr++;
        if ($prnval == 2) {
            $contents.=$ctr . ",";
            $contents.=$ibarcode . ",";
            $contents.=$iname . ",";
            $contents.=$compc . ",";
            $contents.=$iarticle . ",";
            $contents.=$sizes . ",";
            $contents.=$openqty . ",";
            $contents.=$purqty . ",";
            $contents.=$saleqty . ",";
            $contents.=$balqty . ",";
            $contents.=number_format("$newcost", 0, ".", ",") . ",";
            $contents.=number_format("$itemvalue", 0, ".", ",") . "\n";
        } else {
            $contents.=$ctr . ",";
            $contents.=$ibarcode . ",";
            $contents.=$iname . ",";
            $contents.=$compc . ",";
            $contents.=$iarticle . ",";
            $contents.=$sizes . ",";
            $contents.=$openqty . ",";
            $contents.=$purqty . ",";
            $contents.=$saleqty . ",";
            $contents.=$balqty . "\n";
        }
        $gropen = $gropen + $openqty;
        $grsale = $grsale + $saleqty;
        $grpur = $grpur + $purqty;
        $grbal = $grbal + $balqty;
        $grval = $grval + $itemvalue;
    }
    if ($prnval == 2) {
        $contents.=" " . ",";
        $contents.=" " . ",";
        $contents.=" " . ",";
        $contents.=" " . ",";
        $contents.=" " . ",";
        $contents.="Total" . ",";
        $contents.=$gropen . ",";
        $contents.=$grpur . ",";
        $contents.=$grsale . ",";
        $contents.=$grbal . ",";
        $contents.=" " . ",";
        $contents.=$grval . "\n";
    } else {
        $contents.=" " . ",";
        $contents.=" " . ",";
        $contents.=" " . ",";
        $contents.=" " . ",";
        $contents.=" " . ",";
        $contents.="Total" . ",";
        $contents.=$gropen . ",";
        $contents.=$grpur . ",";
        $contents.=$grsale . ",";
        $contents.=$grbal . "\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;
    }
}
if (isset($_POST["subpdf"])) {

    $bcond = "";
    $ncond = "";
    $gcond = "";
    $gflcond = "";
    $cmpcond = "";
    $artcond = "";
    $packcond = "";
    $colrcond = "";
    $szecond = "";
    $maindb = 'item';
    $trandb = 'transacn';

    if (!empty($_POST["barcode"])) {
        $bname = trim($_POST["barcode"]);
        $bcond = "AND `barcode` LIKE '%$bname%' ";
    }
    if (!empty($_POST["name"])) {
        $nname = trim($_POST["name"]);
        $ncond = " and `name` LIKE '$nname%'";
    }
    if (!empty($_POST["company"])) {
        $cmpnme = trim($_POST["company"]);
        $cmpcond = " and `compcode`='$cmpnme'";
    }
    if (!empty($_POST["article"])) {
        $artnme = trim($_POST["article"]);
        $artcond = " and `articode` LIKE '%$artnme%'";
    }
    if (!empty($_POST["size"])) {
        $sizecde = trim($_POST["size"]);
        $szecond = " and `sizecode`='$sizecde'";
    }

    if (!empty($_POST["itemgrp"])) {
        $gname = trim($_POST["itemgrp"]);
        $gcond = " and `itemgrp` = '$gname' ";
    }

    if (empty($_POST["frmdt"])) {
        browse("FROM DATE CANNOT BE EMPTY");
        header("refresh:1; $grid");
    }
    if (empty($_POST["todt"])) {
        browse("TO DATE CANNOT BE EMPTY");
        header("refresh:1; $grid");
    }

    $prnval = $_POST["prnval"];
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $getitem = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE barcode != ''  $bcond $ncond $gcond $cmpcond $artcond $szecond ORDER BY barcode") or die(mysqli_error($conn));
    $getno = mysqli_num_rows($getitem);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    if ($getno == 0) {

        browse("Record Not Found");
        echo "<script>
            window.close();
            </script>";
    }

    require("library/fpdf/fpdf.php");

    class pdf extends fpdf {

        function Footer() {



//Position at 1.5 cm from bottom

            $this->SetY(-15);

//Arial italic 8

            $this->SetFont('Arial', 'I', 8);

//Page number

            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }

    }

    $pdf = new pdf('L', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();
    include("includes/head_rep_pdf.php");
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetFillColor(132, 58, 84);
    $pdf->SetTextColor(255, 255, 255);
    if ($prnval == 2) {
        $pdf->Cell(8, 7, "Sr.", 1, 0, '', 1);
        $pdf->Cell(29, 7, "Barcode", 1, 0, '', 1);
        $pdf->Cell(29, 7, "Name", 1, 0, '', 1);
        $pdf->Cell(29, 7, "Comp", 1, 0, '', 1);
        $pdf->Cell(29, 7, "Article", 1, 0, '', 1);
        $pdf->Cell(10, 7, "Size", 1, 0, '', 1);
        $pdf->Cell(24, 7, "Opening", 1, 0, 'R', 1);
        $pdf->Cell(24, 7, "Inward", 1, 0, 'R', 1);
        $pdf->Cell(24, 7, "Outward", 1, 0, 'R', 1);
        $pdf->Cell(24, 7, "Balance", 1, 0, 'R', 1);
        $pdf->Cell(24, 7, "Cost", 1, 0, 'R', 1);
        $pdf->Cell(24, 7, "Value", 1, 0, 'R', 1);
    } else {
        $pdf->Cell(8, 7, "Sr.", 1, 0, '', 1);
        $pdf->Cell(33, 7, "Barcode", 1, 0, '', 1);
        $pdf->Cell(34, 7, "Name", 1, 0, '', 1);
        $pdf->Cell(34, 7, "Comp", 1, 0, '', 1);
        $pdf->Cell(34, 7, "Article", 1, 0, '', 1);
        $pdf->Cell(19, 7, "Size", 1, 0, '', 1);
        $pdf->Cell(29, 7, "Opening", 1, 0, 'R', 1);
        $pdf->Cell(29, 7, "Inward", 1, 0, 'R', 1);
        $pdf->Cell(29, 7, "Outward", 1, 0, 'R', 1);
        $pdf->Cell(29, 7, "Balance", 1, 0, 'R', 1);
    }
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $gropen = 0;
    $grsale = 0;
    $grpur = 0;
    $grbal = 0;
    $grval = 0;
    $mopen = 0;

    $ctr = 0;
    mysqli_select_db($zdb);
    while ($row = mysqli_fetch_object($getitem)) {

        $tqry1 = mysqli_query($conn, "SELECT name,compcode FROM `compmast` WHERE `compcode` ='$row->compcode' ") or die(mysqli_error($conn));
        $getqty1 = mysqli_fetch_object($tqry1);
        $compc = $getqty1->name;

########### Size ##################

        $size = mysqli_query($conn, "SELECT sizecode,name FROM `size` WHERE `sizecode` ='$row->sizecode' ") or die(mysqli_error($conn));
        $gets = mysqli_fetch_object($size);
        $sizes = $gets->name;


        $icode = $row->code;
        $ibarcode = $row->barcode;
        $iarticle = $row->article;
        $iname = $row->name;

        $mopen = $row->opening;
        $itemname = $row->name;
        $cost = $row->cost;
        if ($cost > 0) {
            $actcost = $cost;
        } else {
            $getpur = mysqli_query($conn, "SELECT `rate` FROM `purtran` WHERE `itemcode`='$icode' and rate > 0 
                                                           and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));
            $chkpurrate = mysqli_fetch_array($getpur);
            $actcost = $chkpurrate[rate];
        }
        $newcost = $actcost;


        $getopen = mysqli_query($conn, "SELECT sum(qty) as sqty FROM `$trandb` WHERE barcode = '$ibarcode'  
                                                             and `vouchdt` < '$mfrmdate' ") or die(mysqli_error($conn));
        $iopen = mysqli_fetch_array($getopen);
        $openqty = $iopen['sqty'] + $mopen;

        $getpur = mysqli_query($conn, "SELECT sum(qty) as sqty FROM `$trandb` WHERE  barcode = '$ibarcode'   and
                                                          `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and qty > 0") or die(mysqli_error($conn));
        $ipur = mysqli_fetch_array($getpur);
        $purqty = $ipur['sqty'];


        $getsale = mysqli_query($conn, "SELECT sum(qty) as sqty FROM `$trandb` WHERE barcode = '$ibarcode'   and
                                                          `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and qty < 0") or die(mysqli_error($conn));
        $isale = mysqli_fetch_array($getsale);
        $saleqty1 = $isale['sqty'];


        $saleqty = $saleqty1 * -1;

        $balqty = $openqty + $purqty - $saleqty;
        $itemvalue = $newcost * $balqty;

        $ctr++;
        $pdf->SetFont('Arial', '', 8);
        if ($prnval == 2) {
            $pdf->Cell(8, 7, "$ctr", 1);
            $pdf->Cell(29, 7, "$ibarcode", 1);
            $pdf->Cell(29, 7, "$iname", 1);
            $pdf->Cell(29, 7, "$compc", 1);
            $pdf->Cell(29, 7, "$iarticle", 1);
            $pdf->Cell(10, 7, "$sizes", 1);
            $pdf->Cell(24, 7, "$openqty", 1, 0, 'R');
            $pdf->Cell(24, 7, "$purqty", 1, 0, 'R');
            $pdf->Cell(24, 7, "$saleqty", 1, 0, 'R');
            $pdf->Cell(24, 7, "$balqty", 1, 0, 'R');
            $pdf->Cell(24, 7, number_format("$newcost", 0, ".", ","), 1, 0, 'R');
            $pdf->Cell(24, 7, number_format("$itemvalue", 0, ".", ","), 1, 0, 'R');
        } else {
            $pdf->Cell(8, 7, "$ctr", 1);
            $pdf->Cell(33, 7, "$ibarcode", 1);
            $pdf->Cell(34, 7, "$iname", 1);
            $pdf->Cell(34, 7, "$compc", 1);
            $pdf->Cell(34, 7, "$iarticle", 1);
            $pdf->Cell(19, 7, "$sizes", 1);
            $pdf->Cell(29, 7, "$openqty", 1, 0, 'R');
            $pdf->Cell(29, 7, "$purqty", 1, 0, 'R');
            $pdf->Cell(29, 7, "$saleqty", 1, 0, 'R');
            $pdf->Cell(29, 7, "$balqty", 1, 0, 'R');
        }
        $pdf->Ln();
        $gropen = $gropen + $openqty;
        $grsale = $grsale + $saleqty;
        $grpur = $grpur + $purqty;
        $grbal = $grbal + $balqty;
        $grval = $grval + $itemvalue;
    }
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetTextColor(239, 007, 030);
    if ($prnval == 2) {
        $pdf->Cell(8, 7, " ", 1, 0, '', 1);
        $pdf->Cell(29, 7, " ", 1, 0, '', 1);
        $pdf->Cell(29, 7, " ", 1, 0, '', 1);
        $pdf->Cell(29, 7, " ", 1, 0, '', 1);
        $pdf->Cell(29, 7, "Total", 1, 0, '', 1);
        $pdf->Cell(10, 7, " ", 1, 0, '', 1);
        $pdf->Cell(24, 7, "$gropen", 1, 0, 'R', 1);
        $pdf->Cell(24, 7, "$grpur", 1, 0, 'R', 1);
        $pdf->Cell(24, 7, "$grsale", 1, 0, 'R', 1);
        $pdf->Cell(24, 7, "$grbal", 1, 0, 'R', 1);
        $pdf->Cell(24, 7, " ", 1, 0, '', 1);
        $pdf->Cell(24, 7, "$grval", 1, 0, 'R', 1);
    } else {
        $pdf->Cell(8, 7, " ", 1, 0, '', 1);
        $pdf->Cell(33, 7, " ", 1, 0, '', 1);
        $pdf->Cell(34, 7, " ", 1, 0, '', 1);
        $pdf->Cell(34, 7, " ", 1, 0, '', 1);
        $pdf->Cell(34, 7, "Total", 1, 0, '', 1);
        $pdf->Cell(19, 7, " ", 1, 0, '', 1);
        $pdf->Cell(29, 7, "$gropen", 1, 0, 'R', 1);
        $pdf->Cell(29, 7, "$grpur", 1, 0, 'R', 1);
        $pdf->Cell(29, 7, "$grsale", 1, 0, 'R', 1);
        $pdf->Cell(29, 7, "$grbal", 1, 0, 'R', 1);
    }
    $pdf->Ln();

    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Output("Stock.pdf", "D");
}
?>