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

$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 ($mpara == "P") {
    $formname = "Product Group Monthly Purchase Summary";
} else {
    $formname = "Product Group Monthly Sale Summary";
}


$prod = $_POST["prod"];
$code = $prod;

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


$getprod = mysqli_query($conn, "SELECT code,name FROM prodgrp where code = '$code' ") or die(mysqli_error($conn));
$prod = mysqli_fetch_array($getprod);
$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>Product Name :- <?= $prod['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">Fuel</th>
                                </tr>
                            </thead>

                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;
                                for ($f = 4; $f <= 12; $f++) {

                                $tamount = 0;

                                    $getpgrp = mysqli_query($conn, "SELECT prodcode,name FROM product where prodgrp = '$code' ") or die(mysqli_error($conn));
                                    while ($prgrp = mysqli_fetch_array($getpgrp)) {

                                        $gcode = $prgrp['prodcode'];
                                        if ($mpara == 'P') {
                                            $actqry = mysqli_query($conn, "SELECT sum(amount) as samt FROM `transacn` where qty > 0 AND prodcode = '$gcode' and monno ='$f'") or die(mysqli_error($conn));
                                        } else {
                                            $actqry = mysqli_query($conn, "SELECT sum(amount) as samt FROM `transacn` where qty < 0 AND prodcode = '$gcode' and monno ='$f'") or die(mysqli_error($conn));
                                        }

                                        $fetqry = mysqli_fetch_array($actqry);
                                        $amount = $fetqry['samt'];
                                        $tamount = $tamount + $amount;

                                    }
                                    
                                    $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'>$tamount</td>";
                                    echo "</tr>";

                                    $totpamt = $totpamt + $tamount;

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

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