<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Invoice Print</title>

  <!-- Normalize or reset CSS with your favorite library -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css">

  <!-- Load paper.css for happy printing -->
  <!--<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/paper-css/0.4.1/paper.css">-->

  <!-- Set page size here: A5, A4 or A3 -->
  <!-- Set also "landscape" if you need -->
  <!-- Custom styles for this document -->
  <link href='https://fonts.googleapis.com/css?family=Tangerine:700' rel='stylesheet' type='text/css'>
   
  <style>
  
  table {
   border-collapse: collapse;
}
table tr td {
   border: 1px solid gray;
}
        @page { margin: 0 }
        body { margin: 0 }
        .sheet {
          margin: 0;
          overflow: hidden;
          position: relative;
          box-sizing: border-box;
          page-break-after: always;
        }
        
        /** Paper sizes **/ 
        body.A3               .sheet { width: 297mm; height: 419mm }
        body.A3.landscape     .sheet { width: 420mm; height: 296mm }
        body.A4               .sheet { width: 210mm; height: 296mm }
        body.A4.landscape     .sheet { width: 297mm; height: 209mm }
        body.A5               .sheet { width: 148mm; height: 209mm }
        body.A5.landscape     .sheet { width: 210mm; height: 147mm }
        body.letter           .sheet { width: 216mm; height: 279mm }
        body.letter.landscape .sheet { width: 280mm; height: 215mm }
        body.legal            .sheet { width: 216mm; height: 356mm }
        body.legal.landscape  .sheet { width: 357mm; height: 215mm }
        body.newprint1               .sheet { width: 139.7mm; height: 100% }
        body.newprint1.landscape     .sheet { width: 139.7mm; height: 139.7mm }
        body.newprint2               .sheet { width: 80mm; height: 100% }
        body.newprint2.landscape     .sheet { width: 80mm; height: 80mm }
        
        /** Padding area **/
        .sheet.padding-10mm { padding: 10mm }
        .sheet.padding-15mm { padding: 15mm }
        .sheet.padding-20mm { padding: 20mm }
        .sheet.padding-25mm { padding: 25mm }
        
        /** For screen preview **/
        @media screen {
          body { background: #e0e0e0 }
          .sheet {
            background: white;
            box-shadow: 0 .5mm 2mm rgba(0,0,0,.3);
            margin: 0mm auto;
            float:left;
          }
        }
        
        /** Fix for Chrome issue #273306 **/
        @media print {
                   body.A3.landscape { width: 420mm }
          body.A3, body.A4.landscape { width: 297mm }
          body.A4, body.A5.landscape { width: 210mm }
          body.A5                    { width: 148mm }
          body.newprint1             { width: 139.7mm;height: 100% ; float:left;}
          body.newprint2             { width: 80mm;height: 100% ; float:left;}
          body.letter, body.legal    { width: 216mm }
          body.letter.landscape      { width: 280mm }
          body.legal.landscape       { width: 357mm }
        }
        
        .center {
            text-align: center;
        } 
        .right {
            text-align: right;
        }
  </style>
</head>

 <?php
            session_start();
            require_once ("connection_sql.php");
            
            $sql = "update s_salma set pirnt_serial='1' where REF_NO='" . $_GET["invno"] . "'";
            $res_inv = $conn->query($sql);
            
            
            $sql = "Select * from s_salma where REF_NO='" . $_GET["invno"] . "'";
            $result = $conn->query($sql);
            $row = $result->fetch();
            
            $sql_out = "Select sum(GRAND_TOT-TOTPAY) AS OUTS from s_salma where C_CODE='" . $row["C_CODE"] . "' AND CANCELL='0'"; 
            $result_out = $conn->query($sql_out);
            $row_out = $result_out->fetch();
            
            $sql1 = "Select * from vendor where CODE='" . $row["C_CODE"] . "'";
            $result1 = $conn->query($sql1);
            $row1 = $result1->fetch();
            
            $sql2 = "Select * from s_salrep where REPCODE='" . $row["SAL_EX"] . "'";
            $result2 = $conn->query($sql2);
            $row2 = $result2->fetch();
            
            $sql_la = "Select * from s_labour where CODE='" . $row["labour"] . "'"; 
            $result_la = $conn->query($sql_la);
            $row_la = $result_la->fetch();
            
            $sql3 = "Select sum(PRICE*QTY)  as pricetot,count(*) as COUNT from s_invo where REF_NO='" . $_GET["invno"] . "'";
            $result3 = $conn->query($sql3);
            $row3 = $result3->fetch();
             
            $sinvtot=$row3["pricetot"];
            $count=$row3["COUNT"];
            
            $sql_invpara = "SELECT * from invpara where flag='".$_SESSION['company']."'";  
            $result_invpara = $conn->query($sql_invpara);
            $row_invpara = $result_invpara->fetch(); 
            
            $rtxtrep = $row2["Name"];
            $rtxtSupCode = $row1["CODE"];
            $rtxtSupName = $row1["NAME"];
            $txtadd = $row1["ADD1"] . " " . $row1["ADD2"];
            $rtxtdate = date("Y-m-d", strtotime($row["SDATE"]));
            $grandtot = $row["GRAND_TOT"];
            $totpay = $row["TOTPAY"];

            $VAT_per = $row["VAT"];
            $disamou=$row["DISCOU"];
            $txtsubtot = $row["AMOUNT"];

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

            if ($row["SVAT"] != "0") {
                $txtcusvat = "Customer VAT  " . $row["vat_no"];
                $txtcomvat = "VAT Reg. " . $row_invpara["VAT"];
                $RTXTVAT = "Suspended VAT " . $row["GST"] . "%  ";
                $RTXVATAMU = $row["BTT"];
                $txttaxinv = "<b>SUSPENDED TAX INVOICE</b>";
                $txtsubtot = $row["AMOUNT"];
                $txtsubtotdes = "Nett Total";

                $txtoursvat = "SVAT Reg. " . $row_invpara["svatno"];
                $txtcussavat = "Customer SVAT " . $row["s_vat_no"];
            }

    $perpage=4;
    $pages=$count/4;

?>
<!--"-->
<body class="newprint2 portrait" onload="window.print();">

 <?php 
 $rowstart=0;
//  for ($pagecount = 0; $pagecount < $pages; $pagecount++) {
 ?>
  <section class="sheet">

    <table style='width:100%;padding:10px;' cellspacing="0" border="0" >
        <tr>
            <td>
            <?php
                 
                 if($row['CANCELL'] =="1"){?>
                 <center><p style="margin: 0px;font-size:24px;color:red" ></p></center>
              <?php  }else{?>
                    <center><p style="margin: 0px;font-size:18px; text-align:right;" >
                <?php   }?>
                
                    <b>
                        <?php 
                                    // if($row["VAT"]=="1"){
                                    //     if ($row["TYPE"] == "CR") {
                                    //         echo "CREDIT";
                                    //     }else{
                                    //         echo "CASH ";
                                    //     } 
                                    //     if($row['CANCELL'] =="1"){
                                    //         echo "CANCELED TAX INVOICE  ";
                                    //     }else{
                                    //          echo "TAX INVOICE  ";
                                    //     }
                                        
                                       
                                    // }else{
                                    //     if ($row["TYPE"] == "CR") {
                                    //         echo "CREDIT";
                                    //     }else{
                                    //         echo "CASH ";
                                    //     } 
                                    //      if($row['CANCELL'] =="1"){
                                    //         echo "CANCELED INVOICE  ";
                                    //     }else{
                                    //           echo "INVOICE  ";
                                    //     }
                                        
                                    // }
                                    
                                    // if($row['pirnt_serial'] =="0"){
                                    //         echo " - COPY";
                                    // }
                        ?> 
            </td>
        </tr>
        <tr>
            <td>
                <center><p style="margin: 0px;!important;font-size: 18px;" ><b><?php echo $row_invpara["COMPANY"] ; ?></b></p> </center>
                <center><p style="margin: 0px;!important;font-size: 13px;" ><b><?php echo $row_invpara["ADD1"].','.$row_invpara["ADD2"] ; ?></b></p></center>
                <center><p style="margin: 0px;!important;font-size: 18px;" ><b><?php echo "Tel -".$row_invpara["TELE"] ; ?></b></p></center> 
                <center><p style="margin: 0px;!important;font-size: 18px;" ><b><?php if($row["VAT"]=="1"){ 
               echo "VAT Registration No. - " . $row_invpara["VAT"];
                  }?></b></i></p></center> 
            </td>  
        </tr>
       
   </table>   
   
   <table style='width:100%;padding:10px;' cellspacing="0" border="0" >
       <tr> 
             <td colspan='2' style='width:50%;text-align:left;border:none !important'><b>Inv No:</b> <?php echo $row['REF_NO']; ?></td> 
        </tr>
         <tr> 
             <td colspan='2' style='border:none !important'><b>Customer Name:</b> <?php echo $row['CUS_NAME']; ?> -<?php echo  strtoupper($row['C_ADD1'])?></td> 
        </tr>
        <tr> 
            
             <td colspan='2' style='width:50%;text-align:left;border:none !important'><b>Date:</b> <?php echo $row['SDATE']; ?></td> 
        </tr>
    </table>
    
    <table style='width:100%;padding:10px;' cellspacing="0" border="0" >
        <tr style="font-size:17px;border:1px solid black !important;">
             
             <td colspan='2'><b>Qty</b></td> 
             <td align="right"><b>Price</b></td>
             <td align="right"><b>Dis (%)</b></td>  
             <!--<td align="right"><b>Dis 2(%)</b></td> -->
             <td align="right"><b>Unit Total</b></td>
        </tr>
        <?php
        $rowperpage=4;
        
        $i=1;  
        $part="";
        $qty=0;
        $TOTPRICE=0;
        $unittot=0;
        $distot=0;
        $disgtot=0;
        $unitgtot=0;
        $rr=0;
        $rrtot=0;
        // limit".$rowstart.",".$rowperpage
        $sql1 = "Select * from s_invo where REF_NO='" . $_GET["invno"] . "' order by id DESC  " ;  
        foreach ($conn->query($sql1) as $row1) {
            $sql_smas = "SELECT * from s_mas where STK_NO='".$row1['STK_NO']."'";
            $result_smas = $conn->query($sql_smas);
            $row_smas= $result_smas->fetch(); 
            
            if (($row["VAT"] == "1") or ($row["VAT"] == "2")) {
                $vatr = 100 + $row["GST"];
                $PRICE = $row1["PRICE"] / $vatr * 100;
                // $PRICE = $row1["PRICE"] ;
                
            } else {
                $PRICE = $row1["PRICE"];
            }
            
            $unittot=$PRICE*$row1['QTY']-($PRICE*$row1['QTY']*$row1["DIS_per"]/100);
            $distot=$PRICE*$row1['QTY']*$row1["DIS_per"]/100;
            $rr=$PRICE*$row1['QTY'];
            ?>
            <tr style="font-size:15px;">   
                <?php
                    if($row1['DESCRIPT']=="PRODUCT"){?>
                        <td class="left" colspan='12'><b><?php echo $row1['DESCRIPT']; ?> </b></td>
                <?php }else{?>
                    <td class="left" colspan='12'><b><?php echo $row1['DESCRIPT']; ?></b></td>
                  
                
               <?php } ?>
                
            </tr>
            <tr style="font-size:18px;">   
                  <td align="left" colspan='12' ><?php echo number_format($row1['QTY'], 0, ".", ","); ?> <?php echo ' * ' ?><?php echo number_format($PRICE, 2, ".", ","); ?>  <?php echo ' * (' ?><?php echo number_format($row1["DIS_per"], 2, ".", ","); ?><?php echo ' %)' ?> <?php echo '=' ?> <?php echo number_format($unittot, 2, ".", ","); ?></td> 
                  <!--<td align="left"  ><?php echo number_format($PRICE, 2, ".", ","); ?>  =</td>    -->
                  <!--<td align="left" colspan='1'>%</td>-->
                  <!--<td align="left" colspan='1'><?php echo number_format($row1["DIS_per"], 2, ".", ","); ?></td>  -->
                  <!--<td align="left" colspan='1'>=</td>-->
                  <!--<td align="right"  ><?php echo number_format($unittot, 2, ".", ","); ?></td>  -->
                   
            </tr>
            <?php
             $i=$i+1;  
             $TOTPRICE=$TOTPRICE+($PRICE*$row1['QTY']);
             $unitgtot=$unitgtot+$unittot;
             $disgtot=$disgtot+$distot;
             $rrtot=+$rrtot+$rr;
             
        }
        ?>

        <?php
        if ($row["VAT"] == "1"){
             $t=8;
        }else{
             $t=9;
        }
        
        // if(($pages-1)==$pagecount){
            while ($i <$t) {
    
                 echo "<tr style='border:none !important'>
        
                 <td style='border:none !important'>&nbsp;</td>
                 <td style='border:none !important'></td>
                 <td style='border:none !important'></td>
                 <td style='border:none !important'></td>  
                 </tr>";
                 $i=$i+1;
             }
        // } 
        $rowstart =$rowstart+2;
     ?>
     
      <?php
        if ($row["VAT"] =="1"){?>
        <tr> 
              
            <td  class="right" colspan='2' style="font-size:18px;"><b><?php echo  $RTXTVAT ?> :</b></td>  
            <td class="right" style="font-size:18px;"><b><?php
            if ($RTXVATAMU > 0) {
                echo number_format($RTXVATAMU, 2, ".", ",");
            }
            ?></b>
             </td>
        </tr>
       <?php }?>
 
 <?php 
//  if(($pages-1)==$pagecount){
     ?>
     <tr> 
        <td  class="right" colspan='4' style="font-size:19px;border:none !important"><b>&nbsp; </b></td>  
        <td class="right" style="font-size:19px;border:none !important"><b>&nbsp;</b></td>
    </tr>
    <tr> 
        <td  class="right" colspan='4' style="font-size:19px;"><b>Grand Total: </b></td>  
        <td class="right" style="font-size:19px;"><b><?php
        if ($grandtot > 0) {
            echo number_format($grandtot, 2, ".", ",");
        }
        ?></b></td>
    </tr>
     <tr> 
        <td  class="right" colspan='4' style="font-size:18px;"><b>Paid: </b></td>  
        <td class="right" style="font-size:18px;"><b><?php 
            echo number_format($totpay, 2, ".", ","); 
        ?></b></td>
    </tr>
     <tr> 
        <td  class="right" colspan='4' style="font-size:18px;"><b>Balance: </b></td>  
        <td class="right" style="font-size:18px;"><b><?php
        if ($grandtot > 0) {
            echo number_format($grandtot-$totpay, 2, ".", ",");
        }
        ?></b></td>
    </tr>
     <!--<tr> -->
        <!--<td  class="right" colspan='4' style="font-size:18px;"><b>Outstanding: </b></td>  -->
        <!--<td class="right" style="font-size:18px;"><b>-->
        <?php
            // if ($row_out['OUTS'] > 0) {
            //     echo number_format($row_out['OUTS'], 2, ".", ",");
            // }
        ?>
        <!--</b></td>-->
    <!--</tr>-->
<?php 

// }
?>

<tr >
              
    </tr>
    </table>

    

  </section>
<?php 
// }
?>
</body>

</html>
