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

$ztit = $_SESSION["tit_" . $dbname];

$maindb = 'salemain';
$trandb = 'saletran';

$file = "lrupdate_fr.php";

$custdb = 'accmast';

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

$party = $_POST["party"];
$company = $_POST["company"];
$ofs = $_POST["ofs"];
$barcode = $_POST["barcode"];
$compmast = $_POST["compmast"];
$dtype = $_POST["dtype"];
$scope = $_POST["scope"];
$ordby = $_POST["ordby"];

$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);
$partycond = "";
$companycond = "";
$barcodecond = "";
$ofscond = "";
$compmastcond = "";

if (isset($_POST["submit"])) {
    $count = $_POST["items"];
    for ($i = 0; $i <= $count; $i++) {

        $src = $_POST["src_$i"];
        $vouchno = $_POST["vouchno_$i"];
        $transport = $_POST["transport_$i"];
        $nocart = $_POST["nocart_$i"];
        $lrno = $_POST["lrno_$i"];
        $lrdate = $_POST["lrdate_$i"];
        $transamt = $_POST["transamt_$i"];

        if ($src == "WH") {
            $maindb = "salemainwh";
        } else {
            $maindb = "salemainrt";
        }

        mysqli_query($conn, "Update `$maindb` set transport='$transport', nocart='$nocart', lrno='$lrno', lrdate='$lrdate' where vouchno='$vouchno'") or die(mysqli_error($conn));
    }
    echo "<script>"
    . "window.close();"
    . "</script>";
}

$rtype = $_POST["rtype"];

$typecond = "";
if (!empty($rtype)) {
    if ($rtype == 1) {
        $typecond = " AND `cashcred`='R' ";
    } else {
        $typecond = " AND `cashcred`='C' ";
    }
}

if (!empty($party)) {
    $partycond = " AND `party`='$party' ";
}
if (!empty($company)) {
    $companycond = " AND `company`='$company' ";
}
if (!empty($barcode)) {
    $barcodecond = " AND `articode`='$barcode' ";
}



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

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

mysqli_query($conn, "Create temporary table `invcomb`
                SELECT *,
                DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                AND `vouchdt` <= '$mtodate' 
                $partycond $typecond 
                ") or die(mysqli_error($conn));

$getpmain = mysqli_query($conn, "Select * from `invcomb` order by vouchdt") or die(mysqli_error($conn));
//print table($getpmain);
if (isset($_POST["view"])) {

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

        @media print{
            .noPrint{
                display: none;
            }
            table thead th{
                font-size:10px;
                font-weight:bold;
            }
            table tr td{
                font-size:9px;
            }
        }
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <form method="POST">

        <body>
            <div class="container" style="width:100%">
                <?php
//            if ($scope != "") {
                include("includes/head_rep.php");
                ?>

                <div class="row">
                    <div class="col-lg-12 pad">
                        <input type="submit" name="submit" value="Update" class="btn btn-danger" style="background-color:green; color: white; float:right; margin-right: 5%;width: 10%">

                        <table style="background: white;width: 100%" class=" table-condensed table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:5%">Sr</th>
                                    <th style="width:5%">Voucher</th>
                                    <th style="width:5%">Date</th>
                                    <th style="width:25%">Party</th>
                                    <th style="width:5%">Type</th>
                                    <th style="width:6%" class="text-right">Net.Amt</th>
                                    <th style="width:15%">Transporter</th>
                                    <th style="width:9%">No. Ctn</th>
                                    <th style="width:9%">LR No.</th>
                                    <th style="width:7%">LR Date</th>
                                    <th style="width:5%">Entry</th>
                                    <th style="width:5%">Bill Print</th>
                                    <th style="width:5%">LR View</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $nctr = 1;
                                $totamt = 0;
                                $totpack = 0;
                                $totbill = 0;
                                $totitmpr = 0;
                                $totiqtty = 0;
                                $linstyle = '';
                                $status = '';

                                while ($pmain = mysqli_fetch_array($getpmain)) {
                                    if ($pmain['cashcred'] == 'C') {
                                        $paytype = "Cash";
                                    } else {
                                        $paytype = "Credit";
                                    }
                                    $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `$custdb` WHERE `accode`='$pmain[party]'") or die(mysqli_error($conn));
                                    $party = mysqli_fetch_array($getparty);

                                    $trge = "<a target='_blank' href='sale.php?vouchno=$pmain[vouchno]&mode=edit'><i class='fa fa-pencil'></i></a>";
                                    $trge2 = "<a target='_blank' href='sale_pdf.php?vouchno=$pmain[vouchno]&mode=edit'><i class='fa fa-print'></i></a>";
                                    $trge3 = "";
                                    if ($pmain['imgpath'] != "") {
                                        $trge3 = "<a id=\"print\" class=\"btn btn-pad btn-square\" style=\"background-color:#5BC0DE;\" href=\"{$pmain['imgpath']}\" target=\"_blank\">View Image</a>";
                                    }

                                    $gettransp = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` WHERE `code`= '$pmain[transport]'") or die(mysqli_error($conn));
                                    $ltrans = mysqli_fetch_array($gettransp);

                                    Echo "<tr id='boldfont' style='$linstyle'>";
                                    Echo "<td>$nctr</td>";
                                    Echo "<td>$pmain[vouchno]</td>";
                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td>$party[name]</td>";
                                    Echo "<td>$paytype</td>";
                                    Echo "<td class='text-right'>$pmain[billamt]</td>";
                                    Echo "<td>";
                                    echo "<select class='form-control transporter select2' name='transport_$ctr' style='width:100%'>";
                                    echo "<option value='$ltrans[accode]'>$ltrans[name]</option>";
                                    echo "<option value=''></option>";
                                    $gtrans = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` order by name") or die(mysqli_error($conn));

                                    while ($ftrans = mysqli_fetch_array($gtrans)) {
                                        echo "<option value='$ftrans[code]'>$ftrans[name]</option>";
                                    }
                                    echo "</select>";
                                    Echo "</td>";
                                    Echo "<td><input type='text' class='form-control nocart' value='$pmain[nocart]' name='nocart_$ctr'> </td>";
                                    Echo "<td><input type='text' class='form-control lrno' value='$pmain[lrno]' name='lrno_$ctr'> </td>";
                                    Echo "<td><input type='date' class='form-control lrdt' value='$pmain[lrdate]'  name='lrdate_$ctr'> </td>";
                                    Echo "<input type='hidden' class='form-control  ' value='$pmain[vouchno]'  name='vouchno_$ctr'>"
                                    . " <input type='hidden' class='form-control  ' value='$pmain[src]'  name='src_$ctr'>";
//                                Echo "<td class='text-center'>$status</td>";
                                    Echo "<td class='text-center noPrint'>$trge</td>";
                                    Echo "<td class='text-center noPrint'>$trge2</td>";
                                    Echo "<td class='text-center noPrint'>$trge3</td>";
                                    Echo "</tr>";
                                    $ctr++;
                                    $nctr++;

                                    $totcgst = $totcgst + $pmain['cgst'];
                                    $totsgst = $totsgst + $pmain['sgst'];
                                    $totigst = $totigst + $pmain['igst'];
                                    $totiqty = $totiqty + $pmain['totqty'];
                                    $totnetamt = $totnetamt + $pmain['billamt'];
                                    $totsubtot = $totsubtot + $pmain['subtotal'];
                                    $totdiscamt = $totdiscamt + $pmain['totrdamt'];
                                    $totfretight = $totfretight + $pmain['freightamt'];
                                }
                                echo "<input type='hidden' name='items' value='$ctr'>";
                                $colr = "grey";
                                echo "<tr> ";
                                echo "<td align='right' colspan='5' style='color:$colr;' id='font'><h4><b>Grand Total</h4></b></td>";
//                                echo "<td align='right' style='color:red;' id='font'><h4><b>$totiqty</b></h4></td>";
//                            echo "<td align='right' style='color:red;' id='font'><h4><b>$totsubtot</b></h4></td>";
//                            echo "<td align='right' style='color:red;' id='font'><h4><b>$totdiscamt</b></h4></td>";
//                            echo "<td align='right' style='color:red;' id='font'><h4><b>$totfretight</b></h4></td>";
                                echo "<td align='right' style='color:red;' id='font'><h4><b>$totnetamt</b></h4></td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <?php
//            }
                ?>
            </div>

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

    </html>
    <?php
}
?>