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

$maindb = "salemain";
$trandb = "saletran";
$formname = "Credit Sales Summary"; 
$file = "credsumm_fr.php";

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

$month = $_POST["month"];

$monthcond = "";

if (!empty($month)) {
    $monthcond = " AND MONTH(`vouchdt`) = '$month' ";
}
//    if (empty($_POST["fromdate"])) {
//        header("refresh:1; slipreg.php");
//    }
//    if (empty($_POST["todt"])) {
//        header("refresh:1; slipreg.php");
//    }AND a.vouchdt = '$fromdate'

//    $getpmain = mysqli_query($conn, "SELECT a.vouchno,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,a.shift,a.party,b.accode,b.name,b.custcode FROM `$maindb` a,accmast b where a.party= b.accode AND grpcode = '010' $accountcond ")or die(mysqli_error($conn));

//            $get = mysqli_num_rows($getpmain);

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

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold;
        }
/*        th{
            background: #6082BF;
        }*/

    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="container">
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condensed table-hover " style="width : 100%" valign="top">
                            <tr>
                               <td style="vertical-align: top;width: 30%;">
                                    <table class="table table-condensed " style="float: left; margin: 2px">
                                        <tr class="mySearchTr">
                                            <th style="width: 35%;">Date</th>                        
                                            <th style="width: 15%;text-align: right">Amount</th>                        
                                        </tr>
                                <?php
                                $ctr = 0;
                                $totamt = 0;

                                $getqry = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE vouchno != '' AND saletype != 'C' $monthcond group by vouchdt order by vouchdt ")or die(mysqli_error($conn));

                                while ($pqry = mysqli_fetch_array($getqry)) {
                                    
//                                    for ($f = 1; $f <= 31; $f++) {

                                    $qrypetrol = mysqli_query($conn, "SELECT sum(amount)as amt FROM `transacn` WHERE `vouchdt` = '$pqry[vouchdt]' AND source= 'SL' $monthcond") or die(mysqli_error($conn));
                                    $fetp = mysqli_fetch_array($qrypetrol);
                                    
                                        $mydt = $pqry['mydt'];
                                        $amount = $fetp['amt'];
                                    
                                        Echo "<tr>";
                                        Echo "<td>$mydt</td>";
                                        Echo "<td class='text-right'>" . number_format($amount, 2) . "</td>";
                                        Echo "</tr>";
                                        
                                        $totamt = $totamt + $amount;
//                                    }
                                }
                                        Echo "<tr style='background-color: #98F9F0'>";
                                        Echo "<td><b>Total</b></td>";
                                        Echo "<td class='text-right numeric' ><b>" . number_format($totamt, 2) . "</b></td>";
                                        Echo "</tr>";
                                
                                       ?>
                                    </table>
                                </td>
                                <td style="width : 70%;vertical-align: top;">
                                    <table class="table table-condensed" style="float: left;margin: 2px">
                                        <tr class="mySearchTr">
                                            <th style="width :35%;">Acc. No. & Name</th>                        
                                            <th style="width :15%;text-align: right">Amount</th>                        
                                        </tr>
                                <?php
                                $ctr = 0;
                                $totqty = 0;

                                
                                 $getp = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE vouchno != '' AND saletype != 'C' $monthcond group by party")or die(mysqli_error($conn));
                                 
                                 while ($pmain = mysqli_fetch_array($getp)) {

                                        $getname = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE accode = '$pmain[party]'")or die(mysqli_error($conn));
                                        $fetname = mysqli_fetch_array($getname);
                                   
                                        $qrypetrol = mysqli_query($conn, "SELECT sum(amount)as amt FROM `transacn` WHERE `accode` = '$pmain[party]' $monthcond") or die(mysqli_error($conn));
                                        $fetp = mysqli_fetch_array($qrypetrol);

                                        $name       = $fetname['name'];
                                        $accode     = $fetname['accode'];
                                        $amt        = $fetp['amt'];
                                   
                                        Echo "<tr>";
                                        Echo "<td>$accode&nbsp;&nbsp; $name</td>";
                                        Echo "<td class='text-right'>" . number_format($amt, 2) . "</td>";
                                        Echo "</tr>";
                                        
                                        $totalamt = $totalamt + $amt;
                                      }
                                        Echo "<tr style='background-color: #98F9F0'>";
                                        Echo "<td><b>Total</b></td>";
                                        Echo "<td class='text-right numeric'><b>" . number_format($totalamt, 2) . "</b></td>";
                                        Echo "</tr>";
                                      ?>
                                     </table>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
            <?php include("includes/linksfoot.php");
            ?>
    </body>
    </html>
    <?php
}

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

    $contents .= "Sr,Voucher No.,Date,Vehicle No.,Account Name,Product,Shift\n";

    $ctr = 0;
    $totqty = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {

        $getname = mysqli_query($conn, "SELECT * FROM `accmast` where accode='$pmain[party]'")or die(mysqli_error($conn));
        $fetname = mysqli_fetch_array($getname);

        $nname = mysqli_query($conn, "SELECT * FROM `product` where code='$pmain[product]'")or die(mysqli_error($conn));
        $godwn = mysqli_fetch_array($nname);

        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[vehicleno]" . ",";
        $contents .= "$fetname[name]" . ",";
        $contents .= "$godwn[name]" . ",";
        $contents .= "$pmain[shift]" . "\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;
    }
}
?>