private a,b,net_profit,db_tot,cr_tot,mwidrawal,close_ac
private filt_con,sche_bar,cl_stock,liability_tot,asset_tot
private cl_stock,filt_con,net_profit,old_cl_stock

sele ledger
defi wind eoy_date from 0,0 to 5,40 pane 
acti wind eoy_date
move wind eoy_date cent
@ 0,2 say 'Balancesheet As On : ' get mdate defa min(sys_date,mend_dt) valid betw(mdate,mstart_dt - 1,mend_dt)
read
rele wind eoy_date
if mdate < mend_dt
	wait wind '!!! W A I T !!! Calculating Closing balances as on ' + dtoc(mdate) nowa
	sele transect
	set filt to date <= mdate
	do chkbal
endif		
do wait_msg
wait wind '!!! W A I T !!! Generating Report ...' nowa

ln = 0
filt_con  = ''
store 0 to liability_tot,asset_tot,cl_stock,net_profit
sele ledger

*** Replace all Balancesheet Order,Effecetive Parent,Effect A_l_Flag with existing values

repl all eff_ord with balanceord ;
	eff_parent with parent_no ;
	eff_al_flg with a_l_flg ;
	eff_bal with iif(a_l_flg = 'A',-1,1)*balance

*** Replace Balancesheet Order,Effecetive Parent,Effect A_l_Flag with ALTERNATE Values

*balance = 'balance' + one_two
loca for ((a_l_flg = 'A' and balance > 0) OR (a_l_flg = 'L' and balance < 0)) and ! group_flg and empt(alt_parent)
if found()
	do err_beep
	do alert with '!!! ATTENTION !!!;Some Accounts with Negative Balances Found...;For Such Account you should give ALTERNATE ACCOUNT HEAD;This time Such Accounts will be reflected with Negative Sign'
endif
scan for ((a_l_flg = 'A' and balance > 0) OR (a_l_flg = 'L' and balance < 0)) and ! group_flg
	if ! empt(alt_parent)
		if rloc()
			repl eff_ord with alt_ord ;
				eff_parent with alt_parent ;
				eff_al_flg with alt_al_flg ;
				eff_bal with iif(eff_al_flg = 'A',-1,1)*balance
			unlo 
		endif
	endif
ends		

*** Calculate Asset & Liability Total

do bal_tot

sele ledger

*** Replace Closing Stock if it is required

if mtrad_flg <> 'Y'
	loca for account_no = 't00000'
	if found()
		old_cl_stock = balance
		repl &balance with -cl_stock
	endif	
endif	

*** Do not consider Exp,Income,Pur,Sale,Op Stock,Net/Gross Profit,Net/Gross Loss
use (path_nam) + 'ledger' agai in 0 alia ledger_a
sele ledger_a
*set filt to (balance <> 0 and ! group_flg) and ! inli(account_no,'s','p','T','N','n','1','I','E')
if mtrad_flg = 'Y'
	set filt to (balance <> 0 and ! group_flg) and ! inli(account_no,'s','p','N','n','1','I','E','T','G','g')
else
	set filt to ( (balance <> 0 or tmp_clbal <> 0) and ! group_flg) and ! inli(account_no,'s','p','N','n','1','I','E','T','G','g')
endif

sele ledger_a
set orde to eff_ord

set orde to account_no in ledger
set rela to eff_parent into ledger

repo form nvertbal to (out_file) pdsetup noco

set filt to
if mtrad_flg <> 'Y'
	loca for account_no = 't00000'
	repl &balance with old_cl_stock
endif

use in ledger_a
sele ledger

wait wind '!!! W A I T !!! ReCalculating Closing balances ' nowa 
if mdate < mend_dt
	sele transect
	set filt to 
	do chkbal
endif		
wait clea

do out_repo
do cls_menu