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

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

mysqli_select_db($conn, $zdb);

$formname = 'Combine Stock';

$maindb = 'item';
$trandb = 'transacn';

$grid = 'stockall.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;

$gbrcd = trim($_POST["itemcode"]);
$gnm = $_POST["name"];
$gname = trim($_POST["itemgrp"]);
$itemsubgrp = trim($_POST["itemsubgrp"]);
$gprodtype = trim($_POST["prodtype"]);
$gsubgrp = trim($_POST["subgrp"]);
$prnval = trim($_POST["prnval"]);
//$prnzero = trim($_POST["prnzero"]);
$gperc = trim($_POST["gperc"]);
$series = trim($_POST["series"]);
$suppcode = $_POST["suppcode"];

$brcond = "";
$ncond = "";
$gcond = "";
$subcond = "";
$gperccond = "";
$gprodcond = "";
$seriescond = "";
$itemsubcond = "";
$suppcond = "";

if (!empty($_POST["itemcode"])) {
    $brcond = " and b.`code` = '$gbrcd' ";
}

if (!empty($_POST["suppcode"])) {
    $suppcond = " and b.`suppcode` = '$suppcode' ";
}

if (!empty($_POST["name"])) {
    $ncond = " and b.`code` = '$gnm' ";
}

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

if (!empty($_POST["itemsubgrp"])) {
    $itemsubcond = " and b.`itemsubgrp` = '$itemsubgrp' ";
}

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

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

$qryser = mysqli_query($conn, "SELECT * FROM $zroot.`series` order by id ") or die(mysqli_error($conn));
//print table($qryser);
//$sr = 0;
//while ($cser = mysqli_fetch_array($qryser)) {
//    $serno = '$sern' . $sr ;
//    $serno = $cser['code'];
//    
//    echo $serno1;
//    $sr++;
//}
//    browse($serno1);

$serno1 = 'ZB1';
$serno2 = 'ZBX';
$serno3 = 'STR';

$getser1 = mysqli_query($conn, "SELECT * FROM $zroot.`series` WHERE id = 1  ") or die(mysqli_error($conn));
$fetser1 = mysqli_fetch_object($getser1);
$sercode1 = $fetser1->code;

$getser = mysqli_query($conn, "SELECT * FROM $zroot.`series` WHERE id != 1 order by srl limit 2") or die(mysqli_error($conn));

$itemvalue = 0;
mysqli_query($conn, "drop temporary table if exists tempstk ") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table tempstk
                                SELECT a.itemcode, sum(a.qty) as sqty, a.series, 
                                          b.name, b.cost, b.rate FROM `$trandb` a, `$maindb` b 
                                          WHERE a.itemcode = b.code and a.`vouchdt` <= '$mtodate' 
                                          $ncond $brcond  $gcond $itemsubcond $suppcond 
                                          group by a.itemcode, a.series order by a.itemcode, a.series") or die(mysqli_error($conn));

//$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

//
//$getser = mysqli_query($conn, "SELECT * FROM $zroot.`series` WHERE id != 1 order by srl limit 2") or die(mysqli_error($conn));
//$fetser = mysqli_fetch_object($getser) ;
//$ferser1 = $fetser ;
//
//while($ferser1){
//    echo $ferser1->series;
//} 
//die();
//

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

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th {
            /*            background : green ;
                        position: sticky;
                        top:0;*/
        }

    </style>

    </head>
    <body>
        <div class="container" style="width:100%">
            <?php
            include("includes/head_rep.php");
            ?>

            <div class="row">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                        <thead class="mySearchTr" >
                            <tr>

                                <th align="left"  width='3%' style="font-family: arial;">Sr.</th>
                                <th align="left"  width='15%'>Code</th>
                                <th align="left"  width='25%'>Item Name</th>
                                <th width='7%' style="text-align: right;"><?php echo $serno1 ?><i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                <th width='7%' style="text-align: right;"><?php echo $serno2 ?><i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                <th width='7%' style="text-align: right;"><?php echo $serno3 ?><i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                <th width='8%' style="text-align: right;">Total<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                <?php if ($prnval == 2) { ?>
                                    <th width='8%' style="text-align: right;">Cost<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                    <th width='9%' style="text-align: right;">Value<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <?php } ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $gropen = 0;
                            $grsale = 0;
                            $grpur = 0;
                            $grbal = 0;
                            $grval = 0;
                            $mopen = 0;
                            $ctr = 0;
                            $totqty = $totval = 0;

                            $gettran = mysqli_query($conn, "SELECT * FROM tempstk  group by itemcode order by itemcode, series ")or die(mysqli_error($conn));

                            while ($row = mysqli_fetch_object($gettran)) {
                                $ctr++;
                                $icode = $row->itemcode;
                                $itemname = $row->name;
                                $cost = $row->cost;
                                $rate = $row->rate;

                                if ($prnval == 2) {
                                    if ($cost > 0) {
                                        $actcost = $cost;
                                    } else {
                                        $getpur = mysqli_query($conn, "SELECT `rate` FROM `purtran` WHERE `itemcode`='$icode' and rate > 0 
                                                                               and `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));
                                        $getnum = mysqli_num_rows($getpur);
                                        if ($getnum > 0) {
                                            $chkpurrate = mysqli_fetch_array($getpur);
                                            $actcost = $chkpurrate['rate'];
                                        } else {
                                            $actcost = $rate;
                                        }
                                    }
                                    $newcost = $actcost;
                                }

                                $get1 = mysqli_query($conn, "SELECT sqty FROM tempstk WHERE `itemcode`='$icode' and `series` = '$serno1'") or die(mysqli_error($conn));
                                $fet1 = mysqli_fetch_array($get1);
                                $qty1 = $fet1['sqty'];

                                $get2 = mysqli_query($conn, "SELECT sqty FROM tempstk WHERE `itemcode`='$icode' and `series` = '$serno2'") or die(mysqli_error($conn));
                                $fet2 = mysqli_fetch_array($get2);
                                $qty2 = $fet2['sqty'];

                                $get3 = mysqli_query($conn, "SELECT sqty FROM tempstk WHERE `itemcode`='$icode' and `series` = '$serno3'") or die(mysqli_error($conn));
                                $fet3 = mysqli_fetch_array($get3);
                                $qty3 = $fet3['sqty'];

                                echo "<tr>";
                                echo "<td align='left'><font size='3px'>$ctr</td>";
                                echo "<td align='left'><font size='3px'>$icode</td>";
                                echo "<td align='left'><font size='3px'>$itemname</td>";
                                if ($qty1 != 0) {
                                    echo "<td align='right' style='background-color:#ffdd95'><font size='3px'><b>$qty1</b></td>";
                                } else {
                                    echo "<td align='right' style='background-color:#ffdd95'><font size='3px'></td>";
                                }

                                if ($qty2 != 0) {
                                    echo "<td align='right' style='background-color:#B0DDF2'><font size='3px'><b>$qty2</b></td>";
                                } else {
                                    echo "<td align='right' style='background-color:#B0DDF2'><font size='3px'></td>";
                                }

                                if ($qty3 != 0) {
                                    echo "<td align='right' style='background-color:#E8FFC6'><font size='3px'><b>$qty3</b></td>";
                                } else {
                                    echo "<td align='right' style='background-color:#E8FFC6'><font size='3px'></td>";
                                }

                                $balqty = $qty1 + $qty2 + $qty3;

                                if ($balqty != 0) {
                                    if ($balqty > 0) {
                                        echo "<td align='right' style='color:darkgreen'><font size='3px'>" . number_format($balqty, 3) . "</td>";
                                    } else {
                                        echo "<td align='right' style='color:red'><font size='3px'>" . number_format($balqty, 3) . "</td>";
                                    }
                                } else {
                                    echo "<td align='right' style='color:darkgreen'><font size='3px'></td>";
                                }


                                if ($prnval == 2) {
                                    $itemvalue = $newcost * $balqty;
                                    $totval = $totval + $itemvalue;

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

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

//                                echo "</tr>";
//                                $gr1 = $gr1 + $qty1;
//                                $gr2 = $gr2 + $qty2;
//                                $gr3 = $gr3 + $qty3;
//                                $grbal = $grbal + $balqty;
//                                $grval = $grval + $itemvalue;
//                            }
//                            echo "<tr style ='background-color:white;'> ";
//                            echo "<td align='right' colspan='3'><font size='4px'><b> Total</b></font></td>";
//                            echo "<td align='right'><font size='3px'<b>$gr1</b></td>";
//                            echo "<td align='right'><font size='3px'<b>$gr2</b></td>";
//                            echo "<td align='right'><font size='3px'<b>$gr3</b></td>";
//                            echo "<td align='right'><font size='3px'<b>$grbal</b></td>";
//                            if ($prnval == 2) {
//                                echo "<td align='right'>";
//                                echo "<font><b></b>";
//                                echo "</td>";
//                                echo "<td align='right'>";
//                                echo "<font size='3px'<b> " . rajnumformat($grval) . "</b>";
//                                echo "</td>";
//                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </body>
    </html>
    <?php
}

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

    $contents .= "sr, barcode, itemname, zb1, zbx, str, total,";

    if ($prnval == 2) {
        $contents .= "cost, value \n";
    }
    $gropen = 0;
    $grsale = 0;
    $grpur = 0;
    $grbal = 0;
    $grval = 0;
    $mopen = 0;
    $ctr = 0;
    $totqty = $totval = 0;

    $gettran = mysqli_query($conn, "SELECT * FROM tempstk  group by itemcode order by itemcode, series ")or die(mysqli_error($conn));

    while ($row = mysqli_fetch_object($gettran)) {
        $ctr++;
        $icode = $row->itemcode;
        $itemname = $row->name;
        $cost = $row->cost;
        $rate = $row->rate;

        if ($prnval == 2) {
            if ($cost > 0) {
                $actcost = $cost;
            } else {
                $getpur = mysqli_query($conn, "SELECT `rate` FROM `purtran` WHERE `itemcode`='$icode' and rate > 0 
                                                                               and `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));
                $getnum = mysqli_num_rows($getpur);
                if ($getnum > 0) {
                    $chkpurrate = mysqli_fetch_array($getpur);
                    $actcost = $chkpurrate['rate'];
                } else {
                    $actcost = $rate;
                }
            }
            $newcost = $actcost;
        }

        $get1 = mysqli_query($conn, "SELECT sqty FROM tempstk WHERE `itemcode`='$icode' and `series` = '$serno1'") or die(mysqli_error($conn));
        $fet1 = mysqli_fetch_array($get1);
        $qty1 = $fet1['sqty'];

        $get2 = mysqli_query($conn, "SELECT sqty FROM tempstk WHERE `itemcode`='$icode' and `series` = '$serno2'") or die(mysqli_error($conn));
        $fet2 = mysqli_fetch_array($get2);
        $qty2 = $fet2['sqty'];

        $get3 = mysqli_query($conn, "SELECT sqty FROM tempstk WHERE `itemcode`='$icode' and `series` = '$serno3'") or die(mysqli_error($conn));
        $fet3 = mysqli_fetch_array($get3);
        $qty3 = $fet3['sqty'];


        $contents .= "$ctr" . ",";
        $contents .= "$icode" . ",";
        $contents .= "$itemname" . ",";

        if ($qty1 != 0) {

            $contents .= "$qty1" . ",";
        } else {
            $contents .= "" . ",";
        }

        if ($qty2 != 0) {
            $contents .= "$qty2" . ",";
        } else {
            $contents .= "" . ",";
        }

        if ($qty3 != 0) {
            $contents .= "$qty3" . ",";
        } else {
            $contents .= "" . ",";
        }

        $balqty = $qty1 + $qty2 + $qty3;

        if ($balqty != 0) {
            if ($balqty > 0) {
                $contents .= "$balqty" . ",";
            } else {

                $contents .= "$balqty" . ",";
            }
        } else {
            $contents .= "" . ",";
        }


        if ($prnval == 2) {
            $itemvalue = $newcost * $balqty;
            $totval = $totval + $itemvalue;

            $contents .= "$newcost" . ",";
            $contents .= "$itemvalue" . "\n";

            $balqty = 0;
            $itemvalue = 0;
            $newcost = 0;
        }

//                                echo "</tr>";
//                                $gr1 = $gr1 + $qty1;
//                                $gr2 = $gr2 + $qty2;
//                                $gr3 = $gr3 + $qty3;
//                                $grbal = $grbal + $balqty;
//                                $grval = $grval + $itemvalue;
//                            }
//                            echo "<tr style ='background-color:white;'> ";
//                            echo "<td align='right' colspan='3'><font size='4px'><b> Total</b></font></td>";
//                            echo "<td align='right'><font size='3px'<b>$gr1</b></td>";
//                            echo "<td align='right'><font size='3px'<b>$gr2</b></td>";
//                            echo "<td align='right'><font size='3px'<b>$gr3</b></td>";
//                            echo "<td align='right'><font size='3px'<b>$grbal</b></td>";
//                            if ($prnval == 2) {
//                                echo "<td align='right'>";
//                                echo "<font><b></b>";
//                                echo "</td>";
//                                echo "<td align='right'>";
//                                echo "<font size='3px'<b> " . rajnumformat($grval) . "</b>";
//                                echo "</td>";
//                            }
        ?>
        <?php
    }
//
//if (isset($_POST["excel"])) {
//
//    $contents .= "Sr, barcode, itemname, zb1, zbx, str, total,";
//
//    if ($prnval == 2) {
//        $contents .= "cost, value \n";
//    }
//    $gropen = 0;
//    $grsale = 0;
//    $grpur = 0;
//    $grbal = 0;
//    $grval = 0;
//    $mopen = 0;
//    $ctr = 0;
//    $totqty = $totval = 0;
//
//    $gettran = mysqli_query($conn, "SELECT * FROM tempstk  group by itemcode order by itemcode, series ")or die(mysqli_error($conn));
//
//    while ($row = mysqli_fetch_object($gettran)) {
//        $ctr++;
//        $icode = $row->itemcode;
//        $itemname = $row->name;
//        $cost = $row->cost;
//        $rate = $row->rate;
//
//        if ($prnval == 2) {
//            if ($cost > 0) {
//                $actcost = $cost;
//            } else {
//                $getpur = mysqli_query($conn, "SELECT `rate` FROM `purtran` WHERE `itemcode`='$icode' and rate > 0 
//                                                                               and `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));
//                $getnum = mysqli_num_rows($getpur);
//                if ($getnum > 0) {
//                    $chkpurrate = mysqli_fetch_array($getpur);
//                    $actcost = $chkpurrate['rate'];
//                } else {
//                    $actcost = $rate;
//                }
//            }
//            $newcost = $actcost;
//        }
//
//        $get1 = mysqli_query($conn, "SELECT sqty FROM tempstk WHERE `itemcode`='$icode' and `series` = '$serno1'") or die(mysqli_error($conn));
//        $fet1 = mysqli_fetch_array($get1);
//        $qty1 = $fet1['sqty'];
//
//        $get2 = mysqli_query($conn, "SELECT sqty FROM tempstk WHERE `itemcode`='$icode' and `series` = '$serno2'") or die(mysqli_error($conn));
//        $fet2 = mysqli_fetch_array($get2);
//        $qty2 = $fet2['sqty'];
//
//        $get3 = mysqli_query($conn, "SELECT sqty FROM tempstk WHERE `itemcode`='$icode' and `series` = '$serno3'") or die(mysqli_error($conn));
//        $fet3 = mysqli_fetch_array($get3);
//        $qty3 = $fet3['sqty'];
//
//        $contents .= "$ctr" . ",";
//        $contents .= "$icode" . ",";
//        $contents .= "$itemname" . ",";
//
//        if ($qty1 != 0) {
//            $contents .= "$qty1" . ",";
//        } else {
//            $contents .= "" . ",";
//        }
//
//        if ($qty2 != 0) {
//            $contents .= "$qty2" . ",";
//        } else {
//            $contents .= "" . ",";
//        }
//
//        if ($qty3 != 0) {
//            $contents .= "$qty3" . ",";
//        } else {
//            $contents .= "" . ",";
//        }
//
//        $balqty = $qty1 + $qty2 + $qty3;
//
//        if ($balqty != 0) {
//            if ($balqty > 0) {
//                $contents .= "$balqty" . ",";
//            } else {
//                $contents .= "$balqty" . ",";
//            }
//        } else {
//            $contents .= "" . ",";
//        }
//
//
//        if ($prnval == 2) {
//            $itemvalue = $newcost * $balqty;
//            $totval = $totval + $itemvalue;
//
//            $contents .= "$newcost" . ",";
//            $contents .= "$itemvalue" . "\n";
//
////            echo "<td style='text-align:right;'>";
////            echo "<font size='3px'><b> " . number_format("$newcost", 2, ".", ",") . "</b>";
////            echo "</td>";
////
////            echo "<td style='text-align:right;'>";
////            echo "<font size='3px'><b> " . number_format("$itemvalue", 0, ".", ",") . "</b>";
////            echo "</td>";
//        }
//        $balqty = 0;
//        $itemvalue = 0;
//        $newcost = 0;
//    }

    $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"])) {

    $gbrcd = trim($_POST["itemcode"]);
    $gnm = $_POST["name"];
    $gname = trim($_POST["itemgrp"]);
    $gprodtype = trim($_POST["prodtype"]);
    $gsubgrp = trim($_POST["subgrp"]);
    $prnval = trim($_POST["prnval"]);
    $gperc = trim($_POST["gperc"]);


    $brcond = "";
    $ncond = "";
    $gcond = "";
    $subcond = "";
    $gperccond = "";
    $gprodcond = "";


    if (!empty($_POST["itemcode"])) {
        $brcond = " and `code` = '$gbrcd' ";
    }

    if (!empty($_POST["name"])) {
        $ncond = " and `citemsn` = '$gnm' ";
    }

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

    if (!empty($_POST["prodtype"])) {
        $gprodcond = " and `prodtype` = '$gprodtype' ";
    }

    if (!empty($_POST["subgrp"])) {
        $subcond = " and `itemsubgrp` = '$gsubgrp' ";
    }

    if (!empty($_POST["gsttype"])) {
        $gperccond = " and `gstperc` = '$gperc' ";
    }

    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");
    }


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

//    $getitem = mysqli_query($conn, "SELECT a.*,b.code, b.name, b.itemgrp, b.cost, b.opening FROM `$trandb` a, `$maindb` b WHERE a.itemcode = b.code 
//                          $ncond $brcond $gcond $subcond group by a.itemcode") or die(mysqli_error($conn));
    $getitem = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE code != '' $ncond $brcond $subcond $gcond $gprodcond $gperccond group by code") or die(mysqli_error($conn));
    $getno = mysqli_num_rows($getitem);
//print table($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(64, 37, 91);
    $pdf->SetTextColor(255, 255, 255);
    if ($prnval == 2) {
        $pdf->Cell(10, 7, "Sr.", 1, 0, '', 1);
        $pdf->Cell(39, 7, "Barcode", 1, 0, '', 1);
        $pdf->Cell(49, 7, "Item Name", 1, 0, '', 1);
        $pdf->Cell(30, 7, "Opening", 1, 0, 'R', 1);
        $pdf->Cell(30, 7, "Add", 1, 0, 'R', 1);
        $pdf->Cell(30, 7, "Less", 1, 0, 'R', 1);
        $pdf->Cell(30, 7, "Balance", 1, 0, 'R', 1);
        $pdf->Cell(30, 7, "Cost", 1, 0, 'R', 1);
        $pdf->Cell(30, 7, "Value", 1, 0, 'R', 1);
    } else {
        $pdf->Cell(10, 7, "Sr.", 1, 0, '', 1);
        $pdf->Cell(48, 7, "Barcode", 1, 0, '', 1);
        $pdf->Cell(60, 7, "Item Name", 1, 0, '', 1);
        $pdf->Cell(40, 7, "Opening", 1, 0, 'R', 1);
        $pdf->Cell(40, 7, "Add", 1, 0, 'R', 1);
        $pdf->Cell(40, 7, "Less", 1, 0, 'R', 1);
        $pdf->Cell(40, 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($conn, $zdb);
    while ($row = mysqli_fetch_object($getitem)) {

        $icode = $row->code;
        $ibarcode = $row->code;

        $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 itemcode = '$icode'  
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  itemcode = '$icode' 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 itemcode = '$icode' 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(10, 7, "$ctr", 1);
            $pdf->Cell(39, 7, "$ibarcode", 1);
            $pdf->Cell(49, 7, "$itemname", 1);
            $pdf->Cell(30, 7, "$openqty", 1, 0, 'R');
            $pdf->Cell(30, 7, "$purqty", 1, 0, 'R');
            $pdf->Cell(30, 7, "$saleqty", 1, 0, 'R');
            $pdf->Cell(30, 7, "$balqty", 1, 0, 'R');
            $pdf->Cell(30, 7, number_format("$newcost", 0, ".", ","), 1, 0, 'R');
            $pdf->Cell(30, 7, number_format("$itemvalue", 0, ".", ","), 1, 0, 'R');
        } else {
            $pdf->Cell(10, 7, "$ctr", 1);
            $pdf->Cell(48, 7, "$ibarcode", 1);
            $pdf->Cell(60, 7, "$itemname", 1);
            $pdf->Cell(40, 7, "$openqty", 1, 0, 'R');
            $pdf->Cell(40, 7, "$purqty", 1, 0, 'R');
            $pdf->Cell(40, 7, "$saleqty", 1, 0, 'R');
            $pdf->Cell(40, 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(10, 7, " ", 1, 0, '', 1);
        $pdf->Cell(39, 7, " ", 1, 0, '', 1);
        $pdf->Cell(49, 7, "Total", 1, 0, '', 1);
        $pdf->Cell(30, 7, "$gropen", 1, 0, 'R', 1);
        $pdf->Cell(30, 7, "$grpur", 1, 0, 'R', 1);
        $pdf->Cell(30, 7, "$grsale", 1, 0, 'R', 1);
        $pdf->Cell(30, 7, "$grbal", 1, 0, 'R', 1);
        $pdf->Cell(30, 7, " ", 1, 0, '', 1);
        $pdf->Cell(30, 7, "$grval", 1, 0, 'R', 1);
    } else {
        $pdf->Cell(10, 7, " ", 1, 0, '', 1);
        $pdf->Cell(48, 7, " ", 1, 0, '', 1);
        $pdf->Cell(60, 7, "Total", 1, 0, '', 1);
        $pdf->Cell(40, 7, "$gropen", 1, 0, 'R', 1);
        $pdf->Cell(40, 7, "$grpur", 1, 0, 'R', 1);
        $pdf->Cell(40, 7, "$grsale", 1, 0, 'R', 1);
        $pdf->Cell(40, 7, "$grbal", 1, 0, 'R', 1);
    }
    $pdf->Ln();

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

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