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

$maindb = 'bankpay';
$actran = 'actran';
$formname = "Expense report";
$file = "expenserep_fr.php";

ini_set('display_errors', 1);
error_reporting(E_ERROR);

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


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

    $cust = $_POST["cust"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];
    $orderby = $_POST["orderby"];

    $gcond = '';
    $mcond = '';
    $cucond = '';
    $stcond = '';
    $fdatecond = '';
    $tdatecond = '';

    if (!empty($cust)) {
        $cucond = " AND `accode`='$cust' ";
    }


    $dcond = " and `vouchdt` >= '$fdate' and `vouchdt` <= '$tdate' ";

    if (empty($fdate)) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; expenserep.php");
    }

    if (empty($tdate)) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; expenserep.php");
    }




    if ($orderby == "1") {
        $getcash = mysqli_query($conn, "select party,count(*) as norec, sum(grossamt) as gamt,sum(totdisc) as tdisc,sum(totvat) as tvat,sum(billamt) as tamt from `salemain` where party != ''  and 
                                                         vouchno in (select vouchno from salemain where `vouchdt` >= '$fdate' and `vouchdt` <= '$tdate') $dcond group by party order by norec desc") or die(mysqli_error($conn));
    } else {
        $getcash = mysqli_query($conn, "select party,count(*) as norec, sum(grossamt) as gamt,sum(totdisc) as tdisc,sum(totvat) as tvat, sum(billamt) as tamt from `salemain` where party != ''  and 
                                                         vouchno in (select vouchno from salemain where `vouchdt` >= '$fdate' and `vouchdt` <= '$tdate') $dcond group by party order by tamt desc") or die(mysqli_error($conn));
    }
    //    print table($getcash);
    $mfdt = date("d-m-Y", strtotime($fdate));
    $mtdt = date("d-m-Y", strtotime($tdate));
    include("includes/linkshead.php");
?>
    <style>
        @media print {

            .no-print,
            .no-print * {
                display: none !important;
            }
        }
    </style>
    </head>

    <body>
        <div class="container">
            <?php
            include("includes/head_rep.php");
            ?>
            <div class="row">
                <div class="col-lg-4 col-md-2 col-sm-2 col-xs-2 pad">
                    <label> <?php echo $cashsale; ?></label>
                </div>
            </div>
            <div class="row">

                <div class="col-lg-12 pad">
                    <table style="width:100%;background: white;" class="table table-condensed table-striped table-bordered table-hover">
                        <thead>
                            <tr style="background-color:#78909C">
                                <th style="width:10%;">Sr.No</th>
                                <th style="width:10%;">Visit</th>
                                <th style="width:40%;">Name </th>
                                <th style="width:15%;" class='text-right'>Amount</th>
                                <th style="width:15%;" class='text-right'>Discount</th>
                                <th style="width:15%;" class='text-right'>VAT</th>
                                <th style="width:15%;" class='text-right'>Bill Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;
                            $gamt = 0;
                            $tdisc = 0;
                            $tvat = 0;
                            while ($cmain = mysqli_fetch_array($getcash)) {

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


                                $tvat = $cmain['tvat'];
                                $gamt = $cmain['gamt'];
                                $tdisc = $cmain['tdisc'];
                                $mamt = $cmain['tamt'];
                                $ctr++;
                                echo "<tr>";
                                echo "<td>$ctr</td>";
                                echo "<td>$cmain[norec]</td>";
                                echo "<td>$showparty[name]</td>";
                                echo "<td class='text-right'>$gamt</td>";
                                echo "<td class='text-right'>$tdisc</td>";
                                echo "<td class='text-right'>$tvat</td>";
                                echo "<td class='text-right'>$mamt</td>";
                                echo "</tr>";
                                $totamt = $totamt + $mamt;
                                $totdamt = $totdamt + $tdisc;
                                $totvat = $totvat + $tvat;
                                $totgamt = $totgamt + $gamt;
                            }

                            echo "<tr style='background-color:#97f9f0'>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td class='text-right'><b>Total</b></td>";
                            echo "<td class='text-right'><b>$totgamt</b></td>";
                            echo "<td class='text-right'><b>$totdamt</b></td>";
                            echo "<td class='text-right'><b>$totvat</b></td>";
                            echo "<td class='text-right'><b>$totamt</b></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php
        include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}

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

    $cust = $_POST["cust"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];
    $orderby = $_POST["orderby"];

    $gcond = '';
    $mcond = '';
    $cucond = '';
    $stcond = '';
    $fdatecond = '';
    $tdatecond = '';

    if (!empty($cust)) {
        $cucond = " AND `accode`='$cust' ";
    }


    $dcond = " and `vouchdt` >= '$fdate' and `vouchdt` <= '$tdate' ";

    if (empty($fdate)) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; expenserep.php");
    }

    if (empty($tdate)) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; expenserep.php");
    }

    if ($orderby == "1") {
        $getcash = mysqli_query($conn, "select party,count(*) as norec, sum(grossamt) as gamt,sum(totdisc) as tdisc,sum(totvat) as tvat,sum(billamt) as tamt from `salemain` where party != ''  and 
                                                         vouchno in (select vouchno from salemain where `vouchdt` >= '$fdate' and `vouchdt` <= '$tdate') $dcond group by party order by norec desc") or die(mysqli_error($conn));
    } else {
        $getcash = mysqli_query($conn, "select party,count(*) as norec, sum(grossamt) as gamt,sum(totdisc) as tdisc,sum(totvat) as tvat, sum(billamt) as tamt from `salemain` where party != ''  and 
                                                         vouchno in (select vouchno from salemain where `vouchdt` >= '$fdate' and `vouchdt` <= '$tdate') $dcond group by party order by tamt desc") or die(mysqli_error($conn));
    }
    //    print table($getcash);
    $mfdt = date("d-m-Y", strtotime($fdate));
    $mtdt = date("d-m-Y", strtotime($tdate));

    $contents .= "Sr.No, Visit, Name, Amount, Discount, VAT, Bill Amount \n";

    $ctr = 0;
    $totamt = 0;
    $gamt = 0;
    $tdisc = 0;
    $tvat = 0;
    while ($cmain = mysqli_fetch_array($getcash)) {

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


        $tvat = $cmain['tvat'];
        $gamt = $cmain['gamt'];
        $tdisc = $cmain['tdisc'];
        $mamt = $cmain['tamt'];
        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$cmain[norec]" . ",";
        $contents .= "$showparty[name]" . ",";
        $contents .= "$gamt" . ",";
        $contents .= "$tdisc" . ",";
        $contents .= "$tvat" . ",";
        $contents .= "$mamt" . "\n";

        $totamt = $totamt + $mamt;
        $totdamt = $totdamt + $tdisc;
        $totvat = $totvat + $tvat;
        $totgamt = $totgamt + $gamt;
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$totgamt" . ",";
    $contents .= "$totdamt" . ",";
    $contents .= "$totvat" . ",";
    $contents .= "$totamt" . "\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;
    }
}

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

    $cust = $_POST["cust"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];
    $orderby = $_POST["orderby"];

    $gcond = '';
    $mcond = '';
    $cucond = '';
    $stcond = '';
    $fdatecond = '';
    $tdatecond = '';

    if (!empty($cust)) {
        $cucond = " AND `accode`='$cust' ";
    }


    $dcond = " and `vouchdt` >= '$fdate' and `vouchdt` <= '$tdate' ";

    if (empty($fdate)) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; expenserep.php");
    }

    if (empty($tdate)) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; expenserep.php");
    }




    if ($orderby == "1") {
        $getcash = mysqli_query($conn, "select party,count(*) as norec, sum(netamt) as tamt from `salemain` where party != '' $dcond group by party order by norec desc") or die(mysqli_error($conn));
    } else {
        $getcash = mysqli_query($conn, "select party,count(*) as norec, sum(netamt) as tamt from `salemain` where party != '' $dcond group by party order by tamt desc") or die(mysqli_error($conn));
    }
    $mfdt = date("d-m-Y", strtotime($fdate));
    $mtdt = date("d-m-Y", strtotime($tdate));


    require("library/fpdf/fpdf.php");

    class pdf extends fpdf
    {

        function Footer()
        {



            //Position at 1.5 cm from bottom

            $this->SetY(-15);

            //Arial italic 8

            $this->SetFont('Arial', 'I', 8);

            //Page number

            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }
    }

    $pdf = new pdf('P', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();
    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(96, 130, 191);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(193, 7, "$cashsale", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(96, 130, 191);
    $pdf->Cell(8, 7, "Sr.", 1, 0, '', 1);
    $pdf->Cell(30, 7, "Visit", 1, 0, '', 1);
    $pdf->Cell(115, 7, "Name", 1, 0, '', 1);
    $pdf->Cell(40, 7, "Amount", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $ctr = 0;
    $totamt = 0;
    while ($cmain = mysqli_fetch_array($getcash)) {

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


        $mamt = $cmain[tamt];
        $ctr++;
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(8, 7, "$ctr", 1);
        $pdf->Cell(30, 7, "$cmain[norec]", 1);
        $pdf->Cell(115, 7, "$showparty[name]", 1);
        $pdf->Cell(40, 7, "$mamt", 1);
        $pdf->Ln();
        $totamt = $totamt + $mamt;
    }

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(255, 0, 0);
    $pdf->Cell(8, 7, " ", 1, 0, '', 1);
    $pdf->Cell(30, 7, " ", 1, 0, '', 1);
    $pdf->Cell(115, 7, "Total", 1, 0, '', 1);
    $pdf->Cell(40, 7, "$totamt", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->Output("Leading Party.pdf", "D");
}
?>