SET DELETED ON
SET DATE GERMAN
SET CENTURY on

USE d:\2020\BAZE\crta ALIAS crta exclusive IN 0
USE d:\2021\BAZE\crta ALIAS crtan exclusive IN 0
USE D:\2021\PROGS\pocetno ALIAS pocetno exclusive IN 0
SELECT pocetno
ZAP

SELECT crta
GOTO top
DO WHILE !EOF()
IF YEAR(crta.datum)<>2019
	SKIP
	LOOP
endif	
a1=crta.kupdob
a2=crta.duguje
a3=crta.potrazuje
SELECT pocetno
SET ORDER TO tag kupdob
GOTO top
SEEK a1
IF !FOUND()
	APPEND BLANK
	replace pocetno.brdok WITH "PS"
	replace pocetno.datum WITH CTOD("01.01.2021")
	replace pocetno.kupdob WITH a1
ENDIF
	replace pocetno.duguje WITH pocetno.duguje+a2
	replace pocetno.potrazuje WITH pocetno.potrazuje+a3
SELECT crta
SKIP
ENDDO

SELECT pocetno
GOTO top
DO WHILE !EOF()		
	a1=pocetno.duguje
	a2=pocetno.potrazuje
	c=a1-a2
	IF c>0
		replace pocetno.duguje WITH c
		replace pocetno.potrazuje WITH 0
	ENDIF
	IF c<0
		replace pocetno.potrazuje WITH ABS(c)
		replace pocetno.duguje WITH 0
	ENDIF
	IF c=0		
		DELETE
	ENDIF
SKIP
ENDDO

SELECT crtan
DELETE ALL FOR ALLTRIM(brdok)=="PS"
PACK
SELECT pocetno
GOTO top
DO WHILE !EOF()
SCATTER memvar
SELECT crtan
APPEND BLANK
GATHER memvar
SELECT pocetno
skip
enddo
RETURN
		