<?php

session_start();

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

////////////////////////////////////////////// Database Connector /////////////////////////////////////////////////////////////
require_once("connectioni.php");



////////////////////////////////////////////// Write XML ////////////////////////////////////////////////////////////////////
header('Content-Type: text/xml');

date_default_timezone_set('Asia/Colombo');

/////////////////////////////////////// GetValue //////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Registration /////////////////////////////////////////////////////////////////////////

function getno($sdate) {
        
            require_once 'acc/gl_posting.php';
            $ayear = ac_year($sdate);
        
            include 'acc/connection_sql.php';
            $sql = "select BANKDEP from a_dep_mas  where current_year = '" . $ayear . "' and COMCODE = '" . $_SESSION['company'] . "'";
            // echo $sql;
            $result = $conn->query($sql);
            $row = $result->fetch();
            $tmpinvno = "000000" . $row["BANKDEP"];
            $lenth=strlen($tmpinvno);
        	$invno= $_SESSION['company'] . "/" . date("y") . "/R/" . substr($tmpinvno, $lenth-7);
        
            return $invno;
        }
        
        
if($_GET["Command"]=="update_bal"){
    
        $open_bal=str_replace(",", "",$_GET["open_bal"]);
        $closing_bal=str_replace(",", "",$_GET["closing_bal"]);
        $txtbf=str_replace(",", "",$_GET["txtbf"]);
        $sales=str_replace(",", "",$_GET["tot_sale"]);
        
        $sql="SELECT max(trn_date) as lastTrnDate FROM cash_book_summery where company='".$_SESSION['company']."'";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
        $row = mysqli_fetch_array($result);
	
        $compareDate = date('Y-m-d',strtotime(($_GET["dtfrom"]).' -1 days'));
        
        $heading='CASH BOOK SUMMRY UPDATE - '.$_GET["dtfrom"];
        
        $invno = getno(date('Y-m-d'));
        
        $ayear = ac_year($_GET["dtfrom"]);
// 		echo $compareDate;
		echo $_GET["dtfrom"];
		
	   // if($row["lastTrnDate"] == $compareDate){	
            $sql1="insert into cash_book_summery (company, trn_date, open_bal, closing_bal, balance, username, stime, sdate,sale) values 
            ('".$_SESSION['company']."', '".$_GET["dtfrom"]."', ".$open_bal.", ".$closing_bal.", ".$txtbf.", '" . $_SESSION["CURRENT_USER"] . "', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "','".$sales."')";
            $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
            
            $sql1="update invpara set CASH_UPDATE = '".$_GET["dtfrom"]."' where flag='".$_SESSION['company']."' ";
            $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
            
        
            $sql = "Insert into a_bankdepmas (refno, bdate, heading, code, name, amount, cash, comcode, cancel, type, tmp_no) Values 
            ('" . trim($invno) . "', '" . $_GET["dtfrom"] . "', '".$heading."', '12504', '', " . $closing_bal . ", 0,'" . $_SESSION['company'] . "', '0', 'D', '" . $invno . "')";
            $result=mysqli_query($GLOBALS['dbinv'],$sql);
            
            $year = date("Y");

            $sql = "insert into a_ledger (l_refno, l_date, l_code, l_amount, l_flag, l_flag1,L_LMEM,Currency,rate,curamo,acyear,COMPANY) value
            ('" . $invno . "','" . $_GET["dtfrom"] . "','12501','" . $closing_bal . "','BDE','CRE','".$heading."','LKR','','" . $closing_bal . "','" . $year . "','".$_SESSION['company']."')";
	        $result=mysqli_query($GLOBALS['dbinv'],$sql);

            $sql = "insert into a_bankdeptrn (refno, bdate, code, amount, flag,nara,comcode,tmp_no) value
            ('" . $invno . "','" . $_GET["dtfrom"] . "','12501','" . $closing_bal . "','CRE','".$heading."','".$_SESSION['company']."','" . $invno . "')";
	        $result=mysqli_query($GLOBALS['dbinv'],$sql);

            $sql = "insert into a_ledger (l_refno, l_date, l_code, l_amount, l_flag, l_flag1,L_LMEM,Currency,rate,curamo,acyear,chno,COMPANY) value
            ('" . $invno . "','" . $_GET["dtfrom"] . "','12504','" . $closing_bal . "','BDE','DEB','".$heading."','LKR','','" . $closing_bal . "','" . $year . "','','".$_SESSION['company']."')";
	        $result=mysqli_query($GLOBALS['dbinv'],$sql);
	        
	        
	        $sql = "update a_dep_mas set BANKDEP=BANKDEP+1  where current_year = '" . $ayear . "' and COMCODE = '" .$_SESSION['company']. "'";
            $result=mysqli_query($GLOBALS['dbinv'],$sql);
 
        

            echo "Balance Updated";
        // }else{
        //     echo "Cannot Update!";
        // }
}
		
