<?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"];

$formname = 'Sale Register';
$file = 'salereg_fr.php';

$maindb = "salemain";
$trandb = "saletran";

$party = $_POST["party"];
$godown = $_POST["godown"];
$brand = $_POST["brand"];
$dtype = $_POST["dtype"];

$gdncond = "";
$partycond = "";
$brandcond = "";
$brandcond2 = "";

if (!empty($party)) {
    $partycond = " AND a.`party`='$party' ";
}
if (!empty($godown)) {
    $gdncond = " AND a.`godown`='$godown' ";
}
if (!empty($brand)) {
    $brandcond = " AND b.`brand`='$brand' ";
}
if (!empty($brand)) {
    $brandcond2 = " AND `brand`='$brand' ";
}


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));
}

if ($_POST["dtype"] != 2) {
    $getpmain = mysqli_query($conn, "SELECT a.*,b.*,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate' 
                                        AND a.`vouchdt` <= '$mtodate' and a.vouchno = b.vouchno $partycond $gdncond group by a.vouchno order by a.vouchno,b.srno") or die(mysqli_error($conn));
} else {
    $getpmain = mysqli_query($conn, "SELECT a.*,b.*,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate' 
                                        AND a.`vouchdt` <= '$mtodate' and a.vouchno = b.vouchno $partycond $brandcond $gdncond group by a.vouchno order by a.vouchno,b.srno") or die(mysqli_error($conn));
}




//$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
//                                        AND `vouchdt` <= '$mtodate' $partycond ")or die(mysqli_error($conn));
//$getno = mysqli_num_rows($getpmain);

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

    include("includes/linkshead.php");
    ?>
    </head>

    <body>
        <div class="container">
            <?php
            include("includes/head_rep.php");

            $getno = mysqli_num_rows($getpmain);

            if ($getno > 0) {
                ?>
                <div class="row">
                    <div class="col-lg-12 ">

                        <table style="background: white;width: 100%;border-radius: 5px"
                            class="table table-condensed table-hover table-bordered">
                            <thead>
                                <tr>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="4%;">Vouchno</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="5%;">Invoice</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="7%;">Date</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="15%;">Party</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="5%;" class="text-right">Qty</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="5%;" class="text-right">Totkg</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="7%;" class="text-right">Amount</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="7%;" class="text-right">Discount</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="7%;" class="text-right">Freight</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="7%;" class="text-right">Labour</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="7%;" class="text-right">Tempo</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="7%;" class="text-right">Packing</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="7%;" class="text-right">All</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="7%;" class="text-right">Net Amt.</th>
                                    <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px"
                                        width="35%;">Remarks</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $subtot = 0;
                                $totdisc = 0;
                                $totfr = 0;
                                $totqty1 = 0;
                                $totnet = 0;

                                while ($pmain = mysqli_fetch_array($getpmain)) {
                                    $getmtype = mysqli_query($conn, "SELECT name FROM `type` WHERE `code`='$pmain[type]'") or die(mysqli_error($conn));
                                    $mtype = mysqli_fetch_array($getmtype);

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

                                    $getmqty = mysqli_query($conn, "SELECT SUM(invqty)as totqty FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'") or die(mysqli_error($conn));
                                    $mqty = mysqli_fetch_array($getmqty);

                                    $ctr++;
                                    echo "<tr class='clickable-row2' data-href='salereg.php?mode=edit&vouchno=$pmain[vouchno]' data-target='_blank'>";
                                    echo "<td>$pmain[vouchno]</td>";
                                    echo "<td>$pmain[invno]</td>";
                                    echo "<td>$pmain[mydt]</td>";
                                    echo "<td>$mcust[name]</td>";
                                    echo "<td class='numeric'>$pmain[tqty]</td>";
                                    echo "<td class='numeric'>$pmain[tweight]</td>";
                                    echo "<td class='numeric'>$pmain[subtotal]</td>";
                                    echo "<td class='numeric'>$pmain[discount]</td>";
                                    echo "<td class='numeric'>$pmain[freight]</td>";
                                    echo "<td class='numeric'>$pmain[labour]</td>";
                                    echo "<td class='numeric'>$pmain[tempo]</td>";
                                    echo "<td class='numeric'>$pmain[packing]</td>";
                                    echo "<td class='numeric'>$pmain[allchrg]</td>";
                                    echo "<td class='numeric'>$pmain[netamt]</td>";
                                    echo "<td style='font-size:12px'>$pmain[remark1]  $pmain[remark2] $pmain[remark3] </td>";
                                    echo "</tr>";
                                    echo "<tr>";

                                    $subtot = $subtot + $pmain['subtotal'];
                                    $totdisc = $totdisc + $pmain['discount'];
                                    $totfr = $totfr + $pmain['freight'];
                                    $totlabour = $totlabour + $pmain['labour'];
                                    $tottempo = $tottempo + $pmain['tempo'];
                                    $totpacking = $totpacking + $pmain['packing'];
                                    $totallchrg = $totallchrg + $pmain['allchrg'];
                                    $tottqty = $tottqty + $pmain['tqty'];
                                    $tottweight = $tottweight + $pmain['tweight'];
                                    $totnet = $totnet + $pmain['netamt'];

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

                                        echo "<tr style='height:10px;'>";
                                        echo "<td colspan='25'>";
                                        echo "<table style='width:100%;'class='table table-condensed table-hover table-bordered'>";
                                        //                                        Echo "<thead>";
                                        echo "<tr class='' style=''>";
                                        echo "<th style='width:2%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;'>Sr</th>";
                                        echo "<th style='width:7%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;'>Brand/Logo</th>";
                                        echo "<th style='width:5%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;'>Code</th>";
                                        echo "<th style='width:10%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;'>Description</th>";
                                        echo "<th style='width:6%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;' class='text-right'>Qty.</th>";
                                        echo "<th style='width:6%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;' class='text-right'>Bale Qty.(Kgs)</th>";
                                        echo "<th style='width:6%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;' class='text-right'>Tot. Qty.(Kgs)</th>";
                                        echo "<th style='width:5%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;' class='text-right'>Rate</th>";
                                        echo "<th style='width:5%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;' class='text-right'>Amount</th>";
                                        echo "</tr>";
                                        //                                        Echo "<thead>";
                                        echo "<tr>";

                                        $ctr2 = 0;
                                        $totqty = 0;
                                        $totamt = 0;
                                        $totinv = 0;
                                        $totdtqty = 0;
                                        $totdtinv = 0;
                                        $totdtamt = 0;

                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' $brandcond2 ") or die(mysqli_error($conn));

                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            $getbrand = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$tran[brand]'") or die(mysqli_error($conn));
                                            $mbrand = mysqli_fetch_array($getbrand);

                                            $finditem = mysqli_query($conn, "SELECT * FROM `item` WHERE `barcode`='$tran[itemcode]'") or die(mysqli_error($conn));
                                            $fitem = mysqli_fetch_array($finditem);

                                            $ctr2++;
                                            echo "<tr>";
                                            echo "<td>$ctr2</td>";
                                            echo "<td>$mbrand[name]</td>";
                                            echo "<td>$fitem[code]</td>";
                                            echo "<td>$fitem[name]</td>";
                                            echo "<td class='numeric'>$tran[qty]</td>";
                                            echo "<td class='numeric'>$tran[baleqty]</td>";
                                            echo "<td class='numeric'>$tran[totqty]</td>";
                                            echo "<td class='numeric'>$tran[rate]</td>";
                                            echo "<td class='numeric'>$tran[amount]</td>";
                                            echo "</tr>";
                                            $totqty = $totqty + $tran['qty'];
                                            $totdtqty = $totdtqty + $tran['totqty'];
                                            $totdtinv = $totdtinv + $tran['baleqty'];
                                            $totdtamt = $totdtamt + $tran['amount'];
                                        }

                                        echo "<tr>";
                                        echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;'></td>";
                                        echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;'></td>";
                                        echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;'></td>";
                                        echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right' ><b>Total </b></td>";
                                        echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right'><b> $totqty </b></td>";
                                        echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right'><b> $totdtinv </b></td>";
                                        echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right'><b> $totdtqty </b></td>";
                                        echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right' ></td>";
                                        echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right'><b> $totdtamt </b> </td>";
                                        echo "</tr>";
                                        echo "</table>";
                                    }
                                }

                                echo "<tr>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td class='text-right'><b>Total</b></td>";
                                echo "<td class='numeric text-right'><b>$tottqty</b></td>";
                                echo "<td class='numeric text-right'><b>$tottweight</b></td>";
                                echo "<td class='numeric text-right'><b>$subtot</b></td>";
                                echo "<td class='numeric text-right'><b>$totdisc</b></td>";
                                echo "<td class='numeric text-right'><b>$totfr</b></td>";
                                echo "<td class='numeric text-right'><b>$totlabour</b></td>";
                                echo "<td class='numeric text-right'><b>$tottempo</b></td>";
                                echo "<td class='numeric text-right'><b>$totpacking</b></td>";
                                echo "<td class='numeric text-right'><b>$totallchrg</b></td>";
                                echo "<td class='numeric text-right'><b>$totnet</b></td>";
                                echo "<td></td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>

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

    </html>
    <?php
}

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

    $getno = mysqli_num_rows($getpmain);
    if ($getno > 0) {

        $contents .= "Vouchno, Invoice, Date, Party, Qty, Totkg, Amount, Discount, Freight, Labour, Tempo, Packing, All, Net Amt., Remarks \n";

        $ctr = 0;
        $subtot = 0;
        $totdisc = 0;
        $totfr = 0;
        $totqty1 = 0;
        $totnet = 0;

        while ($pmain = mysqli_fetch_array($getpmain)) {
            $getmtype = mysqli_query($conn, "SELECT name FROM `type` WHERE `code`='$pmain[type]'") or die(mysqli_error($conn));
            $mtype = mysqli_fetch_array($getmtype);

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

            $getmqty = mysqli_query($conn, "SELECT SUM(invqty)as totqty FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'") or die(mysqli_error($conn));
            $mqty = mysqli_fetch_array($getmqty);

            $ctr++;
            $contents .= $pmain["vouchno"] . ",";
            $contents .= $pmain["invno"] . ",";
            $contents .= $pmain["mydt"] . ",";
            $contents .= $pmain["name"] . ",";
            $contents .= $pmain["tqty"] . ",";
            $contents .= $pmain["tweight"] . ",";
            $contents .= $pmain["subtotal"] . ",";
            $contents .= $pmain["discount"] . ",";
            $contents .= $pmain["freight"] . ",";
            $contents .= $pmain["labour"] . ",";
            $contents .= $pmain["tempo"] . ",";
            $contents .= $pmain["packing"] . ",";
            $contents .= $pmain["allchrg"] . ",";
            $contents .= $pmain["netamt"] . ",";
            $contents .= $pmain["remark1"] . $pmain["remark2"] . $pmain["remark3"] . "\n";



            $subtot = $subtot + $pmain['subtotal'];
            $totdisc = $totdisc + $pmain['discount'];
            $totfr = $totfr + $pmain['freight'];
            $totlabour = $totlabour + $pmain['labour'];
            $tottempo = $tottempo + $pmain['tempo'];
            $totpacking = $totpacking + $pmain['packing'];
            $totallchrg = $totallchrg + $pmain['allchrg'];
            $tottqty = $tottqty + $pmain['tqty'];
            $tottweight = $tottweight + $pmain['tweight'];
            $totnet = $totnet + $pmain['netamt'];

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



                $contents .= "Sr, Brand, Code, Description, Qty., Bale Qty.(Kgs), Tot. Qty.(Kgs), Rate, Amount \n";


                $ctr2 = 0;
                $totqty = 0;
                $totamt = 0;
                $totinv = 0;
                $totdtqty = 0;
                $totdtinv = 0;
                $totdtamt = 0;

                $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' $brandcond2 ") or die(mysqli_error($conn));

                while ($tran = mysqli_fetch_array($gettran)) {

                    $getbrand = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$tran[brand]'") or die(mysqli_error($conn));
                    $mbrand = mysqli_fetch_array($getbrand);

                    $finditem = mysqli_query($conn, "SELECT * FROM `item` WHERE `barcode`='$tran[itemcode]'") or die(mysqli_error($conn));
                    $fitem = mysqli_fetch_array($finditem);

                    $ctr2++;

                    $contents .= $ctr2 . ",";
                    $contents .= $mbrand["name"] . ",";
                    $contents .= $fitem["code"] . ",";
                    $contents .= $fitem["name"] . ",";
                    $contents .= $tran["qty"] . ",";
                    $contents .= $tran["baleqty"] . ",";
                    $contents .= $tran["totqty"] . ",";
                    $contents .= $tran["rate"] . ",";
                    $contents .= $tran["amount"] . "\n";


                    $totqty = $totqty + $tran['qty'];
                    $totdtqty = $totdtqty + $tran['totqty'];
                    $totdtinv = $totdtinv + $tran['baleqty'];
                    $totdtamt = $totdtamt + $tran['amount'];
                }

                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= "$totqty" . ",";
                $contents .= "$totdtinv" . ",";
                $contents .= "$totdtqty" . ",";
                $contents .= "" . ",";
                $contents .= "$totdtamt" . "\n\n";


                // echo "<tr>";
                // echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;'></td>";
                // echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;'></td>";
                // echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;'></td>";
                // echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right' ><b>Total </b></td>";
                // echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right'><b> $totqty </b></td>";
                // echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right'><b> $totdtinv </b></td>";
                // echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right'><b> $totdtqty </b></td>";
                // echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right' ></td>";
                // echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right'><b> $totdtamt </b> </td>";
                // echo "</tr>";
                // echo "</table>";
            }
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$tottqty" . ",";
        $contents .= "$tottweight" . ",";
        $contents .= "$subtot" . ",";
        $contents .= "$totdisc" . ",";
        $contents .= "$totfr" . ",";
        $contents .= "$totlabour" . ",";
        $contents .= "$tottempo" . ",";
        $contents .= "$totpacking" . ",";
        $contents .= "$totallchrg" . ",";
        $contents .= "$totnet" . "\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;
    }
}
?>