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

$maindb = "challan";
//$trandb = "samptran";
$formname = "Analysis - Summary of Regular Work Done Partywise";
$file = "analysis_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];
mysqli_select_db($conn,$zdb);


$name = $_POST["pname"];
$sizemm = $_POST["sizemm"];
$guagemm = $_POST["guagemm"];
$makesizemm = $_POST["makesizemm"];
$fts = $_POST["fts"];



$partycond = "";
$sizecond = "";
$guagecond = "";
$mksizecond = "";
$ftscond = "";

$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);

if (!empty($name)) {
    $partycond = " AND `party`='$name' ";
}
if (!empty($sizemm)) {
    $sizecond = " AND `sizemm`='$sizemm' ";
}
if (!empty($guagemm)) {
    $guagecond = " AND `guaguemm`='$guagemm' ";
}
if (!empty($makesizemm)) {
    $mksizecond = " AND `makemm`='$makesizemm' ";
}
if (!empty($fts)) {
    $ftscond = " AND `foot`='$fts' ";
}

$mainqry = mysqli_query($conn,"SELECT *,DATE_FORMAT(vouchdt, '%d-%m-%Y') as mydt FROM `$maindb` WHERE vouchno != '' $partycond $sizecond $guagecond $mksizecond $ftscond 
                        order by vouchdt ")or die(mysqli_error($conn));

if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>
    </head>
    <body>
        <div class="container">  

            <div class="row">
                <?php include("includes/head_rep.php"); ?>
                <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:5%;">Sr</th>
                                <th style="width:8%;">NO.</th>
                                <th style="width:8%;">Party</th>
                                <th style="width:5%;">Sizes</th>
                                <th style="width:5%;">SWGS</th>
                                <th style="width:7%;">Make Size</th>
                                <th style="width:5%;">DIA</th>
                                <th style="width:5%;">FTS.</th>
                                <th style="width:5%;">T.FTS.</th>
                                <th style="width:5%;" class="text-right">Rate</th>
                                <th style="width:8%;" class="text-right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totqty = 0;
                            $total = 0;

                            while ($fmain = mysqli_fetch_array($mainqry)) {
                                $party = $fmain["party"];
                                $getname = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$party'")or die(mysqli_error($conn));
                                $gname = mysqli_fetch_array($getname);
                                $ctr++;
                                $mydt = $fmain["mydt"];
                                $delvdt = $fmain["delvdt"];
                                $pname = $gname["name"];
                                $size = $fmain["size"];
                                $makesize = $fmain["makesize"];
                                $dia = $fmain["dia"];
                                $foot = $fmain["foot"];
                                $totfeet = $fmain["totfeet"];
                                $rate = $fmain["rate"];
                                $amount = $fmain["amount"];

                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td></td>";
                                Echo "<td>$mydt</td>";
                                Echo "<td>$pname</td>";
                                Echo "<td>$size</td>";
                                Echo "<td>$makesize</td>";
                                Echo "<td>$dia</td>";
                                Echo "<td>$foot</td>";
                                Echo "<td>$totfeet</td>";
                                Echo "<td class='text-right'>$rate</td>";
                                Echo "<td class='text-right'>$amount</td>";
                                Echo "</tr>";
                                $total = $total + $amount;
                            }
                            $colr = "red";
                            echo "<tr> ";
                            echo "<td align='right' style='color:$colr;' id='font' colspan='10'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$total</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    
    include("includes/head_rep_excel.php");
    
    $contents .= "Sr,No., Date, Party,Sizes,SWGS,Make Size,DIA, FTS., T.FTS.,Rate,Amount\n";
    $ctr = 0;
    $totamt = 0;

    while ($fmain = mysqli_fetch_array($mainqry)) {
        $fparty = $fmain['party'];

        $getname = mysqli_query($conn,"SELECT `name` FROM `accmast` WHERE `accode`= '$fparty'")or die(mysqli_error($conn));
        $gname = mysqli_fetch_array($getname);

        $ctr++;

        $mydt = $fmain["mydt"];
        $pname = $gname["name"];
        $size = $fmain["size"];
        $makesize = $fmain["makesize"];
        $dia = $fmain["dia"];
        $foot = $fmain["foot"];
        $totfeet = $fmain["totfeet"];
        $rate = $fmain["rate"];
        $amount = $fmain["amount"];

        $contents .= $ctr . ",";
        $contents .= " " . ",";
        $contents .= $mydt . ",";
        $contents .= $pname . ",";
        $contents .= $size . ",";
        $contents .= "" . ",";
        $contents .= $makesize . ",";
        $contents .= $dia . ",";
        $contents .= $foot . ",";
        $contents .= $totfeet . ",";
        $contents .= $rate . ",";
        $contents .= $amount . "\n";
        $totamt = $totamt + $amount;
    }
    $contents .= ",,,,,,,,,,Grand Total" . ",";
    $contents .= $totamt . ",";

    $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;
    }
}

