SELECT 
	0 as iddok,
	'POCETNO' as ozndokumenta,
	'FAKTURA' as tip,
	'POCETNO' as podtip,
	'2013-01-01' as datum,
	'2013-01-01' as datarh,
	'2013-01-01' as datvalute,
	'2013-01-01' as datprometa,
	1 as arhiviran,
	1 as proknjizen,
	0 as kepu,
	0 as rbroj,
	-1 as rbrdok,
	0 as rbrkepu,
	'Grupno pocetno stanje' as opis,
	0 as storno,
	sum(f.iznos) as iznos,
	sum(f.stornoiznos) as stornoiznos,
	sum(f.uplata) as uplata,
	sum(f.zaduzenje) as zaduzenje,
	sum(f.razduzenje) as razduzenje,
	'p' as usernamepoc,
	'p' as usernamekraj,
	null as oznogranak,
	null as oznrj,
	f.sfrkomitent,
	null as original,
	'U' as vrsta,
	1 as cvprodajna,
	sum(f.vrednostrobe) as vrednostrobe,
	sum(f.vrednostusluga) as vrednostusluga,
			0 as iddok,
	'POCETNO' as ozndokumenta,
	'2013-01-01' as datum,
	'2013-01-01' as datprometa, 
	'2013-01-01' as datvalute, 
	'2013-01-01' as datarh,
	'FAKTURA' as tip,
	'POCETNO' as podtip, 
	sum(d.iznos)		as iznos,
	sum(d.iznosbezporeza)		as iznosbezporeza,
	1 as arhiviran,
	1 as proknjizen,
	0 as procrabata,
	0 as kurs,
	0 as rbroj,
	-1 as rbrdok,
	f.sfrkomitent as sfrkomitent, 
	null as oznrj,
	null as oznogranak,
	'p' as usernamepoc,
	'p' as usernamekraj, 
	null as faktura,
	0 as storno,
	sum(f.uplata) as uplata,
	'' as poruka,
	0 as zaotpremnice,
	null as zauzeto,
	1 as saporezom,
	null as sapravomnaodbitak,
	null as robuizdao,
	null as robuprimio,
	null as licnakarta,
	null as komercijalista,
	null as tekuciracun,
	sum(d.cvprodajna) as cvprodajna,
	0 as rabatukupno,
	sum(d.nabavnaukupno) as nabavnaukupno,
	sum(d.stornoiznos) as stornoiznos,
	null as original,
	null as nalogmagacinu,
	'FAKTURA_STANDARDNA' as discriminator,
	sum(d.vrednostrobe) as vrednostrobe,
	null as nacinotpreme,
	null as potvrda,
	null as pb
	
	FROM Dokument f, faktura d

	WHERE 
	f.arhiviran=1 and f.storno=0 and f.iznos-f.stornoIznos - f.uplata <> 0 and d.iddok = f.iddok and 
	(f.tip='FAKTURA' or f.tip='FAKTURA_IZ_SERVISA' or f.tip='FAKTURA_USLUGA')
group by
f.sfrkomitent
order by 
f.sfrkomitent