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

// $maindb = "accmast";
// $trandb = "actran";
$formname = "Monthly Invoice Summary";
$file = "invsumm_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);

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

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

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

$getopbal = mysqli_query($conn, "SELECT name 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">Amount</th>
                                    <th style="width:12% ; text-align:right">Op.Chgs.</th>
                                    <th style="width:12% ; text-align:right">Arr.Chgs.</th>
                                    <th style="width:12% ; text-align:right">Total Amount</th>
                                </tr>
                            </thead>

                            <tbody>
                                <?php

                                $ctr = 0;
                                $totqty = 0;
                                for ($f = 4; $f <= 12; $f++) {

                                    $tamount2 = 0;
                                    $getqry = mysqli_query($conn, "SELECT sum(`subtotal`) as amount FROM `salemain` WHERE MONTH(`vouchdt`) = '$f' 
                                                                          $cpartycond ") or die(mysqli_error($conn));
                                    $pqry = mysqli_fetch_array($getqry);
                                    $amount = $pqry['amount'];

                                    $actqry = mysqli_query($conn, "SELECT sum(`operchgs`)as operchgs, sum(`arrears`) as arrears FROM `invoice` 
                                                                          WHERE accode != '' AND month = '$f' $cnamecond  group by month ") or die(mysqli_error($conn));
                                    $fetqry = mysqli_fetch_array($actqry);
                                    $operchgs = $fetqry['operchgs'];
                                    $arrears = $fetqry['arrears'];

                                    $tamount2 = $amount + $operchgs + $arrears;

                                    $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'];

                                    echo "<tr id='boldfont'>";
                                    echo "<td>$monname</td>";
                                    echo "<td style='text-align:right'>$amount</td>";
                                    echo "<td style='text-align:right'>$operchgs</td>";
                                    echo "<td style='text-align:right'>$arrears</td>";
                                    echo "<td style='text-align:right'>$tamount2 </td>";
                                    echo "</tr>";

                                    $totpamt = $totpamt + $amount;
                                    $totopchg = $totopchg + $operchgs;
                                    $totarr = $totarr + $arrears;
                                    $totamt2 = $totamt2 + $tamount2;
                                }

                                for ($f = 1; $f <= 3; $f++) {

                                    $tamount2 = 0;
                                    $getqry = mysqli_query($conn, "SELECT sum(`subtotal`) as amount FROM `salemain` WHERE MONTH(`vouchdt`) = '$f' 
                                                                          $cpartycond ") or die(mysqli_error($conn));
                                    $pqry = mysqli_fetch_array($getqry);
                                    $amount = $pqry['amount'];

                                    $actqry = mysqli_query($conn, "SELECT sum(`operchgs`)as operchgs, sum(`arrears`)as arrears FROM `invoice` 
                                                                          WHERE accode != '' AND month = '$f' $cnamecond  group by month ") or die(mysqli_error($conn));
                                    $fetqry = mysqli_fetch_array($actqry);
                                    $operchgs = $fetqry['operchgs'];
                                    $arrears = $fetqry['arrears'];

                                    $tamount2 = $amount + $operchgs + $arrears;

                                    $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'];

                                    echo "<tr id='boldfont'>";
                                    echo "<td>$monname</td>";
                                    echo "<td style='text-align:right'>$amount</td>";
                                    echo "<td style='text-align:right'>$operchgs</td>";
                                    echo "<td style='text-align:right'>$arrears</td>";
                                    echo "<td style='text-align:right'>$tamount2 </td>";
                                    echo "</tr>";

                                    $totpamt = $totpamt + $amount;
                                    $totopchg = $totopchg + $operchgs;
                                    $totarr = $totarr + $arrears;
                                    $totamt2 = $totamt2 + $tamount2;
                                }
                                $pamt = number_format($totpamt, 2);
                                $totop = number_format($totopchg, 2);
                                $tarr = number_format($totarr, 2);
                                $tamt2 = number_format($totamt2, 2);

                                echo "<tr id='boldfont'>";
                                echo "<td style='text-align:right'>Total</td>";
                                echo "<td style='text-align:right'>$pamt</td>";
                                echo "<td style='text-align:right'>$totop</td>";
                                echo "<td style='text-align:right'>$tarr</td>";
                                echo "<td style='text-align:right'>$totamt2</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;
    }
}
?>