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

$maindb = "trnfmain";
$trandb = "trnftran";
$formname = "Internal Transfer Register";
$file = "intrnfreg_fr.php";

$zroot = $_SESSION["zdbroot"];
//browse($zroot);
$keycond = "";

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




$fromgodown = $_POST["fromgodown"];
$togodown = $_POST["togodown"];
$fdate = $_POST["fdate"];
$tdate = $_POST["tdate"];

$dtypecond = "";
$mscopecond = "";
$fromgdwncond = "";
$togodowncond = "";

$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);


if (!empty($itemcode)) {
    $itemcond = " AND `itemcode`='$itemcode' ";
}

if (!empty($fromgodown)) {
    $fromgdwncond = " AND `godown`='$fromgodown' ";
}

if (!empty($togodown)) {
    $togodowncond = " AND `togodown`='$togodown' ";
}

if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; inwreg.php");
}
if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; inwreg.php");
}

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

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

    $get = mysqli_num_rows($getpmain);
    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>

    </head>
    <body>
        <div class="container" style="width:820px">
            <?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:10%">Voucher No</th>
                                <th style="width:10%">Date</th>
                                <th style="width:10%">Godown</th>
                                <th style="width:10%">To Godown</th>
                                <th style="width:20%; padding-left:20px;"  class="">Remark</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $mtqty = 0;
                            $mgtot = 0;


                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $ctr++;


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

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

                                $remarks = $pmain['remark'] . "      " . $pmain['remark2'];

                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$godown</td>";
                                Echo "<td>$togodown</td>";
                                Echo "<td style='padding-left:20px;'>$remarks</td>";
                                Echo "</tr>";

                                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 "<th style='background-color: #cccccc;width:5%;'>Sr</th>";
                                Echo "<th style='background-color: #cccccc;width:5%; class='numeric''>#</th>";
                                Echo "<th style='background-color: #cccccc';width:5%;>Barcode</th>";
                                Echo "<th style='background-color: #cccccc';width:15%;>Brand</th>";
                                Echo "<th style='background-color: #cccccc';width:10%;>Size</th>";
                                Echo "<th style='background-color: #cccccc;width:10%;'>Name</th>";
                                Echo "<th style='background-color: #cccccc;width:14%;' >Quality.</th>";
                                Echo "<th style='background-color: #cccccc;width:10%;'>HSN Code.</th>";
                                Echo "<th style='background-color: #cccccc;width:10%;' class='numeric'>Qty.</th>";
                                Echo "<th style='background-color: #cccccc;width:10%;' class=''>Unit</th>";
                                Echo "<tr>";

//
                                $ctr2 = 0;
                                $tqty = 0;
                                $tvol = 0;
                                $stqty = 0;
                                $tamt = 0;

                                $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                while ($tran = mysqli_fetch_array($gettran)) {
$getitem1 = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$tran[prodcode]' ")or die(mysqli_error($conn));
                                            $fitem = mysqli_fetch_array($getitem1);
                                    $ctr2++;
                                    Echo "<tr>";
                                    Echo "<td>$ctr2</td>";
                                    Echo "<td>$tran[prodtype]</td>";
                                    Echo "<td>$tran[prodcode]</td>";
                                    Echo "<td>$fitem[brand]</td>";
                                    Echo "<td>$fitem[size]</td>";
                                    Echo "<td>$fitem[prodname]</td>";
                                    Echo "<td>$fitem[quality]</td>";
                                    Echo "<td>$tran[hsncode]</td>";
                                    Echo "<td class='numeric'>$tran[qty]</td>";
                                    Echo "<td class='numeric'>$tran[pcs_unit]</td>";
                                    Echo "</tr>";

                                    $tqty = $tran['qty'] + $tqty;
                                }

                                Echo "<tr style=' '>";
                                Echo "<td style=' '></td>";
                                Echo "<td style=' '></td>";
                                Echo "<th style='text-align:right;; '></th>";
                                Echo "<td style=' 'class='numeric'></td>";
                                Echo "<td style=' 'class='numeric'></td>";
//                                Echo "<td style=' 'class='numeric'></td>";
                                Echo "<th style='text-align:right;color:blue;'>Total</th>";
                                Echo "<td style='text-align:right;color:blue;' class='numeric'></td>";
                                Echo "<td style='text-align:right;color:blue;' class='numeric'></td>";
                                Echo "<td style='text-align:right;color:blue;' class='numeric'>$tqty</td>";
                                Echo "<th style='text-align:right;color:blue; '></td>";
                                Echo "</tr>";
                                Echo "</table>";
                                Echo "</td>";
                            }

//                            echo "<tr> ";
//                            echo "<td></td>";
//                            echo "<td></td>";
//                            echo "<th class='text-right' style='color: red;'>Grand Total</td>";
//                            echo "<th class='text-right' style='color: red;'>$mtqty</td>";
//                            echo "<th class='text-right' style='color: red;'>$mgtot</td>";
//                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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



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

    require("library/fpdf/fpdf.php");

    class pdf extends fpdf {

        function Footer() {



//Position at 1.5 cm from bottom

            $this->SetY(-15);

//Arial italic 8

            $this->SetFont('Arial', 'I', 8);

//Page number

            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }

    }

    $pdf = new pdf('P', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();

//$pdf->SetMargins(1,1,1);
//$pdf->SetXY(5,5);
    $pdf->Cell(10, 7, '', 0, 'C');
    $pdf->Ln();

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(72, 25, 89);
    $pdf->Cell(10, 8, 'Sr.', 1, 0, '', 1);
    $pdf->Cell(20, 8, 'Voucher', 1, 0, '', 1);
    $pdf->Cell(25, 8, 'Date', 1, 0, '', 1);
    $pdf->Cell(60, 8, 'Party', 1, 0, '', 1);
    $pdf->Cell(20, 8, 'Invoice', 1, 0, '', 1);
    $pdf->Cell(22, 8, 'Type', 1, 0, '', 1);
    $pdf->Cell(40, 8, 'Remark', 1, 0, '', 1);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();


    $ctr = 0;
    $mtqty = 0;
    $mgtot = 0;
    $mgstk = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $findparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'") or die(mysqli_error($conn));
        $lparty = mysqli_fetch_array($findparty);
        $type = $pmain['type'];
        if ($type == 'SL') {
            $ttype = 'Sale';
        } elseif ($type == 'PR') {
            $ttype = 'Purchase Return';
        } else {
            $ttype = 'Other';
        }

        $ctr++;

        $pdf->SetFont('Arial', '', 10);
//        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(10, 8, $ctr, 1, 0, '', 1);
        $pdf->Cell(20, 8, $pmain['vouchno'], 1, 0, '', 1);
        $pdf->Cell(25, 8, $pmain['mydt'], 1, 0, '', 1);
        $pdf->Cell(60, 8, $lparty['name'], 1, 0, '', 1);
        $pdf->Cell(20, 8, $pmain['invno'], 1, 0, '', 1);
        $pdf->Cell(22, 8, $ttype, 1, 0, '', 1);
        $pdf->Cell(40, 8, $pmain['remark'] . ' ' . $pmain['remark2'], 1, 0, '', 1);
        $pdf->Ln();


        $mtqty = $pmain[qty] + $mtqty;
        $mgtot = $pmain[vol] + $mgtot;
        $mgstk = $pmain[stkvol] + $mgstk;


        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(203, 203, 203);
        $pdf->Cell(8, 8, 'Sr.', 1, 0, '', 1);
        $pdf->Cell(30, 8, 'Item Group', 1, 0, '', 1);
        $pdf->Cell(40, 8, 'Item Name', 1, 0, '', 1);
        $pdf->Cell(23, 8, 'HSN Code', 1, 0, '', 1);
        $pdf->Cell(20, 8, 'Itempart', 1, 0, '', 1);
        $pdf->Cell(15, 8, 'Unit', 1, 0, '', 1);
        $pdf->Cell(15, 8, 'Qty.', 1, 0, '', 1);
        $pdf->Cell(15, 8, 'Vol.', 1, 0, '', 1);
        $pdf->Cell(15, 8, 'Stk.Vol.', 1, 0, '', 1);
        $pdf->Cell(15, 8, 'Stk. Unit', 1, 0, '', 1);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();


        $ctr2 = 0;
        $tqty = 0;
        $tamt = 0;

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

            $getitemgrp = mysqli_query($conn, "SELECT `code`,`name` FROM `itemgrp` WHERE `grpcode`='$tran[itemgrp]'")or die(mysqli_error($conn));
            $itemgrp = mysqli_fetch_array($getitemgrp);

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

            $ctr2++;

            $pdf->SetFont('Arial', '', 10);
//        $pdf->SetTextColor(0, 0, 0);
            $pdf->SetFillColor(255, 255, 255);
            $pdf->Cell(8, 8, $ctr2, 1, 0, '', 1);
            $pdf->Cell(30, 8, $itemgrp[name], 1, 0, '', 1);
            $pdf->Cell(40, 8, $item[name], 1, 0, '', 1);
            $pdf->Cell(23, 8, $tran[hsncode], 1, 0, '', 1);
            $pdf->Cell(20, 8, $tran[size], 1, 0, '', 1);
            $pdf->Cell(15, 8, $tran[unit], 1, 0, '', 1);
            $pdf->Cell(15, 8, $tran[qty], 1, 0, '', 1);
            $pdf->Cell(15, 8, $tran[vol], 1, 0, '', 1);
            $pdf->Cell(15, 8, $tran[stkvol], 1, 0, '', 1);
            $pdf->Cell(15, 8, $tran[stkunit], 1, 0, '', 1);
            $pdf->Ln();

            $tqty = $tran[qty] + $tqty;
            $tvol = $tran[vol] + $tvol;
            $tstk = $tran[stkvol] + $tstk;
        }

        $pdf->SetFont('Arial', '', 10);
        $pdf->SetTextColor(99, 0, 246);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(8, 8, '', 1, 0, '', 1);
        $pdf->Cell(30, 8, '', 1, 0, '', 1);
        $pdf->Cell(40, 8, 'Total', 1, 0, '', 1);
        $pdf->Cell(23, 8, '', 1, 0, '', 1);
        $pdf->Cell(20, 8, '', 1, 0, '', 1);
        $pdf->Cell(15, 8, '', 1, 0, '', 1);
        $pdf->Cell(15, 8, $tqty, 1, 0, '', 1);
        $pdf->Cell(15, 8, $tvol, 1, 0, '', 1);
        $pdf->Cell(15, 8, $tstk, 1, 0, '', 1);
        $pdf->Ln();

        $pdf->Cell(10, 7, '', 0, 'C');
        $pdf->Ln();
    }


    $pdf->Output('InwReg.pdf', 'D');
}


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

    include("includes/head_rep_excel.php");


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

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $ctr++;


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


        $remarks = $pmain[remark] . "      " . $pmain[remark2];


        $contents .= $ctr . ",";
        $contents .= $pmain[vouchno] . ",";
        $contents .= $pmain[mydt] . ",";
        $contents .= $godown . ",";
        $contents .= $remarks . "\n";


        $contents .= "Sr.,#,Barcode,Brand,Size,Name,Quality,HSN Code,Qty, Unit\n";

        $ctr2 = 0;
        $tqty = 0;
        $tvol = 0;
        $stqty = 0;
        $tamt = 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[prodtype] . ",";
            $contents .= $tran[prodcode] . ",";
            $contents .= $tran[brand] . ",";
            $contents .= $tran[size] . ",";
            $contents .= $tran[prodname] . ",";
            $contents .= $tran[quality] . ",";
            $contents .= $tran[hsncode] . ",";
            $contents .= $tran[qty] . ",";
            $contents .= $tran[pcs_unit] . "\n";
            $tqty = $tran[qty] + $tqty;
        }

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