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

$maindb = "transacn";
$trandb = "";
$formname = "Auto Inward Register";
$file = "trnfreg_fr.php";

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

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

if (isset($_POST["view"])) {
    $itemcode = $_POST["itemcode"];
    $itemname = $_POST["itemname"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];
    $fromcomp = $_POST["fromcomp"];
    $tocomp = $_POST["tocomp"];

    $dtypecond = "";
    $mscopecond = "";

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




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

    if (!empty($fromcomp)) {
        $frmcompcond = " AND `fromcomp`='$fromcomp' ";
    }

    if (!empty($tocomp)) {
        $tocompcond = " AND `tocomp`='$tocomp' ";
    }

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

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' and source = 'TR' and qty > 0 $frmcompcond $tocompcond $itemcond" )or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
    
//browse($get);
    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">
            <?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">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:3%">Sr</th>
                                <th style="width:8%">Voucher</th>
                                <th style="width:8%">Date</th>
                                <th style="width:15%">From Company</th>
                                <!--<th style="width:15%">To Company</th>-->
                                <th style="width:25%">Item Name</th>
                                <th style="width:8%">Unit</th>
                                <th style="width:8%;" class="text-right">Qty</th>
                                <th style="width:8%;" class="text-right">Rate</th>
                                <th style="width:10%;" class="text-right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $mtqty = 0;
                            $mgtot = 0;

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

                                $querycomp1 = mysqli_query($conn, "SELECT * FROM $zroot.`company` WHERE code ='$pmain[fromcomp]'") or die(mysqli_error($conn));
                                $fetcmpname1 = mysqli_fetch_object($querycomp1)or die(mysqli_error($conn));
//
//                                $querycomp2 = mysqli_query($conn, "SELECT * FROM $zroot.`company` WHERE code ='$pmain[tocomp]'") or die(mysqli_error($conn));
//                                $fetcmpname2 = mysqli_fetch_object($querycomp2)or die(mysqli_error($conn));

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

                                $getunit = mysqli_query($conn, "SELECT `code`,`name` from `unit` WHERE `code`='$pmain[unit]' ")or die();
                                $showuint = mysqli_fetch_array($getunit);


                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$fetcmpname1->name</td>";
//                                Echo "<td>$fetcmpname2->name</td>";
                                Echo "<td>$item[name]</td>";

                                Echo "<td>$showuint[name]</td>";

                                Echo "<td class='text-right' >$pmain[qty]</td>";
                                Echo "<td class='numeric'>$pmain[rate]</td>";
                                Echo "<td class='numeric'>$pmain[amount]</td>";
                                $mtqty = $pmain[qty] + $mtqty;
                                $mgtot = $pmain[amount] + $mgtot;

                                Echo "</tr>";
                            }

                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            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 "<td></td>";
                            echo "<th class='text-right' style='color: red;'>$mgtot</td>";
//                            echo "<td></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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