<?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"];

$formname = 'Inward Register';
$file = 'inwreg_fr.php';

$maindb = 'inwmain';
$trandb = 'inwtran';

$brand = $_POST['brand'];
$party = $_POST['party'];
$dtype = $_POST['dtype'];
$godown = $_POST['godown'];

$partycond = "";
$brandcond = "";
$gdncond = "";


//if (!empty($party)) {
//    $partycond = " AND `party`='$party' ";
//}
if (empty($godown)) {
    $gdncond = "AND godown = '$godown'";
}
if (!empty($brand)) {
    $brandcond = " AND `brand`='$brand' ";
}


if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
} else {

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        echo"<script>window.close();</script>";
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        echo"<script>window.close();</script>";
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}


if ($_POST["dtype"] != 2) {
    $getpmain = mysqli_query($conn, "SELECT a.*,b.*,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` a, `$trandb` b WHERE a.`vouchdt` >= '$mfrmdate' 
                                        AND a.`vouchdt` <= '$mtodate' and a.vouchno = b.vouchno group by a.vouchno order by a.vouchno,b.srno")or die(mysqli_error($conn));
} else {
    $getpmain = mysqli_query($conn, "SELECT a.*,b.*,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` a, `$trandb` b WHERE a.`vouchdt` >= '$mfrmdate' 
                                        AND a.`vouchdt` <= '$mtodate' and a.vouchno = b.vouchno $brandcond $gdncond group by a.vouchno order by a.vouchno,b.srno")or die(mysqli_error($conn));
}                                        


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

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

                        <table style="background: white;width: 100%;border-radius: 5px" class="table table-condensed table-hover table">
                            <thead >
                                <tr style="background-color: #1ABC9C">
                                    <th width="4%;">Sr</th>
                                    <th width="10%;">Date</th>
                                    <th width="12%;">Container No.</th>
                                    <th width="5%;" class="text-right">Qty</th>
                                    <th width="5%;" class="text-right">Totkg</th>
                                    <th width="6%;" class="text-right">Subtotal</th>
                                    <th width="20%;" >Remarks</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $subtot = 0;
                                $totdisc = 0;
                                $totfr = 0;
                                $totqty1 = 0;
                                $totnet = 0;

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

                                    $ctr++;
                                    Echo "<tr class='clickable-row2' data-href='inward.php?mode=edit&vouchno=$pmain[vouchno]' data-target='_blank'>";
                                    Echo "<td>$ctr</td>";
                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td>$pmain[containerno]</td>";
                                    Echo "<td class='numeric'>$pmain[tqty]</td>";
                                    Echo "<td class='numeric'>$pmain[tweight]</td>";
                                    Echo "<td class='numeric'>$pmain[subtotal]</td>";
                                    Echo "<td>$pmain[remark1]  $pmain[remark2]</td>";
                                    Echo "</tr>";
                                    Echo "<tr>";

                                    $subtot = $subtot + $pmain['subtotal'];
                                    $totdisc = $totdisc + $pmain['discount'];
                                    $totfr = $totfr + $pmain['freight'];
                                    $totnet = $totnet + $pmain['netamt'];
                                    $tottqty = $tottqty + $pmain['tqty'];
                                    $tottweight = $tottweight + $pmain['tweight'];

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

                                        Echo "<tr style='height:10px;'>";
                                        Echo "<td colspan='25'>";
                                        Echo "<table style='width:100%;'class='table table-condensed table-hover'>";
                                        Echo "<thead>";
                                        Echo "<tr class='' style='background-color:#BABFC2'>";
                                        Echo "<th style='width:2%;'>Sr</th>";
                                        Echo "<th style='width:7%;'>Brand/Logo</th>";
                                        Echo "<th style='width:5%;'>Code</th>";
                                        Echo "<th style='width:10%;'>Description</th>";
                                        Echo "<th style='width:6%;' class='text-right'>Qty.</th>";
                                        Echo "<th style='width:6%;' class='text-right'>Bale Qty.(Kgs)</th>";
                                        Echo "<th style='width:6%;' class='text-right'>Tot. Qty.(Kgs)</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;
                                        $totqty = 0;
                                        $totamt = 0;
                                        $totinv = 0;
                                        $totdtqty = 0;
                                        $totdtinv = 0;
                                        $totdtamt = 0;

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

                                            $getbrand = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$tran[brand]'")or die(mysqli_error($conn));
                                            $mbrand = mysqli_fetch_array($getbrand);
                                            $brand = $mbrand['name'];

                                            $finditem = mysqli_query($conn, "SELECT * FROM `item` WHERE `barcode`='$tran[itemcode]'") or die(mysqli_error($conn));
                                            $fitem = mysqli_fetch_array($finditem);

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$brand</td>";
                                            Echo "<td>$fitem[code]</td>";
                                            Echo "<td>$fitem[name]</td>";
                                            Echo "<td class='numeric'>$tran[qty]</td>";
                                            Echo "<td class='numeric'>$tran[baleqty]</td>";
                                            Echo "<td class='numeric'>$tran[totqty]</td>";
                                            Echo "<td class='numeric'>$tran[rate]</td>";
                                            Echo "<td class='numeric'>$tran[amount]</td>";
                                            Echo "</tr>";
                                            $totqty = $totqty + $tran['qty'];
                                            $totdtqty = $totdtqty + $tran['totqty'];
                                            $totdtinv = $totdtinv + $tran['baleqty'];
                                            $totdtamt = $totdtamt + $tran['amount'];
                                        }

                                        Echo "<tr style='background-color:#d8e0e6;'>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td class='text-right' ><b>Total </b></td>";
                                        Echo "<td class='text-right'><b> $totqty </b></td>";
                                        Echo "<td class='text-right'><b> $totdtinv </b></td>";
                                        Echo "<td class='text-right'><b> $totdtqty </b></td>";
                                        Echo "<td class='text-right' ></td>";
                                        Echo "<td class='text-right'><b> $totdtamt </b> </td>";
                                        Echo "</tr>";
                                        Echo "</table>";
                                    }
                                }
                                Echo "<tr>";
                                Echo "<td></td>";
                                Echo "<td class='numeric text-right'><b></b></td>";
                                Echo "<td class='numeric text-right'><b>Total</b></td>";
                                Echo "<td class='numeric text-right'><b>$tottqty</b></td>";
                                Echo "<td class='numeric text-right'><b>$tottweight</b></td>";
                                Echo "<td class='numeric text-right'><b>$subtot</b></td>";
                                Echo "<td class='numeric text-right'><b></b></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div> 
                </div>

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

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

        $contents .= "Sale Register\n";

        $contents .= "Sr,Voucher,Voucher Date,Direct,Shift,A/c Name,Vehicle No.,Slip No.,Total Amount\n";

        $ctr = 0;
        $ptotamt = 0;
        $ptotdiscamt = 0;
        $ptottaxable = 0;
        $ptotgst3 = 0;
        $ptotgst5 = 0;
        $ptotgst12 = 0;
        $ptotgst18 = 0;
        $ptotgst28 = 0;
        $ptotpackamt = $ptotstamt = $ptotothamt = $ptotbillamt = 0;
        $pchtypecond = "";

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

            $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
            $sparty = mysqli_fetch_array($gparty);

            if ($pmain[direct] == 'Y') {
                $direct = "Yes";
            } else {
                $direct = "No";
            }

            $voucher = $pmain['vouchno'];
            $mydt = $pmain['vouchdt'];
            $shift = $pmain['shift'];
            $party = $sparty['name'];
            $vehicleno = $pmain['vehicleno'];
            $slipno = $pmain['slipno'];
            $billamt = $pmain['billamt'];

            $ctr++;

            $contents .= "$ctr" . ",";
            $contents .= "$voucher" . ",";
            $contents .= "$mydt" . ",";
            $contents .= "$direct" . ",";
            $contents .= "$shift" . ",";
            $contents .= "$party" . ",";
            $contents .= "$vehicleno" . ",";
            $contents .= "$slipno" . ",";
            $contents .= "$billamt" . "\n";

            $ptotbillamt = $ptotbillamt + $billamt;

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

                $contents .= "Sr,Product Code,Product,Qty.,Unit,Rate,Amount\n";

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

                $gettran = mysqli_query($conn, "SELECT * FROM `saletran` 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`,`prodcode` FROM `product` WHERE `code`='$tran[product]' ") or die(mysqli_error($conn));
                    $showitem = mysqli_fetch_array($getitem);

                    $ctr2++;
                    $contents .= "$ctr2" . ",";
                    $contents .= "$showitem[prodcode]" . ",";
                    $contents .= "$showitem[name]" . ",";
                    $contents .= "$tran[qty]" . ",";
                    $contents .= "$tran[unit]" . ",";
                    $contents .= "$tran[rate]" . ",";
                    $contents .= "$tran[amount]" . "\n";

                    $pqty = $pqty + $tran['qty'];
                    $pamount = $pamount + $tran['amount'];
                }

                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= "$pqty" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "$pamount" . "\n";
            }
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$ptotbillamt" . "\n";




    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        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;
    }
}
?>