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

$maindb = "challan";
//$trandb = "samptran";
$formname = "Summary Partywise";
$file = "summparty_fr.php";

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

$party = $_POST["pname"];
$dtypecond = "";
$namecond = "";

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

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

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");
}

$mainqry = mysqli_query($conn,"SELECT accode,name from accmast where accode in ( select party from $maindb where `vouchno` !='' $namecond    
                         AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ) group by accode order by name")or die(mysqli_error($conn));
if (isset($_POST["view"])) {

    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"style="width:800px">  

            <div class="row">
                            <?php include("includes/head_rep.php"); ?>
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed  table-bordered">
                        <thead>
                            <tr class="mySearchTr">
                                <th style="width:5%">Sr</th> 
                               <th style="width:75%" class="">Party</th>
                               <th style="width:20%" class="text-right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totqty = 0;
                            $totamt = 0;

                            while ($fmain = mysqli_fetch_array($mainqry)) {
                                
                                $fparty = $fmain['accode'];
                                
                                
                                
                                $gparty = mysqli_query($conn,"SELECT SUM(amount) as totamt FROM `$maindb` WHERE `party`= '$fparty'  AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));
                                $fname = mysqli_fetch_array($gparty);
                                $ftotamt = $fname['totamt'];
                                $pname = $fmain['name'];
                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pname</td>";
                                Echo "<td class='text-right'>$ftotamt</td>";
                                Echo "</tr>";

                                $totamt = $totamt + $ftotamt;
                            }
                            $colr = "red";
                            echo "<tr class='mySearchTr'> ";
                            echo "<td align='right' style='' id='font' colspan=''><h5><b></b></h5></td>";
                            echo "<td align='right' style='' id='font' colspan=''><h5><b>Grand Total</b></h5></td>";
                            echo "<td align='right' style='' id='font'><h5><b>$totamt</b></h5></td>";
//                            echo "<td></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>

    = 
    <?php
    include("includes/head_rep_excel.php");
    $contents.="Sr,Voucher,Date,Name,Amount,Remark\n";
    ?>

    <?php
    $ctr = 0;
    $totqty = 0;
    $total = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getname = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$pmain[name]'")or die(mysqli_error($conn));
        $gname = mysqli_fetch_array($getname);
        $ctr++;
        $contents.="$ctr" . ",";
        $contents.="$pmain[mydt]" . ",";
        $contents.="$pmain[vouchno]" . ",";
        $contents.="$gname[name]" . ",";
        $contents.="$pmain[amount]" . ",";
        $contents.="$pmain[narr1]" . "\n";
        $total = $total + $pmain[amount];
    }
    $contents.="" . ",";
    $contents.="Grand Total" . ",";
    $contents.="$total" . ",";

    $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;
    }
}

