<?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);
$zroot = $_SESSION["zdbroot"];

$maindb = 'purmain';
$trandb = 'purtran';
$formname = 'Purchase Register';
$file = 'purchreg_fr.php';

$itemcond = '';
$vendorcond = '';

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

if (!empty($vendor)) {
    $vendorcond = " AND `vendor`= '$vendor'";
}

if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
} else {

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

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        echo "<script>window.close</script>";
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        echo "<script>window.close</script>";
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

$getpmain2 = mysqli_query($conn, "SELECT *,DATE_FORMAT(vouchdt, '%d-%m-%Y') AS mydt FROM `$maindb`
                                  WHERE `vouchno` != '' $itemcond $vendorcond ORDER BY vouchdt") or die(mysqli_error($conn));
$numrecs = mysqli_num_rows($getpmain2);

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;
        }
    </style>
    </head>

    <body>
        <div class="container-fluid">
            <?php
            include("includes/head_rep2.php");
            ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condesed table-bordered">
                        <tr>
                            <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                Total No. of Record :&nbsp;&nbsp;<b><?php echo $numrecs; ?></b>
                            </td>
                        </tr>
                    </table>

                    <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th width="3%;">Sr</th>
                                <th width="6%;">Date</th>
                                <th width="4%;">Time</th>
                                <th width="22%;">Item</th>
                                <th width="22%;">Vendor</th>
                                <th width="6%;" class="text-right">Qty</th>
                                <th width="6%;">Unit</th>
                                <th width="6%;">Order By</th>
                                <th width="26%;">Remark</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $pctr = $tqty = $tamt = 0;

                            while ($pmain = mysqli_fetch_array($getpmain2)) {

                                $getexp = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[expense]'") or die(mysqli_error($conn));
                                $sexp = mysqli_fetch_array($getexp);

                                $getven = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[vendor]'") or die(mysqli_error($conn));
                                $fven = mysqli_fetch_array($getven);

                                if ($pmain['paidby'] == "C") {
                                    $ptyped = "CASH";
                                } else if ($pmain['paidby'] == "R") {
                                    $ptyped = "CREDIT";
                                } else if ($pmain['paidby'] == "U") {
                                    $ptyped = "UPI";
                                }

                                $voucher = $pmain["vouchno"];
                                $mydt = $pmain["mydt"];
                                $time = date("g:i a", strtotime($pmain['time']));
                                $itemname = $sexp['name'];
                                $vendor = $fven['name'];
                                $qty = $pmain["qty"];
                                $unit = $pmain["unit"];
                                $orderby = $pmain['orderby'];
                                $remark = $pmain['remark'];

                                $pctr++;
                                echo "<tr data-href='order.php?mode=edit&vouchno=$voucher' class='clickable-row2' [target]='_blank'>";
                                echo "<td>$pctr</td>";
                                echo "<td>$mydt</td>";
                                echo "<td>$time</td>";
                                echo "<td>$itemname</td>";
                                echo "<td>$vendor</td>";
                                echo "<td class='numeric text-right'>$qty</td>";
                                echo "<td>$unit</td>";
                                echo "<td>$orderby</td>";
                                echo "<td>$remark</td>";
                                echo "</tr>";

                                $totqty += $qty;
                                $tamt += $amount;
                            }

                            echo "<tr style='background-color: #98F9F0'>";
                            echo "<td colspan='5'class='text-right'><b>Total</b></td>";
                            echo "<td class='text-right'><b>" . number_format($totqty,3) ."</b></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        <?php
    }

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

        $contents .= "Sr, Date, Time, Item, Vendor, Qty, Unit, Order By, Remark \n";

        $pctr = $tqty = $tamt = 0;

        while ($pmain = mysqli_fetch_array($getpmain2)) {

            $getexp = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[expense]'") or die(mysqli_error($conn));
            $sexp = mysqli_fetch_array($getexp);

            $getven = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[vendor]'") or die(mysqli_error($conn));
            $fven = mysqli_fetch_array($getven);

            if ($pmain['paidby'] == "C") {
                $ptyped = "CASH";
            } else if ($pmain['paidby'] == "R") {
                $ptyped = "CREDIT";
            } else if ($pmain['paidby'] == "U") {
                $ptyped = "UPI";
            }

            $voucher = $pmain["vouchno"];
            $mydt = $pmain["mydt"];
            $time = $pmain["time"];
            $itemname = $sexp['name'];
            $vendor = $fven['name'];
            $qty = $pmain["qty"];
            $unit = $pmain["unit"];
            $orderby = $pmain['orderby'];
            $remark = $pmain['remark'];

            $pctr++;
            $contents .= "$pctr" . ",";
            $contents .= "$mydt" . ",";
            $contents .= "$time" . ",";
            $contents .= "$itemname" . ",";
            $contents .= "$vendor" . ",";
            $contents .= "$qty" . ",";
            $contents .= "$unit" . ",";
            $contents .= "$orderby" . ",";
            $contents .= "$remark" . "\n";

            $totqty += $qty;
            $tamt += $amount;
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .=  number_format($totqty,3) . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . "\n";

        $contents = strip_tags($contents);

        if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
            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;
        }
    }

        ?>