<?php session_start();
?>
<?php 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>CASH BOOK REPORT</title>
        
    <style>
        .center {
            text-align: center;
        }

        .right {
            text-align: right;
        }

        .tb  { 
           border: 1px solid black;


           border-collapse: collapse;
       }
       .tb td  {

           border-top: 1px solid black;
           border-left: 1px solid black;
        font-size:18px;

           border-collapse: collapse;
       }
       .tb th  {

           border-top: 1px solid black;
           border-left: 1px solid black;


           border-collapse: collapse;
       }

       .tb1 {

           border: 1px solid #000000;
       }

   </style>
        <style type="text/css">
            <!--
            .style1 {
                color: #0000FF;
                font-weight: bold;
                font-size: 24px;
            }
            -->
        </style>
    </head>

    <body>
        <!-- Progress bar holder -->
        <?php
          require_once("connection_sql.php");
        $sql_invpara = "SELECT * from invpara";
            $result_invpara = $conn->query($sql_invpara);
            $row_invpara = $result_invpara->fetch(); 
        ?>
<center>
 <table width="1000px;" cellspacing="0" border="0">

        <tr>
            <th class="center"  colspan=5 ><?php echo $row_invpara['COMPANY']?></th>
        </tr> 
        
         
        <tr> 
         
            <th class='center' colspan='5'>DATE  <?php echo $_GET['dtfrom']?> To  <?php echo $_GET['dtto']?> </th> 
           
        </tr>
         
         <tr>  
            <th class='center' colspan='5'>Company - <?php echo $_GET['company']?>  </th>  
        </tr>
         
         
        <tr>

            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td align="right">&nbsp;</td>
        </tr>
        
        <tr>

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

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

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

            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
       
    </table>
    </center>
        <?php
        
         

        $dt1 = $_GET['dtfrom'];
        $dt2 = $_GET['dtto'];
 
        echo "<center>
        <table class=\"table table-bordered\" border=\"1\" >";
        echo "<tr>";
        echo "<td><b>COMPANY</b></td>"; 
        echo "<td><b>CUSTOMER</b></td>"; 
        echo "<td><b>REFNO</b></td>"; 
        echo "<td><b>DATE</b></td>";
        echo "<td><b>INVOICE NO</b></td>";
        echo "<td><b>AMOUNT</b></td>"; 


        echo "</tr>";

       
         echo "<tr > ";   
                    echo "<td colspan='6' align='center'><b>MALABE</b></td>";  
                    echo "</tr>";
        
         echo "<tr>"; 
        $tot=0; 
        
        

        $sql = "Select  * from view_salma_s_sttr where  ST_DATE >='" . $dt1 . "' and ST_DATE <='" .$dt2 . "'   and ST_FLAG='CAS' "; 
        if($_GET['company'] !="ALL"){
             $sql.=" and company='".$_GET['company']."'"; 
        }
         
        $sql.=" order by ST_DATE";   
        $date="";
        foreach ($conn->query($sql) as $row) {
            
            if($date !=$row['ST_DATE']){
                   echo "<tr > ";   
                    echo "<td colspan='6'><b>" .  $row['ST_DATE'] . "</b></td>";  
                    echo "</tr>";
            }
          
            echo "<tr > ";
            echo "<td align='center'>" .  $row['company'] . "</td>";  
            echo "<td>" .  $row['CUS_NAME'] . "</td>";  
            echo "<td>" .  $row['ST_REFNO'] . "</td>"; 
            echo "<td>" .  $row['ST_DATE'] . "</td>";  
            echo "<td>" .  $row['ST_INVONO'] . "</td>";   
            echo "<td align='right'>" .  number_format($row['ST_PAID'],2) . "</td>";  
            echo "</tr>";
            
            
                
                if($row['company'] =='C'){ 
                    $m_acode='12101';
                    $m_acode1='12501';
                    $sql_main = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'";  
                    $sql_main = $conn_cash->query($sql_main); 
                    if (!$row_main=  $sql_main->fetch()) {
                         
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'MALABE', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','MALABE')";
                        $result3 = $conn_cash->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','MALABE')";
                        $result3 = $conn_cash->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'MALABE', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'MALABE', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash->query($sql5);
                     
                    }
                    
                }else if($row['company']=='A'){
                    $m_acode='12101';
                    $m_acode1='12501';
                    
                    $sql_main = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'";  
                    $sql_main = $conn_cash_a->query($sql_main);
                    if (!$row_main=  $sql_main->fetch()) {
                    
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'MALABE', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash_a->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','MALABE')";
                        $result3 = $conn_cash_a->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','MALABE')";
                        $result3 = $conn_cash_a->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'MALABE', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash_a->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'MALABE', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash_a->query($sql5);
                    }
                    
                }else if($row['company']=='B'){
                    $m_acode='12101';
                    $m_acode1='12501';
                    $sql_main = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'";  
                    $sql_main = $conn_cash_b->query($sql_main);
                    if (!$row_main=  $sql_main->fetch()) {
                        
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'MALABE', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash_b->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','MALABE')";
                        $result3 = $conn_cash_b->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','MALABE')";
                        $result3 = $conn_cash_b->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'MALABE', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_b->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'MALABE', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_b->query($sql5);
                    }
                    
                }else if($row['company']=='R'){
                    $m_acode='12101';
                    $m_acode1='12501';
                    $sql_main = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'";  
                    $sql_main = $conn_cash_r->query($sql_main);
                    if (!$row_main=  $sql_main->fetch()) {
                        
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'MALABE', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash_r->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','MALABE')";
                        $result3 = $conn_cash_r->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','MALABE')";
                        $result3 = $conn_cash_r->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'MALABE', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_r->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'MALABE', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_r->query($sql5);
                    }
                }
                 
              
            
       
               
             $tot=$tot+$row['ST_PAID'] ; 
             $date=$row['ST_DATE'] ;
            
        }

        echo "<tr style='font-size:18px;'>";
        echo "<td colspan='5'></td>";   
        echo "<td align='right'><b>" . number_format($tot,2). "</b></td>"; 
        echo "</tr>"; 
        // ============
        
         echo "<tr > ";   
                    echo "<td colspan='6' align='center'><b>BATHTHARAMULLA</b></td>";  
                    echo "</tr>";
                    
         echo "<tr>"; 
        $tot_bt=0; 

        $sql = "Select  * from view_salma_s_sttr where  ST_DATE >='" . $dt1 . "' and ST_DATE <='" .$dt2 . "'   and ST_FLAG='CAS' "; 
        if($_GET['company'] !="ALL"){
             $sql.=" and company='".$_GET['company']."'"; 
        }
         
        $sql.=" order by ST_DATE";   
        $date="";
        foreach ($conn_bt->query($sql) as $row) {
            
            if($date !=$row['ST_DATE']){
                   echo "<tr > ";   
                    echo "<td colspan='6'><b>" .  $row['ST_DATE'] . "</b></td>";  
                    echo "</tr>";
            }
           
            echo "<tr > ";
            echo "<td align='center'>" .  $row['company'] . "</td>";  
            echo "<td>" .  $row['CUS_NAME'] . "</td>";  
            echo "<td>" .  $row['ST_REFNO'] . "</td>"; 
            echo "<td>" .  $row['ST_DATE'] . "</td>";  
            echo "<td>" .  $row['ST_INVONO'] . "</td>";   
            echo "<td align='right'>" .  number_format($row['ST_PAID'],2) . "</td>";  
            echo "</tr>";
            
            
                
                if($row['company']=='C'){
                    $m_acode='12101';
                    $m_acode1='12503';
                    $sql_main1 = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'"; 
                    $sql_main1 = $conn_cash->query($sql_main1);
                    if (!$row_main1=  $sql_main1->fetch()) {
                    
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'BATHTHARAMULLA', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','BATHTHARAMULLA')";
                        $result3 = $conn_cash->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','BATHTHARAMULLA')";
                        $result3 = $conn_cash->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'BATHTHARAMULLA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'BATHTHARAMULLA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash->query($sql5);
                    }
                    
                }else if($row['company']=='A'){
                    $m_acode='12101';
                    $m_acode1='12503';
                    $sql_main1 = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'"; 
                    $sql_main1 = $conn_cash_a->query($sql_main1);
                    if (!$row_main1=  $sql_main1->fetch()) {
                    
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'BATHTHARAMULLA', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash_a->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','BATHTHARAMULLA')";
                        $result3 = $conn_cash_a->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','BATHTHARAMULLA')";
                        $result3 = $conn_cash_a->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'BATHTHARAMULLA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash_a->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'BATHTHARAMULLA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash_a->query($sql5);
                    }
                    
                }else if($row['company']=='B'){
                    $m_acode='12101';
                    $m_acode1='12503';
                    $sql_main1 = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'"; 
                    $sql_main1 = $conn_cash_b->query($sql_main1);
                    if (!$row_main1=  $sql_main1->fetch()) {
                    
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'BATHTHARAMULLA', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash_b->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','BATHTHARAMULLA')";
                        $result3 = $conn_cash_b->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','BATHTHARAMULLA')";
                        $result3 = $conn_cash_b->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'BATHTHARAMULLA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_b->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'BATHTHARAMULLA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_b->query($sql5);
                    }
                    
                }else if($row['company']=='R'){
                    $m_acode='12101';
                    $m_acode1='12503';
                    $sql_main1 = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'"; 
                    $sql_main1 = $conn_cash_b->query($sql_main1);
                    if (!$row_main1=  $sql_main1->fetch()) {
                        
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'BATHTHARAMULLA', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash_r->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','BATHTHARAMULLA')";
                        $result3 = $conn_cash_r->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','BATHTHARAMULLA')";
                        $result3 = $conn_cash_r->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'BATHTHARAMULLA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_r->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'BATHTHARAMULLA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_r->query($sql5);
                    }   
                }
	      
               
             $tot_bt=$tot_bt+$row['ST_PAID'] ; 
             $date=$row['ST_DATE'] ;
            
        }

        echo "<tr style='font-size:18px;'>";
        echo "<td colspan='5'></td>";   
        echo "<td align='right'><b>" . number_format($tot_bt,2). "</b></td>"; 
        echo "</tr>"; 
        // ==================
         echo "<tr > ";   
                    echo "<td colspan='6' align='center'><b>KADUWELA</b></td>";  
                    echo "</tr>";
         echo "<tr>"; 
        $tot_ka=0; 

        $sql = "Select  * from view_salma_s_sttr where  ST_DATE >='" . $dt1 . "' and ST_DATE <='" .$dt2 . "'   and ST_FLAG='CAS' "; 
        if($_GET['company'] !="ALL"){
             $sql.=" and company='".$_GET['company']."'"; 
        }
         
        $sql.=" order by ST_DATE";   
        $date="";
        foreach ($conn_ka->query($sql) as $row) {
            
            if($date !=$row['ST_DATE']){
                   echo "<tr > ";   
                    echo "<td colspan='6'><b>" .  $row['ST_DATE'] . "</b></td>";  
                    echo "</tr>";
            }
           
            echo "<tr > ";
            echo "<td align='center'>" .  $row['company'] . "</td>";  
            echo "<td>" .  $row['CUS_NAME'] . "</td>";  
            echo "<td>" .  $row['ST_REFNO'] . "</td>"; 
            echo "<td>" .  $row['ST_DATE'] . "</td>";  
            echo "<td>" .  $row['ST_INVONO'] . "</td>";   
            echo "<td align='right'>" .  number_format($row['ST_PAID'],2) . "</td>";  
            echo "</tr>";
            
            
                 
                 if($row['company']=='C'){
                    $m_acode='12101';
                    $m_acode1='12503';
                    $sql_main2 = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'"; 
                    $sql_main2 = $conn_cash->query($sql_main2);
                    if (!$row_main2=  $sql_main2->fetch()) {    
                        
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'KADUWELA', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KADUWELA')";
                        $result3 = $conn_cash->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KADUWELA')";
                        $result3 = $conn_cash->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'KADUWELA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'KADUWELA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash->query($sql5);
                    }
                    
                }else if($row['company']=='A'){
                    $m_acode='12101';
                    $m_acode1='12503';
                    
                    $sql_main2 = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'"; 
                    $sql_main2 = $conn_cash_a->query($sql_main2);
                    if (!$row_main2=  $sql_main2->fetch()) {  
                            
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'KADUWELA', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash_a->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KADUWELA')";
                        $result3 = $conn_cash_a->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KADUWELA')";
                        $result3 = $conn_cash_a->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'KADUWELA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash_a->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'KADUWELA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash_a->query($sql5);
                    }
                    
                }else if($row['company']=='B'){
                    $m_acode='12101';
                    $m_acode1='12503';
                    
                    $sql_main2 = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'"; 
                    $sql_main2 = $conn_cash_b->query($sql_main2);
                    if (!$row_main2=  $sql_main2->fetch()) {    
                        
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'KADUWELA', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash_b->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KADUWELA')";
                        $result3 = $conn_cash_b->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KADUWELA')";
                        $result3 = $conn_cash_b->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'KADUWELA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_b->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'KADUWELA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_b->query($sql5);
                    }
                    
                }else if($row['company']=='R'){
                    $m_acode='12101';
                    $m_acode1='12505';
                    
                    $sql_main2 = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'"; 
                    $sql_main2 = $conn_cash_r->query($sql_main2);
                    if (!$row_main2=  $sql_main2->fetch()) {    
                        
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'KADUWELA', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash_r->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KADUWELA')";
                        $result3 = $conn_cash_r->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KADUWELA')";
                        $result3 = $conn_cash_r->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'KADUWELA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_r->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'KADUWELA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_r->query($sql5);
                    }
                }
	     
             
               
             $tot_ka=$tot_ka+$row['ST_PAID'] ; 
             $date=$row['ST_DATE'] ;
            
        }

        echo "<tr style='font-size:18px;'>";
        echo "<td colspan='5'></td>";   
        echo "<td align='right'><b>" . number_format($tot_ka,2). "</b></td>"; 
        echo "</tr>"; 
        // ====================
        
         echo "<tr > ";   
                    echo "<td colspan='6' align='center'><b>KULIYAPITIYA</b></td>";  
                    echo "</tr>";
         echo "<tr>"; 
        $tot_ku=0; 

        $sql = "Select  * from view_salma_s_sttr where  ST_DATE >='" . $dt1 . "' and ST_DATE <='" .$dt2 . "'   and ST_FLAG='CAS' "; 
        if($_GET['company'] !="ALL"){
             $sql.=" and company='".$_GET['company']."'"; 
        }
         
        $sql.=" order by ST_DATE";   
        $date="";
        foreach ($conn_ku->query($sql) as $row) {
            
            if($date !=$row['ST_DATE']){
                   echo "<tr > ";   
                    echo "<td colspan='6'><b>" .  $row['ST_DATE'] . "</b></td>";  
                    echo "</tr>";
            }
           
            echo "<tr > ";
            echo "<td align='center'>" .  $row['company'] . "</td>";  
            echo "<td>" .  $row['CUS_NAME'] . "</td>";  
            echo "<td>" .  $row['ST_REFNO'] . "</td>"; 
            echo "<td>" .  $row['ST_DATE'] . "</td>";  
            echo "<td>" .  $row['ST_INVONO'] . "</td>";   
            echo "<td align='right'>" .  number_format($row['ST_PAID'],2) . "</td>";  
            echo "</tr>";
            
            
                 if($row['company']=='C'){
                    $m_acode='12101';
                    $m_acode1='12504';
                    
                    $sql_main3 = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'"; 
                    $sql_main3 = $conn_cash->query($sql_main3);
                    if (!$row_main3=  $sql_main3->fetch()) {
                    
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'KULIYAPITIYA', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KULIYAPITIYA')";
                        $result3 = $conn_cash->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KULIYAPITIYA')";
                        $result3 = $conn_cash->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'KULIYAPITIYA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'KULIYAPITIYA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash->query($sql5);
                    }
                    
                }else if($row['company']=='A'){
                    $m_acode='12101';
                    $m_acode1='12504';
                    
                    $sql_main3 = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'"; 
                    $sql_main3 = $conn_cash_a->query($sql_main3);
                    if (!$row_main3=  $sql_main3->fetch()) {
                        
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'KULIYAPITIYA', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash_a->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KULIYAPITIYA')";
                        $result3 = $conn_cash_a->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KULIYAPITIYA')";
                        $result3 = $conn_cash_a->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'KULIYAPITIYA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash_a->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'KULIYAPITIYA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                         $result5 = $conn_cash_a->query($sql5);
                    }   
                    
                }else if($row['company']=='B'){
                    $m_acode='12101';
                    $m_acode1='12504';
                    
                    $sql_main3 = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'"; 
                    $sql_main3 = $conn_cash_b->query($sql_main3);
                    if (!$row_main3=  $sql_main3->fetch()) {
                        
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'KULIYAPITIYA', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash_b->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KULIYAPITIYA')";
                        $result3 = $conn_cash_b->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KULIYAPITIYA')";
                        $result3 = $conn_cash_b->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'KULIYAPITIYA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_b->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'KULIYAPITIYA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_b->query($sql5);
                    }
                    
                }else if($row['company']=='R'){
                    $m_acode='12101';
                    $m_acode1='12504';
                    
                    $sql_main3 = "SELECT * from ledmas where refno = '" . $row["ST_REFNO"]  . "'"; 
                    $sql_main3 = $conn_cash_r->query($sql_main3);
                    if (!$row_main3=  $sql_main3->fetch()) {
                        
                        $sql2="Insert into ledmas(refno, bdate, details, rights, comcode, cancel, acyear, tmp_no) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . $row['ST_INVONO'] . "', 'KULIYAPITIYA', '" . $row['company'] . "', '0', '" . date("Y") . "', '')";
                        $result2 = $conn_cash_r->query($sql2);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'CRE', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KULIYAPITIYA')";
                        $result3 = $conn_cash_r->query($sql3);
                        
                        $sql3="Insert into ledtrans(refno, bdate, code, amount, flag, jobno, nara, comcode, cancel,rights) Values 
                        ('" . trim($row["ST_REFNO"]) . "', '" . $row['ST_DATE'] . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'DEB', '', '" . $row['ST_INVONO']. "', '" . $row['company'] . "', '0','KULIYAPITIYA')";
                        $result3 = $conn_cash_r->query($sql3);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode) . "', " . $row['ST_PAID'] . ", 'JOU', 'CRE', '" .$row['ST_INVONO'] . "', 'KULIYAPITIYA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_r->query($sql5);
                        
                        $sql5="Insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, rights, l_flag2, l_flag3, comcode, l_yearfl, l_year, l_month, recdate, user, ent_datetime, acyear) Values 
                        ('" .  trim($row["ST_REFNO"]). "', '" .  $row['ST_DATE']  . "', '" . trim($m_acode1) . "', " . $row['ST_PAID'] . ", 'JOU', 'DEB', '" .$row['ST_INVONO'] . "', 'KULIYAPITIYA', '0', 'R', '" .  $row['company'] . "', 0,  ".date("Y").", '".$l_month."', '".$recdate."', '".$_SESSION['UserName']."', '".date("Y-m-d H:i:s")."', '$ayear')";
                        $result5 = $conn_cash_r->query($sql5);
                    }
                }
              
           
               
             $tot_ka=$tot_ka+$row['ST_PAID'] ; 
             $date=$row['ST_DATE'] ;
            
        }

        echo "<tr style='font-size:18px;'>";
        echo "<td colspan='5'></td>";   
        echo "<td align='right'><b>" . number_format($tot_ku,2). "</b></td>"; 
        echo "</tr>"; 
        
         echo "<td colspan='5'></td>";   
        echo "<td align='right' style='font-size:22px'><b>" . number_format($tot+$tot_bt+$tot_ka+$tot_ku,2). "</b></td>"; 
        echo "</tr>"; 
        // ====================

        echo "</table>";
        ?>
    </body>
</html>
 