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

$file = 'inwreg_fr.php';

$maindb = "inwmain";
$trandb = "inwtran";

$item = $_POST["item"];

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


$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");
        header("refresh:1; inwreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; inwreg.php");
    }
    $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,b.*,c.barcode,c.name as item
                                                FROM `$maindb` a 
                                                LEFT JOIN `$trandb` b ON  a.vouchno = b.vouchno
                                                LEFT JOIN `product` c ON b.itemcode = c.code     
                                                WHERE  a.`vouchdt` >= '$mfrmdate' 
                                                AND a.`vouchdt` <= '$mtodate' 
                                                $itemcond   order by a.vouchdt,a.vouchno,b.srno") or die(mysqli_error($conn));

// print table($getpmain2);
$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-fluid">
            <?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="5%;">Voucher</th>
                                    <th width="6%;">Date</th>
                                    <th width="6%;">Code</th>
                                    <th width="18%;">Item</th>
                                    <th width="10%;">Type</th>
                                    <th width="10%;">Batch</th>
                                    <th width="10%;">BBF Mon</th>
                                    <th width="10%;">BBF Year</th>
                                    <th width="8%;" class="text-right">Qty</th>
                                    <th width="8%;" class="text-right">Weight</th>
                                    <th width="6%;">Unit</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 0;
                                $pgrtotalqty = 0;
                                $lastVoucher = null;
                                $colorToggle = false;

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


                                    $voucher = $pmain['vouchno'];
                                    $mydt = $pmain['mydt'];
                                    $barcode = $pmain['barcode'];
                                    $qty = $pmain['qty'];
                                    $itemcode = $pmain['item'];
                                    $type = $pmain['type'];
                                    $batch = $pmain['batch'];
                                    $bbfmon = $pmain['bbfmon'];
                                    $bbfyear = $pmain['bbfyear'];
                                    $weight = $pmain['weight'];
                                    $unit = $pmain['unit'];
                                    // $remark1 = $pmain['rem1'];
                                    // $remark2 = $pmain['rem2'];

                                    $currentVoucher = $pmain['voucher'];

                                    if ($currentVoucher !== $lastVoucher) {
                                        $colorToggle = !$colorToggle; // Toggle color
                                        $lastVoucher = $currentVoucher;
                                    }
                                
                                    $rowColor = $colorToggle ?  "#ffffff" : "#f2f2f2" ;
                            

                                    $pctr++;
                                    echo "<tr data-href='inward.php?mode=edit&vouchno=$voucher' class='clickable-row2' [target]='_blank' style='background-color:$rowColor'>";
                                    echo "<td>$pctr</td>";
                                    echo "<td>$pmain[vouchno]</td>";
                                    echo "<td>$pmain[mydt]</td>";
                                    echo "<td>$barcode</td>";
                                    echo "<td>$pmain[item]</td>";
                                    echo "<td>$pmain[type]</td>";
                                    echo "<td>$pmain[batch]</td>";
                                    echo "<td>$pmain[bbfmon]</td>";
                                    echo "<td>$pmain[bbfyear]</td>";
                                    echo "<td class='text-right'>$pmain[qty]</td>";
                                    echo "<td class='text-right'>$pmain[weight]</td>";
                                    echo "<td>$pmain[unit]</td>";
                                    echo "</tr>";

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

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

    </html>
    <?php
}


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

    $contents .= "Sr, Voucher, Date, Code, Item, Type, Batch, BBF MON, BBF Year, Qty, Weight, Unit \n";


$pctr = 0;
$pgrtotalqty = 0;
$lastVoucher = null;
$colorToggle = false;

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


    $voucher = $pmain['vouchno'];
    $mydt = $pmain['mydt'];
    $barcode = $pmain['barcode'];
    $qty = $pmain['qty'];
    $itemcode = $pmain['item'];
    $type = $pmain['type'];
    $batch = $pmain['batch'];
    $bbfmon = $pmain['bbfmon'];
    $bbfyear = $pmain['bbfyear'];
    $weight = $pmain['weight'];
    $unit = $pmain['unit'];
    // $remark1 = $pmain['rem1'];
    // $remark2 = $pmain['rem2'];

    $currentVoucher = $pmain['voucher'];

    if ($currentVoucher !== $lastVoucher) {
        $colorToggle = !$colorToggle; // Toggle color
        $lastVoucher = $currentVoucher;
    }

    $rowColor = $colorToggle ?  "#ffffff" : "#f2f2f2" ;


    $pctr++;
    $contents .= "$pctr" . ",";
    $contents .= "$pmain[vouchno]" . ",";
    $contents .= "$pmain[mydt]" . ",";
    $contents .= "$barcode" . ",";
    $contents .= "$pmain[item]" . ",";
    $contents .= "$pmain[type]" . ",";
    $contents .= "$pmain[batch]" . ",";
    $contents .= "$pmain[bbfmon]" . ",";
    $contents .= "$pmain[bbfyear]" . ",";
    $contents .= "$pmain[qty]" . ",";
    $contents .= "$pmain[weight]" . ",";
    $contents .= "$pmain[unit]" . "\n";
   

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