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

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

$file = 'inwreg_fr.php';

$supplier = $_POST["supplier"];
$dtype = $_POST["dtype"];

if (!empty($supplier)) {
    $suppcond = " AND b.`supplier` = '$supplier' ";
}

$formname = 'Inward Register';


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));
}

$getpmain2 = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt 
                                  FROM `$maindb` a INNER JOIN `$trandb` b ON a.vouchno = b.vouchno
                                  WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate'
                                  $suppcond order by a.vouchdt,a.vouchno ") or die(mysqli_error($conn));

$getno2 = mysqli_num_rows($getpmain2);


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


    include("includes/linkshead.php");
?>
    <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_rep2.php");

            if ($getno2 > 0) {
            ?>
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condesed table-bordered">
                            <tr class="mySearchTr">
                                <!--9221292520-->
                                <th width="100%" class="h4" style="color:white"><b>Inward Register</b></th>
                            </tr>
                            <tr>
                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                </td>
                            </tr>
                        </table>
                        
                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width="3%;">Sr</th>
                                    <th width="4%;">Voucher</th>
                                    <th width="8%;">Date</th>
                                    <th width="20%;" class="text-right">Total</th>
                                    <th width="67%;">Remarks</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 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($getpmain2)) {
                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
                                    $sparty = mysqli_fetch_array($gparty);

                                    $voucher = $pmain['vouchno'];
                                    $mydt = $pmain['mydt'];
                                    $total = $pmain['grosstotqty'];
                                    $remark1 = $pmain['rem1'];
                                    $remark2 = $pmain['rem2'];

                                    $pctr++;
                                    echo "<tr data-href='purchase.php?mode=edit&vouchno=$voucher' class='clickable-row2' [target]='_blank'>";
                                    echo "<td><b>$pctr</b></td>";
                                    echo "<td><b>$voucher</b></td>";
                                    echo "<td><b>$mydt</b></td>";
                                    echo "<td class='text-right'><b>$total</b></td>";
                                    echo "<td><b>$remark1 $remark2</b></td>";
                                    echo "</tr>";



                                    $pgrtotal = $pgrtotal + $total;
                                    $pgrtotdis = $pgrtotdis + $totdisc;
                                    $pgrtotvat = $pgrtotvat + $totvat;
                                    $pgrtotbillamt = $pgrtotbillamt + $billamt;

                                    if ($_POST["dtype"] == 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:#7f827f'>";
                                        echo "<th style='width:5%;'>Sr</th>";
                                        echo "<th style='width:35%;' class=''>Item</th>";
                                        echo "<th style='width:20%;' class=''>Supplier</th>";
                                        echo "<th style='width:10%;' class=''>Batch</th>";
                                        echo "<th style='width:10%;' class='text-right'>Qty.</th>";
                                        echo "<th style='width:10%;' class='text-right'>Unit</th>";
                                        echo "</tr>";
                                        echo "<thead>";
                                        echo "<tr>";

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

                                        $gettran = mysqli_query($conn, "SELECT a.*,b.name as suppname FROM `$trandb` a LEFT JOIN accmast b ON a.supplier = b.accode 
                                                                         WHERE a.`vouchno`='$pmain[vouchno]' order by a.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);

                                            $getunit = mysqli_query($conn, "SELECT * FROM $zroot.`unit` WHERE code = '$tran[unit]'") or die(mysqli_error($conn));
                                            $fetunit = mysqli_fetch_array($getunit);

                                            $ctr2++;
                                            echo "<tr>";
                                            echo "<td>$ctr2</td>";
                                            echo "<td>$showitem[name]</td>";
                                            echo "<td>$tran[suppname]</td>";
                                            echo "<td>$tran[batch]</td>";
                                            echo "<td class='text-right'>$tran[qty]</td>";
                                            echo "<td class='text-right'>$fetunit[name]</td>";
                                            echo "</tr>";

                                            $pcartqty += $tran['cartqty'];
                                            $pqty += $tran['qty'];
                                            $ptotqty += $tran['totqty'];
                                            $ptotamount += $tran['amount'];
                                            $ptotdiscount += $tran['discamt'];
                                            $ptotvat += $tran['vat'];
                                            $ptotal += $tran['total'];
                                        }

                                        echo "<tr style='background-color:#d8e0e6'>";
                                        echo "<td></td>";
                                        echo "<td></td>";
                                        echo "<td></td>";
                                        echo "<td>Total</td>";
                                        echo "<td class='text-right'>$pqty</td>";
                                        echo "<td></td>";
                                        echo "</tr>";
                                        echo "</table>";
                                    }
                                }
                                echo "<tr style='background-color: #98F9F0; color:#fc001a'>";
                                echo "<td class='text-right' colspan='3'><b>Total</b></td>";
                                echo "<td class='numeric text-right'><b>" . number_format($pgrtotal, 2) . "</b></td>";
                                echo "<td></td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>

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

    </html>
<?php
}

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

    
    if ($getno2 > 0) {
        $contents .= "Inward Register \n";
        $contents .= "Total No. of Record : $getno2 \n";

        $contents .= "Sr, Voucher, Date, Total, Remarks \n";

        $pctr = 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($getpmain2)) {
            $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
            $sparty = mysqli_fetch_array($gparty);

            $voucher = $pmain['vouchno'];
            $mydt = $pmain['mydt'];
            $total = $pmain['grosstotqty'];
            $remark1 = $pmain['rem1'];
            $remark2 = $pmain['rem2'];

            $pctr++;
            $contents .= "$pctr" . ",";
            $contents .= "$voucher" . ",";
            $contents .= "$mydt" . ",";
            $contents .= "$total" . ",";
            $contents .= "$remark1 $remark2" . "\n";

            $pgrtotal = $pgrtotal + $total;
            $pgrtotdis = $pgrtotdis + $totdisc;
            $pgrtotvat = $pgrtotvat + $totvat;
            $pgrtotbillamt = $pgrtotbillamt + $billamt;

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

                $contents .= "Sr, Item, Supplier, Batch, Qty, Unit \n";

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

                $gettran = mysqli_query($conn, "SELECT a.*,b.name as suppname FROM `$trandb` a LEFT JOIN accmast b ON a.supplier = b.accode 
                                                WHERE a.`vouchno`='$pmain[vouchno]' order by a.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);

                    $getunit = mysqli_query($conn, "SELECT * FROM $zroot.`unit` WHERE code = '$tran[unit]'") or die(mysqli_error($conn));
                    $fetunit = mysqli_fetch_array($getunit);

                    $ctr2++;
                    $contents .= "$ctr2" . ",";
                    $contents .= "$showitem[name]" . ",";
                    $contents .= "$tran[suppname]" . ",";
                    $contents .= "$tran[batch]" . ",";
                    $contents .= "$tran[qty]" . ",";
                    $contents .= "$fetunit[name]" . "\n";

                    $pcartqty += $tran['cartqty'];
                    $pqty += $tran['qty'];
                    $ptotqty += $tran['totqty'];
                    $ptotamount += $tran['amount'];
                    $ptotdiscount += $tran['discamt'];
                    $ptotvat += $tran['vat'];
                    $ptotal += $tran['total'];
                }

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

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