<?php session_start();
?>
<!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 Material Requession</title>
        <style type="text/css">
            <!--


            .style5 {
                font-size: 12px
            }
            .companyname {
                color: #0000FF;
                font-weight: bold;
                font-size: 24px;

            }

            .com_address {
                color: #000000;
                font-weight: bold;
                font-size: 18px;
                text-align: left;
            }

            .heading {
                color: #000000;
                font-weight: bold;
                font-size: 20px;
                text-align: left;
            }

            -->
        </style>
    </head>

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

            $sql_head = "select * from invpara";
            $result_head = $db->RunQuery($sql_head);
            $row_head = mysqli_fetch_array($result_head);

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

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

            $sql = "select AMOUNT from s_salma where REF_NO='" . $_GET["invno"] . "'";
            $result = $db->RunQuery($sql);
            $rowSalmaAmount = mysqli_fetch_array($result);

            $sql = "select sum(subtot) as itemTotal FROM `s_invo` where REF_NO='" . $_GET["invno"] . "'";
            $result = $db->RunQuery($sql);
            $rowItemTotal = mysqli_fetch_array($result);

            if ($rowSalmaAmount["AMOUNT"] != $rowItemTotal["itemTotal"]) {
                exit("Error in SubTotal detected. Please contact Supervisor");
            }

            $copy = "";
            ?>


            <table width="922" height="428" border="0" >
                <?php
                //echo $_GET["invno"];

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

                if ($row["pirnt_serial"] == "0") {
                    
                } else if ($row["pirnt_serial"] > 0) {

                    if ($_SESSION['User_Type'] == "0") {

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

                            if ($row["CANCELL"] == "0") {
                                $copy = "CP";
                            } 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 = mysqli_fetch_array($result1);

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

                $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 = mysqli_fetch_array($result2);





                $rtxtrep = $row2["Name"];
                $rtxtSupCode = $row1["CODE"];
                $rtxtSupName = $row1["CODE"] . " " . $row1["NAME"];
                $txtadd1 = $row1["ADD1"];
                //$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 = mysqli_fetch_array($result_salrep);

                $VAT_per = $row["VAT"];


                $sql_para = "Select * from invpara  where COMCODE='" . $_SESSION['company'] . "'";
                $result_para = $db->RunQuery($sql_para);
                $row_para = mysqli_fetch_array($result_para);

                if ($row["VAT"] == "1") {
                    //$txtcusvat= "Customer VAT  " . $row1["vatno"];
                    $txtcusvat = "Customer VAT  " . $_GET["vat1"];
                    $txtcomvat = "VAT Reg. " . $row_para["VAT"];
                    $RTXTVAT = "VAT " . intval($row["GST"]) . "%  ";
                    //$RTXVATAMU = $row["BTT"];
                    $txttaxinv = "<b>TAX INVOICE</b>";
                    $txtsubtot = $row["AMOUNT"];
                    $txtsubtotdes = "Sub Total";
                }

                if ($row["SVAT"] != "0") {
                    //$txtcusvat= "Customer VAT  " . $row1["vatno"];
                    $txtcusvat = "Customer VAT  " . $_GET["vat1"];
                    $txtcomvat = "VAT Reg. " . $row_para["VAT"];
                    $RTXTVAT = "Suspended VAT " . intval($row["GST"]) . "%  ";
                    //$RTXVATAMU= $row["BTT"];
                    $txttaxinv = "<b>SUSPENDED TAX INVOICE</b>";
                    $txtsubtot = $row["AMOUNT"];
                    $txtsubtotdes = "Net Total";

                    $txtoursvat = "SVAT Reg. " . $row_para["svatno"];
                    //$txtcussavat= "Customer SVAT " . $row1["svatno"];
                    $txtcussavat = "Customer SVAT " . $_GET["vat2"];
                }

                $sql2 = "Select * from vendor where CODE='" . $row["C_CODE"] . "'";
                $result2 = $db->RunQuery($sql2);
                $row2 = mysqli_fetch_array($result2);
                ?>

                <tr>
                    <td style="width: 1200px">
                        <p style="margin: 0px;margin-left:300px;color: #189ce7;font-size: 55px" ><i><b><?php echo "TYRE WORLD"; ?></b></i></p>
                        <p style="margin: 0px;margin-left:350px;color: #189ce7;font-size: 25px" ><i><b><?php echo "Total Weel Solution"; ?></b></i></p>
                        <p style="margin: 0px;margin-left:350px;" ><i><b><?php echo $row_head["ADD1"]; ?></b></i></p>
                        <p style="margin: 0px;margin-left:390px;" ><i><b><?php echo $row_head["ADD2"] . '&nbsp;' . $row_head["ADD3"]; ?></b></i></p>
                        <p style="margin: 0px;margin-left:390px;" ><i><b>Hotline : <?php echo $row_head["TELE"]; ?>  </b></i></p>
                        <p style="margin: 0px;margin-left:370px;" ><i><b>Telephone : <?php echo $row_head["FAX"]; ?> <br><b></b></i></p>
                                        <br>
                                            <p style="margin: 0px;" ><i><b>INVOICE</i></p>

<!--<img style="margin-left: 700px;width: 300px;margin-top:  -155px;" src="images/tyreworld.PNG"/>-->
                                            </td>


                                            </tr>
                                            <tr>
                                                <th colspan="4" scope="col">

                                                    <table width="1000" cellpadding="0" cellspacing="1" border="0" style="margin-top: -50px;"><tr><td>
                                                                <table border="0">
                                                                    <tr>
                                                                        <td style="padding: 0px;">
                                                                            <div style="width: 655px;background-color: white;height: 140px; text-align: left;">
                                                                                <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Bill To  &nbsp;&nbsp; :   &nbsp;&nbsp; <?php echo $row['TYPE']; ?></p>
                                                                                <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Name &nbsp;&nbsp;&nbsp;:   &nbsp;&nbsp; <?php echo $row['CUS_NAME']; ?></p>
                                                                                <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Address &nbsp;:   &nbsp;&nbsp; <?php echo $row['C_ADD1']; ?></p>
                                                                                <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Phone &nbsp;&nbsp;:   &nbsp;&nbsp; <?php echo $row2['TELE1']; ?></p>
                                                                                <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">  &nbsp;   &nbsp;&nbsp;  </p>
                                                                                <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Serial No &nbsp;:   &nbsp;&nbsp; <?php echo $row['cardno']; ?></p>

                                                                            </div>
                                                                            <!--border-style: solid;-->
                                                                        </td>
                                                                        <td >
                                                                            <div  style="width: 267px;background-color: white;height: 140px;text-align: left; ">

                                                                                <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Invoice No :  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp;  <?php echo $row['REF_NO']; ?></p>
                                                                                <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Date : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp; <?php echo $row['SDATE']; ?></p>
                                                                                <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Vehicle No: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;  <?php echo $row['veheno']; ?></p>
                                                                                <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Milage(Km) :  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp; </p>
                                                                            </div>
                                                                        </td>

                                                                    </tr>
                                                                </table>
                                                            </td></tr></table>

                                                </th>
                                            </tr>

                                            </table>

                                            <table style="margin-top: -25px;" width="1000" cellpadding="0" border="1" cellspacing="0">
                                                <tr>
                                                    <th width="50" height="22" scope="col">S.R</th>
                                                    <th width="200" height="22" scope="col">Item Code</th>
                                                    <th width="600" height="22" scope="col">Item Description</th>
                                                    <th width="100" scope="col">Qty</th>
                                                    <th width="200" height="22" scope="col">Rate</th>
                                                    <th width="100" scope="col">Amount</th>
                                                    <th width="90" scope="col">Dis</th>
                                                    <th width="150" scope="col">Total Amount</th> 
                                                </tr>
                                                <?php
                                                $sr = 1;
                                                $sql1 = "Select * from s_invo where REF_NO='" . $_GET["invno"] . "'";
                                                $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\">" . $sr . "</span></td>
				<td><span width=100  class=\"style10\">" . $row1["STK_NO"] . "</span></td>
						<td><span width=500  class=\"style10\">" . $row1["DESCRIPT"] . "</span></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\">" . number_format($PRICE, 2, ".", ",") . "</span></td>
						<td align=\"right\"  width=50><span  class=\"style10\">" . floatval($row1["Print_dis1"]) . "</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>";


                                                    $sr += 1;
                                                    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"];
//} else {
// 	$txtdis2= (($totsuntot / (1 + $row["GST"] / 100)) / 100) * $row["DIS1"];
//}

                                                while ($i < 9) {
                                                    echo "<tr><td class=\"style10\">&nbsp;</td>
			<td class=\"style10\">&nbsp;</td>
			<td class=\"style10\">&nbsp;</td>
			<td class=\"style10\">&nbsp;</td>
			<td class=\"style10\">&nbsp;</td>
			<td class=\"style10\">&nbsp;</td>
			<td class=\"style10\">&nbsp;</td>
			<td class=\"style10\">&nbsp;</td></tr>";
                                                    $i = $i + 1;
                                                }
                                                ?>





                                            </table>



                                            <table width="1000" cellpadding="0" cellspacing="1" border="0" style="margin-top: 1px;"><tr><td>
                                                        <table border="0" class="style10">
                                                            <tr>
                                                                <td style="padding: 0px;">
                                                                    <div style="width: 345px;background-color: white;height: 110px; text-align: right;font-size: 18px">
                                                                        <b><p style="margin-right: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;" >Sub Total
                                                                            </p>
                                                                            <p style="margin-right: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Tax Rate</p>
                                                                            <p style="margin-right: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Other Costs </p>
                                                                            <p style="margin-right: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;font-size: 21px">Total Cost
                                                                            </p>
                                                                        </b>
                                                                    </div>

                                                                </td>
                                                                <td style="padding: 0px;">
                                                                    <div style="width: 195px;background-color: white;height: 110px;border-style: solid;text-align: right;border-width: thin;font-size: 18px;margin-top:-8px;">
                                                                        <b><p style="margin-right: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;" > 
                                                                                <?php
                                                                                if ($rtxttot > 0) {
                                                                                    echo number_format($rtxttot, 2, ".", ",");
                                                                                }
                                                                                ?></p>
                                                                            <p style="margin-right: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;"> <?php echo $row['VAT_VAL']; ?></p>
                                                                            <p style="margin-right: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;"> <?php echo "0.00" ?></p>
                                                                            <p style="margin-right: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;"> 
                                                                                <?php
                                                                                if ($rtxttot > 0) {
                                                                                    echo number_format($rtxttot, 2, ".", ",");
                                                                                }
                                                                                ?></p>
                                                                        </b>
                                                                    </div>

                                                                </td>
                                                                <td >
                                                                    <div  style="width: 450px;background-color: white;height: 110px;border-style: solid;text-align: left; border-width: thin;font-size: 18px;margin-top: -8px;">

                                                                        <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Free Checkup(Km):  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <?php echo $row['REFNO']; ?></p>
                                                                        <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">Next Alignment(Km) : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp; <?php echo $row['REFNO']; ?></p>
                                                                        <p style="margin-left: 20px;margin-top: 0px;margin-bottom: 0px;padding-top: 0px;padding-bottom: 4px;">For Wheel Alignment Only: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp; <?php echo $row['REFNO']; ?></p>
                                                                    </div>
                                                                </td>

                                                            </tr>
                                                        </table>
                                                    </td></tr></table>
                                            <table width="1000" border="0">
                                                 <tr> 
                                                    <td width="105"> </td> 
                                                </tr>
                                                <tr> 
                                                <td width="105"> </td> 
                                                </tr>
                                                <tr> 
                                                <td width="105"> </td> 
                                                </tr>
                                                <tr> 
                                                    <td width="105">Make All Checks Payble To Company Name "Genuine NN Enginneering(PVT)LTD"</td>
                                                 </tr>
                                                <tr>

                                                    <td width="105">Thank You For Your Business</td> 
                                                </tr>
                                            </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 class="hidden">
                                                    <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>

                                                </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 : " . $row["cre_pe"] . " Days";
                                                                    }
                                                                    ?></td>
                                                    <td>&nbsp;</td>
                                                    <td></td>
                                                    <td align="right">&nbsp;</td>
                                                    <td align="right">&nbsp;</td>
                                                </tr>


                                            </table>

                                            </body>
                                            </html>
