
<?php session_start();

if ($_SESSION['company'] == "") { 
    exit("Please Login");
}
if ($_SESSION['CURRENT_USER'] == "") { 
    exit("Please Login");
}

// 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;
            font-weight: bold;
        }
        .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;
        }
        .style9 {
            font-size: 20px;
            font-weight: bold;
        }

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

    </style>
</head>

<body><center>
    <?php 
      require_once("connectioni.php");
    
    
    
    $sql="Select count(*) as mcount,REF_NO from s_salma where REF_NO='".$_GET["invno"]."'";
     $result = mysqli_query($GLOBALS['dbinv'], $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 =mysqli_query($GLOBALS['dbinv'], $sql);
    $rowSalmaAmount = mysqli_fetch_array($result);
    
    $sql = "select sum(subtot) as itemTotal FROM `s_invo` where REF_NO='".$_GET["invno"]."'";
    $result =$mysqli_query($GLOBALS['dbinv'], $sql);
    $rowItemTotal = mysqli_fetch_array($result);
    
    if ($rowSalmaAmount["AMOUNT"] != $rowItemTotal["itemTotal"]){
        exit("Error in SubTotal detected. Please contact Supervisor");  
    }
    
    $copy="";
    
    $sql_invpara="SELECT * from invpara ";
    $result_invpara =mysqli_query($GLOBALS['dbinv'], $sql_invpara);
    $row_invpara = mysqli_fetch_array($result_invpara);
    
    $sql="Select * from s_salma where REF_NO='".$_GET["invno"]."'";
    
    $result =mysqli_query($GLOBALS['dbinv'], $sql);
    $row = mysqli_fetch_array($result);         
    
    //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){
        
        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 =mysqli_query($GLOBALS['dbinv'], $sql);
            }
        }   
        
    } 
    
    $sql1="Select * from vendor where CODE='".$row["C_CODE"]."'";  
    $result1 =mysqli_query($GLOBALS['dbinv'], $sql1);
    $row1 = mysqli_fetch_array($result1);   
    
    $sql2="Select * from s_stomas where CODE='".$row["DEPARTMENT"]."'";
    $result2 =mysqli_query($GLOBALS['dbinv'], $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 =mysqli_query($GLOBALS['dbinv'], $sql2);
    $row2 = mysqli_fetch_array($result2);
    
    
    
    
    
    $rtxtrep = $row2["Name"];
    $rtxtSupCode= $row1["CODE"];
    $rtxtSupName=  $row1["CODE"]." ".$row["CUS_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 =mysqli_query($GLOBALS['dbinv'], $sql_salrep);
    $row_salrep = mysqli_fetch_array($result_salrep);
    
    $VAT_per=$row["VAT"];
    
    
    $sql_para="Select * from invpara   ";
    $result_para =mysqli_query($GLOBALS['dbinv'], $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"];
        
    }

    ?>
    
    <table width="1000"   border="0">
        <tr>
            <td  style="width: 500px">
                <p style="margin: 0px;color: #189ce7;font-size: 31px" ><i><b><?php echo "UNIVERSAL TYRE IMPORTS PRIVATE LIMITED" ; ?></b></i></p>
                <p style="margin: 0px;" ><i><b><?php echo $row_para["ADD1"].", ".$row_para["ADD2"].", " .$row_para["ADD3"] ?></b></i></p>
                <!--  <p style="margin: 0px;" ><i><b><?php echo $row_para["ADD1"].", ".$row_para["ADD2"].", " .$row_para["ADD3"]  ?></b></i></p>-->
                <p style="margin: 0px;" ><i><b>TEL : <?php echo $row_para["TELE"]; ?> </b></i></p>
                <p style="margin: 0px;" ><i><b>  E-mail : <?php echo $row_para["EMAIL"]; ?> </b></i></p>
                <p style="margin: 0px;" ><i><b>  Web : www.tyreworld.lk</b></i></p>
                <p style="margin: 0px;" ><i> <b>PV-118764</b></i></p>
                <br>
                <p style="margin: 0px;font-size:25px" ><i><b>INVOICE -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <?php if($row["TYPE"]=="CR"){
       echo "<b style='font-size:25px;'> CREDIT</b>";
    }else if($row["TYPE"]=="CA"){
       echo "<b style='font-size:25px;'> CASH</b>";
        } ?></i></p> 
                
             

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


            </tr>
            

        </table>
        <table width="1000"  border="0" style="margin-top:-60px;">
          <!--<tr> -->
             
              
              <!--<tr>-->
              <!--  <td   colspan="3"> <br />-->
              <!--      <span class="style8">-->
                    <?php 
                    // if ($_GET["vat"]=="true"){
                    //     echo "TAX INVOICE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$row_invpara["VAT"];
                    // } 
                    
                    // if ($_GET["svat"]=="true"){
                    //     echo "SUSPENDED TAX INVOICE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$row_invpara["VAT"];
                    // }   
                    ?>
            <!--        </span>-->
            <!--    </td> -->
            <!--</tr>-->
            
        <!--</tr>-->
        <tr>
            <td height="44" colspan="3" align="right"><span class="style6"><u> </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>Address :</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">Tel No:</td>
                  <td>&nbsp;</td>
                  <td><?php if($row1['TELE1']!==""){
                                echo $row1['TELE1'];
                            }else{
                                echo $row['teleno'];
                            } 
                            ?></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="style9"><?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" style="margin-top:-30px;">
    <!--  <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="115" align="left"><table border="1" width="115" cellspacing="0"><tr><td align="center"><span class="style4"><u>ITEM NO.</u></span></td></tr></table></td>
        <td width="470" align="left"><table border="1"  width="470" cellspacing="0"><tr><td align="center"><span class="style4"><u>DESCRIPTION</u></span></td></tr></table></td>
        <td width="90" align="left"><table border="1"  width="120" cellspacing="0"><tr><td align="center"><span class="style4"><u>BRAND</u></span></td></tr></table></td>
        <td width="50" align="right"><table border="1"  width="50" cellspacing="0"><tr><td align="center"><span class="style4"><u>QTY</u></span></td></tr></table></td>
        <td width="120" align="right"><table border="1"  width="90" cellspacing="0"><tr><td align="center"><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 align="center"><span class="style4"><u>DIS</u></span></td></tr></table></td>
        <td width="100" align="right"><table border="1"  width="100" cellspacing="0"><tr><td align="center"><span class="style4"><u>AMOUNT</u></span></td></tr></table></td>
    </tr>
    <tr>
        <td width="115" align="left"> </td> 
    </tr>
    <?php 
    $i=1;
    $totsuntot=0;
    
    $sql1="Select * from s_invo where REF_NO='".$_GET["invno"]."' order by id";
    $result1 =mysqli_query($GLOBALS['dbinv'], $sql1);
    while ($row1 = mysqli_fetch_array($result1)){
        $sql_part="Select * from s_mas where STK_NO='".$row1["STK_NO"]."'";
        $result_part =mysqli_query($GLOBALS['dbinv'], $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><span width=510  class=\"style11\">".$row_part["BRAND_NAME"]."</span></td>
        <td align=\"right\"  width=50><span  class=\"style10\">".number_format($row1["QTY"], 0, ".", ",")."</span></td>
        <td  align=\"right\"  width=90><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>";
        
        
        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"];
        //} else {
       //   $txtdis2= (($totsuntot / (1 + $row["GST"] / 100)) / 100) * $row["DIS1"];
        //}
    
    while ($i<5){
        echo "<tr><td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td></tr>";
        $i=$i+1;
    }
    ?>
    
    
</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" class="style5"><?php  if ($row['batterytotal']!= 0.00){
            echo "Battery Scarb";
            
        }
           ?> </td>
        <td align="right"><b>
          <span class="style7">      </span></b>
          <table width="250" border="0">
            <tr>
              <td width="175" align="right"><b><span class="style5"> 
                <?php  if ($row['batterytotal']!= 0.00){ echo "("; echo number_format($row['batterytotal'], 2, ".", ",");echo ")";} ?> 
            </span></b></td>
            <td width="62">&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="250" align="right"><b><span class="style7">
                <?php 
                if ($rtxttot>0){
                    if ($row['batterytotal']!= 0.00){
                     echo "Total : "; echo number_format($rtxttot-$row['batterytotal'], 2, ".", ","); 
                    }else{
                      echo "Total : ";  echo number_format($rtxttot, 2, ".", ","); 
                    }
                } 
                ?>
            </span></b></td>
            
        </tr>
        
    </table>
    </td>
    </tr>
<tr>
    <td>Prepared By</td>
    <td>&nbsp;</td>
    <td>Customer</td>
    <td>&nbsp;</td>
    <td>Deliverd By</td> 
    <td colspan="3" align="left">Printed By:<?php echo $_SESSION["CURRENT_USER"] ?> </td>
     
</tr>
<tr>
    <td colspan="4"><?php 
     
      $str=$row["remark1"];
        echo "<p style=\" width: 500px;\">Serial No : ".$str."</p>";
        // echo "<p style=\" word-wrap: break-word;width: 500px;\">Serial No : ".wordwrap($str,15," \n")."</p>";
    
    
    
    ?></td> 
     <!--<td colspan="6" ><b><p >-->
     <?php
    //  $str=$row["remark1"];
    // if ($_GET["credper"]!="0"){
        // echo "Serial No : ".$str." ";
    // }
    ?>
    <!--</p></b></td>-->
</tr>
<tr  style="font-size:18px;">
    <td colspan="2" ><b><p style="margin-top:-21px;"><?php
    // if ($_GET["credper"]!="0"){
        echo "Credit Period : ".$_GET["credper"]." Days";
    // }
    ?></p></b></td> 
     <td colspan="2"><b><p style="margin-top:-25px;"><?php 
    
    $tot = $row["dele_no"];
    if($row["dele_no"] >0){
        echo "Current Mileage : $tot KM";
    }
    
    ?></p></b></td> 
    <td colspan="3"><b><p style="margin-top:-42px;"><?php 
    
    $tot1 = 1500;
    $tot = $row["dele_no"]+$tot1;
    if($row["dele_no"] >0){
        echo "<p  >Free Alignment : $tot KM</p>";
    }
    
     ?></p></b></td> 
    <td colspan="4" ><b><p style="margin-top:-42px;" ><?php  
    $tot1 = 5000;
    $tot = $row["dele_no"]+$tot1;
    if($row["dele_no"] >0){
        echo "<p  >Next Alignment : $tot KM</p>";
    }
    
    
    ?></p></b></td> 
</tr>
 <tr >
    
    <td colspan="8" ><p style="font-size:15px;margin-top:-10px;"> Please Make All Cheque Payments in Favour of  GENUINE NN ENGINEERING (PVT) LTD - PV-118764</p></td>  
</tr>
<tr >
     
   <td colspan="8" ><center><P style="font-size:27px;margin-top:-18px;"><B>Thank You For Your Continues Valued Patronage !</B></P></center></td> 
</tr>

 
</table>

</body>
</html>
