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

$formname = 'Sale Register Export';
$file = 'exsalereg_fr.php';

$partyname = $_POST["partyname"];
$consignee = $_POST["consignee"];
$godown = $_POST["godown"];
$dtype = $_POST["dtype"];

$partycond = "";
$consigcond = "";
$godowncond = "";
$dtypecond = "";

if (!empty($partyname)) {
    $partycond = " AND `partyname` = '$partyname' ";
}
if (!empty($consignee)) {
    $consigcond = " AND `consignee` = '$consignee' ";
}
if (!empty($godown)) {
    $godowncond = " AND `godown` = '$godown' ";
}
if (!empty($dtype)) {
    $dtypecond = " AND `dtype`= '$dtype' ";
}
if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; saleitemgrp.php");
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; saleitemgrp.php");
}

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

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

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



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

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        table tr td {
            font-size: 14px;
        }
    </style>
    </head>

    <body>
        <div class="container">
            <?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-hover table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:4%;">Sr</th>
                                <th style="width:6%;">Voucher</th>
                                <th style="width:8%;">Date</th>
                                <th style="width:23%;">Party Name</th>
                                <th style="width:22%;">Consignee</th>
                                <th style="width:9%;">Godown</th>
                                <th style="width:9%" class="text-right">Subtotal</th>
                                <th style="width:7%" class="text-right">Disc. Amt.</th>
                                <th style="width:11%" class="text-right">Net Amt.</th>
                                <th style="width:14%" class="text-right">INR Amt.</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;

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

                                $customer = $pmain['partyname'];
                                $consignee = $pmain['consignee'];
                                $gcode = $pmain['godown'];

                                $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$customer' AND grpcode='010'  ") or die(mysqli_error($conn));
                                $sparty = mysqli_fetch_array($gparty);

                                $gconsig = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$consignee' AND grpcode='010'  ") or die(mysqli_error($conn));
                                $consig = mysqli_fetch_array($gconsig);

                                $ggodwn = mysqli_query($conn, "SELECT `code`,`name`,`gcode` FROM `godown` where `code` = '$gcode' ") or die(mysqli_error($conn));
                                $godwn = mysqli_fetch_array($ggodwn);

                                $voucher = $pmain['voucher'];
                                $vouchdt = $pmain['mydt'];
                                $party = $sparty['name'];
                                $consigne = $consig['name'];
                                $godown = $godwn['gcode'];
                                $grossamt = $pmain['grossamt'];
                                $discount = $pmain['discount'];
                                $billamt = $pmain['billamt'];
                                $inramt = $pmain['inramt'];

                                $ctr++;
                                echo "<tr>";
                                echo "<td>$ctr</td>";
                                echo "<td>$voucher</td>";
                                echo "<td>$vouchdt</td>";
                                echo "<td>$party</td>";
                                echo "<td>$consigne</td>";
                                echo "<td>$godown</td>";
                                echo "<td class='text-right'>$grossamt</td>";
                                echo "<td class='text-right'>$discount</td>";
                                echo "<td class='text-right'>$billamt</td>";
                                echo "<td class='text-right'>$inramt</td>";
                                echo "</tr>";

                                $totsubtot = $totsubtot + $grossamt;
                                $totdiscount = $totdiscount + $discount;
                                $totbillamt = $totbillamt + $billamt;
                                $totinramt = $totinramt + $inramt;

                                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-striped table-bordered'>";
                                    echo "<thead>";
                                    echo "<tr class='mySearchTr2' >";
                                    echo "<th style='width:2%;'>Sr</th>";
                                    echo "<th style='width:5%;'>Barcode</th>";
                                    echo "<th style='width:6%;' class=''>Product Code</th>";
                                    echo "<th style='width:9%;' class=''>Product Name</th>";
                                    echo "<th style='width:8%;' class=''>HSN Code</th>";
                                    echo "<th style='width:5%;' class=''>Gram</th>";
                                    echo "<th style='width:5%;' class=''>Unit</th>";
                                    echo "<th style='width:5%;' class='text-right'>Mrp</th>";
                                    echo "<th style='width:5%;' class=''>Batch</th>";
                                    echo "<th style='width:5%;' class='text-right'>Qty.</th>";
                                    echo "<th style='width:5%;' class='text-right'>Rate</th>";
                                    echo "<th style='width:5%;' class='text-right'>Amount</th>";
                                    echo "</tr>";
                                    echo "<thead>";
                                    echo "<tr>";

                                    $ctr2 = 0;
                                    $trtotqty = 0;
                                    $trtotamount = 0;

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

                                        $barcode = $tran['barcode'];
                                        $itemcode = $tran['itemcode'];
                                        $itemname = $tran['itemname'];
                                        $hsncode = $tran['hsncode'];
                                        $gram = $tran['gram'];
                                        $unit = $tran['unit'];
                                        $mrp = $tran['mrp'];
                                        $batch = $tran['batch'];
                                        $qty = $tran['qty'];
                                        $rate = $tran['rate'];
                                        $amount = $tran['amount'];

                                        $ctr2++;
                                        echo "<tr>";
                                        echo "<td>$ctr2</td>";
                                        echo "<td>$barcode</td>";
                                        echo "<td>$itemcode</td>";
                                        echo "<td>$itemname</td>";
                                        echo "<td>$hsncode</td>";
                                        echo "<td>$gram</td>";
                                        echo "<td>$unit</td>";
                                        echo "<td class='text-right'>$mrp</td>";
                                        echo "<td>$batch</td>";
                                        echo "<td class='numeric text-right'>$qty</td>";
                                        echo "<td class='numeric text-right'>$rate</td>";
                                        echo "<td class='numeric text-right'>$amount</td>";
                                        echo "</tr>";

                                        $trtotqty = $trtotqty + $qty;
                                        $trtotamount = $trtotamount + $amount;
                                    }

                                    echo "<tr class='mySearchTr2'>";
                                    echo "<td class='text-right' colspan='9'>Total</td>";
                                    echo "<td class='text-right'>$trtotqty</td>";
                                    echo "<td class='text-right'></td>";
                                    echo "<td class='text-right'>$trtotamount </td>";
                                    echo "</tr>";
                                    echo "</table>";
                                }
                            }
                            echo "<tr class='mySearchTr'>";
                            echo "<td class='text-right' colspan='6'><b>Total</b></td>";
                            echo "<td class='numeric text-right'><b>$totsubtot</b></td>";
                            echo "<td class='numeric text-right'><b>$totdiscount</b></td>";
                            echo "<td class='numeric text-right'><b>$totbillamt</b></td>";
                            echo "<td class='numeric text-right'><b>$totinramt</b></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}
if (isset($_POST["excel"])) {


    $contents .= "Sr.,Voucher,Date,Party Name,Consignee,Godown,Subtotal,Disc. Amt.,Net Amt.,INR Amt.\n";

    $ctr = 0;

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

        $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[partyname]' AND grpcode='010'  ") or die(mysqli_error($conn));
        $sparty = mysqli_fetch_array($gparty);

        $gconsig = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[consignee]' AND grpcode='010'  ") or die(mysqli_error($conn));
        $consig = mysqli_fetch_array($gconsig);

        $ggodwn = mysqli_query($conn, "SELECT `code`,`name`,`gcode` FROM `godown` where `code` = '$pmain[godown]' ") or die(mysqli_error($conn));
        $godwn = mysqli_fetch_array($ggodwn);

        $ctr++;

        $contents .= $ctr . ",";
        $contents .= $pmain['voucher'] . ",";
        $contents .= $pmain['mydt'] . ",";
        $contents .= $sparty['name'] . ",";
        $contents .= $consig['name'] . ",";
        $contents .= $godwn['gcode'] . ",";
        $contents .= $pmain['grossamt'] . ",";
        $contents .= $pmain['discount'] . ",";
        $contents .= $pmain['billamt'] . ",";
        $contents .= $pmain['inramt'] . "\n";


        $totsubtot = $totsubtot + $pmain['grossamt'];
        $totdiscount = $totdiscount + $pmain['discount'];
        $totbillamt = $totbillamt + $pmain['billamt'];
        $totinramt = $totinramt + $pmain['inramt'];

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



            $contents .= "Sr,Barcode,Product Code,Product Name,HSN Code,Gram,Unit,Mrp,Batch,Qty.,Rate,Amount,\n";

            $ctr2 = 0;
            $trtotqty = 0;
            $trtotamount = 0;

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


                $ctr2++;
                $contents .= $ctr2 . ",";
                $contents .= $tran['barcode'] . ",";
                $contents .= $tran['itemcode'] . ",";
                $contents .= $tran['itemname'] . ",";
                $contents .= $tran['hsncode'] . ",";
                $contents .= $tran['gram'] . ",";
                $contents .= $tran['unit'] . ",";
                $contents .= $tran['mrp'] . ",";
                $contents .= $tran['batch'] . ",";
                $contents .= $tran['qty'] . ",";
                $contents .= $tran['rate'] . ",";
                $contents .= $tran['amount'] . "\n";

                $trtotqty = $trtotqty + $tran['qty'];
                $trtotamount = $trtotamount + $tran['amount'];
            }


            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= Total . ",";
            $contents .= $trtotqty . ",";
            $contents .= " " . ",";
            $contents .= $trtotamount . "\n";
        }
    }
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Total" . ",";
    $contents .= $totsubtot . ",";
    $contents .= $totdiscount . ",";
    $contents .= $totbillamt . ",";
    $contents .= $totinramt . "\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;
    }
}
?>