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

$maindb = "packmain";
$trandb = "packtran";
$stkdb = "packsacn";

$formname = "Pending Packing Register";
$file = "pendpack_fr.php";

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

$barcode = $_POST["barcode"];
$party = $_POST["party"];
$item = $_POST["item"];
$color = $_POST['color'];
$dtype = $_POST['dtype'];    //  1 - summary  - ---2 - detail

$srcond = "";
$branch = trim($_POST["series"]);
$spltype = $_SESSION["spltype_" . $dbname];

if ($spltype != 1) {
    if (!empty($branch)) {
        $srcond = "and `series`='$branch'";
    }
}
$barcond = "";
$partycond = "";
$itemcond = "";
$colrcond = "";

$mfrmdate = $_POST["fdate"];
$mtodate = $_POST["tdate"];

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

if (!empty($item)) {
    $itemcond = " AND a.`itemcode`='$item' ";
}

if (!empty($color)) {
    $colrcond = " AND `color`='$color' ";
}

$qry = mysqli_query($conn, "SELECT a.barcode, a.itemcode, a.color, sum(a.qty) as sqty, b.name as prodname, c.name as colrname 
                                  from $stkdb a, item b, color c 
                                  where a.vouchno > 0 and a.itemcode = b.code and a.color = c.code $barcond $prodcond $colrcond 
                                  group by a.itemcode, a.color having sum(qty) > 0 order by b.name, c.name ") or die(mysqli_error($conn));

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

    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; 
        }
        .dtype{
            background-color: #C89595;
        }
    </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-striped table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th width='5%'>Sr</th>
                                <th width='6%' class='dtype'>Barcode</th>
                                <th width='6%' class='dtype'>Product</th>
                                <th width='6%' class='dtype'>Color</th>
                                <th class="text-right" width='10%'>Qty</th>
                            </tr>
                        </thead>

                        <tbody>
                            <?php
                            $ctr = 0;
                            $ttotamt = 0;
                            $totpack = 0;
                            $totbill = 0;
                            $totitmpr = 0;
                            $totdisc = 0;
                            $tottaxamt = 0;
                            $totcgst = 0;
                            $totsgst = 0;
                            $totigst = 0;
                            $totroundoff = 0;

                            $ctr2 = 0;
                            $totqty = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 0;

                            while ($tran = mysqli_fetch_array($qry)) {
                                $ordno = $tran['ordno'];

                                $ctr2++;
                                Echo "<tr>";
                                Echo "<td>$ctr2</td>";
                                Echo "<td class=''>$tran[barcode]</td>";
                                Echo "<td class=''>$tran[prodname]</td>";
                                Echo "<td class=''>$tran[colrname]</td>";
                                Echo "<td class='numeric'>$tran[sqty]</td>";
                                Echo "</tr>";
                                
                                $totqty = $totqty + $tran['sqty'];

                                $tqty = number_format($totqty, 2);
                            }
                            $colr1 = "blue";
                            echo "<tr> ";
                            echo "<td style='color:$colr1'</td>";
                            echo "<td style='color:$colr1'</td>";
                            echo "<td style='color:$colr1'</td>";
                            echo "<td style='color:$colr1'><b>Total</b></td>";
                            echo "<td style='color:$colr1'class='text-right'><b>$tqty</b></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>


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


if (isset($_POST["excel"])) {
  
                               $contents .= "Sr,Barcode,Product,Color,Qty\n";     
                          
                            $ctr = 0;
                            $ttotamt = 0;
                            $totpack = 0;
                            $totbill = 0;
                            $totitmpr = 0;
                            $totdisc = 0;
                            $tottaxamt = 0;
                            $totcgst = 0;
                            $totsgst = 0;
                            $totigst = 0;
                            $totroundoff = 0;

                            $ctr2 = 0;
                            $totqty = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 0;

                            while ($tran = mysqli_fetch_array($qry)) {
                                $ordno = $tran['ordno'];

                                $ctr2++;
                                $contents .= "$ctr2" . ",";
                                $contents .= "$tran[barcode]" . ",";
                                $contents .= "$tran[prodname]" . ",";
                                $contents .= "$tran[colrname]" . ",";
                                $contents .= "$tran[sqty]" . "\n";
                               
                                $totqty = $totqty + $tran['sqty'];

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