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

$maindb = "challan";
$formname = "Party Rate Compare";
$file = "partratecomp_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"];
$dtypecond = "";
$namecond = "";

$mfrmdate = trim($_POST["frmdate"]);
$mtodate = trim($_POST["todate"]);

if (!empty($name)) {
    $namecond = " AND `party`='$name' ";
    $gparty = mysqli_query($conn,"SELECT `name`,`add1`, `add2`, `add3` FROM `accmast` WHERE `accode`= '$name'")or die(mysqli_error($conn));
    $party = mysqli_fetch_array($gparty);
    $prtname = $party["name"];
    $prtadd1 = $party["add1"];
    $prtadd2 = $party["add2"];
    $prtadd3 = $party["add3"];
    $prtcity = $party["city"];
    $prtpin = $party["pin"];
}

if (empty($_POST["frmdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; partratecomp.php");
}
if (empty($_POST["todate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; partratecomp.php");
}
$zroot = $_SESSION["zdir"];
$getcomp = mysqli_query($conn,"SELECT * FROM $zroot.`company`")or die(mysqli_error($conn));
$comp = mysqli_fetch_array($getcomp);

$mainqry = mysqli_query($conn,"SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $maindb  WHERE vouchno != '' 
                         AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $namecond ")or die(mysqli_error($conn));
if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">  

            <div class="row">
                <?php include("includes/head_rep2.php"); ?>
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table-condensed table-hover ">
                        <thead class="mySearchTr">
                            <tr style="background-color:#64CCFC">
                                <th style="width:8%;">DATE</th>
                                <th style="width:8%;">NAGS</th>
                                <th style="width:8%;">SIZES</th>
                                <th style="width:8%;" class="">SWGS</th>
                                <th style="width:10%;" class="">MAKE SIZES</th>
                                <th style="width:8%;" class="">DIA</th>
                                <th style="width:10%;" class="">FT S.</th>
                                <th style="width:10%;" class="">T.FT S.</th>
                                <th style="width:10%;" class="">WEIGHT</th>
                                <th style="width:10%;" class="">RATE</th>
                                <th style="width:10%;" class="">AMOUNT</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totqty = 0;
                            $total = 0;

                            while ($fmain = mysqli_fetch_array($mainqry)) {
                                $vouchdt = $fmain['mydt'];
                                $party = $fmain['party'];
                                $nag = $fmain['nag'];
//                                $size = $fmain['size'];
                                $sizemm = $fmain['sizemm'];
                                $guagemm = $fmain['guagemm'];
                                $makesize = $fmain['makesize'];
                                $makemm = $fmain['makemm'];
                                $dia = $fmain['dia'];
                                $foot = $fmain['foot'];
                                $totfeet = $fmain['totfeet'];
                                $weight = $fmain['weight'];
                                $rate = $fmain['rate'];
                                $amount = $fmain['amount'];

                                $getparty = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$party'")or die(mysqli_error($conn));
                                $fparty = mysqli_fetch_array($getparty);
                                $pname = $fparty["name"];
                                $ctr++;

                                Echo "<tr id='boldfont' >";
                                Echo "<td>$vouchdt</td>";
                                Echo "<td>$nag</td>";
                                Echo "<td>$sizemm</td>";
                                Echo "<td>$weight</td>";
                                Echo "<td>$makesize</td>";
                                Echo "<td>$dia</td>";
                                Echo "<td>$foot</td>";
                                Echo "<td>$totfeet</td>";
                                Echo "<td>$weight</td>";
                                Echo "<td>$rate</td>";
                                Echo "<td>$amount</td>";
                                Echo "</tr>";
                                $total = $total + $pmain[amount];

                                $mainqry2 = mysqli_query($conn,"SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $maindb  WHERE vouchno != '' 
                                            AND  `sizemm` = '$sizemm' and  guagemm = '$guagemm' and `makemm`='$makemm' and party != '$party' ")or die(mysqli_error($conn));
                                $ctr2 = 0;
                                while ($fmain2 = mysqli_fetch_array($mainqry2)) {
                                    $vouchdt2 = $fmain2["mydt"];
                                    $rate2 = $fmain2["rate"];
                                    $party2 = $fmain2["party"];

                                    $getparty2 = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$party2'")or die(mysqli_error($conn));
                                    $fparty2 = mysqli_fetch_array($getparty2);
                                    $pname2 = $fparty2["name"];
                                    $ctr2++;
                                    echo "<tr style='background-color:grey;color:white'>";
                                    echo "<td>$vouchdt2</td>";
                                    echo "<td colspan='5'></td>";
                                    echo "<td colspan='3'>Party$ctr2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$pname2</td>";
                                    echo "<td>$rate2</td>";
                                    echo "<td></td>";
                                    echo "</tr>";
                                }
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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








    $contents .= "DATE, NAGS SIZES,SWGS,MAKE SIZES, DIA, FT S., T.FT S., WEIGHT, RATE, AMOUNT\n";


    $ctr = 0;
    $totqty = 0;
    $total = 0;

    while ($fmain = mysqli_fetch_array($mainqry)) {
        $vouchdt = $fmain['mydt'];
        $party = $fmain['party'];
        $nag = $fmain['nag'];
        $sizemm = $fmain['sizemm'];
        $guagemm = $fmain['guagemm'];
        $makesize = $fmain['makesize'];
        $makemm = $fmain['makemm'];
        $dia = $fmain['dia'];
        $foot = $fmain['foot'];
        $totfeet = $fmain['totfeet'];
        $weight = $fmain['weight'];
        $rate = $fmain['rate'];
        $amount = $fmain['amount'];

        $getparty = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$party'")or die(mysqli_error($conn));
        $fparty = mysqli_fetch_array($getparty);
        $pname = $fparty["name"];
        $ctr++;

        $contents.= $vouchdt . ",";
        $contents.= $nag . ",";
        $contents.= $sizemm . ",";
        $contents.= $weight . ",";
        $contents.= $makesize . ",";
        $contents.= $dia . ",";
        $contents.= $foot . ",";
        $contents.= $totfeet . ",";
        $contents.= $weight . ",";
        $contents.= $rate . ",";
        $contents.= $amount . "\n";
        $total = $total + $pmain['amount'];

        $mainqry2 = mysqli_query($conn,"SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $maindb  WHERE vouchno != '' 
                                            AND  `sizemm` = '$sizemm' and  guagemm = '$guagemm' and `makemm`='$makemm'")or die(mysqli_error($conn));
        $ctr2 = 0;
        while ($fmain2 = mysqli_fetch_array($mainqry2)) {
            $vouchdt2 = $fmain2["mydt"];
            $rate2 = $fmain2["rate"];

            $getparty2 = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$party'")or die(mysqli_error($conn));
            $fparty2 = mysqli_fetch_array($getparty2);
            $pname2 = $fparty2["name"];
            $ctr2++;
            $contents.=$vouchdt2 . ",";
            $contents.= ",,,,,,,";
            $contents.= "Party".$ctr2 . " $pname2". ",";
            $contents.= $rate2 . ",";
        }
    }
    
$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;
}
}

