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

$formname = "Monthly Purchase & Sale Summary";
$file = "pgrpsumm_p_fr.php";

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

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


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: 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">Purchase</th>
                                    <th style="width:12% ; text-align:right">Sale</th>
                                </tr>
                            </thead>

                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;
                                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 pamt FROM `actran` WHERE MONTH(date) = '$f' and grpcode ='004'")or die(mysqli_error($conn));
                                    $fetdeb = mysqli_fetch_array($actqry);
                                    $debamt = $fetdeb['pamt'];
                                    $puramt = abs($debamt);
                                    
                                    $actqry1 = mysqli_query($conn, "SELECT SUM(amount) as samt FROM `actran` WHERE MONTH(date) = '$f' and  grpcode ='005' ")or die(mysqli_error($conn));
                                    $fetcrd = mysqli_fetch_array($actqry1);
                                    $crdamt = $fetcrd['samt'];
                                    $salamt = abs($crdamt);
                                     
                                    echo "<tr id='boldfont'>";
                                    echo "<td>$monname</td>";
                                    echo "<td style='text-align:right'>$puramt</td>";
                                    echo "<td style='text-align:right'>$salamt</td>";
                                    echo "</tr>";

                                    $totpamt = $totpamt + $puramt;
                                    $totsamt = $totsamt + $salamt;

                                    $pamt = number_format($totpamt, 2);
                                    $samt = number_format($totsamt, 2);
                                }

                                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 pamt FROM `actran` WHERE MONTH(date) = '$f' and grpcode ='004'")or die(mysqli_error($conn));
                                    $fetdeb = mysqli_fetch_array($actqry);
                                    $debamt = $fetdeb['pamt'];
                                    $puramt = abs($debamt);
                                    
                                    $actqry1 = mysqli_query($conn, "SELECT SUM(amount) as samt FROM `actran` WHERE MONTH(date) = '$f' and  grpcode ='005' ")or die(mysqli_error($conn));
                                    $fetcrd = mysqli_fetch_array($actqry1);
                                    $crdamt = $fetcrd['samt'];
                                    $salamt = abs($crdamt);
                                     
                                    echo "<tr id='boldfont'>";
                                    echo "<td>$monname</td>";
                                    echo "<td style='text-align:right'>$puramt</td>";
                                    echo "<td style='text-align:right'>$salamt</td>";
                                    echo "</tr>";

                                    $totpamt = $totpamt + $puramt;
                                    $totsamt = $totsamt + $salamt;

                                    $pamt = number_format($totpamt, 2);
                                    $samt = number_format($totsamt, 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'>$samt</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;
    }
}
?>