<?php session_start();
if(!isset($_SESSION["UserName"])){
echo "Invalid Login";
exit();
}

require_once ("connection_sql.php");
session_start(); 
$sql = "select * from  invpara where  COMCODE='".$_SESSION['company']."' "; 
$sql = $conn->query($sql);
if (!$row1 = $sql->fetch()) {
    exit('Network Error Please Login Again......!!!');
}

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 
                require_once 'connection_sql.php';
		
		$sql="Select count(*) as mcount,C_CODE from s_salma_mas where tmp_no='".$_GET["tmp_no"]."'";  
                
                $result = $conn->query($sql);
                $row = $result->fetch();
                
                	$sqlout="Select sum(GRAND_TOT-TOTPAY) as out1 from s_salma_mas where C_CODE='".$row["C_CODE"]."' and cancell='0'";
                 
                $resultout = $conn->query($sqlout);
                $rowout = $resultout->fetch();
		
		if ($row["mcount"]>1){
			exit("Duplicate Invoice No Detected. Please contact Supervisor.");	
		}
/*
                $sql="Select aprvd from s_salma where REF_NO='".$_GET["invno"]."' and aprvd ='0' and TYPE='CR' and CANCELL='0'";
		$result =$db->RunQuery($sql);
		$row = mysqli_fetch_array($result);		
		
		if ($row){
                    exit("Invoice (".$_GET["invno"] . ") is not approved!");	
		}
                
                $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="";
		
		$sql_invpara="SELECT * from invpara where COMCODE='".$_SESSION['company']."'";
                
                $result = $conn->query($sql_invpara);
                $row_invpara = $result->fetch();
		
		$sql="Select * from s_salma_mas where tmp_no='".$_GET["tmp_no"]."'";
		
                $result = $conn->query($sql);
                $row = $result->fetch();			
        
	//echo $row["CANCELL"];
		if ($row["pirnt_serial"]=="0"){
			
			if ($row["CANCELL"]=="0"){	
				$sql="update s_salma_mas set pirnt_serial='1' where tmp_no='".$_GET["tmp_no"]."'";
                                $conn->exec($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_mas set pirnt_serial='".$print_serial."' where tmp_no='".$_GET["tmp_no"]."'";
                                        $conn->exec($sql);	
//				}
//			}	
			
		} 
	
		$sql1="Select * from vendor where CODE='".$row["C_CODE"]."'";
                
                $result = $conn->query($sql1);
                $row1 = $result->fetch();
		
		$sql2="Select * from s_stomas where CODE='".$row["DEPARTMENT"]."'";
                
                $result = $conn->query($sql2);
                $row2 = $result->fetch();                
				
		$TXTDEP= $row2["DESCRIPTION"];
		$rtxtinvno= $row["invno"];
		$rtxtordno= $row["ORD_NO"]; 
		
		$sql2="Select * from s_salrep where REPCODE='".$row["SAL_EX"]."'";
                $result = $conn->query($sql2);
                $row2 = $result->fetch(); 
		
		
			
			
			
		$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 = $conn->query($sql_salrep);
                $row_salrep = $result->fetch();                 
		
		$VAT_per=$row["VAT"];
		
                
		$sql_para="Select * from invpara  where COMCODE='".$_SESSION['company']."'";
                $result = $conn->query($sql_para);
                $row_para = $result->fetch();                
		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"];
    
		}
                
//                $sql1 = "Select CODE from s_submas where CODE='".$row["DEPARTMENT"]."'";
//                $result = $conn->query($sql1);
//                $rowStore = $result->fetch();

	?>
<table width="1000"  border="0">
  <tr>
    <td height="50" colspan="3"><span class="style6"><?php echo $row_para["COMPANY"]; ?></span><br />
   <span class="style8"> <?php 
   if (($_GET["vat"]=="true")&&($_GET["isHideVat"]=="false")){
   		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>
    <td colspan="4" align="center"><table width="500" border="0">
      <tr>
          <?php 
              echo "<td width=\"402\"><span class=\"style8\">".$row_para["ADD1"].", ".$row_para["ADD2"].", " .$row_para["ADD3"] . "</span></td>";
          ?>
        
      </tr>
      <tr>
        <td><span class="style8"><?php echo $row_para["TELE"]; ?></span></td>
      </tr>
    </table></td>
  </tr>
  <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">Department: </td>
    <td width="186"><?php echo $row["DEPARTMENT"]; ?></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">TEL:</td>
          <td>&nbsp;</td>
          <td><?php  echo  $row1["TELE1"];  ?></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">Installments</span></td>
        <td width="82"></td>
        <td width="12">:</td>
        <td width="201"><span class="style8"><?php echo $row["insmnt"]; ?></span></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">DownPayment</span></td>
        <td></td>
        <td>:</td>
        <td><span class="style8"><?php echo number_format($row["dwnPmnt"], 2, ".", ","); ?></span></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   width="50" cellspacing="0"><tr><td></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;
                $flgMsg = 0;
		
                $exploded=explode("_",$_GET["invno"]);
                
	  	$sql1="Select * from s_invo where REF_NO='".$exploded[0]."'";

                    foreach ($conn->query($sql1) as $row1) {
        
//			$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($row_part["GROUP1"] == "OTR"){
//                            $flgMsg = 1;
//                        }
                        
			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"]."-(".$row1["barcode"].")</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\">".floatval($row1["Print_dis1"])."</span></td>
						<td align=\"right\"  width=50></td>";
			
//						$discount1=$PRICE*$row1["QTY"]*$row1["DIS_per"]/100;
                                                $disFactor = (100-$row1["Print_dis1"])*(100-$row1["Print_dis2"])/10000;
						$discount1=$PRICE*$row1["QTY"]*$disFactor;
                                                $subtot = $discount1;
//						$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<7){
			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><span class="style6"><?php echo $copy; ?></span></td>
    <td colspan="5"><?php //if ($flgMsg == "1") { $msg = "Above Tyres are guaranteed against manufacturing defects for a period of 12 months from the date of sale.";  echo $msg;} ?></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>&nbsp;</td>
    <td width="98">&nbsp;</td>
    <td width="96">&nbsp;</td>
    <td width="104">&nbsp;</td>
    <td>&nbsp;</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>Additional Discount:<span class="style7">
            <?php if ($row["addDisc"]>0){echo number_format($row["addDisc"], 2, ".", ",");} ?>
          </span></b></td>
          <td width="0">&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">Grand Total:
            <?php if ($rtxttot>0){echo number_format($rtxttot, 2, ".", ",");} ?>
          </span></b></td>
          <td width="0">&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">
    OUTSTANDING: 
        <?php 
    if ($rtxttot>0){echo number_format($rowout['out1'], 2, ".", ",");} 
    ?></td>
  </tr>
</table>
    
</body>
</html>
