alter function dbo.fAllCodeBedBes(@Fc_ID int ,@Pro_ID_VahedMali int ,@TarikhAz int=0, @TarikhEla int=0) returns table
return(
--------------------------جمع اول دوره شخص
	select
		t.ID[ID],
		case when tr.Dom_ID_Nature=799 /*بدهکار*/ then isnull(tr.price,0.0) else 0.0 end [bed],
		case when tr.Dom_ID_Nature=800 /*بستانکار*/ then isnull(tr.price,0.0) else 0.0 end [bes]
	from tafsili t
		join TafsiliRemind tr on t.id = tr.taf_ID
	where 
		t.Dom_ID_noe not in (49 , 51) and 
		tr.Dom_ID_Nature <> 798 and -- بي حساب
		tr.tarikh >= @tarikhAz and
		tr.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or tr.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or tr.Pro_ID_VahedMali = @Pro_ID_VahedMali) 
	union all
	select
		t.ID[ID],
		isnull(tr.price,0.0) [bed],
		0.0 [bes]
	from tafsili t
		join TafsiliRemind tr on t.id = tr.taf_ID
	where 
		Dom_ID_noe in (49 , 51) and
		tr.tarikh >= @tarikhAz and
		tr.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or tr.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or tr.Pro_ID_VahedMali = @Pro_ID_VahedMali) 
	union all
-------------------------------------------------------------------------------------------------DaryaftPardakht => cheque = false
	SELECT 
		t.ID[ID],
		sum(case when d.Taf_ID_Bedehkar =t.ID or (d.Taf_ID = t.ID and d.dom_ID_NoeMaster = 40) then isnull(d.mablagh,0.0) else 0.0 end)[bed],
		sum(case when d.Taf_ID_Bestankar =t.ID or (d.Taf_ID = t.ID and d.dom_ID_NoeMaster = 40) then isnull(d.mablagh,0.0) else 0.0 end)[bes]
	from daryaftpardakht d 
		inner join Tafsili t ON d.Taf_ID_Bedehkar =t.ID or d.Taf_ID_Bestankar =t.ID or d.Taf_ID = t.ID
	WHERE
		d.tarikh >= @tarikhAz and
		d.tarikh <= @tarikhEla and
		d.Dom_ID_vaziat<>101 AND --deleted
		d.Dom_ID_NoeDetail != 44 and -- چک نباشد
		(@Fc_ID = 0 or d.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or d.Pro_ID_VahedMali = @Pro_ID_VahedMali) 
	GROUP BY t.ID
	union all
	
	SELECT 
		t.ID[ID],
		sum(case when d.Taf_ID = t.ID then isnull(d.PriceWage,0.0) else 0.0 end)[bed],
		sum(case when d.Taf_ID_Bestankar = t.ID then isnull(d.PriceWage,0.0) else 0.0 end)[bes]
	from daryaftpardakht d 
		inner join Tafsili t ON d.Taf_ID_Bestankar =t.ID or d.Taf_ID = t.ID
	WHERE
		d.tarikh >= @tarikhAz and
		d.tarikh <= @tarikhEla and
		d.Dom_ID_vaziat<>101 AND --deleted
		d.Dom_ID_NoeDetail != 44 and -- چک نباشد
		(@Fc_ID = 0 or d.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or d.Pro_ID_VahedMali = @Pro_ID_VahedMali)  and
		isnull(d.PriceWage,0) > 0 and d.dom_ID_NoeMaster = 39 -- کارمزد در حواله های پرداختی
	GROUP BY t.ID
	union all
---------------------------------------------------------------------- GardeshChek => Debit
	select
		t.ID [ID],
		sum(isnull(m.mablagh,0.0))[bed],
		0.0[bes]
	from daryaftpardakht m 
		join gardesheChek g on m.ID = g.DaryaftPardakht_ID
		join tafsili t on g.Taf_ID_Bedehkar = t.ID
	where
		g.tarikh >= @tarikhAz and
		g.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or g.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or g.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_vaziat <> 101 and
		m.Dom_ID_NoeDetail = 44 and -- cheque
		(
		case
			when g.Dom_ID_Vaziat = 106	/*DarJarianVosool*/ then -- اول دوره مهم است
				(case
					when m.dom_ID_NoeMaster = 38 /*دریافت*/ then 
						(case
							when isnull(m.isAvaldoreh,0) = 1 then 0 -- در این حالت، دریافت کننده بانک است
							else 0 -- در این حالت، دریافت کننده بانک است
						end)
					when m.dom_ID_NoeMaster = 39 /*پرداخت*/ then -- در این حالت، دریافت کننده یا شخص است یا هزینه
						(case
							when isnull(m.isAvaldoreh,0) = 1 then 0 -- در این حالت، دریافت کننده شخص است یا هزینه
							else 1 -- در این حالت، دریافت کننده شخص است یا هزینه
						end)
				end)
			when g.Dom_ID_Vaziat = 107	/*Bargasht*/ then -- هم دریافت هم پرداخت
				(case
					when m.dom_ID_NoeMaster = 38 /*دریافت*/ then 1 -- دریافت کننده شخص است یا هزینه
					when m.dom_ID_NoeMaster = 39 /*پرداخت*/ then 0 -- دریافت کننده بانک است
				end)
			when g.Dom_ID_Vaziat = 108	/*Deleted*/ then 0 -- نداریم اصلا
			when g.Dom_ID_Vaziat = 109	/*KharjShode*/ then -- فقط دریافت
				(case
					when t.Dom_ID_Noe in (49 /*بانک*/,51) then 0
					else 1
				end)
			when g.Dom_ID_Vaziat = 110	/*Mojood*/ then 0 -- فقط دریافت  -- در این حالت همیشه دریافت کننده، صندوق است
			when g.Dom_ID_Vaziat = 111	/*BargashtBeMoshtari*/ then -- فقط دریافت
				(case
					when t.Dom_ID_Noe = 50 /*Shakhs*/ then 1
					else 0
				end)	
			when g.Dom_ID_Vaziat = 112	/*PassShodeh*/ then  -- هم دریافت هم پرداخت
				(case
					when m.dom_ID_NoeMaster = 38 /*دریافت*/ then 1 -- در این حالت همیشه دریافت کننده، بانک است که مبلغ چک واریز می شود
					when m.dom_ID_NoeMaster = 39 /*پرداخت*/ then 0
				end)
			when g.Dom_ID_Vaziat = 113	/*BargashtAzMoshtari*/ then 0 -- فقط پرداخت -- در این حالت، دریافت کننده همیشه بانک است
			when g.Dom_ID_Vaziat = 114	/*DaryaftNaghdAzMoshtary*/ then -- فقط دریافت
				(case
					when t.Dom_ID_Noe = 51 /*Sandoogh*/ then 1
					else 0
				end)
			when g.Dom_ID_Vaziat = 115	/*PasDadanChekBargashti*/ then 0  -- هیچ وقت ارزش مالی ندارد
			when g.Dom_ID_Vaziat = 116	/*NaghdShodanChekBargashti*/ then 1 -- فقط دریافت - در این حالت، دریافت کننده همیشه صندوق است
			when g.Dom_ID_Vaziat = 117	/*NaghdShodanChek*/ then 1 -- فقط دریافت - در این حالت، دریافت کننده همیشه صندوق است
			when g.Dom_ID_Vaziat = 118	/*MojoodShdanChekKharjShodeh*/ then 0  -- فقط دریافت - در این حالت، دریافت کننده همیشه صندوق است
			else 0
		end
		) = 1
	group by t.ID
---------------------------------------------------------------------- GardeshChek => Credit
	union all
	select
		t.ID[ID],
		0.0[bed],
		sum(isnull(m.mablagh,0))[bes]
	from daryaftpardakht m
		join gardesheChek g on m.ID=g.DaryaftPardakht_ID 
		join tafsili t on g.Taf_ID_Bestankar=t.ID
	where
		g.tarikh >= @tarikhAz and
		g.tarikh <= @tarikhEla and
		m.Dom_ID_vaziat<>101 and
		m.Dom_ID_NoeDetail = 44 and
		(@Fc_ID = 0 or g.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		(
		case
			when g.Dom_ID_Vaziat = 106	/*DarJarianVosool*/ then 0 -- هم دریافت هم پرداخت
			when g.Dom_ID_Vaziat = 107	/*Bargasht*/ then -- هم دریافت هم پرداخت
				(case
					when m.dom_ID_NoeMaster = 38 /*دریافت*/ then 0 -- در این حالت بستانکار همیشه بانک است.
					when m.dom_ID_NoeMaster = 39 /*پرداخت*/ then 1 -- در این حالت معمولا (99 درصد) بستانکار شخص است
				end)
			when g.Dom_ID_Vaziat = 108	/*Deleted*/ then 0 -- اصلا نداریم
			when g.Dom_ID_Vaziat = 109	/*KharjShode*/ then 0 -- فقط دریافت
			when g.Dom_ID_Vaziat = 110	/*Mojood*/ then -- فقط دریافت - اول دوره مهم است
				(case
					when isnull(m.isAvaldoreh,0) = 1 then 0
					when isnull(m.isAvaldoreh,0) = 0 then (case when t.Dom_ID_Noe = 50 /*Shakhs*/ then 1 else 0 end)
				end)
			when g.Dom_ID_Vaziat = 111	/*BargashtBeMoshtari*/ then 0 -- فقط دریافت - در این حالت، بستانکار صندوق است
			when g.Dom_ID_Vaziat = 112	/*PassShodeh*/ then -- -- هم دریافت هم پرداخت
				(case
					when m.dom_ID_NoeMaster = 38 /*دریافت*/ then 0 -- دراین حالت بستانکار نال است
					when m.dom_ID_NoeMaster = 39 /*پرداخت*/ then 1 -- در این حالت، بستانکار همیشه بانک است
				end)
			when g.Dom_ID_Vaziat = 113	/*BargashtAzMoshtari*/ then 1 -- فقط پرداخت - در این حالت بستانکار یا شخص است یا هزینه
			when g.Dom_ID_Vaziat = 114	/*DaryaftNaghdAzMoshtary*/ then 0 --  -- فقط دریافت
			when g.Dom_ID_Vaziat = 115	/*PasDadanChekBargashti*/ then 0 -- فقط دریافت
			when g.Dom_ID_Vaziat = 116	/*NaghdShodanChekBargashti*/ then 1 -- فقط دریافت - در این حالت، بستانکار یا شخص است یا هزینه
			when g.Dom_ID_Vaziat = 117	/*NaghdShodanChek*/ then 0 -- فقط دریافت
			when g.Dom_ID_Vaziat = 118	/*MojoodShdanChekKharjShodeh*/ then 1 -- فقط دریافت - در این حالت، بستانکار یا شخص است یا هزینه
			else 0
		end
		) = 1
	group by t.ID
	union all
--------------------------------------------------------------------------------------   هزینه های بانکی در پاس شدن چک
	select
	isnull(g.Taf_ID_Hazineh,0) [ID],
	isnull(sum(isnull(g.mablagh,0)),0.0)[bed],
	0.0[bes]
	from gardesheChek g 
	where
		g.tarikh >= @tarikhAz and
		g.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or g.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or g.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		g.Taf_ID_Hazineh is not null and
		g.mablagh > 0
	group by g.Taf_ID_Hazineh
	union all
-------------------------------------------------------------------------------------------------تخفیف در برگه های دریافت
SELECT 
		t.ID[ID],
		sum(isnull(d.PriceDiscount,0.0))[bed],
		0.0 [bes]
	from daryaftpardakht d 
		inner join Tafsili t ON d.Taf_ID = t.ID and isnull(d.PriceDiscount,0) > 0
	WHERE
		d.dom_ID_NoeMaster = 38 and
		d.tarikh >= @tarikhAz and
		d.tarikh <= @tarikhEla and
		d.Dom_ID_vaziat<>101 AND --deleted
		(@Fc_ID = 0 or d.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or d.Pro_ID_VahedMali = @Pro_ID_VahedMali) 
	GROUP BY t.ID
	union all
	SELECT 
		t.ID[ID],
		0.0 [bed],
		sum(isnull(d.PriceDiscount,0.0)) [bes]
	from daryaftpardakht d 
		inner join Tafsili t ON d.Taf_ID_Bestankar = t.ID and isnull(d.taf_ID,0) > 0 and isnull(d.PriceDiscount,0) > 0
	WHERE
		d.dom_ID_NoeMaster = 38 and
		d.tarikh >= @tarikhAz and
		d.tarikh <= @tarikhEla and
		d.Dom_ID_vaziat<>101 AND --deleted
		(@Fc_ID = 0 or d.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or d.Pro_ID_VahedMali = @Pro_ID_VahedMali) 
	GROUP BY t.ID
	union all
-------------------------------------------------------------------------------------------------------- برگه
	-----------------------------------تخفیفات 
	select
		isnull(d.Taf_ID_Takhfif,0)[ID],
		sum(case when m.Dom_ID_noe in (28,32,1163,1325) then isnull(MablagheTakhfif,0.0) else 0.0 end)[bed],
		sum(case when m.Dom_ID_noe in (29,31,1162) then isnull(MablagheTakhfif,0.0) else 0.0 end)[bes]
	from bargeh m
		join bargehDetail d on m.ID=d.Bar_ID
		--join tafsili t on d.Taf_ID_Takhfif = t.ID
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe in (28,29,31,32,1162,1163,1190,1325) and 
		m.Dom_ID_vaziat<>101 and
		d.Taf_ID_Takhfif is not null
	group by d.Taf_ID_Takhfif
	union all
	----------------------------------- جمع بستانکاری خدمات در فاکتور فروش
	select
		isnull(d.Taf_ID_Khadamat,0)[ID],
		sum(case when d.Dom_ID_noe = 668 then isnull(mablagh,0)*isnull(meghdar1,0)*isnull(meghdar2,0)*isnull(tedad,0)-isnull(d.mablagheTakhfif,0) else 0.0 end) [bed],
		sum(case when d.Dom_ID_noe = 667 then isnull(mablagh,0)*(case when m.Dom_ID_noe in (1190) then 1 else isnull(meghdar1,0)*isnull(meghdar2,0) end)*isnull(tedad,0)-isnull(d.mablagheTakhfif,0) else 0.0 end) [bes]
	from bargeh m
		join bargehDetail d on m.ID =d.Bar_ID 
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe in (28,29,31,32,1190,1325) and
		m.Dom_ID_vaziat<>101 and
		d.Taf_ID_Khadamat is not null
	group by d.Taf_ID_Khadamat
	union all
	-------------------------------------------------جمع مبلغ مشتری در برگه ها
	select
		isnull(m.Taf_ID_Moshtari,0)[ID],
		sum(
			case 
				when m.Dom_ID_noe in (28,32,792,1163,1275,1325) then 
					isnull(d.MablaghArzeshAfzoodeh , 0.0) + (isnull(d.mablagh, 0.0)*isnull(d.meghdar1, 0.0)*isnull(d.meghdar2, 0.0)*isnull(d.tedad, 0.0))-(case when d.Taf_ID_Takhfif is not null then isnull(MablagheTakhfif,0) else 0 end) 
				when m.Dom_ID_noe in (1190) then 
					isnull(d.MablaghArzeshAfzoodeh , 0.0) + (isnull(d.mablagh, 0.0)*isnull(d.tedad, 0.0))-(case when d.Taf_ID_Takhfif is not null then isnull(MablagheTakhfif,0) else 0 end) 
				else 0.0 
			end
		)[bed],
		sum(
			case when m.Dom_ID_noe in (29,31,791,1162,1276) then (isnull(d.mablagh, 0.0)*isnull(d.meghdar1, 0.0)*isnull(d.meghdar2, 0.0)*isnull(d.tedad, 0.0))-(case when d.Taf_ID_Takhfif is not null then isnull(MablagheTakhfif,0) else 0 end)+(isnull(d.MablaghArzeshAfzoodeh , 0.0)) else 0.0 end
		) [bes]
	from bargeh m
		join bargehDetail d on m.ID = d.Bar_ID 
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe in (28,29,31,32,791,792,1162,1163,1190,1275,1276,1325) and 
		m.Dom_ID_vaziat <> 101
	group by m.Taf_ID_Moshtari
	union all
	select ------------------------------------------------ تخفیفات نهایی برگه برای مشتری
		isnull(b.Taf_ID_Moshtari,0) [ID],
		sum(case when b.Dom_ID_noe in (31,29,11662) then isnull(MablaghTakhfif,0.0) else 0.0 end)[bed],
		sum(case when b.Dom_ID_Noe in (28,32,1163,1190,1325) then isnull(MablaghTakhfif,0.0) else 0.0 end)[bes]
	from bargeh b
	where
		b.tarikh >= @tarikhAz and
		b.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or b.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or b.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		b.Dom_ID_vaziat<>101 and
		b.Dom_ID_Noe in (28,29,31,32,1162,1163,1190,1325) and 
		isnull(b.MablaghTakhfif ,0) > 0
	group by b.Taf_ID_Moshtari
	union all
	select ------------------------------------------------ تخفیفات نهایی عطف
		isnull(b.Taf_ID_Takhfif,0) [ID],
		sum(case when b.Dom_ID_noe in (28,32,1163,1190,1325) then isnull(MablaghTakhfif,0.0) else 0.0 end)[bed],
		sum(case when b.Dom_ID_Noe in (31,29,1162) then isnull(MablaghTakhfif,0.0) else 0.0 end)[bes]
	from bargeh b
	where
		b.tarikh >= @tarikhAz and
		b.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or b.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or b.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		b.Dom_ID_vaziat<>101 and
		b.Dom_ID_Noe in (28,29,31,32,1162,1163,1325) and 
		isnull(b.MablaghTakhfif ,0) > 0
	group by b.Taf_ID_Takhfif
	union all
	select ------------------------------------------------ اضافات نهایی برگه برای مشتری
		isnull(b.Taf_ID_Moshtari,0) [ID],
		sum(case when b.Dom_ID_noe in (28,32,1163,1190,1325) then isnull(b.MablaghEzafeh,0.0) else 0.0 end)[bed],
		sum(case when b.Dom_ID_noe in (29,31,1162) then isnull(b.MablaghEzafeh,0.0) else 0.0 end)[bes]
	from bargeh b
	where
		b.tarikh >= @tarikhAz and
		b.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or b.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or b.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		b.Dom_ID_vaziat<>101 and
		b.Dom_ID_noe in (28,29,31,32,1162,1163,1190,1325) and 
		isnull(b.MablaghEzafeh,0) > 0
	group by b.Taf_ID_Moshtari
	union all
	select ------------------------------------------------ مبلغ نقد برگه برای مشتری
		isnull(b.Taf_ID_Moshtari,0) [ID],
		sum(case when b.Dom_ID_noe in (29,31,1162) then isnull(b.MablaghSandoogh,0.0) else 0.0 end)[bed],
		sum(case when b.Dom_ID_noe in (28,32,1163,1190,1325) then isnull(b.MablaghSandoogh,0.0) else 0.0 end)[bes]
	from bargeh b
	where
		b.tarikh >= @tarikhAz and
		b.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or b.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or b.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		b.Dom_ID_vaziat<>101 and
		b.Dom_ID_noe in (28,29,31,32,1162,1163,1190,1325) and 
		isnull(b.MablaghSandoogh,0) > 0
	group by Taf_ID_Moshtari
	union all
	select ------------------------------------------------ مبلغ نقد برگه برای صندوق
		isnull(b.Taf_ID_Sandoogh,0) [ID],
		sum(case when b.Dom_ID_noe in (28,32,1163,1190,1325) then isnull(b.MablaghSandoogh,0.0) else 0.0 end)[bed],
		sum(case when b.Dom_ID_noe in (29,31,1162) then isnull(b.MablaghSandoogh,0.0) else 0.0 end)[bes]
	from bargeh b
	where
		b.tarikh >= @tarikhAz and
		b.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or b.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or b.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		b.Dom_ID_vaziat<>101 and
		b.Dom_ID_noe in (28,29,31,32,1162,1163,1190,1325) and 
		isnull(b.MablaghSandoogh,0) > 0
	group by b.Taf_ID_Sandoogh
	union all
	select ------------------------------------------------ تخصیص مبلغ در فاکتورها
		isnull(bdp.Taf_ID,0) [ID],
		0.0 [bed],
		sum(isnull(bdp.Mablagh,0.0))[bes]
	from bargeh b
		inner join bargehdetail d on b.id = d.bar_ID
		inner join bargehDetailProperty bdp on d.ID = bdp.BD_ID
	where
		b.tarikh >= @tarikhAz and
		b.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or b.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or b.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		b.Dom_ID_vaziat<>101 and
		b.Dom_ID_noe in (28,29,31,32,1162,1163,1190) and 
		bdp.Dom_ID_Type = 670 and -- تخصيص مبلغ به اشخاص
		isnull(bdp.Taf_ID,0) > 0
	group by bdp.Taf_ID
	union all
	--------------------------------------------------------------------------------- ویزیتوری
	select
		isnull(m.Taf_ID_Visitor,0)[ID],
		sum(case when m.Dom_ID_noe in (29,32) then isnull(m.MablagheVisitor,0.0) else 0.0 end)[bed],
		sum(case when m.Dom_ID_noe in (28,31,1162,1163,1190,1325) then isnull(m.MablagheVisitor,0.0) else 0.0 end)[bes]
	from bargeh m 
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe in (28,29,31,32,1162,1163,1190,1325) and
		m.Dom_ID_vaziat<>101 and
		m.Taf_ID_Visitor is not null
	group by m.Taf_ID_Visitor
	union all
	select
		isnull(d.taf_ID_Visitor,0)[ID],
		sum(case when m.Dom_ID_noe in (29,32) then isnull(d.MablagheVisitor,0.0) else 0.0 end)[bed],
		sum(case when m.Dom_ID_noe in (28,31,1162,1163,1190,1325) then isnull(d.MablagheVisitor,0.0) else 0.0 end)[bes]
	from Bargeh m
		join bargehDetail d on m.ID = d.Bar_ID 
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe in (28,29,31,32,1162,1163,1190,1325) and
		m.Dom_ID_vaziat<>101 and
		d.Taf_ID_Visitor is not null 
	group by d.taf_ID_Visitor
	union all
	--------------------------------------------------------------------------------- هزینه ویزیتور
	select
		isnull(m.Taf_ID_HazineVisitor,0) [ID],
		sum(case when m.Dom_ID_noe in (28,31,1162,1163,1190,1325) then isnull(m.mablagheVisitor,0.0) else 0.0 end)[bed],
		sum(case when m.Dom_ID_noe in (29,32) then isnull(m.mablagheVisitor,0.0) else 0.0 end)[bes]
	from bargeh m
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe in (28,29,31,32,1162,1163,1190,1325) and
		m.Dom_ID_vaziat<>101 and
		m.Taf_ID_HazineVisitor is not null
	group by m.Taf_ID_HazineVisitor
	union all
	select
		isnull(d.Taf_ID_HazinehVisitor,0)[ID],
		sum(case when m.Dom_ID_noe in (28,31,1162,1163,1190,1325) then isnull(d.mablagheVisitor,0.0) else 0.0 end)[bed],
		sum(case when m.Dom_ID_noe in (29,32) then isnull(d.mablagheVisitor,0.0) else 0.0 end)[bes]
	from bargeh m
		join bargehDetail d on m.ID=d.Bar_ID 
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe in (28,29,31,32,1162,1163,1190,1325) and -- Foroosh, Kharid
		m.Dom_ID_vaziat<>101 and
		d.Taf_ID_HazinehVisitor is not null
	group by d.Taf_ID_HazinehVisitor
	union all
	---------------------------------------------------------------------------------  ارزش افزوده 
	select
		isnull(d.Taf_ID_ArzeshAfzoodeh,0)[ID],
		sum(case when m.Dom_ID_noe in (32,29) then isnull(d.MablaghArzeshAfzoodeh , 0.0) else 0.0 end)[bed],
		sum(case when m.Dom_ID_noe in (28,31,1162,1163,1190,1325) then isnull(d.MablaghArzeshAfzoodeh , 0.0) else 0.0 end)[bes]
	from bargeh m
		join bargehDetail d on m.ID=d.Bar_ID
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe in (28,29,31,32,1162,1163,1190,1325) and
		m.Dom_ID_vaziat <> 101 and
		d.Taf_ID_ArzeshAfzoodeh is not null
	group by d.Taf_ID_ArzeshAfzoodeh
	union all
	select
		isnull(m.Taf_ID_Ezafeh,0)[ID],
		sum(case when m.Dom_ID_noe in (32,29) then isnull(m.MablaghEzafeh, 0.0) else 0.0 end)[bed],
		sum(case when m.Dom_ID_noe in (28,31,1162,1163,1190,1325) then isnull(m.MablaghEzafeh , 0.0) else 0.0 end)[bes]
	from bargeh m
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe in (28,29,31,32,1162,1163,1190,1325) and
		m.Dom_ID_vaziat <> 101 and
		m.Taf_ID_Ezafeh is not null and
		m.MablaghEzafeh > 0
		
	group by m.Taf_ID_Ezafeh
	union all
	---------------------------------------------------------------------------------  متمم خرید 
	select
		isnull(p.Taf_ID,0)[ID],
		sum(isnull(p.Mablagh,0.0)) [bed],
		0.0 [bes]
	from bargeh m
		join bargehProperty p on m.id = p.bar_ID
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe in (31) and
		m.Dom_ID_vaziat <> 101
	group by p.Taf_ID
	union all
	select
		isnull(p.Taf_ID_atf,0)[ID],
		0.0 [bed],
		sum(isnull(p.Mablagh,0.0))[bes]
	from bargeh m
		join bargehProperty p on m.id = p.bar_ID
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe in (31) and
		m.Dom_ID_vaziat <> 101
		
	group by p.Taf_ID_atf
	union all
	--------------------------------------------------------------فقط دستمزدهای های تولید در گردش می آیند مابقی در قیمت تمام شده لحاظ می شود
	select
		t.ID[ID],
		sum(case when d.taf_ID2 = t.ID then isnull(d.tedad,1.0) * isnull(d.price,0.0) else 0.0 end) [bed],
		sum(case when d.taf_ID = t.id then isnull(d.tedad,1.0) * isnull(d.price,0.0) else 0.0 end) [bes]
	from Product m
		join ProductDetail d on m.id = d.prod_ID 
		join tafsili t on d.taf_ID = t.ID or d.taf_ID2 = t.ID
	where
		m.Dom_ID_Type = 1583 /*فرمول متغیر*/ and
		d.tarikh >= @tarikhAz and
		d.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or d.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or d.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.dom_ID_vaziat <> 101 and
		d.Dom_ID_Noe = 262 /*Dastmozd*/
	group by t.ID
	union all
		select
		t.ID[ID],
		sum(case when d.taf_ID2 = t.ID then isnull(d.tedad,1.0) * isnull(d.price,0.0) else 0.0 end) [bed],
		sum(case when d.taf_ID = t.id then isnull(d.tedad,1.0) * isnull(d.price,0.0) else 0.0 end) [bes]
	from Product m
		join ProductDetail d on m.id = d.prod_ID 
		join tafsili t on d.taf_ID = t.ID or d.taf_ID2 = t.ID
	where
		m.Dom_ID_Type in (1582 /*فرمول ثابت*/ , 1584 /*فرمول ترکیبی*/) and
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.dom_ID_vaziat <> 101 and
		d.Dom_ID_Noe = 262 /*Dastmozd*/
	group by t.ID
	union all
	---------------------------------------------------------------------------------  حقوق دستمزد
	--------------------------------------------------------------- بستانکاری شخص
	select
		isnull(m.Taf_ID,0)[ID],
		sum(case d.Dom_ID_DocType when 1601 /*کاهشی*/ then d.Price else 0.0 end) [bed],
		sum(case d.Dom_ID_DocType when 1600 /*افزایشی*/ then d.Price else 0.0 end) [bes]
	from SalaryReceipt m
		join SalaryReceiptItem d on m.id = d.SR_ID
		inner join Domain domMonth on m.Dom_ID_Month = domMonth.ID
	where
		isnull((select MAX(fcd.jdate) from Finance_Cycle_Detail fcd where fcd.FC_ID = m.FC_ID and fcd.[Month] = domMonth.Priority),0) >= @tarikhAz and
		isnull((select MAX(fcd.jdate) from Finance_Cycle_Detail fcd where fcd.FC_ID = m.FC_ID and fcd.[Month] = domMonth.Priority),15010101) <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali)
	group by m.Taf_ID
	
	union all
	--------------------------------------------------------------- بدهکاری هزینه حقوق
	select
		isnull(m.Taf_ID_SalaryCost,0)[ID],
		sum(
			case d.Dom_ID_DocType 
				when 1600 /*افزایشی*/ then d.Price 
				when 1606 /*خنثی*/ then d.Price 
				--when 1601 /*کاهشی*/ then (d.Price * -1)
				else 0.0 
			end
		) [bed],
		0.0 [bes]
	from SalaryReceipt m
		join SalaryReceiptItem d on m.id = d.SR_ID
		inner join Domain domMonth on m.Dom_ID_Month = domMonth.ID
	where
		isnull((select MAX(fcd.jdate) from Finance_Cycle_Detail fcd where fcd.FC_ID = m.FC_ID and fcd.[Month] = domMonth.Priority),0) >= @tarikhAz and
		isnull((select MAX(fcd.jdate) from Finance_Cycle_Detail fcd where fcd.FC_ID = m.FC_ID and fcd.[Month] = domMonth.Priority),15010101) <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali)
	group by m.Taf_ID_SalaryCost
	
	union all
	--------------------------------------------------------------- بستانکاری مالیات
	select
		isnull(m.Taf_ID_Tax,0)[ID],
		0.0 [bed],
		sum(d.Price) [bes]
	from SalaryReceipt m
		join SalaryReceiptItem d on m.id = d.SR_ID
		inner join Domain domMonth on m.Dom_ID_Month = domMonth.ID
	where
		isnull((select MAX(fcd.jdate) from Finance_Cycle_Detail fcd where fcd.FC_ID = m.FC_ID and fcd.[Month] = domMonth.Priority),0) >= @tarikhAz and
		isnull((select MAX(fcd.jdate) from Finance_Cycle_Detail fcd where fcd.FC_ID = m.FC_ID and fcd.[Month] = domMonth.Priority),15010101) <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		d.Dom_ID_RowType = 318 /*ماليات*/
	group by m.Taf_ID_Tax
	
	union all
	--------------------------------------------------------------- بستانکاری تامین اجتماعی
	select
		isnull(m.Taf_ID_Insurance,0)[ID],
		0.0 [bed],
		sum(d.Price) [bes]
	from SalaryReceipt m
		join SalaryReceiptItem d on m.id = d.SR_ID
		inner join Domain domMonth on m.Dom_ID_Month = domMonth.ID
	where
		isnull((select MAX(fcd.jdate) from Finance_Cycle_Detail fcd where fcd.FC_ID = m.FC_ID and fcd.[Month] = domMonth.Priority),0) >= @tarikhAz and
		isnull((select MAX(fcd.jdate) from Finance_Cycle_Detail fcd where fcd.FC_ID = m.FC_ID and fcd.[Month] = domMonth.Priority),15010101) <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		d.Dom_ID_RowType in (330,329,1604,1605) /*بيمه سهم کارگر، بيمه سهم کارفرما، بیمه بیکاری، بیمه -سختی کار*/ 
	group by m.Taf_ID_Insurance
	-------------------------------------------------------------------------------------------------------- املاک
	union all
	----------------------------------- بستانکاری درآمد بنگاه
	select
		isnull(m.Taf_ID_Moshtari,0)[ID],
		0.0 [bed],
		sum(isnull(bp.Mablagh,0.0) /*- isnull(bp.Darsad,0.0)*/)[bes]
	from bargeh m
		join bargehProperty bp on m.ID=bp.Bar_ID and bp.dom_ID_Type = 1901 /*کمیسیون بنگاه*/
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe = 1889 and 
		m.Dom_ID_vaziat<>101
	group by m.Taf_ID_Moshtari
	union all
	----------------------------------- بستانکاری ارزش افزوده
	select
		isnull(m.Taf_ID_Ezafeh,0)[ID],
		0.0 [bed],
		sum(isnull(bp.Mablagh,0.0))[bes]
	from bargeh m
		join bargehProperty bp on m.ID=bp.Bar_ID and bp.dom_ID_Type = 1907 /*ارزش افزوده قرارداد مبایعه/اجاره نامه*/
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe = 1889 and 
		m.Dom_ID_vaziat<>101
	group by m.Taf_ID_Ezafeh
	union all
	----------------------------------- بدهکاری فروشنده و خریدار بابت کمیسیون
	select
		isnull(bp.Taf_ID,0)[ID],
		sum(isnull(bp.Mablagh,0.0) - isnull(bp.Darsad,0.0)) [bed],
		0.0 [bes]
	from bargeh m
		join bargehProperty bp on m.ID=bp.Bar_ID and bp.dom_ID_Type in (1892,1893) /*فروشنده ، خریدار*/
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe = 1889 and 
		m.Dom_ID_vaziat<>101
	group by bp.Taf_ID
	union all
	----------------------------------- بدهکاری تخفیف بنگاه، فروشنده، خریدار
	select
		isnull(m.Taf_ID_Takhfif,0)[ID],
		sum(isnull(bp.Darsad,0.0)) [bed],
		0.0 [bes]
	from bargeh m
		join bargehProperty bp on m.ID=bp.Bar_ID and bp.dom_ID_Type in (1892,1893,1901) /*تخفیف کمیسیون بنگاه - تخفیف کمیسون فروشنده - تخفیف کمیسیون خریدار*/
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe = 1889 and 
		m.Dom_ID_vaziat<>101
	group by m.Taf_ID_Takhfif
	union all
	----------------------------------- بستانکاری ویزیتور
	select
		isnull(bp.Taf_ID,0)[ID],
		0.0 [bed],
		sum(isnull(bp.Mablagh,0.0)) [bes]
	from bargeh m
		join bargehProperty bp on m.ID=bp.Bar_ID and bp.dom_ID_Type in (627) /*ويزيتور*/
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe = 1889 and 
		m.Dom_ID_vaziat<>101 and
		isnull(bp.Mablagh,0.0) > 0
	group by bp.Taf_ID

	union all
	----------------------------------- بدهکاری هزینه ویزیتور
	select
		isnull(m.Taf_ID_HazineVisitor,0)[ID],
		sum(isnull(bp.Mablagh,0.0)) [bed],
		0.0 [bes]
	from bargeh m
		join bargehProperty bp on m.ID=bp.Bar_ID and bp.dom_ID_Type in (627) /*ويزيتور*/
	where
		m.tarikh >= @tarikhAz and
		m.tarikh <= @tarikhEla and
		(@Fc_ID = 0 or m.FC_ID = @Fc_ID) and
		(@Pro_ID_VahedMali = 0 or m.Pro_ID_VahedMali = @Pro_ID_VahedMali) and
		m.Dom_ID_noe = 1889 and 
		m.Dom_ID_vaziat<>101 and
		isnull(bp.Mablagh,0.0) > 0
	group by m.Taf_ID_HazineVisitor
)
;