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

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

$maindb = 'saletran';
$zroot = $_SESSION["zdbroot"];

$formname = 'Party + Itemwise Register';
$file = 'tipitemreg_fr.php';


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



    if (!empty($_POST['product'])) {
        foreach ($_POST['product'] as $cucheck1) {
            $ccheck1[$ccount1++] .= "OR `itemcode` = '" . $cucheck1 . "' ";
        }
        foreach ($ccheck1 as $t => $cusvalues1) {
            $cuscheck1 .= $cusvalues1;
        }
        $prodcond = " AND (`itemcode` = '' " . $cuscheck1 . " )";
    }


    $party = $_POST["party"];


    if (!empty($party)) {
        $partycond = " AND `party` = '$party' ";
    }
    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));



    $mainqry = mysqli_query($conn, "SELECT  `itemcode`,  `vouchno`, `gram`, `unit`, `hsncode`, `barcode`, `vouchdt`, `vouchno`, 
                            SUM(`qty`) as `qty`, SUM(`amount`) AS `amount`, SUM(`discamt`) as `discamt`,   SUM(cgstamt+sgstamt+igstamt) as `gst`, SUM(`total`) as `netamt`,
                            DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as `mydt` FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' and `vouchno` IN(SELECT `vouchno` FROM `salemain` where `party` = '$party') $prodcond group by itemcode
                            UNION ALL
                            SELECT  `itemcode`,  `vouchno`, `gram`, `unit`, `hsncode`, `barcode`, `vouchdt`, `vouchno`, 
                            SUM(`qty`) as `qty`, SUM(`amount`) AS `amount`, SUM(`discamt`) as `discamt`,   SUM(cgstamt+sgstamt+igstamt) as `gst`, SUM(`total`) as `netamt`,
                            DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as `mydt` FROM `deextran` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' and `vouchno` IN(SELECT `vouchno` FROM `deexmain` where `party` = '$party') $prodcond group by itemcode")or die(mysqli_error($conn));

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

    $getno = mysqli_num_rows($mainqry);

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #26A69A;
        }
        .dth{
            background:#BABFC2;
        }
        .colr{
            background: #26A69A;
            font-size: 17px;
        }
        table tr td{
            font-size: 14px;
        }

    </style>
    </head>
    <body>
        <div class="container">            
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">

                    <div class="col-lg-12 col-md-12" style="background-color:#D5F3EA; color:black;padding-top:7px">
                        <label>Party : <b><?php Echo $fparty['name'] ?></b></label></div>
                    <table style="background: white;width: 100%" class="table table-condensed table-striped table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th width='4%' >Sr</th>
                                <th width='8%' >Item Code</th>
                                <th width='35%' >Name</th>
                                <th width='8%' >Grammage</th>
                                <th width='5%' >Unit</th>
                                <th width='8%' >Hsn Code</th>
                                <th width='6%' class="text-right">Qty.</th>
                                <th width='9%' class="text-right">Amt.</th>
                                <th width='6%' class="text-right">Discount</th>
                                <th width='6%' class="text-right">GST</th>
                                <th width='7%' class="text-right">Net Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totqty = $totamt = $totdisc = $totgst = $totnet = 0;
//print table($mainqry);
                            while ($fmain = mysqli_fetch_array($mainqry)) {
                                
                                // browse($fmain['vouchno']);
                                $itemcode = $fmain['itemcode'];

                                $gitem = mysqli_query($conn, "SELECT  name FROM product WHERE prodcode = '$itemcode' ")or die(mysqli_error($conn));
                                $fitem = mysqli_fetch_array($gitem);

                                $itemname = $fitem['name'];
                                $gram = $fmain['gram'];
                                $unit = $fmain['unit'];
                                $hsncode = $fmain['hsncode'];
                                $qty = $fmain['qty'];
                                $amount = $fmain['amount'];
                                $discamt = $fmain['discamt'];
                                $gst = $fmain['gst'];
                                $netamt = $fmain['netamt'];

                                $ctr++;
                                Echo"<tr>";
                                Echo"<td>$ctr</td>";
                                Echo"<td>$itemcode</td>";
                                Echo"<td>$itemname</td>";
                                Echo"<td>$gram</td>";
                                Echo"<td>$unit</td>";
                                Echo"<td>$hsncode</td>";
                                Echo"<td class='text-right'>$qty</td>";
                                Echo"<td class='text-right'>$amount</td>";
                                Echo"<td class='text-right'>$discamt</td>";
                                Echo"<td class='text-right'>$gst</td>";
                                Echo"<td class='text-right'>$netamt</td>";
                                Echo"</tr>";

                                $totqty = $totqty + $fmain['qty'];
                                $totamt = $totamt + $fmain['amount'];
                                $totdisc = $totdisc + $fmain['discamt'];
                                $totgst = $totgst + $fmain['gst'];
                                $totnet = $totnet + $fmain['netamt'];

//                                if ($_POST['dtype'] == 2) {
//
//                                    echo "<tr style='height:10px;'>";
//                                    echo "<td colspan='25'>";
//                                    echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
//                                    echo "<thead>";
//                                    echo "<tr class='mySearchTr2'>";
//                                    echo "<th style='width:5%;' class='dth'>Sr</th>";
//                                    echo "<th style='width:7%;' class='dth'>Voucher No.</th>";
//                                    echo "<th style='width:7%;' class='dth'>Date</th>";
//                                    echo "<th style='width:9%;' class='dth'>Qty./th>";
//                                    echo "<th style='width:9%;' class='dth'>Rate/th>";
//                                    echo "</tr>";
//
//                                    echo "<tr>";
//                                    echo "<td>$ctr2</td>";
//                                    echo "<td>$barcode</td>";
//                                    echo "<td>$itemcode</td>";
//                                    echo "<td>$itemname</td>";
//                                    echo "<td>$hsncode</td>";
//                                    echo "<td>$gram</td>";
//                                    echo "<td>$unit</td>";
//                                    echo "<td class='text-right'>$mrp</td>";
//                                    echo "<td>$batch</td>";
//                                    echo "<td class='text-right'>$qty</td>";
//                                    echo "<td class='text-right'>$rate</td>";
//                                    echo "</tr>";
//                                }
                            }

                            Echo "<tr>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='text-right colr'><b>Total</b></td>";
                            Echo "<td class='numeric text-right colr'><b>$totqty</b></td>";
                            Echo "<td class='numeric text-right colr'><b>$totamt</b></td>";
                            Echo "<td class='numeric text-right colr'><b>$totdisc</b></td>";
                            Echo "<td class='numeric text-right colr'><b>$totgst</b></td>";
                            Echo "<td class='numeric text-right colr'><b>$totnet</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
    <?php include("includes/linksfoot.php");
    ?>
    </body>
    </html>
        <?php
    }


    if (isset($_POST["excel"])) {
        $party = $_POST["party"];
        $product = $_POST["product"];

        if (!empty($party)) {
            $partycond = " AND `party` = '$party' ";
        }
        if (!empty($product)) {
            $prodcond = " AND `itemcode` = '$product' ";
        }
        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));



        $mainqry = mysqli_query($conn, "SELECT  `itemcode`, `itemname`, `vouchno`, `gram`, `unit`, `hsncode`, `barcode`, `vouchdt`, `vouchno`, 
                            SUM(`qty`) as `qty`, SUM(`amount`) AS `amount`, SUM(`discamt`) as `discamt`,   SUM(cgstamt+sgstamt+igstamt) as `gst`, SUM(`total`) as `netamt`,
                            DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as `mydt` FROM `saletran` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' and `vouchno` IN(SELECT `vouchno` FROM `salemain` where `party` = '$party') $prodcond group by itemcode")or die(mysqli_error($conn));

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


        $contents .= "Sr.,Item Code, Item Name, Grammage, Unit, Hsn Code, Qty., Amt., Discount, GST, Net Amount";

        $ctr = 0;
        $totqty = $totamt = $totdisc = $totgst = $totnet = 0;

        while ($fmain = mysqli_fetch_array($mainqry)) {

            $ctr++;

            $contents .= $ctr . ",";
            $contents .= $fmain['itemcode'] . ",";
            $contents .= $fmain['itemname'] . ",";
            $contents .= $fmain['gram'] . ",";
            $contents .= $fmain['unit'] . ",";
            $contents .= $fmain['hsncode'] . ",";
            $contents .= $fmain['qty'] . ",";
            $contents .= $fmain['amount'] . ",";
            $contents .= $fmain['discamt'] . ",";
            $contents .= $fmain['gst'] . ",";
            $contents .= $fmain['netamt'] . "\n";


            $totqty = $totqty + $fmain['qty'];
            $totamt = $totamt + $fmain['amount'];
            $totdisc = $totdisc + $fmain['discamt'];
            $totgst = $totgst + $fmain['gst'];
            $totnet = $totnet + $fmain['netamt'];
        }

        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= "Total" . ",";
        $contents .= $totqty . ",";
        $contents .= $totamt . ",";
        $contents .= $totdisc . ",";
        $contents .= $totgst . ",";
        $contents .= $totnet . "\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;
        }
    }
    ?>