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


$formname = "Monthly Account Summary";
$file = "acmnsumm_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);

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

$custcodecond = "";
$cnamecond = "";

if (!empty($custcode)) {
    $custcodecond = " AND `custcode`='$custcode' ";
}
if (!empty($cname)) {
    $cnamecond = " AND `name`='$cname' ";
}
$getmonth = mysqli_query($conn, "SELECT * from $root.month")or die(mysqli_error($conn));

$getopbal = mysqli_query($conn, "SELECT name ,openbal ,dc FROM accmast where accode = $cname ")or die(mysqli_error($conn));
$opbal = mysqli_fetch_array($getopbal);

$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">
                        <div class="row">
                            <label class="label-control marg"><b>Account Name :- <?= $opbal['name'] ?></b></label>
                        </div>
                        <table style="background: white;width: 60%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:7%">Month</th>
                                    <th style="width:12% ; text-align:right">Debit</th>
                                    <th style="width:13% ; text-align:right">Credit</th>
                                    <th style="width:15% ; text-align:right">Balance</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;
                                $balance = $opbal['openbal'] ;

                                Echo "<tr id='boldfont'>";
                                Echo "<td>Opening Balance</td>";

                                if ($opbal['dc'] == 'D') {
                                    echo "<td style='text-align:right'>$opbal[openbal]</td>";
                                    Echo "<td></td>";
                                } else {
                                    Echo "<td></td>";
                                    echo "<td style='text-align:right'>$opbal[openbal]</td>";
                                }

                                Echo "<td style='text-align:right'>$balance</td>";
                                Echo "</tr>";

                                for ($f = 4; $f <= 12; $f++) {
                                    $actmon = mysqli_query($conn, "SELECT monname FROM $zroot.`month` WHERE moncode = '$f' ")or die(mysqli_error($conn));
                                    $fetmon = mysqli_fetch_array($actmon);
                                    $monname = $fetmon['monname'];

                                    $actqry = mysqli_query($conn, "SELECT SUM(amount) as damt FROM `actran` WHERE MONTH(date) = '$f' and amount < 0 and accode ='$accode'")or die(mysqli_error($conn));
                                    $fetdeb = mysqli_fetch_array($actqry);
                                    $debamt = $fetdeb['damt'];
                                    $debamt1 = abs($debamt);

                                    $actqry1 = mysqli_query($conn, "SELECT SUM(amount) as camt FROM `actran` WHERE MONTH(date) = '$f' and amount > 0 and  accode ='$accode'")or die(mysqli_error($conn));
                                    $fetcrd = mysqli_fetch_array($actqry1);
                                    $crdamt = $fetcrd['camt'];

                                    $balance = $balance + $debamt + $crdamt ;

                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$monname</td>";
                                    Echo "<td style='text-align:right'>$debamt1</td>";
                                    Echo "<td style='text-align:right'>$crdamt</td>";
                                    Echo "<td style='text-align:right'>$balance</td>";
                                    Echo "</tr>";

                                    $totdebamt1 = $totdebamt1 + $debamt1;
                                    $totcrdamt  = $totcrdamt  + $crdamt;
                                  
                                }

                                for ($f = 1; $f <= 3; $f++) {
                                    $actmon = mysqli_query($conn, "SELECT monname FROM $zroot.`month` WHERE moncode = '$f' ")or die(mysqli_error($conn));
                                    $fetmon = mysqli_fetch_array($actmon);
                                    $monname = $fetmon['monname'];

                                    $actqry = mysqli_query($conn, "SELECT SUM(amount) as damt FROM `actran` WHERE MONTH(date) = '$f' and amount < 0 and accode ='$accode'")or die(mysqli_error($conn));
                                    $fetdeb = mysqli_fetch_array($actqry);
                                    $debamt = $fetdeb['damt'];
                                    $debamt1 = abs($debamt);

                                    $actqry1 = mysqli_query($conn, "SELECT SUM(amount) as camt FROM `actran` WHERE MONTH(date) = '$f' and amount > 0 and  accode ='$accode'")or die(mysqli_error($conn));
                                    $fetcrd = mysqli_fetch_array($actqry1);
                                    $crdamt = $fetcrd['camt'];

                                    $balance = $balance + $debamt + $crdamt ;

                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$monname</td>";
                                    Echo "<td style=' text-align:right'>$debamt1</td>";
                                    Echo "<td style=' text-align:right'>$crdamt</td>";
                                    Echo "<td style=' text-align:right'>$balance</td>";
                                    Echo "</tr>";

                                    $totdebamt1 = $totdebamt1 + $debamt1;
                                    $totcrdamt  = $totcrdamt  + $crdamt;
                                }
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td style='text-align:right'>Total</td>";
                                    Echo "<td style='text-align:right'>$totdebamt1</td>";
                                    Echo "<td style='text-align:right'>$totcrdamt</td>";
                                    Echo "<td style='text-align:right'></td>";
                                    Echo "</tr>";
                                
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td style='text-align:right'>Closing Balance</td>";
                                    Echo "<td style='text-align:right'>$balance</td>";
                                    Echo "<td style='text-align:right'></td>";
                                    Echo "<td style='text-align:right'></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;
    }
}
?>