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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$maindb = 'rawsacn';
$zroot = $_SESSION["zdbroot"];
$formname = 'Item Inward / Outward Histtory';
$file = 'itemhist.php';

if ($_GET["item"] != '') {
    $itemcode = $_GET["item"];
} else {
    $itemcode = $_POST["item"];
}
$godown = $_POST["godown"];

$pcodecond = '';
$prodcond = '';
$grpcond = '';
$godwncond = '';

if (!empty($godown)) {
    $godwncond = " AND `godown` = '$godown' ";
}

if (!empty($_GET['todt'])) {
    $todate = trim($_GET["todt"]);
} else {
    $todate = trim($_POST["todt"]);
}

$mainqry = mysqli_query($conn, "SELECT SUM(qty) AS `sqty`,itemcode, source, vouchno, DATE_FORMAT(vouchdt , '%d-%m-%Y')as vouchdt FROM `rawsacn`
                        WHERE `itemcode`='$itemcode' and vouchdt <= '$todate' $godwncond group by vouchno order by vouchdt,source")or die(mysqli_error($conn));

$gproduct2 = mysqli_query($conn, "SELECT `code`, `name` FROM `item` WHERE `code` = '$itemcode' ")or die(mysqli_error($conn));
$fproduct = mysqli_fetch_array($gproduct2);
if (isset($_POST["view"]) || ($_GET['view'])) {
    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #26A69A;
        }
        .tsize{
            font-size:18px;
            padding : 6px;
        }
        tr td{
            font-size:14px;
        }
    </style>
    </head>
    <body>
        <div class="container" style="width:50%">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-bordered">
                        <tr class="mySearchTr">
                            <td style="color: white"><b>Product : <?php echo $fproduct['name']; ?></b></td>
                        </tr>
                    </table>
                    <table style="background: white;width: 100%" class="table table-condensed table-hover table-striped table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:3%;">Sr</th>
                                <th style="width:8%;">Date</th>
                                <th style="width:7%;">Source</th>
                                <th style="width:7%;">Vouchno</th>
                                <th style="width:7%;">Description</th>
                                <th style="width:7%;" class="text-right">Add</th>
                                <th style="width:7%;" class="text-right">Less</th>
                                <th style="width:8%;" class="text-right">Balance</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $add = 0;
                            $less = 0;
                            $balance = $totinwd = 0;
                            while ($fproduct = mysqli_fetch_array($mainqry)) {
                                $itemcode = $fproduct['itemcode'];
                                $date = $fproduct['vouchdt'];
                                $src = $fproduct['source'];
                                $vouchno = $fproduct['vouchno'];

                                $add = $less = "";
                                if ($fproduct['sqty'] > 0) {
                                    $add = $fproduct["sqty"];
                                } else if ($fproduct['sqty'] < 0) {
                                    $less = abs($fproduct['sqty']);
                                    $less2 = $fproduct['sqty'];
                                }

                                switch ($src) {
                                    case "OP":
                                        $source = "OPENING";
                                        break;

                                    case "PUPK":
                                        $source = "PURCHASE PACKING";
                                        break;

                                    case "PURW":
                                        $source = "PURCHASE RAW MATERIAL";
                                        break;


                                    case "PRPK":
                                        $source = "PURCHASE RETURN PACKING";
                                        break;

                                    case "PRPW":
                                        $source = "PURCHASE RETURN RAW MATERIAL";
                                        break;

                                    case "DMRM":
                                        $source = "DAMAGED RAW MATERIAL";
                                        break;

                                    case "DMPK":
                                        $source = "DAMAGED PACKING";
                                        break;
                                }

                                $balance = $balance + $add - $less;
                                $ctr++;
                                Echo"<tr>";
                                Echo"<td class=''>$ctr</td>";
                                Echo"<td class=''>$date</td>";
                                Echo"<td class=''>$src</td>";
                                Echo"<td class=''>$vouchno</td>";
                                Echo"<td>$source</td>";
                                Echo"<td class='text-right'>$add</td>";
                                Echo"<td class='text-right'>$less</td>";
                                Echo"<td class='text-right'>" . abs($balance) . "</td>";
                                Echo"</tr>";

                                $totadd = $totadd + $add;
                                $totless = $totless + $less;
                                $totless2 = $totless2 + $less2;
                            }
                            $totbal = $totadd - $totless;

                            Echo "<tr style='background-color:#26A69A'>";
                            Echo "<td class='text-right tsize' colspan = '5'>Total</td>";
                            Echo "<td class='text-right tsize'><b>$totadd</b></td>";
                            Echo "<td class='text-right tsize'><b>$totless</b></td>";
                            Echo "<td class='text-right tsize'><b>$totbal</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {

    $contents .= "Product : $fproduct[name] ,\n";
    $contents .= "\n";
    $contents .= "Sr., Date, Source, Vouchno, Description, Add, Less, Balance\n";

    $ctr = 0;
    $add = 0;
    $less = 0;
    $balance = $totinwd = 0;
    while ($fproduct = mysqli_fetch_array($gadd)) {
        $itemcode = $fproduct['itemcode'];
        $date = $fproduct['vouchdt'];
        $src = $fproduct['source'];
        $vouchno = $fproduct['vouchno'];

        $add = "";
        $less = "";

        if ($fproduct['sqty'] > 0) {
            $add = $fproduct["sqty"];
        } else if ($fproduct['sqty'] < 0) {
            $less = abs($fproduct['sqty']);
            $less2 = $fproduct['sqty'];
        }

        switch ($src) {
            case "OP":
                $source = "OPENING";
                break;

            case "SL":
                $source = "SALE";
                break;

            case "SLDE":
                $source = "DEEMED SALE";
                break;


            case "SLEX":
                $source = "EXPORT SALE";
                break;

            case "IN":
                $source = "INWARD";
                break;

            case "INDE":
                $source = "INWARD DEEMED";
                break;

            case "INEXP":
                $source = "INWARD EXPORT";
                break;

            case "DM":
                $source = "DAMAGED";
                break;
        }

        $balance = $balance + $add - $less;
        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $date . ",";
        $contents .= $src . ",";
        $contents .= $vouchno . ",";
        $contents .= $source . ",";
        $contents .= $add . ",";
        $contents .= $less . ",";
        $contents .= abs($balance) . "\n";

        $totadd = $totadd + $add;
        $totless = $totless + $less;
        $totless2 = $totless2 + $less2;
    }
    $totbal = $totadd + $totless2;

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