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

$maindb = "brand";
$trandb = "saletran";
$formname = "Brand-Wise-Sale Register";
$file = "salebrreg.php";

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

mysqli_select_db($conn, $zdb);

    $brand = $_POST[brand];
    $party = $_POST[party];
    $godown = $_POST[godown];
    $mtype = $_POST[mtype];
    $mfromdt = $_POST[frmdt];
    $mtodt = $_POST[todt];

    $partycond = "";
    $brokercond = "";
    $transportcond = "";
    $godowncond = "";
    $mtypecond = "";
    $brandcond = "";


    if (!empty($brand)) {
        $brandcond = " AND `code`='$brand' ";
        $brandcond1 = " AND b.`brand`='$brand' ";
    }

    if (!empty($party)) {
        $partycond = " AND a.`party`='$party' ";
    }

    if (!empty($godown)) {
        $godowncond = " AND a.`godown`='$godown' ";
    }

    if (!empty($mtype)) {
        $mtypecond = " AND a.`type`='$mtype' ";
    }

    $qrybrand = mysqli_query($conn, "SELECT * FROM brand WHERE name != '' $brandcond and code in 
                                     (SELECT b.brand FROM `salemain` a, `saletran` b WHERE a.vouchno = b.vouchno and
                                     a.`vouchdt` >= '$mfromdt' and a.`vouchdt` <= '$mtodt' $brandcond1 $partycond $godowncond $mtypecond) 
                                     order by name") or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    
    include("includes/linkshead.php");
    ?>
    </head>
    <body> 
        <div class="container">
            <div class="row">
                <?php include("includes/head_rep.php"); ?>
                <div class="col-lg-12 pad">
                    <table style="width:100%;background: white" class="table table-condensed table-hover">
                        <thead>
                            <tr style="height:10px;background-color: #1ABC9C">                            
                                <th>Quality</th>                                                       
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $grqty = 0;
                            $gramt = 0;
                            while ($fetbrand = mysqli_fetch_array($qrybrand)) {
                                $brcode = $fetbrand[code];
                                $ctr++;
                                $tqty = 0;
                                $tamt = 0;
                                Echo "<tr>";
                                Echo "<td colspan='7'>$fetbrand[name]</td>";
                                Echo "</tr>";


                                Echo "<tr style='height:10px;'>";
                                Echo "<td colspan='25'>";
                                Echo "<table style='width:100%;' class='table table-condensed table-hover'>";
                                Echo "<th style='background-color: #cccccc;width:10%;'>Date</th>";
                                Echo "<th style='background-color: #cccccc;width:8%;'>Invoice No.</th>";
                                Echo "<th style='background-color: #cccccc;width:27%;'>Partyname</th>";
                                Echo "<th style='background-color: #cccccc;width:25%;'>Itemname</th>";
                                Echo "<th style='background-color: #cccccc;width:10%;' class='numeric'>Qty.</th>";
                                Echo "<th style='background-color: #cccccc;width:10%;' class='numeric'>Rate</th>";
                                Echo "<th style='background-color: #cccccc;width:10%;' class='numeric'>Amount</th>";
                                Echo "<tr>";

                                $ctr2 = 0;
                                $qrydtl = mysqli_query($conn, "SELECT a.party, a.godown, a.type, a.invno, DATE_FORMAT(a.vouchdt,'%d-%m-%Y') as mydt,
                                        b.* FROM `salemain` a, `saletran` b WHERE a.vouchno = b.vouchno and 
                                        a.`vouchdt` >= '$mfromdt' and a.`vouchdt` <= '$mtodt' and b.brand = '$brcode' 
                                        $partycond $godowncond $mtypecond order by a.vouchdt, a.invno, b.itemcode") or die(mysqli_error($conn));

                                while ($msale = mysqli_fetch_array($qrydtl)) {
                                    $getitem = mysqli_query($conn, "SELECT name FROM `item` WHERE `code`='$msale[itemcode]'")or die(mysqli_error($conn));
                                    $mitem = mysqli_fetch_array($getitem);

                                    $getcust = mysqli_query($conn, "SELECT name FROM `accmast` WHERE `accode`='$msale[party]'")or die(mysqli_error($conn));
                                    $cust = mysqli_fetch_array($getcust);

                                    $getmtype = mysqli_query($conn, "SELECT name FROM `type` WHERE `code`='$msale[type]'")or die(mysqli_error($conn));
                                    $mtype = mysqli_fetch_array($getmtype);

                                    $ctr2++;
                                    $mamt = $msale[invqty] * $msale[rate];
                                    Echo "<tr>";
                                    Echo "<td>$msale[mydt]</td>";
                                    Echo "<td>$msale[invno]</td>";
                                    Echo "<td>$cust[name]</td>";
                                    Echo "<td>$mitem[name]</td>";
                                    Echo "<td class='numeric'>$msale[invqty]</td>";
                                    Echo "<td class='numeric'>$msale[rate]</td>";
                                    Echo "<td class='numeric'>" . number_format($mamt, 2) . "</td>";
                                    Echo "</tr>";
                                    $tqty = $tqty + $msale[invqty];
                                    $tamt = $tamt + $mamt;

                                    $grqty = $grqty + $msale[invqty];
                                    $gramt = $gramt + $mamt;
                                }
                                echo "<tr> ";
                                echo "<td style='width:10%;'></td>";
                                echo "<td style='width:10%;'></td>";
                                echo "<td style='width:30%;'></td>";
                                echo "<td style='width:20%;'><b >Brand Total</b></td>";
                                echo "<td align='right' style='width:10%;'><b>$tqty</b></td>";
                                echo "<td style='width:10%;'></td>";
                                echo "<td align='right' style='width:10%;'><b>" . number_format($tamt, 2) . "</b></td>";
                                echo "</tr>";
                            }
                            echo "<tr> ";
                            echo "<td style='width:10%;'></td>";
                            echo "<td style='width:10%;'></td>";
                            echo "<td style='width:30%;'></td>";
                            echo "<td style='width:20%;'><b >Grand Total</b></td>";
                            echo "<td align='right' style='width:10%;'><b>$grqty</b></td>";
                            echo "<td style='width:10%;'></td>";
                            echo "<td align='right' style='width:10%;'><b>" . number_format($gramt, 2) . "</b></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php"); ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
  
  
    $contents .= "Quality\n";
                            
    
                            $ctr = 0;
                            $grqty = 0;
                            $gramt = 0;
                            while ($fetbrand = mysqli_fetch_array($qrybrand)) {
                                $brcode = $fetbrand[code];
                                $ctr++;
                                $tqty = 0;
                                $tamt = 0;
                                
                                $contents .= "$fetbrand[name]\n";

                                $contents .= "Date,Invoice No.,Party Name,Itemname,Qty.,Rate,Amount\n";
                                
                                $ctr2 = 0;
                                $qrydtl = mysqli_query($conn, "SELECT a.party, a.godown, a.type, a.invno, DATE_FORMAT(a.vouchdt,'%d-%m-%Y') as mydt,
                                        b.* FROM `salemain` a, `saletran` b WHERE a.vouchno = b.vouchno and 
                                        a.`vouchdt` >= '$mfromdt' and a.`vouchdt` <= '$mtodt' and b.brand = '$brcode' 
                                        $partycond $godowncond $mtypecond order by  b.itemcode") or die(mysqli_error($conn));

                                while ($msale = mysqli_fetch_array($qrydtl)) {
                                    $getitem = mysqli_query($conn, "SELECT name FROM `item` WHERE `code`='$msale[itemcode]'")or die(mysqli_error($conn));
                                    $mitem = mysqli_fetch_array($getitem);

                                    $getcust = mysqli_query($conn, "SELECT name FROM `accmast` WHERE `accode`='$msale[party]'")or die(mysqli_error($conn));
                                    $cust = mysqli_fetch_array($getcust);

                                    $getmtype = mysqli_query($conn, "SELECT name FROM `type` WHERE `code`='$msale[type]'")or die(mysqli_error($conn));
                                    $mtype = mysqli_fetch_array($getmtype);

                                    $ctr2++;
                                    
                                    $mamt = $msale[invqty] * $msale[rate];
                                    
                                       $custstr = str_replace( ",", " " ,"$cust[name]");
                                    
                                    $contents .= "$msale[mydt]" . ",";
                                    $contents .= "$msale[invno]" . ",";
                                    $contents .= "$custstr" . ",";
                                    $contents .= "$mitem[name]" . ",";
                                    $contents .= "$msale[invqty]" . ",";
                                    $contents .= "$msale[rate]" . ",";
                                    $contents .= "$mamt" . "\n";
                                   
                                    $tqty = $tqty + $msale[invqty];
                                    $tamt = $tamt + $mamt;

                                    $grqty = $grqty + $msale[invqty];
                                    $gramt = $gramt + $mamt;
                                }
                                    $contents .= "" . ",";
                                    $contents .= "" . ",";
                                    $contents .= "" . ",";
                                    $contents .= "Brand Total" . ",";
                                    $contents .= "$tqty" . ",";
                                    $contents .= "" . ",";
                                    $contents .= "$tamt" . "\n";
                                
                            }
                            
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "Grand Total" . ",";
                            $contents .= "$grqty" . ",";
                            $contents .= "" . ",";
                            $contents .= "$gramt" . "\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;
    }
}
?>