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

$maindb = 'invoice';
$formname = "GST Invoice Register";
$file = "gstinvreg_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$compcd = $_SESSION["comp_" . $dbname];

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn,$zdb);
$zroot = $_SESSION["zdir"];


$party = $_POST[party];
$month = $_POST[month];
$fromdate = $_POST[fromdate];
$todate = $_POST[todate];
$dtype = $_POST[dtype];
mysqli_select_db($conn,$zdb);

//if (empty($_POST["fromdate"])) {
//    browse("From Date Cannot Be Empty");
//    header("refresh:1; gstinvreg.php");
//}

$partycond = '';
$monthcond = '';
$dtypecond = '';


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

if (!empty($month)) {
    $monthcond = " AND `monthno`='$month'";
}

$mfdt = date("d-m-Y", strtotime($fromdate));
$mtdt = date("d-m-Y", strtotime($todate));


$getpmain = mysqli_query($conn,"SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as vouchdt FROM `$maindb` WHERE `vouchno`!='' $partycond $monthcond") or die(mysqli_error($conn));

$getno = mysqli_num_rows($getpmain);

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    <style>
        @media print
        {    
            .no-print, .no-print *
            {
                display: none !important;
            }
        }
        .page {
            width: 297mm;
            min-height: 210mm;
            padding: 4mm;
            margin: 10mm auto;
            border: 1px #D3D3D3 solid;
            border-radius: 5px;
            /*background: white;*/

        }
        @page {
            size: A4;
            margin: 0;
        }
        @media print {
            html, body {
                width: 297mm;
                height: 210mm;        
            }
            .page {
                margin: 0;
                border: initial;
                border-radius: initial;
                width: initial;
                min-height: initial;
                box-shadow: initial;
                background: initial;
                page-break-after: always;
            }
        }

    </style>
    </head>
    <body>
        <div class="page" >
            <?php // include("includes/head_rep.php");
            ?>
            <label style="font-size:20px;">NEW STAR PIPE WORKS</label>
            <table  style="" class="table table-bordered  table-hover table-striped ">
                <thead class="mySearchTr">
                    <tr style=" background-color: #78909C;">
                        <th style="width:3%;">Inv.</th>
                        <th style="width:9%;">Date</th>
                        <th style="width:23%;">Party</th>
                        <th style="width:10%;">Month</th>
                        <th style="width:8%;"class='text-right'>Taxable</th>
                        <th style="width:8%;" class='text-right'>GST 3</th>
                        <th style="width:8%;" class='text-right'>GST 5</th>
                        <th style="width:8%;" class='text-right'>GST 12</th>
                        <th style="width:8%;" class='text-right'>GST 18</th>
                        <th style="width:8%;" class='text-right'>GST 28</th>
                        <th style="width:8%;" class='text-right'>Bill Amt.</th>
                    </tr>
                </thead>
                <?php
                $ttaxable = 0;
                $tgst3 = 0;
                $tgst5 = 0;
                $tgst12 = 0;
                $tgst18 = 0;
                $tgst28 = 0;
                $tbillamt = 0;


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

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

                    if ($pmain[monthno] == '01') {
                        $pmonth = 'January';
                    } elseif ($pmain[monthno] == '02') {
                        $pmonth = 'February';
                    } elseif ($pmain[monthno] == '03') {
                        $pmonth = 'March';
                    } elseif ($pmain[monthno] == '04') {
                        $pmonth = 'April';
                    } elseif ($pmain[monthno] == '05') {
                        $pmonth = 'May';
                    } elseif ($pmain[monthno] == '06') {
                        $pmonth = 'June';
                    } elseif ($pmain[monthno] == '07') {
                        $pmonth = 'July';
                    } elseif ($pmain[monthno] == '08') {
                        $pmonth = 'August';
                    } elseif ($pmain[monthno] == '09') {
                        $pmonth = 'September';
                    } elseif ($pmain[monthno] == '10') {
                        $pmonth = 'October';
                    } elseif ($pmain[monthno] == '11') {
                        $pmonth = 'November';
                    } elseif ($pmain[monthno] == '12') {
                        $pmonth = 'December';
                    }
                    echo "<tbody>";
                    echo "<tr> ";
                    echo "<td>$pmain[vouchno]</td>";
                    echo "<td>$pmain[vouchdt]</td>";
                    echo "<td>$party[name]</td>";
                    echo "<td>$pmonth</td>";
                    echo "<td class='text-right'>$pmain[taxable]</td>";
                    echo "<td class='text-right'>$pmain[totgst1]</td>";
                    echo "<td class='text-right'>$pmain[totgst5]</td>";
                    echo "<td class='text-right'>$pmain[totgst12]</td>";
                    echo "<td class='text-right'>$pmain[totgst18]</td>";
                    echo "<td class='text-right'>$pmain[totgst28]</td>";
                    echo "<td class='text-right'>$pmain[billamt]</td>";
                    echo "</tr>";

                    $ttaxable = $ttaxable + $pmain[taxable];
                    $tgst3 = $tgst3 + $pmain[totgst1];
                    $tgst5 = $tgst5 + $pmain[totgst5];
                    $tgst12 = $tgst12 + $pmain[totgst12];
                    $tgst18 = $tgst18 + $pmain[totgst18];
                    $tgst28 = $tgst28 + $pmain[totgst28];
                    $tbillamt = $tbillamt + $pmain[billamt];
                }

                echo "<td colspan='4' class='text-right' style=' color: red;' colspan='2'><b>Grand Total</b></td>";
                echo "<td class='text-right' style='color: red;'><b>$ttaxable</b></td>";
                echo "<td class='text-right' style='color: red;'><b>$tgst3</b></td>";
                echo "<td class='text-right' style='color: red;'><b>$tgst5</b></td>";
                echo "<td class='text-right' style='color: red;'><b>$tgst12</b></td>";
                echo "<td class='text-right' style='color: red;'><b>$tgst18</b></td>";
                echo "<td class='text-right' style='color: red;'><b>$tgst28</b></td>";
                echo "<td class='text-right' style='color: red;'><b>$tbillamt</b></td>";
                echo "</tr>";
                ?> 
                </tbody>
            </table>
        </div>
        <?php
        include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

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

    $contents .= "NEW STAR PIPE WORKS" . "\n";
    $contents .= "Inv., Date, Party, Month, Taxable, GST 3, GST 5, GST 12, GST 18, GST 28, Bill Amt.\n";

    $ttaxable = 0;
    $tgst3 = 0;
    $tgst5 = 0;
    $tgst12 = 0;
    $tgst18 = 0;
    $tgst28 = 0;
    $tbillamt = 0;


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

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

        if ($pmain[monthno] == '01') {
            $pmonth = 'January';
        } elseif ($pmain[monthno] == '02') {
            $pmonth = 'February';
        } elseif ($pmain[monthno] == '03') {
            $pmonth = 'March';
        } elseif ($pmain[monthno] == '04') {
            $pmonth = 'April';
        } elseif ($pmain[monthno] == '05') {
            $pmonth = 'May';
        } elseif ($pmain[monthno] == '06') {
            $pmonth = 'June';
        } elseif ($pmain[monthno] == '07') {
            $pmonth = 'July';
        } elseif ($pmain[monthno] == '08') {
            $pmonth = 'August';
        } elseif ($pmain[monthno] == '09') {
            $pmonth = 'September';
        } elseif ($pmain[monthno] == '10') {
            $pmonth = 'October';
        } elseif ($pmain[monthno] == '11') {
            $pmonth = 'November';
        } elseif ($pmain[monthno] == '12') {
            $pmonth = 'December';
        }
        $contents .= $pmain[vouchno] . ",";
        $contents .= $pmain[vouchdt] . ",";
        $contents .= $party[name] . ",";
        $contents .= $pmonth . ",";
        $contents .= $pmain[taxable] . ",";
        $contents .= $pmain[totgst1] . ",";
        $contents .= $pmain[totgst5] . ",";
        $contents .= $pmain[totgst12] . ",";
        $contents .= $pmain[totgst18] . ",";
        $contents .= $pmain[totgst28] . ",";
        $contents .= $pmain[billamt] . "\n";

        $ttaxable = $ttaxable + $pmain[taxable];
        $tgst3 = $tgst3 + $pmain[totgst1];
        $tgst5 = $tgst5 + $pmain[totgst5];
        $tgst12 = $tgst12 + $pmain[totgst12];
        $tgst18 = $tgst18 + $pmain[totgst18];
        $tgst28 = $tgst28 + $pmain[totgst28];
        $tbillamt = $tbillamt + $pmain[billamt];
    }
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";
    $contents .= $ttaxable . ",";
    $contents .= $tgst3 . ",";
    $contents .= $tgst5 . ",";
    $contents .= $tgst12 . ",";
    $contents .= $tgst18 . ",";
    $contents .= $tgst28 . ",";
    $contents .= $tbillamt . "\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;
    }
}
?>