--select * from bargeh-- PrcPersonCardDetailNaghdAndSanad 4 , 1 --select * from tafsili IF EXISTS(SELECT NAME FROM sysobjects s WHERE s.name='PrcPersonCardDetailNaghdAndSanad') DROP proc PrcPersonCardDetailNaghdAndSanad GO CREATE proc PrcPersonCardDetailNaghdAndSanad @pcode int , @NoeGardesh smallint as begin declare @NoeTaf smallInt; set @NoeTaf = (select noe From Tafsili where code = @pcode); select ------------------------------------------- مانده اول دوره 0[Radif], 0[Tarikh], 0[Saat], 'مانده اول دوره'[Sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], case t.noe when 2 then -- shakhs case when t.MojoodiAvalDore<0 then abs(t.MojoodiAvalDore) else 0.0 end when 1 then t.MojoodiAvalDore--bank when 3 then t.MojoodiAvalDore--Sandoogh else 0.0 end as [Bedehkar], case t.noe when 2 then -- Shakhs case when t.MojoodiAvalDore>0 then abs(t.MojoodiAvalDore) else 0.0 end else 0.0 end as [Bestankar], 0.0[Mandeh], ''[Mahiat], 0[NoeRadif], t.code[Code] from Tafsili t where t.code= @pcode union all select ------------------------------------------- پرداخت 0[Radif], m.tarikh[Tarikh], m.SaatSabt[Saat], case m.NoeMaster when 0 then case m.noeDetail when 0 then 'دریافت ('+ cast(m.code as nvarchar) + '):'+'چک شماره ('+m.ShomarehSanad+')'+(select name from tafsili where Code =m.CodeTafsiliBestankar )+' '+isnull(m.sharh,'') when 1 then 'دریافت ('+ cast(m.code as nvarchar) + '):'+' شماره حواله بانکي ('+m.ShomarehSanad+')'+(select name from tafsili where Code =m.CodeTafsiliBestankar )+' '+isnull(m.sharh,'') when 2 then 'دریافت وجه ('+ cast(m.code as nvarchar) + ')' +(select name from tafsili where Code =m.CodeTafsiliBestankar )+' ' +isnull(m.sharh,'') end --when 3 then 'دريافت ('+ cast(m.code as nvarchar) + '):'+' شماره حواله حسابداري ('+d.ShomarehSanad+')' +m.sharh+' '+d.sharh end when 1 then case m.noeDetail when 0 then 'پرداخت ('+ cast(m.code as nvarchar) + '):'+'چک شماره ('+m.ShomarehSanad+')' +(select name from tafsili where Code =m.CodeTafsiliBestankar )+' ' +isnull(m.sharh,'') when 1 then 'پرداخت ('+ cast(m.code as nvarchar) + '):'+' شماره حواله بانکي ('+m.ShomarehSanad+')'+(select name from tafsili where Code =m.CodeTafsiliBestankar )+' ' +isnull(m.sharh,'') when 2 then 'پرداخت وجه ('+ cast(m.code as nvarchar) + ')' +(select name from tafsili where Code =m.CodeTafsiliBestankar )+' ' +isnull(m.sharh,'') end --when 3 then 'دريافت (' + cast(m.code as nvarchar) + '):'+' شماره حواله حسابداري ('+d.ShomarehSanad+')'+m.sharh+' '+d.sharh end when 2 then 'پرداخت هزينه ('+ cast(m.code as nvarchar) + ')' +(select name from tafsili where Code =m.CodeTafsiliBestankar )+' ' + isnull(m.sharh,'') when 3 then 'حواله حسابداري ('+ cast( m.code as nvarchar) + ')' +(select name from tafsili where Code =m.CodeTafsiliBestankar )+' ' +isnull(m.sharh,'') end as [sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], m.mablagh as [Bedehkar], 0.0 as [Bestankar], 0.0[Mandeh], ''[Mahiat], case m.NoeMaster when 0 then case m.noeDetail when 0 then case m.IsAvalDoreh when 0 then 2 --DaryaftChek when 1 then 1 end --DaryaftChekAvalDoreh when 1 then 3 --DaryaftHavaleh when 2 then 4 --DaryaftVajh end when 1 then case m.noeDetail when 0 then case m.IsAvalDoreh when 0 then 6 --PardakhtChek when 1 then 5 end --PardakhtChekAvalDoreh when 1 then 7 --PardakhtHavaleh when 2 then 8 -- PardakhtVajh end when 2 then 9--Hazineh when 3 then 10--HavalehHesabdari when 4 then 11--DarAmad end [NoeRadif], --2[NoeRadif], m.code [Code] from daryaftpardakht m where m.Vaziat != 1 and m.CodeBargehParent is null and (m.IsAvalDoreh = 0 or m.IsAvalDoreh is null) and m.CodeTafsiliBedehkar = @pcode union all select ------------------------------------------- دریافت 0[Radif], m.tarikh[Tarikh], m.SaatSabt[Saat], case m.noeMaster when 0 then case m.noeDetail when 0 then 'دریافت (' + cast(m.code as nvarchar) + '):'+'چک شماره ('+m.ShomarehSanad+')'+(select name from tafsili where Code =m.CodeTafsiliBedehkar )+' ' +isnull(m.sharh,'') when 1 then 'دریافت حواله بانکي (' + cast(m.code as nvarchar) + ')'+(select name from tafsili where Code =m.CodeTafsiliBedehkar )+' ' +isnull(m.sharh,'') when 2 then 'دریافت وجه (' + cast(m.code as nvarchar) + ')'+(select name from tafsili where Code =m.CodeTafsiliBedehkar )+' ' +isnull(m.sharh,'') end --when 3 then 'پرداخت حواله حسابداري (' + cast(m.code as nvarchar) + ')'+isnull(m.sharh,'') end when 1 then case m.noeDetail when 0 then 'پرداخت (' + cast(m.code as nvarchar) + '):'+'چک شماره ('+m.ShomarehSanad+')'+(select name from tafsili where Code =m.CodeTafsiliBedehkar )+' ' +isnull(m.sharh,'') when 1 then 'پرداخت حواله بانکي (' + cast(m.code as nvarchar) + ')'+(select name from tafsili where Code =m.CodeTafsiliBedehkar )+' ' +isnull(m.sharh,'') when 2 then 'پرداخت وجه ('+ cast(m.code as nvarchar) + ')'+(select name from tafsili where Code =m.CodeTafsiliBedehkar )+' ' +isnull(m.sharh,'') end --when 3 then 'پرداخت حواله حسابداري (' + cast(m.codeBargeh as nvarchar) + ')' +m.sharh+' '+d.sharh end when 2 then 'دریافت هزينه ('+ cast(m.code as nvarchar) + ')'+(select name from tafsili where Code =m.CodeTafsiliBedehkar )+' ' +isnull(m.sharh,'') when 3 then 'حواله حسابداري ('+ cast( m.code as nvarchar) + ')' +(select name from tafsili where Code =m.CodeTafsiliBedehkar )+' ' +isnull(m.sharh,'') end as [sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], 0.0 as [Bedehkar], m.mablagh as [Bestankar], 0.0[Mandeh], ''[Mahiat], case m.NoeMaster when 0 then case m.noeDetail when 0 then case m.IsAvalDoreh when 0 then 2 --DaryaftChek when 1 then 1 end --DaryaftChekAvalDoreh when 1 then 3 --DaryaftHavaleh when 2 then 4 --DaryaftVajh end when 1 then case m.noeDetail when 0 then case m.IsAvalDoreh when 0 then 6 --PardakhtChek when 1 then 5 end --PardakhtChekAvalDoreh when 1 then 7 --PardakhtHavaleh when 2 then 8 -- PardakhtVajh end when 2 then 9--Hazineh when 3 then 10--HavalehHesabdari when 4 then 11--DarAmad end [NoeRadif], --3 [NoeRadif], m.code [Code] from daryaftpardakht m where m.Vaziat !=1 and m.CodeBargehParent is null and (m.IsAvalDoreh = 0 or m.IsAvalDoreh is null) and m.CodeTafsiliBestankar = @pcode union all select ------------------------------------------- پرداخت به چند نفر 0[Radif], m.Tarikh[Tarikh], m.SaatSabt[Saat], case m.NoeDetail when 0 then 'پرداخت چک به چند نفر (' + cast(m.code as nvarchar)+') :'+'چک شماره ('+m.ShomarehSanad+')' + ' '+isnull(m.sharh,'') + case d.IsChekOwner when 0 then 'شريک چک' when 1 then 'صاحب چک' end when 1 then 'پرداخت حواله بانکي به چند نفر (' + cast(m.code as nvarchar)+') :' +'حواله شماره ('+m.ShomarehSanad+')' + ' '+isnull(m.sharh,'') + case d.IsChekOwner when 0 then 'شريک حواله' when 1 then 'صاحب حواله' end end as [Sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], d.Mablagh as [BedehKar], 0.0 as [BestanKar], 0.0[Mandeh], ''[Mahiat], case m.NoeMaster when 1 then case m.noeDetail when 0 then case m.IsAvalDoreh when 0 then 6 --PardakhtChek when 1 then 5 end --PardakhtChekAvalDoreh when 1 then 7 --PardakhtHavaleh end end [NoeRadif], --3[NoeRadif], m.code as [Code] from DaryaftPardakht m join ChekDetail d on m.Code = d.CodeParent where m.Vaziat !=1 and m.CodeTafsiliBedehkar is null and d.CodeTafsiliMoshtari = @pcode union all select ------------------------------------------- دريافت از چند نفر 0[Radif], m.Tarikh[Tarikh], m.SaatSabt[Saat], case m.NoeDetail when 0 then 'دريافت چک از چند نفر (' + cast(m.code as nvarchar)+') :'+ 'چک شماره ('+m.ShomarehSanad+')' + ' '+isnull(m.sharh,'') + case d.IsChekOwner when 0 then 'شريک چک' when 1 then 'صاحب چک' end when 1 then 'دريافت حواله بانکی از چند نفر (' + cast(m.code as nvarchar)+') :'+ 'حواله شماره ('+m.ShomarehSanad+')' + ' '+isnull(m.sharh,'') + case d.IsChekOwner when 0 then 'شريک حواله' when 1 then 'صاحب حواله' end end as [Sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], 0.0 as [BedehKar], d.Mablagh as [BestanKar], 0.0[Mandeh], ''[Mahiat], case m.NoeMaster when 0 then case m.noeDetail when 0 then case m.IsAvalDoreh when 0 then 2 --DaryaftChek when 1 then 1 end --DaryaftChekAvalDoreh when 1 then 3 --DaryaftHavaleh end end [NoeRadif], --2[NoeRadif], m.code as [Code] from DaryaftPardakht m join ChekDetail d on m.Code = d.CodeParent where m.Vaziat !=1 and m.CodeTafsiliBestankar is null and d.CodeTafsiliMoshtari = @pcode union all select --------------------------------------------------------------------------- اعمال بدهي براي فاکتور فروش - بصورت تک تک کالا گزارش می گیرد 0 as [Radif], m.Tarikh[tarikh], m.SaatSabt[Saat], case m.noe when 0 then case d.IsKala when 0 then 'فروش ('+ cast(m.codeBargeh as nvarchar)+ ')نام خدمات :' +(select name from tafsili where Code = d.CodeKhadamat2)+'('+ cast((select CodeHesab from Tafsili where Code = d.codeKhadamat2)as nvarchar) +')'+isnull(d.sharheRadif,'') when 1 then ' فروش ('+ cast(m.codeBargeh as nvarchar)+ ')نام کالا :' +(select name from Kala where Code = d.CodeKala)+'('+ cast(d.codeKala as nvarchar)+')' +isnull(d.sharheRadif,'') end when 1 then 'برگشت از فروش ('+ cast(m.codeBargeh as nvarchar)+ ')' when 2 then '' when 3 then 'خريد (' + cast(m.codeBargeh as nvarchar)+ ')' when 4 then 'برگشت از خرید (' + cast(m.codeBargeh as nvarchar)+ ')' when 5 then '' when 6 then '' when 7 then '' when 8 then '' end as [sharh], d.Tedad[Meghdar], case m.noe when 0 then case d.IsKala when 0 then --Khadamat (select isnull(name,'') from vahed v where v.code = (select isnull(t.CodeVahed,0) from tafsili t where t.Code = d.CodeKhadamat2)) when 1 then -- Kala (select isnull(name,'') from vahed v where v.code = (select isnull(k.CodeVahed,0) from Kala k where k.Code = d.CodeKala)) end when 1 then -- BargashtAzForoosh (select isnull(name,'') from vahed v where v.code = (select isnull(k.CodeVahed,0) from Kala k where k.Code = d.CodeKala)) when 3 then -- Kharid (select isnull(name,'') from vahed v where v.code = (select isnull(k.CodeVahed,0) from Kala k where k.Code = d.CodeKala)) when 4 then -- BargashtAzKharid (select isnull(name,'') from vahed v where v.code = (select isnull(k.CodeVahed,0) from Kala k where k.Code = d.CodeKala)) end [Vahed], d.Mablagh[Fi], case m.noe when 0 then cast((d.mablagh * d.Meghdar1 * d.Meghdar2 * d.Tedad - isnull(d.MablagheTakhfif,0)) as decimal) when 1 then 0.0 when 2 then 0.0 when 3 then 0.0 when 4 then cast((d.mablagh * d.Meghdar1 * d.Meghdar2 * d.Tedad - isnull(d.MablagheTakhfif,0)) as decimal) when 5 then 0.0 when 6 then 0.0 when 7 then 0.0 when 8 then 0.0 end as [Bedehkar], case m.noe when 0 then 0.0 when 1 then cast((d.mablagh * d.Meghdar1 * d.Meghdar2 * d.Tedad - isnull(d.MablagheTakhfif,0)) as decimal) when 2 then 0.0 when 3 then cast((d.mablagh * d.Meghdar1 * d.Meghdar2 * d.Tedad -isnull( d.MablagheTakhfif , 0)) as decimal) when 4 then 0.0 when 5 then 0.0 when 6 then 0.0 when 7 then 0.0 when 8 then 0.0 end as [Bestankar], 0.0 as [Mandeh], ''[Mahiat], case m.noe when 0 then 12 --Foroosh when 1 then 15 -- BargashtAzForoosh when 3 then 13 -- Kharid when 4 then 14 -- BargashtAzKharid end as [NoeRadif], m.code[code] from (Bargeh m join BargehDetail d on m.code = d.CodeBargeh) where m.Vaziat !=1 and @NoeGardesh = 1 and cast(m.CodeMoshtari as int) = @pcode union all select --------------------------------------------------------------------------- اعمال بدهي براي فاکتور فروش بصورت کلی گزارش می گیرد 0 as [Radif], m.Tarikh[tarikh], m.SaatSabt[Saat], case m.noe when 0 then 'فروش ('+ cast(m.codeBargeh as nvarchar)+ ')' when 1 then 'برگشت از فروش ('+ cast(m.codeBargeh as nvarchar)+ ')' when 2 then '' when 3 then 'خريد (' + cast(m.codeBargeh as nvarchar)+ ')' when 4 then 'برگشت از خرید (' + cast(m.codeBargeh as nvarchar)+ ')' when 5 then '' when 6 then '' when 7 then '' when 8 then '' end as [sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], case m.noe when 0 then isnull ( cast((select sum(d.mablagh * d.Meghdar1 * d.Meghdar2 * d.tedad - isnull(d.MablagheTakhfif , 0)) from BargehDetail d where d.codeBargeh = m.code) as decimal) ,0.0) - isnull ( cast((select sum(d.mablagh) from DaryaftPardakht d where d.codeBargehParent = m.code and d.noeMaster = 2 and d.vaziat != 1) as decimal) ,0.0) + isnull ( cast((select sum(d.mablagh) from DaryaftPardakht d where d.codeBargehParent = m.code and d.noeMaster = 4 and d.vaziat != 1) as decimal) ,0.0) when 1 then 0.0 when 2 then 0.0 when 3 then 0.0 when 4 then isnull ( cast((select sum(d.mablagh * d.Meghdar1 * d.Meghdar2 * d.tedad - isnull(d.MablagheTakhfif,0)) from BargehDetail d where d.codeBargeh = m.code) as decimal) ,0.0) - isnull ( cast((select sum(d.mablagh) from DaryaftPardakht d where d.codeBargehParent = m.code and d.noeMaster = 2 and d.vaziat != 1) as decimal) ,0.0) + isnull ( cast((select sum(d.mablagh) from DaryaftPardakht d where d.codeBargehParent = m.code and d.noeMaster = 4 and d.vaziat != 1) as decimal) ,0.0) when 5 then 0.0 when 6 then 0.0 when 7 then 0.0 when 8 then 0.0 end as [Bedehkar], case m.noe when 0 then 0.0 when 1 then isnull ( cast((select sum(d.mablagh * d.Meghdar1 * d.Meghdar2 * d.tedad - isnull(d.MablagheTakhfif , 0)) from BargehDetail d where d.codeBargeh = m.code) as decimal) ,0.0) - isnull ( cast((select sum(d.mablagh) from DaryaftPardakht d where d.codeBargehParent = m.code and d.noeMaster = 4 and d.vaziat != 1) as decimal) ,0.0) + isnull ( cast((select sum(d.mablagh) from DaryaftPardakht d where d.codeBargehParent = m.code and d.noeMaster = 2 and d.vaziat != 1) as decimal) ,0.0) when 2 then 0.0 when 3 then isnull ( cast((select sum(d.mablagh * d.Meghdar1 * d.Meghdar2 * d.tedad - isnull(d.MablagheTakhfif , 0)) from BargehDetail d where d.codeBargeh = m.code) as decimal) ,0.0) - isnull ( cast((select sum(d.mablagh) from DaryaftPardakht d where d.codeBargehParent = m.code and d.noeMaster = 4 and d.vaziat != 1) as decimal) ,0.0) + isnull ( cast((select sum(d.mablagh) from DaryaftPardakht d where d.codeBargehParent = m.code and d.noeMaster = 2 and d.vaziat != 1) as decimal) ,0.0) when 4 then 0.0 when 5 then 0.0 when 6 then 0.0 when 7 then 0.0 when 8 then 0.0 end as [Bestankar], 0.0 as [Mandeh], ''[Mahiat], case m.noe when 0 then 12 --Foroosh when 1 then 15 --BargashtAzForoosh when 3 then 13 -- Kharid when 4 then 14 -- BargashtAzKharid end as [NoeRadif], m.code[code] from Bargeh m where m.Vaziat !=1 and cast(m.CodeMoshtari as int) = @pcode and @NoeGardesh = 0 union all select ------------------------------------------------------------------------- ويزيتوري کل فاکتور 0[Radif], b.Tarikh[tarikh], b.SaatSabt[Saat], case b.Noe when 0 then 'ويزيتوري فروش (' + cast( b.CodeBargeh as nvarchar) + ')' when 1 then 'ويزيتوري برگشت از فروش (' + cast( b.CodeBargeh as nvarchar) + ')' when 2 then 'ويزيتوري پيش فاکتور فروش (' + cast( b.CodeBargeh as nvarchar) + ')' when 3 then 'ويزيتوري خريد (' + cast( b.CodeBargeh as nvarchar) + ')' when 4 then 'ويزيتوري برگشت از خريد (' + cast( b.CodeBargeh as nvarchar) + ')' when 5 then 'ويزيتوري پيش فاکتور خريد (' + cast( b.CodeBargeh as nvarchar) + ')' when 6 then 'ويزيتوري حواله انبار (' + cast( b.CodeBargeh as nvarchar) + ')' when 7 then 'ويزيتوري برگشت به انبار (' + cast( b.CodeBargeh as nvarchar) + ')' when 8 then 'ويزيتوري انتقال بين انبار (' + cast( b.CodeBargeh as nvarchar) + ')' end as[Sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], 0.0 [BedehKar], b.MablagheVisitor[BestanKar], 0.0 [Mandeh], ''[Mahiat], case b.noe when 0 then 12 --Foroosh when 1 then 15 --BargashtAzForoosh when 3 then 13 -- Kharid when 4 then 14 -- BargashtAzKharid end as [NoeRadif], b.code[Code] from Bargeh b where b.Vaziat !=1 and b.CodeVisitor = @pcode and b.MablagheVisitor is not null and b.MablagheVisitor is not null union all ------------------------------------------------------------------------- ويزيتوري در تک تک کالاها select 0[Radif], m.tarikh[Tarikh], m.SaatSabt[Saat], case m.Noe when 0 then-- 'ويزيتوري فاکتور فروش case d.isKala when 0 then -- Khadamat 'ويزيتوري فروش ('+ cast( d.CodeBargeh as nvarchar) + ') نام خدمات :'+(select name +'('+ cast (CodeHesab as nvarchar)+')' from tafsili where Code = d.CodeKhadamat2) when 1 then --Kala 'ويزيتوري فروش ('+ cast( d.CodeBargeh as nvarchar) + ') نام کالا :' + (select name +'('+ cast (CodeKala as nvarchar)+')' from Kala where Code = d.codeKala) end when 3 then --'ويزيتوري خريد case d.isKala when 1 then --Kala 'ويزيتوري خريد ('+ cast( d.CodeBargeh as nvarchar) + ') نام کالا :' + (select name +'('+ cast (CodeKala as nvarchar)+')' from Kala where Code = d.codeKala) end end as [sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], 0.0 as [Bedehkar], d.MablagheVisitor as [Bestankar], 0.0[Mandeh], ''[Mahiat], case m.noe when 0 then 12 --Foroosh when 3 then 13 -- Kharid end as [NoeRadif], m.code [Code] from (Bargeh m join BargehDetail d on m.code = d.CodeBargeh) where m.Vaziat != 1 and cast(d.CodeVisitor as int) = @pcode and d.DarsadeVisitor is not null and d.MablagheVisitor is not null union all -------------------------------------------------------------------------تخصیص خدمات فاکتور فروش select 0[Radif], m.tarikh[Tarikh], m.SaatSabt[Saat], case m.Noe when 0 then-- 'تخصیص خدمات فاکتور فروش case d.isKala when 0 then -- Khadamat 'تخصیص خدمات فروش ('+ cast( d.CodeBargeh as nvarchar) + ') نام خدمات :'+(select name +'('+ cast (CodeHesab as nvarchar)+')' from tafsili where Code = d.CodeTakhsisKhadamat) end end as [sharh], 1[Meghdar], case m.Noe when 0 then case d.isKala when 0 then (Select isnull(name , '') from vahed v where v.code = (select isnull(t.codeVahed , 0) from tafsili t where t.code = d.CodeTakhsisKhadamat)) end end [Vahed], isnull(d.MablaghTakhsisKhadamat,0)[Fi], 0.0 as [Bedehkar], d.MablaghTakhsisKhadamat as [Bestankar], 0.0[Mandeh], ''[Mahiat], case m.noe when 0 then 12 --Foroosh when 3 then 13 -- Kharid end as [NoeRadif], m.code [Code] from (Bargeh m join BargehDetail d on m.code = d.CodeBargeh) where m.Vaziat != 1 and cast(d.CodeTakhsisKhadamat as int) = @pcode and d.MablaghTakhsisKhadamat is not null and m.noe = 0 union all ------------------------------------------------------------------------------------------ اعمال مبالغ در فاکتورها select 0[Radif], (select b.tarikh from Bargeh b where b.code = m.codeBargehParent) as [Tarikh], m.SaatSabt[Saat], case (select b.Noe from Bargeh b where b.code = m.codeBargehParent) when 0 then case M.NoeDetail when 0 then 'چک طي فروش (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):' + ' شماره چک ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 1 then 'حواله بانکي طي فروش (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره حواله ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 2 then 'وجه طي فروش (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ ')'+isnull(m.sharh,'') end --when 3 then 'حواله حسابداري طي فاکتور فروش (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره حواله حسابداري ('+d.ShomarehSanad+')'+m.sharh+' '+d.sharh end when 1 then case M.NoeDetail when 0 then 'چک طي برگشت از فروش (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):' + ' شماره چک ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 1 then 'حواله بانکي طي برگشت از فروش (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره حواله ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 2 then 'وجه طي برگشت از فروش (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ ')'+isnull(m.sharh,'') end when 2 then '' when 3 then case M.NoeDetail when 0 then 'چک طي خريد ('+ cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):' + ') '+' شماره چک ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 1 then 'حواله بانکي طي خريد ('+ cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره حواله ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 2 then 'وجه نقد طي خريد ('+ cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ ')'+isnull(m.sharh,'') end --when 3 then 'حواله حسابداري طي فاکتور خريد ('+ cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره حواله حسابداري ('+d.ShomarehSanad+')'+m.sharh+' '+d.sharh end when 4 then case M.NoeDetail when 0 then 'چک طي برگشت از خرید (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):' + ' شماره چک ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 1 then 'حواله بانکي طي برگشت از خرید (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره حواله ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 2 then 'وجه طي برگشت از خرید (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ ')'+isnull(m.sharh,'') end when 5 then '' when 6 then '' when 7 then '' end as [Sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], m.mablagh as [BedehKar], 0.0 as [BestanKar], 0.0 as [Mandeh], '' [Mahiat], case (select b.Noe from Bargeh b where b.code = m.codeBargehParent) when 0 then 12 --Foroosh when 3 then 13 -- Kharid when 4 then 14 -- BargashAzKharid end as [NoeRadif], (select b.code from Bargeh b where b.code = m.codeBargehParent) as [Code] from daryaftpardakht m where m.Vaziat != 1 and m.CodeBargehParent is not null and m.CodeTafsiliBedehkar = @pcode union all select 0[Radif], (select b.tarikh from Bargeh b where b.code = m.codeBargehParent) as [Tarikh], m.SaatSabt[Saat], case (select b.Noe from Bargeh b where b.code = m.codeBargehParent) when 0 then case m.NoeDetail when 0 then 'چک طي فروش ('+ cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):' +' شماره چک ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 1 then 'حواله بانکي طي فروش (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره حواله ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 2 then 'وجه طي فروش ('+ cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ ')'+m.sharh+' '+isnull(m.sharh,'') end --when 3 then 'حواله حسابداري طي فاکتور فروش'+ cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره حواله حسابداري ('+d.ShomarehSanad+')' +m.sharh+' '+d.sharh end when 1 then case M.NoeDetail when 0 then 'چک طي برگشت از فروش (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):' + ' شماره چک ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 1 then 'حواله بانکي طي برگشت از فروش (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره حواله ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 2 then 'وجه طي برگشت از فروش (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ ')'+isnull(m.sharh,'') end when 2 then '' when 3 then case m.NoeDetail when 0 then 'جک طي خريد (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره چک ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 1 then 'حواله بانکي طي خريد (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره حواله ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 2 then 'وجه نقد طي خريد (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ ')'+isnull(m.sharh,'') end --when 3 then 'حواله حسابداري طي فاکتور خريد' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره حواله حسابداري ('+d.ShomarehSanad+')'+m.sharh+' '+d.sharh end when 4 then case M.NoeDetail when 0 then 'چک طي برگشت از خرید (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):' + ' شماره چک ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 1 then 'حواله بانکي طي برگشت از خرید (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ '):'+' شماره حواله ('+m.ShomarehSanad+')'+isnull(m.sharh,'') when 2 then 'وجه طي برگشت از خرید (' + cast((select CodeBargeh from Bargeh b where b.code = m.codeBargehParent ) as nvarchar)+ ')'+isnull(m.sharh,'') end when 5 then '' when 6 then '' when 7 then '' end as [Sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], 0.0 as [BedehKar], m.mablagh as [BestanKar], 0.0 as [Mandeh], '' [Mahiat], case (select b.Noe from Bargeh b where b.code = m.codeBargehParent) when 0 then 12 --Foroosh when 1 then 15 --BargashtAzForoosh when 3 then 13 -- Kharid when 4 then 14 -- BargashtAzKharid end as [NoeRadif], (select b.code from Bargeh b where b.code = m.codeBargehParent) as [Code] from daryaftpardakht m where m.Vaziat != 1 and m.CodeBargehParent is not null and m.CodeTafsiliBestankar = @pcode union all -------------------------------------------------------- برای خدمات و هزینه در فاکتور خرید و فروش select 0 as [Radif], m.Tarikh[tarikh], m.SaatSabt[Saat], case m.noe when 0 then --')نام خدمات :' +(select name from tafsili where Code = d.CodeKhadamat2)+'('+ cast((select CodeHesab from Tafsili where Code = d.codeKhadamat2)as nvarchar) +')' 'فروش ('+ cast(m.codeBargeh as nvarchar)+ ')' + ')نام خدمات :' +(select name from tafsili where Code = d.CodeKhadamat2)+'('+ cast((select CodeHesab from Tafsili where Code = d.codeKhadamat2)as nvarchar) +')'+isnull(d.sharheRadif,'') when 1 then 'برگشت از فروش (' + cast(m.codeBargeh as nvarchar)+ ')' + ')نام هزینه:' +(select name from tafsili where Code = d.CodeKhadamat2)+'('+ cast((select CodeHesab from Tafsili where Code = d.codeKhadamat2)as nvarchar) +')'+isnull(d.sharheRadif,'') when 2 then '' when 3 then 'خرید (' + cast(m.codeBargeh as nvarchar)+ ')' + ')نام هزینه:' +(select name from tafsili where Code = d.CodeKhadamat2)+'('+ cast((select CodeHesab from Tafsili where Code = d.codeKhadamat2)as nvarchar) +')'+isnull(d.sharheRadif,'') when 4 then 'برگشت از خرید ('+ cast(m.codeBargeh as nvarchar)+ ')' + ')نام خدمات :' +(select name from tafsili where Code = d.CodeKhadamat2)+'('+ cast((select CodeHesab from Tafsili where Code = d.codeKhadamat2)as nvarchar) +')'+isnull(d.sharheRadif,'') when 5 then '' when 6 then '' when 7 then '' when 8 then '' end as [sharh], d.Tedad[Meghdar], case m.noe when 0 then case d.IsKala when 0 then --Khadamat (select isnull(name,'') from vahed v where v.code = (select isnull(t.CodeVahed,0) from tafsili t where t.Code = d.CodeKhadamat2)) when 1 then -- Kala (select isnull(name,'') from vahed v where v.code = (select isnull(k.CodeVahed,0) from Kala k where k.Code = d.CodeKala)) end when 1 then -- BargashtAzForoosh (select isnull(name,'') from vahed v where v.code = (select isnull(k.CodeVahed,0) from Kala k where k.Code = d.CodeKala)) when 3 then (select isnull(name,'') from vahed v where v.code = (select isnull(k.CodeVahed,0) from Kala k where k.Code = d.CodeKala)) when 4 then -- BargashtAzKharid (select isnull(name,'') from vahed v where v.code = (select isnull(k.CodeVahed,0) from Kala k where k.Code = d.CodeKala)) end [Vahed], d.Mablagh[Fi], case m.noe when 0 then 0.0 when 1 then cast((d.mablagh * d.Tedad - d.MablagheTakhfif) as decimal) -- هزینه when 2 then 0.0 when 3 then cast((d.mablagh * d.Tedad - d.MablagheTakhfif) as decimal) -- هزینه when 4 then 0.0 when 5 then 0.0 when 6 then 0.0 when 7 then 0.0 when 8 then 0.0 end as [Bedehkar], case m.noe when 0 then cast((d.mablagh * d.Tedad - d.MablagheTakhfif) as decimal) -- خدمات - case when d.codetakhsisKhadamat is not null and (select noe from tafsili where code=@pcode)=4 then d.mablaghtakhsisKhadamat else 0 end when 1 then 0.0 when 2 then 0.0 when 3 then 0.0 when 4 then 0.0 when 5 then 0.0 when 6 then 0.0 when 7 then 0.0 when 8 then 0.0 end as [Bestankar], 0.0 as [Mandeh], ''[Mahiat], case m.noe when 0 then 12 --Foroosh when 3 then 13 -- Kharid end as [NoeRadif], m.code[code] from (Bargeh m join BargehDetail d on m.code = d.CodeBargeh) where m.Vaziat <> 1 and cast(d.CodeKhadamat2 as int) = @pcode -------------------------------------------------------- گردش چک ها union all select -------------------------------------------------------- گردش چک دریافتی 0[Radif], case @NoeTaf when 1 then --Bank (select g.tarikh from GardesheChek g where g.Code = (select max(gg.code) from GardesheChek gg where gg.CodeParent = m.code)) when 3 then --sandoogh m.Tarikh end as [Tarikh], d.Saat[Saat], case @NoeTaf when 1 then --Bank case m.VaziateChek when 0 then 'دریافت چک ('+cast(m.shomarehSanad as nvarchar)+') '+'در جریان وصول '+(select name from tafsili where code= d.CodeBestanKar) when 1 then 'برگشت چک ('+cast(m.shomarehSanad as nvarchar)+') '+'از '+(select name from tafsili where code= d.CodeBestanKar) when 2 then 'چک پاک شده ('+cast(m.shomarehSanad as nvarchar)+') ' when 3 then 'چک خرج شده ('+cast(m.shomarehSanad as nvarchar)+') '+(select name from tafsili where code= d.CodeBestanKar) when 4 then 'چک موجود ('+cast(m.shomarehSanad as nvarchar)+') ' when 5 then 'چک ('+cast(m.shomarehSanad as nvarchar)+') '+'برگشت به مشتری '+(select name from tafsili where code= d.CodeBestanKar) when 6 then 'چک پاس شده ('+cast(m.shomarehSanad as nvarchar)+') ' when 7 then 'چک برگشت از مشتری ('+cast(m.shomarehSanad as nvarchar)+') ' when 8 then 'دریافت نقدی از مشتری ('+cast(m.shomarehSanad as nvarchar)+') ' when 9 then 'پس دادن چک برگشتی ('+cast(m.shomarehSanad as nvarchar)+') ' when 10 then 'نقد شدن چک برگشتی ('+cast(m.shomarehSanad as nvarchar)+') ' end when 3 then --Sandoogh case d.Vaziat when 0 then 'دریافت چک ('+cast(m.shomarehSanad as nvarchar)+') '+'در جریان وصول '+(select name from tafsili where code= d.CodeBestanKar) when 1 then 'برگشت چک ('+cast(m.shomarehSanad as nvarchar)+') '+'از '+(select name from tafsili where code= d.CodeBestanKar) when 2 then 'چک پاک شده ('+cast(m.shomarehSanad as nvarchar)+') ' when 3 then 'چک خرج شده ('+cast(m.shomarehSanad as nvarchar)+') '+(select name from tafsili where code= d.CodeBestanKar) when 4 then 'چک موجود ('+cast(m.shomarehSanad as nvarchar)+') ' when 5 then 'چک ('+cast(m.shomarehSanad as nvarchar)+') '+'برگشت به مشتری '+(select name from tafsili where code= d.CodeBestanKar) when 6 then 'چک پاس شده ('+cast(m.shomarehSanad as nvarchar)+') ' when 7 then 'چک برگشت از مشتری ('+cast(m.shomarehSanad as nvarchar)+') ' when 8 then 'دریافت نقدی از مشتری ('+cast(m.shomarehSanad as nvarchar)+') ' when 9 then 'پس دادن چک برگشتی ('+cast(m.shomarehSanad as nvarchar)+') ' when 10 then 'نقد شدن چک برگشتی ('+cast(m.shomarehSanad as nvarchar)+') ' end end as [Sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], m.mablagh as [BedehKar], 0.0 as [BestanKar], 0.0 as [Mandeh], '' [Mahiat], case m.NoeMaster when 0 then -- گردش چک دریافتی case d.Vaziat when 0 then 31 --GardeshChekDaryaftiDarJaryanVosool when 1 then 32 --GardeshChekDaryaftiBargashtChek when 2 then 33 --GardeshChekDaryaftiDeleted when 3 then 34 --GardeshChekDaryaftiKharjShodeh when 4 then 35 --GardeshChekDaryaftiMojood when 5 then 36 --GardeshChekDaryaftiBargashtBeMoshtari when 6 then 37 --GardeshChekDaryaftiPassShodeh --when 7 then -- در پرداخت کاربرد دارد when 8 then 38 --GardeshChekDaryaftiDaryaftNaghdi when 9 then 39 --GardeshChekDaryaftiPasDadanChekBargashti when 10 then 40 --GardeshChekDaryaftiNaghdShodanChekBargashti else 1000 end when 1 then -- گردش چک پرداختی case d.Vaziat when 0 then 21 --GardeshChekPardakhtiDarJaryanVosool when 1 then 22 --GardeshChekPardakhtiBargashtChek when 6 then 23 --GardeshChekPardakhtiPassShodan when 7 then 24 --GardeshChekPardakhtiBargashtAzMoshtari else 1000 end end as [NoeRadif], -- -1[NoeRadif], m.Code [Code] from daryaftpardakht m join GardesheChek d on m.code = d.codeParent where m.Vaziat != 1 and d.CodeBedehKar = @pcode union all select -------------------------------------------------------- گردش چک پرداختی 0[Radif], case @NoeTaf when 1 then --Bank (select g.tarikh from GardesheChek g where g.Code = (select max(gg.code) from GardesheChek gg where gg.CodeParent = m.code)) when 3 then --sandoogh m.Tarikh end as [Tarikh], d.Saat[Saat], case @NoeTaf when 1 then --Bank case m.VaziateChek when 0 then 'دریافت چک ('+cast(m.shomarehSanad as nvarchar)+') '+'در جریان وصول '+(select name from tafsili where code= d.CodeBestanKar) when 1 then 'برگشت چک ('+cast(m.shomarehSanad as nvarchar)+') '+'از '+(select name from tafsili where code= d.CodeBestanKar) when 2 then 'چک پاک شده ('+cast(m.shomarehSanad as nvarchar)+') ' when 3 then 'چک خرج شده ('+cast(m.shomarehSanad as nvarchar)+') '+(select name from tafsili where code= d.CodeBestanKar) when 4 then 'چک موجود ('+cast(m.shomarehSanad as nvarchar)+') ' when 5 then 'چک ('+cast(m.shomarehSanad as nvarchar)+') '+'برگشت به مشتری '+(select name from tafsili where code= d.CodeBestanKar) when 6 then 'چک پاس شده ('+cast(m.shomarehSanad as nvarchar)+') ' when 7 then 'چک برگشت از مشتری ('+cast(m.shomarehSanad as nvarchar)+') ' when 8 then 'دریافت نقدی از مشتری ('+cast(m.shomarehSanad as nvarchar)+') ' when 9 then 'پس دادن چک برگشتی ('+cast(m.shomarehSanad as nvarchar)+') ' when 10 then 'نقد شدن چک برگشتی ('+cast(m.shomarehSanad as nvarchar)+') ' end when 3 then --Sandoogh case d.Vaziat when 0 then 'دریافت چک ('+cast(m.shomarehSanad as nvarchar)+') '+'در جریان وصول '+(select name from tafsili where code= d.CodeBestanKar) when 1 then 'برگشت چک ('+cast(m.shomarehSanad as nvarchar)+') '+'از '+(select name from tafsili where code= d.CodeBestanKar) when 2 then 'چک پاک شده ('+cast(m.shomarehSanad as nvarchar)+') ' when 3 then 'چک خرج شده ('+cast(m.shomarehSanad as nvarchar)+') '+(select name from tafsili where code= d.CodeBestanKar) when 4 then 'چک موجود ('+cast(m.shomarehSanad as nvarchar)+') ' when 5 then 'چک ('+cast(m.shomarehSanad as nvarchar)+') '+'برگشت به مشتری '+(select name from tafsili where code= d.CodeBestanKar) when 6 then 'چک پاس شده ('+cast(m.shomarehSanad as nvarchar)+') ' when 7 then 'چک برگشت از مشتری ('+cast(m.shomarehSanad as nvarchar)+') ' when 8 then 'دریافت نقدی از مشتری ('+cast(m.shomarehSanad as nvarchar)+') ' when 9 then 'پس دادن چک برگشتی ('+cast(m.shomarehSanad as nvarchar)+') ' when 10 then 'نقد شدن چک برگشتی ('+cast(m.shomarehSanad as nvarchar)+') ' end end as [Sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], 0.0 as [BedehKar], m.mablagh as [BestanKar], 0.0 as [Mandeh], '' [Mahiat], case m.NoeMaster when 0 then -- گردش چک دریافتی case d.Vaziat when 0 then 31 --GardeshChekDaryaftiDarJaryanVosool when 1 then 32 --GardeshChekDaryaftiBargashtChek when 2 then 33 --GardeshChekDaryaftiDeleted when 3 then 34 --GardeshChekDaryaftiKharjShodeh when 4 then 35 --GardeshChekDaryaftiMojood when 5 then 36 --GardeshChekDaryaftiBargashtBeMoshtari when 6 then 37 --GardeshChekDaryaftiPassShodeh --when 7 then -- در پرداخت کاربرد دارد when 8 then 38 --GardeshChekDaryaftiDaryaftNaghdi when 9 then 39 --GardeshChekDaryaftiPasDadanChekBargashti when 10 then 40 --GardeshChekDaryaftiNaghdShodanChekBargashti else 1000 end when 1 then -- گردش چک پرداختی case d.Vaziat when 0 then 21 --GardeshChekPardakhtiDarJaryanVosool when 1 then 22 --GardeshChekPardakhtiBargashtChek when 6 then 23 --GardeshChekPardakhtiPassShodan when 7 then 24 --GardeshChekPardakhtiBargashtAzMoshtari else 1000 end end as [NoeRadif], -- -1[NoeRadif], m.Code [Code] from daryaftpardakht m join GardesheChek d on m.code = d.codeParent where m.Vaziat != 1 and d.CodeBestanKar = @pcode union all select ------------------------------------------- دریافت اول دوره 0[Radif], m.tarikh[Tarikh], m.SaatSabt[Saat], 'دریافت چک اول دوره ('+ cast(m.code as nvarchar) + '):'+'به شماره ('+m.ShomarehSanad+')'+ (select name from tafsili where code = m.CodeTafsiliBestankar) + isnull(m.sharh,'') as [sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], m.mablagh as [Bedehkar], 0.0 as [Bestankar], 0.0[Mandeh], ''[Mahiat], 1[NoeRadif], m.code [Code] from daryaftpardakht m where m.Vaziat != 1 and m.CodeBargehParent is null and m.IsAvalDoreh = 1 and m.NoeMaster = 0 and -- Daryaft m.NoeDetail = 0 and -- Chek m.CodeTafsiliBedehkar = @pcode union all select ------------------------------------------- پرداخت اول دوره 0[Radif], m.tarikh[Tarikh], m.SaatSabt[Saat], 'پرداخت چک اول دوره ('+ cast(m.code as nvarchar) + '):'+'به شماره ('+m.ShomarehSanad+')'+(select name from tafsili where code = m.CodeTafsiliBedehkar) + isnull(m.sharh,'') as [sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], 0.0 as [Bedehkar], m.mablagh as [Bestankar], 0.0[Mandeh], ''[Mahiat], 5[NoeRadif], m.code [Code] from daryaftpardakht m where m.Vaziat != 1 and m.CodeBargehParent is null and m.IsAvalDoreh = 1 and m.NoeMaster = 1 and -- Daryaft m.NoeDetail = 0 and -- Chek m.CodeTafsiliBestankar = @pcode union all select ------------------------------------------- تخفیفات برگه دیتیل 0 as [Radif], m.Tarikh[tarikh], m.SaatSabt[Saat], case m.noe when 0 then 'تخفیف فروش ('+ cast(m.codeBargeh as nvarchar)+ ')' + isnull(d.sharheRadif,'') when 1 then 'تخفیف برگشت از فروش ('+ cast(m.codeBargeh as nvarchar)+ ')' + isnull(d.sharheRadif,'') when 2 then '' when 3 then 'تخفیف خرید (' + cast(m.codeBargeh as nvarchar)+ ')' +isnull(d.sharheRadif,'') when 4 then 'تخفیف برگشت از خرید ('+ cast(m.codeBargeh as nvarchar)+ ')' + isnull(d.sharheRadif,'') when 5 then '' when 6 then '' when 7 then '' when 8 then '' end as [sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], case m.noe when 0 then d.mablagheTakhfif when 4 then d.mablagheTakhfif else 0.0 end as [Bedehkar], case m.noe when 3 then d.mablagheTakhfif else 0.0 end as [Bestankar], 0.0 as [Mandeh], ''[Mahiat], case m.noe when 0 then 12 --Foroosh when 1 then 15 -- BargashtAzForoosh when 3 then 13 -- Kharid when 4 then 14 -- BargashtAzKharid end as [NoeRadif], m.code[code] from (Bargeh m join BargehDetail d on m.code = d.CodeBargeh) where m.Vaziat <> 1 and cast(d.CodeTakhfif as int) = @pcode union all select --------------------------------------------------------------------------- اضافات و تخفیفات فاکتور فروش و خرید 0 as [Radif], m.Tarikh[tarikh], m.SaatSabt[Saat], case m.noe when 0 then 'تخفیف فروش (' + cast(m.codeBargeh as nvarchar)+ ')' when 1 then 'اضافات برگشت از فروش (' + cast(m.codeBargeh as nvarchar)+ ')' when 2 then '' when 3 then 'اضافات خرید (' + cast(m.codeBargeh as nvarchar)+ ')' when 4 then 'تخفیف برگشت از خرید (' + cast(m.codeBargeh as nvarchar)+ ')' when 5 then '' when 6 then '' when 7 then '' when 8 then '' end as [sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], case m.noe when 0 then 0.0 when 1 then 0.0 when 2 then 0.0 when 3 then 0.0 when 4 then 0.0 when 5 then 0.0 when 6 then 0.0 when 7 then 0.0 when 8 then 0.0 end as [Bedehkar], case m.noe when 0 then cast((d.mablagh) as decimal) when 1 then cast((d.mablagh) as decimal) when 2 then 0.0 when 3 then cast((d.mablagh) as decimal) when 4 then cast((d.mablagh) as decimal) when 5 then 0.0 when 6 then 0.0 when 7 then 0.0 when 8 then 0.0 end as [Bestankar], 0.0 as [Mandeh], ''[Mahiat], case m.noe when 0 then 12 --Foroosh when 1 then 15 --BargashtAzForoosh when 3 then 13 -- Kharid when 4 then 14 -- BargashtAzKharid end as [NoeRadif], m.code[code] from (Bargeh m join daryaftPardakht d on m.code = d.CodeBargehParent) where m.Vaziat !=1 and @NoeGardesh = 1 and d.NoeMaster = 2 and cast(m.CodeMoshtari as int) = @pcode union all select --------------------------------------------------------------------------- اضافات و تخفیفات فاکتور فروش و خرید 0 as [Radif], m.Tarikh[tarikh], m.SaatSabt[Saat], case m.noe when 0 then 'اضافات فروش (' + cast(m.codeBargeh as nvarchar)+ ')' when 1 then 'تخفیف برگشت از فروش (' + cast(m.codeBargeh as nvarchar)+ ')' when 2 then '' when 3 then 'تخفیف خرید (' + cast(m.codeBargeh as nvarchar)+ ')' when 4 then 'اضافات برگشت از خرید (' + cast(m.codeBargeh as nvarchar)+ ')' when 5 then '' when 6 then '' when 7 then '' when 8 then '' end as [sharh], 0[Meghdar], ''[Vahed], 0.0[Fi], case m.noe when 0 then cast((d.mablagh) as decimal) when 1 then cast((d.mablagh) as decimal) when 2 then 0.0 when 3 then cast((d.mablagh) as decimal) when 4 then cast((d.mablagh) as decimal) when 5 then 0.0 when 6 then 0.0 when 7 then 0.0 when 8 then 0.0 end as [Bedehkar], case m.noe when 0 then 0.0 when 1 then 0.0 when 2 then 0.0 when 3 then 0.0 when 4 then 0.0 when 5 then 0.0 when 6 then 0.0 when 7 then 0.0 when 8 then 0.0 end as [Bestankar], 0.0 as [Mandeh], ''[Mahiat], case m.noe when 0 then 12 --Foroosh when 1 then 15 --BargashtAzForoosh when 3 then 13 -- Kharid when 4 then 14 -- BargashtAzKharid end as [NoeRadif], m.code[code] from (Bargeh m join daryaftPardakht d on m.code = d.CodeBargehParent) where m.Vaziat !=1 and @NoeGardesh = 1 and d.NoeMaster = 4 and cast(m.CodeMoshtari as int) = @pcode union all select ------------------------------------------- تخفیف در گردش چک 0[Radif], g.Tarikh[Tarikh], g.Saat[Saat], 'هزینه پاس چک (' +d.shomarehSanad +')' [Sharh], 1[Meghdar], (select isnull(name,'') from vahed v where v.code = (select t.CodeVahed from Tafsili t where t.code = g.CodeHazineh))[Vahed], g.Mablagh[Fi], g.mablagh as [Bedehkar], 0.0 as [Bestankar], 0.0[Mandeh], ''[Mahiat], case d.noeMaster when 0 then 37 when 0 then 23 end [NoeRadif], g.codeParent[Code] from gardesheChek g join DaryaftPardakht d on d.code = g.codeParent where cast(g.codeHazineh as int) = @pcode and d.vaziat != 1 and d.noeDetail = 0 union all select ------------------------------------------- هزینه ویزیتوزی ها 0 as [Radif], m.Tarikh[tarikh], m.SaatSabt[Saat], case m.noe when 0 then 'ویزیتوری فروش ('+ cast(m.codeBargeh as nvarchar)+ ')' + isnull(d.sharheRadif,'') when 1 then 'ویزیتوری برگشت از فروش ('+ cast(m.codeBargeh as nvarchar)+ ')' + isnull(d.sharheRadif,'') when 2 then '' when 3 then 'ویزیتوری خرید (' + cast(m.codeBargeh as nvarchar)+ ')' +isnull(d.sharheRadif,'') when 4 then 'ویزیتوری برگشت از خرید ('+ cast(m.codeBargeh as nvarchar)+ ')' + isnull(d.sharheRadif,'') when 5 then '' when 6 then '' when 7 then '' when 8 then '' end as [sharh], 0[Meghdar], ''[Vahed], d.MablagheVisitor[Fi], d.MablagheVisitor as [Bedehkar], 0.0 [Bestankar], 0.0 as [Mandeh], ''[Mahiat], case m.noe when 0 then 12 --Foroosh when 1 then 15 -- BargashtAzForoosh when 3 then 13 -- Kharid when 4 then 14 -- BargashtAzKharid end as [NoeRadif], m.code[code] from (Bargeh m join BargehDetail d on m.code = d.CodeBargeh) where m.Vaziat <> 1 and cast(d.CodeFarayandVisitor as int) = @pcode order by tarikh , Saat end GO