<?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);
$zroot = $_SESSION["zdbroot"];

$maindb = 'dispmain';
$trandb = 'disptran';
$formname = 'Dispatch Register';
$file = 'midelvreg_fr.php';


if (isset($_POST["view"])) {
    $partyname = $_POST["partyname"];
    $godown = $_POST["godown"];
    $transmode = $_POST["transmode"];
    $dtype = $_POST["dtype"];

    if (!empty($godown)) {
        $godowncond = " AND `tgodwn` = '$godown' ";
    }
    if (!empty($transmode)) {
        $transcond = " AND `transmode` = '$transmode'";
    }
    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`= '$dtype' ";
    }

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


    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $transcond $godowncond ")or die(mysqli_error($conn));
    $getno = mysqli_num_rows($getpmain);

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

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

                    <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%;">Sr</th>
                                <th style="width:5%;">Voucher</th>
                                <th style="width:10%;">Date</th>
                                <th style="width:10%;">To Godown</th>
                                <th style="width:20%;">Place of Supply</th>
                                <th style="width:20%;">Transport Mode </th>
                                <th style="width:15%" class="text-right">Total Qty</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;

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

                                $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[tgodwn]'") or die(mysqli_error($conn));
                                $sgodw = mysqli_fetch_array($ggodw);

                                $gtrnsprt = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` where `code` = '$pmain[transmode]'") or die(mysqli_error($conn));
                                $transport = mysqli_fetch_array($gtrnsprt);

                                $ctr++;
                                Echo"<tr>";
                                Echo"<td>$ctr</td>";
                                Echo"<td>$pmain[vouchno]</td>";
                                Echo"<td>$pmain[mydt]</td>";
                                Echo"<td>$sgodw[gcode]</td>";
                                Echo"<td>$pmain[plsply]</td>";
                                Echo"<td>$transport[name]</td>";
                                Echo"<td class='text-right'>$pmain[totqty]</td>";
                                Echo"</tr>";


                                $totqty = $totqty + $pmain[totqty];


                                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%;'>Sr</th>";
                                    Echo "<th style='width:15%;'>Product Code</th>";
                                    Echo "<th style='width:33%;' class=''>Description Of Goods</th>";
                                    Echo "<th style='width:8%;' class='text-right'>Mrp</th>";
                                    Echo "<th style='width:8%;' class='text-right'>Net Wt.</th>";
                                    Echo "<th style='width:8%;' class=''>Batch No.</th>";
                                    Echo "<th style='width:15%;' class=''>Hsncode</th>";
                                    Echo "<th style='width:13%;' class='text-right'>Qty</th>";
                                    Echo "</tr>";
                                    Echo "<thead>";
                                    Echo "<tr>";
//
//
                                    $ctr2 = 0;
                                    $totqty = 0;

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

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

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$tran[prodcode]</td>";
                                        Echo "<td>$tran[descrip]</td>";
                                        Echo "<td class='text-right'>$tran[mrp]</td>";
                                        Echo "<td class='text-right'>$tran[netwt]</td>";
                                        Echo "<td>$tran[batchno]</td>";
                                        Echo "<td>$tran[hsncode]</td>";
                                        Echo "<td class='text-right'>$tran[qty]</td>";
                                        Echo "</tr>";

                                        $totqty = $totqty + $tran[qty];
                                    }

                                    Echo "<tr>";
                                    Echo "<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'>$totqty</td>";
                                    Echo "</tr>";
                                    Echo "</table>";
                                }
                            }
                            Echo "<tr class='mySearchTr'>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td class='text-right'><b></b></td>";
                            Echo "<td class='text-right'><b></b></td>";
                            Echo "<td class='text-right'><b>Total</b></td>";
                            Echo "<td class='numeric text-right'><b>$totqty</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    $partyname = $_POST["partyname"];
    $godown = $_POST["godown"];
    $transmode = $_POST["transmode"];
    $dtype = $_POST["dtype"];

    if (!empty($godown)) {
        $godowncond = " AND `tgodwn` = '$godown' ";
    }
    if (!empty($transmode)) {
        $transcond = " AND `transmode` = '$transmode'";
    }
    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`= '$dtype' ";
    }

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


    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $transcond $godowncond ")or die(mysqli_error($conn));

    $contents.= "Sr., Voucher, Date, To Godown,Place of Supply, Transport Mode, Total Qty.,\n";

    $ctr = 0;

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

        $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[tgodwn]'") or die(mysqli_error($conn));
        $sgodw = mysqli_fetch_array($ggodw);

        $gtrnsprt = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` where `code` = '$pmain[transmode]'") or die(mysqli_error($conn));
        $transport = mysqli_fetch_array($gtrnsprt);

        $ctr++;
        $contents.= $ctr . ",";
        $contents.= $pmain[vouchno] . ",";
        $contents.= $pmain[mydt] . ",";
        $contents.= $sgodw[gcode] . ",";
        $contents.= $pmain[plsply] . ",";
        $contents.= $transport[name] . ",";
        $contents.= $pmain[totqty] . "\n";


        $totqty = $totqty + $pmain[totqty];


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


            $contents.= "Sr.,Product Code, Description Of Goods,Mrp, Net Wt., Batch No.,Hsncode,Qty,\n";

            $ctr2 = 0;
            $totqty = 0;

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

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

                $ctr2++;
                $contents.= $ctr2 . ",";
                $contents.= $tran[prodcode] . ",";
                $contents.= $tran[descrip] . ",";
                $contents.= $tran[mrp] . ",";
                $contents.= $tran[netwt] . ",";
                $contents.= $tran[batchno] . ",";
                $contents.= $tran[hsncode] . ",";
                $contents.= $tran[qty] . "\n";

                $totqty = $totqty + $tran[qty];
            }

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