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

$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(billamt) 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(billamt) as tamt from `salemain` where party != '' $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.</th>
                                <th style="width:10%;">No. Of Bills</th>
                                <th style="width:40%;">Name </th>
                                <th style="width:30%;" class='text-right'>Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $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++;
                                Echo "<tr>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$cmain[norec]</td>";
                                Echo "<td>$cmain[party]</td>";
                                Echo "<td class='text-right'>$mamt</td>";
                                Echo "</tr>";
                                $totamt = $totamt + $mamt;
                            }

                            Echo "<tr>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td class='text-right'><b>Total</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(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));
    }
//    print table($getcash);
    $mfdt = date("d-m-Y", strtotime($fdate));
    $mtdt = date("d-m-Y", strtotime($tdate));

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

    $ctr = 1;
    $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];

        $cnorec = $cmain[norec];
        $spname = $showparty[name];


        $contents.= $ctr . ",";
        $contents.= $cnorec . ",";
        $contents.= $spname . ",";
        $contents.= $mamt . "\n";

        $ctr++;

        $totamt = $totamt + $mamt;
    }


    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= "Total" . ",";
    $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");
}
?>