<?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 = 'product';
$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"]);
$giname = trim($_POST["itemname"]);

$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.`barcode` = '$gbrcd' ";
}

$itcond = "";
if (!empty($_POST["itemname"])) {
    $itcond = " and b.`name` like '%$giname%' ";
}


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

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

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


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

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


$gctr = 1;
$getgodw1 = mysqli_query($conn, "SELECT code, gcode FROM `godown` order by code limit 0,1") or die(mysqli_error($conn));
$fetgodw1 = mysqli_fetch_array($getgodw1);
$g1 = $fetgodw1['code'];
$gn1 = $fetgodw1['gcode'];

$getgodw2 = mysqli_query($conn, "SELECT code, gcode FROM `godown` order by code limit 1,1") or die(mysqli_error($conn));
$fetgodw2 = mysqli_fetch_array($getgodw2);
$g2 = $fetgodw2['code'];
$gn2 = $fetgodw2['gcode'];

$getgodw3 = mysqli_query($conn, "SELECT code, gcode FROM `godown` order by code limit 2,1") or die(mysqli_error($conn));
$fetgodw3 = mysqli_fetch_array($getgodw3);
$g3 = $fetgodw3['code'];
$gn3 = $fetgodw3['gcode'];



mysqli_query($conn, "drop temporary table if exists tempdb") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table tempdb 
                            SELECT sum(a.qty) as sqty, b.barcode, a.godown,
                                   b.name, b.cost, b.rate FROM `$trandb` a, `$maindb` b 
                                   WHERE a.barcode = b.barcode and a.`vouchdt` <= '$mtodate' 
                                   $ncond $brcond  $gcond $itcond group by a.godown, a.barcode") or die(mysqli_error($conn));

$gettran = mysqli_query($conn, "SELECT * FROM `tempdb` group by barcode order by name ") or die(mysqli_error($conn));

$get = mysqli_query($conn, "SELECT * FROM `tempdb` order by name ") 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%'>Barcode</th>
                                <th align="left"  width='25%'>Item Name</th>

                                <th width='7%' style="text-align: right;"><?php echo $gn1 ?></th>
                                <th width='7%' style="text-align: right;"><?php echo $gn2 ?></th>
                                <th width='7%' style="text-align: right;"><?php echo $gn3 ?></th>

                                <th width='8%' style="text-align: right;">Total</th>
                                <?php if ($prnval == 2) { ?>
                                    <th width='8%' style="text-align: right;">Cost</th>
                                    <th width='9%' style="text-align: right;">Value</th>
                                    <?php } ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $gropen = 0;
                            $grsale = 0;
                            $grpur = 0;
                            $grbal = 0;
                            $grval = 0;
                            $mopen = 0;
                            $ctr = 0;

                            $totqty = $totval = 0;

                            while ($row = mysqli_fetch_object($gettran)) {
                                $ctr++;
                                $ibarcode = $row->barcode;
                                $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 `barcode`='$ibarcode' 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;
                                }

                                $balqqty1 = $balqty2 = $balqty3 = 0;

                                $getstk1 = mysqli_query($conn, "SELECT sum(sqty) as qty1 FROM `tempdb` where `barcode`='$ibarcode' and godown = '$g1'  ") or die(mysqli_error($conn));
                                $fetstk1 = mysqli_fetch_array($getstk1);
                                $balqty1 = $fetstk1['qty1'];

                                $getstk2 = mysqli_query($conn, "SELECT sum(sqty) as qty2 FROM `tempdb` where `barcode`='$ibarcode' and godown = '$g2'  ") or die(mysqli_error($conn));
                                $fetstk2 = mysqli_fetch_array($getstk2);
                                $balqty2 = $fetstk2['qty2'];

                                $getstk3 = mysqli_query($conn, "SELECT sum(sqty) as qty3 FROM `tempdb` where `barcode`='$ibarcode' and godown = '$g3'  ") or die(mysqli_error($conn));
                                $fetstk3 = mysqli_fetch_array($getstk3);
                                $balqty3 = $fetstk3['qty3'];

                                $balqty = $balqty1 + $balqty2 + $balqty3;

                                echo "<tr>";
                                echo "<td align='left'><font size='3px'>$ctr</td>";
                                echo "<td align='left'><font size='3px'>$ibarcode</td>";
                                echo "<td align='left'><font size='3px'>$itemname</td>";

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

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

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

                                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>";
                                $gr1 = $gr1 + $balqty1;
                                $gr2 = $gr2 + $balqty2;
                                $gr3 = $gr3 + $balqty3;
                                $grbal = $grbal + $balqty;
                                $grval = $grval + $itemvalue;
                            }

                            echo "<tr>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td>Grand Total</td>";

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

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

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

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

                            if ($prnval == 2) {
                                echo "<td></td>";

                                echo "<td style='text-align:right;'>";
                                echo "<font size='3px'><b> " . number_format($grval, 0, ".", ",") . "</b>";
                                echo "</td>";
                            }
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </body>
    </html>
    <?php
}


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

    $contents .= "Sr., Barcode, Itemname, GD, WH, NF, Total, Cost, Value \n";

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

    $totqty = $totval = 0;

    while ($row = mysqli_fetch_object($gettran)) {
        $ctr++;
        $ibarcode = $row->barcode;
        $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 `barcode`='$ibarcode' 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;
        }

        $balqqty1 = $balqty2 = $balqty3 = 0;

        $getstk1 = mysqli_query($conn, "SELECT sum(sqty) as qty1 FROM `tempdb` where `barcode`='$ibarcode' and godown = '$g1'  ") or die(mysqli_error($conn));
        $fetstk1 = mysqli_fetch_array($getstk1);
        $balqty1 = $fetstk1['qty1'];

        $getstk2 = mysqli_query($conn, "SELECT sum(sqty) as qty2 FROM `tempdb` where `barcode`='$ibarcode' and godown = '$g2'  ") or die(mysqli_error($conn));
        $fetstk2 = mysqli_fetch_array($getstk2);
        $balqty2 = $fetstk2['qty2'];

        $getstk3 = mysqli_query($conn, "SELECT sum(sqty) as qty3 FROM `tempdb` where `barcode`='$ibarcode' and godown = '$g3'  ") or die(mysqli_error($conn));
        $fetstk3 = mysqli_fetch_array($getstk3);
        $balqty3 = $fetstk3['qty3'];

        $balqty = $balqty1 + $balqty2 + $balqty3;

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

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

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

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

        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";
        }
        $gr1 = $gr1 + $balqty1;
        $gr2 = $gr2 + $balqty2;
        $gr3 = $gr3 + $balqty3;
        $grbal = $grbal + $balqty;
        $grval = $grval + $itemvalue;
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";

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

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

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

    if ($prnval == 2) {
                 $contents .= "" . ",";
                 $contents .= "$grval" . "\n";
    }
    ?>
    <?php
    $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 `code` = '$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");
}
?>