<?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">
            <!--
            table{
                margin-top: 68px;
                border-collapse:collapse;
                width: 1000px;
                border: 0px;
            }
            table td{
                /*color: red;*/
                font-family:Arial, Helvetica, sans-serif;
                height: 18px;
                font-size:18px;
                padding:0px; /*5*/
            }
            -->

        </style>
    </head>

    <body><center>
            <?php
            $pgcount=0;
            require_once("config.inc.php");
            require_once("DBConnector.php");
            $db = new DBConnector(); $dbinv = $db->GetConnection();

            $sql = "Select count(*) as mcount from s_salma where REF_NO='" . $_GET["invno"] . "'";
            $result = $db->RunQuery($sql);
            $row = $result->fetch();

            if ($row["mcount"] > 1) {
                exit("Duplicate Invoice No Detected. Please contact Supervisor.");
            }

            $copy = "";

            $sql_invpara = "SELECT * from invpara where COMCODE='" . $_SESSION['company'] . "'";
            $result_invpara = $db->RunQuery($sql_invpara);
            $row_invpara = $result_invpara->fetch();
            
             $sql_entrylog = "SELECT * from entry_log where refno='" . $_GET["invno"] . "'";
            $result_entrylog = $db->RunQuery($sql_entrylog);
            $row_entrylog = $result_entrylog->fetch();

            $sql = "Select * from s_salma where REF_NO='" . $_GET["invno"] . "'";

            $result = $db->RunQuery($sql);
            $row = $result->fetch();

            //echo $row["CANCELL"];
            if ($row["pirnt_serial"] == "0") {

                if ($row["CANCELL"] == "0") {
                    $sql = "update s_salma set pirnt_serial='1' where REF_NO='" . $_GET["invno"] . "'";
                    $result = $db->RunQuery($sql);
                } else {
                    $copy = "Cancelled";
                }
            } else if ($row["pirnt_serial"] > 0) {
                $sql = "select doc_print from userpermission where username = '" .$_SESSION['CURRENT_USER']. "' and docid = '27'";
                $result = $db->RunQuery($sql);
                $rowPrm = $result->fetch();
                if ($rowPrm["doc_print"] == "0") {

                    if ($row["CANCELL"] == "0") {
                        if($_SESSION['company'] !="K"){
                             exit("Cannot Print Twice !!!");
                        }
                       
                    } else {
                        exit("Canceled Invoice !!!");
                    }
                } else {
                    if ($rowPrm["doc_print"] == "1") {

                        if ($row["CANCELL"] == "0") {
                            $copy = "COPY";
                        } else {
                            $copy = "Cancelled";
                        }

                        $print_serial = $row["pirnt_serial"] + 1;
                        $sql = "update s_salma set pirnt_serial='" . $print_serial . "' where REF_NO='" . $_GET["invno"] . "'";
                        $result = $db->RunQuery($sql);
                    }
                }
            }

            $sql1 = "Select * from vendor where CODE='" . $row["C_CODE"] . "'";
            $result1 = $db->RunQuery($sql1);
            $row1 = $result1->fetch();

            $sql2 = "Select * from s_stomas where CODE='" . $row["DEPARTMENT"] . "'";
            $result2 = $db->RunQuery($sql2);
            $row2 = $result2->fetch();

            $TXTDEP = $row2["DESCRIPTION"];
            $rtxtinvno = $row["invno"];
            $rtxtordno = $row["ORD_NO"];

            $sql2 = "Select * from s_salrep where REPCODE='" . $row["SAL_EX"] . "'";
            $result2 = $db->RunQuery($sql2);
            $row2 = $result2->fetch();





            $rtxtrep = $row2["Name"];
            $rtxtSupCode = $row1["CODE"];
            $rtxtSupName = $row1["CODE"] . " " . $row["CUS_NAME"];
            $txtadd1 = $row1["ADD1"];
            
            $txtadd1 = substr($txtadd1, 0, 40);
            
            //$txtadd2 = $row1["ADD2"];
            $txtadd2 = $_GET["cus_address2"];
            $txtadd = $row1["ADD1"] . "</br>" . $row1["ADD2"];
            
            $rtxtdate = date("Y-m-d", strtotime($row["SDATE"]));
            $rtxttot = $row["GRAND_TOT"];


            $sql_salrep = "Select * from s_salrep where REPCODE=" . $row["SAL_EX"];
            $result_salrep = $db->RunQuery($sql_salrep);
            $row_salrep = $result_salrep->fetch();

            $VAT_per = $row["VAT"];


            $sql_para = "Select * from invpara  where COMCODE='" . $_SESSION['company'] . "'";
            $result_para = $db->RunQuery($sql_para);
            $row_para = $result_para->fetch();
            $isVat = 0;
            if ($row["VAT"] != "0") {
                $isVat = 1;
            }
//            $isVat = 1;
            ?>

            <table>
                 <tr>
                      <td>&nbsp;</td>
                 </tr>
                <tr>
                    <td colspan="2"><b>Customer</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $rtxtSupName; ?></td>
                    <td style='text-align:left'><b>User &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b> <?php if ($_SESSION['company']!="C")echo $row["use_name"]; ?>&nbsp;</td>
                    <td><b>Invoice</b></td>
                    <td colspan="2" style='text-align:right'><b><?php echo $row["REF_NO"]; ?></b></td>
                </tr>
                <tr>
                    <td colspan="2"><?php echo $txtadd1; ?></td>
                    <td style='text-align:left'><b>Sales Ex&nbsp;&nbsp;</b><?php echo $row_salrep["Name"]; ?></td>
                    <td><b>Date</b></td>
                    <td colspan="2" style='text-align:right'><?php echo $row["SDATE"]; ?></td>
                </tr>
                <tr>
                    <td colspan="2"><?php if ($isVat == 1) echo "VAT " . $row_para["VAT"] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$row["vat_no"]; ?></td>
                    <td align="center"><?php echo $row["ORD_NO"]; ?></td>
                    <td><b>Ent Time</b></td>
                    <td colspan="2" style='text-align:right'><?php echo $row_entrylog['stime'];
                    // if($_GET["isHideAdt"]=="false"){echo date("y-m-d H:i");}
                    
                    ?></td>
                </tr>
                <tr>
                    <td>TAX INVOICE</td>
                </tr>
                
                
                <?php
                
                
                $i = 1;
                $totsuntot = 0;

                $sql1 = "Select * from s_invo where REF_NO='" . $_GET["invno"] . "'";
                $result1 = $db->RunQuery($sql1);
               
               echo "<tr><td width=20%><b><u>Part No</u></b></td>
                <td width=23%><b><u>Brand</u></b></td>
                <td width=27%><b><u>Description</u></b></td>
                <td><b><u>Qty</u></b></td>
                <td align=center width=5%><b><u>Rate</u></b></td>
                <td align=right><b><u>Sub total</u></b></td></tr>";
                
                echo "<tr><td>&nbsp;</td><tr>";
                while ($row1 = $result1->fetch()) {
                    $sql_part = "Select * from s_mas where STK_NO='" . $row1["STK_NO"] . "'";
                    $result_part = $db->RunQuery($sql_part);
                    $row_item = $result_part->fetch();

                    // if (($VAT_per == "1") or ( $VAT_per == "2")) {
                    //     $vatr = 100 + $row["GST"];
                    //     $PRICE = $row1["PRICE"] / $vatr * 100;
                    // } else {
                        $PRICE = $row1["PRICE"];
                    // }
                    $discount1 = $PRICE * $row1["QTY"] * $row1["DIS_per"] / 100;
                    $subtot = ($PRICE * $row1["QTY"]) - $discount1;
                    
                    $partNo = $row_item["PART_NO"];
                    $partNo1 =  str_pad($partNo, 22, "?");
                    $partNo1 = str_replace("?", "&nbsp;", $partNo1);
                    
                    $brand = $row_item["BRAND_NAME"];
                    $brand1 = str_pad($brand, 22, "?");
                    $brand1 = str_replace("?", "&nbsp;", $brand1);
                    $descript = substr($row_item["DESCRIPT"], 0, 26);
                    
//                    echo "<tr><td colspan=3 width=70%>" . $partNo1 . " " . $brand1 . " " . $row_item["DESCRIPT"] . "</td><td>" . number_format($row1["QTY"], 2, ".", ",") . "</td><td align=right width=5%>" . number_format($PRICE, 2, ".", ",") . "</td><td align=right>" . number_format($subtot, 2, ".", ",") . "</td></tr>";
                    echo "<tr><td width=20%>" . $partNo1 . "</td><td width=23%>" . $brand1 . "</td><td width=27%>" . $descript . "</td><td>" . number_format($row1["QTY"], 2, ".", ",") . "</td><td align=right width=5%>" . number_format($PRICE, 2, ".", ",") . "&nbsp;&nbsp;&nbsp;</td><td align=right>" . number_format($subtot, 2, ".", ",") . "</td></tr>";
//                    echo "<tr><td colspan=3 width=70%><table><tr><td>" . $partNo . "</td><td>" . $row_item["BRAND_NAME"] . "</td><td>" . $row_item["DESCRIPT"] . "</td></tr></table><td>" . number_format($row1["QTY"], 2, ".", ",") . "</td><td align=right width=5%>" . number_format($PRICE, 2, ".", ",") . "</td><td align=right>" . number_format($subtot, 2, ".", ",") . "</td></tr>";
                    $totsuntot = $totsuntot + $subtot;
                    $i = $i + 1;
                }
                while ($i < 16) {
                    echo "<tr><td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td></tr>";
                    $i = $i + 1;
                }
                $pgcount=$pgcount+1;
                ?>
                <tr><td>&nbsp;</td><tr>
                        <tr><td colspan="2"><?php if($_GET["isHideAdt"]=="false"){echo $copy;} ?></td><td colspan="3" style='text-align:right'><b>Sub Total</b> :</td></td><td style='text-align:right'><b><?php echo number_format($row["AMOUNT"]+$row["DISCOU"], 2, ".", ","); ?></b></td></tr>
                <tr><td colspan="2"><td colspan="3" style='text-align:right'><b>Discount</b> :</td></td><td style='text-align:right'><b><?php echo number_format($row["DISCOU"], 2, ".", ","); ?></b></td></tr>
                <tr><td colspan="2"><td colspan="3" style='text-align:right'><b>Grand Total</b> :</td></td><td style='text-align:right'><b><?php echo number_format($row["GRAND_TOT"], 2, ".", ","); ?></b></td></tr>
                <?php if ($isVat == 1) { ?><tr><td colspan="2"><td colspan="3" style='text-align:right'><b>Vat Value (<?php echo $row["GST"] . "%"; ?>)</b> :</td></td><td style='text-align:right'><b><?php echo number_format($row["VAT_VAL"], 2, ".", ","); ?></b></td></tr><?php } ?>
               
            </table>

    </body>
</html>


