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

$maindb = "salemain";
$trandb = "saletran";
$formname = "Fast Moving Item";
$file = "fastmvitem_fr.php";
$usertype = $_SESSION["usertype_" . $dbname];


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

$gbrcd = trim($_POST["itemcode"]);
$gnm = $_POST["name"];
$series = $_POST["series"];
$orderby = $_POST["orderby"];
$itemgrp = trim($_POST["itemgrp"]);


$seriescond = "";
$brcond = "";
$ncond = "";
$itemgrpcond = "";

//    $orderbycond = "";

$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);
//browse($mfrmdate);
//browse($mtodate);
//    if (!empty($_POST["orderby"])) {
//        if ($orderby == 1) {
//            $orderbycond = " order by tqty desc";
//        } else {
//            $orderbycond = " order by ttotal desc";
//        }
//    }
$orderbycond = " order by tqty asc";

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

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

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

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

if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; staffcomm.php");
}
if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; staffcomm.php");
}

$getpmain = mysqli_query($conn, "SELECT a.itemcode, a.barcode,  sum(a.qty) as tqty,a.unit, b.itemgrp  FROM `transacn` a
                        INNER JOIN item b on a.itemcode = b.code where (a.source = 'SL' or a.source = 'ONSL') and a.vouchdt >= '$mfrmdate' and a.vouchdt <= '$mtodate'  
                        $seriescond $itemgrpcond $brcond $ncond  group by a.itemcode $orderbycond $optioncond ")or die(mysqli_error($conn));


//$getpmain = mysqli_query($conn, "SELECT itemcode, barcode,  sum(qty) as tqty,unit 
//                                          FROM `transacn` where (source = 'SL' or source = 'ONSL') and vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate'  $seriescond $brcond $ncond  group by itemcode $orderbycond ")or die(mysqli_error($conn));
//$getpmain = mysqli_query($conn, "select a.* from (SELECT item, barcode,  sum(total) as ttotal, sum(qty) as tqty,unit 
//                                          FROM `saletran` where vouchno != ''  $seriescond $brcond $ncond 
//                                 union all 
//                                 SELECT itemcode as item, barcode,  sum(amount) as ttotal, sum(trqty) as tqty,unit 
//                             FROM `onsltran` where vouchno != ''  $seriescond $brcond $ncond) a group by item $orderbycond ")or die(mysqli_error($conn));

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

//    print table($getpmain);
    $fnum = mysqli_num_rows($getpmain);
    if ($fnum <= 0) {
        echo"<script>"
        . "window.close()"
        . "</script>";
    }
//    print table($getpmain);
    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">
            <?php
            include("includes/head_rep.php");
            if ($series != '') {
                $mseries = $series;
            } else {
                $mseries = 'All';
            }
            ?>
            <div class="row pad" style="padding-left:1%;color:red;font-size: 18px"><header><b>Branch : <?= $mseries ?></b></header></div>
            <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 style="width:5%">Sr</th>
                                <th style="width:10%">Barcode</th>
                                <th style="width:10%">Itemname</th>
                                <th style="width:10%" class="text-right">Qty.</th>
                                <!--<th style="width:10%" class="">Unit</th>-->
                                <!--<th style="width:10%" class="text-right">Amount</th>-->
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totqty = 0;
                            $totnet = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $itemcode = $pmain['itemcode'];
                                $barcode = $pmain['barcode'];
                                $tqty = abs($pmain['tqty']);
                                $unit = $pmain['unit'];
                                $ttotal = $pmain['ttotal'];
                                $getitem = mysqli_query($conn, "Select code, name from `item` where code = '$itemcode'") or die(mysqli_error($conn));
                                $fetitem = mysqli_fetch_array($getitem);
                                $itemname = $fetitem['name'];
                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$barcode</td>";
                                Echo "<td>$itemname</td>";
                                Echo "<td class='text-right'>$tqty</td>";
//                                Echo "<td class=''>$unit</td>";
//                                Echo "<td class='text-right'>$ttotal</td>";
                                Echo "</tr>";
                                $totqty = $totqty + $tqty;
                                $totnet = $totnet + $ttotal;
                            }
                            $colr = "red";
                            echo "<tr> ";

                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>" . number_format($totqty, 2) . "</b></h4></td>";
//                            echo "<td></td>";
//                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>" . number_format($totnet, 2) . "</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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


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

    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->SetMargins(1,1,1);
//$pdf->SetXY(5,5);
    include 'includes/head_rep_pdf.php';
    if ($series != '') {
        $mseries = $series;
    } else {
        $mseries = 'All';
    }


    $pdf->Cell(10, 7, '', 0, 'C');
    $pdf->Ln();

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(72, 25, 89);
    $pdf->Cell(30, 8, 'Sr.', 1, 0, '', 1);
    $pdf->Cell(48, 8, 'Barcode', 1, 0, '', 1);
    $pdf->Cell(65, 8, 'Item Name', 1, 0, '', 1);
    $pdf->Cell(65, 8, 'Quantity', 1, 0, 'R', 1);
    $pdf->Cell(70, 8, 'Unit', 1, 0, 'L', 1);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $ctr = 0;
    $totqty = 0;
    $totnet = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $itemcode = $pmain['itemcode'];
        $barcode = $pmain['barcode'];
        $tqty = abs($pmain['tqty']);
        $unit = $pmain['unit'];
        $ttotal = $pmain['ttotal'];
        $getitem = mysqli_query($conn, "Select code, name from `item` where code = '$itemcode'") or die(mysqli_error($conn));
        $fetitem = mysqli_fetch_array($getitem);
        $itemname = $fetitem['name'];
        $ctr++;

        $pdf->SetFont('Arial', '', 10);
//        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(30, 8, $ctr, 1, 0, '', 1);
        $pdf->Cell(48, 8, $barcode, 1, 0, '', 1);
        $pdf->Cell(65, 8, $itemname, 1, 0, '', 1);
        $pdf->Cell(65, 8, $tqty, 1, 0, 'R', 1);
        $pdf->Cell(70, 8, $unit, 1, 0, 'L', 1);
        $pdf->Ln();

        $totqty = $totqty + $tqty;
        $totnet = $totnet + $ttotal;
    }


    $pdf->SetFont('Arial', '', 10);
    $pdf->SetTextColor(255, 15, 53);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(143, 8, 'Grand Total', 1, 0, 'R', 1);
    $pdf->Cell(65, 8, number_format($totqty, 2), 1, 0, 'R', 1);
//    $pdf->Cell(70, 8, number_format($totnet, 2), 1, 0, 'R', 1);
    $pdf->Ln();

    $pdf->Output('FastMvItem.pdf', 'D');
}


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

    include 'includes/head_rep_excel.php';

    $contents .= "Sr.,Voucher No.,Date,Qty,Unit\n";

    $ctr = 0;
    $totqty = 0;
    $totnet = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $itemcode = $pmain['itemcode'];
        $barcode = $pmain['barcode'];
        $tqty = abs($pmain['tqty']);
        $ttotal = $pmain['ttotal'];
        $unit = $pmain['unit'];
        $getitem = mysqli_query($conn, "Select code, name from `item` where code = '$itemcode'") or die(mysqli_error($conn));
        $fetitem = mysqli_fetch_array($getitem);
        $itemname = $fetitem['name'];
        $ctr++;

        $contents .= $ctr . ",";
        $contents .= $barcode . ",";
        $contents .= $itemname . ",";
        $contents .= $tqty . ",";
        $contents .= $unit . "\n";
//        $contents .= $ttotal . "\n";

        $totqty = $totqty + $tqty;
        $totnet = $totnet + $ttotal;
    }

    $contents .= '' . ",";
    $contents .= '' . ",";
    $contents .= 'Grand Total' . ",";
    $contents .= $totqty . "\n";
//    $contents .= $totnet . "\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;
    }
}
?>