<?php
session_start();
if (!isset($_SESSION["UserName"])) {
    echo "Invalid Login";
    exit();
}
date_default_timezone_set('Asia/Colombo');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Print Invoice</title>
        <style type="text/css">
            <!--
            .style2 {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 14px;
            }
            .style4 {
                font-size: 14px;
                font-weight: bold;
            }
            .style5 {
                font-size: 18px;
                font-weight: bold;
            }
            .style6 {
                font-size: 24px;
                font-weight: bold;
            }
            .style7 {font-size: 24px}

            .style8 {
                font-size: 18px;
                font-weight: bold;
            }

            .style10 {
                font-size: 18px;
                font-weight: bold;
            }
            -->

        </style>
    </head>

    <body><center>
            <?php
            include './connection_sql.php';
            require_once("config.inc.php");
            require_once("DBConnector1.php");
            $db = new DBConnector();

            $poid = $_GET['invno'];
            $sql2 = "SELECT * FROM s_ordmas where REFNO = '$poid'";
            echo '<h1> ' . $poid . ' </h1>';
            $result = $db->RunQuery($sql2);
            ?>


            <table width="1000"  border="0">

                <tr>
                    <td height="44" colspan="3" align="right"><span class="style6"><u>INVOICE</u></span></td>
                    <td width="55" height="44" align="center">&nbsp;</td>
                    <td width="171" align="left">Sales Ex : <?php echo $row_salrep["Name"]; ?></td>
                    <td width="80" align="right">Entered By: </td>
                    <td width="186"><?php echo $row["use_name"]; ?></td>
                </tr>
                <tr>
                    <td height="110" colspan="3" valign="top"><table border="1" bordercolor="#000000" cellpadding="0" cellspacing="0"><tr><td width="428"><table width="500" border="0" >
                                        <tr>
                                            <td width="91"><span class="style4">CUSTOMER</span></td>
                                            <td width="8"><strong>:</strong></td>
                                            <td width="443"><span class="style5"><?php echo $rtxtSupName; ?></span></td>
                                        </tr>
                                        <tr>
                                            <td>&nbsp;</td>
                                            <td>&nbsp;</td>
                                            <td><?php echo $txtadd1; ?></td>
                                        </tr>
                                        <tr>
                                            <td height="21">&nbsp;</td>
                                            <td>&nbsp;</td>
                                            <td><?php echo $txtadd2; ?></td>
                                        </tr>
                                        <tr>
                                            <td height="21">&nbsp;</td>
                                            <td>&nbsp;</td>
                                            <td><?php
            if (($_GET["vat"] == "true") || ($_GET["svat"] == "true")) {
                echo $_GET["vat1"] . " " . $_GET["vat2"];
            }
            ?></td>
                                        </tr>
                                    </table></td></tr></table></td>
                    <td colspan="4" valign="top"><table border="1" bordercolor="#000000" cellpadding="0" cellspacing="0"><tr><td><table width="500" border="0">
                                        <tr>
                                            <td width="108"><span class="style4">INV. NO</span></td>
                                            <td width="9">:</td>
                                            <td width="146"><span class="style8"><?php echo $row["REF_NO"]; ?></span></td>
                                            <td width="82"><span class="style4">P. O. NO.</span></td>
                                            <td width="12">:</td>
                                            <td width="201"><?php echo $row["ORD_NO"]; ?></td>
                                        </tr>
                                        <tr>
                                            <td>&nbsp;</td>
                                            <td>&nbsp;</td>
                                            <td>&nbsp;</td>
                                            <td>&nbsp;</td>
                                            <td>&nbsp;</td>
                                            <td>&nbsp;</td>
                                        </tr>
                                        <tr>
                                            <td height="21"><span class="style4">INV. DATE</span></td>
                                            <td>:</td>
                                            <td><span class="style8"><?php echo $row["SDATE"]; ?></span></td>
                                            <td><span class="style4">VEH. NO.</span></td>
                                            <td>:</td>
                                            <td><?php echo $row["veheno"]; ?></td>
                                        </tr>


                                    </table></td></tr></table>
                        <span class="style2">    </span></td>
                </tr>
            </table>
            <table width="1000" height="21" border="0" cellspacing="0">
                <!--  <tr  bgcolor="#999999">
                    <td width="130" height="23"><span class="style1">STK No</span></td>
                    <td width="295"><span class="style1">Description</span></td>
                    <td width="158"><span class="style1">Rate</span></td>
                    <td width="135"><span class="style1">Quantity</span></td>
                    <td width="152"><span class="style1">Sub Total</span></td>
                    </tr> -->
                <tr>
                    <td width="100" align="left"><table border="1" width="100" cellspacing="0"><tr><td><span class="style4"><u>ITEM NO.</u></span></td></tr></table></td>
                    <td width="480" align="left"><table border="1"  width="480" cellspacing="0"><tr><td><span class="style4"><u>DESCRIPTION</u></span></td></tr></table></td>
                    <td width="50" align="right"><table border="1"  width="50" cellspacing="0"><tr><td align="right"><span class="style4"><u>QTY</u></span></td></tr></table></td>
                    <td width="120" align="right"><table border="1"  width="120" cellspacing="0"><tr><td align="right"><span class="style4"><u>RATE</u></span></td></tr></table></td>
                    <td width="50" align="right"><table border="1"  width="50" cellspacing="0"><tr><td><span class="style4"><u>DIS1%</u></span></td></tr></table></td>
                    <td width="50" align="right"><table border="1"  width="50" cellspacing="0"><tr><td><span class="style4"><u>DIS2%</u></span></td></tr></table></td>
                    <td width="100" align="right"><table border="1"  width="100" cellspacing="0"><tr><td align="right"><span class="style4"><u>AMOUNT</u></span></td></tr></table></td>
                </tr>
                <?php
                $i = 1;
                $totsuntot = 0;

                $sql2 = "SELECT * FROM s_ordmas where REFNO = '$poid'";
                echo '<p> ' . $poid . ' <p>';
                $result = $db->RunQuery($sql2);
                $result1 = $db->RunQuery($sql1);
                while ($row1 = mysqli_fetch_array($result1)) {
                    $sql_part = "Select * from s_mas where STK_NO='" . $row1["STK_NO"] . "'";
                    $result_part = $db->RunQuery($sql_part);
                    $row_part = mysqli_fetch_array($result_part);

                    if (($VAT_per == "1") or ( $VAT_per == "2")) {
                        if ($_GET["isHideVat"] == "false") {
                            $vatr = 100 + $row["GST"];
                            $PRICE = $row1["PRICE"] / $vatr * 100;
                        } else {
                            $PRICE = $row1["PRICE"];
                        }
                    } else {
                        $PRICE = $row1["PRICE"];
                    }



                    echo "<tr><td><span width=100  class=\"style10\">" . $row1["STK_NO"] . "</span></td>
						<td><span width=500  class=\"style10\">" . $row1["DESCRIPT"] . "</span></td>
                                                <td><div>abvd</div></td>
                                               <td align=\"right\"  width=50><span  class=\"style10\">" . number_format($row1["QTY"], 0, ".", ",") . "</span></td>
						<td  align=\"right\"  width=100><span  class=\"style10\">" . number_format($PRICE, 2, ".", ",") . "</span></td>
						
						<td align=\"right\"  width=50><span  class=\"style10\">" . floatval($row1["Print_dis1"]) . "</span></td>
						<td align=\"right\"  width=50><span  class=\"style10\">" . floatval($row1["Print_dis2"]) . "</span></td>";

                    $discount1 = $PRICE * $row1["QTY"] * $row1["DIS_per"] / 100;
                    $subtot = ($PRICE * $row1["QTY"]) - $discount1;
                    echo "<td align=\"right\"  width=100><span  class=\"style10\">&nbsp;&nbsp;&nbsp;&nbsp;" . number_format($subtot, 2, ".", ",") . "</span></td>";


                    echo "<td align=\"right\"><span class=\"style\"></span></td>";
                    //}
                    echo "</tr>";
                    $totsuntot = $totsuntot + $subtot;
                    $i = $i + 1;
                }

                if ($row["DIS1"] > 0) {
                    $txtspdis = "Special Discount   " . floatval($row["DIS1"]) . " %";
                }

                if ($VAT_per == "1") {
                    $RTXVATAMU = $row["AMOUNT"] * $row["GST"] / 100;
                } else {
                    $RTXVATAMU = "";
                }

//	if ($row["VAT"]=="0") {
                $txtdis2 = $totsuntot / 100 * $row["DIS1"];

                $sql1 = "Select * from s_ordmas where REFNO='" . $row["REFNO"] . "'";
                $result1 = $db->RunQuery($sql1);
                $row1 = mysqli_fetch_array($result1);
                while ($row1 = mysqli_fetch_array($result1)) {

                    $REFNO = $row["REFNO"];
                }
                $table .= "</table>";

                echo $table;
                ?>


            </table>
            <table width="1000" border="0">


                <tr>
                    <td colspan="4">
                        <?php
                        if ($_GET["svat"] == "true") {
                            echo "Our SVAT No : " . $row_invpara["svatno"];
                        }
                        ?>    </td>
                    <td width="105"></td>
                    <td width="51">&nbsp;</td>
                    <td width="144" align="right"></td>
                    <td width="288" align="right">&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="4">
                        <?php
                        if ($_GET["svat"] == "true") {
                            echo "Your SVAT No : " . $_GET["vat2"];
                        }
                        ?>    </td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td align="right">&nbsp;</td>
                    <td align="right">&nbsp;</td>
                </tr>
                <tr>
                    <td width="80">&nbsp;</td>
                    <td colspan="3"><span class="style6"><?php echo $copy; ?></span></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>
                        <?php
                        if ($_GET["isHideVat"] == "false") {
                            if ($_GET["vat"] == "true") {
                                echo "VAT " . intval($row["GST"]) . "%";
                            }
                            if ($_GET["svat"] == "true") {
                                echo "SVAT " . intval($row["GST"]) . "%";
                            }
                        }
                        ?>    </td>
                    <td align="right"><table width="250" border="0">
                            <tr>
                                <td width="175" align="right"><?php
                        if ($_GET["isHideVat"] == "false") {
                            if (($_GET["vat"] == "true") or ( $_GET["svat"] == "true")) {
                                echo $_GET["tax"];
                            }
                        }
                        ?></td>
                                <td width="66">&nbsp;</td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td>__________</td>
                    <td width="98">&nbsp;</td>
                    <td width="96">____________</td>
                    <td width="104">&nbsp;</td>
                    <td>____________</td>
                    <td></td>
                    <td align="right">&nbsp;</td>
                    <td align="right"><b>
                            <span class="style7">      </span></b>
                        <table width="250" border="0">
                            <tr>
                                <td width="175" align="right"><b><span class="style7">
                                            <?php
                                            if ($rtxttot > 0) {
                                                echo number_format($rtxttot, 2, ".", ",");
                                            }
                                            ?>
                                        </span></b></td>
                                <td width="62">&nbsp;</td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td>Checked By</td>
                    <td>&nbsp;</td>
                    <td>Customer</td>
                    <td>&nbsp;</td>
                    <td>Deliverd By</td>
                    <td></td>
                    <td align="right">Printed By:<?php echo $_SESSION["CURRENT_USER"]; ?> </td>
                    <td align="right"></td>
                </tr>
                <tr>
                    <td colspan="4"><?php
                                            if ($_GET["credper"] != "0") {
                                                echo "Credit Period : " . $_GET["credper"] . " Days";
                                            }
                                            ?></td>
                    <td>&nbsp;</td>
                    <td></td>
                    <td align="right">&nbsp;</td>
                    <td align="right">&nbsp;</td>
                </tr>
            </table>

    </body>
</html>
