<?php

session_start();

header("Content-type: text/html; charset=" . $_SESSION['encodingmode']);
if ($_SESSION['dbName'] == "") {
	header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");
include("Function_Amortization.inc.php");



$connection = @mysql_connect($config['dbServer'], $config['dbUser'], $config['dbPass'], true, 65536) or die("Could not connect to DB");
@mysql_select_db($_SESSION['dbName'], $connection) or die("Could not find DB");
if ($_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8') {
	@mysql_query("SET NAMES 'utf8'");
	@mysql_query('SET CHARACTER SET utf8');
	mysql_set_charset('utf8', $connection);
}

if ($_POST) {
	extract($_POST);
}
if ($_GET) {
	extract($_GET);
}
$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '" . $vPrdCurr . "' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '" . $vPrdCurr . "' )";
$result = selectData($connection, $query);
$rate_dt = $result[0];

$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '" . $base1 . "' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '" . $base1 . "' )";
$result = selectData($connection, $query);
$rate_dt1 = $result[0];

$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '" . $base2 . "' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '" . $base2 . "' )";
$result = selectData($connection, $query);
$rate_dt2 = $result[0];


//Read From Module Table
$querymodule = " Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, " .
	" ifNull(Module_ProjectDt,0) As Module_ProjectDt, ifNull(Module_CostcenterDt,0) As Module_CostCenterDt, " .
	" ifNull(Module_MultiCashType,0) As Module_MultiCashType, ifNull(Module_TransStandardView,0) As Module_TransStandardView, ifnull(journal_distribution,0) as journal_distribution From Module ";
$resultmodule = @mysql_query($querymodule, $connection) or die(mysql_error());
while ($rowmodule = mysql_fetch_array($resultmodule)) {
	extract($rowmodule);
}


//Get Default Project
$querydflproject = "select project_code as defaultproject from " . $_SESSION['dbLabel'] . ".loginstk where user_account='" . $_SESSION['useraccount'] . "' and default_project=1 ";
$resultdflproject = @mysql_query($querydflproject, $connection) or die(mysql_error());
while ($rows = mysql_fetch_array($resultdflproject)) {
	extract($rows);
}

//Get Default Coscenter
$querydflcostcent = "select costcent_code as defaultcostcent from " . $_SESSION['dbLabel'] . ".loginstk where user_account='" . $_SESSION['useraccount'] . "' and default_costcenter=1 ";
$resultdflcostcent = @mysql_query($querydflcostcent, $connection) or die(mysql_error());
while ($rows = mysql_fetch_array($resultdflcostcent)) {
	extract($rows);
}


if ((empty($txtProjectCode)) && $Module_Project == 1)
	$txtProjectCode = $defaultproject;
if ((empty($txtCostCentCode)) && $Module_CostCenter == 1)
	$txtCostCentCode = $defaultcostcent;


$querysetup = "select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup";
$resultsetup = @mysql_query($querysetup, $connection) or die(mysql_error());
while ($rowsetup = mysql_fetch_array($resultsetup)) {
	extract($rowsetup);
}

$query_login = "select login_belongstogrp from " . $_SESSION['dbLabel'] . ".login where user_account='" . $_SESSION['useraccount'] . "' and Upper(Login_Schema) = Upper('" . $_SESSION['dbName'] . "')  ";
$resultlogin = @mysql_query($query_login, $connection) or die(mysql_error());

$row_login = mysql_fetch_row($resultlogin);
$belongstogroup = $row_login[0];

$MainCompanyName = explode('_', $_SESSION['dbName']);
$QueryDir = " Select Date_Format(StartFinancialDate,'%Y-%m-%d'),Date_Format(EndFinancialDate,'%Y-%m-%d') , Concat(CompanyName, '_', DirYear) As DirSchema " .
	" From " . $_SESSION['dbLabel'] . ".Directories " .
	" Where  Upper(CompanyName) = Upper('" . $MainCompanyName[0] . "') " .
	" and Upper(Diryear) = Upper('" . $MainCompanyName[1] . "') " .
	" Order By DirYear ";
//echo $QueryDir;			
$ResultDir = @mysql_query($QueryDir, $connection) or die(mysql_error());
$RowsDir = mysql_fetch_row($ResultDir);
$StartFinancialDate = $RowsDir[0];
$EndFinancialDate = $RowsDir[1];


/// Color Grids    Base1 Base2     Decimal for Amounts
$querysetup = "select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup";
$resultsetup = @mysql_query($querysetup, $connection) or die(mysql_error());
while ($rowsetup = mysql_fetch_array($resultsetup)) {
	extract($rowsetup);
}

/// user name
$query_user = "select User_Name  from " . $_SESSION['dbLabel'] . ".users where user_account='" . $_SESSION['useraccount'] . "'";
$resultuser = @mysql_query($query_user, $connection) or die(mysql_error());

$row_user = mysql_fetch_row($resultuser);
$username = $row_user[0];




if ($actionmode == 'addItem') {
	$itemCode = $_GET['itemCode'];

	$query = "select distinct ItemJour_Code from (
			select ItemJour_Code from itemjour where ItemJour_Code like '$itemCode%'
			union
			select ItemJour_Code from temp_assetsitemjour where ItemJour_Code like '$itemCode%'
			union
			select ItemJour_Code from assetsitemjour where ItemJour_Code like '$itemCode%'
			) as Q
			 order by ItemJour_Code desc limit 1"; //echo$query;
	$result = @mysql_query($query, $connection) or die(mysql_error());
	$row = mysql_fetch_row($result);
	$itemcode = $row[0];
	if ($itemcode == '') $itemcode = $itemCode . "1";
	else $itemcode++;
	echo "[BRK]";
	echo $itemcode;
	exit;
}

if ($actionmode == 'post' and $mainaction == 'insert') {

	//$assets_id = PostHeader($assets_date,$journal_valuedate,$assets_amount,$assets_amount1,$assets_amount2,$accountno,$amortization_currency_code,$assets_name,$costcenter_code,$project_code,$assets_desc,$useraccount,$connection);
	//   echo"<pre>";
	//   var_dump($_POST);
	//   echo"</pre>";


	if ($assets_id != '') {

		$update = "update temp_assets set Assets_date = " . GetSQLValueString(insertMySQLDate($assets_date), 'date') . " 
									 ,LEDGER_NUMBER = " . GetSQLValueString($accountno, 'text') . "
									 ,CURRENCY_CODE = " . GetSQLValueString($amortization_currency_code, 'text') . "
									 ,Assets_Name = " . GetSQLValueString($assets_name, 'text') . "
									 ,COSTCENTer_CODE = " . GetSQLValueString($costcenter_code, 'text') . "
									 ,Project_code = " . GetSQLValueString($project_code, 'text') . "
									 ,Assets_DESC = " . GetSQLValueString($assets_desc, 'text') . "
									 ,VATFixedAssetAccount = " . GetSQLValueString($assets_vatpref, 'text') . "
						where assets_id = $assets_id";
		$result = @mysql_query($update, $connection) or die(mysql_error());
		//Calculating Master Amounts
		$CalculateResult = Calculate_MasterAmountsFixedAssets($assets_id, $connection);



		$SMode = 'FV';
		$query = " SELECT CopyData_TempAssets('" . $SMode . "', " . $assets_id . ", '" . $_SESSION['useraccount'] . "') FROM Dual ";
		// echo $query; exit;
		$result = @mysql_query($query, $connection) or die(mysql_error());
		$row = mysql_fetch_array($result);
		$results = $result[0];

		if ($result != 0) {
			fcommit();
			$message = Create_FixedAssetsJv($assets_id, $_SESSION['dbName'], $_SESSION['dbLabel'], $_SESSION['useraccount'], $_SESSION['username']);

			$querySel = "select ItemJour_Code, Journal_id, ItemJour_Description, assetsitemjour.Currency_Code as Currency_Code, Costcent_code, assetsitemjour.Project_Code as Project_Code, ItemJour_prcnt, ItemJour_amount,ItemJour_amount1, ItemJour_amount2, assetsitemjour.Ledger_Number as Ledger_Number, Ledger_NumberDebit, CostCent_CodeDebit, Project_codeDebit, Ledger_NumberCredit,CostCent_CodeCredit,
							 Project_CodeCredit, Journal_Date, JournalDt_ValueDate, ItemJour_Remark, ItemJour_DateAmortized, ItemJour_PreviousDate, ItemJour_PreviousAmount,
							 ItemJour_PreviousAmount1, ItemJour_PreviousAmount2, ItemJour_Sold, ItemJour_SaleDate, Ledger_NumberCash, CostCent_CodeCash, Project_CodeCash,
							 ItemJour_CashAmount, ItemJour_CashAmount1, ItemJour_CashAmount2, Ledger_NumberProfit, Ledger_NumberLost, ItemJour_BelongsToGroup, Item_OriginalCode,
							 assetsitemjour.Synchronized as Synchronized, itemjour_location, itemjour_barcode, adjustmentdt_id, assetsitemjour.assetsdt_id as assetsdt_id, assetsdt_quantity
							from assetsitemjour
							left join assetsdt on assetsitemjour.assetsdt_id = assetsdt.assetsdt_id
							left join assets on assets.assets_id = assetsdt.assets_id
							where  assetsdt.assets_id =$assets_id";
			$resSel = @mysql_query($querySel, $connection) or die(mysql_error());
			while ($rowSel = mysql_fetch_array($resSel)) {
				extract($rowSel);
				if ($assetsdt_quantity == '' || $assetsdt_quantity == 0) $assetsdt_quantity = 1;

				for ($ct = 0; $ct < $assetsdt_quantity; $ct++) {
					$query1 = "insert into itemjour (ItemJour_Code, Journaldt_id, ItemJour_Description, Currency_Code, Costcent_code, Project_Code, ItemJour_prcnt, ItemJour_amount, ItemJour_amount1, ItemJour_amount2, Ledger_Number, Ledger_NumberDebit, CostCent_CodeDebit, Project_codeDebit, Ledger_NumberCredit, CostCent_CodeCredit, Project_CodeCredit, Journal_Date, JournalDt_ValueDate, ItemJour_Remark, ItemJour_DateAmortized, ItemJour_PreviousDate, ItemJour_PreviousAmount, ItemJour_PreviousAmount1, ItemJour_PreviousAmount2, ItemJour_Sold, ItemJour_SaleDate, Ledger_NumberCash, CostCent_CodeCash, Project_CodeCash, ItemJour_CashAmount, ItemJour_CashAmount1, ItemJour_CashAmount2, Ledger_NumberProfit, Ledger_NumberLost, ItemJour_BelongsToGroup, Item_OriginalCode, Synchronized, itemjour_location, itemjour_barcode, adjustmentdt_id, assetsdt_id)
								values ( " . GetSQLValueString($ItemJour_Code, 'text') . ", " . GetSQLValueString($Journal_id, 'double') . ", " . GetSQLValueString($ItemJour_Description, 'text') . ", 
									" . GetSQLValueString($Currency_Code, 'text') . ", " . GetSQLValueString($Costcent_code, 'text') . ", " . GetSQLValueString($Project_Code, 'text') . ", 
									" . GetSQLValueString($ItemJour_prcnt, 'double') . ", " . GetSQLValueString($ItemJour_amount, 'double') . ", " . GetSQLValueString($ItemJour_amount1, 'double') . ",
									" . GetSQLValueString($ItemJour_amount2, 'double') . "," . GetSQLValueString($Ledger_Number, 'text') . "," . GetSQLValueString($Ledger_NumberDebit, 'text') . ",
									" . GetSQLValueString($CostCent_CodeDebit, 'text') . "," . GetSQLValueString($Project_codeDebit, 'text') . "," . GetSQLValueString($Ledger_NumberCredit, 'text') . ",
									" . GetSQLValueString($CostCent_CodeCredit, 'text') . "," . GetSQLValueString($Project_CodeCredit, 'text') . "," . GetSQLValueString($Journal_Date, 'text') . ",
									" . GetSQLValueString($JournalDt_ValueDate, 'text') . "," . GetSQLValueString($ItemJour_Remark, 'text') . "," . GetSQLValueString($ItemJour_DateAmortized, 'text') . ",
									" . GetSQLValueString($ItemJour_PreviousDate, 'text') . "," . GetSQLValueString($ItemJour_PreviousAmount, 'double') . "," . GetSQLValueString($ItemJour_PreviousAmount1, 'double') . ",
									" . GetSQLValueString($ItemJour_PreviousAmount2, 'double') . "," . GetSQLValueString($ItemJour_Sold, 'double') . "," . GetSQLValueString($ItemJour_SaleDate, 'text') . ",
									" . GetSQLValueString($Ledger_NumberCash, 'text') . "," . GetSQLValueString($CostCent_CodeCash, 'text') . "," . GetSQLValueString($Project_CodeCash, 'text') . ",
									" . GetSQLValueString($ItemJour_CashAmount, 'double') . "," . GetSQLValueString($ItemJour_CashAmount1, 'double') . "," . GetSQLValueString($ItemJour_CashAmount2, 'double') . ",
									" . GetSQLValueString($Ledger_NumberProfit, 'text') . "," . GetSQLValueString($Ledger_NumberLost, 'text') . "," . GetSQLValueString($ItemJour_BelongsToGroup, 'int') . ",
									" . GetSQLValueString($Item_OriginalCode, 'text') . "," . GetSQLValueString($Synchronized, 'int') . "," . GetSQLValueString($itemjour_location, 'text') . ",
									" . GetSQLValueString($itemjour_barcode, 'text') . "," . GetSQLValueString($adjustmentdt_id, 'int') . "," . GetSQLValueString($assetsdt_id, 'int') . "
								)";
					$res = @mysql_query($query1, $connection) or die(mysql_error());
				}
			}

			$message = Create_FixedAssetsJv($assets_id, $_SESSION['dbName'], $_SESSION['dbLabel'], $_SESSION['useraccount'], $_SESSION['username']);
		} else {
			//$message  = mysql_error(); 
			$message  = 'Error In CopyData_TempAssets Function.';
			frollback();
		}
	} else {

		$message  = mysql_error();
		frollback();
	}

	if ($message != '' && $message != '0')
		header("Location: ./Form_Amortization.php?actionmode=$actionmode&mainaction=$mainaction&message=" . $message);
	else header("Location: ./Form_Amortization.php?actionmode=$actionmode&mainaction=$mainaction&assetsid=$assets_id");
} else 
if ($actionmode == 'post' and $mainaction == 'edit' and $assets_id != '') {

	$query = "update temp_assets set Assets_DATE = " . GetSQLValueString(insertMySQLDate($assets_date), 'date') . "
										,Journal_valuedate= " . GetSQLValueString(insertMySQLDate($journal_valuedate), 'date') . "
										,Assets_AMOUNT  = " . GetSQLValueString($assets_amount, 'double') . "
										,Assets_AMOUNT1 = " . GetSQLValueString($assets_amount1, 'double') . "
										,Assets_AMOUNT2 = " . GetSQLValueString($assets_amount2, 'double') . "
										,LEDGER_NUMBER = " . GetSQLValueString($accountno, 'text') . "
										,CURRENCY_CODE = " . GetSQLValueString($amortization_currency_code, 'text') . "
										,Assets_Name = " . GetSQLValueString($assets_name, 'text') . "
										,COSTCENTer_CODE = " . GetSQLValueString($costcenter_code, 'text') . "
										,Project_code = " . GetSQLValueString($project_code, 'text') . "
										,Assets_DESC = " . GetSQLValueString($assets_desc, 'text') . "
										,logged_code = " . GetSQLValueString($_SESSION['useraccount'], 'text') . "	
										,VATFixedAssetAccount = " . GetSQLValueString($assets_vatpref, 'text') . "		
								where assets_id = " . $assets_id;
	// echo $query;exit;
	$result = @mysql_query($query, $connection) or die(mysql_error());

	if ($result) {
		//Calculating Master Amounts
		$CalculateResult = Calculate_MasterAmountsFixedAssets($assets_id, $connection);


		$SMode = 'FV';
		$query = " SELECT CopyData_TempAssets('" . $SMode . "', " . $assets_id . ", '" . $_SESSION['useraccount'] . "') FROM Dual ";
		// echo $query."<br><br><br>";  
		$result = @mysql_query($query, $connection) or die(mysql_error());
		$row = mysql_fetch_array($result);
		$row = $result[0];
		//echo $query; exit;
		//echo 'error='.$result; exit;
		if ($result != 0) {
			fcommit();
			$message = Create_FixedAssetsJv($assets_id, $_SESSION['dbName'], $_SESSION['dbLabel'], $_SESSION['useraccount'], $_SESSION['username']);
			// echo "$message";exit;
			if ($message == 0) $message = '';
		} else {
			//$message  = mysql_error(); 
			$message  = 'Error In CopyData_TempAssets Function.';
			frollback();
		}
	} else {
		$message  = mysql_error();
		frollback();
	}
	if ($result != 0) {
		$q = "DELETE FROM itemjour  where assetsdt_id in (select assetsdt_id from assetsdt where assets_id = $assets_id);";

		$res = @mysql_query($q, $connection) or die(mysql_error());

		$querySel = "select ItemJour_Code, Journal_id, ItemJour_Description, assetsitemjour.Currency_Code as Currency_Code, Costcent_code, assetsitemjour.Project_Code as Project_Code, ItemJour_prcnt, ItemJour_amount,ItemJour_amount1, ItemJour_amount2, assetsitemjour.Ledger_Number as Ledger_Number, Ledger_NumberDebit, CostCent_CodeDebit, Project_codeDebit, Ledger_NumberCredit,CostCent_CodeCredit,
							 Project_CodeCredit, Journal_Date, JournalDt_ValueDate, ItemJour_Remark, ItemJour_DateAmortized, ItemJour_PreviousDate, ItemJour_PreviousAmount,
							 ItemJour_PreviousAmount1, ItemJour_PreviousAmount2, ItemJour_Sold, ItemJour_SaleDate, Ledger_NumberCash, CostCent_CodeCash, Project_CodeCash,
							 ItemJour_CashAmount, ItemJour_CashAmount1, ItemJour_CashAmount2, Ledger_NumberProfit, Ledger_NumberLost, ItemJour_BelongsToGroup, Item_OriginalCode,
							 assetsitemjour.Synchronized as Synchronized, itemjour_location, itemjour_barcode, adjustmentdt_id, assetsitemjour.assetsdt_id as assetsdt_id, assetsdt_quantity
							from assetsitemjour
							left join assetsdt on assetsitemjour.assetsdt_id = assetsdt.assetsdt_id
							left join assets on assets.assets_id = assetsdt.assets_id
							where  assetsdt.assets_id =$assets_id";
		$resSel = @mysql_query($querySel, $connection) or die(mysql_error());

		while ($rowSel = mysql_fetch_array($resSel)) {

			extract($rowSel);
			if ($assetsdt_quantity == '' || $assetsdt_quantity == 0) $assetsdt_quantity = 1;

			for ($ct = 0; $ct < $assetsdt_quantity; $ct++) {
				$query1 = "insert into itemjour (ItemJour_Code, Journaldt_id, ItemJour_Description, Currency_Code, Costcent_code, Project_Code, ItemJour_prcnt, ItemJour_amount, ItemJour_amount1, ItemJour_amount2, Ledger_Number, Ledger_NumberDebit, CostCent_CodeDebit, Project_codeDebit, Ledger_NumberCredit, CostCent_CodeCredit, Project_CodeCredit, Journal_Date, JournalDt_ValueDate, ItemJour_Remark, ItemJour_DateAmortized, ItemJour_PreviousDate, ItemJour_PreviousAmount, ItemJour_PreviousAmount1, ItemJour_PreviousAmount2, ItemJour_Sold, ItemJour_SaleDate, Ledger_NumberCash, CostCent_CodeCash, Project_CodeCash, ItemJour_CashAmount, ItemJour_CashAmount1, ItemJour_CashAmount2, Ledger_NumberProfit, Ledger_NumberLost, ItemJour_BelongsToGroup, Item_OriginalCode, Synchronized, itemjour_location, itemjour_barcode, adjustmentdt_id, assetsdt_id)
								values ( " . GetSQLValueString($ItemJour_Code, 'text') . ", " . GetSQLValueString($Journal_id, 'double') . ", " . GetSQLValueString($ItemJour_Description, 'text') . ", 
									" . GetSQLValueString($Currency_Code, 'text') . ", " . GetSQLValueString($Costcent_code, 'text') . ", " . GetSQLValueString($Project_Code, 'text') . ", 
									" . GetSQLValueString($ItemJour_prcnt, 'double') . ", " . GetSQLValueString($ItemJour_amount, 'double') . ", " . GetSQLValueString($ItemJour_amount1, 'double') . ",
									" . GetSQLValueString($ItemJour_amount2, 'double') . "," . GetSQLValueString($Ledger_Number, 'text') . "," . GetSQLValueString($Ledger_NumberDebit, 'text') . ",
									" . GetSQLValueString($CostCent_CodeDebit, 'text') . "," . GetSQLValueString($Project_codeDebit, 'text') . "," . GetSQLValueString($Ledger_NumberCredit, 'text') . ",
									" . GetSQLValueString($CostCent_CodeCredit, 'text') . "," . GetSQLValueString($Project_CodeCredit, 'text') . "," . GetSQLValueString($Journal_Date, 'text') . ",
									" . GetSQLValueString($JournalDt_ValueDate, 'text') . "," . GetSQLValueString($ItemJour_Remark, 'text') . "," . GetSQLValueString($ItemJour_DateAmortized, 'text') . ",
									" . GetSQLValueString($ItemJour_PreviousDate, 'text') . "," . GetSQLValueString($ItemJour_PreviousAmount, 'double') . "," . GetSQLValueString($ItemJour_PreviousAmount1, 'double') . ",
									" . GetSQLValueString($ItemJour_PreviousAmount2, 'double') . "," . GetSQLValueString($ItemJour_Sold, 'double') . "," . GetSQLValueString($ItemJour_SaleDate, 'text') . ",
									" . GetSQLValueString($Ledger_NumberCash, 'text') . "," . GetSQLValueString($CostCent_CodeCash, 'text') . "," . GetSQLValueString($Project_CodeCash, 'text') . ",
									" . GetSQLValueString($ItemJour_CashAmount, 'double') . "," . GetSQLValueString($ItemJour_CashAmount1, 'double') . "," . GetSQLValueString($ItemJour_CashAmount2, 'double') . ",
									" . GetSQLValueString($Ledger_NumberProfit, 'text') . "," . GetSQLValueString($Ledger_NumberLost, 'text') . "," . GetSQLValueString($ItemJour_BelongsToGroup, 'int') . ",
									" . GetSQLValueString($Item_OriginalCode, 'text') . "," . GetSQLValueString($Synchronized, 'int') . "," . GetSQLValueString($itemjour_location, 'text') . ",
									" . GetSQLValueString($itemjour_barcode, 'text') . "," . GetSQLValueString($adjustmentdt_id, 'int') . "," . GetSQLValueString($assetsdt_id, 'int') . "
								)";
				// echo $query1."<br>";
				$res = @mysql_query($query1, $connection) or die(mysql_error());
			}
		}
	}
	//  exit;			
	if ($message != '')
		header("Location: ./Form_Amortization.php?actionmode=$actionmode&mainaction=$mainaction&message=" . $message);
	else header("Location: ./Form_Amortization.php?actionmode=$actionmode&mainaction=$mainaction&assetsid=$assets_id");
} else
if($actionmode=='submitassetsdt' and $vmode=='e' ){


	$assets_id 					= $_GET["assets_id"];
	$assets_date 				= $_GET["value12"];
	$accountno 					= $_GET["value17"];
	$amortization_currency_code = $_GET["value18"];
	$assets_name 				= $_GET["value19"];
	$assets_desc 				= $_GET["value22"];
	
	$ledger_number 				= $_GET["value1"];
	$currency_code 				= $_GET["value2"];
	$assetsdt_description 		= $_GET["value3"];
	$assetsdt_amount 			= $_GET["value4"];
	$assetsdt_amount1 			= $_GET["value5"];
	$assetsdt_amount2 			= $_GET["value6"];
	$newdate = explode('/', $_GET["value7"]);
	$assetsdt_valuedate 		= $newdate[2]."-".$newdate[1]."-".$newdate[0];
	$assetsdt_isvat 			= $_GET["value8"];
	$project_code 				= $_GET["value10"];
	$costcenter_code 			= $_GET["value11"];
	$assetsdt_id 				= $_GET["value9"];
	$assets_vatpref				= $_GET["value24"];
	$assetsdt_qty				= $_GET["value25"];
	$assetsdt_unitPrice			= $_GET["value26"];
	
	 //echo "[BRK]";echo $value4;exit;
			 
	$update = "update temp_assets set Assets_date = ".GetSQLValueString(insertMySQLDate($assets_date),'date')." 
							 ,LEDGER_NUMBER = ".GetSQLValueString($accountno,'text')."
							 ,CURRENCY_CODE = ".GetSQLValueString($amortization_currency_code,'text')."
							 ,Assets_Name = ".GetSQLValueString($assets_name,'text')."
							 ,COSTCENTer_CODE = ".GetSQLValueString($costcenter_code,'text')."
							 ,Project_code = ".GetSQLValueString($project_code,'text')."
							 ,Assets_DESC = ".GetSQLValueString($assets_desc,'text')."
							 ,VATFixedAssetAccount = ".GetSQLValueString($assets_vatpref,'text')."
				where assets_id = $assets_id";
	$result=mysql_query($update,$connection) or die(mysql_error());			
	if( $assetsdt_id !='' ) {
	$query = " update temp_assetsdt set ledger_number = ".GetSQLValueString($ledger_number,'text')."
				,currency_code = ".GetSQLValueString($currency_code,'text')."
				,assetsdt_description = ".GetSQLValueString($assetsdt_description,'text')."
				,assetsdt_amount =$value4
				,assetsdt_amount1 = ".GetSQLValueString($assetsdt_amount1,'double')."
				,assetsdt_amount2 = ".GetSQLValueString($assetsdt_amount2,'double')."
				,assetsdt_valuedate = '$assetsdt_valuedate'
				,assetsdt_isvat = ".GetSQLValueString($assetsdt_isvat,'double')."
				,project_code = ".GetSQLValueString($project_code,'text')."
				,costcenter_code = ".GetSQLValueString($costcenter_code,'text')."
				,assetsdt_quantity = ".GetSQLValueString($assetsdt_qty,'text')."
				,assetsdt_unitprice = ".GetSQLValueString($assetsdt_unitPrice,'text')."
				where assetsdt_id = $assetsdt_id";
		//echo $query;exit;	
	$result=mysql_query($query,$connection) or die(mysql_error());
	
	
	//Calculating Master Amounts
		$CalculateResult = Calculate_MasterAmountsFixedAssets($assets_id, $connection);
		//echo $CalculateResult;
		
	
	echo "[BRK]";
	
	draw_fixedassetsamount($assets_id,$base1,$base2,$DecimalBase,$DecimalBase1,$DecimalBase2,$Setup_VatPercentage,$connection) ;
		
	}
	
	}
	else if($actionmode=='submitassetsdt' and $vmode=='i' ){
	
	$assets_id 					= $_GET["assets_id"];
	$ledger_number			 	= $_GET["value1"];
	$currency_code 				= $_GET["value2"];
	$assetsdt_description 		= $_GET["value3"];
	$assetsdt_amount 			= $_GET["value4"];
	$assetsdt_amount1 			= $_GET["value5"];
	$assetsdt_amount2		 	= $_GET["value6"];
	$newdate 					= explode('/', $_GET["value7"]);
	$assetsdt_valuedate 		= $newdate[2]."-".$newdate[1]."-".$newdate[0];
	$assetsdt_isvat 			= $_GET["value8"];
	$project_code 				= $_GET["value10"];
	$costcenter_code 			= $_GET["value11"];
	$assets_vatpref				= $_GET["value24"];
	$assetsdt_qty				= $_GET["value25"];
	$assetsdt_unitPrice			= $_GET["value26"];
	
	 if ($assets_id=='')
		{
			$assets_date 				= $_GET["value12"];
			$journal_valuedate 			= $_GET["value13"];
			$assets_amount 				= $_GET["value14"];
			$assets_amount1 			= $_GET["value15"];
			$assets_amount2 			= $_GET["value16"];
			$accountno 					= $_GET["value17"];
			$amortization_currency_code = $_GET["value18"];
			$assets_name 				= $_GET["value19"];
			$costcenter_code 			= $_GET["value20"];
			$project_code 				= $_GET["value21"];
			$assets_desc 				= $_GET["value22"];
		}
				
	if ($assets_id == '')	
		$assets_id = PostHeader($assets_date,$journal_valuedate,$assets_amount,$assets_amount1,$assets_amount2,$accountno,$amortization_currency_code,$assets_name,$costcenter_code,$project_code,$assets_desc,$assets_vatpref,$_SESSION['useraccount'],$connection);
		
		
			fbegin();
		$queryid = "select max(assetsdt_id)+1 from (
					select max(assetsdt_id) as assetsdt_id from temp_assetsdt
					union
					select max(assetsdt_id) as assetsdt_id from assetsdt
					) as Q;";
		$resid = mysql_query($queryid,$connection);
		$rowid = mysql_fetch_row($resid);
		$assetsdt_id = $rowid[0];
		
	$query = " insert into temp_assetsdt(assetsdt_id,assets_id,ledger_number,currency_code,assetsdt_description,assetsdt_amount,assetsdt_amount1
				,assetsdt_amount2,assetsdt_valuedate,assetsdt_isvat,project_code,costcenter_code
				,assetsdt_quantity,assetsdt_unitprice) values
				(".GetSQLValueString($assetsdt_id,'text').", ".GetSQLValueString($assets_id,'text')."
				,".GetSQLValueString($ledger_number,'text')."
				,".GetSQLValueString($currency_code,'text')."
				, ".GetSQLValueString($assetsdt_description,'text')."
				, ".GetSQLValueString($assetsdt_amount,'double')."
				, ".GetSQLValueString($assetsdt_amount1,'double')."
				, ".GetSQLValueString($assetsdt_amount2,'double')."
				,'$assetsdt_valuedate'
				, ".GetSQLValueString($assetsdt_isvat,'double')."
				, ".GetSQLValueString($project_code,'text')."
				, ".GetSQLValueString($costcenter_code,'text')."
				,".GetSQLValueString($assetsdt_qty,'text')."
				,".GetSQLValueString($assetsdt_unitPrice,'text')."
				)";
		
	$result=mysql_query($query,$connection) or die(mysql_error());
	
				if ($result){
					fcommit();
				}
				 else{
					$message  = mysql_error(); 
					frollback();
					}
		
	//Calculating Master Amounts
		$CalculateResult = Calculate_MasterAmountsFixedAssets($assets_id, $connection);
	
		
	echo $assets_id;
	echo "[BRK]";
	//echo $query;
	draw_assetsdt($assets_id,$connection,$orderby,$startrange,$perpage,$option,$direction,$base1,$base2,'insert');
	
	echo "[BRK]";
	
	draw_fixedassetsamount($assets_id,$base1,$base2,$DecimalBase,$DecimalBase1,$DecimalBase2,$Setup_VatPercentage,$connection) ;
	
	echo "[BRK]";
	echo $assets_id;
	} else if ($actionmode == 'submititemjour' and $vmode == 'i') {

	$assetsdt_id = $_GET["assetsdt_id"];

	$itemjour_location = $_GET["value1"];
	$itemjour_barcode = $_GET["value2"];
	$itemjour_code = $_GET["value3"];
	$itemjour_description = $_GET["value4"];
	$itemjour_prcnt = $_GET["value5"];
	$itemjourproject_code = $_GET["value6"];
	$itemjourcostcent_code = $_GET["value7"];
	$itemjour_amount = $_GET["value8"];
	$itemjour_amount1 = $_GET["value9"];
	$itemjour_amount2 = $_GET["value10"];
	$ledger_numberdebit = $_GET["value11"];
	$ledger_numbercredit = $_GET["value12"];
	$project_codecredit = $_GET["value13"];
	$costcent_codecredit = $_GET["value14"];
	$itemjour_remark = $_GET["value15"];
	$itemjour_previousdate = $_GET["value16"];
	$txtCostCentCodeDt     = $_GET["value17"];

	if ($itemjour_previousdate == '')
		$itemjour_previousdate = NULL;
	else $itemjour_previousdate = insertMySQLDate($itemjour_previousdate);

	if ($Module_CostCenterDt == 1) {
		if (empty($txtCostCentCodeDt))	$CostCentCodeDt = 'Null';
		else	$CostCentCodeDt = "'" . $txtCostCentCodeDt . "'";
	} else {
		$CostCentCodeDt = $CostCentCode;
	}

	$query = "select ledger_number,assetsdt_valuedate,currency_code from temp_assetsdt where assetsdt_id = $assetsdt_id ";
	$result = @mysql_query($query, $connection) or die(mysql_error());
	$rows = mysql_fetch_array($result);
	$assetsdtledger_number = $rows[0];
	$assetsdt_valuedate = $rows[1];
	$currency_code = $rows[2];




	fbegin();
	if ($journal_distribution == '1') {
		$query = " insert into temp_assetsitemjour(
			itemjour_location ,itemjour_barcode ,itemjour_code ,itemjour_description ,itemjour_prcnt ,project_code ,costcent_code ,itemjour_amount ,
			itemjour_amount1 ,itemjour_amount2 ,ledger_numberdebit ,ledger_numbercredit ,project_codecredit ,costcent_codecredit ,itemjour_remark ,itemjour_previousdate 
			,logged_code,assetsdt_id,ledger_number,journaldt_valuedate,currency_code,csotcent_dist_code
			) values
			(" . GetSQLValueString($itemjour_location, 'text') . "
			," . GetSQLValueString($itemjour_barcode, 'text') . "
			," . GetSQLValueString($itemjour_code, 'text') . "
			, " . GetSQLValueString($itemjour_description, 'text') . "
			, " . GetSQLValueString($itemjour_prcnt, 'double') . "
			, " . GetSQLValueString($itemjourproject_code, 'text') . "
			, " . GetSQLValueString($itemjourcostcent_code, 'text') . "
			, " . GetSQLValueString($itemjour_amount, 'double') . "
			, " . GetSQLValueString($itemjour_amount1, 'double') . "
			, " . GetSQLValueString($itemjour_amount2, 'double') . "
			, " . GetSQLValueString($ledger_numberdebit, 'text') . "
			, " . GetSQLValueString($ledger_numbercredit, 'text') . "
			, " . GetSQLValueString($project_codecredit, 'text') . "
			, " . GetSQLValueString($costcent_codecredit, 'text') . "
			, " . GetSQLValueString($itemjour_remark, 'text') . "
			, " . GetSQLValueString($itemjour_previousdate, 'date') . "
			, " . GetSQLValueString($_SESSION['useraccount'], 'text') . "
			, " . GetSQLValueString($assetsdt_id, 'text') . "
			, " . GetSQLValueString($assetsdtledger_number, 'text') . "
			, " . GetSQLValueString($assetsdt_valuedate, 'date') . "
			, " . GetSQLValueString($currency_code, 'text') . "
			, " . GetSQLValueString($costcent_codecredit, 'text') . "
			)";
	} else {
		$query = " insert into temp_assetsitemjour(
				itemjour_location ,itemjour_barcode ,itemjour_code ,itemjour_description ,itemjour_prcnt ,project_code ,costcent_code ,itemjour_amount ,
				itemjour_amount1 ,itemjour_amount2 ,ledger_numberdebit ,ledger_numbercredit ,project_codecredit ,costcent_codecredit ,itemjour_remark ,itemjour_previousdate 
				,logged_code,assetsdt_id,ledger_number,journaldt_valuedate,currency_code
				) values
				(" . GetSQLValueString($itemjour_location, 'text') . "
				," . GetSQLValueString($itemjour_barcode, 'text') . "
				," . GetSQLValueString($itemjour_code, 'text') . "
				, " . GetSQLValueString($itemjour_description, 'text') . "
				, " . GetSQLValueString($itemjour_prcnt, 'double') . "
				, " . GetSQLValueString($itemjourproject_code, 'text') . "
				, " . GetSQLValueString($itemjourcostcent_code, 'text') . "
				, " . GetSQLValueString($itemjour_amount, 'double') . "
				, " . GetSQLValueString($itemjour_amount1, 'double') . "
				, " . GetSQLValueString($itemjour_amount2, 'double') . "
				, " . GetSQLValueString($ledger_numberdebit, 'text') . "
				, " . GetSQLValueString($ledger_numbercredit, 'text') . "
				, " . GetSQLValueString($project_codecredit, 'text') . "
				, " . GetSQLValueString($costcent_codecredit, 'text') . "
				, " . GetSQLValueString($itemjour_remark, 'text') . "
				, " . GetSQLValueString($itemjour_previousdate, 'date') . "
				, " . GetSQLValueString($_SESSION['useraccount'], 'text') . "
				, " . GetSQLValueString($assetsdt_id, 'text') . "
				, " . GetSQLValueString($assetsdtledger_number, 'text') . "
				, " . GetSQLValueString($assetsdt_valuedate, 'date') . "
				, " . GetSQLValueString($currency_code, 'text') . "
				)";
	}
	//echo $query;
	$result = @mysql_query($query, $connection) or die(mysql_error());

	if ($result) {
		fcommit();
	} else {
		$message  = mysql_error();
		frollback();
	}

	// $project_code = $itemjourproject_code;
	// $costcent_code = $itemjourcostcent_code;
	// $cstjour_prcnt = '100';
	// $cstjour_amount = $itemjour_amount;
	// $cstjour_amount1 = $itemjour_amount1;
	// $cstjour_amount2 = $itemjour_amount2;
	// $numrow = 0;
	// fbegin();
	// if ($project_code != '' || $costcent_code != '') {
	// 	if($journal_distribution == 1 && $txtCostCentCodeDt != '' ){// && $txtJourDtDistCode != ''
	// 					$query = "select costcent_code, ifnull(CostCent_Percentage,0), ($cstjour_amount*ifnull(CostCent_Percentage,0))/100 as amnt, ($cstjour_amount1*ifnull(CostCent_Percentage,0))/100 as amnt1,
	// 								($cstjour_amount2*ifnull(CostCent_Percentage,0))/100  as amnt2 from costcent
	// 								where costcent_code like '$txtCostCentCodeDt%' and costcent_code <> '$txtCostCentCodeDt' "; 
	// 					$result = mysql_query($query,$connection);
	// 					$numrow = mysql_num_rows($result);
	// 					while($row = mysql_fetch_array($result)){
	// 							if ($row[1] > 0){ 
	// 							$queryCst = " INSERT INTO temp_assetscstjour (project_code,costcent_code,Cstjour_prcnt,Cstjour_amount,Cstjour_amount1,Cstjour_amount2,assetsdt_id,user_account,logged_code ) ". 
	// 									" VALUES ( ".GetSQLValueString($project_code,'text').", ".
	// 									"          ".GetSQLValueString($row[0],'text').", ".GetSQLValueString($row[1],'double').", ".
	// 									"          ".GetSQLValueString($row[2],'double').", ".GetSQLValueString($row[3],'double').", ".
	// 									"          ".GetSQLValueString($row[4],'double').", ".GetSQLValueString($assetsdt_id,'text').", ".GetSQLValueString($_SESSION['useraccount'],'text').", ".
	// 									"          ".GetSQLValueString($_SESSION['useraccount'],'text')."	) "; 
	// 							$resCst = mysql_query($queryCst,$connection);
	// 						} 
	// 					}
	// 				}
	// 	if($numrow == 0) {
	// 	$query = " insert into temp_assetscstjour(
	// 				project_code,costcent_code,Cstjour_prcnt,Cstjour_amount,Cstjour_amount1,Cstjour_amount2,assetsdt_id,user_account,logged_code
	// 				) values
	// 				(".GetSQLValueString($project_code,'text')."
	// 				,".GetSQLValueString($costcent_code,'text')."
	// 				,".GetSQLValueString($cstjour_prcnt,'text')."
	// 				, ".GetSQLValueString($cstjour_amount,'text')."
	// 				, ".GetSQLValueString($cstjour_amount1,'double')."
	// 				, ".GetSQLValueString($cstjour_amount2,'text')."
	// 				, ".GetSQLValueString($assetsdt_id,'text')."
	// 				, ".GetSQLValueString($_SESSION['useraccount'],'text')."
	// 				, ".GetSQLValueString($_SESSION['useraccount'],'text')."
	// 				)";

	// 	}
	// }

	// $result=@mysql_query($query,$connection) or die(mysql_error());

	// 			if ($result){
	// 				fcommit();
	// 			}
	// 			else{
	// 				$message  = mysql_error(); 
	// 				frollback();
	// 				}

	//echo $query;
	echo "[BRK]";
	draw_itemjour($assetsdt_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $defaultproject, $defaultcostcent);
} else if ($actionmode == 'submititemjour2') {

	$assetsdt_id = $_GET["assetsdt_id"];

	$itemjour2ledger_number 			= $_GET["value1"];
	$itemjour2itemjour_prcnt 			= $_GET["value2"];
	$itemjour2itemjour_code 			= $_GET["value3"];
	$itemjour2itemjour_barcode 			= $_GET["value4"];
	$itemjour2itemjour_description 		= $_GET["value5"];
	$itemjour2journaldt_valuedate 		= $_GET["value6"];
	$itemjour2itemjour_location 		= $_GET["value7"];
	$itemjour2currency_code 			= $_GET["value8"];
	$itemjour2itemjour_amount 			= $_GET["value9"];
	$itemjour2itemjour_amount1 			= $_GET["value10"];
	$itemjour2itemjour_amount2 			= $_GET["value11"];
	$itemjour2itemjour_previousdate 	= $_GET["value12"];
	$itemjour2itemjour_previousamount 	= $_GET["value13"];
	$itemjour2itemjour_previousamount1	= $_GET["value14"];
	$itemjour2itemjour_previousamount2 	= $_GET["value15"];
	$itemjour2itemjour_dateamortized 	= $_GET["value16"];
	$costcent_code 						= $_GET["value17"];
	$project_code 						= $_GET["value18"];
	$Ledger_NumberDebit 				= $_GET["value19"];
	$Ledger_NumberCredit 				= $_GET["value20"];
	$costcent_codecredit 				= $_GET["value21"];
	$project_codecredit 				= $_GET["value22"];
	$ItemJour_Sold 						= $_GET["value23"];
	$ItemJour_SaleDate 					= $_GET["value24"];
	$Ledger_NumberCash 					= $_GET["value25"];
	$CostCent_CodeCash 					= $_GET["value26"];
	$Project_CodeCash 					= $_GET["value27"];
	$ItemJour_CashAmount 				= $_GET["value28"];
	$ItemJour_CashAmount1 				= $_GET["value29"];
	$ItemJour_CashAmount2 				= $_GET["value30"];
	$Ledger_NumberProfit 				= $_GET["value31"];
	$Ledger_NumberLost 					= $_GET["value32"];
	$itemjour_remark 					= $_GET["value33"];
	$autoincrement						= $_GET['autoincrement'];



	fbegin();
	$Item_OriginalCode = $itemjour2itemjour_code;
	if ($vmode == 'i')
		$query = " insert into itemjour(
				Ledger_Number
				,ItemJour_prcnt
				,ItemJour_Code
				,itemjour_barcode
				,ItemJour_Description
				,JournalDt_ValueDate
				,itemjour_location
				,Currency_Code
				,ItemJour_amount
				,ItemJour_amount1
				,ItemJour_amount2
				,ItemJour_PreviousDate
				,ItemJour_PreviousAmount
				,ItemJour_PreviousAmount1
				,ItemJour_PreviousAmount2
				,ItemJour_DateAmortized
				,Costcent_code
				,Project_Code
				,Ledger_NumberDebit
				,Ledger_NumberCredit
				,CostCent_CodeCredit
				,Project_CodeCredit
				,ItemJour_Sold
				,ItemJour_SaleDate
				,Ledger_NumberCash
				,CostCent_CodeCash
				,Project_CodeCash
				,ItemJour_CashAmount
				,ItemJour_CashAmount1
				,ItemJour_CashAmount2
				,Ledger_NumberProfit
				,Ledger_NumberLost
				,ItemJour_Remark
				,ItemJour_BelongsToGroup
				,Project_codeDebit
				,Item_OriginalCode
				,CostCent_CodeDebit
				) values
				(" . GetSQLValueString($itemjour2ledger_number, 'text') . "
				," . GetSQLValueString($itemjour2itemjour_prcnt, 'double') . "
				," . GetSQLValueString($itemjour2itemjour_code, 'text') . "
				, " . GetSQLValueString($itemjour2itemjour_barcode, 'text') . "
				, " . GetSQLValueString($itemjour2itemjour_description, 'text') . "
				, " . GetSQLValueString(insertMySQLDate($itemjour2journaldt_valuedate), 'date') . "
				, " . GetSQLValueString($itemjour2itemjour_location, 'text') . "
				, " . GetSQLValueString($itemjour2currency_code, 'text') . "
				, " . GetSQLValueString($itemjour2itemjour_amount, 'double') . "
				, " . GetSQLValueString($itemjour2itemjour_amount1, 'double') . "
				, " . GetSQLValueString($itemjour2itemjour_amount2, 'double') . "
				, " . GetSQLValueString(insertMySQLDate($itemjour2itemjour_previousdate), 'date') . "
				, " . GetSQLValueString($itemjour2itemjour_previousamount, 'double') . "
				, " . GetSQLValueString($itemjour2itemjour_previousamount1, 'double') . "
				, " . GetSQLValueString($itemjour2itemjour_previousamount2, 'double') . "
				, " . GetSQLValueString(insertMySQLDate($itemjour2itemjour_dateamortized), 'date') . "
				, " . GetSQLValueString($costcent_code, 'text') . "
				, " . GetSQLValueString($project_code, 'text') . "
				, " . GetSQLValueString($Ledger_NumberDebit, 'text') . "
				, " . GetSQLValueString($Ledger_NumberCredit, 'text') . "
				, " . GetSQLValueString($costcent_codecredit, 'text') . "
				, " . GetSQLValueString($project_codecredit, 'text') . "
				, " . GetSQLValueString($ItemJour_Sold, 'text') . "
				, " . GetSQLValueString(insertMySQLDate($ItemJour_SaleDate), 'date') . "
				, " . GetSQLValueString($Ledger_NumberCash, 'text') . "
				, " . GetSQLValueString($CostCent_CodeCash, 'text') . "
				, " . GetSQLValueString($Project_CodeCash, 'text') . "
				, " . GetSQLValueString($ItemJour_CashAmount, 'double') . "
				, " . GetSQLValueString($ItemJour_CashAmount1, 'double') . "
				, " . GetSQLValueString($ItemJour_CashAmount2, 'double') . "
				, " . GetSQLValueString($Ledger_NumberProfit, 'text') . "
				, " . GetSQLValueString($Ledger_NumberLost, 'text') . "
				, " . GetSQLValueString($itemjour_remark, 'text') . "
				, " . GetSQLValueString($belongstogroup, 'int') . "
				, " . GetSQLValueString($txtProjectCode, 'int') . "
				, " . GetSQLValueString($Item_OriginalCode, 'text') . "
				, " . GetSQLValueString($txtCostCentCode, 'text') . "
				)";
	else if ($vmode == 'e')
		$query = " update itemjour set 
		Ledger_Number = " . GetSQLValueString($itemjour2ledger_number, 'text') . "					,ItemJour_prcnt = " . GetSQLValueString($itemjour2itemjour_prcnt, 'double') . "
								,ItemJour_Code = " . GetSQLValueString($itemjour2itemjour_code, 'text') . "							,itemjour_barcode = " . GetSQLValueString($itemjour2itemjour_barcode, 'text') . "
								,ItemJour_Description = " . GetSQLValueString($itemjour2itemjour_description, 'text') . "			,JournalDt_ValueDate = " . GetSQLValueString(insertMySQLDate($itemjour2journaldt_valuedate), 'date') . "
								,itemjour_location = " . GetSQLValueString($itemjour2itemjour_location, 'text') . "					,Currency_Code = " . GetSQLValueString($itemjour2currency_code, 'text') . "
								,ItemJour_amount = 	" . GetSQLValueString($itemjour2itemjour_amount, 'double') . "					,ItemJour_amount1 = " . GetSQLValueString($itemjour2itemjour_amount1, 'double') . "
								,ItemJour_amount2 = " . GetSQLValueString($itemjour2itemjour_amount2, 'double') . " 					,ItemJour_PreviousDate = " . GetSQLValueString(insertMySQLDate($itemjour2itemjour_previousdate), 'date') . "
								,ItemJour_PreviousAmount =  " . GetSQLValueString($itemjour2itemjour_previousamount, 'double') . "	,ItemJour_PreviousAmount1 = " . GetSQLValueString($itemjour2itemjour_previousamount1, 'double') . "
								,ItemJour_PreviousAmount2 = " . GetSQLValueString($itemjour2itemjour_previousamount2, 'double') . "	,ItemJour_DateAmortized = " . GetSQLValueString(insertMySQLDate($itemjour2itemjour_dateamortized), 'date') . "
								,Costcent_code = " . GetSQLValueString($costcent_code, 'text') . "									,Project_Code = " . GetSQLValueString($project_code, 'text') . "
								,Ledger_NumberDebit = " . GetSQLValueString($Ledger_NumberDebit, 'text') . "							,Ledger_NumberCredit = " . GetSQLValueString($Ledger_NumberCredit, 'text') . "
								,CostCent_CodeCredit = " . GetSQLValueString($costcent_codecredit, 'text') . "						,Project_CodeCredit = " . GetSQLValueString($project_codecredit, 'text') . "
								,ItemJour_Sold = " . GetSQLValueString($ItemJour_Sold, 'text') . "									,ItemJour_SaleDate = " . GetSQLValueString(insertMySQLDate($ItemJour_SaleDate), 'date') . "
								,Ledger_NumberCash = " . GetSQLValueString($Ledger_NumberCash, 'text') . "							,CostCent_CodeCash = " . GetSQLValueString($CostCent_CodeCash, 'text') . "
								,Project_CodeCash = " . GetSQLValueString($Project_CodeCash, 'text') . "								,ItemJour_CashAmount = " . GetSQLValueString($ItemJour_CashAmount, 'double') . "
								,ItemJour_CashAmount1 = " . GetSQLValueString($ItemJour_CashAmount1, 'double') . "					,ItemJour_CashAmount2 = " . GetSQLValueString($ItemJour_CashAmount2, 'double') . "
								,Ledger_NumberProfit =  " . GetSQLValueString($Ledger_NumberProfit, 'text') . "						,Ledger_NumberLost = " . GetSQLValueString($Ledger_NumberLost, 'text') . "
								,ItemJour_Remark = " . GetSQLValueString($itemjour_remark, 'text') . "								,ItemJour_BelongsToGroup = " . GetSQLValueString($belongstogroup, 'int') . "
								,Project_codeDebit = " . GetSQLValueString($txtProjectCode, 'int') . " 								,Item_OriginalCode = " . GetSQLValueString($Item_OriginalCode, 'text') . "
								,CostCent_CodeDebit = " . GetSQLValueString($txtCostCentCode, 'text') . " where Autoincrement = $autoincrement ";
	$result = @mysql_query($query, $connection) or die(mysql_error());

	if ($result) {
		fcommit();
	} else {
		$message  = mysql_error();
		frollback();
	}


	//echo $query;
	draw_itemjour2($connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $StartFinancialDate, $EndFinancialDate, $DecimalBase, $DecimalBase1, $DecimalBase2, $DateTimePickerCloseYear);
} else if ($actionmode == 'submitcstjour' and $vmode == 'i') {

	$assetsdt_id = $_GET["assetsdt_id"];

	$project_code = $_GET["value1"];
	$costcent_code = $_GET["value2"];
	$cstjour_prcnt = $_GET["value3"];
	$cstjour_amount = $_GET["value4"];
	$cstjour_amount1 = $_GET["value5"];
	$cstjour_amount2 = $_GET["value6"];

	fbegin();

	$query = " insert into temp_assetscstjour(
							project_code,costcent_code,Cstjour_prcnt,Cstjour_amount,Cstjour_amount1,Cstjour_amount2,assetsdt_id,logged_code
				) values
				(" . GetSQLValueString($project_code, 'text') . "
				," . GetSQLValueString($costcent_code, 'text') . "
				," . GetSQLValueString($cstjour_prcnt, 'text') . "
				, " . GetSQLValueString($cstjour_amount, 'text') . "
				, " . GetSQLValueString($cstjour_amount1, 'double') . "
				, " . GetSQLValueString($cstjour_amount2, 'text') . "
				, " . GetSQLValueString($assetsdt_id, 'text') . "
				, " . GetSQLValueString($_SESSION['useraccount'], 'text') . "
				)";


	$result = @mysql_query($query, $connection) or die(mysql_error());

	if ($result) {
		fcommit();
	} else {
		$message  = mysql_error();
		frollback();
	}


	//echo $query;
	echo "[BRK]";
	draw_FixedAssetscstjour($assetsdt_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2);
} else if ($actionmode == 'submititemjour' and $vmode == 'e') {

	$assetsdt_id = $_GET["assetsdt_id"];

	$itemjour_location = $_GET["value1"];
	$itemjour_barcode = $_GET["value2"];
	$itemjour_code = $_GET["value3"];
	$itemjour_description = $_GET["value4"];
	$itemjour_prcnt = $_GET["value5"];
	$itemjourproject_code = $_GET["value6"];
	$itemjourcostcent_code = $_GET["value7"];
	$itemjour_amount = $_GET["value8"];
	$itemjour_amount1 = $_GET["value9"];
	$itemjour_amount2 = $_GET["value10"];
	$ledger_numberdebit = $_GET["value11"];
	$ledger_numbercredit = $_GET["value12"];
	$project_codecredit = $_GET["value13"];
	$costcent_codecredit = $_GET["value14"];
	$itemjour_remark = $_GET["value15"];
	$itemjour_previousdate = $_GET["value16"];
	$autoincrement = $_GET["autoincrement"];

	if ($itemjour_previousdate == '')
		$itemjour_previousdate = NULL;
	else $itemjour_previousdate = insertMySQLDate($itemjour_previousdate);

	$query = "select ledger_number ,assetsdt_valuedate,currency_code from temp_assetsdt where assetsdt_id = $assetsdt_id ";
	$result = @mysql_query($query, $connection) or die(mysql_error());
	$rows = mysql_fetch_array($result);
	$assetsdtledger_number = $rows[0];
	$assetsdt_valuedate = $rows[1];
	$currency_code = $rows[2];

	fbegin();
	if ($journal_distribution == '1') {
		$query = " update temp_assetsitemjour set 
			itemjour_location  = " . GetSQLValueString($itemjour_location, 'text') . "
			,itemjour_barcode = " . GetSQLValueString($itemjour_barcode, 'text') . "
			,itemjour_code = " . GetSQLValueString($itemjour_code, 'text') . "
			,itemjour_description = " . GetSQLValueString($itemjour_description, 'text') . "
			,itemjour_prcnt = " . GetSQLValueString($itemjour_prcnt, 'text') . "
			,project_code = " . GetSQLValueString($itemjourproject_code, 'text') . "
			,costcent_code = " . GetSQLValueString($itemjourcostcent_code, 'text') . "
			,itemjour_amount = " . GetSQLValueString($itemjour_amount, 'text') . "
			,itemjour_amount1 = " . GetSQLValueString($itemjour_amount1, 'text') . "
			,itemjour_amount2 = " . GetSQLValueString($itemjour_amount2, 'text') . "
			,ledger_numberdebit = " . GetSQLValueString($ledger_numberdebit, 'text') . "
			,ledger_numbercredit = " . GetSQLValueString($ledger_numbercredit, 'text') . "
			,project_codecredit = " . GetSQLValueString($project_codecredit, 'text') . "
			,costcent_codecredit = " . GetSQLValueString($costcent_codecredit, 'text') . "
			,itemjour_remark = " . GetSQLValueString($itemjour_remark, 'text') . "
			,itemjour_previousdate = " . GetSQLValueString($itemjour_previousdate, 'text') . "
			,logged_code = " . GetSQLValueString($_SESSION['useraccount'], 'Date') . "
			,ledger_number = " . GetSQLValueString($assetsdtledger_number, 'text') . "
			,journaldt_valuedate = " . GetSQLValueString($assetsdt_valuedate, 'date') . "
			,currency_code = " . GetSQLValueString($currency_code, 'text') . "
			,csotcent_dist_code = " . GetSQLValueString($costcent_codecredit, 'text') . "
			where autoincrement = $autoincrement ";
	} else {
		$query = " update temp_assetsitemjour set 
				itemjour_location  = " . GetSQLValueString($itemjour_location, 'text') . "
				,itemjour_barcode = " . GetSQLValueString($itemjour_barcode, 'text') . "
				,itemjour_code = " . GetSQLValueString($itemjour_code, 'text') . "
				,itemjour_description = " . GetSQLValueString($itemjour_description, 'text') . "
				,itemjour_prcnt = " . GetSQLValueString($itemjour_prcnt, 'text') . "
				,project_code = " . GetSQLValueString($itemjourproject_code, 'text') . "
				,costcent_code = " . GetSQLValueString($itemjourcostcent_code, 'text') . "
				,itemjour_amount = " . GetSQLValueString($itemjour_amount, 'text') . "
				,itemjour_amount1 = " . GetSQLValueString($itemjour_amount1, 'text') . "
				,itemjour_amount2 = " . GetSQLValueString($itemjour_amount2, 'text') . "
				,ledger_numberdebit = " . GetSQLValueString($ledger_numberdebit, 'text') . "
				,ledger_numbercredit = " . GetSQLValueString($ledger_numbercredit, 'text') . "
				,project_codecredit = " . GetSQLValueString($project_codecredit, 'text') . "
				,costcent_codecredit = " . GetSQLValueString($costcent_codecredit, 'text') . "
				,itemjour_remark = " . GetSQLValueString($itemjour_remark, 'text') . "
				,itemjour_previousdate = " . GetSQLValueString($itemjour_previousdate, 'text') . "
				,logged_code = " . GetSQLValueString($_SESSION['useraccount'], 'Date') . "
				,ledger_number = " . GetSQLValueString($assetsdtledger_number, 'text') . "
				,journaldt_valuedate = " . GetSQLValueString($assetsdt_valuedate, 'date') . "
				,currency_code = " . GetSQLValueString($currency_code, 'text') . "
				where autoincrement = $autoincrement ";
	}

	//echo $query;exit;
	$result = @mysql_query($query, $connection) or die(mysql_error());

	if ($result) {
		fcommit();
	} else {
		$message  = mysql_error();
		frollback();
	}
} else if ($actionmode == 'submitcstjour' and $vmode == 'e') {

	$assetsdt_id = $_GET["assetsdt_id"];

	$project_code = $_GET["value1"];
	$costcent_code = $_GET["value2"];
	$cstjour_prcnt = $_GET["value3"];
	$cstjour_amount = $_GET["value4"];
	$cstjour_amount1 = $_GET["value5"];
	$cstjour_amount2 = $_GET["value6"];
	$fixedassetscstjour_id = $_GET["fixedassetscstjour_id"];

	fbegin();

	$query = " update temp_assetscstjour set project_code=" . GetSQLValueString($project_code, 'text') . "
										,costcent_code=" . GetSQLValueString($costcent_code, 'text') . "
										,cstjour_prcnt=" . GetSQLValueString($cstjour_prcnt, 'double') . "
										,cstjour_amount=" . GetSQLValueString($cstjour_amount, 'double') . "
										,cstjour_amount1=" . GetSQLValueString($cstjour_amount1, 'double') . "
										,cstjour_amount2=" . GetSQLValueString($cstjour_amount2, 'double') . "
										,assetsdt_id=" . GetSQLValueString($assetsdt_id, 'double') . "
										,logged_code=" . GetSQLValueString($_SESSION['useraccount'], 'text') . "
										where Cst_increment = $fixedassetscstjour_id ";


	//echo $query;
	$result = @mysql_query($query, $connection) or die(mysql_error());

	if ($result) {
		fcommit();
	} else {
		$message  = mysql_error();
		frollback();
	}


	//echo $query;
	draw_FixedAssetscstjour($assetsdt_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2);
} else if ($actionmode == 'changeperpage') {

	draw_assetsdt($assets_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, 'changeperpage');
} else if ($actionmode == 'changeperpageItemjour2') {
	$strt = $_GET["strt"];
	$perpage = $_GET["perpage"];
	$morelines = $_GET["morelines"];
	$orderby = $_GET["orderby"];
	$option = $_GET["option"];


	draw_itemjour2($connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $StartFinancialDate, $EndFinancialDate, $DecimalBase, $DecimalBase1, $DecimalBase2, $DateTimePickerCloseYear);
} else if ($actionmode == 'changeperpageamortjournaldt') {
	$strt = $_GET["strt"];
	$perpage = $_GET["perpage"];
	$morelines = $_GET["morelines"];
	$orderby = $_GET["orderby"];
	$option = $_GET["option"];
	$journal_id = $_GET["journal_id"];

	draw_amortjournaldtgrid($journal_id, $connection, $orderby, $strt, $perpage, $option, $direction, $base1, $base2, $actionbefore);
} else if ($actionmode == 'changeperpagesalejourjournaldt') {
	$strt = $_GET["strt"];
	$perpage = $_GET["perpage"];
	$morelines = $_GET["morelines"];
	$orderby = $_GET["orderby"];
	$option = $_GET["option"];

	$journal_id = $_GET["journal_id"];


	draw_salejourjournaldtgrid($journal_id, $connection, $orderby, $strt, $perpage, $option, $direction, $base1, $base2, $actionbefore);
} else if ($actionmode == 'nextpage') {
	$vid = $_GET["vid"];
	$assets_id = $_GET["assets_id"];
	$morelines = $_GET["morelines"];
	$orderby = $_GET["orderby"];
	$strt = $_GET["strt"];
	$end = $_GET["end"];
	$perpage = $_GET["perpage"];
	$option = $_GET["option"];

	echo
	draw_assetsdt($assets_id, $connection, $orderby, $strt, $perpage, $option, $direction, $base1, $base2, 'nextpage');
} else if ($actionmode == 'nextpageamortjournaldt') {
	$vid = $_GET["vid"];
	$journal_id = $_GET["journal_id"];
	$morelines = $_GET["morelines"];
	$orderby = $_GET["orderby"];
	$strt = $_GET["strt"];
	$end = $_GET["end"];
	$perpage = $_GET["perpage"];
	$option = $_GET["option"];

	echo
	draw_amortjournaldtgrid($journal_id, $connection, $orderby, $strt, $perpage, $option, $direction, $base1, $base2, $actionbefore);
} else if ($actionmode == 'nextpagesalejourjournaldt') {
	$vid = $_GET["vid"];
	$journal_id = $_GET["journal_id"];
	$morelines = $_GET["morelines"];
	$orderby = $_GET["orderby"];
	$strt = $_GET["strt"];
	$end = $_GET["end"];
	$perpage = $_GET["perpage"];
	$option = $_GET["option"];

	echo
	draw_salejourjournaldtgrid($journal_id, $connection, $orderby, $strt, $perpage, $option, $direction, $base1, $base2, $actionbefore);
} else if ($actionmode == 'nextpageitemjour2') {
	$vid = $_GET["vid"];
	$assets_id = $_GET["assets_id"];
	$morelines = $_GET["morelines"];
	$orderby = $_GET["orderby"];
	$strt = $_GET["strt"];
	$end = $_GET["end"];
	$perpage = $_GET["perpage"];
	$option = $_GET["option"];

	echo draw_itemjour2($connection, $orderby, $strt, $perpage, $option, $direction, $base1, $base2, $StartFinancialDate, $EndFinancialDate, $DecimalBase, $DecimalBase1, $DecimalBase2, $DateTimePickerCloseYear);
} else if ($actionmode == 'cleargrid') {

	draw_assetsdt($assets_id, $connection, $orderby, $strt, $perpage, $option, $direction, $base1, $base2, 'cleargrid');
}
//Search Mode	
else if ($mainaction == "search" && $SAssetsId != '') {

	$ErrorExists = 0;
	//Copy Data From Main Tables To Temp Tables
	$query = " INSERT INTO temp_assets (Assets_refgovernment, Assets_ref, Assets_date, Assets_name, Project_Code, Costcenter_code, Assets_amount, Assets_amount1, Assets_amount2, Assets_mode, Ledger_number, Currency_code, Assets_desc, Assets_sysdate, Assets_time, Assets_user, Journal_refgovernment, Curdt_rate, Curdt_parity, Journal_id, Assets_Edit, Assets_BelongsToGroup, Journal_valuedate, Synchronized, SendReceive_Reference
				, Assets_MaturityDate, assets_id, VATFixedAssetAccount, assets_price, assets_price1, assets_price2, assets_vat, assets_vat1, assets_vat2, User_Account ,Logged_code ) " .
		" SELECT Assets_refgovernment, Assets_ref, Assets_date, Assets_name, Project_Code, Costcenter_code, Assets_amount, Assets_amount1, Assets_amount2, Assets_mode, Ledger_number, Currency_code, Assets_desc, Assets_sysdate, Assets_time, Assets_user, Journal_refgovernment, Curdt_rate, Curdt_parity, Journal_id, Assets_Edit, Assets_BelongsToGroup, Journal_valuedate, Synchronized, SendReceive_Reference
				, Assets_MaturityDate, assets_id,VATFixedAssetAccount, assets_price, assets_price1, assets_price2, assets_vat, assets_vat1, assets_vat2, User_Account, '" . $_SESSION['useraccount'] . "' " .
		" FROM assets " .
		" WHERE assets_id = " . $SAssetsId;
	// echo $query;exit;
	if (!updateData($connection, $query, true)) {
		$ErrorExists = 1;
		$message  = mysql_error();
	} else {

		if ($journal_distribution == '1') {

			$query = " INSERT INTO temp_assetsdt(assetsdt_id, assets_id, ledger_number, currency_code, assetsdt_description, assetsdt_amount, assetsdt_amount1, assetsdt_amount2, assetsdt_valuedate, assetsdt_isvat, project_code, costcenter_code, assetsdt_quantity,
				assetsdt_unitprice,	assetsdt_unitprice1, assetsdt_unitprice2, logged_code,User_Account,csotcent_dist_code) 
				select  assetsdt_id, assets_id, ledger_number, currency_code, assetsdt_description, assetsdt_amount, assetsdt_amount1, assetsdt_amount2, assetsdt_valuedate, assetsdt_isvat, project_code, costcenter_code, assetsdt_quantity,
				assetsdt_unitprice,	assetsdt_unitprice1, assetsdt_unitprice2
					,'" . $_SESSION['useraccount'] . "','" . $_SESSION['useraccount'] . "',costcenter_code	
					from assetsdt where assets_id =  " . $SAssetsId;
		} else {
			$query = " INSERT INTO temp_assetsdt(assetsdt_id, assets_id, ledger_number, currency_code, assetsdt_description, assetsdt_amount, assetsdt_amount1, assetsdt_amount2, assetsdt_valuedate, assetsdt_isvat, project_code, costcenter_code, assetsdt_quantity,
				assetsdt_unitprice,	assetsdt_unitprice1, assetsdt_unitprice2, logged_code,User_Account) 
				select  assetsdt_id, assets_id, ledger_number, currency_code, assetsdt_description, assetsdt_amount, assetsdt_amount1, assetsdt_amount2, assetsdt_valuedate, assetsdt_isvat, project_code, costcenter_code, assetsdt_quantity,
				assetsdt_unitprice,	assetsdt_unitprice1, assetsdt_unitprice2
					,'" . $_SESSION['useraccount'] . "','" . $_SESSION['useraccount'] . "'	
					from assetsdt where assets_id =  " . $SAssetsId;
		}
		if (!updateData($connection, $query, true)) {
			$ErrorExists = 1;
			$message  = mysql_error();
		} else {
			if ($journal_distribution == '1') {
				$query = " INSERT INTO temp_assetsitemjour(assetsdt_id, ItemJour_Code, Journaldt_id, ItemJour_Description, Currency_Code, Costcent_code, Project_Code, ItemJour_prcnt, ItemJour_amount, ItemJour_amount1, ItemJour_amount2, Ledger_Number, Ledger_NumberDebit, CostCent_CodeDebit, Project_codeDebit, Ledger_NumberCredit, CostCent_CodeCredit, Project_CodeCredit, Journal_Date, JournalDt_ValueDate, ItemJour_Remark, ItemJour_DateAmortized, ItemJour_PreviousDate, ItemJour_PreviousAmount, ItemJour_PreviousAmount1, ItemJour_PreviousAmount2, ItemJour_Sold, ItemJour_SaleDate, Ledger_NumberCash, CostCent_CodeCash, Project_CodeCash, ItemJour_CashAmount, ItemJour_CashAmount1, ItemJour_CashAmount2, Ledger_NumberProfit, Ledger_NumberLost, ItemJour_BelongsToGroup, Item_OriginalCode, Synchronized, itemjour_location, itemjour_barcode, logged_code,csotcent_dist_code)
						select assetsdt_id, ItemJour_Code, Journaldt_id, ItemJour_Description, Currency_Code, Costcent_code, Project_Code, ItemJour_prcnt, ItemJour_amount, ItemJour_amount1, ItemJour_amount2, Ledger_Number, Ledger_NumberDebit, CostCent_CodeDebit, Project_codeDebit, Ledger_NumberCredit, CostCent_CodeCredit, Project_CodeCredit, Journal_Date, JournalDt_ValueDate, ItemJour_Remark, ItemJour_DateAmortized, ItemJour_PreviousDate, ItemJour_PreviousAmount, ItemJour_PreviousAmount1, ItemJour_PreviousAmount2, ItemJour_Sold, ItemJour_SaleDate, Ledger_NumberCash, CostCent_CodeCash, Project_CodeCash, ItemJour_CashAmount, ItemJour_CashAmount1, ItemJour_CashAmount2, Ledger_NumberProfit, Ledger_NumberLost, ItemJour_BelongsToGroup, Item_OriginalCode, Synchronized, itemjour_location, itemjour_barcode, 
							'" . $_SESSION['useraccount'] . "',CostCent_CodeCredit
						from assetsitemjour where assetsdt_id in (select assetsdt_id from assetsdt where assets_id = " . $SAssetsId . ") ";
			} else {
				$query = " INSERT INTO temp_assetsitemjour(assetsdt_id, ItemJour_Code, Journaldt_id, ItemJour_Description, Currency_Code, Costcent_code, Project_Code, ItemJour_prcnt, ItemJour_amount, ItemJour_amount1, ItemJour_amount2, Ledger_Number, Ledger_NumberDebit, CostCent_CodeDebit, Project_codeDebit, Ledger_NumberCredit, CostCent_CodeCredit, Project_CodeCredit, Journal_Date, JournalDt_ValueDate, ItemJour_Remark, ItemJour_DateAmortized, ItemJour_PreviousDate, ItemJour_PreviousAmount, ItemJour_PreviousAmount1, ItemJour_PreviousAmount2, ItemJour_Sold, ItemJour_SaleDate, Ledger_NumberCash, CostCent_CodeCash, Project_CodeCash, ItemJour_CashAmount, ItemJour_CashAmount1, ItemJour_CashAmount2, Ledger_NumberProfit, Ledger_NumberLost, ItemJour_BelongsToGroup, Item_OriginalCode, Synchronized, itemjour_location, itemjour_barcode, logged_code)
						select assetsdt_id, ItemJour_Code, Journaldt_id, ItemJour_Description, Currency_Code, Costcent_code, Project_Code, ItemJour_prcnt, ItemJour_amount, ItemJour_amount1, ItemJour_amount2, Ledger_Number, Ledger_NumberDebit, CostCent_CodeDebit, Project_codeDebit, Ledger_NumberCredit, CostCent_CodeCredit, Project_CodeCredit, Journal_Date, JournalDt_ValueDate, ItemJour_Remark, ItemJour_DateAmortized, ItemJour_PreviousDate, ItemJour_PreviousAmount, ItemJour_PreviousAmount1, ItemJour_PreviousAmount2, ItemJour_Sold, ItemJour_SaleDate, Ledger_NumberCash, CostCent_CodeCash, Project_CodeCash, ItemJour_CashAmount, ItemJour_CashAmount1, ItemJour_CashAmount2, Ledger_NumberProfit, Ledger_NumberLost, ItemJour_BelongsToGroup, Item_OriginalCode, Synchronized, itemjour_location, itemjour_barcode, 
							'" . $_SESSION['useraccount'] . "'	
						from assetsitemjour where assetsdt_id in (select assetsdt_id from assetsdt where assets_id = " . $SAssetsId . ") ";
			}
			if (!updateData($connection, $query, true)) {
				$ErrorExists = 1;
				$message  = mysql_error();
			}


			$query = " INSERT INTO temp_assetscstjour(Journaldt_id, Costcent_code, Project_Code, Cstjour_prcnt, Cstjour_amount, Cstjour_amount1, Cstjour_amount2, Costcent_Precode, CstJour_Posted, Synchronized, Journal_JVRef, Variant_Code, user_account,  assetsdt_id,logged_code)
						select Journaldt_id, Costcent_code, Project_Code, Cstjour_prcnt, Cstjour_amount, Cstjour_amount1, Cstjour_amount2, Costcent_Precode, CstJour_Posted, Synchronized, Journal_JVRef, Variant_Code, user_account,  assetsdt_id
							,'" . $_SESSION['useraccount'] . "'	
						from assetscstjour where assetsdt_id in (select assetsdt_id from assetsdt where assets_id = " . $SAssetsId . ") ";

			if (!updateData($connection, $query, true)) {
				$ErrorExists = 1;
				$message  = mysql_error();
			}
		}
	}



	header("Location: ./Form_AmortizationSearch.php?PMode=1&message=" . $message);
} else if ($mainaction == "delete") {


	$SAssetsId = $_GET["SAssetsId"];
	$ErrorExit = 0;

	$query = "select Journal_id from assets where assets_id = $SAssetsId ";
	$res = @mysql_query($query, $connection) or die(mysql_error());
	$row = mysql_fetch_row($res);
	$Journal_id = $row[0];

	$query = " delete from assetscstjour where assetsdt_id   in (select assetsdt_id from assetsdt where assets_id = $SAssetsId)";
	$result1 = @mysql_query($query, $connection) or die(mysql_error());
	$message1  = mysql_error();
	if ($message1 != '')
		$ErrorExit = 1;

	if ($ErrorExit == 0) {
		$query = " delete from assetsitemjour where assetsdt_id   in (select assetsdt_id from assetsdt where assets_id = $SAssetsId)";
		$result1 = @mysql_query($query, $connection) or die(mysql_error());
		$message2  = mysql_error();
		if ($message2 != '')
			$ErrorExit = 1;
	}
	if ($ErrorExit == 0) {
		$query = " delete from assetsdt where assets_id = $SAssetsId";
		$result1 = @mysql_query($query, $connection) or die(mysql_error());
		$message3  = mysql_error();
		if ($message3 != '')
			$ErrorExit = 1;
	}

	if ($ErrorExit == 0) {
		$query = " delete from assets where assets_id = $SAssetsId";
		$result1 = @mysql_query($query, $connection) or die(mysql_error());
		$message4  = mysql_error();
		if ($message4 != '')
			$ErrorExit = 1;
	}
	if ($ErrorExit == 0) {
		$querydel = "delete from journaldt where journal_id = $Journal_id;";
		$result1 = @mysql_query($querydel, $connection) or die(mysql_error());
		$querydel = "delete from journal where journal_id = $Journal_id;";
		$result1 = @mysql_query($querydel, $connection) or die(mysql_error());
	}
	if ($ErrorExit == 1) {
		$message = "Error in Delete";
	}
	header("Location: ./Form_AmortizationSearch.php?message=$message");
} else if ($mainaction == "deletepending") {


	$SAssetsId = $_GET["SAssetsId"];

	$query = " delete from temp_assetscstjour where assetsdt_id   in (select assetsdt_id from temp_assetsdt where assets_id = $SAssetsId)";
	$result1 = @mysql_query($query, $connection) or die(mysql_error());
	$message1  = mysql_error();

	$query = " delete from temp_assetsitemjour where assetsdt_id   in (select assetsdt_id from temp_assetsdt where assets_id = $SAssetsId)";
	$result1 = @mysql_query($query, $connection) or die(mysql_error());
	$message2  = mysql_error();

	$query = " delete from temp_assetsdt where assets_id = $SAssetsId";
	$result1 = @mysql_query($query, $connection) or die(mysql_error());
	$message3  = mysql_error();


	$query = " delete from temp_assets where assets_id = $SAssetsId";
	$result1 = @mysql_query($query, $connection) or die(mysql_error());
	$message4  = mysql_error();


	header("Location: ./Form_AmortizationSearchPending.php");
} else if ($mainaction == "cancel") {

	echo '111';
	$SAssetsId = $_GET["SAssetsId"];
	if ($SAssetsId != '') {
		$query = " delete from temp_assetscstjour where assetsdt_id   in (select assetsdt_id from temp_assetsdt where assets_id = $SAssetsId)";
		$result1 = @mysql_query($query, $connection) or die(mysql_error());
		$message1  = mysql_error();

		$query = " delete from temp_assetsitemjour where assetsdt_id   in (select assetsdt_id from temp_assetsdt where assets_id = $SAssetsId)";
		$result1 = @mysql_query($query, $connection) or die(mysql_error());
		$message2  = mysql_error();

		$query = " delete from temp_assetsdt where assets_id = $SAssetsId";
		$result1 = @mysql_query($query, $connection) or die(mysql_error());
		$message3  = mysql_error();


		$query = " delete from temp_assets where assets_id = $SAssetsId";
		$result1 = @mysql_query($query, $connection) or die(mysql_error());
		$message4  = mysql_error();
	}

	header("Location: ./Form_Amortization.php");
} else if ($actionmode == "deleteassetsdt") {

	$assets_id = $_GET["assetsid"];
	$assetsdt_id = $_GET["assetsdtid"];

	$query = " delete from temp_assetscstjour   where assetsdt_id  =$assetsdt_id ";
	$result1 = @mysql_query($query, $connection) or die(mysql_error());
	$message1  = mysql_error();


	$query = "delete from temp_assetsitemjour  where assetsdt_id = $assetsdt_id ";

	$result2 = @mysql_query($query, $connection) or die(mysql_error());
	$message2  = mysql_error();

	if ($message1 == '' and $message2 == '') {
		$query = " delete from temp_assetsdt where assetsdt_id = $assetsdt_id"; //echo $query;
		$result = @mysql_query($query, $connection) or die(mysql_error());
		$CalculateResult = Calculate_MasterAmountsFixedAssets($assets_id, $connection);
	}
	echo "[BRK]";
	draw_assetsdt($assets_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, 'deleteassetsdt');
	echo "[BRK]";
	draw_fixedassetsamount($assets_id, $base1, $base2, $DecimalBase, $DecimalBase1, $DecimalBase2, $Setup_VatPercentage, $connection);
} else if ($actionmode == "checkpercentages") {
	$assets_id = $_GET["assets_id"];

	$query = " select 1 from (
				select sum(ifnull(cstjour_prcnt,0)) as sumprc
				from temp_assetscstjour,temp_assetsdt,temp_assets
				where temp_assets.assets_id = $assets_id
				and temp_assetscstjour.assetsdt_id = temp_assetsdt.assetsdt_id
				and temp_assetsdt.assets_id = temp_assets.assets_id
				group by temp_assetscstjour.assetsdt_id ) q1
				where q1.sumprc < 100 
		";
	$result = mysql_query($query, $connection) or die(mysql_error());
	$totalrows = mysql_num_rows($result);

	if ($totalrows > 0)
		echo "0";
	else
		echo "1";
} else if ($actionmode == "checkitemjourexists") {
	$assets_id = $_GET["assets_id"];
	$error_exists = 0;
	$query = " select assetsdt_id from temp_assetsdt where assets_id = $assets_id";
	$result = mysql_query($query, $connection) or die(mysql_error());
	while ($row = mysql_fetch_array($result)) {
		$query = " select count(*) from temp_assetsitemjour where assetsdt_id = " . $row[0];
		$result2 = mysql_query($query, $connection) or die(mysql_error());
		$row_count = mysql_fetch_array($result2);
		if ($row_count[0] == 0) $error_exists = 1;
	}

	if ($error_exists == 1)
		echo "0";
	else
		echo "1";
} else if ($actionmode == "deleteitemjour") {

	$autoincrement = $_GET["autoincrement"];
	$assetsdt_id = $_GET["assetsdtid"];

	$query = " delete from temp_assetsitemjour where autoincrement = $autoincrement";
	$result = @mysql_query($query, $connection) or die(mysql_error());

	draw_itemjour($assetsdt_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $defaultproject, $defaultcostcent);
} else if ($actionmode == "deleteitemjour2") {

	$autoincrement = $_GET["autoincrement"];

	$query = " delete from itemjour where autoincrement = $autoincrement";
	$result = @mysql_query($query, $connection) or die(mysql_error());

	draw_itemjour2($connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $StartFinancialDate, $EndFinancialDate, $DecimalBase, $DecimalBase1, $DecimalBase2, $DateTimePickerCloseYear);
} else if ($actionmode == "filteritemjour2") {

	$searchedledger = $_GET["searchedledger"];
	$DateTimePickerCloseYear = $_GET["DateTimePickerCloseYear"];

	draw_itemjour2($connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $StartFinancialDate, $EndFinancialDate, $DecimalBase, $DecimalBase1, $DecimalBase2, $DateTimePickerCloseYear, $searchedledger);
} else if ($actionmode == "generatebarcode") {

	$searchedledger = $_GET["searchedledger"];
	$DateTimePickerCloseYear = $_GET["DateTimePickerCloseYear"];




	fbegin();
	$resultvars = @mysql_query("set @brcode :=0 ", $connection) or die(mysql_error());

	$query = "select Autoincrement,ItemJour_Code from itemjour where (itemjour_barcode is null or itemjour_barcode='') order by itemjour_code";
	//echo $query;
	$result = @mysql_query($query, $connection)  or die(mysql_error());

	while ($row = mysql_fetch_row($result)) {

		$barcode = '';
		$Autoincrement = $row[0];
		$query = 'select if(max(itemjour_barcode) is null,"",max(itemjour_barcode)) from itemjour';
		$result1 = @mysql_query($query, $connection)  or die(mysql_error());
		$row1 = mysql_fetch_row($result1);
		$barcode = $row1[0];

		if (preg_match('/[A-Za-z]/', $barcode)) {
			draw_itemjour2($connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $StartFinancialDate, $EndFinancialDate, $DecimalBase, $DecimalBase1, $DecimalBase2, $DateTimePickerCloseYear, $searchedledger);
			exit;
		} else {
			if ($barcode == '') {
				$barcode = '0000000001';
				$Q = "update itemjour set  itemjour_barcode= '" . $barcode . "' where Autoincrement=" . $Autoincrement;
				$Qresult = @mysql_query($Q, $connection)  or die(mysql_error());
				$message  = mysql_error();
			} else {
				$barcode = $barcode + 1;
				$Q  = "update itemjour set  itemjour_barcode=NumericString('" . $barcode . "',10) where Autoincrement=" . $Autoincrement;
				$Qresult = @mysql_query($Q, $connection)  or die(mysql_error());
				$message  = mysql_error();
			}
			//echo $Q."<br>";	

		}
	}

	if ($result and $message == '') {
		fcommit();
	} else {
		$message  = mysql_error();
		frollback();
	}

	draw_itemjour2($connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $StartFinancialDate, $EndFinancialDate, $DecimalBase, $DecimalBase1, $DecimalBase2, $DateTimePickerCloseYear, $searchedledger);
} else if ($actionmode == "converttoprevious") {


	/*$query = " delete from itemjour where autoincrement = $autoincrement";
$result=@mysql_query($query,$connection) or die(mysql_error());*/


	$MainCompanyName = explode('_', $_SESSION['dbName']);
	$QueryDir = " Select Date_Format(StartFinancialDate,'%Y-%m-%d'),Date_Format(EndFinancialDate,'%Y-%m-%d') , Concat(CompanyName, '_', DirYear) As DirSchema " .
		" ,Date_Format(EndFinancialDate,'%d/%m/%Y') " .
		" From " . $_SESSION['dbLabel'] . ".Directories " .
		" Where  Upper(CompanyName) = Upper('" . $MainCompanyName[0] . "') " .
		" and Upper(Diryear) = Upper('" . $MainCompanyName[1] . "') " .
		" Order By DirYear ";

	$ResultDir = @mysql_query($QueryDir, $connection) or die(mysql_error());
	$RowsDir = mysql_fetch_row($ResultDir);
	$StartFinancialDate = $RowsDir[0];
	$EndFinancialDate = $RowsDir[1];
	$EndFinancialDate1 = $RowsDir[3];


	convert_amortized_amount_to_previousamount($connection, $StartFinancialDate, $DateTimePickerCloseYear, $DecimalBase, $DecimalBase1, $DecimalBase2, $base1, $base2);


	//draw_itemjour2($connection,$orderby,$startrange,$perpage,$option,$direction,$base1,$base2,$StartFinancialDate,$EndFinancialDate,$DecimalBase,$DecimalBase1,$DecimalBase2,$DateTimePickerCloseYear);

} else if ($actionmode == "deletecstjour") {

	$cstjour_id = $_GET["cstjour_id"];
	$assetsdt_id = $_GET["assetsdtid"];

	$query = " delete from temp_assetscstjour where Cst_increment = $cstjour_id";
	$result = @mysql_query($query, $connection) or die(mysql_error());

	draw_FixedAssetscstjour($assetsdt_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2);
} else if ($actionmode == 'checkitemjour') {
	$itemjour = $_GET["itemcode"];

	if ($itemjour != '') {
		$query = "select concat(left(item,length(item)-length(right(item,4))),lpad(right(item,4)+1,4,'0')) from (
			select max(item) as item from (
			select max(TRIM(LEADING '" . $itemjour . "' FROM itemjour_code)) as item
			from assetsitemjour where itemjour_code like '" . $itemjour . "%'
			and length(itemjour_code) = length(concat('" . $itemjour . "','0000'))
			union
			select max(TRIM(LEADING '" . $itemjour . "' FROM itemjour_code)) as item
			from temp_assetsitemjour where itemjour_code like '" . $itemjour . "%'
			and length(itemjour_code) = length(concat('" . $itemjour . "','0000')) ) q2
			) p1 ";
		//where p1.item REGEXP '^-?[0-9]+$' ";

		//echo $query;
		$result = @mysql_query($query, $connection) or die(mysql_error());
		$rows = mysql_fetch_array($result);

		if ($rows[0] == '') {
			//echo $itemjour.'0001';
			echo $itemjour;
		} else
			echo $itemjour . $rows[0];
	}
} else if ($actionmode == 'changeitemjouramount') {
	$Curr = $_GET["vcur"];
	$Date = $_GET["vdate"];
	$amnt = $_GET["vobj"];


	$amountbase1 = ConvertCurrency($Curr, $base1, insertMySQLDate($Date), $amnt, $connection);
	$amountbase2 = ConvertCurrency($Curr, $base2, insertMySQLDate($Date), $amnt, $connection);

	echo $amountbase1 . "[BRK]" . $amountbase2;
} else if ($actionmode == 'changeassetsdtamount') {
	$Curr = $_GET["vcur"];
	$Date = $_GET["vdate"];
	$vchoice = $_GET["vchoice"];
	$amnt = $_GET["vobj"];
	$amountbase1 = $_GET["vobj1"];
	$amountbase2 = $_GET["vobj2"];
	$lastcurr = $_GET["lastcurr"];

	if ($vchoice == 'amount') {
		$amountbase1 = ConvertCurrency($Curr, $base1, insertMySQLDate($Date), $amnt, $connection);
		$amountbase2 = ConvertCurrency($Curr, $base2, insertMySQLDate($Date), $amnt, $connection);
	} else if ($vchoice == 'amount1' and $Curr == $base1) {
		$amnt = ConvertCurrency($Curr, $base1, insertMySQLDate($Date), $amountbase1, $connection);
	} else if ($vchoice == 'amount2' and $Curr == $base2) {
		$amnt = $amountbase2;
	} else if ($vchoice == 'currency') {
		$amountbase1 = ConvertCurrency($Curr, $base1, insertMySQLDate($Date), $amnt, $connection);
		$amountbase2 = ConvertCurrency($Curr, $base2, insertMySQLDate($Date), $amnt, $connection);
	}
	/*

else if ($vchoice=='currency'){
$amnt = ConvertCurrency($lastcurr, $Curr, insertMySQLDate($Date), $amnt, $connection);

}

*/
	/*
if ($vchoice=='amount'){
	$amountbase1 = ConvertCurrency($Curr, $base1, insertMySQLDate($Date), $amnt, $connection);
	$amountbase2 = ConvertCurrency($Curr, $base2, insertMySQLDate($Date), $amnt, $connection);
	} 
else if ($vchoice=='amount1'){
	$amnt = ConvertCurrency($Curr, $base1, insertMySQLDate($Date), $amountbase1, $connection);
	//$amountbase2 = ConvertCurrency($Curr, $base2, insertMySQLDate($Date), $amountbase1, $connection);
}
*/


	echo $amnt . "[BRK]" . $amountbase1 . "[BRK]" . $amountbase2;
} else if ($actionmode == 'changeassetjouramount') {
	$Curr = $_GET["vcur"];
	$Date = $_GET["vdate"];
	$amnt = $_GET["vobj"];
	$vpercamt = $_GET["vpercobj"];
	$vchoice = $_GET["vchoice"];
	$vassetsdt_id = $_GET["vassetsdt_id"];

	$query = "select assetsdt_amount from temp_assetsdt where assetsdt_id = $vassetsdt_id";

	$result = @mysql_query($query, $connection) or die(mysql_error());
	$rows = mysql_fetch_array($result);
	$assetsdt_amount = $rows[0];


	if ($vchoice == 'percentage') {
		$amnt = ($assetsdt_amount * $vpercamt) / 100;
		$amountbase1 = ConvertCurrency($Curr, $base1, insertMySQLDate($Date), $amnt, $connection);
		$amountbase2 = ConvertCurrency($Curr, $base2, insertMySQLDate($Date), $amnt, $connection);
	} else	if ($vchoice == 'amount') {
		$vpercamt = ($amnt / $assetsdt_amount) * 100;
		$amountbase1 = ConvertCurrency($Curr, $base1, insertMySQLDate($Date), $amnt, $connection);
		$amountbase2 = ConvertCurrency($Curr, $base2, insertMySQLDate($Date), $amnt, $connection);
	}

	echo $vpercamt . "[BRK]" . $amnt . "[BRK]" . $amountbase1 . "[BRK]" . $amountbase2;
} else if ($actionmode == 'showrelationtoitemjour') {
	$assetsdt_id = $_GET["assetsdt_id"];

	draw_itemjour($assetsdt_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $defaultproject, $defaultcostcent);
	echo "[BRK]";
	draw_FixedAssetscstjour($assetsdt_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2);
} else if ($actionmode == 'showrelationtocstjour') {
	$journaldt_id = $_GET["journaldt_id"];

	draw_cstjour($journaldt_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2);
} else if ($actionmode == 'showrelationtocstjour_salejour') {
	$journaldt_id = $_GET["journaldt_id"];

	draw_cstjour_salejour($journaldt_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2);
} else if ($actionmode == 'showrelationtosalejourcstjour') {
	$journaldt_id = $_GET["journaldt_id"];

	draw_salejourcstjour($journaldt_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2);
} else if ($actionmode == 'generation') {

	$amortamort_fromdate = $_GET["amortamort_fromdate"];
	$amortamort_todate = $_GET["amortamort_todate"];

	$query = "select date_format(Journaldt_ValueDate,'%Y-%m-%d'),ItemJour_Prcnt ,Autoincrement 
			,ItemJour_Amount
			,ItemJour_Amount1
			,ItemJour_Amount2
			from ItemJour where ifnull(ItemJour_Sold,'f') <> 'S'";
	$result = @mysql_query($query, $connection)  or die(mysql_error());


	$i = 0;
	$amortamort_fromdate1 = $amortamort_fromdate;
	$amortamort_todate1 = $amortamort_todate;
	while ($row = mysql_fetch_row($result)) {

		$amortamort_fromdate = $amortamort_fromdate1;
		$amortamort_todate = $amortamort_todate1;

		$itemjour2journaldt_valuedate[$i] = round(strtotime($row[0]) / 86400, 0);
		$amortamort_fromdate = round(strtotime(changedateformat($amortamort_fromdate)) / 86400, 0);
		$amortamort_todate = round(strtotime(changedateformat($amortamort_todate)) / 86400, 0);
		$ItemJour2ItemJour_Prcnt[$i] = $row[1];
		$Autoincrement[$i] = $row[2];
		$ItemJour2ItemJour_Amount[$i] = $row[3];
		$ItemJour2ItemJour_Amount1[$i] = $row[4];
		$ItemJour2ItemJour_Amount1[$i] = $row[5];

		$ValidDay[$i] = $itemjour2journaldt_valuedate[$i] + ((100 / $ItemJour2ItemJour_Prcnt[$i]) * 365) - $amortamort_fromdate;
		$xx = $amortamort_todate - $amortamort_fromdate;

		if ($xx < $ValidDay[$i])
			$ToDate = $amortamort_fromdate;
		else
			$ToDate = $itemjour2journaldt_valuedate[$i] + ((100 / $ItemJour2ItemJour_Prcnt[$i]) * 365);


		$AmountToDateDays = $ToDate - $itemjour2journaldt_valuedate[$i];

		$AmountToDate = (($ItemJour2ItemJour_Amount[$i] * $ItemJour2ItemJour_Prcnt[$i]) / 100) / 365 * ($AmountToDateDays);
		$AmountToDate1 = (($ItemJour2ItemJour_Amount1[$i] * $ItemJour2ItemJour_Prcnt[$i]) / 100) / 365 * ($AmountToDateDays);
		$AmountToDate2 = (($ItemJour2ItemJour_Amount1[$i] * $ItemJour2ItemJour_Prcnt[$i]) / 100) / 365 * ($AmountToDateDays);
		//echo $AmountToDateDays."/".$ItemJour2ItemJour_Amount[$i]."/".$ItemJour2ItemJour_Prcnt[$i]."/".$AmountToDateDays."<br>";



		$i += 1;
	}
} else if ($actionmode == 'checkaccountno') {

	$vaccountno = $_GET["vaccountno"];

	$query = " select ledger_name from ledger where ledger_number = '" . $vaccountno . "' and LEDGER_GRPDET=FALSE";

	$result = @mysql_query($query, $connection)  or die(mysql_error());
	$row = mysql_fetch_row($result);

	if ($row[0] != '')
		echo $row[0];
	else
		echo 0;
} else if ($actionmode == 'checkprojectcostcenter') {

	$val = $_GET["val"];
	$vtype = $_GET["vtype"];

	if ($vtype == 'project')
		$query = " select project_name from project where project_code = '" . $val . "' ";
	else
		$query = " select costcent_name from costcent where costcent_code = '" . $val . "' ";
	//echo $query;

	$result = @mysql_query($query, $connection)  or die(mysql_error());
	$row = mysql_fetch_row($result);

	if ($row[0] != '')
		echo $row[0];
	else
		echo 0;
} else if ($actionmode == "drawamortjournaldtgrid") {

	$journal_id = $_GET["journal_id"];


	draw_amortjournaldtgrid($journal_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $actionbefore);
} else if ($actionmode == "drawsalejourjournaldtgridgenerate") {

	$journal_id = $_GET["journal_id"];


	draw_salejourjournaldtgridgenerate($journal_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $actionbefore);
} else if ($actionmode == "drawsalejourjournaldtgrid") {

	$journal_id = $_GET["journal_id"];


	draw_salejourjournaldtgrid($journal_id, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $actionbefore);
} else if ($actionmode == "generateamortization") {

	$amortamort_fromdate	= $_GET["amortamort_fromdate"];
	$amortamort_todate		= $_GET["amortamort_todate"];
	$amortamort_description = $_GET["amortamort_description"];

	echo "[BRK]";
	fbegin();
	$resultvars = mysql_query("set @id :=0 ", $connection);

	$query = " Call Create_ItemJourAutoJv (" . GetSQLValueString(insertMySQLDate1($amortamort_fromdate), 'date') . "," . GetSQLValueString(insertMySQLDate1($amortamort_todate), 'date') . ",'" . $base1 . "'
				,1, '" . $_SESSION['dbName'] . "','" . $_SESSION['dbLabel'] . "','" . $_SESSION['useraccount'] . "','" . $username . "'
				,'" . $base2 . "',0, '" . $amortamort_description . "',@jvid ) ";
	//echo $query; exit; 
	$result = mysql_query($query, $connection);
	//echo $query ;

	$query1 = " select @jvid from dual ";
	$result1 = mysql_query($query1, $connection) or die(mysql_error());
	$cur1 = mysql_fetch_row($result1);
	$jvid = $cur1[0];
	if ($jvid != '') {

		$query = " insert  into amortization(Amort_FromDate,Amort_ToDate,Amort_Description,Journal_Id) 
					values (" . GetSQLValueString(insertMySQLDate1($amortamort_fromdate), 'date') . "," . GetSQLValueString(insertMySQLDate1($amortamort_todate), 'date') . ",
					" . GetSQLValueString($amortamort_description, 'text') . ",$jvid)";
		$result = mysql_query($query, $connection);
		$error = mysql_error();

		$id = mysql_insert_id($result);

		$query = " select Journal_refgovernment,Journal_TypeRef,Journal_folio from journal where journal_id = " . $jvid;
		$result = mysql_query($query, $connection);
		$jour = mysql_fetch_row($result);
		echo $jour[0] . "[BRK]" . $jour[1] . "[BRK]" . $jour[2];
	}
	echo "[BRK]";

	draw_amortjournaldtgrid($jvid, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $actionbefore);

	fcommit();
} else if ($actionmode == "generatesalejour") {

	$salejoursalejour_date = $_GET["salejoursalejour_date"];
	$salejoursalejour_description = $_GET["salejoursalejour_description"];

	echo "[BRK]";
	fbegin();
	$resultvars = @mysql_query("set @id :=0 ", $connection) or die(mysql_error());

	$query = " Call Create_SaleJourAutoJv (" . GetSQLValueString(insertMySQLDate1($salejoursalejour_date), 'date') . "
															,'" . $base1 . "'
                                                             ,1, '" . $_SESSION['dbName'] . "'
                                                             ,'" . $_SESSION['dbLabel'] . "'
                                                             ,'" . $_SESSION['useraccount'] . "','" . $username . "'
                                                             ,'" . $base2 . "'
                                                             ,0
                                                             , '" . $salejoursalejour_description . "'
                                                             ,@jvid ) ";
	$result = @mysql_query($query, $connection) or die(mysql_error());
	//echo $query ;

	$query1 = " select @jvid from dual ";
	$result1 = @mysql_query($query1, $connection) or die(mysql_error());
	$cur1 = mysql_fetch_row($result1);
	$jvid = $cur1[0];


	if ($jvid != '') {


		fbegin();
		$SaleJour_Id = get_nextsequence('SaleJour_Id', '', $connection);
		fcommit();

		fbegin();
		$SaleJour_Reference = get_nextsequence('SaleJour_Reference', '', $connection);
		fcommit();


		$query = " insert  into salejour(SaleJour_Id,SaleJour_Reference,SaleJour_Date,SaleJour_Description,Journal_Id) 
				values ('" . $SaleJour_Id . "''" . $SaleJour_Reference . "','" . $assetjourassetjour_fromdate . "', '" . $salejoursalejour_description . "',$jvid)";
		$result = @mysql_query($query, $connection) or die(mysql_error());
		$error = mysql_error();

		$id = mysql_insert_id($result);

		$query = " select Journal_TypeRef,Journal_Folio from journal where journal_id = " . $jvid;
		$result = @mysql_query($query, $connection) or die(mysql_error());
		$jour = mysql_fetch_row($result);
		echo $SaleJour_Reference . "[BRK]" . $jour[0] . "[BRK]" . $jour[1];
	}
	echo "[BRK]";

	draw_amortjournaldtgrid($jvid, $connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $actionbefore);

	fcommit();
}


function PostHeader($assets_date, $journal_valuedate, $assets_amount, $assets_amount1, $assets_amount2, $accountno, $amortization_currency_code, $assets_name, $costcenter_code, $project_code, $assets_desc, $assets_vatpref, $useraccount, $connection)
{

	fbegin();
	$assets_id = get_nextsequence('assets_id', '', $connection);
	fcommit();

	fbegin();
	$assets_refgovernment = get_nextsequence('Assets_RefGovernment', '', $connection);
	fcommit();



	fbegin();

	$query = "insert into temp_assets(assets_id,assets_refgovernment,Assets_DATE,Journal_valuedate,Assets_AMOUNT,Assets_AMOUNT1,Assets_AMOUNT2
							,LEDGER_NUMBER,CURRENCY_CODE,Assets_Name,COSTCENTer_CODE,Project_code,Assets_DESC,logged_code, VATFixedAssetAccount)
				values ( 
					" . GetSQLValueString($assets_id, 'double') . "
					," . GetSQLValueString($assets_refgovernment, 'text') . "
					," . GetSQLValueString(insertMySQLDate($assets_date), 'date') . "
					," . GetSQLValueString(insertMySQLDate($journal_valuedate), 'date') . "
					," . GetSQLValueString($assets_amount, 'double') . "
					," . GetSQLValueString($assets_amount1, 'double') . "
					," . GetSQLValueString($assets_amount2, 'double') . "
					," . GetSQLValueString($accountno, 'text') . "
					," . GetSQLValueString($amortization_currency_code, 'text') . "
					," . GetSQLValueString($assets_name, 'text') . "
					," . GetSQLValueString($costcenter_code, 'text') . "
					," . GetSQLValueString($project_code, 'text') . "
					," . GetSQLValueString($assets_desc, 'text') . "
					," . GetSQLValueString($useraccount, 'text') . " 
					," . GetSQLValueString($assets_vatpref, 'text') . "
		)";
	//echo $query;
	//echo "[BRK]";exit;
	$result = @mysql_query($query, $connection) or die(mysql_error());

	if ($result) {
		fcommit();
	} else {
		$message  = mysql_error();
		frollback();
	}

	return $assets_id;
}

function convert_amortized_amount_to_previousamount($connection, $StartFinancialDate, $DateTimePickerCloseYear, $DecimalBase, $DecimalBase1, $DecimalBase2, $base1, $base2)
{

	$DateTimePickerCloseYear1 = $DateTimePickerCloseYear;
	$DateTimePickerCloseYear = strtotime(($DateTimePickerCloseYear));

	#Autoincrement, ItemJour_Code, Journaldt_id, ItemJour_Description, Currency_Code, Costcent_code, Project_Code, ItemJour_prcnt, ItemJour_amount, ItemJour_amount1, ItemJour_amount2, Ledger_Number, Ledger_NumberDebit, CostCent_CodeDebit, Project_codeDebit, Ledger_NumberCredit, CostCent_CodeCredit, Project_CodeCredit, Journal_Date, JournalDt_ValueDate, ItemJour_Remark, ItemJour_DateAmortized, ItemJour_PreviousDate, ItemJour_PreviousAmount, ItemJour_PreviousAmount1, ItemJour_PreviousAmount2, ItemJour_Sold, ItemJour_SaleDate, Ledger_NumberCash, CostCent_CodeCash, Project_CodeCash, ItemJour_CashAmount, ItemJour_CashAmount1, ItemJour_CashAmount2, Ledger_NumberProfit, Ledger_NumberLost, ItemJour_BelongsToGroup, Item_OriginalCode, Synchronized, itemjour_location, itemjour_barcode, assetsdt_id
	$query = "select itemjour.Ledger_Number,ledger.ledger_name,journal.journal_folio,journal.Journal_refgovernment,itemjour.ItemJour_prcnt
				,ItemJour_Code,itemjour_barcode,ItemJour_Description,date_format(itemjour.JournalDt_ValueDate,'%d/%m/%Y')
				,itemjour_location,itemjour.Currency_Code,ItemJour_amount,ItemJour_amount1,ItemJour_amount2,date_format(ItemJour_PreviousDate,'%d/%m/%Y'),ItemJour_PreviousAmount,ItemJour_PreviousAmount1
				,ItemJour_PreviousAmount2,date_format(itemjour.JournalDt_ValueDate,'%m/%d/%Y')
				,date_format(ItemJour_DateAmortized,'%Y-%m-%d')
				,date_format(ItemJour_DateAmortized,'%d/%m/%Y'),itemjour.costcent_code,costcent.costcent_name
				,itemjour.project_code,project.project_name
				,Ledger_Debit.Ledger_Name,Ledger_Credit.Ledger_Name
				,costcent_credit.costcent_name,project_credit.project_name
				,itemjour.Ledger_NumberDebit
				,itemjour.Ledger_NumberCredit
				,itemjour.costcent_codecredit
				,itemjour.project_codecredit
				,date_format(itemjour.ItemJour_SaleDate,'%d/%m/%Y')
				,Ledger_NumberCash
				,CostCent_CodeCash
				,Project_CodeCash
				,ItemJour_CashAmount
				,ItemJour_CashAmount1
				,ItemJour_CashAmount2
				,Ledger_NumberProfit
				,Ledger_Profit.Ledger_Name
				,Ledger_NumberLost
				,Ledger_Lost.Ledger_Name
				,project_credit.project_name
				,itemjour.ItemJour_Sold
				,Ledger_Cash.Ledger_Name
				,itemjour.itemjour_remark
				,itemjour.autoincrement
				 from itemjour left join project on project.project_code=itemjour.project_code
				left join costcent on costcent.costcent_code=itemjour.costcent_code             
				left join costcent as costcent_credit on costcent_credit.costcent_code=itemjour.costcent_codecredit             
				left join project as project_credit on project_credit.project_code=itemjour.project_codecredit             
				left join ledger on ledger.ledger_number=itemjour.Ledger_Number
				left join ledger as Ledger_Debit on Ledger_Debit.ledger_number=itemjour.Ledger_NumberCredit
				left join ledger as Ledger_Credit on Ledger_Credit.ledger_number=itemjour.Ledger_NumberDebit
				left join ledger as Ledger_Profit on Ledger_Profit.ledger_number=itemjour.Ledger_NumberProfit
				left join ledger as Ledger_Lost on Ledger_Lost.ledger_number=itemjour.Ledger_NumberLost
				left join ledger as Ledger_Cash on Ledger_Cash.ledger_number=itemjour.Ledger_NumberCash
				left join journaldt on journaldt.journaldt_id=itemjour.journaldt_id
				left join journal on journal.journal_id=journaldt.journal_id
				order by itemjour.Ledger_Number,itemjour.Currency_Code,itemjour.JournalDt_ValueDate,itemjour.ItemJour_Description ";


	$result = @mysql_query($query, $connection)  or die(mysql_error());


	$i = 0;

	while ($row = mysql_fetch_row($result)) {

		$convert = 1;

		$itemjour2ledger_number[$i] = $row[0];
		$itemjour2ledger_name[$i] = $row[1];
		$itemjour2journal_folio[$i] = $row[2];
		$itemjour2journal_refgovernment[$i] = $row[3];
		$itemjour2itemjour_prcnt[$i] = $row[4];
		$itemjour2itemjour_code[$i] = $row[5];
		$itemjour2itemjour_barcode[$i] = $row[6];
		$itemjour2itemjour_description[$i] = $row[7];
		$itemjour2journaldt_valuedate[$i] = $row[8];

		$itemjour2itemjour_location[$i] = $row[9];
		$itemjour2currency_code[$i] = $row[10];
		$itemjour2itemjour_amount[$i] = $row[11];
		$itemjour2itemjour_amount1[$i] = $row[12];
		$itemjour2itemjour_amount2[$i] = $row[13];
		$itemjour2itemjour_previousdate[$i] = $row[14];
		$itemjour2itemjour_previousamount[$i] = $row[15];
		$itemjour2itemjour_previousamount1[$i] = $row[16];
		$itemjour2itemjour_previousamount2[$i] = $row[17];
		$itemjour_dateamortized[$i] = $row[19];
		$itemjour2itemjour_dateamortized[$i] = $row[20];

		$costcent_code[$i] = $row[21];
		$costcent_name[$i] = $row[22];

		$project_code[$i] = $row[23];
		$project_name[$i] = $row[24];

		$Ledger_NameDebit[$i] = $row[25];
		$Ledger_NameCredit[$i] = $row[26];
		$costcent_namecredit[$i] = $row[27];
		$project_namecredit[$i] = $row[28];


		$Ledger_NumberDebit[$i] = $row[29];
		$Ledger_NumberCredit[$i] = $row[30];
		$costcent_codecredit[$i] = $row[31];
		$project_codecredit[$i] = $row[32];

		$ItemJour_SaleDate[$i] = $row[33];
		$Ledger_NumberCash[$i] = $row[34];
		$CostCent_CodeCash[$i] = $row[35];
		$Project_CodeCash[$i] = $row[36];
		$ItemJour_CashAmount[$i] = $row[37];
		$ItemJour_CashAmount1[$i] = $row[38];
		$ItemJour_CashAmount2[$i] = $row[39];
		$Ledger_NumberProfit[$i] = $row[40];
		$Ledger_NameProfit[$i] = $row[41];
		$Ledger_NumberLost[$i] = $row[42];
		$Ledger_NameLost[$i] = $row[43];

		$ItemJour_Sold[$i] = $row[45];
		$Ledger_NameCash[$i] = $row[46];
		$itemjour_remark[$i] = $row[47];

		$autoincrement[$i] = $row[48];


		$ValidDay[$i] = 0;
		$valuedate[$i] = strtotime(changedateformat($itemjour2journaldt_valuedate[$i]));





		$dateamortized[$i] = strtotime($itemjour_dateamortized[$i]);
		$previousdate[$i] = strtotime($itemjour2itemjour_previousdate[$i]);


		if ($itemjour2itemjour_prcnt[$i] <> 0)
			$ValidDay[$i] = (($valuedate[$i] / 86400) - ($dateamortized[$i] / 86400)) + ((100 / $itemjour2itemjour_prcnt[$i]) * 365);



		if ($ValidDay[$i] > 0) {

			$Days[$i] = round(($dateamortized[$i] - $valuedate[$i]) / 86400, 0);
			$Days[$i] = round($Days[$i], 0);

			if ($Days[$i] > 0) {
				//echo $itemjour2itemjour_amount[$i].":".$itemjour2itemjour_prcnt[$i].":".$Days[$i]."<br>";
				$itemjour2amountamortized[$i] = (($itemjour2itemjour_amount[$i] * $itemjour2itemjour_prcnt[$i]) / 100) / 365 * ($Days[$i]);
				$itemjour2amountamortized1[$i] = (($itemjour2itemjour_amount1[$i] * $itemjour2itemjour_prcnt[$i]) / 100) / 365 * ($Days[$i]);
				$itemjour2amountamortized2[$i] = (($itemjour2itemjour_amount2[$i] * $itemjour2itemjour_prcnt[$i]) / 100) / 365 * ($Days[$i]);
			} else {
				$itemjour2amountamortized[$i] = 0;
				$itemjour2amountamortized1[$i] = 0;
				$itemjour2amountamortized2[$i] = 0;
			}
		} else {
			$itemjour2amountamortized[$i]  =  $itemjour2itemjour_amount[$i];
			$itemjour2amountamortized1[$i] = $itemjour2itemjour_amount1[$i];
			$itemjour2amountamortized2[$i] = $itemjour2itemjour_amount2[$i];
		}


		if (($itemjour2itemjour_previousdate[$i] <> '') and ($previousdate[$i] <= $dateamortized[$i])) {
			if ($itemjour2itemjour_previousamount[$i] <= $itemjour2amountamortized[$i]) {
				$ItemJour2AmountRemain[$i] = $itemjour2itemjour_amount[$i] - $itemjour2amountamortized[$i];
				$ItemJour2AmountRemain1[$i] = $itemjour2itemjour_amount1[$i] - $itemjour2amountamortized1[$i];
				$ItemJour2AmountRemain2[$i] = $itemjour2itemjour_amount2[$i] - $itemjour2amountamortized2[$i];
			} else {
				$ItemJour2AmountRemain[$i] = $itemjour2itemjour_amount[$i] - $itemjour2itemjour_previousamount[$i];
				$ItemJour2AmountRemain1[$i] = $itemjour2itemjour_amount1[$i] - $itemjour2itemjour_previousamount1[$i];
				$ItemJour2AmountRemain2[$i] = $itemjour2itemjour_amount2[$i] - $itemjour2itemjour_previousamount2[$i];
			}
		} else {
			$ItemJour2AmountRemain[$i] = $itemjour2itemjour_amount[$i] - $itemjour2amountamortized[$i];
			$ItemJour2AmountRemain1[$i] = $itemjour2itemjour_amount1[$i] - $itemjour2amountamortized1[$i];
			$ItemJour2AmountRemain2[$i] = $itemjour2itemjour_amount2[$i] - $itemjour2amountamortized2[$i];
		}


		if ($itemjour2amountamortized[$i] > $itemjour2itemjour_previousamount[$i]) {
			$ItemJour2Amortn[$i] = $itemjour2amountamortized[$i] - $itemjour2itemjour_previousamount[$i];
		} else {
			$ItemJour2Amortn[$i] = 0;
			$itemjour2amountamortized[$i] = $itemjour2itemjour_previousamount[$i];
		}
		if ($itemjour2amountamortized1[$i] > $itemjour2itemjour_previousamount1[$i])
			$ItemJour2AmortnBase1[$i] = $itemjour2amountamortized1[$i] - $itemjour2itemjour_previousamount1[$i];
		else {
			$ItemJour2AmortnBase1[$i] = 0;
			$itemjour2amountamortized1[$i] = $itemjour2itemjour_previousamount1[$i];
		}
		if ($itemjour2amountamortized2[$i] > $itemjour2itemjour_previousamount2[$i])
			$ItemJour2AmortnBase2[$i] = $itemjour2amountamortized2[$i] - $itemjour2itemjour_previousamount2[$i];
		else {
			$ItemJour2AmortnBase2[$i] = 0;
			$itemjour2amountamortized2[$i] = $itemjour2itemjour_previousamount2[$i];
		}

		$itemjour2amountamortized[$i] = Round($itemjour2amountamortized[$i] * 100) / 100;
		$itemjour2amountamortized1[$i] = Round($itemjour2amountamortized1[$i] * 100) / 100;
		$itemjour2amountamortized2[$i] = Round($itemjour2amountamortized2[$i] * 100) / 100;
		$ItemJour2AmountRemain[$i] = Round($ItemJour2AmountRemain[$i] * 100) / 100;
		$ItemJour2AmountRemain1[$i] = Round($ItemJour2AmountRemain1[$i] * 100) / 100;
		$ItemJour2AmountRemain2[$i] = Round($ItemJour2AmountRemain2[$i] * 100) / 100;




		$ValidDay[$i] = 0;
		if ($itemjour2itemjour_prcnt[$i] <> 0)
			if ($convert == 1)
				$ValidDay[$i] = ($valuedate[$i] / 86400) + ((100 / $itemjour2itemjour_prcnt[$i]) * 365) - ($DateTimePickerCloseYear / 86400);
			else
				$ValidDay[$i] = ($valuedate[$i] / 86400) + ((100 / $itemjour2itemjour_prcnt[$i]) * 365) -  ($dateamortized[$i] / 86400);



		if ($ValidDay[$i] > 0) {
			if ($convert == 1)
				$Days[$i] = round(($DateTimePickerCloseYear / 86400) - ($valuedate[$i] / 86400), 0);
			else
				$Days[$i] = round(($dateamortized[$i] / 86400) - ($valuedate[$i] / 86400), 0);
			$Days[$i] =  round($Days[$i], 0);
			//echo $autoincrement[$i]."--------".$itemjour2itemjour_amount[$i]." ".$itemjour2itemjour_prcnt[$i]." ".$Days[$i]."<br>";



			if ($Days[$i] > 0) {
				$itemjour2amountamort[$i] = (($itemjour2itemjour_amount[$i]   * $itemjour2itemjour_prcnt[$i]) / 100) / 365 * ($Days[$i]);
				$itemjour2amountamort1[$i] = (($itemjour2itemjour_amount1[$i] * $itemjour2itemjour_prcnt[$i]) / 100) / 365 * ($Days[$i]);
				$itemjour2amountamort2[$i] = (($itemjour2itemjour_amount2[$i] * $itemjour2itemjour_prcnt[$i]) / 100) / 365 * ($Days[$i]);
			} else {
				$itemjour2amountamort[$i] = 0;
				$itemjour2amountamort1[$i] = 0;
				$itemjour2amountamort2[$i] = 0;
			}
		} else {
			$itemjour2amountamort[$i] = $itemjour2itemjour_amount[$i];
			$itemjour2amountamort1[$i] = $itemjour2itemjour_amount1[$i];
			$itemjour2amountamort2[$i] = $itemjour2itemjour_amount2[$i];
		}


		if ($itemjour2amountamort[$i] > $itemjour2itemjour_previousamount[$i])
			$ItemJour2Amortn2[$i] = round(($itemjour2amountamort[$i] - $itemjour2itemjour_previousamount[$i]) * 100) / 100;
		else {
			$ItemJour2Amortn2[$i] = 0;
			$itemjour2amountamort[$i] = $itemjour2itemjour_previousamount[$i];
		}



		if ($itemjour2amountamort1[$i] > $itemjour2itemjour_previousamount1[$i])
			$ItemJour2Amortn2Base1[$i] = round(($itemjour2amountamort1[$i] - $itemjour2itemjour_previousamount1[$i]) * 100) / 100;
		else {
			$ItemJour2Amortn2Base1[$i] = 0;
			$itemjour2amountamort1[$i] = $itemjour2itemjour_previousamount1[$i];
		}
		if ($itemjour2amountamort2[$i] > $itemjour2itemjour_previousamount2[$i])
			$ItemJour2Amortn2Base2[$i] = round(($itemjour2amountamort2[$i] - $itemjour2itemjour_previousamount2[$i]) * 100) / 100;
		else {
			$ItemJour2Amortn2Base2[$i] = 0;
			$itemjour2amountamort2[$i] = $itemjour2itemjour_previousamount2[$i];
		}
		$itemjour2amountamort[$i] = Round($itemjour2amountamortized[$i] * 100) / 100;
		$itemjour2amountamort1[$i] = Round($itemjour2amountamortized1[$i] * 100) / 100;
		$itemjour2amountamort2[$i] = Round($itemjour2amountamortized2[$i] * 100) / 100;


		$amtny = $itemjour2itemjour_amount[$i] * $itemjour2itemjour_prcnt[$i] / 100;
		$amtny1 = $itemjour2itemjour_amount1[$i] * $itemjour2itemjour_prcnt[$i] / 100;
		$amtny2 = $itemjour2itemjour_amount2[$i] * $itemjour2itemjour_prcnt[$i] / 100;
		$Days[$i] = 0;

		if ($valuedate[$i] > strtotime($StartFinancialDate)) {
			$Days[$i] = round(($valuedate[$i] / 86400 - strtotime($StartFinancialDate) / 86400), 0);
		}

		$amtny = $amtny - ($amtny / 365 * $Days[$i]);
		$amtny1 = $amtny1 - ($amtny1 / 365 * $Days[$i]);
		$amtny2 = $amtny2 - ($amtny2 / 365 * $Days[$i]);

		$amtny = round($amtny * 100) / 100;
		$amtny1 = round($amtny1 * 100) / 100;
		$amtny2 = round($amtny2 * 100) / 100;



		if ($amtny > $ItemJour2AmountRemain[$i])
			$ItemJour2Amortny[$i] = $ItemJour2AmountRemain[$i];
		else
			$ItemJour2Amortny[$i] = $amtny;

		if ($amtny1 > $ItemJour2AmountRemain1[$i])
			$itemjour2amortny1[$i] = $ItemJour2AmountRemain1[$i];
		else
			$itemjour2amortny1[$i] = $amtny1;

		if ($amtny2 > $ItemJour2AmountRemain2[$i])
			$itemjour2amortny2[$i] = $ItemJour2AmountRemain2[$i];
		else
			$itemjour2amortny2[$i] = $amtny2;

		if (strtotime($StartFinancialDate) > $valuedate[$i])
			$DaysB[$i] = (strtotime($StartFinancialDate) / 86400 - $valuedate[$i] / 86400);
		else
			$DaysB[$i] = 1;


		$DaysC[$i] = ($DateTimePickerCloseYear) / 86400 + 1 - $valuedate[$i] / 86400;
		$DaysD[$i] = 0;

		if ($itemjour2itemjour_prcnt[$i] <> 0)
			$DaysD[$i] = ($itemjour2itemjour_amount[$i] / (($itemjour2itemjour_amount[$i] * $itemjour2itemjour_prcnt[$i] / 100) / 365)) + 1;




		if ($DaysB[$i] > $DaysD[$i])
			$Days[$i] = 0;

		else if ($DaysC[$i] > $DaysD[$i])
			$Days[$i] = $DaysD[$i] - $DaysB[$i];
		else
			$Days[$i] = $DaysC[$i] - $DaysB[$i];

		//echo $Days[$i]."<br>";
		if ($Days[$i] > 0) {
			$itemjour2amortnymanual[$i]  = (($itemjour2itemjour_amount[$i] * $itemjour2itemjour_prcnt[$i]) / 100) / 365 * ($Days[$i]);
			$itemjour2amortny1manual[$i] = (($itemjour2itemjour_amount1[$i] * $itemjour2itemjour_prcnt[$i]) / 100) / 365 * ($Days[$i]);
			$itemjour2amortny2Manual[$i] = (($itemjour2itemjour_amount2[$i] * $itemjour2itemjour_prcnt[$i]) / 100) / 365 * ($Days[$i]);
		} else {
			$itemjour2amortnymanual[$i]  = '0';
			$itemjour2amortny1manual[$i] = '0';
			$itemjour2amortny2Manual[$i] = '0';
		}

		//echo $ItemJour2Amortn2[$i]."<br>";



		//// update itemjour table		
		if (($itemjour2journaldt_valuedate[$i] <> '') and ($itemjour2itemjour_prcnt[$i] <> 0)) {

			if (($itemjour2itemjour_previousamount[$i] + $ItemJour2Amortn2[$i]) < $itemjour2itemjour_amount[$i]) {
				$itemjour2itemjour_previousamount[$i] =  $itemjour2itemjour_previousamount[$i]  + $ItemJour2Amortn2[$i];
				$itemjour2itemjour_previousamount1[$i] = $itemjour2itemjour_previousamount1[$i] + $ItemJour2Amortn2Base1[$i];
				$itemjour2itemjour_previousamount2[$i] = $itemjour2itemjour_previousamount2[$i] + $ItemJour2Amortn2Base2[$i];
			} else {

				if ($ItemJour2Currency_Code[$i] = $base1)
					$itemjour2itemjour_previousamount[$i] = $itemjour2itemjour_amount[$i] - 1;
				else
					$itemjour2itemjour_previousamount[$i] = $itemjour2itemjour_amount[$i];

				$itemjour2itemjour_previousamount1[$i] = $itemjour2itemjour_amount1[$i] - 1;
				$itemjour2itemjour_previousamount2[$i] = $itemjour2itemjour_amount2[$i];
			}

			$q = "update itemjour set ItemJour_PreviousAmount      = $itemjour2itemjour_previousamount[$i]
										, ItemJour_PreviousAmount1 = $itemjour2itemjour_previousamount1[$i]
										, itemjour_previousamount2 = $itemjour2itemjour_previousamount2[$i]
										where autoincrement = $autoincrement[$i]";
			//echo $q."<br>";
			$result1q = @mysql_query($q, $connection) or die(mysql_error());
		}

		$i += 1;
	}
	draw_itemjour2($connection, $orderby, $startrange, $perpage, $option, $direction, $base1, $base2, $StartFinancialDate, $EndFinancialDate, $DecimalBase, $DecimalBase1, $DecimalBase2, $DateTimePickerCloseYear1);
}
