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

$maindb = "cashpay";
$trandb = "";
$formname = "Partywise Expense Report";
$file = "partyexp_fr.php";

$zroot = '';

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

if (isset($_POST["view"])) {
    $party = $_POST["party"];
    $expense = $_POST["expense"];
    $dtype = $_POST["dtype"];


//    browse($expense);
    $dtypecond = "";
    $expensecond = "";
    $partycond = "";

    $mfrmdate = trim($_POST["fdate"]);
    $mtodate = trim($_POST["tdate"]);




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


//    if (!empty($frmdt)) {
//        $frmdtcond = " AND `vouchdt`='$frmdt' ";
//    }
//
//    if (!empty($todt)) {
//        $todtcond = " AND `todt`='$todt' ";
//    }

    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; partyexp.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; partyexp.php");
    }

    $getpmain = mysqli_query($conn,"SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate'  $partycond $expensecond group by party")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>

            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-hover">
                        <thead >
                            <tr class="mySearchTr">
                                <th style="width:10%">Sr</th>
                                <th style="width:65%">Party</th>
                                <th style="width:25%" class="text-right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;

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

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$supp[name]</td>";
                                Echo "<td class='text-right'>$pmain[amount]</td>";
                                Echo "</tr>";
                                $totamt = $totamt + $pmain[amount];

                                if ($_POST["dtype"] == 2) {

                                    Echo "<tr style='height:10px;'>";
                                    Echo "<td colspan='3'>";
                                    Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    Echo "<th style='background-color: #cccccc;width:5%;'>Sr</th>";
                                    Echo "<th style='background-color: #cccccc;width:10%; class='numeric''>Voucher</th>";
                                    Echo "<th style='background-color: #cccccc';width:10%; >Date</th>";
                                    Echo "<th style='background-color: #cccccc;width:55%;' class=''>Expense</th>";
                                    Echo "<th style='background-color: #cccccc;width:20%;' class='numeric'>Amount</th>";
                                    Echo "<tr>";


                                    $ctr2 = 0;
//                                    $totamt = 0;
//                                    $totdisc = 0;

                                    $gettran = mysqli_query($conn,"SELECT *, date_format(`vouchdt`,'%d/%m/%Y') as mydt FROM `$maindb` WHERE `party`='$pmain[party]' $expensecond")or die(mysqli_error($conn));
                                    while ($tran = mysqli_fetch_array($gettran)) {

                                        $getexp = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$tran[expense]'")or die(mysqli_error($conn));
                                        $iexp = mysqli_fetch_array($getexp);

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td class=''>$tran[vouchno]</td>";
                                        Echo "<td class=''>$tran[mydt]</td>";
                                        Echo "<td class=''>$iexp[name]</td>";
                                        Echo "<td class='numeric'>$tran[amount]</td>";
                                        Echo "</tr>";
                                        $dtotamt = $dtotamt + $tran[amount];
//                                        $totdisc = $totdisc + $tran[discount];
                                    }
                                    Echo "</table>";
                                    Echo "</td>";
                                }
                                if ($_POST["dtype"] == 2) {
                                    echo "<tr> ";
                                    $colr1 = "blue";

                                    echo "<td style='color:$colr1;' align='right' id='font1'><h5><b></b></h5></td>";
                                    echo "<td style='color:$colr1;' align='right' id='font1'><h5><b>Total </b></h5></td>";
                                    echo "<td style='color:$colr1;' align='right' id='font1'><h5><b>$dtotamt</b></h5></td>";
                                    echo "</tr>";
                                }
                            }
                            $colr = "green";
                            echo "<tr> ";

                            echo "<td></td>";
                            echo "<td align='right' style='color:$colr;'><h4><b>Grand Total</b></h4></td>";

                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totamt</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    $dtypecond = "";
    $partycond = "";
    $expensecond = "";
    $mscopecond = "";

    $mfrmdate = trim($_POST["fdate"]);
    $mtodate = trim($_POST["tdate"]);




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

    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`='$dtype' ";
    }

    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; purreg.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; purreg.php");
    }

    $getpmain = mysqli_query($conn,"SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $partycond $expensecond group by party ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);

    $contents.="Sr.,Party,Amount\n";

    $ctr = 0;
    $totamt = 0;

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

        $ctr++;
        $psupp = $supp[name];
        $pamount = $pmain[amount];

        $contents.=$ctr . ",";
        $contents.=$psupp . ",";
        $contents.=$pamount . "\n";

        $totamt = $totamt + $pmain[amount];

        if ($_POST["dtype"] == 2) {

            $contents.="Sr.,Voucher,Date,Expense,Amount\n";

            $ctr2 = 0;
            $dtotamt = 0;

            $gettran = mysqli_query($conn,"SELECT *, date_format(`vouchdt`,'%d/%m/%Y') as mydt FROM `$maindb` WHERE `party`='$pmain[party]' $expensecond")or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $getexp = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$tran[expense]'")or die(mysqli_error($conn));
                $iexp = mysqli_fetch_array($getexp);

                $ctr2++;

                $pvouchno = $tran[vouchno];
                $pmydt = $tran[mydt];
                $pexpense = $iexp[name];
                $ptamount = $tran[amount];

                $contents.=$ctr2 . ",";
                $contents.=$pvouchno . ",";
                $contents.=$pmydt . ",";
                $contents.=$pexpense . ",";
                $contents.=$ptamount . "\n";

                $dtotamt = $dtotamt + $tran[amount];
            }
        }

        if ($_POST["dtype"] == 2) {
            $contents.=" " . ",";
            $contents.=" " . ",";
            $contents.=" " . ",";
            $contents.="Total" . ",";
            $contents.="$dtotamt" . "\n";
        }
    }


    $contents.=" " . ",";
    $contents.="Grand Total" . ",";
    $contents.="$totamt" . "\n";

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
    }
    print $contents;
}
?>