<?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 = 'inwmain';
$trandb = 'inwtran';

$formname = 'Inward Register';
$zroot = $_SESSION["zdir"];
$file = 'tiinwdreg_fr.php';


$prodcode = $_POST["prodcode"];
$product = $_POST["product"];
$godown = $_POST["godown"];
$reptype = $_POST["type"];

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

$prodcond = '';
if (!empty($prodcode)) {
    $prodcond = " AND b.`itemcode`='$prodcode' ";
    $prodcond2 = " AND `itemcode`='$prodcode' ";
}

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


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

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

    </style>
    </head>
    <body>
        <div class="container">
            <?php
            include("includes/head_rep2.php");
            $itqty = 0;

            if ($_POST['dtype'] != 3) {
                if ($reptype == "" || $reptype == "L") {

                    $mainqryl = mysqli_query($conn, "SELECT a.*, b.itemcode, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt
                         FROM `inwmain` a, `inwtran` b WHERE a.vouchno = b.vouchno and  
                         a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $prodcond 
                         $godowncond group by a.vouchno ")or die(mysqli_error($conn));

//                    print table ($mainqry1);
                    $getno = mysqli_num_rows($mainqryl);

                    if ($getno > 0) {
                        ?>

                        <div class="row">
                            <div class="col-lg-12 pad">
                                <table class="table table-condesed table-bordered">
                                    <tr class="mySearchTr">
                                        <th width="100%" class="h4" style="color:white"><b>Local - Inward Register</b></th>
                                    </tr>
                                    <tr>
                                        <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                            Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno; ?></b>
                                        </td>
                                    </tr>

                                </table>
                                <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">

                                    <tr class="mySearchTr">
                                        <th>Sr</th>
                                        <th>Voucher</th>
                                        <th>Date</th>
                                        <th>Godown</th>
                                        <th style="text-align:right">Total Qty.</th>
                                    </tr>

                                    <tbody>
                                        <?php
                                        $ctr = 0;
                                        $totqty = 0;

                                        while ($pmain = mysqli_fetch_array($mainqryl)) {

                                            $ggodwn = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code` = '$pmain[godown]' ")or die(mysqli_error($conn));
                                            $godwn = mysqli_fetch_array($ggodwn);

                                            $ctr++;
                                            Echo "<tr id='boldfont'>";
                                            Echo "<td>$ctr</td>";
                                            Echo "<td>$pmain[vouchno]</td>";
                                            Echo "<td>$pmain[mydt]</td>";
                                            Echo "<td>$godwn[gcode]</td>";
                                            if ($prodcond == '') {
                                                Echo "<td style='text-align:right;'>$pmain[totqty]</td>";
                                                $totqty = $totqty + $pmain[totqty];
                                            } else {
                                                Echo "<td style='text-align:right;'></td>";
                                            }
                                            Echo "</tr>";


                                            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-bordered'>";
                                                Echo "<tr class='mySearchTr2'>";
                                                Echo "<th style='width:5%;'>Sr</th>";
                                                Echo "<th style='width:8%;'>Barcode</th>";
                                                Echo "<th style='width:8%;' class=''>Product Code</th>";
                                                Echo "<th style='width:25%;' class=''>Product Name</th>";
                                                Echo "<th style='width:5%;' class=''>HSN Code</th>";
                                                Echo "<th style='width:5%;' class=''>Gram</th>";
                                                Echo "<th style='width:5%;' class=''>Unit</th>";
                                                Echo "<th style='width:5%;' class='text-right'>Batch</th>";
                                                Echo "<th style='width:5%;' class='text-right'>Qty.</th>";
                                                Echo "<th style='width:20%;' class=''>Remark</th>";
                                                Echo "</tr>";


                                                $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' $prodcond2 ")or die(mysqli_error($conn));
                                                $ctr2 = 0;
                                                $trtotqty = 0;
                                                while ($tran = mysqli_fetch_array($gettran)) {
                                                    $gitemname = mysqli_query($conn, "SELECT `code`,`name` FROM `product` WHERE `prodcode`='$tran[itemcode]'")or die(mysqli_error($conn));
                                                    $fitemname = mysqli_fetch_array($gitemname);


                                                    $ctr2++;
                                                    Echo "<tr>";
                                                    Echo "<td>$ctr2</td>";
                                                    Echo "<td>$tran[barcode]</td>";
                                                    Echo "<td>$tran[itemcode]</td>";
                                                    Echo "<td>$fitemname[name]</td>";
                                                    Echo "<td>$tran[hsncode]</td>";
                                                    Echo "<td class=''>$tran[gram]</td>";
                                                    Echo "<td>$tran[unit]</td>";
                                                    Echo "<td>$tran[batch]</td>";
                                                    Echo "<td class='text-right'>$tran[qty]</td>";
                                                    Echo "<td>$tran[remark]</td>";
                                                    Echo "</tr>";

                                                    $trtotqty = $trtotqty + $tran['qty'];
                                                    $itqty = $itqty + $tran['qty'];
                                                }
                                                Echo "<tr class='mySearchTr2'>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td class='text-right'>Total</td>";
                                                Echo "<td class='text-right'>$trtotqty</td>";
                                                Echo "<td></td>";
                                                Echo "</tr>";

                                                Echo "</table>";
                                                Echo "</td>";
                                                $trtotqty = 0;
                                            }
                                        }

                                        Echo "<tr>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td class='text-right'><b>Total</b></td>";

                                        if ($prodcond == '') {
                                            Echo "<td style='text-align:right'><b>$totqty</b></td>";
                                        } else {
                                            Echo "<td style='text-align:right'><b>$itqty</b></td>";
                                        }
                                        Echo "</tr>";
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <?php
                    }
                }

                if ($reptype == "" || $reptype == "E") {

                    $trtotqty = 0;

                    $mainqrye = mysqli_query($conn, "SELECT a.*, b.itemcode, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt
                                         FROM `inwexpmain` a, `inwexptran` b WHERE a.vouchno = b.vouchno and  
                                         a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $prodcond 
                                         $godowncond group by vouchno   ")or die(mysqli_error($conn));
                    $getno2 = mysqli_num_rows($mainqrye);

                    if ($getno2 > 0) {
                        ?>

                        <div class="row">
                            <div class="col-lg-12 pad">
                                <table class="table table-condesed table-bordered">
                                    <tr class="mySearchTr">
                                        <th width="100%" class="h4" style="color:white"><b>Export - Inward Register</b></th>
                                    </tr>
                                    <tr>
                                        <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                            Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                        </td>
                                    </tr>

                                </table>
                                <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">

                                    <tr class="mySearchTr">
                                        <th>Sr</th>
                                        <th>Voucher</th>
                                        <th>Date</th>
                                        <th>Godown</th>
                                        <th style="text-align:right">Total Qty.</th>
                                    </tr>

                                    <tbody>
                                        <?php
                                        $ctr = 0;
                                        $totqty = 0;

                                        while ($pmain = mysqli_fetch_array($mainqrye)) {

                                            $ggodwn = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code` = '$pmain[godown]' ")or die(mysqli_error($conn));
                                            $godwn = mysqli_fetch_array($ggodwn);

                                            $ctr++;
                                            Echo "<tr id='boldfont'>";
                                            Echo "<td>$ctr</td>";
                                            Echo "<td>$pmain[vouchno]</td>";
                                            Echo "<td>$pmain[mydt]</td>";
                                            Echo "<td>$godwn[gcode]</td>";
                                            if ($prodcond == '') {
                                                Echo "<td style='text-align:right;'>$pmain[totqty]</td>";
                                                $totqty = $totqty + $pmain[totqty];
                                            } else {
                                                Echo "<td style='text-align:right;'></td>";
                                            }
                                            Echo "</tr>";


                                            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-bordered'>";
                                                Echo "<tr class='mySearchTr2'>";
                                                Echo "<th style='width:5%;'>Sr</th>";
                                                Echo "<th style='width:10%;'>Barcode</th>";
                                                Echo "<th style='width:10%;' class=''>Item Code</th>";
                                                Echo "<th style='width:15%;' class=''>Item Name</th>";
                                                Echo "<th style='width:10%;' class=''>HSN Code</th>";
                                                Echo "<th style='width:8%;' class=''>Gram</th>";
                                                Echo "<th style='width:8%;' class=''>Unit</th>";
                                                Echo "<th style='width:8%;' class='text-right'>Batch</th>";
                                                Echo "<th style='width:8%;' class='text-right'>Qty.</th>";
                                                Echo "<th style='width:20%;' class=''>Remark</th>";
                                                Echo "</tr>";

                                                $gettran = mysqli_query($conn, "SELECT * FROM `inwexptran` WHERE `vouchno`='$pmain[vouchno]'$prodcond2")or die(mysqli_error($conn));
                                                $ctr2 = 0;
                                                $trtotqty = 0;
                                                while ($tran = mysqli_fetch_array($gettran)) {

                                                    $gitemname = mysqli_query($conn, "SELECT `code`,`name` FROM `product` WHERE `prodcode`='$tran[itemcode]'")or die(mysqli_error($conn));
                                                    $fitemname = mysqli_fetch_array($gitemname);
                                                    $itemname = $fitemname['name'];

                                                    $ctr2++;
                                                    Echo "<tr>";
                                                    Echo "<td>$ctr2</td>";
                                                    Echo "<td>$tran[barcode]</td>";
                                                    Echo "<td>$tran[itemcode]</td>";
                                                    Echo "<td>$fitemname[name]</td>";
                                                    Echo "<td>$tran[hsncode]</td>";
                                                    Echo "<td class=''>$tran[gram]</td>";
                                                    Echo "<td>$tran[unit]</td>";
                                                    Echo "<td>$tran[batch]</td>";
                                                    Echo "<td class='text-right'>$tran[qty]</td>";
                                                    Echo "<td>$tran[remark]</td>";
                                                    Echo "</tr>";

                                                    $trtotqty = $trtotqty + $tran['qty'];
                                                    $itqty    = $itqty    + $tran['qty'];
                                                }
                                                Echo "<tr class='mySearchTr2'>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td></td>";
                                                Echo "<td class='text-right'>Total</td>";
                                                Echo "<td class='text-right'>$trtotqty</td>";
                                                Echo "<td></td>";
                                                Echo "</tr>";

                                                Echo "</table>";
                                                Echo "</td>";
                                                $trtotqty = 0;
                                            }
                                        }

                                        Echo "<tr>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td class='text-right'><b>Total</b></td>";
                                        if ($prodcond == '') {
                                            Echo "<td style='text-align:right'><b>$totqty</b></td>";
                                        } else {
                                            Echo "<td style='text-align:right'><b>$itqty</b></td>";
                                        }
                                        Echo "</tr>";
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        <?php
                    }
                }
            } else {

                $cond = '';
                if ($reptype == "L") {

                    $maindb2 = "inwmain";
                    $trandb2 = "inwtran";
                    $report = "Local";
                } else if ($reptype == "E") {

                    $maindb2 = "inwexpmain";
                    $trandb2 = "inwexptran";
                    $cond = " and a.inwtype = 'DI' ";
                    $inwtype = ", a.inwtype ";
                    $report = "Export";
                } else if ($reptype == "D") {

                    $maindb2 = "inwexpmain";
                    $trandb2 = "inwexptran";
                    $cond = " and a.inwtype = 'DE' ";
                    $inwtype = ", a.inwtype ";
                    $report = "Deemed";
                }


                if ($reptype == "") {

                    $itemwiseqry = mysqli_query($conn, "SELECT itemcode, godown , DATE_FORMAT(vouchdt , '%d-%m-%Y') AS mydt, vouchno , qty, unit from transacn where 
                                                vouchno != '' and (source = 'IN' or source = 'INEX') $prodcond2 $godowncond  order by  itemcode")or die(mysqli_error($conn));
                    $report = "All";
                } else {

                    $itemwiseqry = mysqli_query($conn, "SELECT a.godown , b.vouchno, DATE_FORMAT(b.vouchdt , '%d-%m-%Y') AS mydt,
                                                b.itemcode, b.hsncode, b.qty, b.unit $inwtype from $maindb2 a,
                                                $trandb2 b where a.vouchno = b.vouchno and  b.vouchdt >= '$mfrmdate' and 
                                                b.`vouchdt` <= '$mtodate' $prodcond $godowncond $cond order by b.itemcode, b.vouchdt")or die(mysqli_error($conn));
                }
                ?>

                <div class="row">
                    <div class="col-lg-12 pad">
                        <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                            <tr class="mySearchTr"><th><h4><b>Item Inward : <?= $report; ?></b></h4></th></tr>
                        </table>
                        <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                            <tr class="mySearchTr">
                                <th>Sr</th>
                                <th>Code</th>
                                <th>Product</th>
                                <th>Unit</th>
                                <th>Date</th>
                                <th>Voucher</th>
                                <th style="text-align:right">Qty.</th>
                            </tr>
                            <?
                            $ctr3 = 0;
                            $totqty2 = 0;
                            $totqty3 = 0;
                            while ($fitemwise = mysqli_fetch_array($itemwiseqry)) {

                                $abc = $itemname;

                                $gitemname = mysqli_query($conn, "SELECT `name` FROM `product` WHERE `prodcode`='$fitemwise[itemcode]'")or die(mysqli_error($conn));
                                $fitemname = mysqli_fetch_array($gitemname);
                                $itemname = $fitemname['name'];

                                if ($itemname != $abc) {
                                    if ($ctr3 != 0) {
                                        ?>
                                        <tr>
                                            <td class="text-right" colspan="6"><b>Item Total</b></td>
                                            <td class="text-right"><b><?= $totqty2; ?></b></td>
                                        </tr>

                                        <?
                                        $totqty2 = 0;
                                    }
                                }

                                $ctr3++;
                                ?>
                                <tr class="bgwhite">
                                    <td><?= $ctr3 ?></td>
                                    <td><?= $fitemwise['itemcode']; ?></td>
                                    <td><?= $itemname; ?></td>
                                    <td><?= $fitemwise['unit'] ?></td>
                                    <td><?= $fitemwise['mydt'] ?></td>
                                    <td><?= $fitemwise['vouchno'] ?></td>
                                    <td style="text-align:right"><?= $fitemwise['qty']; ?></td>
                                </tr>
                                <?
                                $totqty2 = $totqty2 + $fitemwise['qty'];
                                $totqty3 = $totqty3 + $fitemwise['qty'];
//                                browse($fitemwise['qty']);
//                                browse($totqty3);
                            }
                            ?>                        

                            <tr class="bgwhite">
                                <td colspan="6" style="text-align:right; font-size: 20px">Total</td>
                                <td style="text-align:right; font-size: 20px"><b><?= $totqty3; ?></b></td>
                            </tr>

                        </table>
                    </div>
                </div>


                <?
            }

            include("includes/linksfoot.php");
            ?>
        </div>
    </body>
    </html>
    <?php
}

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

    $itqty = 0;
    if ($_POST['dtype'] == 3) {


        $cond = '';
        if ($reptype == "L") {

            $maindb2 = "inwmain";
            $trandb2 = "inwtran";
            $report = "Local";
        } else if ($reptype == "E") {

            $maindb2 = "inwexpmain";
            $trandb2 = "inwexptran";
            $cond = " and a.inwtype = 'DI' ";
            $inwtype = ", a.inwtype ";
            $report = "Export";
        } else if ($reptype == "D") {

            $maindb2 = "inwexpmain";
            $trandb2 = "inwexptran";
            $cond = " and a.inwtype = 'DE' ";
            $inwtype = ", a.inwtype ";
            $report = "Deemed";
        }


        if ($reptype == "") {

            $itemwiseqry = mysqli_query($conn, "SELECT itemcode, godown , DATE_FORMAT(vouchdt , '%d-%m-%Y') AS mydt, vouchno , qty, unit from transacn where 
                                                vouchno != '' and (source = 'IN' or source = 'INEX') $prodcond2 $godowncond  order by  itemcode")or die(mysqli_error($conn));
            $report = "All";
        } else {

            $itemwiseqry = mysqli_query($conn, "SELECT a.godown , b.vouchno, DATE_FORMAT(b.vouchdt , '%d-%m-%Y') AS mydt,
                                                b.itemcode, b.hsncode, b.qty, b.unit $inwtype from $maindb2 a,
                                                $trandb2 b where a.vouchno = b.vouchno and  b.vouchdt >= '$mfrmdate' and 
                                                b.`vouchdt` <= '$mtodate' $prodcond $godowncond $cond order by b.itemcode, b.vouchdt")or die(mysqli_error($conn));
        }


        $contents .= "Item Inward : $report \n";


        $contents .= "Sr., Code, Product, Unit, Date, Voucher, Qty.\n";


        $ctr3 = 0;
        $totqty2 = 0;
        $totqty3 = 0;
        while ($fitemwise = mysqli_fetch_array($itemwiseqry)) {

            $abc = $itemname;

            $gitemname = mysqli_query($conn, "SELECT `name` FROM `product` WHERE `prodcode`='$fitemwise[itemcode]'")or die(mysqli_error($conn));
            $fitemname = mysqli_fetch_array($gitemname);
            $itemname = $fitemname['name'];

            if ($itemname != $abc) {
                if ($ctr3 != 0) {


                    $contents .= ",,,,,Item Total" . ",";
                    $contents .= $totqty2 . "\n";
                    $totqty2 = 0;
                }
            }

            $ctr3++;

            $contents .= $ctr3 . ",";
            $contents .= $fitemwise['itemcode'] . ",";
            $contents .= $itemname . ",";
            $contents .= $fitemwise['unit'] . ",";
            $contents .= $fitemwise['mydt'] . ",";
            $contents .= $fitemwise['vouchno'] . ",";
            $contents .= $fitemwise['qty'] . "\n";


            $totqty2 = $totqty2 + $fitemwise['qty'];
            $totqty3 = $totqty3 + $fitemwise['qty'];
        }

        $contents .= ",,,,,Item Total" . ",";
        $contents .= $totqty3 . "\n";
    } else {
        if ($reptype == "" || $reptype == "L") {

            $mainqryl = mysqli_query($conn, "SELECT a.*, b.itemcode, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt
                                    FROM `inwmain` a, `inwtran` b WHERE a.vouchno = b.vouchno and  
                                    a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $prodcond 
                                    $godowncond group by vouchno   ")or die(mysqli_error($conn));
            $getno = mysqli_num_rows($mainqryl);

            if ($getno > 0) {

                $contents .= "Local - Inward Register\n";

                $contents .= "Total No. of Record : $getno\n";

                $contents .= "Sr., Voucher, Date, Godown, Total Qty.\n";

                $ctr = 0;
                $totqty = 0;

                while ($pmain = mysqli_fetch_array($mainqryl)) {

                    $ggodwn = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code` = '$pmain[godown]' ")or die(mysqli_error($conn));
                    $godwn = mysqli_fetch_array($ggodwn);

                    $ctr++;
                    $contents .= "$ctr" . ",";
                    $contents .= "$pmain[vouchno]" . ",";
                    $contents .= "$pmain[mydt]" . ",";
                    $contents .= "$godwn[gcode]" . ",";
                    if ($prodcond == '') {
                        $contents .= "$pmain[totqty]" . "\n";
                        $totqty = $totqty + $pmain['totqty'];
                    } else {
                        $contents .= "" . "\n";
                    }

                    if ($_POST["dtype"] == 2) {

                        $contents .= "Sr., Barcode, Product Code, Product Name, HSN Code, Gram, Unit, Batch, Qty., Remark\n";

                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' $prodcond2 ")or die(mysqli_error($conn));
                        $ctr2 = 0;
                        $trtotqty = 0;
                        while ($tran = mysqli_fetch_array($gettran)) {
                            $gitemname = mysqli_query($conn, "SELECT `code`,`name` FROM `product` WHERE `prodcode`='$tran[itemcode]'")or die(mysqli_error($conn));
                            $fitemname = mysqli_fetch_array($gitemname);


                            $ctr2++;
                            $contents .= "$ctr2" . ",";
                            $contents .= "$tran[barcode]" . ",";
                            $contents .= "$tran[itemcode]" . ",";
                            $contents .= "$fitemname[name]" . ",";
                            $contents .= "$tran[hsncode]" . ",";
                            $contents .= "$tran[gram]" . ",";
                            $contents .= "$tran[gramunit]" . ",";
                            $contents .= "$tran[batch]" . ",";
                            $contents .= "$tran[qty]" . ",";
                            $contents .= "$tran[remark]" . "\n";

                            $trtotqty = $trtotqty + $tran['qty'];
                            $itqty = $itqty + $tran['qty'];
                        }
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= "Total" . ",";
                        $contents .= "$trtotqty" . ",";
                        $contents .= " " . "\n";

                        $trtotqty = 0;
                    }
                }

                $contents .= " " . ",";
                $contents .= " " . ",";
                $contents .= " " . ",";
                $contents .= "Total" . ",";
                if ($prodcond == '') {
                    $contents .= "$totqty" . "\n";
                } else {
                    $contents .= "$itqty" . "\n";
                }
            }
        }
        if ($reptype == "" || $reptype == "E") {

            $trtotqty = 0;

            $mainqrye = mysqli_query($conn, "SELECT a.*, b.itemcode, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt
FROM `inwexpmain` a, `inwexptran` b WHERE a.vouchno = b.vouchno and  
a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $prodcond 
$godowncond group by vouchno   ")or die(mysqli_error($conn));
            $getno2 = mysqli_num_rows($mainqrye);

            if ($getno2 > 0) {

                $contents .= "Export - Inward Register\n";
                $contents .= "Total No. of Record : $getno2\n";

                $contents .= "Sr., Voucher, Date, Godown, Total Qty.\n";

                $ctr = 0;
                $totqty = 0;

                while ($pmain = mysqli_fetch_array($mainqrye)) {

                    $ggodwn = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code` = '$pmain[godown]' ")or die(mysqli_error($conn));
                    $godwn = mysqli_fetch_array($ggodwn);

                    $ctr++;
                    $contents .= "$ctr" . ",";
                    $contents .= "$pmain[vouchno]" . ",";
                    $contents .= "$pmain[mydt]" . ",";
                    $contents .= "$godwn[gcode]" . ",";
                    if ($prodcond == '') {
                        $contents .= "$pmain[totqty]" . "\n";
                        $totqty = $totqty + $pmain['totqty'];
                    } else {
                        $contents .= "" . "\n";
                    }


                    if ($_POST["dtype"] == 2) {

                        $contents .= "Sr., Barcode, Item Code, Item Name, HSN Code, Gram, Unit, Batch, Qty., Remark\n";

                        $gettran = mysqli_query($conn, "SELECT * FROM `inwexptran` WHERE `vouchno`='$pmain[vouchno]'$prodcond2")or die(mysqli_error($conn));
                        $ctr2 = 0;
                        $trtotqty = 0;
                        while ($tran = mysqli_fetch_array($gettran)) {

                            $gitemname = mysqli_query($conn, "SELECT `code`,`name` FROM `product` WHERE `prodcode`='$tran[itemcode]'")or die(mysqli_error($conn));
                            $fitemname = mysqli_fetch_array($gitemname);
                            $itemname = $fitemname['name'];

                            $ctr2++;
                            $contents .= "$ctr2" . ",";
                            $contents .= "$tran[barcode]" . ",";
                            $contents .= "$tran[itemcode]" . ",";
                            $contents .= "$fitemname[name]" . ",";
                            $contents .= "$tran[hsncode]" . ",";
                            $contents .= "$tran[gram]" . ",";
                            $contents .= "$tran[unit]" . ",";
                            $contents .= "$tran[batch]" . ",";
                            $contents .= "$tran[qty]" . ",";
                            $contents .= "$tran[remark]" . "\n";

                            $trtotqty = $trtotqty + $tran['qty'];
                            $itqty = $itqty + $tran['qty'];
                        }
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= " " . ",";
                        $contents .= "Total" . ",";
                        $contents .= "$trtotqty" . ",";
                        $contents .= " " . "\n";
                        $trtotqty = 0;
                    }
                }


                $contents .= " " . ",";
                $contents .= " " . ",";
                $contents .= " " . ",";
                $contents .= "Total" . ",";
                if ($prodcond == '') {
                    $contents .= "$totqty" . "\n";
                } else {
                    $contents .= "$itqty" . "\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;
    }
}
?>