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

$formname = 'Purchase Summary Report';
$file = 'purchsummreg_fr.php';

$action = 'purrawreg_fr.php';

$party = $_POST["supplier"];
$mono = $_POST["mono"];

$partycond = '';
$monocond = '';

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

$mainqry = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt,  SUM(billamt) as amount 
                             FROM `$maindb` WHERE mono != '' $partycond $monocond group by mono ")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:650px">
            <?php include("includes/head_rep.php"); ?>
        <!--<div class="" style="padding-left:220px;padding-right:220px">-->
            <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 style="">
                                <th style="width:10%;">Sr</th>
                                <th style="width:10%;">Month</th>
                                <th style="width:40%;" class='text-right'>Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamount = 0;
                            while ($fmain = mysqli_fetch_array($mainqry)) {
//                                
                                switch ($fmain['mono']) {

                                     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><h5>$ctr</h5></td>";
                                Echo"<td><h5>$month</h5></td>";
                                $amount = $fmain['amount'];
                                Echo"<td class='text-right'><h5>$amount</h5></td>";
                                Echo"</tr>";

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

                            Echo "<tr>";
                            Echo "<td></td>";
                            Echo "<td class='text-right'><b><h5>Total</h5></b></td>";
                            Echo "<td class='text-right'><b><h5>$totamount</h5></b></td>";
                            Echo "</tr>";

                            Echo "<tr class='mySearchTr'>";
                            Echo "<td colspan='3' ></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>

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


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

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

        switch ($fmain['mono']) {

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