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

$maindb = 'trnfmain';
$trandb = 'trnftran';
$formname = "Invoice - Garbage";
$file = "invgarbagereg_fr.php";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$zroot = $_SESSION["zdir"];

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

    $frgodw = $_POST["frgodw"];
    $togodw = $_POST["togodw"];
    $frmdt = $_POST["frmdt"];
    $todt = $_POST["todt"];
    $detail = $_POST["detail"];

    $frgodwcond = "";
    $togodwcond = "";

    if (!empty($frgodw)) {
        $frgodwcond = "AND `fgodown`= '$frgodw' ";
    }

    if (!empty($togodw)) {
        $togodwcond = "AND `tgodown`= '$togodw' ";
    }

    if (empty($frmdt)) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; invgarbagereg.php");
    }

    if (empty($todt)) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; invgarbagereg.php");
    }

    $mfrmdate = $frmdt;
    $mtodate = $todt;
    $fdate = date("d-m-Y", strtotime($mfrmdate));
    $tdate = date("d-m-Y", strtotime($mtodate));

    $getpmain = mysqli_query($conn, "SELECT *, date_format(`vouchdt`,'%d-%m-%Y') as vouchdt FROM `$maindb` where 
                            `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and vouchno != ''  $frgodwcond 
                             $togodwcond") or die(mysqli_error($conn));
    $getno = mysqli_num_rows($getpmain);

    include("includes/linkshead.php");
    ?>
    <style>
        @media print
        {    
            .no-print, .no-print *
            {
                display: none !important;
            }
        }
        .th1{
            background-color: #00AAFC;            
        }
        .th2{
            background-color: #88D8BF;            
        }
    </style>
    </head>
    <body>
        <div class="container">

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

            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="width:100%;background: white;" class="table table-condensed table-hover table-striped table-bordered">
                        <thead class="mySearchTr">
                            <tr style="">
                                <th style="width:6%;">Sr</th>
                                <th style="width:7%;">Voucher</th>
                                <th style="width:8%;">Date</th>
                                <th style="width:10%;">From Godown</th>
                                <th style="width:10%;">To Godown </th>
                                <th style="width:10%;" class='text-right'>Total Qty. </th>
                                <th style="width:40%;" >Remarks</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = $tqty = 0;


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

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

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


                                $ctr++;
                                Echo "<tr style='background-color:white' data-href='transfer.php?mode=edit&vouchno=$pmain[vouchno]' class='clickable-row2' [target]='_blank'>";
                                Echo "<td><b>$ctr</th>";
                                Echo "<td><b>$pmain[vouchno]</th>";
                                Echo "<td><b>$pmain[vouchdt]</th>";
                                Echo "<td><b>$fgodown[gcode]</th>";
                                Echo "<td><b>$sgodown[gcode]</th>";
                                Echo "<td class='text-right'><b>$pmain[totqty]</th>";
                                Echo "<td class=''><b>$pmain[remark1]$pmain[remark2]</th>";
                                Echo "</tr>";

                                $tqty = $tqty + $pmain[totqty];
                                if ($detail == 2) {

                                    Echo "<tr style='height:10px;'>";
                                    Echo "<td colspan='25'>";
                                    Echo "<table style='width:100%;'  class='table table-condensed  table-bordered'>";


                                    Echo "<tr class='th2'>";
                                    Echo "<td style='width:5%;'><b><i>Sr.</i></b></td>";
                                    Echo "<td style='width:7%;'><b><i>Barcode</i></b></td>";
                                    Echo "<td style='width:17%;'><b><i>Product Name</i></b></td>";
                                    Echo "<td style='width:7%;'><b><i>HSN Code</i></b></td>";
                                    Echo "<td style='width:5%;'><b><i>Gram</i></b></td>";
                                    Echo "<td style='width:5%;' class='text-right'><b><i>Unit</i></b></td>";
                                    Echo "<td style='width:5%;' class='text-right'><b><i>Batch</i></b></td>";
                                    Echo "<td style='width:5%;' class='text-right'><b><i>Qty</i></b></td>";
                                    Echo "<td style='width:25%;'><b><i>Remark</i></b></td>";
                                    Echo "</tr>";

                                    $ctr2 = $trantotqty = $tranrate = $tranamt = 0;

                                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` where vouchno ='$pmain[vouchno]' ") or die(mysqli_error($conn));

                                    while ($tran = mysqli_fetch_array($gettran)) {

                                        $gitem = mysqli_query($conn, "SELECT name FROM `product` WHERE barcode ='$tran[barcode]'") or die(mysqli_error($conn));
                                        $fitem = mysqli_fetch_array($gitem);
                                        $itemname = $fitem['name'];




                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td><i>$ctr2</i></td>";
                                        Echo "<td><i>$tran[barcode]</i></td>";
                                        Echo "<td><i>$itemname</i></td>";
                                        Echo "<td><i>$tran[hsncode]</i></td>";
                                        Echo "<td><i>$tran[gram]</i></td>";
                                        Echo "<td class='text-right'><i>$tran[unit]</i></td>";
                                        Echo "<td class='text-right'><i>$tran[batch]</i></td>";
                                        Echo "<td class='text-right'><i>$tran[qty]</i></td>";
                                        Echo "<td><i>$tran[remark]</i></td>";
                                        Echo "</tr>";
                                        $trtotqty = $trtotqty + $tran[qty];
                                    }
                                    Echo "<tr style='background-color:white'>";
                                    Echo "<td ><b><i></i></b></td>";
                                    Echo "<td ><b><i></i></b></td>";
                                    Echo "<td ><b><i></i></b></td>";
                                    Echo "<td ><b><i></i></b></td>";
                                    Echo "<td ><b><i></i></b></td>";
                                    Echo "<td style='color:red;' class='text-right'><b><i></i></b></td>";
                                    Echo "<td class='text-right'style='color:red;'><b><i>Total</i></b></td>";
                                    Echo "<td class='text-right' style='color:red;' ><b><i>$trtotqty</i></b></td>";
                                    Echo "<td class='text-right' style='color:red;'><b><i></i></b></td>";
                                    Echo "</tr>";


                                    Echo "</table>";
                                    Echo "</td>";
                                    Echo "</tr>";
                                }
                            }
                            ?>
                            <tr>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td style=" color:#00AAFC;" class='text-right' ><b>Total</b></td>
                                <td style=" color:#00AAFC;" class='text-right'><b><?= $trtotqty ?></b></td>
                                <td></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php"); ?>
    </body>
    </html>
    <?php
}

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

    $frgodw = $_POST["frgodw"];
    $togodw = $_POST["togodw"];
    $frmdt = $_POST["frmdt"];
    $todt = $_POST["todt"];
    $detail = $_POST["detail"];

    $frgodwcond = "";
    $togodwcond = "";

    if (!empty($frgodw)) {
        $frgodwcond = "AND `fgodown`= '$frgodw' ";
    }

    if (!empty($togodw)) {
        $togodwcond = "AND `tgodown`= '$togodw' ";
    }

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

    $mfrmdate = $frmdt;
    $mtodate = $todt;

    $fdate = date("d-m-Y", strtotime($mfrmdate));
    $tdate = date("d-m-Y", strtotime($mtodate));

    $getpmain = mysqli_query($conn, "SELECT *, date_format(`vouchdt`,'%d-%m-%Y') as vouchdt FROM `$maindb` where 
                            `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and vouchno != ''  $frgodwcond 
                             $togodwcond") or die(mysqli_error($conn));


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


    $ctr = $tqty = 0;

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

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

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


        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $pmain['vouchno'] . ",";
        $contents .= $pmain['vouchdt'] . ",";
        $contents .= $fgodown['gcode'] . ",";
        $contents .= $sgodown['gcode'] . ",";
        $contents .= $pmain['totqty'] . ",";
        $contents .= $pmain['remark1'] . $pmain['remark2'] . "\n";

        $tqty = $tqty + $pmain[totqty];
        if ($detail == 2) {




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

            $ctr2 = $trantotqty = $tranrate = $tranamt = 0;
            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` where vouchno ='$pmain[vouchno]' ") or die(mysqli_error($conn));

            while ($tran = mysqli_fetch_array($gettran)) {

                $ctr2++;
                $contents .= $ctr2 . ",";
                $contents .= $tran[barcode] . ",";
                $contents .= $tran[itemname] . ",";
                $contents .= $tran[hsncode] . ",";
                $contents .= $tran[gram] . ",";
                $contents .= $tran[unit] . ",";
                $contents .= $tran[batch] . ",";
                $contents .= $tran[qty] . ",";
                $contents .= $tran[remark] . "\n";
                $trtotqty = $trtotqty + $tran[qty];
            }
            $contents .= " " . " ,";
            $contents .= " " . " ,";
            $contents .= " " . " ,";
            $contents .= " " . " ,";
            $contents .= " " . " ,";
            $contents .= " " . " ,";
            ;
            $contents .= " Total" . ",";
            $contents .= $trtotqty . ",";
            $contents .= " " . "\n";
        }
    }

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

