CLOSE DATABASES
CLOSE TABLES


OPEN DATABASE d:\robno\baze\belcar EXCLUSIVE
SET DEFAULT TO d:\robno\stbaz
SET PATH TO d:\robno\baze

SET century on
SET DATE GERMAN
SET DELETED ON


USE zalihe ALIAS zalihe IN 0
USE roba ALIAS roba IN 0
USE tarife ALIAS tarife IN 0	

USE brojac ALIAS brojac IN 0	
select brojac
ZAP
APPEND BLANK
GOTO top

USE zagfak	ALIAS zagfak IN 0
SELECT zagfak
ZAP

USE fakstav	ALIAS fakstav IN 0
SELECT fakstav
ZAP

USE zagifak	ALIAS zagifak IN 0
SELECT zagifak
ZAP

USE ifakstav aLIAS ifakstav IN 0
SELECT ifakstav
ZAP

USE zagkalk	ALIAS zagkalk IN 0
SELECT zagkalk
ZAP

USE kalkstav ALIAS kalkstav IN 0
SELECT kalkstav
ZAP

USE MSzagkalk	ALIAS MSzagkalk IN 0
SELECT MSzagkalk
ZAP

USE MSkalkstav ALIAS MSkalkstav IN 0
SELECT MSkalkstav
ZAP

USE zagvrd	ALIAS zagvrd IN 0
SELECT zagvrd
ZAP

USE vrdstav ALIAS vrdstav IN 0
SELECT vrdstav
ZAP

USE zagvrk	ALIAS zagvrk IN 0
SELECT zagvrk
ZAP

USE vrkstav ALIAS vrkstav IN 0
SELECT vrkstav
ZAP


USE zagrev	ALIAS zagrev IN 0
SELECT zagrev
ZAP

USE revstav ALIAS revstav IN 0
SELECT revstav
ZAP

USE zaglom ALIAS zaglom IN 0
SELECT zaglom
ZAP

USE lomstav ALIAS lomstav IN 0
SELECT lomstav
ZAP

USE zagniv ALIAS zagniv IN 0
SELECT zagniv
ZAP

USE nivstav ALIAS nivstav IN 0
SELECT nivstav
ZAP

USE zagkalo ALIAS zagkalo in 0
SELECT zagkalo
ZAP

USE kalostav ALIAS kalostav in 0
SELECT kalostav
ZAP


SELECT zagkalk
APPEND BLANK
replace zagkalk.brdok WITH "PS"
replace zagkalk.datum	WITH CTOD("01.01.2009")
replace zagkalk.brdoki WITH "POPISNA LISTA"
replace zagkalk.datrac WITH CTOD("01.01.2009")


************************ kalkulacija *******************************
SELECT zalihe
GOTO top
DO WHILE !EOF()
a1=zalihe.sifra
a3=zalihe.ukol-zalihe.ikol
a4=zalihe.nabcen
a5=zalihe.procen
SELECT roba
SET ORDER TO tag sifrob
GOTO top
SEEK a1
a6=roba.tarifa
SELECT tarife
SET ORDER TO tag sifra
GOTO top
SEEK a6
a7=tarife.zbir
IF a3>0
SELECT kalkstav
APPEND BLANK
replace kalkstav.brdok	WITH "PS"
replace kalkstav.datum  WITH CTOD("01.01.2009")
replace kalkstav.sifrob	WITH a1
replace kalkstav.kolicina WITH a3
replace kalkstav.ncena	WITH a4
replace kalkstav.pcena	WITH a5
replace kalkstav.tarifa	WITH a6
replace kalkstav.stopa	WITH a7
endif
SELECT zalihe
SKIP
ENDDO



******************* kalkulacija obracun *******************************

SELECT zagkalk
GOTO top
DO WHILE !EOF()
a1=zagkalk.brdok
a2=zagkalk.datum
a3=zagkalk.kupdob
m.nvred=0
m.pvred=0
m.porez=0
m.marza=0
m.dobavljac=0
m.rabat=0
SELECT kalkstav
SET ORDER TO tag brdok
GOTO top
SEEK a1
DO WHILE !EOF()
IF kalkstav.brdok<>a1
	EXIT
ENDIF
replace kalkstav.datum WITH a2
replace kalkstav.kupdob	WITH a3
m.nvred=m.nvred+kalkstav.kolicina*kalkstav.ncena*(1-kalkstav.rabat/100)	
m.pvred=m.pvred+kalkstav.kolicina*kalkstav.pcena
m.dobavljac=m.dobavljac+kalkstav.kolicina*ncena
m.rabat=m.rabat+kalkstav.kolicina*kalkstav.ncena*kalkstav.rabat/100
m.porez=m.porez+kalkstav.kolicina*kalkstav.ncena*(1-kalkstav.rabat/100)*kalkstav.stopa/100
SKIP
ENDDO
m.marza=m.pvred-m.nvred
SELECT zagkalk
replace zagkalk.nvred		WITH m.nvred
replace zagkalk.pvred		WITH m.pvred
replace zagkalk.porez		WITH m.porez
replace zagkalk.marza		WITH m.marza
replace zagkalk.dobavljac	WITH m.dobavljac
replace zagkalk.rabat		WITH m.rabat
SKIP
ENDDO

RETURN


PROCEDURE FORMIRAJ
PARAMETERS dokument,koliko
ml=LEN(ALLTRIM(dokument))
dokument=REPLICATE("0",koliko-ml)+ALLTRIM(dokument)
return(dokument)