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

$maindb = "item";
$trandb = "itemsub";
$formname = "Item Print";
$file = "itemprint.php";

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

if (isset($_POST["view"])) {
    $brand = $_POST['brand'];
    $item = $_POST['item'];
    $type = $_POST['type'];
    $rtype = $_POST['rtype'];

    $brandcond = "";
    $itemcond = "";
    $typecond = "";
    $rtypecond = "";

    if (!empty($brand)) {
        $brandcond = " AND `brand`='$brand' ";
    }
    if (!empty($item)) {
        $itemcond = " AND `name`=%$item% ";
    }
    if (!empty($type)) {
        $tyepcond = " AND `type`='$type' ";
    }


    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code`!='' $brandcond $itemcond $tyepcond")or die(mysqli_error($conn));

    include("includes/linkshead.php");
    ?>
    <script src="MyScript/item.js" type="text/javascript"></script>
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="width:100%;background: white" class="table table-condensed table-hover">
                        <thead>
                            <tr style="height:10px;background-color: #1ABC9C">
                                <th>Sr</th>
                                <th>Code</th>
                                <th>Name</th>
                                <th>Unit</th>                            
                                <th>Brand</th>
                                <th class='numeric'>Cost</th>
                                <th class='numeric'>Selling</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $getmtype = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$pmain[brand]'")or die(mysqli_error($conn));
                                $mtype = mysqli_fetch_array($getmtype);
                                $getmqty = mysqli_query($conn, "SELECT SUM(qty)as totqty FROM `$trandb` WHERE `code`='$pmain[code]'")or die(mysqli_error($conn));
                                $mqty = mysqli_fetch_array($getmqty);
                                $ctr++;
                                Echo "<tr>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[code]</td>";
                                Echo "<td>$pmain[name]</td>";
                                Echo "<td>$pmain[unit]</td>";
                                Echo "<td>$mtype[name]</td>";
                                Echo "<td class='numeric'>$pmain[cost]</td>";
                                Echo "<td class='numeric'>$pmain[selling]</td>";
                                Echo "</tr>";
                                Echo "<tr>";

                                if ($_POST["rtype"] == 2) {

                                    Echo "<tr style='height:10px;'>";
                                    Echo "<td colspan='25'>";
                                    Echo "<table style='width:100%;' class='table table-condensed table-hover'>";
                                    Echo "<th style='background-color: #cccccc'>Sr</th>";
                                    Echo "<th style='background-color: #cccccc'>Godown</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Taka</th>";
                                    Echo "<th style='background-color: #cccccc'>Batch</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Qty.</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Tot. Qty.</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Flag Qty.</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Act Qty.</th>";
                                    Echo "<tr>";


                                    $ctr2 = 0;
                                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `code`='$pmain[code]'")or die(mysqli_error($conn));
                                    while ($tran = mysqli_fetch_array($gettran)) {

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$tran[godown]</td>";
                                        Echo "<td class='numeric'>$tran[taka]</td>";
                                        Echo "<td>$tran[batch]</td>";
                                        Echo "<td class='numeric'>$tran[qty]</td>";
                                        Echo "<td class='numeric'>$tran[totqty]</td>";
                                        Echo "<td class='numeric'>$tran[flagqty]</td>";
                                        Echo "<td class='numeric'>$tran[actqty]</td>";
                                        Echo "</tr>";
                                    }
                                    Echo "</table>";
                                    Echo "</td>";
                                }
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}


if (isset($_POST["subpdf"])) {
    $brand = $_POST['brand'];
    $item = $_POST['item'];
    $type = $_POST['type'];
    $rtype = $_POST['rtype'];

    $brandcond = "";
    $itemcond = "";
    $typecond = "";
    $rtypecond = "";

    if (!empty($brand)) {
        $brandcond = " AND `brand`='$brand' ";
    }
    if (!empty($item)) {
        $itemcond = " AND `name`=%$item% ";
    }
    if (!empty($type)) {
        $tyepcond = " AND `type`='$type' ";
    }


    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code`!='' $brandcond $itemcond $tyepcond")or die(mysqli_error($conn));


    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();
    $pdf->SetFont('Arial', 'I', 8);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(26, 188 , 156);
    $pdf->Cell(8, 7, "Sr.", 1, 0, '', 1);
    $pdf->Cell(20, 7, "Code", 1, 0, '', 1);
    $pdf->Cell(80, 7, "Name", 1, 0, '', 1);
    $pdf->Cell(20, 7, "Unit", 1, 0, '', 1);
    $pdf->Cell(25, 7, "Brand", 1, 0, '', 1);
    $pdf->Cell(40, 7, "Cost", 1, 0, '', 1);
    $pdf->Cell(40, 7, "Selling", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();
    $ctr = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getmtype = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$pmain[brand]'")or die(mysqli_error($conn));
        $mtype = mysqli_fetch_array($getmtype);
       
        $ctr++;
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(8, 7, "$ctr", 1, 'C');
        $pdf->Cell(20, 7, "$pmain[code]", 1);
        $pdf->Cell(80, 7, "$pmain[name]", 1);
        $pdf->Cell(20, 7, "$pmain[unit]", 1);
        $pdf->Cell(25, 7, "$mtype[name]", 1);
        $pdf->Cell(40, 7, "$pmain[cost]", 1);
        $pdf->Cell(40, 7, "$pmain[selling]", 1);
        $pdf->Ln();
    }

    $pdf->Output("item.pdf", "D");
}


if (isset($_POST["excel"])) {
    $brand = $_POST['brand'];
    $item = $_POST['item'];
    $type = $_POST['type'];
    $rtype = $_POST['rtype'];

    $brandcond = "";
    $itemcond = "";
    $typecond = "";
    $rtypecond = "";

    if (!empty($brand)) {
        $brandcond = " AND `brand`='$brand' ";
    }
    if (!empty($item)) {
        $itemcond = " AND `name`=%$item% ";
    }
    if (!empty($type)) {
        $tyepcond = " AND `type`='$type' ";
    }


    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code`!='' $brandcond $itemcond $tyepcond")or die(mysqli_error($conn));

    $contents .= "Sr, Type, Item, Item, Qty., Cost, Selling\n";

    $ctr = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getmtype = mysqli_query($conn, "SELECT * FROM `type` WHERE `code`='$pmain[type]'")or die(mysqli_error($conn));
        $mtype = mysqli_fetch_array($getmtype);
        $getmqty = mysqli_query($conn, "SELECT SUM(qty)as totqty FROM `$trandb` WHERE `code`='$pmain[code]'")or die(mysqli_error($conn));
        $mqty = mysqli_fetch_array($getmqty);
        $ctr++;

        $ctr;
        $pname = $mtype['name'];
        $pname1 = $pmain['name'];
        $punit = $pmain['unit'];
        $pmqty = $mqty['totqty'];
        $ppcost = $pmain['cost'];
        $pselling = $pmain['selling'];

        $contents .= $ctr . ",";
        $contents .= $pname . ",";
        $contents .= $pname1 . ",";
        $contents .= $punit . ",";
        $contents .= $pmqty . ",";
        $contents .= $ppcost . ",";
        $contents .= $pselling . "\n";

        if ($_POST["rtype"] == 2) {
            $contents .= "Sr, Godown, Taka, Batch, Qty., Tot. Qty., Flag Qty., Act Qty.\n";

            $ctr2 = 0;
            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `code`='$pmain[code]'")or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $ctr2++;
                $rgodown = $tran['godown'];
                $rtaka = $tran['taka'];
                $rbatch = $tran['batch'];
                $rqty = $tran['qty'];
                $rtotqty = $tran['totqty'];
                $rflagqty = $tran['flagqty'];
                $ractqty = $tran['actqty'];

                $contents .= $ctr2 . ",";
                $contents .= $rgodown . ",";
                $contents .= $rtaka . ",";
                $contents .= $rbatch . ",";
                $contents .= $rqty . ",";
                $contents .= $rtotqty . ",";
                $contents .= $rflagqty . ",";
                $contents .= $ractqty . "\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;
    }
}
?>