<?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 = 'salemain';
$trandb = 'saletran';
$zroot = $_SESSION["zdbroot"];

$file = 'leadprodsale_fr.php';

$prodgrp = $_POST["prodgrp"];
$product = $_POST["product"];
$prodlike = $_POST["prodlike"];
$ol = $_POST["ol"];
$dtype = $_POST["dtype"];
$repby = $_POST["repby"];

if ($repby == 2) {
    $report_type = "Amt.";
} else {
    $report_type = "Qty.";
}

$formname = "Leading Product In Sale Register (" . $report_type . ")";
$pgrpcond = '';
$prodcond = '';
$prodlikecond = '';
$prodcond2 = '';

if (!empty($prodgrp)) {
    $pgrpcond = " AND b.`prodgrp` = '$prodgrp' ";
}

//if (!empty($product)) {
//    $prodcond = " AND `itemcode` = '$product' ";
//    $prodcond2 = " AND `code` = '$product' ";
//}

if (!empty($prodlike)) {
    $prodlikecond = " AND b.`name` LIKE '%$prodlike%' ";
}
if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; prodwisesale.php");
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; prodwisesale.php");
}

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

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

mysqli_query($conn, "drop temporary table if exists saletmp") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table saletmp SELECT `barcode`, itemcode, SUM(qty) AS slqty, qty as rtqty, rate, SUM(amount) AS slamt FROM `saletran` 
                                        where vouchno in (select vouchno from salemain where `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate') group by itemcode ") or die(mysqli_error($conn));


mysqli_query($conn, "drop temporary table if exists credtmp") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table credtmp SELECT `barcode`, itemcode, SUM(qty) AS rtqty, SUM(subtot) AS rtamt  
                                                            FROM `credtran` where `vouchdt` >= '$mfrmdate' 
                                                            AND `vouchdt` <= '$mtodate' group by itemcode ") or die(mysqli_error($conn));

mysqli_query($conn, "update saletmp set rtqty = 0 ") or die(mysqli_error($conn));


$gret = mysqli_query($conn, "SELECT * FROM `credtmp` ") or die(mysqli_error($conn));
while ($fgret = mysqli_fetch_array($gret)) {
    $itemcode = $fgret['itemcode'];
    $rtqty = $fgret['rtqty'];

    $gchk = mysqli_query($conn, "SELECT * FROM `saletmp` where itemcode = '$itemcode' ") or die(mysqli_error($conn));
    if (mysqli_num_rows($gchk) > 0) {
        mysqli_query($conn, "update saletmp set rtqty = '$rtqty' where itemcode = '$itemcode' ") or die(mysqli_error($conn));
    } else {
        mysqli_query($conn, "insert into saletmp (`itemcode`, `rtqty`) values ('$itemcode','$rtqty')  ") or die(mysqli_error($conn));
    }
}

$mainqry = mysqli_query($conn, "SELECT a.*, (a.slqty - a.rtqty) as netqty, b.name as itemname from saletmp a, product b where a.itemcode = b.code 
                                       $pgrpcond $prodlikecond order by netqty desc, b.name ") or die(mysqli_error($conn));
$getno = mysqli_num_rows($mainqry);

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        th {
            background: #26A69A;
        }

        .dth {
            background: #BABFC2;
        }

        .colr {
            background: #26A69A;
            font-size: 17px;
        }

        table tr td {
            font-size: 14px;
        }

        total {
            font-size: 16px;

        }
    </style>
    </head>

    <body>


        <div class="container" style="width:90%">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">

                    <table style="background: white;width: 100%" class="table table-condensed table-striped table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th width='5%'>Sr</th>
                                <th width='40%'>Product Name</th>
                                <? if ($repby == 2) { ?>
                                    <th width='7%' class="text-right">Total Sale Amt.</th>
                                    <th width='7%' class="text-right">Total Return Amt.</th>
                                    <th width='7%' class="text-right">Net Amt.</th>
                                <? } else {
                                ?>
                                    <th width='7%' class="text-right">Total Sale Qty.</th>
                                    <th width='7%' class="text-right">Return</th>
                                    <th width='7%' class="text-right">Net Qty.</th>
                                <? } ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $grtotsalqty = $grtotretqty = $grtotqty = 0;
                            $grtotlocamt = $grtotdeeamt = $grtotexpamt = $grtotalamt = $grtotretamt = $grtotnetamt = 0;

                            while ($fmain = mysqli_fetch_array($mainqry)) {
                                $locqty = abs($fmain['slqty']);
                                $retqty = $fmain['rtqty'];
                                $itemname = $fmain['itemname'];

                                $salqty = $locqty;
                                $totqty = $salqty - $retqty;
                                $ctr++;

                                echo "<tr>";
                                echo "<td>$ctr</td>";
                                echo "<td>$itemname</td>";
                                if ($repby == 2) {
                                    echo "<td class='text-right'>$totalamt</td>";
                                    echo "<td class='text-right'>$retamt</td>";
                                    echo "<td class='text-right'>$netamt</td>";

                                    $grtotlocamt = $grtotlocamt + $locamt;
                                    $grtotalamt = $grtotalamt + $totalamt;
                                    $grtotretamt = $grtotretamt + $retamt;
                                    $grtotnetamt = $grtotnetamt + $netamt;
                                } else {
                                    echo "<td class='text-right'>$salqty</td>";
                                    if ($retqty > 0) {
                                        echo "<td class='text-right' style='color:red'>$retqty</td>";
                                    } else {
                                        echo "<td class='text-right'>$retqty</td>";
                                    }
                                    echo "<td class='text-right'>$totqty</td>";

                                    $grtotsalqty = $grtotsalqty + $salqty;
                                    $grtotretqty = $grtotretqty + $retqty;
                                    $grtotloc = $grtotloc + $locqty;
                                    $grtotonl = $grtotonl + $onlqty;
                                    $grtotqty = $grtotqty + $totqty;
                                }
                                echo "</tr>";
                            }
                            if ($repby == 2) {
                                echo "<tr style='color: blue; font-size: 14px;'>";
                                echo "<td ></td>";
                                echo "<td class='text-right '><b>Total</b></td>";
                                echo "<td class='numeric text-right'><b>$grtotlocamt</b></td>";
                                echo "<td class='numeric text-right'><b>$grtotalamt</b></td>";
                                echo "<td class='numeric text-right'><b>$grtotretamt</b></td>";
                                echo "<td class='numeric text-right'><b>$grtotnetamt</b></td>";
                                echo "</tr>";

                                $mtotlocamt = $grtotlocamt;
                                $mtotonlamt = $grtotonlamt;
                                $mtotalamt = $grtotalamt;
                                $mtotretamt = $grtotretamt;
                                $mtotnetamt = $grtotnetamt;

                                echo "<tr style='background-color: #98F9F0; color: red'>";
                                echo "<td class='text-right' colspan='2'><b>Grand Total</b></td>";
                                echo "<td class='numeric text-right'><b>$mtotalamt</b></td>";
                                echo "<td class='numeric text-right'><b>$mtotretamt</b></td>";
                                echo "<td class='numeric text-right'><b>$mtotnetamt</b></td>";
                                echo "</tr>";
                            } else {
                                echo "<tr style='background-color: #98F9F0 !important;'>";
                                echo "<td class=''></td>";
                                echo "<td class='text-right '><b>Total</b></td>";
                                echo "<td class='numeric text-right '><b>$grtotsalqty</b></td>";
                                echo "<td class='numeric text-right '><b>$grtotretqty</b></td>";
                                echo "<td class='numeric text-right '><b>$grtotqty</b></td>";
                                echo "</tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}

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

    $contents .= "Sr" . ",";
    $contents .= "Product Name" . ",";
    if ($repby == 2) {
        $contents .= "Total Sale Amt." . ",";
        $contents .= "Total Return Amt." . ",";
        $contents .= "Net Amt." . ",";
    } else {
        $contents .= "Total Sale Qty." . ",";
        $contents .= "Return" . ",";
        $contents .= "Net Qty." . "\n";
    }

    $ctr = 0;
    $grtotsalqty = $grtotretqty = $grtotqty = 0;
    $grtotlocamt = $grtotdeeamt = $grtotexpamt = $grtotalamt = $grtotretamt = $grtotnetamt = 0;

    while ($fmain = mysqli_fetch_array($mainqry)) {
        $locqty = abs($fmain['slqty']);
        $retqty = $fmain['rtqty'];
        $itemname = $fmain['itemname'];

        $salqty = $locqty;
        $totqty = $salqty - $retqty;
        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$itemname" . ",";
        if ($repby == 2) {
            $contents .= "$totalamt" . ",";
            $contents .= "$retamt" . ",";
            $contents .= "$netamt" . ",";

            $grtotlocamt = $grtotlocamt + $locamt;
            $grtotalamt = $grtotalamt + $totalamt;
            $grtotretamt = $grtotretamt + $retamt;
            $grtotnetamt = $grtotnetamt + $netamt;
        } else {
            $contents .= "$salqty" . ",";
            if ($retqty > 0) {
                $contents .= "$retqty" . ",";
            } else {
                $contents .= "$retqty" . ",";
            }
            $contents .= "$totqty" . "\n";

            $grtotsalqty = $grtotsalqty + $salqty;
            $grtotretqty = $grtotretqty + $retqty;
            $grtotloc = $grtotloc + $locqty;
            $grtotonl = $grtotonl + $onlqty;
            $grtotqty = $grtotqty + $totqty;
        }
        
    }
    if ($repby == 2) {

        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$grtotlocamt" . ",";
        $contents .= "$grtotalamt" . ",";
        $contents .= "$grtotretamt" . ",";
        $contents .= "$grtotnetamt" . "\n";

        $mtotlocamt = $grtotlocamt;
        $mtotonlamt = $grtotonlamt;
        $mtotalamt = $grtotalamt;
        $mtotretamt = $grtotretamt;
        $mtotnetamt = $grtotnetamt;

        $contents .= "Grand Total" . ",";
        $contents .= "$mtotalamt" . ",";
        $contents .= "$mtotretamt" . ",";
        $contents .= "$mtotnetamt" . "\n";
    } else {

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