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

$maindb = "salemain";
//$trandb = "damtran";
$formname = "L.R Register";
$file = "lrreg_fr.php";

$zroot = $_SESSION["zdir"];

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

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

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


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

    $pcond = '';
    $gstcond = '';
    
    if(!empty($party)){
        $pcond = "and party = '$party' " ;
    }
       if (!empty($gsttype)) {
        if ($gsttype == 1) {
            $gstcond = "AND `oms` = '1'";
        } else {
            $gstcond = "AND `oms` = ''";
        }
    }
    
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));


    $getpmain = mysqli_query($conn, "SELECT vouchno, invno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as invdt, 
                             party, billamt, lrno, lrdate FROM $maindb WHERE `vouchdt` >= '$mfrmdate'
                             AND `vouchdt` <= '$mtodate' and lrno != '' $pcond $gstcond")or die(mysqli_error($conn));


    $getno = mysqli_num_rows($getpmain);

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }

        table tr td{
            font-size: 14px;
        }
    </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 table-striped table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%">Sr</th>
                                <th style="width:10%">Date</th>
                                <th style="width:10%">Invoice No.</th>
                                <th style="width:25%">Party Name</th>
                                <th style="width:15%" class="text-right">Bill Amt.</th>
                                <th style="width:15%" >L.R. No.</th>
                                <th style="width:10%" >L.R. Date</th>
                                <th style="width:10%" >Image</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totqty = 0;
                            $total = 0;

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

                                $invno = $pmain['invno'];
                                $vouchno = $pmain['vouchno'];
                                $mydt = $pmain['invdt'];
                                $party = $pmain['party'];
                                $billamt = $pmain['billamt'];
                                $lrno = $pmain['lrno'];
                                $lrdate = $pmain['lrdate'];

                                $getparty = mysqli_query($conn, "SELECT name from accmast where accode = '$party' ")or die(mysqli_error($conn));
                                $fparty = mysqli_fetch_array($getparty);
                                $pname = $fparty['name'];


                                $gimage = mysqli_query($conn, "SELECT imgpath FROM `lrimage` WHERE `vouchno` = '$vouchno' and source = '$source' ") or die(mysqli_error($conn));
                                $fimage = mysqli_fetch_array($gimage);
                                $imgpath = $fimage['imgpath'];

                                $ctr++;
                                Echo "<tr data-href='damaged.php?mode=edit&vouchno=$vouchno' class='clickable-row2' [target]='_blank'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$mydt</td>";
                                Echo "<td>$invno</td>";
                                Echo "<td>$pname</td>";
                                Echo "<td class='text-right'>$billamt</td>";
                                Echo "<td>$lrno</td>";
                                Echo "<td>$lrdate</td>";
                                if ($imagpath != '') {
                                    Echo "<td><a id='print' class='btn btn-pad btn-square ' style='background-color:#5BC0DE;' href='$imgpath' target='_blank'>
                                            View Image
                                        </a>
                                        </td>";
                                } else {
                                    Echo "<td></td>";
                                }
                                Echo "</tr>";
                                $totbillamt = $totbillamt + $billamt;
                            }
                            
                            
                            
                            echo "<tr class='mySearchTr'> ";
                            echo "<td align='right' colspan='4'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' ><h4><b>$totbillamt</b></h4></td>";
                            echo "<td align='right' ><h4><b></b></h4></td>";
                            echo "<td align='right' ><h4><b></b></h4></td>";
                            echo "<td align='right' ><h4><b></b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <?php include("includes/linksfoot.php");
    ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    $itemcode = $_POST["itemcode"];
    $itemname = $_POST["itemname"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];

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

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

    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; inweg.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' ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);

    $contents .= "Sr.,Vouch No.,Date, Godown, Total Qty \n";

    $ctr = 0;
    $totqty = 0;
    $total = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $ggodwn = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code` = '$pmain[godown]'")or die(mysqli_error($conn));
        $godwn = mysqli_fetch_array($ggodwn);

        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $pmain['vouchno'] . ",";
        $contents .= $pmain['mydt'] . ",";
        $contents .= $godwn['gcode'] . ",";
        $contents .= $pmain['totqty'] . "\n";

        $totqty = $totqty + $pmain['totqty'];

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

            $contents .= "Sr.,Barcode, Product Code, Product Name,HSN Code, Gram, Unit, Batch,Qty., Rate,Mrp\n ";

            $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)) {

                $gitem = mysqli_query($conn, "SELECT name FROM `product` WHERE barcode ='$tran[itemcode]'") or die(mysqli_error($conn));
                $fitem = mysqli_fetch_array($gitem);
                $itemname = $fitem['name'];


                $ctr2++;
                $contents .= $ctr2 . ",";
                $contents .= $tran['barcode'] . ",";
                $contents .= $tran['itemcode'] . ",";
                $contents .= $itemname . ",";
                $contents .= $tran['hsncode'] . ",";
                $contents .= $tran['gram'] . ",";
                $contents .= $tran['unit'] . ",";
                $contents .= $tran['batch'] . ",";
                $contents .= $tran['qty'] . ",";
                $contents .= $tran['rate'] . ",";
                $contents .= $tran['mrp'] . "\n";

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

            $contents .= ",";
            $contents .= ",";
            $contents .= ",";
            $contents .= ",";
            $contents .= ",";
            $contents .= ",";
            $contents .= ",";
            $contents .= 'Total' . ",";
            $contents .= $totqty . ",";
            $contents .= ",";
            $contents .= "\n";
        }
    }
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Grand Total" . ",";
    $contents .= $totqty . ",";
    $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;
    }
}
?>