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

$maindb = "salemain";
$trandb = "saletran";
$formname = "Sale Register";
$file = "salereg_fr.php";

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

$prodcode = $_POST["prodcode"];
$party = $_POST["party"];
$product = $_POST["product"];
$prodtype = $_POST["prodtype"];
$dtype = $_POST["dtype"];
$scope = $_POST["scope"];
$printnar = $_POST["prna"];
$dtypecond = "";
$mscopecond = "";
$itemcond = "";
$prodtypecond = "";

$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);


if (!empty($prodcode)) {
    $pcodecond = " AND `itemcode` = '$prodcode' ";
}
if (!empty($salesman)) {
    $salemcond = " AND `salesman`='$salesman' ";
}
//if (!empty($product)) {
//    $prodcond = " AND `itemname`='$product' ";
//    $prodcond2 = " AND a.`itemname`='$product' ";
//}
if (!empty($prodtype)) {
    $prodtypecond = " AND a.`prodtype`='$prodtype' ";
}
if (!empty($dtype)) {
    $dtypecond = " AND `dtype`='$dtype' ";
}
if (!empty($scope)) {
    $mscopecond = " AND `type`='$scope' ";
}
if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; purreg.php");
}
if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; purreg.php");
}

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

$getpmain = mysqli_query($conn, "SELECT itemcode, unit, vouchdt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt, vouchno, 
                         sum(qty) as qty, rate, sum(amount) as amount from $trandb where itemcode != ''  $prodcond and vouchno in 
                         (SELECT vouchno from $maindb where `vouchdt` >= '$mfrmdate'  AND `vouchdt` <= '$mtodate' ) group by itemcode
                         order by itemcode ")or die(mysqli_error($conn));


$getno = mysqli_num_rows($getpmain);

if ($getno == 0) {
    browse("No Record Found");
    echo "<script>window.close()</script>";
}

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


    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        .right{
            text-align: right;
        }
    </style>
    </head>
    <body>
        <div class="container" style="width:55%">
            <?php include("includes/head_rep.php"); ?>

            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%">Sr</th>
                                <th style="width:15%">Product Code</th>
                                <th style="width:50%">Product Name</th>
                                <th style="width:10%">Unit</th>
                                <th style="width:13%" class="text-right">Qty</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $qty = 0;

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

                                
                                $getitem = mysqli_query($conn, "SELECT name from product where prodcode = '$pmain[itemcode]' ")or die(mysqli_error($conn));
                                $fitem = mysqli_fetch_array($getitem);
                                $itemname = $fitem['name'];
                                
                                $ctr++;
                                Echo "<tr>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[itemcode]</td>";
                                Echo "<td>$itemname</td>";
                                Echo "<td>$pmain[unit]</td>";
                                Echo "<td class='text-right'>$pmain[qty]</td>";
                                Echo "</tr>";


                                $qty = $qty + $pmain['qty'];


                                if ($_POST["dtype"] == 2) {

                                    Echo "<tr style='height:10px;'>";
                                    Echo "<td colspan='25'>";
                                    Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    Echo "<th style='background-color: #cccccc;width:5%;'>Sr</th>";
                                    Echo "<th style='background-color: #cccccc;width:10%;' class='numeric'>Date</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;'>Voucher</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;'>Party</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class= 'text-right'>Qty</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class= 'text-right'>Rate</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class= 'text-right'>Amount</th>";

                                    Echo "<tr>";


                                    $ctr2 = 0;
                                    $tqty = 0;
                                    $trate = 0;
                                    $tamount = 0;

                                    $gettran = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, b.party,
                                                            b.voucher from $trandb a, $maindb b where a.vouchno = b.vouchno 
                                                            $prodcond2 ")or die(mysqli_error($conn));
                                    while ($tran = mysqli_fetch_array($gettran)) {

                                        $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$tran[party]'")or die(mysqli_error($conn));
                                        $supp = mysqli_fetch_array($getsupp);

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td class='numeric'>$tran[mydt]</td>";
                                        Echo "<td>$tran[voucher]</td>";
                                        Echo "<td>$supp[name]</td>";
                                        Echo "<td class='right'>$tran[qty]</td>";
                                        Echo "<td class='right'>$tran[rate]</td>";
                                        Echo "<td class='right'>$tran[amount]</td>";
                                        Echo "</tr>";

                                        $tqty = $tqty + $tran['qty'];
                                        $trate = $trate + $tran['rate'];
                                        $tamount = $tamount + $tran['amount'];
                                    }

                                    Echo "<tr>";
                                    Echo "<td style='width:10%;'><i></i></td>";
                                    Echo "<td style='width:15%;'><i></i></td>";
                                    Echo "<td style='width:15%;'><i></i></td>";
                                    Echo "<td class='right' style='width:20%; color:red;'><i><b>Total</b></i></td>";
                                    Echo "<td class='right' style='width:10%;'><i><b>$tqty</b></i></td>";
                                    Echo "<td class='right' style='width:10%;'><i><b>$trate</b></i></td>";
                                    Echo "<td class='right' style='width:20%;'><i><b>$tamount</b></i></td>";
                                    Echo "</tr>";
                                    Echo "</table>";
                                    Echo "</td>";
                                }
                            }


                            echo "<tr>";
                            
                            echo "<td style='font-size:15px;' colspan='4' class='text-right'><b>Grand Total</b></td>";
                            echo "<td style=font-size:15px; class='right'><b>$qty</b></td>";
                            echo "</tr>";

                            echo "<tr class='mySearchTr'>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    

    $contents .= "Sr, Product Code, Product Name, Unit, Qty \n";

    $ctr = 0;
    $qty = 0;

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

        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $pmain[itemcode] . ",";
        $contents .= $pmain[itemname] . ",";
        $contents .= $pmain[unit] . ",";
        $contents .= $pmain[qty] . "\n";

        $qty = $qty + $pmain[qty];


        if ($_POST["dtype"] == 2) {

            $contents .= "Sr, Date, Voucher, Party, Qty, Rate, Amount \n";

            $ctr2 = 0;
            $tqty = 0;
            $trate = 0;
            $tamount = 0;

            $gettran = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, b.party,
                                                            b.voucher from $trandb a, $maindb b where a.vouchno = b.vouchno 
                                                            $prodcond2 ")or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$tran[party]'")or die(mysqli_error($conn));
                $supp = mysqli_fetch_array($getsupp);



                $ctr2++;

                $contents .= $ctr2 . ",";
                $contents .= $tran[mydt] . ",";
                $contents .= $tran[voucher] . ",";
                $contents .= $supp[name] . ",";
                $contents .= $tran[qty] . ",";
                $contents .= $tran[rate] . ",";
                $contents .= $tran[amount] . "\n";

                $tqty = $tqty + $tran[qty];
                $trate = $trate + $tran[rate];
                $tamount = $tamount + $tran[amount];
            }

            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "Total" . ",";
            $contents .= $tqty . ",";
            $contents .= $trate . ",";
            $contents .= $tamount . "\n";
        }
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";
    $contents .= $qty . "\n";


    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
    }
    print $contents;
}
?>