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

$maindb = 'trnfmain';
$trandb = 'trnftran';
$formname = "Transfer Register";
$file = "tranreg_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; tranreg.php");
    }

    if (empty($todt)) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; tranreg.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: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=''><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%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    Echo "<thead>";
                                    Echo "<tr class='' style='background-color:#BABFC2'>";
                                    Echo "<th style='width:5%;'>Sr</th>";
                                    Echo "<th style='width:8%;'>Code</th>";
                                    Echo "<th style='width:15%;' class=''>Item</th>";
                                    Echo "<th style='width:4%;' class='text-right'>Ctn. Qty.</th>";
                                    // Echo "<th style='width:5%;' class='text-right'>Rate</th>";
                                    // Echo "<th style='width:5%;' class='text-right'>Amount</th>";
                                    Echo "</tr>";
                                    Echo "<thead>";
                                    Echo "<tr>";
//
//
                                    $ctr2 = 0;
                                    $pctr2 = 0;
                                    $pcartqty = $pqty = $ptotqty = $ptotamount = $ptotdiscount = $ptotvat = $ptotal = 0;

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

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

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$tran[barcode]</td>";
                                        Echo "<td>$showitem[name]</td>";
                                        Echo "<td class='text-right'>$tran[qty]</td>";
                                        // Echo "<td class='text-right'>$tran[cost]</td>";
                                        // Echo "<td class='text-right'>$tran[amount]</td>";
                                        Echo "</tr>";

                                        $pcartqty = $pcartqty + $tran['cartqty'];
                                        $pqty = $pqty + $tran['qty'];
                                        $ptotqty = $ptotqty + $tran['totqty'];
                                        $ptotamount = $ptotamount + $tran['amount'];
                                    }

                                    Echo "<tr style='background-color:#c7ecee;font-weight:bold;'>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td class='text-right'>Total</td>";
                                    Echo "<td class='text-right'>$pqty</td>";
                                    // Echo "<td></td>";
                                    // Echo "<td class='text-right'>$ptotamount</td>";
                                    Echo "</tr>";
                                    Echo "</table>";
                                }
                            }
                            ?>
                            <!-- <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; tranreg.php");
    }

    if (empty($todt)) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; tranreg.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);

    $contents .= "Sr.,Voucher,Date,From Godown,To Godown,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['remark1'] . $pmain['remark2'] . "\n";

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

            $contents .= "Sr.,code,Item,Ctn. Qty.\n";

            $ctr2 = 0;
            $pctr2 = 0;
            $pcartqty = $pqty = $ptotqty = $ptotamount = $ptotdiscount = $ptotvat = $ptotal = 0;

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

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

                $ctr2++;
                $contents .= "$ctr2" . ",";
                $contents .= "$tran[barcode]" . ",";
                $contents .= "$showitem[name]" . ",";
                $contents .= "$tran[qty]" . "\n";
                // $contents .= "$tran[cost]" . ",";
                // $contents .= "$tran[amount]" . "\n";
                
                $pcartqty = $pcartqty + $tran['cartqty'];
                                        $pqty = $pqty + $tran['qty'];
                                        $ptotqty = $ptotqty + $tran['totqty'];
                                        $ptotamount = $ptotamount + $tran['amount'];
            }
            $contents .= " " . " ,";
            $contents .= " " . " ,";
            $contents .= " Total" . ",";
            $contents .= $pqty . ",";
            // $contents .= " " . ",";
            // $contents .= "$ptotamount" . ",";
        }
    }
    
    // $contents .= " " . " ,";
    // $contents .= " " . " ,";
    // $contents .= " " . " ,";
    // $contents .= " " . " ,";
    // $contents .= " Total" . ",";
    // $contents .= $trtotqty . ",";
    // $contents .= " " . "\n";



    $contents = strip_tags($contents);

    if ($detail == 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;
    }
}
?>

