<?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"];
    $itemsubgrp = $_POST["itemsubgrp"];

    $custcond = "";
    $custcond1 = "";
    $itemgrpcond = "";
    $itemsubgrpcond = "";
    $godown = trim($_POST["godown"]);
    $godowncond = "";
    if (!empty($_POST["godown"])) {
        $godowncond = " and `godown` = '$godown' ";
    }
    if (!empty($_POST["cust"])) {
        $custcond = " AND `party` = '$cust' ";
        $custcond1 = " AND a.`party` = '$cust' ";
    }
    if (!empty($_POST["itemsubgrp"])) {
        $itemsubgrpcond = " AND b.`itemsubgrp` = '$itemsubgrp' ";
    }

    if (!empty($_POST["itemgrp"])) {
        $itemgrpcond = " AND b.`prodgrp` = '$itemgrp' ";
//    } else {
//        browse("Item Group Cannot Be Empty");
//        header("refresh:1; saleitemgrp.php");
    }



    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));


//    die("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 a.vouchno in (select vouchno from 
//                            `$maindb` where party != '' $custcond ) group by b.itemgrp order by b.itemgrp");


    $getmain = mysqli_query($conn, "SELECT a.`itemcode`, b.`prodgrp` FROM $trandb a, product b WHERE a.`itemcode` = b.`code` and 
                            a.`vouchdt`>= '$mfrmdate'  AND  a.`vouchdt` <='$mtodate' $itemsubgrpcond $itemgrpcond $godowncond and a.vouchno in (select vouchno from 
                            `$maindb` where party != '' $custcond ) group by b.prodgrp order by b.prodgrp") 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['prodgrp'];

                $qryname = mysqli_query($conn, "SELECT name FROM `prodgrp` 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' $godowncond
                                    AND a.`vouchno` = b.`vouchno` $custcond1 and 
                                    b.itemcode in (select code from `product` where `prodgrp` = '$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 product Where `code`='$row[itemcode]'") 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 ";
    } else {
        browse("Item Group Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }

    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 a.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.`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) {

            $contents.="Item Group" . $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[item]'") or die(mysqli_error($conn));
                $item = mysqli_fetch_array($getitem);

                $ctr++;
                $contents.=$ctr . ",";
                $contents.=$row[mydt] . ",";
                $contents.=$sparty[name] . ",";
                $contents.=$item[name] . ",";
                $contents.=$row[vouchno] . ",";
                $contents.=$row[qty] . ",";
                $contents.=$row[rate] . ",";
                $contents.=$row[amount] . "\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;
    }
}
if (isset($_POST["subpdf"])) {
    $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 ";
    } else {
        browse("Item Group Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }

    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 a.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;
    require("library/fpdf/fpdf.php");

    class pdf extends fpdf {

        function Footer() {



//Position at 1.5 cm from bottom

            $this->SetY(-15);

//Arial italic 8

            $this->SetFont('Arial', 'I', 8);

//Page number

            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }

    }

    $pdf = new pdf('L', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();
    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) {
            $pdf->SetFont('Arial', 'I', 10);
            $pdf->SetTextColor(96, 130, 191);
            $pdf->SetFillColor(255, 255, 255);
            $pdf->Cell(278, 7, "Item Group - " . $itemgrpname, 1, 0, '', 1);
            $pdf->SetFont('Arial', '', 1, 0);

            $pdf->SetTextColor(0, 0, 0);
            $pdf->Ln();

            $pdf->SetFont('Arial', 'I', 10);
            $pdf->SetTextColor(255, 255, 255);
            $pdf->SetFillColor(96, 130, 191);
            $pdf->Cell(8, 7, "Sr.", 1, 0, '', 1);
            $pdf->Cell(30, 7, "Date", 1, 0, '', 1);
            $pdf->Cell(50, 7, "Customer", 1, 0, '', 1);
            $pdf->Cell(70, 7, "Item Name", 1, 0, '', 1);
            $pdf->Cell(30, 7, "Bill No.", 1, 0, '', 1);
            $pdf->Cell(30, 7, "Qty.", 1, 0, '', 1);
            $pdf->Cell(30, 7, "Rate", 1, 0, '', 1);
            $pdf->Cell(30, 7, "Bill Amount", 1, 0, '', 1);
            $pdf->SetFont('Arial', '', 1, 0);

            $pdf->SetTextColor(0, 0, 0);
            $pdf->Ln();

            $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++;
                $pdf->SetFont('Arial', '', 8);
                $pdf->Cell(8, 7, "$ctr", 1);
                $pdf->Cell(30, 7, "$row[mydt]", 1);
                $pdf->Cell(50, 7, "$sparty[name]", 1);
                $pdf->Cell(70, 7, "$item[name]", 1);
                $pdf->Cell(30, 7, "$row[vouchno]", 1);
                $pdf->Cell(30, 7, "$row[qty]", 1);
                $pdf->Cell(30, 7, "$row[rate]", 1);
                $pdf->Cell(30, 7, "$row[amount]", 1);
                $pdf->Ln();

                $tqty = $tqty + $row[qty];
                $trate = $trate + $row[rate];
                $totamt = $totamt + $row[amount];
            }
            $pdf->SetFont('Arial', 'I', 10);
            $pdf->SetTextColor(66, 244, 149);
            $pdf->SetFillColor(255, 255, 255);
            $pdf->Cell(8, 7, " ", 1, 0, '', 1);
            $pdf->Cell(30, 7, " ", 1, 0, '', 1);
            $pdf->Cell(50, 7, " ", 1, 0, '', 1);
            $pdf->Cell(70, 7, " ", 1, 0, '', 1);
            $pdf->Cell(30, 7, "Total", 1, 0, '', 1);
            $pdf->Cell(30, 7, "$tqty", 1, 0, '', 1);
            $pdf->Cell(30, 7, "$trate", 1, 0, '', 1);
            $pdf->Cell(30, 7, "$totamt", 1, 0, '', 1);
            $pdf->SetFont('Arial', '', 1, 0);

            $pdf->SetTextColor(0, 0, 0);
            $pdf->Ln();
        }
        $gtqty = $gtqty + $tqty;
        $gtrate = $gtrate + $trate;
        $gtott = $gtott + $totamt;
    }

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(255, 0, 0);
    $pdf->Cell(8, 7, " ", 1, 0, '', 1);
    $pdf->Cell(30, 7, " ", 1, 0, '', 1);
    $pdf->Cell(50, 7, " ", 1, 0, '', 1);
    $pdf->Cell(70, 7, " ", 1, 0, '', 1);
    $pdf->Cell(30, 7, "Grand Total", 1, 0, '', 1);
    $pdf->Cell(30, 7, "$gtqty", 1, 0, '', 1);
    $pdf->Cell(30, 7, "$gtrate", 1, 0, '', 1);
    $pdf->Cell(30, 7, "$gtott", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->Output("Sale Item Group.pdf", "D");
}
?>