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

$maindb = "consultmain";
$formname = "GST Report";
$file = "gstrevenue.php";
$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];
$bseries = $_SESSION["user_series"];

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$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 p.medtotal, p.meddiscamt, p.consultfees, p.vouchno AS pax_voucher,DATE_FORMAT(p.vouchdt, '%d-%m-%Y') AS mydt,
//                                    GROUP_CONCAT(t.name SEPARATOR ', ') AS treatment_names, a.name AS paxname, 
//                                    (SELECT SUM(amount)  FROM actran WHERE slvouch = p.vouchno AND accode = p.accode) AS tamt,
//                                    (SELECT paidfor FROM actran WHERE slvouch = p.vouchno AND accode = p.accode LIMIT 1) AS paidfor FROM $maindb p 
//                                    LEFT JOIN accmast a ON p.accode = a.accode 
//                                    LEFT JOIN treatbill b ON p.vouchno = b.vouchno 
//                                    LEFT JOIN treatment t ON b.treatment = t.code WHERE p.vouchdt >= '$mfrmdate' AND p.vouchdt <= '$mtodate' GROUP BY p.vouchno ORDER BY p.vouchno") or die(mysqli_error($conn));
//    $getpmain = mysqli_query($conn, "Select * from accmast where grpcode = '010' and accode in (Select accode from actran where date >= '$mfrmdate' AND date <= '$mtodate') order by name") or die(mysqli_error($conn));
$getpmain = mysqli_query($conn, "Select a.purpvisit,a.vouchdt, sum(a.consultfees) as tconsultfees, sum(a.medtotal) as medtotal, sum(a.meddiscamt) as meddiscamt,
                                    sum(a.treattotal) as treattotal, sum(a.trdiscamt) as trdiscamt,b.accode, b.name, 
                                    date_format(a.vouchdt,'%d-%m-%Y') as mydt  from consultmain a, accmast b where a.accode = b.accode 
                                    and a.vouchdt >= '$mfrmdate' AND a.vouchdt <= '$mtodate'
                                    group by a.vouchdt,a.accode order by a.vouchdt ASC") or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
//    print table($getpmain);
    ?>

    </head>

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

                <div class="col-lg-12" style="width:100%;">
                    <div class="box">
                        <div id="sortableTable" class="body collapse in">
                            <table style="background: white;width: 100%" class="table table-bordered sktb">
                                <thead class="mySearchTr">
                                    <tr>
                                        <th width="5%;">Date</th>
                                        <th width="13%;">Patient Name</th>
                                        <th width="13%;">Treatment Name</th>
                                        <th width="4%;" class="text-right">Consultation(₹)</th>
                                        <th width="5%;" class="text-right">Product(₹)</th>
                                        <th width="10%;" class="text-right">Treatment Amt(₹)</th>
                                        <th width="8%;" class="text-right">Paid Amount</th>
                                        <th width="12%;" class="text-right">Homeopathy Medicines(₹)</th>
                                        <th width="8%;">Payment Mode</th>
                                        <th width="8%;">Bank/Cash</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $paidamt = 0;
                                    $tcons = 0;
                                    $tmed = 0;
                                    $ttreat = 0;
                                    $tpaid = 0;
                                    while ($pmain = mysqli_fetch_array($getpmain)) {
                                        $maccode = $pmain['accode'];

//                                        $getcons = mysqli_query($conn, "Select purpvisit,sum(consultfees) as tconsultfees, sum(medtotal) as medtotal, sum(meddiscamt) as meddiscamt,
//                                                                        sum(treattotal) as treattotal, sum(trdiscamt) as trdiscamt from consultmain where vouchdt = '$pmain[date]'
//                                                                        and accode='$maccode'") or die(mysqli_error($conn));
//                                        $fetcons = mysqli_fetch_array($getcons);
                                        $vouchdt = $pmain['vouchdt'];
                                        $consfees = $pmain['tconsultfees'];
                                        $treatamt = $pmain['treattotal'] - $pmain['trdiscamt'];

                                        $getsale = mysqli_query($conn, "Select sum(netamt) as tnetamt from salemain where party='$maccode' AND vouchdt = '$vouchdt'") or die(mysqli_error($conn));
                                        $fetsale = mysqli_fetch_array($getsale);
                                        $saleamt = $fetsale['tnetamt'];
                                        $medamount = $pmain['medtotal'] - $pmain['meddiscamt'] + $saleamt;
                                        $totfees = $consfees + $medamount + $treatamt;

                                        if ($totfees > 0) {


                                            $getpaid = mysqli_query($conn, "Select sum(amount) as tpaidamt,source, bankinit from actran where accode='$maccode' AND date = '$vouchdt'  and amount > 0") or die(mysqli_error($conn));
                                            $fetpaid = mysqli_fetch_array($getpaid);
                                            $paidamt = $fetpaid['tpaidamt'];
                                            $ptype = $fetpaid['source'];
                                            if ($ptype == "BR") {
                                                $paytype = "Bank";
                                            } else {
                                                $paytype = "Cash";
                                            }
                                            $pmode = $fetpaid['bankinit'];
                                            if ($pmode == "GPAY") {
                                                $paymode = "GPAY";
                                            } else {
                                                $paymode = "Cash";
                                            }
                                            $tcons = $tcons + $consfees;
                                            $tmed = $tmed + $medamount;
                                            $ttreat = $ttreat + $treatamt;
                                            $tpaid = $tpaid + $paidamt;

                                            $ctr++;
                                            echo "<tr>";
                                            echo "<td class='text-uppercase'>$pmain[mydt]</td>";
                                            echo "<td class='text-uppercase'>$pmain[name]</td>";
                                            echo "<td class='text-uppercase'>$pmain[purpvisit]</td>";
                                            echo "<td class='text-right text-uppercase'>$consfees</td>";
                                            echo "<td class='text-right'>$medamount</td>";
                                            echo "<td class='text-right'>$treatamt</td>";
                                            echo "<td class='text-right'>$paidamt</td>";
                                            echo "<td class='text-right'></td>";
                                            echo "<td class='text-center'>$paytype</td>";
                                            echo "<td class='text-center'>$paymode</td>";
//                                        echo "<td class='text-uppercase'>$pmain[cmpltdt]</td>";
//                                        echo "<td class='text-uppercase'>$pmain[notes1] $pmain[notes2] $pmain[notes3] $pmain[notes4]</td>";
                                            echo "</tr>";
                                        }
                                    }
                                    echo "<tr>";
                                    echo "<td class='text-uppercase'></td>";
                                    echo "<td class='text-uppercase'></td>";
                                    echo "<td class='text-right'><b>Total</b></td>";
                                    echo "<td class='text-right'><b>$tcons</b></td>";
                                    echo "<td class='text-right'><b>$tmed</b></td>";
                                    echo "<td class='text-right'><b>$ttreat</b></td>";
                                    echo "<td class='text-right'><b>$tpaid</b></td>";
                                    echo "<td class='text-right'></td>";
                                    echo "<td class='text-center'></td>";
                                    echo "<td class='text-center'></td>";
//                                        echo "<td class='text-uppercase'>$pmain[cmpltdt]</td>";
//                                        echo "<td class='text-uppercase'>$pmain[notes1] $pmain[notes2] $pmain[notes3] $pmain[notes4]</td>";
                                    echo "</tr>";
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!-- /.col-lg-6 -->
            </div>
            <?php
            include("includes/linksfoot.php");
            ?>
        </div>

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

    $contents .= "Date, Patient Name, Treatment Name, Consultation, Product,  Treatment Amt, Paid Amount, Homeopathy Medicines, Payment Mode, Bank/Cash \n";

    $paidamt = 0;
    $tcons = 0;
    $tmed = 0;
    $ttreat = 0;
    $tpaid = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $maccode = $pmain['accode'];

//                                        $getcons = mysqli_query($conn, "Select purpvisit,sum(consultfees) as tconsultfees, sum(medtotal) as medtotal, sum(meddiscamt) as meddiscamt,
//                                                                        sum(treattotal) as treattotal, sum(trdiscamt) as trdiscamt from consultmain where vouchdt = '$pmain[date]'
//                                                                        and accode='$maccode'") or die(mysqli_error($conn));
//                                        $fetcons = mysqli_fetch_array($getcons);
        $vouchdt = $pmain['vouchdt'];
        $consfees = $pmain['tconsultfees'];
        $treatamt = $pmain['treattotal'] - $pmain['trdiscamt'];

        $getsale = mysqli_query($conn, "Select sum(netamt) as tnetamt from salemain where party='$maccode' AND vouchdt = '$vouchdt'") or die(mysqli_error($conn));
        $fetsale = mysqli_fetch_array($getsale);
        $saleamt = $fetsale['tnetamt'];
        $medamount = $pmain['medtotal'] - $pmain['meddiscamt'] + $saleamt;
        $totfees = $consfees + $medamount + $treatamt;

        if ($totfees > 0) {


            $getpaid = mysqli_query($conn, "Select sum(amount) as tpaidamt,source, bankinit from actran where accode='$maccode' AND date = '$vouchdt'  and amount > 0") or die(mysqli_error($conn));
            $fetpaid = mysqli_fetch_array($getpaid);
            $paidamt = $fetpaid['tpaidamt'];
            $ptype = $fetpaid['source'];
            if ($ptype == "BR") {
                $paytype = "Bank";
            } else {
                $paytype = "Cash";
            }
            $pmode = $fetpaid['bankinit'];
            if ($pmode == "GPAY") {
                $paymode = "GPAY";
            } else {
                $paymode = "Cash";
            }
            $tcons = $tcons + $consfees;
            $tmed = $tmed + $medamount;
            $ttreat = $ttreat + $treatamt;
            $tpaid = $tpaid + $paidamt;

            $contents .= "$pmain[mydt]" . ",";
            $contents .= "$pmain[name]" . ",";
            $contents .= "$pmain[purpvisit]" . ",";
            $contents .= "$consfees" . ",";
            $contents .= "$medamount" . ",";
            $contents .= "$treatamt" . ",";
            $contents .= "$paidamt" . ",";
            $contents .= "" . ",";
            $contents .= "$paytype" . ",";
            $contents .= "$paymode" . "\n";
        }
    }

    $contents .= "\n" . "";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "$tcons" . ",";
    $contents .= "$tmed" . ",";
    $contents .= "$ttreat" . ",";
    $contents .= "$tpaid" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . "\n";

    $contents = strip_tags($contents);

    header("Content-Disposition: attachment; filename=$formname" . date('d-m-Y') . ".csv");
    print $contents;
}
?>