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

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

$formname = 'Sale Item Group Wise';
$file = 'saleitemgrp_fr.php';
$maindb = 'salemain';
$trandb = 'saletran';






if (isset($_POST["view"])) {
    $cust = $_POST["cust"];
    $itemgrp = $_POST["itemgrp"];

    $custcond = "";
    $custcond1 = "";
    $itemgrpcond = "";

    if (!empty($_POST["cust"])) {
        $custcond = " AND `party` = $cust ";
        $custcond1 = " AND a.`party` = $cust ";
    }

    if (!empty($_POST["itemgrp"])) {
        $itemgrpcond = " AND b.`itemgrp` = $itemgrp ";
    }

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));

    $getmain = mysqli_query($conn, "SELECT a.`item`, b.`itemgrp` FROM $trandb a, item b WHERE a.`item` = b.`code` and 
                            a.`vouchdt`>= '$mfrmdate'  AND  a.`vouchdt` <='$mtodate'  $itemgrpcond and vouchno in (select vouchno from 
                            `$maindb` where party != '' $custcond ) group by b.itemgrp order by b.itemgrp") or die(mysqli_error($conn));
    $nos = mysqli_num_rows($getmain);

    include("includes/linkshead.php");
    ?>
    <style>
        @media print
        {    
            .no-print, .no-print *
            {
                display: none !important;
            }
        }
    </style>
    </head>
    <body>
        <div class="container">




            <?php include("includes/head_rep.php"); ?>

            <?php
            $gtott = 0;
            $gtqty = 0;
            $gtrate = 0;
            while ($rowm = mysqli_fetch_array($getmain)) {
                $igrpcode = $rowm[itemgrp];

                $qryname = mysqli_query($conn, "SELECT name FROM `itemgrp` WHERE code = '$igrpcode' ") or die(mysqli_error($conn));
                $fetgrpname = mysqli_fetch_array($qryname);
                $itemgrpname = $fetgrpname[name];

                $get = mysqli_query($conn, "SELECT DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y') as  mydt , a.`party`, a.`vouchno`, b.`qty`, b.`rate`, b.`amount`, b.`item` 
                                    FROM $maindb a, $trandb b  WHERE  a.`vouchdt`>= '$mfrmdate'  AND  a.`vouchdt` <='$mtodate'
                                    AND a.`vouchno` = b.`vouchno` $custcond1 and 
                                    b.item in (select code from `item` where `itemgrp` = '$igrpcode') order by a.vouchdt, a.vouchno") or die(mysqli_error($conn));
                $getno = mysqli_num_rows($get);
                if ($getno > 0) {
                    ?>
                    <div class = "row">
                        <div class = "col-lg-6 col-sm-6">
                            <label class = "label-control">Item Group -  <?Php echo $itemgrpname; ?></label>
                        </div>
                    </div>
                    <div class="row">
                        <table  style="width: 100%" class="table table-bordered  table-hover table-striped ">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:5%; background-color: #78909C;">Sr.</th>
                                    <th style="width:10%; background-color: #78909C;">Date</th>
                                    <th style="width:25%; background-color: #78909C;">Customer</th>
                                    <th style="width:20%; background-color: #78909C;">Item Name</th>
                                    <th style="width:10%; background-color: #78909C;">Bill No.</th>
                                    <th style="width:10%; background-color: #78909C; text-align: right;">Qty.</th>
                                    <th style="width:10%; background-color: #78909C; text-align: right;">Rate</th>
                                    <th style="width:10%; background-color: #78909C; text-align: right;">Bill Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $tqty = 0;
                                $trate = 0;
                                $totamt = 0;
                                while ($row = mysqli_fetch_array($get)) {

                                    $getparty = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE  `accode`='$row[party]' ORDER BY `name`") or die(mysqli_error($conn));
                                    $sparty = mysqli_fetch_array($getparty);

                                    $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM item Where `code`='$row[item]'") or die(mysqli_error($conn));
                                    $item = mysqli_fetch_array($getitem);

                                    $ctr++;
                                    echo "<tr> ";
                                    echo "<td  >$ctr</td>";
                                    echo "<td  >$row[mydt]</td>";
                                    echo "<td  >$sparty[name]</td>";
                                    echo "<td  >$item[name]</td>";
                                    echo "<td  >$row[vouchno]</td>";
                                    echo "<td class='text-right' >$row[qty]</td>";
                                    echo "<td class='text-right' >$row[rate]</td>";
                                    echo "<td class='text-right' >$row[amount]</td>";
                                    echo "</tr>";

                                    $tqty = $tqty + $row[qty];
                                    $trate = $trate + $row[rate];
                                    $totamt = $totamt + $row[amount];
                                }
                                echo "<tr> ";
                                echo "<td  ></td>";
                                echo "<td  ></td>";
                                echo "<td  ></td>";
                                echo "<td  ></td>";
                                echo "<td class='text-right' style=' color: #1ABC9C;'><b>Total</b></td>";
                                echo "<td class='text-right' style=' color: #1ABC9C;'><b>$tqty</b></td>";
                                echo "<td class='text-right' style=' color: #1ABC9C;' ><b>$trate</b></td>";
                                echo "<td class='text-right' style=' color: #1ABC9C;' ><b>$totamt</b></td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                    <?php
                }
                $gtqty = $gtqty + $tqty;
                $gtrate = $gtrate + $trate;
                $gtott = $gtott + $totamt;
            }
            ?>
            <div class="row">
                <?php
                echo "<table style='width: 100%' class='table table-bordered  table-hover table-striped '> ";
                echo "<thead>";
                echo "<tr> ";
                echo "<td style='width:5%;' ></td>";
                echo "<td style='width:10%;' ></td>";
                echo "<td style='width:30%;' ></td>";
                echo "<td style='width:15%;' ></td>";
                echo "<td class='text-right' style=' color: red;'width:10%;><b>Grand Total</b></td>";
                echo "<td class='text-right' style=' color: red;width:10%;'><b>$gtqty</b></td>";
                echo "<td class='text-right' style=' color: red; width:10%;' ><b>$gtrate</b></td>";
                echo "<td class='text-right' style=' color: red;width:10%;' ><b>$gtott</b></td>";
                echo "</tr>";
                echo "</thead>";
                echo "</table>";
                ?> 
            </div>
        </div>
    </body>
    </html>
    <?php
    include("includes/linksfoot.php");
    ?>
    <?php
}
if (isset($_POST["excel"])) {
    $cust = $_POST["cust"];
    $itemgrp = $_POST["itemgrp"];

    $custcond = "";
    $custcond1 = "";
    $itemgrpcond = "";

    if (!empty($_POST["cust"])) {
        $custcond = " AND `party` = $cust ";
        $custcond1 = " AND a.`party` = $cust ";
    }

    if (!empty($_POST["itemgrp"])) {
        $itemgrpcond = " AND b.`itemgrp` = $itemgrp ";
    }

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);


    $getmain = mysqli_query($conn, "SELECT a.`item`, b.`itemgrp` FROM $trandb a, item b WHERE a.`item` = b.`code` and 
                            a.`vouchdt`>= '$mfrmdate'  AND  a.`vouchdt` <='$mtodate'  $itemgrpcond and vouchno in (select vouchno from 
                            `$maindb` where party != '' $custcond ) group by b.itemgrp order by b.itemgrp") or die(mysqli_error($conn));
    $nos = mysqli_num_rows($getmain);

    $gtott = 0;
    $gtqty = 0;
    $gtrate = 0;
    while ($rowm = mysqli_fetch_array($getmain)) {
        $igrpcode = $rowm[itemgrp];

        $qryname = mysqli_query($conn, "SELECT name FROM `itemgrp` WHERE code = '$igrpcode' ") or die(mysqli_error($conn));
        $fetgrpname = mysqli_fetch_array($qryname);
        $itemgrpname = $fetgrpname[name];

        $get = mysqli_query($conn, "SELECT DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y') as  mydt , a.`party`, a.`vouchno`, b.`qty`, b.`rate`, b.`amount`, b.`itemcode` 
                                    FROM $maindb a, $trandb b  WHERE  a.`vouchdt`>= '$mfrmdate'  AND  a.`vouchdt` <='$mtodate'
                                    AND a.`vouchno` = b.`vouchno` $custcond1 and 
                                    b.itemcode in (select code from `item` where `itemgrp` = '$igrpcode') order by a.vouchdt, a.vouchno") or die(mysqli_error($conn));
        $getno = mysqli_num_rows($get);
        if ($getno > 0) {
            $contents.="Item Group " . ",";
            $contents.=$itemgrpname . "\n";

            $contents.="Sr.,Date,Customer,Item Name,Bill No.,Qty.,Rate,Bill Amount\n";

            $ctr = 0;
            $tqty = 0;
            $trate = 0;
            $totamt = 0;
            while ($row = mysqli_fetch_array($get)) {

                $getparty = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE  `accode`='$row[party]' ORDER BY `name`") or die(mysqli_error($conn));
                $sparty = mysqli_fetch_array($getparty);

                $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM item Where `code`='$row[itemcode]'") or die(mysqli_error($conn));
                $item = mysqli_fetch_array($getitem);

                $ctr++;
                $pdate = $row[mydt];
                $ppartyname = $sparty[name];
                $pitemname = $item[name];
                $pvouchno = $row[vouchno];
                $pqty = $row[qty];
                $prate = $row[rate];
                $pamount = $row[amount];

                $contents.=$ctr . ",";
                $contents.=$pdate . ",";
                $contents.=$ppartyname . ",";
                $contents.=$pitemname . ",";
                $contents.=$pvouchno . ",";
                $contents.=$pqty . ",";
                $contents.=$prate . ",";
                $contents.=$pamount . "\n";

                $tqty = $tqty + $row[qty];
                $trate = $trate + $row[rate];
                $totamt = $totamt + $row[amount];
            }
            $contents.=" " . ",";
            $contents.=" " . ",";
            $contents.=" " . ",";
            $contents.=" " . ",";
            $contents.="Total" . ",";
            $contents.=$tqty . ",";
            $contents.=$trate . ",";
            $contents.=$totamt . "\n";
        }
        $gtqty = $gtqty + $tqty;
        $gtrate = $gtrate + $trate;
        $gtott = $gtott + $totamt;
    }
    $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.="Grand Total" . ",";
    $contents.=$gtqty . ",";
    $contents.=$gtrate . ",";
    $contents.=$gtott . "\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;
    }
}
?>