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

 $maindb = "salemain";
 $trandb = "saletran";
$formname = "Monthly Account Summary";
$file = "salesumm_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$root = $_SESSION["zdir"];

$fyear = substr($myyear, 0, 2);
$lyear = substr($myyear, -2);
mysqli_select_db($conn, $zdb);

$month = $_POST["month"];
$cname = $_POST["cname"];
$accode = $cname;

$monthcond = "";
$monthcond1 = "";
$cnamecond = "";

if (!empty($month)) {
    $monthcond = " AND MONTH(a.`vouchdt`) = '$month' ";
}

if (!empty($month)) {
    $monthcond1 = " AND MONTH(`vouchdt`) = '$month' ";
}
if (!empty($cname)) {
    $cnamecond = " AND a.`party`='$cname' ";
}
$getmonth = mysqli_query($conn, "SELECT * from $root.month")or die(mysqli_error($conn));

$getqry = mysqli_query($conn, "SELECT a.*,b.* FROM $maindb a,$trandb b where a.vouchno = b.vouchno $cnamecond $monthcond group by party")or die(mysqli_error($conn));
//$fetqry = mysqli_fetch_array($getqry);r

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 style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:30%">Acc.No & Name</th>
                                    <th style="width:12% ; text-align:right">PETROL</th>
                                    <th style="width:13% ; text-align:right">DIESEL</th>
                                    <th style="width:13% ; text-align:right">CNG</th>
                                    <th style="width:13% ; text-align:right">XYZ</th>
                                    <th style="width:15% ; text-align:right">Others</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;
                        
                                while ($pmain = mysqli_fetch_array($getqry)) {

                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
                                    $sparty = mysqli_fetch_array($gparty);
                                    
                                    $qrypetrol = mysqli_query($conn, "SELECT sum(amount)as amt FROM `transacn` where `prodcode` = 'P' AND `accode` = '$pmain[party]' $monthcond1") or die(mysqli_error($conn));
                                    $fetp = mysqli_fetch_array($qrypetrol);
                                    
                                    $qrydiesel = mysqli_query($conn, "SELECT sum(amount)as amt FROM `transacn` where `prodcode` = 'D' AND `accode` = '$pmain[party]' $monthcond1") or die(mysqli_error($conn));
                                    $fetd = mysqli_fetch_array($qrydiesel); 
                                    
                                    $qrycng = mysqli_query($conn, "SELECT sum(amount)as amt FROM `transacn` where `prodcode` = 'C' AND `accode` = '$pmain[party]' $monthcond1") or die(mysqli_error($conn));
                                    $fetc = mysqli_fetch_array($qrycng); 
                                    
                                    $petrol1 = $fetp['amt'];
                                    if ($petrol1 > 0){
                                        $petrol = $fetp['amt'];
                                    } else {
                                      $petrol = 0;  
                                    }
                                    
                                    $diesel1 = $fetd['amt'];
                                    if ($diesel1 > 0){
                                        $diesel = $fetd['amt'];
                                    } else {
                                      $diesel = 0;  
                                    }
                                    
                                    $cng1 = $fetc['amt'];
                                    if ($cng1 > 0){
                                        $cng = $fetc['amt'];
                                    } else {
                                      $cng = 0;  
                                    }
                                    
                                    $name       = $sparty['name'];
                                    $accode     = $sparty['accode'];
                                    
                                    $ctr++;
                                    Echo"<tr>";
                                    Echo"<td>$accode&nbsp;&nbsp; $name</td>";
                                    Echo"<td class='numeric text-right'>$petrol</td>";
                                    Echo"<td class='numeric text-right'>$diesel</td>";
                                    Echo"<td class='numeric text-right'>$cng</td>";
                                    Echo"<td class='numeric text-right'>$xyz</td>";
                                    Echo"<td class='numeric text-right'>$others</td>";
                                    Echo"</tr>";

                                    $totpetrol   = $totpetrol + $petrol;
                                    $totdiesel   = $totdiesel + $diesel;
                                    $totcng   = $totcng + $cng;

                                }
                                Echo "<tr style='background-color: #98F9F0'>";
                                Echo "<td class='text-right'><b>Total</b></td>";
                                Echo "<td class='numeric text-right'><b>$totpetrol</b></td>";
                                Echo "<td class='numeric text-right'><b>$totdiesel</b></td>";
                                Echo "<td class='numeric text-right'><b>$totcng</b></td>";
                                Echo "<td class='numeric text-right'><b>$totxyz</b></td>";
                                Echo "<td class='numeric text-right'><b>$totothers</b></td>";
                                Echo "</tr>";
                                
                                ?>
                            </tbody>
                        </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;
    }
}
?>