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

$fyear = substr($myyear, 0, 2);
$lyear = substr($myyear, -2);

$maindb = 'salemain';
$trandb = 'saletran';
$zroot = $_SESSION["zdbroot"];

$formname = 'Sale Summary Report';
$file = 'rpsalesumm_fr.php';

$action = 'tisalereg_fr.php';

$party = $_POST["party"];
$month = $_POST["mono"];
$tomono = $_POST["tomono"];

$partycond = '';
$monofrom = '';
$monoto = '';

if (!empty($party)) {
    $partycond = " AND `party` = '$party' ";
}

switch ($month) {

    case '1':
        $month = "January";
        $fdt = "20$lyear-01-01";
        break;

    case "2":
        $month = "February";
        $fdt = "20$lyear-02-01";
        break;

    case "3":
        $month = "March";
        $fdt = "20$lyear-03-01";
        break;

    case "4":
        $month = "April";
        $fdt = "20$fyear-04-01";
        break;

    case "5":
        $month = "May";
        $fdt = "20$fyear-05-01";
        break;

    case "6":
        $month = "June";
        $fdt = "20$fyear-06-01";
        break;

    case "7":
        $month = "July";
        $fdt = "20$fyear-07-01";
        break;

    case "8":
        $month = "August";
        $fdt = "20$fyear-08-01";
        break;

    case "9":
        $month = "September";
        $fdt = "20$fyear-09-01";
        break;

    case "10":
        $month = "October";
        $fdt = "20$fyear-10-01";
        break;

    case "11":
        $month = "November";
        $fdt = "20$fyear-11-01";
        break;

    case "12":
        $month = "December";
        $fdt = "20$fyear-12-01";
        break;
}

if (!empty($month)) {
    $monofrom = " AND vouchdt >= '$fdt'  ";
}

switch ($tomono) {

    case '1':
        $month = "January";
        $tdt = "20$lyear-01-31";
        break;

    case "2":
        $month = "February";
        $tdt = "20$lyear-02-29";
        break;

    case "3":
        $month = "March";
        $tdt = "20$lyear-03-31";
        break;

    case "4":
        $month = "April";
        $tdt = "20$fyear-04-30";
        break;

    case "5":
        $month = "May";
        $tdt = "20$fyear-05-31";
        break;

    case "6":
        $month = "June";
        $tdt = "20$fyear-06-30";
        break;

    case "7":
        $month = "July";
        $tdt = "20$fyear-07-31";
        break;

    case "8":
        $month = "August";
        $tdt = "20$fyear-08-31";
        break;

    case "9":
        $month = "September";
        $tdt = "20$fyear-09-30";
        break;

    case "10":
        $month = "October";
        $tdt = "20$fyear-10-31";
        break;

    case "11":
        $month = "November";
        $tdt = "20$fyear-11-30";
        break;

    case "12":
        $month = "December";
        $tdt = "20$fyear-12-31";
        break;
}


if (!empty($tomono)) {
    $monoto = " AND vouchdt <= '$tdt' ";
}

//browse($monofrom) ;
//browse($monoto) ;

$mainqry = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt,  month(vouchdt) as monno, SUM(billamt) as amount 
                             FROM `$maindb` WHERE vouchno != '' $partycond $monofrom $monoto group by month(vouchdt) order by vouchdt")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; 
        }

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

                    <table style="background: white;width:100%" class="table table-condensed table-hover table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%;">Sr</th>
                                <th style="width:10%;">Month</th>
                                <th style="width:10%;">Local Sale</th>
                                <th style="width:10%;">Deemed Sale</th>
                                <th style="width:10%;">Export Sale</th>
                                <th style="width:10%;">Other Sale</th>
                                <th style="width:12%;" class="text-right">Total</th>
                                <th style="width:11%;" class="text-right">Return</th>
                                <th style="width:12%;" class="text-right">Actual Sale</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamount = 0;
                            while ($fmain = mysqli_fetch_array($mainqry)) {

                                switch ($fmain['monno']) {

                                    case '1':
                                        $month = "January";
                                        $fdt = "01-01-20$lyear";
                                        $tdt = "31-01-20$lyear";
                                        break;

                                    case "2":
                                        $month = "February";
                                        $fdt = "01-02-20$lyear";
                                        $tdt = "29-02-20$fyear";
                                        break;

                                    case "3":
                                        $month = "March";
                                        $fdt = "01-03-20$lyear";
                                        $tdt = "31-03-20$lyear";
                                        break;

                                    case "4":
                                        $month = "April";
                                        $fdt = "01-04-20$fyear";
                                        $tdt = "30-04-20$fyear";
                                        break;

                                    case "5":
                                        $month = "May";
                                        $fdt = "01-05-20$fyear";
                                        $tdt = "31-05-20$fyear";
                                        break;

                                    case "6":
                                        $month = "June";
                                        $fdt = "01-06-20$fyear";
                                        $tdt = "30-06-20$fyear";
                                        break;

                                    case "7":
                                        $month = "July";
                                        $fdt = "01-07-20$fyear";
                                        $tdt = "31-07-20$fyear";
                                        break;

                                    case "8":
                                        $month = "August";
                                        $fdt = "01-08-20$fyear";
                                        $tdt = "31-08-20$fyear";
                                        break;

                                    case "9":
                                        $month = "September";
                                        $fdt = "01-09-20$fyear";
                                        $tdt = "30-09-20$fyear";
                                        break;

                                    case "10":
                                        $month = "October";
                                        $fdt = "01-10-20$fyear";
                                        $tdt = "31-10-20$fyear";
                                        break;

                                    case "11":
                                        $month = "November";
                                        $fdt = "01-11-20$fyear";
                                        $tdt = "30-11-20$fyear";
                                        break;

                                    case "12":
                                        $month = "December";
                                        $fdt = "01-12-20$fyear";
                                        $tdt = "31-12-20$fyear";
                                        break;
                                }

                                $ctr++;
                                Echo"<tr class='clickable-row2' data-href='$action?view=view&p=$party&fdt=$fdt&tdt=$tdt&rpt=1' [target]='_blank'>";
                                Echo"<td>$ctrz</td>";
                                Echo"<td>$month</td>";
                                Echo"<td class='text-right'>" . number_format($fmain['amount']) . "</td>";
                                Echo"</tr>";

                                $totamount = $totamount + $fmain['amount'];
                            }

                            Echo "<tr>";
                            Echo "<td></td>";
                            Echo "<td class='text-right' style='font-size:15px;'><b><h5>Total</h5></b></td>";
                            Echo "<td class='text-right' style='font-size:15px;'><b><h5>" . number_format($totamount,2) . "</h5></b></td>";
                            Echo "</tr>";

                            Echo "<tr class='mySearchTr'>";
                            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"])) {



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

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

    $contents .= "Sr. , Month, Sale\n";

    $ctr = 0;
    $totamount = 0;
    while ($fmain = mysqli_fetch_array($mainqry)) {

        switch ($fmain['monno']) {

            case '1':
                $month = "January";
                break;

            case '2':
                $month = "February";
                break;

            case '3':
                $month = "March";
                break;

            case '4':
                $month = "April";
                break;

            case '5':
                $month = "May";
                break;

            case '6':
                $month = "June";
                break;

            case '7':
                $month = "July";
                break;

            case '8':
                $month = "August";
                break;

            case '9':
                $month = "September";
                break;

            case '10':
                $month = "October";
                break;

            case '11':
                $month = "November";
                break;

            case '12':
                $month = "December";
                break;
        }

        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $month . ",";
        $contents .= $fmain['amount'] . "\n";

        $totamount = $totamount + $fmain['amount'];
    }
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= $totamount . "\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;
    }
}
?>