

IF not EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[T_NoReportUser]') AND type in (N'U'))

Begin
CREATE TABLE dbo.T_NoReportUser
	(
	FldKey int NOT NULL IDENTITY (1, 1),
	UserID int NULL,
	GOID int NULL,
	KOLID int NULL,
	HESABID int NULL,
	THESABID int NULL
	)  ON [PRIMARY]


ALTER TABLE dbo.T_NoReportUser ADD CONSTRAINT
	PK_T_NoReportUser PRIMARY KEY CLUSTERED 
	(
	FldKey
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]


ALTER TABLE dbo.T_NoReportUser SET (LOCK_ESCALATION = TABLE)


end


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_NoReportUser'))

ALTER TABLE dbo.T_NoReportUser ADD
	userCreate int NULL,
	userModify int NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'NameCar' AND Object_ID = Object_ID(N'T_NameKala'))

ALTER TABLE dbo.T_NameKala  ADD
	NameCar NVARCHAR(250) NULL,
	Pelak NVARCHAR(250) NULL,
	Color NVARCHAR(250) NULL,
	ModelCar NVARCHAR(250) NULL,
	ShMotor NVARCHAR(250) NULL,
	CodeMF NVARCHAR(250) NULL,
	CodeMKh NVARCHAR(250) NULL,
	TelF NVARCHAR(250) NULL,
	TelKh NVARCHAR(250) NULL,
	Maref NVARCHAR(250) NULL,
	Address_s NVARCHAR(250) NULL,
  ForShandeh NVARCHAR(250) NULL
 ,KhariDar NVARCHAR(250) NULL
GO

IF not EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[T_RelateKala]') AND type in (N'U'))
	CREATE TABLE [dbo].[T_RelateKala](
	[Kalaid2] [int] IDENTITY(1,1) NOT NULL,
	[KalaidRelate] [int] NULL,
	[NameKala2] [nvarchar](255) NULL,
	[ZaribKala] [decimal](19, 4) NULL
) 
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_Asnad_Dar'))

ALTER TABLE dbo.T_Asnad_Dar ADD
	userCreate int NULL,
	userModify int NULL
GO



IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'UnitPriceKha' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	UnitPriceKha DECIMAL(19,4) NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'CodeMelli' AND Object_ID = Object_ID(N'T_T_Heasb'))

ALTER TABLE dbo.T_T_Heasb ADD
	CodeMelli NVARCHAR(255) NULL
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'FieldT1' AND Object_ID = Object_ID(N'T_GropHesab'))

ALTER TABLE dbo.T_GropHesab ADD
	FieldT1 NVARCHAR(255) NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_Asnad'))

ALTER TABLE dbo.T_Asnad ADD
	userCreate int NULL,
	userModify int NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_SA2'))

ALTER TABLE dbo.T_SA2 ADD
	userCreate int NULL,
	userModify int NULL
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_Kharid'))

ALTER TABLE dbo.T_Kharid ADD
	userCreate int NULL,
	userModify int NULL
GO
IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	userCreate int NULL,
	userModify int NULL
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'SarGOID' AND Object_ID = Object_ID(N'T_GropHesab'))

ALTER TABLE dbo.T_GropHesab ADD
	SarGOID int NULL
GO



IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'KeraiehHaml' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	KeraiehHaml DECIMAL(19,4) NULL,
	keraiehbargiri DECIMAL(19,4) NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_PishFactor'))

ALTER TABLE dbo.T_PishFactor ADD
	userCreate int NULL,
	userModify int NULL
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'RefForoshid' AND Object_ID = Object_ID(N'T_PishFactor'))

ALTER TABLE dbo.T_PishFactor ADD
	RefForoshid int NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_Dar8'))

ALTER TABLE dbo.T_Dar8 ADD
	userCreate int NULL,
	userModify int NULL
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'GOIDDeh' AND Object_ID = Object_ID(N'T_Heasb'))

ALTER TABLE dbo.T_Heasb ADD
	GOIDDeh int NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'KolIDDeh' AND Object_ID = Object_ID(N'T_Heasb'))

ALTER TABLE dbo.T_Heasb ADD
	KolIDDeh int NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'HesabidDeh' AND Object_ID = Object_ID(N'T_Heasb'))

ALTER TABLE dbo.T_Heasb ADD
	HesabidDeh int NULL
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabidDeh' AND Object_ID = Object_ID(N'T_Heasb'))

ALTER TABLE dbo.T_Heasb ADD
	ThesabidDeh int NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_Par7'))

ALTER TABLE dbo.T_Par7 ADD
	userCreate int NULL,
	userModify int NULL
GO


/****** Object:  View [dbo].[QSubPfrm_ListKharid]    Script Date: 07/03/2017 09:25:59 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_ListKharid]'))
DROP VIEW [dbo].[QSubPfrm_ListKharid]
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_Dar1'))

ALTER TABLE dbo.T_Dar1 ADD
	userCreate int NULL,
	userModify int NULL
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_Par1'))

ALTER TABLE dbo.T_Par1 ADD
	userCreate int NULL,
	userModify int NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	userCreate int NULL,
	userModify int NULL
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'Table1'))

ALTER TABLE dbo.Table1 ADD
	userCreate int NULL,
	userModify int NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'NameBakhsh' AND Object_ID = Object_ID(N'T_Heasb'))

ALTER TABLE dbo.T_Heasb ADD
	NameBakhsh nvarchar(255) NULL,
	NameZihesab nvarchar(255) NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'NoeOperation' AND Object_ID = Object_ID(N'[TRANSACTION]'))

ALTER TABLE dbo.[TRANSACTION] ADD
	NoeOperation int NULL,
	IDOperation int NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'NoeOperation' AND Object_ID = Object_ID(N'T_Par1'))

ALTER TABLE dbo.T_Par1 ADD
	NoeOperation int NULL,
	IDOperation int NULL
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'boat_Sh_Sabt' AND Object_ID = Object_ID(N'T_T_Heasb'))

ALTER TABLE dbo.T_T_Heasb ADD
	boat_Sh_Sabt nvarchar(255) NULL,
	boat_Sh_Motor nvarchar(255) NULL,
	boat_Kamat nvarchar(255) NULL,
	boat_Kamat_Phone nvarchar(255) NULL,
	boat_CodeMelli nvarchar(255) NULL,
	boat_Pedar nvarchar(255) NULL
	
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'boat_CodeMelli' AND Object_ID = Object_ID(N'T_T_Heasb'))

ALTER TABLE dbo.T_T_Heasb ADD
	boat_CodeMelli nvarchar(255) NULL,
	boat_Pedar nvarchar(255) NULL
	
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_TRANSACTION2_TMP_Forosh_Sandogh]') AND parent_object_id = OBJECT_ID(N'[dbo].[TRANSACTION2]'))
ALTER TABLE [dbo].[TRANSACTION2] DROP CONSTRAINT [FK_TRANSACTION2_TMP_Forosh_Sandogh]
GO

IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_TRANSACTION2_FieldN7]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[TRANSACTION2] DROP CONSTRAINT [DF_TRANSACTION2_FieldN7]
END

GO


/****** Object:  Table [dbo].[TRANSACTION2]    Script Date: 09/10/2017 14:00:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TRANSACTION2]') AND type in (N'U'))
DROP TABLE [dbo].[TRANSACTION2]
GO


/****** Object:  Table [dbo].[TRANSACTION2]    Script Date: 09/10/2017 14:00:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[TRANSACTION2](
	[TRID] [int] IDENTITY(1,1) NOT NULL,
	[TRAID] [int] NULL,
	[SAID2] [int] NULL,
	[RadifSAID2] [int] NULL,
	[TextNHSID2] [nvarchar](255) NULL,
	[TextShSA] [ntext] NULL,
	[HESABID] [int] NULL,
	[GOID] [int] NULL,
	[KOLID] [int] NULL,
	[THESABID] [int] NULL,
	[ThesabIDSath2] [int] NULL,
	[MOINID] [int] NULL,
	[KALAID] [int] NULL,
	[NAMEKHID] [int] NULL,
	[AID] [int] NULL,
	[GAID] [int] NULL,
	[TedadVA] [decimal](19, 4) NULL,
	[TedadKhA] [decimal](19, 4) NULL,
	[VKALAID] [int] NULL,
	[ModelKala] [nvarchar](255) NULL,
	[SAID] [int] NULL,
	[TextNumberSA] [int] NULL,
	[GOKID] [int] NULL,
	[PARBes] [money] NULL,
	[DARBed] [money] NULL,
	[TEMP] [money] NULL,
	[TEMPR] [nvarchar](255) NULL,
	[TempMandeh] [money] NULL,
	[Sharh] [ntext] NULL,
	[radif] [int] NULL,
	[Date1] [nvarchar](255) NULL,
	[BANKID] [int] NULL,
	[KHOROJID] [int] NULL,
	[TextAIDKh] [nvarchar](255) NULL,
	[TextNumberKHO] [int] NULL,
	[TedadKhorooj] [decimal](19, 4) NULL,
	[VORODID] [int] NULL,
	[TextAID] [nvarchar](255) NULL,
	[TextNumberVO] [int] NULL,
	[TedadList] [decimal](19, 4) NULL,
	[TedadVorood] [decimal](19, 4) NULL,
	[SHomareKOtag] [nvarchar](255) NULL,
	[SHomarePArvaneh] [nvarchar](255) NULL,
	[SHomarePAking] [nvarchar](255) NULL,
	[SHoKAmyon] [nvarchar](255) NULL,
	[SHomareLAng] [nvarchar](255) NULL,
	[PFOROSHID] [int] NULL,
	[FOROSHID] [int] NULL,
	[SForoshID] [int] NULL,
	[TextAIDFo] [nvarchar](255) NULL,
	[TextNumberFO] [int] NULL,
	[TedadFo] [decimal](19, 4) NULL,
	[UnitPriceFo] [money] NULL,
	[PKHARIDID] [int] NULL,
	[KHARIDID] [int] NULL,
	[TextAIDKharid] [nvarchar](255) NULL,
	[TextNumberKH] [int] NULL,
	[Tedad] [decimal](19, 4) NULL,
	[UnitPrice] [money] NULL,
	[SumofPrice] [money] NULL,
	[BARFOROSHID] [int] NULL,
	[TedadBarFo] [decimal](19, 4) NULL,
	[UnitPriceBarFo] [money] NULL,
	[BARKHARIDID] [int] NULL,
	[TedadBarKh] [decimal](19, 4) NULL,
	[UnitPriceBarKh] [money] NULL,
	[DKHID] [int] NULL,
	[TedadDKh] [decimal](19, 4) NULL,
	[UnitPriceDKh] [money] NULL,
	[HKHID] [int] NULL,
	[TedadHKh] [decimal](19, 4) NULL,
	[UnitPriceHKh] [money] NULL,
	[TAFOID] [int] NULL,
	[TAKHID] [int] NULL,
	[IDSHARH] [int] NULL,
	[NobatSandoghid] [int] NULL,
	[PARID1] [int] NULL,
	[PARID2] [int] NULL,
	[PARID3] [int] NULL,
	[PARID4] [int] NULL,
	[PARID5] [int] NULL,
	[PARID6] [int] NULL,
	[PARID7] [int] NULL,
	[PARID8] [int] NULL,
	[DARID1] [int] NULL,
	[DARID2] [int] NULL,
	[DARID3] [int] NULL,
	[DARID4] [int] NULL,
	[DARID5] [int] NULL,
	[DARID6] [int] NULL,
	[DARID7] [int] NULL,
	[DARID8] [int] NULL,
	[ID1] [int] NULL,
	[ID2] [int] NULL,
	[ID3] [int] NULL,
	[ID4] [int] NULL,
	[FieldN1] [int] NULL,
	[FieldN2] [money] NULL,
	[FieldN3] [money] NULL,
	[FieldN4] [money] NULL,
	[FieldN5] [money] NULL,
	[FieldN6] [money] NULL,
	[FieldN7] [money] NULL,
	[FieldT1] [nvarchar](255) NULL,
	[FieldT2] [nvarchar](255) NULL,
	[FieldT3] [nvarchar](255) NULL,
	[FieldT4] [nvarchar](255) NULL,
	[FieldT5] [nvarchar](255) NULL,
	[FieldT6] [nvarchar](255) NULL,
	[FieldT7] [nvarchar](255) NULL,
	[FieldT8] [nvarchar](255) NULL,
	[Flag1] [bit] NULL,
	[Flag2] [bit] NULL,
	[Flag3] [bit] NULL,
	[Flag4] [bit] NULL,
	[Flag5] [bit] NULL,
	[Flag6] [bit] NULL,
	[Flag7] [bit] NULL,
	[Flag8] [bit] NULL,
	[Flag9] [bit] NULL,
	[Flag10] [bit] NULL,
	[TextKalaName] [nvarchar](255) NULL,
	[LinkedID] [int] NULL,
	[OperType] [int] NULL,
	[CurMablagh] [decimal](19, 4) NULL,
	[CurRef] [int] NULL,
	[CurRate] [decimal](19, 4) NULL,
	[Tedad2] [decimal](19, 4) NULL,
	[Tedad1] [decimal](19, 4) NULL,
	[Zarib] [decimal](19, 4) NULL,
	[TedadHazine] [decimal](19, 4) NULL,
	[MabVahedHazine] [decimal](19, 4) NULL,
 CONSTRAINT [PK_TRANSACTION2] PRIMARY KEY CLUSTERED 
(
	[TRID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

EXEC sys.sp_addextendedproperty @name=N'MS_DisplayControl', @value=N'109' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TRANSACTION2', @level2type=N'COLUMN',@level2name=N'TedadHazine'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Format', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TRANSACTION2', @level2type=N'COLUMN',@level2name=N'TedadHazine'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_IMEMode', @value=N'0' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TRANSACTION2', @level2type=N'COLUMN',@level2name=N'TedadHazine'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_DisplayControl', @value=N'109' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TRANSACTION2', @level2type=N'COLUMN',@level2name=N'MabVahedHazine'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Format', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TRANSACTION2', @level2type=N'COLUMN',@level2name=N'MabVahedHazine'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_IMEMode', @value=N'0' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TRANSACTION2', @level2type=N'COLUMN',@level2name=N'MabVahedHazine'
GO

ALTER TABLE [dbo].[TRANSACTION2]  WITH NOCHECK ADD  CONSTRAINT [FK_TRANSACTION2_TMP_Forosh_Sandogh] FOREIGN KEY([SForoshID])
REFERENCES [dbo].[TMP_Forosh_Sandogh] ([FOROSHID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[TRANSACTION2] CHECK CONSTRAINT [FK_TRANSACTION2_TMP_Forosh_Sandogh]
GO

ALTER TABLE [dbo].[TRANSACTION2] ADD  CONSTRAINT [DF_TRANSACTION2_FieldN7]  DEFAULT ((0)) FOR [FieldN7]
GO

EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[TedadVA]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[TedadKhA]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[PARBes]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[DARBed]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[TEMP]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[TempMandeh]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[TedadKhorooj]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[TedadList]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[TedadVorood]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[TedadFo]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[UnitPriceFo]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[Tedad]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[UnitPrice]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[SumofPrice]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[TedadBarFo]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[UnitPriceBarFo]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[TedadBarKh]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[UnitPriceBarKh]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[TedadDKh]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[UnitPriceDKh]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[TedadHKh]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[UnitPriceHKh]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[true]', @objname=N'[dbo].[TRANSACTION2].[Flag2]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[true]', @objname=N'[dbo].[TRANSACTION2].[Flag3]' , @futureonly='futureonly'
GO
EXEC sys.sp_bindefault @defname=N'[dbo].[false]', @objname=N'[dbo].[TRANSACTION2].[CurMablagh]' , @futureonly='futureonly'
GO




/****** Object:  UserDefinedFunction [dbo].[sgfn_DateToShamsiDatePart]    Script Date: 11/27/2018 11:21:52 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sgfn_DateToShamsiDatePart]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[sgfn_DateToShamsiDatePart]
GO


/****** Object:  UserDefinedFunction [dbo].[sgfn_DateToShamsiDatePart]    Script Date: 11/27/2018 11:21:52 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

Create FUNCTION [dbo].[sgfn_DateToShamsiDatePart] (@MiDate DateTime , @ADatePart CHAR) RETURNS INT
AS
BEGIN

  Declare @TmpY int, @Leap int

  Declare @Sh_Y int , @Sh_M int , @Sh_D int, @Result int

  if @MiDate is null 

    return 0


  Set @Result = convert(int, convert(float,@MiDate))

  if @Result <= 78

  begin

    Set @Sh_Y = 1278

    Set @Sh_M = (@Result + 10) / 30 + 10 

    Set @Sh_D = (@Result + 10) % 30 + 1

  end 

  else

  begin

    Set @Result = @Result - 78

    Set @Sh_Y = 1279

    while 1 = 1 

    begin

      Set @TmpY = @Sh_Y + 11

      Set @TmpY = @TmpY - ( @TmpY / 33) * 33

      if  (@TmpY <> 32) and ( (@TmpY / 4) * 4 = @TmpY )

        Set @Leap = 1

      else

        Set @Leap = 0

      if @Result <= (365+@Leap)

        break

      Set @Result = @Result -  (365+@Leap)

      Set @Sh_Y = @Sh_Y + 1

    end

    if @Result <= 31*6

    begin

      Set @Sh_M = (@Result-1) / 31 + 1

      Set @Sh_D = (@Result-1) % 31 + 1

    end

    else

    begin

      Set @Sh_M = ((@Result-1) - 31*6) / 30 + 7

      Set @Sh_D = ((@Result-1) - 31*6) % 30 + 1

    end

  end

  return

    case @ADatePart

      when 'Y' then  @Sh_Y

      when 'M' then @Sh_M

      when 'D' then @Sh_D

    else  0

    end

end

GO




/****** Object:  UserDefinedFunction [dbo].[sgfn_DateToShamsiDate]    Script Date: 11/27/2018 11:21:48 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sgfn_DateToShamsiDate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[sgfn_DateToShamsiDate]
GO



/****** Object:  UserDefinedFunction [dbo].[sgfn_DateToShamsiDate]    Script Date: 11/27/2018 11:21:48 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[sgfn_DateToShamsiDate](@ChirsDate SmallDateTime) returns Char(10)  as

begin

  declare @SolarDate char(10)

  declare @Day  Char(2)

  declare @Mon Char(2)

  declare @SDay  Int

  declare @SMon Int

  declare @SYear Int

  set @SYear = dbo.sgfn_DateToShamsiDatePart(@ChirsDate, 'Y')

  set @SMon = dbo.sgfn_DateToShamsiDatePart(@ChirsDate, 'M')

  set @SDay = dbo.sgfn_DateToShamsiDatePart(@ChirsDate, 'D')


  if @SMon <= 9

    select @Mon = '0'+Convert(Char(1),@SMon)

  else

    select @Mon = Convert(Char(2),@SMon)

  if @SDay <= 9

    select @Day = '0'+Convert(Char(1),@SDay)

  else

    select @Day = Convert(Char(2),@SDay)

  select  @SolarDate = SubString(Convert(Char(4),@SYear),1,4)+'/'+@Mon+'/'+@Day

  return @SolarDate

end

GO



IF  not EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Tbl_Device]') AND type in (N'U'))
CREATE TABLE [dbo].[Tbl_Device](
	[IDAuto] [int] IDENTITY(1,1) NOT NULL,
	[GOID] [int] NULL,
	[KOLID] [int] NULL,
	[HESABID] [int] NULL,
	[THESABID] [int] NULL,
	[NameHesab] [nvarchar](255) NULL,
	[ShSerial] [nvarchar](255) NULL,
	[TxtAddress] [nvarchar](255) NULL,
	[NoeDevice] [nvarchar](255) NULL,
	[dateStart] [nvarchar](10) NULL,
	[dateEnd] [nvarchar](10) NULL,
	[DateCeate] [smalldatetime] NULL,
	[DateModified] [smalldatetime] NULL,
	[UserCreated] [int] NULL,
	[UserModified] [int] NULL,
	[Flag1] [bit] NULL,
	[Flag2] [bit] NULL,
	[Flag3] [bit] NULL,
	[Flag4] [bit] NULL,
	[Flag5] [bit] NULL,
	[TxtNote] [nvarchar](255) NULL,
	[ShPigiri] [nvarchar](255) NULL,
	[DatePigiri] [nvarchar](10) NULL,

)

GO



IF  not EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[T_DKhadZanali]') AND type in (N'U'))
CREATE TABLE [dbo].[T_DKhadZanali](
	[IDS] [int] IDENTITY(1,1) NOT NULL,
	[TxtNoeKala] [nvarchar](100) NULL,
	[TxtTedadKala] [nvarchar](100) NULL,
	[TxtTedadKantiner] [nvarchar](100) NULL,
	[TxtTedadBime] [nvarchar](100) NULL,
	[TxtShBl] [nvarchar](100) NULL,
	[TxtShBime] [nvarchar](100) NULL,
	[TxtRadifMarzi] [nvarchar](100) NULL,
	[TxtShomare] [nvarchar](100) NULL,
	[TxtDateKotaj] [nvarchar](100) NULL,
	[TxtShKotaj] [nvarchar](100) NULL,
	[TxtParvaneTaranzit] [nvarchar](100) NULL,
	[TxtGomrokVorod] [nvarchar](100) NULL,
	[TxtGomrokM] [nvarchar](100) NULL,
	[TxtWazn] [nvarchar](100) NULL,
	[FieldN1] [decimal](19, 4) NULL,
	[FieldN2] [decimal](19, 4) NULL,
	[FieldN3] [decimal](19, 4) NULL,
	[FieldN4] [decimal](19, 4) NULL,
	[FieldT1] [nvarchar](255) NULL,
	[FieldT2] [nvarchar](255) NULL,
	[FieldT3] [nvarchar](255) NULL,
	[FieldT4] [nvarchar](255) NULL,
	[Flag1] [bit] NULL,
	[Flag2] [bit] NULL,
	[Flag3] [bit] NULL,
	[Flag4] [bit] NULL,
	[PrimarySaid2] [int] NULL,
	[DKHID] [int] NULL,
	[HesabID] [int] NULL,
	[GOID] [int] NULL,
	[KOLID] [int] NULL,
	[THESABID] [int] NULL)
GO

/* ماليات SF3 Sath2 */


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'CodKala' AND Object_ID = Object_ID(N'T_Defult'))

ALTER TABLE dbo.T_Defult ADD
	CodKala INT
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'CodGroup' AND Object_ID = Object_ID(N'T_Defult'))

ALTER TABLE dbo.T_Defult ADD
	CodGroup INT
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'VaznKala' AND Object_ID = Object_ID(N'T_NameKala'))

ALTER TABLE dbo.T_NameKala ADD
	VaznKala DECIMAL(19,4) DEFAULT 0 
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'HazineKala' AND Object_ID = Object_ID(N'T_NameKala'))

ALTER TABLE dbo.T_NameKala ADD
	HazineKala DECIMAL(19,4) DEFAULT 0 
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'DarsadSoodKala' AND Object_ID = Object_ID(N'T_NameKala'))

ALTER TABLE dbo.T_NameKala ADD
	DarsadSoodKala DECIMAL(19,4) DEFAULT 0 
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'DarsadSoodKala2' AND Object_ID = Object_ID(N'T_NameKala'))

ALTER TABLE dbo.T_NameKala ADD
	DarsadSoodKala2 DECIMAL(19,4) DEFAULT 0 
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Kolid' AND Object_ID = Object_ID(N'T_Anbar'))

ALTER TABLE dbo.T_Anbar ADD
	Kolid INT
GO



IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'UnitPriceVo' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	UnitPriceVo DECIMAL(19,4) DEFAULT 0
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'UnitPriceKho' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	UnitPriceKho DECIMAL(19,4) DEFAULT 0
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'SalaTypeid' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	SalaTypeid INT
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'MablaghKol' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	MablaghKol DECIMAL(19,4) DEFAULT 0
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'MablaghKol' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	MablaghKol DECIMAL(19,4) DEFAULT 0
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Ezafat' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	Ezafat DECIMAL(19,4) DEFAULT 0
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Poorsant' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	Poorsant DECIMAL(19,4) DEFAULT 0
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userCreate' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	userCreate INT
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'userModify' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	userModify INT
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'dateSettlement' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	dateSettlement nvarchar(50)
GO



IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	ThesabIDSath2 INT
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Goid' AND Object_ID = Object_ID(N'T_Anbar'))

ALTER TABLE dbo.T_Anbar ADD
	Goid INT
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Thesabid' AND Object_ID = Object_ID(N'T_Anbar'))

ALTER TABLE dbo.T_Anbar ADD
	Thesabid INT
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'hesabid' AND Object_ID = Object_ID(N'T_Anbar'))

ALTER TABLE dbo.T_Anbar ADD
	hesabid INT
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'TimeSabt' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	TimeSabt nvarchar(10)
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ShGhabz' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	ShGhabz INT
GO


 
IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'UnitPriceKharid' AND Object_ID = Object_ID(N'T_NameKala'))

ALTER TABLE dbo.[T_NameKala] ADD
	UnitPriceKharid decimal(19, 4) NULL
GO



IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'FlagNoeKala' AND Object_ID = Object_ID(N'T_NameKala'))

ALTER TABLE dbo.T_NameKala ADD
	FlagNoeKala Bit
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'VasPlace' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	VasPlace INT
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'NerkhService' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	NerkhService decimal(19, 4)
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'MabService' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	MabService decimal(19, 4)
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'address2' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	address2 nvarchar(255)
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Maliyat' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	Maliyat decimal(19, 4)
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Avarez' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	Avarez decimal(19, 4)
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Footnote' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	Footnote nvarchar(255)
GO



IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2' AND Object_ID = Object_ID(N'T_Asnad_Dar'))

ALTER TABLE dbo.T_Asnad_Dar ADD
	ThesabIDSath2 INT
GO	
IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2' AND Object_ID = Object_ID(N'T_Asnad'))

ALTER TABLE dbo.T_Asnad ADD
	ThesabIDSath2 INT
GO	
IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2' AND Object_ID = Object_ID(N'T_Par7'))

ALTER TABLE dbo.T_Par7 ADD
	ThesabIDSath2 INT
GO	
IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2' AND Object_ID = Object_ID(N'T_Dar8'))

ALTER TABLE dbo.T_Dar8 ADD
	ThesabIDSath2 INT
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	ThesabIDSath2 INT
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Maliyat' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	Maliyat decimal(19, 4)
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Avarez' AND Object_ID = Object_ID(N'T_Forosh'))

ALTER TABLE dbo.T_Forosh ADD
	Avarez decimal(19, 4)
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2' AND Object_ID = Object_ID(N'Table4'))

ALTER TABLE dbo.Table4 ADD
	ThesabIDSath2 INT
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2_1' AND Object_ID = Object_ID(N'Table4'))

ALTER TABLE dbo.Table4 ADD
	ThesabIDSath2_1 INT
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2' AND Object_ID = Object_ID(N'T_Kharid'))

ALTER TABLE dbo.T_Kharid ADD
	ThesabIDSath2 INT
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Avarez' AND Object_ID = Object_ID(N'T_Kharid'))

ALTER TABLE dbo.T_Kharid ADD
	Avarez decimal(19, 4)
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Maliyat' AND Object_ID = Object_ID(N'T_Kharid'))

ALTER TABLE dbo.T_Kharid ADD
	Maliyat decimal(19, 4)
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Avarez' AND Object_ID = Object_ID(N'T_Dar8'))

ALTER TABLE dbo.T_Dar8 ADD
	Avarez decimal(19, 4)
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Maliyat' AND Object_ID = Object_ID(N'T_Dar8'))

ALTER TABLE dbo.T_Dar8 ADD
	Maliyat decimal(19, 4)
GO	



IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Avarez' AND Object_ID = Object_ID(N'T_Par7'))

ALTER TABLE dbo.T_Par7 ADD
	Avarez decimal(19, 4)
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Maliyat' AND Object_ID = Object_ID(N'T_Par7'))

ALTER TABLE dbo.T_Par7 ADD
	Maliyat decimal(19, 4)
GO	


 

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	ThesabIDSath2 INT
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'DateKhStartDate' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	DateKhStartDate nvarchar(255)
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'DateKhEndDate' AND Object_ID = Object_ID(N'T_DKhad'))

ALTER TABLE dbo.T_DKhad ADD
	DateKhEndDate nvarchar(255)
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2' AND Object_ID = Object_ID(N'T_PishFactor'))

ALTER TABLE dbo.T_PishFactor ADD
	ThesabIDSath2 INT
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2' AND Object_ID = Object_ID(N'Table1'))

ALTER TABLE dbo.Table1 ADD
	ThesabIDSath2 INT
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'ThesabIDSath2' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	ThesabIDSath2 INT
GO
	
IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Avarez' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	Avarez DECIMAL(19,4) DEFAULT 0
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Ezafat' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	Ezafat DECIMAL(19,4) DEFAULT 0
GO




IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Maliatkala' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	Maliatkala DECIMAL(19,4) DEFAULT 0
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'FlagPrint' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	FlagPrint INT
GO
	
/* Alter Transactoin2 */

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'FlagCh ' AND Object_ID = Object_ID(N'T_RepGen'))

ALTER TABLE dbo.T_RepGen ADD
	FlagCh nvarchar(255) NULL DEFAULT 0
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'RepStoredProc ' AND Object_ID = Object_ID(N'T_RepGen'))

ALTER TABLE dbo.T_RepGen ADD
	RepStoredProc nvarchar(255) NULL
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'storedprocedPararmeter ' AND Object_ID = Object_ID(N'T_RepGen'))

ALTER TABLE dbo.T_RepGen ADD
	storedprocedPararmeter nvarchar(255) NULL
GO	



/* End Alter Transactoin2 */

/* Alter Table4 */
IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'MabVahedHazine' AND Object_ID = Object_ID(N'Table4'))

ALTER TABLE dbo.[Table4] ADD
	GOKID int NULL
GO

IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'MabVahedHazine' AND Object_ID = Object_ID(N'Table4'))

ALTER TABLE dbo.[Table4] ADD
	MabVahedHazine decimal(19, 4) NULL
GO

IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'TedadHazine' AND Object_ID = Object_ID(N'Table4'))

ALTER TABLE dbo.[Table4] ADD
	TedadHazine decimal(19, 4) NULL
GO

/* End Alter Table4 */
/* Alter Transactoin */
IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'MabVahedHazine' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	MabVahedHazine decimal(19, 4) NULL
GO

IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'TedadHazine' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	TedadHazine decimal(19, 4) NULL
GO

IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'Tedad2' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	Tedad2 decimal(19, 4) NULL
GO

IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'Tedad1' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	Tedad1 decimal(19, 4) NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'zarib' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
    zarib decimal(19, 4) NULL
GO

/* End Alter Transactoin */


/* Alter T_TarSPish*/

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Ezafat' AND Object_ID = Object_ID(N'T_TarSPish'))

ALTER TABLE dbo.[T_TarSPish] ADD
	Ezafat DECIMAL(19,4) DEFAULT 0
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Maliatkala' AND Object_ID = Object_ID(N'T_TarSPish'))
ALTER TABLE dbo.T_TarSPish ADD
	Maliatkala money NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Maliyat' AND Object_ID = Object_ID(N'T_PishFactor'))
ALTER TABLE dbo.T_PishFactor ADD
	Maliyat money NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Maliat' AND Object_ID = Object_ID(N'T_TarSPish'))
ALTER TABLE dbo.T_TarSPish ADD
	Maliat money NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Avarez' AND Object_ID = Object_ID(N'T_TarSPish'))
ALTER TABLE dbo.T_TarSPish ADD
	Avarez money NULL
GO	


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Avarez' AND Object_ID = Object_ID(N'T_PishFactor'))
ALTER TABLE dbo.T_PishFactor ADD
	Avarez money NULL
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Zarib' AND Object_ID = Object_ID(N'T_TarSPish'))

ALTER TABLE dbo.T_TarSPish ADD
	Zarib decimal(19, 4) NULL
GO	

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Tedad2' AND Object_ID = Object_ID(N'T_TarSPish'))

ALTER TABLE dbo.T_TarSPish ADD
	Tedad2 decimal(19, 4) NULL
GO	


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Tedad1' AND Object_ID = Object_ID(N'T_TarSPish'))

ALTER TABLE dbo.T_TarSPish ADD
	Tedad1 decimal(19, 4) NULL
GO	


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Ezafat' AND Object_ID = Object_ID(N'T_TarSPish'))

ALTER TABLE dbo.[T_TarSPish] ADD
	Ezafat DECIMAL(19,4) DEFAULT 0
GO

/* End Alter T_TarSPish*/


IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'MabVahedHazine' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	MabVahedHazine decimal(19, 4) NULL
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Avarez' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	Avarez DECIMAL(19,4) DEFAULT 0
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Maliatkala' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	Maliatkala DECIMAL(19,4) DEFAULT 0
GO

IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'TedadHazine' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	TedadHazine decimal(19, 4) NULL
GO

IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'Tedad2' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	Tedad2 decimal(19, 4) NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'zarib' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
    zarib decimal(19, 4) NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Ezafat' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	Ezafat DECIMAL(19,4) DEFAULT 0
GO

if not exists (select
                     column_name
               from
                     INFORMATION_SCHEMA.columns
               where
                     table_name = 'T_Kharid'
                     and column_name = 'dateSettlement')
                     
alter table T_Kharid 

add dateSettlement nvarchar(50)


if not exists (select
                     column_name
               from
                     INFORMATION_SCHEMA.columns
               where
                     table_name = 'T_Forosh'
                     and column_name = 'dateSettlement')
                     
alter table T_Forosh 

add dateSettlement nvarchar(50)


if not exists (select
                     column_name
               from
                     INFORMATION_SCHEMA.columns
               where
                     table_name = 'T_PishFactor'
                     and column_name = 'dateSettlement')
                     
alter table T_PishFactor 

add dateSettlement nvarchar(50)


if not exists (select
                     column_name
               from
                     INFORMATION_SCHEMA.columns
               where
                     table_name = 'T_Par7'
                     and column_name = 'dateSettlement')
                     
alter table T_Par7 

add dateSettlement nvarchar(50)


if not exists (select
                     column_name
               from
                     INFORMATION_SCHEMA.columns
               where
                     table_name = 'T_Dar8'
                     and column_name = 'dateSettlement')
                     
alter table T_Dar8 

add dateSettlement nvarchar(50)


if not exists (select
                     column_name
               from
                     INFORMATION_SCHEMA.columns
               where
                     table_name = 'T_DKhad'
                     and column_name = 'dateSettlement')
                     
alter table T_DKhad 

add dateSettlement nvarchar(50)

GO


IF NOT EXISTS(SELECT name FROM sys.sysobjects WHERE Name = N'T_T_HeasbSath2' AND xtype = N'U')
CREATE TABLE [dbo].[T_T_HeasbSath2](
	[THESABID] [int] IDENTITY(1,1) NOT NULL,
	[THESABNumber] [nvarchar](50) NULL,
	[DateCreate] [smalldatetime] NULL,
	[DateModified] [smalldatetime] NULL,
	[NameHesabT] [nvarchar](255) NULL,
	[TextName] [nvarchar](255) NULL,
	[Address] [nvarchar](255) NULL,
	[Address2] [nvarchar](255) NULL,
	[Note] [ntext] NULL,
	[Telex] [nvarchar](50) NULL,
	[Shahr] [nvarchar](50) NULL,
	[Ostan] [nvarchar](50) NULL,
	[CodPosti] [nvarchar](50) NULL,
	[CodEghtesdi] [nvarchar](50) NULL,
	[Keshvar] [nvarchar](50) NULL,
	[BankHesab] [nvarchar](50) NULL,
	[Mob] [nvarchar](50) NULL,
	[Websit] [nvarchar](50) NULL,
	[Phone1] [nvarchar](255) NULL,
	[Phone2] [nvarchar](255) NULL,
	[Email] [nvarchar](255) NULL,
	[VMo] [bit] NULL,
	[VFo] [bit] NULL,
	[VKh] [bit] NULL,
	[VSa] [bit] NULL,
	[VDa] [bit] NULL,
	[CurrencyDefault] [int] NULL,
	[Active] [int] NULL,
	[SaghfBed] [money] NULL,
	[SaghfBes] [money] NULL,
	[Commision] [int] NULL,
	[UnitPriceFo] [money] NULL,
	[FieldN1] [decimal](19, 4) NULL,
	[FieldN2] [decimal](19, 4) NULL,
	[FieldN3] [decimal](19, 4) NULL,
	[FieldN4] [decimal](19, 4) NULL,
	[FieldT1] [nvarchar](255) NULL,
	[FieldT2] [nvarchar](255) NULL,
	[FieldT3] [nvarchar](255) NULL,
	[FieldT4] [nvarchar](255) NULL,
	[Flag1] [bit] NULL,
	[Flag2] [bit] NULL,
	[Flag3] [bit] NULL,
	[Flag4] [bit] NULL,
	[Flag5] [bit] NULL,
	[Flag6] [bit] NULL,
	[Flag7] [bit] NULL,
	[Flag8] [bit] NULL,
	[Balance] [money] NULL,
 CONSTRAINT [PK_T_T_HeasbSath2] PRIMARY KEY CLUSTERED 
(
	[THESABID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

IF NOT EXISTS(SELECT name FROM sys.sysobjects WHERE Name = N'T_RelateTafSiliSath1-2' AND xtype = N'U')

CREATE TABLE [dbo].[T_RelateTafSiliSath1-2](
	[IDRelatSath2] [int] IDENTITY(1,1) NOT NULL,
	[THesabIDSath1] [int] NOT NULL,
	[THesabIDSath2] [int] NOT NULL,
 CONSTRAINT [PK_T_RelateTafSiliSath1-2] PRIMARY KEY CLUSTERED 
(
	[IDRelatSath2] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


ALTER TABLE dbo.T_T_HeasbSath2 SET (LOCK_ESCALATION = TABLE)
GO
IF (NOT EXISTS(SELECT 1 FROM sysconstraints WHERE OBJECT_NAME(constid) = 'FK_TRANSACTION_T_T_HeasbSath2' AND OBJECT_NAME(id) = 'TRANSACTION')) 

ALTER TABLE dbo.[TRANSACTION] ADD CONSTRAINT
	FK_TRANSACTION_T_T_HeasbSath2 FOREIGN KEY
	(
	ThesabIDSath2
	) REFERENCES dbo.T_T_HeasbSath2
	(
	THESABID
	) ON UPDATE  CASCADE 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.[TRANSACTION] SET (LOCK_ESCALATION = TABLE)
GO





SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON

GO
CREATE TABLE dbo.Tmp_Table1
	(
	ID1 int NOT NULL IDENTITY (1, 1),
	ID1Number nvarchar(50) NULL,
	NumberKH int NULL,
	NumSID2 int NULL,
	VSabt int NULL,
	PrimarySaid2 int NULL,
	Dateeng datetime NULL,
	DateCreate datetime NULL,
	DateModified datetime NULL,
	HesabID int NULL,
	GOID int NULL,
	KOLID int NULL,
	MOINID int NULL,
	THESABID int NULL,
	THESABIDAmel int NULL,
	TextName nvarchar(255) NULL,
	TextName1 nvarchar(255) NULL,
	TextNameT nvarchar(255) NULL,
	DateID1 nvarchar(255) NULL,
	Takhfif decimal(19, 4) NULL,
	TextShSA nvarchar(255) NULL,
	Sharh nvarchar(255) NULL,
	FieldN1 decimal(19, 4) NULL,
	FieldN2 decimal(19, 4) NULL,
	FieldN3 decimal(19, 4) NULL,
	FieldN4 decimal(19, 4) NULL,
	FieldT1 nvarchar(255) NULL,
	FieldT2 nvarchar(255) NULL,
	FieldT3 nvarchar(255) NULL,
	FieldT4 nvarchar(255) NULL,
	Flag1 bit NULL,
	Flag2 bit NULL,
	Flag3 bit NULL,
	Flag4 bit NULL,
	Flag5 bit NULL,
	Flag6 bit NULL,
	Flag7 bit NULL,
	Flag8 bit NULL,
	UserID int NULL,
	Shift int NULL,
	FlgSabtFact bit NULL,
	SalaTypeid int NULL,
	Maliyat decimal(19, 4) NULL,
	MablaghKol decimal(19, 4) NULL,
	Avarez decimal(19, 4) NULL,
	Ezafat decimal(19, 4) NULL,
	Poorsant decimal(19, 4) NULL,
	userCreate int NULL,
	userModify int NULL,
	dateSettlement nvarchar(50) NULL,
	NameAID1 nvarchar(255) NULL,
	NameAID2 nvarchar(255) NULL,
	NameKala nvarchar(255) NULL,
	NameGorup1 nvarchar(255) NULL,
	NameGorup2 nvarchar(255) NULL,
	AID1 int NULL,
	AID2 int NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_Table1 SET (LOCK_ESCALATION = TABLE)
GO
EXECUTE sp_bindefault N'dbo.default_current_date', N'dbo.Tmp_Table1.DateCreate'
GO
EXECUTE sp_bindefault N'dbo.default_current_date', N'dbo.Tmp_Table1.DateModified'
GO
EXECUTE sp_bindefault N'dbo.false', N'dbo.Tmp_Table1.Takhfif'
GO
EXECUTE sp_bindefault N'dbo.false', N'dbo.Tmp_Table1.FieldN1'
GO
EXECUTE sp_bindefault N'dbo.false', N'dbo.Tmp_Table1.FieldN2'
GO
EXECUTE sp_bindefault N'dbo.false', N'dbo.Tmp_Table1.FieldN3'
GO
EXECUTE sp_bindefault N'dbo.false', N'dbo.Tmp_Table1.FieldN4'
GO
EXECUTE sp_bindefault N'dbo.false', N'dbo.Tmp_Table1.Flag1'
GO
EXECUTE sp_bindefault N'dbo.false', N'dbo.Tmp_Table1.FlgSabtFact'
GO
SET IDENTITY_INSERT dbo.Tmp_Table1 ON
GO
IF EXISTS(SELECT * FROM dbo.Table1)
	 EXEC('INSERT INTO dbo.Tmp_Table1 ( ID1, ID1Number, NumberKH, NumSID2, VSabt, PrimarySaid2, Dateeng, DateCreate, DateModified, HesabID, GOID, KOLID, MOINID, THESABID, THESABIDAmel, TextName, TextName1, TextNameT, 
                      DateID1, Takhfif, TextShSA, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, UserID, Shift, FlgSabtFact, 
                      SalaTypeid, Maliyat, MablaghKol, Avarez, Ezafat, Poorsant, userCreate, userModify, dateSettlement, NameAID1, NameAID2, NameKala, NameGorup1, NameGorup2, AID1, AID2)
		SELECT  ID1, ID1Number, NumberKH, NumSID2, VSabt, PrimarySaid2, Dateeng, DateCreate, DateModified, HesabID, GOID, KOLID, MOINID, THESABID, THESABIDAmel, TextName, TextName1, TextNameT, 
                      DateID1, Takhfif, TextShSA, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, UserID, Shift, FlgSabtFact, 
                      SalaTypeid, Maliyat, MablaghKol, Avarez, Ezafat, Poorsant, userCreate, userModify, dateSettlement, NameAID1, NameAID2, NameKala, NameGorup1, NameGorup2, AID1, AID2 FROM dbo.Table1 WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_Table1 OFF
GO
ALTER TABLE dbo.[TRANSACTION]
	DROP CONSTRAINT FK_TRANSACTION_Table1
GO
DROP TABLE dbo.Table1
GO
EXECUTE sp_rename N'dbo.Tmp_Table1', N'Table1', 'OBJECT' 
GO
ALTER TABLE dbo.Table1 ADD CONSTRAINT
	PK_Table1 PRIMARY KEY CLUSTERED 
	(
	ID1
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
GO
ALTER TABLE dbo.[TRANSACTION] WITH NOCHECK ADD CONSTRAINT
	FK_TRANSACTION_Table1 FOREIGN KEY
	(
	ID1
	) REFERENCES dbo.Table1
	(
	ID1
	) ON UPDATE  CASCADE 
	 ON DELETE  CASCADE 
	
GO
ALTER TABLE dbo.[TRANSACTION] SET (LOCK_ESCALATION = TABLE)
GO


IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'Tedad1User' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	Tedad1User decimal(19, 4) NULL
GO
IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'Tedad2User' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	Tedad2User decimal(19, 4) NULL
GO


IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'UnitPriceUser' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	UnitPriceUser decimal(19, 4) NULL
GO


IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'ZaribChildUser' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	ZaribChildUser decimal(19, 4) NULL
GO



IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'Tedad1User' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	Tedad1User decimal(19, 4) NULL
GO
IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'Tedad2User' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	Tedad2User decimal(19, 4) NULL
GO


IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'UnitPriceUser' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	UnitPriceUser decimal(19, 4) NULL
GO


IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'ZaribChildUser' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	ZaribChildUser decimal(19, 4) NULL
GO




IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'Tedad1User' AND Object_ID = Object_ID(N'T_TarSPish'))

ALTER TABLE dbo.[T_TarSPish] ADD
	Tedad1User decimal(19, 4) NULL
GO
IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'Tedad2User' AND Object_ID = Object_ID(N'T_TarSPish'))

ALTER TABLE dbo.[T_TarSPish] ADD
	Tedad2User decimal(19, 4) NULL
GO


IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'UnitPriceUser' AND Object_ID = Object_ID(N'T_TarSPish'))

ALTER TABLE dbo.[T_TarSPish] ADD
	UnitPriceUser decimal(19, 4) NULL
GO


IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'ZaribChildUser' AND Object_ID = Object_ID(N'T_TarSPish'))

ALTER TABLE dbo.[T_TarSPish] ADD
	ZaribChildUser decimal(19, 4) NULL
GO




IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'KalaIDChild' AND Object_ID = Object_ID(N'TRANSACTION'))

ALTER TABLE dbo.[TRANSACTION] ADD
	KalaIDChild INT
GO

IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'KalaIDChild' AND Object_ID = Object_ID(N'T_TarSPish'))

ALTER TABLE dbo.[T_TarSPish] ADD
	KalaIDChild INT
GO


IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'KalaIDChild' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	KalaIDChild INT
GO

IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'MabVahedHazine' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	MabVahedHazine decimal(19, 4) NULL
GO


IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Avarez' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	Avarez money DEFAULT 0
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Maliatkala' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	Maliatkala money DEFAULT 0
GO

IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'TedadHazine' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	TedadHazine decimal(19, 4) NULL
GO

IF not EXISTS(SELECT * FROM sys.columns WHERE Name = N'Tedad2' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	Tedad2 decimal(19, 4) NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'zarib' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
    zarib decimal(19, 4) NULL
GO

IF NOT EXISTS (SELECT * FROM sys.columns  WHERE Name = N'Ezafat' AND Object_ID = Object_ID(N'TRANSACTION2'))

ALTER TABLE dbo.[TRANSACTION2] ADD
	Ezafat money DEFAULT 0
GO


/****** Object:  UserDefinedFunction [dbo].[HesabName]    Script Date: 01/10/2018 12:59:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[HesabName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[HesabName]
GO



/****** Object:  UserDefinedFunction [dbo].[HesabName]    Script Date: 01/10/2018 12:59:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE FUNCTION [dbo].[HesabName] 	( 	@KolID int ,@Hesabid int,@THesabid int 	) RETURNS NVarchar(255)  AS 	BEGIN 		  /*DECLARE @RasidList Varchar(50)*/ IF (@THesabid IS NOT NULL) 			 	DECLARE CUR CURSOR FOR SELECT  NameHesabT AS NameHesab FROM T_T_Heasb WHERE THESABID = @THesabid 		 ELSE 			 	BEGIN 			       		IF (@Hesabid IS NOT NULL) 			             			DECLARE CUR CURSOR FOR SELECT NameHesab FROM T_Heasb WHERE HESABID = @Hesabid  			        		ELSE 			             			DECLARE CUR CURSOR FOR SELECT NameHesab FROM T_KolHesab WHERE  KOLID = @KolID  			 	END; 		 		 		 	 	OPEN CUR 		 	DECLARE @NameHesab  NVarchar(255) 		 	DECLARE @SUM NVarchar(255) 		 	SET @SUM = '' 		 		 	FETCH NEXT FROM CUR INTO @NameHesab 			 	WHILE @@FETCH_STATUS = 0 				 		BEGIN 				      			SET @SUM =  @NameHesab 				       			FETCH NEXT FROM CUR INTO @NameHesab 				 		END 			 			 	CLOSE CUR  		 	DEALLOCATE CUR  	 	RETURN @SUM 	 END              
GO



/****** Object:  UserDefinedFunction [dbo].[HesabNameCod]    Script Date: 01/10/2018 12:59:42 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[HesabNameCod]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[HesabNameCod]
GO



/****** Object:  UserDefinedFunction [dbo].[HesabNameCod]    Script Date: 01/10/2018 12:59:42 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



    CREATE FUNCTION [dbo].[HesabNameCod] 	( 	
    @KolID int ,
    @Hesabid int,
    @THesabid int 	) 
    RETURNS NVarchar(255)  AS 	
    
    BEGIN 		  /*DECLARE @RasidList Varchar(50)*/ 
    IF (@THesabid IS NOT NULL) 			 	
		DECLARE CUR CURSOR FOR SELECT  THESABNumber AS NameHesab FROM T_T_Heasb WHERE THESABID = @THesabid 		 
    ELSE 			 	
		BEGIN 			       		
			IF (@Hesabid IS NOT NULL) 			             			
				DECLARE CUR CURSOR FOR SELECT HESABNumber as NameHesab FROM T_Heasb WHERE HESABID = @Hesabid  			        		
			ELSE 			             			
				DECLARE CUR CURSOR FOR SELECT KolNumber as NameHesab FROM T_KolHesab WHERE  KOLID = @KolID  			 	
		 END; 		 		 		 	 	
		 
	OPEN CUR 		 	
	DECLARE @NameHesab  NVarchar(255) 		 	
	DECLARE @SUM NVarchar(255) 	
		 	
	SET @SUM = '' 	
		 		 	
	FETCH NEXT FROM CUR INTO @NameHesab 
				 	
	WHILE @@FETCH_STATUS = 0 	
				 		
	BEGIN 				      			
		SET @SUM =  @NameHesab 				       			
		FETCH NEXT FROM CUR INTO @NameHesab 				 		
	END 	
			 			 	
	CLOSE CUR  		 	
	DEALLOCATE CUR  	 	
	RETURN @SUM 
		
	END 


GO



/****** Object:  UserDefinedFunction [dbo].[HesabNameSath2SF3]    Script Date: 01/10/2018 12:59:50 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[HesabNameSath2SF3]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[HesabNameSath2SF3]
GO


/****** Object:  UserDefinedFunction [dbo].[HesabNameSath2SF3]    Script Date: 01/10/2018 12:59:50 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    Create FUNCTION [dbo].[HesabNameSath2SF3] 	( 	@THesabidSath2 int 	) RETURNS NVarchar(255)  AS 	
    
    BEGIN 		  /*DECLARE @RasidList Varchar(50)*/ 
		DECLARE @SUM NVarchar(255) 
		
		SELECT  @SUM=NameHesabT  FROM T_T_HeasbSath2 WHERE THESABID = @THesabidSath2 
				 
		RETURN @SUM 
    	 
    END              
GO









/****** Object:  UserDefinedFunction [dbo].[F1StartDate]    Script Date: 08/21/2017 12:36:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F1StartDate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F1StartDate]
GO



/****** Object:  UserDefinedFunction [dbo].[F1StartDate]    Script Date: 08/21/2017 12:36:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE FUNCTION [dbo].[F1StartDate] 	( ) RETURNS NVarchar(255)  AS 	   BEGIN 		  /*DECLARE @RasidList Varchar(50)*/ 					 	   DECLARE CUR CURSOR FOR     SELECT Replace(StartDate,'/','') as SD	FROM AC_Setting.dbo.T_YearMony	Where [dbid]= Db_Name() 		 		 		 	 	    OPEN CUR 		 	    DECLARE @NameHesab  NVarchar(255) 		 	    DECLARE @SUM NVarchar(255) 		 	    SET @SUM = '' 		 		 	    FETCH NEXT FROM CUR INTO @NameHesab 			 	    WHILE @@FETCH_STATUS = 0 				 		 	   BEGIN 				      			 		   SET @SUM =  @NameHesab 				       			 		   FETCH NEXT FROM CUR INTO @NameHesab 				 		 	   END 			 			 	    CLOSE CUR  		 	    DEALLOCATE CUR  	 	    RETURN @SUM 	     END  
GO



/****** Object:  UserDefinedFunction [dbo].[F1EndDate]    Script Date: 08/21/2017 12:36:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F1EndDate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F1EndDate]
GO



/****** Object:  UserDefinedFunction [dbo].[F1EndDate]    Script Date: 08/21/2017 12:36:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE FUNCTION [dbo].[F1EndDate] 	( ) RETURNS NVarchar(255)  AS 	   BEGIN 		  /*DECLARE @RasidList Varchar(50)*/ 					 	   DECLARE CUR CURSOR FOR     SELECT Replace(EndDate,'/','') as SD	FROM AC_Setting.dbo.T_YearMony	Where [dbid]= Db_Name() 		 		 		 	 	    OPEN CUR 		 	    DECLARE @NameHesab  NVarchar(255) 		 	    DECLARE @SUM NVarchar(255) 		 	    SET @SUM = '' 		 		 	    FETCH NEXT FROM CUR INTO @NameHesab 			 	    WHILE @@FETCH_STATUS = 0 				 		 	   BEGIN 				      			 		   SET @SUM =  @NameHesab 				       			 		   FETCH NEXT FROM CUR INTO @NameHesab 				 		 	   END 			 			 	    CLOSE CUR  		 	    DEALLOCATE CUR  	 	    RETURN @SUM 	     END  
GO



/****** Object:  View [dbo].[View_SubTransactionKala]    Script Date: 12/11/2017 09:18:27 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_SubTransactionKala]'))
DROP VIEW [dbo].[View_SubTransactionKala]
GO


/****** Object:  View [dbo].[View_SubTransactionKala]    Script Date: 12/11/2017 09:18:27 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE VIEW [dbo].[View_SubTransactionKala]
AS
SELECT     'مانده اول دوره' AS NoeFa1, 0 AS GOID, 0 AS KolID, 0 AS HesabID, 0 AS THesabID, dbo.F1StartDate() AS Tarikh, 0 AS ShomareFactor, 'موجودی اول دوره' AS Namehesab, NameKalaID AS KALAID, AID, 
                      GOKID, Mojodi AS TedadVarde, 0 AS TedadSadre, 0 AS Tedad, UnitPrice * Mojodi AS MVarde, 0 AS MSadre, 9 AS NoeFa, '' AS SharhKala, 0 AS idFact, 0 AS TedadKh, 0 AS TedadVorood, 
                      0 AS TedadBarFo, 0 AS TedadVA, 0 AS TedadFo, 0 AS TedadKhorooj, 0 AS TedadBarKh, 0 AS TedadKhA, UnitPrice AS UnitPriceKH, 0 AS UnitpriceFo, 0 AS KharidID, 0 AS FieldN3, 
                      CONVERT(Float, CONVERT(nvarchar(255), T_Kala.NameKalaID) + N'.' + (CASE WHEN (T_Kala.AID % 10) 
                      = 0 THEN CONVERT(nvarchar(255), T_Kala.AID) + '11111' ELSE CONVERT(nvarchar(255), T_Kala.AID) END)) AS LinkKalaAid,0 AS FieldN5
FROM         T_Kala
WHERE     (Mojodi <> 0)
UNION ALL
SELECT     CASE WHEN (KHARIDID) IS NOT NULL THEN 'خريد ' WHEN (FOROSHID) IS NOT NULL THEN 'فروش ' WHEN (PARID7) IS NOT NULL THEN 'ب.فروش ' WHEN (DARID8) IS NOT NULL 
                      THEN 'ب.خريد ' WHEN (ID1) IS NOT NULL THEN 'جابه جايی ' WHEN (VORODID) IS NOT NULL THEN 'رسيد انبار ' WHEN (KHOROJID) IS NOT NULL THEN 'حواله انبار ' ELSE '' END AS NoeFa1, GOID, KOLID, 
                      HESABID, THESABID, Date1 AS Tarikh, OperationCode AS ShomareFactor, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, KALAID, AID, GOKID, ISNULL(TedadKh, 0) 
                      + ISNULL(TedadVorood, 0) + ISNULL(TedadBarFo, 0) + ISNULL(TedadVA, 0) AS TedadVarde, ISNULL(TedadFo, 0) + ISNULL(TedadKhorooj, 0) + ISNULL(TedadBarKh, 0) + ISNULL(TedadKhA, 0) 
                      AS TedadSadre, ABS((ISNULL(TedadKh, 0) + ISNULL(TedadVorood, 0) + ISNULL(TedadBarFo, 0) + ISNULL(TedadVA, 0)) - (ISNULL(TedadFo, 0) + ISNULL(TedadKhorooj, 0) + ISNULL(TedadBarKh, 0) 
                      + ISNULL(TedadKhA, 0))) AS Tedad, (CASE WHEN ((KHARIDID) IS NOT NULL OR
                      (PARID7) IS NOT NULL OR
                      (VORODID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) AS MVarde, (CASE WHEN ((FOROSHID) IS NOT NULL OR
                      (dbo.[TRANSACTION].DARID8) IS NOT NULL OR
                      (KHOROJID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) AS MSadre, CASE WHEN (KHARIDID) IS NOT NULL THEN 1 WHEN (FOROSHID) IS NOT NULL 
                      THEN 2 WHEN (PARID7) IS NOT NULL THEN 4 WHEN (DARID8) IS NOT NULL THEN 3 WHEN (VORODID) IS NOT NULL THEN 5 WHEN (KHOROJID) IS NOT NULL THEN 6 WHEN (ID1) IS NOT NULL 
                      THEN 6 ELSE 8 END AS NoeFa, CONVERT(NVARCHAR(255), Sharh) AS SharhKala, CASE WHEN (KHARIDID) IS NOT NULL THEN KHARIDID WHEN (FOROSHID) IS NOT NULL 
                      THEN FOROSHID WHEN (PARID7) IS NOT NULL THEN PARID7 WHEN (DARID8) IS NOT NULL THEN DARID8 WHEN (VORODID) IS NOT NULL THEN VORODID WHEN (KHOROJID) IS NOT NULL 
                      THEN KHOROJID WHEN (ID1) IS NOT NULL THEN ID1 END AS idFact, CONVERT(float, TedadKh) AS TedadKh, CONVERT(float, TedadVorood) AS TedadVorood, CONVERT(float, TedadBarFo) 
                      AS TedadBarFo, CONVERT(float, TedadVA) AS TedadVA, CONVERT(float, TedadFo) AS TedadFo, CONVERT(float, TedadKhorooj) AS TedadKhorooj, CONVERT(float, TedadBarKh) AS TedadBarKh, 
                      CONVERT(float, TedadKhA) AS TedadKhA, ISNULL(UnitPriceKh, 0) + ISNULL(UnitPriceVo, 0) AS UnitPriceKh, UnitPriceFo, KHARIDID, FieldN3, CONVERT(Float, CONVERT(nvarchar(255), KALAID) + N'.' + (CASE WHEN (AID % 10) 
                      = 0 THEN CONVERT(nvarchar(255), AID) + '11111' ELSE CONVERT(nvarchar(255), AID) END)) AS LinkKalaAid, FieldN5
FROM         [TRANSACTION]
WHERE     (NOT (KALAID IS NULL)) AND (NOT (Date1 IS NULL))



GO




/****** Object:  View [dbo].[View_ForoshSandogh]    Script Date: 01/28/2019 10:16:55 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_ForoshSandogh]'))
DROP VIEW [dbo].[View_ForoshSandogh]
GO


/****** Object:  View [dbo].[View_ForoshSandogh]    Script Date: 01/28/2019 10:16:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[View_ForoshSandogh]
AS
SELECT     FOROSHID, ForoshNumbr, NumberKH, NumSID2, VSabt, PrimarySaid2, Dateeng, DateCreate, DateModified, HesabID, GOID, KOLID, MOINID, THESABID, THESABIDAmel, TextName, TextName1, 
                      TextNameT, DateFo, Takhfif, TextShSA, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, UserID, Shift, FlgSabtFact,
                       SalaTypeid, Maliyat, MablaghKol, Avarez, Ezafat, Poorsant, userCreate, userModify, dateSettlement, ThesabIDSath2
FROM         dbo.T_Forosh
WHERE     (Flag1 = 0)

GO


/****** Object:  View [dbo].[View_TRANSACTION]    Script Date: 01/02/2018 17:52:43 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_TRANSACTION]'))
DROP VIEW [dbo].[View_TRANSACTION]
GO


/****** Object:  View [dbo].[View_TRANSACTION]    Script Date: 01/02/2018 17:52:43 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[View_TRANSACTION]
AS
SELECT     dbo.[TRANSACTION].TRAID, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].ThesabIDSath2, 
                      dbo.[TRANSACTION].SAID2, dbo.[TRANSACTION].DateCreate, dbo.[TRANSACTION].DateModified, dbo.[TRANSACTION].RadifSAID2, dbo.[TRANSACTION].TextNHSID2, dbo.[TRANSACTION].TextShSA, 
                      dbo.[TRANSACTION].PARBes, dbo.[TRANSACTION].DARBed, dbo.[TRANSACTION].Sharh, dbo.[TRANSACTION].radif, dbo.[TRANSACTION].Date1, dbo.[TRANSACTION].Dateeng, 
                      dbo.[TRANSACTION].SAIDType, CONVERT(float, dbo.T_GropHesab.GoNumber) AS CodGroup, dbo.T_GropHesab.NameGo, dbo.T_KolHesab.NameHesab, dbo.T_KolHesab.KolNumber, 
                      dbo.T_Heasb.HESABNumber, dbo.T_Heasb.NameHesab AS NameHesabMoin, dbo.T_T_Heasb.THESABNumber, dbo.T_T_Heasb.NameHesabT, 
                      dbo.T_T_HeasbSath2.THESABNumber AS THESABNumberSath2, dbo.T_T_HeasbSath2.NameHesabT AS NameHesabTSath2, dbo.T_T_Heasb.Mob, dbo.T_T_Heasb.Phone1, dbo.T_T_Heasb.Phone2, 
                      dbo.T_T_Heasb.Active
FROM         dbo.[TRANSACTION] INNER JOIN
                      dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID INNER JOIN
                      dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID LEFT OUTER JOIN
                      dbo.T_T_HeasbSath2 ON dbo.[TRANSACTION].ThesabIDSath2 = dbo.T_T_HeasbSath2.THESABID LEFT OUTER JOIN
                      dbo.T_T_Heasb ON dbo.[TRANSACTION].THESABID = dbo.T_T_Heasb.THESABID LEFT OUTER JOIN
                      dbo.T_Heasb ON dbo.[TRANSACTION].HESABID = dbo.T_Heasb.HESABID
WHERE     (NOT (dbo.[TRANSACTION].PARBes IS NULL)) AND (NOT (dbo.[TRANSACTION].DARBed IS NULL))

GO




/****** Object:  View [dbo].[View_InfoKala]    Script Date: 11/05/2018 09:42:39 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_InfoKala]'))
DROP VIEW [dbo].[View_InfoKala]
GO



/****** Object:  View [dbo].[View_InfoKala]    Script Date: 11/05/2018 09:42:39 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[View_InfoKala]
AS
SELECT  T_NameKala1.ID, CONVERT(float, T_NameKala1.KalaNumber) AS KalaNumber, T_NameKala1.NameKala1, T_Kala1.GOKID, CONVERT(float, T_NameGoKala1.NameGoKalanumbr) AS NameGoKalanumbr, 
               T_NameGoKala1.NameGoKala1, T_Kala1.AID, CONVERT(float, T_Anbar1.AIDNumber) AS AIDNumber, T_Anbar1.NameAnbar, T_NameVahed1.NameVahed AS NameVahed1, 
               T_NameVahed_1.NameVahed AS NameVahed2, T_Kala1.VKalaID, T_NameKala1.Active, T_NameKala1.VAHED1, T_NameKala1.VAHED2, T_NameKala1.MinPrice, T_NameKala1.MaxPrice, T_NameKala1.Zarib, 
               T_NameKala1.MinMogoody, T_NameKala1.MaxMogoody, T_NameKala1.BarCode, T_NameKala1.UnitPrice2, T_NameKala1.UnitPrice1, T_NameKala1.UnitPrice3, T_NameKala1.UnitPrice4, T_NameKala1.UnitPrice5, 
               T_NameKala1.UnitPrice6, T_NameKala1.DarsadAmel1, T_NameKala1.DarsadAmel2, T_NameKala1.DarsadAmel3, T_NameKala1.DarsadAmel4, T_NameKala1.DarsadAmel5, T_NameKala1.DarsadAmel6, 
               T_NameKala1.DarsadKasr1, T_NameKala1.DarsadKasr2, T_NameKala1.DarsadKasr3, T_NameKala1.DarsadKasr4, T_NameKala1.DarsadKasr5, T_NameKala1.DarsadKasr6, T_NameKala1.Currency1, 
               T_NameKala1.Currency2, T_NameKala1.Currency3, T_NameKala1.Currency4, T_NameKala1.Currency5, T_NameKala1.Currency6, T_NameKala1.Model, T_NameKala1.UnitPrice, T_NameKala1.Unitfo, 
               T_NameKala1.VKalaID AS VKalaID0, T_NameKala1.FieldN1, T_NameKala1.FieldN2, T_NameKala1.FieldN3, T_NameKala1.FieldN4, T_NameKala1.FieldT1, T_NameKala1.FieldT2, T_NameKala1.FieldT3, 
               T_NameKala1.FieldT4, T_NameKala1.Flag1, T_NameKala1.Flag2, T_NameKala1.Flag3, T_NameKala1.Flag4, T_NameKala1.TedadBal, T_NameKala1.DateCreate, T_NameKala1.DateModified, 
               CONVERT(nvarchar(255), T_NameKala1.Note) AS note, CONVERT(Float, CONVERT(nvarchar(255), T_NameKala1.ID) + N'.' + (CASE WHEN (T_Kala1.AID % 10) = 0 THEN CONVERT(nvarchar(255), T_Kala1.AID) 
               + '11111' ELSE CONVERT(nvarchar(255), T_Kala1.AID) END)) AS LinkKalaAid, T_Anbar1.Sharh, T_NameKala1.FlagNoeKala, T_Kala1.Mojodi AS MojodiEbtedaDore, T_Kala1.UnitPrice AS UnitPriceEbtedaDore, 
               T_NameKala1.UnitPriceKharid, T_NameKala1.vaznKala, T_NameKala1.HazineKala, T_NameKala1.DarsadSoodKala, T_NameKala1.DarsadSoodKala2
FROM     T_NameGoKala AS T_NameGoKala1 INNER JOIN
               T_Kala AS T_Kala1 ON T_NameGoKala1.ID = T_Kala1.GOKID INNER JOIN
               T_Anbar AS T_Anbar1 ON T_Kala1.AID = T_Anbar1.AID RIGHT OUTER JOIN
               T_NameKala AS T_NameKala1 INNER JOIN
               T_NameVahed AS T_NameVahed1 ON T_NameKala1.VAHED1 = T_NameVahed1.VAHED1 LEFT OUTER JOIN
               T_NameVahed AS T_NameVahed_1 ON T_NameKala1.VAHED2 = T_NameVahed_1.VAHED1 ON T_Kala1.NameKalaID = T_NameKala1.ID             
GO





/****** Object:  UserDefinedFunction [dbo].[F_LastPriceKharid]    Script Date: 01/10/2018 12:39:13 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F_LastPriceKharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F_LastPriceKharid]
GO



/****** Object:  UserDefinedFunction [dbo].[F_LastPriceKharid]    Script Date: 01/10/2018 12:39:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE FUNCTION [dbo].[F_LastPriceKharid] () RETURNS TABLE AS RETURN ( 
    	
    	
SELECT     KALAID, GOKID, AID, UnitPriceKh, Tarikh, CONVERT(nvarchar(255), KALAID) + N'.' + CONVERT(nvarchar(255), Tarikh) AS LinkKalaAid
FROM         (SELECT DISTINCT  KALAID, GOKID, AID, ISNULL(UnitPriceKH, 0) AS UnitPriceKh, Tarikh
                        FROM         View_SubTransactionKala
                        WHERE     (TedadKh + TedadVA > 0)
                        UNION ALL
                        SELECT DISTINCT  NameKalaID, GOKID, AID, UnitPrice, dbo.F1StartDate() AS Expr1
                        FROM         T_Kala
                        ) AS derivedtbl_1

    	
    	
    	 )  
GO





/****** Object:  UserDefinedFunction [dbo].[F_LastDateKharid]    Script Date: 03/04/2018 11:57:04 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F_LastDateKharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F_LastDateKharid]
GO



/****** Object:  UserDefinedFunction [dbo].[F_LastDateKharid]    Script Date: 03/04/2018 11:57:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


    CREATE FUNCTION [dbo].[F_LastDateKharid]() RETURNS TABLE AS RETURN ( 
    	
SELECT     KALAID, MAX(Tarikh) AS Tarikh, GOKID, AID, MAX(CONVERT(nvarchar(255), KALAID) + N'.' + CONVERT(nvarchar(255), Tarikh)) AS LinkKalaAid
FROM         (SELECT DISTINCT  KALAID, Tarikh, GOKID, AID
                        FROM         View_SubTransactionKala
                        WHERE     (TedadKh + TedadVA > 0)
                        UNION ALL
                        SELECT DISTINCT  NameKalaID, dbo.F1StartDate() AS Expr1, GOKID, AID
                        FROM         T_Kala) AS derivedtbl_1
GROUP BY KALAID, GOKID, AID
    
    )  

GO






/****** Object:  Table [dbo].[Table_UserDastrasi]    Script Date: 07/03/2017 09:30:22 ******/
IF  NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Table_UserDastrasi]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[Table_UserDastrasi](
	[USERID] [int] NOT NULL,
	[IDNoeOP] [int] NOT NULL
) ON [PRIMARY]

END
GO

/****** Object:  StoredProcedure [dbo].[Table_UserDastrasi_DELETE]    Script Date: 07/03/2017 09:44:45 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Table_UserDastrasi_DELETE]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Table_UserDastrasi_DELETE]
GO


/****** Object:  StoredProcedure [dbo].[Table_UserDastrasi_DELETE]    Script Date: 07/03/2017 09:44:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

  create PROCEDURE [dbo].[Table_UserDastrasi_DELETE]  		@USERID int, 		@IDNoeOP int  AS BEGIN   DELETE FROM Table_UserDastrasi WHERE     (USERID = @USERID) AND (IDNoeOP = @IDNoeOP)  END 
GO




/****** Object:  StoredProcedure [dbo].[Table_UserDastrasi_GetValue]    Script Date: 07/03/2017 09:44:55 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Table_UserDastrasi_GetValue]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Table_UserDastrasi_GetValue]
GO


/****** Object:  StoredProcedure [dbo].[Table_UserDastrasi_GetValue]    Script Date: 07/03/2017 09:44:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

  create PROCEDURE [dbo].[Table_UserDastrasi_GetValue] 		@USERID int, 		@IDNoeOP int, 		@CountValue int=-1 output  AS BEGIN  SELECT    @CountValue= COUNT(USERID)  FROM         Table_UserDastrasi WHERE     (IDNoeOP = @IDNoeOP) AND (USERID = @USERID)  END 
GO

/****** Object:  StoredProcedure [dbo].[Table_UserDastrasi_Insert]    Script Date: 07/03/2017 09:45:01 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Table_UserDastrasi_Insert]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Table_UserDastrasi_Insert]
GO


/****** Object:  StoredProcedure [dbo].[Table_UserDastrasi_Insert]    Script Date: 07/03/2017 09:45:01 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

  create PROCEDURE [dbo].[Table_UserDastrasi_Insert]  		@USERID int, 		@IDNoeOP int  AS BEGIN    INSERT INTO Table_UserDastrasi                       (USERID, IDNoeOP) VALUES     (@USERID,@IDNoeOP)  END 
GO

UPDATE    T_RepGen
SET              WhereFiled = N'WHERE 1=1'
WHERE     (TypeID = N'106') OR
                      (TypeID = N'107') OR
                      (TypeID = N'108') OR
                      (TypeID = N'109')
GO                      

UPDATE    T_RepGen
SET              FlagCh = N'0'
WHERE     (FlagCh IS NULL)


GO






/****** Object:  UserDefinedFunction [dbo].[FunInfoKalaAll]    Script Date: 11/05/2018 09:44:53 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FunInfoKalaAll]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[FunInfoKalaAll]
GO



/****** Object:  UserDefinedFunction [dbo].[FunInfoKalaAll]    Script Date: 11/05/2018 09:44:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE FUNCTION  [dbo].[FunInfoKalaAll](@activeKala INT) RETURNS TABLE  AS      
      

      RETURN  (   	 

 
SELECT  ID, KalaNumber, NameKala1, GOKID, NameGoKalanumbr, NameGoKala1, AID, AIDNumber, NameAnbar, NameVahed1, NameVahed2, VKalaID, Active, VAHED1, VAHED2, MinPrice, MaxPrice, Zarib, MinMogoody, 
               MaxMogoody, BarCode, UnitPrice2, UnitPrice1, UnitPrice3, UnitPrice4, UnitPrice5, UnitPrice6, DarsadAmel1, DarsadAmel2, DarsadAmel3, DarsadAmel4, DarsadAmel5, DarsadAmel6, DarsadKasr1, DarsadKasr2, 
               DarsadKasr3, DarsadKasr4, DarsadKasr5, DarsadKasr6, Currency1, Currency2, Currency3, Currency4, Currency5, Currency6, Model, UnitPrice, Unitfo, VKalaID0, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, 
               FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, TedadBal, DateCreate, DateModified, note, LinkKalaAid, Sharh AS SharhAnbar, FlagNoeKala, MojodiEbtedaDore, UnitPriceEbtedaDore, UnitPriceKharid, vaznKala, 
               HazineKala, DarsadSoodKala, DarsadSoodKala2
FROM     View_InfoKala
WHERE  (CASE WHEN @activeKala > 0 THEN Active ELSE 5 END <> CASE WHEN @activeKala > 0 THEN @activeKala ELSE 6 END)
      
      
      
      )






GO







/****** Object:  UserDefinedFunction [dbo].[SubTransactionKala]    Script Date: 09/05/2017 11:25:02 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SubTransactionKala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[SubTransactionKala]
GO



/****** Object:  UserDefinedFunction [dbo].[SubTransactionKala]    Script Date: 09/05/2017 11:25:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

  /* =============================================*/ 
  
  /*-- Author:		<Author,,Name>*/ 
  /*-- Create date: 2015/07/04*/ /*-- Description:	اطلاعات تراکنش کالا*/ 
  
  /*-- =============================================*/  
  CREATE FUNCTION  [dbo].[SubTransactionKala]() RETURNS TABLE  AS 
  RETURN  ( SELECT     'مانده اول دوره' AS NoeFa1, 0 AS GOID, 0 AS KolID, 0 AS HesabID, 0 AS THesabID, dbo.F1StartDate() AS Tarikh, 0 AS ShomareFactor, 'موجودی اول دوره' AS Namehesab, NameKalaID AS KALAID, AID, 
                      GOKID, Mojodi AS TedadVarde, 0 AS TedadSadre, 0 AS Tedad, UnitPrice * Mojodi AS MVarde, 0 AS MSadre, 9 AS NoeFa, '' AS SharhKala, 0 AS idFact, 0 AS TedadKh, 0 AS TedadVorood, 
                      0 AS TedadBarFo, 0 AS TedadVA, 0 AS TedadFo, 0 AS TedadKhorooj, 0 AS TedadBarKh, 0 AS TedadKhA, UnitPrice AS UnitPriceKH, 0 AS UnitpriceFo
FROM         T_Kala
WHERE     (Mojodi <> 0)
UNION ALL
SELECT     CASE WHEN (KHARIDID) IS NOT NULL THEN 'خريد ' WHEN (FOROSHID) IS NOT NULL THEN 'فروش ' WHEN (PARID7) IS NOT NULL THEN 'ب.فروش ' WHEN (DARID8) IS NOT NULL 
                      THEN 'ب.خريد ' WHEN (ID1) IS NOT NULL THEN 'جابه جايی ' WHEN (VORODID) IS NOT NULL THEN 'رسيد انبار ' WHEN (KHOROJID) IS NOT NULL THEN 'حواله انبار ' ELSE '' END AS NoeFa1, GOID, KOLID, 
                      HESABID, THESABID, Date1 AS Tarikh, OperationCode AS ShomareFactor, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, KALAID, AID, GOKID, ISNULL(TedadKh, 0) 
                      + ISNULL(TedadVorood, 0) + ISNULL(TedadBarFo, 0) + ISNULL(TedadVA, 0) AS TedadVarde, ISNULL(TedadFo, 0) + ISNULL(TedadKhorooj, 0) + ISNULL(TedadBarKh, 0) + ISNULL(TedadKhA, 0) 
                      AS TedadSadre, ABS((ISNULL(TedadKh, 0) + ISNULL(TedadVorood, 0) + ISNULL(TedadBarFo, 0) + ISNULL(TedadVA, 0)) - (ISNULL(TedadFo, 0) + ISNULL(TedadKhorooj, 0) + ISNULL(TedadBarKh, 0) 
                      + ISNULL(TedadKhA, 0))) AS Tedad, (CASE WHEN ((KHARIDID) IS NOT NULL OR
                      (PARID7) IS NOT NULL OR
                      (VORODID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) AS MVarde, (CASE WHEN ((FOROSHID) IS NOT NULL OR
                      (dbo.[TRANSACTION].DARID8) IS NOT NULL OR
                      (KHOROJID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) AS MSadre, CASE WHEN (KHARIDID) IS NOT NULL THEN 1 WHEN (FOROSHID) IS NOT NULL 
                      THEN 2 WHEN (PARID7) IS NOT NULL THEN 4 WHEN (DARID8) IS NOT NULL THEN 3 WHEN (VORODID) IS NOT NULL THEN 5 WHEN (KHOROJID) IS NOT NULL THEN 6 WHEN (ID1) IS NOT NULL 
                      THEN 6 ELSE 8 END AS NoeFa, CONVERT(NVARCHAR(255), Sharh) AS SharhKala, CASE WHEN (KHARIDID) IS NOT NULL THEN KHARIDID WHEN (FOROSHID) IS NOT NULL 
                      THEN FOROSHID WHEN (PARID7) IS NOT NULL THEN PARID7 WHEN (DARID8) IS NOT NULL THEN DARID8 WHEN (VORODID) IS NOT NULL THEN VORODID WHEN (KHOROJID) IS NOT NULL 
                      THEN KHOROJID WHEN (ID1) IS NOT NULL THEN ID1 END AS idFact, CONVERT(float, TedadKh) AS TedadKh, CONVERT(float, TedadVorood) AS TedadVorood, CONVERT(float, TedadBarFo) 
                      AS TedadBarFo, CONVERT(float, TedadVA) AS TedadVA, CONVERT(float, TedadFo) AS TedadFo, CONVERT(float, TedadKhorooj) AS TedadKhorooj, CONVERT(float, TedadBarKh) AS TedadBarKh, 
                      CONVERT(float, TedadKhA) AS TedadKhA, ISNULL(UnitPriceKh, 0) + ISNULL(UnitPriceVo, 0) AS UnitPriceKh, UnitPriceFo
FROM         [TRANSACTION]
WHERE     (NOT (KALAID IS NULL)) AND (NOT (Date1 IS NULL)) ) 
GO







/****** Object:  UserDefinedFunction [dbo].[Fun_SubAkharinKharid]    Script Date: 01/10/2018 12:39:40 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Fun_SubAkharinKharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[Fun_SubAkharinKharid]
GO



/****** Object:  UserDefinedFunction [dbo].[Fun_SubAkharinKharid]    Script Date: 01/10/2018 12:39:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  CREATE FUNCTION  [dbo].[Fun_SubAkharinKharid](@activeKala INT) RETURNS TABLE  AS 
  RETURN  (
  	
SELECT     MAX(F_LastPriceKharid.UnitPriceKh) AS MaxUnitPriceKh, F_LastDateKharid.KALAID, MAX(F_LastDateKharid.Tarikh) AS MaxDate1, F_LastDateKharid.GOKID, F_LastDateKharid.AID, CONVERT(Float, 
                      CONVERT(nvarchar(255), F_LastDateKharid.KALAID) + N'.' + (CASE WHEN (F_LastDateKharid.AID % 10) = 0 THEN CONVERT(nvarchar(255), F_LastDateKharid.AID) 
                      + '11111' ELSE CONVERT(nvarchar(255), F_LastDateKharid.AID) END)) AS LinkKalaAid
FROM         dbo.F_LastDateKharid() AS F_LastDateKharid INNER JOIN
                      dbo.F_LastPriceKharid() AS F_LastPriceKharid ON F_LastDateKharid.LinkKalaAid = F_LastPriceKharid.LinkKalaAid
GROUP BY F_LastDateKharid.KALAID, F_LastDateKharid.GOKID, F_LastDateKharid.AID, CONVERT(Float, CONVERT(nvarchar(255), F_LastDateKharid.KALAID) + N'.' + (CASE WHEN (F_LastDateKharid.AID % 10)
                       = 0 THEN CONVERT(nvarchar(255), F_LastDateKharid.AID) + '11111' ELSE CONVERT(nvarchar(255), F_LastDateKharid.AID) END))
 )





GO







/****** Object:  View [dbo].[View_Fun_CQ_ListKala_Sandoogh_Analyz]    Script Date: 01/10/2018 12:34:04 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_Fun_CQ_ListKala_Sandoogh_Analyz]'))
DROP VIEW [dbo].[View_Fun_CQ_ListKala_Sandoogh_Analyz]
GO



/****** Object:  View [dbo].[View_Fun_CQ_ListKala_Sandoogh_Analyz]    Script Date: 01/10/2018 12:34:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[View_Fun_CQ_ListKala_Sandoogh_Analyz]
AS
SELECT     KALAID, AID, GOKID, SUM(Tedad) AS Tedad, SUM(MaxUnitPriceKh) AS MaxUnitPriceKh, CONVERT(Float, CONVERT(nvarchar(255), KALAID) + N'.' + (CASE WHEN (AID % 10) 
                      = 0 THEN CONVERT(nvarchar(255), AID) + '11111' ELSE CONVERT(nvarchar(255), AID) END)) AS LinkKalaAID
FROM         (SELECT     KALAID, AID, GOKID, TedadVarde - TedadSadre AS Tedad, 0 AS MaxUnitPriceKh
                        FROM         dbo.View_SubTransactionKala
                        UNION ALL
                        SELECT     KALAID, AID, GOKID, 0 AS Expr1, MaxUnitPriceKh
                        FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1) AS derivedtbl_1
GROUP BY KALAID, AID, GOKID, CONVERT(Float, CONVERT(nvarchar(255), KALAID) + N'.' + (CASE WHEN (AID % 10) = 0 THEN CONVERT(nvarchar(255), AID) + '11111' ELSE CONVERT(nvarchar(255), AID) 
                      END))

GO






/****** Object:  UserDefinedFunction [dbo].[F_SoratHesabKol]    Script Date: 09/24/2018 11:20:05 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F_SoratHesabKol]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F_SoratHesabKol]
GO


/****** Object:  UserDefinedFunction [dbo].[F_SoratHesabKol]    Script Date: 09/24/2018 11:20:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



    CREATE FUNCTION [dbo].[F_SoratHesabKol] (@Date1 NVARCHAR(255)) RETURNS TABLE AS RETURN ( 
    

SELECT     tbl_Tar_1.RadifSAID2, T_KolHesab.NameHesab AS NameHesabmoin, CASE WHEN (tbl_Tar_1.NameKala IS NOT NULL) THEN tbl_Tar_1.NameKala ELSE CONVERT(nvarchar(255), isnull(TextShSA, 
                      N' ')) END AS SharhRadif, DERIVEDTBL.mande AS MandeHesab, tbl_Tar_1.DARBed AS Bedehkar, tbl_Tar_1.PARBes AS Bestankar, tbl_Tar_1.Date1 AS Tarikh, 
                      T_SA2.SAID2Number AS ShomareSanad, T_KolHesab.KOLID AS HesabID, T_KolHesab.KolNumber AS CodeHesab, tbl_Tar_1.srtfield, tbl_Tar_1.tedad, tbl_Tar_1.unitprice, tbl_Tar_1.NameVahed, 
                      tbl_Tar_1.NameKala, tbl_Tar_1.MatnFi, tbl_Tar_1.SAIDType, tbl_Tar_1.NoeAmaliyat, tbl_Tar_1.TedadSadere, tbl_Tar_1.TedadVarede, tbl_Tar_1.TRAID, tbl_Tar_1.radif, tbl_Tar_1.nameKala2, 
                      tbl_Tar_1.NameKala1, tbl_Tar_1.FlagPrint
FROM         (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, KOLID AS HESABID
                        FROM         [TRANSACTION] AS Tran1
                        WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                        GROUP BY KOLID) AS DERIVEDTBL RIGHT OUTER JOIN
                      T_SA2 INNER JOIN
                          (SELECT     Tbl_2.RadifSAID2, Tbl_2.Sharh, Tbl_2.Date1, ISNULL(Tbl_2.PARBes, 0) + CONVERT(decimal(19, 4), Tbl_2.TedadBarFo + Tbl_2.TedadKh) * (Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo) AS PARBes, 
                      ISNULL(Tbl_2.DARBed, 0) + CONVERT(decimal(19, 4), Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh) * (Tbl_2.UnitPriceFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh) AS DARBed, 
                      CONVERT(int, CASE WHEN (NOT (Tbl_2.SAID2 IS NULL)) THEN Tbl_2.SAID2 ELSE [Tbl_2].fieldn2 END) AS SAID2, CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN Tedad1User + Tbl_2.TedadKh + Tbl_2.TedadBarFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh ELSE Tbl_2.TedadFo + Tbl_2.TedadKh + Tbl_2.TedadBarFo + Tbl_2.TedadBarKh
                       + Tbl_2.TedadDKh END AS tedad, CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN UnitPriceUser + Tbl_2.UnitPriceFo + Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh ELSE Tbl_2.UnitPriceFo + Tbl_2.UnitPriceKh +
                       Tbl_2.UnitPriceBarFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh END AS unitprice, T_NameVahed.NameVahed, CASE WHEN (NOT KALAID IS NULL) THEN ((CASE WHEN Tbl_2.FOROSHID > 0 AND
                       Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + ')' ELSE T_NameKala.NameKala1 END) + N' ' + CONVERT(nvarchar(255), isnull(Tbl_2.Sharh, N''))) 
                      ELSE (name + N' ' + CONVERT(nvarchar(255), isnull(Tbl_2.Sharh, N''))) END + N' ' + CASE WHEN (tedadfo > 0) THEN (N'فاکتور فروش' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadkh > 0) 
                      THEN (N'فاکتور خريد' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadbarfo > 0) THEN (N'فاکتور برگشت از فروش' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadbarkh > 0) 
                      THEN (N'فاکتور برگشت از خريد ' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedaddkh > 0) THEN (N'فاکتور خدمات ' + isnull(Tbl_2.OperationCode, N'')) ELSE NULL 
                      END COLLATE Arabic_CI_AS  AS NameKala, CASE WHEN (Tbl_2.SAID2 IS NULL) THEN N'في : ' ELSE NULL END AS MatnFi, CASE WHEN (NOT KALAID IS NULL) 
                      THEN (CASE WHEN Tbl_2.FOROSHID > 0 AND Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + ')' ELSE T_NameKala.NameKala1 END) ELSE (name + N' ') 
                      END COLLATE Arabic_CI_AS  AS nameKala2, Tbl_2.KOLID AS Hesabid, Tbl_2.TextShSA, CASE WHEN (Tbl_2.SAID2 IS NULL) THEN 2 ELSE 1 END AS srtfield, Tbl_2.SAIDType, 
                      Tbl_2.TRAID, (CASE WHEN (tedadfo > 0) THEN (N'فاکتور فروش' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadkh > 0) THEN (N'فاکتور خريد' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadbarfo > 0)
                       THEN (N'فاکتور برگشت از فروش' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadbarkh > 0) THEN (N'فاکتور برگشت از خريد' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedaddkh > 0) 
                      THEN (N'فاکتور خدمات' + isnull(Tbl_2.OperationCode, N'')) WHEN (Tbl_2.DARID1 IS NOT NULL) THEN (N'دريافت نقدي ' + isnull(T_SA2_1.OperationCode, 0)) WHEN (Tbl_2.PARID1 IS NOT NULL) 
                      THEN (N'پرداخت نقدي ' + isnull(T_SA2_1.OperationCode, 0)) WHEN ((Tbl_2.PARID3 IS NOT NULL) OR
                      (Tbl_2.PARID2 IS NOT NULL)) THEN (N'پرداخت چک ' + isnull(T_SA2_1.OperationCode, 0)) WHEN ((Tbl_2.DARID2 IS NOT NULL) OR
                      (Tbl_2.DARID3 IS NOT NULL)) THEN (N'دريافت چک ' + isnull(T_SA2_1.OperationCode, 0)) ELSE N'سند حسابداری' + str(T_SA2_1.SAID2Number) END) AS NoeAmaliyat, 
                      CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN Tedad1User + Tbl_2.TedadBarKh + Tbl_2.TedadDKh ELSE Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh END AS TedadSadere, 
                      Tbl_2.TedadBarFo + Tbl_2.TedadKh AS TedadVarede, (Tbl_2.TedadBarFo + Tbl_2.TedadKh) * (Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo) AS beskala, 
                      (Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh) * (Tbl_2.UnitPriceFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh) AS bedkala, T_NameKhad.Name, CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + ')' ELSE T_NameKala.NameKala1 END COLLATE Arabic_CI_AS  AS NameKala1, 
                      Tbl_2.OperationCode, Tbl_2.radif, T_SA2_1.OperationCode AS OperationCode1, Tbl_2.FlagPrint
FROM         T_RelateKala RIGHT OUTER JOIN
                      [TRANSACTION] AS Tbl_2 ON T_RelateKala.Kalaid2 = Tbl_2.KalaIDChild LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON Tbl_2.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN
                      T_NameKhad ON Tbl_2.NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                      T_NameKala INNER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 ON Tbl_2.KALAID = T_NameKala.ID
WHERE     (Tbl_2.SAIDType <> 3) OR
                      (Tbl_2.SAIDType IS NULL)) AS tbl_Tar_1 ON T_SA2.SAID2 = tbl_Tar_1.SAID2 INNER JOIN
                      T_KolHesab ON tbl_Tar_1.Hesabid = T_KolHesab.KOLID ON DERIVEDTBL.HESABID = T_KolHesab.KOLID
                             
                             UNION ALL
                             
SELECT     1001 AS Expr3, dbo.HesabName(NULL, NULL, tbl_Tar.KOLID) AS NameHesabmoin, N'بابت تخفيفات ' + CONVERT(nvarchar(255), ISNULL(tbl_Tar.TextShSA, N'')) AS SharhRadif, NULL AS MandeHesab, 
                      tbl_Tar.PARBes AS Bedehkar, tbl_Tar.DARBed AS Bestankar, tbl_Tar.Date1 AS Tarikh, tbl_Tar.SAID2Number AS ShomareSanad, WhereHesab.KOLID, NULL AS CodeHesab, 3 AS srtfield, 0 AS tedad, 
                      0 AS unitprice, NULL AS NameVahed, NULL AS NameKala, NULL AS MatnFi, NULL AS SAIDType, NULL AS NoeAmaliyat, 0 AS TedadSadere, 0 AS TedadVarede, tbl_Tar.TRAID, tbl_Tar.radif, NULL 
                      AS Expr1, NULL AS Expr2, tbl_Tar.FlagPrint
FROM         [TRANSACTION] AS tbl_Tar INNER JOIN
                          (SELECT     SAID2, KOLID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (SAID2Number IS NULL)) AND (NOT (RadifSAID2 IS NULL))
                             GROUP BY SAID2, KOLID) AS WhereHesab ON tbl_Tar.SAID2 = WhereHesab.SAID2
WHERE     (tbl_Tar.SAIDType = 6) AND (tbl_Tar.KOLID IN (24, 25, 26))



    UNION ALL

SELECT     999 AS Expr2, dbo.HesabName(T_Forosh.KOLID, NULL, NULL) AS NameHesabmoin, N'عوارض فروش' AS SharhRadif, NULL AS MandeHesab, T_Forosh.Avarez AS Bedehkar, 0 AS Bestankar, 
                      T_Forosh.DateFo AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_Forosh.KOLID, T_KolHesab.KolNumber, 2 AS Expr10, 0 AS Expr3, 0 AS Expr4, NULL AS Expr5, NULL AS Expr6, NULL 
                      AS Expr11, NULL AS Expr12, NULL AS Expr13, 0 AS Expr7, 0 AS Expr8, NULL AS Expr14, NULL AS Expr15, NULL AS Expr9, NULL AS Expr1, 0 AS Expr16
FROM         T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON T_Forosh.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, KOLID AS HESABID
FROM         [TRANSACTION] AS Tran1
WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
GROUP BY KOLID) AS DERIVEDTBL ON T_KolHesab.KOLID = DERIVEDTBL.HESABID
WHERE     (ISNULL(T_Forosh.Avarez, 0) > 0)
                             

UNION ALL



SELECT     999 AS Expr2, dbo.HesabName(T_Forosh.KOLID, NULL, NULL) AS NameHesabmoin, N'مالیات فروش' AS SharhRadif, NULL AS MandeHesab, T_Forosh.Maliyat AS Bedehkar, 0 AS Bestankar, 
                      T_Forosh.DateFo AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_Forosh.KOLID, T_KolHesab.KolNumber, 2 AS Expr10, 0 AS Expr3, 0 AS Expr4, NULL AS Expr5, NULL AS Expr6, NULL 
                      AS Expr11, NULL AS Expr12, NULL AS Expr13, 0 AS Expr7, 0 AS Expr8, NULL AS Expr14, NULL AS Expr15, NULL AS Expr9, NULL AS Expr1, 0 AS Expr16
FROM         T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON T_Forosh.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, KOLID AS HESABID
FROM         [TRANSACTION] AS Tran1
WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
GROUP BY KOLID) AS DERIVEDTBL ON T_KolHesab.KOLID = DERIVEDTBL.HESABID
WHERE     (ISNULL(T_Forosh.Maliyat, 0) > 0)

    	)    	



GO



/****** Object:  UserDefinedFunction [dbo].[F_SoratHesabMoein]    Script Date: 09/24/2018 11:20:11 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F_SoratHesabMoein]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F_SoratHesabMoein]
GO


/****** Object:  UserDefinedFunction [dbo].[F_SoratHesabMoein]    Script Date: 09/24/2018 11:20:11 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



    CREATE FUNCTION [dbo].[F_SoratHesabMoein] (@Date1 NVARCHAR(255)) RETURNS TABLE AS RETURN ( 
    	
 	
    	
SELECT     tbl_Tar_1.RadifSAID2, T_Heasb.NameHesab AS NameHesabmoin
, CASE WHEN (tbl_Tar_1.NameKala IS NOT NULL) THEN tbl_Tar_1.NameKala ELSE CONVERT(nvarchar(255), isnull(TextShSA,N' ')) END  AS SharhRadif, 
DERIVEDTBL.mande AS MandeHesab, tbl_Tar_1.DARBed AS Bedehkar, tbl_Tar_1.PARBes AS Bestankar, tbl_Tar_1.Date1 AS Tarikh, 
                      T_SA2.SAID2Number AS ShomareSanad, T_Heasb.HESABID AS HesabID, T_Heasb.HESABNumber AS CodeHesab, tbl_Tar_1.srtfield, tbl_Tar_1.tedad, tbl_Tar_1.unitprice, tbl_Tar_1.NameVahed, 
                      tbl_Tar_1.NameKala, tbl_Tar_1.MatnFi, tbl_Tar_1.SAIDType, tbl_Tar_1.NoeAmaliyat, tbl_Tar_1.TedadSadere, tbl_Tar_1.TedadVarede, tbl_Tar_1.TRAID, tbl_Tar_1.radif, tbl_Tar_1.nameKala2, 
                      tbl_Tar_1.NameKala1, tbl_Tar_1.FlagPrint
FROM         T_Heasb INNER JOIN
                      T_SA2 INNER JOIN
                          (SELECT     Tbl_2.RadifSAID2, Tbl_2.Sharh, Tbl_2.Date1, ISNULL(Tbl_2.PARBes, 0) + CONVERT(decimal(19, 4), Tbl_2.TedadBarFo + Tbl_2.TedadKh) * (Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo) AS PARBes, 
                      ISNULL(Tbl_2.DARBed, 0) + CONVERT(decimal(19, 4), Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh) * (Tbl_2.UnitPriceFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh) AS DARBed, 
                      CONVERT(int, CASE WHEN (NOT (Tbl_2.SAID2 IS NULL)) THEN Tbl_2.SAID2 ELSE [Tbl_2].fieldn2 END) AS SAID2, CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN Tedad1User + Tbl_2.TedadKh + Tbl_2.TedadBarFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh ELSE Tbl_2.TedadFo + Tbl_2.TedadKh + Tbl_2.TedadBarFo + Tbl_2.TedadBarKh
                       + Tbl_2.TedadDKh END AS tedad, 
                      CASE WHEN Tbl_2.FOROSHID >0  AND Tbl_2.KalaIDChild > 0 THEN UnitPriceUser + Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh ELSE Tbl_2.UnitPriceFo +
                       Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh END AS unitprice, T_NameVahed.NameVahed, CASE WHEN (NOT KALAID IS NULL) 
                      THEN ((CASE WHEN Tbl_2.FOROSHID > 0 AND Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + ')' ELSE T_NameKala.NameKala1 END) 
                      + N' ' + CONVERT(nvarchar(255), isnull(Tbl_2.Sharh, N''))) ELSE (name + N' ' + CONVERT(nvarchar(255), isnull(Tbl_2.Sharh, N''))) END + N' ' + CASE WHEN (tedadfo > 0) 
                      THEN (N'فاکتور فروش' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadkh > 0) THEN (N'فاکتور خريد' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadbarfo > 0) 
                      THEN (N'فاکتور برگشت از فروش' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadbarkh > 0) THEN (N'فاکتور برگشت از خريد ' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedaddkh > 0) 
                      THEN (N'فاکتور خدمات ' + isnull(Tbl_2.OperationCode, N'')) ELSE NULL END  COLLATE Arabic_CI_AS AS NameKala, CASE WHEN (Tbl_2.SAID2 IS NULL) THEN N'في : ' ELSE NULL END AS MatnFi, 
                      CASE WHEN (NOT KALAID IS NULL) THEN (CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + ')' ELSE T_NameKala.NameKala1 END) ELSE (name + N' ') END COLLATE Arabic_CI_AS AS nameKala2, Tbl_2.HESABID AS Hesabid, 
                      Tbl_2.TextShSA, CASE WHEN (Tbl_2.SAID2 IS NULL) THEN 2 ELSE 1 END AS srtfield, Tbl_2.SAIDType, Tbl_2.TRAID, (CASE WHEN (tedadfo > 0) THEN (N'فاکتور فروش' + isnull(Tbl_2.OperationCode, 
                      N'')) WHEN (tedadkh > 0) THEN (N'فاکتور خريد' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadbarfo > 0) THEN (N'فاکتور برگشت از فروش' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadbarkh > 0) 
                      THEN (N'فاکتور برگشت از خريد' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedaddkh > 0) THEN (N'فاکتور خدمات' + isnull(Tbl_2.OperationCode, N'')) WHEN (Tbl_2.DARID1 IS NOT NULL) 
                      THEN (N'دريافت نقدي ' + isnull(T_SA2_1.OperationCode, 0)) WHEN (Tbl_2.PARID1 IS NOT NULL) THEN (N'پرداخت نقدي ' + isnull(T_SA2_1.OperationCode, 0)) WHEN ((Tbl_2.PARID3 IS NOT NULL) OR
                      (Tbl_2.PARID2 IS NOT NULL)) THEN (N'پرداخت چک ' + isnull(T_SA2_1.OperationCode, 0)) WHEN ((Tbl_2.DARID2 IS NOT NULL) OR
                      (Tbl_2.DARID3 IS NOT NULL)) THEN (N'دريافت چک ' + isnull(T_SA2_1.OperationCode, 0)) ELSE N'سند حسابداری' + str(T_SA2_1.SAID2Number) END) AS NoeAmaliyat, 
                      CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN Tedad1User + Tbl_2.TedadBarKh + Tbl_2.TedadDKh ELSE Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh END AS TedadSadere, 
                      Tbl_2.TedadBarFo + Tbl_2.TedadKh AS TedadVarede, (Tbl_2.TedadBarFo + Tbl_2.TedadKh) * (Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo) AS beskala, 
                      (Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh) * (Tbl_2.UnitPriceFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh) AS bedkala, T_NameKhad.Name, CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + ')' ELSE T_NameKala.NameKala1 END COLLATE Arabic_CI_AS  AS NameKala1, 
                      Tbl_2.OperationCode, Tbl_2.radif, T_SA2_1.OperationCode AS OperationCode1, Tbl_2.FlagPrint
FROM         T_RelateKala RIGHT OUTER JOIN
                      [TRANSACTION] AS Tbl_2 ON T_RelateKala.Kalaid2 = Tbl_2.KalaIDChild LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON Tbl_2.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN
                      T_NameKhad ON Tbl_2.NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                      T_NameKala INNER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 ON Tbl_2.KALAID = T_NameKala.ID
WHERE     (Tbl_2.SAIDType <> 3) OR
                      (Tbl_2.SAIDType IS NULL)) AS tbl_Tar_1 ON T_SA2.SAID2 = tbl_Tar_1.SAID2 ON T_Heasb.HESABID = tbl_Tar_1.Hesabid LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, HESABID
                             FROM         [TRANSACTION] AS Tran1
                             WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                             GROUP BY HESABID) AS DERIVEDTBL ON T_Heasb.HESABID = DERIVEDTBL.HESABID
                             
                             UNION ALL
                             
SELECT     1001 AS Expr3, dbo.HesabName(NULL, NULL, tbl_Tar.HESABID) AS NameHesabmoin, N' تخفيف ' + CONVERT(nvarchar(255), ISNULL(tbl_Tar.TextShSA, N'')) AS SharhRadif, NULL AS MandeHesab, 
                      tbl_Tar.PARBes AS Bedehkar, tbl_Tar.DARBed AS Bestankar, tbl_Tar.Date1 AS Tarikh, tbl_Tar.SAID2Number AS ShomareSanad, WhereHesab.HESABID, NULL AS CodeHesab, 3 AS srtfield, 
                      0 AS tedad, 0 AS unitprice, NULL AS NameVahed, NULL AS NameKala, NULL AS MatnFi, NULL AS SAIDType, NULL AS NoeAmaliyat, 0 AS TedadSadere, 0 AS TedadVarede, tbl_Tar.TRAID, 
                      tbl_Tar.radif, NULL AS Expr1, NULL AS Expr2, tbl_Tar.FlagPrint
FROM         [TRANSACTION] AS tbl_Tar INNER JOIN
                          (SELECT     SAID2, HESABID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (SAID2Number IS NULL)) AND (NOT (RadifSAID2 IS NULL))
                             GROUP BY SAID2, HESABID) AS WhereHesab ON tbl_Tar.SAID2 = WhereHesab.SAID2
WHERE     (tbl_Tar.KOLID IN (24, 25, 26)) AND (tbl_Tar.SAIDType = 6)
    	

    UNION ALL

SELECT     999 AS Expr2, dbo.HesabName(NULL, T_Forosh.HesabID, NULL) AS NameHesabmoin, N'عوارض فروش' AS SharhRadif, NULL AS MandeHesab, T_Forosh.Avarez AS Bedehkar, 0 AS Bestankar, 
                      T_Forosh.DateFo AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_Forosh.HesabID, T_Heasb.HESABNumber, 2 AS Expr10, 0 AS Expr3, 0 AS Expr4, NULL AS Expr5, NULL AS Expr6, NULL 
                      AS Expr11, NULL AS Expr12, NULL AS Expr13, 0 AS Expr7, 0 AS Expr8, NULL AS Expr14, NULL AS Expr15, NULL AS Expr9, NULL AS Expr1, 0 AS Expr16
FROM         (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, HESABID
                        FROM         [TRANSACTION] AS Tran1
                        WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                        GROUP BY HESABID) AS DERIVEDTBL RIGHT OUTER JOIN
                      T_Heasb ON DERIVEDTBL.HESABID = T_Heasb.HESABID RIGHT OUTER JOIN
                      T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 ON T_Heasb.HESABID = T_Forosh.HesabID
WHERE     (ISNULL(T_Forosh.Avarez, 0) > 0)
                             

UNION ALL


SELECT     999 AS Expr2, dbo.HesabName(NULL, T_Forosh.HesabID, NULL) AS NameHesabmoin, N'مالیات فروش' AS SharhRadif, NULL AS MandeHesab, T_Forosh.Maliyat AS Bedehkar, 0 AS Bestankar, 
                      T_Forosh.DateFo AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_Forosh.HesabID, T_Heasb.HESABNumber, 2 AS Expr10, 0 AS Expr3, 0 AS Expr4, NULL AS Expr5, NULL AS Expr6, NULL 
                      AS Expr11, NULL AS Expr12, NULL AS Expr13, 0 AS Expr7, 0 AS Expr8, NULL AS Expr14, NULL AS Expr15, NULL AS Expr9, NULL AS Expr1, 0 AS Expr16
FROM         (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, HESABID
                        FROM         [TRANSACTION] AS Tran1
                        WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                        GROUP BY HESABID) AS DERIVEDTBL RIGHT OUTER JOIN
                      T_Heasb ON DERIVEDTBL.HESABID = T_Heasb.HESABID RIGHT OUTER JOIN
                      T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 ON T_Heasb.HESABID = T_Forosh.HesabID
WHERE     (ISNULL(T_Forosh.Maliyat, 0) > 0)



    )    


GO



/****** Object:  UserDefinedFunction [dbo].[F_SoratHesabTafsili]    Script Date: 09/24/2018 11:20:17 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F_SoratHesabTafsili]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F_SoratHesabTafsili]
GO


/****** Object:  UserDefinedFunction [dbo].[F_SoratHesabTafsili]    Script Date: 09/24/2018 11:20:17 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





    CREATE FUNCTION [dbo].[F_SoratHesabTafsili] (@Date1 NVARCHAR(255)) RETURNS TABLE AS RETURN ( 
    	
    	
SELECT     tbl_Tar_1.RadifSAID2, T_T_Heasb.NameHesabT AS NameHesabmoin, CASE WHEN (tbl_Tar_1.NameKala IS NOT NULL) THEN tbl_Tar_1.NameKala ELSE CONVERT(nvarchar(255), 
                      isnull(tbl_Tar_1.TextShSA, N' ')) END AS SharhRadif, DERIVEDTBL.mande AS MandeHesab, tbl_Tar_1.DARBed AS Bedehkar, tbl_Tar_1.PARBes AS Bestankar, tbl_Tar_1.Date1 AS Tarikh, 
                      T_SA2.SAID2Number AS ShomareSanad, T_T_Heasb.THESABID AS HesabID, T_T_Heasb.THESABNumber AS CodeHesab, tbl_Tar_1.srtfield, tbl_Tar_1.tedad, tbl_Tar_1.unitprice, 
                      tbl_Tar_1.NameVahed, tbl_Tar_1.NameKala, tbl_Tar_1.MatnFi, tbl_Tar_1.SAIDType, tbl_Tar_1.NoeAmaliyat, tbl_Tar_1.TedadSadere, tbl_Tar_1.TedadVarede, tbl_Tar_1.TRAID, tbl_Tar_1.radif, 
                      tbl_Tar_1.nameKala2, tbl_Tar_1.NameKala1, tbl_Tar_1.FlagPrint
FROM         T_SA2 INNER JOIN
                          (SELECT     Tbl_2.RadifSAID2, Tbl_2.Sharh, Tbl_2.Date1, ISNULL(Tbl_2.PARBes, 0) + CONVERT(decimal(19, 4), Tbl_2.TedadBarFo + Tbl_2.TedadKh) * (Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo) AS PARBes, 
                      ISNULL(Tbl_2.DARBed, 0) + CONVERT(decimal(19, 4), Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh) * (Tbl_2.UnitPriceFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh) AS DARBed, 
                      CONVERT(int, CASE WHEN (NOT (Tbl_2.SAID2 IS NULL)) THEN Tbl_2.SAID2 ELSE [Tbl_2].fieldn2 END) AS SAID2, CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN Tedad1User + Tbl_2.TedadKh + Tbl_2.TedadBarFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh ELSE Tbl_2.TedadFo + Tbl_2.TedadKh + Tbl_2.TedadBarFo + Tbl_2.TedadBarKh
                       + Tbl_2.TedadDKh END AS tedad, CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN UnitPriceUser + Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh ELSE Tbl_2.UnitPriceFo + Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo
                       + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh END AS unitprice, T_NameVahed.NameVahed, CASE WHEN (NOT KALAID IS NULL) THEN ((CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + ')' ELSE T_NameKala.NameKala1 END) + N' ' + CONVERT(nvarchar(255), isnull(Tbl_2.Sharh, N''))) 
                      ELSE (name + N' ' + CONVERT(nvarchar(255), isnull(Tbl_2.Sharh, N''))) END + N' ' + CASE WHEN (tedadfo > 0) THEN (N'فاکتور فروش' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadkh > 0) 
                      THEN (N'فاکتور خريد' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadbarfo > 0) THEN (N'فاکتور برگشت از فروش' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadbarkh > 0) 
                      THEN (N'فاکتور برگشت از خريد ' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedaddkh > 0) THEN (N'فاکتور خدمات ' + isnull(Tbl_2.OperationCode, N'')) ELSE NULL 
                      END COLLATE Arabic_CI_AS  AS NameKala, CASE WHEN (Tbl_2.SAID2 IS NULL) THEN N'في : ' ELSE NULL END AS MatnFi, CASE WHEN (NOT KALAID IS NULL) 
                      THEN NameKala1 ELSE (name + N' ') END AS nameKala2, Tbl_2.THESABID AS Hesabid, Tbl_2.TextShSA, CASE WHEN (Tbl_2.SAID2 IS NULL) THEN 2 ELSE 1 END AS srtfield, Tbl_2.SAIDType, 
                      Tbl_2.TRAID, (CASE WHEN (tedadfo > 0) THEN (N'فاکتور فروش' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadkh > 0) THEN (N'فاکتور خريد' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadbarfo > 0)
                       THEN (N'فاکتور برگشت از فروش' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedadbarkh > 0) THEN (N'فاکتور برگشت از خريد' + isnull(Tbl_2.OperationCode, N'')) WHEN (tedaddkh > 0) 
                      THEN (N'فاکتور خدمات' + isnull(Tbl_2.OperationCode, N'')) WHEN (Tbl_2.DARID1 IS NOT NULL) THEN (N'دريافت نقدي ' + isnull(T_SA2_1.OperationCode, 0)) WHEN (Tbl_2.PARID1 IS NOT NULL) 
                      THEN (N'پرداخت نقدي ' + isnull(T_SA2_1.OperationCode, 0)) WHEN ((Tbl_2.PARID3 IS NOT NULL) OR
                      (Tbl_2.PARID2 IS NOT NULL)) THEN (N'پرداخت چک ' + isnull(T_SA2_1.OperationCode, 0)) WHEN ((Tbl_2.DARID2 IS NOT NULL) OR
                      (Tbl_2.DARID3 IS NOT NULL)) THEN (N'دريافت چک ' + isnull(T_SA2_1.OperationCode, 0)) ELSE N'سند حسابداری' + str(T_SA2_1.SAID2Number) END) AS NoeAmaliyat, 
                      CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN Tedad1User + Tbl_2.TedadBarKh + Tbl_2.TedadDKh ELSE Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh END AS TedadSadere, 
                      Tbl_2.TedadBarFo + Tbl_2.TedadKh AS TedadVarede, (Tbl_2.TedadBarFo + Tbl_2.TedadKh) * (Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo) AS beskala, 
                      (Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh) * (Tbl_2.UnitPriceFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh) AS bedkala, T_NameKhad.Name, Tbl_2.OperationCode, Tbl_2.radif, 
                      T_SA2_1.OperationCode AS OperationCode1, Tbl_2.FlagPrint, CASE WHEN Tbl_2.FOROSHID > 0 AND 
                      Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + ')' ELSE T_NameKala.NameKala1 END  COLLATE Arabic_CI_AS AS NameKala1, 
                      Tbl_2.KALAID
FROM         T_RelateKala RIGHT OUTER JOIN
                      [TRANSACTION] AS Tbl_2 ON T_RelateKala.Kalaid2 = Tbl_2.KalaIDChild LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON Tbl_2.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN
                      T_NameKhad ON Tbl_2.NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                      T_NameKala INNER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 ON Tbl_2.KALAID = T_NameKala.ID
WHERE     (Tbl_2.SAIDType <> 3) OR
                      (Tbl_2.SAIDType IS NULL)) AS tbl_Tar_1 ON T_SA2.SAID2 = tbl_Tar_1.SAID2 INNER JOIN
                      T_T_Heasb ON tbl_Tar_1.Hesabid = T_T_Heasb.THESABID LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID AS HESABID
                             FROM         [TRANSACTION] AS Tran1
                             WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                             GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.HESABID
                             
                             UNION ALL

SELECT     1001 AS Expr3, dbo.HesabName(NULL, NULL, tbl_Tar.THESABID) AS NameHesabmoin, N'بابت تخفيفات ' + CONVERT(nvarchar(255), ISNULL(tbl_Tar.TextShSA, N'')) AS SharhRadif, NULL 
                      AS MandeHesab, tbl_Tar.PARBes AS Bedehkar, tbl_Tar.DARBed AS Bestankar, tbl_Tar.Date1 AS Tarikh, tbl_Tar.SAID2Number AS ShomareSanad, WhereHesab.THESABID, NULL AS CodeHesab, 
                      3 AS srtfield, 0 AS tedad, 0 AS unitprice, NULL AS NameVahed, NULL AS NameKala, NULL AS MatnFi, NULL AS SAIDType, NULL AS NoeAmaliyat, 0 AS TedadSadere, 0 AS TedadVarede, 
                      tbl_Tar.TRAID, tbl_Tar.radif, NULL AS Expr1, NULL AS Expr2, tbl_Tar.FlagPrint
FROM         [TRANSACTION] AS tbl_Tar INNER JOIN
                          (SELECT     SAID2, THESABID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (SAID2Number IS NULL)) AND (NOT (RadifSAID2 IS NULL))
                             GROUP BY SAID2, THESABID) AS WhereHesab ON tbl_Tar.SAID2 = WhereHesab.SAID2
WHERE     (tbl_Tar.KOLID IN (24, 25, 26)) AND (tbl_Tar.SAIDType = 6)


UNION ALL

SELECT     999 AS Expr2, dbo.HesabName(NULL, NULL, T_Forosh.THESABID) AS NameHesabmoin, N'عوارض فروش'  AS SharhRadif, NULL AS MandeHesab, 
                       Avarez  AS Bedehkar, 0 AS Bestankar, T_Forosh.DateFo AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, 
                      T_T_Heasb.THESABID AS HesabID, T_T_Heasb.THESABNumber AS CodeHesab, 2 AS Expr10, 0 AS Expr3, 0 AS Expr4, NULL AS Expr5, NULL AS Expr6, NULL AS Expr11, NULL AS Expr12, NULL 
                      AS Expr13, 0 AS Expr7, 0 AS Expr8, NULL AS Expr14, NULL AS Expr15, NULL AS Expr9, NULL AS Expr1, 0 AS Expr16
FROM         T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_T_Heasb ON T_Forosh.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID AS HESABID
                             FROM         [TRANSACTION] AS Tran1
                             WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                             GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.HESABID
WHERE     ISNULL(T_Forosh.Avarez,0)>0
                             

UNION ALL

SELECT     999 AS Expr2, dbo.HesabName(NULL, NULL, T_Forosh.THESABID) AS NameHesabmoin, N'مالیات فروش'  AS SharhRadif, NULL AS MandeHesab, 
                       Maliyat  AS Bedehkar, 0 AS Bestankar, T_Forosh.DateFo AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, 
                      T_T_Heasb.THESABID AS HesabID, T_T_Heasb.THESABNumber AS CodeHesab, 2 AS Expr10, 0 AS Expr3, 0 AS Expr4, NULL AS Expr5, NULL AS Expr6, NULL AS Expr11, NULL AS Expr12, NULL 
                      AS Expr13, 0 AS Expr7, 0 AS Expr8, NULL AS Expr14, NULL AS Expr15, NULL AS Expr9, NULL AS Expr1, 0 AS Expr16
FROM         T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_T_Heasb ON T_Forosh.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID AS HESABID
                             FROM         [TRANSACTION] AS Tran1
                             WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                             GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.HESABID
WHERE     ISNULL(T_Forosh.Maliyat,0)>0

	)    




GO





/****** Object:  UserDefinedFunction [dbo].[Fun_Sood_Zian_Kala]    Script Date: 08/21/2017 12:15:09 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Fun_Sood_Zian_Kala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[Fun_Sood_Zian_Kala]
GO


/****** Object:  UserDefinedFunction [dbo].[Fun_Sood_Zian_Kala]    Script Date: 08/21/2017 12:15:09 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE FUNCTION [dbo].[Fun_Sood_Zian_Kala](@startDate nvarchar(50), @enddate nvarchar(50)) RETURNS TABLE AS RETURN ( SELECT     dbo.[TRANSACTION].KALAID, CASE WHEN SUM(FieldN5) < 0 THEN abs(SUM(FieldN5)) ELSE 0 END AS Zian, CASE WHEN SUM(FieldN5)                        > 0 THEN SUM(FieldN5) ELSE 0 END AS Sood FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID INNER JOIN                       dbo.PFrm_Rep_SoodKala_Mojoodi ON dbo.T_NameKala.ID = dbo.PFrm_Rep_SoodKala_Mojoodi.KALAID WHERE     (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) GROUP BY dbo.[TRANSACTION].KALAID ) 
GO





/****** Object:  UserDefinedFunction [dbo].[FunSubKardexKala]    Script Date: 08/21/2017 12:19:53 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FunSubKardexKala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[FunSubKardexKala]
GO


/****** Object:  UserDefinedFunction [dbo].[FunSubKardexKala]    Script Date: 08/21/2017 12:19:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


 CREATE FUNCTION  [dbo].[FunSubKardexKala]() RETURNS TABLE  AS RETURN  ( 
 	
 	
SELECT     'مانده اول دوره' AS NoeFa1, 0 AS GOID, 0 AS KolID, 0 AS HesabID, 0 AS THesabID, NameKalaID AS KALAID, dbo.F1StartDate() AS Tarikh, 0 AS MMande, UnitPrice * Mojodi AS MVarde, 0 AS MSadre, 
                      0 AS ShomareFactor, 0 AS Tedad, Mojodi AS Varde, 0 AS Sadre, 0 AS PHI_M, AID, GOKID, '--' AS Namehesab, 9 AS NoeFa, FieldN1 AS Tm, 0 AS FieldN3
FROM         T_Kala
WHERE     (Mojodi > 0)
UNION ALL
SELECT     CASE WHEN (KHARIDID) IS NOT NULL THEN 'خريد ' WHEN (FOROSHID) IS NOT NULL THEN 'فروش ' WHEN (PARID7) IS NOT NULL THEN 'ب.فروش شماره ' WHEN (DARID8) IS NOT NULL 
                      THEN 'ب.خريد ' WHEN (VORODID) IS NOT NULL THEN 'رسيد انبار ' WHEN (KHOROJID) IS NOT NULL THEN 'حواله انبار ' WHEN (ID1) IS NOT NULL THEN 'جابه جايی ' ELSE '' END AS NoeFa1, GOID, KOLID, 
                      HESABID, THESABID, KALAID, Date1 AS Tarikh, (CASE WHEN ((KHARIDID) IS NOT NULL OR
                      (PARID7) IS NOT NULL OR
                      (VORODID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) - (CASE WHEN ((FOROSHID) IS NOT NULL OR
                      (DARID8) IS NOT NULL OR
                      (KHOROJID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) AS MMande, (CASE WHEN ((KHARIDID) IS NOT NULL OR
                      (PARID7) IS NOT NULL OR
                      (VORODID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) AS MVarde, (CASE WHEN ((FOROSHID) IS NOT NULL OR
                      (dbo.[TRANSACTION].DARID8) IS NOT NULL OR
                      (KHOROJID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) AS MSadre, OperationCode AS ShomareFactor, (TedadKh + TedadVorood + TedadBarFo + TedadVA) 
                      - (TedadFo + TedadKhorooj + TedadBarKh + TedadKhA) AS Tedad, TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
                      0 AS PHI_M, AID, GOKID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, CASE WHEN (KHARIDID) IS NOT NULL THEN 1 WHEN (FOROSHID) IS NOT NULL THEN 2 WHEN (PARID7) 
                      IS NOT NULL THEN 4 WHEN (DARID8) IS NOT NULL THEN 3 WHEN (VORODID) IS NOT NULL THEN 5 WHEN (KHOROJID) IS NOT NULL THEN 6 WHEN (ID1) IS NOT NULL 
                      THEN 7 ELSE 8 END AS NoeFa, CurRef AS Tm, FieldN3
FROM         [TRANSACTION]
WHERE     (NOT (KALAID IS NULL)) ) 



GO










/****** Object:  UserDefinedFunction [dbo].[QMande_HesabKol_SanadHesabdary]    Script Date: 03/04/2018 08:51:28 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QMande_HesabKol_SanadHesabdary]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[QMande_HesabKol_SanadHesabdary]
GO



/****** Object:  UserDefinedFunction [dbo].[QMande_HesabKol_SanadHesabdary]    Script Date: 03/04/2018 08:51:28 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



    CREATE FUNCTION [dbo].[QMande_HesabKol_SanadHesabdary] (@said int = NULL,@Flag1 bit) RETURNS TABLE AS RETURN ( 
    
SELECT     SUM(View_TRANSACTION.PARBes) AS PARBes, SUM(View_TRANSACTION.DARBed) AS DARBed, T_KolHesab.KOLID, T_KolHesab.KolNumber, T_KolHesab.NameHesab, CONVERT(money, 
                      ISNULL(SUM(View_TRANSACTION.DARBed), 0) - ISNULL(SUM(View_TRANSACTION.PARBes), 0), 1) AS Mande, T_KolHesab.GOID
FROM         (SELECT     View_TRANSACTION.*
FROM         View_TRANSACTION
 WHERE     (ISNULL(View_TRANSACTION.SAID2, - 1) <> @said)) as View_TRANSACTION RIGHT OUTER JOIN
                      T_KolHesab ON View_TRANSACTION.KOLID = T_KolHesab.KOLID
GROUP BY T_KolHesab.KOLID, T_KolHesab.KolNumber, T_KolHesab.NameHesab, T_KolHesab.GOID
 )   


GO




/****** Object:  UserDefinedFunction [dbo].[QMande_HesabMoin_SanadHesabdary]    Script Date: 03/04/2018 08:51:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QMande_HesabMoin_SanadHesabdary]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[QMande_HesabMoin_SanadHesabdary]
GO



/****** Object:  UserDefinedFunction [dbo].[QMande_HesabMoin_SanadHesabdary]    Script Date: 03/04/2018 08:51:33 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



    CREATE FUNCTION [dbo].[QMande_HesabMoin_SanadHesabdary] (@said int = NULL,@Flag1 bit) RETURNS TABLE AS RETURN ( 
SELECT     SUM(View_TRANSACTION.PARBes) AS PARBes, SUM(View_TRANSACTION.DARBed) AS DARBed, T_Heasb.HESABID, T_Heasb.HESABNumber, T_Heasb.NameHesab AS NameHesabMoin, 
                      CONVERT(money, ISNULL(SUM(View_TRANSACTION.DARBed), 0) - ISNULL(SUM(View_TRANSACTION.PARBes), 0), 1) AS Mande, T_Heasb.KolID
FROM         (SELECT     View_TRANSACTION.*
FROM         View_TRANSACTION
 WHERE     (ISNULL(View_TRANSACTION.SAID2, - 1) <> @said)) as View_TRANSACTION RIGHT OUTER JOIN
                      T_Heasb ON View_TRANSACTION.HESABID = T_Heasb.HESABID
GROUP BY T_Heasb.HESABID, T_Heasb.HESABNumber, T_Heasb.NameHesab, T_Heasb.KolID
    
    ) 


GO





/****** Object:  UserDefinedFunction [dbo].[QMande_HesabTafsili_SanadHesabdary]    Script Date: 08/28/2019 12:52:19 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QMande_HesabTafsili_SanadHesabdary]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[QMande_HesabTafsili_SanadHesabdary]
GO



/****** Object:  UserDefinedFunction [dbo].[QMande_HesabTafsili_SanadHesabdary]    Script Date: 08/28/2019 12:52:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



    CREATE FUNCTION [dbo].[QMande_HesabTafsili_SanadHesabdary] (@said int = NULL,@Flag1 bit) RETURNS TABLE AS RETURN (
    
SELECT     SUM(View_TRANSACTION.PARBes) AS PARBes, SUM(View_TRANSACTION.DARBed) AS DARBed, T_T_Heasb.THESABID, T_T_Heasb.Mob, T_T_Heasb.Phone1, T_T_Heasb.Phone2, 
                      T_T_Heasb.NameHesabT, T_T_Heasb.NameHesabT + N'   ' + ISNULL(T_T_Heasb.Mob, N'') + N'    ' + ISNULL(T_T_Heasb.Phone1, N'') + N'   ' + ISNULL(T_T_Heasb.Phone2, N'') AS Onvane, 
                      CONVERT(money, ISNULL(SUM(View_TRANSACTION.DARBed), 0) - ISNULL(SUM(View_TRANSACTION.PARBes), 0), 1) AS Mande, T_T_Heasb.THESABNumber, T_T_Heasb.HESABID, 
                      T_T_Heasb.Active, View_TRANSACTION.KOLID
FROM         (SELECT     TRAID, GOID, KOLID, HESABID, THESABID, ThesabIDSath2, SAID2, DateCreate, DateModified, RadifSAID2, TextNHSID2, TextShSA, PARBes, DARBed, Sharh, radif, Date1, Dateeng, 
                                              SAIDType, CodGroup, NameGo, NameHesab, KolNumber, HESABNumber, NameHesabMoin, THESABNumber, NameHesabT, THESABNumberSath2, NameHesabTSath2, Mob, Phone1, 
                                              Phone2, Active
                        FROM         View_TRANSACTION AS View_TRANSACTION_1
                        WHERE     (ISNULL(SAID2, - 1) <> @said)) AS View_TRANSACTION RIGHT OUTER JOIN
                      T_T_Heasb ON View_TRANSACTION.THESABID = T_T_Heasb.THESABID
GROUP BY T_T_Heasb.THESABID, T_T_Heasb.Mob, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.NameHesabT, T_T_Heasb.NameHesabT + N'   ' + ISNULL(T_T_Heasb.Mob, N'') 
                      + N'    ' + ISNULL(T_T_Heasb.Phone1, N'') + N'   ' + ISNULL(T_T_Heasb.Phone2, N''), T_T_Heasb.THESABNumber, T_T_Heasb.HESABID, T_T_Heasb.Active, View_TRANSACTION.KOLID    )      


GO






/****** Object:  UserDefinedFunction [dbo].[QMande_HesabTafsili_SanadHesabdarySath2SF3]    Script Date: 03/04/2018 08:51:42 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QMande_HesabTafsili_SanadHesabdarySath2SF3]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[QMande_HesabTafsili_SanadHesabdarySath2SF3]
GO



/****** Object:  UserDefinedFunction [dbo].[QMande_HesabTafsili_SanadHesabdarySath2SF3]    Script Date: 03/04/2018 08:51:42 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




    CREATE FUNCTION [dbo].[QMande_HesabTafsili_SanadHesabdarySath2SF3] (@said int = NULL,@Flag1 bit) RETURNS TABLE AS RETURN (
 
SELECT     SUM(View_TRANSACTION.PARBes) AS PARBes, SUM(View_TRANSACTION.DARBed) AS DARBed, T_T_HeasbSath2.THESABID AS THESABIDSath2, T_T_HeasbSath2.Mob, T_T_HeasbSath2.Phone1, 
                      T_T_HeasbSath2.Phone2, T_T_HeasbSath2.NameHesabT, T_T_HeasbSath2.NameHesabT + N'   ' + ISNULL(T_T_HeasbSath2.Mob, N'') + N'    ' + ISNULL(T_T_HeasbSath2.Phone1, N'') 
                      + N'   ' + ISNULL(T_T_HeasbSath2.Phone2, N'') AS Onvane, CONVERT(money, ISNULL(SUM(View_TRANSACTION.DARBed), 0) - ISNULL(SUM(View_TRANSACTION.PARBes), 0), 1) AS Mande, 
                      T_T_HeasbSath2.THESABNumber, T_T_HeasbSath2.Active
FROM         (SELECT     View_TRANSACTION.*
FROM         View_TRANSACTION
 WHERE     (ISNULL(View_TRANSACTION.SAID2, - 1) <> @said)) AS View_TRANSACTION RIGHT OUTER JOIN
                      T_T_HeasbSath2 ON View_TRANSACTION.THESABID = T_T_HeasbSath2.THESABID
GROUP BY T_T_HeasbSath2.THESABID, T_T_HeasbSath2.Mob, T_T_HeasbSath2.Phone1, T_T_HeasbSath2.Phone2, T_T_HeasbSath2.NameHesabT, 
                      T_T_HeasbSath2.NameHesabT + N'   ' + ISNULL(T_T_HeasbSath2.Mob, N'') + N'    ' + ISNULL(T_T_HeasbSath2.Phone1, N'') + N'   ' + ISNULL(T_T_HeasbSath2.Phone2, N''), 
                      T_T_HeasbSath2.THESABNumber, T_T_HeasbSath2.Active  )      



GO










/****** Object:  UserDefinedFunction [dbo].[F_EditMojodiKala]    Script Date: 08/21/2017 12:33:26 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F_EditMojodiKala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F_EditMojodiKala]
GO


/****** Object:  UserDefinedFunction [dbo].[F_EditMojodiKala]    Script Date: 08/21/2017 12:33:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[F_EditMojodiKala](@KalaID nvarchar(50),
@GOKID int,
@AID nvarchar(255))
RETURNS TABLE
AS
RETURN ( SELECT  TOP 100 PERCENT dbo.T_Kala.Mojodi, dbo.T_Kala.UnitPrice, dbo.T_Kala.NameKalaID, dbo.T_Kala.GOKID, dbo.T_Kala.AID, dbo.T_NameKala.NameKala1, dbo.T_Anbar.NameAnbar, @KALAID AS Expr1, @GOKID AS Expr2, @AID AS Expr3, 
               dbo.T_NameGoKala.NameGoKala1, dbo.T_NameKala.KalaNumber
FROM     dbo.T_Kala INNER JOIN
               dbo.T_NameKala ON dbo.T_Kala.NameKalaID = dbo.T_NameKala.ID INNER JOIN
               dbo.T_Anbar ON dbo.T_Kala.AID = dbo.T_Anbar.AID INNER JOIN
               dbo.T_NameGoKala ON dbo.T_Kala.GOKID = dbo.T_NameGoKala.ID
WHERE  (@KALAID IS NULL) AND (@GOKID IS NULL) AND (@AID IS NULL) OR
               (@KALAID IS NOT NULL) AND (@GOKID IS NULL) AND (@AID IS NULL) AND (dbo.T_Kala.NameKalaID = @KALAID) OR
               (@KALAID IS NULL) AND (@GOKID IS NOT NULL) AND (@AID IS NULL) AND (dbo.T_Kala.GOKID = @GOKID) OR
               (@KALAID IS NOT NULL) AND (@GOKID IS NOT NULL) AND (@AID IS NULL) AND (dbo.T_Kala.NameKalaID = @KALAID) AND (dbo.T_Kala.GOKID = @GOKID) OR
               (@KALAID IS NULL) AND (@GOKID IS NULL) AND (@AID IS NOT NULL) AND (dbo.T_Kala.AID = @AID) OR
               (@KALAID IS NOT NULL) AND (@GOKID IS NULL) AND (@AID IS NOT NULL) AND (dbo.T_Kala.NameKalaID = @KALAID) AND (dbo.T_Kala.AID = @AID) OR
               (@KALAID IS NULL) AND (@GOKID IS NOT NULL) AND (@AID IS NOT NULL) AND (dbo.T_Kala.GOKID = @GOKID) AND (dbo.T_Kala.AID = @AID) OR
               (@KALAID IS NOT NULL) AND (@GOKID IS NOT NULL) AND (@AID IS NOT NULL) AND (dbo.T_Kala.NameKalaID = @KALAID) AND (dbo.T_Kala.GOKID = @GOKID) AND (dbo.T_Kala.AID = @AID)
ORDER BY dbo.T_NameKala.NameKala1, dbo.T_Anbar.NameAnbar, dbo.T_NameGoKala.NameGoKala1 )

GO






/****** Object:  UserDefinedFunction [dbo].[PHiMianginKharid]    Script Date: 08/21/2017 12:34:11 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PHiMianginKharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[PHiMianginKharid]
GO


/****** Object:  UserDefinedFunction [dbo].[PHiMianginKharid]    Script Date: 08/21/2017 12:34:11 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE FUNCTION [dbo].[PHiMianginKharid] 	(@KALAID0 int,@Startdate int) RETURNS Money  AS 	   BEGIN 		  /*DECLARE @RasidList Varchar(50)*/ 	 	 	  	IF(@Startdate=-1) 		SET @Startdate = dbo.F1EndDate() 		 	IF(@Startdate=0) 		SET @Startdate = dbo.F1StartDate() 		 			DECLARE CUR CURSOR FOR 	 			 						SELECT     Kardex.Tarikh, Kardex.KALAID, Kardex.Varde, Kardex.Sadre, CONVERT(money, Kardex.MVarde) AS Mvarde, 0 AS PHI_M, 0 AS TM, Kardex.MMande,  				CONVERT(money, Kardex.MSadre) AS MSadre 				FROM         dbo.FunSubKardexKala() AS Kardex INNER JOIN 				T_NameKala ON Kardex.KALAID = T_NameKala.ID WHERE     (Kardex.KALAID = @KALAID0) AND (Kardex.Tarikh <= @Startdate) ORDER BY Kardex.Tarikh, Kardex.Sadre     			 	 	   OPEN CUR     	  	   	 		DECLARE @Tarikh NVARCHAR(4000) 		DECLARE @KALAID bigint 		DECLARE @TM decimal(19,4) 		DECLARE @TedadVarde decimal(19,4) 		DECLARE @TedadSadre decimal(19,4) 		DECLARE @MSadre decimal(19,4) 		DECLARE @Mvarde decimal(19,4) 		DECLARE @PHI_M decimal(19,4) 		DECLARE @PHI_FO decimal(19,4) 		DECLARE @MMande decimal(19,4) 	   	 	 		DECLARE @ColKalaID_CH bigint 		DECLARE @MTedadi decimal(19,4) 		DECLARE @MRiali decimal(19,4)   	   								 		Set @PHI_FO=0 		set @ColKalaID_CH=0 		set @PHI_M=0 		set @MTedadi=0 		set @MRiali=0	 		 		 	 	   FETCH NEXT FROM CUR INTO @Tarikh,@KALAID,@TedadVarde,@TedadSadre,@Mvarde,@PHI_M, @TM, @MMande, @MSadre	 	 	   WHILE @@FETCH_STATUS = 0 				 		 		   BEGIN 				      			  			Set @MTedadi=@MTedadi+(@TedadVarde-@TedadSadre) 			 			set @MRiali=@MRiali+@Mvarde-(@PHI_FO * @TedadSadre) 				  			if (@TedadVarde<>0) and (@MTedadi<>0) 				Begin 					Set @PHI_M =Convert(money, round(ABS(@MRiali/@MTedadi),0)) 				End 			 			if (@TedadSadre<>0) 				Begin 					Set @PHI_M=@PHI_FO  					set @MRiali=@PHI_FO *@MTedadi 				End  			if (@MTedadi=0) 				Set @PHI_M = @PHI_FO  				 			Set @PHI_FO =@PHI_M 				 			FETCH NEXT FROM CUR INTO @Tarikh,@KALAID,@TedadVarde,@TedadSadre,@Mvarde,@PHI_M, @TM, @MMande,@MSadre			 		 			 		   END 			 			 	    CLOSE CUR  		 	    DEALLOCATE CUR       if (Isnull(@PHI_FO,0)=0)	Set  @PHI_FO = 1     RETURN @PHI_FO    END          
GO




/****** Object:  UserDefinedFunction [dbo].[FunSub_05239042]    Script Date: 08/21/2017 12:34:46 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FunSub_05239042]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[FunSub_05239042]
GO


/****** Object:  UserDefinedFunction [dbo].[FunSub_05239042]    Script Date: 08/21/2017 12:34:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


 CREATE FUNCTION  [dbo].[FunSub_05239042](@startDate NVARCHAR(255),@enddate NVARCHAR(255)) RETURNS TABLE  AS RETURN  ( 
 	
 	
SELECT     [TRANSACTION].HESABID, [TRANSACTION].THESABID, [TRANSACTION].KOLID, [TRANSACTION].GOID, CASE WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
                      THEN FOROSHID WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN KHOROJID ELSE NULL END AS IDAUtoFact, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN FOROSHID WHEN (dbo.[TRANSACTION].[PARID7]) 
                      IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
                      THEN VORODID WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN KHOROJID WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ID1 ELSE '""' END AS sho, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN 'خريد' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 'فروش' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN 'ب.فروش' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 'ب.خريد' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN 'رسيد انبار' WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN 'حواله انبار' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 'جابه جاي' ELSE '' END AS NoeFa, 
                      [TRANSACTION].TedadFo + [TRANSACTION].TedadKhorooj + [TRANSACTION].TedadBarKh + [TRANSACTION].TedadKhA AS Sadre, [TRANSACTION].FieldN3, T_NameKala.VAHED1, 
                      [TRANSACTION].KALAID, [TRANSACTION].AID, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
                      THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
                      THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 7 ELSE '' END AS NoFAKTOR, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS namehesab, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].Date1, 
                      T_NameKala.KalaNumber, T_NameKala.NameKala1, RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].KOLID), 4) 
                      + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].HESABID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].THESABID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      [TRANSACTION].AID), 4) AS Expr5, [TRANSACTION].GOKID
FROM         T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID
WHERE     (CASE WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN FOROSHID WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN KHOROJID ELSE NULL END IS NOT NULL) AND ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)
 
 
 ) 



GO



/****** Object:  UserDefinedFunction [dbo].[FunSub_05239043]    Script Date: 08/21/2017 12:34:51 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FunSub_05239043]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[FunSub_05239043]
GO


/****** Object:  UserDefinedFunction [dbo].[FunSub_05239043]    Script Date: 08/21/2017 12:34:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


 CREATE FUNCTION  [dbo].[FunSub_05239043](@startDate NVARCHAR(255),@enddate NVARCHAR(255)) RETURNS TABLE  AS RETURN  ( 
 	
 	
SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN 'خريد' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 'فروش' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN 'ب.فروش' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 'ب.خريد' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN 'رسيد انبار' WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN 'حواله انبار' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 'جابه جاي' ELSE '' END AS NoeFa, ROUND(CONVERT(float, 
                      ([TRANSACTION].TedadKh + [TRANSACTION].TedadVorood + [TRANSACTION].TedadBarFo + [TRANSACTION].TedadVA) 
                      - ([TRANSACTION].TedadFo + [TRANSACTION].TedadKhorooj + [TRANSACTION].TedadBarKh + [TRANSACTION].TedadKhA)), 2) AS Mande, ROUND(CONVERT(float, 
                      [TRANSACTION].TedadKh + [TRANSACTION].TedadVorood + [TRANSACTION].TedadBarFo + [TRANSACTION].TedadVA), 2) AS Varde, ROUND(CONVERT(float, 
                      [TRANSACTION].TedadFo + [TRANSACTION].TedadKhorooj + [TRANSACTION].TedadBarKh + [TRANSACTION].TedadKhA), 2) AS Sadre, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL 
                      THEN unitpricekh WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN unitpriceBarfo WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
                      THEN unitpricefo WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN unitpricebarkh ELSE NULL END AS Fi, RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) 
                      + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].HESABID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      [TRANSACTION].THESABID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].THESABID), 4) AS Expr5, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL 
                      THEN KHARIDID WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN VORODID ELSE NULL END AS Expr1, 
                      [TRANSACTION].Date1 AS MinOfDate1, [TRANSACTION].Date1 AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, 
                      [TRANSACTION].THESABID) AS namehesab, T_NameKala.KalaNumber, [TRANSACTION].KALAID, T_NameKala.NameKala1, [TRANSACTION].AID, [TRANSACTION].GOKID, [TRANSACTION].Date1
FROM         T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID
WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND (CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[PARID7]) 
                      IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN VORODID ELSE NULL END IS NOT NULL)                      )

GO




/****** Object:  UserDefinedFunction [dbo].[FunSub_05239044]    Script Date: 08/21/2017 12:34:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FunSub_05239044]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[FunSub_05239044]
GO



/****** Object:  UserDefinedFunction [dbo].[FunSub_05239044]    Script Date: 08/21/2017 12:34:56 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


 Create FUNCTION  [dbo].[FunSub_05239044](@startDate NVARCHAR(255),@enddate NVARCHAR(255)) RETURNS TABLE  AS RETURN  ( 
 	
 	

SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[VORODID]) 
                      IS NOT NULL THEN VORODID ELSE NULL END AS Expr1, [TRANSACTION].HESABID, [TRANSACTION].THESABID, [TRANSACTION].KOLID, [TRANSACTION].GOID, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN FOROSHID WHEN (dbo.[TRANSACTION].[PARID7]) 
                      IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
                      THEN VORODID WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN KHOROJID WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ID1 ELSE '""' END AS sh, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN 'خريد' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 'فروش' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN 'ب.فروش' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 'ب.خريد' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN 'رسيد انبار' WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN 'حواله انبار' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 'جابه جاي' ELSE '' END AS NoeFa, SUM(ROUND(CONVERT(float, 
                      [TRANSACTION].TedadKh + [TRANSACTION].TedadVorood + [TRANSACTION].TedadBarFo), 2)) AS Varde, [TRANSACTION].FieldN3, RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) 
                      + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].HESABID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      [TRANSACTION].THESABID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].AID), 4) AS Expr5, T_NameKala.VAHED1, [TRANSACTION].KALAID, [TRANSACTION].AID, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL 
                      THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 7 ELSE '' END AS NoFAKTOR, MIN([TRANSACTION].Date1) AS MinOfDate1, MAX([TRANSACTION].Date1) AS MaxOfDate1, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS namehesab, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].Date1, 
                      T_NameKala.KalaNumber, T_NameKala.NameKala1
FROM         T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID
WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)
GROUP BY CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN 'خريد' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 'فروش' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN 'ب.فروش' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 'ب.خريد' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN 'رسيد انبار' WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN 'حواله انبار' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 'جابه جاي' ELSE '' END, [TRANSACTION].AID, [TRANSACTION].KALAID, [TRANSACTION].HESABID, 
                      [TRANSACTION].THESABID, [TRANSACTION].GOID, [TRANSACTION].KOLID, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[PARID7]) 
                      IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN VORODID ELSE NULL END, RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) 
                      + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].HESABID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      [TRANSACTION].THESABID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].AID), 4), CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL 
                      THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
                      THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL 
                      THEN 7 ELSE '' END, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID), [TRANSACTION].FieldN3, T_NameKala.VAHED1, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN FOROSHID WHEN (dbo.[TRANSACTION].[PARID7]) 
                      IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
                      THEN VORODID WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN KHOROJID WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ID1 ELSE '""' END, [TRANSACTION].Date1, 
                      T_NameKala.KalaNumber, T_NameKala.NameKala1
HAVING      (CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[VORODID]) 
                      IS NOT NULL THEN VORODID ELSE NULL END IS NOT NULL) OR
                      (CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[VORODID]) 
                      IS NOT NULL THEN VORODID ELSE NULL END IS NOT NULL)

                )

GO




/****** Object:  UserDefinedFunction [dbo].[FunSub_05239045]    Script Date: 08/21/2017 12:35:01 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FunSub_05239045]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[FunSub_05239045]
GO

/****** Object:  UserDefinedFunction [dbo].[FunSub_05239045]    Script Date: 08/21/2017 12:35:01 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


 CREATE FUNCTION  [dbo].[FunSub_05239045](@startDate NVARCHAR(255),@enddate NVARCHAR(255)) RETURNS TABLE  AS RETURN  ( 
 	
 	


SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN 'خريد' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 'فروش' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN 'ب.فروش' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 'ب.خريد' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN 'رسيد انبار' WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN 'حواله انبار' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 'جابه جاي' ELSE '' END AS NoeFa, 
                      [TRANSACTION].TedadKh + [TRANSACTION].TedadVorood + [TRANSACTION].TedadBarFo + [TRANSACTION].TedadVA AS Varde, 
                      [TRANSACTION].TedadFo + [TRANSACTION].TedadKhorooj + [TRANSACTION].TedadBarKh + [TRANSACTION].TedadKhA AS Sadre, [TRANSACTION].AID, [TRANSACTION].KALAID, 
                      CASE WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN FOROSHID WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN KHOROJID ELSE NULL END AS Expr1, RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].KOLID), 4) 
                      + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].HESABID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].THESABID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      [TRANSACTION].AID), 4) AS Expr5, [TRANSACTION].Date1 AS MinOfDate1, [TRANSACTION].Date1 AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS namehesab, T_NameKala.KalaNumber, T_NameKala.NameKala1, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL 
                      THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 7 ELSE '' END AS NoeFact, [TRANSACTION].GOKID, [TRANSACTION].Date1
FROM         T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID
WHERE     (CASE WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN FOROSHID WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN KHOROJID ELSE NULL END IS NOT NULL) AND ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)
 )

GO




/****** Object:  UserDefinedFunction [dbo].[PersianToGregorian]    Script Date: 08/21/2017 12:37:08 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PersianToGregorian]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[PersianToGregorian]
GO


/****** Object:  UserDefinedFunction [dbo].[PersianToGregorian]    Script Date: 08/21/2017 12:37:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[PersianToGregorian](@DateStr varchar(10))
RETURNS DATEtime
AS 
BEGIN
   declare @YYear int
   declare @MMonth int
   declare @DDay int
   declare @epbase int
   declare @epyear int
   declare @mdays int
   declare @persian_jdn int
   declare @i int
   declare @j int
   declare @l int
   declare @n int
   declare @TMPRESULT varchar(10)
   declare @IsValideDate int
   declare @TempStr varchar(20)
   DECLARE @TmpDateStr varchar(10)

   SET @i=charindex('/',@DateStr)

   IF LEN(@DateStr) - CHARINDEX('/', @DateStr,CHARINDEX('/', @DateStr,1)+1) = 4
   BEGIN
   /* SET @TmpDateStr = dbo.ReversDate(@DateStr)*/
     IF ( ISDATE(@TmpDateStr) =1 ) 
       RETURN @TmpDateStr
     ELSE
        RETURN NULL
   END
   ELSE
     SET @TmpDateStr = @DateStr

   IF ((@i<>0) and
     /* (dbo.SubStrCount('/', @TmpDateStr)=2) and*/
        (ISNUMERIC(REPLACE(@TmpDateStr,'/',''))=1) and
        (charindex('.',@TmpDateStr)=0)
       )
   BEGIN
       SET @YYear=CAST(SUBSTRING(@TmpDateStr,1,@i-1) AS INT)
                IF ( @YYear< 1300 )
                      SET @YYear =@YYear + 1300
                IF @YYear > 9999
                  RETURN NULL
   
       SET @TempStr= SUBSTRING(@TmpDateStr,@i+1,Len(@TmpDateStr))
   
       SET @i=charindex('/',@TempStr)
       SET @MMonth=CAST(SUBSTRING(@TempStr,1,@i-1) AS INT)
       SET @MMonth=@MMonth /*-- -1*/
      
       SET @TempStr= SUBSTRING(@TempStr,@i+1,Len(@TempStr))  
   
       SET @DDay=CAST(@TempStr AS INT)
       SET @DDay=@DDay/*-- - 1*/
          
                 IF ( @YYear >= 0 )
                     SET @epbase = @YYear - 474
                 Else
                     SET @epbase = @YYear - 473
                 SET @epyear = 474 + (@epbase % 2820)

        IF (@MMonth <= 7 )
                       SET @mdays = ((@MMonth) - 1) * 31
        Else
            SET @mdays = ((@MMonth) - 1) * 30 + 6

        SET @persian_jdn =(@DDay)  + @mdays + CAST((((@epyear * 682) - 110) / 2816) as int)  + (@epyear - 1) * 365  +  CAST((@epbase / 2820)  as int ) * 1029983  + (1948321 - 1)

        IF (@persian_jdn > 2299160)
                 BEGIN
            SET @l = @persian_jdn + 68569
            SET @n = CAST(((4 * @l) / 146097) as int)
            SET @l = @l -  CAST(((146097 * @n + 3) / 4) as int)
            SET @i =  CAST(((4000 * (@l + 1)) / 1461001) as int)
            SET @l = @l - CAST( ((1461 * @i) / 4) as int) + 31
            SET @j =  CAST(((80 * @l) / 2447) as int)
            SET @DDay = @l - CAST( ((2447 * @j) / 80) as int)
             SET @l =  CAST((@j / 11) as int)
            SET @MMonth = @j + 2 - 12 * @l
            SET @YYear = 100 * (@n - 49) + @i + @l
                 END

           SET @TMPRESULT=Cast(@MMonth as varchar(2))+'/'+CAST(@DDay as Varchar(2))+'/'+CAST(@YYear as varchar(4)) 
           RETURN Cast(@TMPRESULT as DATETIME)

    END
    RETURN NULL     

END


GO




/****** Object:  UserDefinedFunction [dbo].[KalaName]    Script Date: 08/21/2017 12:37:00 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[KalaName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[KalaName]
GO


/****** Object:  UserDefinedFunction [dbo].[KalaName]    Script Date: 08/21/2017 12:37:00 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE FUNCTION [dbo].[KalaName] 	( 	@KalaID int 	) RETURNS NVarchar(255)  AS 	BEGIN 		/*DECLARE @RasidList NVarchar(50)*/ 		DECLARE CUR CURSOR FOR SELECT NameKala1 FROM T_NameKala WHERE ID = @KalaID 		 		OPEN CUR 		DECLARE @NameKala1  NVarchar(255) 		DECLARE @SUM NVarchar(255) 		SET @SUM = '' 		 		FETCH NEXT FROM CUR INTO @NameKala1 			WHILE @@FETCH_STATUS = 0 				BEGIN 				     SET @SUM =  @NameKala1 				      FETCH NEXT FROM CUR INTO @NameKala1 				END 			 			CLOSE CUR  		DEALLOCATE CUR  	RETURN @SUM 	END           
GO




/****** Object:  UserDefinedFunction [dbo].[KalaKhadamatName]    Script Date: 08/21/2017 12:36:55 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[KalaKhadamatName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[KalaKhadamatName]
GO
/****** Object:  UserDefinedFunction [dbo].[KalaKhadamatName]    Script Date: 08/21/2017 12:36:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE FUNCTION [dbo].[KalaKhadamatName] 	( 	@KalaID int 	) RETURNS Varchar(255)  AS 	BEGIN 		 		DECLARE CUR CURSOR FOR SELECT Name FROM T_NameKhad WHERE NAMEKHID = @KalaID 		 		OPEN CUR 		DECLARE @Name  Varchar(255) 		DECLARE @SUM Varchar(255) 		SET @SUM = '' 		 		FETCH NEXT FROM CUR INTO @Name 			WHILE @@FETCH_STATUS = 0 				BEGIN 				     SET @SUM =  @Name 				      FETCH NEXT FROM CUR INTO @Name 				END 			 			CLOSE CUR  		DEALLOCATE CUR 		 	RETURN @SUM 	END    
GO
/****** Object:  View [dbo].[QSubPfrm_Kharid]    Script Date: 10/06/2018 09:00:41 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_Kharid]'))
DROP VIEW [dbo].[QSubPfrm_Kharid]
GO
/****** Object:  View [dbo].[QSubPfrm_Kharid]    Script Date: 10/06/2018 09:00:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[QSubPfrm_Kharid]
AS
SELECT  TOP (100) PERCENT TRID, AID, KALAID, Sharh, Date1, KHARIDID, Tedad, UnitPrice, CONVERT(money, ISNULL(Tedad, 0) * ISNULL(UnitPrice, 0)) AS Mablaghnakhales, radif, GOKID, LinkedID, TextKalaName, 
               SumofPrice, FieldN6, OperType, Flag1, DARBed, PARBes, FieldN2, FieldN3, FieldN4, FieldN5, Tedad2, CONVERT(money, ISNULL(Tedad, 0) * ISNULL(UnitPrice, 0) + ISNULL(FieldN1, 0) + ISNULL(FieldN2, 0) 
               - ISNULL(FieldN7, 0)) AS Mablaghkhales, FieldT1, FieldN1, FieldN7, TextAID, TextNumberVO, Zarib, Tedad1, CONVERT(money, ISNULL(Tedad, 0) * ISNULL(UnitPrice, 0)) AS Sumation, TRAID, Avarez, Maliatkala, 
               Ezafat, Tedad1User, Tedad2User, UnitPriceUser, ZaribChildUser, KalaIDChild, FieldT2, FieldT3, FieldT4, FieldT5, FieldT6, FieldT7, FieldT8, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, Flag9, Flag10, THESABID, 
               GOID, KOLID, HESABID, DARID8, DARID7, DARID6, DARID5, DARID4, DARID3, DARID2, DARID1, UnitPriceBarFo , CONVERT(money, ISNULL(Tedad, 0) * ISNULL(UnitPriceBarFo, 0)) AS SumRadifVazn
FROM     TRANSACTION2
ORDER BY radif

GO
/****** Object:  View [dbo].[QSubPfrm_Khadamat]    Script Date: 08/21/2017 12:41:11 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_Khadamat]'))
DROP VIEW [dbo].[QSubPfrm_Khadamat]
GO
/****** Object:  View [dbo].[QSubPfrm_Khadamat]    Script Date: 08/21/2017 12:41:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[QSubPfrm_Khadamat]
AS
SELECT     TOP (100) PERCENT AID, KALAID, Sharh, Date1, KHARIDID, Tedad, UnitPrice, CONVERT(money, Tedad * UnitPrice) AS Sumation, radif, GOKID, CONVERT(nvarchar(20), AID) 
                      + CONVERT(nvarchar(20), GOKID) + CONVERT(nvarchar(20), KALAID) AS Expr1, LinkedID, TextKalaName, SumofPrice, FieldN6, OperType, DARBed, PARBes
FROM         dbo.TRANSACTION2
ORDER BY radif
GO
/****** Object:  StoredProcedure [dbo].[CQ_ListGroupKala]    Script Date: 01/22/2018 10:23:45 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListGroupKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListGroupKala]
GO
/****** Object:  StoredProcedure [dbo].[CQ_ListGroupKala]    Script Date: 01/22/2018 10:23:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


       CREATE PROCEDURE [dbo].[CQ_ListGroupKala] (@AID int) AS 
       
       IF ((@aID IS NULL) OR (@aID=0))
       
SELECT     ID, NameGoKalanumbr AS [كد گروه], NameGoKala1 AS [عنوان گروه], NameGoKalanumbr, NameGoKala1
FROM         T_NameGoKala
GROUP BY ID, NameGoKalanumbr, NameGoKala1
ORDER BY [عنوان گروه]

       else
SELECT     T_NameGoKala.ID, T_NameGoKala.NameGoKalanumbr AS [كد گروه], T_NameGoKala.NameGoKala1 AS [عنوان گروه], T_NameGoKala.NameGoKalanumbr, T_NameGoKala.NameGoKala1
FROM         T_NameGoKala INNER JOIN
                      T_Kala ON T_NameGoKala.ID = T_Kala.GOKID
WHERE     (T_Kala.AID = @AID) AND (NOT (@AID IS NULL)) OR
                      (@AID IS NULL)
GROUP BY T_NameGoKala.ID, T_NameGoKala.NameGoKalanumbr, T_NameGoKala.NameGoKala1
ORDER BY [عنوان گروه]

GO
/****** Object:  StoredProcedure [dbo].[CQ_ListAnbar]    Script Date: 08/21/2017 12:43:19 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListAnbar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListAnbar]
GO
/****** Object:  StoredProcedure [dbo].[CQ_ListAnbar]    Script Date: 08/21/2017 12:43:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[CQ_ListAnbar] AS SELECT     AID, AIDNumber AS [كد انبار], NameAnbar AS [نام انبار], NameAnbar, AIDNumber FROM         dbo.T_Anbar ORDER BY NameAnbar
GO
/****** Object:  StoredProcedure [dbo].[ConvertArabicToPersion]    Script Date: 08/21/2017 12:42:21 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ConvertArabicToPersion]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ConvertArabicToPersion]
GO
/****** Object:  StoredProcedure [dbo].[ConvertArabicToPersion]    Script Date: 08/21/2017 12:42:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 CREATE PROCEDURE [dbo].[ConvertArabicToPersion] AS    DECLARE @Table NVARCHAR(4000),@Column NVARCHAR(4000)  DECLARE Table_Cursor CURSOR  For  /* پيدا کردن تمام فيلدهاي متني تمام جداول ديتابيس جاري*/  SELECT T.name, /* Table */         C.name  /* Column */  FROM sysobjects T,syscolumns C  WHERE  T.id = C.id  AND T.xtype = 'u' /* User Table */           			AND (C.xtype = 99 /* ntext */  					OR C.xtype = 35   /* text */  					OR C.xtype = 231  /* nvarchar */  					OR C.xtype = 167  /* varchar */  					OR C.xtype = 175  /* char */  					OR C.xtype = 239  /* nchar */)  					  OPEN Table_Cursor FETCH NEXT FROM Table_Cursor INTO @Table,@Column  WHILE (@@FETCH_STATUS = 0)  BEGIN  	EXEC ('Update [' + @Table + '] Set [' + @Column + '] = REPLACE(REPLACE(CAST([' + @Column +	'] as nvarchar(4000)), NCHAR(1610), NCHAR(1740)), NCHAR(1603), NCHAR(1705))')  FETCH NEXT FROM Table_Cursor INTO @Table,@Column  END CLOSE Table_Cursor    DEALLOCATE Table_Cursor   
GO
/****** Object:  StoredProcedure [dbo].[QListGoroupHesab_SanadHesabdari]    Script Date: 08/21/2017 12:46:39 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QListGoroupHesab_SanadHesabdari]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QListGoroupHesab_SanadHesabdari]
GO
/****** Object:  StoredProcedure [dbo].[QListGoroupHesab_SanadHesabdari]    Script Date: 08/21/2017 12:46:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[QListGoroupHesab_SanadHesabdari]  AS 
SELECT     T_GropHesab.GoID AS ACCID, T_GropHesab.GoID, T_GropHesab.GoNumber AS CodeACC, T_GropHesab.NameGo AS NameACC, T_GropHesab.GoNumber AS [كد حساب], 
                      T_GropHesab.NameGo AS [عنوان حساب], QMande_GoHesab_SanadHesabdary.DARBed - QMande_GoHesab_SanadHesabdary.PARBes AS مانده, CASE WHEN (ISNULL((darbed - parbes), 0)) 
                      > 0 THEN N'بد' WHEN (ISNULL((darbed - parbes), 0)) < 0 THEN N'بس' ELSE '--' END AS [تشخيص ]
FROM         T_GropHesab LEFT OUTER JOIN
                          (SELECT     SUM(PARBes) AS PARBes, SUM(DARBed) AS DARBed, GOID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY GOID, GOID) AS QMande_GoHesab_SanadHesabdary ON T_GropHesab.GoID = QMande_GoHesab_SanadHesabdary.GOID
ORDER BY NameACC
GO
/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKolHesab]    Script Date: 08/21/2017 12:58:45 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListKolHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListKolHesab]
GO
/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKolHesab]    Script Date: 08/21/2017 12:58:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[QSubPFrm_ListKolHesab] AS SELECT     CONVERT(int, dbo.T_KolHesab.KolNumber) AS Expr1, dbo.T_KolHesab.NameHesab, dbo.T_GropHesab.NameGo, dbo.T_KolHesab.KOLID,                        dbo.T_KolHesab.GOID, dbo.T_KolHesab.KolNumber FROM         dbo.T_KolHesab INNER JOIN                       dbo.T_GropHesab ON dbo.T_KolHesab.GOID = dbo.T_GropHesab.GoID ORDER BY CONVERT(int, dbo.T_KolHesab.KolNumber) DESC  
GO
/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListTafsiliHesab]    Script Date: 03/13/2018 12:25:53 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListTafsiliHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListTafsiliHesab]
GO
/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListTafsiliHesab]    Script Date: 03/13/2018 12:25:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[QSubPFrm_ListTafsiliHesab] AS 
SELECT     T_T_Heasb.THESABID, T_T_Heasb.THESABNumber AS CodeH, T_T_Heasb.DateCreate, T_T_Heasb.DateModified, T_T_Heasb.HESABID, T_T_Heasb.NameHesabT, T_T_Heasb.GOID, 
                      T_T_Heasb.TextName, T_T_Heasb.KolID, T_T_Heasb.Address, T_T_Heasb.Note, T_T_Heasb.Address2, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.CodPosti, T_T_Heasb.Ostan, 
                      T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_T_Heasb.Active, 
                      T_T_Heasb.SaghfBed, T_T_Heasb.SaghfBes, T_T_Heasb.Commision, T_T_Heasb.UnitPriceFo, T_KolHesab.NameHesab AS KolName, T_Heasb.NameHesab AS MoinName, 
                      T_T_Heasb.THESABNumber, CASE WHEN Active = 0 THEN N'فعال' ELSE N'غیر فعال' END AS VAShESAB
FROM         T_T_Heasb LEFT OUTER JOIN
                      T_Heasb ON T_T_Heasb.HESABID = T_Heasb.HESABID LEFT OUTER JOIN
                      T_KolHesab ON T_T_Heasb.KolID = T_KolHesab.KOLID
ORDER BY CONVERT(Float, T_T_Heasb.THESABNumber) DESC
GO
/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListSanadHesabdary]    Script Date: 08/21/2017 12:59:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_ListSanadHesabdary]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPfrm_ListSanadHesabdary]
GO
/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListSanadHesabdary]    Script Date: 08/21/2017 12:59:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


        CREATE PROCEDURE [dbo].[QSubPfrm_ListSanadHesabdary] AS SELECT     SAID2, Sharh, Date2, FieldN2, FieldN1, FieldT1, Flag1, Flag2, Flag3, SAID2Number, OperationCode FROM         dbo.T_SA2 ORDER BY SAID2 DESC  

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListNameKala]    Script Date: 08/21/2017 12:58:59 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListNameKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListNameKala]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListNameKala]    Script Date: 08/21/2017 12:58:59 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[QSubPFrm_ListNameKala] AS SELECT     dbo.T_NameKala.ID, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.DateCreate, dbo.T_NameKala.DateModified, dbo.T_NameKala.NameKala1,                        dbo.T_NameKala.Active, dbo.T_NameKala.VAHED1, dbo.T_NameKala.VAHED2, dbo.T_NameKala.MinPrice, dbo.T_NameKala.MaxPrice,                        dbo.T_NameKala.MinMogoody, ABS(dbo.T_NameKala.Zarib) AS Zarib, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode,                        dbo.T_NameKala.Note, dbo.T_NameKala.UnitPrice1, dbo.T_NameKala.UnitPrice2, dbo.T_NameKala.UnitPrice3, dbo.T_NameKala.UnitPrice4,                        dbo.T_NameKala.UnitPrice5, dbo.T_NameKala.UnitPrice6, dbo.T_NameKala.DarsadAmel1, dbo.T_NameKala.Currency1, dbo.T_NameKala.Currency2,                        dbo.T_NameKala.Currency3, dbo.T_NameKala.Currency4, dbo.T_NameKala.Currency5, dbo.T_NameKala.Model, dbo.T_NameKala.Currency6,                        CONVERT(int, dbo.T_NameKala.KalaNumber) AS Expr1, dbo.T_NameVahed.NameVahed, T_NameVahed_1.NameVahed AS NameVahed2,                        t_1.NameGoKala1 FROM         dbo.T_NameVahed T_NameVahed_1 RIGHT OUTER JOIN                       dbo.T_NameKala LEFT OUTER JOIN                           (SELECT     dbo.T_NameGoKala.NameGoKala1, dbo.T_Kala.NameKalaID                              FROM         dbo.T_Kala INNER JOIN                                                    dbo.T_NameGoKala ON dbo.T_Kala.GOKID = dbo.T_NameGoKala.ID                              GROUP BY dbo.T_NameGoKala.NameGoKala1, dbo.T_Kala.NameKalaID) t_1 ON dbo.T_NameKala.ID = t_1.NameKalaID LEFT OUTER JOIN                       dbo.T_NameVahed ON dbo.T_NameKala.VAHED1 = dbo.T_NameVahed.VAHED1 ON T_NameVahed_1.VAHED1 = dbo.T_NameKala.VAHED2 ORDER BY CONVERT(int, dbo.T_NameKala.KalaNumber) DESC

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListPishForoosh]    Script Date: 08/21/2017 12:59:08 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListPishForoosh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListPishForoosh]
GO

/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListPishForoosh]    Script Date: 08/21/2017 12:59:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


        CREATE PROCEDURE [dbo].[QSubPFrm_ListPishForoosh] AS SELECT     FOROSHID, HesabID, GOID, KOLID, MOINID, THESABID, TextName, TextName1, TextNameT, NumberKH, DateFo, Takhfif, TextShSA, NumSID2, Sharh,                        FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT3 AS Expr1, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8,                        ForoshNumbr, PrimarySaid2, THESABIDAmel, dbo.HesabName(KOLID, HesabID, THESABID) AS namehesab FROM         dbo.T_PishFactor ORDER BY FOROSHID DESC 

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListNameKhadamat]    Script Date: 08/21/2017 12:59:04 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListNameKhadamat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListNameKhadamat]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListNameKhadamat]    Script Date: 08/21/2017 12:59:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


    CREATE PROCEDURE [dbo].[QSubPFrm_ListNameKhadamat] AS SELECT     NAMEKHID, Name, UnitPrice, Unit, KhadamatNumber, CONVERT(int, KhadamatNumber) AS Expr1 FROM         dbo.T_NameKhad ORDER BY CONVERT(int, KhadamatNumber)

GO




/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListMsgGoKala]    Script Date: 08/21/2017 12:58:54 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_ListMsgGoKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPfrm_ListMsgGoKala]
GO



/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListMsgGoKala]    Script Date: 08/21/2017 12:58:54 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[QSubPfrm_ListMsgGoKala] AS SELECT     ID, NameGoKala1, note, NameGoKalanumbr, CONVERT(int, NameGoKalanumbr) AS Expr1 FROM         dbo.T_NameGoKala ORDER BY NameGoKala1

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListMoinHesab]    Script Date: 08/21/2017 12:58:49 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListMoinHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListMoinHesab]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListMoinHesab]    Script Date: 08/21/2017 12:58:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


        CREATE PROCEDURE [dbo].[QSubPFrm_ListMoinHesab] AS SELECT     dbo.T_Heasb.HESABID, dbo.T_Heasb.NameHesab, dbo.T_Heasb.GOID, dbo.T_Heasb.KolID, CONVERT(int, dbo.T_Heasb.HESABNumber) AS CodeH,                        dbo.T_KolHesab.NameHesab AS KolName, dbo.T_GropHesab.NameGo AS GoroupName, dbo.T_Heasb.HESABNumber FROM         dbo.T_Heasb LEFT OUTER JOIN                       dbo.T_KolHesab ON dbo.T_Heasb.KolID = dbo.T_KolHesab.KOLID LEFT OUTER JOIN                       dbo.T_GropHesab ON dbo.T_Heasb.GOID = dbo.T_GropHesab.GoID ORDER BY CONVERT(int, dbo.T_Heasb.HESABNumber)  

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqVosooly]    Script Date: 08/21/2017 12:58:21 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqVosooly]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqVosooly]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqVosooly]    Script Date: 08/21/2017 12:58:21 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqVosooly] AS SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 3) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPassPar]    Script Date: 08/21/2017 12:58:17 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqPassPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqPassPar]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPassPar]    Script Date: 08/21/2017 12:58:17 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


        CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqPassPar] AS 
        DECLARE @FieldN1_Vas int=1 ;
    
SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
                                              T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR
    

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPass]    Script Date: 08/21/2017 12:58:11 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqPass]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqPass]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPass]    Script Date: 08/21/2017 12:58:11 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqPass] AS SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 1) OR                       (dbo.T_Asnad_Dar.FieldN1 = 5) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPassPar]    Script Date: 08/21/2017 12:58:07 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqNoPassPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqNoPassPar]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPassPar]    Script Date: 08/21/2017 12:58:07 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqNoPassPar]
AS DECLARE @FieldN1_Vas int=4 ;
    
SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
                                              T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR
    

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPass]    Script Date: 08/21/2017 12:58:03 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqNoPass]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqNoPass]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPass]    Script Date: 08/21/2017 12:58:03 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqNoPass] AS SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 4) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNaghdiPar]    Script Date: 08/21/2017 12:57:57 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqNaghdiPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqNaghdiPar]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNaghdiPar]    Script Date: 08/21/2017 12:57:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


        CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqNaghdiPar] AS 
        
        
        DECLARE @FieldN1_Vas int=3 ;
    
SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
                                              T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR




GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBatelShodePar]    Script Date: 08/21/2017 12:57:52 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqBatelShodePar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqBatelShodePar]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBatelShodePar]    Script Date: 08/21/2017 12:57:52 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


        CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqBatelShodePar] AS 
        
        DECLARE @FieldN1_Vas int=2 ;
    
SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
                                              T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBargashti]    Script Date: 08/21/2017 12:57:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqBargashti]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqBargashti]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBargashti]    Script Date: 08/21/2017 12:57:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqBargashti] AS SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 2) OR                       (dbo.T_Asnad_Dar.FieldN1 = 6) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC

GO





/****** Object:  View [dbo].[QSubPFrm_Foroosh_Sandogh]    Script Date: 01/28/2019 10:33:53 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_Sandogh]'))
DROP VIEW [dbo].[QSubPFrm_Foroosh_Sandogh]
GO



/****** Object:  View [dbo].[QSubPFrm_Foroosh_Sandogh]    Script Date: 01/28/2019 10:33:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[QSubPFrm_Foroosh_Sandogh]
AS

SELECT      TOP (100) PERCENT [TRANSACTION].AID, [TRANSACTION].KALAID, [TRANSACTION].Sharh, [TRANSACTION].Date1, [TRANSACTION].FOROSHID, [TRANSACTION].TedadFo, [TRANSACTION].UnitPriceFo, 
                      [TRANSACTION].TedadFo * [TRANSACTION].UnitPriceFo + ISNULL([TRANSACTION].Avarez, 0) + ISNULL([TRANSACTION].Maliatkala, 0) - ISNULL([TRANSACTION].FieldN7, 0) AS Stion, 
                      [TRANSACTION].radif, [TRANSACTION].TextAIDKharid, [TRANSACTION].FieldN6, [TRANSACTION].GOKID, [TRANSACTION].FOROSHID AS LinkedID, [TRANSACTION].FieldN7, [TRANSACTION].FieldN3, 
                      [TRANSACTION].Avarez, [TRANSACTION].Maliatkala, [TRANSACTION].TedadFo * [TRANSACTION].UnitPriceFo - ISNULL([TRANSACTION].FieldN7, 0) AS SumRadif, T_NameKala.BarCode, 
                      T_NameKala.UnitPrice1, T_NameKala.Currency1
FROM         [TRANSACTION] INNER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID INNER JOIN
                      View_ForoshSandogh ON [TRANSACTION].FOROSHID = View_ForoshSandogh.FOROSHID
ORDER BY [TRANSACTION].radif

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Sandogh_CALC]    Script Date: 08/21/2017 13:09:02 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_Sandogh_CALC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_Sandogh_CALC]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Sandogh_CALC]    Script Date: 08/21/2017 13:09:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_Sandogh_CALC]
	
		@FOROSHID int,
        @SUMFACT DECIMAL(19,4) =0 OUTPUT
AS
BEGIN
	
SELECT     @SUMFACT= SUM(TedadFo * UnitPriceFo + ISNULL(Avarez, 0) + ISNULL(Maliatkala, 0) )
FROM         [TRANSACTION]
WHERE      (FOROSHID = @FOROSHID)
GROUP BY FOROSHID


							  
END





GO



/****** Object:  StoredProcedure [dbo].[QSubPfrm_RaasChek]    Script Date: 08/21/2017 13:07:13 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_RaasChek]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPfrm_RaasChek]
GO



/****** Object:  StoredProcedure [dbo].[QSubPfrm_RaasChek]    Script Date: 08/21/2017 13:07:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[QSubPfrm_RaasChek] (@StartDate nvarchar(50), @LinkIdtosub nvarchar(255), @FAddDay1 int = 0) AS SELECT     TOP 100 PERCENT AID, KALAID, Sharh, Date1, KHARIDID, Tedad, UnitPrice, CONVERT(money, DATEDIFF(day, DATEADD(day, @FAddDay1,                        CAST(dbo.PersianToGregorian(SUBSTRING(@StartDate, 1, 4) + '/' + SUBSTRING(@StartDate, 5, 2) + '/' + SUBSTRING(@StartDate, 7, 2)) AS datetime)),                        CAST(dbo.PersianToGregorian(SUBSTRING(TextKalaName, 1, 4) + '/' + SUBSTRING(TextKalaName, 5, 2) + '/' + SUBSTRING(TextKalaName, 7, 2)) AS datetime))                        * UnitPrice) AS Sumation, radif, GOKID, CONVERT(nvarchar(20), AID) + CONVERT(nvarchar(20), GOKID) + CONVERT(nvarchar(20), KALAID) AS Expr1, LinkedID,                        TextKalaName, SumofPrice, FieldN6, OperType, @StartDate AS Expr2, DATEDIFF(day, DATEADD(day, @FAddDay1,                        CAST(dbo.PersianToGregorian(SUBSTRING(@StartDate, 1, 4) + '/' + SUBSTRING(@StartDate, 5, 2) + '/' + SUBSTRING(@StartDate, 7, 2)) AS datetime)),                        CAST(dbo.PersianToGregorian(SUBSTRING(TextKalaName, 1, 4) + '/' + SUBSTRING(TextKalaName, 5, 2) + '/' + SUBSTRING(TextKalaName, 7, 2)) AS datetime))                        AS ZamanMandeh, TextAIDKh, @FAddDay1 AS Expr3 FROM         dbo.TRANSACTION2 WHERE     (Date1 = @LinkIdtosub) ORDER BY radif

GO



/****** Object:  StoredProcedure [dbo].[Q_ListPardakhtNaghdi]    Script Date: 08/21/2017 13:06:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_ListPardakhtNaghdi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_ListPardakhtNaghdi]
GO

/****** Object:  StoredProcedure [dbo].[Q_ListPardakhtNaghdi]    Script Date: 08/21/2017 13:06:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[Q_ListPardakhtNaghdi] AS SELECT     dbo.T_Par1.TextSharh, dbo.T_Par1.TextName2, dbo.T_Par1.Mablagh, dbo.T_Par1.PARID1, dbo.T_SA2.SAID2Number, dbo.T_Par1.PrimarySaid2,                        dbo.T_Par1.PARID1Number, dbo.T_Par1.DATE2, dbo.T_Par1.TextShSA, dbo.T_Par1.TextName1 FROM         dbo.T_Par1 INNER JOIN                       dbo.T_SA2 ON dbo.T_Par1.PrimarySaid2 = dbo.T_SA2.SAID2 ORDER BY dbo.T_Par1.PARID1 DESC  
GO




/****** Object:  StoredProcedure [dbo].[Q_ListDaryaftNaghdi]    Script Date: 08/21/2017 13:06:18 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_ListDaryaftNaghdi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_ListDaryaftNaghdi]
GO

/****** Object:  StoredProcedure [dbo].[Q_ListDaryaftNaghdi]    Script Date: 08/21/2017 13:06:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[Q_ListDaryaftNaghdi] AS SELECT     dbo.T_Dar1.TextSharh, dbo.T_Dar1.TextName1, dbo.T_Dar1.TextName2, dbo.T_Dar1.Mablagh, dbo.T_Dar1.DARID1, dbo.T_SA2.SAID2Number,                        dbo.T_Dar1.DATE2, dbo.T_Dar1.DAR1Numbr, dbo.T_Dar1.PrimarySaid2 FROM         dbo.T_Dar1 INNER JOIN                       dbo.T_SA2 ON dbo.T_Dar1.PrimarySaid2 = dbo.T_SA2.SAID2 ORDER BY dbo.T_Dar1.DARID1 DESC  
GO



/****** Object:  StoredProcedure [dbo].[QSubListChTFrm_Anbar]    Script Date: 08/21/2017 13:04:09 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubListChTFrm_Anbar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubListChTFrm_Anbar]
GO



/****** Object:  StoredProcedure [dbo].[QSubListChTFrm_Anbar]    Script Date: 08/21/2017 13:04:09 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

  CREATE PROCEDURE [dbo].[QSubListChTFrm_Anbar] AS SELECT     dbo.T_Anbar.*, CONVERT(int, AIDNumber) AS Expr1,'حساب انبار' AS nameHesab FROM         dbo.T_Anbar
GO




/****** Object:  View [dbo].[QSub_Frm_DastehChek]    Script Date: 08/21/2017 13:05:26 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[QSub_Frm_DastehChek]'))
DROP VIEW [dbo].[QSub_Frm_DastehChek]
GO


/****** Object:  View [dbo].[QSub_Frm_DastehChek]    Script Date: 08/21/2017 13:05:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

          CREATE VIEW [dbo].[QSub_Frm_DastehChek] AS SELECT     DastehCHID, NameBank, Sh_Hesab, SeriDastehCD, Serialnum1, Serialnum2, Flag, NameHesab, CodeHesab FROM         dbo.DastehChek WHERE     (Flag = 0) 
GO



/****** Object:  StoredProcedure [dbo].[QSub_MSG_PReviewListCHeckForRunProgramDAR]    Script Date: 08/21/2017 13:16:07 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSub_MSG_PReviewListCHeckForRunProgramDAR]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSub_MSG_PReviewListCHeckForRunProgramDAR]
GO



/****** Object:  StoredProcedure [dbo].[QSub_MSG_PReviewListCHeckForRunProgramDAR]    Script Date: 08/21/2017 13:16:07 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


    CREATE PROCEDURE [dbo].[QSub_MSG_PReviewListCHeckForRunProgramDAR](@Baze nvarchar(50), @AID nvarchar(50)) AS 

SELECT     ASNADDARID, Sharh, Mablagh, DateTA, DateVO, ShSeryal, FieldN1, GOID, KOLID, HESABID, THESABID, dbo.HesabName(KOLID, HESABID, THESABID) AS Daryaft_az_Hesab, 
                      CASE WHEN (T_Asnad_Dar.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_Dar.[FieldN1]) = 2 THEN 'برگشتي ' WHEN (T_Asnad_Dar.[FieldN1]) 
                      = 3 THEN 'در جریان وصول ' WHEN (T_Asnad_Dar.[FieldN1]) = 4 THEN 'پاس نشده' WHEN (T_Asnad_Dar.[FieldN1]) = 5 THEN 'پاس شده ' WHEN (T_Asnad_Dar.[FieldN1]) 
                      = 6 THEN ' برگشتی' END AS chkvaz, DARID3, AsnadDarNumber, Namebank, ShHesab, @AID AS Expr3
FROM         T_Asnad_Dar
WHERE     (FieldN1 = 4) and
   (DateVO <= @Baze)


GO



/****** Object:  StoredProcedure [dbo].[Rep_SoodKol]    Script Date: 08/21/2017 13:15:22 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_SoodKol]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_SoodKol]
GO


/****** Object:  StoredProcedure [dbo].[Rep_SoodKol]    Script Date: 08/21/2017 13:15:22 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE PROCEDURE [dbo].[Rep_SoodKol] (@startDate nvarchar(50), @enddate nvarchar(50)) AS SELECT     [TRANSACTION].GOID, [TRANSACTION].KOLID, SUM([TRANSACTION].DARBed) AS SumOfDARBed, SUM([TRANSACTION].PARBes) AS SumOfPARBes,                        SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes) AS Expr1, @startDate AS startDate, @enddate AS enddate, T_KolHesab.NameHesab,                        T_GropHesab.NameGo FROM         [TRANSACTION] INNER JOIN                       T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID INNER JOIN                       T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID INNER JOIN                       T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND (T_SA2.Flag4 <> 1 OR                       T_SA2.Flag4 IS NULL) GROUP BY [TRANSACTION].GOID, [TRANSACTION].KOLID, T_KolHesab.NameHesab, T_GropHesab.NameGo HAVING      ([TRANSACTION].GOID IN (8, 9)) AND (SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes) <> 0) UNION ALL  SELECT     8 AS GOID, - 1000 AS kolid, SUM(Zian) AS Zian, SUM(Sood) AS Sood, 0 AS Expr1, @startDate AS Expr2, @enddate AS Expr3, 'درآمد حاصل از فروش' AS NameHesab,                        'درآمدها' AS Expr4 FROM         dbo.Fun_Sood_Zian_Kala(@startDate, @enddate) Fun_Sood_Zian_Kala 
GO



/****** Object:  StoredProcedure [dbo].[QSub_MSG_PReviewListCHeckForRunProgramPar]    Script Date: 08/21/2017 13:16:19 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSub_MSG_PReviewListCHeckForRunProgramPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSub_MSG_PReviewListCHeckForRunProgramPar]
GO


/****** Object:  StoredProcedure [dbo].[QSub_MSG_PReviewListCHeckForRunProgramPar]    Script Date: 08/21/2017 13:16:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


    CREATE PROCEDURE [dbo].[QSub_MSG_PReviewListCHeckForRunProgramPar](@AID nvarchar(50), @Baze nvarchar(50)) AS 
    
    
    
SELECT     @AID AS Expr1, Sharh, ShSeryal, FieldN1, GOID, KOLID, HESABID, THESABID, DateSO, DateSR, HESABID2, THESABID2, chkvaz, Pardakht_az_Hesab, ASNADID, Mablagh, Flag2, SharhSub, DarBed, 
                      variz_be_Hesab, HESABIDM, THESABIDM, AsnadNumber
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = 4) AND (T_Asnad.DateSR <= @Baze)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = 4) AND (T_Asnad_1.DateSR <= @Baze)) AS SubAsnad
ORDER BY DateSR
                        


GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_Bed_Bes_CALC]    Script Date: 08/21/2017 13:18:41 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdari_Bed_Bes_CALC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_Bed_Bes_CALC]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_Bed_Bes_CALC]    Script Date: 08/21/2017 13:18:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_Bed_Bes_CALC]

		@LinkSubID NVARCHAR(255),
        @SUMBed DECIMAL(19,4) =0 OUTPUT,
        @SUMBes DECIMAL(19,4) =0 OUTPUT,
        @SUMArz DECIMAL(19,4) =0 OUTPUT
AS
BEGIN

	
SELECT     @SUMBes=SUM(PARBes) , @SUMBed=SUM(DARBed ),@SUMArz=SUM(CurMablagh)
FROM         TRANSACTION2
WHERE     (Date1 = @LinkSubID)


							  
END






GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_ChekSaghf_Bed_Bes]    Script Date: 08/21/2017 13:18:48 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdari_ChekSaghf_Bed_Bes]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_ChekSaghf_Bed_Bes]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_ChekSaghf_Bed_Bes]    Script Date: 08/21/2017 13:18:48 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_ChekSaghf_Bed_Bes]
		@SAID2 INT,
		@SAID2_1 INT,
		@THESABID INT,
		@LinkSubID NVARCHAR(255),
        @SUMBedFact DECIMAL(19,4) ,
        @SUMBesFact DECIMAL(19,4) ,
        @MabAddBed DECIMAL(19,4) =0 OUTPUT,
        @MabAddBes DECIMAL(19,4) =0 OUTPUT
        
AS
BEGIN

DECLARE @SaghfBed  DECIMAL(19,4);
DECLARE @SaghfBes  DECIMAL(19,4);
DECLARE @DARBedGh DECIMAL(19,4);
DECLARE @PARBesGh DECIMAL(19,4) ;
DECLARE @DARBedTemp DECIMAL(19,4);
DECLARE @PARBesTemp DECIMAL(19,4);

set @DARBedGh=0
SET @PARBesGh=0
set @DARBedTemp=0
set @PARBesTemp=0

if (@SAID2 IS NULL)
	set @SAID2=-1
if (@LinkSubID IS NULL)
	SET @LinkSubID=-1
	
			
		SELECT     @SaghfBed=SaghfBed, @SaghfBes=SaghfBes
		FROM         T_T_Heasb
		WHERE     (THESABID = @THESABID)
		

	IF (@SaghfBed>0 OR @SaghfBes>0)
		BEGIN
			SELECT     @PARBesGh=SUM(PARBes)  , @DARBedGh=SUM(DARBed) 
			FROM         [TRANSACTION]
			WHERE     (NOT (SAID2 IS NULL)) AND (THESABID = @THESABID) AND (NOT(SAID2 IN (@SAID2,@SAID2_1)))

				
			SELECT     @PARBesTemp=isnull(SUM(PARBes),0) , @DARBedTemp=isnull(SUM(DARBed ),0)
			FROM         TRANSACTION2
			WHERE     ((THESABID = @THESABID) AND (Date1 = @LinkSubID) AND (KALAID IS NULL))


				
			/* فروش */
			SELECT     @PARBesTemp=@PARBesTemp+isnull(SUM(DARBed),0) 
			FROM         TRANSACTION2
			WHERE     ( (TextAIDFo = @LinkSubID) AND (FieldN6 =2) AND (THESABID=@THESABID))
			
			SELECT      @DARBedTemp=@DARBedTemp+isnull(SUM(DARBed ),0)
			FROM         TRANSACTION2
			WHERE      ( (TextAIDFo = @LinkSubID) AND (FieldN6 =2) AND (FieldN4=@THESABID))
			
			
			/* خريد */
			SELECT     @PARBesTemp=@PARBesTemp+isnull(SUM(DARBed),0) 
			FROM         TRANSACTION2
			WHERE     ( (TextAIDFo = @LinkSubID) AND (FieldN6 =1) AND (FieldN4=@THESABID))
			
			SELECT      @DARBedTemp=@DARBedTemp+isnull(SUM(DARBed ),0)
			FROM         TRANSACTION2
			WHERE      ( (TextAIDFo = @LinkSubID) AND (FieldN6 =1) AND (THESABID=@THESABID))
			
			
			SET @DARBedTemp=isnull(@DARBedTemp,0)+isnull(@DARBedGh,0)+ISNULL(@SUMBedFact,0)

			SET @PARBesTemp=isnull(@PARBesTemp,0)+isnull(@PARBesGh,0)+ISNULL(@SUMBesFact,0)


			if (@DARBedTemp-@PARBesTemp>0) 
				BEGIN	
					set @DARBedTemp=@DARBedTemp-@PARBesTemp
					set @PARBesTemp=0
				END		
			ELSE
				BEGIN	
					set @PARBesTemp=@PARBesTemp-@DARBedTemp
					set @DARBedTemp=0
				END		
			
			IF (@SaghfBed>0)	
				set @MabAddBed=@SaghfBed-@DARBedTemp
			ELSE 
				set @MabAddBed=0		
						
			IF (@SaghfBes>0)
				set @MabAddBes=@SaghfBes-@PARBesTemp	
			ELSE
				set @MabAddBes=0
		End	
End

GO


/****** Object:  StoredProcedure [dbo].[Rep_SoratHesab]    Script Date: 08/21/2017 13:22:53 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_SoratHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_SoratHesab]
GO



/****** Object:  StoredProcedure [dbo].[Rep_SoratHesab]    Script Date: 08/21/2017 13:22:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[Rep_SoratHesab] ( 
@THESABID int,
@HESABID int,
@KOLID int,
@GoID INT,
@Date1 NVARCHAR(10),
@Date2 NVARCHAR(10)
  )           
  AS     
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';                  	
  SET @ParmDefinition = N'@THESABID int=null, 								
						@HESABID int=null, 								
						@KOLID  int=null, 								
						@GoID int=null,
						@Date1 NVARCHAR(10)=null,
						@Date2 NVARCHAR(10)=null  ';            	
	
	IF(NOT (@THESABID IS NULL))	 
		SET @SQLString =  ' 
SELECT     ISNULL(RadifSAID2, ISNULL(radif, 1000)) AS RadifSAID2, NameHesabmoin, ISNULL(NameKala2, SharhRadif) AS SharhRadif, MandeHesab, Bedehkar, Bestankar, Tarikh, ShomareSanad, HesabID, 
                      CodeHesab, srtfield, tedad, unitprice, NameVahed, NameKala, MatnFi, SAIDType, NoeAmaliyat, TedadSadere, TedadVarede
FROM         dbo.F_SoratHesabTafsili(@Date1) AS F_SoratHesabTafsili_1
WHERE     (Tarikh BETWEEN @Date1 AND @Date2) AND (HesabID = @THESABID)
ORDER BY Tarikh
          
'
        
   else IF(NOT (@HESABID IS NULL))	 
		SET @SQLString ='SELECT     ISNULL(RadifSAID2, ISNULL(radif, 1000)) AS RadifSAID2, NameHesabmoin, ISNULL(NameKala2, SharhRadif) AS SharhRadif, MandeHesab, Bedehkar, Bestankar, Tarikh, ShomareSanad, HesabID, CodeHesab, srtfield, tedad, unitprice, NameVahed, NameKala, 
                      MatnFi, SAIDType, NoeAmaliyat, TedadSadere, TedadVarede
FROM         dbo.F_SoratHesabMoein(@Date1) AS F_SoratHesabMoein_1
WHERE     (Tarikh BETWEEN @Date1 AND @Date2) AND (HesabID = @HESABID) ' 	
	else IF(NOT (@KOLID IS NULL))	 
		SET @SQLString ='
		SELECT     ISNULL(RadifSAID2, ISNULL(radif, 1000)) AS RadifSAID2, NameHesabmoin, ISNULL(NameKala2, SharhRadif) AS SharhRadif, MandeHesab, Bedehkar, Bestankar, Tarikh, ShomareSanad, HesabID, CodeHesab, srtfield, tedad, unitprice, NameVahed, NameKala, 
                      MatnFi, SAIDType, NoeAmaliyat, TedadSadere, TedadVarede
FROM        dbo.F_SoratHesabKol(@Date1) AS F_SoratHesabKol_1 
WHERE     (Tarikh BETWEEN @Date1 AND @Date2) AND (HESABID = @KOLID)
' 	
  /* -----------------------نشان دادن کالاهای فعال */ 
	
/*if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' HAVING      (T_NameKala.Active = 0) '; 					
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' HAVING      (T_NameKala.Active > -1000) '; 	
               
  /* -----------------------شرط انبار */ 			
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Kala.AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 			
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Kala.GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	
*/
						

set @SQLString=@SQLString 	
     			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@THESABID=@THESABID ,@HESABID=@HESABID,@KOLID=@KOLID,@GoID=@GoID,@Date1=@Date1,@Date2=@Date2;  		
return END 


GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepGen_UpdateFlagCH]    Script Date: 08/21/2017 13:22:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_RepGen_UpdateFlagCH]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_RepGen_UpdateFlagCH]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepGen_UpdateFlagCH]    Script Date: 08/21/2017 13:22:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE  PROCEDURE [dbo].[QSubPFrm_RepGen_UpdateFlagCH]

		@IDRep INT,
		@FlagCh bit
AS

BEGIN

	
UPDATE    T_RepGen
SET              FlagCh = @FlagCh
WHERE     (IDKey = @IDRep)


							  
END






GO


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011762]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011762]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011763]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011763]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011764]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011764]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011970]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011970]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011971]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011971]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011972]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011972]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011974]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011974]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011975]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011975]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021014]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021014]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021058]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021058]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021091]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021091]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021446]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021446]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021447]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021447]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021448]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021448]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021450]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021450]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021451]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021451]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0305200]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0305200]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030259]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030259]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030293]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030293]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030294]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030294]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030295]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030295]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030296]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030296]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_03029751]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_03029751]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_03029752]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_03029752]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030315]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030315]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030316]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030316]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030317]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030317]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030318]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030318]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021340]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021340]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021341]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021341]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021342]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021342]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_012076]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_012076]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_012077]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_012077]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_012078]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_012078]
GO


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030623]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030623]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030624]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030624]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_03062424]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_03062424]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030825]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030825]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030826]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030826]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030827]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030827]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_032598]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_032598]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_032599]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_032599]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011660]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011660]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011661]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011661]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021156]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021156]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021157]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021157]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030102]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030102]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030103]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030103]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030104]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030104]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030107]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030107]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05238835]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05238835]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05228521]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05228521]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030210]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030210]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030212]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030212]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030521]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030521]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030522]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030522]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0215]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0215]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0121]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0121]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05238833]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05238833]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05238834]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05238834]
GO




/****** Object:  StoredProcedure [dbo].[Rep_030930]    Script Date: 11/25/2017 16:20:16 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030930]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030930]
GO

/****** Object:  StoredProcedure [dbo].[Rep_030930]    Script Date: 11/25/2017 16:20:16 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[Rep_030930](@startDate nvarchar(50),
									@enddate nvarchar(50),
									@startSanadID int,
									@endSanadID INT,
									@GOID INT,
									@KOLID INT,
									@HESABID INT,
									@THESABID int,
									@THESABIDSath2 INT)
			AS 
BEGIN        	
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=NUll';
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   
	        
set @SQLString=N' SELECT     [TRANSACTION].Date1, T_SA2.SAID2Number, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab, [TRANSACTION].DARBed, 
                      [TRANSACTION].PARBes, [TRANSACTION].TextShSA, [TRANSACTION].TextNHSID2, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].KOLID, [TRANSACTION].GOID, 
                      [TRANSACTION].HESABID, [TRANSACTION].THESABID, [TRANSACTION].TEMPR, [TRANSACTION].SAID2, CASE WHEN DarBed = 0 THEN 1 ELSE 2 END AS Sort1, [TRANSACTION].radif, 
                      dbo.HesabNameCod([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS CodeHesab, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, NULL) 
                      AS NameHesabMoin, dbo.HesabName([TRANSACTION].KOLID, NULL, NULL) AS NameHesabKol
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2
WHERE     ([TRANSACTION].DARBed IS NOT NULL) AND ([TRANSACTION].PARBes IS NOT NULL) '


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
     /*  --------------------تاريخ  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startDate  IS NULL)) AND (NOT (@enddate  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].Date1 BetWeen @startDate and  @enddate)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number BetWeen @startSanadID and  @endSanadID)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
   
       /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   
   set @SQLString= @SQLString + @WhrStatement 



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END 






GO








/****** Object:  StoredProcedure [dbo].[Rep_030931]    Script Date: 11/21/2017 17:56:53 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030931]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030931]
GO



/****** Object:  StoredProcedure [dbo].[Rep_030931]    Script Date: 11/21/2017 17:56:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[Rep_030931](@startDate nvarchar(50),
@enddate nvarchar(50),
@startSanadID int,
@endSanadID int,
@GOID INT,
@KOLID INT,
@HESABID INT,
@THESABID INT,
@THESABIDSath2 INT)
AS 

BEGIN 
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=null'; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   


set @SQLString=N'SELECT     [TRANSACTION].Date1, T_SA2.SAID2Number, [TRANSACTION].DARBed, [TRANSACTION].PARBes, [TRANSACTION].Flag2, [TRANSACTION].Sharh, [TRANSACTION].TextShSA, T_KolHesab.NameHesab, 
                      [TRANSACTION].DARBed - [TRANSACTION].PARBes AS mande, [TRANSACTION].KOLID, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].GOID, [TRANSACTION].DARBedCur, 
                      [TRANSACTION].PARBesCur, [TRANSACTION].CurRate, [TRANSACTION].CurRef, T_SA2.SAID2
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID
WHERE     ([TRANSACTION].DARBed IS NOT NULL) AND ([TRANSACTION].PARBes IS NOT NULL) AND (@startDate IS NOT NULL) AND ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) '


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
      /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
     /*  --------------------تاريخ  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startDate  IS NULL)) AND (NOT (@enddate  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].Date1 BetWeen @startDate and  @enddate)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number BetWeen @startSanadID and  @endSanadID)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
   
   
   set @SQLString= @SQLString + @WhrStatement 



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END 

GO



/****** Object:  StoredProcedure [dbo].[Rep_030932]    Script Date: 11/21/2017 17:56:58 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030932]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030932]
GO



/****** Object:  StoredProcedure [dbo].[Rep_030932]    Script Date: 11/21/2017 17:56:58 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[Rep_030932](@startDate nvarchar(50),
@enddate nvarchar(50),
@startSanadID int,
@endSanadID int,
@GOID INT,
@KOLID INT,
@HESABID INT,
@THESABID int,
@THESABIDSath2 INT)
AS 

BEGIN 
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=NUll'; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   


set @SQLString=N' SELECT     T_SA2.SAID2Number, [TRANSACTION].DARBed, [TRANSACTION].PARBes, [TRANSACTION].Sharh, [TRANSACTION].TextShSA, T_Heasb.NameHesab, 
                      [TRANSACTION].DARBed - [TRANSACTION].PARBes AS mande, [TRANSACTION].KOLID, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].Date1, T_Heasb.HESABID, 
                      [TRANSACTION].DARBedCur, [TRANSACTION].PARBesCur, [TRANSACTION].CurRate, [TRANSACTION].CurRef, T_SA2.SAID2
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     ([TRANSACTION].DARBed IS NOT NULL) AND ([TRANSACTION].PARBes IS NOT NULL) AND ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)'
                      
                      
  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
     /*  --------------------تاريخ  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startDate  IS NULL)) AND (NOT (@enddate  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].Date1 BetWeen @startDate and  @enddate)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number BetWeen @startSanadID and  @endSanadID)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
    /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   set @SQLString= @SQLString + @WhrStatement 



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END                       


GO




/****** Object:  StoredProcedure [dbo].[Rep_030992]    Script Date: 11/21/2017 17:58:13 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030992]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030992]
GO


/****** Object:  StoredProcedure [dbo].[Rep_030992]    Script Date: 11/21/2017 17:58:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[Rep_030992](@startDate nvarchar(50),
@enddate nvarchar(50),
@startSanadID int,
@endSanadID int,
@GOID INT,
@KOLID INT,
@HESABID INT,
@THESABID int,
@THESABIDSath2 INT)
AS 

BEGIN 
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=NUll'; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   


set @SQLString=N' SELECT      [TRANSACTION].Date1, T_SA2.SAID2Number, [TRANSACTION].DARBed, [TRANSACTION].PARBes, [TRANSACTION].Flag2, [TRANSACTION].Sharh, [TRANSACTION].TextShSA, 
                      T_T_Heasb.NameHesabT, [TRANSACTION].DARBed - [TRANSACTION].PARBes AS mande, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].DARBedCur, [TRANSACTION].PARBesCur, 
                      [TRANSACTION].CurRate, [TRANSACTION].CurRef, T_SA2.SAID2, [TRANSACTION].THESABID
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID
WHERE     ([TRANSACTION].DARBed IS NOT NULL) AND ([TRANSACTION].PARBes IS NOT NULL) AND ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)'

 /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
     /*  --------------------تاريخ  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startDate  IS NULL)) AND (NOT (@enddate  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].Date1 BetWeen @startDate and  @enddate)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number BetWeen @startSanadID and  @endSanadID)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
     
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   
   
   set @SQLString= @SQLString + @WhrStatement 



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END                       


GO




              
GO




IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523893921]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0523893921]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894024]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0523894024]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894126]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0523894126]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894131]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0523894131]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894133]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0523894133]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239042]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05239042]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239043]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05239043]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239044]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05239044]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239045]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05239045]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239045]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
        CREATE PROCEDURE [Rep_05239045](       
			@Goid int, 
			@KolID int, 
			@HesabID int, 
			@THesabID int, 
			@startDate nvarchar(50), 
			@enddate nvarchar(50), 
			@IdKala INT=NULL,
			@IDA INT=NULL,
			@IDGokala INT=NULL,
			@faktorN nvarchar(50)) 
         	AS 
         	
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';   
  
  
    SET @ParmDefinition = N''@Goid int=null, 
							@KolID int=null, 
							@HesabID int=null, 
							@THesabID int=null, 
							@startDate nvarchar(50)=N''''0'''', 
							@enddate nvarchar(50)=N''''99999999'''', 
							@IdKala int=null, 
							@IDA int=null, 
							@IDGokala INT=NULL,
							@faktorN nvarchar(50)=null''    

  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate=N''00000000''; 				   
	END   
	      
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate=N''99999999''; 				   
	END 
			 				 
SET @SQLString =''SELECT     NoeFa, Varde, Sadre, AID, KALAID, Expr1, Expr5, Date1 AS MinOfDate1, Date1 AS MaxOfDate1, startDate, enddate, namehesab, KalaNumber, NameKala1, NoeFact, GOKID
FROM         dbo.FunSub_05239045(@StartDate, @Enddate) AS FunSub_05239045_1 ''

                      

   /* --------------------شرط حساب سرگروه روی ساب*/ 			
  set @ParmAndstr='' AND '' 			
  if(NOT (@Goid IS NULL)) 			
  BEGIN 				
  set @WhrStatement= @WhrStatement + @ParmAndstr + N''(Goid = @Goid )''; 				
  set @ParmAndstr='' AND '' 			
  END             
   /* --------------------شرط حساب کل  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@KolID IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KolID = @KolID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
   /*  --------------------شرط حساب معین  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@HesabID  IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(HesabID = @HesabID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
    /* --------------------   شرط حساب تفصیلی */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@THesabID  IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(THesabID = @THesabID )''; 				
    set @ParmAndstr='' AND '' 			
    END            
    
    
        /* --------------------   شرط کالا */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IdKala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KALAID = @IdKala )''; 				
		set @ParmAndstr='' AND '' 			
    END      
    
            /* --------------------   شرط گروه */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDGokala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(GOKID = @IDGokala )''; 				
		set @ParmAndstr='' AND '' 			
    END  
    
            /* --------------------   شرط انبار */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDA IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(AID = @IDA )''; 				
    set @ParmAndstr='' AND '' 			
    END      
    
   /* --------------------   شرط فاکتور */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@faktorN IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(NoeFact = @faktorN )''; 				
    set @ParmAndstr='' AND '' 			
    END   
    
    set @SQLString=@SQLString +@WhrStatement+ N'' GROUP BY NoeFa, Varde, Sadre, AID, KALAID, Expr1, Expr5, startDate, enddate, namehesab, KalaNumber, NameKala1, NoeFact, GOKID, Date1 ''
    
    
    EXECUTE sp_executesql   @SQLString, @ParmDefinition,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@startDate =@startDate,@enddate =@enddate,@IdKala =@IdKala,	@IDA =@IDA,@IDGokala=@IDGokala,@faktorN =@faktorN;  		
    END  
         

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239044]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
         CREATE PROCEDURE [Rep_05239044](        
			@Goid int, 
			@KolID int, 
			@HesabID int, 
			@THesabID int, 
			@startDate nvarchar(50), 
			@enddate nvarchar(50), 
			@IdKala INT=NULL,
			@IDA INT=NULL,
			@IDGokala INT=NULL,
			@faktorN nvarchar(50)) 
         	AS 
         	
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';   
  
  
    SET @ParmDefinition = N''@Goid int=null, 
							@KolID int=null, 
							@HesabID int=null, 
							@THesabID int=null, 
							@startDate nvarchar(50)=N''''0'''', 
							@enddate nvarchar(50)=N''''99999999'''', 
							@IdKala int=null, 
							@IDA int=null, 
							@IDGokala INT=NULL,
							@faktorN nvarchar(50)=null''    

  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate=N''00000000''; 				   
	END   
	      
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate=N''99999999''; 				   
	END 
			 				 
SET @SQLString =''SELECT     Expr1, HESABID, THESABID, KOLID, GOID, sh, NoeFa, SUM(Varde) AS Varde, FieldN3, Expr5, VAHED1, KALAID, AID, NoFAKTOR, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, 
                      namehesab, startDate, enddate, Date1, KalaNumber, NameKala1
FROM         dbo.FunSub_05239044(@StartDate,@Enddate) AS FunSub_05239044_1 where 1=1
 ''

                      

   /* --------------------شرط حساب سرگروه روی ساب*/ 			
  set @ParmAndstr='' AND '' 			
  if(NOT (@Goid IS NULL)) 			
  BEGIN 				
  set @WhrStatement= @WhrStatement + @ParmAndstr + N''(Goid = @Goid )''; 				
  set @ParmAndstr='' AND '' 			
  END             
   /* --------------------شرط حساب کل  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@KolID IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KolID = @KolID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
   /*  --------------------شرط حساب معین  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@HesabID  IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(HesabID = @HesabID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
    /* --------------------   شرط حساب تفصیلی */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@THesabID  IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(THesabID = @THesabID )''; 				
    set @ParmAndstr='' AND '' 			
    END            
    
    
        /* --------------------   شرط کالا */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IdKala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KALAID = @IdKala )''; 				
		set @ParmAndstr='' AND '' 			
    END      
    
            /* --------------------   شرط گروه */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDGokala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(GOKID = @IDGokala )''; 				
		set @ParmAndstr='' AND '' 			
    END  
    
            /* --------------------   شرط انبار */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDA IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(AID = @IDA )''; 				
    set @ParmAndstr='' AND '' 			
    END      
    
   /* --------------------   شرط فاکتور */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@faktorN IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(NoFAKTOR = @faktorN )''; 				
    set @ParmAndstr='' AND '' 			
    END   
    
    set @SQLString=@SQLString +@WhrStatement+ N'' GROUP BY Expr1, HESABID, THESABID, KOLID, GOID, sh, NoeFa, FieldN3, Expr5, VAHED1, KALAID, AID, NoFAKTOR, namehesab, startDate, enddate, Date1, KalaNumber, NameKala1''
     			
    EXECUTE sp_executesql   @SQLString, @ParmDefinition,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@startDate =@startDate,@enddate =@enddate,@IdKala =@IdKala,	@IDA =@IDA,@IDGokala=@IDGokala,@faktorN =@faktorN;  		
    END  
         
         
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239043]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
        CREATE PROCEDURE [Rep_05239043](       
        	
			@Goid int, 
			@KolID int, 
			@HesabID int, 
			@THesabID int, 
			@startDate nvarchar(50), 
			@enddate nvarchar(50), 
			@IdKala nvarchar(50), 
			@IDA nvarchar(50),
			@IDGokala nvarchar(50),  
			@faktorN nvarchar(50)) 
         	AS 
         	
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';   
  
  
    SET @ParmDefinition = N''@Goid int=null, 
							@KolID int=null, 
							@HesabID int=null, 
							@THesabID int=null, 
							@startDate nvarchar(50)=N''''0'''', 
							@enddate nvarchar(50)=N''''99999999'''', 
							@IdKala int=null, 
							@IDA int=null,
							@IDGokala int=null, 
							@faktorN nvarchar(50)=null''    

  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate=N''00000000''; 				   
	END   
	      
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate=N''99999999''; 				   
	END 
								 
SET @SQLString =''     SELECT     gokid,NoeFa, SUM(Mande) AS Mande, SUM(Varde) AS Varde, SUM(Sadre) AS Sadre, Fi, Expr5, Expr1, MinOfDate1, MaxOfDate1, startDate, enddate, namehesab, KalaNumber, KALAID, NameKala1, 
                      AID
FROM         dbo.FunSub_05239043(@StartDate,@Enddate) AS FunSub_05239043_1 where (1=1)
''


   /* --------------------شرط حساب سرگروه روی ساب*/ 			
  set @ParmAndstr='' AND '' 			
  if(NOT (@Goid IS NULL)) 			
  BEGIN 				
  set @WhrStatement= @WhrStatement + @ParmAndstr + N''(Goid = @Goid )''; 				
  set @ParmAndstr='' AND '' 			
  END             
   /* --------------------شرط حساب کل  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@KolID IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KolID = @KolID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
   /*  --------------------شرط حساب معین  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@HesabID  IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(HesabID = @HesabID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
    /* --------------------   شرط حساب تفصیلی */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@THesabID  IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(THesabID = @THesabID )''; 				
    set @ParmAndstr='' AND '' 			
    END            
    
    
        /* --------------------   شرط کالا */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IdKala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KALAID = @IdKala )''; 				
		set @ParmAndstr='' AND '' 			
    END      
    
            /* --------------------   شرط انبار */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDA IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(AID = @IDA )''; 				
    set @ParmAndstr='' AND '' 			
    END      
    
                /* --------------------   شرط گروه */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDGokala IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(GOKID = @IDGokala )''; 				
    set @ParmAndstr='' AND '' 			
    END     
    
   /* --------------------   شرط فاکتور */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@faktorN IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(NoeFa = @faktorN )''; 				
    set @ParmAndstr='' AND '' 			
    END   
    
    set @SQLString=@SQLString +@WhrStatement+ N'' GROUP BY gokid,NoeFa, Fi, Expr5, Expr1, MinOfDate1, MaxOfDate1, startDate, enddate, namehesab, KalaNumber, KALAID, NameKala1, AID ''  	
     			
    EXECUTE sp_executesql   @SQLString, @ParmDefinition,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@startDate =@startDate,@enddate =@enddate,@IdKala =@IdKala,	@IDA =@IDA,@IDGokala=@IDGokala,@faktorN =@faktorN;  		
    END  
    

        	
        
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239042]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
         CREATE PROCEDURE [Rep_05239042](
			@Goid int, 
			@KolID int, 
			@HesabID int, 
			@THesabID int, 
			@startDate nvarchar(50), 
			@enddate nvarchar(50), 
			@IdKala INT=NULL,
			@IDA INT=NULL,
			@IDGokala INT=NULL,
			@faktorN nvarchar(50)) 
         	AS 
         	
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';   
  
  
    SET @ParmDefinition = N''@Goid int=null, 
							@KolID int=null, 
							@HesabID int=null, 
							@THesabID int=null, 
							@startDate nvarchar(50)=N''''0'''', 
							@enddate nvarchar(50)=N''''99999999'''', 
							@IdKala int=null, 
							@IDA int=null, 
							@IDGokala INT=NULL,
							@faktorN nvarchar(50)=null''    

  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate=N''00000000''; 				   
	END   
	      
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate=N''99999999''; 				   
	END 
			 				 
SET @SQLString =''SELECT    GOKID, GOID, KOLID, HESABID, THESABID, IDAUtoFact, sho, NoeFa, Sadre, FieldN3, VAHED1, KALAID, AID, NoFAKTOR, namehesab, startDate, enddate, Date1, KalaNumber, NameKala1, Expr5, MIN(Date1) 
                      AS MinOfDate1, MAX(Date1) AS MaxOfDate1
FROM         dbo.FunSub_05239042(@startDate, @enddate) AS FunSub_05239042_1 where (1=1)

''

                      

   /* --------------------شرط حساب سرگروه روی ساب*/ 			
  set @ParmAndstr='' AND '' 			
  if(NOT (@Goid IS NULL)) 			
  BEGIN 				
  set @WhrStatement= @WhrStatement + @ParmAndstr + N''(Goid = @Goid )''; 				
  set @ParmAndstr='' AND '' 			
  END             
   /* --------------------شرط حساب کل  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@KolID IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KolID = @KolID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
   /*  --------------------شرط حساب معین  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@HesabID  IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(HesabID = @HesabID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
    /* --------------------   شرط حساب تفصیلی */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@THesabID  IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(THesabID = @THesabID )''; 				
    set @ParmAndstr='' AND '' 			
    END            
    
    
        /* --------------------   شرط کالا */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IdKala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KALAID = @IdKala )''; 				
		set @ParmAndstr='' AND '' 			
    END      
    
            /* --------------------   شرط گروه */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDGokala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(GOKID = @IDGokala )''; 				
		set @ParmAndstr='' AND '' 			
    END  
    
            /* --------------------   شرط انبار */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDA IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(AID = @IDA )''; 				
    set @ParmAndstr='' AND '' 			
    END      
    
   /* --------------------   شرط فاکتور */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@faktorN IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(NoFAKTOR = @faktorN )''; 				
    set @ParmAndstr='' AND '' 			
    END   
    
    set @SQLString=@SQLString +@WhrStatement+ N'' GROUP BY GOKID,GOID, KOLID, HESABID, THESABID,Expr5, IDAUtoFact, sho, NoeFa, Sadre, FieldN3, VAHED1, KALAID, AID, NoFAKTOR, namehesab, startDate, enddate, Date1, KalaNumber, NameKala1 ''  	
     			
    EXECUTE sp_executesql   @SQLString, @ParmDefinition,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@startDate =@startDate,@enddate =@enddate,@IdKala =@IdKala,	@IDA =@IDA,@IDGokala=@IDGokala,@faktorN =@faktorN;  		
    END  
    

			
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894133]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_0523894133] ( 
  	@IdKala int, 
  	@IDA int, 
  	@KalaGO int, 
  	@startDate nvarchar(50), 
  	@enddate nvarchar(50)  )           
  	AS     
  	begin 	
  		SET NOCOUNT ON; 	 	
  		DECLARE @SQLString  NVARCHAR(4000);  	
  		DECLARE @WhrStatement  NVARCHAR(2000);         	
  		DECLARE @ParmDefinition  NVARCHAR(2000);         	
  		DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  		set @WhrStatement  = N'''';         	
  		set @ParmAndstr = N'''';                  	
  		SET @ParmDefinition = N''@IdKala int=null, 												
  								@IDA int=null, 												
  								@KalaGO int=null, 												
  								@startDate nvarchar(50)=''''0'''', 												
  								@enddate nvarchar(50)=''''99999999'''''';            	
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
begin 	 	  		 
	SET @SQLString =''SELECT     CONVERT(float, ISNULL(TBL_TRA.SumTedadKh, 0)) AS SumTedadKh, CONVERT(float, ISNULL(TBL_TRA.SumTedadVA, 0)) AS SumTedadVA, 
	                  CONVERT(float, ISNULL(TBL_TRA.SumTedadKhA, 0)) AS SumTedadKhA, CONVERT(float, ISNULL(TBL_TRA.SumTedadFo, 0)) AS SumTedadFo, 
	                  CONVERT(float, ISNULL(TBL_TRA.SumTedadBarFo, 0)) AS SumTedadBarFo, CONVERT(float, ISNULL(TBL_TRA.SumTedadBarKh, 0))            
	                  AS SumTedadBarKh, CONVERT(float, SUM(T_Kala.Mojodi)) AS M_Ebteda, ROUND(CONVERT(Float, ISNULL(TBL_TRA.MojoodiTra, 0)     
	                  + (CASE WHEN dbo.F1StartDate() >= @startDate THEN SUM(dbo.T_Kala.Mojodi) ELSE 0 END)), 2) AS Mande, 
	                  CONVERT(float, ISNULL(TBL_TRA.Varde, 0)) AS Varde, CONVERT(float, ISNULL(TBL_TRA.Sadre, 0)) AS Sadre, T_Kala.GOKID, 
	                  TBL_TRA.MojoodiTra, T_NameGoKala.NameGoKala1 FROM         
	                  (SELECT     GOKID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj)         
	                  + SUM(TedadBarKh) + SUM(TedadKhA)) AS MojoodiTra, SUM(TedadKh + TedadVorood + TedadBarFo + TedadVA) AS Varde,            
	                  SUM(TedadFo + TedadKhorooj + TedadBarKh + TedadKhA) AS Sadre, SUM(TedadKh) AS SumTedadKh, SUM(TedadVA) AS SumTedadVA,             
	                  SUM(TedadKhA) AS SumTedadKhA, SUM(TedadFo) AS SumTedadFo, SUM(TedadBarFo) AS SumTedadBarFo, SUM(TedadBarKh)                          
	                  AS SumTedadBarKh                         
	                   FROM         [TRANSACTION]                         WHERE     (Date1 BETWEEN @startDate AND @enddate)'' 			 			 			
set @ParmAndstr='' AND '' 			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end                                
set @SQLString=@SQLString + @WhrStatement+'' GROUP BY GOKID) AS TBL_TRA RIGHT OUTER JOIN                       
											T_Kala ON TBL_TRA.GOKID = T_Kala.GOKID RIGHT OUTER JOIN                       
											T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID WHERE     (T_Kala.VKalaID = 1)'' 			
set @WhrStatement='''' 			 			
set @ParmAndstr='' AND ''   			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			
if(NOT (@IdKala  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			 			
if(NOT (@KalaGO  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
	set @ParmAndstr='' AND '' 			
end 			   			
set @SQLString=@SQLString + @WhrStatement + N'' GROUP BY T_Kala.GOKID, CONVERT(float, ISNULL(TBL_TRA.Varde, 0)), CONVERT(float, ISNULL(TBL_TRA.Sadre, 0)), TBL_TRA.MojoodiTra, CONVERT(float,                        
												ISNULL(TBL_TRA.SumTedadKh, 0)), CONVERT(float, ISNULL(TBL_TRA.SumTedadVA, 0)), CONVERT(float, ISNULL(TBL_TRA.SumTedadKhA, 0)),                        
												CONVERT(float, ISNULL(TBL_TRA.SumTedadFo, 0)), CONVERT(float, ISNULL(TBL_TRA.SumTedadBarFo, 0)), CONVERT(float,                        
												ISNULL(TBL_TRA.SumTedadBarKh, 0)), T_NameGoKala.NameGoKala1'' 	 			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IDA=@IDA,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@enddate=@enddate;  		
end                                                  
return end                      
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894131]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_0523894131] ( 
  	@IdKala int, 
  	@IDA int, 
  	@KalaGO int, 
  	@startDate nvarchar(50), 
  	@enddate nvarchar(50)  )           
  	AS     
  	begin 	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  	set @WhrStatement  = N'''';         	
  	set @ParmAndstr = N'''';                  	
  	SET @ParmDefinition = N''@IdKala int=null, 												
  							@IDA int=null, 												
  							@KalaGO int=null, 												
  							@startDate nvarchar(50)=''''0'''', 												
  							@enddate nvarchar(50)=''''99999999'''''';            	
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
begin 	 	  		 
	SET @SQLString =''SELECT     Convert(float,ISNULL(SumTedadKh,0)) as SumTedadKh,  								
	Convert(float,ISNULL(SumTedadVA,0)) as SumTedadVA,  								
	Convert(float,ISNULL(SumTedadKhA,0)) as SumTedadKhA,  								
	Convert(float,ISNULL(SumTedadFo,0)) as SumTedadFo,  								
	Convert(float,ISNULL(SumTedadBarFo,0)) as SumTedadBarFo,  								
	Convert(float,ISNULL(SumTedadBarKh,0)) as SumTedadBarKh, 							
	convert(float,SUM(dbo.T_Kala.Mojodi)) as M_Ebteda,round(convert(Float,ISNULL(TBL_TRA.MojoodiTra,0)+ (case when dbo.F1StartDate()>=@startDate 
	then SUM(dbo.T_Kala.Mojodi) else 0 end)),2) AS Mande, convert(float,ISNULL(TBL_TRA.Varde, 0)) AS Varde, Convert(float,ISNULL(TBL_TRA.Sadre, 0)) 	
	AS Sadre, dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, dbo.T_NameKala.KalaNumber AS KalaNumber,  							
	dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib, dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode,  							
	dbo.T_Kala.NameKalaID, TBL_TRA.MojoodiTra 							
	FROM         dbo.T_NameKala INNER JOIN 							
	dbo.T_NameVahed ON dbo.T_NameKala.VAHED1 = dbo.T_NameVahed.VAHED1 INNER JOIN 							
	dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN 							
	dbo.T_NameVahed T_NameVahed_2 ON dbo.T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN 							
	(SELECT     KALAID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj) + SUM(TedadBarKh) 
	+ SUM(TedadKhA)) AS MojoodiTra, SUM(TedadKh + TedadVorood + TedadBarFo + TedadVA) AS Varde,                        
	SUM(TedadFo + TedadKhorooj + TedadBarKh + TedadKhA) AS Sadre, SUM(TedadKh) AS SumTedadKh, SUM(TedadVA) AS SumTedadVA,                        
	SUM(TedadKhA) AS SumTedadKhA, SUM(TedadFo) AS SumTedadFo, SUM(TedadBarFo) AS SumTedadBarFo, SUM(TedadBarKh)                        
	AS SumTedadBarKh FROM         [TRANSACTION] Where ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) '' 			 			 			
	
	set @ParmAndstr='' AND '' 			
	if(NOT (@IDA  IS NULL)) 			
	begin 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].AID = @IDA )''; 				
		set @ParmAndstr='' AND '' 			
	end                                
	set @SQLString=@SQLString + @WhrStatement+'' GROUP BY KALAID) TBL_TRA ON dbo.T_Kala.NameKalaID = TBL_TRA.KALAID WHERE     (dbo.T_Kala.VKalaID = 1) ''   
	set @WhrStatement='''' 			 			
	set @ParmAndstr='' AND ''   			
	if(NOT (@IDA  IS NULL)) 			
	begin 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @IDA )''; 				
		set @ParmAndstr='' AND '' 			
	end 			 			
	if(NOT (@IdKala  IS NULL)) 			
	begin 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
		set @ParmAndstr='' AND '' 			
	end 			 			 			
	if(NOT (@KalaGO  IS NULL)) 			
	begin 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
		set @ParmAndstr='' AND '' 			
	end 			   			
	set @SQLString=@SQLString + @WhrStatement + N''  GROUP BY  dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed, dbo.T_NameKala.KalaNumber, 
	dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib,                        
	dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode, dbo.T_Kala.NameKalaID, convert(float,ISNULL(TBL_TRA.Varde, 0)),
convert(float,ISNULL(TBL_TRA.Sadre, 0)), TBL_TRA.MojoodiTra, Convert(float,ISNULL(SumTedadKh,0)), Convert(float,ISNULL(SumTedadVA,0)), 
Convert(float,ISNULL(SumTedadKhA,0)), Convert(float,ISNULL(SumTedadFo,0)), Convert(float,ISNULL(SumTedadBarFo,0)), Convert(float,ISNULL(SumTedadBarKh,0)) '' 
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IDA=@IDA,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@enddate=@enddate;  		
end                                                  
return end                      
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894126]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_0523894126] ( 
  	@IdKala int, 
  	@IDA int, 
  	@KalaGO int, 
  	@startDate nvarchar(50), 
  	@enddate nvarchar(50)  )           
  	AS     
  	begin 	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  	set @WhrStatement  = N'''';         	
  	set @ParmAndstr = N'''';                  	
  	SET @ParmDefinition = N''@IdKala int=null, 												
  							@IDA int=null, 												
  							@KalaGO int=null, 												
  							@startDate nvarchar(50)=''''0'''', 												
  							@enddate nvarchar(50)=''''99999999'''''';            	
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
begin 	 	  		 
	SET @SQLString =''SELECT     convert(float,SUM(dbo.T_Kala.Mojodi)) as M_Ebteda,round(convert(Float,ISNULL(TBL_TRA.MojoodiTra,0)+ (case when dbo.F1StartDate()>=@startDate then SUM(dbo.T_Kala.Mojodi) else 0 end)),2) AS Mande, convert(float,ISNULL(TBL_TRA.Varde, 0)) AS Varde, Convert(float,ISNULL(TBL_TRA.Sadre, 0)) 							
	AS Sadre, dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, dbo.T_NameKala.KalaNumber AS KalaNumber,  							dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib, dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode,  							dbo.T_Kala.NameKalaID, TBL_TRA.MojoodiTra 							
	                 FROM         dbo.T_NameKala INNER JOIN 							dbo.T_NameVahed ON dbo.T_NameKala.VAHED1 = dbo.T_NameVahed.VAHED1 INNER JOIN 							dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN 							dbo.T_NameVahed T_NameVahed_2 ON dbo.T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN 		
	                 					(SELECT     KALAID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj)  							+ SUM(TedadBarKh) + SUM(TedadKhA)) AS MojoodiTra, SUM(TedadKh + TedadVorood + TedadBarFo + TedadVA) AS Varde,  							SUM(TedadFo + TedadKhorooj + TedadBarKh + TedadKhA) AS Sadre 	
	                 											FROM         dbo.[TRANSACTION] Where ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) '' 			 			 			set @ParmAndstr='' AND '' 			if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end      
                          
set @SQLString=@SQLString + @WhrStatement+'' GROUP BY KALAID) TBL_TRA ON dbo.T_Kala.NameKalaID = TBL_TRA.KALAID WHERE     (dbo.T_Kala.VKalaID = 1) ''  
set @WhrStatement='''' 			 			
set @ParmAndstr='' AND ''   			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			
if(NOT (@IdKala  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			 			
if(NOT (@KalaGO  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
	set @ParmAndstr='' AND '' 			
end 			   			
set @SQLString=@SQLString + @WhrStatement + N''  GROUP BY  dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib,                        
												dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode, dbo.T_Kala.NameKalaID, convert(float,ISNULL(TBL_TRA.Varde, 0)),                        
												convert(float,ISNULL(TBL_TRA.Sadre, 0)), TBL_TRA.MojoodiTra '' 	 	 			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IDA=@IDA,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@enddate=@enddate;  		
end                                                  
return end                      
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894024]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_0523894024] ( 
  	@IdKala int, 
  	@IDA int, 
  	@KalaGO int, 
  	@startDate nvarchar(50), 
  	@enddate nvarchar(50)  )           
  	AS     
  	begin 	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  	set @WhrStatement  = N'''';         	
  	set @ParmAndstr = N'''';                  	
  	SET @ParmDefinition = N''@IdKala int=null, 												
  							@IDA int=null, 												
  							@KalaGO int=null, 												
  							@startDate nvarchar(50)=''''0'''', 												
  							@enddate nvarchar(50)=''''99999999'''''';            	
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
begin 	 	  		 
	SET @SQLString = ''SELECT     round(convert(Float,ISNULL(TBL_TRA.MojoodiTra,0)+ (case when dbo.F1StartDate()>=@startDate then SUM(dbo.T_Kala.Mojodi) else 0 end)),2) AS Mande, ISNULL(TBL_TRA.Varde, 0) AS Varde, ISNULL(TBL_TRA.Sadre, 0)  							
	AS Sadre, dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, dbo.T_NameKala.KalaNumber AS KalaNumber,  							dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib, dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode,  							
	dbo.T_Kala.NameKalaID, TBL_TRA.MojoodiTra 							FROM         dbo.T_NameKala INNER JOIN 							dbo.T_NameVahed ON dbo.T_NameKala.VAHED1 = dbo.T_NameVahed.VAHED1 INNER JOIN 							
	dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN 							dbo.T_NameVahed T_NameVahed_2 ON dbo.T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN 							
	(SELECT     KALAID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj)  							+ SUM(TedadBarKh) + SUM(TedadKhA)) AS MojoodiTra, SUM(TedadKh + TedadVorood + TedadBarFo + TedadVA) AS Varde,  							
	SUM(TedadFo + TedadKhorooj + TedadBarKh + TedadKhA) AS Sadre 							
	 FROM         dbo.[TRANSACTION] Where ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)  '' 			 			 			

set @ParmAndstr='' AND '' 			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end    
                            
set @SQLString=@SQLString + @WhrStatement+'' GROUP BY KALAID) TBL_TRA ON dbo.T_Kala.NameKalaID = TBL_TRA.KALAID WHERE     (dbo.T_Kala.VKalaID = 1) ''               			
set @WhrStatement='''' 			 			
set @ParmAndstr='' AND ''   			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			
if(NOT (@IdKala  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			 			
if(NOT (@KalaGO  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
	set @ParmAndstr='' AND '' 			
end 			   			
set @SQLString=@SQLString + @WhrStatement + N''  GROUP BY dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib,                        
												dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode, dbo.T_Kala.NameKalaID, ISNULL(TBL_TRA.Varde, 0),                        
												ISNULL(TBL_TRA.Sadre, 0), TBL_TRA.MojoodiTra   HAVING      (ROUND(CONVERT(Float, ISNULL(TBL_TRA.MojoodiTra, 0) + (CASE WHEN dbo.F1StartDate() >= @startDate THEN SUM(dbo.T_Kala.Mojodi) ELSE 0 END)), 2) <> 0)          '' 	 	 			

EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IDA=@IDA,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@enddate=@enddate;  		
end                                                  

return end   


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523893921]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_0523893921] ( 
  	@IdKala int, 
  	@IDA int, 
  	@KalaGO int, 
  	@startDate nvarchar(50), 
  	@enddate nvarchar(50)  )           
  	
  	AS     
  	
  	begin 	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  	set @WhrStatement  = N'''';         	
  	set @ParmAndstr = N'''';                  	
  	SET @ParmDefinition = N''@IdKala int=null, 												
  							@IDA int=null, 												
  							@KalaGO int=null, 												
  							@startDate nvarchar(50)=''''0'''', 												
  							@enddate nvarchar(50)=''''99999999'''''';            	
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
begin 	 	  		 
	SET @SQLString =  					''SELECT     round(convert(Float,ISNULL(TBL_TRA.MojoodiTra,0)+ (case when dbo.F1StartDate()>=@startDate then SUM(dbo.T_Kala.Mojodi) else 0 end)),2) AS Mande, ISNULL(TBL_TRA.Varde, 0) AS Varde, ISNULL(TBL_TRA.Sadre, 0)  							AS Sadre, dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, dbo.T_NameKala.KalaNumber AS KalaNumber,  
								dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib, dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode,  							dbo.T_Kala.NameKalaID, TBL_TRA.MojoodiTra 							FROM         dbo.T_NameKala INNER JOIN 							dbo.T_NameVahed ON dbo.T_NameKala.VAHED1 = dbo.T_NameVahed.VAHED1 INNER JOIN 	
														dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN 							dbo.T_NameVahed T_NameVahed_2 ON dbo.T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN 							(SELECT     KALAID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj)  
																					+ SUM(TedadBarKh) + SUM(TedadKhA)) AS MojoodiTra, SUM(TedadKh + TedadVorood + TedadBarFo + TedadVA) AS Varde,  							SUM(TedadFo + TedadKhorooj + TedadBarKh + TedadKhA) AS Sadre 							FROM         dbo.[TRANSACTION] Where ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) '' 	
set @ParmAndstr='' AND '' 			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end                                
set @SQLString=@SQLString + @WhrStatement+'' GROUP BY KALAID) TBL_TRA ON dbo.T_Kala.NameKalaID = TBL_TRA.KALAID WHERE     (dbo.T_Kala.VKalaID = 1) ''  

set @WhrStatement='''' 			 			
set @ParmAndstr='' AND ''   			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			
if(NOT (@IdKala  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			 			
if(NOT (@KalaGO  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
	set @ParmAndstr='' AND '' 			
end 			   			
set @SQLString=@SQLString + @WhrStatement + N''  GROUP BY dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib,                        
												dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode, dbo.T_Kala.NameKalaID, ISNULL(TBL_TRA.Varde, 0),                        
												ISNULL(TBL_TRA.Sadre, 0), TBL_TRA.MojoodiTra Having round(convert(Float,ISNULL(TBL_TRA.MojoodiTra,0)+ (case when dbo.F1StartDate()>=@startDate then SUM(dbo.T_Kala.Mojodi) else 0 end)),2)<0'' 	 	 			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IDA=@IDA,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@enddate=@enddate;  		
end                                                  
return end                      
' 
END

GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05238834]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_05238834] ( @IdKala int, @KalaGO int )           AS     
  begin 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';                  
  SET @ParmDefinition = N''@IdKala int=null,@KalaGO int=null'';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
  begin 	 	  		
  	SET @SQLString = ''SELECT     T_NameKala.BarCode, T_NameKala.NameKala1, T_NameVahed.NameVahed, SUM(ROUND(CONVERT(float, T_Kala.Mojodi), 2)) AS Mojodi,  
  	                 AVG(T_Kala.UnitPrice) AS Expr1, T_NameKala.KalaNumber FROM         T_Kala INNER JOIN                       
  	                 T_NameKala ON T_Kala.NameKalaID = T_NameKala.ID INNER JOIN                       
  	                 T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN                       
  	                 T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID '' 			 			
  	SET @WhrStatement='''' 			 			
  	set @ParmAndstr='' Where '' 			
  	if(NOT (@IdKala  IS NULL)) 			
  	begin 				
  		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
  		set @ParmAndstr='' AND '' 			
  	end 			 			 			
  	if(NOT (@KalaGO  IS NULL)) 			
  	begin 				
  		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
  		set @ParmAndstr='' AND '' 			
  	end 			   			
  	set @SQLString=@SQLString + @WhrStatement+N'' GROUP BY T_NameKala.KalaNumber,T_NameKala.BarCode, T_NameKala.NameKala1, T_NameVahed.NameVahed'' 	 	 			EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IdKala=@IdKala,@KalaGO=@KalaGO;  		end                                                  return end                      
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05238833]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_05238833] ( @IdKala int, @KalaGO int )           AS     
  
  begin 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';                  
  SET @ParmDefinition = N''@IdKala int=null,@KalaGO int=null'';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
  begin 	 	  		
  	SET @SQLString = ''SELECT     T_NameKala.BarCode, T_NameKala.NameKala1, T_NameVahed.NameVahed, SUM(ROUND(CONVERT(float, T_Kala.Mojodi), 2)) AS Mojodi, 
  						AVG(T_Kala.UnitPrice) AS Expr1 FROM         T_Kala INNER JOIN                       
  						T_NameKala ON T_Kala.NameKalaID = T_NameKala.ID INNER JOIN                       
  						T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN                       
  						T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID '' 			 			
	SET @WhrStatement='''' 			 			
	set @ParmAndstr='' Where '' 			
	if(NOT (@IdKala  IS NULL)) 			
	begin 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
		set @ParmAndstr='' AND '' 			
	end 			 			 			
	if(NOT (@KalaGO  IS NULL)) 			
	begin 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
		set @ParmAndstr='' AND '' 			
	end 			   			
	set @SQLString=@SQLString + @WhrStatement+N'' GROUP BY T_NameKala.BarCode, T_NameKala.NameKala1, T_NameVahed.NameVahed'' 	 	 			
	EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IdKala=@IdKala,@KalaGO=@KalaGO;  		
  
end                                                  
return end                      
' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0121]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
   CREATE PROCEDURE [Rep_0121] (   
   	@SortRep nvarchar(255),  	
   	@Goid int, 	
   	@KolID int,  	
   	@HesabID int, 	
   	@THesabID int,  	
   	@ShChk nvarchar(100), 	
   	@VazChk nvarchar(50), 	
   	@StartDateSodor nvarchar(50)=''0'', 	
   	@EnddateSodor nvarchar(50)=''99999999'', 	
   	@StartdateRes nvarchar(50)=''0'', 	
   	@EnddateRes nvarchar(50)=''99999999'' )              AS        
   	
   	BEGIN 	SET NOCOUNT ON; 	 	   
   	DECLARE @SQLString  NVARCHAR(4000);  	   
   	DECLARE @WhrStatement  NVARCHAR(2000);         	   
   	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
   	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
   	set @WhrStatement  = N'''';         	   
   	set @ParmAndstr = N'''';                  	   
   	SET @ParmDefinition = N''@SortRep nvarchar(255)=null,  							
   							@Goid int=null, 							
   							@KolID int=null,  							
   							@HesabID int=null, 							
   							@THesabID int=null,  							
   							@ShChk nvarchar(100)=null, 							
   							@VazChk nvarchar(50)=null, 							
   							@StartDateSodor nvarchar(50)=''''0'''', 							
   							@EnddateSodor nvarchar(50)=''''99999999'''', 							
   							@StartdateRes nvarchar(50)=''''0'''', 							
   							@EnddateRes nvarchar(50)=''''99999999'''''';            	   
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		    
BEGIN         
	if(@StartDateSodor IS NULL) 			   
	BEGIN 				   
		set @StartDateSodor=''0''; 				   
	END         
	if(@EnddateSodor IS NULL) 			   
	BEGIN 				   
		set @EnddateSodor=''99999999''; 				   
	END         if(@StartdateRes IS NULL) 			   
	BEGIN 				   
		set @StartdateRes=''0''; 				   
	END         
	
	if(@EnddateRes IS NULL) 			   
	BEGIN 				   
		set @EnddateRes=''99999999''; 				   
	END     	  		    
	SET @SQLString =''SELECT     Sharh, ShSeryal, FieldN1, GOID, KOLID, HESABID, THESABID, DateSO, DateSR, HESABID2, THESABID2, chkvaz, Pardakht_az_Hesab, 
								ASNADID, Mablagh, Flag2, SharhSub, DarBed, variz_be_Hesab,                        HESABIDM, THESABIDM 
								FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, 
								T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2,                         
								T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN ''''پاس شده'''' WHEN (T_Asnad.[FieldN1]) = 2 THEN ''''باطل شده '''' 
								WHEN (T_Asnad.[FieldN1]) = 3 THEN ''''نقدی '''' WHEN (T_Asnad.[FieldN1]) = 4 THEN ''''پاس نشده '''' END AS chkvaz, 
								dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab,  T_Asnad.ASNADID, T_Asnad.Mablagh, 
								T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1,       
								[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, 
								dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID)AS variz_be_Hesab                         
								              FROM         T_Asnad INNER JOIN                                               
								              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2 WHERE     
								              ([TRANSACTION].DARBed > 0)                         UNION ALL                         
								              SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, 
								              T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR,                     
								              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN ''''پاس شده'''' 
								              WHEN (T_Asnad_1.[FieldN1]) = 2 THEN ''''باطل شده '''' WHEN (T_Asnad_1.[FieldN1]) = 3 THEN ''''نقدی '''' 
								              WHEN (T_Asnad_1.[FieldN1]) = 4 THEN ''''پاس نشده '''' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, 
								              T_Asnad_1.HESABID, T_Asnad_1.THESABID)                                                AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM,                                                Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab                         FROM         T_Asnad AS T_Asnad_1 INNER JOIN                                               Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number                         WHERE     (T_Asnad_1.Flag2 = 1)) AS SubAsnad WHERE     (DateSR BETWEEN @StartdateRes AND @EnddateRes) and (DateSO BETWEEN @StartDateSodor AND @EnddateSodor)    '' 	    		    
	
	/* --------------------شرط حساب سرگروه روی ساب*/ 			   
	set @ParmAndstr='' AND '' 			   
	if(NOT (@Goid IS NULL)) 			   
	BEGIN 				   
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''((Goid = @Goid))''; 				   
		set @ParmAndstr='' AND '' 			   
	END                 
	/* --------------------شرط حساب کل  */ 			    
	
	set @ParmAndstr='' AND '' 			    
	if(NOT (@KolID IS NULL)) 			    
	BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''((KolID = @KolID) )''; 				    
		set @ParmAndstr='' AND '' 			    
	END                 
	/*  --------------------شرط حساب معین  */ 			   
	set @ParmAndstr='' AND '' 
				    
	if(NOT (@HesabID  IS NULL)) 			    
	BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''((HesabID = @HesabID) or (HESABIDM = @HesabID))''; 				    
		set @ParmAndstr='' AND '' 			   
	END                  
		
		/* --------------------   شرط حساب تفصیلی */ 			     
		
	set @ParmAndstr='' AND '' 			     
	if(NOT (@THesabID  IS NULL)) 			     
	BEGIN 				     
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''((THesabID = @THesabID) or (THESABIDM = @THesabID) )''; 				     
		set @ParmAndstr='' AND '' 			     
	END                    		
	/* --------------------   وضعیت  چک */ 			     
	set @ParmAndstr='' AND '' 			     
	if(NOT (@VazChk  IS NULL)) 			     
	BEGIN 				     
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (FieldN1= @VazChk ) ''; 				     
		set @ParmAndstr='' AND '' 			     
	END    		
	/* --------------------   شماره سریال چک */ 			     
	set @ParmAndstr='' AND '' 			     
	if(NOT (@ShChk  IS NULL)) 			     
	BEGIN 				     
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (ShSeryal = @ShChk ) ''; 				     
		set @ParmAndstr='' AND '' 			     
	END    		
	/* --------------------  شماره سریال  1 مرتب سازی */ 			     
	if(@SortRep=N''ShSeryal'') 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY ShSeryal ''; 				     
	END  	 		
	/* --------------------   تاریخ صدور  2 مرتب سازی */ 			 			     
	if(@SortRep=N''DateSO'') 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement + N''  ORDER BY DateSO ''; 				     
	END    	    
	
	/* --------------------   تاریخ سررسید  3 مرتب سازی */ 			     
	if(@SortRep=N''DateSR'') 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement + N''  ORDER BY DateSR ''; 				     
	END    		    
	/* --------------------   تاریخ سررسید  3 مرتب سازی */ 			     
	if(@SortRep  IS NULL) 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement + N''  ORDER BY DateSR ''; 				     
	END           			          
	EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@ShChk=@ShChk,@VazChk=@VazChk,@StartDateSodor=@StartDateSodor,@EnddateSodor=@EnddateSodor,@StartdateRes=@StartdateRes,@EnddateRes=@EnddateRes;  		     
END 		      

/* --------پایان چاپ*/  		 		  

return END      
' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0215]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
   CREATE PROCEDURE [Rep_0215] (   
   	@SortRep nvarchar(255),  	
   	@Goid int, 	
   	@KolID int,  	
   	@HesabID int, 	
   	@THesabID int,  	
   	@ShChk nvarchar(100), 	
   	@VazChk nvarchar(50), 	
   	@StartDateSodor nvarchar(50)=''0'', 	
   	@EnddateSodor nvarchar(50)=''99999999'', 	
   	@StartdateRes nvarchar(50)=''0'', 	
   	@EnddateRes nvarchar(50)=''99999999'' )              AS        
   	BEGIN 	SET NOCOUNT ON; 	 	   
   	DECLARE @SQLString  NVARCHAR(4000);  	   
   	DECLARE @WhrStatement  NVARCHAR(2000);         	   
   	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
   	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
   	set @WhrStatement  = N'''';         	   
   	set @ParmAndstr = N'''';                  	   
   	SET @ParmDefinition = N''@SortRep nvarchar(255)=null,  							
   							@Goid int=null, 							
   							@KolID int=null,  							
   							@HesabID int=null, 							
   							@THesabID int=null,  							
   							@ShChk nvarchar(100)=null, 							
   							@VazChk nvarchar(50)=null, 							
   							@StartDateSodor nvarchar(50)=''''0'''', 							
   							@EnddateSodor nvarchar(50)=''''99999999'''', 							
   							@StartdateRes nvarchar(50)=''''0'''', 							
   							@EnddateRes nvarchar(50)=''''99999999'''''';            	   
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		    
BEGIN         
	if(@StartDateSodor IS NULL) 			   
	BEGIN 				   
		set @StartDateSodor=''0''; 				   
	END         
	
	if(@EnddateSodor IS NULL) 			   
	BEGIN 				   
		set @EnddateSodor=''99999999''; 				   
	END         if(@StartdateRes IS NULL) 			   
	BEGIN 				   
		set @StartdateRes=''0''; 				   
	END         
	if(@EnddateRes IS NULL) 			   
	BEGIN 				   
		set @EnddateRes=''99999999''; 				   
	END     	  		    
	
	SET @SQLString =''SELECT     ASNADDARID, Sharh, Mablagh, DateTA, DateVO, ShSeryal, FieldN1, GOID, KOLID, HESABID , THESABID, dbo.HesabName(KOLID, HESABID, THESABID) AS Daryaft_az_Hesab,                        CASE WHEN (T_Asnad_Dar.[FieldN1]) = 1 THEN ''''پاس شده'''' WHEN (T_Asnad_Dar.[FieldN1]) = 2 THEN ''''برگشتي '''' WHEN (T_Asnad_Dar.[FieldN1])                        = 3 THEN ''''در جریان وصول '''' WHEN (T_Asnad_Dar.[FieldN1]) = 4 THEN ''''پاس نشده'''' WHEN (T_Asnad_Dar.[FieldN1]) = 5 THEN ''''پاس شده '''' WHEN (T_Asnad_Dar.[FieldN1])                        = 6 THEN '''' برگشتی'''' END AS chkvaz, DARID3 					FROM         T_Asnad_Dar WHERE     (DateVO BETWEEN @StartdateRes AND @EnddateRes) and (DateTA BETWEEN @StartDateSodor AND @EnddateSodor)    '' 	    		    
	/* --------------------شرط حساب سرگروه روی ساب*/ 			   
	set @ParmAndstr='' AND '' 			   
	if(NOT (@Goid IS NULL)) 			   
	BEGIN 				   
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(Goid = @Goid )''; 				  
		set @ParmAndstr='' AND '' 			   
	END                 
	
	/* --------------------شرط حساب کل  */ 			    
	
	set @ParmAndstr='' AND '' 			    
	if(NOT (@KolID IS NULL)) 			    
	BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KolID = @KolID )''; 				    
		set @ParmAndstr='' AND '' 			    
	END                 
	
	/*  --------------------شرط حساب معین  */ 			    
	set @ParmAndstr='' AND '' 			    
	if(NOT (@HesabID  IS NULL)) 			    
	BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(HesabID = @HesabID )''; 				    
		set @ParmAndstr='' AND '' 			    
	END                  
	
	/* --------------------   شرط حساب تفصیلی */ 			     
	set @ParmAndstr='' AND '' 			     
	if(NOT (@THesabID  IS NULL)) 			     
	BEGIN 				     
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(THesabID = @THesabID )''; 				     
		set @ParmAndstr='' AND '' 			     
	END                    		
	
	/* --------------------   وضعیت  چک */ 			     
	set @ParmAndstr='' AND '' 			     
	if(NOT (@VazChk  IS NULL)) 			     
	BEGIN 				     
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (FieldN1= @VazChk ) ''; 				     
		set @ParmAndstr='' AND '' 			     
	END    		
	
	/* --------------------   شماره سریال چک */ 			     
	set @ParmAndstr='' AND '' 			     
	if(NOT (@ShChk  IS NULL)) 			     
	BEGIN 				     
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (ShSeryal = @ShChk ) ''; 				     
		set @ParmAndstr='' AND '' 			     
	END    		
	
	/* --------------------  شماره سریال  1 مرتب سازی */ 			     
	if(@SortRep=N''ShSeryal'') 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY ShSeryal ''; 				     
	END  	 		
	
	/* --------------------   تاریخ صدور  2 مرتب سازی */ 			 			     
	if(@SortRep=N''DateTA'') 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement + N''  ORDER BY DateTA ''; 				     
	END    	    
	
	/* --------------------   تاریخ سررسید  3 مرتب سازی */ 			     
	if(@SortRep=N''DateVO'') 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement + N''  ORDER BY DateVO ''; 				     
	END    		    
	
	/* --------------------   تاریخ سررسید  3 مرتب سازی */ 			     
	if(@SortRep  IS NULL) 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement + N''  ORDER BY DateVO ''; 				     
	END           			          
	EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@ShChk=@ShChk,@VazChk=@VazChk,@StartDateSodor=@StartDateSodor,@EnddateSodor=@EnddateSodor,@StartdateRes=@StartdateRes,@EnddateRes=@EnddateRes;  		     
END 		      
/* --------پایان چاپ*/  		 		  

return END 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030522]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
          CREATE PROCEDURE [Rep_030522](
          	                  
          
              	@startDate nvarchar(50), 
        	@enddate nvarchar(50), 
        	@GOID INT,
        	@KOLID INT,
        	@HESABID INT,
        	@THESABID int) AS 
BEGIN        	
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'''';         	   
  	set @ParmAndstr = N'''';     
  	   
  	 SET @ParmDefinition = N''@startDate nvarchar(50)=''''0'''', 
							@enddate nvarchar(50)=''''99999999'''', 
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null''; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate=''0''; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate=''99999999''; 				   
	END   
	        
set @SQLString=N''            SELECT      GOID, KOLID, HESABID, THESABID, SUM(SumOfPARBes) AS SumOfSumOfPARBes, SUM(SumOfDARBed) AS SumOfSumOfDARBed, SUM(mande) AS SumOfmande, 
                      MIN(MinOfDate1) AS MinOfDate1, MAX(MaxOfDate1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab, @startdate AS startdate, @enddate AS enddate
FROM         dbo.Q_Bed_Bes(@startdate, @enddate) AS Q_Bed_Bes
WHERE     (mande < 0) ''


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].GOID = @Goid )''; 				   
		set @ParmAndstr='' AND '' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].KolID = @KolID )''; 				    
   	set @ParmAndstr='' AND '' 			    
   END              
   /*  --------------------شرط حساب معین  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].HESABID = @HESABID )''; 				    
   	set @ParmAndstr='' AND '' 			    
   END          
   /*  --------------------شرط حساب تفصیلی  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].THESABID = @THESABID )''; 				    
   	set @ParmAndstr='' AND '' 			    
   END 
   
   
   set @SQLString= @SQLString + @WhrStatement  + N'' GROUP BY GOID, KOLID, HESABID, THESABID, dbo.HesabName(KOLID, HESABID, THESABID)   ''



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID;  		     

END      
 

 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030521]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
          CREATE PROCEDURE [Rep_030521](
          	
              	@startDate nvarchar(50), 
        	@enddate nvarchar(50), 
        	@GOID INT,
        	@KOLID INT,
        	@HESABID INT,
        	@THESABID int) AS 
BEGIN        	
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'''';         	   
  	set @ParmAndstr = N'''';     
  	   
  	 SET @ParmDefinition = N''@startDate nvarchar(50)=''''0'''', 
							@enddate nvarchar(50)=''''99999999'''', 
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null''; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate=''0''; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate=''99999999''; 				   
	END   
	        
set @SQLString=N'' SELECT      GOID, KOLID, HESABID, THESABID, SUM(SumOfPARBes) AS SumOfSumOfPARBes, SUM(SumOfDARBed) AS SumOfSumOfDARBed, SUM(mande) AS SumOfmande, 
                      MIN(MinOfDate1) AS MinOfDate1, MAX(MaxOfDate1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab, @startdate AS startdate, @enddate AS enddate
FROM         dbo.Q_Bed_Bes(@startdate, @enddate) AS Q_Bed_Bes
WHERE     (mande < 0) ''


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].GOID = @Goid )''; 				   
		set @ParmAndstr='' AND '' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].KolID = @KolID )''; 				    
   	set @ParmAndstr='' AND '' 			    
   END              
   /*  --------------------شرط حساب معین  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].HESABID = @HESABID )''; 				    
   	set @ParmAndstr='' AND '' 			    
   END          
   /*  --------------------شرط حساب تفصیلی  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].THESABID = @THESABID )''; 				    
   	set @ParmAndstr='' AND '' 			    
   END 
   
   
   set @SQLString= @SQLString + @WhrStatement  + N'' GROUP BY GOID, KOLID, HESABID, THESABID, dbo.HesabName(KOLID, HESABID, THESABID) ''
						
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID;  		     

END 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030212]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
   CREATE PROCEDURE [Rep_030212] (  	@SortRep nvarchar(255),  	@NoeSortRep nvarchar(255), 	@Goid int, 	@KolID int,  	@HesabID nvarchar(50), 	@THesabID int,  	@StartDate nvarchar(50)=''0'', 	@Enddate nvarchar(50)=''99999999'', 	@StartSanad nvarchar(50), 	@EndSanad nvarchar(50) )              AS        BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	   DECLARE @WhrStatement  NVARCHAR(2000);         	   DECLARE @ParmDefinition  NVARCHAR(2000);         	   DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   set @WhrStatement  = N'''';         	   set @ParmAndstr = N'''';                  	   SET @ParmDefinition = N''@SortRep nvarchar(255)=null,  							@NoeSortRep nvarchar(255)=null, 							@Goid int=null, 							@KolID int=null,  							@HesabID nvarchar(50)=null, 							@THesabID int=null, 							@StartDate nvarchar(50)=''''0'''', 							@Enddate nvarchar(50)=''''99999999'''', 							@StartSanad nvarchar(50)=null, 							@EndSanad nvarchar(50)=null'';            	   /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		    BEGIN       if(@StartDate IS NULL) 			   BEGIN 				   set @StartDate=''0''; 				   END         if(@Enddate IS NULL) 			   BEGIN 				   set @Enddate=''99999999''; 				   END         if(@NoeSortRep IS NULL) 			   BEGIN 				   set @NoeSortRep=0; 				   END        	  		    SET @SQLString =''SELECT     @Enddate as endDate,@StartDate as startDate,[TRANSACTION].GOID, [TRANSACTION].KOLID, T_KolHesab.KolNumber, T_KolHesab.NameHesab, SUM([TRANSACTION].PARBes) AS SumOfPARBes, SUM([TRANSACTION].DARBed)                        AS SumOfDARBed, SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes) AS mande, dbo.F1StartDate() AS MinOfDate1, dbo.F1EndDate() AS MaxOfDate1, CASE WHEN (CONVERT(money,                        CONVERT(money, SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed)                        - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes, CASE WHEN SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes)                        > 0 THEN SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes) END AS MandeBed, CASE WHEN SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes)                        < 0 THEN SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes) END AS MandeBes  FROM         [TRANSACTION] INNER JOIN                       T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN                       T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 WHERE     ([TRANSACTION].Date1 BETWEEN @StartDate AND @Enddate) ''               /*  --------------------شرط حساب معين  */ 			    set @ParmAndstr='' AND '' 			    if(NOT (@HesabID  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].HesabID = @HesabID )''; 				    set @ParmAndstr='' AND '' 			    END                  /* --------------------   شرط حساب تفصيلي */ 			     set @ParmAndstr='' AND '' 			     if(NOT (@THesabID  IS NULL)) 			     BEGIN 				     set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].THesabID = @THesabID )''; 				     set @ParmAndstr='' AND '' 			     END                    		/* --------------------     محدوده سند */ 			     set @ParmAndstr='' AND '' 			     if((NOT (@StartSanad  IS NULL)) and (NOT (@EndSanad  IS NULL)) ) 			     BEGIN 				     set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (T_SA2.SAID2Number BETWEEN @StartSanad AND @EndSanad) ''; 				     set @ParmAndstr='' AND '' 			     END                           	set @SQLString=@SQLString + @WhrStatement + N'' GROUP BY [TRANSACTION].GOID, [TRANSACTION].KOLID, T_KolHesab.KolNumber, T_KolHesab.NameHesab HAVING      ([TRANSACTION].KOLID IS NOT NULL) '';    /* --------------------شرط حساب سرگروه روي ساب*/ 			 	   if(NOT (@Goid IS NULL)) 			   BEGIN 				   set @SQLString=@SQLString +  N''  And ([TRANSACTION].GOID = @Goid ) ''; 				   END                    /* --------------------شرط حساب کل  */ 			    if(NOT (@KolID IS NULL)) 			    BEGIN 				    set @SQLString=@SQLString +  N'' And ([TRANSACTION].KolID = @KolID ) ''; 				 	    END             	/* --------------------  نام حساب  1 مرتب سازي */ 			     if ((@SortRep=1))     		     BEGIN      	 		if ((@NoeSortRep=1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY T_KolHesab.NameHesab DESC ''; 	 		END 		 		if ((@NoeSortRep<>1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY T_KolHesab.NameHesab  ''; 	 		END 	    		     END  	 	/* --------------------  کد حساب  2 مرتب سازي */ 			     if ((@SortRep=2))     		     BEGIN      	 		if ((@NoeSortRep=1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY T_KolHesab.KolNumber DESC ''; 	 		END 		 		if ((@NoeSortRep<>1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY T_KolHesab.KolNumber  ''; 	 		END 	    		     END       			          EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@NoeSortRep=@NoeSortRep,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@StartDate=@StartDate,@Enddate=@Enddate,@StartSanad=@StartSanad,@EndSanad=@EndSanad;  		     END 		      /* --------پايان چاپ*/  		 		  return END 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030210]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
   CREATE PROCEDURE [Rep_030210] (  	@SortRep nvarchar(255),  	@NoeSortRep nvarchar(255), 	@Goid int, 	@KolID int,  	@HesabID nvarchar(50), 	@THesabID int,  	@StartDate nvarchar(50)=''0'', 	@Enddate nvarchar(50)=''99999999'', 	@StartSanad nvarchar(50), 	@EndSanad nvarchar(50) )              AS        BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	   DECLARE @WhrStatement  NVARCHAR(2000);         	   DECLARE @ParmDefinition  NVARCHAR(2000);         	   DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   set @WhrStatement  = N'''';         	   set @ParmAndstr = N'''';                  	   SET @ParmDefinition = N''@SortRep nvarchar(255)=null,  							@NoeSortRep nvarchar(255)=null, 							@Goid int=null, 							@KolID int=null,  							@HesabID nvarchar(50)=null, 							@THesabID int=null, 							@StartDate nvarchar(50)=''''0'''', 							@Enddate nvarchar(50)=''''99999999'''', 							@StartSanad nvarchar(50)=null, 							@EndSanad nvarchar(50)=null'';            	   /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		    BEGIN       if(@StartDate IS NULL) 			   BEGIN 				   set @StartDate=''0''; 				   END         if(@Enddate IS NULL) 			   BEGIN 				   set @Enddate=''99999999''; 				   END         if(@NoeSortRep IS NULL) 			   BEGIN 				   set @NoeSortRep=0; 				   END        	  		    SET @SQLString =''SELECT     @Enddate as endDate,@StartDate as startDate , [TRANSACTION].GOID, [TRANSACTION].KOLID, [TRANSACTION].HESABID, SUM([TRANSACTION].PARBes) AS SumOfPARBes, SUM([TRANSACTION].DARBed) AS SumOfDARBed, CONVERT(money,                        SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes)) AS mande, MIN(dbo.F1StartDate()) AS MinOfDate1, MAX(dbo.F1EndDate()) AS MaxOfDate1, CASE WHEN (CONVERT(money,                        CONVERT(money, SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS bed, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed)                        - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS bes, dbo.HesabName([TRANSACTION].KOLID,                        [TRANSACTION].HESABID, NULL) AS namehesab, dbo.HesabNamecod( [TRANSACTION].KOLID, [TRANSACTION].HESABID, NULL) AS Hesabnumber FROM         [TRANSACTION] LEFT OUTER JOIN                       T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 WHERE     ([TRANSACTION].Date1 BETWEEN @StartDate AND @Enddate) ''                            /* --------------------   شرط حساب تفصيلي */ 			     set @ParmAndstr='' AND '' 			     if(NOT (@THesabID  IS NULL)) 			     BEGIN 				     set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].THesabID = @THesabID )''; 				     set @ParmAndstr='' AND '' 			     END                    		/* --------------------     محدوده سند */ 			     set @ParmAndstr='' AND '' 			     if((NOT (@StartSanad  IS NULL)) and (NOT (@EndSanad  IS NULL)) ) 			     BEGIN 				     set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (T_SA2.SAID2Number BETWEEN @StartSanad AND @EndSanad) ''; 				     set @ParmAndstr='' AND '' 			     END    	set @SQLString=@SQLString + @WhrStatement + N'' GROUP BY [TRANSACTION].GOID, [TRANSACTION].KOLID, [TRANSACTION].HESABID, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, NULL), dbo.HesabNameCod([TRANSACTION].KOLID, [TRANSACTION].HESABID, NULL) HAVING      (NOT ([TRANSACTION].HESABID IS NULL))'';    /* --------------------شرط حساب سرگروه روي ساب*/ 			 	   if(NOT (@Goid IS NULL)) 			   BEGIN 				   set @SQLString=@SQLString +  N''  And ([TRANSACTION].GOID = @Goid ) ''; 				   END                    /* --------------------شرط حساب کل  */ 			    if(NOT (@KolID IS NULL)) 			    BEGIN 				    set @SQLString=@SQLString +  N'' And ([TRANSACTION].KolID = @KolID ) ''; 				 	    END              /* --------------------شرط حساب معين  */ 			    if(NOT (@HesabID IS NULL)) 			    BEGIN 				    set @SQLString=@SQLString +  N'' And ([TRANSACTION].HesabID = @HesabID ) ''; 				    END        	/* --------------------  نام حساب  1 مرتب سازي */ 			     if ((@SortRep=''NameHesab''))     		     BEGIN      	 		if ((@NoeSortRep=1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY namehesab DESC ''; 	 		END 		 		if ((@NoeSortRep<>1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY namehesab  ''; 	 		END 	    		     END  	 	/* --------------------  کد حساب  2 مرتب سازي */ 			     if ((@SortRep=''hesabid''))     		     BEGIN      	 		if ((@NoeSortRep=1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY Hesabnumber DESC ''; 	 		END 		 		if ((@NoeSortRep<>1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY Hesabnumber  ''; 	 		END 	    		     END       			          EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@NoeSortRep=@NoeSortRep,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@StartDate=@StartDate,@Enddate=@Enddate,@StartSanad=@StartSanad,@EndSanad=@EndSanad;  		     END 		      /* --------پايان چاپ*/  		 		  return END 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05228521]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_05228521](@KalaGO int) AS SELECT     ID, NameGoKala1, @KalaGO AS KalaGO, NameGoKalanumbr FROM         dbo.T_NameGoKala WHERE     (@KalaGO IS NULL) OR                       (@KalaGO IS NOT NULL) AND (ID = @KalaGO)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05238835]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
         CREATE PROCEDURE [Rep_05238835](@GoKala int) AS 
         
         SELECT     @GoKala AS Expr1, ID, NameGoKala1, NameGoKalanumbr
FROM         T_NameGoKala
WHERE     (@GoKala IS NULL) OR
                      (@GoKala IS NOT NULL) AND (ID = @GoKala)   
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030107]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030107](@AccID nvarchar(50)) AS SELECT     THESABID, GOID, KolID, HESABID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KolID),                        4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), THESABID), 4), 1, LEN(@AccID)) AS HESABIDt,                        NameHesabT FROM         dbo.T_T_Heasb WHERE     (@AccID IS NULL) OR                       (NOT (@AccID IS NULL)) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KolID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), THESABID), 4), 1, LEN(@AccID)) = @AccID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030104]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030104](@AccID nvarchar(50)) AS SELECT     HESABID, NameHesab, GOID, KolID, @AccID AS Expr1, HESABNumber, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KolID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), HESABID), 4), 1, LEN(@AccID)) AS hesabidt FROM         dbo.T_Heasb WHERE     (@AccID IS NULL) OR                       (NOT (@AccID IS NULL)) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KolID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), HESABID), 4), 1, LEN(@AccID)) = @AccID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030103]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030103](@AccID nvarchar(50)) AS SELECT     KOLID, NameHesab, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1,                        LEN(@AccID)) AS hesabid, GOID FROM         dbo.T_KolHesab WHERE     (@AccID IS NULL) OR                       (NOT (@AccID IS NULL)) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1,                        LEN(@AccID)) = @AccID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030102]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030102] AS SELECT     GoID, NameGo FROM         dbo.T_GropHesab   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021157]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021157](@AccID nvarchar(50)) AS SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID, dbo.HesabName(dbo.T_Forosh.KOLID,                        dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID) AS namehesab, dbo.T_T_Heasb.Phone1, dbo.T_Forosh.KOLID, dbo.T_Forosh.THESABID,                        dbo.T_Forosh.HesabID, dbo.T_T_Heasb.Address, dbo.T_T_Heasb.CodEghtesdi, dbo.T_T_Heasb.Email FROM         dbo.T_KolHesab INNER JOIN                       dbo.T_Forosh ON dbo.T_KolHesab.KOLID = dbo.T_Forosh.KOLID LEFT OUTER JOIN                       dbo.T_T_Heasb ON dbo.T_Forosh.THESABID = dbo.T_T_Heasb.THESABID GROUP BY dbo.HesabName(dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)),                        dbo.T_T_Heasb.Phone1, dbo.T_Forosh.KOLID, dbo.T_Forosh.THESABID, dbo.T_Forosh.HesabID, dbo.T_T_Heasb.Address,                        dbo.T_T_Heasb.CodEghtesdi, dbo.T_T_Heasb.Email HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021156]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021156](@AccID nvarchar(50)) AS SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID, dbo.HesabName(dbo.T_Forosh.KOLID,                        dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID) AS namehesab, dbo.T_T_Heasb.Phone1, dbo.T_T_Heasb.Phone2, dbo.T_Forosh.KOLID,                        dbo.T_Forosh.THESABID, dbo.T_Forosh.HesabID FROM         dbo.T_KolHesab INNER JOIN                       dbo.T_Forosh ON dbo.T_KolHesab.KOLID = dbo.T_Forosh.KOLID LEFT OUTER JOIN                       dbo.T_T_Heasb ON dbo.T_Forosh.THESABID = dbo.T_T_Heasb.THESABID GROUP BY dbo.HesabName(dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)),                        dbo.T_T_Heasb.Phone1, dbo.T_T_Heasb.Phone2, dbo.T_Forosh.KOLID, dbo.T_Forosh.THESABID, dbo.T_Forosh.HesabID HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011661]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011661](@AccID nvarchar(50)) AS SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID, dbo.HesabName(dbo.T_Kharid.KOLID,                        dbo.T_Kharid.HesabID, dbo.T_Kharid.THESABID) AS namehesab, dbo.T_T_Heasb.Phone1, dbo.T_Kharid.KOLID, dbo.T_Kharid.THESABID,                        dbo.T_Kharid.HesabID, dbo.T_T_Heasb.Address, dbo.T_T_Heasb.Email FROM         dbo.T_Kharid INNER JOIN                       dbo.T_KolHesab ON dbo.T_Kharid.KOLID = dbo.T_KolHesab.KOLID LEFT OUTER JOIN                       dbo.T_T_Heasb ON dbo.T_Kharid.THESABID = dbo.T_T_Heasb.THESABID GROUP BY dbo.HesabName(dbo.T_Kharid.KOLID, dbo.T_Kharid.HesabID, dbo.T_Kharid.THESABID), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)),                        dbo.T_T_Heasb.Phone1, dbo.T_Kharid.KOLID, dbo.T_Kharid.THESABID, dbo.T_Kharid.HesabID, dbo.T_T_Heasb.Address,                        dbo.T_T_Heasb.Email HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011660]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011660](@AccID nvarchar(50)) AS SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID, dbo.HesabName(dbo.T_Kharid.KOLID,                        dbo.T_Kharid.HesabID, dbo.T_Kharid.THESABID) AS namehesab, dbo.T_T_Heasb.Phone1, dbo.T_T_Heasb.Phone2, dbo.T_Kharid.KOLID,                        dbo.T_Kharid.THESABID, dbo.T_Kharid.HesabID FROM         dbo.T_Kharid INNER JOIN                       dbo.T_KolHesab ON dbo.T_Kharid.KOLID = dbo.T_KolHesab.KOLID LEFT OUTER JOIN                       dbo.T_T_Heasb ON dbo.T_Kharid.THESABID = dbo.T_T_Heasb.THESABID GROUP BY dbo.HesabName(dbo.T_Kharid.KOLID, dbo.T_Kharid.HesabID, dbo.T_Kharid.THESABID), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)),                        dbo.T_T_Heasb.Phone1, dbo.T_T_Heasb.Phone2, dbo.T_Kharid.KOLID, dbo.T_Kharid.THESABID, dbo.T_Kharid.HesabID HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_032599]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
    /*  بروز رساني گزارش سود و زيان معين */ CREATE PROCEDURE [Rep_032599] (@startDate nvarchar(50), @enddate nvarchar(50)) AS          SELECT     [TRANSACTION].GOID, [TRANSACTION].KOLID, SUM([TRANSACTION].DARBed) AS SumOfDARBed, SUM([TRANSACTION].PARBes) AS SumOfPARBes, ABS(SUM([TRANSACTION].DARBed)                        - SUM([TRANSACTION].PARBes)) AS Expr1, T_KolHesab.FieldN1, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].HESABID, T_KolHesab.KOLID AS Expr2,                        MIN([TRANSACTION].Date1) AS MinOfDate1, MAX([TRANSACTION].Date1) AS MaxOfDate1, T_KolHesab.NameHesab, T_GropHesab.NameGo, T_Heasb.NameHesab AS NameHesabMo FROM         [TRANSACTION] INNER JOIN                       T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID INNER JOIN                       T_GropHesab ON T_KolHesab.GOID = T_GropHesab.GoID LEFT OUTER JOIN                       T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) GROUP BY [TRANSACTION].GOID, [TRANSACTION].KOLID, T_KolHesab.FieldN1, T_KolHesab.KOLID, [TRANSACTION].HESABID, T_KolHesab.NameHesab, T_GropHesab.NameGo, T_Heasb.NameHesab HAVING      ([TRANSACTION].GOID >= 8) AND (@startDate IS NULL) AND (SUM([TRANSACTION].DARBed) IS NOT NULL) AND (SUM([TRANSACTION].PARBes) IS NOT NULL) OR                       ([TRANSACTION].GOID >= 8) AND (@startDate IS NOT NULL) AND (SUM([TRANSACTION].DARBed) IS NOT NULL) AND (SUM([TRANSACTION].PARBes) IS NOT NULL)
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_032598]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
         CREATE PROCEDURE [Rep_032598](@startDate nvarchar(50), @enddate nvarchar(50)) AS           SELECT     [TRANSACTION].GOID, [TRANSACTION].KOLID, SUM([TRANSACTION].DARBed) AS SumOfDARBed, SUM([TRANSACTION].PARBes) AS SumOfPARBes, ABS(SUM([TRANSACTION].DARBed)                        - SUM([TRANSACTION].PARBes)) AS Expr1, T_KolHesab.FieldN1, [TRANSACTION].Date1, @startDate AS startDate, @enddate AS enddate, T_KolHesab.KOLID AS Expr3, MIN([TRANSACTION].Date1)                        AS MinOfDate1, MAX([TRANSACTION].Date1) AS MaxOfDate1, T_GropHesab.NameGo, T_KolHesab.NameHesab FROM         [TRANSACTION] INNER JOIN                       T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID INNER JOIN                       T_GropHesab ON T_KolHesab.GOID = T_GropHesab.GoID GROUP BY [TRANSACTION].GOID, [TRANSACTION].KOLID, T_KolHesab.FieldN1, [TRANSACTION].Date1, T_KolHesab.KOLID, T_GropHesab.NameGo, T_KolHesab.NameHesab HAVING      ([TRANSACTION].GOID >= 8) AND (@startDate IS NULL) AND (SUM([TRANSACTION].DARBed) IS NOT NULL) AND (SUM([TRANSACTION].PARBes) IS NOT NULL) OR                       ([TRANSACTION].GOID >= 8) AND (@startDate IS NOT NULL) AND (SUM([TRANSACTION].DARBed) IS NOT NULL) AND (SUM([TRANSACTION].PARBes) IS NOT NULL) AND                        ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030827]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030827](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endsanadID int) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed,                        SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt, @startDate AS startDate, @enddate AS enddate, CONVERT(money,                        SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes)) AS mande, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @AccID AS AccID, @startSanadID AS startSanadID, dbo.T_Heasb.HESABNumber,                        dbo.T_Heasb.NameHesab, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed)                        - SUM(PARBes)) ELSE 0 END AS bed, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed)                        - SUM(PARBes)) ELSE 0 END AS bes FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Heasb ON dbo.[TRANSACTION].HESABID = dbo.T_Heasb.HESABID LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4), 1, LEN(@AccID)), dbo.[TRANSACTION].HESABID, dbo.T_Heasb.HESABNumber,                        dbo.T_Heasb.NameHesab HAVING      (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) ORDER BY dbo.[TRANSACTION].HESABID  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030826]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030826](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endsanadID int) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4), 1, LEN(@AccID)) AS hesabidt, @startDate AS startDate, @enddate AS enddate, CONVERT(money,                        SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes)) AS mande, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @AccID AS AccID, @startSanadID AS startSanadID, dbo.T_KolHesab.KolNumber,                        dbo.T_KolHesab.NameHesab, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed)                        - SUM(PARBes)) ELSE 0 END AS bed, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed)                        - SUM(PARBes)) ELSE 0 END AS bes FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4), 1, LEN(@AccID)), dbo.T_KolHesab.KolNumber,                        dbo.T_KolHesab.NameHesab HAVING      (@AccID IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030825]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030825](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endsanadID int) AS SELECT     dbo.[TRANSACTION].GOID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID,                        @startDate AS startDate, @enddate AS enddate, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS mande,                        MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1, MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startSanadID AS startSanadID,                        dbo.T_GropHesab.GoNumber, dbo.T_GropHesab.NameGo, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes))))                        > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS bed, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes))))                        < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS bes FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) GROUP BY dbo.[TRANSACTION].GOID, dbo.T_GropHesab.GoNumber, dbo.T_GropHesab.NameGo HAVING      (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1,                        LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_03062424]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_03062424](@saID int) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID,                        dbo.[TRANSACTION].DARBed AS SumOfDARBed, dbo.[TRANSACTION].PARBes AS SumOfPARBes, dbo.[TRANSACTION].TextShSA,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab,                        dbo.[TRANSACTION].Date1, dbo.[TRANSACTION].SAID2Number, dbo.[TRANSACTION].Sharh, CASE WHEN [TRANSACTION].Sharh IS NULL                        THEN TextSHSa ELSE [TRANSACTION].Sharh END AS SharhSanad, dbo.T_SA2.Sharh AS Expr2 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (dbo.[TRANSACTION].SAID2Number = @saID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030624]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030624](@startDate nvarchar(50), @enddate nvarchar(50), @startSanadID nvarchar(50), @endSanadID nvarchar(50), @AccID nvarchar(50)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID AS Expr1, dbo.[TRANSACTION].THESABID,                        dbo.[TRANSACTION].DARBed AS SumOfDARBed, dbo.[TRANSACTION].PARBes AS SumOfPARBes, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) AS Hesabid, @startDate AS startDate, @enddate AS enddate, dbo.[TRANSACTION].TextShSA,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab,                        dbo.[TRANSACTION].Date1, dbo.[TRANSACTION].Date1 AS MinOfDate1, dbo.[TRANSACTION].Date1 AS MaxOfDate1, @AccID AS AccID,                        @startSanadID AS startSanadID, dbo.T_SA2.SAID2Number, dbo.[TRANSACTION].Sharh, CASE WHEN [TRANSACTION].Sharh IS NULL                        THEN TextSHSa ELSE [TRANSACTION].Sharh END AS SharhSanad, dbo.T_SA2.Sharh AS Expr2, dbo.[TRANSACTION].SAID2Number AS Expr3 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND (@startSanadID IS NULL)                        AND (@startDate IS NULL) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND (@startSanadID IS NULL)                        AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NULL) AND (@startDate IS NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030623]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030623](@startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID int, @AccID nvarchar(50)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID AS Expr1, dbo.[TRANSACTION].THESABID,                        dbo.[TRANSACTION].DARBed AS SumOfDARBed, dbo.[TRANSACTION].PARBes AS SumOfPARBes, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) AS Hesabid, @startDate AS startDate, @enddate AS enddate, dbo.[TRANSACTION].TextShSA,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab,                        dbo.[TRANSACTION].Date1, dbo.[TRANSACTION].Date1 AS MinOfDate1, dbo.[TRANSACTION].Date1 AS MaxOfDate1, @AccID AS AccID,                        @startSanadID AS startSanadID, dbo.T_SA2.SAID2Number, dbo.[TRANSACTION].Sharh, CASE WHEN [TRANSACTION].Sharh IS NULL                        THEN TextSHSa ELSE [TRANSACTION].Sharh END AS SharhSanad, dbo.T_SA2.Sharh AS Expr2 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND (@startSanadID IS NULL)                        AND (@startDate IS NULL) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND (@startSanadID IS NULL)                        AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NULL) AND (@startDate IS NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_012078]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_012078](@AccID nvarchar(50), @startDate nvarchar(50), @startSanadID nvarchar(50), @endSanadID nvarchar(50), @enddate nvarchar(50)) AS SELECT     dbo.[TRANSACTION].TextAIDKharid, dbo.[TRANSACTION].UnitPriceBarKh, dbo.[TRANSACTION].TedadBarKh, ABS(dbo.[TRANSACTION].FieldN3)                        AS Expr1, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.T_Dar8.DateKh,                        @startDate AS startDate, dbo.[TRANSACTION].DARID8, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID,                        dbo.[TRANSACTION].KALAID, @enddate AS enddate, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID INNER JOIN                       dbo.T_Dar8 ON dbo.[TRANSACTION].DARID8 = dbo.T_Dar8.DARID8 WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].TextAIDKharid, dbo.[TRANSACTION].UnitPriceBarKh, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].KALAID, ABS(dbo.[TRANSACTION].FieldN3),                        dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1, dbo.[TRANSACTION].DARID8,                        dbo.[TRANSACTION].TedadBarKh, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)), dbo.HesabName(dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID), dbo.T_Dar8.DateKh HAVING      (@startDate IS NULL) AND (@AccID IS NULL) OR                       (@startDate IS NULL) AND (@AccID IS NULL) AND (dbo.[TRANSACTION].DARID8 BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NULL) AND (dbo.T_Dar8.DateKh BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@AccID IS NULL) AND (dbo.[TRANSACTION].DARID8 BETWEEN @startSanadID AND @endSanadID) AND                        (dbo.T_Dar8.DateKh BETWEEN @startDate AND @enddate) OR                       (@startDate IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NULL) AND (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARID8 BETWEEN @startSanadID AND @endSanadID) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.T_Dar8.DateKh BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARID8 BETWEEN @startSanadID AND @endSanadID) AND                        (dbo.T_Dar8.DateKh BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_012077]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_012077](@AccID nvarchar(50), @startDate nvarchar(50), @startSanadID nvarchar(50), @endSanadID nvarchar(50), @enddate nvarchar(50)) AS SELECT     dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].UnitPriceKh, dbo.[TRANSACTION].TedadVorood, ABS(dbo.[TRANSACTION].FieldN3) AS Expr1,                        dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.T_VorodKala.DateV,                        @startDate AS startDate, dbo.[TRANSACTION].VORODID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID,                        dbo.[TRANSACTION].KALAID, @enddate AS enddate, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID INNER JOIN                       dbo.T_VorodKala ON dbo.[TRANSACTION].VORODID = dbo.T_VorodKala.VORODID WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].UnitPriceKh, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].KALAID, ABS(dbo.[TRANSACTION].FieldN3),                        dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1, dbo.[TRANSACTION].VORODID,                        dbo.[TRANSACTION].TedadVorood, dbo.T_VorodKala.DateV, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)), dbo.HesabName(dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) HAVING      (@startDate IS NULL) AND (@AccID IS NULL) OR                       (@startDate IS NULL) AND (@AccID IS NULL) AND (dbo.[TRANSACTION].VORODID BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NULL) AND (dbo.T_VorodKala.DateV BETWEEN @startdate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@AccID IS NULL) AND (dbo.[TRANSACTION].VORODID BETWEEN @startSanadID AND @endSanadID) AND                        (dbo.T_VorodKala.DateV BETWEEN @startdate AND @enddate) OR                       (@startDate IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NULL) AND (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].VORODID BETWEEN @startSanadID AND @endSanadID) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.T_VorodKala.DateV BETWEEN @startdate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].VORODID BETWEEN @startSanadID AND @endSanadID) AND                        (dbo.T_VorodKala.DateV BETWEEN @startdate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID),                        4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID),                        4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_012076]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_012076](@AccID nvarchar(50), @startDate nvarchar(50), @startSanadID nvarchar(50), @endSanadID nvarchar(50), @enddate nvarchar(50)) AS SELECT     dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].UnitPriceKh, dbo.[TRANSACTION].TedadKh, dbo.T_Kharid.Takhfif, dbo.[TRANSACTION].GOID,                        dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].KHARIDID, dbo.T_Kharid.DateKh,                        @startDate AS startDate, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_Kharid.THESABID), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID, dbo.[TRANSACTION].KALAID, dbo.[TRANSACTION].VKALAID,                        dbo.[TRANSACTION].FieldN6, @enddate AS enddate, dbo.HesabName(dbo.T_Kharid.KOLID, dbo.[TRANSACTION].HESABID, dbo.T_Kharid.THESABID)                        AS namehesab, ABS(dbo.[TRANSACTION].FieldN3) AS Expr1, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1,                        dbo.T_NameKala.VAHED1 FROM         dbo.T_Kharid INNER JOIN                       dbo.[TRANSACTION] ON dbo.T_Kharid.KHARIDID = dbo.[TRANSACTION].KHARIDID INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].UnitPriceKh, dbo.[TRANSACTION].TedadKh, dbo.T_Kharid.Takhfif, dbo.[TRANSACTION].GOID,                        dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].KHARIDID, dbo.T_Kharid.DateKh,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.THESABID), 4),                        1, LEN(@AccID)), dbo.[TRANSACTION].KALAID, dbo.[TRANSACTION].VKALAID, dbo.[TRANSACTION].FieldN6, dbo.HesabName(dbo.T_Kharid.KOLID,                        dbo.[TRANSACTION].HESABID, dbo.T_Kharid.THESABID), ABS(dbo.[TRANSACTION].FieldN3), dbo.T_NameKala.KalaNumber,                        dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1 HAVING      (@startDate IS NULL) AND (@AccID IS NULL) OR                       (@startDate IS NULL) AND (@AccID IS NULL) AND (dbo.[TRANSACTION].KHARIDID = @startSanadID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NULL) AND (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@AccID IS NULL) AND (dbo.[TRANSACTION].KHARIDID = @startSanadID) AND (dbo.T_Kharid.DateKh BETWEEN                        @startDate AND @enddate) OR                       (@startDate IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.THESABID), 4), 1, LEN(@AccID)) BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NULL) AND (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].KHARIDID = @startSanadID) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.THESABID), 4),                        1, LEN(@AccID)) BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.THESABID), 4),                        1, LEN(@AccID)) BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].KHARIDID = @startSanadID) AND (dbo.T_Kharid.DateKh BETWEEN                        @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.THESABID), 4), 1, LEN(@AccID)) BETWEEN @startSanadID AND @endSanadID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021342]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021342](@startSanadID int, @endSanadID int, @startDate nvarchar(255), @enddate nvarchar(255), @AccID nvarchar(50)) AS SELECT     dbo.[TRANSACTION].PARID7, dbo.[TRANSACTION].TedadBarFo, dbo.[TRANSACTION].UnitPriceBarFo, CONVERT(money, SUM(CONVERT(money,                        ABS(dbo.[TRANSACTION].FieldN3)))) AS abs, dbo.[TRANSACTION].KALAID, dbo.T_Par7.DateFo, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) AS hesabidt,                        @startDate AS startDate, @startSanadID AS startSanadID, @AccID AS AccID, dbo.[TRANSACTION].TextAIDFo, dbo.T_Par7.Takhfif,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab,                        dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Par7 ON dbo.[TRANSACTION].PARID7 = dbo.T_Par7.PARID7 INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].PARID7, dbo.[TRANSACTION].TedadBarFo, dbo.[TRANSACTION].UnitPriceBarFo, dbo.T_Par7.DateFo,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)), dbo.[TRANSACTION].KALAID, dbo.[TRANSACTION].TextAIDFo, dbo.T_Par7.Takhfif,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID), dbo.T_NameKala.KalaNumber,                        dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1 HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.T_Par7.DateFo BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND (dbo.T_Par7.DateFo BETWEEN                        @startDate AND @enddate) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].PARID7 BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.[TRANSACTION].PARID7 BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NULL) AND (dbo.T_Par7.DateFo BETWEEN @startDate AND @enddate) AND (dbo.[TRANSACTION].PARID7 BETWEEN @startSanadID AND                        @endSanadID) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND (dbo.T_Par7.DateFo BETWEEN                        @startDate AND @enddate) AND (dbo.[TRANSACTION].PARID7 BETWEEN @startSanadID AND @endSanadID) ORDER BY dbo.[TRANSACTION].PARID7  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021341]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021341](@startSanadID int, @endSanadID int, @startDate nvarchar(255), @enddate nvarchar(255), @AccID nvarchar(50)) AS SELECT     dbo.[TRANSACTION].KHOROJID, CONVERT(money, SUM(CONVERT(money, ABS(dbo.[TRANSACTION].FieldN3)))) AS abs,                        dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].UnitPriceFo, dbo.[TRANSACTION].TedadKhorooj, dbo.T_KhorojKala.DAteKh,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) AS hesabidt, @startDate AS startDate, @startSanadID AS startSanadID, @AccID AS AccID,                        dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1, dbo.HesabName(dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KhorojKala ON dbo.[TRANSACTION].KHOROJID = dbo.T_KhorojKala.KHOROJID INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].KHOROJID, dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].UnitPriceFo, dbo.[TRANSACTION].TedadKhorooj,                        dbo.T_KhorojKala.DAteKh, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)), dbo.T_NameKala.KalaNumber,                        dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1, dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID,                        dbo.[TRANSACTION].THESABID) HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.T_KhorojKala.DAteKh BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND (dbo.T_KhorojKala.DAteKh BETWEEN                        @startDate AND @enddate) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].KHOROJID BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.[TRANSACTION].KHOROJID BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NULL) AND (dbo.T_KhorojKala.DAteKh BETWEEN @startDate AND @enddate) AND (dbo.[TRANSACTION].KHOROJID BETWEEN                        @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND (dbo.T_KhorojKala.DAteKh BETWEEN                        @startDate AND @enddate) AND (dbo.[TRANSACTION].KHOROJID BETWEEN @startSanadID AND @endSanadID) ORDER BY dbo.[TRANSACTION].KHOROJID  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021340]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021340](@startSanadID int, @endSanadID int, @startDate nvarchar(255), @enddate nvarchar(255), @AccID nvarchar(50)) AS SELECT     CONVERT(int, dbo.T_Forosh.ForoshNumbr) AS FOROSHID, dbo.[TRANSACTION].TextAIDFo, dbo.[TRANSACTION].UnitPriceFo,                        dbo.[TRANSACTION].TedadFo, dbo.T_Forosh.Takhfif, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID,                        dbo.[TRANSACTION].THESABID, dbo.T_Forosh.DateFo, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, dbo.T_Forosh.DateFo AS MinOfDateFo, dbo.T_Forosh.DateFo AS MaxOfDateFo, @startDate AS startDate,                        @startSanadID AS startSanadID, @AccID AS AccID, dbo.[TRANSACTION].KALAID, dbo.[TRANSACTION].FieldN3 AS abs,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab,                        dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_Forosh.FOROSHID AS Expr1, F_MabHazine_Foroosh.MabHazine FROM         dbo.T_Forosh INNER JOIN                       dbo.[TRANSACTION] ON dbo.T_Forosh.FOROSHID = dbo.[TRANSACTION].FOROSHID INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID LEFT OUTER JOIN                       dbo.F_MabHazine_Foroosh() F_MabHazine_Foroosh ON dbo.T_Forosh.FOROSHID = F_MabHazine_Foroosh.FactID WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND                        @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (CONVERT(int, dbo.T_Forosh.ForoshNumbr) BETWEEN                        @startSanadID AND @endSanadID) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (CONVERT(int, dbo.T_Forosh.ForoshNumbr) BETWEEN                        @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND                        @enddate) AND (CONVERT(int, dbo.T_Forosh.ForoshNumbr) BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND                        @enddate) AND (CONVERT(int, dbo.T_Forosh.ForoshNumbr) BETWEEN @startSanadID AND @endSanadID) ORDER BY CONVERT(int, dbo.T_Forosh.ForoshNumbr)  ' 
END
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030318]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030318](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID int) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes,                        SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1, MAX(dbo.[TRANSACTION].Date1)                        AS MaxOfDate1, COUNT(dbo.[TRANSACTION].PARBes) AS CountOfPARBes, COUNT(dbo.[TRANSACTION].DARBed) AS CountOfDARBed,                        @enddate AS enddate, @startDate AS startdate, dbo.T_Heasb.NameHesab AS namehesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Heasb ON dbo.[TRANSACTION].HESABID = dbo.T_Heasb.HESABID WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].HESABID), 4), 1, LEN(@AccID)), dbo.T_Heasb.NameHesab HAVING      (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID),                        4), 1, LEN(@AccID)) = @AccID) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].HESABID IS NOT NULL)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030317]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030317](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID int) AS SELECT     dbo.[TRANSACTION].GOID, SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed,                        MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1, MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, COUNT(dbo.[TRANSACTION].PARBes)                        AS CountOfPARBes, @enddate AS enddate, @startDate AS startdate, dbo.T_GropHesab.NameGo AS namehesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) GROUP BY dbo.[TRANSACTION].GOID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)),                        dbo.T_GropHesab.NameGo HAVING      (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].GOID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].GOID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].GOID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].GOID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) = @AccID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030316]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030316](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID int) AS SELECT     GOID, KOLID, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1,                        COUNT(PARBes) AS CountOfPARBes, @enddate AS enddate, @startDate AS startdate, dbo.HesabName(KOLID, NULL, NULL) AS namehesab FROM         dbo.[TRANSACTION] WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (NOT (@startSanadID IS NULL)) AND (SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startSanadID AND                        @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startSanadID AND                        @endSanadID) GROUP BY GOID, KOLID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID)),                        dbo.HesabName(KOLID, NULL, NULL) HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND (KOLID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (NOT (@startDate IS NULL)) AND                        (KOLID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND (KOLID IS NOT NULL)                        AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID))                        = @AccID) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (NOT (@startDate IS NULL)) AND                        (KOLID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND (KOLID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (NOT (@startDate IS NULL)) AND                        (KOLID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND (KOLID IS NOT NULL)                        AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID))                        = @AccID) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (NOT (@startDate IS NULL)) AND                        (KOLID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1,                        LEN(@AccID)) = @AccID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030315]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030315](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID int) AS SELECT     GOID, KOLID, HESABID, THESABID, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, MIN(Date1) AS MinOfDate1, MAX(Date1)                        AS MaxOfDate1, COUNT(PARBes) AS CountOfPARBes, @enddate AS enddate, @startDate AS startdate, dbo.HesabName(KOLID, HESABID, THESABID)                        AS namehesab FROM         dbo.[TRANSACTION] WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@startSanadID IS NOT NULL) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startSanadID AND                        @endSanadID) OR                       (@startSanadID IS NOT NULL) AND (SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@startSanadID IS NOT NULL) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startSanadID AND @endSanadID) GROUP BY GOID, KOLID, HESABID, THESABID, dbo.HesabName(KOLID, HESABID, THESABID) HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND (dbo.HesabName(KOLID,                        HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NOT NULL) AND                        (dbo.HesabName(KOLID, HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.HesabName(KOLID, HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.HesabName(KOLID, HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND (dbo.HesabName(KOLID,                        HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NOT NULL) AND                        (dbo.HesabName(KOLID, HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.HesabName(KOLID, HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.HesabName(KOLID, HESABID, THESABID) IS NOT NULL)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_03029752]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_03029752](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed,                        SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1,                        MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1, MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate,                        CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed,                        CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes,                        dbo.T_KolHesab.NameHesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID INNER JOIN                       dbo.T_Heasb ON dbo.[TRANSACTION].HESABID = dbo.T_Heasb.HESABID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.T_KolHesab.NameHesab HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_03029751]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_03029751](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes, dbo.T_KolHesab.NameHesab,                        dbo.T_GropHesab.NameGo FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab, dbo.T_GropHesab.NameGo HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030296]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030296](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes, dbo.T_KolHesab.NameHesab,                        dbo.T_GropHesab.NameGo FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab, dbo.T_GropHesab.NameGo HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030295]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030295](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes, dbo.T_KolHesab.NameHesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030294]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030294](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, dbo.T_KolHesab.NameHesab,                        dbo.T_GropHesab.NameGo FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab, dbo.T_GropHesab.NameGo HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030293]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030293](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, dbo.T_KolHesab.NameHesab,                        dbo.T_KolHesab.KolNumber FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab, dbo.T_KolHesab.KolNumber HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030259]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030259](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endsanadID int) AS SELECT     dbo.[TRANSACTION].GOID, SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) AS hesabidt, SUM(dbo.[TRANSACTION].DARBed)                        - SUM(dbo.[TRANSACTION].PARBes) AS mande, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1, MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1,                        @startDate AS startDate, @enddate AS enddate, @AccID AS AccID, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed)                        - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS bed, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed)                        - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS bes FROM         dbo.[TRANSACTION] LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) GROUP BY dbo.[TRANSACTION].GOID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) HAVING      (@AccID IS NULL) AND (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].GOID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) = @AccID) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL)   ' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0305200]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_0305200](@startDate nvarchar(50), @enddate nvarchar(50), @AccID nvarchar(50)) AS SELECT     TOP 100 PERCENT GOID, KOLID, HESABID, THESABID, SUM(SumOfPARBes) AS SumOfSumOfPARBes, SUM(SumOfDARBed) AS SumOfSumOfDARBed,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, SUM(mande) AS SumOfmande, MIN(MinOfDate1) AS MinOfDate1, MAX(MaxOfDate1) AS MaxOfDate1,                        dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab, @startdate AS startdate, @enddate AS enddate FROM         dbo.Q_Bed_Bes(@startdate, @enddate) Q_Bed_Bes GROUP BY GOID, KOLID, HESABID, THESABID, dbo.HesabName(KOLID, HESABID, THESABID), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) HAVING      (SUM(mande) > 0) AND (@AccID IS NULL) OR                       (SUM(mande) > 0) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021451]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021451](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) > 0) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) > 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021450]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021450](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) > 0) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) > 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021448]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021448](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) < 0) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) < 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021447]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021447](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) < 0) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) < 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021446]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021446](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed) - SUM(PARBes) AS Mande, @enddate AS enddate,                        @startDate AS startdate, RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        @AccID AS Expr1 FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh WHERE     (@startDate IS NULL) OR                       (@startDate IS NULL) OR                       (Date1 BETWEEN @startDate AND @enddate) AND (@startDate IS NOT NULL) OR                       (Date1 BETWEEN @startDate AND @enddate) AND (@startDate IS NOT NULL) GROUP BY RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID) HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021091]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021091](@AccID nvarchar(50), @startDate nvarchar(100), @enddate nvarchar(100), @startSanadID int, @endSanadID int) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID, @startDate AS startDate, @enddate AS enddate, @endSanadID AS endSanadID,                        RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID,                        THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL)                        AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021058]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021058](@AccID nvarchar(50), @startDate nvarchar(100), @enddate nvarchar(100), @startSanadID int, @endSanadID int) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID, @startDate AS startDate, @enddate AS enddate, @endSanadID AS endSanadID,                        RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID,                        THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL)                        AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021014]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Rep_021014](@AccID nvarchar(50), @startDate nvarchar(100), @enddate nvarchar(100), @startSanadID int, @endSanadID int) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID,                        @startDate AS startDate, @enddate AS enddate, @endSanadID AS endSanadID, RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID, THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (Date1 BETWEEN                        @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND (Date1 BETWEEN @startDate AND                        @enddate) OR                       (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND (SAID2Number BETWEEN                        @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND (SAID2Number BETWEEN                        @startDate AND @enddate) AND (Date1 BETWEEN @startDate AND @enddate)' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011975]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011975](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NULL) AND (SUM(DARBed - PARBes) < 0) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(DARBed - PARBes) < 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011974]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011974](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NULL) AND (SUM(DARBed - PARBes) < 0) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(DARBed - PARBes) < 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011972]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011972](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NULL) AND (SUM(DARBed - PARBes) > 0) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(DARBed - PARBes) > 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011971]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011971](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NULL) AND (SUM(DARBed - PARBes) > 0) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (SUM(DARBed - PARBes) > 0) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011970]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011970](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed) - SUM(PARBes) AS mande, @enddate AS enddate,                        @startDate AS startdate, RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        @AccID AS Expr1 FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid WHERE     (@startDate IS NULL) OR                       (@startDate IS NULL) OR                       (Date1 BETWEEN @startDate AND @enddate) AND (@startDate IS NOT NULL) OR                       (Date1 BETWEEN @startDate AND @enddate) AND (@startDate IS NOT NULL) GROUP BY RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID) HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NULL) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NULL) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011764]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011764](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID nvarchar(50)) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID, @startDate AS startdate, @enddate AS enddate, @endSanadID AS endSanadID,                        RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID,                        THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid WHERE     (@AccID IS NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (@startSanadID IS NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (@startSanadID IS NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (@startSanadID IS NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND                        (SAID2Number BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL))                        AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011763]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011763](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID nvarchar(50)) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID, @startDate AS startdate, @enddate AS enddate, @endSanadID AS endSanadID,                        RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID,                        THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid WHERE     (@AccID IS NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (@startSanadID IS NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (@startSanadID IS NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (@startSanadID IS NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND                        (SAID2Number BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL))                        AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011762]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011762](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID nvarchar(50)) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID, @startDate AS startdate, @enddate AS enddate, @endSanadID AS endSanadID,                        RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID,                        THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid WHERE     (@AccID IS NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NULL) AND (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL)                        AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate)  ' 
END
GO





/****** Object:  StoredProcedure [dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_Minimum]    Script Date: 11/16/2017 17:57:50 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_Minimum]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_Minimum]
GO


/****** Object:  StoredProcedure [dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_Minimum]    Script Date: 11/16/2017 17:57:50 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  CREATE PROCEDURE [dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_Minimum]            
  	

  	
  	AS     
  	
  	
SELECT     SubTransactionKala_1.KALAID, SubTransactionKala_1.AID, SubTransactionKala_1.GOKID, SUM(SubTransactionKala_1.Tedad) AS SumTedad, MAX(ISNULL(FunInfoKalaAll_1.MinMogoody, 0)) 
                      AS MinMogoody, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) AS Mande, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.KalaNumber, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.BarCode
FROM         dbo.SubTransactionKala() AS SubTransactionKala_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON SubTransactionKala_1.KALAID = FunInfoKalaAll_1.ID AND SubTransactionKala_1.AID = FunInfoKalaAll_1.AID
GROUP BY SubTransactionKala_1.KALAID, SubTransactionKala_1.AID, SubTransactionKala_1.GOKID, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameVahed1, 
                      FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.BarCode
HAVING      (MAX(ISNULL(FunInfoKalaAll_1.MinMogoody, 0)) - SUM(SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre) > 0)
ORDER BY FunInfoKalaAll_1.NameKala1             


GO






DECLARE @KOLID INT;

SELECT     @KOLID=COUNT(*) 
FROM         T_KolHesab
WHERE     (KOLID = 26)

IF (@KOLID=0)
BEGIN
	SET IDENTITY_INSERT [dbo].[T_KolHesab] ON
	INSERT [dbo].[T_KolHesab] ([KOLID], [KolNumber], [DateCreate], [DateModified], [NameHesab], [GOID], [FieldN1], [FieldN2], [FieldN3], [FieldN4], [FieldT1], [FieldT2], [FieldT3], [FieldT4], [Flag1], [Flag2], [Flag3], [Flag4], [Flag5], [Flag6], [Flag7], [Flag8], [Balance]) VALUES (26, N'26', CAST(0xA42B0419 AS SmallDateTime), CAST(0xA42B0419 AS SmallDateTime), N'تخفیفات خدمات', 8, CAST(0.0000 AS Decimal(19, 4)), CAST(0.0000 AS Decimal(19, 4)), CAST(0.0000 AS Decimal(19, 4)), CAST(0.0000 AS Decimal(19, 4)), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0.0000)
	SET IDENTITY_INSERT [dbo].[T_KolHesab] OFF
end






/****** Object:  StoredProcedure [dbo].[SELECTGOKID]    Script Date: 09/08/2017 22:45:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SELECTGOKID]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SELECTGOKID]
GO



/****** Object:  StoredProcedure [dbo].[SELECTGOKID]    Script Date: 09/08/2017 22:45:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  cREATE PROCEDURE [dbo].[SELECTGOKID] ( 
@GOKID int)           
  AS     
  BEGIN
  	 	
SELECT     ID, NameGoKalanumbr, DateCreate, DateModified, NameGoKala1, note, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4
FROM         T_NameGoKala
WHERE     (ID = @GOKID)

 END 



GO




/****** Object:  StoredProcedure [dbo].[DEFULT_SELECTAID]    Script Date: 09/08/2017 22:45:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DEFULT_SELECTAID]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[DEFULT_SELECTAID]
GO



/****** Object:  StoredProcedure [dbo].[DEFULT_SELECTAID]    Script Date: 09/08/2017 22:45:33 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  cREATE PROCEDURE [dbo].[DEFULT_SELECTAID] ( 
@AID int)           
  AS     
  BEGIN
  	 	
SELECT     AID, AIDNumber, DateCreate, DateModified, kalaid, NameAnbar, Phone, Address, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4
FROM         T_Anbar
WHERE     (AID = @AID)

 END 



GO





IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_ALL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CQ_ListKala_ALL]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_ALL_GroupFilter]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CQ_ListKala_ALL_GroupFilter]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_ALL_GroupFilter_Rep]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CQ_ListKala_ALL_GroupFilter_Rep]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_ALL_Rep]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CQ_ListKala_ALL_Rep]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_JabejaeiAnbar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CQ_ListKala_JabejaeiAnbar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_JabejaeiAnbar_GroupFilter]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CQ_ListKala_JabejaeiAnbar_GroupFilter]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_Sandoogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CQ_ListKala_Sandoogh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_Sandoogh_GroupFilter]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CQ_ListKala_Sandoogh_GroupFilter]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_Sandoogh_Name_BarForosooh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListKala_Sandoogh_Name_BarForosooh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_Sandoogh_Name_BarKharid]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListKala_Sandoogh_Name_BarKharid]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_Sandoogh_Name_Kharid]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListKala_Sandoogh_Name_Kharid]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_SoodKol]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_SoodKol]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Std_0050023008900390022]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Std_0050023008900390022]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Std_05002300880033]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Std_05002300880033]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Std_05002300880034]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Std_05002300880034]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900390021]') AND type in (N'P', N'PC'))
DROP PROCEDURE [std_050023008900390021]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_sood_Sub1]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Rep_sood_Sub1]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_Test]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_Test]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_MojoodiKharid]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_MojoodiKharid]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011762]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011762]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011763]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011763]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011764]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011764]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011970]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011970]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011971]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011971]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011972]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011972]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011974]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011974]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011975]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011975]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021014]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021014]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021058]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021058]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021091]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021091]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021446]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021446]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021447]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021447]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021448]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021448]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021450]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021450]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021451]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021451]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0305200]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0305200]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_Sandoogh_Name]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListKala_Sandoogh_Name]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Fun_Sood_Zian_Kala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Fun_Sood_Zian_Kala]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[PHiMianginKharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [PHiMianginKharid]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[stored_Rep_Kol1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [stored_Rep_Kol1]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[stored_Rep_Mo1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [stored_Rep_Mo1]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[stored_Rep_TH1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [stored_Rep_TH1]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure1]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900380019]') AND type in (N'P', N'PC'))
DROP PROCEDURE [std_050023008900380019]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure15]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure15]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure2]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure3]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure4]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure4]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[SubTransactionKala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [SubTransactionKala]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[AghlamAsnadMandeDar]'))
DROP VIEW [AghlamAsnadMandeDar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AnalyzKharidForosh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [AnalyzKharidForosh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AsnadMandeDar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [AsnadMandeDar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Check_MojoodiKalaInFactor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Check_MojoodiKalaInFactor]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DataRep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [DataRep1]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CUSTOM_REP_FO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CUSTOM_REP_FO]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CUSTOM_REP_KH]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CUSTOM_REP_KH]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MabFact_EditSanad]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MabFact_EditSanad]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[MandeTaf]') AND type in (N'P', N'PC'))
DROP PROCEDURE [MandeTaf]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[PFrm_ListKala_MoshabehMojoodi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [PFrm_ListKala_MoshabehMojoodi]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Mudp_Rep_030293]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Mudp_Rep_030293]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Mudp_Rep_030294]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Mudp_Rep_030294]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Mudp_Rep_030295]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Mudp_Rep_030295]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Mudp_Rep_030296]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Mudp_Rep_030296]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Mudp_Rep_03029751]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Mudp_Rep_03029751]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Mudp_Rep_03029752]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Mudp_Rep_03029752]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditKol_Tar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditKol_Tar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_Tar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditMoin_Tar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MandeGhabl_AllHesab]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MandeGhabl_AllHesab]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MandeGhabl_Goid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MandeGhabl_Goid]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MandeGhabl_Hesab]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MandeGhabl_Hesab]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MandeGhabl_Tafsili]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MandeGhabl_Tafsili]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_ManeGhabl_Kol]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_ManeGhabl_Kol]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MojoodiKala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MojoodiKala]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MojoodiKala_Sandogh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MojoodiKala_Sandogh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Enteghal_Mande_KOlHesab]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Enteghal_Mande_KOlHesab]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Enteghal_Mande_KOlHesabBES]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Enteghal_Mande_KOlHesabBES]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Enteghal_Mande_MOInHesabBED]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Enteghal_Mande_MOInHesabBED]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Enteghal_Mande_MOInHesabBES]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Enteghal_Mande_MOInHesabBES]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Enteghal_Mande_TAFHesabBED]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Enteghal_Mande_TAFHesabBED]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Enteghal_Mande_TAFHesabBES]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Enteghal_Mande_TAFHesabBES]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[FunSub_05239042]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [FunSub_05239042]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[FunSub_05239043]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [FunSub_05239043]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[FunSub_05239044]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [FunSub_05239044]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[FunSub_05239045]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [FunSub_05239045]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[FunSubKardexKala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [FunSubKardexKala]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Fun_01186606]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Fun_01186606]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListMoinHesab_SanadHesabdariNotFiltered]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListMoinHesab_SanadHesabdariNotFiltered]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListMoinHesab1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListMoinHesab1]
GO


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QRep_khadamatG]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QRep_khadamatG]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListKolHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListKolHesab]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListMoinHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListMoinHesab]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListKolHesab_SanadHesabdariNotFiltered]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListKolHesab_SanadHesabdariNotFiltered]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListKolHesabNotLimited]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListKolHesabNotLimited]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListTafsiliHesab_SanadHesabdariNotFiltered]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListTafsiliHesab_SanadHesabdariNotFiltered]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListTafsiliHesab1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListTafsiliHesab1]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListTafsiliHesabNotLimited]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListTafsiliHesabNotLimited]
GO


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_Foroosh_Sandogh_CALC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_Foroosh_Sandogh_CALC]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_Foroosh_Sandoghcc]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_Foroosh_Sandoghcc]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListTafsiliHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListTafsiliHesab]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSub_MSG_PReviewListCHeckForRunProgramPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSub_MSG_PReviewListCHeckForRunProgramPar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqBatelShodePar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListCheqBatelShodePar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqNaghdiPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListCheqNaghdiPar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqNoPassPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListCheqNoPassPar]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqPassPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListCheqPassPar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_SanadHesabdari_ChekSaghf_Bed_Bes]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_SanadHesabdari_ChekSaghf_Bed_Bes]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListEditGroup_Append]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListEditGroup_Append]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_MandeHesabGo]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_MandeHesabGo]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_MandeHesabInFactor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_MandeHesabInFactor]
GO



/****** Object:  StoredProcedure [dbo].[Q_MandeHesabMoin]    Script Date: 11/21/2017 17:52:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_MandeHesabMoin]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_MandeHesabMoin]
GO


/****** Object:  StoredProcedure [dbo].[Q_MandeHesabMoin]    Script Date: 11/21/2017 17:52:56 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[Q_MandeHesabMoin](
			@startDate nvarchar(50),
			@enddate nvarchar(50),
			@startSanadID int,
			@endSanadID int,
			@GOID INT,
			@KOLID INT,
			@HESABID INT,
			@THESABID INT,
			@THESABIDSath2 INT ) 
			
AS 
        
BEGIN 
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';   
  	
  	SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=null'; 
	
	
	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   
set @SQLString=N'       SELECT     HESABID, CONVERT(money, SUM(DARBed) - SUM(PARBes)) AS Mande, SUM(PARBes) AS sumbes, SUM(DARBed) AS sumbed
FROM         [TRANSACTION]
WHERE     (SAID2 IS NOT NULL) AND (Date1 < @startDate)
 '

  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
      /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number < @startSanadID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
   
   
   set @SQLString= @SQLString + @WhrStatement  + ' GROUP BY HESABID
HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL)  '



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END 
GO




/****** Object:  StoredProcedure [dbo].[QMandeAllHesab]    Script Date: 11/21/2017 17:52:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QMandeAllHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QMandeAllHesab]
GO



/****** Object:  StoredProcedure [dbo].[QMandeAllHesab]    Script Date: 11/21/2017 17:52:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[QMandeAllHesab](
			@startDate nvarchar(50),
			@enddate nvarchar(50),
			@startSanadID int,
			@endSanadID int,
			@GOID INT,
			@KOLID INT,
			@HESABID INT,
			@THESABID INT,
			@THESABIDSath2 INT ) 
			
AS 
        
BEGIN 
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';   
  	
  	SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=null'; 
	
	
	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   
set @SQLString=N'        
       SELECT     THESABID AS CodeHesab, SUM(DARBed) - SUM(PARBes) AS Mande
FROM         [TRANSACTION]
WHERE     (Date1 < @startDate)
  '
                      
        

  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
      /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   
   
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number < @startSanadID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
   
   
   set @SQLString= @SQLString + @WhrStatement  + ' GROUP BY THESABID '



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END 
GO



/****** Object:  StoredProcedure [dbo].[Q_MandeHesabKol]    Script Date: 11/21/2017 17:52:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_MandeHesabKol]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_MandeHesabKol]
GO


/****** Object:  StoredProcedure [dbo].[Q_MandeHesabKol]    Script Date: 11/21/2017 17:52:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[Q_MandeHesabKol](  
			@startDate nvarchar(50),
			@enddate nvarchar(50),
			@startSanadID int,
			@endSanadID int,
			@GOID INT,
			@KOLID INT,
			@HESABID INT,
			@THESABID INT,
			@THESABIDSath2 INT )
AS 

BEGIN 
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';   
  	
  	SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=null'; 
	
	
	  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   
	
	set @SQLString=N' SELECT     KOLID, CONVERT(money, SUM(DARBed) - SUM(PARBes)) AS Mande, SUM(PARBes) AS sumbes, SUM(DARBed) AS sumbed
FROM         [TRANSACTION]
WHERE     (SAID2 IS NOT NULL) AND (Date1 < @StartDate)
 '


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
      /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number < @startSanadID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
   
   
   set @SQLString= @SQLString + @WhrStatement  + 'GROUP BY KOLID
HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) '



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END 
GO




IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_MandeHesabTafsili]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_MandeHesabTafsili]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_MandeHRial]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Q_MandeHRial]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_MandePrintSandogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_MandePrintSandogh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Bed_Bes]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Q_Bed_Bes]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_Tar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditTafsili_Tar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_SummryOfSandogh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Q_SummryOfSandogh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_TRANSACTION]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Q_TRANSACTION]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_TRANSACTION_Foroosh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Q_TRANSACTION_Foroosh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_TRANSACTION_Kharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Q_TRANSACTION_Kharid]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[Q_PreviwSanadHEsabdary]'))
DROP VIEW [Q_PreviwSanadHEsabdary]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_SabtSanadFactSandogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_SabtSanadFactSandogh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_SabtSanadNaghdiSandogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_SabtSanadNaghdiSandogh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Sandogh_Hazine_Bed]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_Sandogh_Hazine_Bed]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Sandogh_Hazine_Bes]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_Sandogh_Hazine_Bes]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListGoroupHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListGoroupHesab]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListGoroupHesab_SanadHesabdari]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListGoroupHesab_SanadHesabdari]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListGoroupHesab_sood]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListGoroupHesab_sood]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030623]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030623]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030624]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030624]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_03062424]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_03062424]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030825]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030825]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030826]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030826]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030827]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030827]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030259]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030259]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030293]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030293]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030294]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030294]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030295]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030295]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030296]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030296]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_03029751]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_03029751]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_03029752]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_03029752]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030315]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030315]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030316]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030316]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030317]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030317]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030318]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030318]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_012076]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_012076]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_012077]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_012077]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_012078]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_012078]

GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021340]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021340]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021341]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021341]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021342]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021342]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_000000000000]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_000000000000]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm2_CloseShift]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm2_CloseShift]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm3_CloseShift]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm3_CloseShift]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_MOHesab_BedBes]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_MOHesab_BedBes]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_KolHesab_BedBes]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_KolHesab_BedBes]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_ForooshSandogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_ForooshSandogh]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_032598]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_032598]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_032599]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_032599]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_BarForoosh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_BarForoosh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_BarKharid]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_BarKharid]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_Daftar_Roozname]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_Daftar_Roozname]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_sood]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_sood]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_THesab_BedBes]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_THesab_BedBes]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[SP_VosoolManabe_CheckDblicateShomare]') AND type in (N'P', N'PC'))
DROP PROCEDURE [SP_VosoolManabe_CheckDblicateShomare]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_05002300880019****]') AND type in (N'P', N'PC'))
DROP PROCEDURE [std_05002300880019****]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900390022]') AND type in (N'P', N'PC'))
DROP PROCEDURE [std_050023008900390022]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900400024]') AND type in (N'P', N'PC'))
DROP PROCEDURE [std_050023008900400024]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900410026]') AND type in (N'P', N'PC'))
DROP PROCEDURE [std_050023008900410026]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900410031]') AND type in (N'P', N'PC'))
DROP PROCEDURE [std_050023008900410031]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[test6]'))
DROP VIEW [test6]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[TestP]') AND type in (N'P', N'PC'))
DROP PROCEDURE [TestP]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Tmp_DatarepGen]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Tmp_DatarepGen]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure6]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure6]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure13]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure13]
GO

IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View_SublistSoorathesab]'))
DROP VIEW [View_SublistSoorathesab]
GO

IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View_AvgFo]'))
DROP VIEW [View_AvgFo]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View_AvgKH]'))
DROP VIEW [View_AvgKH]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View1]'))
DROP VIEW [View1]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View2]'))
DROP VIEW [View2]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View3]'))
DROP VIEW [View3]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View5]'))
DROP VIEW [View5]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View6]'))
DROP VIEW [View6]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View7]'))
DROP VIEW [View7]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure7]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure7]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure8]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure8]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure9]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure9]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_PFrm_RepSAnadHesabdary2Date]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_PFrm_RepSAnadHesabdary2Date]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_PFrm_RepSAnadHesabdary2Mablagh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_PFrm_RepSAnadHesabdary2Mablagh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_PFrm_RepSAnadHesabdary2Said]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_PFrm_RepSAnadHesabdary2Said]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_SanadHesabdary]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_SanadHesabdary]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_SanadHesabdaryMorakab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_SanadHesabdaryMorakab]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_SanadHesabdaryMorakab1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_SanadHesabdaryMorakab1]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_date]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_date]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_Foroosh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_Foroosh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_ForooshPishFactor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_ForooshPishFactor]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_Khadamat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_Khadamat]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_Kharid]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_Kharid]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011660]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011660]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011661]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_011661]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021156]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021156]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021157]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_021157]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKalaKharid]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListKalaKharid]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKalaKharid2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListKalaKharid2]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_Sandoogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListKala_Sandoogh]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_listKala_sandoogh_GoroupKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_listKala_sandoogh_GoroupKala]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Pfrm_Virayesh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_Pfrm_Virayesh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListAnbar_Mojoodi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListAnbar_Mojoodi]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListGroupKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListGroupKala]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_EditGroup]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListKala_EditGroup]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_listKala_EditGroup_Sub2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_listKala_EditGroup_Sub2]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSub_MSG_PReviewListCHeckForRunProgramDAR]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSub_MSG_PReviewListCHeckForRunProgramDAR]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_SanadHesabdari_Bed_Bes_CALC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_SanadHesabdari_Bed_Bes_CALC]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListPishForoosh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListPishForoosh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_Pardakhtchek]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_Pardakhtchek]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPfrm_RaasChek]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPfrm_RaasChek]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqVosooly]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListCheqVosooly]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListNameKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListNameKala]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqPass]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListCheqPass]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqNoPass]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListCheqNoPass]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QSub_subform_T1]'))
DROP VIEW [QSub_subform_T1]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QSubPfrm_Khadamat]'))
DROP VIEW [QSubPfrm_Khadamat]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListBarForoosh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListBarForoosh]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqBargashti]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListCheqBargashti]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListKolHesabNotLimited_sood]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListKolHesabNotLimited_sood]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListMOinHesab_HazineFact]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListMOinHesab_HazineFact]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QRepKhadamat3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QRepKhadamat3]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListMoinHesabNotLimited]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListMoinHesabNotLimited]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListMoinHesabNotLimited_sood]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListMoinHesabNotLimited_sood]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[M_EbtedaDore]'))
DROP VIEW [M_EbtedaDore]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MojoodiKalaEbtedaDore]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MojoodiKalaEbtedaDore]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MojoodiKalaEbtedaDoreGrp]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MojoodiKalaEbtedaDoreGrp]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_Pfrm_Virayesh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_Pfrm_Virayesh]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[PFrm_Rep_SoodKala_MojoodiEbtedaDore]'))
DROP VIEW [PFrm_Rep_SoodKala_MojoodiEbtedaDore]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MabHazine_Sandogh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MabHazine_Sandogh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_Append_EditGroup]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_Append_EditGroup]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_EditMojodiKala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_EditMojodiKala]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure16]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure16]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure17]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure17]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure18]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure18]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure5]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure5]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure12]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure12]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure10]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure10]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure**1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure**1]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MabTakhfif_Foroosh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MabTakhfif_Foroosh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MabTakhfif_Kharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MabTakhfif_Kharid]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListAnbar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CQ_ListAnbar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKhadamt_All]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CQ_ListKhadamt_All]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MabHazine_Foroosh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MabHazine_Foroosh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MabHazine_Kharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F_MabHazine_Kharid]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DEFULT_SELECTAID]') AND type in (N'P', N'PC'))
DROP PROCEDURE [DEFULT_SELECTAID]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[PreviwSanadHEsabdary]'))
DROP VIEW [PreviwSanadHEsabdary]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_AsnadDar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditTafsili_AsnadDar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_AsnadPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditTafsili_AsnadPar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_BarFo]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditTafsili_BarFo]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_BarKh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditTafsili_BarKh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_Fo]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditTafsili_Fo]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_HazineFact]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditTafsili_HazineFact]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_Kh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditTafsili_Kh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_Khadmat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditTafsili_Khadmat]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_PishFact]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditTafsili_PishFact]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_AsnadDar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditMoin_AsnadDar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_AsnadPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditMoin_AsnadPar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_BarFo]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditMoin_BarFo]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_BarKh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditMoin_BarKh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_Fo]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditMoin_Fo]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_HazineFact]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditMoin_HazineFact]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_Kh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditMoin_Kh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_Khadmat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditMoin_Khadmat]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_PishFact]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditMoin_PishFact]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_Tafsili]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditMoin_Tafsili]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[FunShFishKhadamat]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [FunShFishKhadamat]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[KalaKhadamatName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [KalaKhadamatName]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[KalaName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [KalaName]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QSub_Frm_DastehChek]'))
DROP VIEW [QSub_Frm_DastehChek]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QMandeMoinHesab]'))
DROP VIEW [QMandeMoinHesab]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QMandeTafsiliHesab]'))
DROP VIEW [QMandeTafsiliHesab]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QMandeTafsiliHesab1]'))
DROP VIEW [QMandeTafsiliHesab1]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubListChTFrm_Anbar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubListChTFrm_Anbar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListTafsiliHesab_HazineFact]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListTafsiliHesab_HazineFact]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListKolHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListKolHesab]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListMoinHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListMoinHesab]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPfrm_ListMsgGoKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPfrm_ListMsgGoKala]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListNameKhadamat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_ListNameKhadamat]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_RepGen_UpdateFlagCH]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm_RepGen_UpdateFlagCH]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPfrm_ListSanadHesabdary]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPfrm_ListSanadHesabdary]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm1_CloseShift]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QSubPFrm1_CloseShift]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_Mojoodi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListKala_Mojoodi]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListGroup]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListGroup]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKalaKhadamat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListKalaKhadamat]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_PFrm_Daryaft]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_PFrm_Daryaft]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_PFrm_Pardakht]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_PFrm_Pardakht]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListKala]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_ALL_Barcode]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListKala_ALL_Barcode]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_listKala_ALL_GoroupKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_listKala_ALL_GoroupKala]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListDaryaftNaghdi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListDaryaftNaghdi]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListDasteCheck]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListDasteCheck]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Forosh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Q_Forosh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ForoshDate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Q_ForoshDate]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Kharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Q_Kharid]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_List_Anbar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_List_Anbar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListAnbar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListAnbar]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_CheckingSubFactSandogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_CheckingSubFactSandogh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_DeleteSanadFactSandogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_DeleteSanadFactSandogh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditKol_Moin]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditKol_Moin]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditKol_Tafsili]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_EditKol_Tafsili]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListNameAllHesab]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [Q_ListNameAllHesab]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListPardakhtNaghdi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListPardakhtNaghdi]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QList_KalaKhadamat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QList_KalaKhadamat]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListAmelTozie]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListAmelTozie]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030102]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030102]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030103]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030103]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030104]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030104]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030107]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030107]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QSum_HazineForoosh]'))
DROP VIEW [QSum_HazineForoosh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05228521]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05228521]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05238835]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05238835]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_PForm_Virayesh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_PForm_Virayesh]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Std_05002300880035]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Std_05002300880035]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[SELECTGOKID]') AND type in (N'P', N'PC'))
DROP PROCEDURE [SELECTGOKID]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure14]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[UserCheckLogin]') AND type in (N'P', N'PC'))
DROP PROCEDURE [UserCheckLogin]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[UserContoroling]') AND type in (N'P', N'PC'))
DROP PROCEDURE [UserContoroling]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[UserLogin]') AND type in (N'P', N'PC'))
DROP PROCEDURE [UserLogin]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[ViewRepChek]'))
DROP VIEW [ViewRepChek]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View4]'))
DROP VIEW [View4]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View_Rep103]'))
DROP VIEW [View_Rep103]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View_T_RepGen]'))
DROP VIEW [View_T_RepGen]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[ViewPishFactKharid]'))
DROP VIEW [ViewPishFactKharid]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Table_UserDastrasi_DELETE]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Table_UserDastrasi_DELETE]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Table_UserDastrasi_GetValue]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Table_UserDastrasi_GetValue]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Table_UserDastrasi_Insert]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Table_UserDastrasi_Insert]
GO
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QueryCompinfo]'))
DROP VIEW [QueryCompinfo]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Phonebook_Search]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_Phonebook_Search]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_SoratHesabHatami]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_SoratHesabHatami]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListCurrency]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Q_ListCurrency]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F1EndDate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F1EndDate]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F1StartDate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [F1StartDate]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Caption_TreeViewVasKharid]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Caption_TreeViewVasKharid]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure11]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProcedure11]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CloseHesab_InsertInformationHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CloseHesab_InsertInformationHesab]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CloseHesab_InsertInformationT_RepGen]') AND type in (N'P', N'PC'))
DROP PROCEDURE [CloseHesab_InsertInformationT_RepGen]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[ConvertArabicToPersion]') AND type in (N'P', N'PC'))
DROP PROCEDURE [ConvertArabicToPersion]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[GetShamsiInfo]') AND type in (N'P', N'PC'))
DROP PROCEDURE [GetShamsiInfo]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProceduretest]') AND type in (N'P', N'PC'))
DROP PROCEDURE [StoredProceduretest]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[PersianToGregorian]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [PersianToGregorian]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListHesab_SanadHesabdari]') AND type in (N'P', N'PC'))
DROP PROCEDURE [QListHesab_SanadHesabdari]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0121]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0121]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030210]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030210]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030212]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030212]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030521]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030521]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030522]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_030522]
GO


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0215]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0215]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900410033]') AND type in (N'P', N'PC'))
DROP PROCEDURE [std_050023008900410033]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_05002300900042]') AND type in (N'P', N'PC'))
DROP PROCEDURE [std_05002300900042]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_05002300900043]') AND type in (N'P', N'PC'))
DROP PROCEDURE [std_05002300900043]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_05002300900044]') AND type in (N'P', N'PC'))
DROP PROCEDURE [std_05002300900044]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_05002300900045]') AND type in (N'P', N'PC'))
DROP PROCEDURE [std_05002300900045]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_sood_Sub]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_sood_Sub]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[RepFST_0523894024]') AND type in (N'P', N'PC'))
DROP PROCEDURE [RepFST_0523894024]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_SooDRooZaneh9048]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_SooDRooZaneh9048]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_SoratHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_SoratHesab]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523893921]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0523893921]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894024]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0523894024]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894126]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0523894126]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894131]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0523894131]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894133]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_0523894133]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239042]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05239042]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239043]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05239043]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239044]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05239044]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239045]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05239045]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05238833]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05238833]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05238834]') AND type in (N'P', N'PC'))
DROP PROCEDURE [Rep_05238834]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05238834]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_05238834] ( @IdKala int, @KalaGO int )           AS     
  begin 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';                  
  SET @ParmDefinition = N''@IdKala int=null,@KalaGO int=null'';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
  begin 	 	  		
  	SET @SQLString = ''SELECT     T_NameKala.BarCode, T_NameKala.NameKala1, T_NameVahed.NameVahed, SUM(ROUND(CONVERT(float, T_Kala.Mojodi), 2)) AS Mojodi,  
  	                 AVG(T_Kala.UnitPrice) AS Expr1, T_NameKala.KalaNumber FROM         T_Kala INNER JOIN                       
  	                 T_NameKala ON T_Kala.NameKalaID = T_NameKala.ID INNER JOIN                       
  	                 T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN                       
  	                 T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID '' 			 			
  	SET @WhrStatement='''' 			 			
  	set @ParmAndstr='' Where '' 			
  	if(NOT (@IdKala  IS NULL)) 			
  	begin 				
  		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
  		set @ParmAndstr='' AND '' 			
  	end 			 			 			
  	if(NOT (@KalaGO  IS NULL)) 			
  	begin 				
  		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
  		set @ParmAndstr='' AND '' 			
  	end 			   			
  	set @SQLString=@SQLString + @WhrStatement+N'' GROUP BY T_NameKala.KalaNumber,T_NameKala.BarCode, T_NameKala.NameKala1, T_NameVahed.NameVahed'' 	 	 			EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IdKala=@IdKala,@KalaGO=@KalaGO;  		end                                                  return end                      
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05238833]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_05238833] ( @IdKala int, @KalaGO int )           AS     
  
  begin 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';                  
  SET @ParmDefinition = N''@IdKala int=null,@KalaGO int=null'';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
  begin 	 	  		
  	SET @SQLString = ''SELECT     T_NameKala.BarCode, T_NameKala.NameKala1, T_NameVahed.NameVahed, SUM(ROUND(CONVERT(float, T_Kala.Mojodi), 2)) AS Mojodi, 
  						AVG(T_Kala.UnitPrice) AS Expr1 FROM         T_Kala INNER JOIN                       
  						T_NameKala ON T_Kala.NameKalaID = T_NameKala.ID INNER JOIN                       
  						T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN                       
  						T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID '' 			 			
	SET @WhrStatement='''' 			 			
	set @ParmAndstr='' Where '' 			
	if(NOT (@IdKala  IS NULL)) 			
	begin 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
		set @ParmAndstr='' AND '' 			
	end 			 			 			
	if(NOT (@KalaGO  IS NULL)) 			
	begin 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
		set @ParmAndstr='' AND '' 			
	end 			   			
	set @SQLString=@SQLString + @WhrStatement+N'' GROUP BY T_NameKala.BarCode, T_NameKala.NameKala1, T_NameVahed.NameVahed'' 	 	 			
	EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IdKala=@IdKala,@KalaGO=@KalaGO;  		
  
end                                                  
return end                      
' 
END

GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239045]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
        CREATE PROCEDURE [Rep_05239045](       
			@Goid int, 
			@KolID int, 
			@HesabID int, 
			@THesabID int, 
			@startDate nvarchar(50), 
			@enddate nvarchar(50), 
			@IdKala INT=NULL,
			@IDA INT=NULL,
			@IDGokala INT=NULL,
			@faktorN nvarchar(50)) 
         	AS 
         	
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';   
  
  
    SET @ParmDefinition = N''@Goid int=null, 
							@KolID int=null, 
							@HesabID int=null, 
							@THesabID int=null, 
							@startDate nvarchar(50)=N''''0'''', 
							@enddate nvarchar(50)=N''''99999999'''', 
							@IdKala int=null, 
							@IDA int=null, 
							@IDGokala INT=NULL,
							@faktorN nvarchar(50)=null''    

  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate=N''00000000''; 				   
	END   
	      
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate=N''99999999''; 				   
	END 
			 				 
SET @SQLString =''SELECT     NoeFa, Varde, Sadre, AID, KALAID, Expr1, Expr5, Date1 AS MinOfDate1, Date1 AS MaxOfDate1, startDate, enddate, namehesab, KalaNumber, NameKala1, NoeFact, GOKID
FROM         dbo.FunSub_05239045(@StartDate, @Enddate) AS FunSub_05239045_1 ''

                      

   /* --------------------شرط حساب سرگروه روی ساب*/ 			
  set @ParmAndstr='' AND '' 			
  if(NOT (@Goid IS NULL)) 			
  BEGIN 				
  set @WhrStatement= @WhrStatement + @ParmAndstr + N''(Goid = @Goid )''; 				
  set @ParmAndstr='' AND '' 			
  END             
   /* --------------------شرط حساب کل  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@KolID IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KolID = @KolID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
   /*  --------------------شرط حساب معین  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@HesabID  IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(HesabID = @HesabID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
    /* --------------------   شرط حساب تفصیلی */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@THesabID  IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(THesabID = @THesabID )''; 				
    set @ParmAndstr='' AND '' 			
    END            
    
    
        /* --------------------   شرط کالا */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IdKala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KALAID = @IdKala )''; 				
		set @ParmAndstr='' AND '' 			
    END      
    
            /* --------------------   شرط گروه */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDGokala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(GOKID = @IDGokala )''; 				
		set @ParmAndstr='' AND '' 			
    END  
    
            /* --------------------   شرط انبار */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDA IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(AID = @IDA )''; 				
    set @ParmAndstr='' AND '' 			
    END      
    
   /* --------------------   شرط فاکتور */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@faktorN IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(NoeFact = @faktorN )''; 				
    set @ParmAndstr='' AND '' 			
    END   
    
    set @SQLString=@SQLString +@WhrStatement+ N'' GROUP BY NoeFa, Varde, Sadre, AID, KALAID, Expr1, Expr5, startDate, enddate, namehesab, KalaNumber, NameKala1, NoeFact, GOKID, Date1 ''
    
    
    EXECUTE sp_executesql   @SQLString, @ParmDefinition,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@startDate =@startDate,@enddate =@enddate,@IdKala =@IdKala,	@IDA =@IDA,@IDGokala=@IDGokala,@faktorN =@faktorN;  		
    END  
         

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239044]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
         CREATE PROCEDURE [Rep_05239044](        
			@Goid int, 
			@KolID int, 
			@HesabID int, 
			@THesabID int, 
			@startDate nvarchar(50), 
			@enddate nvarchar(50), 
			@IdKala INT=NULL,
			@IDA INT=NULL,
			@IDGokala INT=NULL,
			@faktorN nvarchar(50)) 
         	AS 
         	
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';   
  
  
    SET @ParmDefinition = N''@Goid int=null, 
							@KolID int=null, 
							@HesabID int=null, 
							@THesabID int=null, 
							@startDate nvarchar(50)=N''''0'''', 
							@enddate nvarchar(50)=N''''99999999'''', 
							@IdKala int=null, 
							@IDA int=null, 
							@IDGokala INT=NULL,
							@faktorN nvarchar(50)=null''    

  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate=N''00000000''; 				   
	END   
	      
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate=N''99999999''; 				   
	END 
			 				 
SET @SQLString =''SELECT     Expr1, HESABID, THESABID, KOLID, GOID, sh, NoeFa, SUM(Varde) AS Varde, FieldN3, Expr5, VAHED1, KALAID, AID, NoFAKTOR, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, 
                      namehesab, startDate, enddate, Date1, KalaNumber, NameKala1
FROM         dbo.FunSub_05239044(@StartDate,@Enddate) AS FunSub_05239044_1 where 1=1
 ''

                      

   /* --------------------شرط حساب سرگروه روی ساب*/ 			
  set @ParmAndstr='' AND '' 			
  if(NOT (@Goid IS NULL)) 			
  BEGIN 				
  set @WhrStatement= @WhrStatement + @ParmAndstr + N''(Goid = @Goid )''; 				
  set @ParmAndstr='' AND '' 			
  END             
   /* --------------------شرط حساب کل  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@KolID IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KolID = @KolID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
   /*  --------------------شرط حساب معین  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@HesabID  IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(HesabID = @HesabID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
    /* --------------------   شرط حساب تفصیلی */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@THesabID  IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(THesabID = @THesabID )''; 				
    set @ParmAndstr='' AND '' 			
    END            
    
    
        /* --------------------   شرط کالا */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IdKala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KALAID = @IdKala )''; 				
		set @ParmAndstr='' AND '' 			
    END      
    
            /* --------------------   شرط گروه */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDGokala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(GOKID = @IDGokala )''; 				
		set @ParmAndstr='' AND '' 			
    END  
    
            /* --------------------   شرط انبار */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDA IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(AID = @IDA )''; 				
    set @ParmAndstr='' AND '' 			
    END      
    
   /* --------------------   شرط فاکتور */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@faktorN IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(NoFAKTOR = @faktorN )''; 				
    set @ParmAndstr='' AND '' 			
    END   
    
    set @SQLString=@SQLString +@WhrStatement+ N'' GROUP BY Expr1, HESABID, THESABID, KOLID, GOID, sh, NoeFa, FieldN3, Expr5, VAHED1, KALAID, AID, NoFAKTOR, namehesab, startDate, enddate, Date1, KalaNumber, NameKala1''
     			
    EXECUTE sp_executesql   @SQLString, @ParmDefinition,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@startDate =@startDate,@enddate =@enddate,@IdKala =@IdKala,	@IDA =@IDA,@IDGokala=@IDGokala,@faktorN =@faktorN;  		
    END  
         
         
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239043]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
        CREATE PROCEDURE [Rep_05239043](       
        	
			@Goid int, 
			@KolID int, 
			@HesabID int, 
			@THesabID int, 
			@startDate nvarchar(50), 
			@enddate nvarchar(50), 
			@IdKala nvarchar(50), 
			@IDA nvarchar(50),
			@IDGokala nvarchar(50),  
			@faktorN nvarchar(50)) 
         	AS 
         	
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';   
  
  
    SET @ParmDefinition = N''@Goid int=null, 
							@KolID int=null, 
							@HesabID int=null, 
							@THesabID int=null, 
							@startDate nvarchar(50)=N''''0'''', 
							@enddate nvarchar(50)=N''''99999999'''', 
							@IdKala int=null, 
							@IDA int=null,
							@IDGokala int=null, 
							@faktorN nvarchar(50)=null''    

  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate=N''00000000''; 				   
	END   
	      
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate=N''99999999''; 				   
	END 
								 
SET @SQLString =''     SELECT     gokid,NoeFa, SUM(Mande) AS Mande, SUM(Varde) AS Varde, SUM(Sadre) AS Sadre, Fi, Expr5, Expr1, MinOfDate1, MaxOfDate1, startDate, enddate, namehesab, KalaNumber, KALAID, NameKala1, 
                      AID
FROM         dbo.FunSub_05239043(@StartDate,@Enddate) AS FunSub_05239043_1 where (1=1)
''


   /* --------------------شرط حساب سرگروه روی ساب*/ 			
  set @ParmAndstr='' AND '' 			
  if(NOT (@Goid IS NULL)) 			
  BEGIN 				
  set @WhrStatement= @WhrStatement + @ParmAndstr + N''(Goid = @Goid )''; 				
  set @ParmAndstr='' AND '' 			
  END             
   /* --------------------شرط حساب کل  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@KolID IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KolID = @KolID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
   /*  --------------------شرط حساب معین  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@HesabID  IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(HesabID = @HesabID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
    /* --------------------   شرط حساب تفصیلی */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@THesabID  IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(THesabID = @THesabID )''; 				
    set @ParmAndstr='' AND '' 			
    END            
    
    
        /* --------------------   شرط کالا */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IdKala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KALAID = @IdKala )''; 				
		set @ParmAndstr='' AND '' 			
    END      
    
            /* --------------------   شرط انبار */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDA IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(AID = @IDA )''; 				
    set @ParmAndstr='' AND '' 			
    END      
    
                /* --------------------   شرط گروه */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDGokala IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(GOKID = @IDGokala )''; 				
    set @ParmAndstr='' AND '' 			
    END     
    
   /* --------------------   شرط فاکتور */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@faktorN IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(NoeFa = @faktorN )''; 				
    set @ParmAndstr='' AND '' 			
    END   
    
    set @SQLString=@SQLString +@WhrStatement+ N'' GROUP BY gokid,NoeFa, Fi, Expr5, Expr1, MinOfDate1, MaxOfDate1, startDate, enddate, namehesab, KalaNumber, KALAID, NameKala1, AID ''  	
     			
    EXECUTE sp_executesql   @SQLString, @ParmDefinition,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@startDate =@startDate,@enddate =@enddate,@IdKala =@IdKala,	@IDA =@IDA,@IDGokala=@IDGokala,@faktorN =@faktorN;  		
    END  
    

        	
        
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05239042]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
         CREATE PROCEDURE [Rep_05239042](
			@Goid int, 
			@KolID int, 
			@HesabID int, 
			@THesabID int, 
			@startDate nvarchar(50), 
			@enddate nvarchar(50), 
			@IdKala INT=NULL,
			@IDA INT=NULL,
			@IDGokala INT=NULL,
			@faktorN nvarchar(50)) 
         	AS 
         	
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';   
  
  
    SET @ParmDefinition = N''@Goid int=null, 
							@KolID int=null, 
							@HesabID int=null, 
							@THesabID int=null, 
							@startDate nvarchar(50)=N''''0'''', 
							@enddate nvarchar(50)=N''''99999999'''', 
							@IdKala int=null, 
							@IDA int=null, 
							@IDGokala INT=NULL,
							@faktorN nvarchar(50)=null''    

  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate=N''00000000''; 				   
	END   
	      
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate=N''99999999''; 				   
	END 
			 				 
SET @SQLString =''SELECT    GOKID, GOID, KOLID, HESABID, THESABID, IDAUtoFact, sho, NoeFa, Sadre, FieldN3, VAHED1, KALAID, AID, NoFAKTOR, namehesab, startDate, enddate, Date1, KalaNumber, NameKala1, Expr5, MIN(Date1) 
                      AS MinOfDate1, MAX(Date1) AS MaxOfDate1
FROM         dbo.FunSub_05239042(@startDate, @enddate) AS FunSub_05239042_1 where (1=1)

''

                      

   /* --------------------شرط حساب سرگروه روی ساب*/ 			
  set @ParmAndstr='' AND '' 			
  if(NOT (@Goid IS NULL)) 			
  BEGIN 				
  set @WhrStatement= @WhrStatement + @ParmAndstr + N''(Goid = @Goid )''; 				
  set @ParmAndstr='' AND '' 			
  END             
   /* --------------------شرط حساب کل  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@KolID IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KolID = @KolID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
   /*  --------------------شرط حساب معین  */ 			
   set @ParmAndstr='' AND '' 			
   if(NOT (@HesabID  IS NULL)) 			
   BEGIN 				
   set @WhrStatement= @WhrStatement + @ParmAndstr + N''(HesabID = @HesabID )''; 				
   set @ParmAndstr='' AND '' 			
   END             
    /* --------------------   شرط حساب تفصیلی */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@THesabID  IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(THesabID = @THesabID )''; 				
    set @ParmAndstr='' AND '' 			
    END            
    
    
        /* --------------------   شرط کالا */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IdKala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KALAID = @IdKala )''; 				
		set @ParmAndstr='' AND '' 			
    END      
    
            /* --------------------   شرط گروه */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDGokala IS NULL)) 			
    BEGIN 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(GOKID = @IDGokala )''; 				
		set @ParmAndstr='' AND '' 			
    END  
    
            /* --------------------   شرط انبار */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@IDA IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(AID = @IDA )''; 				
    set @ParmAndstr='' AND '' 			
    END      
    
   /* --------------------   شرط فاکتور */ 			
    set @ParmAndstr='' AND '' 			
    if(NOT (@faktorN IS NULL)) 			
    BEGIN 				
    set @WhrStatement= @WhrStatement + @ParmAndstr + N''(NoFAKTOR = @faktorN )''; 				
    set @ParmAndstr='' AND '' 			
    END   
    
    set @SQLString=@SQLString +@WhrStatement+ N'' GROUP BY GOKID,GOID, KOLID, HESABID, THESABID,Expr5, IDAUtoFact, sho, NoeFa, Sadre, FieldN3, VAHED1, KALAID, AID, NoFAKTOR, namehesab, startDate, enddate, Date1, KalaNumber, NameKala1 ''  	
     			
    EXECUTE sp_executesql   @SQLString, @ParmDefinition,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@startDate =@startDate,@enddate =@enddate,@IdKala =@IdKala,	@IDA =@IDA,@IDGokala=@IDGokala,@faktorN =@faktorN;  		
    END  
    

			
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894133]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_0523894133] ( 
  	@IdKala int, 
  	@IDA int, 
  	@KalaGO int, 
  	@startDate nvarchar(50), 
  	@enddate nvarchar(50)  )           
  	AS     
  	begin 	
  		SET NOCOUNT ON; 	 	
  		DECLARE @SQLString  NVARCHAR(4000);  	
  		DECLARE @WhrStatement  NVARCHAR(2000);         	
  		DECLARE @ParmDefinition  NVARCHAR(2000);         	
  		DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  		set @WhrStatement  = N'''';         	
  		set @ParmAndstr = N'''';                  	
  		SET @ParmDefinition = N''@IdKala int=null, 												
  								@IDA int=null, 												
  								@KalaGO int=null, 												
  								@startDate nvarchar(50)=''''0'''', 												
  								@enddate nvarchar(50)=''''99999999'''''';            	
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
begin 	 	  		 
	SET @SQLString =''SELECT     CONVERT(float, ISNULL(TBL_TRA.SumTedadKh, 0)) AS SumTedadKh, CONVERT(float, ISNULL(TBL_TRA.SumTedadVA, 0)) AS SumTedadVA, 
	                  CONVERT(float, ISNULL(TBL_TRA.SumTedadKhA, 0)) AS SumTedadKhA, CONVERT(float, ISNULL(TBL_TRA.SumTedadFo, 0)) AS SumTedadFo, 
	                  CONVERT(float, ISNULL(TBL_TRA.SumTedadBarFo, 0)) AS SumTedadBarFo, CONVERT(float, ISNULL(TBL_TRA.SumTedadBarKh, 0))            
	                  AS SumTedadBarKh, CONVERT(float, SUM(T_Kala.Mojodi)) AS M_Ebteda, ROUND(CONVERT(Float, ISNULL(TBL_TRA.MojoodiTra, 0)     
	                  + (CASE WHEN dbo.F1StartDate() >= @startDate THEN SUM(dbo.T_Kala.Mojodi) ELSE 0 END)), 2) AS Mande, 
	                  CONVERT(float, ISNULL(TBL_TRA.Varde, 0)) AS Varde, CONVERT(float, ISNULL(TBL_TRA.Sadre, 0)) AS Sadre, T_Kala.GOKID, 
	                  TBL_TRA.MojoodiTra, T_NameGoKala.NameGoKala1 FROM         
	                  (SELECT     GOKID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj)         
	                  + SUM(TedadBarKh) + SUM(TedadKhA)) AS MojoodiTra, SUM(TedadKh + TedadVorood + TedadBarFo + TedadVA) AS Varde,            
	                  SUM(TedadFo + TedadKhorooj + TedadBarKh + TedadKhA) AS Sadre, SUM(TedadKh) AS SumTedadKh, SUM(TedadVA) AS SumTedadVA,             
	                  SUM(TedadKhA) AS SumTedadKhA, SUM(TedadFo) AS SumTedadFo, SUM(TedadBarFo) AS SumTedadBarFo, SUM(TedadBarKh)                          
	                  AS SumTedadBarKh                         
	                   FROM         [TRANSACTION]                         WHERE     (Date1 BETWEEN @startDate AND @enddate)'' 			 			 			
set @ParmAndstr='' AND '' 			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end                                
set @SQLString=@SQLString + @WhrStatement+'' GROUP BY GOKID) AS TBL_TRA RIGHT OUTER JOIN                       
											T_Kala ON TBL_TRA.GOKID = T_Kala.GOKID RIGHT OUTER JOIN                       
											T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID WHERE     (T_Kala.VKalaID = 1)'' 			
set @WhrStatement='''' 			 			
set @ParmAndstr='' AND ''   			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			
if(NOT (@IdKala  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			 			
if(NOT (@KalaGO  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
	set @ParmAndstr='' AND '' 			
end 			   			
set @SQLString=@SQLString + @WhrStatement + N'' GROUP BY T_Kala.GOKID, CONVERT(float, ISNULL(TBL_TRA.Varde, 0)), CONVERT(float, ISNULL(TBL_TRA.Sadre, 0)), TBL_TRA.MojoodiTra, CONVERT(float,                        
												ISNULL(TBL_TRA.SumTedadKh, 0)), CONVERT(float, ISNULL(TBL_TRA.SumTedadVA, 0)), CONVERT(float, ISNULL(TBL_TRA.SumTedadKhA, 0)),                        
												CONVERT(float, ISNULL(TBL_TRA.SumTedadFo, 0)), CONVERT(float, ISNULL(TBL_TRA.SumTedadBarFo, 0)), CONVERT(float,                        
												ISNULL(TBL_TRA.SumTedadBarKh, 0)), T_NameGoKala.NameGoKala1'' 	 			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IDA=@IDA,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@enddate=@enddate;  		
end                                                  
return end                      
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894131]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_0523894131] ( 
  	@IdKala int, 
  	@IDA int, 
  	@KalaGO int, 
  	@startDate nvarchar(50), 
  	@enddate nvarchar(50)  )           
  	AS     
  	begin 	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  	set @WhrStatement  = N'''';         	
  	set @ParmAndstr = N'''';                  	
  	SET @ParmDefinition = N''@IdKala int=null, 												
  							@IDA int=null, 												
  							@KalaGO int=null, 												
  							@startDate nvarchar(50)=''''0'''', 												
  							@enddate nvarchar(50)=''''99999999'''''';            	
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
begin 	 	  		 
	SET @SQLString =''SELECT     Convert(float,ISNULL(SumTedadKh,0)) as SumTedadKh,  								
	Convert(float,ISNULL(SumTedadVA,0)) as SumTedadVA,  								
	Convert(float,ISNULL(SumTedadKhA,0)) as SumTedadKhA,  								
	Convert(float,ISNULL(SumTedadFo,0)) as SumTedadFo,  								
	Convert(float,ISNULL(SumTedadBarFo,0)) as SumTedadBarFo,  								
	Convert(float,ISNULL(SumTedadBarKh,0)) as SumTedadBarKh, 							
	convert(float,SUM(dbo.T_Kala.Mojodi)) as M_Ebteda,round(convert(Float,ISNULL(TBL_TRA.MojoodiTra,0)+ (case when dbo.F1StartDate()>=@startDate 
	then SUM(dbo.T_Kala.Mojodi) else 0 end)),2) AS Mande, convert(float,ISNULL(TBL_TRA.Varde, 0)) AS Varde, Convert(float,ISNULL(TBL_TRA.Sadre, 0)) 	
	AS Sadre, dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, dbo.T_NameKala.KalaNumber AS KalaNumber,  							
	dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib, dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode,  							
	dbo.T_Kala.NameKalaID, TBL_TRA.MojoodiTra 							
	FROM         dbo.T_NameKala INNER JOIN 							
	dbo.T_NameVahed ON dbo.T_NameKala.VAHED1 = dbo.T_NameVahed.VAHED1 INNER JOIN 							
	dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN 							
	dbo.T_NameVahed T_NameVahed_2 ON dbo.T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN 							
	(SELECT     KALAID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj) + SUM(TedadBarKh) 
	+ SUM(TedadKhA)) AS MojoodiTra, SUM(TedadKh + TedadVorood + TedadBarFo + TedadVA) AS Varde,                        
	SUM(TedadFo + TedadKhorooj + TedadBarKh + TedadKhA) AS Sadre, SUM(TedadKh) AS SumTedadKh, SUM(TedadVA) AS SumTedadVA,                        
	SUM(TedadKhA) AS SumTedadKhA, SUM(TedadFo) AS SumTedadFo, SUM(TedadBarFo) AS SumTedadBarFo, SUM(TedadBarKh)                        
	AS SumTedadBarKh FROM         [TRANSACTION] Where ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) '' 			 			 			
	
	set @ParmAndstr='' AND '' 			
	if(NOT (@IDA  IS NULL)) 			
	begin 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].AID = @IDA )''; 				
		set @ParmAndstr='' AND '' 			
	end                                
	set @SQLString=@SQLString + @WhrStatement+'' GROUP BY KALAID) TBL_TRA ON dbo.T_Kala.NameKalaID = TBL_TRA.KALAID WHERE     (dbo.T_Kala.VKalaID = 1) ''   
	set @WhrStatement='''' 			 			
	set @ParmAndstr='' AND ''   			
	if(NOT (@IDA  IS NULL)) 			
	begin 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @IDA )''; 				
		set @ParmAndstr='' AND '' 			
	end 			 			
	if(NOT (@IdKala  IS NULL)) 			
	begin 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
		set @ParmAndstr='' AND '' 			
	end 			 			 			
	if(NOT (@KalaGO  IS NULL)) 			
	begin 				
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
		set @ParmAndstr='' AND '' 			
	end 			   			
	set @SQLString=@SQLString + @WhrStatement + N''  GROUP BY  dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed, dbo.T_NameKala.KalaNumber, 
	dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib,                        
	dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode, dbo.T_Kala.NameKalaID, convert(float,ISNULL(TBL_TRA.Varde, 0)),
convert(float,ISNULL(TBL_TRA.Sadre, 0)), TBL_TRA.MojoodiTra, Convert(float,ISNULL(SumTedadKh,0)), Convert(float,ISNULL(SumTedadVA,0)), 
Convert(float,ISNULL(SumTedadKhA,0)), Convert(float,ISNULL(SumTedadFo,0)), Convert(float,ISNULL(SumTedadBarFo,0)), Convert(float,ISNULL(SumTedadBarKh,0)) '' 
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IDA=@IDA,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@enddate=@enddate;  		
end                                                  
return end                      
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894126]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_0523894126] ( 
  	@IdKala int, 
  	@IDA int, 
  	@KalaGO int, 
  	@startDate nvarchar(50), 
  	@enddate nvarchar(50)  )           
  	AS     
  	begin 	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  	set @WhrStatement  = N'''';         	
  	set @ParmAndstr = N'''';                  	
  	SET @ParmDefinition = N''@IdKala int=null, 												
  							@IDA int=null, 												
  							@KalaGO int=null, 												
  							@startDate nvarchar(50)=''''0'''', 												
  							@enddate nvarchar(50)=''''99999999'''''';            	
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
begin 	 	  		 
	SET @SQLString =''SELECT     convert(float,SUM(dbo.T_Kala.Mojodi)) as M_Ebteda,round(convert(Float,ISNULL(TBL_TRA.MojoodiTra,0)+ (case when dbo.F1StartDate()>=@startDate then SUM(dbo.T_Kala.Mojodi) else 0 end)),2) AS Mande, convert(float,ISNULL(TBL_TRA.Varde, 0)) AS Varde, Convert(float,ISNULL(TBL_TRA.Sadre, 0)) 							
	AS Sadre, dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, dbo.T_NameKala.KalaNumber AS KalaNumber,  							dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib, dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode,  							dbo.T_Kala.NameKalaID, TBL_TRA.MojoodiTra 							
	                 FROM         dbo.T_NameKala INNER JOIN 							dbo.T_NameVahed ON dbo.T_NameKala.VAHED1 = dbo.T_NameVahed.VAHED1 INNER JOIN 							dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN 							dbo.T_NameVahed T_NameVahed_2 ON dbo.T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN 		
	                 					(SELECT     KALAID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj)  							+ SUM(TedadBarKh) + SUM(TedadKhA)) AS MojoodiTra, SUM(TedadKh + TedadVorood + TedadBarFo + TedadVA) AS Varde,  							SUM(TedadFo + TedadKhorooj + TedadBarKh + TedadKhA) AS Sadre 	
	                 											FROM         dbo.[TRANSACTION] Where ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) '' 			 			 			set @ParmAndstr='' AND '' 			if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end      
                          
set @SQLString=@SQLString + @WhrStatement+'' GROUP BY KALAID) TBL_TRA ON dbo.T_Kala.NameKalaID = TBL_TRA.KALAID WHERE     (dbo.T_Kala.VKalaID = 1) ''  
set @WhrStatement='''' 			 			
set @ParmAndstr='' AND ''   			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			
if(NOT (@IdKala  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			 			
if(NOT (@KalaGO  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
	set @ParmAndstr='' AND '' 			
end 			   			
set @SQLString=@SQLString + @WhrStatement + N''  GROUP BY  dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib,                        
												dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode, dbo.T_Kala.NameKalaID, convert(float,ISNULL(TBL_TRA.Varde, 0)),                        
												convert(float,ISNULL(TBL_TRA.Sadre, 0)), TBL_TRA.MojoodiTra '' 	 	 			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IDA=@IDA,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@enddate=@enddate;  		
end                                                  
return end                      
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523894024]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_0523894024] ( 
  	@IdKala int, 
  	@IDA int, 
  	@KalaGO int, 
  	@startDate nvarchar(50), 
  	@enddate nvarchar(50)  )           
  	AS     
  	begin 	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  	set @WhrStatement  = N'''';         	
  	set @ParmAndstr = N'''';                  	
  	SET @ParmDefinition = N''@IdKala int=null, 												
  							@IDA int=null, 												
  							@KalaGO int=null, 												
  							@startDate nvarchar(50)=''''0'''', 												
  							@enddate nvarchar(50)=''''99999999'''''';            	
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
begin 	 	  		 
	SET @SQLString = ''SELECT     round(convert(Float,ISNULL(TBL_TRA.MojoodiTra,0)+ (case when dbo.F1StartDate()>=@startDate then SUM(dbo.T_Kala.Mojodi) else 0 end)),2) AS Mande, ISNULL(TBL_TRA.Varde, 0) AS Varde, ISNULL(TBL_TRA.Sadre, 0)  							
	AS Sadre, dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, dbo.T_NameKala.KalaNumber AS KalaNumber,  							dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib, dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode,  							
	dbo.T_Kala.NameKalaID, TBL_TRA.MojoodiTra 							FROM         dbo.T_NameKala INNER JOIN 							dbo.T_NameVahed ON dbo.T_NameKala.VAHED1 = dbo.T_NameVahed.VAHED1 INNER JOIN 							
	dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN 							dbo.T_NameVahed T_NameVahed_2 ON dbo.T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN 							
	(SELECT     KALAID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj)  							+ SUM(TedadBarKh) + SUM(TedadKhA)) AS MojoodiTra, SUM(TedadKh + TedadVorood + TedadBarFo + TedadVA) AS Varde,  							
	SUM(TedadFo + TedadKhorooj + TedadBarKh + TedadKhA) AS Sadre 							
	 FROM         dbo.[TRANSACTION] Where ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)  '' 			 			 			

set @ParmAndstr='' AND '' 			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end    
                            
set @SQLString=@SQLString + @WhrStatement+'' GROUP BY KALAID) TBL_TRA ON dbo.T_Kala.NameKalaID = TBL_TRA.KALAID WHERE     (dbo.T_Kala.VKalaID = 1) ''               			
set @WhrStatement='''' 			 			
set @ParmAndstr='' AND ''   			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			
if(NOT (@IdKala  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			 			
if(NOT (@KalaGO  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
	set @ParmAndstr='' AND '' 			
end 			   			
set @SQLString=@SQLString + @WhrStatement + N''  GROUP BY dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib,                        
												dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode, dbo.T_Kala.NameKalaID, ISNULL(TBL_TRA.Varde, 0),                        
												ISNULL(TBL_TRA.Sadre, 0), TBL_TRA.MojoodiTra   HAVING      (ROUND(CONVERT(Float, ISNULL(TBL_TRA.MojoodiTra, 0) + (CASE WHEN dbo.F1StartDate() >= @startDate THEN SUM(dbo.T_Kala.Mojodi) ELSE 0 END)), 2) <> 0)          '' 	 	 			

EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IDA=@IDA,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@enddate=@enddate;  		
end                                                  

return end   


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0523893921]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [Rep_0523893921] ( 
  	@IdKala int, 
  	@IDA int, 
  	@KalaGO int, 
  	@startDate nvarchar(50), 
  	@enddate nvarchar(50)  )           
  	
  	AS     
  	
  	begin 	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  	set @WhrStatement  = N'''';         	
  	set @ParmAndstr = N'''';                  	
  	SET @ParmDefinition = N''@IdKala int=null, 												
  							@IDA int=null, 												
  							@KalaGO int=null, 												
  							@startDate nvarchar(50)=''''0'''', 												
  							@enddate nvarchar(50)=''''99999999'''''';            	
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
begin 	 	  		 
	SET @SQLString =  					''SELECT     round(convert(Float,ISNULL(TBL_TRA.MojoodiTra,0)+ (case when dbo.F1StartDate()>=@startDate then SUM(dbo.T_Kala.Mojodi) else 0 end)),2) AS Mande, ISNULL(TBL_TRA.Varde, 0) AS Varde, ISNULL(TBL_TRA.Sadre, 0)  							AS Sadre, dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, dbo.T_NameKala.KalaNumber AS KalaNumber,  
								dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib, dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode,  							dbo.T_Kala.NameKalaID, TBL_TRA.MojoodiTra 							FROM         dbo.T_NameKala INNER JOIN 							dbo.T_NameVahed ON dbo.T_NameKala.VAHED1 = dbo.T_NameVahed.VAHED1 INNER JOIN 	
														dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN 							dbo.T_NameVahed T_NameVahed_2 ON dbo.T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN 							(SELECT     KALAID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj)  
																					+ SUM(TedadBarKh) + SUM(TedadKhA)) AS MojoodiTra, SUM(TedadKh + TedadVorood + TedadBarFo + TedadVA) AS Varde,  							SUM(TedadFo + TedadKhorooj + TedadBarKh + TedadKhA) AS Sadre 							FROM         dbo.[TRANSACTION] Where ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) '' 	
set @ParmAndstr='' AND '' 			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end                                
set @SQLString=@SQLString + @WhrStatement+'' GROUP BY KALAID) TBL_TRA ON dbo.T_Kala.NameKalaID = TBL_TRA.KALAID WHERE     (dbo.T_Kala.VKalaID = 1) ''  

set @WhrStatement='''' 			 			
set @ParmAndstr='' AND ''   			
if(NOT (@IDA  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @IDA )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			
if(NOT (@IdKala  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @IdKala )''; 				
	set @ParmAndstr='' AND '' 			
end 			 			 			
if(NOT (@KalaGO  IS NULL)) 			
begin 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @KalaGO )''; 				
	set @ParmAndstr='' AND '' 			
end 			   			
set @SQLString=@SQLString + @WhrStatement + N''  GROUP BY dbo.T_NameVahed.NameVahed, T_NameVahed_2.NameVahed, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.Zarib,                        
												dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode, dbo.T_Kala.NameKalaID, ISNULL(TBL_TRA.Varde, 0),                        
												ISNULL(TBL_TRA.Sadre, 0), TBL_TRA.MojoodiTra Having round(convert(Float,ISNULL(TBL_TRA.MojoodiTra,0)+ (case when dbo.F1StartDate()>=@startDate then SUM(dbo.T_Kala.Mojodi) else 0 end)),2)<0'' 	 	 			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IDA=@IDA,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@enddate=@enddate;  		
end                                                  
return end                      
' 
END

GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_SoratHesab]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

  CREATE PROCEDURE [Rep_SoratHesab] ( 
@THESABID int,
@HESABID int,
@KOLID int,
@GoID INT,
@Date1 NVARCHAR(10),
@Date2 NVARCHAR(10)
  )           
  AS     
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';                  	
  SET @ParmDefinition = N''@THESABID int=null, 								
						@HESABID int=null, 								
						@KOLID  int=null, 								
						@GoID int=null,
						@Date1 NVARCHAR(10)=null,
						@Date2 NVARCHAR(10)=null  '';            	
	
	IF(NOT (@THESABID IS NULL))	 
		SET @SQLString =  '' 
SELECT     ISNULL(RadifSAID2, ISNULL(radif, 1000)) AS RadifSAID2, NameHesabmoin, ISNULL(NameKala2, SharhRadif) AS SharhRadif, MandeHesab, Bedehkar, Bestankar, Tarikh, ShomareSanad, HesabID, 
                      CodeHesab, srtfield, tedad, unitprice, NameVahed, NameKala, MatnFi, SAIDType, NoeAmaliyat, TedadSadere, TedadVarede
FROM         dbo.F_SoratHesabTafsili(@Date1) AS F_SoratHesabTafsili_1
WHERE     (Tarikh BETWEEN @Date1 AND @Date2) AND (HesabID = @THESABID)
ORDER BY Tarikh
          
''
        
   else IF(NOT (@HESABID IS NULL))	 
		SET @SQLString =''SELECT     ISNULL(RadifSAID2, ISNULL(radif, 1000)) AS RadifSAID2, NameHesabmoin, ISNULL(NameKala2, SharhRadif) AS SharhRadif, MandeHesab, Bedehkar, Bestankar, Tarikh, ShomareSanad, HesabID, CodeHesab, srtfield, tedad, unitprice, NameVahed, NameKala, 
                      MatnFi, SAIDType, NoeAmaliyat, TedadSadere, TedadVarede
FROM         dbo.F_SoratHesabMoein(@Date1) AS F_SoratHesabMoein_1
WHERE     (Tarikh BETWEEN @Date1 AND @Date2) AND (HesabID = @HESABID) '' 	
	else IF(NOT (@KOLID IS NULL))	 
		SET @SQLString =''
		SELECT     ISNULL(RadifSAID2, ISNULL(radif, 1000)) AS RadifSAID2, NameHesabmoin, ISNULL(NameKala2, SharhRadif) AS SharhRadif, MandeHesab, Bedehkar, Bestankar, Tarikh, ShomareSanad, HesabID, CodeHesab, srtfield, tedad, unitprice, NameVahed, NameKala, 
                      MatnFi, SAIDType, NoeAmaliyat, TedadSadere, TedadVarede
FROM        dbo.F_SoratHesabKol(@Date1) AS F_SoratHesabKol_1 
WHERE     (Tarikh BETWEEN @Date1 AND @Date2) AND (HESABID = @KOLID)
'' 	
  /* -----------------------نشان دادن کالاهای فعال */ 
	
/*if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N'' HAVING      (T_NameKala.Active = 0) ''; 					
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N'' HAVING      (T_NameKala.Active > -1000) ''; 	
               
  /* -----------------------شرط انبار */ 			
set @ParmAndstr='' AND '' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (T_Kala.AID = @AID ) ''; 				
set @ParmAndstr='' AND '' 			
END 			
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr='' AND '' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (T_Kala.GOKID = @GoKID ) ''; 				
	set @ParmAndstr='' AND '' 			
END 	
*/
						

set @SQLString=@SQLString 	
     			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@THESABID=@THESABID ,@HESABID=@HESABID,@KOLID=@KOLID,@GoID=@GoID,@Date1=@Date1,@Date2=@Date2;  		
return END 


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_SooDRooZaneh9048]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  Create PROCEDURE [Rep_SooDRooZaneh9048] ( 
@USERID int,
@Date1 NVARCHAR(10),
@Date2 NVARCHAR(10)
  )           
  AS     
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';                  	
  SET @ParmDefinition = N''@USERID int=null, 								
						@Date1 NVARCHAR(10)=null,
						@Date2 NVARCHAR(10)=null'';            	

		SET @SQLString =''
			SELECT     SUM([TRANSACTION].PARBes) AS PARBes, SUM([TRANSACTION].DARBed) AS DARBed, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab
			FROM         [TRANSACTION] INNER JOIN
								  T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID
			WHERE     ([TRANSACTION].GOID = 8) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2) AND (ISNULL([TRANSACTION].HESABID, 0) <> 85)
			GROUP BY dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID)

			  
			   UNION ALL
			   
			SELECT     SUM([TRANSACTION].PARBes) AS PARBes, SUM([TRANSACTION].DARBed) AS DARBed, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab
			FROM         [TRANSACTION] INNER JOIN
								  T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID
			WHERE     ([TRANSACTION].GOID = 9) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2) AND (ISNULL([TRANSACTION].HESABID, 0) <> 85)
			GROUP BY dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 

			   UNION ALL
			            
			SELECT     SUM(CONVERT(float, SubTransactionKala_1.TedadFo * (SubTransactionKala_1.UnitpriceFo - Fun_SubAkharinKharid_1.MaxUnitPriceKh - ISNULL(FunInfoKalaAll_1.UnitPrice2, 0)))) AS Parbes, 
								  SUM(0) AS Expr2, N''''سود حاصل از فروش'''' AS Expr3
			FROM         dbo.SubTransactionKala() AS SubTransactionKala_1 INNER JOIN
								  dbo.Fun_SubAkharinKharid(1) AS Fun_SubAkharinKharid_1 ON SubTransactionKala_1.KALAID = Fun_SubAkharinKharid_1.KALAID INNER JOIN
								  dbo.FunInfoKalaAll(1) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.KALAID = FunInfoKalaAll_1.ID
			WHERE     (SubTransactionKala_1.TedadFo > 0) AND (SubTransactionKala_1.Tarikh BETWEEN @Date1 AND @Date2)
'' 	
 
						

set @SQLString=@SQLString 	
     			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@USERID=@USERID ,@Date1=@Date1,@Date2=@Date2;  		
return END 

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[RepFST_0523894024]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [RepFST_0523894024] ( 
@NameKalaID int,
@TxtNameKalaID NVARCHAR(255),
@AID int,
@GOKID int,
@Date1 NVARCHAR(10),
@Date2 NVARCHAR(10)
  )           
  AS     
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'''';         	
  set @ParmAndstr = N'''';                  	
  SET @ParmDefinition = N''@NameKalaID int=null, 
						@TxtNameKalaID NVARCHAR(255)=null,								
						@AID int=null, 								
						@GOKID  int=null, 								
						@Date1 NVARCHAR(10)=null,
						@Date2 NVARCHAR(10)=null  '';            	
	

  	if(@Date1 IS NULL) 			   
	BEGIN 				   
		set @Date1=''0''; 				   
	END   
	
	      
	if(@Date2 IS NULL) 			   
	BEGIN 				   
		set @Date2=''99999999''; 				   
	END   
	
		SET @SQLString =  '' 
SELECT     TRaKalaid.KALAID, T_NameGoKala.NameGoKalanumbr,T_NameGoKala.NameGoKala1, T_Anbar.AIDNumber, T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, 
                      T_NameVahed.NameVahed, SUM(TRaKalaid.Varde) AS Varde, SUM(TRaKalaid.Sadre) AS Sadre, SUM(TRaKalaid.Varde - TRaKalaid.Sadre) AS Mande
FROM         T_Anbar INNER JOIN
                          (SELECT     KALAID, AID, GOKID, TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, Date1
                             FROM         [TRANSACTION]
                             WHERE     (NOT (KALAID IS NULL))
                             UNION ALL
                             SELECT     NameKalaID, AID, GOKID, Mojodi AS Varde, 0 AS Sadre, dbo.F1StartDate() AS Date12
                             FROM         T_Kala
                             WHERE     (Mojodi <> 0)) AS TRaKalaid INNER JOIN
                      T_NameVahed INNER JOIN
                      T_NameKala ON T_NameVahed.VAHED1 = T_NameKala.VAHED1 ON TRaKalaid.KALAID = T_NameKala.ID INNER JOIN
                      T_NameGoKala ON TRaKalaid.GOKID = T_NameGoKala.ID ON T_Anbar.AID = TRaKalaid.AID  WHERE (Date1 BetWeen @Date1 and  @Date2) 
          
''


set @ParmAndstr='' AND '' 			
if(NOT (@NameKalaID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (NameKalaID  = @NameKalaID  ) ''; 				
set @ParmAndstr='' AND '' 			
END 

set @ParmAndstr='' AND '' 			
if(NOT (@TxtNameKalaID   IS NULL)) 			
BEGIN
	set @TxtNameKalaID =''%'' + @TxtNameKalaID + ''%''	 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (NameKala1  Like @TxtNameKalaID ) ''; 				
set @ParmAndstr='' AND '' 			
END 

set @ParmAndstr='' AND '' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (TRaKalaid.AID = @AID ) ''; 				
set @ParmAndstr='' AND '' 			
END 			
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr='' AND '' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (GOKID = @GoKID ) ''; 				
	set @ParmAndstr='' AND '' 			
END 	

		

set @SQLString=@SQLString +@WhrStatement+	'' GROUP BY TRaKalaid.KALAID, T_NameGoKala.NameGoKalanumbr,T_NameGoKala.NameGoKala1, T_Anbar.AIDNumber, T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, 
                      T_NameVahed.NameVahed	HAVING      (SUM(TRaKalaid.Varde - TRaKalaid.Sadre) <> 0)''	
     			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@nameKalaID=@nameKalaID ,@TxtNameKalaID=@TxtNameKalaID,@aiD=@aiD,@gokID=@gokID,@Date1=@Date1,@Date2=@Date2;  		
return END 



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_sood_Sub]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'          CREATE PROCEDURE [Rep_sood_Sub](@StartDate nvarchar(50), @EndDate nvarchar(50)) AS SELECT     @StartDate AS startdate, @EndDate AS enddate  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_05002300900045]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'Create PROCEDURE [std_05002300900045] AS select 1




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_05002300900044]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'Create PROCEDURE [std_05002300900044] AS select 1




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_05002300900043]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'Create PROCEDURE [std_05002300900043] AS select 1




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_05002300900042]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'Create PROCEDURE [std_05002300900042] AS select 1




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900410033]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'Create PROCEDURE [std_050023008900410033] AS select 1




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0215]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
   CREATE PROCEDURE [Rep_0215] (   
   	@SortRep nvarchar(255),  	
   	@Goid int, 	
   	@KolID int,  	
   	@HesabID int, 	
   	@THesabID int,  	
   	@ShChk nvarchar(100), 	
   	@VazChk nvarchar(50), 	
   	@StartDateSodor nvarchar(50)=''0'', 	
   	@EnddateSodor nvarchar(50)=''99999999'', 	
   	@StartdateRes nvarchar(50)=''0'', 	
   	@EnddateRes nvarchar(50)=''99999999'' )              AS        
   	BEGIN 	SET NOCOUNT ON; 	 	   
   	DECLARE @SQLString  NVARCHAR(4000);  	   
   	DECLARE @WhrStatement  NVARCHAR(2000);         	   
   	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
   	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
   	set @WhrStatement  = N'''';         	   
   	set @ParmAndstr = N'''';                  	   
   	SET @ParmDefinition = N''@SortRep nvarchar(255)=null,  							
   							@Goid int=null, 							
   							@KolID int=null,  							
   							@HesabID int=null, 							
   							@THesabID int=null,  							
   							@ShChk nvarchar(100)=null, 							
   							@VazChk nvarchar(50)=null, 							
   							@StartDateSodor nvarchar(50)=''''0'''', 							
   							@EnddateSodor nvarchar(50)=''''99999999'''', 							
   							@StartdateRes nvarchar(50)=''''0'''', 							
   							@EnddateRes nvarchar(50)=''''99999999'''''';            	   
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		    
BEGIN         
	if(@StartDateSodor IS NULL) 			   
	BEGIN 				   
		set @StartDateSodor=''0''; 				   
	END         
	
	if(@EnddateSodor IS NULL) 			   
	BEGIN 				   
		set @EnddateSodor=''99999999''; 				   
	END         if(@StartdateRes IS NULL) 			   
	BEGIN 				   
		set @StartdateRes=''0''; 				   
	END         
	if(@EnddateRes IS NULL) 			   
	BEGIN 				   
		set @EnddateRes=''99999999''; 				   
	END     	  		    
	
	SET @SQLString =''SELECT     ASNADDARID, Sharh, Mablagh, DateTA, DateVO, ShSeryal, FieldN1, GOID, KOLID, HESABID , THESABID, dbo.HesabName(KOLID, HESABID, THESABID) AS Daryaft_az_Hesab,                        CASE WHEN (T_Asnad_Dar.[FieldN1]) = 1 THEN ''''پاس شده'''' WHEN (T_Asnad_Dar.[FieldN1]) = 2 THEN ''''برگشتي '''' WHEN (T_Asnad_Dar.[FieldN1])                        = 3 THEN ''''در جریان وصول '''' WHEN (T_Asnad_Dar.[FieldN1]) = 4 THEN ''''پاس نشده'''' WHEN (T_Asnad_Dar.[FieldN1]) = 5 THEN ''''پاس شده '''' WHEN (T_Asnad_Dar.[FieldN1])                        = 6 THEN '''' برگشتی'''' END AS chkvaz, DARID3 					FROM         T_Asnad_Dar WHERE     (DateVO BETWEEN @StartdateRes AND @EnddateRes) and (DateTA BETWEEN @StartDateSodor AND @EnddateSodor)    '' 	    		    
	/* --------------------شرط حساب سرگروه روی ساب*/ 			   
	set @ParmAndstr='' AND '' 			   
	if(NOT (@Goid IS NULL)) 			   
	BEGIN 				   
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(Goid = @Goid )''; 				  
		set @ParmAndstr='' AND '' 			   
	END                 
	
	/* --------------------شرط حساب کل  */ 			    
	
	set @ParmAndstr='' AND '' 			    
	if(NOT (@KolID IS NULL)) 			    
	BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(KolID = @KolID )''; 				    
		set @ParmAndstr='' AND '' 			    
	END                 
	
	/*  --------------------شرط حساب معین  */ 			    
	set @ParmAndstr='' AND '' 			    
	if(NOT (@HesabID  IS NULL)) 			    
	BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(HesabID = @HesabID )''; 				    
		set @ParmAndstr='' AND '' 			    
	END                  
	
	/* --------------------   شرط حساب تفصیلی */ 			     
	set @ParmAndstr='' AND '' 			     
	if(NOT (@THesabID  IS NULL)) 			     
	BEGIN 				     
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''(THesabID = @THesabID )''; 				     
		set @ParmAndstr='' AND '' 			     
	END                    		
	
	/* --------------------   وضعیت  چک */ 			     
	set @ParmAndstr='' AND '' 			     
	if(NOT (@VazChk  IS NULL)) 			     
	BEGIN 				     
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (FieldN1= @VazChk ) ''; 				     
		set @ParmAndstr='' AND '' 			     
	END    		
	
	/* --------------------   شماره سریال چک */ 			     
	set @ParmAndstr='' AND '' 			     
	if(NOT (@ShChk  IS NULL)) 			     
	BEGIN 				     
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (ShSeryal = @ShChk ) ''; 				     
		set @ParmAndstr='' AND '' 			     
	END    		
	
	/* --------------------  شماره سریال  1 مرتب سازی */ 			     
	if(@SortRep=N''ShSeryal'') 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY ShSeryal ''; 				     
	END  	 		
	
	/* --------------------   تاریخ صدور  2 مرتب سازی */ 			 			     
	if(@SortRep=N''DateTA'') 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement + N''  ORDER BY DateTA ''; 				     
	END    	    
	
	/* --------------------   تاریخ سررسید  3 مرتب سازی */ 			     
	if(@SortRep=N''DateVO'') 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement + N''  ORDER BY DateVO ''; 				     
	END    		    
	
	/* --------------------   تاریخ سررسید  3 مرتب سازی */ 			     
	if(@SortRep  IS NULL) 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement + N''  ORDER BY DateVO ''; 				     
	END           			          
	EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@ShChk=@ShChk,@VazChk=@VazChk,@StartDateSodor=@StartDateSodor,@EnddateSodor=@EnddateSodor,@StartdateRes=@StartdateRes,@EnddateRes=@EnddateRes;  		     
END 		      
/* --------پایان چاپ*/  		 		  

return END 
' 
END
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030522]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
          CREATE PROCEDURE [Rep_030522](
          	                  
          
              	@startDate nvarchar(50), 
        	@enddate nvarchar(50), 
        	@GOID INT,
        	@KOLID INT,
        	@HESABID INT,
        	@THESABID int) AS 
BEGIN        	
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'''';         	   
  	set @ParmAndstr = N'''';     
  	   
  	 SET @ParmDefinition = N''@startDate nvarchar(50)=''''0'''', 
							@enddate nvarchar(50)=''''99999999'''', 
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null''; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate=''0''; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate=''99999999''; 				   
	END   
	        
set @SQLString=N''            SELECT      GOID, KOLID, HESABID, THESABID, SUM(SumOfPARBes) AS SumOfSumOfPARBes, SUM(SumOfDARBed) AS SumOfSumOfDARBed, SUM(mande) AS SumOfmande, 
                      MIN(MinOfDate1) AS MinOfDate1, MAX(MaxOfDate1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab, @startdate AS startdate, @enddate AS enddate
FROM         dbo.Q_Bed_Bes(@startdate, @enddate) AS Q_Bed_Bes
WHERE     (mande < 0) ''


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].GOID = @Goid )''; 				   
		set @ParmAndstr='' AND '' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].KolID = @KolID )''; 				    
   	set @ParmAndstr='' AND '' 			    
   END              
   /*  --------------------شرط حساب معین  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].HESABID = @HESABID )''; 				    
   	set @ParmAndstr='' AND '' 			    
   END          
   /*  --------------------شرط حساب تفصیلی  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].THESABID = @THESABID )''; 				    
   	set @ParmAndstr='' AND '' 			    
   END 
   
   
   set @SQLString= @SQLString + @WhrStatement  + N'' GROUP BY GOID, KOLID, HESABID, THESABID, dbo.HesabName(KOLID, HESABID, THESABID)   ''



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID;  		     

END      
 

 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030521]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
          CREATE PROCEDURE [Rep_030521](
          	
              	@startDate nvarchar(50), 
        	@enddate nvarchar(50), 
        	@GOID INT,
        	@KOLID INT,
        	@HESABID INT,
        	@THESABID int) AS 
BEGIN        	
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'''';         	   
  	set @ParmAndstr = N'''';     
  	   
  	 SET @ParmDefinition = N''@startDate nvarchar(50)=''''0'''', 
							@enddate nvarchar(50)=''''99999999'''', 
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null''; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate=''0''; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate=''99999999''; 				   
	END   
	        
set @SQLString=N'' SELECT      GOID, KOLID, HESABID, THESABID, SUM(SumOfPARBes) AS SumOfSumOfPARBes, SUM(SumOfDARBed) AS SumOfSumOfDARBed, SUM(mande) AS SumOfmande, 
                      MIN(MinOfDate1) AS MinOfDate1, MAX(MaxOfDate1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab, @startdate AS startdate, @enddate AS enddate
FROM         dbo.Q_Bed_Bes(@startdate, @enddate) AS Q_Bed_Bes
WHERE     (mande < 0) ''


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].GOID = @Goid )''; 				   
		set @ParmAndstr='' AND '' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].KolID = @KolID )''; 				    
   	set @ParmAndstr='' AND '' 			    
   END              
   /*  --------------------شرط حساب معین  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].HESABID = @HESABID )''; 				    
   	set @ParmAndstr='' AND '' 			    
   END          
   /*  --------------------شرط حساب تفصیلی  */ 			    
   set @ParmAndstr='' AND '' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N''([Q_Bed_Bes].THESABID = @THESABID )''; 				    
   	set @ParmAndstr='' AND '' 			    
   END 
   
   
   set @SQLString= @SQLString + @WhrStatement  + N'' GROUP BY GOID, KOLID, HESABID, THESABID, dbo.HesabName(KOLID, HESABID, THESABID) ''
						
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID;  		     

END 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030212]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
   CREATE PROCEDURE [Rep_030212] (  	@SortRep nvarchar(255),  	@NoeSortRep nvarchar(255), 	@Goid int, 	@KolID int,  	@HesabID nvarchar(50), 	@THesabID int,  	@StartDate nvarchar(50)=''0'', 	@Enddate nvarchar(50)=''99999999'', 	@StartSanad nvarchar(50), 	@EndSanad nvarchar(50) )              AS        BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	   DECLARE @WhrStatement  NVARCHAR(2000);         	   DECLARE @ParmDefinition  NVARCHAR(2000);         	   DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   set @WhrStatement  = N'''';         	   set @ParmAndstr = N'''';                  	   SET @ParmDefinition = N''@SortRep nvarchar(255)=null,  							@NoeSortRep nvarchar(255)=null, 							@Goid int=null, 							@KolID int=null,  							@HesabID nvarchar(50)=null, 							@THesabID int=null, 							@StartDate nvarchar(50)=''''0'''', 							@Enddate nvarchar(50)=''''99999999'''', 							@StartSanad nvarchar(50)=null, 							@EndSanad nvarchar(50)=null'';            	   /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		    BEGIN       if(@StartDate IS NULL) 			   BEGIN 				   set @StartDate=''0''; 				   END         if(@Enddate IS NULL) 			   BEGIN 				   set @Enddate=''99999999''; 				   END         if(@NoeSortRep IS NULL) 			   BEGIN 				   set @NoeSortRep=0; 				   END        	  		    SET @SQLString =''SELECT     @Enddate as endDate,@StartDate as startDate,[TRANSACTION].GOID, [TRANSACTION].KOLID, T_KolHesab.KolNumber, T_KolHesab.NameHesab, SUM([TRANSACTION].PARBes) AS SumOfPARBes, SUM([TRANSACTION].DARBed)                        AS SumOfDARBed, SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes) AS mande, dbo.F1StartDate() AS MinOfDate1, dbo.F1EndDate() AS MaxOfDate1, CASE WHEN (CONVERT(money,                        CONVERT(money, SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed)                        - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes, CASE WHEN SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes)                        > 0 THEN SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes) END AS MandeBed, CASE WHEN SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes)                        < 0 THEN SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes) END AS MandeBes  FROM         [TRANSACTION] INNER JOIN                       T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN                       T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 WHERE     ([TRANSACTION].Date1 BETWEEN @StartDate AND @Enddate) ''               /*  --------------------شرط حساب معين  */ 			    set @ParmAndstr='' AND '' 			    if(NOT (@HesabID  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].HesabID = @HesabID )''; 				    set @ParmAndstr='' AND '' 			    END                  /* --------------------   شرط حساب تفصيلي */ 			     set @ParmAndstr='' AND '' 			     if(NOT (@THesabID  IS NULL)) 			     BEGIN 				     set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].THesabID = @THesabID )''; 				     set @ParmAndstr='' AND '' 			     END                    		/* --------------------     محدوده سند */ 			     set @ParmAndstr='' AND '' 			     if((NOT (@StartSanad  IS NULL)) and (NOT (@EndSanad  IS NULL)) ) 			     BEGIN 				     set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (T_SA2.SAID2Number BETWEEN @StartSanad AND @EndSanad) ''; 				     set @ParmAndstr='' AND '' 			     END                           	set @SQLString=@SQLString + @WhrStatement + N'' GROUP BY [TRANSACTION].GOID, [TRANSACTION].KOLID, T_KolHesab.KolNumber, T_KolHesab.NameHesab HAVING      ([TRANSACTION].KOLID IS NOT NULL) '';    /* --------------------شرط حساب سرگروه روي ساب*/ 			 	   if(NOT (@Goid IS NULL)) 			   BEGIN 				   set @SQLString=@SQLString +  N''  And ([TRANSACTION].GOID = @Goid ) ''; 				   END                    /* --------------------شرط حساب کل  */ 			    if(NOT (@KolID IS NULL)) 			    BEGIN 				    set @SQLString=@SQLString +  N'' And ([TRANSACTION].KolID = @KolID ) ''; 				 	    END             	/* --------------------  نام حساب  1 مرتب سازي */ 			     if ((@SortRep=1))     		     BEGIN      	 		if ((@NoeSortRep=1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY T_KolHesab.NameHesab DESC ''; 	 		END 		 		if ((@NoeSortRep<>1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY T_KolHesab.NameHesab  ''; 	 		END 	    		     END  	 	/* --------------------  کد حساب  2 مرتب سازي */ 			     if ((@SortRep=2))     		     BEGIN      	 		if ((@NoeSortRep=1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY T_KolHesab.KolNumber DESC ''; 	 		END 		 		if ((@NoeSortRep<>1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY T_KolHesab.KolNumber  ''; 	 		END 	    		     END       			          EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@NoeSortRep=@NoeSortRep,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@StartDate=@StartDate,@Enddate=@Enddate,@StartSanad=@StartSanad,@EndSanad=@EndSanad;  		     END 		      /* --------پايان چاپ*/  		 		  return END 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030210]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
   CREATE PROCEDURE [Rep_030210] (  	@SortRep nvarchar(255),  	@NoeSortRep nvarchar(255), 	@Goid int, 	@KolID int,  	@HesabID nvarchar(50), 	@THesabID int,  	@StartDate nvarchar(50)=''0'', 	@Enddate nvarchar(50)=''99999999'', 	@StartSanad nvarchar(50), 	@EndSanad nvarchar(50) )              AS        BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	   DECLARE @WhrStatement  NVARCHAR(2000);         	   DECLARE @ParmDefinition  NVARCHAR(2000);         	   DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   set @WhrStatement  = N'''';         	   set @ParmAndstr = N'''';                  	   SET @ParmDefinition = N''@SortRep nvarchar(255)=null,  							@NoeSortRep nvarchar(255)=null, 							@Goid int=null, 							@KolID int=null,  							@HesabID nvarchar(50)=null, 							@THesabID int=null, 							@StartDate nvarchar(50)=''''0'''', 							@Enddate nvarchar(50)=''''99999999'''', 							@StartSanad nvarchar(50)=null, 							@EndSanad nvarchar(50)=null'';            	   /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		    BEGIN       if(@StartDate IS NULL) 			   BEGIN 				   set @StartDate=''0''; 				   END         if(@Enddate IS NULL) 			   BEGIN 				   set @Enddate=''99999999''; 				   END         if(@NoeSortRep IS NULL) 			   BEGIN 				   set @NoeSortRep=0; 				   END        	  		    SET @SQLString =''SELECT     @Enddate as endDate,@StartDate as startDate , [TRANSACTION].GOID, [TRANSACTION].KOLID, [TRANSACTION].HESABID, SUM([TRANSACTION].PARBes) AS SumOfPARBes, SUM([TRANSACTION].DARBed) AS SumOfDARBed, CONVERT(money,                        SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes)) AS mande, MIN(dbo.F1StartDate()) AS MinOfDate1, MAX(dbo.F1EndDate()) AS MaxOfDate1, CASE WHEN (CONVERT(money,                        CONVERT(money, SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS bed, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed)                        - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS bes, dbo.HesabName([TRANSACTION].KOLID,                        [TRANSACTION].HESABID, NULL) AS namehesab, dbo.HesabNamecod( [TRANSACTION].KOLID, [TRANSACTION].HESABID, NULL) AS Hesabnumber FROM         [TRANSACTION] LEFT OUTER JOIN                       T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 WHERE     ([TRANSACTION].Date1 BETWEEN @StartDate AND @Enddate) ''                            /* --------------------   شرط حساب تفصيلي */ 			     set @ParmAndstr='' AND '' 			     if(NOT (@THesabID  IS NULL)) 			     BEGIN 				     set @WhrStatement= @WhrStatement + @ParmAndstr + N''([TRANSACTION].THesabID = @THesabID )''; 				     set @ParmAndstr='' AND '' 			     END                    		/* --------------------     محدوده سند */ 			     set @ParmAndstr='' AND '' 			     if((NOT (@StartSanad  IS NULL)) and (NOT (@EndSanad  IS NULL)) ) 			     BEGIN 				     set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (T_SA2.SAID2Number BETWEEN @StartSanad AND @EndSanad) ''; 				     set @ParmAndstr='' AND '' 			     END    	set @SQLString=@SQLString + @WhrStatement + N'' GROUP BY [TRANSACTION].GOID, [TRANSACTION].KOLID, [TRANSACTION].HESABID, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, NULL), dbo.HesabNameCod([TRANSACTION].KOLID, [TRANSACTION].HESABID, NULL) HAVING      (NOT ([TRANSACTION].HESABID IS NULL))'';    /* --------------------شرط حساب سرگروه روي ساب*/ 			 	   if(NOT (@Goid IS NULL)) 			   BEGIN 				   set @SQLString=@SQLString +  N''  And ([TRANSACTION].GOID = @Goid ) ''; 				   END                    /* --------------------شرط حساب کل  */ 			    if(NOT (@KolID IS NULL)) 			    BEGIN 				    set @SQLString=@SQLString +  N'' And ([TRANSACTION].KolID = @KolID ) ''; 				 	    END              /* --------------------شرط حساب معين  */ 			    if(NOT (@HesabID IS NULL)) 			    BEGIN 				    set @SQLString=@SQLString +  N'' And ([TRANSACTION].HesabID = @HesabID ) ''; 				    END        	/* --------------------  نام حساب  1 مرتب سازي */ 			     if ((@SortRep=''NameHesab''))     		     BEGIN      	 		if ((@NoeSortRep=1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY namehesab DESC ''; 	 		END 		 		if ((@NoeSortRep<>1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY namehesab  ''; 	 		END 	    		     END  	 	/* --------------------  کد حساب  2 مرتب سازي */ 			     if ((@SortRep=''hesabid''))     		     BEGIN      	 		if ((@NoeSortRep=1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY Hesabnumber DESC ''; 	 		END 		 		if ((@NoeSortRep<>1) )		 		BEGIN 		 			set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY Hesabnumber  ''; 	 		END 	    		     END       			          EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@NoeSortRep=@NoeSortRep,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@StartDate=@StartDate,@Enddate=@Enddate,@StartSanad=@StartSanad,@EndSanad=@EndSanad;  		     END 		      /* --------پايان چاپ*/  		 		  return END 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0121]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
   CREATE PROCEDURE [Rep_0121] (   
   	@SortRep nvarchar(255),  	
   	@Goid int, 	
   	@KolID int,  	
   	@HesabID int, 	
   	@THesabID int,  	
   	@ShChk nvarchar(100), 	
   	@VazChk nvarchar(50), 	
   	@StartDateSodor nvarchar(50)=''0'', 	
   	@EnddateSodor nvarchar(50)=''99999999'', 	
   	@StartdateRes nvarchar(50)=''0'', 	
   	@EnddateRes nvarchar(50)=''99999999'' )              AS        
   	
   	BEGIN 	SET NOCOUNT ON; 	 	   
   	DECLARE @SQLString  NVARCHAR(4000);  	   
   	DECLARE @WhrStatement  NVARCHAR(2000);         	   
   	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
   	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
   	set @WhrStatement  = N'''';         	   
   	set @ParmAndstr = N'''';                  	   
   	SET @ParmDefinition = N''@SortRep nvarchar(255)=null,  							
   							@Goid int=null, 							
   							@KolID int=null,  							
   							@HesabID int=null, 							
   							@THesabID int=null,  							
   							@ShChk nvarchar(100)=null, 							
   							@VazChk nvarchar(50)=null, 							
   							@StartDateSodor nvarchar(50)=''''0'''', 							
   							@EnddateSodor nvarchar(50)=''''99999999'''', 							
   							@StartdateRes nvarchar(50)=''''0'''', 							
   							@EnddateRes nvarchar(50)=''''99999999'''''';            	   
/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		    
BEGIN         
	if(@StartDateSodor IS NULL) 			   
	BEGIN 				   
		set @StartDateSodor=''0''; 				   
	END         
	if(@EnddateSodor IS NULL) 			   
	BEGIN 				   
		set @EnddateSodor=''99999999''; 				   
	END         if(@StartdateRes IS NULL) 			   
	BEGIN 				   
		set @StartdateRes=''0''; 				   
	END         
	
	if(@EnddateRes IS NULL) 			   
	BEGIN 				   
		set @EnddateRes=''99999999''; 				   
	END     	  		    
	SET @SQLString =''SELECT     Sharh, ShSeryal, FieldN1, GOID, KOLID, HESABID, THESABID, DateSO, DateSR, HESABID2, THESABID2, chkvaz, Pardakht_az_Hesab, 
								ASNADID, Mablagh, Flag2, SharhSub, DarBed, variz_be_Hesab,                        HESABIDM, THESABIDM 
								FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, 
								T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2,                         
								T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN ''''پاس شده'''' WHEN (T_Asnad.[FieldN1]) = 2 THEN ''''باطل شده '''' 
								WHEN (T_Asnad.[FieldN1]) = 3 THEN ''''نقدی '''' WHEN (T_Asnad.[FieldN1]) = 4 THEN ''''پاس نشده '''' END AS chkvaz, 
								dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab,  T_Asnad.ASNADID, T_Asnad.Mablagh, 
								T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1,       
								[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, 
								dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID)AS variz_be_Hesab                         
								              FROM         T_Asnad INNER JOIN                                               
								              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2 WHERE     
								              ([TRANSACTION].DARBed > 0)                         UNION ALL                         
								              SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, 
								              T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR,                     
								              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN ''''پاس شده'''' 
								              WHEN (T_Asnad_1.[FieldN1]) = 2 THEN ''''باطل شده '''' WHEN (T_Asnad_1.[FieldN1]) = 3 THEN ''''نقدی '''' 
								              WHEN (T_Asnad_1.[FieldN1]) = 4 THEN ''''پاس نشده '''' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, 
								              T_Asnad_1.HESABID, T_Asnad_1.THESABID)                                                AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM,                                                Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab                         FROM         T_Asnad AS T_Asnad_1 INNER JOIN                                               Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number                         WHERE     (T_Asnad_1.Flag2 = 1)) AS SubAsnad WHERE     (DateSR BETWEEN @StartdateRes AND @EnddateRes) and (DateSO BETWEEN @StartDateSodor AND @EnddateSodor)    '' 	    		    
	
	/* --------------------شرط حساب سرگروه روی ساب*/ 			   
	set @ParmAndstr='' AND '' 			   
	if(NOT (@Goid IS NULL)) 			   
	BEGIN 				   
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''((Goid = @Goid))''; 				   
		set @ParmAndstr='' AND '' 			   
	END                 
	/* --------------------شرط حساب کل  */ 			    
	
	set @ParmAndstr='' AND '' 			    
	if(NOT (@KolID IS NULL)) 			    
	BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''((KolID = @KolID) )''; 				    
		set @ParmAndstr='' AND '' 			    
	END                 
	/*  --------------------شرط حساب معین  */ 			   
	set @ParmAndstr='' AND '' 
				    
	if(NOT (@HesabID  IS NULL)) 			    
	BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''((HesabID = @HesabID) or (HESABIDM = @HesabID))''; 				    
		set @ParmAndstr='' AND '' 			   
	END                  
		
		/* --------------------   شرط حساب تفصیلی */ 			     
		
	set @ParmAndstr='' AND '' 			     
	if(NOT (@THesabID  IS NULL)) 			     
	BEGIN 				     
		set @WhrStatement= @WhrStatement + @ParmAndstr + N''((THesabID = @THesabID) or (THESABIDM = @THesabID) )''; 				     
		set @ParmAndstr='' AND '' 			     
	END                    		
	/* --------------------   وضعیت  چک */ 			     
	set @ParmAndstr='' AND '' 			     
	if(NOT (@VazChk  IS NULL)) 			     
	BEGIN 				     
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (FieldN1= @VazChk ) ''; 				     
		set @ParmAndstr='' AND '' 			     
	END    		
	/* --------------------   شماره سریال چک */ 			     
	set @ParmAndstr='' AND '' 			     
	if(NOT (@ShChk  IS NULL)) 			     
	BEGIN 				     
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'' (ShSeryal = @ShChk ) ''; 				     
		set @ParmAndstr='' AND '' 			     
	END    		
	/* --------------------  شماره سریال  1 مرتب سازی */ 			     
	if(@SortRep=N''ShSeryal'') 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement  + N''  ORDER BY ShSeryal ''; 				     
	END  	 		
	/* --------------------   تاریخ صدور  2 مرتب سازی */ 			 			     
	if(@SortRep=N''DateSO'') 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement + N''  ORDER BY DateSO ''; 				     
	END    	    
	
	/* --------------------   تاریخ سررسید  3 مرتب سازی */ 			     
	if(@SortRep=N''DateSR'') 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement + N''  ORDER BY DateSR ''; 				     
	END    		    
	/* --------------------   تاریخ سررسید  3 مرتب سازی */ 			     
	if(@SortRep  IS NULL) 			     
	BEGIN 				     
		set @SQLString= @SQLString + @WhrStatement + N''  ORDER BY DateSR ''; 				     
	END           			          
	EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@ShChk=@ShChk,@VazChk=@VazChk,@StartDateSodor=@StartDateSodor,@EnddateSodor=@EnddateSodor,@StartdateRes=@StartdateRes,@EnddateRes=@EnddateRes;  		     
END 		      

/* --------پایان چاپ*/  		 		  

return END      
' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListHesab_SanadHesabdari]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [QListHesab_SanadHesabdari](@Sath int=1,@Goid int=0,@Kolid int=0,@Hesabid int=0)
AS 

SET NOCOUNT ON;

DECLARE @SQLString  NVARCHAR(4000);
DECLARE @WhrStatement  NVARCHAR(2000);
DECLARE @ParmAndstr  NVARCHAR(20);

	set @WhrStatement  = N'''';        

if(@Sath=1)
begin
	set @SQLString=N''
		SELECT     T_GropHesab.GoID AS ACCID, T_GropHesab.GoID, T_GropHesab.GoNumber AS CodeACC, T_GropHesab.NameGo AS NameACC, T_GropHesab.GoNumber AS [كد حساب], 
                      T_GropHesab.NameGo AS [عنوان حساب],CONVERT(money, ABS(ISNULL(QMande_GoHesab_SanadHesabdary.DARBed, 
							  0) - ISNULL(QMande_GoHesab_SanadHesabdary.PARBes, 0)), 1) AS [مانده حساب] ,CASE WHEN (ISNULL((darbed - parbes), 0)) > 0 THEN N''''بد'''' WHEN (ISNULL((darbed - parbes), 0)) 
                      < 0 THEN N''''بس'''' ELSE ''''--'''' END AS [تشخيص .], N'''''''' AS Expr1
		FROM         T_GropHesab LEFT OUTER JOIN
								  (SELECT     SUM(PARBes) AS PARBes, SUM(DARBed) AS DARBed, GOID
									 FROM         [TRANSACTION]
									 WHERE     (NOT (SAID2 IS NULL))
									 GROUP BY GOID, GOID) AS QMande_GoHesab_SanadHesabdary ON T_GropHesab.GoID = QMande_GoHesab_SanadHesabdary.GOID
		ORDER BY T_GropHesab.NameGo''
	
end
else if(@Sath=2)
begin
	
	if((@Goid <> 0))
	begin
		set @WhrStatement= N''(dbo.T_KolHesab.GOID = ''+ CONVERT(nvarchar(50), @Goid ) +'')'';
	end
		
	if(len (@WhrStatement)>0)
	begin
		set @WhrStatement=N'' WHERE ''+ @WhrStatement ;
	end

	set @SQLString=N''
		SELECT     dbo.T_KolHesab.KOLID AS ACCID, dbo.T_KolHesab.GOID, dbo.T_KolHesab.KolNumber AS CodeACC, dbo.T_KolHesab.NameHesab AS NameACC, 
							  dbo.T_KolHesab.KolNumber AS [كد حساب], dbo.T_KolHesab.NameHesab AS [عنوان حساب], CONVERT(money, ABS(ISNULL(QMande_GoHesab_SanadHesabdary.DARBed, 
							  0) - ISNULL(QMande_GoHesab_SanadHesabdary.PARBes, 0)), 1) AS [مانده حساب], CASE WHEN (ISNULL((darbed - parbes), 0)) 
							  > 0 THEN N''''بد'''' WHEN (ISNULL((darbed - parbes), 0)) < 0 THEN N''''بس'''' ELSE ''''--'''' END AS [تشخيص .], NULL AS Expr1
		FROM         dbo.T_KolHesab LEFT OUTER JOIN
								  (SELECT     SUM(PARBes) AS PARBes, SUM(DARBed) AS DARBed, KOLID
									 FROM         [TRANSACTION]
									 WHERE     (NOT (SAID2 IS NULL))
									 GROUP BY KOLID, KOLID) QMande_GoHesab_SanadHesabdary ON dbo.T_KolHesab.KOLID = QMande_GoHesab_SanadHesabdary.KOLID
		''+ @WhrStatement + ''
		ORDER BY NameACC''

end
else if(@Sath=3)
begin
	
	if((@Goid <> 0))
	begin
		set @WhrStatement= N''(dbo.T_Heasb.GOID = ''+ CONVERT(nvarchar(50), @Goid ) +'')'';
	end
	
	if((@Kolid <> 0))
	begin
		set @WhrStatement= N''(dbo.T_Heasb.KolID = ''+  CONVERT(nvarchar(50),@Kolid)  +'')'';
	end
	
	if(len (@WhrStatement)>0)
	begin
		set @WhrStatement=N'' WHERE ''+ @WhrStatement ;
	end

	set @SQLString=N''
		SELECT     dbo.T_Heasb.HESABID AS ACCID, dbo.T_Heasb.KolID, dbo.T_Heasb.HESABNumber AS CodeACC, dbo.T_Heasb.NameHesab AS NameACC, 
						  dbo.T_Heasb.HESABNumber AS [کد حساب], dbo.T_Heasb.NameHesab AS [عنوان حساب], CONVERT(money, ABS(ISNULL(QMande_GoHesab_SanadHesabdary.DARBed, 0) 
						  - ISNULL(QMande_GoHesab_SanadHesabdary.PARBes, 0))) AS [مانده حساب], CASE WHEN (ISNULL((darbed - parbes), 0)) 
						  > 0 THEN N''''بد'''' WHEN (ISNULL((darbed - parbes), 0)) < 0 THEN N''''بس'''' ELSE ''''--'''' END AS [تشخيص .]
		FROM         dbo.T_Heasb LEFT OUTER JOIN
								  (SELECT     SUM(PARBes) AS PARBes, SUM(DARBed) AS DARBed, hesabID
									 FROM         [TRANSACTION]
									 WHERE     (NOT (SAID2 IS NULL))
									 GROUP BY hesabID, hesabID) QMande_GoHesab_SanadHesabdary ON dbo.T_Heasb.HESABID = QMande_GoHesab_SanadHesabdary.hesabID
		''+ @WhrStatement + ''
		ORDER BY dbo.T_Heasb.NameHesab''

end
else if(@Sath=4)
begin
	
	if(@Goid <> 0 )
	begin
		set @WhrStatement=  N''(dbo.T_T_Heasb.GOID = ''+ CONVERT(nvarchar(50), @Goid ) +'')'';
	end
	
	if(@Kolid <> 0)
	begin
		set @WhrStatement=  N''(dbo.T_T_Heasb.KolID = ''+ CONVERT(nvarchar(50),@Kolid) +'')'';
	end
	
	if(@HESABID <>0)
	begin
		set @WhrStatement=  N''(T_T_Heasb.HESABID = ''+  CONVERT(nvarchar(50),@HESABID) +'')'';
	end
	
	if(len (@WhrStatement)>0)
	begin
		set @WhrStatement=N'' WHERE ''+ @WhrStatement ;
	end
	
	set @SQLString=N''
		SELECT     dbo.T_T_Heasb.THESABID AS ACCID, T_T_Heasb.HESABID, dbo.T_T_Heasb.THESABNumber AS CodeACC, dbo.T_T_Heasb.NameHesabT AS NameACC, 
						  dbo.T_T_Heasb.THESABNumber AS [کد حساب], dbo.T_T_Heasb.NameHesabT AS [عنوان حساب], CONVERT(money, 
						  ABS(ISNULL(QMande_HesabTafsili_SanadHesabdary.DARBed, 0) - ISNULL(QMande_HesabTafsili_SanadHesabdary.PARBes, 0))) AS [مانده حساب], 
						  CASE WHEN (ISNULL((darbed - parbes), 0)) > 0 THEN N''''بد'''' WHEN (ISNULL((darbed - parbes), 0)) < 0 THEN N''''بس'''' ELSE ''''--'''' END AS [تشخيص .], 
						  NULL AS Expr1
		FROM         dbo.T_T_Heasb LEFT OUTER JOIN
								  (SELECT     SUM(PARBes) AS PARBes, SUM(DARBed) AS DARBed, thesabID
									 FROM         [TRANSACTION]
									 WHERE     (NOT (SAID2 IS NULL))
									 GROUP BY thesabID, thesabID) QMande_HesabTafsili_SanadHesabdary ON 
							  dbo.T_T_Heasb.THESABID = QMande_HesabTafsili_SanadHesabdary.thesabID
		'' + @WhrStatement + ''
		 ORDER BY dbo.T_T_Heasb.NameHesabT''
		
		
end
exec (@SQLString);
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[PersianToGregorian]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'
CREATE FUNCTION [PersianToGregorian](@DateStr varchar(10))
RETURNS DATEtime
AS 
BEGIN
   declare @YYear int
   declare @MMonth int
   declare @DDay int
   declare @epbase int
   declare @epyear int
   declare @mdays int
   declare @persian_jdn int
   declare @i int
   declare @j int
   declare @l int
   declare @n int
   declare @TMPRESULT varchar(10)
   declare @IsValideDate int
   declare @TempStr varchar(20)
   DECLARE @TmpDateStr varchar(10)

   SET @i=charindex(''/'',@DateStr)

   IF LEN(@DateStr) - CHARINDEX(''/'', @DateStr,CHARINDEX(''/'', @DateStr,1)+1) = 4
   BEGIN
   /* SET @TmpDateStr = dbo.ReversDate(@DateStr)*/
     IF ( ISDATE(@TmpDateStr) =1 ) 
       RETURN @TmpDateStr
     ELSE
        RETURN NULL
   END
   ELSE
     SET @TmpDateStr = @DateStr

   IF ((@i<>0) and
     /* (dbo.SubStrCount(''/'', @TmpDateStr)=2) and*/
        (ISNUMERIC(REPLACE(@TmpDateStr,''/'',''''))=1) and
        (charindex(''.'',@TmpDateStr)=0)
       )
   BEGIN
       SET @YYear=CAST(SUBSTRING(@TmpDateStr,1,@i-1) AS INT)
                IF ( @YYear< 1300 )
                      SET @YYear =@YYear + 1300
                IF @YYear > 9999
                  RETURN NULL
   
       SET @TempStr= SUBSTRING(@TmpDateStr,@i+1,Len(@TmpDateStr))
   
       SET @i=charindex(''/'',@TempStr)
       SET @MMonth=CAST(SUBSTRING(@TempStr,1,@i-1) AS INT)
       SET @MMonth=@MMonth /*-- -1*/
      
       SET @TempStr= SUBSTRING(@TempStr,@i+1,Len(@TempStr))  
   
       SET @DDay=CAST(@TempStr AS INT)
       SET @DDay=@DDay/*-- - 1*/
          
                 IF ( @YYear >= 0 )
                     SET @epbase = @YYear - 474
                 Else
                     SET @epbase = @YYear - 473
                 SET @epyear = 474 + (@epbase % 2820)

        IF (@MMonth <= 7 )
                       SET @mdays = ((@MMonth) - 1) * 31
        Else
            SET @mdays = ((@MMonth) - 1) * 30 + 6

        SET @persian_jdn =(@DDay)  + @mdays + CAST((((@epyear * 682) - 110) / 2816) as int)  + (@epyear - 1) * 365  +  CAST((@epbase / 2820)  as int ) * 1029983  + (1948321 - 1)

        IF (@persian_jdn > 2299160)
                 BEGIN
            SET @l = @persian_jdn + 68569
            SET @n = CAST(((4 * @l) / 146097) as int)
            SET @l = @l -  CAST(((146097 * @n + 3) / 4) as int)
            SET @i =  CAST(((4000 * (@l + 1)) / 1461001) as int)
            SET @l = @l - CAST( ((1461 * @i) / 4) as int) + 31
            SET @j =  CAST(((80 * @l) / 2447) as int)
            SET @DDay = @l - CAST( ((2447 * @j) / 80) as int)
             SET @l =  CAST((@j / 11) as int)
            SET @MMonth = @j + 2 - 12 * @l
            SET @YYear = 100 * (@n - 49) + @i + @l
                 END

           SET @TMPRESULT=Cast(@MMonth as varchar(2))+''/''+CAST(@DDay as Varchar(2))+''/''+CAST(@YYear as varchar(4)) 
           RETURN Cast(@TMPRESULT as DATETIME)

    END
    RETURN NULL     

END


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProceduretest]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProceduretest](@IDA int, @KalaGo int, @KalaName nvarchar(510)) AS SELECT     AID, NameKala1, GOKID, NameKalaID, ID FROM         dbo.Functiontest() Functiontest WHERE     (@IDA IS NULL) AND (@KalaGo IS NULL) AND (@KalaName IS NULL) OR                       (@IDA IS NOT NULL) AND (@KalaGo IS NULL) AND (@KalaName IS NULL) AND (AID = @IDA) OR                       (@IDA IS NULL) AND (@KalaGo IS NOT NULL) AND (@KalaName IS NULL) AND (GOKID = @KalaGo) OR                       (@IDA IS NOT NULL) AND (@KalaGo IS NOT NULL) AND (@KalaName IS NULL) AND (AID = @IDA) AND (GOKID = @KalaGo) OR                       (@IDA IS NULL) AND (@KalaGo IS NULL) AND (@KalaName IS NOT NULL) AND (NameKala1 = @KalaName) OR                       (@IDA IS NOT NULL) AND (@KalaGo IS NULL) AND (@KalaName IS NOT NULL) AND (AID = @IDA) AND (NameKala1 = @KalaName) OR                       (@IDA IS NULL) AND (@KalaGo IS NOT NULL) AND (@KalaName IS NOT NULL) AND (GOKID = @KalaGo) AND (NameKala1 = @KalaName) OR                       (@IDA IS NOT NULL) AND (@KalaGo IS NOT NULL) AND (@KalaName IS NOT NULL) AND (AID = @IDA) AND (GOKID = @KalaGo) AND                        (NameKala1 = @KalaName)  ' 
END
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[GetShamsiInfo]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        Create Proc [GetShamsiInfo] as  Declare   @Year Integer,    @Month Integer,   @Day Integer,   @F_Year Integer,   @F_Month Integer,   @F_Day Integer,   @F_Day_Name Varchar(10),    @F_Month_Name Varchar(10),    @LastDay Integer,   @Plus Integer,   @Minus Integer,   @Intercalary Integer,   @S_Year Varchar(5),    @S_Month Varchar(5),   @S_Day Varchar(5),   @E_Date Varchar(20),   @Ret Varchar(20)  Set @Plus = 0  Set @Year   = Year(Getdate()) Set @Month  = Month(Getdate()) Set @Day    = Day(Getdate())  ----- بدست آوردن تاريخ ميلادي Set @S_Year  = Cast(@Year AS VarChar(5)) Set @S_Month = Cast(@Month AS VarChar(5)) Set @S_Day   = Cast(@Day AS VarChar(5))  IF  Len(@S_Month) < 2    Set @S_Month = ''0''+@S_Month 	   IF  Len(@S_Day) < 2 	   Set @S_Day = ''0''+@S_Day  Set @E_Date =   @S_Year + @S_Month + @S_Day  ----- بدست آوردن تاريخ ميلادي  ----- بدست آوردن نام روز به فارسي Set @F_Day_Name = Case DATEPART(dw, Getdate())  	When 1 Then ''ي?شنبه''	 	When 2 Then ''دوشنبه''	 	When 3 Then ''سه شنبه''	 	When 4 Then ''?هارشنبه''	 	When 5 Then ''?نجشنبه''	 	When 6 Then ''جمعه''	 	When 7 Then ''شنبه''	 End ----- بدست آوردن نام روز به فارسي  IF ((@Month = 1) or (@Month = 5) or (@Month = 6))   Set @Plus = 10		  IF ((@Month = 2) or (@Month = 4))   Set @Plus = 11		  IF ((@Month = 3) or (@Month = 7) or (@Month = 8) or     (@Month = 9) or (@Month = 11) or (@Month = 12))   Set @Plus = 9		  IF (@Month = 10)   Set @Plus = 8		  Set @Year = @Year % 100  Set @Intercalary = @Year   IF (@Intercalary % 4 = 0)   IF (@Month > 2)     Set @Plus = @Plus + 1  IF ((@Intercalary - 1) % 4 = 0)   begin   Set @LastDay = 30   IF (@Month <= 3)      Set @Plus = @Plus + 1   end Else   Set @LastDay = 29  Set @F_Year = @Year - 22  IF (@F_Year < 0)    Set @F_Year = @F_Year + 100  Set @F_Month = @Month + 9  IF (@F_Month > 12)   begin   Set @F_Month = @F_Month - 12   Set @F_Year = @F_Year + 1   end  Set @F_Day = @Day + @Plus  IF (@F_Month <= 6)    Set @Minus = 31 Else    IF ((@F_Month > 6) and (@F_Month<12))      Set @Minus = 30   Else      Set @Minus = @LastDay  IF (@F_Day > @Minus)   begin   Set @F_Day = @F_Day - @Minus;   Set @F_Month = @F_Month + 1   end  IF (@F_Month > 12)   begin   Set @F_Month = @F_Month - 12;   Set @F_Year  = @F_Year + 1    end;  IF @F_Year >= 10    Set @Ret = Cast(@F_Year As Varchar(4)) Else     Set @Ret = ''0''+ Cast(@F_Year As Varchar(4))   IF @F_Month >= 10    Set @Ret = @Ret + Cast(@F_Month As Varchar(4)) Else     Set @Ret = @Ret +''0''+ Cast(@F_Month As Varchar(4))  ----- بدست آوردن نام ماه به فارسي Set @F_Month_Name = Case @F_Month 	When 1 Then  ''فروردين'' 	When 2 Then  ''ارديبهشت'' 	When 3 Then  ''خرداد'' 	When 4 Then  ''تير'' 	When 5 Then  ''مرداد''	 	When 6 Then  ''شهريور''	 	When 7 Then  ''مهر''	 	When 8 Then  ''آبان''	 	When 9 Then  ''آذر''	 	When 10 Then ''دي''	 	When 11 Then ''بهمن'' 	When 12 Then ''اسفند'' End ----- بدست آوردن نام ماه به فارسي  IF @F_Day >= 10    Set @Ret = @Ret + Cast(@F_Day As Varchar(4)) Else     Set @Ret = @Ret + ''0''+ Cast(@F_Day As Varchar(4))  Select           @E_Date as MiladiDate,  	@Ret As FarsiDate,  	@F_Month_Name AS FarsiMonthName, 	@F_Day_Name AS FarsiDayName  ' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[ConvertArabicToPersion]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
   CREATE PROCEDURE [ConvertArabicToPersion] AS    DECLARE @Table NVARCHAR(4000),@Column NVARCHAR(4000)  DECLARE Table_Cursor CURSOR  For  /* پيدا کردن تمام فيلدهاي متني تمام جداول ديتابيس جاري*/  SELECT T.name, /* Table */         C.name  /* Column */  FROM sysobjects T,syscolumns C  WHERE  T.id = C.id  AND T.xtype = ''u'' /* User Table */           			AND (C.xtype = 99 /* ntext */  					OR C.xtype = 35   /* text */  					OR C.xtype = 231  /* nvarchar */  					OR C.xtype = 167  /* varchar */  					OR C.xtype = 175  /* char */  					OR C.xtype = 239  /* nchar */)  					  OPEN Table_Cursor FETCH NEXT FROM Table_Cursor INTO @Table,@Column  WHILE (@@FETCH_STATUS = 0)  BEGIN  	EXEC (''Update ['' + @Table + ''] Set ['' + @Column + ''] = REPLACE(REPLACE(CAST(['' + @Column +	''] as nvarchar(4000)), NCHAR(1610), NCHAR(1740)), NCHAR(1603), NCHAR(1705))'')  FETCH NEXT FROM Table_Cursor INTO @Table,@Column  END CLOSE Table_Cursor    DEALLOCATE Table_Cursor   
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CloseHesab_InsertInformationT_RepGen]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [CloseHesab_InsertInformationT_RepGen] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = '' SET IDENTITY_INSERT ''+ @StrDes + ''.[dbo].[T_RepGen] ON '' + CHAR(10) + '' Delete FROM'' + CHAR(10) + @StrDes +N''.dbo.T_RepGen  ''
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N'' INSERT INTO '' + CHAR(10) + @StrDes + ''.dbo.T_RepGen  '' + CHAR(10)
												+'' ( RepID, TypeID, RepTitle, FlagShow, Description, OrgDataBinary, ChgDataBinary, SelectFrom, StrFrom, WhereFiled, GroupFileld, HavingField, OrderField, FlagDef, TypeRep, FlagSendP,  '' + CHAR(10) 
												+'' FlagShowDlgPrn, PrnCnt, PrnName, RepStoredProc, storedprocedPararmeter, FlagCh) '' + CHAR(10) 
												+'' SELECT      RepID, TypeID, RepTitle, FlagShow, Description, OrgDataBinary, ChgDataBinary, SelectFrom, StrFrom, WhereFiled, GroupFileld, HavingField, OrderField, FlagDef, TypeRep, FlagSendP, '' + CHAR(10)
												+'' FlagShowDlgPrn, PrnCnt, PrnName, RepStoredProc, storedprocedPararmeter, FlagCh '' + CHAR(10)
												+'' FROM dbo.T_RepGen '' + CHAR(10) 
												+'' SET IDENTITY_INSERT ''+ @StrDes + ''.[dbo].[T_RepGen] OFF''

    Set @ParamDefinition = ''@StrDes NVARCHAR(255) '' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CloseHesab_InsertInformationHesab]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [CloseHesab_InsertInformationHesab] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 



	SET @SQLQuery1 = '' SET IDENTITY_INSERT ''+ @StrDes + ''.[dbo].[T_GropHesab] ON '' + CHAR(10) + '' Delete FROM'' + CHAR(10) + @StrDes +N''.dbo.T_GropHesab ''
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N'' INSERT INTO '' + CHAR(10) + @StrDes + ''.dbo.T_GropHesab '' + CHAR(10)
												+'' (GoID, GoNumber, DateCreate, DateModified, NameGo)'' + CHAR(10)
												+'' SELECT        GoID, GoNumber, DateCreate, DateModified, NameGo '' + CHAR(10)
												+'' FROM dbo.T_GropHesab   SET IDENTITY_INSERT ''+ @StrDes + ''.[dbo].[T_GropHesab] OFF''

    Set @ParamDefinition = ''@StrDes NVARCHAR(255) '' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

	
	
	

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure11]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE  PROCEDURE [StoredProcedure11] AS  RESTORE FILELISTONLY from disk =''c:\backup''    ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Caption_TreeViewVasKharid]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
   CREATE PROCEDURE [Caption_TreeViewVasKharid] AS 
   
   
SELECT     Cod, HeaderCaption, CodGroup
FROM         T_HeaderCaption
WHERE     (CodGroup = 1)' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F1StartDate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'
    CREATE FUNCTION [F1StartDate] 	( ) RETURNS NVarchar(255)  AS 	   BEGIN 		  /*DECLARE @RasidList Varchar(50)*/ 					 	   DECLARE CUR CURSOR FOR     SELECT Replace(StartDate,''/'','''') as SD	FROM AC_Setting.dbo.T_YearMony	Where [dbid]= Db_Name() 		 		 		 	 	    OPEN CUR 		 	    DECLARE @NameHesab  NVarchar(255) 		 	    DECLARE @SUM NVarchar(255) 		 	    SET @SUM = '''' 		 		 	    FETCH NEXT FROM CUR INTO @NameHesab 			 	    WHILE @@FETCH_STATUS = 0 				 		 	   BEGIN 				      			 		   SET @SUM =  @NameHesab 				       			 		   FETCH NEXT FROM CUR INTO @NameHesab 				 		 	   END 			 			 	    CLOSE CUR  		 	    DEALLOCATE CUR  	 	    RETURN @SUM 	     END  
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F1EndDate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'
    CREATE FUNCTION [F1EndDate] 	( ) RETURNS NVarchar(255)  AS 	   BEGIN 		  /*DECLARE @RasidList Varchar(50)*/ 					 	   DECLARE CUR CURSOR FOR     SELECT Replace(EndDate,''/'','''') as SD	FROM AC_Setting.dbo.T_YearMony	Where [dbid]= Db_Name() 		 		 		 	 	    OPEN CUR 		 	    DECLARE @NameHesab  NVarchar(255) 		 	    DECLARE @SUM NVarchar(255) 		 	    SET @SUM = '''' 		 		 	    FETCH NEXT FROM CUR INTO @NameHesab 			 	    WHILE @@FETCH_STATUS = 0 				 		 	   BEGIN 				      			 		   SET @SUM =  @NameHesab 				       			 		   FETCH NEXT FROM CUR INTO @NameHesab 				 		 	   END 			 			 	    CLOSE CUR  		 	    DEALLOCATE CUR  	 	    RETURN @SUM 	     END  
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListCurrency]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Q_ListCurrency] AS SELECT     CurrencyID AS [كد ارز], Title AS [نام ارز], Title, CurrencyID FROM         dbo.Currency ORDER BY Title  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_SoratHesabHatami]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [Q_SoratHesabHatami](@Said int)
AS SELECT     Tran_SoratHesab.Date1, Tran_SoratHesab.Sharh, Tran_SoratHesab.SAID, Tran_SoratHesab.HESABID, Tran_SoratHesab.PARBes, 
                      Tran_SoratHesab.DARBed, Tran_SoratHesab.radif, Tran_SoratHesab.GOID, Tran_SoratHesab.KOLID, Tran_SoratHesab.THESABID, 
                      Tran_SoratHesab.FieldT1, Tran_SoratHesab.IDSHARH, Tran_SoratHesab.TextNumberSA, Tran_SoratHesab.TRAID, Tran_SoratHesab.DARID1, 
                      T_Sorathesab.DateSa, T_Sorathesab.Sharh AS factsharh, T_Sorathesab.NumberSA, T_Sorathesab.FieldN1, T_Sorathesab.FieldN2, 
                      T_Sorathesab.FieldN3
FROM         Tran_SoratHesab INNER JOIN
                      T_Sorathesab ON Tran_SoratHesab.DARID1 = T_Sorathesab.SAID
WHERE     (Tran_SoratHesab.DARID1 = @Said)' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Phonebook_Search]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_Phonebook_Search](@Family nvarchar(50), @Onvan nvarchar(50), @Tel1 nvarchar(50), @Tel2 nvarchar(50), @Tel3 nvarchar(50), @Mob1 nvarchar(50), @Mob2 nvarchar(50), @Fax nvarchar(50), @Telex nvarchar(50), @Site nvarchar(100), @Email1 nvarchar(100), @Email2 nvarchar(100), @Address1 nvarchar(255), @Address2 nvarchar(255), @CodePosti nvarchar(50), @SPosti nvarchar(50), @Coutry nvarchar(50), @Ostan nvarchar(50), @City nvarchar(50), @ShHesabBank nvarchar(50)) AS SELECT     PHONE_ID, Name, Family, Onvan, Tel1, Tel2, Tel3, Mob1, Mob2, Fax, Telex, Site, Email1, Email2, Address1, Address2, CodePosti, SPosti, Coutry,                        Ostan, City, ShHesabBank FROM         AC_Setting.dbo.T_Phone WHERE     (Onvan LIKE ISNULL(@Onvan, N''%%'')) AND (Tel1 LIKE ISNULL(@Tel1, N''%%'')) AND (Tel2 LIKE ISNULL(@Tel2, N''%%'')) AND (Tel3 LIKE ISNULL(@Tel3,                        N''%%'')) AND (Mob1 LIKE ISNULL(@Mob1, N''%%'')) AND (Mob2 LIKE ISNULL(@Mob2, N''%%'')) AND (Fax LIKE ISNULL(@Fax, N''%%'')) AND                        (Telex LIKE ISNULL(@Telex, N''%%'')) AND (Site LIKE ISNULL(@Site, N''%%'')) AND (Email1 LIKE ISNULL(@Email1, N''%%'')) AND                        (Email2 LIKE ISNULL(@Email2, N''%%'')) AND (Address1 LIKE ISNULL(@Address1, N''%%'')) AND (Address2 LIKE ISNULL(@Address2, N''%%'')) AND                        (CodePosti LIKE ISNULL(@CodePosti, N''%%'')) AND (SPosti LIKE ISNULL(@SPosti, N''%%'')) AND (Coutry LIKE ISNULL(@Coutry, N''%%'')) AND                        (Ostan LIKE ISNULL(@Ostan, N''%%'')) AND (City LIKE ISNULL(@City, N''%%'')) AND (ShHesabBank LIKE ISNULL(@ShHesabBank, N''%%'')) AND                        (Family LIKE ISNULL(@Family, N''%%''))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QueryCompinfo]'))
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [QueryCompinfo] AS 
SELECT     AC_Setting.dbo.T_yCompanyName.COMPANYID, AC_Setting.dbo.T_yCompanyName.Name, AC_Setting.dbo.T_yCompanyName.NameLa, 
AC_Setting.dbo.T_yCompanyName.ZamineKary, AC_Setting.dbo.T_yCompanyName.Namemodir, AC_Setting.dbo.T_yCompanyName.Address,
CONVERT(varbinary, AC_Setting.dbo.T_yCompanyName.Addresspic) as Logo ,AC_Setting.dbo.T_yCompanyName.Phone1, AC_Setting.dbo.T_yCompanyName.Fax,
AC_Setting.dbo.T_yCompanyName.Email, AC_Setting.dbo.T_yCompanyName.PostalCode, AC_Setting.dbo.T_yCompanyName.CodeEghtesady,
AC_Setting.dbo.T_YearMony.StartDate, AC_Setting.dbo.T_YearMony.EndDate, AC_Setting.dbo.T_YearMony.IDDOREHM,
AC_Setting.dbo.T_YearMony.DBID , 0 AS Prmt1, 0 AS Prmt2, 0 AS Prmt3, 0 AS Prmt4, 0 AS Prmt5, 0 AS Prmt6, 0 AS Prmt7, 0 AS Prmt8, 0 AS Prmt9, 0 AS Prmt10, 0 AS Prmt11, 0 AS Prmt12, 0 AS Prmt13, 0 AS Prmt14, 0 AS Prmt15
FROM         AC_Setting.dbo.T_yCompanyName INNER JOIN
AC_Setting.dbo.T_YearMony ON AC_Setting.dbo.T_yCompanyName.COMPANYID = AC_Setting.dbo.T_YearMony.COMPANYID
WHERE     (AC_Setting.dbo.T_YearMony.DBID = N''AC_047_2'')'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Table_UserDastrasi_Insert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  create PROCEDURE [Table_UserDastrasi_Insert]  		@USERID int, 		@IDNoeOP int  AS BEGIN    INSERT INTO Table_UserDastrasi                       (USERID, IDNoeOP) VALUES     (@USERID,@IDNoeOP)  END 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Table_UserDastrasi_GetValue]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  create PROCEDURE [Table_UserDastrasi_GetValue] 		@USERID int, 		@IDNoeOP int, 		@CountValue int=-1 output  AS BEGIN  SELECT    @CountValue= COUNT(USERID)  FROM         Table_UserDastrasi WHERE     (IDNoeOP = @IDNoeOP) AND (USERID = @USERID)  END 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Table_UserDastrasi_DELETE]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  create PROCEDURE [Table_UserDastrasi_DELETE]  		@USERID int, 		@IDNoeOP int  AS BEGIN   DELETE FROM Table_UserDastrasi WHERE     (USERID = @USERID) AND (IDNoeOP = @IDNoeOP)  END 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[UserLogin]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [UserLogin](@UserID int) AS INSERT INTO dbo.TMP_UserCode                       (USERID) VALUES     (@userid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[UserContoroling]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [UserContoroling](@OprType int, @OprID int) AS SELECT     USERID, OperationType, OperationID, DATEDIFF(second, TimeSabt, GETDATE()) AS TimeDif, DATEDIFF(day, TimeSabt, GETDATE()) AS Expr1 FROM         dbo.TMP_UserCode WHERE     (DATEDIFF(second, TimeSabt, GETDATE()) <= 5) AND (OperationType = @OprType) AND (OperationID = @OprID) AND (DATEDIFF(day, TimeSabt,                        GETDATE()) = 0)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[UserCheckLogin]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [UserCheckLogin](@UserID int) AS SELECT     USERID, OperationType, OperationID, DATEDIFF(second, TimeSabt, GETDATE()) AS TimeDif, DATEDIFF(day, TimeSabt, GETDATE()) AS Expr1 FROM         dbo.TMP_UserCode WHERE     (DATEDIFF(second, TimeSabt, GETDATE()) <= 5) AND (DATEDIFF(day, TimeSabt, GETDATE()) = 0) AND (USERID = @UserID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure14]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure14](@SAID int) AS SELECT     dbo.Table4.KOlID1, dbo.Table4.HESABID1, dbo.Table4.THESABID1, dbo.T_KolHesab.GOID, SUM(dbo.Table4.MabHazine) AS MabHazine,                        3 AS Expr1 FROM         dbo.Table4 INNER JOIN                       dbo.T_Forosh ON dbo.Table4.FactID = dbo.T_Forosh.FOROSHID INNER JOIN                       dbo.T_KolHesab ON dbo.Table4.KOlID1 = dbo.T_KolHesab.KOLID WHERE     (dbo.Table4.TypeFact = 2) AND (dbo.T_Forosh.PrimarySaid2 = @SAID) GROUP BY dbo.Table4.KOlID1, dbo.Table4.HESABID1, dbo.Table4.THESABID1, dbo.T_KolHesab.GOID  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[SELECTGOKID]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

  cREATE PROCEDURE [SELECTGOKID] ( 
@GOKID int)           
  AS     
  BEGIN
  	 	
SELECT     ID, NameGoKalanumbr, DateCreate, DateModified, NameGoKala1, note, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4
FROM         T_NameGoKala
WHERE     (ID = @GOKID)

 END 


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Std_05002300880035]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'





CREATE PROCEDURE [Std_05002300880035] 
(
@pvchAction			    nvarchar(50)=''RepGen'', /* Analyze - Print */
@pintGoid				int=null, 
@pintKolid				int=null, 
@pintHesabid			int=null, 
@pintTHesabid			int=null,
@pintAid				int=null, 
@pintGoKalaid			int=null, 
@pintKalaid				int=null, 
@pnvaStatdate			nvarchar(50)=null,
@pnvaEnddate			nvarchar(50)=null,
@pintFactType			int=null, 
@pnvaFactorStartid		nvarchar(50)=null,
@pnvaFactorEndid		nvarchar(50)=null,
@pnvaSanadStartid		int=null,
@pnvaSanadEndid			int=null,
@pnvaSrt			nvarchar(255)=null,
@pnvaShowkala			nvarchar(255)=null
)        

 AS 
begin

    /* Specify the parameter format one time. */
	if(NOT (@pintGoKalaid IS NULL))
		begin
SELECT     NameGoKalanumbr, NameGoKala1, note, ID AS idGroup
FROM         T_NameGoKala
WHERE     (ID = @pintGoKalaid)
ORDER BY NameGoKala1		end
	else
		begin		
			SELECT     NameGoKalanumbr, NameGoKala1, note, ID AS idGroup
			FROM         T_NameGoKala
			ORDER BY NameGoKala1	
		end	
                                               
return
end
















' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_PForm_Virayesh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_PForm_Virayesh] AS SELECT     KalaNumber, NameKala1, VAHED1, UnitPrice1, UnitPrice2, UnitPrice3, BarCode, VAHED2, MinPrice, MaxPrice, Zarib, MinMogoody, MaxMogoody FROM         dbo.T_NameKala  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05238835]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
         CREATE PROCEDURE [Rep_05238835](@GoKala int) AS 
         
         SELECT     @GoKala AS Expr1, ID, NameGoKala1, NameGoKalanumbr
FROM         T_NameGoKala
WHERE     (@GoKala IS NULL) OR
                      (@GoKala IS NOT NULL) AND (ID = @GoKala)   
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_05228521]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_05228521](@KalaGO int) AS SELECT     ID, NameGoKala1, @KalaGO AS KalaGO, NameGoKalanumbr FROM         dbo.T_NameGoKala WHERE     (@KalaGO IS NULL) OR                       (@KalaGO IS NOT NULL) AND (ID = @KalaGO)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QSum_HazineForoosh]'))
EXEC dbo.sp_executesql @statement = N'          CREATE VIEW [QSum_HazineForoosh] AS SELECT     FactID, SUM(MabHazine) AS MabHazine FROM         dbo.Table4 WHERE     (Flag1 = 0) AND (TypeFact = 2) GROUP BY FactID  '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030107]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030107](@AccID nvarchar(50)) AS SELECT     THESABID, GOID, KolID, HESABID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KolID),                        4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), THESABID), 4), 1, LEN(@AccID)) AS HESABIDt,                        NameHesabT FROM         dbo.T_T_Heasb WHERE     (@AccID IS NULL) OR                       (NOT (@AccID IS NULL)) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KolID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), THESABID), 4), 1, LEN(@AccID)) = @AccID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030104]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030104](@AccID nvarchar(50)) AS SELECT     HESABID, NameHesab, GOID, KolID, @AccID AS Expr1, HESABNumber, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KolID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), HESABID), 4), 1, LEN(@AccID)) AS hesabidt FROM         dbo.T_Heasb WHERE     (@AccID IS NULL) OR                       (NOT (@AccID IS NULL)) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KolID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), HESABID), 4), 1, LEN(@AccID)) = @AccID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030103]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030103](@AccID nvarchar(50)) AS SELECT     KOLID, NameHesab, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1,                        LEN(@AccID)) AS hesabid, GOID FROM         dbo.T_KolHesab WHERE     (@AccID IS NULL) OR                       (NOT (@AccID IS NULL)) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1,                        LEN(@AccID)) = @AccID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030102]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030102] AS SELECT     GoID, NameGo FROM         dbo.T_GropHesab   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListAmelTozie]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [QListAmelTozie] AS SELECT     THESABID, HESABID, NameHesabT, GOID, KolID, CONVERT(money, ABS(CONVERT(money, ISNULL(Balance, 0)))) AS Expr1, CASE WHEN (CONVERT(money,                        CONVERT(money, ISNULL(Balance, 0)))) > 0 THEN N''بد'' WHEN (CONVERT(money, CONVERT(money, ISNULL(Balance, 0)))) < 0 THEN N''بس'' ELSE ''--'' END AS Expr2,                        Commision FROM         dbo.T_T_Heasb WHERE     (Commision > 0) ORDER BY NameHesabT' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QList_KalaKhadamat]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QList_KalaKhadamat] AS SELECT     NAMEKHID, Name, UnitPrice FROM         dbo.T_NameKhad ORDER BY Name  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListPardakhtNaghdi]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
        CREATE PROCEDURE [Q_ListPardakhtNaghdi] AS SELECT     dbo.T_Par1.TextSharh, dbo.T_Par1.TextName2, dbo.T_Par1.Mablagh, dbo.T_Par1.PARID1, dbo.T_SA2.SAID2Number, dbo.T_Par1.PrimarySaid2,                        dbo.T_Par1.PARID1Number, dbo.T_Par1.DATE2, dbo.T_Par1.TextShSA, dbo.T_Par1.TextName1 FROM         dbo.T_Par1 INNER JOIN                       dbo.T_SA2 ON dbo.T_Par1.PrimarySaid2 = dbo.T_SA2.SAID2 ORDER BY dbo.T_Par1.PARID1 DESC  
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListNameAllHesab]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [Q_ListNameAllHesab]() RETURNS TABLE AS RETURN ( SELECT     KOLID, NameHesab, NULL AS HesabID, NULL AS THesabID FROM         dbo.T_KolHesab UNION ALL SELECT     KolID, NameHesab, HESABID AS HesabID, NULL AS THesabID FROM         dbo.T_Heasb UNION ALL SELECT     KolID, NameHesabT AS NameHesab, HESABID, THESABID FROM         dbo.T_T_Heasb )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditKol_Tafsili]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditKol_Tafsili](@Goid int, @Kolid int) AS UPDATE    dbo.T_T_Heasb SET              GOID = @Goid WHERE     (KolID = @Kolid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditKol_Moin]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditKol_Moin](@Goid int, @Kolid int) AS UPDATE    dbo.T_Heasb SET              GOID = @Goid WHERE     (KolID = @Kolid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_DeleteSanadFactSandogh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_DeleteSanadFactSandogh](@dateFo nvarchar(50)) AS DELETE FROM dbo.T_SA2 WHERE     (SAID2 IN                           (SELECT     dbo.T_Forosh.PrimarySaid2                              FROM         dbo.T_Forosh LEFT OUTER JOIN                                                    dbo.T_SA2 ON dbo.T_Forosh.PrimarySaid2 = dbo.T_SA2.SAID2                              WHERE     (dbo.T_SA2.FieldN2 = 26) AND (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) AND (dbo.T_Forosh.KOLID IS NULL)))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_CheckingSubFactSandogh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_CheckingSubFactSandogh](@dateFo nvarchar(50)) AS SELECT     dbo.T_Forosh.FOROSHID, dbo.T_Forosh.DateFo, dbo.T_Forosh.Flag1, dbo.T_Forosh.NumberKH FROM         dbo.T_Forosh LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Forosh.PrimarySaid2 = dbo.T_SA2.SAID2 WHERE     (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) OR                       (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) ORDER BY dbo.T_Forosh.DateFo, dbo.T_Forosh.FOROSHID  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListAnbar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListAnbar] AS SELECT     AID, NameAnbar FROM         dbo.T_Anbar ORDER BY NameAnbar  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_List_Anbar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_List_Anbar] AS SELECT     AID, NameAnbar FROM         dbo.T_Anbar ORDER BY NameAnbar  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Kharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'       CREATE FUNCTION [Q_Kharid](@AccID nvarchar(50)) RETURNS TABLE AS RETURN ( SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_GropHesab.GoID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt FROM         dbo.T_Kharid INNER JOIN                       dbo.T_GropHesab ON dbo.T_Kharid.GOID = dbo.T_GropHesab.GoID GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_GropHesab.GoID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) )    ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ForoshDate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [Q_ForoshDate]() RETURNS TABLE AS RETURN ( SELECT     DateFo FROM         dbo.T_Forosh )   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Forosh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'       CREATE FUNCTION [Q_Forosh](@AccID nvarchar(50)) RETURNS TABLE AS RETURN ( SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_GropHesab.GoID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidtt FROM         dbo.T_Forosh INNER JOIN                       dbo.T_GropHesab ON dbo.T_Forosh.GOID = dbo.T_GropHesab.GoID GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_GropHesab.GoID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) )    ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListDasteCheck]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListDasteCheck](@THesabID int, @TextSeryal float) AS SELECT     Serialnum1, DastehCHID, NameHesab, NameBank, CodeHesab, Flag FROM         dbo.DastehChek WHERE     (Flag = 0) AND (CodeHesab = @THesabID) OR                       (Flag = 0) AND (CodeHesab = @THesabID) AND (Serialnum1 = @TextSeryal) ORDER BY Serialnum1  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListDaryaftNaghdi]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
        CREATE PROCEDURE [Q_ListDaryaftNaghdi] AS SELECT     dbo.T_Dar1.TextSharh, dbo.T_Dar1.TextName1, dbo.T_Dar1.TextName2, dbo.T_Dar1.Mablagh, dbo.T_Dar1.DARID1, dbo.T_SA2.SAID2Number,                        dbo.T_Dar1.DATE2, dbo.T_Dar1.DAR1Numbr, dbo.T_Dar1.PrimarySaid2 FROM         dbo.T_Dar1 INNER JOIN                       dbo.T_SA2 ON dbo.T_Dar1.PrimarySaid2 = dbo.T_SA2.SAID2 ORDER BY dbo.T_Dar1.DARID1 DESC  
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_listKala_ALL_GoroupKala]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Q_listKala_ALL_GoroupKala] AS SELECT     ID, NameGoKala1 FROM         dbo.T_NameGoKala GROUP BY NameGoKala1, ID HAVING      (NOT (NameGoKala1 IS NULL))' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_ALL_Barcode]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Q_ListKala_ALL_Barcode] AS SELECT     ID, BarCode, NameKala1, NULL AS Expr1, NULL AS Expr2, UnitPrice1, Currency1 FROM         dbo.T_NameKala ORDER BY BarCode' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListKala] AS SELECT     ID, NameKala1 FROM         dbo.T_NameKala ORDER BY NameKala1  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_PFrm_Pardakht]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Q_PFrm_Pardakht](@Parid int) AS SELECT     PARID1, PARID1Number, DATE2, TextName2, Mablagh, TextSharh FROM         dbo.T_Par1 WHERE     (PARID1 = @parid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_PFrm_Daryaft]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Q_PFrm_Daryaft](@darid int) AS SELECT     DARID1, DAR1Numbr, DATE2, TextName1, TextSharh, Mablagh FROM         dbo.T_Dar1 WHERE     (DARID1 = @darid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKalaKhadamat]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListKalaKhadamat] AS SELECT     NAMEKHID, Name, UnitPrice FROM         dbo.T_NameKhad ORDER BY Name  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListGroup]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListGroup] AS SELECT     ID, NameGoKala1 FROM         dbo.T_NameGoKala ORDER BY NameGoKala1  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_Mojoodi]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListKala_Mojoodi] AS SELECT     ID, NameKala1 FROM         dbo.T_NameKala ORDER BY NameKala1  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm1_CloseShift]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QSubPFrm1_CloseShift] AS SELECT     COUNT(dbo.T_Forosh.FOROSHID) AS Expr1, dbo.T_Forosh.DateFo, dbo.T_Forosh.Flag1 FROM         dbo.T_Forosh LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Forosh.PrimarySaid2 = dbo.T_SA2.SAID2 WHERE     (dbo.T_SA2.FieldN2 = 26) AND (NOT (dbo.T_Forosh.UserID IS NULL)) OR                       (dbo.T_SA2.FieldN2 IS NULL) AND (dbo.T_Forosh.Flag1 = 0) AND (NOT (dbo.T_Forosh.UserID IS NULL)) GROUP BY dbo.T_Forosh.DateFo, dbo.T_Forosh.Flag1 ORDER BY dbo.T_Forosh.DateFo DESC, dbo.T_Forosh.Flag1  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPfrm_ListSanadHesabdary]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

        CREATE PROCEDURE [QSubPfrm_ListSanadHesabdary] AS SELECT     SAID2, Sharh, Date2, FieldN2, FieldN1, FieldT1, Flag1, Flag2, Flag3, SAID2Number, OperationCode FROM         dbo.T_SA2 ORDER BY SAID2 DESC  

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_RepGen_UpdateFlagCH]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'



CREATE  PROCEDURE [QSubPFrm_RepGen_UpdateFlagCH]

		@IDRep INT,
		@FlagCh bit
AS

BEGIN

	
UPDATE    T_RepGen
SET              FlagCh = @FlagCh
WHERE     (IDKey = @IDRep)


							  
END






' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListNameKhadamat]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

    CREATE PROCEDURE [QSubPFrm_ListNameKhadamat] AS SELECT     NAMEKHID, Name, UnitPrice, Unit, KhadamatNumber, CONVERT(int, KhadamatNumber) AS Expr1 FROM         dbo.T_NameKhad ORDER BY CONVERT(int, KhadamatNumber)

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPfrm_ListMsgGoKala]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

  CREATE PROCEDURE [QSubPfrm_ListMsgGoKala] AS SELECT     ID, NameGoKala1, note, NameGoKalanumbr, CONVERT(int, NameGoKalanumbr) AS Expr1 FROM         dbo.T_NameGoKala ORDER BY NameGoKala1

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListMoinHesab]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

        CREATE PROCEDURE [QSubPFrm_ListMoinHesab] AS SELECT     dbo.T_Heasb.HESABID, dbo.T_Heasb.NameHesab, dbo.T_Heasb.GOID, dbo.T_Heasb.KolID, CONVERT(int, dbo.T_Heasb.HESABNumber) AS CodeH,                        dbo.T_KolHesab.NameHesab AS KolName, dbo.T_GropHesab.NameGo AS GoroupName, dbo.T_Heasb.HESABNumber FROM         dbo.T_Heasb LEFT OUTER JOIN                       dbo.T_KolHesab ON dbo.T_Heasb.KolID = dbo.T_KolHesab.KOLID LEFT OUTER JOIN                       dbo.T_GropHesab ON dbo.T_Heasb.GOID = dbo.T_GropHesab.GoID ORDER BY CONVERT(int, dbo.T_Heasb.HESABNumber)  

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListKolHesab]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

        CREATE PROCEDURE [QSubPFrm_ListKolHesab] AS SELECT     CONVERT(int, dbo.T_KolHesab.KolNumber) AS Expr1, dbo.T_KolHesab.NameHesab, dbo.T_GropHesab.NameGo, dbo.T_KolHesab.KOLID,                        dbo.T_KolHesab.GOID, dbo.T_KolHesab.KolNumber FROM         dbo.T_KolHesab INNER JOIN                       dbo.T_GropHesab ON dbo.T_KolHesab.GOID = dbo.T_GropHesab.GoID ORDER BY CONVERT(int, dbo.T_KolHesab.KolNumber) DESC  

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListTafsiliHesab_HazineFact]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QListTafsiliHesab_HazineFact](@FieldN3 int) AS SELECT     THESABID, HESABID, NameHesabT, GOID, KolID, CONVERT(money, ABS(CONVERT(money, ISNULL(Balance, 0)))) AS Expr1,                        CASE WHEN (CONVERT(money, CONVERT(money, ISNULL(Balance, 0)))) > 0 THEN N''بد'' WHEN (CONVERT(money, CONVERT(money, ISNULL(Balance,                        0)))) < 0 THEN N''بس'' ELSE ''--'' END AS Expr2 FROM         dbo.T_T_Heasb WHERE     (HESABID = @FieldN3) ORDER BY NameHesabT  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubListChTFrm_Anbar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
  CREATE PROCEDURE [QSubListChTFrm_Anbar] AS SELECT     dbo.T_Anbar.*, CONVERT(int, AIDNumber) AS Expr1,''حساب انبار'' AS nameHesab FROM         dbo.T_Anbar
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QMandeTafsiliHesab1]'))
EXEC dbo.sp_executesql @statement = N'          CREATE VIEW [QMandeTafsiliHesab1] AS SELECT     HESABID, SUM(CONVERT(money, ISNULL(Balance, 0))) AS MandeTafsili FROM         dbo.T_T_Heasb GROUP BY HESABID '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QMandeTafsiliHesab]'))
EXEC dbo.sp_executesql @statement = N'          CREATE VIEW [QMandeTafsiliHesab] AS SELECT     KolID, SUM(CONVERT(money, ISNULL(Balance, 0))) AS MandeTafsili FROM         dbo.T_T_Heasb GROUP BY KolID  '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QMandeMoinHesab]'))
EXEC dbo.sp_executesql @statement = N'          CREATE VIEW [QMandeMoinHesab] AS SELECT     KolID, SUM(CONVERT(money, ISNULL(Balance, 0))) AS MandeMoin FROM         dbo.T_Heasb GROUP BY KolID  '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QSub_Frm_DastehChek]'))
EXEC dbo.sp_executesql @statement = N'
          CREATE VIEW [QSub_Frm_DastehChek] AS SELECT     DastehCHID, NameBank, Sh_Hesab, SeriDastehCD, Serialnum1, Serialnum2, Flag, NameHesab, CodeHesab FROM         dbo.DastehChek WHERE     (Flag = 0) 
'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[KalaName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'
    CREATE FUNCTION [KalaName] 	( 	@KalaID int 	) RETURNS NVarchar(255)  AS 	BEGIN 		/*DECLARE @RasidList NVarchar(50)*/ 		DECLARE CUR CURSOR FOR SELECT NameKala1 FROM T_NameKala WHERE ID = @KalaID 		 		OPEN CUR 		DECLARE @NameKala1  NVarchar(255) 		DECLARE @SUM NVarchar(255) 		SET @SUM = '''' 		 		FETCH NEXT FROM CUR INTO @NameKala1 			WHILE @@FETCH_STATUS = 0 				BEGIN 				     SET @SUM =  @NameKala1 				      FETCH NEXT FROM CUR INTO @NameKala1 				END 			 			CLOSE CUR  		DEALLOCATE CUR  	RETURN @SUM 	END           
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[KalaKhadamatName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'
    CREATE FUNCTION [KalaKhadamatName] 	( 	@KalaID int 	) RETURNS Varchar(255)  AS 	BEGIN 		 		DECLARE CUR CURSOR FOR SELECT Name FROM T_NameKhad WHERE NAMEKHID = @KalaID 		 		OPEN CUR 		DECLARE @Name  Varchar(255) 		DECLARE @SUM Varchar(255) 		SET @SUM = '''' 		 		FETCH NEXT FROM CUR INTO @Name 			WHILE @@FETCH_STATUS = 0 				BEGIN 				     SET @SUM =  @Name 				      FETCH NEXT FROM CUR INTO @Name 				END 			 			CLOSE CUR  		DEALLOCATE CUR 		 	RETURN @SUM 	END    
' 
END
GO



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[FunShFishKhadamat]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'CREATE FUNCTION [FunShFishKhadamat] 	( 	@ShFactorKh int) RETURNS Varchar(255)  AS 	
BEGIN 		

/*DECLARE @RasidList Varchar(50)*/
IF (@ShFactorKh IS NOT NULL) 			
		BEGIN 			      
	            
			DECLARE CUR CURSOR FOR SELECT DAR1Numbr
												FROM         T_Dar1
												WHERE     (NumSID2 = @ShFactorKh)	
			OPEN CUR
			 		
			DECLARE @DAR1Numbr  Varchar(255) 		
			DECLARE @SUM Varchar(255) 		
			SET @SUM = '''' 		 		
			FETCH NEXT FROM CUR INTO @DAR1Numbr			
			WHILE @@FETCH_STATUS = 0 				
				BEGIN 	
					if 	(len(@SUM)=0 )		     
						SET @SUM =  @DAR1Numbr 	
					else
						SET @SUM = @SUM + '' - '' + @DAR1Numbr 	   
					FETCH NEXT FROM CUR INTO @DAR1Numbr 				
				END 			 			
			CLOSE CUR  		
			DEALLOCATE CUR  	
	END 
	RETURN @SUM 	
END' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_Tafsili]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditMoin_Tafsili](@Goid int, @Kolid int, @Hesabid int) AS UPDATE    dbo.T_T_Heasb SET              GOID = @Goid, KolID = @Kolid WHERE     (HESABID = @Hesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_PishFact]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditMoin_PishFact](@Kolid int, @Hesabid int) AS UPDATE    dbo.T_PishFactor SET              KOLID = @Kolid WHERE     (HesabID = @Hesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_Khadmat]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditMoin_Khadmat](@Kolid int, @Hesabid int) AS UPDATE    dbo.T_DKhad SET              KOLID = @Kolid WHERE     (HesabID = @Hesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_Kh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditMoin_Kh](@Kolid int, @Hesabid int) AS UPDATE    dbo.T_Kharid SET              KOLID = @Kolid WHERE     (HesabID = @Hesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_HazineFact]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditMoin_HazineFact](@Kolid int, @Hesabid int) AS UPDATE    dbo.Table4 SET              KOlID = @Kolid, KOlID1 = @Kolid WHERE     (HESABID = @Hesabid) OR                       (HESABID1 = @Hesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_Fo]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditMoin_Fo](@Kolid int, @Hesabid int) AS UPDATE    dbo.T_Forosh SET              KOLID = @Kolid WHERE     (HesabID = @Hesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_BarKh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditMoin_BarKh](@Kolid int, @Hesabid int) AS UPDATE    dbo.T_Dar8 SET              KOLID = @Kolid WHERE     (HesabID = @Hesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_BarFo]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditMoin_BarFo](@Kolid int, @Hesabid int) AS UPDATE    dbo.T_Par7 SET              KOLID = @Kolid WHERE     (HesabID = @Hesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_AsnadPar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditMoin_AsnadPar](@Kolid int, @Hesabid int) AS UPDATE    dbo.T_Asnad SET              KOLID = @Kolid WHERE     (HESABID = @Hesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_AsnadDar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditMoin_AsnadDar](@Kolid int, @Hesabid int) AS UPDATE    dbo.T_Asnad_Dar SET              KOLID = @Kolid WHERE     (HESABID = @Hesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_PishFact]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditTafsili_PishFact](@Kolid int, @Hesabid int, @Thesabid int) AS UPDATE    dbo.T_PishFactor SET              KOLID = @Kolid, HesabID = @Hesabid WHERE     (THESABID = @Thesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_Khadmat]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditTafsili_Khadmat](@Kolid int, @Hesabid int, @Thesabid int) AS UPDATE    dbo.T_DKhad SET              KOLID = @Kolid, HesabID = @Hesabid WHERE     (THESABID = @Thesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_Kh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditTafsili_Kh](@Kolid int, @Hesabid int, @Thesabid int) AS UPDATE    dbo.T_Kharid SET              KOLID = @Kolid, HesabID = @Hesabid WHERE     (THESABID = @Thesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_HazineFact]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditTafsili_HazineFact](@Kolid int, @Hesabid int, @Thesabid int) AS UPDATE    dbo.Table4 SET              KOlID = @Kolid, HESABID = @Hesabid, KOlID1 = @Kolid, HESABID1 = @Hesabid WHERE     (THESABID = @Thesabid) OR                       (THESABID1 = @Thesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_Fo]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditTafsili_Fo](@Kolid int, @Hesabid int, @Thesabid int) AS UPDATE    dbo.T_Forosh SET              KOLID = @Kolid, HesabID = @Hesabid WHERE     (THESABID = @Thesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_BarKh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditTafsili_BarKh](@Kolid int, @Hesabid int, @Thesabid int) AS UPDATE    dbo.T_Dar8 SET              KOLID = @Kolid, HesabID = @Hesabid WHERE     (THESABID = @Thesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_BarFo]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditTafsili_BarFo](@Kolid int, @Hesabid int, @Thesabid int) AS UPDATE    dbo.T_Par7 SET              KOLID = @Kolid, HesabID = @Hesabid WHERE     (THESABID = @Thesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_AsnadPar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditTafsili_AsnadPar](@Kolid int, @Hesabid int, @Thesabid int) AS UPDATE    dbo.T_Asnad SET              KOLID = @Kolid, HESABID = @Hesabid WHERE     (THESABID = @Thesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_AsnadDar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditTafsili_AsnadDar](@Kolid int, @Hesabid int, @Thesabid int) AS UPDATE    dbo.T_Asnad_Dar SET              KOLID = @Kolid, HESABID = @Hesabid WHERE     (THESABID = @Thesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[PreviwSanadHEsabdary]'))
EXEC dbo.sp_executesql @statement = N'        CREATE VIEW [PreviwSanadHEsabdary] AS SELECT     SAID2Number, SAID2, Sharh, Date2, OperationCode FROM         dbo.T_SA2  '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DEFULT_SELECTAID]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

  cREATE PROCEDURE [DEFULT_SELECTAID] ( 
@AID int)           
  AS     
  BEGIN
  	 	
SELECT     AID, AIDNumber, DateCreate, DateModified, kalaid, NameAnbar, Phone, Address, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4 
FROM         T_Anbar
WHERE     (AID = @AID)

 END 


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MabHazine_Kharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [F_MabHazine_Kharid]() RETURNS TABLE AS RETURN ( SELECT     FactID, TypeFact, SUM(MabHazine) AS MabHazine FROM         dbo.Table4 WHERE     (Flag1 = 0) GROUP BY FactID, TypeFact HAVING      (TypeFact = 1) )   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MabHazine_Foroosh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [F_MabHazine_Foroosh]() RETURNS TABLE AS RETURN ( SELECT     FactID, TypeFact, SUM(MabHazine) AS MabHazine FROM         dbo.Table4 WHERE     (Flag1 = 0) GROUP BY FactID, TypeFact HAVING      (TypeFact = 2) )   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKhadamt_All]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [CQ_ListKhadamt_All]
AS SELECT     NAMEKHID, CONVERT(int, KhadamatNumber) AS KhadamatNumber, Name, UnitPrice
FROM         dbo.T_NameKhad' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListAnbar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
       create PROCEDURE [CQ_ListAnbar] AS SELECT     AID, AIDNumber AS [كد انبار], NameAnbar AS [نام انبار], NameAnbar, AIDNumber FROM         dbo.T_Anbar ORDER BY NameAnbar
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MabTakhfif_Kharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [F_MabTakhfif_Kharid]() RETURNS TABLE AS RETURN ( SELECT     KHARIDID, SUM(ISNULL(Takhfif, 0)) AS Takhfif FROM         dbo.T_Kharid GROUP BY KHARIDID )   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MabTakhfif_Foroosh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [F_MabTakhfif_Foroosh]() RETURNS TABLE AS RETURN ( SELECT     FOROSHID, SUM(ISNULL(Takhfif, 0)) AS Takhfif FROM         dbo.T_Forosh GROUP BY FOROSHID )   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure**1]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure**1](@TypeFact nvarchar(50), @UserID int) AS SELECT     *, FlgSabtFact AS Expr3, Flag1 AS Expr1, NumberKH AS Expr2, UserID AS Expr4 FROM         dbo.T_Forosh WHERE     (@TypeFact = 2) AND (UserID = @UserID) AND (Flag1 = 0) AND (FlgSabtFact = 0) OR                       (@TypeFact = 1) AND (UserID = @UserID) AND (Flag1 = 0) ORDER BY NumberKH  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure10]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure10] AS SELECT     Shift, PrimarySaid2 FROM         dbo.T_Forosh WHERE     (PrimarySaid2 IS NULL)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure12]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure12](@PrimarySaid2 int, @Dateeng smalldatetime, @HesabID int, @KOLID int, @THESABID int, @dateFo nvarchar(255)) AS UPDATE    dbo.T_Forosh SET              PrimarySaid2 = @PrimarySaid2, Dateeng = @Dateeng, HesabID = @HesabID, KOLID = @KOLID, THESABID = @THESABID, Flag1 = 1, FlgSabtFact = 1,                        DateFo = @dateFo WHERE     (FOROSHID IN                           (SELECT     dbo.T_Forosh.FOROSHID                              FROM         dbo.T_Forosh LEFT OUTER JOIN                                                    dbo.T_SA2 ON dbo.T_Forosh.PrimarySaid2 = dbo.T_SA2.SAID2                              WHERE     (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) AND (dbo.T_Forosh.KOLID IS NULL) AND (dbo.T_SA2.FieldN2 = 26) OR                                                    (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) AND (dbo.T_Forosh.KOLID IS NULL) AND (dbo.T_SA2.FieldN2 IS NULL)))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure5]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure5] AS SELECT     ID, NameKala1, FieldT1, KalaNumber FROM         dbo.T_NameKala  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure18]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [StoredProcedure18]
AS SELECT      Row_number() OVER (ORDER BY HESABNumber) AS radif, HESABID
FROM          (SELECT      HESABID, HESABNumber, MSGHESAB
                         FROM          dbo.T_Heasb) T1' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure17]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'  CREATE PROCEDURE [StoredProcedure17] AS SELECT     IDKey, RepID FROM         dbo.T_RepGen WHERE     (IDKey = 47) AND (RepID = 7)' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure16]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [StoredProcedure16] AS SELECT     dbo.TRANSACTION2.* FROM         dbo.TRANSACTION2' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_EditMojodiKala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'
CREATE FUNCTION [F_EditMojodiKala](@KalaID nvarchar(50),
@GOKID int,
@AID nvarchar(255))
RETURNS TABLE
AS
RETURN ( SELECT  TOP 100 PERCENT dbo.T_Kala.Mojodi, dbo.T_Kala.UnitPrice, dbo.T_Kala.NameKalaID, dbo.T_Kala.GOKID, dbo.T_Kala.AID, dbo.T_NameKala.NameKala1, dbo.T_Anbar.NameAnbar, @KALAID AS Expr1, @GOKID AS Expr2, @AID AS Expr3, 
               dbo.T_NameGoKala.NameGoKala1, dbo.T_NameKala.KalaNumber
FROM     dbo.T_Kala INNER JOIN
               dbo.T_NameKala ON dbo.T_Kala.NameKalaID = dbo.T_NameKala.ID INNER JOIN
               dbo.T_Anbar ON dbo.T_Kala.AID = dbo.T_Anbar.AID INNER JOIN
               dbo.T_NameGoKala ON dbo.T_Kala.GOKID = dbo.T_NameGoKala.ID
WHERE  (@KALAID IS NULL) AND (@GOKID IS NULL) AND (@AID IS NULL) OR
               (@KALAID IS NOT NULL) AND (@GOKID IS NULL) AND (@AID IS NULL) AND (dbo.T_Kala.NameKalaID = @KALAID) OR
               (@KALAID IS NULL) AND (@GOKID IS NOT NULL) AND (@AID IS NULL) AND (dbo.T_Kala.GOKID = @GOKID) OR
               (@KALAID IS NOT NULL) AND (@GOKID IS NOT NULL) AND (@AID IS NULL) AND (dbo.T_Kala.NameKalaID = @KALAID) AND (dbo.T_Kala.GOKID = @GOKID) OR
               (@KALAID IS NULL) AND (@GOKID IS NULL) AND (@AID IS NOT NULL) AND (dbo.T_Kala.AID = @AID) OR
               (@KALAID IS NOT NULL) AND (@GOKID IS NULL) AND (@AID IS NOT NULL) AND (dbo.T_Kala.NameKalaID = @KALAID) AND (dbo.T_Kala.AID = @AID) OR
               (@KALAID IS NULL) AND (@GOKID IS NOT NULL) AND (@AID IS NOT NULL) AND (dbo.T_Kala.GOKID = @GOKID) AND (dbo.T_Kala.AID = @AID) OR
               (@KALAID IS NOT NULL) AND (@GOKID IS NOT NULL) AND (@AID IS NOT NULL) AND (dbo.T_Kala.NameKalaID = @KALAID) AND (dbo.T_Kala.GOKID = @GOKID) AND (dbo.T_Kala.AID = @AID)
ORDER BY dbo.T_NameKala.NameKala1, dbo.T_Anbar.NameAnbar, dbo.T_NameGoKala.NameGoKala1 )

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_Append_EditGroup]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'       CREATE FUNCTION [F_Append_EditGroup] (@AID int, @GOKID int, @KalaID int, @userID nvarchar(50)) RETURNS TABLE AS RETURN (SELECT     dbo.T_Kala.KALAID, dbo.T_NameKala.NameKala1, dbo.T_NameGoKala.NameGoKala1, dbo.T_Anbar.NameAnbar, dbo.T_NameKala.BarCode,                        dbo.T_Kala.Flag1, dbo.T_NameKala.KalaNumber, dbo.T_Kala.AID, dbo.T_Kala.GOKID, dbo.T_Kala.NameKalaID FROM         dbo.T_Kala INNER JOIN                       dbo.T_NameKala ON dbo.T_Kala.NameKalaID = dbo.T_NameKala.ID INNER JOIN                       dbo.T_NameGoKala ON dbo.T_Kala.GOKID = dbo.T_NameGoKala.ID INNER JOIN                       dbo.T_Anbar ON dbo.T_Kala.AID = dbo.T_Anbar.AID WHERE     (@AID IS NOT NULL) AND (@GOKID IS NULL) AND (@KalaID IS NULL) AND (dbo.T_Kala.AID = @AID) AND (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) OR                       (@AID IS NULL) AND (@GOKID IS NOT NULL) AND (@KalaID IS NULL) AND (dbo.T_Kala.GOKID = @GOKID) AND (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) OR                       (@AID IS NOT NULL) AND (@GOKID IS NOT NULL) AND (@KalaID IS NULL) AND (dbo.T_Kala.AID = @AID) AND (dbo.T_Kala.GOKID = @GOKID) AND                        (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) OR                       (@AID IS NULL) AND (@GOKID IS NULL) AND (@KalaID IS NOT NULL) AND (dbo.T_Kala.NameKalaID = @KalaID) AND                        (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) OR                       (@AID IS NOT NULL) AND (@GOKID IS NULL) AND (@KalaID IS NOT NULL) AND (dbo.T_Kala.AID = @AID) AND (dbo.T_Kala.NameKalaID = @KalaID)                        AND (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) OR                       (@AID IS NULL) AND (@GOKID IS NOT NULL) AND (@KalaID IS NOT NULL) AND (dbo.T_Kala.GOKID = @GOKID) AND                        (dbo.T_Kala.NameKalaID = @KalaID) AND (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) OR                       (@AID IS NOT NULL) AND (@GOKID IS NOT NULL) AND (@KalaID IS NOT NULL) AND (dbo.T_Kala.AID = @AID) AND (dbo.T_Kala.GOKID = @GOKID)                        AND (dbo.T_Kala.NameKalaID = @KalaID) AND (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID))))  )     ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MabHazine_Sandogh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [F_MabHazine_Sandogh]() RETURNS TABLE AS RETURN ( SELECT     SUM(DARBed) AS mabhazine, FieldN5 AS Factid, FieldN6, Flag1 FROM         dbo.TRANSACTION2 WHERE     (NOT (FieldT1 IS NULL)) AND (NOT (FieldT2 IS NULL)) GROUP BY FieldN5, FieldN6, Flag1 HAVING      (FieldN6 = 30) AND (Flag1 = 0) )   ' 
END



/****** Object:  UserDefinedFunction [dbo].[F_MabHazine_KharidBarKHarid]    Script Date: 02/19/2018 11:36:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F_MabHazine_KharidBarKHarid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F_MabHazine_KharidBarKHarid]
GO



/****** Object:  UserDefinedFunction [dbo].[F_MabHazine_KharidBarKHarid]    Script Date: 02/19/2018 11:36:32 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

      CREATE FUNCTION [dbo].[F_MabHazine_KharidBarKHarid]() RETURNS TABLE AS RETURN ( 
      	
      	SELECT     FactID, TypeFact, SUM(MabHazine) AS MabHazine
FROM         Table4
WHERE     (Flag1 = 0)
GROUP BY FactID, TypeFact
HAVING      (TypeFact = 3)
      
      )   
GO



/****** Object:  UserDefinedFunction [dbo].[F_MabHazine_BarForosh]    Script Date: 02/22/2018 09:08:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F_MabHazine_BarForosh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F_MabHazine_BarForosh]
GO


/****** Object:  UserDefinedFunction [dbo].[F_MabHazine_BarForosh]    Script Date: 02/22/2018 09:08:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

      create FUNCTION [dbo].[F_MabHazine_BarForosh]() RETURNS TABLE AS RETURN ( 
      	
      	SELECT     FactID, TypeFact, SUM(MabHazine) AS MabHazine
FROM         Table4
WHERE     (Flag1 = 0)
GROUP BY FactID, TypeFact
HAVING      (TypeFact = 4)
      
      )   
GO




GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[PFrm_Rep_SoodKala_MojoodiEbtedaDore]'))
EXEC dbo.sp_executesql @statement = N'  CREATE VIEW [PFrm_Rep_SoodKala_MojoodiEbtedaDore] AS SELECT     NameKalaID, SUM(CONVERT(float, Mojodi)) AS Mojodi, SUM(UnitPrice) AS SumMablagh FROM         dbo.T_Kala GROUP BY NameKalaID '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_Pfrm_Virayesh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'    CREATE FUNCTION [F_Pfrm_Virayesh](@IDA int, @KalaGo int, @KalaID int, @Bcode nvarchar(255)) RETURNS TABLE AS RETURN ( SELECT     dbo.T_Kala.AID, dbo.T_Kala.GOKID, dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1, dbo.T_NameKala.VAHED2,                        dbo.T_NameKala.Active, dbo.T_NameKala.MinPrice, dbo.T_NameKala.MaxPrice, dbo.T_NameKala.Zarib, dbo.T_NameKala.MinMogoody,                        dbo.T_NameKala.MaxMogoody, dbo.T_NameKala.BarCode, dbo.T_NameKala.UnitPrice1, dbo.T_NameKala.UnitPrice2, dbo.T_NameKala.UnitPrice3,                        dbo.T_NameKala.KalaNumber FROM         dbo.T_Kala INNER JOIN                       dbo.T_NameKala ON dbo.T_Kala.NameKalaID = dbo.T_NameKala.ID WHERE     (@IDA IS NULL) AND (@KalaGo IS NULL) AND (@KalaID IS NULL) AND (@Bcode IS NULL) OR                       (@IDA IS NOT NULL) AND (@KalaGo IS NULL) AND (@KalaID IS NULL) AND (dbo.T_Kala.AID = @IDA) AND (@Bcode IS NULL) OR                       (@IDA IS NULL) AND (@KalaGo IS NOT NULL) AND (@KalaID IS NULL) AND (dbo.T_Kala.GOKID = @KalaGo) AND (@Bcode IS NULL) OR                       (@IDA IS NOT NULL) AND (@KalaGo IS NOT NULL) AND (@KalaID IS NULL) AND (dbo.T_Kala.AID = @IDA) AND (dbo.T_Kala.GOKID = @KalaGo) AND                        (@Bcode IS NULL) OR                       (@IDA IS NULL) AND (@KalaGo IS NULL) AND (@KalaID IS NOT NULL) AND (dbo.T_NameKala.ID = @KalaID) AND (@Bcode IS NULL) OR                       (@IDA IS NOT NULL) AND (@KalaGo IS NULL) AND (@KalaID IS NOT NULL) AND (dbo.T_Kala.AID = @IDA) AND (dbo.T_NameKala.ID = @KalaID) AND                        (@Bcode IS NULL) OR                       (@IDA IS NULL) AND (@KalaGo IS NOT NULL) AND (@KalaID IS NOT NULL) AND (dbo.T_Kala.GOKID = @KalaGo) AND (dbo.T_NameKala.ID = @KalaID)                        AND (@Bcode IS NULL) OR                       (@IDA IS NOT NULL) AND (@KalaGo IS NOT NULL) AND (@KalaID IS NOT NULL) AND (dbo.T_Kala.AID = @IDA) AND (dbo.T_Kala.GOKID = @KalaGo)                        AND (dbo.T_NameKala.ID = @KalaID) AND (@Bcode IS NULL) OR                       (@IDA IS NULL) AND (@KalaGo IS NULL) AND (@KalaID IS NULL) AND (dbo.T_NameKala.ID IS NULL) AND (NOT (@Bcode IS NULL)) AND                        (dbo.T_NameKala.BarCode = @Bcode) )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MojoodiKalaEbtedaDoreGrp]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [F_MojoodiKalaEbtedaDoreGrp](@GokID int, @AID int) RETURNS TABLE AS RETURN ( SELECT     dbo.T_Kala.GOKID, SUM(ROUND(dbo.T_Kala.Mojodi, 2)) AS Mojodi, dbo.T_NameGoKala.NameGoKala1, dbo.T_NameGoKala.NameGoKalanumbr,                        @GokID AS Expr1 FROM         dbo.T_Kala INNER JOIN                       dbo.T_NameGoKala ON dbo.T_Kala.GOKID = dbo.T_NameGoKala.ID WHERE     (@AID IS NULL) OR                       (@AID IS NOT NULL) AND (dbo.T_Kala.AID = @AID) OR                       (@AID IS NULL) OR                       (@AID IS NOT NULL) AND (dbo.T_Kala.AID = @AID) GROUP BY dbo.T_NameGoKala.NameGoKala1, dbo.T_NameGoKala.NameGoKalanumbr, dbo.T_Kala.GOKID HAVING      (@GokID IS NULL) OR                       (@GokID IS NULL) OR                       (@GokID IS NOT NULL) AND (dbo.T_Kala.GOKID = @GokID) OR                       (@GokID IS NOT NULL) AND (dbo.T_Kala.GOKID = @GokID) )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MojoodiKalaEbtedaDore]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [F_MojoodiKalaEbtedaDore](@aid int, @GokID int) RETURNS TABLE AS RETURN ( SELECT     dbo.T_Kala.NameKalaID, SUM(dbo.T_Kala.Mojodi) AS Mojodi, dbo.T_NameKala.NameKala1, dbo.T_NameKala.KalaNumber,                        dbo.T_NameKala.VAHED1 FROM         dbo.T_Kala INNER JOIN                       dbo.T_NameKala ON dbo.T_Kala.NameKalaID = dbo.T_NameKala.ID WHERE     (@AID IS NULL) AND (@GokID IS NULL) OR                       (@AID IS NOT NULL) AND (@GokID IS NULL) AND (dbo.T_Kala.AID = @AID) OR                       (@AID IS NULL) AND (@GokID IS NOT NULL) AND (dbo.T_Kala.GOKID = @GokID) OR                       (@AID IS NOT NULL) AND (@GokID IS NOT NULL) AND (dbo.T_Kala.AID = @AID) AND (dbo.T_Kala.GOKID = @GokID) GROUP BY dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.VAHED1 )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[M_EbtedaDore]'))
EXEC dbo.sp_executesql @statement = N'  CREATE VIEW [M_EbtedaDore] AS SELECT     NameKalaID, SUM(CONVERT(float, Mojodi)) AS Mojodi, SUM(Mojodi * UnitPrice) AS MojodiRiali FROM         dbo.T_Kala GROUP BY NameKalaID '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListMoinHesabNotLimited_sood]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QListMoinHesabNotLimited_sood](@kolid int) AS SELECT     TOP 100 PERCENT dbo.T_Heasb.HESABID, dbo.T_Heasb.NameHesab, dbo.T_Heasb.KolID, dbo.T_Heasb.GOID, CONVERT(money,                        ABS(CONVERT(money, ISNULL(dbo.T_Heasb.Balance, 0)) + CONVERT(money, ISNULL(dbo.QMandeTafsiliHesab1.MandeTafsili, 0)))) AS Expr1,                        CASE WHEN (CONVERT(money, CONVERT(money, ISNULL(T_Heasb.Balance, 0)) + CONVERT(money, ISNULL(QMandeTafsiliHesab1.MandeTafsili,                        0)))) > 0 THEN N''بد'' WHEN (CONVERT(money, CONVERT(money, ISNULL(T_Heasb.Balance, 0)) + CONVERT(money,                        ISNULL(QMandeTafsiliHesab1.MandeTafsili, 0)))) < 0 THEN N''بس'' ELSE ''--'' END AS Expr2, RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_Heasb.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Heasb.KolID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_Heasb.HESABID), 4) AS Expr3 FROM         dbo.T_Heasb LEFT OUTER JOIN                       dbo.QMandeTafsiliHesab1 ON dbo.T_Heasb.HESABID = dbo.QMandeTafsiliHesab1.HESABID WHERE     (@kolid IS NOT NULL) AND (dbo.T_Heasb.KolID = @kolid) AND (dbo.T_Heasb.GOID = 8) OR                       (@kolid IS NULL) AND (dbo.T_Heasb.GOID = 9) ORDER BY dbo.T_Heasb.NameHesab  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListMoinHesabNotLimited]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QListMoinHesabNotLimited](@kolid int) AS SELECT     TOP 100 PERCENT dbo.T_Heasb.HESABID, dbo.T_Heasb.NameHesab, dbo.T_Heasb.KolID, dbo.T_Heasb.GOID, CONVERT(money,                        ABS(CONVERT(money, ISNULL(dbo.T_Heasb.Balance, 0)) + CONVERT(money, ISNULL(dbo.QMandeTafsiliHesab1.MandeTafsili, 0)))) AS Expr1,                        CASE WHEN (CONVERT(money, CONVERT(money, ISNULL(T_Heasb.Balance, 0)) + CONVERT(money, ISNULL(QMandeTafsiliHesab1.MandeTafsili,                        0)))) > 0 THEN N''بد'' WHEN (CONVERT(money, CONVERT(money, ISNULL(T_Heasb.Balance, 0)) + CONVERT(money,                        ISNULL(QMandeTafsiliHesab1.MandeTafsili, 0)))) < 0 THEN N''بس'' ELSE ''--'' END AS Expr2, RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_Heasb.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Heasb.KolID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_Heasb.HESABID), 4) AS Expr3 FROM         dbo.T_Heasb LEFT OUTER JOIN                       dbo.QMandeTafsiliHesab1 ON dbo.T_Heasb.HESABID = dbo.QMandeTafsiliHesab1.HESABID WHERE     (dbo.T_Heasb.KolID = @kolid) AND (@kolid IS NOT NULL) OR                       (@kolid IS NULL) ORDER BY dbo.T_Heasb.NameHesab   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QRepKhadamat3]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'    CREATE PROCEDURE [QRepKhadamat3] AS
SELECT     DKHID, DKHNumbr, NumberKH, GOID, KOLID, HesabID, PrimarySaid2, THESABID, TextName, dbo.FunShFishKhadamat(DKHID) 
                      AS ShFish
FROM         T_DKhad

' 

END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListMOinHesab_HazineFact]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QListMOinHesab_HazineFact](@FieldN2 int) AS SELECT     TOP 100 PERCENT dbo.T_Heasb.HESABID, dbo.T_Heasb.NameHesab, dbo.T_Heasb.KolID, dbo.T_Heasb.GOID, CONVERT(money,                        ABS(CONVERT(money, ISNULL(dbo.T_Heasb.Balance, 0)) + CONVERT(money, ISNULL(dbo.QMandeTafsiliHesab1.MandeTafsili, 0)))) AS Expr1,                        CASE WHEN (CONVERT(money, CONVERT(money, ISNULL(T_Heasb.Balance, 0)) + CONVERT(money, ISNULL(QMandeTafsiliHesab1.MandeTafsili,                        0)))) > 0 THEN N''بد'' WHEN (CONVERT(money, CONVERT(money, ISNULL(T_Heasb.Balance, 0)) + CONVERT(money,                        ISNULL(QMandeTafsiliHesab1.MandeTafsili, 0)))) < 0 THEN N''بس'' ELSE ''--'' END AS Expr2 FROM         dbo.T_Heasb LEFT OUTER JOIN                       dbo.QMandeTafsiliHesab1 ON dbo.T_Heasb.HESABID = dbo.QMandeTafsiliHesab1.HESABID WHERE     (dbo.T_Heasb.KolID = @FieldN2) ORDER BY dbo.T_Heasb.NameHesab  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListKolHesabNotLimited_sood]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QListKolHesabNotLimited_sood](@Goid int) AS SELECT     TOP 100 PERCENT dbo.T_KolHesab.KOLID, dbo.T_KolHesab.NameHesab, dbo.T_KolHesab.GOID, CONVERT(money, ABS(CONVERT(money,                        ISNULL(dbo.T_KolHesab.Balance, 0)) + CONVERT(money, ISNULL(dbo.QMandeMoinHesab.MandeMoin, 0)) + CONVERT(money,                        ISNULL(dbo.QMandeTafsiliHesab.MandeTafsili, 0)))) AS Expr1, CASE WHEN (CONVERT(money, CONVERT(money, ISNULL(dbo.T_KolHesab.Balance,                        0)) + CONVERT(money, ISNULL(dbo.QMandeMoinHesab.MandeMoin, 0)) + CONVERT(money, ISNULL(dbo.QMandeTafsiliHesab.MandeTafsili, 0))))                        > 0 THEN N''بد'' WHEN (CONVERT(money, CONVERT(money, ISNULL(dbo.T_KolHesab.Balance, 0)) + CONVERT(money,                        ISNULL(dbo.QMandeMoinHesab.MandeMoin, 0)) + CONVERT(money, ISNULL(dbo.QMandeTafsiliHesab.MandeTafsili, 0))))                        < 0 THEN N''بس'' ELSE ''--'' END AS Expr2, RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.KOLID), 4) AS Expr3 FROM         dbo.T_KolHesab LEFT OUTER JOIN                       dbo.QMandeMoinHesab ON dbo.T_KolHesab.KOLID = dbo.QMandeMoinHesab.KolID LEFT OUTER JOIN                       dbo.QMandeTafsiliHesab ON dbo.T_KolHesab.KOLID = dbo.QMandeTafsiliHesab.KolID WHERE     (@Goid IS NOT NULL) AND (dbo.T_KolHesab.GOID = @Goid) OR                       (@Goid IS NULL) AND (dbo.T_KolHesab.GOID = 8) OR                       (dbo.T_KolHesab.GOID = 9) ORDER BY dbo.T_KolHesab.NameHesab  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqBargashti]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

      CREATE PROCEDURE [QSubPFrm_ListCheqBargashti] AS SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 2) OR                       (dbo.T_Asnad_Dar.FieldN1 = 6) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC

' 
END
GO


/****** Object:  View [dbo].[View_RepKOL1]    Script Date: 12/16/2017 10:55:41 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_RepKOL1]'))
DROP VIEW [dbo].[View_RepKOL1]
GO



/****** Object:  View [dbo].[View_RepKOL1]    Script Date: 12/16/2017 10:55:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[View_RepKOL1]
AS
SELECT     dbo.[TRANSACTION].Date1, dbo.T_KolHesab.KOLID, dbo.T_SA2.SAID2Number, dbo.[TRANSACTION].DARBed, dbo.[TRANSACTION].PARBes, dbo.[TRANSACTION].Flag2, dbo.[TRANSACTION].Sharh, 
                      dbo.[TRANSACTION].TextShSA, dbo.T_KolHesab.NameHesab, dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes AS mande, dbo.[TRANSACTION].TEMPR, dbo.[TRANSACTION].TRAID, 
                      dbo.[TRANSACTION].TEMP, dbo.T_SA2.SAID2, dbo.[TRANSACTION].FlagPrint
FROM         dbo.[TRANSACTION] INNER JOIN
                      dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 INNER JOIN
                      dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID
WHERE     (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL)

GO
/****** Object:  View [dbo].[View_RepMO1]    Script Date: 12/16/2017 10:55:44 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_RepMO1]'))
DROP VIEW [dbo].[View_RepMO1]
GO



/****** Object:  View [dbo].[View_RepMO1]    Script Date: 12/16/2017 10:55:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[View_RepMO1]
AS
SELECT     dbo.[TRANSACTION].Date1, dbo.T_SA2.SAID2Number, dbo.[TRANSACTION].DARBed, dbo.[TRANSACTION].PARBes, dbo.[TRANSACTION].Flag2, dbo.[TRANSACTION].Sharh, 
                      dbo.[TRANSACTION].TextShSA, dbo.T_Heasb.NameHesab, dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes AS mande, dbo.[TRANSACTION].TEMPR, dbo.[TRANSACTION].TRAID, 
                      dbo.[TRANSACTION].TEMP, dbo.T_Heasb.HESABID, dbo.T_SA2.SAID2, dbo.[TRANSACTION].FlagPrint
FROM         dbo.[TRANSACTION] INNER JOIN
                      dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 INNER JOIN
                      dbo.T_Heasb ON dbo.[TRANSACTION].HESABID = dbo.T_Heasb.HESABID
WHERE     (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL)

GO


/****** Object:  View [dbo].[View_RepTH1]    Script Date: 12/16/2017 10:55:50 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[View_RepTH1]'))
DROP VIEW [dbo].[View_RepTH1]
GO



/****** Object:  View [dbo].[View_RepTH1]    Script Date: 12/16/2017 10:55:50 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[View_RepTH1]
AS
SELECT     dbo.[TRANSACTION].Date1, dbo.T_SA2.SAID2Number, dbo.[TRANSACTION].DARBed, dbo.[TRANSACTION].PARBes, dbo.[TRANSACTION].Flag2, dbo.[TRANSACTION].Sharh, 
                      dbo.[TRANSACTION].TextShSA, dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes AS mande, dbo.[TRANSACTION].TEMPR, dbo.[TRANSACTION].TRAID, dbo.[TRANSACTION].TEMP, 
                      dbo.[TRANSACTION].HESABID, dbo.T_T_Heasb.THESABID, dbo.T_T_Heasb.NameHesabT AS namehesab, dbo.T_SA2.SAID2, dbo.[TRANSACTION].ThesabIDSath2, dbo.[TRANSACTION].FlagPrint
FROM         dbo.[TRANSACTION] INNER JOIN
                      dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 INNER JOIN
                      dbo.T_T_Heasb ON dbo.[TRANSACTION].THESABID = dbo.T_T_Heasb.THESABID
WHERE     (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL)

GO



/****** Object:  View [dbo].[QSubPFrm_SanadHesabdary]    Script Date: 10/21/2017 16:44:47 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdary]'))
DROP VIEW [dbo].[QSubPFrm_SanadHesabdary]
GO


/****** Object:  View [dbo].[QSubPFrm_SanadHesabdary]    Script Date: 10/21/2017 16:44:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE VIEW [dbo].[QSubPFrm_SanadHesabdary]
AS
SELECT     TOP (100) PERCENT TRID, Flag1, Flag2, Date1, Sharh, SAID, HESABID, PARBes, DARBed, radif, GOID, KOLID, THESABID, FieldT1, TextNumberSA, TextShSA, FieldN1, TRAID, Flag3, TextNHSID2, 
                      CurMablagh, CurRef, CurRate, FieldT2, ThesabIDSath2
FROM         dbo.TRANSACTION2
ORDER BY radif

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QSubPfrm_Khadamat]'))
EXEC dbo.sp_executesql @statement = N'
CREATE VIEW [QSubPfrm_Khadamat]
AS
SELECT     TOP (100) PERCENT AID, KALAID, Sharh, Date1, KHARIDID, Tedad, UnitPrice, CONVERT(money, Tedad * UnitPrice) AS Sumation, radif, GOKID, CONVERT(nvarchar(20), AID) 
                      + CONVERT(nvarchar(20), GOKID) + CONVERT(nvarchar(20), KALAID) AS Expr1, LinkedID, TextKalaName, SumofPrice, FieldN6, OperType, DARBed, PARBes
FROM         dbo.TRANSACTION2
ORDER BY radif

'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[QSub_subform_T1]'))
EXEC dbo.sp_executesql @statement = N'      CREATE VIEW [QSub_subform_T1] AS SELECT     TOP 100 PERCENT dbo.T_Kala.NameKalaID, dbo.T_NameKala.Model, dbo.T_NameKala.NameKala1, dbo.T_Kala.UnitPrice, dbo.T_Kala.Unitfo,                        dbo.T_Kala.KALAID, dbo.T_Kala.FieldN1, dbo.T_Kala.FieldN2, dbo.T_Kala.GOKID, dbo.T_Kala.AID, dbo.T_NameKala.KalaNumber FROM         dbo.T_NameKala INNER JOIN                       dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID ORDER BY dbo.T_NameKala.NameKala1  '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqNoPass]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

      CREATE PROCEDURE [QSubPFrm_ListCheqNoPass] AS SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 4) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqPass]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

      CREATE PROCEDURE [QSubPFrm_ListCheqPass] AS SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 1) OR                       (dbo.T_Asnad_Dar.FieldN1 = 5) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC

' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListNameKala]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

  CREATE PROCEDURE [QSubPFrm_ListNameKala] AS 
  
SELECT     ID, KalaNumber, DateCreate, DateModified, NameKala1, Active, VAHED1, VAHED2, MinPrice, MaxPrice, MinMogoody, ABS(Zarib) AS Zarib, MaxMogoody, BarCode, note, UnitPrice1, UnitPrice2, 
                      UnitPrice3, UnitPrice4, UnitPrice5, UnitPrice6, DarsadAmel1, Currency1, Currency2, Currency3, Currency4, Currency5, Model, Currency6, CONVERT(int, KalaNumber) AS Expr1, 
                      NameVahed1 AS NameVahed, NameVahed2, NameGoKala1, CASE WHEN Isnull(FlagNoeKala, 0) = 0 THEN N''کالا'' ELSE N''خدمت'' END AS NoeKala
FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1
GROUP BY ID, KalaNumber, DateCreate, DateModified, NameKala1, VAHED1, VAHED2, MinPrice, MaxPrice, MinMogoody, ABS(Zarib), MaxMogoody, BarCode, note, UnitPrice1, UnitPrice2, UnitPrice3, 
                      UnitPrice4, UnitPrice5, UnitPrice6, DarsadAmel1, Currency1, Currency2, Currency3, Currency4, Currency5, Model, Currency6, CONVERT(int, KalaNumber), NameVahed1, NameVahed2, 
                      NameGoKala1, Active, CASE WHEN Isnull(FlagNoeKala, 0) = 0 THEN N''کالا'' ELSE N''خدمت'' END
ORDER BY Expr1 DESC


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqVosooly]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

      CREATE PROCEDURE [QSubPFrm_ListCheqVosooly] AS SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 3) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPfrm_RaasChek]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

  CREATE PROCEDURE [QSubPfrm_RaasChek] (@StartDate nvarchar(50), @LinkIdtosub nvarchar(255), @FAddDay1 int = 0) AS SELECT     TOP 100 PERCENT AID, KALAID, Sharh, Date1, KHARIDID, Tedad, UnitPrice, CONVERT(money, DATEDIFF(day, DATEADD(day, @FAddDay1,                        CAST(dbo.PersianToGregorian(SUBSTRING(@StartDate, 1, 4) + ''/'' + SUBSTRING(@StartDate, 5, 2) + ''/'' + SUBSTRING(@StartDate, 7, 2)) AS datetime)),                        CAST(dbo.PersianToGregorian(SUBSTRING(TextKalaName, 1, 4) + ''/'' + SUBSTRING(TextKalaName, 5, 2) + ''/'' + SUBSTRING(TextKalaName, 7, 2)) AS datetime))                        * UnitPrice) AS Sumation, radif, GOKID, CONVERT(nvarchar(20), AID) + CONVERT(nvarchar(20), GOKID) + CONVERT(nvarchar(20), KALAID) AS Expr1, LinkedID,                        TextKalaName, SumofPrice, FieldN6, OperType, @StartDate AS Expr2, DATEDIFF(day, DATEADD(day, @FAddDay1,                        CAST(dbo.PersianToGregorian(SUBSTRING(@StartDate, 1, 4) + ''/'' + SUBSTRING(@StartDate, 5, 2) + ''/'' + SUBSTRING(@StartDate, 7, 2)) AS datetime)),                        CAST(dbo.PersianToGregorian(SUBSTRING(TextKalaName, 1, 4) + ''/'' + SUBSTRING(TextKalaName, 5, 2) + ''/'' + SUBSTRING(TextKalaName, 7, 2)) AS datetime))                        AS ZamanMandeh, TextAIDKh, @FAddDay1 AS Expr3 FROM         dbo.TRANSACTION2 WHERE     (Date1 = @LinkIdtosub) ORDER BY radif

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_Pardakhtchek]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'  CREATE PROCEDURE [QSubPFrm_Pardakhtchek] (@TxtLinkTosub nvarchar(255)) AS SELECT     PARID4, KOLID, HESABID, THESABID, FieldN1, FieldN2, FieldN3, FieldN4, DARBed, Flag1, FieldT1, FieldT2, TEMPR, FieldN5, GOID, FieldN6, Sharh, PARID2, [TEMP], Date1 FROM         dbo.TRANSACTION2 WHERE     (Date1 = @TxtLinkTosub)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListPishForoosh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

        CREATE PROCEDURE [QSubPFrm_ListPishForoosh] AS SELECT     FOROSHID, HesabID, GOID, KOLID, MOINID, THESABID, TextName, TextName1, TextNameT, NumberKH, DateFo, Takhfif, TextShSA, NumSID2, Sharh,                        FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT3 AS Expr1, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8,                        ForoshNumbr, PrimarySaid2, THESABIDAmel, dbo.HesabName(KOLID, HesabID, THESABID) AS namehesab FROM         dbo.T_PishFactor ORDER BY FOROSHID DESC 

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_SanadHesabdari_Bed_Bes_CALC]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'


CREATE  PROCEDURE [QSubPFrm_SanadHesabdari_Bed_Bes_CALC]

		@LinkSubID NVARCHAR(255),
        @SUMBed DECIMAL(19,4) =0 OUTPUT,
        @SUMBes DECIMAL(19,4) =0 OUTPUT,
        @SUMArz DECIMAL(19,4) =0 OUTPUT
AS
BEGIN

	
SELECT     @SUMBes=SUM(PARBes) , @SUMBed=SUM(DARBed ),@SUMArz=SUM(CurMablagh)
FROM         TRANSACTION2
WHERE     (Date1 = @LinkSubID)


							  
END






' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSub_MSG_PReviewListCHeckForRunProgramDAR]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

    CREATE PROCEDURE [QSub_MSG_PReviewListCHeckForRunProgramDAR](@Baze nvarchar(50), @AID nvarchar(50)) AS 

SELECT     ASNADDARID, Sharh, Mablagh, DateTA, DateVO, ShSeryal, FieldN1, GOID, KOLID, HESABID, THESABID, dbo.HesabName(KOLID, HESABID, THESABID) AS Daryaft_az_Hesab, 
                      CASE WHEN (T_Asnad_Dar.[FieldN1]) = 1 THEN ''پاس شده'' WHEN (T_Asnad_Dar.[FieldN1]) = 2 THEN ''برگشتي '' WHEN (T_Asnad_Dar.[FieldN1]) 
                      = 3 THEN ''در جریان وصول '' WHEN (T_Asnad_Dar.[FieldN1]) = 4 THEN ''پاس نشده'' WHEN (T_Asnad_Dar.[FieldN1]) = 5 THEN ''پاس شده '' WHEN (T_Asnad_Dar.[FieldN1]) 
                      = 6 THEN '' برگشتی'' END AS chkvaz, DARID3, AsnadDarNumber, Namebank, ShHesab, @AID AS Expr3
FROM         T_Asnad_Dar
WHERE     (FieldN1 = 4) and
   (DateVO <= @Baze)


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_listKala_EditGroup_Sub2]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_listKala_EditGroup_Sub2](@userID int) AS SELECT     ID1, ID2, FieldT1, FieldT2, FieldT3, FieldT4, FieldT5 FROM         dbo.TRANSACTION2 WHERE     (ID1 = @userID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_EditGroup]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListKala_EditGroup](@AID int, @GOKID int, @KalaID int, @userID nvarchar(50)) AS SELECT     dbo.T_Kala.KALAID, dbo.T_NameKala.NameKala1, dbo.T_NameGoKala.NameGoKala1, dbo.T_Anbar.NameAnbar, dbo.T_NameKala.BarCode,                        dbo.T_Kala.Flag1, dbo.T_NameKala.KalaNumber, dbo.T_Kala.AID, dbo.T_Kala.GOKID, dbo.T_Kala.NameKalaID FROM         dbo.T_Kala INNER JOIN                       dbo.T_NameKala ON dbo.T_Kala.NameKalaID = dbo.T_NameKala.ID INNER JOIN                       dbo.T_NameGoKala ON dbo.T_Kala.GOKID = dbo.T_NameGoKala.ID INNER JOIN                       dbo.T_Anbar ON dbo.T_Kala.AID = dbo.T_Anbar.AID WHERE     (@AID IS NOT NULL) AND (@GOKID IS NULL) AND (@KalaID IS NULL) AND (dbo.T_Kala.AID = @AID) AND (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) OR                       (@AID IS NULL) AND (@GOKID IS NOT NULL) AND (@KalaID IS NULL) AND (dbo.T_Kala.GOKID = @GOKID) AND (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) OR                       (@AID IS NOT NULL) AND (@GOKID IS NOT NULL) AND (@KalaID IS NULL) AND (dbo.T_Kala.AID = @AID) AND (dbo.T_Kala.GOKID = @GOKID) AND                        (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) OR                       (@AID IS NULL) AND (@GOKID IS NULL) AND (@KalaID IS NOT NULL) AND (dbo.T_Kala.NameKalaID = @KalaID) AND                        (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) OR                       (@AID IS NOT NULL) AND (@GOKID IS NULL) AND (@KalaID IS NOT NULL) AND (dbo.T_Kala.AID = @AID) AND (dbo.T_Kala.NameKalaID = @KalaID)                        AND (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) OR                       (@AID IS NULL) AND (@GOKID IS NOT NULL) AND (@KalaID IS NOT NULL) AND (dbo.T_Kala.GOKID = @GOKID) AND                        (dbo.T_Kala.NameKalaID = @KalaID) AND (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) OR                       (@AID IS NOT NULL) AND (@GOKID IS NOT NULL) AND (@KalaID IS NOT NULL) AND (dbo.T_Kala.AID = @AID) AND (dbo.T_Kala.GOKID = @GOKID)                        AND (dbo.T_Kala.NameKalaID = @KalaID) AND (NOT (dbo.T_Kala.KALAID IN                           (SELECT     ID2                              FROM         dbo.TRANSACTION2                              WHERE     (ID1 = @userID)))) ORDER BY dbo.T_NameKala.NameKala1  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListGroupKala]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [Q_ListGroupKala](@AID int)
AS SELECT  dbo.T_Kala.GOKID, dbo.T_NameGoKala.NameGoKala1, dbo.T_Kala.AID
FROM     dbo.T_Kala INNER JOIN
               dbo.T_NameGoKala ON dbo.T_Kala.GOKID = dbo.T_NameGoKala.ID
GROUP BY dbo.T_Kala.GOKID, dbo.T_NameGoKala.NameGoKala1, dbo.T_Kala.AID
HAVING  (dbo.T_Kala.AID = @AID)
ORDER BY dbo.T_NameGoKala.NameGoKala1' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListAnbar_Mojoodi]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListAnbar_Mojoodi](@Kalaid int) AS SELECT     dbo.T_Anbar.AID, dbo.T_Anbar.NameAnbar, dbo.T_Kala.NameKalaID FROM         dbo.T_Anbar INNER JOIN                       dbo.T_Kala ON dbo.T_Anbar.AID = dbo.T_Kala.AID WHERE     (dbo.T_Kala.NameKalaID = @Kalaid) ORDER BY dbo.T_Anbar.NameAnbar  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Pfrm_Virayesh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_Pfrm_Virayesh](@IDA int, @KalaGo int, @KalaName nvarchar(255)) AS SELECT     dbo.T_Kala.AID, dbo.T_Kala.GOKID, dbo.T_NameKala.NameKala1 FROM         dbo.T_Kala INNER JOIN                       dbo.T_NameKala ON dbo.T_Kala.NameKalaID = dbo.T_NameKala.ID WHERE     (@IDA IS NULL) AND (@KalaGo IS NULL) AND (@KalaName IS NULL) OR                       (@IDA IS NOT NULL) AND (@KalaGo IS NULL) AND (@KalaName IS NULL) AND (dbo.T_Kala.AID = @IDA) OR                       (@IDA IS NULL) AND (@KalaGo IS NOT NULL) AND (@KalaName IS NULL) AND (dbo.T_Kala.GOKID = @KalaGo) OR                       (@IDA IS NOT NULL) AND (@KalaGo IS NOT NULL) AND (@KalaName IS NULL) AND (dbo.T_Kala.AID = @IDA) AND (dbo.T_Kala.GOKID = @KalaGo) OR                       (@IDA IS NULL) AND (@KalaGo IS NULL) AND (@KalaName IS NOT NULL) AND (dbo.T_NameKala.NameKala1 = @KalaName) OR                       (@IDA IS NOT NULL) AND (@KalaGo IS NULL) AND (@KalaName IS NOT NULL) AND (dbo.T_Kala.AID = @IDA) AND                        (dbo.T_NameKala.NameKala1 = @KalaName) OR                       (@IDA IS NULL) AND (@KalaGo IS NOT NULL) AND (@KalaName IS NOT NULL) AND (dbo.T_Kala.GOKID = @KalaGo) AND                        (dbo.T_NameKala.NameKala1 = @KalaName) OR                       (@IDA IS NOT NULL) AND (@KalaGo IS NOT NULL) AND (@KalaName IS NOT NULL) AND (dbo.T_Kala.AID = @IDA) AND                        (dbo.T_Kala.GOKID = @KalaGo) AND (dbo.T_NameKala.NameKala1 = @KalaName)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_listKala_sandoogh_GoroupKala]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [Q_listKala_sandoogh_GoroupKala](@AID int)
AS SELECT  dbo.T_Kala.GOKID, dbo.T_NameGoKala.NameGoKala1
FROM     dbo.T_Kala INNER JOIN
               dbo.T_NameGoKala ON dbo.T_Kala.GOKID = dbo.T_NameGoKala.ID
WHERE  (dbo.T_Kala.AID = @AID)
GROUP BY dbo.T_Kala.GOKID, dbo.T_NameGoKala.NameGoKala1
HAVING  (NOT (dbo.T_NameGoKala.NameGoKala1 IS NULL))
ORDER BY dbo.T_NameGoKala.NameGoKala1' 
END
GO


/****** Object:  StoredProcedure [dbo].[Q_ListKala_Sandoogh_Barcode]    Script Date: 02/10/2018 10:18:22 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_ListKala_Sandoogh_Barcode]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_ListKala_Sandoogh_Barcode]
GO



/****** Object:  StoredProcedure [dbo].[Q_ListKala_Sandoogh_Barcode]    Script Date: 02/10/2018 10:18:22 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

      CREATE PROCEDURE [dbo].[Q_ListKala_Sandoogh_Barcode](@AID int) AS 
      
      
SELECT     ID AS NameKalaID, BarCode, NameKala1, AID, GOKID, UnitPrice1, Currency1, NameGoKala1, NameAnbar, Zarib, NameVahed1, NameVahed2
FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1
WHERE     (AID = @AID)
ORDER BY BarCode
GO




GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_Sandoogh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'       CREATE PROCEDURE [Q_ListKala_Sandoogh] (@AID int, @GokalaID int) AS SELECT     dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1 AS [نام كالا], dbo.T_NameKala.BarCode, dbo.T_Kala.AID, dbo.T_Kala.GOKID,                        dbo.T_NameKala.BarCode AS باركد, dbo.T_NameKala.UnitPrice1 AS Expr1, dbo.T_NameKala.UnitPrice1 AS [قيمت فروش],                        dbo.T_NameKala.UnitPrice1 AS [قيمت فروش], dbo.T_NameKala.UnitPrice1 AS Expr2, @GokalaID AS Expr3, dbo.T_Kala.NameKalaID AS Expr4,                        dbo.T_Kala.NameKalaID AS Expr5 FROM         dbo.T_NameKala INNER JOIN                       dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID GROUP BY dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, dbo.T_Kala.AID, dbo.T_Kala.GOKID,                        dbo.T_NameKala.UnitPrice1 HAVING      (dbo.T_Kala.AID = @AID) AND (dbo.T_Kala.GOKID = @GokalaID) AND (NOT (@GokalaID IS NULL)) OR                       (dbo.T_Kala.AID = @AID) AND (@GokalaID IS NULL) ORDER BY dbo.T_NameKala.NameKala1  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKalaKharid2]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListKalaKharid2](@GokID int, @AID int, @LinkedID nvarchar(50)) AS SELECT     dbo.T_NameKala.ID AS Expr2, dbo.T_NameKala.NameKala1 AS Expr3, dbo.T_Kala.AID AS Expr4, dbo.T_Kala.Unitfo AS Expr5,                        dbo.T_Kala.UnitPrice AS Expr6, dbo.T_Kala.GOKID AS Expr7, dbo.T_Kala.FieldN1 AS Expr8, ROUND(ISNULL(dbo.T_NameKala.TedadBal, 0)                        + ISNULL(Function1.sumTedad, 0), 2) AS Expr1, Function1.sumTedad AS Expr9, @LinkedID AS Expr10 FROM         dbo.Function1(@LinkedID) Function1 RIGHT OUTER JOIN                       dbo.T_Kala ON Function1.KALAID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN                       dbo.T_NameKala ON dbo.T_Kala.NameKalaID = dbo.T_NameKala.ID WHERE     (dbo.T_Kala.GOKID = @GokID) AND (dbo.T_Kala.AID = @AID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKalaKharid]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListKalaKharid](@GokID int, @AID int, @LinkedID nvarchar(50)) AS SELECT     dbo.T_NameKala.ID AS Expr2, dbo.T_NameKala.NameKala1 AS Expr3, dbo.T_Kala.AID AS Expr4, dbo.T_Kala.Unitfo AS Expr5,                        dbo.T_Kala.UnitPrice AS Expr6, dbo.T_Kala.GOKID AS Expr7, dbo.T_Kala.FieldN1 AS Expr8, ROUND(ISNULL(dbo.T_NameKala.TedadBal, 0)                        + ISNULL(Function1.sumTedad, 0), 2) AS Expr1, Function1.sumTedad AS Expr9, @LinkedID AS Expr10 FROM         dbo.Function1(@LinkedID) Function1 RIGHT OUTER JOIN                       dbo.T_Kala ON Function1.KALAID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN                       dbo.T_NameKala ON dbo.T_Kala.NameKalaID = dbo.T_NameKala.ID WHERE     (dbo.T_Kala.GOKID = @GokID) AND (dbo.T_Kala.AID = @AID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021157]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021157](@AccID nvarchar(50)) AS SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID, dbo.HesabName(dbo.T_Forosh.KOLID,                        dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID) AS namehesab, dbo.T_T_Heasb.Phone1, dbo.T_Forosh.KOLID, dbo.T_Forosh.THESABID,                        dbo.T_Forosh.HesabID, dbo.T_T_Heasb.Address, dbo.T_T_Heasb.CodEghtesdi, dbo.T_T_Heasb.Email FROM         dbo.T_KolHesab INNER JOIN                       dbo.T_Forosh ON dbo.T_KolHesab.KOLID = dbo.T_Forosh.KOLID LEFT OUTER JOIN                       dbo.T_T_Heasb ON dbo.T_Forosh.THESABID = dbo.T_T_Heasb.THESABID GROUP BY dbo.HesabName(dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)),                        dbo.T_T_Heasb.Phone1, dbo.T_Forosh.KOLID, dbo.T_Forosh.THESABID, dbo.T_Forosh.HesabID, dbo.T_T_Heasb.Address,                        dbo.T_T_Heasb.CodEghtesdi, dbo.T_T_Heasb.Email HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021156]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021156](@AccID nvarchar(50)) AS SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID, dbo.HesabName(dbo.T_Forosh.KOLID,                        dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID) AS namehesab, dbo.T_T_Heasb.Phone1, dbo.T_T_Heasb.Phone2, dbo.T_Forosh.KOLID,                        dbo.T_Forosh.THESABID, dbo.T_Forosh.HesabID FROM         dbo.T_KolHesab INNER JOIN                       dbo.T_Forosh ON dbo.T_KolHesab.KOLID = dbo.T_Forosh.KOLID LEFT OUTER JOIN                       dbo.T_T_Heasb ON dbo.T_Forosh.THESABID = dbo.T_T_Heasb.THESABID GROUP BY dbo.HesabName(dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)),                        dbo.T_T_Heasb.Phone1, dbo.T_T_Heasb.Phone2, dbo.T_Forosh.KOLID, dbo.T_Forosh.THESABID, dbo.T_Forosh.HesabID HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011661]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011661](@AccID nvarchar(50)) AS SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID, dbo.HesabName(dbo.T_Kharid.KOLID,                        dbo.T_Kharid.HesabID, dbo.T_Kharid.THESABID) AS namehesab, dbo.T_T_Heasb.Phone1, dbo.T_Kharid.KOLID, dbo.T_Kharid.THESABID,                        dbo.T_Kharid.HesabID, dbo.T_T_Heasb.Address, dbo.T_T_Heasb.Email FROM         dbo.T_Kharid INNER JOIN                       dbo.T_KolHesab ON dbo.T_Kharid.KOLID = dbo.T_KolHesab.KOLID LEFT OUTER JOIN                       dbo.T_T_Heasb ON dbo.T_Kharid.THESABID = dbo.T_T_Heasb.THESABID GROUP BY dbo.HesabName(dbo.T_Kharid.KOLID, dbo.T_Kharid.HesabID, dbo.T_Kharid.THESABID), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)),                        dbo.T_T_Heasb.Phone1, dbo.T_Kharid.KOLID, dbo.T_Kharid.THESABID, dbo.T_Kharid.HesabID, dbo.T_T_Heasb.Address,                        dbo.T_T_Heasb.Email HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011660]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011660](@AccID nvarchar(50)) AS SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID, dbo.HesabName(dbo.T_Kharid.KOLID,                        dbo.T_Kharid.HesabID, dbo.T_Kharid.THESABID) AS namehesab, dbo.T_T_Heasb.Phone1, dbo.T_T_Heasb.Phone2, dbo.T_Kharid.KOLID,                        dbo.T_Kharid.THESABID, dbo.T_Kharid.HesabID FROM         dbo.T_Kharid INNER JOIN                       dbo.T_KolHesab ON dbo.T_Kharid.KOLID = dbo.T_KolHesab.KOLID LEFT OUTER JOIN                       dbo.T_T_Heasb ON dbo.T_Kharid.THESABID = dbo.T_T_Heasb.THESABID GROUP BY dbo.HesabName(dbo.T_Kharid.KOLID, dbo.T_Kharid.HesabID, dbo.T_Kharid.THESABID), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)),                        dbo.T_T_Heasb.Phone1, dbo.T_T_Heasb.Phone2, dbo.T_Kharid.KOLID, dbo.T_Kharid.THESABID, dbo.T_Kharid.HesabID HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_KolHesab.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_Kharid]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Rep_Kharid](@LinkedID int, @OperType int) AS SELECT     TOP 100 PERCENT dbo.TRANSACTION2.AID, dbo.TRANSACTION2.KALAID, dbo.TRANSACTION2.Sharh, dbo.TRANSACTION2.Date1,                        dbo.T_Kharid.KHARIDID, dbo.TRANSACTION2.Tedad, dbo.TRANSACTION2.UnitPrice, CONVERT(money,                        dbo.TRANSACTION2.Tedad * dbo.TRANSACTION2.UnitPrice) AS Sumation, dbo.TRANSACTION2.radif, dbo.TRANSACTION2.GOKID,                        CONVERT(nvarchar(20), dbo.TRANSACTION2.AID) + CONVERT(nvarchar(20), dbo.TRANSACTION2.GOKID) + CONVERT(nvarchar(20),                        dbo.TRANSACTION2.KALAID) AS Expr1, dbo.TRANSACTION2.LinkedID, dbo.TRANSACTION2.TextKalaName, dbo.TRANSACTION2.SumofPrice,                        dbo.TRANSACTION2.FieldN6, dbo.TRANSACTION2.OperType, dbo.HesabName(dbo.T_Kharid.KOLID, dbo.T_Kharid.HesabID, dbo.T_Kharid.THESABID)                        AS NameHesab, dbo.T_Kharid.Takhfif, dbo.T_T_Heasb.Address, dbo.T_T_Heasb.CodEghtesdi, dbo.T_T_Heasb.CodPosti, dbo.T_T_Heasb.Phone1,                        dbo.T_Kharid.Sharh AS Expr2, dbo.T_Kharid.NumberKH, dbo.T_Kharid.DateKh, dbo.T_NameKala.FieldT1, dbo.T_NameKala.Zarib,                        dbo.T_NameKala.NameKala1, dbo.T_NameKala.Note FROM         dbo.TRANSACTION2 INNER JOIN                       dbo.T_Kharid ON dbo.TRANSACTION2.LinkedID = dbo.T_Kharid.KHARIDID LEFT OUTER JOIN                       dbo.T_T_Heasb ON dbo.T_Kharid.THESABID = dbo.T_T_Heasb.THESABID LEFT OUTER JOIN                       dbo.T_NameKala ON dbo.TRANSACTION2.KALAID = dbo.T_NameKala.ID WHERE     (dbo.TRANSACTION2.LinkedID = @LinkedID) AND (dbo.TRANSACTION2.OperType = @OperType) ORDER BY dbo.TRANSACTION2.radif ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_Khadamat]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_Khadamat](@LinkedID int, @OperType int) AS SELECT     TOP 100 PERCENT AID, KALAID, Sharh, Date1, KHARIDID, Tedad, UnitPrice, CONVERT(money, Tedad * UnitPrice) AS Sumation, radif, GOKID,                        CONVERT(nvarchar(20), AID) + CONVERT(nvarchar(20), GOKID) + CONVERT(nvarchar(20), KALAID) AS Expr1, LinkedID, TextKalaName, SumofPrice,                        FieldN6, OperType FROM         dbo.TRANSACTION2 WHERE     (LinkedID = @LinkedID) AND (OperType = @OperType) ORDER BY radif  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_ForooshPishFactor]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Rep_ForooshPishFactor] (@Factid int) AS SELECT     dbo.T_NameKala.NameKala1, dbo.T_PishFactor.FOROSHID, dbo.T_TarSPish.TedadFo, dbo.T_TarSPish.UnitPriceFo,                        dbo.T_TarSPish.TedadFo * dbo.T_TarSPish.UnitPriceFo AS Mab, dbo.T_TarSPish.GOKID, dbo.T_TarSPish.Sharh,                        dbo.HesabName(dbo.T_PishFactor.KOLID, dbo.T_PishFactor.HesabID, dbo.T_PishFactor.THESABID) AS NameHesab, dbo.T_PishFactor.Takhfif,                        dbo.T_PishFactor.DateFo, dbo.T_PishFactor.ForoshNumbr, dbo.T_PishFactor.Sharh AS Note, dbo.T_PishFactor.PrimarySaid2 AS PSAID2,                        dbo.T_NameKala.Zarib, dbo.T_T_Heasb.Address, dbo.T_T_Heasb.CodPosti, dbo.T_T_Heasb.CodEghtesdi, dbo.T_T_Heasb.Phone1,                        dbo.T_PishFactor.FieldT3, dbo.T_TarSPish.radif FROM         dbo.T_TarSPish INNER JOIN                       dbo.T_PishFactor ON dbo.T_TarSPish.FOROSHID = dbo.T_PishFactor.FOROSHID INNER JOIN                       dbo.T_NameKala ON dbo.T_TarSPish.KALAID = dbo.T_NameKala.ID LEFT OUTER JOIN                       dbo.T_T_Heasb ON dbo.T_PishFactor.THESABID = dbo.T_T_Heasb.THESABID WHERE     (dbo.T_PishFactor.FOROSHID = @Factid) ORDER BY dbo.T_NameKala.NameKala1, dbo.T_TarSPish.GOKID' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_Foroosh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_Foroosh](@LinkedID int, @OperType int) AS SELECT     TOP 100 PERCENT TRANSACTION2.AID, TRANSACTION2.KALAID, TRANSACTION2.Sharh, TRANSACTION2.Date1, TRANSACTION2.KHARIDID,                        TRANSACTION2.Tedad, TRANSACTION2.UnitPrice, CONVERT(money, TRANSACTION2.Tedad * TRANSACTION2.UnitPrice) AS Sumation,                        TRANSACTION2.radif, TRANSACTION2.GOKID, CONVERT(nvarchar(20), TRANSACTION2.AID) + CONVERT(nvarchar(20), TRANSACTION2.GOKID)                        + CONVERT(nvarchar(20), TRANSACTION2.KALAID) AS Expr1, TRANSACTION2.LinkedID, TRANSACTION2.TextKalaName, TRANSACTION2.SumofPrice,                        TRANSACTION2.FieldN6, TRANSACTION2.OperType, dbo.HesabName(T_Forosh.KOLID, T_Forosh.HesabID, T_Forosh.THESABID) AS NameHesab,                        T_Forosh.Takhfif, T_T_Heasb.Address, T_T_Heasb.CodEghtesdi, T_T_Heasb.CodPosti, T_T_Heasb.Phone1, T_Forosh.Sharh AS Expr2,                        T_Forosh.NumberKH, T_Forosh.DateFo, T_NameKala.FieldT1, T_NameKala.Zarib FROM         TRANSACTION2 INNER JOIN                       T_Forosh ON TRANSACTION2.LinkedID = T_Forosh.FOROSHID LEFT OUTER JOIN                       T_NameKala ON TRANSACTION2.KALAID = T_NameKala.ID LEFT OUTER JOIN                       T_T_Heasb ON T_Forosh.THESABID = T_T_Heasb.THESABID WHERE     (TRANSACTION2.LinkedID = @LinkedID) AND (TRANSACTION2.OperType = @OperType) ORDER BY TRANSACTION2.radif   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_date]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_date](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     Date1, Sharh, SAID, HESABID, PARBes, DARBed, radif, GOID, KOLID, THESABID, FieldT1, CONVERT(Nvarchar(20), GOID)                        + N''.'' + CONVERT(Nvarchar(20), KOLID) + N''.'' + CONVERT(Nvarchar(20), HESABID) + N''.'' + CONVERT(Nvarchar(20), THESABID) AS CodeH, IDSHARH,                        TextNumberSA, FieldN1, TRAID FROM         dbo.TRANSACTION2 WHERE     (Date1 BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_SanadHesabdaryMorakab1]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_SanadHesabdaryMorakab1](@Said int) AS SELECT     dbo.TRANSACTION2.Date1, dbo.TRANSACTION2.Sharh, dbo.TRANSACTION2.SAID2, dbo.TRANSACTION2.HESABID, dbo.TRANSACTION2.PARBes,                        dbo.TRANSACTION2.DARBed, dbo.TRANSACTION2.radif, dbo.TRANSACTION2.GOID, dbo.TRANSACTION2.KOLID, dbo.TRANSACTION2.THESABID,                        dbo.TRANSACTION2.FieldT1, dbo.TRANSACTION2.IDSHARH, dbo.TRANSACTION2.TextNumberSA, dbo.TRANSACTION2.TRAID,                        dbo.TRANSACTION2.FieldN1, CASE WHEN darbed = 0 THEN 2 ELSE 1 END AS Expr1, CONVERT(int, dbo.T_KolHesab.KolNumber) AS KOLID1,                        dbo.HesabName(dbo.TRANSACTION2.KOLID, dbo.TRANSACTION2.HESABID, dbo.TRANSACTION2.THESABID) AS hesabname FROM         dbo.TRANSACTION2 INNER JOIN                       dbo.T_KolHesab ON dbo.TRANSACTION2.KOLID = dbo.T_KolHesab.KOLID WHERE     (dbo.TRANSACTION2.SAID = @Said) ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_SanadHesabdaryMorakab]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_SanadHesabdaryMorakab](@Said int) AS SELECT     Date1, Sharh, SAID, HESABID, PARBes, DARBed, radif, GOID, KOLID, THESABID, FieldT1, IDSHARH, TextNumberSA, TRAID, FieldN1,                        CASE WHEN darbed = 0 THEN 2 ELSE 1 END AS Expr1 FROM         dbo.TRANSACTION2 WHERE     (SAID = @Said)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_SanadHesabdary]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_SanadHesabdary](@saID int) AS SELECT     Date1, Sharh, SAID, HESABID, PARBes, DARBed, radif, GOID, KOLID, THESABID, FieldT1, CONVERT(Nvarchar(20), GOID)                        + N''.'' + CONVERT(Nvarchar(20), KOLID) + N''.'' + CONVERT(Nvarchar(20), HESABID) + N''.'' + CONVERT(Nvarchar(20), THESABID) AS CodeH, IDSHARH,                        TextNumberSA, FieldN1, TRAID FROM         dbo.TRANSACTION2 WHERE     (SAID = @saID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_PFrm_RepSAnadHesabdary2Said]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_PFrm_RepSAnadHesabdary2Said](@startsanadID int, @endsanadID int) AS SELECT     dbo.TRANSACTION2.Sharh, dbo.TRANSACTION2.HESABID, dbo.TRANSACTION2.PARBes, dbo.TRANSACTION2.DARBed, dbo.TRANSACTION2.GOID,                        dbo.TRANSACTION2.KOLID, dbo.TRANSACTION2.THESABID, CONVERT(Nvarchar(20), dbo.TRANSACTION2.GOID) + N''.'' + CONVERT(Nvarchar(20),                        dbo.TRANSACTION2.KOLID) + N''.'' + CONVERT(Nvarchar(20), dbo.TRANSACTION2.HESABID) + N''.'' + CONVERT(Nvarchar(20),                        dbo.TRANSACTION2.THESABID) AS CodeH, dbo.T_SA2.SAID2Number, dbo.TRANSACTION2.RadifSAID2, dbo.TRANSACTION2.TextNHSID2,                        dbo.T_SA2.Sharh AS expr1, dbo.T_SA2.Date2 FROM         dbo.TRANSACTION2 INNER JOIN                       dbo.T_SA2 ON dbo.TRANSACTION2.SAID2 = dbo.T_SA2.SAID2 WHERE     (dbo.T_SA2.SAID2Number BETWEEN @startsanadID AND @endsanadID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_PFrm_RepSAnadHesabdary2Mablagh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_PFrm_RepSAnadHesabdary2Mablagh](@startdate nvarchar(50), @enddate nvarchar(255)) AS SELECT     dbo.TRANSACTION2.Sharh, dbo.TRANSACTION2.HESABID, dbo.TRANSACTION2.PARBes, dbo.TRANSACTION2.DARBed, dbo.TRANSACTION2.GOID,                        dbo.TRANSACTION2.KOLID, dbo.TRANSACTION2.THESABID, RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.TRANSACTION2.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.TRANSACTION2.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.TRANSACTION2.HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.TRANSACTION2.THESABID), 4) AS CodeH, dbo.T_SA2.Date2, dbo.T_SA2.SAID2,                        dbo.TRANSACTION2.RadifSAID2, dbo.TRANSACTION2.TextNHSID2, dbo.T_SA2.Sharh AS expr1 FROM         dbo.TRANSACTION2 INNER JOIN                       dbo.T_SA2 ON dbo.TRANSACTION2.SAID2 = dbo.T_SA2.SAID2 WHERE     (@startdate IS NULL) OR                       (@startdate IS NOT NULL) AND (dbo.T_SA2.Date2 BETWEEN @startdate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_PFrm_RepSAnadHesabdary2Date]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_PFrm_RepSAnadHesabdary2Date](@startdate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.TRANSACTION2.Sharh, dbo.TRANSACTION2.HESABID, dbo.TRANSACTION2.PARBes, dbo.TRANSACTION2.DARBed, dbo.TRANSACTION2.GOID,                        dbo.TRANSACTION2.KOLID, dbo.TRANSACTION2.THESABID, dbo.T_SA2.SAID2Number, dbo.TRANSACTION2.TextNHSID2, dbo.T_SA2.Sharh AS expr1,                        dbo.T_SA2.Date2, CONVERT(Nvarchar(20), dbo.TRANSACTION2.GOID) + N''.'' + CONVERT(Nvarchar(20), dbo.TRANSACTION2.KOLID)                        + N''.'' + CONVERT(Nvarchar(20), dbo.TRANSACTION2.HESABID) + N''.'' + CONVERT(Nvarchar(20), dbo.TRANSACTION2.THESABID) AS CodeH FROM         dbo.TRANSACTION2 LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.TRANSACTION2.SAID2 = dbo.T_SA2.SAID2 WHERE     (dbo.T_SA2.Date2 BETWEEN @startdate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure9]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure9] AS SELECT     NameKalaID, Flag1 FROM         dbo.T_Kala  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure8]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure8] AS UPDATE    dbo.TRANSACTION2 SET              Tedad = Tedad + 1 WHERE     (AID = 1) AND (KALAID = 2) AND (GOKID = 3) AND (OperType = 5) AND (LinkedID = 6) AND (UnitPrice = 4)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure7]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure7] AS SELECT     * FROM         dbo.TRANSACTION2  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View7]'))
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [View7]
AS
SELECT     dbo.[TRANSACTION].*
FROM         dbo.[TRANSACTION]
'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View6]'))
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [View6]
AS
SELECT     dbo.T_T_Heasb.THESABNumber, dbo.T_T_Heasb.NameHesabT, dbo.[TRANSACTION].PARBes, dbo.[TRANSACTION].DARBed, dbo.T_T_Heasb.Mob, 
                      dbo.[TRANSACTION].Date1, dbo.[TRANSACTION].THESABID
FROM         dbo.[TRANSACTION] INNER JOIN
                      dbo.T_T_Heasb ON dbo.[TRANSACTION].THESABID = dbo.T_T_Heasb.THESABID
WHERE     (NOT (dbo.[TRANSACTION].THESABID IS NULL)) AND (dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes <> 0)
'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View5]'))
EXEC dbo.sp_executesql @statement = N'      CREATE VIEW [View5] AS SELECT     TRAID, SAIDType FROM         dbo.[TRANSACTION] WHERE     (SAID2 = 688) '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View3]'))
EXEC dbo.sp_executesql @statement = N'      CREATE VIEW [View3] AS SELECT     dbo.T_Forosh.ForoshNumbr, dbo.T_Forosh.NumberKH, dbo.[TRANSACTION].OperationCode, dbo.[TRANSACTION].FieldN1, dbo.[TRANSACTION].FieldN2,                        dbo.T_Forosh.FOROSHID FROM         dbo.T_Forosh INNER JOIN                       dbo.[TRANSACTION] ON dbo.T_Forosh.FOROSHID = dbo.[TRANSACTION].FOROSHID  '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View2]'))
EXEC dbo.sp_executesql @statement = N'        CREATE VIEW [View2] AS SELECT     IDENT_INCR(''view2'') AS Expr1, SAID2, HESABID, NEWID() AS Expr2 FROM         dbo.[TRANSACTION]  '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View1]'))
EXEC dbo.sp_executesql @statement = N'          CREATE VIEW [View1] AS SELECT     RIGHT(N''000000'' + CONVERT(nvarchar(6), KOLID), 6) + RIGHT(N''000000'' + CONVERT(nvarchar(6), ISNULL(HESABID, 0)), 6)                        + RIGHT(N''000000'' + CONVERT(nvarchar(6), ISNULL(THESABID, 0)), 6) AS CodeH, TedadFo * UnitPriceFo AS MabFact, KALAID FROM         dbo.[TRANSACTION] WHERE     (NOT (KALAID IS NULL))  '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View_AvgKH]'))
EXEC dbo.sp_executesql @statement = N'      CREATE VIEW [View_AvgKH] AS SELECT     KALAID, ABS(SUM(FieldN3)) AS Mablagh, SUM(TedadKh) AS tedad FROM         dbo.[TRANSACTION] WHERE     (NOT (KHARIDID IS NULL)) GROUP BY KALAID '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View_AvgFo]'))
EXEC dbo.sp_executesql @statement = N'       create VIEW [View_AvgFo] AS SELECT     KALAID, SUM(FieldN3) / SUM(TedadFo) AS avgfo FROM         dbo.[TRANSACTION] WHERE     (NOT (FOROSHID IS NULL)) GROUP BY KALAID  '
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[View_SublistSoorathesab]'))
EXEC dbo.sp_executesql @statement = N'      CREATE VIEW [View_SublistSoorathesab] AS SELECT     dbo.T_NameKala.NameKala1, dbo.T_Par7.PARID7Number, dbo.T_Dar8.DARID8Numbr, dbo.T_Forosh.ForoshNumbr,                        dbo.[TRANSACTION].TedadFo + dbo.[TRANSACTION].TedadKh + dbo.[TRANSACTION].TedadBarFo + dbo.[TRANSACTION].TedadBarKh + dbo.[TRANSACTION].TedadDKh                        AS Tedad, dbo.[TRANSACTION].UnitPriceFo, dbo.[TRANSACTION].TedadKh, dbo.[TRANSACTION].UnitPriceKh, dbo.[TRANSACTION].TedadBarFo,                        dbo.[TRANSACTION].UnitPriceBarFo, dbo.[TRANSACTION].TedadBarKh, dbo.[TRANSACTION].UnitPriceBarKh, dbo.[TRANSACTION].UnitPriceDKh,                        dbo.[TRANSACTION].TedadDKh, ABS(dbo.[TRANSACTION].FieldN3) AS Expr6, dbo.T_DKhad.DKHNumbr, ISNULL(dbo.T_Kharid.PrimarySaid2, 0)                        + ISNULL(dbo.T_Forosh.PrimarySaid2, 0) + ISNULL(dbo.T_Dar8.PrimarySaid2, 0) + ISNULL(dbo.T_Par7.PrimarySaid2, 0)                        + ISNULL(dbo.T_DKhad.PrimarySaid2, 0) AS PrimarySaid2, ISNULL(dbo.T_Kharid.KharidNumbr, N'''') + ISNULL(dbo.T_Forosh.ForoshNumbr, N'''')                        + ISNULL(dbo.T_Dar8.DARID8Numbr, N'''') + ISNULL(dbo.T_Par7.PARID7Number, N'''') + ISNULL(dbo.T_DKhad.DKHNumbr, N'''') AS FactCode,                        dbo.[TRANSACTION].UnitPriceFo + dbo.[TRANSACTION].UnitPriceKh + dbo.[TRANSACTION].UnitPriceBarFo + dbo.[TRANSACTION].UnitPriceBarKh + dbo.[TRANSACTION].UnitPriceDKh                        AS UnitPrice, ISNULL(dbo.T_DKhad.Takhfif, 0) + ISNULL(dbo.T_Kharid.Takhfif, 0) + ISNULL(dbo.T_Par7.Takhfif, 0) + ISNULL(dbo.T_Forosh.Takhfif, 0)                        + ISNULL(dbo.T_Dar8.Takhfif, 0) AS Takhfif, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].Sharh FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID LEFT OUTER JOIN                       dbo.T_DKhad ON dbo.[TRANSACTION].DKHID = dbo.T_DKhad.DKHID LEFT OUTER JOIN                       dbo.T_Kharid ON dbo.[TRANSACTION].KHARIDID = dbo.T_Kharid.KHARIDID LEFT OUTER JOIN                       dbo.T_Forosh ON dbo.[TRANSACTION].FOROSHID = dbo.T_Forosh.FOROSHID LEFT OUTER JOIN                       dbo.T_Dar8 ON dbo.[TRANSACTION].DARID8 = dbo.T_Dar8.DARID8 LEFT OUTER JOIN                       dbo.T_Par7 ON dbo.[TRANSACTION].PARID7 = dbo.T_Par7.PARID7 '
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure13]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure13] AS SELECT     dbo.[TRANSACTION].* FROM         dbo.[TRANSACTION]  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure6]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure6] AS SELECT     SAID2, FieldN2, SAID2Number, KALAID FROM         dbo.[TRANSACTION] WHERE     (SAID2 = 9) AND (NOT (SAID2Number IS NULL)) OR                       (NOT (SAID2Number IS NULL)) AND (FieldN2 = 9)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Tmp_DatarepGen]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [Tmp_DatarepGen] AS SELECT     T_T_Heasb.NameHesabT AS NameHesabmoin, CASE WHEN (tbl_Tar.NameKala IS NOT NULL) THEN tbl_Tar.NameKala ELSE CONVERT(nvarchar(255),                        isnull(TextShSA, N'''')) END AS SharhRadif, DERIVEDTBL.mande AS MandeHesab, tbl_Tar.DARBed AS Bedehkar, tbl_Tar.PARBes AS Bestankar,                        tbl_Tar.Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_T_Heasb.THESABID AS HesabID, T_T_Heasb.THESABNumber AS CodeHesab, tbl_Tar.srtfield,                        tbl_Tar.tedad, tbl_Tar.unitprice, tbl_Tar.NameVahed, tbl_Tar.NameKala, tbl_Tar.MatnFi, tbl_Tar.SAIDType, tbl_Tar.NoeAmaliyat, tbl_Tar.TedadSadere,                        tbl_Tar.TedadVarede FROM         T_SA2 INNER JOIN                           (SELECT     Tbl_2.Sharh, Tbl_2.Date1, ISNULL(Tbl_2.PARBes, 0) + CONVERT(decimal(19, 4), Tbl_2.TedadBarFo + Tbl_2.TedadKh)                                                     * (Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo) AS PARBes, ISNULL(Tbl_2.DARBed, 0) + CONVERT(decimal(19, 4),                                                     Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh) * (Tbl_2.UnitPriceFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh) AS DARBed, CONVERT(int,                                                     CASE WHEN (NOT (SAID2 IS NULL)) THEN SAID2 ELSE [Tbl_2].fieldn2 END) AS SAID2,                                                     Tbl_2.TedadFo + Tbl_2.TedadKh + Tbl_2.TedadBarFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh AS tedad,                                                     Tbl_2.UnitPriceFo + Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh AS unitprice, T_NameVahed.NameVahed,                                                     CASE WHEN (NOT tbl_2.KALAID IS NULL) THEN (t_Namekala.NameKala1 + N'' '' + CONVERT(nvarchar(255), isnull(tbl_2.Sharh, N'''')))                                                     ELSE ([T_NameKhad].name + N'' '' + CONVERT(nvarchar(255), isnull(tbl_2.Sharh, N''''))) END + N'' '' + CASE WHEN (tedadfo > 0)                                                     THEN (N''فاکتور فروش['' + OperationCode + N''] '') WHEN (tedadkh > 0) THEN (N''فاکتور خريد[ '' + OperationCode + N''] '') WHEN (tedadbarfo > 0)                                                     THEN (N''فاکتور برگشت از فروش[ '' + OperationCode + N''] '') WHEN (tedadbarkh > 0) THEN (N''فاکتور برگشت از خريد[ '' + OperationCode + N''] '')                                                     WHEN (tedaddkh > 0) THEN (N''فاکتور خدمات[ '' + OperationCode + N''] '') ELSE NULL END AS NameKala, CASE WHEN (SAID2 IS NULL)                                                     THEN N''في : '' ELSE NULL END AS MatnFi, Tbl_2.THESABID AS Hesabid, Tbl_2.TextShSA, CASE WHEN (SAID2 IS NULL) THEN 2 ELSE 1 END AS srtfield,                                                     Tbl_2.SAIDType, Tbl_2.TRAID, CASE WHEN (tedadfo > 0) THEN (N''فاکتور '' + OperationCode + N''فروش '') WHEN (tedadkh > 0)                                                     THEN (N''فاکتور '' + OperationCode + N''خريد '') WHEN (tedadbarfo > 0) THEN (N''فاکتور '' + OperationCode + N''برگشت از فروش '') WHEN (tedadbarkh > 0)                                                     THEN (N''فاکتور '' + OperationCode + N''برگشت از خريد '') WHEN (tedaddkh > 0) THEN (N''فاکتور '' + OperationCode + N''خدمات '') ELSE NULL                                                     END AS NoeAmaliyat, Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh AS TedadSadere, Tbl_2.TedadBarFo + Tbl_2.TedadKh AS TedadVarede,                                                     (Tbl_2.TedadBarFo + Tbl_2.TedadKh) * (Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo) AS beskala, (Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh)                                                     * (Tbl_2.UnitPriceFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh) AS bedkala, T_NameKhad.Name, T_NameKala.NameKala1,                                                     Tbl_2.OperationCode                              FROM         T_NameKhad RIGHT OUTER JOIN                                                    [TRANSACTION] AS Tbl_2 ON T_NameKhad.NAMEKHID = Tbl_2.NAMEKHID LEFT OUTER JOIN                                                    T_NameKala INNER JOIN                                                    T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 ON Tbl_2.KALAID = T_NameKala.ID                              WHERE     (Tbl_2.SAIDType <> 3) OR                                                    (Tbl_2.SAIDType IS NULL)) AS tbl_Tar ON T_SA2.SAID2 = tbl_Tar.SAID2 INNER JOIN                       T_T_Heasb ON tbl_Tar.Hesabid = T_T_Heasb.THESABID LEFT OUTER JOIN                           (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID AS HESABID
FROM         [TRANSACTION] AS TRANSACTION_1                              WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < ''13000101'')                              GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.HESABID WHERE     (tbl_Tar.Date1 BETWEEN N''13930101'' AND N''13930101'') AND (T_T_Heasb.THESABID = 1) UNION ALL SELECT     dbo.HesabName(NULL, NULL, tbl2.HESABID) AS NameHesabmoin, N''بابت تخفيف '' + CONVERT(nvarchar(255), ISNULL(tbl_Tar.TextShSA, N'''')) AS SharhRadif, NULL                        AS MandeHesab, tbl_Tar.PARBes AS Bedehkar, tbl_Tar.DARBed AS Bestankar, tbl_Tar.Date1 AS Tarikh, tbl_Tar.SAID2Number AS ShomareSanad, tbl2.HESABID, NULL                        AS CodeHesab, 3 AS srtfield, 0 AS tedad, 0 AS unitprice, NULL AS NameVahed, NULL AS NameKala, NULL AS MatnFi, NULL AS SAIDType, NULL AS NoeAmaliyat,                        0 AS TedadSadere, 0 AS TedadVarede FROM         [TRANSACTION] AS tbl_Tar INNER JOIN                           (SELECT     SAID2, THESABID AS HESABID                              FROM         [TRANSACTION]                              GROUP BY SAID2, THESABID) AS tbl2 ON tbl_Tar.SAID2 = tbl2.SAID2 WHERE     (tbl_Tar.KOLID IN (24, 25, 26)) AND (tbl_Tar.SAIDType = 6) AND (tbl2.HESABID = 1) AND (tbl_Tar.Date1 BETWEEN N''13930101'' AND N''13930101'')



ORDER BY tbl_Tar.Date1, ShomareSanad, tbl_Tar.srtfield' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[TestP]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [TestP]
AS SELECT     SAID2, HESABID
FROM         dbo.[TRANSACTION]' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[test6]'))
EXEC dbo.sp_executesql @statement = N'      CREATE VIEW [test6] AS SELECT     dbo.T_Forosh.FOROSHID, dbo.T_Forosh.ForoshNumbr, dbo.T_Forosh.NumberKH, COUNT(dbo.[TRANSACTION].KALAID) AS Expr1, dbo.T_Forosh.DateFo FROM         dbo.T_Forosh LEFT OUTER JOIN                       dbo.[TRANSACTION] ON dbo.T_Forosh.FOROSHID = dbo.[TRANSACTION].FOROSHID GROUP BY dbo.T_Forosh.FOROSHID, dbo.T_Forosh.ForoshNumbr, dbo.T_Forosh.NumberKH, dbo.T_Forosh.DateFo '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900410031]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [std_050023008900410031]
(
@pvchAction			    nvarchar(50)=''Analyze'', /* Analyze - Print */
@pintGoid				int=null, 
@pintKolid				int=null, 
@pintHesabid			int=null, 
@pintTHesabid			int=null,
@pintAid				int=null, 
@pintGoKalaid			int=null, 
@pintKalaid				int=null, 
@pnvaStatdate			nvarchar(50)=null,
@pnvaEnddate			nvarchar(50)=null,
@pintFactType			int=null, 
@pnvaFactorStartid		nvarchar(50)=null,
@pnvaFactorEndid		nvarchar(50)=null,
@pnvaSanadStartid		int=null,
@pnvaSanadEndid			int=null,
@pnvaSrt				nvarchar(255)=null,
@pnvaShowkala			nvarchar(255)=null

)        

 AS 
 
 begin
	SET NOCOUNT ON;
	
	DECLARE @SQLString  NVARCHAR(4000);        
	DECLARE @WhrStatement  NVARCHAR(2000);        
	DECLARE @ParmDefinition  NVARCHAR(2000);        
	DECLARE @ParmAndstr  NVARCHAR(2000);        
	
	set @WhrStatement  = N'''';        
	set @ParmAndstr = N'''';        
    
    	SET @ParmDefinition = N''@pvchAction  nvarchar(50)=''''Analyze'''', 
								@pintGoid				int=null, 
								@pintKolid				int=null, 
								@pintHesabid			int=null, 
								@pintTHesabid			int=null,
								@pintAid				int=null, 
								@pintGoKalaid			int=null, 
								@pintKalaid				int=null, 
								@pnvaStatdate			nvarchar(50)=null,
								@pnvaEnddate			nvarchar(50)=null,
								@pintFactType			int=null, 
								@pnvaFactorStartid		nvarchar(50)=null,
								@pnvaFactorEndid		nvarchar(50)=null,
								@pnvaSanadStartid		int=null,
								@pnvaSanadEndid			int=null,
								@pnvaSrt				nvarchar(255)=null,
								@pnvaShowkala			nvarchar(255)=null'';

    
    
	/* Build the SQL string one time. */

    /* Specify the parameter format one time. */
	if(@pvchAction =''repgen'')
		begin
		
			SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''موجودی اول دوره'' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
								  + T_Kala.Mojodi AS Tedad, TBL_TRA.Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''00000000'') AS Tarikh, T_Kala.NameKalaID, 
								  T_NameKala.NameKala1, T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, 
								  T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, 
								  T_NameGoKala.NameGoKala1 AS NameGoKala
			FROM         T_NameKala INNER JOIN
								  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
								  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
								  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
								  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
								  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
									  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''خرید شماره '' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
								  THEN ''فروش شماره '' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''ب.فروش شماره '' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
								  THEN ''ب.خرید شماره '' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''رسید انبار شماره '' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL 
								  THEN ''حواله انبار شماره '' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''جابه جای شماره '' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
								  TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
								  TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
								  UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL 
								  THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
								  THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
								  THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 7 ELSE NULL END AS TypeFact, AID, 
								  Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
			FROM         [TRANSACTION]
			WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
			ORDER BY ISNULL(TBL_TRA.Tarikh, N''00000000'')
               
		end	
    	if(@pvchAction =''Print'')
		begin
	
	  		 SET @SQLString =

				''SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''''موجودی اول دوره'''' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
									  + T_Kala.Mojodi AS Tedad, TBL_TRA.Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''''00000000'''') AS Tarikh, T_Kala.NameKalaID, T_NameKala.NameKala1, 
									  T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, T_NameKala.Zarib, 
									  T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, 
									  T_NameGoKala.NameGoKala1 AS NameGoKala
				FROM         T_NameKala INNER JOIN
									  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
									  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
									  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
									  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
									  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
										  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''''خرید شماره '''' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
																   THEN ''''فروش شماره '''' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''''ب.فروش شماره '''' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
																   THEN ''''ب.خرید شماره '''' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''''رسید انبار شماره '''' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL
																	THEN ''''حواله انبار شماره '''' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''''جابه جای شماره '''' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
																   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) 
																   IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
																   THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
																   THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL 
																   THEN 7 ELSE NULL END AS TypeFact, AID, Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
											 FROM         [TRANSACTION]
											 WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
				WHERE     (T_Kala.VKalaID = 1) ''
	
			
			set @ParmAndstr='' AND ''
  			if(NOT (@pintAid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintGoKalaid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pnvaStatdate IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.Tarikh BETWEEN @pnvaStatdate AND @pnvaEnddate)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintKalaid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @pintKalaid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintFactType IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.NoeFa = @pintFactType)'';
				set @ParmAndstr='' AND ''
			end

			/*------------------------------*/
			if(NOT (@pintGoid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.GOID = @pintGoid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintKolid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.KOLID = @pintKolid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintHesabid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.HESABID = @pintHesabid)'';
				set @ParmAndstr='' AND ''
			end

			if(NOT (@pintTHesabid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.THESABID = @pintTHesabid)'';
				set @ParmAndstr='' AND ''
			end

			set @SQLString=@SQLString + @WhrStatement + N'' ORDER BY TBL_TRA.Tarikh''
	
	
			EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
								@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
								@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
								@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

		end
    	if(@pvchAction =''Analyze'')
		begin
	  		 
	  		 DECLARE @tmpdbname varchar(50)
	  		 set @tmpdbname= right( REPLACE( CONVERT(uniqueidentifier, newid()), ''-'', ''''),8) ;
	  		 
	  		  
	  		 
	  		 IF OBJECT_ID(''tempdb..##'' + @tmpdbname +'''' ) IS NOT NULL
				exec (''DROP TABLE ##'' + @tmpdbname +'''');
	  		 
	  		 SET @SQLString =
				 N''SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''''موجودی اول دوره'''' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
									  + T_Kala.Mojodi AS Tedad, TBL_TRA.Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''''00000000'''') AS Tarikh, T_Kala.NameKalaID, T_NameKala.NameKala1, 
									  T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, T_NameKala.Zarib, 
									  T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, 
									  T_NameGoKala.NameGoKala1 AS NameGoKala, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3
				INTO         ##'' + @tmpdbname +''
				FROM         T_NameKala INNER JOIN
									  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
									  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
									  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
									  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
									  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
										  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''''خرید شماره '''' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
																   THEN ''''فروش شماره '''' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''''ب.فروش شماره '''' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
																   THEN ''''ب.خرید شماره '''' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''''رسید انبار شماره '''' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL
																	THEN ''''حواله انبار شماره '''' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''''جابه جای شماره '''' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
																   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) 
																   IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
																   THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
																   THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL 
																   THEN 7 ELSE NULL END AS TypeFact, AID, Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
											 FROM         [TRANSACTION]
											 WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
				WHERE     (T_Kala.VKalaID = 1) '';
		
		set @WhrStatement=''''
		set @ParmAndstr='' AND ''
  		if(NOT (@pintAid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintGoKalaid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pnvaStatdate IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.Tarikh BETWEEN @pnvaStatdate AND @pnvaEnddate)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintKalaid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @pintKalaid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintFactType IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.NoeFa = @pintFactType)'';
			set @ParmAndstr='' AND '';
		end
		
		/*------------------------------*/
		if(NOT (@pintGoid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.GOID = @pintGoid)'';
			set @ParmAndstr='' AND ''
		end
		
		if(NOT (@pintKolid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.KOLID = @pintKolid)'';
			set @ParmAndstr='' AND ''
		end
		
		if(NOT (@pintHesabid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.HESABID = @pintHesabid)'';
			set @ParmAndstr='' AND ''
		end

		if(NOT (@pintTHesabid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.THESABID = @pintTHesabid)'';
			set @ParmAndstr='' AND ''
		end
				
		set @SQLString=@SQLString + @WhrStatement + N'' ORDER BY T_NameKala.NameKala1, ISNULL(TBL_TRA.Tarikh, N''''00000000''''), TBL_TRA.Varde DESC   ; '';
		
		set @SQLString=@SQLString + N''
						Declare Cr2 Cursor Local for
						Select NameKalaID,UnitPrice1,UnitPrice2,(isnull(Varde,0) - isnull(Sadre,0)) as mandehr from [##'' + @tmpdbname +''] 
						for Update

						DECLARE @Radif int
						DECLARE @col3 int
						DECLARE @col44 int								
						DECLARE @TotalM decimal(19,4)		
						DECLARE @Col1 decimal(19,4)		
						DECLARE @Col2 decimal(19,4)		
						DECLARE @mandehr decimal(19,4)		

						set @Radif=0
						set @col44=0
						set @TotalM=0
						Open Cr2
							fetch Next from Cr2 into @col3,@Col1,@Col2,@mandehr
							while @@FETCH_STATUS=0
							Begin
								if(@col3 <> @col44)
									set	@TotalM = 0
								
								Set @col44 = @col3
								Set @Radif = @Radif + 1
								Set @TotalM = @TotalM + @mandehr
								
								Update ##'' + @tmpdbname +'' set UnitPrice1=@Radif,UnitPrice2=@TotalM where current of cr2;
								fetch Next from Cr2 into @col3,@Col1,@Col2,@mandehr
							End	
						
						Close cr2
						deallocate cr2
						''

						EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
										@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
										@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
										@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

						EXEC (''select * from [##'' + @tmpdbname +''] 
								DROP TABLE ##'' + @tmpdbname +'''');

	
	end
                                               
return
end






























' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900410026]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [std_050023008900410026]
(
@pvchAction			    nvarchar(50)=''Analyze'', /* Analyze - Print */
@pintGoid				int=null, 
@pintKolid				int=null, 
@pintHesabid			int=null, 
@pintTHesabid			int=null,
@pintAid				int=null, 
@pintGoKalaid			int=null, 
@pintKalaid				int=null, 
@pnvaStatdate			nvarchar(50)=null,
@pnvaEnddate			nvarchar(50)=null,
@pintFactType			int=null, 
@pnvaFactorStartid		nvarchar(50)=null,
@pnvaFactorEndid		nvarchar(50)=null,
@pnvaSanadStartid		int=null,
@pnvaSanadEndid			int=null,
@pnvaSrt				nvarchar(255)=null,
@pnvaShowkala			nvarchar(255)=null

)        

 AS 
 
 begin
	SET NOCOUNT ON;
	
	DECLARE @SQLString  NVARCHAR(4000);        
	DECLARE @WhrStatement  NVARCHAR(2000);        
	DECLARE @ParmDefinition  NVARCHAR(2000);        
	DECLARE @ParmAndstr  NVARCHAR(2000);        
	
	set @WhrStatement  = N'''';        
	set @ParmAndstr = N'''';        
    
    	SET @ParmDefinition = N''@pvchAction  nvarchar(50)=''''Analyze'''', 
								@pintGoid				int=null, 
								@pintKolid				int=null, 
								@pintHesabid			int=null, 
								@pintTHesabid			int=null,
								@pintAid				int=null, 
								@pintGoKalaid			int=null, 
								@pintKalaid				int=null, 
								@pnvaStatdate			nvarchar(50)=null,
								@pnvaEnddate			nvarchar(50)=null,
								@pintFactType			int=null, 
								@pnvaFactorStartid		nvarchar(50)=null,
								@pnvaFactorEndid		nvarchar(50)=null,
								@pnvaSanadStartid		int=null,
								@pnvaSanadEndid			int=null,
								@pnvaSrt				nvarchar(255)=null,
								@pnvaShowkala			nvarchar(255)=null'';

    
    
	/* Build the SQL string one time. */

    /* Specify the parameter format one time. */
	if(@pvchAction =''repgen'')
		begin
		
			SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''موجودی اول دوره'' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
								  + T_Kala.Mojodi AS Tedad, TBL_TRA.Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''00000000'') AS Tarikh, T_Kala.NameKalaID, 
								  T_NameKala.NameKala1, T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, 
								  T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, 
								  T_NameGoKala.NameGoKala1 AS NameGoKala
			FROM         T_NameKala INNER JOIN
								  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
								  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
								  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
								  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
								  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
									  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''خرید شماره '' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
								  THEN ''فروش شماره '' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''ب.فروش شماره '' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
								  THEN ''ب.خرید شماره '' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''رسید انبار شماره '' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL 
								  THEN ''حواله انبار شماره '' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''جابه جای شماره '' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
								  TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
								  TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
								  UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL 
								  THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
								  THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
								  THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 7 ELSE NULL END AS TypeFact, AID, 
								  Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
			FROM         [TRANSACTION]
			WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
			ORDER BY ISNULL(TBL_TRA.Tarikh, N''00000000'')
               
		end	
    	if(@pvchAction =''Print'')
		begin
	
	  		 SET @SQLString =

				''SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''''موجودی اول دوره'''' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
									  + T_Kala.Mojodi AS Tedad, TBL_TRA.Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''''00000000'''') AS Tarikh, T_Kala.NameKalaID, T_NameKala.NameKala1, 
									  T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, T_NameKala.Zarib, 
									  T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, 
									  T_NameGoKala.NameGoKala1 AS NameGoKala
				FROM         T_NameKala INNER JOIN
									  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
									  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
									  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
									  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
									  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
										  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''''خرید شماره '''' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
																   THEN ''''فروش شماره '''' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''''ب.فروش شماره '''' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
																   THEN ''''ب.خرید شماره '''' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''''رسید انبار شماره '''' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL
																	THEN ''''حواله انبار شماره '''' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''''جابه جای شماره '''' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
																   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) 
																   IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
																   THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
																   THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL 
																   THEN 7 ELSE NULL END AS TypeFact, AID, Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
											 FROM         [TRANSACTION]
											 WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
				WHERE     (T_Kala.VKalaID = 1) ''
	
			
			set @ParmAndstr='' AND ''
  			if(NOT (@pintAid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintGoKalaid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pnvaStatdate IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.Tarikh BETWEEN @pnvaStatdate AND @pnvaEnddate)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintKalaid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @pintKalaid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintFactType IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.NoeFa = @pintFactType)'';
				set @ParmAndstr='' AND ''
			end

			/*------------------------------*/
			if(NOT (@pintGoid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.GOID = @pintGoid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintKolid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.KOLID = @pintKolid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintHesabid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.HESABID = @pintHesabid)'';
				set @ParmAndstr='' AND ''
			end

			if(NOT (@pintTHesabid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.THESABID = @pintTHesabid)'';
				set @ParmAndstr='' AND ''
			end

			set @SQLString=@SQLString + @WhrStatement + N'' ORDER BY TBL_TRA.Tarikh''
	
	
			EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
								@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
								@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
								@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

		end
    	if(@pvchAction =''Analyze'')
		begin
	  		 
	  		 DECLARE @tmpdbname varchar(50)
	  		 set @tmpdbname= right( REPLACE( CONVERT(uniqueidentifier, newid()), ''-'', ''''),8) ;
	  		 
	  		  
	  		 
	  		 IF OBJECT_ID(''tempdb..##'' + @tmpdbname +'''' ) IS NOT NULL
				exec (''DROP TABLE ##'' + @tmpdbname +'''');
	  		 
	  		 SET @SQLString =
				 N''SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''''موجودی اول دوره'''' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
									  + T_Kala.Mojodi AS Tedad, TBL_TRA.Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''''00000000'''') AS Tarikh, T_Kala.NameKalaID, T_NameKala.NameKala1, 
									  T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, T_NameKala.Zarib, 
									  T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, 
									  T_NameGoKala.NameGoKala1 AS NameGoKala, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3
				INTO         ##'' + @tmpdbname +''
				FROM         T_NameKala INNER JOIN
									  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
									  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
									  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
									  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
									  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
										  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''''خرید شماره '''' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
																   THEN ''''فروش شماره '''' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''''ب.فروش شماره '''' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
																   THEN ''''ب.خرید شماره '''' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''''رسید انبار شماره '''' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL
																	THEN ''''حواله انبار شماره '''' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''''جابه جای شماره '''' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
																   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) 
																   IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
																   THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
																   THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL 
																   THEN 7 ELSE NULL END AS TypeFact, AID, Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
											 FROM         [TRANSACTION]
											 WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
				WHERE     (T_Kala.VKalaID = 1) '';
		
		set @WhrStatement=''''
		set @ParmAndstr='' AND ''
  		if(NOT (@pintAid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintGoKalaid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pnvaStatdate IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.Tarikh BETWEEN @pnvaStatdate AND @pnvaEnddate)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintKalaid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @pintKalaid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintFactType IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.NoeFa = @pintFactType)'';
			set @ParmAndstr='' AND '';
		end
		
		/*------------------------------*/
		if(NOT (@pintGoid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.GOID = @pintGoid)'';
			set @ParmAndstr='' AND ''
		end
		
		if(NOT (@pintKolid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.KOLID = @pintKolid)'';
			set @ParmAndstr='' AND ''
		end
		
		if(NOT (@pintHesabid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.HESABID = @pintHesabid)'';
			set @ParmAndstr='' AND ''
		end

		if(NOT (@pintTHesabid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.THESABID = @pintTHesabid)'';
			set @ParmAndstr='' AND ''
		end
				
		set @SQLString=@SQLString + @WhrStatement + N'' ORDER BY T_NameKala.NameKala1, ISNULL(TBL_TRA.Tarikh, N''''00000000''''), TBL_TRA.Varde DESC   ; '';
		
		set @SQLString=@SQLString + N''
						Declare Cr2 Cursor Local for
						Select NameKalaID,UnitPrice1,UnitPrice2,(isnull(Varde,0) - isnull(Sadre,0)) as mandehr from [##'' + @tmpdbname +''] 
						for Update

						DECLARE @Radif int
						DECLARE @col3 int
						DECLARE @col44 int								
						DECLARE @TotalM decimal(19,4)		
						DECLARE @Col1 decimal(19,4)		
						DECLARE @Col2 decimal(19,4)		
						DECLARE @mandehr decimal(19,4)		

						set @Radif=0
						set @col44=0
						set @TotalM=0
						Open Cr2
							fetch Next from Cr2 into @col3,@Col1,@Col2,@mandehr
							while @@FETCH_STATUS=0
							Begin
								if(@col3 <> @col44)
									set	@TotalM = 0
								
								Set @col44 = @col3
								Set @Radif = @Radif + 1
								Set @TotalM = @TotalM + @mandehr
								
								Update ##'' + @tmpdbname +'' set UnitPrice1=@Radif,UnitPrice2=@TotalM where current of cr2;
								fetch Next from Cr2 into @col3,@Col1,@Col2,@mandehr
							End	
						
						Close cr2
						deallocate cr2
						''

						EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
										@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
										@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
										@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

						EXEC (''select * from [##'' + @tmpdbname +''] 
								DROP TABLE ##'' + @tmpdbname +'''');

	
	end
                                               
return
end

























' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900400024]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [std_050023008900400024]
(
@pvchAction			    nvarchar(50)=''Analyze'', /* Analyze - Print */
@pintGoid				int=null, 
@pintKolid				int=null, 
@pintHesabid			int=null, 
@pintTHesabid			int=null,
@pintAid				int=null, 
@pintGoKalaid			int=null, 
@pintKalaid				int=null, 
@pnvaStatdate			nvarchar(50)=null,
@pnvaEnddate			nvarchar(50)=null,
@pintFactType			int=null, 
@pnvaFactorStartid		nvarchar(50)=null,
@pnvaFactorEndid		nvarchar(50)=null,
@pnvaSanadStartid		int=null,
@pnvaSanadEndid			int=null,
@pnvaSrt				nvarchar(255)=null,
@pnvaShowkala			nvarchar(255)=null

)        

 AS 
 
 begin
	SET NOCOUNT ON;
	
	DECLARE @SQLString  NVARCHAR(4000);        
	DECLARE @WhrStatement  NVARCHAR(2000);        
	DECLARE @ParmDefinition  NVARCHAR(2000);        
	DECLARE @ParmAndstr  NVARCHAR(2000);        
	
	set @WhrStatement  = N'''';        
	set @ParmAndstr = N'''';        
    
    	SET @ParmDefinition = N''@pvchAction  nvarchar(50)=''''Analyze'''', 
								@pintGoid				int=null, 
								@pintKolid				int=null, 
								@pintHesabid			int=null, 
								@pintTHesabid			int=null,
								@pintAid				int=null, 
								@pintGoKalaid			int=null, 
								@pintKalaid				int=null, 
								@pnvaStatdate			nvarchar(50)=null,
								@pnvaEnddate			nvarchar(50)=null,
								@pintFactType			int=null, 
								@pnvaFactorStartid		nvarchar(50)=null,
								@pnvaFactorEndid		nvarchar(50)=null,
								@pnvaSanadStartid		int=null,
								@pnvaSanadEndid			int=null,
								@pnvaSrt				nvarchar(255)=null,
								@pnvaShowkala			nvarchar(255)=null'';

    
    
	/* Build the SQL string one time. */

    /* Specify the parameter format one time. */
	if(@pvchAction =''repgen'')
		begin
		
			SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''موجودی اول دوره'' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
								  + T_Kala.Mojodi AS Tedad, TBL_TRA.Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''00000000'') AS Tarikh, T_Kala.NameKalaID, 
								  T_NameKala.NameKala1, T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, 
								  T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, 
								  T_NameGoKala.NameGoKala1 AS NameGoKala
			FROM         T_NameKala INNER JOIN
								  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
								  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
								  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
								  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
								  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
									  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''خرید شماره '' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
								  THEN ''فروش شماره '' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''ب.فروش شماره '' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
								  THEN ''ب.خرید شماره '' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''رسید انبار شماره '' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL 
								  THEN ''حواله انبار شماره '' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''جابه جای شماره '' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
								  TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
								  TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
								  UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL 
								  THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
								  THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
								  THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 7 ELSE NULL END AS TypeFact, AID, 
								  Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
			FROM         [TRANSACTION]
			WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
			ORDER BY ISNULL(TBL_TRA.Tarikh, N''00000000'')
               
		end	
    	if(@pvchAction =''Print'')
		begin
	
	  		 SET @SQLString =

				''SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''''موجودی اول دوره'''' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
									  + T_Kala.Mojodi AS Tedad, TBL_TRA.Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''''00000000'''') AS Tarikh, T_Kala.NameKalaID, T_NameKala.NameKala1, 
									  T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, T_NameKala.Zarib, 
									  T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, 
									  T_NameGoKala.NameGoKala1 AS NameGoKala
				FROM         T_NameKala INNER JOIN
									  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
									  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
									  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
									  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
									  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
										  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''''خرید شماره '''' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
																   THEN ''''فروش شماره '''' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''''ب.فروش شماره '''' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
																   THEN ''''ب.خرید شماره '''' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''''رسید انبار شماره '''' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL
																	THEN ''''حواله انبار شماره '''' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''''جابه جای شماره '''' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
																   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) 
																   IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
																   THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
																   THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL 
																   THEN 7 ELSE NULL END AS TypeFact, AID, Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
											 FROM         [TRANSACTION]
											 WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
				WHERE     (T_Kala.VKalaID = 1) ''
	
			
			set @ParmAndstr='' AND ''
  			if(NOT (@pintAid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintGoKalaid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pnvaStatdate IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.Tarikh BETWEEN @pnvaStatdate AND @pnvaEnddate)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintKalaid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @pintKalaid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintFactType IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.NoeFa = @pintFactType)'';
				set @ParmAndstr='' AND ''
			end

			/*------------------------------*/
			if(NOT (@pintGoid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.GOID = @pintGoid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintKolid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.KOLID = @pintKolid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintHesabid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.HESABID = @pintHesabid)'';
				set @ParmAndstr='' AND ''
			end

			if(NOT (@pintTHesabid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.THESABID = @pintTHesabid)'';
				set @ParmAndstr='' AND ''
			end

			set @SQLString=@SQLString + @WhrStatement + N'' ORDER BY TBL_TRA.Tarikh''
	
	
			EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
								@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
								@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
								@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

		end
    	if(@pvchAction =''Analyze'')
		begin
	  		 
	  		 DECLARE @tmpdbname varchar(50)
	  		 set @tmpdbname= right( REPLACE( CONVERT(uniqueidentifier, newid()), ''-'', ''''),8) ;
	  		 
	  		  
	  		 
	  		 IF OBJECT_ID(''tempdb..##'' + @tmpdbname +'''' ) IS NOT NULL
				exec (''DROP TABLE ##'' + @tmpdbname +'''');
	  		 
	  		 SET @SQLString =
				 N''SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''''موجودی اول دوره'''' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
									  + T_Kala.Mojodi AS Tedad, TBL_TRA.Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''''00000000'''') AS Tarikh, T_Kala.NameKalaID, T_NameKala.NameKala1, 
									  T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, T_NameKala.Zarib, 
									  T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, 
									  T_NameGoKala.NameGoKala1 AS NameGoKala, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3
				INTO         ##'' + @tmpdbname +''
				FROM         T_NameKala INNER JOIN
									  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
									  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
									  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
									  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
									  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
										  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''''خرید شماره '''' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
																   THEN ''''فروش شماره '''' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''''ب.فروش شماره '''' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
																   THEN ''''ب.خرید شماره '''' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''''رسید انبار شماره '''' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL
																	THEN ''''حواله انبار شماره '''' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''''جابه جای شماره '''' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
																   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) 
																   IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
																   THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
																   THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL 
																   THEN 7 ELSE NULL END AS TypeFact, AID, Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
											 FROM         [TRANSACTION]
											 WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
				WHERE     (T_Kala.VKalaID = 1) '';
		
		set @WhrStatement=''''
		set @ParmAndstr='' AND ''
  		if(NOT (@pintAid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintGoKalaid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pnvaStatdate IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.Tarikh BETWEEN @pnvaStatdate AND @pnvaEnddate)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintKalaid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @pintKalaid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintFactType IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.NoeFa = @pintFactType)'';
			set @ParmAndstr='' AND '';
		end
		
		/*------------------------------*/
		if(NOT (@pintGoid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.GOID = @pintGoid)'';
			set @ParmAndstr='' AND ''
		end
		
		if(NOT (@pintKolid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.KOLID = @pintKolid)'';
			set @ParmAndstr='' AND ''
		end
		
		if(NOT (@pintHesabid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.HESABID = @pintHesabid)'';
			set @ParmAndstr='' AND ''
		end

		if(NOT (@pintTHesabid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.THESABID = @pintTHesabid)'';
			set @ParmAndstr='' AND ''
		end
				
		set @SQLString=@SQLString + @WhrStatement + N'' ORDER BY T_NameKala.NameKala1, ISNULL(TBL_TRA.Tarikh, N''''00000000''''), TBL_TRA.Varde DESC   ; '';
		
		set @SQLString=@SQLString + N''
						Declare Cr2 Cursor Local for
						Select NameKalaID,UnitPrice1,UnitPrice2,(isnull(Varde,0) - isnull(Sadre,0)) as mandehr from [##'' + @tmpdbname +''] 
						for Update

						DECLARE @Radif int
						DECLARE @col3 int
						DECLARE @col44 int								
						DECLARE @TotalM decimal(19,4)		
						DECLARE @Col1 decimal(19,4)		
						DECLARE @Col2 decimal(19,4)		
						DECLARE @mandehr decimal(19,4)		

						set @Radif=0
						set @col44=0
						set @TotalM=0
						Open Cr2
							fetch Next from Cr2 into @col3,@Col1,@Col2,@mandehr
							while @@FETCH_STATUS=0
							Begin
								if(@col3 <> @col44)
									set	@TotalM = 0
								
								Set @col44 = @col3
								Set @Radif = @Radif + 1
								Set @TotalM = @TotalM + @mandehr
								
								Update ##'' + @tmpdbname +'' set UnitPrice1=@Radif,UnitPrice2=@TotalM where current of cr2;
								fetch Next from Cr2 into @col3,@Col1,@Col2,@mandehr
							End	
						
						Close cr2
						deallocate cr2
						''

						EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
										@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
										@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
										@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

						EXEC (''select * from [##'' + @tmpdbname +''] 
								DROP TABLE ##'' + @tmpdbname +'''');

	
	end
                                               
return
end




















' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900390022]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'create PROCEDURE [std_050023008900390022]
(
@pvchAction			    nvarchar(50)=''repgen'', /* Analyze - Print */
@pintGoid				int=null, 
@pintKolid				int=null, 
@pintHesabid			int=null, 
@pintTHesabid			int=null,
@pintAid				int=null, 
@pintGoKalaid			int=null, 
@pintKalaid				int=null, 
@pnvaStatdate			nvarchar(50)=null,
@pnvaEnddate			nvarchar(50)=null,
@pintFactType			int=null, 
@pnvaFactorStartid		nvarchar(50)=null,
@pnvaFactorEndid		nvarchar(50)=null,
@pnvaSanadStartid		int=null,
@pnvaSanadEndid			int=null,
@pnvaSrt				nvarchar(255)=null,
@pnvaShowkala			nvarchar(255)=null

)        

 AS 
 
 begin
	SET NOCOUNT ON;
	
	DECLARE @SQLString  NVARCHAR(4000);        
	DECLARE @WhrStatement  NVARCHAR(2000);        
	DECLARE @ParmDefinition  NVARCHAR(2000);        
	DECLARE @ParmAndstr  NVARCHAR(2000);        
	
	set @WhrStatement  = N'''';        
	set @ParmAndstr = N'''';        
    
    	SET @ParmDefinition = N''@pvchAction  nvarchar(50)=''''repgen'''', 
								@pintGoid				int=null, 
								@pintKolid				int=null, 
								@pintHesabid			int=null, 
								@pintTHesabid			int=null,
								@pintAid				int=null, 
								@pintGoKalaid			int=null, 
								@pintKalaid				int=null, 
								@pnvaStatdate			nvarchar(50)=null,
								@pnvaEnddate			nvarchar(50)=null,
								@pintFactType			int=null, 
								@pnvaFactorStartid		nvarchar(50)=null,
								@pnvaFactorEndid		nvarchar(50)=null,
								@pnvaSanadStartid		int=null,
								@pnvaSanadEndid			int=null,
								@pnvaSrt				nvarchar(255)=null,
								@pnvaShowkala			nvarchar(255)=null'';

    
    
	/* Build the SQL string one time. */

    /* Specify the parameter format one time. */
	if(@pvchAction =''repgen'')
		begin
		
SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''موجودی اول دوره'' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
                      + T_Kala.Mojodi AS Tedad, CASE WHEN ISNULL(TBL_TRA.Varde, T_Kala.Mojodi) > 0 THEN 1 ELSE 2 END as srt2, ISNULL(TBL_TRA.Varde, T_Kala.Mojodi) AS Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''00000000'') 
                      AS Tarikh, T_Kala.NameKalaID, T_NameKala.NameKala1, T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, 
                      T_NameVahed_2.NameVahed AS NameVahed2, T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, 
                      TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, T_NameGoKala.NameGoKala1 AS NameGoKala
FROM         T_NameKala INNER JOIN
                      T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
                      T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
                      T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
                      T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
                          (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''خرید شماره '' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
                                                   THEN ''فروش شماره '' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''ب.فروش شماره '' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
                                                   THEN ''ب.خرید شماره '' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''رسید انبار شماره '' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL
                                                    THEN ''حواله انبار شماره '' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''جابه جای شماره '' ELSE NULL END AS NoeFa, 
                                                   OperationCode AS ShomareFactor, TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
                                                   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
                                                   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) 
                                                   IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                                                   THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
                                                   THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 7 ELSE NULL 
                                                   END AS TypeFact, AID, Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, 
                                                   KOLID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
ORDER BY ISNULL(TBL_TRA.Tarikh, N''00000000'')          
		end	
    	if(@pvchAction =''Print'')
		begin
	
	  		 SET @SQLString =

				''SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''''موجودی اول دوره'''' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
									  + T_Kala.Mojodi AS Tedad, CASE WHEN ISNULL(TBL_TRA.Varde, T_Kala.Mojodi) > 0 THEN 1 ELSE 2 END AS srt2, ISNULL(TBL_TRA.Varde, T_Kala.Mojodi) AS Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''''00000000'''') AS Tarikh, T_Kala.NameKalaID, T_NameKala.NameKala1, 
									  T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, T_NameKala.Zarib, 
									  T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, T_Kala.GOKID, 
									  T_NameGoKala.NameGoKala1 AS NameGoKala
				FROM         T_NameKala INNER JOIN
									  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
									  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
									  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
									  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
									  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
										  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''''خرید شماره '''' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
																   THEN ''''فروش شماره '''' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''''ب.فروش شماره '''' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
																   THEN ''''ب.خرید شماره '''' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''''رسید انبار شماره '''' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL
																	THEN ''''حواله انبار شماره '''' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''''جابه جای شماره '''' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
																   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) 
																   IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
																   THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
																   THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL 
																   THEN 7 ELSE NULL END AS TypeFact, AID, Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
											 FROM         [TRANSACTION]
											 WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
				WHERE     (T_Kala.VKalaID = 1) ''
	
			
			set @ParmAndstr='' AND ''
  			if(NOT (@pintAid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintGoKalaid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pnvaStatdate IS NULL) AND NOT (@pnvaEnddate IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(ISNULL(TBL_TRA.Tarikh, N''''00000000'''') BETWEEN @pnvaStatdate AND @pnvaEnddate)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintKalaid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @pintKalaid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintFactType IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.TypeFact = @pintFactType)'';
				set @ParmAndstr='' AND ''
			end

			/*------------------------------*/
			if(NOT (@pintGoid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.GOID = @pintGoid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintKolid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.KOLID = @pintKolid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintHesabid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.HESABID = @pintHesabid)'';
				set @ParmAndstr='' AND ''
			end

			if(NOT (@pintTHesabid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.THESABID = @pintTHesabid)'';
				set @ParmAndstr='' AND ''
			end

			set @SQLString=@SQLString + @WhrStatement + N'' ORDER BY TBL_TRA.Tarikh''
	
	
			EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
								@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
								@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
								@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

		end
    	if(@pvchAction =''Analyze'')
		begin
	  		 
	  		 DECLARE @tmpdbname varchar(50)
	  		 set @tmpdbname= right( REPLACE( CONVERT(uniqueidentifier, newid()), ''-'', ''''),8) ;
	  		 
	  		  
	  		 
	  		 IF OBJECT_ID(''tempdb..##'' + @tmpdbname +'''' ) IS NOT NULL
				exec (''DROP TABLE ##'' + @tmpdbname +'''');
	  		 
	  		 SET @SQLString =
				 N''SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''''موجودی اول دوره'''' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
									  + T_Kala.Mojodi AS Tedad, TBL_TRA.Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''''00000000'''') AS Tarikh, T_Kala.NameKalaID, T_NameKala.NameKala1, 
									  T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, T_NameKala.Zarib, 
									  T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, 
									  T_NameGoKala.NameGoKala1 AS NameGoKala, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3
				INTO         ##'' + @tmpdbname +''
				FROM         T_NameKala INNER JOIN
									  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
									  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
									  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
									  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
									  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
										  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''''خرید شماره '''' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
																   THEN ''''فروش شماره '''' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''''ب.فروش شماره '''' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
																   THEN ''''ب.خرید شماره '''' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''''رسید انبار شماره '''' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL
																	THEN ''''حواله انبار شماره '''' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''''جابه جای شماره '''' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
																   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) 
																   IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
																   THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
																   THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL 
																   THEN 7 ELSE NULL END AS TypeFact, AID, Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
											 FROM         [TRANSACTION]
											 WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
				WHERE     (T_Kala.VKalaID = 1) '';
		
		set @WhrStatement=''''
		set @ParmAndstr='' AND ''
  		if(NOT (@pintAid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintGoKalaid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pnvaStatdate IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(ISNULL(TBL_TRA.Tarikh, N''''00000000'''') BETWEEN @pnvaStatdate AND @pnvaEnddate)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintKalaid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @pintKalaid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintFactType IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.TypeFact = @pintFactType)'';
			set @ParmAndstr='' AND '';
		end
		
		/*------------------------------*/
		if(NOT (@pintGoid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.GOID = @pintGoid)'';
			set @ParmAndstr='' AND ''
		end
		
		if(NOT (@pintKolid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.KOLID = @pintKolid)'';
			set @ParmAndstr='' AND ''
		end
		
		if(NOT (@pintHesabid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.HESABID = @pintHesabid)'';
			set @ParmAndstr='' AND ''
		end

		if(NOT (@pintTHesabid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.THESABID = @pintTHesabid)'';
			set @ParmAndstr='' AND ''
		end
				
		set @SQLString=@SQLString + @WhrStatement + N'' ORDER BY T_NameKala.NameKala1, ISNULL(TBL_TRA.Tarikh, N''''00000000''''), CASE WHEN ISNULL(TBL_TRA.Varde, T_Kala.Mojodi) > 0 THEN 1 ELSE 2 END ; '';
		
		set @SQLString=@SQLString + N''
						Declare Cr2 Cursor Local for
						Select NameKalaID,UnitPrice1,UnitPrice2,(ISNULL(Varde, tedad) - isnull(Sadre,0)) as mandehr from [##'' + @tmpdbname +''] 
						for Update

						DECLARE @Radif int
						DECLARE @col3 int
						DECLARE @col44 int								
						DECLARE @TotalM decimal(19,4)		
						DECLARE @Col1 decimal(19,4)		
						DECLARE @Col2 decimal(19,4)		
						DECLARE @mandehr decimal(19,4)		

						set @Radif=0
						set @col44=0
						set @TotalM=0
						Open Cr2
							fetch Next from Cr2 into @col3,@Col1,@Col2,@mandehr
							while @@FETCH_STATUS=0
							Begin
								if(@col3 <> @col44)
									set	@TotalM = 0
								
								Set @col44 = @col3
								Set @Radif = @Radif + 1
								Set @TotalM = @TotalM + @mandehr
								
								Update ##'' + @tmpdbname +'' set UnitPrice1=@Radif,UnitPrice2=@TotalM where current of cr2;
								fetch Next from Cr2 into @col3,@Col1,@Col2,@mandehr
							End	
						
						Close cr2
						deallocate cr2
						''

						EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
										@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
										@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
										@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

						EXEC (''select * from [##'' + @tmpdbname +'']
								DROP TABLE ##'' + @tmpdbname +'''');

	
	end
                                               
return
end





' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_05002300880019****]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'Create PROCEDURE [std_05002300880019****] AS SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''موجودی اول دوره'' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, 
                      ISNULL(TBL_TRA.Tedad, 0) + dbo.T_Kala.Mojodi AS Tedad, TBL_TRA.Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, TBL_TRA.AID, TBL_TRA.Tarikh, 
                      dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1, dbo.T_Anbar.NameAnbar, dbo.T_NameKala.KalaNumber, dbo.T_NameVahed.NameVahed, 
                      T_NameVahed_2.NameVahed AS NameVahed2, dbo.T_NameKala.Zarib, dbo.T_NameKala.MinMogoody, dbo.T_NameKala.MaxMogoody, 
                      dbo.T_NameKala.BarCode, TBL_TRA.Namehesab, TBL_TRA.NoeFa
FROM         dbo.T_NameKala INNER JOIN
                      dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID INNER JOIN
                      dbo.T_Anbar ON dbo.T_Kala.AID = dbo.T_Anbar.AID INNER JOIN
                      dbo.T_NameVahed ON dbo.T_NameKala.VAHED1 = dbo.T_NameVahed.VAHED1 LEFT OUTER JOIN
                      dbo.T_NameVahed T_NameVahed_2 ON dbo.T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
                          (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''خرید شماره '' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
                                                   THEN ''فروش شماره '' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''ب.فروش شماره '' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
                                                   THEN ''ب.خرید شماره '' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''رسید انبار شماره '' WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                                                   IS NOT NULL THEN ''حواله انبار شماره '' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''جابه جای شماره '' ELSE '''' END AS NoeFa, 
                                                   OperationCode AS ShomareFactor, 
                                                   TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
                                                   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
                                                   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, 
                                                   CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
                                                   THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
                                                   THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL 
                                                   THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 7 ELSE '''' END AS TypeFact, AID, Date1 AS Tarikh, KALAID, 
                                                   dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab
                             FROM         dbo.[TRANSACTION]) TBL_TRA ON dbo.T_Kala.AID = TBL_TRA.AID AND dbo.T_Kala.NameKalaID = TBL_TRA.KALAID
WHERE     (dbo.T_Kala.VKalaID = 1) AND (ISNULL(TBL_TRA.Tedad, 0) + dbo.T_Kala.Mojodi <> 0)


ORDER BY TBL_TRA.Tarikh' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[SP_VosoolManabe_CheckDblicateShomare]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
Create PROCEDURE [SP_VosoolManabe_CheckDblicateShomare]
	-- Add the parameters for the stored procedure here

        @ResultVal INT =0 OUTPUT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	
	declare @cntTedadManfi INT
	
SELECT     @cntTedadManfi=COUNT(*) 
FROM         T_NameKala
WHERE     (ID IN
                          (SELECT     T_Kala.NameKalaID
                             FROM         T_NameKala INNER JOIN
                                                   T_Kala ON T_NameKala.ID = T_Kala.NameKalaID LEFT OUTER JOIN
                                                   [TRANSACTION] ON T_Kala.NameKalaID = [TRANSACTION].KALAID AND T_Kala.AID = [TRANSACTION].AID AND T_Kala.GOKID = [TRANSACTION].GOKID LEFT OUTER JOIN
                                                       (SELECT     NameKalaID, SUM(CONVERT(float, Mojodi)) AS Mojodi, SUM(Mojodi * UnitPrice) AS MojodiRiali, AID
                                                          FROM         T_Kala AS T_Kala_1
                                                          GROUP BY NameKalaID, AID) AS derivedtbl_1 ON T_Kala.NameKalaID = derivedtbl_1.NameKalaID AND T_Kala.AID = derivedtbl_1.AID
                             GROUP BY T_Kala.NameKalaID, T_NameKala.NameKala1, derivedtbl_1.Mojodi, T_Kala.AID
                             HAVING      (ISNULL(ROUND(SUM(([TRANSACTION].TedadKh + [TRANSACTION].TedadBarFo) - ([TRANSACTION].TedadFo + [TRANSACTION].TedadBarKh)), 2), 0) + ISNULL(derivedtbl_1.Mojodi, 0) 
                                                   < 0)))

	
	if (@cntTedadManfi= 0)
		BEGIN
			SET @ResultVal=0
			return 0
		END
	ELSE
		BEGIN
			SET @ResultVal=1
			return 1
		END
END


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_THesab_BedBes]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_THesab_BedBes](@TypRep smallint, @ACCID int, @Startdate nvarchar(8), @Enddate nvarchar(8)) AS SELECT     dbo.T_T_Heasb.THESABID, dbo.T_T_Heasb.NameHesabT, SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) AS mande,                        dbo.[TRANSACTION].HESABID, SUM(dbo.[TRANSACTION].DARBed) AS DARBed, SUM(dbo.[TRANSACTION].PARBes) AS PARBes,                        dbo.T_T_Heasb.THESABNumber FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_T_Heasb ON dbo.[TRANSACTION].THESABID = dbo.T_T_Heasb.THESABID WHERE     (dbo.[TRANSACTION].Date1 BETWEEN @Startdate AND @Enddate) OR                       (dbo.[TRANSACTION].Date1 BETWEEN @Startdate AND @Enddate) GROUP BY dbo.T_T_Heasb.THESABID, dbo.T_T_Heasb.NameHesabT, dbo.[TRANSACTION].HESABID, dbo.T_T_Heasb.THESABNumber HAVING      (SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0) AND (@TypRep = 1) AND (dbo.[TRANSACTION].HESABID = @ACCID) OR                       (SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0) AND (@TypRep = 2) AND (dbo.[TRANSACTION].HESABID = @ACCID) ORDER BY dbo.T_T_Heasb.NameHesabT ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_sood]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'          CREATE PROCEDURE [Rep_sood](@startdate nvarchar(50), @enddate nvarchar(50)) AS SELECT     dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].PARBes) AS PARBes, SUM(dbo.[TRANSACTION].DARBed) AS DARBed,                        dbo.[TRANSACTION].GOID, SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) AS Mande, dbo.T_KolHesab.NameHesab,                        @startdate AS StartDate, @enddate AS enddate FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID WHERE     (dbo.[TRANSACTION].Date1 BETWEEN @startdate AND @enddate) GROUP BY dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, dbo.T_KolHesab.NameHesab HAVING      (dbo.[TRANSACTION].GOID >= 8) AND (SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) <> 0) AND                        (NOT (dbo.[TRANSACTION].KOLID IN (24, 25, 30)))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_Daftar_Roozname]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_Daftar_Roozname](@Text4 int, @Text6 int, @Text8 nvarchar(50), @Text10 nvarchar(50)) AS SELECT     SAID2, TextNHSID2, TextShSA, PARBes, DARBed, TEMPR, Date1, GOID, KOLID, THESABID, HESABID FROM         dbo.[TRANSACTION] WHERE     (SAID2 BETWEEN @Text4 AND @Text6) AND (Date1 BETWEEN @Text8 AND @Text10)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_BarKharid]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Rep_BarKharid](@Factid int) AS SELECT     dbo.T_NameKala.NameKala1 + N'' '' + CONVERT(nvarchar(50), dbo.T_NameKala.Note) AS NameKala1, dbo.T_Dar8.DARID8,                        dbo.[TRANSACTION].TedadBarKh, dbo.[TRANSACTION].UnitPriceBarKh, dbo.[TRANSACTION].TedadBarKh * dbo.[TRANSACTION].UnitPriceBarKh AS Mab,                        dbo.[TRANSACTION].GOKID, dbo.[TRANSACTION].Sharh, dbo.HesabName(dbo.T_Dar8.KOLID, dbo.T_Dar8.HesabID, dbo.T_Dar8.THESABID)                        AS NameHesab, dbo.T_Dar8.Takhfif, dbo.T_Dar8.DateKh, dbo.T_Dar8.DARID8Numbr, dbo.T_Dar8.Sharh AS Note,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Dar8.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Dar8.HesabID,                        0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Dar8.THESABID, 0)), 4), 1, 12) AS ACID, dbo.T_Dar8.PrimarySaid2 AS PSAID2,                        dbo.T_NameKala.Zarib, dbo.[TRANSACTION].FieldN7, dbo.T_NameKala.Currency1, dbo.[TRANSACTION].radif, dbo.T_NameKala.Note AS Expr1 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID INNER JOIN                       dbo.T_Dar8 ON dbo.[TRANSACTION].DARID8 = dbo.T_Dar8.DARID8 WHERE     (dbo.T_Dar8.DARID8 = @Factid) ORDER BY dbo.T_NameKala.NameKala1 + N'' '' + CONVERT(nvarchar(50), dbo.T_NameKala.Note), dbo.[TRANSACTION].GOKID ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_BarForoosh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Rep_BarForoosh](@Factid int) AS SELECT     dbo.T_NameKala.NameKala1 + N'' '' + CONVERT(nvarchar(50), dbo.T_NameKala.Note) AS NameKala1, dbo.T_Par7.PARID7,                        dbo.[TRANSACTION].TedadBarFo, dbo.[TRANSACTION].UnitPriceBarFo, dbo.[TRANSACTION].TedadBarFo * dbo.[TRANSACTION].UnitPriceBarFo AS Mab,                        dbo.[TRANSACTION].GOKID, dbo.[TRANSACTION].Sharh, dbo.HesabName(dbo.T_Par7.KOLID, dbo.T_Par7.HesabID, dbo.T_Par7.THESABID)                        AS NameHesab, dbo.T_Par7.Takhfif, dbo.T_Par7.DateFo, dbo.T_Par7.PARID7Number, dbo.T_Par7.Sharh AS Note,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Par7.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Par7.HesabID,                        0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Par7.THESABID, 0)), 4), 1, 12) AS ACID, dbo.T_Par7.PrimarySaid2 AS PSAID2,                        dbo.T_NameKala.Zarib, dbo.[TRANSACTION].FieldN7, dbo.T_NameKala.Currency1, dbo.[TRANSACTION].radif, dbo.T_NameKala.Note AS Expr1 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID INNER JOIN                       dbo.T_Par7 ON dbo.[TRANSACTION].PARID7 = dbo.T_Par7.PARID7 WHERE     (dbo.T_Par7.PARID7 = @Factid) ORDER BY dbo.T_NameKala.NameKala1 + N'' '' + CONVERT(nvarchar(50), dbo.T_NameKala.Note), dbo.[TRANSACTION].GOKID ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_032599]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
    /*  بروز رساني گزارش سود و زيان معين */ CREATE PROCEDURE [Rep_032599] (@startDate nvarchar(50), @enddate nvarchar(50)) AS          SELECT     [TRANSACTION].GOID, [TRANSACTION].KOLID, SUM([TRANSACTION].DARBed) AS SumOfDARBed, SUM([TRANSACTION].PARBes) AS SumOfPARBes, ABS(SUM([TRANSACTION].DARBed)                        - SUM([TRANSACTION].PARBes)) AS Expr1, T_KolHesab.FieldN1, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].HESABID, T_KolHesab.KOLID AS Expr2,                        MIN([TRANSACTION].Date1) AS MinOfDate1, MAX([TRANSACTION].Date1) AS MaxOfDate1, T_KolHesab.NameHesab, T_GropHesab.NameGo, T_Heasb.NameHesab AS NameHesabMo FROM         [TRANSACTION] INNER JOIN                       T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID INNER JOIN                       T_GropHesab ON T_KolHesab.GOID = T_GropHesab.GoID LEFT OUTER JOIN                       T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) GROUP BY [TRANSACTION].GOID, [TRANSACTION].KOLID, T_KolHesab.FieldN1, T_KolHesab.KOLID, [TRANSACTION].HESABID, T_KolHesab.NameHesab, T_GropHesab.NameGo, T_Heasb.NameHesab HAVING      ([TRANSACTION].GOID >= 8) AND (@startDate IS NULL) AND (SUM([TRANSACTION].DARBed) IS NOT NULL) AND (SUM([TRANSACTION].PARBes) IS NOT NULL) OR                       ([TRANSACTION].GOID >= 8) AND (@startDate IS NOT NULL) AND (SUM([TRANSACTION].DARBed) IS NOT NULL) AND (SUM([TRANSACTION].PARBes) IS NOT NULL)
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_032598]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
         CREATE PROCEDURE [Rep_032598](@startDate nvarchar(50), @enddate nvarchar(50)) AS           SELECT     [TRANSACTION].GOID, [TRANSACTION].KOLID, SUM([TRANSACTION].DARBed) AS SumOfDARBed, SUM([TRANSACTION].PARBes) AS SumOfPARBes, ABS(SUM([TRANSACTION].DARBed)                        - SUM([TRANSACTION].PARBes)) AS Expr1, T_KolHesab.FieldN1, [TRANSACTION].Date1, @startDate AS startDate, @enddate AS enddate, T_KolHesab.KOLID AS Expr3, MIN([TRANSACTION].Date1)                        AS MinOfDate1, MAX([TRANSACTION].Date1) AS MaxOfDate1, T_GropHesab.NameGo, T_KolHesab.NameHesab FROM         [TRANSACTION] INNER JOIN                       T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID INNER JOIN                       T_GropHesab ON T_KolHesab.GOID = T_GropHesab.GoID GROUP BY [TRANSACTION].GOID, [TRANSACTION].KOLID, T_KolHesab.FieldN1, [TRANSACTION].Date1, T_KolHesab.KOLID, T_GropHesab.NameGo, T_KolHesab.NameHesab HAVING      ([TRANSACTION].GOID >= 8) AND (@startDate IS NULL) AND (SUM([TRANSACTION].DARBed) IS NOT NULL) AND (SUM([TRANSACTION].PARBes) IS NOT NULL) OR                       ([TRANSACTION].GOID >= 8) AND (@startDate IS NOT NULL) AND (SUM([TRANSACTION].DARBed) IS NOT NULL) AND (SUM([TRANSACTION].PARBes) IS NOT NULL) AND                        ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_ForooshSandogh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Rep_ForooshSandogh] (@Factid int) AS SELECT     dbo.T_NameKala.NameKala1 AS NameKala1, dbo.T_Forosh.FOROSHID, dbo.[TRANSACTION].TedadFo, dbo.[TRANSACTION].UnitPriceFo,                        dbo.[TRANSACTION].TedadFo * dbo.[TRANSACTION].UnitPriceFo AS Mab, dbo.[TRANSACTION].GOKID, dbo.[TRANSACTION].Sharh,                        dbo.HesabName(dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID) AS NameHesab, dbo.T_Forosh.Takhfif, dbo.T_Forosh.DateFo,                        dbo.T_Forosh.ForoshNumbr, dbo.T_Forosh.Sharh AS Note, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, 12) AS ACID, dbo.T_Forosh.PrimarySaid2 AS PSAID2, dbo.T_NameKala.Zarib, dbo.[TRANSACTION].FieldN7,                        dbo.T_NameKala.Currency1, dbo.[TRANSACTION].radif, dbo.T_NameKala.Note AS Expr1, dbo.[TRANSACTION].AID, dbo.T_Anbar.NameAnbar,                        dbo.T_NameVahed.NameVahed, dbo.T_Anbar.Address FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID INNER JOIN                       dbo.T_Forosh ON dbo.[TRANSACTION].FOROSHID = dbo.T_Forosh.FOROSHID INNER JOIN                       dbo.T_Anbar ON dbo.[TRANSACTION].AID = dbo.T_Anbar.AID INNER JOIN                       dbo.T_NameVahed ON dbo.T_NameKala.VAHED1 = dbo.T_NameVahed.VAHED1 WHERE     (dbo.T_Forosh.FOROSHID = @Factid) ORDER BY dbo.T_NameKala.NameKala1, dbo.[TRANSACTION].GOKID ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_KolHesab_BedBes]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_KolHesab_BedBes](@TypRep smallint, @ACCID int, @Startdate nvarchar(8), @Enddate nvarchar(8)) AS SELECT     dbo.T_KolHesab.KOLID, dbo.T_KolHesab.NameHesab, SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) AS mande,                        dbo.[TRANSACTION].GOID, SUM(dbo.[TRANSACTION].DARBed) AS DARBed, SUM(dbo.[TRANSACTION].PARBes) AS PARBes,                        dbo.T_KolHesab.KolNumber FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID WHERE     (dbo.[TRANSACTION].Date1 BETWEEN @Startdate AND @Enddate) OR                       (dbo.[TRANSACTION].Date1 BETWEEN @Startdate AND @Enddate) GROUP BY dbo.T_KolHesab.KOLID, dbo.T_KolHesab.NameHesab, dbo.[TRANSACTION].GOID, dbo.T_KolHesab.KolNumber HAVING      (SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0) AND (@TypRep = 1) AND (dbo.[TRANSACTION].GOID = @ACCID) OR                       (SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0) AND (@TypRep = 2) AND (dbo.[TRANSACTION].GOID = @ACCID) ORDER BY dbo.T_KolHesab.NameHesab ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_MOHesab_BedBes]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_MOHesab_BedBes](@TypRep smallint, @ACCID int, @Startdate nvarchar(8), @Enddate nvarchar(8)) AS SELECT     dbo.T_Heasb.HESABID, dbo.T_Heasb.NameHesab, SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) AS mande,                        dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS DARBed, SUM(dbo.[TRANSACTION].PARBes) AS PARBes,                        dbo.T_Heasb.HESABNumber FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Heasb ON dbo.[TRANSACTION].HESABID = dbo.T_Heasb.HESABID WHERE     (dbo.[TRANSACTION].Date1 BETWEEN @Startdate AND @Enddate) OR                       (dbo.[TRANSACTION].Date1 BETWEEN @Startdate AND @Enddate) GROUP BY dbo.T_Heasb.HESABID, dbo.T_Heasb.NameHesab, dbo.[TRANSACTION].KOLID, dbo.T_Heasb.HESABNumber HAVING      (SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0) AND (@TypRep = 1) AND (dbo.[TRANSACTION].KOLID = @ACCID) OR                       (SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0) AND (@TypRep = 2) AND (dbo.[TRANSACTION].KOLID = @ACCID) ORDER BY dbo.T_Heasb.NameHesab ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm3_CloseShift]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QSubPFrm3_CloseShift](@foroshid int) AS SELECT     TOP 100 PERCENT AID, KALAID, Sharh, Date1, FOROSHID, TedadFo, UnitPriceFo, CONVERT(money, TedadFo * UnitPriceFo) AS Sumation, radif,                        TextAIDKharid, FieldN6, GOKID, FOROSHID AS LinkedID, dbo.KalaName(KALAID) AS NameKala FROM         dbo.[TRANSACTION] WHERE     (FOROSHID = @foroshid) ORDER BY radif  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm2_CloseShift]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [QSubPFrm2_CloseShift](@VFlag bit, @dateFo nvarchar(255)) AS SELECT     dbo.T_Forosh.FOROSHID, dbo.T_Forosh.DateFo, dbo.T_Forosh.Flag1, dbo.HesabName(dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID,                        dbo.T_Forosh.THESABID) AS NameHesab, dbo.[TRANSACTION].FieldN2, SUM(CONVERT(money,                        dbo.[TRANSACTION].TedadFo * dbo.[TRANSACTION].UnitPriceFo)) AS sumation, CONVERT(nvarchar(50), dbo.T_Forosh.Sharh) AS sharh,                        dbo.T_Forosh.Takhfif, F_MabHazine_Sandogh.mabhazine AS mabhazine, SUM(CONVERT(money, CASE WHEN dbo.T_Forosh.KOLID IS NULL                        THEN (dbo.[TRANSACTION].TedadFo * dbo.[TRANSACTION].UnitPriceFo) ELSE 0 END)) AS MabSaleSandogh, SUM(CONVERT(money,                        CASE WHEN dbo.T_Forosh.KOLID IS NOT NULL THEN (dbo.[TRANSACTION].TedadFo * dbo.[TRANSACTION].UnitPriceFo) ELSE 0 END))                        AS MabSaleCust, CASE WHEN dbo.T_Forosh.KOLID IS NOT NULL THEN (1) ELSE 0 END AS CountSaleCust, dbo.T_Forosh.NumberKH,                        dbo.T_Forosh.ForoshNumbr FROM         dbo.T_Forosh INNER JOIN                       dbo.[TRANSACTION] ON dbo.T_Forosh.FOROSHID = dbo.[TRANSACTION].FOROSHID LEFT OUTER JOIN                       dbo.F_MabHazine_Sandogh() F_MabHazine_Sandogh ON dbo.T_Forosh.FOROSHID = F_MabHazine_Sandogh.Factid LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Forosh.PrimarySaid2 = dbo.T_SA2.SAID2 WHERE     (dbo.T_SA2.FieldN2 = 26) OR                       (dbo.T_SA2.FieldN2 IS NULL) GROUP BY dbo.T_Forosh.FOROSHID, dbo.T_Forosh.DateFo, dbo.HesabName(dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID),                        dbo.[TRANSACTION].FieldN2, dbo.T_Forosh.Flag1, CONVERT(nvarchar(50), dbo.T_Forosh.Sharh), dbo.T_Forosh.Takhfif,                        F_MabHazine_Sandogh.mabhazine, CASE WHEN dbo.T_Forosh.KOLID IS NOT NULL THEN (1) ELSE 0 END, dbo.T_Forosh.NumberKH,                        dbo.T_Forosh.ForoshNumbr HAVING      (dbo.T_Forosh.Flag1 = @VFlag) AND (dbo.T_Forosh.DateFo = @dateFo) OR                       (dbo.T_Forosh.Flag1 = @VFlag) AND (dbo.T_Forosh.DateFo = @dateFo) ORDER BY dbo.T_Forosh.DateFo DESC, dbo.T_Forosh.FOROSHID DESC ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_000000000000]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_000000000000](@startSanadID int, @endSanadID int, @startDate nvarchar(255), @enddate nvarchar(255), @AccID nvarchar(50)) AS SELECT     dbo.[TRANSACTION].FOROSHID, dbo.[TRANSACTION].TextAIDFo, dbo.[TRANSACTION].UnitPriceFo, dbo.[TRANSACTION].TedadFo, dbo.T_Forosh.Takhfif,                        dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.T_Forosh.DateFo,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) AS hesabidt, dbo.T_Forosh.DateFo AS MinOfDateFo, dbo.T_Forosh.DateFo AS MaxOfDateFo,                        @startDate AS startDate, @startSanadID AS startSanadID, @AccID AS AccID, dbo.[TRANSACTION].KALAID, dbo.[TRANSACTION].VKALAID,                        CONVERT(money, SUM(CONVERT(money, ABS(dbo.[TRANSACTION].FieldN3)))) AS abs, dbo.HesabName(dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab FROM         dbo.T_Forosh INNER JOIN                       dbo.[TRANSACTION] ON dbo.T_Forosh.FOROSHID = dbo.[TRANSACTION].FOROSHID WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].FOROSHID, dbo.[TRANSACTION].TextAIDFo, dbo.[TRANSACTION].UnitPriceFo, dbo.[TRANSACTION].TedadFo,                        dbo.T_Forosh.Takhfif, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID,                        dbo.T_Forosh.DateFo, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)), dbo.[TRANSACTION].KALAID, dbo.[TRANSACTION].VKALAID,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND (dbo.T_Forosh.DateFo BETWEEN                        @startDate AND @enddate) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].FOROSHID BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].FOROSHID BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NULL) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) AND (dbo.[TRANSACTION].FOROSHID BETWEEN                        @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) AND (dbo.[TRANSACTION].FOROSHID BETWEEN                        @startSanadID AND @endSanadID) ORDER BY dbo.[TRANSACTION].FOROSHID  ' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021342]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021342](@startSanadID int, @endSanadID int, @startDate nvarchar(255), @enddate nvarchar(255), @AccID nvarchar(50)) AS SELECT     dbo.[TRANSACTION].PARID7, dbo.[TRANSACTION].TedadBarFo, dbo.[TRANSACTION].UnitPriceBarFo, CONVERT(money, SUM(CONVERT(money,                        ABS(dbo.[TRANSACTION].FieldN3)))) AS abs, dbo.[TRANSACTION].KALAID, dbo.T_Par7.DateFo, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) AS hesabidt,                        @startDate AS startDate, @startSanadID AS startSanadID, @AccID AS AccID, dbo.[TRANSACTION].TextAIDFo, dbo.T_Par7.Takhfif,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab,                        dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Par7 ON dbo.[TRANSACTION].PARID7 = dbo.T_Par7.PARID7 INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].PARID7, dbo.[TRANSACTION].TedadBarFo, dbo.[TRANSACTION].UnitPriceBarFo, dbo.T_Par7.DateFo,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)), dbo.[TRANSACTION].KALAID, dbo.[TRANSACTION].TextAIDFo, dbo.T_Par7.Takhfif,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID), dbo.T_NameKala.KalaNumber,                        dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1 HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.T_Par7.DateFo BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND (dbo.T_Par7.DateFo BETWEEN                        @startDate AND @enddate) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].PARID7 BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.[TRANSACTION].PARID7 BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NULL) AND (dbo.T_Par7.DateFo BETWEEN @startDate AND @enddate) AND (dbo.[TRANSACTION].PARID7 BETWEEN @startSanadID AND                        @endSanadID) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND (dbo.T_Par7.DateFo BETWEEN                        @startDate AND @enddate) AND (dbo.[TRANSACTION].PARID7 BETWEEN @startSanadID AND @endSanadID) ORDER BY dbo.[TRANSACTION].PARID7  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021341]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021341](@startSanadID int, @endSanadID int, @startDate nvarchar(255), @enddate nvarchar(255), @AccID nvarchar(50)) AS SELECT     dbo.[TRANSACTION].KHOROJID, CONVERT(money, SUM(CONVERT(money, ABS(dbo.[TRANSACTION].FieldN3)))) AS abs,                        dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].UnitPriceFo, dbo.[TRANSACTION].TedadKhorooj, dbo.T_KhorojKala.DAteKh,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) AS hesabidt, @startDate AS startDate, @startSanadID AS startSanadID, @AccID AS AccID,                        dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1, dbo.HesabName(dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KhorojKala ON dbo.[TRANSACTION].KHOROJID = dbo.T_KhorojKala.KHOROJID INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].KHOROJID, dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].UnitPriceFo, dbo.[TRANSACTION].TedadKhorooj,                        dbo.T_KhorojKala.DAteKh, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)), dbo.T_NameKala.KalaNumber,                        dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1, dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID,                        dbo.[TRANSACTION].THESABID) HAVING      (@AccID IS NULL) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.T_KhorojKala.DAteKh BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND (dbo.T_KhorojKala.DAteKh BETWEEN                        @startDate AND @enddate) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].KHOROJID BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.[TRANSACTION].KHOROJID BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NULL) AND (dbo.T_KhorojKala.DAteKh BETWEEN @startDate AND @enddate) AND (dbo.[TRANSACTION].KHOROJID BETWEEN                        @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) AND (dbo.T_KhorojKala.DAteKh BETWEEN                        @startDate AND @enddate) AND (dbo.[TRANSACTION].KHOROJID BETWEEN @startSanadID AND @endSanadID) ORDER BY dbo.[TRANSACTION].KHOROJID  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021340]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021340](@startSanadID int, @endSanadID int, @startDate nvarchar(255), @enddate nvarchar(255), @AccID nvarchar(50)) AS SELECT     CONVERT(int, dbo.T_Forosh.ForoshNumbr) AS FOROSHID, dbo.[TRANSACTION].TextAIDFo, dbo.[TRANSACTION].UnitPriceFo,                        dbo.[TRANSACTION].TedadFo, dbo.T_Forosh.Takhfif, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID,                        dbo.[TRANSACTION].THESABID, dbo.T_Forosh.DateFo, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, dbo.T_Forosh.DateFo AS MinOfDateFo, dbo.T_Forosh.DateFo AS MaxOfDateFo, @startDate AS startDate,                        @startSanadID AS startSanadID, @AccID AS AccID, dbo.[TRANSACTION].KALAID, dbo.[TRANSACTION].FieldN3 AS abs,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab,                        dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_Forosh.FOROSHID AS Expr1, F_MabHazine_Foroosh.MabHazine FROM         dbo.T_Forosh INNER JOIN                       dbo.[TRANSACTION] ON dbo.T_Forosh.FOROSHID = dbo.[TRANSACTION].FOROSHID INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID LEFT OUTER JOIN                       dbo.F_MabHazine_Foroosh() F_MabHazine_Foroosh ON dbo.T_Forosh.FOROSHID = F_MabHazine_Foroosh.FactID WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND                        @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (CONVERT(int, dbo.T_Forosh.ForoshNumbr) BETWEEN                        @startSanadID AND @endSanadID) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (CONVERT(int, dbo.T_Forosh.ForoshNumbr) BETWEEN                        @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND                        @enddate) AND (CONVERT(int, dbo.T_Forosh.ForoshNumbr) BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND                        @enddate) AND (CONVERT(int, dbo.T_Forosh.ForoshNumbr) BETWEEN @startSanadID AND @endSanadID) ORDER BY CONVERT(int, dbo.T_Forosh.ForoshNumbr)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_012078]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_012078](@AccID nvarchar(50), @startDate nvarchar(50), @startSanadID nvarchar(50), @endSanadID nvarchar(50), @enddate nvarchar(50)) AS SELECT     dbo.[TRANSACTION].TextAIDKharid, dbo.[TRANSACTION].UnitPriceBarKh, dbo.[TRANSACTION].TedadBarKh, ABS(dbo.[TRANSACTION].FieldN3)                        AS Expr1, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.T_Dar8.DateKh,                        @startDate AS startDate, dbo.[TRANSACTION].DARID8, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID,                        dbo.[TRANSACTION].KALAID, @enddate AS enddate, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID INNER JOIN                       dbo.T_Dar8 ON dbo.[TRANSACTION].DARID8 = dbo.T_Dar8.DARID8 WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].TextAIDKharid, dbo.[TRANSACTION].UnitPriceBarKh, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].KALAID, ABS(dbo.[TRANSACTION].FieldN3),                        dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1, dbo.[TRANSACTION].DARID8,                        dbo.[TRANSACTION].TedadBarKh, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)), dbo.HesabName(dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID), dbo.T_Dar8.DateKh HAVING      (@startDate IS NULL) AND (@AccID IS NULL) OR                       (@startDate IS NULL) AND (@AccID IS NULL) AND (dbo.[TRANSACTION].DARID8 BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NULL) AND (dbo.T_Dar8.DateKh BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@AccID IS NULL) AND (dbo.[TRANSACTION].DARID8 BETWEEN @startSanadID AND @endSanadID) AND                        (dbo.T_Dar8.DateKh BETWEEN @startDate AND @enddate) OR                       (@startDate IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NULL) AND (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARID8 BETWEEN @startSanadID AND @endSanadID) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.T_Dar8.DateKh BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARID8 BETWEEN @startSanadID AND @endSanadID) AND                        (dbo.T_Dar8.DateKh BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_012077]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_012077](@AccID nvarchar(50), @startDate nvarchar(50), @startSanadID nvarchar(50), @endSanadID nvarchar(50), @enddate nvarchar(50)) AS SELECT     dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].UnitPriceKh, dbo.[TRANSACTION].TedadVorood, ABS(dbo.[TRANSACTION].FieldN3) AS Expr1,                        dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.T_VorodKala.DateV,                        @startDate AS startDate, dbo.[TRANSACTION].VORODID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID,                        dbo.[TRANSACTION].KALAID, @enddate AS enddate, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID INNER JOIN                       dbo.T_VorodKala ON dbo.[TRANSACTION].VORODID = dbo.T_VorodKala.VORODID WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].UnitPriceKh, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].KALAID, ABS(dbo.[TRANSACTION].FieldN3),                        dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1, dbo.[TRANSACTION].VORODID,                        dbo.[TRANSACTION].TedadVorood, dbo.T_VorodKala.DateV, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)), dbo.HesabName(dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) HAVING      (@startDate IS NULL) AND (@AccID IS NULL) OR                       (@startDate IS NULL) AND (@AccID IS NULL) AND (dbo.[TRANSACTION].VORODID BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NULL) AND (dbo.T_VorodKala.DateV BETWEEN @startdate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@AccID IS NULL) AND (dbo.[TRANSACTION].VORODID BETWEEN @startSanadID AND @endSanadID) AND                        (dbo.T_VorodKala.DateV BETWEEN @startdate AND @enddate) OR                       (@startDate IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NULL) AND (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].VORODID BETWEEN @startSanadID AND @endSanadID) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.T_VorodKala.DateV BETWEEN @startdate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].VORODID BETWEEN @startSanadID AND @endSanadID) AND                        (dbo.T_VorodKala.DateV BETWEEN @startdate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID),                        4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID),                        4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].THESABID), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_012076]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_012076](@AccID nvarchar(50), @startDate nvarchar(50), @startSanadID nvarchar(50), @endSanadID nvarchar(50), @enddate nvarchar(50)) AS SELECT     dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].UnitPriceKh, dbo.[TRANSACTION].TedadKh, dbo.T_Kharid.Takhfif, dbo.[TRANSACTION].GOID,                        dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].KHARIDID, dbo.T_Kharid.DateKh,                        @startDate AS startDate, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.T_Kharid.THESABID), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID, dbo.[TRANSACTION].KALAID, dbo.[TRANSACTION].VKALAID,                        dbo.[TRANSACTION].FieldN6, @enddate AS enddate, dbo.HesabName(dbo.T_Kharid.KOLID, dbo.[TRANSACTION].HESABID, dbo.T_Kharid.THESABID)                        AS namehesab, ABS(dbo.[TRANSACTION].FieldN3) AS Expr1, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1,                        dbo.T_NameKala.VAHED1 FROM         dbo.T_Kharid INNER JOIN                       dbo.[TRANSACTION] ON dbo.T_Kharid.KHARIDID = dbo.[TRANSACTION].KHARIDID INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].UnitPriceKh, dbo.[TRANSACTION].TedadKh, dbo.T_Kharid.Takhfif, dbo.[TRANSACTION].GOID,                        dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].KHARIDID, dbo.T_Kharid.DateKh,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.THESABID), 4),                        1, LEN(@AccID)), dbo.[TRANSACTION].KALAID, dbo.[TRANSACTION].VKALAID, dbo.[TRANSACTION].FieldN6, dbo.HesabName(dbo.T_Kharid.KOLID,                        dbo.[TRANSACTION].HESABID, dbo.T_Kharid.THESABID), ABS(dbo.[TRANSACTION].FieldN3), dbo.T_NameKala.KalaNumber,                        dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1 HAVING      (@startDate IS NULL) AND (@AccID IS NULL) OR                       (@startDate IS NULL) AND (@AccID IS NULL) AND (dbo.[TRANSACTION].KHARIDID = @startSanadID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NULL) AND (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@AccID IS NULL) AND (dbo.[TRANSACTION].KHARIDID = @startSanadID) AND (dbo.T_Kharid.DateKh BETWEEN                        @startDate AND @enddate) OR                       (@startDate IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.THESABID), 4), 1, LEN(@AccID)) BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NULL) AND (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].KHARIDID = @startSanadID) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.THESABID), 4),                        1, LEN(@AccID)) BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.THESABID), 4),                        1, LEN(@AccID)) BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].KHARIDID = @startSanadID) AND (dbo.T_Kharid.DateKh BETWEEN                        @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.THESABID), 4), 1, LEN(@AccID)) BETWEEN @startSanadID AND @endSanadID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030318]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030318](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID int) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes,                        SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1, MAX(dbo.[TRANSACTION].Date1)                        AS MaxOfDate1, COUNT(dbo.[TRANSACTION].PARBes) AS CountOfPARBes, COUNT(dbo.[TRANSACTION].DARBed) AS CountOfDARBed,                        @enddate AS enddate, @startDate AS startdate, dbo.T_Heasb.NameHesab AS namehesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Heasb ON dbo.[TRANSACTION].HESABID = dbo.T_Heasb.HESABID WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].HESABID), 4), 1, LEN(@AccID)), dbo.T_Heasb.NameHesab HAVING      (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].HESABID),                        4), 1, LEN(@AccID)) = @AccID) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].HESABID IS NOT NULL)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030317]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030317](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID int) AS SELECT     dbo.[TRANSACTION].GOID, SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed,                        MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1, MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, COUNT(dbo.[TRANSACTION].PARBes)                        AS CountOfPARBes, @enddate AS enddate, @startDate AS startdate, dbo.T_GropHesab.NameGo AS namehesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (dbo.[TRANSACTION].SAID2Number BETWEEN @startSanadID AND @endSanadID) GROUP BY dbo.[TRANSACTION].GOID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)),                        dbo.T_GropHesab.NameGo HAVING      (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].GOID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].GOID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (@startDate IS NULL) AND (dbo.[TRANSACTION].GOID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (NOT (@startDate IS NULL)) AND (dbo.[TRANSACTION].GOID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) = @AccID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030316]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030316](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID int) AS SELECT     GOID, KOLID, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1,                        COUNT(PARBes) AS CountOfPARBes, @enddate AS enddate, @startDate AS startdate, dbo.HesabName(KOLID, NULL, NULL) AS namehesab FROM         dbo.[TRANSACTION] WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (NOT (@startSanadID IS NULL)) AND (SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startSanadID AND                        @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (NOT (@startSanadID IS NULL)) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startSanadID AND                        @endSanadID) GROUP BY GOID, KOLID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID)),                        dbo.HesabName(KOLID, NULL, NULL) HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND (KOLID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (NOT (@startDate IS NULL)) AND                        (KOLID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND (KOLID IS NOT NULL)                        AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID))                        = @AccID) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (NOT (@startDate IS NULL)) AND                        (KOLID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND (KOLID IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (NOT (@startDate IS NULL)) AND                        (KOLID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND (KOLID IS NOT NULL)                        AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID))                        = @AccID) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (NOT (@startDate IS NULL)) AND                        (KOLID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1,                        LEN(@AccID)) = @AccID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030315]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030315](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID int) AS SELECT     GOID, KOLID, HESABID, THESABID, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, MIN(Date1) AS MinOfDate1, MAX(Date1)                        AS MaxOfDate1, COUNT(PARBes) AS CountOfPARBes, @enddate AS enddate, @startDate AS startdate, dbo.HesabName(KOLID, HESABID, THESABID)                        AS namehesab FROM         dbo.[TRANSACTION] WHERE     (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) OR                       (@startSanadID IS NULL) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@startSanadID IS NOT NULL) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startSanadID AND                        @endSanadID) OR                       (@startSanadID IS NOT NULL) AND (SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@startSanadID IS NOT NULL) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startSanadID AND @endSanadID) GROUP BY GOID, KOLID, HESABID, THESABID, dbo.HesabName(KOLID, HESABID, THESABID) HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND (dbo.HesabName(KOLID,                        HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NOT NULL) AND                        (dbo.HesabName(KOLID, HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.HesabName(KOLID, HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.HesabName(KOLID, HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND (dbo.HesabName(KOLID,                        HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NOT NULL) AND                        (dbo.HesabName(KOLID, HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.HesabName(KOLID, HESABID, THESABID) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@startDate IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.HesabName(KOLID, HESABID, THESABID) IS NOT NULL)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_03029752]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_03029752](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed,                        SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1,                        MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1, MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate,                        CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed,                        CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes,                        dbo.T_KolHesab.NameHesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID INNER JOIN                       dbo.T_Heasb ON dbo.[TRANSACTION].HESABID = dbo.T_Heasb.HESABID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.T_KolHesab.NameHesab HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_03029751]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_03029751](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes, dbo.T_KolHesab.NameHesab,                        dbo.T_GropHesab.NameGo FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab, dbo.T_GropHesab.NameGo HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030296]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030296](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes, dbo.T_KolHesab.NameHesab,                        dbo.T_GropHesab.NameGo FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab, dbo.T_GropHesab.NameGo HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030295]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030295](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes, dbo.T_KolHesab.NameHesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030294]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030294](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, dbo.T_KolHesab.NameHesab,                        dbo.T_GropHesab.NameGo FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab, dbo.T_GropHesab.NameGo HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030293]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030293](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, dbo.T_KolHesab.NameHesab,                        dbo.T_KolHesab.KolNumber FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab, dbo.T_KolHesab.KolNumber HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030259]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030259](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endsanadID int) AS SELECT     dbo.[TRANSACTION].GOID, SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) AS hesabidt, SUM(dbo.[TRANSACTION].DARBed)                        - SUM(dbo.[TRANSACTION].PARBes) AS mande, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1, MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1,                        @startDate AS startDate, @enddate AS enddate, @AccID AS AccID, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed)                        - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS bed, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed)                        - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS bes FROM         dbo.[TRANSACTION] LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) GROUP BY dbo.[TRANSACTION].GOID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) HAVING      (@AccID IS NULL) AND (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].GOID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) = @AccID) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030827]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030827](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endsanadID int) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed,                        SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt, @startDate AS startDate, @enddate AS enddate, CONVERT(money,                        SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes)) AS mande, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @AccID AS AccID, @startSanadID AS startSanadID, dbo.T_Heasb.HESABNumber,                        dbo.T_Heasb.NameHesab, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed)                        - SUM(PARBes)) ELSE 0 END AS bed, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed)                        - SUM(PARBes)) ELSE 0 END AS bes FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Heasb ON dbo.[TRANSACTION].HESABID = dbo.T_Heasb.HESABID LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4), 1, LEN(@AccID)), dbo.[TRANSACTION].HESABID, dbo.T_Heasb.HESABNumber,                        dbo.T_Heasb.NameHesab HAVING      (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) ORDER BY dbo.[TRANSACTION].HESABID  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030826]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030826](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endsanadID int) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4), 1, LEN(@AccID)) AS hesabidt, @startDate AS startDate, @enddate AS enddate, CONVERT(money,                        SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes)) AS mande, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @AccID AS AccID, @startSanadID AS startSanadID, dbo.T_KolHesab.KolNumber,                        dbo.T_KolHesab.NameHesab, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed)                        - SUM(PARBes)) ELSE 0 END AS bed, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed)                        - SUM(PARBes)) ELSE 0 END AS bes FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4), 1, LEN(@AccID)), dbo.T_KolHesab.KolNumber,                        dbo.T_KolHesab.NameHesab HAVING      (@AccID IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4), 1, LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030825]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030825](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endsanadID int) AS SELECT     dbo.[TRANSACTION].GOID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1, LEN(@AccID)) AS hesabidt, @AccID AS AccID,                        @startDate AS startDate, @enddate AS enddate, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS mande,                        MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1, MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startSanadID AS startSanadID,                        dbo.T_GropHesab.GoNumber, dbo.T_GropHesab.NameGo, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes))))                        > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS bed, CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes))))                        < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS bes FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NULL) AND (@startDate IS NULL) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endsanadID) AND                        (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) GROUP BY dbo.[TRANSACTION].GOID, dbo.T_GropHesab.GoNumber, dbo.T_GropHesab.NameGo HAVING      (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (dbo.[TRANSACTION].GOID IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4), 1,                        LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_03062424]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_03062424](@saID int) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID,                        dbo.[TRANSACTION].DARBed AS SumOfDARBed, dbo.[TRANSACTION].PARBes AS SumOfPARBes, dbo.[TRANSACTION].TextShSA,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab,                        dbo.[TRANSACTION].Date1, dbo.[TRANSACTION].SAID2Number, dbo.[TRANSACTION].Sharh, CASE WHEN [TRANSACTION].Sharh IS NULL                        THEN TextSHSa ELSE [TRANSACTION].Sharh END AS SharhSanad, dbo.T_SA2.Sharh AS Expr2 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (dbo.[TRANSACTION].SAID2Number = @saID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030624]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_030624](@startDate nvarchar(50), @enddate nvarchar(50), @startSanadID nvarchar(50), @endSanadID nvarchar(50), @AccID nvarchar(50)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID AS Expr1, dbo.[TRANSACTION].THESABID,                        dbo.[TRANSACTION].DARBed AS SumOfDARBed, dbo.[TRANSACTION].PARBes AS SumOfPARBes, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) AS Hesabid, @startDate AS startDate, @enddate AS enddate, dbo.[TRANSACTION].TextShSA,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab,                        dbo.[TRANSACTION].Date1, dbo.[TRANSACTION].Date1 AS MinOfDate1, dbo.[TRANSACTION].Date1 AS MaxOfDate1, @AccID AS AccID,                        @startSanadID AS startSanadID, dbo.T_SA2.SAID2Number, dbo.[TRANSACTION].Sharh, CASE WHEN [TRANSACTION].Sharh IS NULL                        THEN TextSHSa ELSE [TRANSACTION].Sharh END AS SharhSanad, dbo.T_SA2.Sharh AS Expr2, dbo.[TRANSACTION].SAID2Number AS Expr3 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND (@startSanadID IS NULL)                        AND (@startDate IS NULL) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND (@startSanadID IS NULL)                        AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NULL) AND (@startDate IS NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_030623]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_030623](@startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID int, @AccID nvarchar(50)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID AS Expr1, dbo.[TRANSACTION].THESABID,                        dbo.[TRANSACTION].DARBed AS SumOfDARBed, dbo.[TRANSACTION].PARBes AS SumOfPARBes, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) AS Hesabid, @startDate AS startDate, @enddate AS enddate, dbo.[TRANSACTION].TextShSA,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab,                        dbo.[TRANSACTION].Date1, dbo.[TRANSACTION].Date1 AS MinOfDate1, dbo.[TRANSACTION].Date1 AS MaxOfDate1, @AccID AS AccID,                        @startSanadID AS startSanadID, dbo.T_SA2.SAID2Number, dbo.[TRANSACTION].Sharh, CASE WHEN [TRANSACTION].Sharh IS NULL                        THEN TextSHSa ELSE [TRANSACTION].Sharh END AS SharhSanad, dbo.T_SA2.Sharh AS Expr2 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND (@startSanadID IS NULL)                        AND (@startDate IS NULL) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND (@startSanadID IS NULL)                        AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NULL) AND (@startDate IS NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID) OR                       (@AccID IS NOT NULL) AND (dbo.[TRANSACTION].DARBed IS NOT NULL) AND (dbo.[TRANSACTION].PARBes IS NOT NULL) AND                        (@startSanadID IS NOT NULL) AND (@startDate IS NOT NULL) AND (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.T_SA2.SAID2Number BETWEEN @startSanadID AND @endSanadID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListGoroupHesab_sood]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QListGoroupHesab_sood] AS SELECT     dbo.T_GropHesab.GoID, dbo.T_GropHesab.NameGo, RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_GropHesab.GoID), 4) AS Expr1,                        ABS(SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes)) AS Expr4, CASE WHEN (ISNULL(SUM(DARBed) - SUM(PARBes), 0))                        > 0 THEN N''بد'' WHEN (ISNULL(SUM(DARBed) - SUM(PARBes), 0)) < 0 THEN N''بس'' ELSE ''--'' END AS Expr5 FROM         dbo.T_GropHesab INNER JOIN                       dbo.[TRANSACTION] ON dbo.T_GropHesab.GoID = dbo.[TRANSACTION].GOID GROUP BY dbo.T_GropHesab.GoID, dbo.T_GropHesab.NameGo, RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_GropHesab.GoID), 4) HAVING      (dbo.T_GropHesab.GoID = 8) OR                       (dbo.T_GropHesab.GoID = 9) ORDER BY dbo.T_GropHesab.NameGo  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListGoroupHesab_SanadHesabdari]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

CREATE PROCEDURE [QListGoroupHesab_SanadHesabdari]  AS 



SELECT     T_GropHesab.GoID AS ACCID, T_GropHesab.GoID, T_GropHesab.GoNumber AS CodeACC, T_GropHesab.NameGo AS NameACC, T_GropHesab.GoNumber AS [كد حساب], 
                      T_GropHesab.NameGo AS [عنوان حساب], QMande_GoHesab_SanadHesabdary.DARBed - QMande_GoHesab_SanadHesabdary.PARBes AS مانده, CASE WHEN (ISNULL((darbed - parbes), 0)) 
                      > 0 THEN N''بد'' WHEN (ISNULL((darbed - parbes), 0)) < 0 THEN N''بس'' ELSE ''--'' END AS [تشخيص ]
FROM         T_GropHesab LEFT OUTER JOIN
                          (SELECT     SUM(PARBes) AS PARBes, SUM(DARBed) AS DARBed, GOID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY GOID, GOID) AS QMande_GoHesab_SanadHesabdary ON T_GropHesab.GoID = QMande_GoHesab_SanadHesabdary.GOID
ORDER BY NameACC

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListGoroupHesab]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [QListGoroupHesab]
AS SELECT  dbo.T_GropHesab.GoID, dbo.T_GropHesab.NameGo, ABS(SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes)) AS Expr4, CASE WHEN (ISNULL(SUM(DARBed) - SUM(PARBes), 0)) 
               > 0 THEN N''بد'' WHEN (ISNULL(SUM(DARBed) - SUM(PARBes), 0)) < 0 THEN N''بس'' ELSE ''--'' END AS Expr5, RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_GropHesab.GoID), 4) AS Expr1
FROM     dbo.T_GropHesab INNER JOIN
               dbo.[TRANSACTION] ON dbo.T_GropHesab.GoID = dbo.[TRANSACTION].GOID
GROUP BY dbo.T_GropHesab.GoID, dbo.T_GropHesab.NameGo, RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_GropHesab.GoID), 4)
ORDER BY dbo.T_GropHesab.NameGo' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Sandogh_Hazine_Bes]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_Sandogh_Hazine_Bes](@dateFo nvarchar(50), @TextShSA nvarchar(255), @SAID2 int, @SAID2Number int, @Dateeng datetime, @SAIDType int) AS INSERT INTO dbo.[TRANSACTION]                       (KOLID, HESABID, THESABID, GOID, PARBes, TextShSA, Date1, SAID2, SAID2Number, Dateeng, SAIDType) SELECT     dbo.Table4.KOlID, dbo.Table4.HESABID, dbo.Table4.THESABID, dbo.T_KolHesab.GOID, SUM(dbo.Table4.MabHazine) AS MabHazine,                        @TextShSA AS Expr1, @dateFo AS Expr2, @SAID2 AS Expr3, @SAID2Number AS Expr4, @Dateeng AS Expr5, @SAIDType AS Expr6 FROM         dbo.Table4 LEFT OUTER JOIN                       dbo.T_KolHesab ON dbo.Table4.KOlID = dbo.T_KolHesab.KOLID WHERE     (dbo.Table4.FactID IN                           (SELECT     dbo.T_Forosh.FOROSHID                              FROM         dbo.T_Forosh LEFT OUTER JOIN                                                    dbo.T_SA2 ON dbo.T_Forosh.PrimarySaid2 = dbo.T_SA2.SAID2                              WHERE     (dbo.T_SA2.FieldN2 = 26) OR                                                    (dbo.T_SA2.FieldN2 IS NULL)                              GROUP BY dbo.T_Forosh.FOROSHID, dbo.T_Forosh.DateFo, dbo.T_Forosh.Flag1, dbo.T_Forosh.KOLID                              HAVING      (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) AND (dbo.T_Forosh.KOLID IS NULL) OR                                                    (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) AND (dbo.T_Forosh.KOLID IS NULL))) AND (dbo.Table4.TypeFact = 2) GROUP BY dbo.Table4.KOlID, dbo.Table4.HESABID, dbo.Table4.THESABID, dbo.T_KolHesab.GOID  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Sandogh_Hazine_Bed]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_Sandogh_Hazine_Bed](@dateFo nvarchar(50), @TextShSA nvarchar(255), @SAID2 int, @SAID2Number int, @Dateeng datetime, @SAIDType int) AS INSERT INTO dbo.[TRANSACTION]                       (KOLID, HESABID, THESABID, GOID, DARBed, TextShSA, Date1, SAID2, SAID2Number, Dateeng, SAIDType) SELECT     dbo.Table4.KOlID1, dbo.Table4.HESABID1, dbo.Table4.THESABID1, dbo.T_KolHesab.GOID, SUM(dbo.Table4.MabHazine) AS MabHazine,                        @TextShSA AS Expr1, @dateFo AS Expr2, @SAID2 AS Expr3, @SAID2Number AS Expr4, @Dateeng AS Expr5, @SAIDType AS Expr6 FROM         dbo.Table4 LEFT OUTER JOIN                       dbo.T_KolHesab ON dbo.Table4.KOlID1 = dbo.T_KolHesab.KOLID WHERE     (dbo.Table4.FactID IN                           (SELECT     dbo.T_Forosh.FOROSHID                              FROM         dbo.T_Forosh LEFT OUTER JOIN                                                    dbo.T_SA2 ON dbo.T_Forosh.PrimarySaid2 = dbo.T_SA2.SAID2                              WHERE     (dbo.T_SA2.FieldN2 = 26) OR                                                    (dbo.T_SA2.FieldN2 IS NULL)                              GROUP BY dbo.T_Forosh.FOROSHID, dbo.T_Forosh.DateFo, dbo.T_Forosh.Flag1, dbo.T_Forosh.KOLID                              HAVING      (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) AND (dbo.T_Forosh.KOLID IS NULL) OR                                                    (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) AND (dbo.T_Forosh.KOLID IS NULL))) AND (dbo.Table4.TypeFact = 2) GROUP BY dbo.Table4.KOlID1, dbo.Table4.HESABID1, dbo.Table4.THESABID1, dbo.T_KolHesab.GOID  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_SabtSanadNaghdiSandogh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_SabtSanadNaghdiSandogh](@dateFo nvarchar(50)) AS SELECT     dbo.T_Forosh.FOROSHID, dbo.T_Forosh.DateFo, dbo.T_Forosh.Flag1, dbo.[TRANSACTION].FieldN2, SUM(CONVERT(money,                        dbo.[TRANSACTION].TedadFo * dbo.[TRANSACTION].UnitPriceFo)) AS sumation, CONVERT(nvarchar(200), dbo.T_Forosh.Sharh) AS sharh,                        dbo.T_Forosh.Takhfif, F_MabHazine_Sandogh.mabhazine AS mabhazine, dbo.T_Forosh.NumberKH, dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID,                        dbo.T_Forosh.THESABID, dbo.T_SA2.SAID2, dbo.HesabName(dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID) AS NameHesab,                        dbo.T_Forosh.ForoshNumbr FROM         dbo.T_Forosh INNER JOIN                       dbo.[TRANSACTION] ON dbo.T_Forosh.FOROSHID = dbo.[TRANSACTION].FOROSHID LEFT OUTER JOIN                       dbo.F_MabHazine_Sandogh() F_MabHazine_Sandogh ON dbo.T_Forosh.FOROSHID = F_MabHazine_Sandogh.Factid LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Forosh.PrimarySaid2 = dbo.T_SA2.SAID2 WHERE     (dbo.T_SA2.FieldN2 = 26) OR                       (dbo.T_SA2.FieldN2 IS NULL) GROUP BY dbo.T_Forosh.FOROSHID, dbo.T_Forosh.DateFo, dbo.[TRANSACTION].FieldN2, dbo.T_Forosh.Flag1, CONVERT(nvarchar(200), dbo.T_Forosh.Sharh),                        dbo.T_Forosh.Takhfif, F_MabHazine_Sandogh.mabhazine, dbo.T_Forosh.NumberKH, dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID,                        dbo.T_Forosh.THESABID, dbo.T_SA2.SAID2, dbo.HesabName(dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID),                        dbo.T_Forosh.ForoshNumbr HAVING      (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) AND (dbo.T_Forosh.KOLID IS NULL) OR                       (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) AND (dbo.T_Forosh.KOLID IS NULL) ORDER BY dbo.T_Forosh.DateFo, dbo.T_Forosh.FOROSHID  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_SabtSanadFactSandogh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_SabtSanadFactSandogh](@dateFo nvarchar(50)) AS SELECT     dbo.T_Forosh.FOROSHID, dbo.T_Forosh.DateFo, dbo.T_Forosh.Flag1, dbo.[TRANSACTION].FieldN2, SUM(CONVERT(money,                        dbo.[TRANSACTION].TedadFo * dbo.[TRANSACTION].UnitPriceFo)) AS sumation, CONVERT(nvarchar(50), dbo.T_Forosh.Sharh) AS sharh,                        dbo.T_Forosh.Takhfif, F_MabHazine_Sandogh.mabhazine AS mabhazine, SUM(CONVERT(money, CASE WHEN dbo.T_Forosh.KOLID IS NULL                        THEN (dbo.[TRANSACTION].TedadFo * dbo.[TRANSACTION].UnitPriceFo) ELSE 0 END)) AS MabSaleSandogh, dbo.T_Forosh.NumberKH,                        dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID, dbo.T_SA2.SAID2, dbo.HesabName(dbo.T_Forosh.KOLID,                        dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID) AS NameHesab, dbo.T_Forosh.ForoshNumbr FROM         dbo.T_Forosh INNER JOIN                       dbo.[TRANSACTION] ON dbo.T_Forosh.FOROSHID = dbo.[TRANSACTION].FOROSHID LEFT OUTER JOIN                       dbo.F_MabHazine_Sandogh() F_MabHazine_Sandogh ON dbo.T_Forosh.FOROSHID = F_MabHazine_Sandogh.Factid LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Forosh.PrimarySaid2 = dbo.T_SA2.SAID2 WHERE     (dbo.T_SA2.FieldN2 = 26) OR                       (dbo.T_SA2.FieldN2 IS NULL) GROUP BY dbo.T_Forosh.FOROSHID, dbo.T_Forosh.DateFo, dbo.[TRANSACTION].FieldN2, dbo.T_Forosh.Flag1, CONVERT(nvarchar(50), dbo.T_Forosh.Sharh),                        dbo.T_Forosh.Takhfif, F_MabHazine_Sandogh.mabhazine, dbo.T_Forosh.NumberKH, dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID,                        dbo.T_Forosh.THESABID, dbo.T_SA2.SAID2, dbo.HesabName(dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID),                        dbo.T_Forosh.ForoshNumbr HAVING      (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) AND (dbo.T_Forosh.KOLID IS NOT NULL) OR                       (dbo.T_Forosh.Flag1 = 0) AND (dbo.T_Forosh.DateFo = @dateFo) AND (dbo.T_Forosh.KOLID IS NOT NULL) ORDER BY dbo.T_Forosh.DateFo, dbo.T_Forosh.FOROSHID  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[Q_PreviwSanadHEsabdary]'))
EXEC dbo.sp_executesql @statement = N'        CREATE VIEW [Q_PreviwSanadHEsabdary] AS SELECT     TOP 100 PERCENT dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID AS Expr1, dbo.[TRANSACTION].THESABID,                        dbo.[TRANSACTION].DARBed, dbo.[TRANSACTION].PARBes, dbo.[TRANSACTION].TextShSA, dbo.HesabName(dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab, dbo.[TRANSACTION].Date1, dbo.[TRANSACTION].Date1 AS MinOfDate1,                        dbo.[TRANSACTION].Date1 AS MaxOfDate1, dbo.T_SA2.SAID2Number, dbo.[TRANSACTION].Sharh, CASE WHEN [TRANSACTION].Sharh IS NULL                        THEN TextSHSa ELSE [TRANSACTION].Sharh END AS SharhSanad, dbo.[TRANSACTION].radif, dbo.[TRANSACTION].SAID2, CONVERT(Nvarchar(20),                        dbo.[TRANSACTION].GOID) + N''.'' + CONVERT(Nvarchar(20), dbo.[TRANSACTION].KOLID) + N''.'' + CONVERT(Nvarchar(20),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)) + N''.'' + CONVERT(Nvarchar(20), ISNULL(dbo.[TRANSACTION].THESABID, 0)) AS codeh,                        dbo.[TRANSACTION].RadifSAID2 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 ORDER BY dbo.[TRANSACTION].RadifSAID2  '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_TRANSACTION_Kharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [Q_TRANSACTION_Kharid]() RETURNS TABLE AS RETURN ( SELECT     HESABID AS Expr1, THESABID AS Expr2, CONVERT(nvarchar(50), Date1) AS Date1, KOLID, HESABID, THESABID, TRAID, SAID2, KALAID, NAMEKHID, AID, VKALAID,                        SAID, KHOROJID, VORODID, FOROSHID, KHARIDID, DKHID, HKHID, TAFOID, TAKHID, PARID1, PARID2, PARID3, PARID4, PARID7, DARID1, DARID2, DARID3, DARID4,                        DARID5, DARID6, DARID7, DARID8, ID1, ID2, OperationCode, SAID2Number, VSabt, DateCreate, DateModified, RadifSAID2, TextNHSID2, TextShSA, GOID, THESABID2,                        MOINID, GAID, TedadVA, TedadKhA, ModelKala, TextNumberSA, GOKID, PARBes, DARBed, [TEMP], TEMPR, TempMandeh, Sharh, radif, Dateeng, BANKID, TextAIDKh,                        TextNumberKHO, TedadKhorooj, TextAID, TextNumberVO, TedadList, TedadVorood, SHomarePArvaneh, SHomarePAking, UnitPriceVo, UnitPriceKho,                        TextAIDFo, TextNumberFO, TedadFo, UnitPriceFo, TextAIDKharid, TextNumberKH, TedadKh, UnitPriceKh, BARFOROSHID, TedadBarFo, UnitPriceBarFo, BARKHARIDID,                        TedadBarKh, UnitPriceBarKh, TedadDKh, UnitPriceDKh, TedadHKh, UnitPriceHKh, IDSHARH, PARID5, PARID6, PARID8, ID3, ID4, ID14, ID15, FieldN1, FieldN2, FieldN3,                        FieldN4, FieldN5, FieldN6, FieldN7, FieldT1, FieldT2, FieldT3, FieldT4, FieldT5, FieldT6, FieldT7, FieldT8, SAIDType, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7,                        Flag8, Flag9, Flag10, Maliatkala, Avarez, CurRef, CurRate, PARBesCur, DARBedCur, KOLID AS exkolid FROM         dbo.[TRANSACTION] WHERE     (HESABID IN                           (SELECT     HESABID                              FROM         dbo.T_Kharid)) OR                       (THESABID IN                           (SELECT     THESABID                              FROM         dbo.T_Kharid)) OR                       (KOLID IN                           (SELECT     KolID                              FROM         dbo.T_Kharid)) ) ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_TRANSACTION_Foroosh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [Q_TRANSACTION_Foroosh]() RETURNS TABLE AS RETURN ( SELECT     HESABID AS Expr1, THESABID AS Expr2, CONVERT(nvarchar(50), Date1) AS Date1, KOLID, THESABID, HESABID, SAID2, TRAID, KALAID, NAMEKHID, AID, VKALAID,                        SAID, FOROSHID, KHARIDID, DKHID, HKHID, TAKHID, PARID1, PARID2, PARID3, PARID4, PARID7, DARID1, DARID2, DARID3, DARID4, DARID5, DARID6, DARID7,                        DARID8, ID1, ID2, OperationCode, SAID2Number, VSabt, DateCreate, DateModified, RadifSAID2, TextNHSID2, TextShSA, GOID, THESABID2, MOINID, GAID, TedadVA,                        TedadKhA, ModelKala, TextNumberSA, GOKID, PARBes, DARBed, [TEMP], TEMPR, TempMandeh, Sharh, radif, Dateeng, BANKID, TextAIDKh, TextNumberKHO,                        TedadKhorooj, TextAID, TextNumberVO, TedadList, TedadVorood, SHomarePArvaneh, SHomarePAking, UnitPriceVo, UnitPriceKho, TextAIDFo,                        TextNumberFO, TedadFo, UnitPriceFo, TextAIDKharid, TextNumberKH, TedadKh, UnitPriceKh, BARFOROSHID, TedadBarFo, UnitPriceBarFo, BARKHARIDID,                        TedadBarKh, UnitPriceBarKh, TedadDKh, UnitPriceDKh, TedadHKh, UnitPriceHKh, IDSHARH, PARID5, PARID6, PARID8, ID3, ID4, ID14, ID15, FieldN1, FieldN2, FieldN3,                        FieldN4, FieldN5, FieldN6, FieldN7, FieldT1, FieldT2, FieldT3, FieldT4, FieldT5, FieldT6, FieldT7, FieldT8, SAIDType, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7,                        Flag8, Flag9, Flag10, Maliatkala, Avarez, CurRef, CurRate, PARBesCur, DARBedCur, KOLID AS exprkol FROM         dbo.[TRANSACTION] WHERE     (HESABID IN                           (SELECT     HESABID                              FROM         dbo.T_Forosh)) OR                       (THESABID IN                           (SELECT     THESABID                              FROM         dbo.T_Forosh)) OR                       (KOLID IN                           (SELECT     kolID                              FROM         dbo.T_Forosh)) ) ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_TRANSACTION]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'       CREATE FUNCTION [Q_TRANSACTION](@AccID nvarchar(50)) RETURNS TABLE AS RETURN ( SELECT     dbo.[TRANSACTION].*, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS Expr1 FROM         dbo.[TRANSACTION] )    ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_SummryOfSandogh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [Q_SummryOfSandogh]() RETURNS TABLE AS RETURN ( SELECT     CONVERT(money, dbo.[TRANSACTION].TedadFo * dbo.[TRANSACTION].UnitPriceFo) AS Mablagh, dbo.T_Forosh.KOLID, dbo.T_Forosh.Takhfif,                        dbo.T_Forosh.UserID FROM         dbo.T_Forosh INNER JOIN                       dbo.[TRANSACTION] ON dbo.T_Forosh.FOROSHID = dbo.[TRANSACTION].FOROSHID WHERE     (dbo.T_Forosh.Flag1 = 0) )   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditTafsili_Tar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditTafsili_Tar](@GOID int, @Kolid int, @Hesabid int, @Thesabid int) AS UPDATE    dbo.[TRANSACTION] SET              GOID = @GOID, KOLID = @Kolid, HESABID = @Hesabid WHERE     (THESABID = @Thesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_Bed_Bes]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'       CREATE FUNCTION [Q_Bed_Bes](@startdate nvarchar(50), @enddate nvarchar(50)) RETURNS TABLE AS RETURN ( SELECT     TOP 100 PERCENT GOID, KOLID, HESABID, THESABID, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed)                        - SUM(PARBes) AS mande, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1 FROM         dbo.[TRANSACTION] WHERE     (Date1 BETWEEN @startdate AND @enddate) AND (@startdate IS NOT NULL) OR                       (@startdate IS NULL) GROUP BY GOID, KOLID, HESABID, THESABID ORDER BY KOLID )    ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_MandePrintSandogh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_MandePrintSandogh](@ACCID nvarchar(50), @PrimarySaid2 int) AS SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), THESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), THESABID), 4), 1, LEN(@ACCID)) AS Expr1,                        CONVERT(money, SUM(DARBed) - SUM(PARBes)) AS Mande, SUM(PARBes) AS sumbes, SUM(DARBed) AS sumbed FROM         dbo.[TRANSACTION] WHERE     (SAID2 <> @PrimarySaid2) GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), THESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), THESABID), 4), 1, LEN(@ACCID)) HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_MandeHRial]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'     CREATE FUNCTION [Q_MandeHRial]() RETURNS TABLE AS RETURN ( SELECT     ROUND(SUM(dbo.[TRANSACTION].UnitPriceKh) + ISNULL(dbo.M_EbtedaDore.MojodiRiali, 0), 2) AS MablaghMiangin, dbo.[TRANSACTION].KALAID,                        COUNT(dbo.[TRANSACTION].KHARIDID) AS expr, dbo.M_EbtedaDore.MojodiRiali, dbo.M_EbtedaDore.Mojodi, CASE WHEN (isnull(Mojodi, 0) > 0)                        THEN COUNT(dbo.[TRANSACTION].kharidid) + 1 ELSE COUNT(dbo.[TRANSACTION].kharidid) END AS CountKharid FROM         dbo.[TRANSACTION] LEFT OUTER JOIN                       dbo.M_EbtedaDore ON dbo.[TRANSACTION].KALAID = dbo.M_EbtedaDore.NameKalaID WHERE     (NOT (dbo.[TRANSACTION].KHARIDID IS NULL)) GROUP BY dbo.[TRANSACTION].KALAID, dbo.M_EbtedaDore.MojodiRiali, dbo.M_EbtedaDore.Mojodi )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_MandeHesabTafsili]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_MandeHesabTafsili](@startdate nvarchar(50)) AS SELECT     THESABID, CONVERT(money, SUM(DARBed) - SUM(PARBes)) AS Mande, SUM(PARBes) AS sumbes, SUM(DARBed) AS sumbed FROM         dbo.[TRANSACTION] WHERE     (SAID2 IS NOT NULL) AND (Date1 < @startdate) GROUP BY THESABID HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) ' 
END
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_MandeHesabInFactor]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_MandeHesabInFactor](@PSAID2 int) AS SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, 12) AS hesabidt, SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes)                        AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed FROM         dbo.[TRANSACTION] WHERE     (SAID2 <> @PSAID2) GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, 12) HAVING      (NOT (SUM(DARBed) - SUM(PARBes) IS NULL)) ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_MandeHesabGo]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_MandeHesabGo](@startdate nvarchar(50)) AS SELECT     GOID, CONVERT(money, SUM(DARBed) - SUM(PARBes)) AS Mande, SUM(PARBes) AS sumbes, SUM(DARBed) AS sumbed FROM         dbo.[TRANSACTION] WHERE     (SAID2 IS NOT NULL) AND (Date1 < @startdate) GROUP BY GOID HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListEditGroup_Append]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListEditGroup_Append](@AID nvarchar(50), @GOKID nvarchar(50), @KALAID nvarchar(50), @Userid nvarchar(50)) AS INSERT INTO dbo.TRANSACTION2                       (ID2, FieldT1, FieldT4, FieldT3, FieldT2, FieldT5, ID1) SELECT     KALAID, NameKala1, NameAnbar, NameGoKala1, BarCode, KalaNumber, @UserID AS Expr1 FROM         dbo.F_Append_EditGroup(@AID, @GOKID, @KALAID, @Userid) F_Append_EditGroup  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_SanadHesabdari_ChekSaghf_Bed_Bes]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'


CREATE  PROCEDURE [QSubPFrm_SanadHesabdari_ChekSaghf_Bed_Bes]
		@SAID2 INT,
		@SAID2_1 INT,
		@THESABID INT,
		@LinkSubID NVARCHAR(255),
        @SUMBedFact DECIMAL(19,4) ,
        @SUMBesFact DECIMAL(19,4) ,
        @MabAddBed DECIMAL(19,4) =0 OUTPUT,
        @MabAddBes DECIMAL(19,4) =0 OUTPUT
        
AS
BEGIN

DECLARE @SaghfBed  DECIMAL(19,4);
DECLARE @SaghfBes  DECIMAL(19,4);
DECLARE @DARBedGh DECIMAL(19,4);
DECLARE @PARBesGh DECIMAL(19,4) ;
DECLARE @DARBedTemp DECIMAL(19,4);
DECLARE @PARBesTemp DECIMAL(19,4);

set @DARBedGh=0
SET @PARBesGh=0
set @DARBedTemp=0
set @PARBesTemp=0

if (@SAID2 IS NULL)
	set @SAID2=-1
if (@LinkSubID IS NULL)
	SET @LinkSubID=-1
	
			
		SELECT     @SaghfBed=SaghfBed, @SaghfBes=SaghfBes
		FROM         T_T_Heasb
		WHERE     (THESABID = @THESABID)
		

	IF (@SaghfBed>0 OR @SaghfBes>0)
		BEGIN
			SELECT     @PARBesGh=SUM(PARBes)  , @DARBedGh=SUM(DARBed) 
			FROM         [TRANSACTION]
			WHERE     (NOT (SAID2 IS NULL)) AND (THESABID = @THESABID) AND (NOT(SAID2 IN (@SAID2,@SAID2_1)))

				
			SELECT     @PARBesTemp=isnull(SUM(PARBes),0) , @DARBedTemp=isnull(SUM(DARBed ),0)
			FROM         TRANSACTION2
			WHERE     ((THESABID = @THESABID) AND (Date1 = @LinkSubID) AND (KALAID IS NULL))


				
			/* فروش */
			SELECT     @PARBesTemp=@PARBesTemp+isnull(SUM(DARBed),0) 
			FROM         TRANSACTION2
			WHERE     ( (TextAIDFo = @LinkSubID) AND (FieldN6 =2) AND (THESABID=@THESABID))
			
			SELECT      @DARBedTemp=@DARBedTemp+isnull(SUM(DARBed ),0)
			FROM         TRANSACTION2
			WHERE      ( (TextAIDFo = @LinkSubID) AND (FieldN6 =2) AND (FieldN4=@THESABID))
			
			
			/* خريد */
			SELECT     @PARBesTemp=@PARBesTemp+isnull(SUM(DARBed),0) 
			FROM         TRANSACTION2
			WHERE     ( (TextAIDFo = @LinkSubID) AND (FieldN6 =1) AND (FieldN4=@THESABID))
			
			SELECT      @DARBedTemp=@DARBedTemp+isnull(SUM(DARBed ),0)
			FROM         TRANSACTION2
			WHERE      ( (TextAIDFo = @LinkSubID) AND (FieldN6 =1) AND (THESABID=@THESABID))
			
			
			SET @DARBedTemp=isnull(@DARBedTemp,0)+isnull(@DARBedGh,0)+ISNULL(@SUMBedFact,0)

			SET @PARBesTemp=isnull(@PARBesTemp,0)+isnull(@PARBesGh,0)+ISNULL(@SUMBesFact,0)


			if (@DARBedTemp-@PARBesTemp>0) 
				BEGIN	
					set @DARBedTemp=@DARBedTemp-@PARBesTemp
					set @PARBesTemp=0
				END		
			ELSE
				BEGIN	
					set @PARBesTemp=@PARBesTemp-@DARBedTemp
					set @DARBedTemp=0
				END		
			
			IF (@SaghfBed>0)	
				set @MabAddBed=@SaghfBed-@DARBedTemp
			ELSE 
				set @MabAddBed=0		
						
			IF (@SaghfBes>0)
				set @MabAddBes=@SaghfBes-@PARBesTemp	
			ELSE
				set @MabAddBes=0
		End	
End

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqPassPar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

        CREATE PROCEDURE [QSubPFrm_ListCheqPassPar] AS 
        DECLARE @FieldN1_Vas int=1 ;
    
SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN ''پاس شده'' WHEN (T_Asnad.[FieldN1]) = 2 THEN ''باطل شده '' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN ''نقدی '' WHEN (T_Asnad.[FieldN1]) = 4 THEN ''پاس نشده '' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN ''پاس شده'' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN ''باطل شده '' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN ''نقدی '' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN ''پاس نشده '' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
                                              T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR
    

' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqNoPassPar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'


CREATE PROCEDURE [QSubPFrm_ListCheqNoPassPar]
AS DECLARE @FieldN1_Vas int=4 ;
    
SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN ''پاس شده'' WHEN (T_Asnad.[FieldN1]) = 2 THEN ''باطل شده '' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN ''نقدی '' WHEN (T_Asnad.[FieldN1]) = 4 THEN ''پاس نشده '' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN ''پاس شده'' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN ''باطل شده '' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN ''نقدی '' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN ''پاس نشده '' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
                                              T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR
    

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqNaghdiPar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

        CREATE PROCEDURE [QSubPFrm_ListCheqNaghdiPar] AS 
        
        
        DECLARE @FieldN1_Vas int=3 ;
    
SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN ''پاس شده'' WHEN (T_Asnad.[FieldN1]) = 2 THEN ''باطل شده '' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN ''نقدی '' WHEN (T_Asnad.[FieldN1]) = 4 THEN ''پاس نشده '' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN ''پاس شده'' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN ''باطل شده '' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN ''نقدی '' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN ''پاس نشده '' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
                                              T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR




' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_ListCheqBatelShodePar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

        CREATE PROCEDURE [QSubPFrm_ListCheqBatelShodePar] AS 
        
        DECLARE @FieldN1_Vas int=2 ;
    
SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN ''پاس شده'' WHEN (T_Asnad.[FieldN1]) = 2 THEN ''باطل شده '' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN ''نقدی '' WHEN (T_Asnad.[FieldN1]) = 4 THEN ''پاس نشده '' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN ''پاس شده'' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN ''باطل شده '' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN ''نقدی '' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN ''پاس نشده '' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
                                              T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSub_MSG_PReviewListCHeckForRunProgramPar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

    CREATE PROCEDURE [QSub_MSG_PReviewListCHeckForRunProgramPar](@AID nvarchar(50), @Baze nvarchar(50)) AS 
    
    
    
SELECT     @AID AS Expr1, Sharh, ShSeryal, FieldN1, GOID, KOLID, HESABID, THESABID, DateSO, DateSR, HESABID2, THESABID2, chkvaz, Pardakht_az_Hesab, ASNADID, Mablagh, Flag2, SharhSub, DarBed, 
                      variz_be_Hesab, HESABIDM, THESABIDM, AsnadNumber
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN ''پاس شده'' WHEN (T_Asnad.[FieldN1]) = 2 THEN ''باطل شده '' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN ''نقدی '' WHEN (T_Asnad.[FieldN1]) = 4 THEN ''پاس نشده '' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = 4) AND (T_Asnad.DateSR <= @Baze)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN ''پاس شده'' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN ''باطل شده '' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN ''نقدی '' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN ''پاس نشده '' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = 4) AND (T_Asnad_1.DateSR <= @Baze)) AS SubAsnad
ORDER BY DateSR
                        


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListTafsiliHesab]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QListTafsiliHesab](@hesabid int) AS SELECT     T_T_Heasb.THESABID, T_T_Heasb.HESABID, T_T_Heasb.NameHesabT, T_T_Heasb.GOID, T_T_Heasb.KolID,                        ABS(ISNULL(SUM([TRANSACTION].DARBed), 0) - ISNULL(SUM([TRANSACTION].PARBes), 0)) AS Expr1, CASE WHEN (ISNULL(SUM(darbed)                        - SUM(parbes), 0)) > 0 THEN N''بد'' WHEN (ISNULL(SUM(darbed) - SUM(parbes), 0)) < 0 THEN N''بس'' ELSE ''--'' END AS Expr2 FROM         T_T_Heasb LEFT OUTER JOIN                       [TRANSACTION] ON T_T_Heasb.THESABID = [TRANSACTION].THESABID GROUP BY T_T_Heasb.THESABID, T_T_Heasb.HESABID, T_T_Heasb.NameHesabT, T_T_Heasb.GOID, T_T_Heasb.KolID HAVING      (T_T_Heasb.HESABID = @hesabid) ORDER BY T_T_Heasb.NameHesabT  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_Foroosh_Sandoghcc]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

CREATE PROCEDURE [QSubPFrm_Foroosh_Sandoghcc] 



AS
BEGIN

	SELECT     TOP 100 PERCENT AID, KALAID, Sharh, Date1, FOROSHID, TedadFo, UnitPriceFo, TedadFo * UnitPriceFo + ISNULL(Avarez, 0) + ISNULL(Maliatkala, 0)                        - ISNULL(FieldN7, 0) AS Stion, radif, TextAIDKharid, FieldN6, GOKID, FOROSHID AS LinkedID, FieldN7, FieldN3, Avarez, Maliatkala,                        TedadFo * UnitPriceFo - ISNULL(FieldN7, 0) AS SumRadif FROM         dbo.[TRANSACTION] WHERE     (NOT (FOROSHID IS NULL)) ORDER BY radif
	

END



              



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QSubPFrm_Foroosh_Sandogh_CALC]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE  PROCEDURE [QSubPFrm_Foroosh_Sandogh_CALC]
	
		@FOROSHID int,
        @SUMFACT DECIMAL(19,4) =0 OUTPUT
AS
BEGIN
	
SELECT     @SUMFACT= SUM(TedadFo * UnitPriceFo + ISNULL(Avarez, 0) + ISNULL(Maliatkala, 0) )
FROM         [TRANSACTION]
WHERE      (FOROSHID = @FOROSHID)
GROUP BY FOROSHID


							  
END





' 
END
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListTafsiliHesabNotLimited]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QListTafsiliHesabNotLimited](@hesabid int) AS SELECT     dbo.T_T_Heasb.THESABID, dbo.T_T_Heasb.NameHesabT, RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_T_Heasb.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_T_Heasb.KolID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_T_Heasb.HESABID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_T_Heasb.THESABID), 4) AS Expr3, dbo.T_T_Heasb.HESABID, dbo.T_T_Heasb.GOID,                        dbo.T_T_Heasb.KolID, ABS(ISNULL(SUM(dbo.[TRANSACTION].DARBed), 0) - ISNULL(SUM(dbo.[TRANSACTION].PARBes), 0)) AS Expr1,                        CASE WHEN (ISNULL(SUM(darbed) - SUM(parbes), 0)) > 0 THEN N''بد'' WHEN (ISNULL(SUM(darbed) - SUM(parbes), 0))                        < 0 THEN N''بس'' ELSE ''--'' END AS Expr2, @hesabid AS Expr4 FROM         dbo.T_T_Heasb LEFT OUTER JOIN                       dbo.[TRANSACTION] ON dbo.T_T_Heasb.THESABID = dbo.[TRANSACTION].THESABID GROUP BY dbo.T_T_Heasb.HESABID, dbo.T_T_Heasb.THESABID, dbo.T_T_Heasb.NameHesabT, dbo.T_T_Heasb.GOID, dbo.T_T_Heasb.KolID HAVING      (@hesabid IS NOT NULL) AND (dbo.T_T_Heasb.HESABID = @hesabid) OR                       (@hesabid IS NULL) ORDER BY dbo.T_T_Heasb.NameHesabT  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListTafsiliHesab1]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QListTafsiliHesab1](@hesabid1 int) AS SELECT     dbo.T_T_Heasb.THESABID, dbo.T_T_Heasb.HESABID, dbo.T_T_Heasb.NameHesabT, dbo.T_T_Heasb.GOID, dbo.T_T_Heasb.KolID,                        ABS(ISNULL(SUM(dbo.[TRANSACTION].DARBed), 0) - ISNULL(SUM(dbo.[TRANSACTION].PARBes), 0)) AS Expr1, CASE WHEN (ISNULL(SUM(darbed)                        - SUM(parbes), 0)) > 0 THEN N''بد'' WHEN (ISNULL(SUM(darbed) - SUM(parbes), 0)) < 0 THEN N''بس'' ELSE ''--'' END AS Expr2 FROM         dbo.T_T_Heasb LEFT OUTER JOIN                       dbo.[TRANSACTION] ON dbo.T_T_Heasb.THESABID = dbo.[TRANSACTION].THESABID GROUP BY dbo.T_T_Heasb.THESABID, dbo.T_T_Heasb.HESABID, dbo.T_T_Heasb.NameHesabT, dbo.T_T_Heasb.GOID, dbo.T_T_Heasb.KolID HAVING      (dbo.T_T_Heasb.HESABID = @hesabid1) ORDER BY dbo.T_T_Heasb.NameHesabT  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListTafsiliHesab_SanadHesabdariNotFiltered]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [QListTafsiliHesab_SanadHesabdariNotFiltered](@FilteerCode int)
AS SELECT     dbo.T_T_Heasb.THESABID AS ACCID, dbo.T_T_Heasb.HESABID, dbo.T_T_Heasb.THESABNumber AS CodeACC, dbo.T_T_Heasb.NameHesabT AS NameACC, 
                      dbo.T_T_Heasb.THESABNumber AS [کد حساب], dbo.T_T_Heasb.NameHesabT AS [عنوان حساب], CONVERT(money, 
                      ABS(ISNULL(QMande_HesabTafsili_SanadHesabdary.DARBed, 0) - ISNULL(QMande_HesabTafsili_SanadHesabdary.PARBes, 0))) AS [مانده حساب], 
                      CASE WHEN (ISNULL((darbed - parbes), 0)) > 0 THEN N''بد'' WHEN (ISNULL((darbed - parbes), 0)) < 0 THEN N''بس'' ELSE ''--'' END AS [تشخيص .], 
                      @FilteerCode AS Expr1
FROM         dbo.T_T_Heasb LEFT OUTER JOIN
                          (SELECT     SUM(PARBes) AS PARBes, SUM(DARBed) AS DARBed, thesabID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY thesabID, thesabID) QMande_HesabTafsili_SanadHesabdary ON 
                      dbo.T_T_Heasb.THESABID = QMande_HesabTafsili_SanadHesabdary.thesabID
ORDER BY dbo.T_T_Heasb.NameHesabT' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListKolHesabNotLimited]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [QListKolHesabNotLimited](@Goid int) AS SELECT     TOP 100 PERCENT T_KolHesab.KOLID, T_KolHesab.NameHesab, T_KolHesab.GOID, ABS(ISNULL(SUM([TRANSACTION].DARBed), 0)                        - ISNULL(SUM([TRANSACTION].PARBes), 0)) AS Expr1, CASE WHEN (ISNULL(SUM(darbed) - SUM(parbes), 0))                        > 0 THEN N''بد'' WHEN (ISNULL(SUM(darbed) - SUM(parbes), 0)) < 0 THEN N''بس'' ELSE ''--'' END AS Expr2, RIGHT(N''0000'' + CONVERT(nvarchar(4),                        T_KolHesab.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), T_KolHesab.KOLID), 4) AS Expr3, @Goid AS Expr4 FROM         T_KolHesab LEFT OUTER JOIN                       [TRANSACTION] ON T_KolHesab.KOLID = [TRANSACTION].KOLID GROUP BY T_KolHesab.KOLID, T_KolHesab.NameHesab, T_KolHesab.GOID, RIGHT(N''0000'' + CONVERT(nvarchar(4), T_KolHesab.GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), T_KolHesab.KOLID), 4) HAVING      (T_KolHesab.GOID = @Goid) AND (@Goid IS NOT NULL) OR                       (@Goid IS NULL) ORDER BY T_KolHesab.NameHesab   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListKolHesab_SanadHesabdariNotFiltered]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [QListKolHesab_SanadHesabdariNotFiltered](@FilteerCode int)
AS SELECT     dbo.T_KolHesab.KOLID AS ACCID, dbo.T_KolHesab.GOID, dbo.T_KolHesab.KolNumber AS CodeACC, dbo.T_KolHesab.NameHesab AS NameACC, 
                      dbo.T_KolHesab.KolNumber AS [كد حساب], dbo.T_KolHesab.NameHesab AS [عنوان حساب], CONVERT(money, ABS(ISNULL(QMande_GoHesab_SanadHesabdary.DARBed, 
                      0) - ISNULL(QMande_GoHesab_SanadHesabdary.PARBes, 0)), 1) AS [مانده حساب], CASE WHEN (ISNULL((darbed - parbes), 0)) 
                      > 0 THEN N''بد'' WHEN (ISNULL((darbed - parbes), 0)) < 0 THEN N''بس'' ELSE ''--'' END AS [تشخيص .], @FilteerCode AS Expr1
FROM         dbo.T_KolHesab LEFT OUTER JOIN
                          (SELECT     SUM(PARBes) AS PARBes, SUM(DARBed) AS DARBed, KOLID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY KOLID, KOLID) QMande_GoHesab_SanadHesabdary ON dbo.T_KolHesab.KOLID = QMande_GoHesab_SanadHesabdary.KOLID
ORDER BY NameACC' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListMoinHesab]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'          CREATE PROCEDURE [QListMoinHesab](@kolid int) AS SELECT     TOP 100 PERCENT T_Heasb.HESABID, T_Heasb.NameHesab, T_Heasb.KolID, T_Heasb.GOID, ABS(ISNULL(SUM([TRANSACTION].DARBed), 0)                        - ISNULL(SUM([TRANSACTION].PARBes), 0)) AS Expr1, CASE WHEN (ISNULL(SUM(darbed) - SUM(parbes), 0))                        > 0 THEN N''بد'' WHEN (ISNULL(SUM(darbed) - SUM(parbes), 0)) < 0 THEN N''بس'' ELSE ''--'' END AS Expr2 FROM         T_Heasb LEFT OUTER JOIN                       [TRANSACTION] ON T_Heasb.HESABID = [TRANSACTION].HESABID GROUP BY T_Heasb.GOID, T_Heasb.NameHesab, T_Heasb.HESABID, T_Heasb.KolID HAVING      (T_Heasb.KolID = @kolid) ORDER BY T_Heasb.NameHesab  SET ANSI_NULLS OFF  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListKolHesab]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QListKolHesab] AS SELECT     TOP 100 PERCENT T_KolHesab.KOLID, T_KolHesab.NameHesab, T_KolHesab.GOID, convert(money,ABS(ISNULL(SUM([TRANSACTION].DARBed), 0)                        - ISNULL(SUM([TRANSACTION].PARBes), 0)), 1) AS Expr1, CASE WHEN (ISNULL(SUM(darbed) - SUM(parbes), 0))                        > 0 THEN N''بد'' WHEN (ISNULL(SUM(darbed) - SUM(parbes), 0)) < 0 THEN N''بس'' ELSE ''--'' END AS Expr2 FROM         T_KolHesab LEFT OUTER JOIN                       [TRANSACTION] ON T_KolHesab.KOLID = [TRANSACTION].KOLID GROUP BY T_KolHesab.KOLID, T_KolHesab.NameHesab, T_KolHesab.GOID ORDER BY T_KolHesab.NameHesab  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QRep_khadamatG]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'    CREATE PROCEDURE [QRep_khadamatG]   ( @prmnvarStartDate nvarchar(50), @prmnvarenddate nvarchar(50), @prmintIdKala int, @prmintGoid int=null, @prmintkolid int=null, @prmintHesabid int=null, @prmintTHesabid int=null  )   AS       DECLARE @cond int;      DECLARE @condKala int;      	   if(NOT (@prmintTHesabid IS NULL)) 	 	      begin 		 	   set @cond= @prmintTHesabid; 	 	   end  	else if(NOT (@prmintHesabid IS NULL))   		   begin  set @cond= @prmintHesabid; 	 	   end   else if(NOT (@prmintkolid IS NULL))  	 	   begin  set @cond= @prmintkolid; 	 	   end else if(NOT (@prmintGoid IS NULL))  		    begin  set @cond= @prmintGoid; 	 	   end 	 else 	 	begin 	 		set @cond= -1; 	 	   end 	 	 if (not (@prmintIdKala is null)) 	 	begin 			 set @condKala= @prmintIdKala; 	 	end  	else 		 begin 			 set @condKala= -1; 	 	end      SELECT     dbo.T_NameKhad.KhadamatNumber, dbo.T_NameKhad.Name AS NameKH, SUM(CONVERT(money, dbo.[TRANSACTION].TedadDKh)                        * dbo.[TRANSACTION].UnitPriceDKh) AS Mablag, SUM(CONVERT(FLOAT, dbo.[TRANSACTION].TedadDKh)) AS TedadDKh, dbo.[TRANSACTION].GOID,                        dbo.T_DKhad.KOLID, dbo.T_DKhad.HesabID, dbo.T_DKhad.THESABID, dbo.HesabName(dbo.T_DKhad.KOLID, dbo.T_DKhad.HesabID, dbo.T_DKhad.THESABID)                        AS namehesab, CASE WHEN NOT (@prmintTHesabid IS NULL) THEN T_DKhad.Thesabid WHEN NOT (@prmintHesabid IS NULL)                        THEN T_DKhad.hesabid WHEN NOT (@prmintkolid IS NULL) THEN T_DKhad.kolid WHEN NOT (@prmintgoid IS NULL)                        THEN [TRANSACTION].goid ELSE - 1 END AS Expr1, CASE WHEN NOT (@prmintIdKala IS NULL) THEN T_NameKhad.NAMEKHID ELSE - 1 END AS Expr2,                        dbo.T_NameKhad.NAMEKHID, CONVERT(nvarchar, dbo.[TRANSACTION].KOLID) + N''.'' + CONVERT(nvarchar, dbo.T_DKhad.HesabID) + N''.'' + CONVERT(nvarchar,                        dbo.T_DKhad.THESABID) AS codeHesab, dbo.T_DKhad.DateKh, dbo.T_NameKhad.Unit, dbo.T_DKhad.Sharh, dbo.[TRANSACTION].UnitPriceDKh,                        dbo.T_DKhad.DKHNumbr, dbo.T_KolHesab.NameHesab AS kolhesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_NameKhad ON dbo.[TRANSACTION].NAMEKHID = dbo.T_NameKhad.NAMEKHID INNER JOIN                       dbo.T_DKhad ON dbo.[TRANSACTION].DKHID = dbo.T_DKhad.DKHID INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID WHERE     (dbo.T_DKhad.DateKh BETWEEN @prmnvarStartDate AND @prmnvarenddate) AND (CASE WHEN NOT (@prmintTHesabid IS NULL)                        THEN T_DKhad.Thesabid WHEN NOT (@prmintHesabid IS NULL) THEN T_DKhad.hesabid WHEN NOT (@prmintkolid IS NULL)                        THEN T_DKhad.kolid WHEN NOT (@prmintgoid IS NULL) THEN [TRANSACTION].goid ELSE - 1 END = @cond) AND (CASE WHEN NOT (@prmintIdKala IS NULL)                        THEN T_NameKhad.NAMEKHID ELSE - 1 END = @condkala) GROUP BY dbo.T_NameKhad.KhadamatNumber, dbo.T_NameKhad.Name, dbo.T_DKhad.KOLID, dbo.T_DKhad.THESABID, dbo.T_DKhad.HesabID,                        dbo.HesabName(dbo.T_DKhad.KOLID, dbo.T_DKhad.HesabID, dbo.T_DKhad.THESABID), dbo.[TRANSACTION].GOID, CASE WHEN NOT (@prmintTHesabid IS NULL)                        THEN T_DKhad.Thesabid WHEN NOT (@prmintHesabid IS NULL) THEN T_DKhad.hesabid WHEN NOT (@prmintkolid IS NULL)                        THEN T_DKhad.kolid WHEN NOT (@prmintgoid IS NULL) THEN [TRANSACTION].goid ELSE - 1 END, CASE WHEN NOT (@prmintIdKala IS NULL)                        THEN T_NameKhad.NAMEKHID ELSE - 1 END, dbo.T_NameKhad.NAMEKHID, CONVERT(nvarchar, dbo.[TRANSACTION].KOLID) + N''.'' + CONVERT(nvarchar,                        dbo.T_DKhad.HesabID) + N''.'' + CONVERT(nvarchar, dbo.T_DKhad.THESABID), dbo.T_DKhad.DateKh, dbo.T_NameKhad.Unit, dbo.T_DKhad.Sharh,                        dbo.[TRANSACTION].UnitPriceDKh, dbo.T_DKhad.DKHNumbr, dbo.T_KolHesab.NameHesab ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListMoinHesab1]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [QListMoinHesab1](@kolid1 int) AS SELECT     TOP 100 PERCENT dbo.T_Heasb.HESABID, dbo.T_Heasb.NameHesab, dbo.T_Heasb.KolID, dbo.T_Heasb.GOID,                        ABS(ISNULL(SUM(dbo.[TRANSACTION].DARBed), 0) - ISNULL(SUM(dbo.[TRANSACTION].PARBes), 0)) AS Expr1, CASE WHEN (ISNULL(SUM(darbed)                        - SUM(parbes), 0)) > 0 THEN N''بد'' WHEN (ISNULL(SUM(darbed) - SUM(parbes), 0)) < 0 THEN N''بس'' ELSE ''--'' END AS Expr2 FROM         dbo.T_Heasb LEFT OUTER JOIN                       dbo.[TRANSACTION] ON dbo.T_Heasb.HESABID = dbo.[TRANSACTION].HESABID GROUP BY dbo.T_Heasb.GOID, dbo.T_Heasb.NameHesab, dbo.T_Heasb.HESABID, dbo.T_Heasb.KolID HAVING      (dbo.T_Heasb.KolID = @kolid1) ORDER BY dbo.T_Heasb.NameHesab  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[QListMoinHesab_SanadHesabdariNotFiltered]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [QListMoinHesab_SanadHesabdariNotFiltered](@FilteerCode int)
AS SELECT     dbo.T_Heasb.HESABID AS ACCID, dbo.T_Heasb.KolID, dbo.T_Heasb.HESABNumber AS CodeACC, dbo.T_Heasb.NameHesab AS NameACC, 
                      dbo.T_Heasb.HESABNumber AS [کد حساب], dbo.T_Heasb.NameHesab AS [عنوان حساب], CONVERT(money, ABS(ISNULL(QMande_HesabMoin_SanadHesabdary.DARBed, 
                      0) - ISNULL(QMande_HesabMoin_SanadHesabdary.PARBes, 0))) AS [مانده حساب], CASE WHEN (ISNULL((darbed - parbes), 0)) 
                      > 0 THEN N''بد'' WHEN (ISNULL((darbed - parbes), 0)) < 0 THEN N''بس'' ELSE ''--'' END AS [تشخيص .], @FilteerCode AS Expr1
FROM         dbo.T_Heasb LEFT OUTER JOIN
                          (SELECT     SUM(PARBes) AS PARBes, SUM(DARBed) AS DARBed, hesabID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY hesabID, hesabID) QMande_HesabMoin_SanadHesabdary ON dbo.T_Heasb.HESABID = QMande_HesabMoin_SanadHesabdary.hesabID
ORDER BY dbo.T_Heasb.NameHesab' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Fun_01186606]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'CREATE FUNCTION [Fun_01186606]()
RETURNS TABLE
AS
RETURN ( SELECT     KHARIDID, SUM(ABS(FieldN3)) AS MabFact, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab, GOID AS GOID, HESABID, THESABID, 
                      KOLID
FROM         dbo.[TRANSACTION]
WHERE     (NOT (KHARIDID IS NULL))
GROUP BY dbo.HesabName(KOLID, HESABID, THESABID), KHARIDID, GOID, HESABID, THESABID, KOLID )
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[FunSubKardexKala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'

 CREATE FUNCTION  [FunSubKardexKala]() RETURNS TABLE  AS RETURN  ( 
 	
 	
SELECT     ''مانده اول دوره'' AS NoeFa1, 0 AS GOID, 0 AS KolID, 0 AS HesabID, 0 AS THesabID, NameKalaID AS KALAID, dbo.F1StartDate() AS Tarikh, 0 AS MMande, UnitPrice * Mojodi AS MVarde, 0 AS MSadre, 
                      0 AS ShomareFactor, 0 AS Tedad, Mojodi AS Varde, 0 AS Sadre, 0 AS PHI_M, AID, GOKID, ''--'' AS Namehesab, 9 AS NoeFa, FieldN1 AS Tm, 0 AS FieldN3
FROM         T_Kala
WHERE     (Mojodi > 0)
UNION ALL
SELECT     CASE WHEN (KHARIDID) IS NOT NULL THEN ''خريد '' WHEN (FOROSHID) IS NOT NULL THEN ''فروش '' WHEN (PARID7) IS NOT NULL THEN ''ب.فروش شماره '' WHEN (DARID8) IS NOT NULL 
                      THEN ''ب.خريد '' WHEN (VORODID) IS NOT NULL THEN ''رسيد انبار '' WHEN (KHOROJID) IS NOT NULL THEN ''حواله انبار '' WHEN (ID1) IS NOT NULL THEN ''جابه جايی '' ELSE '''' END AS NoeFa1, GOID, KOLID, 
                      HESABID, THESABID, KALAID, Date1 AS Tarikh, (CASE WHEN ((KHARIDID) IS NOT NULL OR
                      (PARID7) IS NOT NULL OR
                      (VORODID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) - (CASE WHEN ((FOROSHID) IS NOT NULL OR
                      (DARID8) IS NOT NULL OR
                      (KHOROJID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) AS MMande, (CASE WHEN ((KHARIDID) IS NOT NULL OR
                      (PARID7) IS NOT NULL OR
                      (VORODID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) AS MVarde, (CASE WHEN ((FOROSHID) IS NOT NULL OR
                      (dbo.[TRANSACTION].DARID8) IS NOT NULL OR
                      (KHOROJID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) AS MSadre, OperationCode AS ShomareFactor, (TedadKh + TedadVorood + TedadBarFo + TedadVA) 
                      - (TedadFo + TedadKhorooj + TedadBarKh + TedadKhA) AS Tedad, TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
                      0 AS PHI_M, AID, GOKID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, CASE WHEN (KHARIDID) IS NOT NULL THEN 1 WHEN (FOROSHID) IS NOT NULL THEN 2 WHEN (PARID7) 
                      IS NOT NULL THEN 4 WHEN (DARID8) IS NOT NULL THEN 3 WHEN (VORODID) IS NOT NULL THEN 5 WHEN (KHOROJID) IS NOT NULL THEN 6 WHEN (ID1) IS NOT NULL 
                      THEN 7 ELSE 8 END AS NoeFa, CurRef AS Tm, FieldN3
FROM         [TRANSACTION]
WHERE     (NOT (KALAID IS NULL)) ) 



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[FunSub_05239045]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'

 CREATE FUNCTION  [FunSub_05239045](@startDate NVARCHAR(255),@enddate NVARCHAR(255)) RETURNS TABLE  AS RETURN  ( 
 	
 	


SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''خريد'' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN ''فروش'' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN ''ب.فروش'' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN ''ب.خريد'' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''رسيد انبار'' WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN ''حواله انبار'' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''جابه جاي'' ELSE '''' END AS NoeFa, 
                      [TRANSACTION].TedadKh + [TRANSACTION].TedadVorood + [TRANSACTION].TedadBarFo + [TRANSACTION].TedadVA AS Varde, 
                      [TRANSACTION].TedadFo + [TRANSACTION].TedadKhorooj + [TRANSACTION].TedadBarKh + [TRANSACTION].TedadKhA AS Sadre, [TRANSACTION].AID, [TRANSACTION].KALAID, 
                      CASE WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN FOROSHID WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN KHOROJID ELSE NULL END AS Expr1, RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].KOLID), 4) 
                      + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].THESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), 
                      [TRANSACTION].AID), 4) AS Expr5, [TRANSACTION].Date1 AS MinOfDate1, [TRANSACTION].Date1 AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS namehesab, T_NameKala.KalaNumber, T_NameKala.NameKala1, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL 
                      THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 7 ELSE '''' END AS NoeFact, [TRANSACTION].GOKID, [TRANSACTION].Date1
FROM         T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID
WHERE     (CASE WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN FOROSHID WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN KHOROJID ELSE NULL END IS NOT NULL) AND ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)
 )

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[FunSub_05239044]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'

 Create FUNCTION  [FunSub_05239044](@startDate NVARCHAR(255),@enddate NVARCHAR(255)) RETURNS TABLE  AS RETURN  ( 
 	
 	

SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[VORODID]) 
                      IS NOT NULL THEN VORODID ELSE NULL END AS Expr1, [TRANSACTION].HESABID, [TRANSACTION].THESABID, [TRANSACTION].KOLID, [TRANSACTION].GOID, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN FOROSHID WHEN (dbo.[TRANSACTION].[PARID7]) 
                      IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
                      THEN VORODID WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN KHOROJID WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ID1 ELSE ''""'' END AS sh, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''خريد'' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN ''فروش'' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN ''ب.فروش'' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN ''ب.خريد'' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''رسيد انبار'' WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN ''حواله انبار'' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''جابه جاي'' ELSE '''' END AS NoeFa, SUM(ROUND(CONVERT(float, 
                      [TRANSACTION].TedadKh + [TRANSACTION].TedadVorood + [TRANSACTION].TedadBarFo), 2)) AS Varde, [TRANSACTION].FieldN3, RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) 
                      + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), 
                      [TRANSACTION].THESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].AID), 4) AS Expr5, T_NameKala.VAHED1, [TRANSACTION].KALAID, [TRANSACTION].AID, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL 
                      THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 7 ELSE '''' END AS NoFAKTOR, MIN([TRANSACTION].Date1) AS MinOfDate1, MAX([TRANSACTION].Date1) AS MaxOfDate1, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS namehesab, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].Date1, 
                      T_NameKala.KalaNumber, T_NameKala.NameKala1
FROM         T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID
WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)
GROUP BY CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''خريد'' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN ''فروش'' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN ''ب.فروش'' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN ''ب.خريد'' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''رسيد انبار'' WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN ''حواله انبار'' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''جابه جاي'' ELSE '''' END, [TRANSACTION].AID, [TRANSACTION].KALAID, [TRANSACTION].HESABID, 
                      [TRANSACTION].THESABID, [TRANSACTION].GOID, [TRANSACTION].KOLID, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[PARID7]) 
                      IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN VORODID ELSE NULL END, RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) 
                      + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), 
                      [TRANSACTION].THESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].AID), 4), CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL 
                      THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
                      THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL 
                      THEN 7 ELSE '''' END, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID), [TRANSACTION].FieldN3, T_NameKala.VAHED1, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN FOROSHID WHEN (dbo.[TRANSACTION].[PARID7]) 
                      IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
                      THEN VORODID WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN KHOROJID WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ID1 ELSE ''""'' END, [TRANSACTION].Date1, 
                      T_NameKala.KalaNumber, T_NameKala.NameKala1
HAVING      (CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[VORODID]) 
                      IS NOT NULL THEN VORODID ELSE NULL END IS NOT NULL) OR
                      (CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[VORODID]) 
                      IS NOT NULL THEN VORODID ELSE NULL END IS NOT NULL)

                )

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[FunSub_05239043]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'

 CREATE FUNCTION  [FunSub_05239043](@startDate NVARCHAR(255),@enddate NVARCHAR(255)) RETURNS TABLE  AS RETURN  ( 
 	
 	
SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''خريد'' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN ''فروش'' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN ''ب.فروش'' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN ''ب.خريد'' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''رسيد انبار'' WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN ''حواله انبار'' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''جابه جاي'' ELSE '''' END AS NoeFa, ROUND(CONVERT(float, 
                      ([TRANSACTION].TedadKh + [TRANSACTION].TedadVorood + [TRANSACTION].TedadBarFo + [TRANSACTION].TedadVA) 
                      - ([TRANSACTION].TedadFo + [TRANSACTION].TedadKhorooj + [TRANSACTION].TedadBarKh + [TRANSACTION].TedadKhA)), 2) AS Mande, ROUND(CONVERT(float, 
                      [TRANSACTION].TedadKh + [TRANSACTION].TedadVorood + [TRANSACTION].TedadBarFo + [TRANSACTION].TedadVA), 2) AS Varde, ROUND(CONVERT(float, 
                      [TRANSACTION].TedadFo + [TRANSACTION].TedadKhorooj + [TRANSACTION].TedadBarKh + [TRANSACTION].TedadKhA), 2) AS Sadre, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL 
                      THEN unitpricekh WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN unitpriceBarfo WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
                      THEN unitpricefo WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN unitpricebarkh ELSE NULL END AS Fi, RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) 
                      + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), 
                      [TRANSACTION].THESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].THESABID), 4) AS Expr5, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL 
                      THEN KHARIDID WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN VORODID ELSE NULL END AS Expr1, 
                      [TRANSACTION].Date1 AS MinOfDate1, [TRANSACTION].Date1 AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, 
                      [TRANSACTION].THESABID) AS namehesab, T_NameKala.KalaNumber, [TRANSACTION].KALAID, T_NameKala.NameKala1, [TRANSACTION].AID, [TRANSACTION].GOKID, [TRANSACTION].Date1
FROM         T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID
WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND (CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[PARID7]) 
                      IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN VORODID ELSE NULL END IS NOT NULL)                      )

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[FunSub_05239042]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'

 CREATE FUNCTION  [FunSub_05239042](@startDate NVARCHAR(255),@enddate NVARCHAR(255)) RETURNS TABLE  AS RETURN  ( 
 	
 	
SELECT     [TRANSACTION].HESABID, [TRANSACTION].THESABID, [TRANSACTION].KOLID, [TRANSACTION].GOID, CASE WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
                      THEN FOROSHID WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN KHOROJID ELSE NULL END AS IDAUtoFact, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN KHARIDID WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN FOROSHID WHEN (dbo.[TRANSACTION].[PARID7]) 
                      IS NOT NULL THEN PARID7 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
                      THEN VORODID WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN KHOROJID WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ID1 ELSE ''""'' END AS sho, 
                      CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''خريد'' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN ''فروش'' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                      THEN ''ب.فروش'' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN ''ب.خريد'' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''رسيد انبار'' WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN ''حواله انبار'' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''جابه جاي'' ELSE '''' END AS NoeFa, 
                      [TRANSACTION].TedadFo + [TRANSACTION].TedadKhorooj + [TRANSACTION].TedadBarKh + [TRANSACTION].TedadKhA AS Sadre, [TRANSACTION].FieldN3, T_NameKala.VAHED1, 
                      [TRANSACTION].KALAID, [TRANSACTION].AID, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
                      THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
                      THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 7 ELSE '''' END AS NoFAKTOR, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS namehesab, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].Date1, 
                      T_NameKala.KalaNumber, T_NameKala.NameKala1, RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].KOLID), 4) 
                      + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].HESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), [TRANSACTION].THESABID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), 
                      [TRANSACTION].AID), 4) AS Expr5, [TRANSACTION].GOKID
FROM         T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID
WHERE     (CASE WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN FOROSHID WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN DARID8 WHEN (dbo.[TRANSACTION].[KHOROJID]) 
                      IS NOT NULL THEN KHOROJID ELSE NULL END IS NOT NULL) AND ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)
 
 
 ) 



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Enteghal_Mande_TAFHesabBES]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [Enteghal_Mande_TAFHesabBES]() RETURNS TABLE AS RETURN ( SELECT     TOP 100 PERCENT dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, 1 AS SAID2, 1 AS SAID2Number, 1 AS RadifSAID2,                        ''مانده حساب سال 1388'' AS Sharh, ''مانده حساب سال 1388'' AS TextShSA, ''00000000'' AS Date1,                        SUM(ROUND(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes, 0)) AS Darbed, 0 AS PARBes, dbo.[TRANSACTION].THESABID,                        dbo.[TRANSACTION].HESABID, 2 AS SAIDType FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_T_Heasb ON dbo.[TRANSACTION].THESABID = dbo.T_T_Heasb.THESABID GROUP BY dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].HESABID,                        dbo.T_T_Heasb.NameHesabT HAVING      (SUM(ROUND(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes, 0)) < 0) AND (dbo.[TRANSACTION].THESABID IS NOT NULL) AND                        (dbo.[TRANSACTION].GOID <> 9) ORDER BY dbo.T_T_Heasb.NameHesabT )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Enteghal_Mande_TAFHesabBED]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [Enteghal_Mande_TAFHesabBED]() RETURNS TABLE AS RETURN ( SELECT     TOP 100 PERCENT dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, 1 AS SAID2, 1 AS SAID2Number, 1 AS RadifSAID2,                        ''مانده حساب سال 1388'' AS Sharh, ''مانده حساب سال 1388'' AS TextShSA, ''00000000'' AS Date1,                        SUM(ROUND(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes, 0)) AS Darbed, 0 AS PARBes, dbo.[TRANSACTION].THESABID,                        dbo.[TRANSACTION].HESABID, 2 AS SAIDType FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_T_Heasb ON dbo.[TRANSACTION].THESABID = dbo.T_T_Heasb.THESABID GROUP BY dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].HESABID,                        dbo.T_T_Heasb.NameHesabT HAVING      (SUM(ROUND(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes, 0)) > 0) AND (dbo.[TRANSACTION].THESABID IS NOT NULL) AND                        (dbo.[TRANSACTION].GOID <> 9) ORDER BY dbo.T_T_Heasb.NameHesabT )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Enteghal_Mande_MOInHesabBES]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [Enteghal_Mande_MOInHesabBES]() RETURNS TABLE AS RETURN ( SELECT     TOP 100 PERCENT dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, 1 AS SAID2, 1 AS SAID2Number, 1 AS RadifSAID2,                        ''مانده حساب سال 1388'' AS Sharh, ''مانده حساب سال 1388'' AS TextShSA, ''00000000'' AS Date1,                        SUM(ROUND(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes, 0)) AS Darbed, 0 AS PARBes, dbo.[TRANSACTION].THESABID,                        dbo.[TRANSACTION].HESABID, 2 AS SAIDType FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Heasb ON dbo.[TRANSACTION].HESABID = dbo.T_Heasb.HESABID GROUP BY dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].HESABID,                        dbo.T_Heasb.NameHesab HAVING      (SUM(ROUND(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes, 0)) < 0) AND (dbo.[TRANSACTION].THESABID IS NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) AND (dbo.[TRANSACTION].GOID <> 9) ORDER BY dbo.T_Heasb.NameHesab )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Enteghal_Mande_MOInHesabBED]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [Enteghal_Mande_MOInHesabBED]() RETURNS TABLE AS RETURN ( SELECT     TOP 100 PERCENT dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, 1 AS SAID2, 1 AS SAID2Number, 1 AS RadifSAID2,                        ''مانده حساب سال 1388'' AS Sharh, ''مانده حساب سال 1388'' AS TextShSA, ''00000000'' AS Date1,                        SUM(ROUND(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes, 0)) AS Darbed, 0 AS PARBes, dbo.[TRANSACTION].THESABID,                        dbo.[TRANSACTION].HESABID, 2 AS SAIDType FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Heasb ON dbo.[TRANSACTION].HESABID = dbo.T_Heasb.HESABID GROUP BY dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].THESABID, dbo.[TRANSACTION].HESABID,                        dbo.T_Heasb.NameHesab HAVING      (SUM(ROUND(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes, 0)) > 0) AND (dbo.[TRANSACTION].THESABID IS NULL) AND                        (dbo.[TRANSACTION].HESABID IS NOT NULL) AND (dbo.[TRANSACTION].GOID <> 9) ORDER BY dbo.T_Heasb.NameHesab )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Enteghal_Mande_KOlHesabBES]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [Enteghal_Mande_KOlHesabBES]() RETURNS TABLE AS RETURN ( SELECT     TOP 100 PERCENT dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, 1 AS SAID2, 1 AS SAID2Number, 1 AS RadifSAID2,                        ''مانده حساب سال 1388'' AS Sharh, ''مانده حساب سال 1388'' AS TextShSA, ''00000000'' AS Date1,                        SUM(ROUND(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes, 0)) AS Darbed, 0 AS PARBes, dbo.[TRANSACTION].HESABID,                        2 AS SAIDType FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID GROUP BY dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].HESABID, dbo.T_KolHesab.NameHesab HAVING      (dbo.[TRANSACTION].HESABID IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND                        (SUM(ROUND(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes, 0)) < 0) AND (dbo.[TRANSACTION].GOID <> 9) ORDER BY dbo.T_KolHesab.NameHesab )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Enteghal_Mande_KOlHesab]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [Enteghal_Mande_KOlHesab]() RETURNS TABLE AS RETURN ( SELECT     TOP 100 PERCENT dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, 1 AS SAID2, 1 AS SAID2Number, 1 AS RadifSAID2,                        ''مانده حساب سال 1388'' AS Sharh, ''مانده حساب سال 1388'' AS TextShSA, ''00000000'' AS Date1,                        SUM(ROUND(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes, 0)) AS Darbed, 0 AS PARBes, dbo.[TRANSACTION].HESABID,                        2 AS SAIDType FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID GROUP BY dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].HESABID, dbo.T_KolHesab.NameHesab HAVING      (dbo.[TRANSACTION].HESABID IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND                        (SUM(ROUND(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes, 0)) > 0) AND (dbo.[TRANSACTION].GOID <> 9) ORDER BY dbo.T_KolHesab.NameHesab )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MojoodiKala_Sandogh]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [F_MojoodiKala_Sandogh]() RETURNS TABLE AS RETURN ( SELECT     ROUND((SUM(TedadKh) + SUM(TedadBarFo)) - (SUM(TedadFo) + SUM(TedadBarKh)), 2) AS Mojoodi, KALAID FROM         dbo.[TRANSACTION] GROUP BY KALAID HAVING      (NOT (KALAID IS NULL)) )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MojoodiKala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'    CREATE FUNCTION [F_MojoodiKala] (@operType int,@LinkedID int,@Vazeyat bit) RETURNS TABLE AS RETURN ( SELECT     ROUND((SUM(TedadKh) + SUM(TedadBarFo)) - (SUM(TedadFo) + SUM(TedadBarKh)), 2) AS Mojoodi, KALAID FROM         [TRANSACTION] WHERE     (CASE WHEN @operType = 1 THEN KHARIDID WHEN @operType = 2 THEN FOROSHID WHEN @operType = 3 THEN DARID8 WHEN @operType = 4 THEN PARID7 END                        <> @LinkedID) OR                       (NOT (KHARIDID IS NULL)) OR                       (NOT (FOROSHID IS NULL)) OR                       (NOT (DARID8 IS NULL)) OR                       (NOT (PARID7 IS NULL)) GROUP BY KALAID HAVING      (NOT (KALAID IS NULL)) UNION ALL SELECT     CASE WHEN @operType = 2 OR                       @operType = 4 THEN - SUM(Tedad) ELSE SUM(Tedad) END AS mojoodi, KALAID FROM         TRANSACTION2 WHERE     (OperType = @operType) AND (LinkedID = @LinkedID) GROUP BY KALAID)    ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_ManeGhabl_Kol]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'        CREATE FUNCTION [F_ManeGhabl_Kol](@startdate nvarchar(50), @StartSanadID int) RETURNS TABLE AS RETURN ( SELECT     SUM(dbo.[TRANSACTION].DARBed) AS DARBedGhabl, SUM(dbo.[TRANSACTION].PARBes) AS PARBesGhabl, dbo.[TRANSACTION].KOLID,                        SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS MandeGhabl FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (dbo.[TRANSACTION].Date1 < @startdate) AND (@startdate IS NOT NULL) AND (dbo.T_SA2.SAID2Number < @StartSanadID) AND                        (@StartSanadID IS NOT NULL) OR                       (dbo.[TRANSACTION].Date1 < @startdate) AND (@startdate IS NOT NULL) AND (@StartSanadID IS NULL) OR                       (@startdate IS NULL) AND (dbo.T_SA2.SAID2Number < @StartSanadID) AND (@StartSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].KOLID )   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MandeGhabl_Tafsili]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'        CREATE FUNCTION [F_MandeGhabl_Tafsili](@startdate nvarchar(50), @StartSanadID int) RETURNS TABLE AS RETURN ( SELECT     SUM(dbo.[TRANSACTION].DARBed) AS DARBedGhabl, SUM(dbo.[TRANSACTION].PARBes) AS PARBesGhabl, dbo.[TRANSACTION].THESABID,                        SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS MandeGhabl FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (dbo.[TRANSACTION].Date1 < @startdate) AND (@startdate IS NOT NULL) AND (dbo.T_SA2.SAID2Number < @StartSanadID) AND                        (@StartSanadID IS NOT NULL) OR                       (dbo.[TRANSACTION].Date1 < @startdate) AND (@startdate IS NOT NULL) AND (@StartSanadID IS NULL) OR                       (@startdate IS NULL) AND (dbo.T_SA2.SAID2Number < @StartSanadID) AND (@StartSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].THESABID )   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MandeGhabl_Hesab]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'        CREATE FUNCTION [F_MandeGhabl_Hesab](@startdate nvarchar(50), @StartSanadID int) RETURNS TABLE AS RETURN ( SELECT     SUM(dbo.[TRANSACTION].DARBed) AS DARBedGhabl, SUM(dbo.[TRANSACTION].PARBes) AS PARBesGhabl, dbo.[TRANSACTION].HESABID,                        SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS MandeGhabl FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (dbo.[TRANSACTION].Date1 < @startdate) AND (@startdate IS NOT NULL) AND (dbo.T_SA2.SAID2Number < @StartSanadID) AND                        (@StartSanadID IS NOT NULL) OR                       (dbo.[TRANSACTION].Date1 < @startdate) AND (@startdate IS NOT NULL) AND (@StartSanadID IS NULL) OR                       (@startdate IS NULL) AND (dbo.T_SA2.SAID2Number < @StartSanadID) AND (@StartSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].HESABID )   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MandeGhabl_Goid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'        CREATE FUNCTION [F_MandeGhabl_Goid](@startdate nvarchar(50), @StartSanadID int) RETURNS TABLE AS RETURN ( SELECT     SUM(dbo.[TRANSACTION].DARBed) AS DARBedGhabl, SUM(dbo.[TRANSACTION].PARBes) AS PARBesGhabl, dbo.[TRANSACTION].GOID,                        SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS MandeGhabl FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (dbo.[TRANSACTION].Date1 < @startdate) AND (@startdate IS NOT NULL) AND (dbo.T_SA2.SAID2Number < @StartSanadID) AND                        (@StartSanadID IS NOT NULL) OR                       (dbo.[TRANSACTION].Date1 < @startdate) AND (@startdate IS NOT NULL) AND (@StartSanadID IS NULL) OR                       (@startdate IS NULL) AND (dbo.T_SA2.SAID2Number < @StartSanadID) AND (@StartSanadID IS NOT NULL) GROUP BY dbo.[TRANSACTION].GOID )   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MandeGhabl_AllHesab]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'        CREATE FUNCTION [F_MandeGhabl_AllHesab](@startdate nvarchar(50), @AccID nvarchar(50), @StartSanadID int) RETURNS TABLE AS RETURN ( SELECT     SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].GOID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].KOLID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) AS HesabID,                        SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS MandeGhabl, SUM(dbo.[TRANSACTION].DARBed) AS DARBedGhabl,                        SUM(dbo.[TRANSACTION].PARBes) AS PARBesGhabl FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 WHERE     (dbo.[TRANSACTION].Date1 < @startdate) AND (@startdate IS NOT NULL) AND (dbo.T_SA2.SAID2Number < @StartSanadID) AND                        (@StartSanadID IS NOT NULL) OR                       (dbo.[TRANSACTION].Date1 < @startdate) AND (@startdate IS NOT NULL) AND (@StartSanadID IS NULL) OR                       (@startdate IS NULL) AND (dbo.T_SA2.SAID2Number < @StartSanadID) AND (@StartSanadID IS NOT NULL) GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].GOID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].KOLID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) )   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditMoin_Tar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditMoin_Tar](@GOID int, @Kolid int, @Hesabid int) AS UPDATE    dbo.[TRANSACTION] SET              GOID = @GOID, KOLID = @Kolid WHERE     (HESABID = @Hesabid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_EditKol_Tar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_EditKol_Tar](@Goid int, @Kolid int) AS UPDATE    dbo.[TRANSACTION] SET              GOID = @Goid WHERE     (KOLID = @Kolid)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Mudp_Rep_03029752]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'          CREATE PROCEDURE [Mudp_Rep_03029752](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed,                        SUM(dbo.[TRANSACTION].PARBes) AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1,                        MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1, MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate,                        CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed,                        CASE WHEN (CONVERT(money, CONVERT(money, SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes,                        dbo.T_KolHesab.NameHesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID INNER JOIN                       dbo.T_Heasb ON dbo.[TRANSACTION].HESABID = dbo.T_Heasb.HESABID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.T_KolHesab.NameHesab HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Mudp_Rep_03029751]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'          CREATE PROCEDURE [Mudp_Rep_03029751](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes, dbo.T_KolHesab.NameHesab,                        dbo.T_GropHesab.NameGo FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab, dbo.T_GropHesab.NameGo HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Mudp_Rep_030296]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'          CREATE PROCEDURE [Mudp_Rep_030296](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes, dbo.T_KolHesab.NameHesab,                        dbo.T_GropHesab.NameGo FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab, dbo.T_GropHesab.NameGo HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Mudp_Rep_030295]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'          CREATE PROCEDURE [Mudp_Rep_030295](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bed, CASE WHEN (CONVERT(money, CONVERT(money,                        SUM(DARBed) - SUM(PARBes)))) < 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS Bes, dbo.T_KolHesab.NameHesab FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Mudp_Rep_030294]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'          CREATE PROCEDURE [Mudp_Rep_030294](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, dbo.T_KolHesab.NameHesab,                        dbo.T_GropHesab.NameGo FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID INNER JOIN                       dbo.T_GropHesab ON dbo.[TRANSACTION].GOID = dbo.T_GropHesab.GoID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab, dbo.T_GropHesab.NameGo HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Mudp_Rep_030293]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'          CREATE PROCEDURE [Mudp_Rep_030293](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, SUM(dbo.[TRANSACTION].DARBed) AS SumOfDARBed, SUM(dbo.[TRANSACTION].PARBes)                        AS SumOfPARBes, SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS Expr1, MIN(dbo.[TRANSACTION].Date1) AS MinOfDate1,                        MAX(dbo.[TRANSACTION].Date1) AS MaxOfDate1, @startDate AS startDate, @enddate AS enddate, dbo.T_KolHesab.NameHesab,                        dbo.T_KolHesab.KolNumber FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID GROUP BY dbo.[TRANSACTION].GOID, dbo.[TRANSACTION].KOLID, dbo.T_KolHesab.NameHesab, dbo.T_KolHesab.KolNumber HAVING      (@startDate IS NULL) AND (dbo.[TRANSACTION].KOLID IS NOT NULL) AND (SUM(dbo.[TRANSACTION].DARBed) IS NOT NULL) AND                        (SUM(dbo.[TRANSACTION].PARBes) IS NOT NULL) OR                       (@startDate IS NOT NULL) AND (MIN(dbo.[TRANSACTION].Date1) BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



/****** Object:  View [dbo].[PFrm_Rep_SoodKala_Mojoodi]    Script Date: 04/18/2018 10:12:32 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_Rep_SoodKala_Mojoodi]'))
DROP VIEW [dbo].[PFrm_Rep_SoodKala_Mojoodi]
GO



/****** Object:  View [dbo].[PFrm_Rep_SoodKala_Mojoodi]    Script Date: 04/18/2018 10:12:32 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE VIEW [dbo].[PFrm_Rep_SoodKala_Mojoodi] AS SELECT     CONVERT(float, ROUND((SUM([TRANSACTION].TedadKh)+SUM([TRANSACTION].tedadva)+ SUM([TRANSACTION].TedadBarFo)) - (SUM([TRANSACTION].TedadFo)+SUM([TRANSACTION].Tedadkha)  + SUM([TRANSACTION].TedadBarKh)) 
                      + PFrm_Rep_SoodKala_MojoodiEbtedaDore.Mojodi, 2)) AS Mojoodi, [TRANSACTION].KALAID, PFrm_Rep_SoodKala_MojoodiEbtedaDore.Mojodi AS ebtedadore, 
                      PFrm_Rep_SoodKala_MojoodiEbtedaDore.SumMablagh
FROM         [TRANSACTION] LEFT OUTER JOIN
                      PFrm_Rep_SoodKala_MojoodiEbtedaDore ON [TRANSACTION].KALAID = PFrm_Rep_SoodKala_MojoodiEbtedaDore.NameKalaID
GROUP BY [TRANSACTION].KALAID, PFrm_Rep_SoodKala_MojoodiEbtedaDore.Mojodi, PFrm_Rep_SoodKala_MojoodiEbtedaDore.SumMablagh
HAVING      (NOT ([TRANSACTION].KALAID IS NULL)) 
GO


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[PFrm_ListKala_MoshabehMojoodi]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [PFrm_ListKala_MoshabehMojoodi] (@KalaID int) AS SELECT     (SUM(dbo.[TRANSACTION].TedadKh) + SUM(dbo.[TRANSACTION].TedadVorood) + SUM(dbo.[TRANSACTION].TedadBarFo) + SUM(dbo.[TRANSACTION].TedadVA))                        - (SUM(dbo.[TRANSACTION].TedadFo) + SUM(dbo.[TRANSACTION].TedadKhorooj) + SUM(dbo.[TRANSACTION].TedadBarKh) + SUM(dbo.[TRANSACTION].TedadKhA))                        + ISNULL(F_MojoodiKalaEbteda.Mojodi, 0) AS Expr1, dbo.T_Anbar.NameAnbar, dbo.T_NameKala.NameKala1, dbo.[TRANSACTION].KALAID, dbo.T_GoKala.GOKID, F_MojoodiKalaEbteda.Mojodi,                        dbo.T_GoKala.GOKalaID, dbo.T_GoKala.FieldT1, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.BarCode FROM         dbo.T_NameKala INNER JOIN                       dbo.T_GoKala ON dbo.T_NameKala.ID = dbo.T_GoKala.GOKalaID INNER JOIN                           (SELECT     NameKalaID, AID, SUM(Mojodi) AS Mojodi                              FROM         dbo.T_Kala                              GROUP BY NameKalaID, AID) F_MojoodiKalaEbteda INNER JOIN                       dbo.T_Anbar ON F_MojoodiKalaEbteda.AID = dbo.T_Anbar.AID ON dbo.T_GoKala.GOKalaID = F_MojoodiKalaEbteda.NameKalaID LEFT OUTER JOIN                       dbo.[TRANSACTION] ON F_MojoodiKalaEbteda.AID = dbo.[TRANSACTION].AID AND F_MojoodiKalaEbteda.NameKalaID = dbo.[TRANSACTION].KALAID GROUP BY dbo.T_Anbar.NameAnbar, dbo.T_NameKala.NameKala1, dbo.[TRANSACTION].KALAID, dbo.T_GoKala.GOKID, F_MojoodiKalaEbteda.Mojodi, dbo.T_GoKala.GOKalaID, dbo.T_GoKala.FieldT1,                        dbo.T_NameKala.KalaNumber, dbo.T_NameKala.BarCode HAVING      (dbo.T_GoKala.GOKID = @KalaID) ORDER BY dbo.T_NameKala.NameKala1' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[MandeTaf]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [MandeTaf](@Startdate nvarchar(8),
@EndDate nvarchar(50),
@hesabidM int)
AS SELECT     dbo.T_T_Heasb.THESABID, dbo.T_T_Heasb.THESABNumber, dbo.T_T_Heasb.NameHesabT, dbo.T_T_Heasb.Mob, @Startdate AS Expr1, 
                      mande AS MandeGh, SUM(dbo.[TRANSACTION].DARBed) AS SumBed, SUM(dbo.[TRANSACTION].PARBes) AS SuMBes, @EndDate AS Expr2, 
                      SUM(dbo.[TRANSACTION].DARBed) - SUM(dbo.[TRANSACTION].PARBes) AS MandeDore, @hesabidM AS Expr3
FROM         dbo.[TRANSACTION] INNER JOIN
                      dbo.T_SA2 ON dbo.[TRANSACTION].SAID2 = dbo.T_SA2.SAID2 INNER JOIN
                      dbo.T_T_Heasb ON dbo.[TRANSACTION].THESABID = dbo.T_T_Heasb.THESABID LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, tHESABID
                             FROM         dbo.[TRANSACTION]
                             WHERE     (NOT (SAID2 IS NULL) AND (Date1 < @Startdate))
                             GROUP BY tHESABID) DERIVEDTBL ON dbo.T_T_Heasb.THESABID = DERIVEDTBL.tHESABID
WHERE     (dbo.[TRANSACTION].Date1 BETWEEN @Startdate AND @EndDate) AND (dbo.[TRANSACTION].HESABID = @hesabidM)
GROUP BY dbo.T_T_Heasb.THESABID, dbo.T_T_Heasb.NameHesabT, mande, dbo.T_T_Heasb.Mob, dbo.T_T_Heasb.THESABNumber
HAVING      (NOT (@hesabidM IS NULL))' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[F_MabFact_EditSanad]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'      CREATE FUNCTION [F_MabFact_EditSanad](@said int) RETURNS TABLE AS RETURN ( SELECT     SUM(dbo.[TRANSACTION].TedadFo * dbo.[TRANSACTION].UnitPriceFo) - ISNULL(dbo.T_Forosh.Takhfif, 0) AS MabFact, dbo.T_Forosh.PrimarySaid2,                        dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID, dbo.T_KolHesab.GOID FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Forosh ON dbo.[TRANSACTION].FOROSHID = dbo.T_Forosh.FOROSHID INNER JOIN                       dbo.T_KolHesab ON dbo.[TRANSACTION].KOLID = dbo.T_KolHesab.KOLID WHERE     (NOT (dbo.[TRANSACTION].KALAID IS NULL)) GROUP BY dbo.T_Forosh.Takhfif, dbo.T_Forosh.PrimarySaid2, dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID,                        dbo.T_KolHesab.GOID HAVING      (dbo.T_Forosh.PrimarySaid2 = @said) )   ' 
END
GO


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CUSTOM_REP_KH]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [CUSTOM_REP_KH](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     SUM(dbo.[TRANSACTION].TedadKh) AS SumOfTedadKh, SUM(F_MabHazine_Kharid.MabHazine) AS MabHazine, SUM(dbo.T_Kharid.Takhfif)                        AS SumOfTakhfif, SUM(ABS(dbo.[TRANSACTION].FieldN3)) AS MabFact, SUM(F_MabTakhfif_Kharid.Takhfif) AS Takhfif,                        SUM(ABS(dbo.[TRANSACTION].FieldN3)) + SUM(ISNULL(F_MabHazine_Kharid.MabHazine, 0)) - SUM(F_MabTakhfif_Kharid.Takhfif) AS Total,                        MIN(dbo.T_Kharid.DateKh) AS MinOfDateKh, MAX(dbo.T_Kharid.DateKh) AS MaxOfDateKh, @startDate AS startDate, @enddate AS enddate,                        SUBSTRING(dbo.T_Kharid.DateKh, 1, 6) AS DateM FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Kharid ON dbo.[TRANSACTION].KHARIDID = dbo.T_Kharid.KHARIDID LEFT OUTER JOIN                       dbo.F_MabTakhfif_Kharid() F_MabTakhfif_Kharid ON dbo.T_Kharid.KHARIDID = F_MabTakhfif_Kharid.KHARIDID LEFT OUTER JOIN                       dbo.F_MabHazine_Kharid() F_MabHazine_Kharid ON dbo.T_Kharid.KHARIDID = F_MabHazine_Kharid.FactID WHERE     (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) GROUP BY SUBSTRING(dbo.T_Kharid.DateKh, 1, 6)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CUSTOM_REP_FO]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [CUSTOM_REP_FO](@startDate nvarchar(255), @enddate nvarchar(255)) AS SELECT     SUM(dbo.[TRANSACTION].TedadFo) AS SumOfTedadfo, SUM(F_MabHazine_Foroosh.MabHazine) AS MabHazine, SUM(dbo.T_Forosh.Takhfif)                        AS SumOfTakhfif, SUM(ABS(dbo.[TRANSACTION].FieldN3)) AS MabFact, SUM(F_MabTakhfif_Foroosh.Takhfif) AS Takhfif,                        SUM(ABS(dbo.[TRANSACTION].FieldN3)) + SUM(ISNULL(F_MabHazine_Foroosh.MabHazine, 0)) - SUM(F_MabTakhfif_Foroosh.Takhfif) AS Total,                        MIN(dbo.T_Forosh.DateFo) AS MinOfDatefo, MAX(dbo.T_Forosh.DateFo) AS MaxOfDatefo, @startDate AS startDate, @enddate AS enddate,                        SUBSTRING(dbo.T_Forosh.DateFo, 1, 6) AS DateM FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Forosh ON dbo.[TRANSACTION].FOROSHID = dbo.T_Forosh.FOROSHID LEFT OUTER JOIN                       dbo.F_MabHazine_Foroosh() F_MabHazine_Foroosh ON dbo.T_Forosh.FOROSHID = F_MabHazine_Foroosh.FactID LEFT OUTER JOIN                       dbo.F_MabTakhfif_Foroosh() F_MabTakhfif_Foroosh ON dbo.T_Forosh.FOROSHID = F_MabTakhfif_Foroosh.FOROSHID WHERE     (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) GROUP BY SUBSTRING(dbo.T_Forosh.DateFo, 1, 6) HAVING      (@startDate IS NOT NULL)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DataRep1]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [DataRep1] AS 
SELECT     dbo.[TRANSACTION].TedadFo AS Tedad, dbo.[TRANSACTION].UnitPriceFo AS netprice,                         CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab                         END AS NameTarafHesab, dbo.T_Heasb.NameHesab AS NameHesabMoin, dbo.T_T_Heasb.NameHesabT AS NameHesabTafsili,                         dbo.T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, T_Forosh.KOLID AS CodeKol, T_Forosh.Hesab AS CodeMoin,                         dbo.[TRANSACTION].FieldN3 AS Mablagh, T_Forosh.Takhfif, T_Forosh.DateFo AS Tarikh, dbo.[TRANSACTION].Sharh AS SharhRadif,                         T_Forosh.Sharh AS TozihatFactor, dbo.T_NameVahed.NameVahed AS NameVahedAsli, T_NameVahed_1.NameVahed AS NameVahedFarei,                         dbo.T_NameKala.Zarib, dbo.T_NameKala.BarCode, dbo.T_NameKala.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor,                         T_Forosh.ForoshNumbr AS Serial, T_Forosh.NumberKH AS ShomareFactor, dbo.T_T_Heasb.Address AS AddressTarafHesab,                         dbo.T_T_Heasb.Address2 AS Address2TarafHesab, dbo.T_T_Heasb.Telex, dbo.T_T_Heasb.Shahr, dbo.T_T_Heasb.Ostan, dbo.T_T_Heasb.CodPosti,                         dbo.T_T_Heasb.CodEghtesdi, dbo.T_T_Heasb.Keshvar, dbo.T_T_Heasb.BankHesab, dbo.T_T_Heasb.Mob, dbo.T_T_Heasb.Websit,                         dbo.T_T_Heasb.Phone1, dbo.T_T_Heasb.Phone2, dbo.T_T_Heasb.Email, dbo.T_NameKala.NameKala1, T_Forosh.FOROSHID,                         DERIVEDTBL.mande AS MandeHesab
 FROM         dbo.T_NameVahed T_NameVahed_1 RIGHT OUTER JOIN                        dbo.T_NameVahed RIGHT OUTER JOIN                            (SELECT     FactID, SUM(MabHazine) AS HazineFactor                               FROM         dbo.Table4                               WHERE     (TypeFact = 2) AND (Flag1 = 0)                               GROUP BY FactID) TBL RIGHT OUTER JOIN                        dbo.T_Heasb RIGHT OUTER JOIN                        dbo.T_NameKala INNER JOIN                        dbo.[TRANSACTION] ON dbo.T_NameKala.ID = dbo.[TRANSACTION].KALAID INNER JOIN                            (SELECT     ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH,                                                      FOROSHID                               FROM         dbo.T_Forosh) T_Forosh ON dbo.[TRANSACTION].FOROSHID = T_Forosh.FOROSHID LEFT OUTER JOIN                            (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0)                                                      AS HESAB, KOLID, ISNULL(THESABID, 0) AS THESAB                               FROM         dbo.[TRANSACTION]                               WHERE     (NOT (SAID2 IS NULL))                               GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) DERIVEDTBL ON T_Forosh.KOLID = DERIVEDTBL.KOLID AND                         T_Forosh.Hesab = DERIVEDTBL.HESAB AND T_Forosh.THesab = DERIVEDTBL.THESAB ON                         dbo.T_Heasb.HESABID = T_Forosh.Hesab LEFT OUTER JOIN                        dbo.T_T_Heasb ON T_Forosh.THesab = dbo.T_T_Heasb.THESABID LEFT OUTER JOIN                        dbo.T_KolHesab ON T_Forosh.KOLID = dbo.T_KolHesab.KOLID ON TBL.FactID = T_Forosh.FOROSHID ON                         dbo.T_NameVahed.VAHED1 = dbo.T_NameKala.VAHED1 ON T_NameVahed_1.VAHED1 = dbo.T_NameKala.VAHED2
 WHERE ((T_Forosh.FOROSHID = 3281))



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Check_MojoodiKalaInFactor]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Check_MojoodiKalaInFactor] (@LinkMasterID nvarchar(50), @TarikhFact nvarchar(50),@Opertype int, @Factid int ) AS SELECT     dbo.T_Kala.NameKalaID, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, dbo.T_NameKala.Zarib, dbo.T_Kala.AID,                        dbo.T_Kala.GOKID, dbo.T_NameKala.KalaNumber AS F_CodeKala, dbo.T_NameKala.NameKala1 AS F_NameKala, dbo.T_NameVahed.NameVahed AS F_Vahed,                        dbo.T_Anbar.NameAnbar AS F_NameAnbar, TBL_TRA.MojoodiTra, ROUND(ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(dbo.T_Kala.Mojodi, 0), 2) AS F_MojoodiKala,                        SUM(dbo.TRANSACTION2.Tedad) AS F_TedadInFactor, ROUND(ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(dbo.T_Kala.Mojodi, 0)                        - ISNULL(SUM(dbo.TRANSACTION2.Tedad), 0), 2) AS F_MojoodiNahaei, dbo.T_Kala.Mojodi, @TarikhFact AS F_TarikhFactor FROM         dbo.T_NameVahed INNER JOIN                       dbo.T_NameKala INNER JOIN                       dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID INNER JOIN                       dbo.T_Anbar ON dbo.T_Kala.AID = dbo.T_Anbar.AID ON dbo.T_NameVahed.VAHED1 = dbo.T_NameKala.VAHED1 INNER JOIN                       dbo.TRANSACTION2 ON dbo.T_Kala.AID = dbo.TRANSACTION2.AID AND dbo.T_Kala.NameKalaID = dbo.TRANSACTION2.KALAID LEFT OUTER JOIN                           (SELECT     KALAID, AID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj) + SUM(TedadBarKh)                                                     + SUM(TedadKhA)) AS MojoodiTra                              FROM         [TRANSACTION]                              WHERE     (Date1 <= @TarikhFact) AND (CASE @Opertype WHEN 2 THEN isnull(FOROSHID, -1) WHEN 1 THEN isnull(KHARIDID,-1) WHEN 3 THEN isnull(DARID8, -1)                        WHEN 4 THEN isnull(PARID7, -1) END <> @Factid)                               GROUP BY KALAID, AID) TBL_TRA ON dbo.T_Kala.NameKalaID = TBL_TRA.KALAID AND dbo.T_Kala.AID = TBL_TRA.AID WHERE     (dbo.TRANSACTION2.Date1 = @LinkMasterID)  GROUP BY dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, dbo.T_Kala.AID, dbo.T_Kala.GOKID, TBL_TRA.MojoodiTra,                        ROUND(ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(dbo.T_Kala.Mojodi, 0), 2), dbo.T_Anbar.NameAnbar, dbo.T_NameKala.KalaNumber,                        dbo.T_NameVahed.NameVahed, dbo.T_NameKala.Zarib, dbo.T_Kala.Mojodi HAVING      (ROUND(ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(dbo.T_Kala.Mojodi, 0) - ISNULL(SUM(dbo.TRANSACTION2.Tedad), 0), 2) < 0) ORDER BY dbo.T_NameKala.NameKala1' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AsnadMandeDar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N' CREATE PROCEDURE [AsnadMandeDar] AS SELECT     SAID2, Sharh, Date2, SAID2Number, OperationCode FROM         dbo.T_SA2 WHERE     (SAID2 IN                           (SELECT     SAID2                              FROM         [TRANSACTION]                              GROUP BY SAID2                              HAVING      (SUM(DARBed - PARBes) <> 0)))' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AnalyzKharidForosh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'   
   CREATE PROCEDURE [AnalyzKharidForosh] AS 

SELECT     [TRANSACTION].KALAID, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab, ISNULL([TRANSACTION].TedadFo, 0) 
                      + ISNULL([TRANSACTION].TedadKh, 0) + ISNULL([TRANSACTION].TedadBarFo, 0) + ISNULL([TRANSACTION].TedadBarKh, 0) AS Tedad, ISNULL([TRANSACTION].UnitPriceKh, 0) 
                      + ISNULL([TRANSACTION].UnitPriceFo, 0) + ISNULL([TRANSACTION].UnitPriceBarFo, 0) + ISNULL([TRANSACTION].UnitPriceBarKh, 0) AS PHiVahed, [TRANSACTION].OperationCode, 
                      [TRANSACTION].Date1, (ISNULL([TRANSACTION].TedadFo, 0) + ISNULL([TRANSACTION].TedadKh, 0) + ISNULL([TRANSACTION].TedadBarFo, 0) + ISNULL([TRANSACTION].TedadBarKh, 0)) 
                      * (ISNULL([TRANSACTION].UnitPriceKh, 0) + ISNULL([TRANSACTION].UnitPriceFo, 0) + ISNULL([TRANSACTION].UnitPriceBarFo, 0) + ISNULL([TRANSACTION].UnitPriceBarKh, 0)) AS Sumation, 
                      T_NameKala.NameKala1, T_NameKala.KalaNumber, [TRANSACTION].Sharh, [TRANSACTION].FOROSHID, [TRANSACTION].KHARIDID, [TRANSACTION].DARID8, [TRANSACTION].PARID7, 
                      T_NameGoKala.NameGoKala1, T_Anbar.NameAnbar, CASE WHEN ISNULL([TRANSACTION].FOROSHID, 0) > 0 THEN ''ف فروش'' WHEN ISNULL([TRANSACTION].KHARIDID, 0) 
                      > 0 THEN ''ف خرید'' WHEN ISNULL([TRANSACTION].DARID8, 0) > 0 THEN ''ف ب خرید'' WHEN ISNULL([TRANSACTION].PARID7, 0) > 0 THEN ''ف ب فروش'' END AS NoeFact
FROM         [TRANSACTION] LEFT OUTER JOIN
                      T_Anbar ON [TRANSACTION].AID = T_Anbar.AID LEFT OUTER JOIN
                      T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID LEFT OUTER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[AghlamAsnadMandeDar]'))
EXEC dbo.sp_executesql @statement = N'  CREATE VIEW [AghlamAsnadMandeDar] AS SELECT     TOP 100 PERCENT SAID2, GOID, KOLID, HESABID, THESABID, PARBes, DARBed, Sharh FROM         dbo.[TRANSACTION] WHERE     (SAID2 IN                           (SELECT     SAID2                              FROM         [TRANSACTION]                              GROUP BY SAID2                              HAVING      (SUM(DARBed - PARBes) <> 0))) ORDER BY SAID2 '
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[SubTransactionKala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'
  /* =============================================*/ 
  
  /*-- Author:		<Author,,Name>*/ 
  /*-- Create date: 2015/07/04*/ /*-- Description:	اطلاعات تراکنش کالا*/ 
  
  /*-- =============================================*/  
  CREATE FUNCTION  [SubTransactionKala]() RETURNS TABLE  AS 
  RETURN  ( SELECT     ''مانده اول دوره'' AS NoeFa1, 0 AS GOID, 0 AS KolID, 0 AS HesabID, 0 AS THesabID, dbo.F1StartDate() AS Tarikh, 0 AS ShomareFactor, ''موجودی اول دوره'' AS Namehesab, NameKalaID AS KALAID, AID, 
                      GOKID, Mojodi AS TedadVarde, 0 AS TedadSadre, 0 AS Tedad, UnitPrice * Mojodi AS MVarde, 0 AS MSadre, 9 AS NoeFa, '''' AS SharhKala, 0 AS idFact, 0 AS TedadKh, 0 AS TedadVorood, 
                      0 AS TedadBarFo, 0 AS TedadVA, 0 AS TedadFo, 0 AS TedadKhorooj, 0 AS TedadBarKh, 0 AS TedadKhA, UnitPrice AS UnitPriceKH, 0 AS UnitpriceFo
FROM         T_Kala
WHERE     (Mojodi <> 0)
UNION ALL
SELECT     CASE WHEN (KHARIDID) IS NOT NULL THEN ''خريد '' WHEN (FOROSHID) IS NOT NULL THEN ''فروش '' WHEN (PARID7) IS NOT NULL THEN ''ب.فروش '' WHEN (DARID8) IS NOT NULL 
                      THEN ''ب.خريد '' WHEN (ID1) IS NOT NULL THEN ''جابه جايی '' WHEN (VORODID) IS NOT NULL THEN ''رسيد انبار '' WHEN (KHOROJID) IS NOT NULL THEN ''حواله انبار '' ELSE '''' END AS NoeFa1, GOID, KOLID, 
                      HESABID, THESABID, Date1 AS Tarikh, OperationCode AS ShomareFactor, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, KALAID, AID, GOKID, ISNULL(TedadKh, 0) 
                      + ISNULL(TedadVorood, 0) + ISNULL(TedadBarFo, 0) + ISNULL(TedadVA, 0) AS TedadVarde, ISNULL(TedadFo, 0) + ISNULL(TedadKhorooj, 0) + ISNULL(TedadBarKh, 0) + ISNULL(TedadKhA, 0) 
                      AS TedadSadre, ABS((ISNULL(TedadKh, 0) + ISNULL(TedadVorood, 0) + ISNULL(TedadBarFo, 0) + ISNULL(TedadVA, 0)) - (ISNULL(TedadFo, 0) + ISNULL(TedadKhorooj, 0) + ISNULL(TedadBarKh, 0) 
                      + ISNULL(TedadKhA, 0))) AS Tedad, (CASE WHEN ((KHARIDID) IS NOT NULL OR
                      (PARID7) IS NOT NULL OR
                      (VORODID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) AS MVarde, (CASE WHEN ((FOROSHID) IS NOT NULL OR
                      (dbo.[TRANSACTION].DARID8) IS NOT NULL OR
                      (KHOROJID) IS NOT NULL) THEN ABS(CONVERT(money, FieldN3)) ELSE 0 END) AS MSadre, CASE WHEN (KHARIDID) IS NOT NULL THEN 1 WHEN (FOROSHID) IS NOT NULL 
                      THEN 2 WHEN (PARID7) IS NOT NULL THEN 4 WHEN (DARID8) IS NOT NULL THEN 3 WHEN (VORODID) IS NOT NULL THEN 5 WHEN (KHOROJID) IS NOT NULL THEN 6 WHEN (ID1) IS NOT NULL 
                      THEN 6 ELSE 8 END AS NoeFa, CONVERT(NVARCHAR(255), Sharh) AS SharhKala, CASE WHEN (KHARIDID) IS NOT NULL THEN KHARIDID WHEN (FOROSHID) IS NOT NULL 
                      THEN FOROSHID WHEN (PARID7) IS NOT NULL THEN PARID7 WHEN (DARID8) IS NOT NULL THEN DARID8 WHEN (VORODID) IS NOT NULL THEN VORODID WHEN (KHOROJID) IS NOT NULL 
                      THEN KHOROJID WHEN (ID1) IS NOT NULL THEN ID1 END AS idFact, CONVERT(float, TedadKh) AS TedadKh, CONVERT(float, TedadVorood) AS TedadVorood, CONVERT(float, TedadBarFo) 
                      AS TedadBarFo, CONVERT(float, TedadVA) AS TedadVA, CONVERT(float, TedadFo) AS TedadFo, CONVERT(float, TedadKhorooj) AS TedadKhorooj, CONVERT(float, TedadBarKh) AS TedadBarKh, 
                      CONVERT(float, TedadKhA) AS TedadKhA, ISNULL(UnitPriceKh, 0) + ISNULL(UnitPriceVo, 0) AS UnitPriceKh, UnitPriceFo
FROM         [TRANSACTION]
WHERE     (NOT (KALAID IS NULL)) AND (NOT (Date1 IS NULL)) ) 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure4]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [StoredProcedure4] AS SELECT     @@CURSOR_ROWS + @@CURSOR_ROWS AS Expr1, SAID2, HESABID FROM         dbo.[TRANSACTION] ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure3]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure3] AS SELECT     DARID1 FROM         dbo.[TRANSACTION] WHERE     (DARID1 IS NOT NULL)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure2]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure2] AS SELECT     dbo.[TRANSACTION].* FROM         dbo.[TRANSACTION]  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure15]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [StoredProcedure15] (  @Opertype int,@TarikhFact nvarchar(50),@Factid int) AS SELECT     KALAID, AID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) as MojoodiTra1 , (SUM(TedadFo) + SUM(TedadKhorooj) + SUM(TedadBarKh)  + SUM(TedadKhA)) AS MojoodiTra                              FROM          [TRANSACTION]                              WHERE     (Date1 <= @TarikhFact) and                                                    (CASE @Opertype WHEN 1 THEN isnull(FOROSHID,0)  WHEN 2 THEN isnull(KHARIDID,0)  WHEN 3 THEN isnull(DARID8,0)  WHEN 4 THEN isnull(PARID7,0) END <> @Factid) GROUP BY KALAID, AID' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900380019]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [std_050023008900380019]
(
@pvchAction			    nvarchar(50)=''repgen'', /* Analyze - Print */
@pintGoid				int=null, 
@pintKolid				int=null, 
@pintHesabid			int=null, 
@pintTHesabid			int=null,
@pintAid				int=null, 
@pintGoKalaid			int=null, 
@pintKalaid				int=null, 
@pnvaStatdate			nvarchar(50)=null,
@pnvaEnddate			nvarchar(50)=null,
@pintFactType			int=null, 
@pnvaFactorStartid		nvarchar(50)=null,
@pnvaFactorEndid		nvarchar(50)=null,
@pnvaSanadStartid		int=null,
@pnvaSanadEndid			int=null,
@pnvaSrt				nvarchar(255)=null,
@pnvaShowkala			nvarchar(255)=null

)        

 AS 
 
 begin
	SET NOCOUNT ON;
	
	DECLARE @SQLString  NVARCHAR(4000);        
	DECLARE @WhrStatement  NVARCHAR(2000);        
	DECLARE @ParmDefinition  NVARCHAR(2000);        
	DECLARE @ParmAndstr  NVARCHAR(2000);        
	
	set @WhrStatement  = N'''';        
	set @ParmAndstr = N'''';        
    
    	SET @ParmDefinition = N''@pvchAction  nvarchar(50)=''''repgen'''', 
								@pintGoid				int=null, 
								@pintKolid				int=null, 
								@pintHesabid			int=null, 
								@pintTHesabid			int=null,
								@pintAid				int=null, 
								@pintGoKalaid			int=null, 
								@pintKalaid				int=null, 
								@pnvaStatdate			nvarchar(50)=null,
								@pnvaEnddate			nvarchar(50)=null,
								@pintFactType			int=null, 
								@pnvaFactorStartid		nvarchar(50)=null,
								@pnvaFactorEndid		nvarchar(50)=null,
								@pnvaSanadStartid		int=null,
								@pnvaSanadEndid			int=null,
								@pnvaSrt				nvarchar(255)=null,
								@pnvaShowkala			nvarchar(255)=null'';

    
    
	/* Build the SQL string one time. */

    /* Specify the parameter format one time. */
	if(@pvchAction =''repgen'')
		begin
		
SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''موجودی اول دوره'' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
                      + T_Kala.Mojodi AS Tedad, CASE WHEN ISNULL(TBL_TRA.Varde, T_Kala.Mojodi) > 0 THEN 1 ELSE 2 END as srt2, ISNULL(TBL_TRA.Varde, T_Kala.Mojodi) AS Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''00000000'') 
                      AS Tarikh, T_Kala.NameKalaID, T_NameKala.NameKala1, T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, 
                      T_NameVahed_2.NameVahed AS NameVahed2, T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, 
                      TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, T_NameGoKala.NameGoKala1 AS NameGoKala
FROM         T_NameKala INNER JOIN
                      T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
                      T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
                      T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
                      T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
                          (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''خرید شماره '' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
                                                   THEN ''فروش شماره '' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''ب.فروش شماره '' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
                                                   THEN ''ب.خرید شماره '' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''رسید انبار شماره '' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL
                                                    THEN ''حواله انبار شماره '' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''جابه جای شماره '' ELSE NULL END AS NoeFa, 
                                                   OperationCode AS ShomareFactor, TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
                                                   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
                                                   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) 
                                                   IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
                                                   THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
                                                   THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN 7 ELSE NULL 
                                                   END AS TypeFact, AID, Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, 
                                                   KOLID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
ORDER BY ISNULL(TBL_TRA.Tarikh, N''00000000'')          
		end	
    	if(@pvchAction =''Print'')
		begin
	
	  		 SET @SQLString =

				''SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''''موجودی اول دوره'''' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
									  + T_Kala.Mojodi AS Tedad, CASE WHEN ISNULL(TBL_TRA.Varde, T_Kala.Mojodi) > 0 THEN 1 ELSE 2 END AS srt2, ISNULL(TBL_TRA.Varde, T_Kala.Mojodi) AS Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''''00000000'''') AS Tarikh, T_Kala.NameKalaID, T_NameKala.NameKala1, 
									  T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, T_NameKala.Zarib, 
									  T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, T_Kala.GOKID, 
									  T_NameGoKala.NameGoKala1 AS NameGoKala
				FROM         T_NameKala INNER JOIN
									  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
									  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
									  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
									  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
									  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
										  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''''خرید شماره '''' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
																   THEN ''''فروش شماره '''' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''''ب.فروش شماره '''' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
																   THEN ''''ب.خرید شماره '''' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''''رسید انبار شماره '''' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL
																	THEN ''''حواله انبار شماره '''' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''''جابه جای شماره '''' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
																   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) 
																   IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
																   THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
																   THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL 
																   THEN 7 ELSE NULL END AS TypeFact, AID, Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
											 FROM         [TRANSACTION]
											 WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
				WHERE     (T_Kala.VKalaID = 1) ''
	
			
			set @ParmAndstr='' AND ''
  			if(NOT (@pintAid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintGoKalaid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pnvaStatdate IS NULL) AND NOT (@pnvaEnddate IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(ISNULL(TBL_TRA.Tarikh, N''''00000000'''') BETWEEN @pnvaStatdate AND @pnvaEnddate)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintKalaid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @pintKalaid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintFactType IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.TypeFact = @pintFactType)'';
				set @ParmAndstr='' AND ''
			end

			/*------------------------------*/
			if(NOT (@pintGoid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.GOID = @pintGoid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintKolid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.KOLID = @pintKolid)'';
				set @ParmAndstr='' AND ''
			end
			
			if(NOT (@pintHesabid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.HESABID = @pintHesabid)'';
				set @ParmAndstr='' AND ''
			end

			if(NOT (@pintTHesabid IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.THESABID = @pintTHesabid)'';
				set @ParmAndstr='' AND ''
			end

			set @SQLString=@SQLString + @WhrStatement + N'' ORDER BY TBL_TRA.Tarikh''
	
	
			EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
								@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
								@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
								@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

		end
    	if(@pvchAction =''Analyze'')
		begin
	  		 
	  		 DECLARE @tmpdbname varchar(50)
	  		 set @tmpdbname= right( REPLACE( CONVERT(uniqueidentifier, newid()), ''-'', ''''),8) ;
	  		 
	  		  
	  		 
	  		 IF OBJECT_ID(''tempdb..##'' + @tmpdbname +'''' ) IS NOT NULL
				exec (''DROP TABLE ##'' + @tmpdbname +'''');
	  		 
	  		 SET @SQLString =
				 N''SELECT     CASE WHEN TBL_TRA.NoeFa IS NULL THEN ''''موجودی اول دوره'''' ELSE TBL_TRA.NoeFa END AS NoeFa1, TBL_TRA.ShomareFactor, ISNULL(TBL_TRA.Tedad, 0) 
									  + T_Kala.Mojodi AS Tedad, TBL_TRA.Varde, TBL_TRA.Sadre, TBL_TRA.Ghimat, T_Kala.AID, ISNULL(TBL_TRA.Tarikh, N''''00000000'''') AS Tarikh, T_Kala.NameKalaID, T_NameKala.NameKala1, 
									  T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameVahed_2.NameVahed AS NameVahed2, T_NameKala.Zarib, 
									  T_NameKala.MinMogoody, T_NameKala.MaxMogoody, T_NameKala.BarCode, TBL_TRA.Namehesab, TBL_TRA.NoeFa, T_Kala.GOKID, 
									  T_NameGoKala.NameGoKala1 AS NameGoKala, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3
				INTO         ##'' + @tmpdbname +''
				FROM         T_NameKala INNER JOIN
									  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
									  T_Anbar ON T_Kala.AID = T_Anbar.AID INNER JOIN
									  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
									  T_NameGoKala ON T_Kala.GOKID = T_NameGoKala.ID LEFT OUTER JOIN
									  T_NameVahed AS T_NameVahed_2 ON T_NameKala.VAHED2 = T_NameVahed_2.VAHED1 LEFT OUTER JOIN
										  (SELECT     CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) IS NOT NULL THEN ''''خرید شماره '''' WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL 
																   THEN ''''فروش شماره '''' WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL THEN ''''ب.فروش شماره '''' WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL 
																   THEN ''''ب.خرید شماره '''' WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL THEN ''''رسید انبار شماره '''' WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL
																	THEN ''''حواله انبار شماره '''' WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL THEN ''''جابه جای شماره '''' ELSE NULL END AS NoeFa, OperationCode AS ShomareFactor, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA + TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Tedad, 
																   TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
																   UnitPriceFo + UnitPriceKh + UnitPriceBarFo + UnitPriceBarKh + UnitPriceDKh + UnitPriceHKh AS Ghimat, CASE WHEN (dbo.[TRANSACTION].[KHARIDID]) 
																   IS NOT NULL THEN 1 WHEN (dbo.[TRANSACTION].[FOROSHID]) IS NOT NULL THEN 2 WHEN (dbo.[TRANSACTION].[PARID7]) IS NOT NULL 
																   THEN 4 WHEN (dbo.[TRANSACTION].[DARID8]) IS NOT NULL THEN 3 WHEN (dbo.[TRANSACTION].[VORODID]) IS NOT NULL 
																   THEN 5 WHEN (dbo.[TRANSACTION].[KHOROJID]) IS NOT NULL THEN 6 WHEN (dbo.[TRANSACTION].[ID1]) IS NOT NULL 
																   THEN 7 ELSE NULL END AS TypeFact, AID, Date1 AS Tarikh, KALAID, dbo.HesabName(KOLID, HESABID, THESABID) AS Namehesab, HESABID, THESABID, GOID, KOLID
											 FROM         [TRANSACTION]
											 WHERE     (NOT (KALAID IS NULL))) AS TBL_TRA ON T_Kala.AID = TBL_TRA.AID AND T_Kala.NameKalaID = TBL_TRA.KALAID
				WHERE     (T_Kala.VKalaID = 1) '';
		
		set @WhrStatement=''''
		set @ParmAndstr='' AND ''
  		if(NOT (@pintAid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintGoKalaid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pnvaStatdate IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(ISNULL(TBL_TRA.Tarikh, N''''00000000'''') BETWEEN @pnvaStatdate AND @pnvaEnddate)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintKalaid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.NameKalaID = @pintKalaid)'';
			set @ParmAndstr='' AND '';
		end
		
		if(NOT (@pintFactType IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.TypeFact = @pintFactType)'';
			set @ParmAndstr='' AND '';
		end
		
		/*------------------------------*/
		if(NOT (@pintGoid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.GOID = @pintGoid)'';
			set @ParmAndstr='' AND ''
		end
		
		if(NOT (@pintKolid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.KOLID = @pintKolid)'';
			set @ParmAndstr='' AND ''
		end
		
		if(NOT (@pintHesabid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.HESABID = @pintHesabid)'';
			set @ParmAndstr='' AND ''
		end

		if(NOT (@pintTHesabid IS NULL))
		begin
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(TBL_TRA.THESABID = @pintTHesabid)'';
			set @ParmAndstr='' AND ''
		end
				
		set @SQLString=@SQLString + @WhrStatement + N'' ORDER BY T_NameKala.NameKala1, ISNULL(TBL_TRA.Tarikh, N''''00000000''''), CASE WHEN ISNULL(TBL_TRA.Varde, T_Kala.Mojodi) > 0 THEN 1 ELSE 2 END ; '';
		
		set @SQLString=@SQLString + N''
						Declare Cr2 Cursor Local for
						Select NameKalaID,UnitPrice1,UnitPrice2,(ISNULL(Varde, tedad) - isnull(Sadre,0)) as mandehr from [##'' + @tmpdbname +''] 
						for Update

						DECLARE @Radif int
						DECLARE @col3 int
						DECLARE @col44 int								
						DECLARE @TotalM decimal(19,4)		
						DECLARE @Col1 decimal(19,4)		
						DECLARE @Col2 decimal(19,4)		
						DECLARE @mandehr decimal(19,4)		

						set @Radif=0
						set @col44=0
						set @TotalM=0
						Open Cr2
							fetch Next from Cr2 into @col3,@Col1,@Col2,@mandehr
							while @@FETCH_STATUS=0
							Begin
								if(@col3 <> @col44)
									set	@TotalM = 0
								
								Set @col44 = @col3
								Set @Radif = @Radif + 1
								Set @TotalM = @TotalM + @mandehr
								
								Update ##'' + @tmpdbname +'' set UnitPrice1=@Radif,UnitPrice2=@TotalM where current of cr2;
								fetch Next from Cr2 into @col3,@Col1,@Col2,@mandehr
							End	
						
						Close cr2
						deallocate cr2
						''

						EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
										@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
										@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
										@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

						EXEC (''select * from [##'' + @tmpdbname +'']
								DROP TABLE ##'' + @tmpdbname +'''');

	
	end
                                               
return
end





' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[StoredProcedure1]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [StoredProcedure1] AS SELECT     DARBed AS Expr1, PARBes AS Expr2, SAIDType AS Expr3, SAID2 AS Expr4, ID4 AS Expr5, dbo.[TRANSACTION].* FROM         dbo.[TRANSACTION]  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[stored_Rep_TH1]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'             create PROCEDURE [stored_Rep_TH1] 	( 		@AccID int,@startDate nvarchar(50),@enddate nvarchar(50),@Srt1 smallint,@Srt2 smallint 	)  AS begin  Declare @Sum money ,@Mandeh money,@i int   Set @Sum = 0 Set @i = 1 /* set @Srt1 =1 set @Srt2=2 set @AccID=3 set @startDate=''00000000'' set @enddate=''99999999'' */  DECLARE c2 cursor read_only FOR   SELECT SUM(DARBed - PARBes) AS Expr1 FROM  dbo.[TRANSACTION] WHERE   (Date1 < @startDate)  AND (DARBed IS NOT NULL) AND (PARBes IS NOT NULL) GROUP BY THESABID HAVING (THESABID =  @AccID)  OPEN c2 FETCH c2 INTO @Sum if (@@FETCH_STATUS <> 0) set @Sum=0  CLOSE c2 DEALLOCATE c2  DECLARE c1 cursor FORWARD_ONLY FOR SELECT     mande FROM         View_RepTH1 WHERE     (Date1 BETWEEN @startDate AND @enddate) AND (THESABID = @AccID) AND (DARBed IS NOT NULL) AND (PARBes IS NOT NULL) ORDER BY CASE @Srt1 WHEN 1 THEN Date1 ELSE SAID2Number END,                        CASE @Srt2 WHEN 2 THEN Date1 ELSE SAID2Number END OPEN c1  FETCH c1 INTO @Mandeh WHILE (@@fetch_status = 0) 	BEGIN  		set @Sum=@Sum+@Mandeh 		update View_RepTH1 set [TEMP]= @Sum , TEMPR= convert(nvarchar(20),@i) 		where current of c1 		 		set @i=@i+1 		FETCH c1 INTO @Mandeh 	END CLOSE c1 DEALLOCATE  c1 /* SET NOCOUNT ON */ 	return    END         ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[stored_Rep_Mo1]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'             create PROCEDURE [stored_Rep_Mo1] 	( 		@AccID int,@startDate nvarchar(50),@enddate nvarchar(50),@Srt1 smallint,@Srt2 smallint 	)  AS begin  Declare @Sum money ,@Mandeh money,@i int   Set @Sum = 0 Set @i = 1 /* set @Srt1 =1 set @Srt2=2 set @AccID=3 set @startDate=''00000000'' set @enddate=''99999999'' */  DECLARE c2 cursor read_only FOR   SELECT SUM(DARBed - PARBes) AS Expr1 FROM  dbo.[TRANSACTION] WHERE   (Date1 < @startDate)  AND (DARBed IS NOT NULL) AND (PARBes IS NOT NULL) GROUP BY HesabID HAVING (HesabID =  @AccID)  OPEN c2 FETCH c2 INTO @Sum if (@@FETCH_STATUS <> 0) set @Sum=0  CLOSE c2 DEALLOCATE c2  DECLARE c1 cursor FORWARD_ONLY FOR SELECT     mande FROM         View_RepMO1 WHERE     (Date1 BETWEEN @startDate AND @enddate) AND (HesabID = @AccID) AND (DARBed IS NOT NULL) AND (PARBes IS NOT NULL) ORDER BY CASE @Srt1 WHEN 1 THEN Date1 ELSE SAID2Number END,                        CASE @Srt2 WHEN 2 THEN Date1 ELSE SAID2Number END OPEN c1  FETCH c1 INTO @Mandeh WHILE (@@fetch_status = 0) 	BEGIN  		set @Sum=@Sum+@Mandeh 		update View_RepMO1 set [TEMP]= @Sum , TEMPR= convert(nvarchar(20),@i) 		where current of c1 		 		set @i=@i+1 		FETCH c1 INTO @Mandeh 	END CLOSE c1 DEALLOCATE  c1 /* SET NOCOUNT ON */ 	return    END         ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[stored_Rep_Kol1]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'            CREATE PROCEDURE [stored_Rep_Kol1] 	( 		@AccID int,@startDate nvarchar(50),@enddate nvarchar(50),@Srt1 smallint,@Srt2 smallint 	)  AS begin  Declare @Sum money ,@Mandeh money,@i int   Set @Sum = 0 Set @i = 1 /* set @Srt1 =1 set @Srt2=2 set @AccID=3 set @startDate=''00000000'' set @enddate=''99999999'' */  DECLARE c2 cursor read_only FOR   SELECT SUM(DARBed - PARBes) AS Expr1 FROM  dbo.[TRANSACTION] WHERE   (Date1 < @startDate)  AND (DARBed IS NOT NULL) AND (PARBes IS NOT NULL) GROUP BY KOLID HAVING (KOLID =  @AccID)  OPEN c2 FETCH c2 INTO @Sum if (@@FETCH_STATUS <> 0) set @Sum=0  CLOSE c2 DEALLOCATE c2  DECLARE c1 cursor FORWARD_ONLY FOR SELECT     mande FROM         View_RepKOL1 WHERE     (Date1 BETWEEN @startDate AND @enddate) AND (KOLID = @AccID) AND (DARBed IS NOT NULL) AND (PARBes IS NOT NULL) ORDER BY CASE @Srt1 WHEN 1 THEN Date1 ELSE SAID2Number END,                        CASE @Srt2 WHEN 2 THEN Date1 ELSE SAID2Number END OPEN c1  FETCH c1 INTO @Mandeh WHILE (@@fetch_status = 0) 	BEGIN  		set @Sum=@Sum+@Mandeh 		update View_RepKOL1 set [TEMP]= @Sum , TEMPR= convert(nvarchar(20),@i) 		where current of c1 		 		set @i=@i+1 		FETCH c1 INTO @Mandeh 	END CLOSE c1 DEALLOCATE  c1 /* SET NOCOUNT ON */ 	return    END        ' 
END




/****** Object:  UserDefinedFunction [dbo].[F_AkharinKharid]    Script Date: 01/10/2018 12:40:43 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F_AkharinKharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F_AkharinKharid]
GO



/****** Object:  UserDefinedFunction [dbo].[F_AkharinKharid]    Script Date: 01/10/2018 12:40:43 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

 CREATE FUNCTION [dbo].[F_AkharinKharid]() RETURNS TABLE AS RETURN ( 
 	
SELECT     MAX(F_LastPriceKharid.UnitPriceKh) AS UnitPriceKh, F_LastDateKharid.KALAID
FROM         dbo.F_LastPriceKharid() AS F_LastPriceKharid INNER JOIN
                      dbo.F_LastDateKharid() AS F_LastDateKharid ON F_LastPriceKharid.LinkKalaAid = F_LastDateKharid.LinkKalaAid
GROUP BY F_LastDateKharid.KALAID )
GO






GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[PHiMianginKharid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'
    CREATE FUNCTION [PHiMianginKharid] 	(@KALAID0 int,@Startdate int) RETURNS Money  AS 	   BEGIN 		  /*DECLARE @RasidList Varchar(50)*/ 	 	 	  	IF(@Startdate=-1) 		SET @Startdate = dbo.F1EndDate() 		 	IF(@Startdate=0) 		SET @Startdate = dbo.F1StartDate() 		 			DECLARE CUR CURSOR FOR 	 			 						SELECT     Kardex.Tarikh, Kardex.KALAID, Kardex.Varde, Kardex.Sadre, CONVERT(money, Kardex.MVarde) AS Mvarde, 0 AS PHI_M, 0 AS TM, Kardex.MMande,  				CONVERT(money, Kardex.MSadre) AS MSadre 				FROM         dbo.FunSubKardexKala() AS Kardex INNER JOIN 				T_NameKala ON Kardex.KALAID = T_NameKala.ID WHERE     (Kardex.KALAID = @KALAID0) AND (Kardex.Tarikh <= @Startdate) ORDER BY Kardex.Tarikh, Kardex.Sadre     			 	 	   OPEN CUR     	  	   	 		DECLARE @Tarikh NVARCHAR(4000) 		DECLARE @KALAID bigint 		DECLARE @TM decimal(19,4) 		DECLARE @TedadVarde decimal(19,4) 		DECLARE @TedadSadre decimal(19,4) 		DECLARE @MSadre decimal(19,4) 		DECLARE @Mvarde decimal(19,4) 		DECLARE @PHI_M decimal(19,4) 		DECLARE @PHI_FO decimal(19,4) 		DECLARE @MMande decimal(19,4) 	   	 	 		DECLARE @ColKalaID_CH bigint 		DECLARE @MTedadi decimal(19,4) 		DECLARE @MRiali decimal(19,4)   	   								 		Set @PHI_FO=0 		set @ColKalaID_CH=0 		set @PHI_M=0 		set @MTedadi=0 		set @MRiali=0	 		 		 	 	   FETCH NEXT FROM CUR INTO @Tarikh,@KALAID,@TedadVarde,@TedadSadre,@Mvarde,@PHI_M, @TM, @MMande, @MSadre	 	 	   WHILE @@FETCH_STATUS = 0 				 		 		   BEGIN 				      			  			Set @MTedadi=@MTedadi+(@TedadVarde-@TedadSadre) 			 			set @MRiali=@MRiali+@Mvarde-(@PHI_FO * @TedadSadre) 				  			if (@TedadVarde<>0) and (@MTedadi<>0) 				Begin 					Set @PHI_M =Convert(money, round(ABS(@MRiali/@MTedadi),0)) 				End 			 			if (@TedadSadre<>0) 				Begin 					Set @PHI_M=@PHI_FO  					set @MRiali=@PHI_FO *@MTedadi 				End  			if (@MTedadi=0) 				Set @PHI_M = @PHI_FO  				 			Set @PHI_FO =@PHI_M 				 			FETCH NEXT FROM CUR INTO @Tarikh,@KALAID,@TedadVarde,@TedadSadre,@Mvarde,@PHI_M, @TM, @MMande,@MSadre			 		 			 		   END 			 			 	    CLOSE CUR  		 	    DEALLOCATE CUR       if (Isnull(@PHI_FO,0)=0)	Set  @PHI_FO = 1     RETURN @PHI_FO    END          
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Fun_Sood_Zian_Kala]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'
    CREATE FUNCTION [Fun_Sood_Zian_Kala](@startDate nvarchar(50), @enddate nvarchar(50)) RETURNS TABLE AS RETURN ( SELECT     dbo.[TRANSACTION].KALAID, CASE WHEN SUM(FieldN5) < 0 THEN abs(SUM(FieldN5)) ELSE 0 END AS Zian, CASE WHEN SUM(FieldN5)                        > 0 THEN SUM(FieldN5) ELSE 0 END AS Sood FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID INNER JOIN                       dbo.PFrm_Rep_SoodKala_Mojoodi ON dbo.T_NameKala.ID = dbo.PFrm_Rep_SoodKala_Mojoodi.KALAID WHERE     (dbo.[TRANSACTION].Date1 BETWEEN @startDate AND @enddate) GROUP BY dbo.[TRANSACTION].KALAID ) 
' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_Sandoogh_Name]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Q_ListKala_Sandoogh_Name](@AID int, @GokalaID int, @Option1 bit, @LinkMasterfield int) AS SELECT     dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1 AS [نام كالا], dbo.T_NameKala.BarCode, dbo.T_Kala.AID, dbo.T_Kala.GOKID,                        ROUND(ISNULL(SUM(F_MojoodiKala.Mojoodi), 0) + ISNULL(SUM(dbo.T_Kala.Mojodi), 0), 2) AS موجودي, @Option1 AS Expr1,                        dbo.T_NameKala.UnitPrice1 AS [قيمت فروش], dbo.T_NameKala.UnitPrice1 AS [---], @LinkMasterfield AS Expr2, @GokalaID AS Expr3,                        dbo.T_NameKala.FieldN1, dbo.T_NameKala.UnitPrice1 FROM         dbo.T_NameKala INNER JOIN                       dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN                       dbo.F_MojoodiKala(2, @LinkMasterfield, @Option1) F_MojoodiKala ON dbo.T_NameKala.ID = F_MojoodiKala.KALAID GROUP BY dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, dbo.T_Kala.AID, dbo.T_Kala.GOKID, dbo.T_NameKala.UnitPrice1,                        dbo.T_NameKala.FieldN1 HAVING      (dbo.T_Kala.AID = @AID) AND (dbo.T_Kala.GOKID = @GokalaID) AND (NOT (@GokalaID IS NULL)) OR                       (dbo.T_Kala.AID = @AID) AND (@GokalaID IS NULL) ORDER BY dbo.T_NameKala.NameKala1 ' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_0305200]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'         CREATE PROCEDURE [Rep_0305200](@startDate nvarchar(50), @enddate nvarchar(50), @AccID nvarchar(50)) AS SELECT     TOP 100 PERCENT GOID, KOLID, HESABID, THESABID, SUM(SumOfPARBes) AS SumOfSumOfPARBes, SUM(SumOfDARBed) AS SumOfSumOfDARBed,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, SUM(mande) AS SumOfmande, MIN(MinOfDate1) AS MinOfDate1, MAX(MaxOfDate1) AS MaxOfDate1,                        dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab, @startdate AS startdate, @enddate AS enddate FROM         dbo.Q_Bed_Bes(@startdate, @enddate) Q_Bed_Bes GROUP BY GOID, KOLID, HESABID, THESABID, dbo.HesabName(KOLID, HESABID, THESABID), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) HAVING      (SUM(mande) > 0) AND (@AccID IS NULL) OR                       (SUM(mande) > 0) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID)   ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021451]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021451](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) > 0) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) > 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021450]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021450](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) > 0) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) > 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021448]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021448](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) < 0) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) < 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021447]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021447](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) < 0) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (SUM(DARBed - PARBes) < 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021446]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021446](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed) - SUM(PARBes) AS Mande, @enddate AS enddate,                        @startDate AS startdate, RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        @AccID AS Expr1 FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh WHERE     (@startDate IS NULL) OR                       (@startDate IS NULL) OR                       (Date1 BETWEEN @startDate AND @enddate) AND (@startDate IS NOT NULL) OR                       (Date1 BETWEEN @startDate AND @enddate) AND (@startDate IS NOT NULL) GROUP BY RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID) HAVING      (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021091]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021091](@AccID nvarchar(50), @startDate nvarchar(100), @enddate nvarchar(100), @startSanadID int, @endSanadID int) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID, @startDate AS startDate, @enddate AS enddate, @endSanadID AS endSanadID,                        RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID,                        THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL)                        AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021058]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_021058](@AccID nvarchar(50), @startDate nvarchar(100), @enddate nvarchar(100), @startSanadID int, @endSanadID int) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID, @startDate AS startDate, @enddate AS enddate, @endSanadID AS endSanadID,                        RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID,                        THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL)                        AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_021014]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'      CREATE PROCEDURE [Rep_021014](@AccID nvarchar(50), @startDate nvarchar(100), @enddate nvarchar(100), @startSanadID int, @endSanadID int) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID,                        @startDate AS startDate, @enddate AS enddate, @endSanadID AS endSanadID, RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID, THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (Date1 BETWEEN                        @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND (Date1 BETWEEN @startDate AND                        @enddate) OR                       (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND (SAID2Number BETWEEN                        @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (@AccID IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND (SAID2Number BETWEEN                        @startDate AND @enddate) AND (Date1 BETWEEN @startDate AND @enddate)' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011975]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011975](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NULL) AND (SUM(DARBed - PARBes) < 0) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(DARBed - PARBes) < 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011974]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011974](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NULL) AND (SUM(DARBed - PARBes) < 0) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(DARBed - PARBes) < 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011972]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011972](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NULL) AND (SUM(DARBed - PARBes) > 0) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NOT NULL) AND (SUM(DARBed - PARBes) > 0) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011971]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011971](@AccID nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed - PARBes) AS mande,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        KOLID FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID), KOLID HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NULL) AND (SUM(DARBed - PARBes) > 0) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (SUM(DARBed - PARBes) > 0) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011970]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011970](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50)) AS SELECT     SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, SUM(DARBed) - SUM(PARBes) AS mande, @enddate AS enddate,                        @startDate AS startdate, RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, MIN(Date1) AS MinOfDate1, MAX(Date1) AS MaxOfDate1, dbo.HesabName(KOLID, HESABID, THESABID) AS namehesab,                        @AccID AS Expr1 FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid WHERE     (@startDate IS NULL) OR                       (@startDate IS NULL) OR                       (Date1 BETWEEN @startDate AND @enddate) AND (@startDate IS NOT NULL) OR                       (Date1 BETWEEN @startDate AND @enddate) AND (@startDate IS NOT NULL) GROUP BY RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4),                        GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)), dbo.HesabName(KOLID, HESABID, THESABID) HAVING      (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NULL) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NULL) OR                       (SUM(PARBes) IS NOT NULL) AND (SUM(DARBed) IS NOT NULL) AND (@AccID IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) ORDER BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID))  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011764]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011764](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID nvarchar(50)) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID, @startDate AS startdate, @enddate AS enddate, @endSanadID AS endSanadID,                        RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID,                        THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid WHERE     (@AccID IS NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (@startSanadID IS NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (@startSanadID IS NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (@startSanadID IS NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND                        (SAID2Number BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL))                        AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011763]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011763](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID nvarchar(50)) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID, @startDate AS startdate, @enddate AS enddate, @endSanadID AS endSanadID,                        RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID,                        THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid WHERE     (@AccID IS NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (@startSanadID IS NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (@startSanadID IS NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (@startSanadID IS NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND                        (SAID2Number BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND (NOT (@startSanadID IS NULL))                        AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_011762]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_011762](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID nvarchar(50)) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID, @startDate AS startdate, @enddate AS enddate, @endSanadID AS endSanadID,                        RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID,                        THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Kharid() Q_TRANSACTION_Kharid WHERE     (@AccID IS NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NULL) AND (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL)                        AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate)  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_](@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @startSanadID int, @endSanadID nvarchar(50)) AS SELECT     GOID, KOLID, HESABID, THESABID, PARBes, DARBed, TextShSA, DARBed - PARBes AS mande, SAID2Number, Date1,                        SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) AS hesabidt, @startSanadID AS startSanadID, @startDate AS startdate, @enddate AS enddate, @endSanadID AS endSanadID,                        RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4),                        ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4) AS Hesab, SAID2, dbo.HesabName(KOLID, HESABID,                        THESABID) AS NameHesab FROM         dbo.Q_TRANSACTION_Foroosh() Q_TRANSACTION_Foroosh WHERE     (@AccID IS NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (Date1 BETWEEN @startDate AND @enddate) OR                       (@AccID IS NULL) AND (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NULL) AND (NOT (@startSanadID IS NULL)) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NULL) AND (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL) AND                        (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate) OR                       (@AccID IS NOT NULL) AND (@startDate IS NOT NULL) AND (NOT (@startSanadID IS NULL)) AND (PARBes IS NOT NULL) AND (DARBed IS NOT NULL)                        AND (SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), KOLID), 4)                        + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(HESABID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (Date1 BETWEEN @startDate AND @enddate) AND (SAID2Number BETWEEN @startDate AND @enddate)  ' 
END

GO




/****** Object:  StoredProcedure [dbo].[Rep_kol1]    Script Date: 12/16/2017 11:06:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_kol1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_kol1]
GO



/****** Object:  StoredProcedure [dbo].[Rep_kol1]    Script Date: 12/16/2017 11:06:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

         CREATE PROCEDURE [dbo].[Rep_kol1] (@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @Srt1 smallint,@Srt2 smallint ) AS  
         
         
         
SELECT     Date1, KOLID, SAID2, SAID2Number, DARBed, PARBes, Flag2, Sharh, TextShSA, NameHesab, mande, @startDate AS startDate, @enddate AS enddate, TEMPR, 
                      CASE @Srt1 WHEN 1 THEN Date1 WHEN 2 THEN SAID2Number END AS Expr1, CASE @Srt2 WHEN 2 THEN Date1 WHEN 1 THEN SAID2Number END AS Expr2, @startDate AS textdate1, TEMP, 
                      FlagPrint, TRAID
FROM         View_RepKOL1
WHERE     (Date1 BETWEEN @startDate AND @enddate) AND (KOLID = @AccID) AND (DARBed IS NOT NULL) AND (PARBes IS NOT NULL)
ORDER BY CASE @Srt1 WHEN 1 THEN Date1 ELSE SAID2Number END, CASE @Srt2 WHEN 2 THEN Date1 ELSE SAID2Number END
GO




/****** Object:  StoredProcedure [dbo].[Rep_MO1]    Script Date: 12/16/2017 11:06:37 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_MO1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_MO1]
GO



/****** Object:  StoredProcedure [dbo].[Rep_MO1]    Script Date: 12/16/2017 11:06:37 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

          CREATE PROCEDURE [dbo].[Rep_MO1] (@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @Srt1 smallint,@Srt2 smallint ) AS  
          
          
SELECT     Date1, SAID2, HESABID, SAID2Number, DARBed, PARBes, Flag2, Sharh, TextShSA, NameHesab, mande, @startDate AS startDate, @enddate AS enddate, TEMPR, 
                      CASE @Srt1 WHEN 1 THEN Date1 WHEN 2 THEN SAID2Number END AS Expr1, CASE @Srt2 WHEN 2 THEN Date1 WHEN 1 THEN SAID2Number END AS Expr2, @startDate AS textdate1, TEMP, 
                      FlagPrint, TRAID
FROM         View_RepMO1
WHERE     (Date1 BETWEEN @startDate AND @enddate) AND (HESABID = @AccID) AND (DARBed IS NOT NULL) AND (PARBes IS NOT NULL)
ORDER BY CASE @Srt1 WHEN 1 THEN Date1 ELSE SAID2Number END, CASE @Srt2 WHEN 2 THEN Date1 ELSE SAID2Number END
GO




/****** Object:  StoredProcedure [dbo].[Rep_TH1]    Script Date: 12/16/2017 11:06:42 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_TH1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_TH1]
GO



/****** Object:  StoredProcedure [dbo].[Rep_TH1]    Script Date: 12/16/2017 11:06:42 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

          CREATE PROCEDURE [dbo].[Rep_TH1] (@AccID nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @Srt1 smallint,@Srt2 smallint ) AS 
 SELECT     Date1, SAID2, THESABID, SAID2Number, DARBed, PARBes, Flag2, Sharh, TextShSA, namehesab, mande, @startDate AS startDate, @enddate AS enddate, TEMPR, 
                      CASE @Srt1 WHEN 1 THEN Date1 WHEN 2 THEN SAID2Number END AS Expr1, CASE @Srt2 WHEN 2 THEN Date1 WHEN 1 THEN SAID2Number END AS Expr2, @startDate AS textdate1, TEMP, 
                      FlagPrint, TRAID
FROM         View_RepTH1
WHERE     (Date1 BETWEEN @startDate AND @enddate) AND (THESABID = @AccID) AND (DARBed IS NOT NULL) AND (PARBes IS NOT NULL)
ORDER BY CASE @Srt1 WHEN 1 THEN Date1 ELSE SAID2Number END, CASE @Srt2 WHEN 2 THEN Date1 ELSE SAID2Number END
GO


 

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_MojoodiKharid]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Rep_MojoodiKharid](@IdKala int, @KalaGO nvarchar(50)) AS SELECT     dbo.T_NameKala.NameKala1, dbo.T_NameKala.VAHED1, @IdKala AS IdKala, @KalaGO AS KalaGO, dbo.T_NameKala.KalaNumber,                        dbo.PFrm_Rep_SoodKala_Mojoodi.Mojoodi, dbo.T_NameKala.ID, ISNULL(dbo.PFrm_Rep_SoodKala_Mojoodi.ebtedadore, 0)                        + ISNULL(dbo.View_AvgKH.tedad, 0) AS Expr1, (ISNULL(dbo.View_AvgKH.Mablagh, 0) + ISNULL(dbo.PFrm_Rep_SoodKala_Mojoodi.SumMablagh, 0))                        / (ISNULL(dbo.PFrm_Rep_SoodKala_Mojoodi.ebtedadore, 0) + ISNULL(dbo.View_AvgKH.tedad, 0))                        * dbo.PFrm_Rep_SoodKala_Mojoodi.Mojoodi AS MojoodiRiali, (ISNULL(dbo.View_AvgKH.Mablagh, 0)                        + ISNULL(dbo.PFrm_Rep_SoodKala_Mojoodi.SumMablagh, 0)) / (ISNULL(dbo.PFrm_Rep_SoodKala_Mojoodi.ebtedadore, 0)                        + ISNULL(dbo.View_AvgKH.tedad, 0)) AS Ghimat FROM         dbo.T_NameKala LEFT OUTER JOIN                       dbo.View_AvgKH ON dbo.T_NameKala.ID = dbo.View_AvgKH.KALAID LEFT OUTER JOIN                       dbo.PFrm_Rep_SoodKala_Mojoodi ON dbo.T_NameKala.ID = dbo.PFrm_Rep_SoodKala_Mojoodi.KALAID WHERE     (@IdKala IS NULL) AND (ISNULL(dbo.PFrm_Rep_SoodKala_Mojoodi.ebtedadore, 0) + ISNULL(dbo.View_AvgKH.tedad, 0) > 0) OR                       (@IdKala IS NOT NULL) AND (ISNULL(dbo.PFrm_Rep_SoodKala_Mojoodi.ebtedadore, 0) + ISNULL(dbo.View_AvgKH.tedad, 0) > 0) AND                        (dbo.T_NameKala.ID = @IdKala) ' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_Test]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [Rep_Test](@startSanadID int,
@endSanadID int,
@startDate nvarchar(255),
@enddate nvarchar(255),
@AccID nvarchar(50))
AS SELECT     SUM(ISNULL(dbo.T_Kharid.Takhfif, 0)) AS Takhfif, MIN(dbo.T_Kharid.DateKh) AS MinDateKh, MAX(dbo.T_Kharid.DateKh) AS MaxDateKh, 
                      @enddate AS EndDate, SUM(ISNULL(F_MabHazine_Kharid.MabHazine, 0)) AS MabHazine, @startDate AS startDate, @startSanadID AS startSanadID, 
                      @AccID AS AccID, SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), Fun_01186606.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), 
                      dbo.T_Kharid.KOLID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), 
                      ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) AS CodeHesab, SUM(Fun_01186606.MabFact) AS MabFact, Fun_01186606.namehesab, 
                      SUM(ISNULL(Fun_01186606.MabFact, 0) + ISNULL(F_MabHazine_Kharid.MabHazine, 0) - ISNULL(dbo.T_Kharid.Takhfif, 0)) AS Total
FROM         dbo.T_Kharid INNER JOIN
                      dbo.F_MabTakhfif_Kharid() F_MabTakhfif_Kharid ON dbo.T_Kharid.KHARIDID = F_MabTakhfif_Kharid.KHARIDID INNER JOIN
                      dbo.Fun_01186606() Fun_01186606 ON dbo.T_Kharid.KHARIDID = Fun_01186606.KHARIDID LEFT OUTER JOIN
                      dbo.F_MabHazine_Kharid() F_MabHazine_Kharid ON dbo.T_Kharid.KHARIDID = F_MabHazine_Kharid.FactID
WHERE     (CONVERT(int, dbo.T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) OR
                      (CONVERT(int, dbo.T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) OR
                      (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) OR
                      (CONVERT(int, dbo.T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) AND (dbo.T_Kharid.DateKh BETWEEN @startDate AND 
                      @enddate) OR
                      (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) OR
                      (CONVERT(int, dbo.T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) AND (dbo.T_Kharid.DateKh BETWEEN @startDate AND 
                      @enddate)
GROUP BY SUBSTRING(RIGHT(N''0000'' + CONVERT(nvarchar(4), Fun_01186606.GOID), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) 
                      + RIGHT(N''0000'' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N''0000'' + CONVERT(nvarchar(4), 
                      ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)), Fun_01186606.namehesab
HAVING      (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) OR
                      (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) OR
                      (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) OR
                      (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) OR
                      (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) OR
                      (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) OR
                      (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) OR
                      (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL)' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_sood_Sub1]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'     CREATE FUNCTION [Rep_sood_Sub1]() RETURNS TABLE AS RETURN ( SELECT     ISNULL(ROUND((SUM((dbo.[TRANSACTION].TedadKh + dbo.[TRANSACTION].TedadBarFo)                        - (dbo.[TRANSACTION].TedadFo + dbo.[TRANSACTION].TedadBarKh)) + Q_MandeHRial.Mojodi) * (CASE WHEN q_MandeHRial.CountKharid = NULL                        THEN 0 ELSE Q_MandeHRial.MablaghMiangin / Q_MandeHRial.CountKharid END), 2), 0) AS MablaghMo, dbo.[TRANSACTION].KALAID,                        Q_MandeHRial.MablaghMiangin, Q_MandeHRial.CountKharid, Q_MandeHRial.Mojodi FROM         dbo.[TRANSACTION] LEFT OUTER JOIN                       dbo.Q_MandeHRial() Q_MandeHRial ON dbo.[TRANSACTION].KALAID = Q_MandeHRial.KALAID GROUP BY dbo.[TRANSACTION].KALAID, Q_MandeHRial.MablaghMiangin, Q_MandeHRial.CountKharid, Q_MandeHRial.Mojodi HAVING      (NOT (dbo.[TRANSACTION].KALAID IS NULL)) )  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[std_050023008900390021]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [std_050023008900390021]
(
@pvchAction			    nvarchar(50)=''RepGen'', /* Analyze - Print */
@pintGoid				int=null, 
@pintKolid				int=null, 
@pintHesabid			int=null, 
@pintTHesabid			int=null,
@pintAid				int=null, 
@pintGoKalaid			int=null, 
@pintKalaid				int=null, 
@pnvaStatdate			nvarchar(50)=null,
@pnvaEnddate			nvarchar(50)=null,
@pintFactType			int=null, 
@pnvaFactorStartid		nvarchar(50)=null,
@pnvaFactorEndid		nvarchar(50)=null,
@pnvaSanadStartid		int=null,
@pnvaSanadEndid			int=null,
@pnvaSrt				nvarchar(255)=null,
@pnvaShowkala			nvarchar(255)=null
)        

 AS 
begin

	DECLARE @SQLString  NVARCHAR(4000);        
	DECLARE @WhrStatement  NVARCHAR(2000);        
	DECLARE @WhrStatementsub  NVARCHAR(500);        
	DECLARE @HavStatement  NVARCHAR(2000);        
	DECLARE @ParmDefinition  NVARCHAR(2000);        
	DECLARE @ParmAndstr  NVARCHAR(2000);        
	
	set @WhrStatement  = N'''';        
	set @HavStatement  = N'''';        
	set @ParmAndstr = N'''';        
	set @WhrStatementsub=N''AID <> 0''

	SET @ParmDefinition = 	  N''@pvchAction  nvarchar(50)=''''RepGen'''', 
								@pintGoid				int=null, 
								@pintKolid				int=null, 
								@pintHesabid			int=null, 
								@pintTHesabid			int=null,
								@pintAid				int=null, 
								@pintGoKalaid			int=null, 
								@pintKalaid				int=null, 
								@pnvaStatdate			nvarchar(50)=null,
								@pnvaEnddate			nvarchar(50)=null,
								@pintFactType			int=null, 
								@pnvaFactorStartid		nvarchar(50)=null,
								@pnvaFactorEndid		nvarchar(50)=null,
								@pnvaSanadStartid		int=null,
								@pnvaSanadEndid			int=null,
								@pnvaSrt				nvarchar(255)=null,
								@pnvaShowkala			nvarchar(255)=null'';


	/* Build the SQL string one time. */

    
    /* Specify the parameter format one time. */
	if(@pvchAction =''repgen'')
	begin
		
		SELECT     T_NameKala.ID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameVahed.NameVahed AS NameVahed1, T_NameVahed_1.NameVahed AS NameVahed2, 
							  T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, 
							  T_NameKala.UnitPrice6, ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) 
							  + SUM([TRANSACTION].TedadVA)) - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) 
							  + SUM([TRANSACTION].TedadKhA)) + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2) AS Mande, ISNULL(F_AkharinKharid_1.UnitPriceKh, MAX(T_Kala.UnitPrice)) 
							  AS AkharinKharid, MAX(T_Kala.UnitPrice) AS Expr1, F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, 
							  T_NameKala.MaxMogoody
		FROM         T_NameKala INNER JOIN
							  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
							  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
								  (SELECT     NameKalaID, SUM(Mojodi) AS mojoodiEbteda
									 FROM         T_Kala AS T_Kala_1
									 WHERE     (AID <> 0)
									 GROUP BY NameKalaID) AS Mojoodiebteda ON T_Kala.NameKalaID = Mojoodiebteda.NameKalaID LEFT OUTER JOIN
							  dbo.F_AkharinKharid() AS F_AkharinKharid_1 ON T_Kala.NameKalaID = F_AkharinKharid_1.KALAID LEFT OUTER JOIN
							  [TRANSACTION] ON T_Kala.NameKalaID = [TRANSACTION].KALAID AND T_Kala.AID = [TRANSACTION].AID AND 
							  T_Kala.GOKID = [TRANSACTION].GOKID LEFT OUTER JOIN
							  T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1
		GROUP BY T_NameKala.ID, T_NameKala.NameKala1, T_NameKala.KalaNumber, T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameVahed.NameVahed, 
							  T_NameVahed_1.NameVahed, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, 
							  F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody
		HAVING      (ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) + SUM([TRANSACTION].TedadVA)) 
							  - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) + SUM([TRANSACTION].TedadKhA)) 
							  + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2) <> 0)	
			
	
	end	
	else if(@pvchAction =''analyze'' or @pvchAction =''print'')
	begin
	
		if(NOT (@pnvaShowkala IS NULL))
		begin
															
			set @HavStatement= @HavStatement + N''
											  HAVING      (ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) + SUM([TRANSACTION].TedadVA)) 
											  - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) + SUM([TRANSACTION].TedadKhA)) 
											  + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2)'' + @pnvaShowkala +'')'';
		end
		
		set @ParmAndstr=''''
  		if(NOT (@pintAid IS NULL))
		begin
		
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
			set @WhrStatementsub= N''(AID = @pintAid)'';
			set @ParmAndstr='' AND ''
			
		end
		
		if(NOT (@pintGoKalaid IS NULL))
		begin
			
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';
			set @ParmAndstr='' AND ''
			
		end
		
		if(NOT (@pintKalaid IS NULL))
		begin
			
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_NameKala.ID = @pintKalaid)'';
			set @ParmAndstr='' AND ''
			
		end
		
		if(NOT (@pnvaStatdate IS NULL) AND NOT (@pnvaEnddate IS NULL))
			begin
				set @WhrStatement= @WhrStatement + @ParmAndstr + N'' ([TRANSACTION].Date1  BETWEEN @pnvaStatdate AND @pnvaEnddate)'';
				set @ParmAndstr='' AND ''
			end
		
		if (len( @WhrStatement)> 0 )
			set @WhrStatement=''WHERE ''+ @WhrStatement
		
  		 SET @SQLString =
					 N''SELECT     T_NameKala.ID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameVahed.NameVahed AS NameVahed1, T_NameVahed_1.NameVahed AS NameVahed2, 
									  T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, 
									  T_NameKala.UnitPrice6, ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) 
									  + SUM([TRANSACTION].TedadVA)) - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) 
									  + SUM([TRANSACTION].TedadKhA)) + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2) AS Mande, ISNULL(F_AkharinKharid_1.UnitPriceKh, MAX(T_Kala.UnitPrice)) 
									  AS AkharinKharid, MAX(T_Kala.UnitPrice) AS Expr1, F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, 
									  T_NameKala.MaxMogoody
						FROM         T_NameKala INNER JOIN
											  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
											  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
												  (SELECT     NameKalaID, SUM(Mojodi) AS mojoodiEbteda
													 FROM         T_Kala AS T_Kala_1
													 WHERE     (''+@WhrStatementsub + N'')
													 GROUP BY NameKalaID) AS Mojoodiebteda ON T_Kala.NameKalaID = Mojoodiebteda.NameKalaID LEFT OUTER JOIN
											  dbo.F_AkharinKharid() AS F_AkharinKharid_1 ON T_Kala.NameKalaID = F_AkharinKharid_1.KALAID LEFT OUTER JOIN
											  [TRANSACTION] ON T_Kala.NameKalaID = [TRANSACTION].KALAID AND T_Kala.AID = [TRANSACTION].AID AND 
											  T_Kala.GOKID = [TRANSACTION].GOKID LEFT OUTER JOIN
											  T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1 ''+ @WhrStatement +
						N'' GROUP BY T_NameKala.ID, T_NameKala.NameKala1, T_NameKala.KalaNumber, T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameVahed.NameVahed, 
											  T_NameVahed_1.NameVahed, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, 
											  F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody'' + @HavStatement

	
		EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
								@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
								@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
								@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

	end                                                             
return
end



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Std_05002300880034]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [Std_05002300880034] 
(
@pvchAction			    nvarchar(50)=''RepGen'', /* Analyze - Print */
@pintGoid				int=null, 
@pintKolid				int=null, 
@pintHesabid			int=null, 
@pintTHesabid			int=null,
@pintAid				int=null, 
@pintGoKalaid			int=null, 
@pintKalaid				int=null, 
@pnvaStatdate			nvarchar(50)=null,
@pnvaEnddate			nvarchar(50)=null,
@pintFactType			int=null, 
@pnvaFactorStartid		nvarchar(50)=null,
@pnvaFactorEndid		nvarchar(50)=null,
@pnvaSanadStartid		int=null,
@pnvaSanadEndid			int=null,
@pnvaSrt				nvarchar(255)=null,
@pnvaShowkala			nvarchar(255)=null
)        

 AS 
begin

	DECLARE @SQLString  NVARCHAR(4000);        
	DECLARE @WhrStatement  NVARCHAR(2000);        
	DECLARE @WhrStatementsub  NVARCHAR(500);        
	DECLARE @HavStatement  NVARCHAR(2000);        
	DECLARE @ParmDefinition  NVARCHAR(2000);        
	DECLARE @ParmAndstr  NVARCHAR(2000);        
	
	set @WhrStatement  = N'''';        
	set @HavStatement  = N'''';        
	set @ParmAndstr = N'''';        
	set @WhrStatementsub=N''AID <> 0''

	SET @ParmDefinition = 	  N''@pvchAction  nvarchar(50)=''''RepGen'''', 
								@pintGoid				int=null, 
								@pintKolid				int=null, 
								@pintHesabid			int=null, 
								@pintTHesabid			int=null,
								@pintAid				int=null, 
								@pintGoKalaid			int=null, 
								@pintKalaid				int=null, 
								@pnvaStatdate			nvarchar(50)=null,
								@pnvaEnddate			nvarchar(50)=null,
								@pintFactType			int=null, 
								@pnvaFactorStartid		nvarchar(50)=null,
								@pnvaFactorEndid		nvarchar(50)=null,
								@pnvaSanadStartid		int=null,
								@pnvaSanadEndid			int=null,
								@pnvaSrt				nvarchar(255)=null,
								@pnvaShowkala			nvarchar(255)=null'';


	/* Build the SQL string one time. */

    
    /* Specify the parameter format one time. */
	if(@pvchAction =''repgen'')
	begin
		
		SELECT     T_NameKala.ID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameVahed.NameVahed AS NameVahed1, T_NameVahed_1.NameVahed AS NameVahed2, 
							  T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, 
							  T_NameKala.UnitPrice6, ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) 
							  + SUM([TRANSACTION].TedadVA)) - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) 
							  + SUM([TRANSACTION].TedadKhA)) + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2) AS Mande, ISNULL(F_AkharinKharid_1.UnitPriceKh, MAX(T_Kala.UnitPrice)) 
							  AS AkharinKharid, MAX(T_Kala.UnitPrice) AS Expr1, F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, 
							  T_NameKala.MaxMogoody
		FROM         T_NameKala INNER JOIN
							  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
							  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
								  (SELECT     NameKalaID, SUM(Mojodi) AS mojoodiEbteda
									 FROM         T_Kala AS T_Kala_1
									 WHERE     (AID <> 0)
									 GROUP BY NameKalaID) AS Mojoodiebteda ON T_Kala.NameKalaID = Mojoodiebteda.NameKalaID LEFT OUTER JOIN
							  dbo.F_AkharinKharid() AS F_AkharinKharid_1 ON T_Kala.NameKalaID = F_AkharinKharid_1.KALAID LEFT OUTER JOIN
							  [TRANSACTION] ON T_Kala.NameKalaID = [TRANSACTION].KALAID AND T_Kala.AID = [TRANSACTION].AID AND 
							  T_Kala.GOKID = [TRANSACTION].GOKID LEFT OUTER JOIN
							  T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1
		GROUP BY T_NameKala.ID, T_NameKala.NameKala1, T_NameKala.KalaNumber, T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameVahed.NameVahed, 
							  T_NameVahed_1.NameVahed, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, 
							  F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody
	end	
	else if(@pvchAction =''analyze'' or @pvchAction =''print'')
	begin
	
		if(NOT (@pnvaShowkala IS NULL))
		begin
			set @HavStatement= @HavStatement + N''HAVING      (ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) + SUM([TRANSACTION].TedadVA)) 
											  - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) + SUM([TRANSACTION].TedadKhA)) 
											  + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2)'' + @pnvaShowkala +'')'';
		end
		
		set @ParmAndstr=''''
  		if(NOT (@pintAid IS NULL))
		begin
		
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
			set @ParmAndstr='' AND ''
			
		end
		
		if(NOT (@pintGoKalaid IS NULL))
		begin
			
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';
			set @WhrStatementsub= N''(AID = @pintGoKalaid)'';
			
			set @ParmAndstr='' AND ''
			
		end
		
		if(NOT (@pintKalaid IS NULL))
		begin
			
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_NameKala.ID = @pintKalaid)'';
			set @ParmAndstr='' AND ''
			
		end
		
		if (len( @WhrStatement)> 0 )
			set @WhrStatement=''WHERE ''+ @WhrStatement
		
  		 SET @SQLString =
					 N''SELECT     T_NameKala.ID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameVahed.NameVahed AS NameVahed1, T_NameVahed_1.NameVahed AS NameVahed2, 
									  T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, 
									  T_NameKala.UnitPrice6, ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) 
									  + SUM([TRANSACTION].TedadVA)) - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) 
									  + SUM([TRANSACTION].TedadKhA)) + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2) AS Mande, ISNULL(F_AkharinKharid_1.UnitPriceKh, MAX(T_Kala.UnitPrice)) 
									  AS AkharinKharid, MAX(T_Kala.UnitPrice) AS Expr1, F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, 
									  T_NameKala.MaxMogoody
						FROM         T_NameKala INNER JOIN
											  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
											  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
												  (SELECT     NameKalaID, SUM(Mojodi) AS mojoodiEbteda
													 FROM         T_Kala AS T_Kala_1
													 WHERE     (''+@WhrStatementsub + N'')
													 GROUP BY NameKalaID) AS Mojoodiebteda ON T_Kala.NameKalaID = Mojoodiebteda.NameKalaID LEFT OUTER JOIN
											  dbo.F_AkharinKharid() AS F_AkharinKharid_1 ON T_Kala.NameKalaID = F_AkharinKharid_1.KALAID LEFT OUTER JOIN
											  [TRANSACTION] ON T_Kala.NameKalaID = [TRANSACTION].KALAID AND T_Kala.AID = [TRANSACTION].AID AND 
											  T_Kala.GOKID = [TRANSACTION].GOKID LEFT OUTER JOIN
											  T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1 ''+ @WhrStatement +
						N'' GROUP BY T_NameKala.ID, T_NameKala.NameKala1, T_NameKala.KalaNumber, T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameVahed.NameVahed, 
											  T_NameVahed_1.NameVahed, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, 
											  F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody'' + @HavStatement

	
		EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
								@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
								@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
								@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

	end                                                
return
end









' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Std_05002300880033]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [Std_05002300880033]
(
@pvchAction			    nvarchar(50)=''RepGen'', /* Analyze - Print */
@pintGoid				int=null, 
@pintKolid				int=null, 
@pintHesabid			int=null, 
@pintTHesabid			int=null,
@pintAid				int=null, 
@pintGoKalaid			int=null, 
@pintKalaid				int=null, 
@pnvaStatdate			nvarchar(50)=null,
@pnvaEnddate			nvarchar(50)=null,
@pintFactType			int=null, 
@pnvaFactorStartid		nvarchar(50)=null,
@pnvaFactorEndid		nvarchar(50)=null,
@pnvaSanadStartid		int=null,
@pnvaSanadEndid			int=null,
@pnvaSrt				nvarchar(255)=null,
@pnvaShowkala			nvarchar(255)=null
)        

 AS 
begin

	DECLARE @SQLString  NVARCHAR(4000);        
	DECLARE @WhrStatement  NVARCHAR(2000);        
	DECLARE @WhrStatementsub  NVARCHAR(500);        
	DECLARE @HavStatement  NVARCHAR(2000);        
	DECLARE @ParmDefinition  NVARCHAR(2000);        
	DECLARE @ParmAndstr  NVARCHAR(2000);        
	
	set @WhrStatement  = N'''';        
	set @HavStatement  = N'''';        
	set @ParmAndstr = N'''';        
	set @WhrStatementsub=N''AID <> 0''

	SET @ParmDefinition = 	  N''@pvchAction  nvarchar(50)=''''RepGen'''', 
								@pintGoid				int=null, 
								@pintKolid				int=null, 
								@pintHesabid			int=null, 
								@pintTHesabid			int=null,
								@pintAid				int=null, 
								@pintGoKalaid			int=null, 
								@pintKalaid				int=null, 
								@pnvaStatdate			nvarchar(50)=null,
								@pnvaEnddate			nvarchar(50)=null,
								@pintFactType			int=null, 
								@pnvaFactorStartid		nvarchar(50)=null,
								@pnvaFactorEndid		nvarchar(50)=null,
								@pnvaSanadStartid		int=null,
								@pnvaSanadEndid			int=null,
								@pnvaSrt				nvarchar(255)=null,
								@pnvaShowkala			nvarchar(255)=null'';


	/* Build the SQL string one time. */

    
    /* Specify the parameter format one time. */
	if(@pvchAction =''repgen'')
	begin
		
		SELECT     T_NameKala.ID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameVahed.NameVahed AS NameVahed1, T_NameVahed_1.NameVahed AS NameVahed2, 
							  T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, 
							  T_NameKala.UnitPrice6, ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) 
							  + SUM([TRANSACTION].TedadVA)) - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) 
							  + SUM([TRANSACTION].TedadKhA)) + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2) AS Mande, ISNULL(F_AkharinKharid_1.UnitPriceKh, MAX(T_Kala.UnitPrice)) 
							  AS AkharinKharid, MAX(T_Kala.UnitPrice) AS Expr1, F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, 
							  T_NameKala.MaxMogoody
		FROM         T_NameKala INNER JOIN
							  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
							  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
								  (SELECT     NameKalaID, SUM(Mojodi) AS mojoodiEbteda
									 FROM         T_Kala AS T_Kala_1
									 WHERE     (AID <> 0)
									 GROUP BY NameKalaID) AS Mojoodiebteda ON T_Kala.NameKalaID = Mojoodiebteda.NameKalaID LEFT OUTER JOIN
							  dbo.F_AkharinKharid() AS F_AkharinKharid_1 ON T_Kala.NameKalaID = F_AkharinKharid_1.KALAID LEFT OUTER JOIN
							  [TRANSACTION] ON T_Kala.NameKalaID = [TRANSACTION].KALAID AND T_Kala.AID = [TRANSACTION].AID AND 
							  T_Kala.GOKID = [TRANSACTION].GOKID LEFT OUTER JOIN
							  T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1
		GROUP BY T_NameKala.ID, T_NameKala.NameKala1, T_NameKala.KalaNumber, T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameVahed.NameVahed, 
							  T_NameVahed_1.NameVahed, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, 
							  F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody
		HAVING      (ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) + SUM([TRANSACTION].TedadVA)) 
							  - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) + SUM([TRANSACTION].TedadKhA)) 
							  + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2) <> 0)	
			
	
	end	
	else if(@pvchAction =''analyze'' or @pvchAction =''print'')
	begin
	
		if(NOT (@pnvaShowkala IS NULL))
		begin
															
			set @HavStatement= @HavStatement + N''
											  HAVING      (ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) + SUM([TRANSACTION].TedadVA)) 
											  - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) + SUM([TRANSACTION].TedadKhA)) 
											  + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2)'' + @pnvaShowkala +'')'';
		end
		
		set @ParmAndstr=''''
  		if(NOT (@pintAid IS NULL))
		begin
		
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
			set @WhrStatementsub= N''(AID = @pintAid)'';
			set @ParmAndstr='' AND ''
			
		end
		
		if(NOT (@pintGoKalaid IS NULL))
		begin
			
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';			
			set @ParmAndstr='' AND ''
			
		end
		
		if(NOT (@pintKalaid IS NULL))
		begin
			
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_NameKala.ID = @pintKalaid)'';
			set @ParmAndstr='' AND ''
			
		end
		
		if (len( @WhrStatement)> 0 )
			set @WhrStatement=''WHERE ''+ @WhrStatement
		
  		 SET @SQLString =
					 N''SELECT     T_NameKala.ID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameVahed.NameVahed AS NameVahed1, T_NameVahed_1.NameVahed AS NameVahed2, 
									  T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, 
									  T_NameKala.UnitPrice6, ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) 
									  + SUM([TRANSACTION].TedadVA)) - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) 
									  + SUM([TRANSACTION].TedadKhA)) + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2) AS Mande, ISNULL(F_AkharinKharid_1.UnitPriceKh, MAX(T_Kala.UnitPrice)) 
									  AS AkharinKharid, MAX(T_Kala.UnitPrice) AS Expr1, F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, 
									  T_NameKala.MaxMogoody
						FROM         T_NameKala INNER JOIN
											  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
											  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
												  (SELECT     NameKalaID, SUM(Mojodi) AS mojoodiEbteda
													 FROM         T_Kala AS T_Kala_1
													 WHERE     (''+@WhrStatementsub + N'')
													 GROUP BY NameKalaID) AS Mojoodiebteda ON T_Kala.NameKalaID = Mojoodiebteda.NameKalaID LEFT OUTER JOIN
											  dbo.F_AkharinKharid() AS F_AkharinKharid_1 ON T_Kala.NameKalaID = F_AkharinKharid_1.KALAID LEFT OUTER JOIN
											  [TRANSACTION] ON T_Kala.NameKalaID = [TRANSACTION].KALAID AND T_Kala.AID = [TRANSACTION].AID AND 
											  T_Kala.GOKID = [TRANSACTION].GOKID LEFT OUTER JOIN
											  T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1 ''+ @WhrStatement +
						N'' GROUP BY T_NameKala.ID, T_NameKala.NameKala1, T_NameKala.KalaNumber, T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameVahed.NameVahed, 
											  T_NameVahed_1.NameVahed, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, 
											  F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody'' + @HavStatement

	
		EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
								@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
								@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
								@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

	end                                                             
return
end



' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Std_0050023008900390022]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [Std_0050023008900390022] (
@pvchAction			    nvarchar(50)=''RepGen'', /* Analyze - Print */
@pintGoid				int=null, 
@pintKolid				int=null, 
@pintHesabid			int=null, 
@pintTHesabid			int=null,
@pintAid				int=null, 
@pintGoKalaid			int=null, 
@pintKalaid				int=null, 
@pnvaStatdate			nvarchar(50)=null,
@pnvaEnddate			nvarchar(50)=null,
@pintFactType			int=null, 
@pnvaFactorStartid		nvarchar(50)=null,
@pnvaFactorEndid		nvarchar(50)=null,
@pnvaSanadStartid		int=null,
@pnvaSanadEndid			int=null,
@pnvaSrt				nvarchar(255)=null,
@pnvaShowkala			nvarchar(255)=null
)        

 AS 
begin

	DECLARE @SQLString  NVARCHAR(4000);        
	DECLARE @WhrStatement  NVARCHAR(2000);        
	DECLARE @WhrStatementsub  NVARCHAR(500);        
	DECLARE @HavStatement  NVARCHAR(2000);        
	DECLARE @ParmDefinition  NVARCHAR(2000);        
	DECLARE @ParmAndstr  NVARCHAR(2000);        
	
	set @WhrStatement  = N'''';        
	set @HavStatement  = N'''';        
	set @ParmAndstr = N'''';        
	set @WhrStatementsub=N''AID <> 0''

	SET @ParmDefinition = 	  N''@pvchAction  nvarchar(50)=''''RepGen'''', 
								@pintGoid				int=null, 
								@pintKolid				int=null, 
								@pintHesabid			int=null, 
								@pintTHesabid			int=null,
								@pintAid				int=null, 
								@pintGoKalaid			int=null, 
								@pintKalaid				int=null, 
								@pnvaStatdate			nvarchar(50)=null,
								@pnvaEnddate			nvarchar(50)=null,
								@pintFactType			int=null, 
								@pnvaFactorStartid		nvarchar(50)=null,
								@pnvaFactorEndid		nvarchar(50)=null,
								@pnvaSanadStartid		int=null,
								@pnvaSanadEndid			int=null,
								@pnvaSrt				nvarchar(255)=null,
								@pnvaShowkala			nvarchar(255)=null'';


	/* Build the SQL string one time. */

    
    /* Specify the parameter format one time. */
	if(@pvchAction =''repgen'')
	begin
		
		SELECT     T_NameKala.ID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameVahed.NameVahed AS NameVahed1, T_NameVahed_1.NameVahed AS NameVahed2, 
							  T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, 
							  T_NameKala.UnitPrice6, ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) 
							  + SUM([TRANSACTION].TedadVA)) - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) 
							  + SUM([TRANSACTION].TedadKhA)) + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2) AS Mande, ISNULL(F_AkharinKharid_1.UnitPriceKh, MAX(T_Kala.UnitPrice)) 
							  AS AkharinKharid, MAX(T_Kala.UnitPrice) AS Expr1, F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, 
							  T_NameKala.MaxMogoody
		FROM         T_NameKala INNER JOIN
							  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
							  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
								  (SELECT     NameKalaID, SUM(Mojodi) AS mojoodiEbteda
									 FROM         T_Kala AS T_Kala_1
									 WHERE     (AID <> 0)
									 GROUP BY NameKalaID) AS Mojoodiebteda ON T_Kala.NameKalaID = Mojoodiebteda.NameKalaID LEFT OUTER JOIN
							  dbo.F_AkharinKharid() AS F_AkharinKharid_1 ON T_Kala.NameKalaID = F_AkharinKharid_1.KALAID LEFT OUTER JOIN
							  [TRANSACTION] ON T_Kala.NameKalaID = [TRANSACTION].KALAID AND T_Kala.AID = [TRANSACTION].AID AND 
							  T_Kala.GOKID = [TRANSACTION].GOKID LEFT OUTER JOIN
							  T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1
		GROUP BY T_NameKala.ID, T_NameKala.NameKala1, T_NameKala.KalaNumber, T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameVahed.NameVahed, 
							  T_NameVahed_1.NameVahed, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, 
							  F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody
		HAVING      (ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) + SUM([TRANSACTION].TedadVA)) 
							  - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) + SUM([TRANSACTION].TedadKhA)) 
							  + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2) <> 0)	
			
	
	end	
	else if(@pvchAction =''analyze'' or @pvchAction =''print'')
	begin
	
		if(NOT (@pnvaShowkala IS NULL))
		begin
															
			set @HavStatement= @HavStatement + N''
											  HAVING      (ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) + SUM([TRANSACTION].TedadVA)) 
											  - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) + SUM([TRANSACTION].TedadKhA)) 
											  + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2)'' + @pnvaShowkala +'')'';
		end
		
		set @ParmAndstr=''''
  		if(NOT (@pintAid IS NULL))
		begin
		
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.AID = @pintAid)'';
			set @WhrStatementsub= N''(AID = @pintAid)'';
			set @ParmAndstr='' AND ''
			
		end
		
		if(NOT (@pintGoKalaid IS NULL))
		begin
			
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_Kala.GOKID = @pintGoKalaid)'';			
			set @ParmAndstr='' AND ''
			
		end
		
		if(NOT (@pintKalaid IS NULL))
		begin
			
			set @WhrStatement= @WhrStatement + @ParmAndstr + N''(T_NameKala.ID = @pintKalaid)'';
			set @ParmAndstr='' AND ''
			
		end
		
		if (len( @WhrStatement)> 0 )
			set @WhrStatement=''WHERE ''+ @WhrStatement
		
  		 SET @SQLString =
					 N''SELECT     T_NameKala.ID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameVahed.NameVahed AS NameVahed1, T_NameVahed_1.NameVahed AS NameVahed2, 
									  T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, 
									  T_NameKala.UnitPrice6, ROUND((SUM([TRANSACTION].TedadKh) + SUM([TRANSACTION].TedadVorood) + SUM([TRANSACTION].TedadBarFo) 
									  + SUM([TRANSACTION].TedadVA)) - (SUM([TRANSACTION].TedadFo) + SUM([TRANSACTION].TedadKhorooj) + SUM([TRANSACTION].TedadBarKh) 
									  + SUM([TRANSACTION].TedadKhA)) + ISNULL(Mojoodiebteda.mojoodiEbteda, 0), 2) AS Mande, ISNULL(F_AkharinKharid_1.UnitPriceKh, MAX(T_Kala.UnitPrice)) 
									  AS AkharinKharid, MAX(T_Kala.UnitPrice) AS Expr1, F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, 
									  T_NameKala.MaxMogoody
						FROM         T_NameKala INNER JOIN
											  T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
											  T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
												  (SELECT     NameKalaID, SUM(Mojodi) AS mojoodiEbteda
													 FROM         T_Kala AS T_Kala_1
													 WHERE     (''+@WhrStatementsub + N'')
													 GROUP BY NameKalaID) AS Mojoodiebteda ON T_Kala.NameKalaID = Mojoodiebteda.NameKalaID LEFT OUTER JOIN
											  dbo.F_AkharinKharid() AS F_AkharinKharid_1 ON T_Kala.NameKalaID = F_AkharinKharid_1.KALAID LEFT OUTER JOIN
											  [TRANSACTION] ON T_Kala.NameKalaID = [TRANSACTION].KALAID AND T_Kala.AID = [TRANSACTION].AID AND 
											  T_Kala.GOKID = [TRANSACTION].GOKID LEFT OUTER JOIN
											  T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1 ''+ @WhrStatement +
						N'' GROUP BY T_NameKala.ID, T_NameKala.NameKala1, T_NameKala.KalaNumber, T_NameKala.BarCode, T_NameKala.UnitPrice1, T_NameVahed.NameVahed, 
											  T_NameVahed_1.NameVahed, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, 
											  F_AkharinKharid_1.UnitPriceKh, Mojoodiebteda.mojoodiEbteda, T_NameKala.Zarib, T_NameKala.MinMogoody, T_NameKala.MaxMogoody'' + @HavStatement

	
		EXECUTE sp_executesql   @SQLString, @ParmDefinition, @pvchAction=@pvchAction ,@pintGoid=@pintGoid,@pintKolid	=@pintKolid, @pintHesabid =@pintHesabid, 
								@pintTHesabid =@pintTHesabid,@pintAid =@pintAid, @pintGoKalaid =@pintGoKalaid, @pintKalaid	=@pintKalaid, @pnvaStatdate =@pnvaStatdate,
								@pnvaEnddate =@pnvaEnddate,@pintFactType =@pintFactType, @pnvaFactorStartid =@pnvaFactorStartid,@pnvaFactorEndid =@pnvaFactorEndid,
								@pnvaSanadStartid =@pnvaSanadStartid,@pnvaSanadEndid =@pnvaSanadEndid ,@pnvaSrt =@pnvaSrt,@pnvaShowkala =@pnvaShowkala;

	end                                                             
return
end



' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Rep_SoodKol]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
    CREATE PROCEDURE [Rep_SoodKol] (@startDate nvarchar(50), @enddate nvarchar(50)) AS SELECT     [TRANSACTION].GOID, [TRANSACTION].KOLID, SUM([TRANSACTION].DARBed) AS SumOfDARBed, SUM([TRANSACTION].PARBes) AS SumOfPARBes,                        SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes) AS Expr1, @startDate AS startDate, @enddate AS enddate, T_KolHesab.NameHesab,                        T_GropHesab.NameGo FROM         [TRANSACTION] INNER JOIN                       T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID INNER JOIN                       T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID INNER JOIN                       T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND (T_SA2.Flag4 <> 1 OR                       T_SA2.Flag4 IS NULL) GROUP BY [TRANSACTION].GOID, [TRANSACTION].KOLID, T_KolHesab.NameHesab, T_GropHesab.NameGo HAVING      ([TRANSACTION].GOID IN (8, 9)) AND (SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes) <> 0) UNION ALL  SELECT     8 AS GOID, - 1000 AS kolid, SUM(Zian) AS Zian, SUM(Sood) AS Sood, 0 AS Expr1, @startDate AS Expr2, @enddate AS Expr3, ''درآمد حاصل از فروش'' AS NameHesab,                        ''درآمدها'' AS Expr4 FROM         dbo.Fun_Sood_Zian_Kala(@startDate, @enddate) Fun_Sood_Zian_Kala 
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_Sandoogh_Name_Kharid]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListKala_Sandoogh_Name_Kharid] (@AID int, @GokalaID int, @Option1 bit, @LinkMasterfield int) AS SELECT     dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1 AS [نام كالا], dbo.T_NameKala.BarCode, dbo.T_Kala.AID, dbo.T_Kala.GOKID,                        ROUND(ISNULL(SUM(F_MojoodiKala.Mojoodi), 0) + ISNULL(SUM(dbo.T_Kala.Mojodi), 0), 2) AS موجودي, @Option1 AS Expr1,                        dbo.T_NameKala.UnitPrice1 AS [قيمت فروش], CASE WHEN (isnull(UnitPriceKh, 0) <> 0) THEN unitpricekh ELSE T_NameKala.FieldN1 END AS [آخرين خريد],                        @LinkMasterfield AS Expr2, @GokalaID AS Expr3, F_AkharinKharid.UnitPriceKh, dbo.T_NameKala.FieldN1 FROM         dbo.T_NameKala INNER JOIN                       dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN                       dbo.F_AkharinKharid() F_AkharinKharid ON dbo.T_NameKala.ID = F_AkharinKharid.KALAID LEFT OUTER JOIN                       dbo.F_MojoodiKala(1, @LinkMasterfield, @Option1) F_MojoodiKala ON dbo.T_NameKala.ID = F_MojoodiKala.KALAID GROUP BY dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, dbo.T_Kala.AID, dbo.T_Kala.GOKID, dbo.T_NameKala.UnitPrice1,                        F_AkharinKharid.UnitPriceKh, dbo.T_NameKala.FieldN1 HAVING      (dbo.T_Kala.AID = @AID) AND (dbo.T_Kala.GOKID = @GokalaID) AND (NOT (@GokalaID IS NULL)) OR                       (dbo.T_Kala.AID = @AID) AND (@GokalaID IS NULL) ORDER BY dbo.T_NameKala.NameKala1  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_Sandoogh_Name_BarKharid]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListKala_Sandoogh_Name_BarKharid] (@AID int, @GokalaID int, @Option1 bit, @LinkMasterfield int) AS SELECT     dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1 AS [نام كالا], dbo.T_NameKala.BarCode, dbo.T_Kala.AID, dbo.T_Kala.GOKID,                        ROUND(ISNULL(SUM(F_MojoodiKala.Mojoodi), 0) + ISNULL(SUM(dbo.T_Kala.Mojodi), 0), 2) AS موجودي, @Option1 AS Expr1,                        dbo.T_NameKala.UnitPrice1 AS [قيمت فروش], CASE WHEN (isnull(UnitPriceKh, 0) <> 0) THEN unitpricekh ELSE T_NameKala.FieldN1 END AS [آخرين خريد],                        @LinkMasterfield AS Expr2, @GokalaID AS Expr3, F_AkharinKharid.UnitPriceKh, dbo.T_NameKala.FieldN1 FROM         dbo.T_NameKala INNER JOIN                       dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN                       dbo.F_AkharinKharid() F_AkharinKharid ON dbo.T_NameKala.ID = F_AkharinKharid.KALAID LEFT OUTER JOIN                       dbo.F_MojoodiKala(3, @LinkMasterfield, @Option1) F_MojoodiKala ON dbo.T_NameKala.ID = F_MojoodiKala.KALAID GROUP BY dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, dbo.T_Kala.AID, dbo.T_Kala.GOKID, dbo.T_NameKala.UnitPrice1,                        F_AkharinKharid.UnitPriceKh, dbo.T_NameKala.FieldN1 HAVING      (dbo.T_Kala.AID = @AID) AND (dbo.T_Kala.GOKID = @GokalaID) AND (NOT (@GokalaID IS NULL)) OR                       (dbo.T_Kala.AID = @AID) AND (@GokalaID IS NULL) ORDER BY dbo.T_NameKala.NameKala1  ' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Q_ListKala_Sandoogh_Name_BarForosooh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'        CREATE PROCEDURE [Q_ListKala_Sandoogh_Name_BarForosooh] (@AID int, @GokalaID int, @Option1 bit, @LinkMasterfield int) AS SELECT     dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1 AS [نام كالا], dbo.T_NameKala.BarCode, dbo.T_Kala.AID, dbo.T_Kala.GOKID,                        ROUND(ISNULL(SUM(F_MojoodiKala.Mojoodi), 0) + ISNULL(SUM(dbo.T_Kala.Mojodi), 0), 2) AS موجودي, @Option1 AS Expr1,                        dbo.T_NameKala.UnitPrice1 AS [قيمت فروش], CASE WHEN (isnull(UnitPriceKh, 0) <> 0) THEN unitpricekh ELSE T_NameKala.FieldN1 END AS [آخرين خريد],                        @LinkMasterfield AS Expr2, @GokalaID AS Expr3, F_AkharinKharid.UnitPriceKh, dbo.T_NameKala.FieldN1 FROM         dbo.T_NameKala INNER JOIN                       dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID LEFT OUTER JOIN                       dbo.F_AkharinKharid() F_AkharinKharid ON dbo.T_NameKala.ID = F_AkharinKharid.KALAID LEFT OUTER JOIN                       dbo.F_MojoodiKala(4, @LinkMasterfield, @Option1) F_MojoodiKala ON dbo.T_NameKala.ID = F_MojoodiKala.KALAID GROUP BY dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, dbo.T_Kala.AID, dbo.T_Kala.GOKID, dbo.T_NameKala.UnitPrice1,                        F_AkharinKharid.UnitPriceKh, dbo.T_NameKala.FieldN1 HAVING      (dbo.T_Kala.AID = @AID) AND (dbo.T_Kala.GOKID = @GokalaID) AND (NOT (@GokalaID IS NULL)) OR                       (dbo.T_Kala.AID = @AID) AND (@GokalaID IS NULL) ORDER BY dbo.T_NameKala.NameKala1  ' 
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_Sandoogh_GroupFilter]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'   CREATE PROCEDURE [CQ_ListKala_Sandoogh_GroupFilter](@AID int, @OperType int, @LinkMasterID int, @GoKID int) AS SELECT     T_Kala.NameKalaID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, T_NameKala.Zarib, T_Kala.AID, T_Kala.GOKID, T_NameKala.KalaNumber AS [كد كالا], 
                      T_NameKala.NameKala1 AS [نام كالا], T_NameVahed.NameVahed AS [واحد ], CONVERT(float, ROUND(ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(T_Kala.Mojodi, 0), 2)) AS [مانده انبار], 
                      T_Anbar.NameAnbar AS [نام انبار], T_NameKala.UnitPrice1 AS [قيمت فروش], CASE WHEN (isnull(F_AkharinKharid.UnitPriceKh, 0) <> 0) 
                      THEN F_AkharinKharid.unitpricekh ELSE T_Kala.UnitPrice END AS [آخرين خريد], T_NameKala.UnitPrice2 AS [قيمت فروش2], T_NameKala.UnitPrice3 AS [قيمت فروش3], 
                      T_NameKala.UnitPrice4 AS [قيمت فروش4], T_NameKala.UnitPrice5 AS [قيمت فروش5], T_NameKala.UnitPrice6 AS [قيمت فروش6], T_NameKala.FieldN1, F_AkharinKharid.UnitPriceKh, @OperType AS Expr4, 
                      @LinkMasterID AS Expr5, CONVERT(float, TBL_TRA.MojoodiTra) AS Expr1
FROM         T_NameVahed INNER JOIN
                      T_NameKala INNER JOIN
                      T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
                      T_Anbar ON T_Kala.AID = T_Anbar.AID ON T_NameVahed.VAHED1 = T_NameKala.VAHED1 LEFT OUTER JOIN
                          (SELECT     KALAID, AID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj) + SUM(TedadBarKh) + SUM(TedadKhA)) 
                                                   AS MojoodiTra
                             FROM         [TRANSACTION]
                             GROUP BY KALAID, AID) AS TBL_TRA ON T_Kala.NameKalaID = TBL_TRA.KALAID AND T_Kala.AID = TBL_TRA.AID LEFT OUTER JOIN
                      dbo.F_AkharinKharid() AS F_AkharinKharid ON T_NameKala.ID = F_AkharinKharid.KALAID
GROUP BY T_Kala.NameKalaID, T_NameKala.NameKala1, T_NameKala.BarCode, T_Kala.AID, T_Kala.GOKID, T_NameKala.UnitPrice1, F_AkharinKharid.UnitPriceKh, T_NameKala.FieldN1, CONVERT(float, 
                      TBL_TRA.MojoodiTra), CONVERT(float, ROUND(ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(T_Kala.Mojodi, 0), 2)), CASE WHEN (isnull(F_AkharinKharid.UnitPriceKh, 0) <> 0) 
                      THEN F_AkharinKharid.unitpricekh ELSE T_Kala.UnitPrice END, T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, 
                      T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, T_NameKala.Zarib
HAVING      (T_Kala.AID = @AID) AND (T_Kala.GOKID = @GOKID)
ORDER BY T_NameKala.NameKala1' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_Sandoogh]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'   CREATE PROCEDURE [CQ_ListKala_Sandoogh](@AID int, @OperType nvarchar(50), @LinkMasterID nvarchar(50)) AS SELECT     T_Kala.NameKalaID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, T_NameKala.Zarib, T_Kala.AID, T_Kala.GOKID, T_NameKala.KalaNumber AS [كد كالا], 
                      T_NameKala.NameKala1 AS [نام كالا], T_NameVahed.NameVahed AS [واحد ], CONVERT(float, ROUND(ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(T_Kala.Mojodi, 0), 2)) AS [مانده انبار], 
                      T_Anbar.NameAnbar AS [نام انبار], T_NameKala.UnitPrice1 AS [قيمت فروش], CASE WHEN (isnull(F_AkharinKharid.UnitPriceKh, 0) <> 0) 
                      THEN F_AkharinKharid.unitpricekh ELSE T_Kala.UnitPrice + isnull(T_NameKala.UnitPrice2, 0) END AS [آخرين خريد], T_NameKala.UnitPrice2 AS [قيمت فروش2], T_NameKala.UnitPrice3 AS [قيمت فروش3], 
                      T_NameKala.UnitPrice4 AS [قيمت فروش4], T_NameKala.UnitPrice5 AS [قيمت فروش5], T_NameKala.UnitPrice6 AS [قيمت فروش6], T_NameKala.FieldN1, F_AkharinKharid.UnitPriceKh, @OperType AS Expr4, 
                      @LinkMasterID AS Expr5, CONVERT(float, TBL_TRA.MojoodiTra) AS Expr1
FROM         T_NameVahed INNER JOIN
                      T_NameKala INNER JOIN
                      T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
                      T_Anbar ON T_Kala.AID = T_Anbar.AID ON T_NameVahed.VAHED1 = T_NameKala.VAHED1 LEFT OUTER JOIN
                          (SELECT     KALAID, AID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj) + SUM(TedadBarKh) + SUM(TedadKhA)) 
                                                   AS MojoodiTra
                             FROM         [TRANSACTION]
                             GROUP BY KALAID, AID) AS TBL_TRA ON T_Kala.NameKalaID = TBL_TRA.KALAID AND T_Kala.AID = TBL_TRA.AID LEFT OUTER JOIN
                      dbo.F_AkharinKharid() AS F_AkharinKharid ON T_NameKala.ID = F_AkharinKharid.KALAID
GROUP BY T_Kala.NameKalaID, T_NameKala.NameKala1, T_NameKala.BarCode, T_Kala.AID, T_Kala.GOKID, T_NameKala.UnitPrice1, F_AkharinKharid.UnitPriceKh, T_NameKala.FieldN1, CONVERT(float, 
                      TBL_TRA.MojoodiTra), CONVERT(float, ROUND(ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(T_Kala.Mojodi, 0), 2)), CASE WHEN (isnull(F_AkharinKharid.UnitPriceKh, 0) <> 0) 
                      THEN F_AkharinKharid.unitpricekh ELSE T_Kala.UnitPrice + isnull(T_NameKala.UnitPrice2, 0) END, T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameVahed.NameVahed, 
                      T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, T_NameKala.Zarib
HAVING      (T_Kala.AID = @AID)
ORDER BY T_NameKala.NameKala1' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_JabejaeiAnbar_GroupFilter]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [CQ_ListKala_JabejaeiAnbar_GroupFilter](@AID int,
@OperType nvarchar(50),
@LinkMasterID nvarchar(50),
@AID2 int
,@GoKid int
)
AS SELECT     T_KALA0.NameKalaID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, T_NameKala.Zarib, @AID2 AS Expr1, T_KALA0.AID, 
                      T_NameKala.KalaNumber AS [كد كالا], T_NameKala.NameKala1 AS [نام كالا], T_NameVahed.NameVahed AS [واحد ], ROUND(CONVERT(Float, 
                      ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(T_KALA0.Mojodi, 0)), 2) AS [مانده انبار], T_Anbar.NameAnbar AS [نام انبار], 
                      T_NameKala.UnitPrice1 AS [قيمت فروش], CASE WHEN (isnull(F_AkharinKharid.UnitPriceKh, 0) <> 0) 
                      THEN F_AkharinKharid.unitpricekh ELSE T_NameKala.FieldN1 END AS [آخرين خريد], T_NameKala.UnitPrice2 AS [قيمت فروش2], 
                      T_NameKala.UnitPrice3 AS [قيمت فروش3], T_NameKala.UnitPrice4 AS [قيمت فروش4], T_NameKala.UnitPrice5 AS [قيمت فروش5], 
                      T_NameKala.UnitPrice6 AS [قيمت فروش6], T_NameKala.FieldN1, F_AkharinKharid.UnitPriceKh, @OperType AS Expr4, @LinkMasterID AS Expr5, 
                      TBL_TRA.MojoodiTra, @GoKid AS Expr2
FROM         (SELECT     KALAID, AID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj) 
                                              + SUM(TedadBarKh) + SUM(TedadKhA)) AS MojoodiTra
                        FROM         [TRANSACTION]
                        GROUP BY KALAID, AID) AS TBL_TRA RIGHT OUTER JOIN
                      T_NameKala INNER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
                      T_Anbar INNER JOIN
                          (SELECT     T_Kala_2.AID, T_Kala_2.NameKalaID, T_Kala_2.GOKID, T_Kala_2.KALANumbr, T_Kala_2.KALAID, T_Kala_2.DateModified, T_Kala_2.DateCreate, 
                      T_Kala_2.VKalaID, T_Kala_2.UnitPrice, T_Kala_2.Unitfo, T_Kala_2.Unit, T_Kala_2.Mojodi, T_Kala_2.Textname, T_Kala_2.FieldN1, T_Kala_2.FieldN2, 
                      T_Kala_2.FieldN3, T_Kala_2.FieldN4, T_Kala_2.FieldT1, T_Kala_2.FieldT2, T_Kala_2.FieldT3, T_Kala_2.FieldT4, T_Kala_2.Flag1, T_Kala_2.Flag2, 
                      T_Kala_2.Flag3, T_Kala_2.Flag4, T_Kala_2.Flag5, T_Kala_2.Flag6, T_Kala_2.Flag8, T_Kala_2.Flag7
FROM         T_Kala AS T_Kala_2 INNER JOIN
                      T_Kala AS T_Kala_1 ON T_Kala_2.NameKalaID = T_Kala_1.NameKalaID
WHERE     (T_Kala_2.AID = @AID) AND (T_Kala_1.AID = @AID2) AND (T_Kala_2.GOKID = @GOKID)) AS T_KALA0 ON T_Anbar.AID = T_KALA0.AID ON 
                      T_NameKala.ID = T_KALA0.NameKalaID ON TBL_TRA.KALAID = T_KALA0.NameKalaID AND TBL_TRA.AID = T_KALA0.AID FULL OUTER JOIN
                      dbo.F_AkharinKharid() AS F_AkharinKharid ON T_NameKala.ID = F_AkharinKharid.KALAID
WHERE     (T_NameKala.Active = 0)
GROUP BY T_KALA0.NameKalaID, T_NameKala.NameKala1, T_NameKala.BarCode, T_KALA0.AID, T_NameKala.UnitPrice1, F_AkharinKharid.UnitPriceKh, 
                      T_NameKala.FieldN1, TBL_TRA.MojoodiTra, ROUND(CONVERT(Float, ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(T_KALA0.Mojodi, 0)), 2), 
                      CASE WHEN (isnull(F_AkharinKharid.UnitPriceKh, 0) <> 0) THEN F_AkharinKharid.unitpricekh ELSE T_NameKala.FieldN1 END, T_Anbar.NameAnbar, 
                      T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, 
                      T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, T_NameKala.Zarib
HAVING      (T_KALA0.AID = @AID)
ORDER BY T_NameKala.NameKala1' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_JabejaeiAnbar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [CQ_ListKala_JabejaeiAnbar](@AID int,
@OperType nvarchar(50),
@LinkMasterID nvarchar(50),
@AID2 int)
AS SELECT     T_KALA0.NameKalaID, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, dbo.T_NameKala.Zarib, 
                      @AID2 AS Expr1, T_KALA0.AID, dbo.T_NameKala.KalaNumber AS [كد كالا], dbo.T_NameKala.NameKala1 AS [نام كالا], 
                      dbo.T_NameVahed.NameVahed AS [واحد ], ROUND(CONVERT(Float, ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(T_KALA0.Mojodi, 0)), 2) AS [مانده انبار], 
                      dbo.T_Anbar.NameAnbar AS [نام انبار], dbo.T_NameKala.UnitPrice1 AS [قيمت فروش], CASE WHEN (isnull(F_AkharinKharid.UnitPriceKh, 0) <> 0) 
                      THEN F_AkharinKharid.unitpricekh ELSE T_NameKala.FieldN1 END AS [آخرين خريد], dbo.T_NameKala.UnitPrice2 AS [قيمت فروش2], 
                      dbo.T_NameKala.UnitPrice3 AS [قيمت فروش3], dbo.T_NameKala.UnitPrice4 AS [قيمت فروش4], dbo.T_NameKala.UnitPrice5 AS [قيمت فروش5], 
                      dbo.T_NameKala.UnitPrice6 AS [قيمت فروش6], dbo.T_NameKala.FieldN1, F_AkharinKharid.UnitPriceKh, @OperType AS Expr4, @LinkMasterID AS Expr5, 
                      TBL_TRA.MojoodiTra
FROM         (SELECT     KALAID, AID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj) 
                                              + SUM(TedadBarKh) + SUM(TedadKhA)) AS MojoodiTra
                        FROM         [TRANSACTION]
                        GROUP BY KALAID, AID) TBL_TRA RIGHT OUTER JOIN
                      dbo.T_NameKala INNER JOIN
                      dbo.T_NameVahed ON dbo.T_NameKala.VAHED1 = dbo.T_NameVahed.VAHED1 INNER JOIN
                      dbo.T_Anbar INNER JOIN
                          (SELECT     T_Kala_2.AID, T_Kala_2.NameKalaID, T_Kala_2.GOKID, T_Kala_2.KALANumbr, T_Kala_2.KALAID, T_Kala_2.DateModified, 
                                                   T_Kala_2.DateCreate, T_Kala_2.VKalaID, T_Kala_2.UnitPrice, T_Kala_2.Unitfo, T_Kala_2.Unit, T_Kala_2.Mojodi, T_Kala_2.Textname, 
                                                   T_Kala_2.FieldN1, T_Kala_2.FieldN2, T_Kala_2.FieldN3, T_Kala_2.FieldN4, T_Kala_2.FieldT1, T_Kala_2.FieldT2, T_Kala_2.FieldT3, 
                                                   T_Kala_2.FieldT4, T_Kala_2.Flag1, T_Kala_2.Flag2, T_Kala_2.Flag3, T_Kala_2.Flag4, T_Kala_2.Flag5, T_Kala_2.Flag6, T_Kala_2.Flag8, 
                                                   T_Kala_2.Flag7
                             FROM         T_Kala AS T_Kala_2 INNER JOIN
                                                   T_Kala AS T_Kala_1 ON T_Kala_2.NameKalaID = T_Kala_1.NameKalaID
                             WHERE     (T_Kala_2.AID = @AID) AND (T_Kala_1.AID = @AID2)) T_KALA0 ON dbo.T_Anbar.AID = T_KALA0.AID ON 
                      dbo.T_NameKala.ID = T_KALA0.NameKalaID ON TBL_TRA.KALAID = T_KALA0.NameKalaID AND TBL_TRA.AID = T_KALA0.AID FULL OUTER JOIN
                      dbo.F_AkharinKharid() F_AkharinKharid ON dbo.T_NameKala.ID = F_AkharinKharid.KALAID
WHERE     (dbo.T_NameKala.Active = 0)
GROUP BY T_KALA0.NameKalaID, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, T_KALA0.AID, dbo.T_NameKala.UnitPrice1, 
                      F_AkharinKharid.UnitPriceKh, dbo.T_NameKala.FieldN1, TBL_TRA.MojoodiTra, ROUND(CONVERT(Float, ISNULL(TBL_TRA.MojoodiTra, 0) 
                      + ISNULL(T_KALA0.Mojodi, 0)), 2), CASE WHEN (isnull(F_AkharinKharid.UnitPriceKh, 0) <> 0) 
                      THEN F_AkharinKharid.unitpricekh ELSE T_NameKala.FieldN1 END, dbo.T_Anbar.NameAnbar, dbo.T_NameKala.KalaNumber, 
                      dbo.T_NameVahed.NameVahed, dbo.T_NameKala.UnitPrice2, dbo.T_NameKala.UnitPrice3, dbo.T_NameKala.UnitPrice4, dbo.T_NameKala.UnitPrice5, 
                      dbo.T_NameKala.UnitPrice6, dbo.T_NameKala.Zarib
HAVING      (T_KALA0.AID = @AID)
ORDER BY dbo.T_NameKala.NameKala1' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_ALL_Rep]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [CQ_ListKala_ALL_Rep](@Aid int,
@OperType int,
@LinkMasterID nvarchar(50))
AS 


SELECT     T_Kala.NameKalaID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, T_NameKala.Zarib, NULL AS Expr2, T_Kala.GOKID, 
                      T_NameKala.KalaNumber AS [كد كالا], T_NameKala.NameKala1 AS [نام كالا], T_NameVahed.NameVahed AS [واحد ], SUM(ROUND(CONVERT(money, 
                      ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(T_Kala.Mojodi, 0)), 2)) AS [موجودي كل], NULL AS [ ], T_NameKala.UnitPrice1 AS [قيمت فروش], 
                      CASE WHEN (isnull(F_AkharinKharid.UnitPriceKh, 0) <> 0) THEN F_AkharinKharid.unitpricekh ELSE T_NameKala.FieldN1 END AS [آخرين خريد], 
                      T_NameKala.UnitPrice2 AS [قيمت فروش2], T_NameKala.UnitPrice3 AS [قيمت فروش3], T_NameKala.UnitPrice4 AS [قيمت فروش4], T_NameKala.UnitPrice5 AS [قيمت فروش5], 
                      T_NameKala.UnitPrice6 AS [قيمت فروش6], T_NameKala.FieldN1, F_AkharinKharid.UnitPriceKh, @OperType AS Expr4, @LinkMasterID AS Expr5, @AID AS Expr1, 
                      TBL_TRA.MojoodiTra
FROM         T_NameVahed INNER JOIN
                      T_NameKala INNER JOIN
                      T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
                      T_Anbar ON T_Kala.AID = T_Anbar.AID ON T_NameVahed.VAHED1 = T_NameKala.VAHED1 LEFT OUTER JOIN
                          (SELECT     KALAID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj) + SUM(TedadBarKh) 
                                                   + SUM(TedadKhA)) AS MojoodiTra
                             FROM         [TRANSACTION]
                             GROUP BY KALAID) AS TBL_TRA ON T_Kala.NameKalaID = TBL_TRA.KALAID LEFT OUTER JOIN
                      dbo.F_AkharinKharid() AS F_AkharinKharid ON T_NameKala.ID = F_AkharinKharid.KALAID
WHERE     (T_NameKala.Active = 0)
GROUP BY T_Kala.NameKalaID, T_NameKala.NameKala1, T_NameKala.BarCode, T_Kala.GOKID, T_NameKala.UnitPrice1, F_AkharinKharid.UnitPriceKh, T_NameKala.FieldN1, 
                      TBL_TRA.MojoodiTra, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, 
                      T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, T_NameKala.Zarib
ORDER BY T_NameKala.NameKala1' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_ALL_GroupFilter_Rep]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [CQ_ListKala_ALL_GroupFilter_Rep](@AID int,
@OperType int,
@LinkMasterID nvarchar(50),
@GoKID int)
AS 
SELECT     T_Kala.NameKalaID, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, T_NameKala.Zarib, NULL AS AID, NULL AS GOKID, 
                      T_NameKala.KalaNumber AS [كد كالا], T_NameKala.NameKala1 AS [نام كالا], T_NameVahed.NameVahed AS [واحد ], SUM(ROUND(CONVERT(money, 
                      ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(T_Kala.Mojodi, 0)), 2)) AS [موجودي كل], T_NameKala.UnitPrice1 AS [قيمت فروش], 
                      CASE WHEN (isnull(F_AkharinKharid.UnitPriceKh, 0) <> 0) THEN F_AkharinKharid.unitpricekh ELSE T_NameKala.FieldN1 END AS [آخرين خريد], 
                      T_NameKala.UnitPrice2 AS [قيمت فروش2], T_NameKala.UnitPrice3 AS [قيمت فروش3], T_NameKala.UnitPrice4 AS [قيمت فروش4], T_NameKala.UnitPrice5 AS [قيمت فروش5], 
                      T_NameKala.UnitPrice6 AS [قيمت فروش6], T_NameKala.FieldN1, F_AkharinKharid.UnitPriceKh, @OperType AS Expr4, @LinkMasterID AS Expr5, @AID AS Expr1, 
                      TBL_TRA.MojoodiTra
FROM         T_NameVahed INNER JOIN
                      T_NameKala INNER JOIN
                      T_Kala ON T_NameKala.ID = T_Kala.NameKalaID INNER JOIN
                      T_Anbar ON T_Kala.AID = T_Anbar.AID ON T_NameVahed.VAHED1 = T_NameKala.VAHED1 LEFT OUTER JOIN
                          (SELECT     KALAID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj) + SUM(TedadBarKh) 
                                                   + SUM(TedadKhA)) AS MojoodiTra
                             FROM         [TRANSACTION]
                             GROUP BY KALAID) AS TBL_TRA ON T_Kala.NameKalaID = TBL_TRA.KALAID LEFT OUTER JOIN
                      dbo.F_AkharinKharid() AS F_AkharinKharid ON T_NameKala.ID = F_AkharinKharid.KALAID
WHERE     (T_NameKala.Active = 0) AND (T_Kala.GOKID = @GOKID)
GROUP BY T_Kala.NameKalaID, T_NameKala.NameKala1, T_NameKala.BarCode, T_NameKala.UnitPrice1, F_AkharinKharid.UnitPriceKh, T_NameKala.FieldN1, 
                      TBL_TRA.MojoodiTra, T_NameKala.KalaNumber, T_NameVahed.NameVahed, T_NameKala.UnitPrice2, T_NameKala.UnitPrice3, T_NameKala.UnitPrice4, 
                      T_NameKala.UnitPrice5, T_NameKala.UnitPrice6, T_NameKala.Zarib
ORDER BY T_NameKala.NameKala1' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_ALL_GroupFilter]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'    CREATE PROCEDURE [CQ_ListKala_ALL_GroupFilter](@AID int, @OperType int, @GoKID int, @LinkMasterID nvarchar(50)) AS SELECT     dbo.T_Kala.NameKalaID, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, dbo.T_NameKala.Zarib,                        dbo.T_Kala.AID, dbo.T_Kala.GOKID, dbo.T_NameKala.KalaNumber AS [كد كالا], dbo.T_NameKala.NameKala1 AS [نام كالا],                        dbo.T_NameVahed.NameVahed AS [واحد ], CONVERT(float, ROUND(ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(dbo.T_Kala.Mojodi, 0), 2)) AS [موجودي كل],                        dbo.T_NameKala.UnitPrice1 AS [قيمت فروش], CASE WHEN (isnull(F_AkharinKharid.UnitPriceKh, 0) <> 0)                        THEN F_AkharinKharid.unitpricekh ELSE T_NameKala.FieldN1 END AS [آخرين خريد], dbo.T_NameKala.UnitPrice2 AS [قيمت فروش2],                        dbo.T_NameKala.UnitPrice3 AS [قيمت فروش3], dbo.T_NameKala.UnitPrice4 AS [قيمت فروش4], dbo.T_NameKala.UnitPrice5 AS [قيمت فروش5],                        dbo.T_NameKala.UnitPrice6 AS [قيمت فروش6], dbo.T_NameKala.FieldN1, F_AkharinKharid.UnitPriceKh, @OperType AS Expr4, @LinkMasterID AS Expr5,                        @AID AS Expr1, TBL_TRA.MojoodiTra FROM         dbo.T_NameVahed INNER JOIN                       dbo.T_NameKala INNER JOIN                       dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID INNER JOIN                       dbo.T_Anbar ON dbo.T_Kala.AID = dbo.T_Anbar.AID ON dbo.T_NameVahed.VAHED1 = dbo.T_NameKala.VAHED1 LEFT OUTER JOIN                           (SELECT     KALAID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj)                                                     + SUM(TedadBarKh) + SUM(TedadKhA)) AS MojoodiTra                              FROM         dbo.[TRANSACTION]                              GROUP BY KALAID) TBL_TRA ON dbo.T_Kala.NameKalaID = TBL_TRA.KALAID LEFT OUTER JOIN                       dbo.F_AkharinKharid() F_AkharinKharid ON dbo.T_NameKala.ID = F_AkharinKharid.KALAID GROUP BY dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, dbo.T_Kala.GOKID, dbo.T_NameKala.UnitPrice1,                        F_AkharinKharid.UnitPriceKh, dbo.T_NameKala.FieldN1, TBL_TRA.MojoodiTra, dbo.T_NameKala.KalaNumber, dbo.T_NameVahed.NameVahed,                        dbo.T_NameKala.UnitPrice2, dbo.T_NameKala.UnitPrice3, dbo.T_NameKala.UnitPrice4, dbo.T_NameKala.UnitPrice5, dbo.T_NameKala.UnitPrice6,                        CONVERT(float, ROUND(ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(dbo.T_Kala.Mojodi, 0), 2)), dbo.T_NameKala.Zarib, dbo.T_Kala.AID HAVING      (dbo.T_Kala.GOKID = @GOKID) ORDER BY dbo.T_NameKala.NameKala1' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[CQ_ListKala_ALL]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'   CREATE PROCEDURE [CQ_ListKala_ALL](@Aid int, @OperType int, @LinkMasterID nvarchar(50)) AS SELECT     dbo.T_Kala.NameKalaID, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, dbo.T_NameKala.Zarib, NULL                        AS Expr2, dbo.T_Kala.GOKID, dbo.T_NameKala.KalaNumber AS [كد كالا], dbo.T_NameKala.NameKala1 AS [نام كالا],                        dbo.T_NameVahed.NameVahed AS [واحد ], CONVERT(float, ROUND(ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(dbo.T_Kala.Mojodi, 0), 2))                        AS [موجودي كل], NULL AS [ ], dbo.T_NameKala.UnitPrice1 AS [قيمت فروش], CASE WHEN (isnull(F_AkharinKharid.UnitPriceKh, 0) <> 0)                        THEN F_AkharinKharid.unitpricekh ELSE T_NameKala.FieldN1 END AS [آخرين خريد], dbo.T_NameKala.UnitPrice2 AS [قيمت فروش2],                        dbo.T_NameKala.UnitPrice3 AS [قيمت فروش3], dbo.T_NameKala.UnitPrice4 AS [قيمت فروش4], dbo.T_NameKala.UnitPrice5 AS [قيمت فروش5],                        dbo.T_NameKala.UnitPrice6 AS [قيمت فروش6], dbo.T_NameKala.FieldN1, F_AkharinKharid.UnitPriceKh, @OperType AS Expr4, @LinkMasterID AS Expr5,                        @AID AS Expr1, TBL_TRA.MojoodiTra FROM         dbo.T_NameVahed INNER JOIN                       dbo.T_NameKala INNER JOIN                       dbo.T_Kala ON dbo.T_NameKala.ID = dbo.T_Kala.NameKalaID INNER JOIN                       dbo.T_Anbar ON dbo.T_Kala.AID = dbo.T_Anbar.AID ON dbo.T_NameVahed.VAHED1 = dbo.T_NameKala.VAHED1 LEFT OUTER JOIN                           (SELECT     KALAID, (SUM(TedadKh) + SUM(TedadVorood) + SUM(TedadBarFo) + SUM(TedadVA)) - (SUM(TedadFo) + SUM(TedadKhorooj)                                                     + SUM(TedadBarKh) + SUM(TedadKhA)) AS MojoodiTra                              FROM         dbo.[TRANSACTION]                              GROUP BY KALAID) TBL_TRA ON dbo.T_Kala.NameKalaID = TBL_TRA.KALAID LEFT OUTER JOIN                       dbo.F_AkharinKharid() F_AkharinKharid ON dbo.T_NameKala.ID = F_AkharinKharid.KALAID GROUP BY dbo.T_Kala.NameKalaID, dbo.T_NameKala.NameKala1, dbo.T_NameKala.BarCode, dbo.T_Kala.GOKID, dbo.T_NameKala.UnitPrice1,                        F_AkharinKharid.UnitPriceKh, dbo.T_NameKala.FieldN1, TBL_TRA.MojoodiTra, dbo.T_NameKala.KalaNumber, dbo.T_NameVahed.NameVahed,                        dbo.T_NameKala.UnitPrice2, dbo.T_NameKala.UnitPrice3, dbo.T_NameKala.UnitPrice4, dbo.T_NameKala.UnitPrice5, dbo.T_NameKala.UnitPrice6,                        CONVERT(float, ROUND(ISNULL(TBL_TRA.MojoodiTra, 0) + ISNULL(dbo.T_Kala.Mojodi, 0), 2)), dbo.T_NameKala.Zarib ORDER BY dbo.T_NameKala.NameKala1' 
END
GO


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_Table1_Jabejaei_KolKeriehSF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1]
GO



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

     CREATE PROCEDURE [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1] (  
     @USERID int, @date1 NVARCHAR(10), @date2 NVARCHAR(10), @Goid INT,@Kolid INT,@Hesabid INT,@THesabid INT    )              AS        
     BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	   
     DECLARE @WhrStatement  NVARCHAR(2000);         	   
     DECLARE @ParmDefinition  NVARCHAR(2000);         	   
     DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
     set @WhrStatement  = N'';         	   
     set @ParmAndstr = N'';                  	   
     SET @ParmDefinition = N'@USERID int=null, 							
							@date1 NVARCHAR(10)=null, 							
							@date2 NVARCHAR(10)=null, 							
							@Goid INT=null, 							
							@Kolid INT=null, 							
							@Hesabid INT=null, 							
							@THesabid INT=null';            	  		
SET @SQLString =' SELECT     Table1.ID1, Table1.ID1Number, SUM([TRANSACTION].TedadKhA) AS TedadKhA, [TRANSACTION].MabVahedHazine, Table1.DateID1, 
                      SUM([TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine) AS MabHazine, dbo.HesabName(Table1.KOLID, T_Anbar.hesabid, Table1.THESABID) AS NameDriver, [TRANSACTION].GOKID, 
                      T_NameGoKala.NameGoKala1, T_NameGoKala.NameGoKalanumbr, T_NameGoKala.ID, CONVERT(nvarchar(255), [TRANSACTION].MabVahedHazine) + T_NameGoKala.NameGoKala1 AS Grp
FROM         Table1 INNER JOIN
                      [TRANSACTION] ON Table1.ID1 = [TRANSACTION].ID1 INNER JOIN
                      T_Anbar ON Table1.AID1 = T_Anbar.AID INNER JOIN
                      T_Anbar AS T_Anbar_1 ON Table1.AID2 = T_Anbar_1.AID INNER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID INNER JOIN
                      T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID
WHERE     ([TRANSACTION].TedadKhA > 0) AND (Table1.DateID1 BETWEEN @Date1 AND @Date2) AND (Table1.KOLID = @KOLID) AND (Table1.HesabID = @HesabID) AND (Table1.THESABID = @THESABID)
GROUP BY Table1.ID1, Table1.ID1Number, [TRANSACTION].MabVahedHazine, Table1.DateID1, dbo.HesabName(Table1.KOLID, T_Anbar.hesabid, Table1.THESABID), [TRANSACTION].GOKID, 
                      T_NameGoKala.NameGoKala1, T_NameGoKala.NameGoKalanumbr, T_NameGoKala.ID, CONVERT(nvarchar(255), [TRANSACTION].MabVahedHazine) + T_NameGoKala.NameGoKala1
HAVING      (SUM([TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine) > 0)' 	   						  set @SQLString=@SQLString 	  EXECUTE sp_executesql   @SQLString, @ParmDefinition,@USERID=@USERID ,@date1=@date1,@date2=@date2,@Goid=@Goid,@Kolid=@Kolid,@Hesabid=@Hesabid,@THesabid=@THesabid;  		 return END   
GO







IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_Table1_JabejaeiSF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_Table1_JabejaeiSF1]
GO



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[Rep_Table1_JabejaeiSF1] ( 
@USERID int,
@IDKeyFact int
  )           
  AS     
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';                  	
  SET @ParmDefinition = N'@USERID int=null, 								
						@IDKeyFact int=null';            	
	 
		SET @SQLString ='
SELECT     Table1.ID1, Table1.ID1Number, T_Anbar.AIDNumber AS AIDNumberMabda, T_Anbar.NameAnbar AS NameAnbarMabda, T_Anbar_1.AIDNumber AS AIDNumberMagh, 
                      T_Anbar_1.NameAnbar AS NameAnbarMagh, Table1.AID1 AS AID1Mabda, Table1.AID2 AS AID2Magh, [TRANSACTION].TedadKhA, [TRANSACTION].MabVahedHazine, 
                      [TRANSACTION].Sharh AS SharhRadif, Table1.DateID1, T_NameKala.NameKala1, Table1.Sharh, [TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine AS MabHazine
FROM         Table1 INNER JOIN
                      [TRANSACTION] ON Table1.ID1 = [TRANSACTION].ID1 INNER JOIN
                      T_Anbar ON Table1.AID1 = T_Anbar.AID INNER JOIN
                      T_Anbar AS T_Anbar_1 ON Table1.AID2 = T_Anbar_1.AID INNER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID
WHERE     ([TRANSACTION].TedadKhA > 0) AND (Table1.ID1 = @IDKeyFact)' 	
 
						

set @SQLString=@SQLString 	
     			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@USERID=@USERID ,@IDKeyFact=@IDKeyFact;  		
return END 


GO





IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1]
GO



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  CREATE PROCEDURE [dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1] AS  
   
  
  
SELECT      Table1.ID1, Table1.ID1Number, Table1.NumberKH, Table1.NumSID2, Table1.VSabt, Table1.PrimarySaid2, Table1.Dateeng, Table1.DateCreate, Table1.DateModified, 
                      Table1.HesabID, Table1.GOID, Table1.KOLID, Table1.MOINID, Table1.THESABID, Table1.THESABIDAmel, Table1.TextName, Table1.TextName1, Table1.TextNameT, Table1.DateID1, Table1.Takhfif, 
                      Table1.TextShSA, Table1.Sharh, Table1.FieldN1, Table1.FieldN2, Table1.FieldN3, Table1.FieldN4, Table1.FieldT1, Table1.FieldT2, Table1.FieldT3, Table1.FieldT4, Table1.Flag1, Table1.Flag2, 
                      Table1.Flag3, Table1.Flag4, Table1.Flag5, Table1.Flag6, Table1.Flag7, Table1.Flag8, Table1.UserID, Table1.Shift, Table1.FlgSabtFact, Table1.SalaTypeid, Table1.Maliyat, Table1.MablaghKol, 
                      Table1.Avarez, Table1.Ezafat, Table1.Poorsant, Table1.userCreate, Table1.userModify, Table1.dateSettlement, Table1.NameAID1, Table1.NameAID2, Table1.NameKala, Table1.NameGorup1, 
                      Table1.NameGorup2, Table1.AID1, Table1.AID2, T_SA2.SAID2Number, T_SA2.Sharh AS Expr1, T_Anbar_1.NameAnbar AS NameAnbar1, T_Anbar.NameAnbar AS NameAnbar2, 
                      dbo.HesabName(Table1.KOLID, Table1.HesabID, Table1.THESABID) AS Namehesab
FROM         T_Anbar AS T_Anbar_1 RIGHT OUTER JOIN
                      T_SA2 RIGHT OUTER JOIN
                      Table1 LEFT OUTER JOIN
                      T_Anbar ON Table1.AID2 = T_Anbar.AID ON T_SA2.SAID2 = Table1.PrimarySaid2 ON T_Anbar_1.AID = Table1.AID1
GO



/****** Object:  StoredProcedure [dbo].[SabtKala_Table1_Insert]    Script Date: 09/27/2017 13:20:07 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SabtKala_Table1_Insert]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SabtKala_Table1_Insert]
GO


/****** Object:  StoredProcedure [dbo].[SabtKala_Table1_Insert]    Script Date: 09/27/2017 13:20:07 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[SabtKala_Table1_Insert]
		
		@AID2 INT,
		@PrimeryOperationID INT,
		@PrimarySAnadID INT,
		@SecondrySanadid INT,
		@LinkTosub NVARCHAR(255),
		@TypeOfFact INT,
		@SecondryOperationCode NVARCHAR(255),
		@Date1 NVARCHAR(20),
		@DateMiladi NVARCHAR(20)

AS

BEGIN

SET NOCOUNT ON;  

DECLARE @FieldN3 DECIMAL(19,4)
DECLARE @DateNow DATETIME


set @DateNow=GETDATE()



/*  ثبت خروج از انبار */
INSERT INTO [TRANSACTION]
                      (Zarib, FieldN7, Maliatkala, Avarez, radif, Tedad2, ID1, FieldN1, FieldN2, SAID2Number, OperationCode, FieldN6, AID, GOKID, KALAID, TextAIDKharid, Sharh, TedadKhA, MabVahedHazine, FieldN3, 
                      Date1, Dateeng, DARBed, PARBes, VKALAID, DateCreate, DateModified, UnitPriceKho)
SELECT     TRANSACTION2.Zarib AS Expr1, TRANSACTION2.FieldN7 AS Expr2, TRANSACTION2.FieldN1 AS Expr3, TRANSACTION2.FieldN2 AS Expr4, TRANSACTION2.radif AS Expr5, 
                      TRANSACTION2.Tedad2 AS Expr6, @PrimeryOperationID AS ex1, @PrimeryOperationID AS ex2, @PrimarySAnadID AS ex3, @SecondrySanadid AS ex4, @SecondryOperationCode AS ex5, 
                      TRANSACTION2.FieldN6 AS Expr7, TRANSACTION2.AID AS Expr8, TRANSACTION2.GOKID AS Expr9, TRANSACTION2.KALAID AS Expr10, TRANSACTION2.TextAIDKharid AS Expr11, 
                      TRANSACTION2.Sharh AS Expr12, TRANSACTION2.Tedad AS Expr13, TRANSACTION2.UnitPrice AS Expr14, Fun_SubAkharinKharid_1.MaxUnitPriceKh * TRANSACTION2.Tedad AS ex6, 
                      @Date1 AS ex7, @DateMiladi AS ex12, NULL AS Ex13, NULL AS Ex14, 1 AS ex15, @DateNow AS fsf, @DateNow AS dfghf, Fun_SubAkharinKharid_1.MaxUnitPriceKh
FROM         TRANSACTION2 LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(1) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.KALAID = Fun_SubAkharinKharid_1.KALAID
WHERE     (NOT (TRANSACTION2.KALAID IS NULL)) AND (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.TRAID IS NULL)
ORDER BY Expr5


/* ثبت ورود به انبار*/
DECLARE @AID INT, @GOKID  INT,@KALAID INT

DECLARE KalaID_Cursor CURSOR FOR 

SELECT     AID, GOKID, KALAID
FROM         TRANSACTION2
WHERE     (Date1 = @LinkTosub) AND (NOT (KALAID IS NULL)) AND (TRAID IS NULL)
GROUP BY AID, GOKID, KALAID


OPEN KalaID_Cursor

	FETCH NEXT FROM KalaID_Cursor   
	INTO @AID, @GOKID  ,@KALAID

	WHILE @@FETCH_STATUS = 0  
	BEGIN 

			DECLARE @KALAIDInAID2 int
										
			SELECT    @KALAIDInAID2= COUNT(*)  
			FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1
			WHERE     (AID = @AID2) AND (ID = @KALAID)

			IF (isnull(@KALAIDInAID2,0)=0)
				INSERT INTO T_Kala
								(NameKalaID, AID, GOKID, DateCreate, DateModified, VKalaID)
							VALUES			(@KalaID,@AID2,@GOKID, GETDATE(), GETDATE(), 1)
								
						
INSERT INTO [TRANSACTION]
                      (Zarib, FieldN7, Maliatkala, Avarez, radif, Tedad2, ID1, FieldN1, FieldN2, SAID2Number, OperationCode, FieldN6, AID, GOKID, KALAID, TextAIDKharid, Sharh, TedadVA, MabVahedHazine, FieldN3, 
                      Date1, Dateeng, DARBed, PARBes, VKALAID, DateCreate, DateModified, UnitPriceVo)
SELECT     TRANSACTION2.Zarib, TRANSACTION2.FieldN7, TRANSACTION2.FieldN1, TRANSACTION2.FieldN2, TRANSACTION2.radif, TRANSACTION2.Tedad2, @PrimeryOperationID AS ex1, 
                      @PrimeryOperationID AS ex2, @PrimarySAnadID AS ex3, @SecondrySanadid AS ex4, @SecondryOperationCode AS ex5, TRANSACTION2.FieldN6, @AID2 AS Expr1, TRANSACTION2.GOKID, 
                      @KALAID AS Expr2, TRANSACTION2.TextAIDKharid, TRANSACTION2.Sharh, TRANSACTION2.Tedad, TRANSACTION2.UnitPrice, 
                      Fun_SubAkharinKharid_1.MaxUnitPriceKh * TRANSACTION2.Tedad AS ex6, @Date1 AS ex7, @DateMiladi AS ex12, NULL AS Ex13, NULL AS Ex14, 1 AS ex15, @DateNow AS fsf, @DateNow AS dfghf, 
                      Fun_SubAkharinKharid_1.MaxUnitPriceKh
FROM         TRANSACTION2 LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(1) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.KALAID = Fun_SubAkharinKharid_1.KALAID
WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.AID = @AID) AND (TRANSACTION2.KALAID = @KALAID) AND (TRANSACTION2.TRAID IS NULL)
ORDER BY TRANSACTION2.radif


				FETCH NEXT FROM KalaID_Cursor   
				INTO @AID, @GOKID  ,@KALAID
	 	
	END   
	CLOSE KalaID_Cursor;  
	DEALLOCATE KalaID_Cursor; 
	
END







GO



/****** Object:  StoredProcedure [dbo].[EDitKala_Table1_Insert]    Script Date: 09/27/2017 13:19:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EDitKala_Table1_Insert]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[EDitKala_Table1_Insert]
GO


/****** Object:  StoredProcedure [dbo].[EDitKala_Table1_Insert]    Script Date: 09/27/2017 13:19:56 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[EDitKala_Table1_Insert]
		@LinkMasterField INT,
		@PrimeryOperationID INT,
		@PrimarySAnadID INT,
		@SecondrySanadid INT,
		@LinkTosub NVARCHAR(255),
		@SecondryOperationCode NVARCHAR(255),
		@Date1 NVARCHAR(20),
		@DateMiladi NVARCHAR(20)

AS

BEGIN

SET NOCOUNT ON;  

DECLARE @DateNow datetime

set @DateNow=GETDATE()



/* حذف کالاهايي حذف شده*/

DELETE FROM [TRANSACTION]
WHERE     (ID1 = @LinkMasterField) AND (NOT (KALAID IN
                          (SELECT     KALAID
FROM         TRANSACTION2
WHERE     (Date1 = @LinkTosub) AND (TRAID > 0))))

/* بروزرساني خروجي کالا*/
UPDATE    [TRANSACTION]
SET              Tedad2 = TRANSACTION2.Tedad2, ID1 = @PrimeryOperationID, FieldN1 = @PrimeryOperationID, FieldN2 = @PrimarySAnadID, SAID2Number = @SecondrySanadid, 
                      OperationCode = @SecondryOperationCode, Sharh = TRANSACTION2.Sharh, TedadKhA = TRANSACTION2.Tedad, FieldN3 = TRANSACTION2.Tedad * Fun_SubAkharinKharid_1.MaxUnitPriceKh, 
                      UnitPriceKho = Fun_SubAkharinKharid_1.MaxUnitPriceKh, Date1 = @Date1,  
                      Dateeng = @DateMiladi, VKALAID = 1, DateModified = @DateNow
FROM         TRANSACTION2 INNER JOIN
                      [TRANSACTION] ON TRANSACTION2.KALAID = [TRANSACTION].KALAID LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(1) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.KALAID = Fun_SubAkharinKharid_1.KALAID
WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.TRAID > 0) AND ([TRANSACTION].TedadKhA > 0)

/* بروزرساني ورودي انبار*/

UPDATE    [TRANSACTION]
SET              Tedad2 = TRANSACTION2.Tedad2, ID1 = @PrimeryOperationID, FieldN1 = @PrimeryOperationID, FieldN2 = @PrimarySAnadID, SAID2Number = @SecondrySanadid, 
                      OperationCode = @SecondryOperationCode, Sharh = TRANSACTION2.Sharh, TedadVA = TRANSACTION2.Tedad, FieldN3 = TRANSACTION2.Tedad * Fun_SubAkharinKharid_1.MaxUnitPriceKh, 
                      UnitPriceVo = Fun_SubAkharinKharid_1.MaxUnitPriceKh, Date1 = @Date1, 
                      Dateeng = @DateMiladi, VKALAID = 1, DateModified = @DateNow
FROM         TRANSACTION2 INNER JOIN
                      [TRANSACTION] ON TRANSACTION2.KALAID = [TRANSACTION].KALAID LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(1) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.KALAID = Fun_SubAkharinKharid_1.KALAID
WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.TRAID > 0) AND ([TRANSACTION].TedadVA > 0)

	
END

                

GO





/****** Object:  UserDefinedFunction [dbo].[Fun_CQ_ListKala_Sandoogh_Analyz]    Script Date: 01/10/2018 12:39:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Fun_CQ_ListKala_Sandoogh_Analyz]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[Fun_CQ_ListKala_Sandoogh_Analyz]
GO


/****** Object:  UserDefinedFunction [dbo].[Fun_CQ_ListKala_Sandoogh_Analyz]    Script Date: 01/10/2018 12:39:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  /* =============================================*/ 
  
  /*-- Author:		<Author,,Name>*/ 
  /*-- Create date: 2015/07/04*/ /*-- Description:	اطلاعات تراکنش کالا*/ 
  
  /*-- =============================================*/  
  CREATE FUNCTION  [dbo].[Fun_CQ_ListKala_Sandoogh_Analyz]() RETURNS TABLE  AS 
  RETURN  (
 
SELECT     KALAID, AID, GOKID, Tedad, MaxUnitPriceKh, LinkKalaAID
FROM         View_Fun_CQ_ListKala_Sandoogh_Analyz

 )


GO







/****** Object:  StoredProcedure [dbo].[Rep_Table1_JabejaeiSF1]    Script Date: 10/02/2017 10:25:21 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_Table1_JabejaeiSF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_Table1_JabejaeiSF1]
GO


/****** Object:  StoredProcedure [dbo].[Rep_Table1_JabejaeiSF1]    Script Date: 10/02/2017 10:25:21 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

     CREATE PROCEDURE [dbo].[Rep_Table1_JabejaeiSF1] (  @USERID int, @IDKeyFact int   )              AS        BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	   DECLARE @WhrStatement  NVARCHAR(2000);         	   DECLARE @ParmDefinition  NVARCHAR(2000);         	   DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   set @WhrStatement  = N'';         	   set @ParmAndstr = N'';                  	   SET @ParmDefinition = N'@USERID int=null, 								 						@IDKeyFact int=null';            	 	  		SET @SQLString =' SELECT     Table1.ID1, Table1.ID1Number, T_Anbar.AIDNumber AS AIDNumberMabda, T_Anbar.NameAnbar AS NameAnbarMabda, T_Anbar_1.AIDNumber AS AIDNumberMagh,                        T_Anbar_1.NameAnbar AS NameAnbarMagh, Table1.AID1 AS AID1Mabda, Table1.AID2 AS AID2Magh, [TRANSACTION].TedadKhA, [TRANSACTION].MabVahedHazine,                        [TRANSACTION].Sharh AS SharhRadif, Table1.DateID1, T_NameKala.NameKala1, Table1.Sharh, [TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine AS MabHazine FROM         Table1 INNER JOIN                       [TRANSACTION] ON Table1.ID1 = [TRANSACTION].ID1 INNER JOIN                       T_Anbar ON Table1.AID1 = T_Anbar.AID INNER JOIN                       T_Anbar AS T_Anbar_1 ON Table1.AID2 = T_Anbar_1.AID INNER JOIN                       T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID WHERE     ([TRANSACTION].TedadKhA > 0) AND (Table1.ID1 = @IDKeyFact)' 	   						  set @SQLString=@SQLString 	      			 EXECUTE sp_executesql   @SQLString, @ParmDefinition,@USERID=@USERID ,@IDKeyFact=@IDKeyFact;  		 return END   
GO



/****** Object:  StoredProcedure [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1]    Script Date: 10/02/2017 10:25:15 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_Table1_Jabejaei_KolKeriehSF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1]
GO


/****** Object:  StoredProcedure [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1]    Script Date: 10/02/2017 10:25:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

       CREATE PROCEDURE [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1] (        @USERID int, @date1 NVARCHAR(10), @date2 NVARCHAR(10), @Goid INT,@Kolid INT,@Hesabid INT,@THesabid INT    )              AS              BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	         DECLARE @WhrStatement  NVARCHAR(2000);         	         DECLARE @ParmDefinition  NVARCHAR(2000);         	         DECLARE @ParmAndstr  NVARCHAR(2000);         	 	         set @WhrStatement  = N'';         	         set @ParmAndstr = N'';                  	         SET @ParmDefinition = N'@USERID int=null, 							 							@date1 NVARCHAR(10)=null, 							 							@date2 NVARCHAR(10)=null, 							 							@Goid INT=null, 							 							@Kolid INT=null, 							 							@Hesabid INT=null, 							 							@THesabid INT=null';            	  		 SET @SQLString =' SELECT     Table1.ID1, Table1.ID1Number, SUM([TRANSACTION].TedadKhA) AS TedadKhA, [TRANSACTION].MabVahedHazine, Table1.DateID1,                        SUM([TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine) AS MabHazine, dbo.HesabName(Table1.KOLID, T_Anbar.hesabid, Table1.THESABID) AS NameDriver, [TRANSACTION].GOKID,                        T_NameGoKala.NameGoKala1, T_NameGoKala.NameGoKalanumbr, T_NameGoKala.ID, CONVERT(nvarchar(255), [TRANSACTION].MabVahedHazine) + T_NameGoKala.NameGoKala1 AS Grp FROM         Table1 INNER JOIN                       [TRANSACTION] ON Table1.ID1 = [TRANSACTION].ID1 INNER JOIN                       T_Anbar ON Table1.AID1 = T_Anbar.AID INNER JOIN                       T_Anbar AS T_Anbar_1 ON Table1.AID2 = T_Anbar_1.AID INNER JOIN                       T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID INNER JOIN                       T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID WHERE     ([TRANSACTION].TedadKhA > 0) AND (Table1.DateID1 BETWEEN @Date1 AND @Date2) AND (Table1.KOLID = @KOLID) AND (Table1.HesabID = @HesabID) AND (Table1.THESABID = @THESABID) GROUP BY Table1.ID1, Table1.ID1Number, [TRANSACTION].MabVahedHazine, Table1.DateID1, dbo.HesabName(Table1.KOLID, T_Anbar.hesabid, Table1.THESABID), [TRANSACTION].GOKID,                        T_NameGoKala.NameGoKala1, T_NameGoKala.NameGoKalanumbr, T_NameGoKala.ID, CONVERT(nvarchar(255), [TRANSACTION].MabVahedHazine) + T_NameGoKala.NameGoKala1 HAVING      (SUM([TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine) > 0)' 	   						  set @SQLString=@SQLString 	  EXECUTE sp_executesql   @SQLString, @ParmDefinition,@USERID=@USERID ,@date1=@date1,@date2=@date2,@Goid=@Goid,@Kolid=@Kolid,@Hesabid=@Hesabid,@THesabid=@THesabid;  		 return END   
GO




/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1]    Script Date: 10/02/2017 10:25:11 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1]    Script Date: 10/02/2017 10:25:11 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

      CREATE PROCEDURE [dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1] AS                
      
            SELECT      Table1.ID1, CONVERT(float, Table1.ID1Number) AS ID1Number, Table1.NumberKH, Table1.NumSID2, Table1.VSabt, Table1.PrimarySaid2, Table1.Dateeng, Table1.DateCreate, 
                      Table1.DateModified, Table1.HesabID, Table1.GOID, Table1.KOLID, Table1.MOINID, Table1.THESABID, Table1.THESABIDAmel, Table1.TextName, Table1.TextName1, Table1.TextNameT, 
                      Table1.DateID1, Table1.Takhfif, Table1.TextShSA, Table1.Sharh, Table1.FieldN1, Table1.FieldN2, Table1.FieldN3, Table1.FieldN4, Table1.FieldT1, Table1.FieldT2, Table1.FieldT3, Table1.FieldT4, 
                      Table1.Flag1, Table1.Flag2, Table1.Flag3, Table1.Flag4, Table1.Flag5, Table1.Flag6, Table1.Flag7, Table1.Flag8, Table1.UserID, Table1.Shift, Table1.FlgSabtFact, Table1.SalaTypeid, 
                      Table1.Maliyat, Table1.MablaghKol, Table1.Avarez, Table1.Ezafat, Table1.Poorsant, Table1.userCreate, Table1.userModify, Table1.dateSettlement, Table1.NameAID1, Table1.NameAID2, 
                      Table1.NameKala, Table1.NameGorup1, Table1.NameGorup2, Table1.AID1, Table1.AID2, T_SA2.SAID2Number, T_SA2.Sharh AS Expr1, T_Anbar_1.NameAnbar AS NameAnbar1, 
                      T_Anbar.NameAnbar AS NameAnbar2, dbo.HesabName(Table1.KOLID, Table1.HesabID, Table1.THESABID) AS Namehesab
FROM         T_Anbar AS T_Anbar_1 RIGHT OUTER JOIN
                      T_SA2 RIGHT OUTER JOIN
                      Table1 LEFT OUTER JOIN
                      T_Anbar ON Table1.AID2 = T_Anbar.AID ON T_SA2.SAID2 = Table1.PrimarySaid2 ON T_Anbar_1.AID = Table1.AID1
ORDER BY Table1.DateID1 DESC, ID1Number DESC
GO








/****** Object:  StoredProcedure [dbo].[QSubPFrm_Hesab_ExistTafsiliHesab_SF3]    Script Date: 10/21/2017 16:58:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Hesab_ExistTafsiliHesab_SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Hesab_ExistTafsiliHesab_SF3]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Hesab_ExistTafsiliHesab_SF3]    Script Date: 10/21/2017 16:58:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





Create  PROCEDURE [dbo].[QSubPFrm_Hesab_ExistTafsiliHesab_SF3]

		@Userid INT,
		@HesabID INT,
		@THesabID INT=0 OUTPUT
AS

BEGIN


SELECT     @THesabID=THESABID
FROM         T_T_Heasb
WHERE     (HESABID = @HESABID)

							  
END







GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_KOLHesab_ExistMoinHesab_SF3]    Script Date: 10/21/2017 16:58:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_KOLHesab_ExistMoinHesab_SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_KOLHesab_ExistMoinHesab_SF3]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_KOLHesab_ExistMoinHesab_SF3]    Script Date: 10/21/2017 16:58:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





Create  PROCEDURE [dbo].[QSubPFrm_KOLHesab_ExistMoinHesab_SF3]

		@Userid INT,
		@KOLID INT,
		@HesabID INT=0 OUTPUT
AS

BEGIN

SELECT     @HesabID=HESABID
FROM         T_Heasb
WHERE     (KolID = @KolID)


							  
END







GO





/****** Object:  StoredProcedure [dbo].[QSubPFrm_TafsiliHesab_ExistTafsiliSath2_SF3]    Script Date: 10/21/2017 16:58:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_TafsiliHesab_ExistTafsiliSath2_SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_TafsiliHesab_ExistTafsiliSath2_SF3]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_TafsiliHesab_ExistTafsiliSath2_SF3]    Script Date: 10/21/2017 16:58:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





create  PROCEDURE [dbo].[QSubPFrm_TafsiliHesab_ExistTafsiliSath2_SF3]

		@Userid INT,
		@THesabID INT,
		@THesabIDSath2 INT=0 OUTPUT
AS

BEGIN


SELECT     @THesabIDSath2=THesabIDSath2
FROM         [T_RelateTafSiliSath1-2]
WHERE     (THesabIDSath1 = @THesabID)

							  
END







GO


/****** Object:  StoredProcedure [dbo].[Rep_TH1Sath2SF3]    Script Date: 10/21/2017 16:58:53 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_TH1Sath2SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_TH1Sath2SF3]
GO


/****** Object:  StoredProcedure [dbo].[Rep_TH1Sath2SF3]    Script Date: 10/21/2017 16:58:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


          create PROCEDURE [dbo].[Rep_TH1Sath2SF3] (@THESABID nvarchar(50),@ThesabIDSath2 nvarchar(50), @startDate nvarchar(50), @enddate nvarchar(50), @Srt1 smallint,@Srt2 smallint ) AS  
          
          
SELECT     Date1, SAID2, THESABID, SAID2Number, DARBed, PARBes, Flag2, Sharh, TextShSA, namehesab, mande, @startDate AS startDate, @enddate AS enddate, TEMPR, 
                      CASE @Srt1 WHEN 1 THEN Date1 WHEN 2 THEN SAID2Number END AS Expr1, CASE @Srt2 WHEN 2 THEN Date1 WHEN 1 THEN SAID2Number END AS Expr2, @startDate AS textdate1, TEMP, 
                      ThesabIDSath2
FROM         View_RepTH1
WHERE     (Date1 BETWEEN @startDate AND @enddate) AND (THESABID = @THESABID) AND (DARBed IS NOT NULL) AND (PARBes IS NOT NULL) AND (ThesabIDSath2 = @ThesabIDSath2)
ORDER BY CASE @Srt1 WHEN 1 THEN Date1 ELSE SAID2Number END, CASE @Srt2 WHEN 2 THEN Date1 ELSE SAID2Number END
GO



/****** Object:  StoredProcedure [dbo].[stored_Rep_TH1Sath2SF3]    Script Date: 10/21/2017 16:58:57 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[stored_Rep_TH1Sath2SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[stored_Rep_TH1Sath2SF3]
GO


/****** Object:  StoredProcedure [dbo].[stored_Rep_TH1Sath2SF3]    Script Date: 10/21/2017 16:58:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

             create PROCEDURE [dbo].[stored_Rep_TH1Sath2SF3] 	( 		
             	
             	@THESABID int,
             	@THESABIDSath2 int,
             	@startDate nvarchar(50),
             	@enddate nvarchar(50),
             	@Srt1 smallint,
             	@Srt2 smallint 	)  AS 
             	
             	begin  
             		
             		Declare @Sum money ,@Mandeh money,@i int   
             		
             		Set @Sum = 0 
             		Set @i = 1 /* set @Srt1 =1 set @Srt2=2 set @AccID=3 set @startDate='00000000' set @enddate='99999999' */  
             		
             	DECLARE c2 cursor read_only FOR   
             	
				SELECT     SUM(DARBed - PARBes) AS Expr1
				FROM         [TRANSACTION]
				WHERE     (Date1 < @startDate) AND (DARBed IS NOT NULL) AND (PARBes IS NOT NULL)
				GROUP BY THESABID, ThesabIDSath2
				HAVING      (THESABID = @THESABID) AND (ThesabIDSath2 = @ThesabIDSath2) 
             	
             	OPEN c2 FETCH c2 INTO @Sum 
             	
             	if (@@FETCH_STATUS <> 0) 
             		set @Sum=0  
             	CLOSE c2 
             	
             	DEALLOCATE c2  
             	
             	DECLARE c1 cursor FORWARD_ONLY FOR 
             	
			SELECT     mande
			FROM         View_RepTH1
			WHERE     (Date1 BETWEEN @startDate AND @enddate) AND (ThesabIDSath2=@ThesabIDSath2) and (THESABID = @THESABID) AND (DARBed IS NOT NULL) AND (PARBes IS NOT NULL)
			ORDER BY CASE @Srt1 WHEN 1 THEN Date1 ELSE SAID2Number END, CASE @Srt2 WHEN 2 THEN Date1 ELSE SAID2Number END 

             	OPEN c1  
             	
             	FETCH c1 INTO @Mandeh 
             	WHILE (@@fetch_status = 0) 	
             	BEGIN  		
             		set @Sum=@Sum+@Mandeh 
             				
             		update View_RepTH1 set [TEMP]= @Sum , TEMPR= convert(nvarchar(20),@i) 		
             		where current of c1 		 		
             		set @i=@i+1 		
             		FETCH c1 INTO @Mandeh 	
             	END 
             	CLOSE c1 
             	DEALLOCATE  c1 /* SET NOCOUNT ON */ 	
             	return    END         
GO






/****** Object:  StoredProcedure [dbo].[AnalyzeReview_TafsiliHesabSF3]    Script Date: 10/24/2017 10:06:14 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AnalyzeReview_TafsiliHesabSF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[AnalyzeReview_TafsiliHesabSF3]
GO


/****** Object:  StoredProcedure [dbo].[AnalyzeReview_TafsiliHesabSF3]    Script Date: 10/24/2017 10:06:14 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[AnalyzeReview_TafsiliHesabSF3] 
	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)
                 

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'




	SET @SQLQuery = ' SELECT     ROW_NUMBER() OVER (ORDER BY THESABNumber) AS RowNumber,CASE WHEN View_TRANSACTION.THESABID IN('+ isnull(@THesabidID,'-1') +') THEN 1 ELSE 0 END AS SelectedRow,
     SUM(PARBes) AS SumPARBes, SUM(DARBed) AS SumDARBed, CASE WHEN (SUM(DARBed) - SUM(PARBes)) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS MandeBed, 
                      CASE WHEN (SUM(PARBes) - SUM(DARBed)) > 0 THEN (SUM(PARBes) - SUM(DARBed)) ELSE 0 END AS MandeBes, THESABID, THESABNumber, NameHesabT
FROM         View_TRANSACTION
WHERE     (NOT (THESABID IS NULL))  '
			 
	SET @GROUPBy = ' GROUP BY THESABID, THESABNumber, NameHesabT'
	SET @OrderBy = ' ORDER BY THESABNumber'

	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         

   

   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END

GO




/****** Object:  StoredProcedure [dbo].[AnalyzeReview_TafsiliHesabSath2SF3]    Script Date: 10/24/2017 10:06:09 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AnalyzeReview_TafsiliHesabSath2SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[AnalyzeReview_TafsiliHesabSath2SF3]
GO



/****** Object:  StoredProcedure [dbo].[AnalyzeReview_TafsiliHesabSath2SF3]    Script Date: 10/24/2017 10:06:09 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


Create PROCEDURE [dbo].[AnalyzeReview_TafsiliHesabSath2SF3] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)
                 

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'




	SET @SQLQuery = ' SELECT     ROW_NUMBER() OVER (ORDER BY THESABNumberSath2) AS RowNumber,CASE WHEN View_TRANSACTION.ThesabIDSath2 IN('+ isnull(@THesabidIDSath2,'-1') +') THEN 1 ELSE 0 END AS SelectedRow,
				SUM(PARBes) AS SumPARBes, SUM(DARBed) AS SumDARBed, CASE WHEN (SUM(DARBed) - SUM(PARBes)) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS MandeBed, 
				CASE WHEN (SUM(PARBes) - SUM(DARBed)) > 0 THEN (SUM(PARBes) - SUM(DARBed)) ELSE 0 END AS MandeBes, ThesabIDSath2, THESABNumberSath2, NameHesabTSath2
				FROM         View_TRANSACTION
				WHERE     (NOT (ThesabIDSath2 IS NULL)) '
			 
	SET @GROUPBy = ' GROUP BY ThesabIDSath2, THESABNumberSath2, NameHesabTSath2'
	SET @OrderBy = ' ORDER BY THESABNumberSath2'

	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         

   

   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END

GO



/****** Object:  StoredProcedure [dbo].[AnalyzeReview_RizAghlamSath2SF3]    Script Date: 10/24/2017 10:06:03 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AnalyzeReview_RizAghlamSath2SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[AnalyzeReview_RizAghlamSath2SF3]
GO


/****** Object:  StoredProcedure [dbo].[AnalyzeReview_RizAghlamSath2SF3]    Script Date: 10/24/2017 10:06:03 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[AnalyzeReview_RizAghlamSath2SF3] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)
                 

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'



/*CASE WHEN View_TRANSACTION.TRAID IN('+ isnull(@THesabidIDSath2,'-1') +') THEN 1 ELSE 0 END*/

	SET @SQLQuery = ' SELECT     ROW_NUMBER() OVER (ORDER BY View_TRANSACTION.SAID2, View_TRANSACTION.RadifSAID2) AS RowNumber,0 AS SelectedRow,
     View_TRANSACTION.PARBes AS SumPARBes, View_TRANSACTION.DARBed AS SumDARBed, T_SA2.SAID2Number, View_TRANSACTION.NameHesabTSath2, 
                      CASE WHEN (NameHesabT IS NOT NULL) THEN NameHesabT WHEN (NameHesabMoin IS NOT NULL) THEN NameHesabMoin ELSE NameHesab END AS NameHesabT1, 
                      View_TRANSACTION.Date1, View_TRANSACTION.Sharh, View_TRANSACTION.SAID2, View_TRANSACTION.RadifSAID2, View_TRANSACTION.TRAID, CASE WHEN (THESABNumber IS NOT NULL) 
                      THEN THESABNumber WHEN (HESABNumber IS NOT NULL) THEN HESABNumber ELSE KolNumber END AS CodHesab, View_TRANSACTION.THESABNumberSath2
FROM         View_TRANSACTION INNER JOIN
                      T_SA2 ON View_TRANSACTION.SAID2 = T_SA2.SAID2

'
			 
	SET @GROUPBy = ' '
	SET @OrderBy = ' ORDER BY View_TRANSACTION.SAID2, View_TRANSACTION.RadifSAID2'

	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END

GO



/****** Object:  StoredProcedure [dbo].[AnalyzeReview_MoinHesabSF3]    Script Date: 10/24/2017 10:05:58 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AnalyzeReview_MoinHesabSF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[AnalyzeReview_MoinHesabSF3]
GO


/****** Object:  StoredProcedure [dbo].[AnalyzeReview_MoinHesabSF3]    Script Date: 10/24/2017 10:05:58 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[AnalyzeReview_MoinHesabSF3] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)
                 

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'




	SET @SQLQuery = ' SELECT     ROW_NUMBER() OVER (ORDER BY HESABNumber) AS RowNumber,CASE WHEN View_TRANSACTION.HESABID IN('+ isnull(@HesabidID,'-1') +') THEN 1 ELSE 0 END AS SelectedRow,
						     SUM(PARBes) AS SumPARBes, SUM(DARBed) AS SumDARBed, CASE WHEN (SUM(DARBed) - SUM(PARBes)) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS MandeBed, 
						CASE WHEN (SUM(PARBes) - SUM(DARBed)) > 0 THEN (SUM(PARBes) - SUM(DARBed)) ELSE 0 END AS MandeBes, HESABID, HESABNumber, NameHesabMoin
						FROM         View_TRANSACTION
						WHERE     (NOT (HESABID IS NULL))
						
						 '
			 
	SET @GROUPBy = ' GROUP BY HESABID, NameHesabMoin, HESABNumber'
	SET @OrderBy = ' ORDER BY HESABNumber'

	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         

   

   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END

GO



/****** Object:  StoredProcedure [dbo].[AnalyzeReview_KOLHesabSF3]    Script Date: 10/24/2017 10:05:54 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AnalyzeReview_KOLHesabSF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[AnalyzeReview_KOLHesabSF3]
GO



/****** Object:  StoredProcedure [dbo].[AnalyzeReview_KOLHesabSF3]    Script Date: 10/24/2017 10:05:54 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[AnalyzeReview_KOLHesabSF3] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)
                 

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'




	SET @SQLQuery = ' SELECT ROW_NUMBER() OVER (ORDER BY KolNumber) AS RowNumber,CASE WHEN View_TRANSACTION.KOLID IN('+ isnull(@KOLID,'-1') +') THEN 1 ELSE 0 END AS SelectedRow,
						SUM(PARBes) AS SumPARBes, SUM(DARBed) AS SumDARBed, CASE WHEN (SUM(DARBed) - SUM(PARBes)) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS MandeBed, 
						CASE WHEN (SUM(PARBes) - SUM(DARBed)) > 0 THEN (SUM(PARBes) - SUM(DARBed)) ELSE 0 END AS MandeBes, KOLID, KolNumber, NameHesab
						FROM         View_TRANSACTION
						WHERE     (1 = 1) '
			 
	SET @GROUPBy = ' GROUP BY KOLID, KolNumber, NameHesab'
	SET @OrderBy = ' ORDER BY KolNumber'

	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         

   

   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END

GO




/****** Object:  StoredProcedure [dbo].[AnalyzeReview_GroupHesabSF3]    Script Date: 10/24/2017 10:05:46 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AnalyzeReview_GroupHesabSF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[AnalyzeReview_GroupHesabSF3]
GO



/****** Object:  StoredProcedure [dbo].[AnalyzeReview_GroupHesabSF3]    Script Date: 10/24/2017 10:05:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[AnalyzeReview_GroupHesabSF3] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)
                 

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'





	SET @SQLQuery = ' SELECT     ROW_NUMBER() OVER (ORDER BY CodGroup) AS RowNumber,CASE WHEN GOID IN('+ isnull(@GroupID,'-1') +') THEN 1 ELSE 0 END AS SelectedRow,GOID,CodGroup, NameGo, SUM(PARBes) AS SumPARBes, SUM(DARBed) AS SumDARBed
						,CASE WHEN (SUM(DARBed)-SUM(PARBes)) >0 THEN (SUM(DARBed)-SUM(PARBes)) ELSE 0 END AS MandeBed,
						CASE WHEN (SUM(PARBes)-SUM(DARBed)) >0 THEN (SUM(PARBes)-SUM(DARBed)) ELSE 0 END AS MandeBes
						FROM         View_TRANSACTION 
						Where 1=1 '
			 
	SET @GROUPBy = 'GROUP BY GOID,CodGroup,NameGo'
	SET @OrderBy = 'ORDER BY CodGroup'

    /* check for the condition and build the WHERE clause accordingly*/ 
    	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.GOID IN('+ @GroupID +'))'
         
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         

   

   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END

GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationTable2]    Script Date: 11/07/2017 10:35:10 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationTable2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationTable2]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationTable2]    Script Date: 11/07/2017 10:35:10 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationTable2] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	
	
    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 =  ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.Table2  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.Table2  ' + CHAR(10)
												+' ( FieldN2, FieldN3, FieldN4, FieldN1, FieldT1, ID2Number ) ' + CHAR(10) 
												+' SELECT     [TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID, [TRANSACTION].DARBed, [TRANSACTION].Sharh, T_Asnad.ASNADID  ' + CHAR(10) 
												+' FROM         [TRANSACTION] INNER JOIN T_Asnad ON [TRANSACTION].SAID2 = T_Asnad.SAID2 WHERE     ([TRANSACTION].DARBed <> 0) AND (T_Asnad.FieldN1 = 4) AND (T_Asnad.Flag2 IS NULL) OR ([TRANSACTION].DARBed <> 0) AND (T_Asnad.FieldN1 = 4) AND (T_Asnad.Flag2 = 0) ' + CHAR(10)
												 +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.Table2  ' + CHAR(10)
												+' ( ID2Number, DateCreate, DateModified, DATE2, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4 ) ' + CHAR(10) 
												+' SELECT     ID2Number, DateCreate, DateModified, DATE2, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4  ' + CHAR(10) 
												+'  FROM         Table2 AS Table2_1 WHERE     (ID2Number IN (SELECT     ASNADID FROM T_Asnad  WHERE     (FieldN1 = 4) AND (Flag2 = 1))) ' + CHAR(10)


												
    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

		
    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END




GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_YColor]    Script Date: 11/07/2017 10:35:06 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_YColor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_YColor]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_YColor]    Script Date: 11/07/2017 10:35:06 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_YColor] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 


	SET @SQLQuery1 = '  ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_YColor  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_YColor  ' + CHAR(10)
												+' (COLORID, ID, Name, Newcolor, colorDefult, FieldN1, FieldN2, FieldN3, FieldN4, FieldN5, FieldN6, FieldN7, FieldN8, FieldT1, FieldT2, FieldT3, FieldT4, ' + CHAR(10) 
												+' FieldT5, FieldT6, FieldT7, FieldT8, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8)  ' + CHAR(10) 
												+' SELECT     COLORID, ID, Name, Newcolor, colorDefult, FieldN1, FieldN2, FieldN3, FieldN4, FieldN5, FieldN6, FieldN7, FieldN8, FieldT1, FieldT2, FieldT3, FieldT4, ' + CHAR(10)
												+' FieldT5 , FieldT6, FieldT7, FieldT8, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8 ' + CHAR(10)
												+' FROM dbo.T_YColor    '

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

	
	
	

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO


 
/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_VKala]    Script Date: 11/07/2017 10:35:01 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_VKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_VKala]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_VKala]    Script Date: 11/07/2017 10:35:01 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_VKala] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 


	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_VKala] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_VKala  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_VKala  ' + CHAR(10)
												+' (VKALAID, VKalaNumber, DateCreate, DateModified, Name, GROUPKALAID) ' + CHAR(10) 
												+' SELECT     VKALAID, VKalaNumber, DateCreate, DateModified, Name, GROUPKALAID ' + CHAR(10)
												+' FROM dbo.T_VKala    SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_VKala] OFF'

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

	
	
	

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_SA2]    Script Date: 11/07/2017 10:34:48 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_SA2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_SA2]
GO


/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_SA2]    Script Date: 11/07/2017 10:34:48 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_SA2] 

@StrDes NVARCHAR(255),	   
@Date2 NVARCHAR(10), 
@Sharh NVARCHAR(255),
@OperationCode NVARCHAR(50),  
@FieldT1 NVARCHAR(255), 
@dateeng DATETIME,
@SAID2 INT =-1 OUTPUT


AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	
	SET @SQLQuery1 = CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_SA2  ' + CHAR(10)
												+' (  SAID2Number, Date2, Sharh, OperationCode, FieldN1, FieldN2, FieldT1, Flag1,dateeng) ' + CHAR(10) 
												+' VALUES ( 1,@Date2,@Sharh,@OperationCode, 1, 16,@FieldT1, 0,@dateeng) ' + CHAR(10) 

	
	
    Set @ParamDefinition = '@StrDes NVARCHAR(255),@Date2 NVARCHAR(10), @Sharh NVARCHAR(255), @OperationCode NVARCHAR(50),  @FieldT1 NVARCHAR(255), @dateeng NVARCHAR(255), @SAID2 INT' 	

	
		
    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes,
						    @Date2,
						    @Sharh, 
							@OperationCode,  
							@FieldT1, 
							@dateeng,
							@SAID2

	set @SAID2=@@IDENTITY;
	
    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END


              



GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_RepGen]    Script Date: 11/07/2017 10:34:43 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_RepGen]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_RepGen]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_RepGen]    Script Date: 11/07/2017 10:34:43 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_RepGen] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 


	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_RepGen] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_RepGen  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_RepGen  ' + CHAR(10)
												+' (IDKey, RepID, TypeID, RepTitle, FlagShow, Description, OrgDataBinary, ChgDataBinary, SelectFrom, StrFrom, WhereFiled, GroupFileld, HavingField, OrderField, FlagDef, TypeRep, FlagSendP,' + CHAR(10) 
												+' FlagShowDlgPrn, PrnCnt, PrnName, RepStoredProc, storedprocedPararmeter, FlagCh)  ' + CHAR(10) 
												+' SELECT   IDKey, RepID, TypeID, RepTitle, FlagShow, Description, OrgDataBinary, ChgDataBinary, SelectFrom, StrFrom, WhereFiled, GroupFileld, HavingField, OrderField, FlagDef, TypeRep, FlagSendP, ' + CHAR(10)
												+' FlagShowDlgPrn, PrnCnt, PrnName, RepStoredProc, storedprocedPararmeter, FlagCh ' + CHAR(10)
												+' FROM dbo.T_RepGen    SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_RepGen] OFF'

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes



    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Par2]    Script Date: 11/07/2017 10:34:39 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_Par2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_Par2]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Par2]    Script Date: 11/07/2017 10:34:39 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_Par2] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Par2] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_Par2  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_Par2  ' + CHAR(10)
												+' ( PARID2, VSabt, DateCreate, DateModified, DATE2, Dateeng, TextShSA, NumSID2, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4 ) ' + CHAR(10) 
												+' SELECT PARID2, VSabt, DateCreate, DateModified, DATE2, Dateeng, TextShSA, NumSID2, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4  ' + CHAR(10) 
												+' FROM         T_Par2 AS T_Par2_1 WHERE     (PARID2 IN (SELECT PARID2 FROM T_Asnad AS T_Par2_1 WHERE     (FieldN1 = 4))) SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Par2] OFF ' + CHAR(10) 

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

		
    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END


    
                      

GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_NameVahed]    Script Date: 11/07/2017 10:34:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_NameVahed]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_NameVahed]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_NameVahed]    Script Date: 11/07/2017 10:34:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_NameVahed] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 


              
	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_NameVahed] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_NameVahed  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_NameVahed  ' + CHAR(10)
												+' (VAHED1, NameVahed, DateCreate, VAHEDNumber, DateModified) ' + CHAR(10) 
												+' SELECT     VAHED1, NameVahed, DateCreate, VAHEDNumber, DateModified  ' + CHAR(10)
												+' FROM dbo.T_NameVahed    SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_NameVahed] OFF'

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

	
	
	

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_NameKhad]    Script Date: 11/07/2017 10:34:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_NameKhad]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_NameKhad]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_NameKhad]    Script Date: 11/07/2017 10:34:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_NameKhad] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_NameKhad] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_NameKhad  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_NameKhad  ' + CHAR(10)
												+' ( NAMEKHID, KhadamatNumber, DateCreate, DateModified, Name, UnitPrice, Unit, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4) ' + CHAR(10) 
												+' SELECT NAMEKHID, KhadamatNumber, DateCreate, DateModified, Name, UnitPrice, Unit, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4' + CHAR(10) 
												+' FROM T_NameKhad SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_NameKhad] OFF ' + CHAR(10) 

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO





/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_NameGoKala]    Script Date: 11/07/2017 10:34:22 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_NameGoKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_NameGoKala]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_NameGoKala]    Script Date: 11/07/2017 10:34:22 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_NameGoKala] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_NameGoKala] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_NameGoKala  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_NameGoKala  ' + CHAR(10)
												+' (ID, NameGoKalanumbr, DateCreate, DateModified, NameGoKala1, note, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4) ' + CHAR(10) 
												+' SELECT     ID, NameGoKalanumbr, DateCreate, DateModified, NameGoKala1, note, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4 ' + CHAR(10) 
												+' FROM T_NameGoKala SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_NameGoKala] OFF ' + CHAR(10) 

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_KolHesab]    Script Date: 11/07/2017 10:34:14 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_KolHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_KolHesab]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_KolHesab]    Script Date: 11/07/2017 10:34:14 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


create PROCEDURE [dbo].[CloseHesab_InsertInformationT_KolHesab] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

    

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_KolHesab] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_KolHesab '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_KolHesab ' + CHAR(10)
												+' (KOLID, KolNumber, DateCreate, NameHesab, DateModified, GOID)' + CHAR(10)
												+' SELECT        KOLID AS Expr1, KolNumber AS Expr2, DateCreate AS Expr3, NameHesab AS Expr4, DateModified AS Expr5, GOID As Expr6 ' + CHAR(10)
												+' FROM dbo.T_KolHesab   SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_KolHesab] OFF'

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

	
	
	

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO




GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Heasb]    Script Date: 11/07/2017 10:34:05 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_Heasb]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_Heasb]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Heasb]    Script Date: 11/07/2017 10:34:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_Heasb] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 



	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Heasb] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_Heasb '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_Heasb ' + CHAR(10)
												+' (HESABID, HESABNumber, DateCreate, DateModified, NameHesab, GOID, KolID)' + CHAR(10)
												+' SELECT        HESABID, HESABNumber, DateCreate, DateModified, NameHesab, GOID, KolID ' + CHAR(10)
												+' FROM dbo.T_Heasb   SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Heasb] OFF'

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

	
	
	

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Groupkala]    Script Date: 11/07/2017 10:34:00 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_Groupkala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_Groupkala]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Groupkala]    Script Date: 11/07/2017 10:34:00 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_Groupkala] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 


	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Groupkala] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_Groupkala  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_Groupkala  ' + CHAR(10)
												+' (GROUPKALAID, Name, DateCreate, DateModified, FieldN1, FieldT1) ' + CHAR(10) 
												+' SELECT     GROUPKALAID, Name, DateCreate, DateModified, FieldN1, FieldT1 ' + CHAR(10)
												+' FROM dbo.T_Groupkala    SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Groupkala] OFF'

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

	
	
	

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Defult]    Script Date: 11/07/2017 10:33:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_Defult]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_Defult]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Defult]    Script Date: 11/07/2017 10:33:56 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_Defult] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 


	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Defult] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_Defult  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_Defult  ' + CHAR(10)
												+' (ID1, GROUPID, OPTIONID, DateCreate, DateModified, FieldN1, FieldN2, FieldN3, FieldN4, FieldN5, FieldN7, FieldN6, FieldN8, FieldT2, FieldT1, FieldT3,' + CHAR(10) 
												+' FieldT6, FieldT4, FieldT5, FieldT8, FieldT7, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8)  ' + CHAR(10) 
												+' SELECT   ID1, GROUPID, OPTIONID, DateCreate, DateModified, FieldN1, FieldN2, FieldN3, FieldN4, FieldN5, FieldN7, FieldN6, FieldN8, FieldT2, FieldT1, FieldT3, ' + CHAR(10)
												+' FieldT6 , FieldT4, FieldT5, FieldT8, FieldT7, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8 ' + CHAR(10)
												+' FROM dbo.T_Defult    SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Defult] OFF'

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

	
	
	

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Dar3]    Script Date: 11/07/2017 10:33:52 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_Dar3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_Dar3]
GO


/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Dar3]    Script Date: 11/07/2017 10:33:52 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_Dar3] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Dar3] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_Dar3  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_Dar3  ' + CHAR(10)
												+' ( DARID3, VSabt, DateCreate, DateModified, TextShSA, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4 ) ' + CHAR(10) 
												+' SELECT DARID3, VSabt, DateCreate, DateModified, TextShSA, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4 ' + CHAR(10) 
												+' FROM         T_Dar3 WHERE     (DARID3 IN(SELECT DARID3 FROM T_Asnad_Dar WHERE (FieldN1 = 3) OR (FieldN1 = 4))) SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Dar3] OFF ' + CHAR(10) 

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

		
    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END




              



GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Dar2]    Script Date: 11/07/2017 10:33:48 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_Dar2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_Dar2]
GO


/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Dar2]    Script Date: 11/07/2017 10:33:48 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_Dar2] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Dar2] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_Dar2  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_Dar2  ' + CHAR(10)
												+' ( DARID2, VSabt, DateCreate, DateModified, TextShSA, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4 ) ' + CHAR(10) 
												+' SELECT DARID2, VSabt, DateCreate, DateModified, TextShSA, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4 ' + CHAR(10) 
												+' FROM  T_Dar2 WHERE  (DARID2 IN (SELECT  DARID2 FROM T_Asnad_Dar  WHERE  (FieldN1 = 3) OR  (FieldN1 = 4))) SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Dar2] OFF ' + CHAR(10) 

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

		
    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END


              



GO


/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Asnad_Dar]    Script Date: 11/08/2017 11:05:02 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_Asnad_Dar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_Asnad_Dar]
GO


/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Asnad_Dar]    Script Date: 11/08/2017 11:05:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_Asnad_Dar] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Asnad_Dar] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_Asnad_Dar  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_Asnad_Dar  ' + CHAR(10)
												+' ( ASNADDARID, AsnadDarNumber, DateCreate, DateModified, VSabt, GOID, KOLID, HESABID, THESABID, TARID, SAID2, Mablagh, ShHesab, Namebank, ShSeryal, Girandeh, DARID2, DARID3, DateTA, ' + CHAR(10) 
												+' DateVO, DateCH, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, FlagStatus) ' + CHAR(10)
												+' SELECT ASNADDARID, AsnadDarNumber, DateCreate, DateModified, VSabt, GOID, KOLID, HESABID, THESABID, TARID, SAID2, Mablagh, ShHesab, Namebank, ShSeryal, Girandeh, DARID2, DARID3, DateTA, ' + CHAR(10) 
												+'   DateVO, DateCH, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, 1 as Flag2, Flag3, Flag4, FlagStatus ' + CHAR(10) 
												+' FROM  T_Asnad_Dar WHERE (FieldN1 = 4) OR(FieldN1 = 3) ' + CHAR(10) 
												+' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Asnad_Dar] OFF ' + CHAR(10) 

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 
    
                     



    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO



              



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Asnad]    Script Date: 11/07/2017 10:33:40 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_Asnad]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_Asnad]
GO


/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Asnad]    Script Date: 11/07/2017 10:33:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_Asnad] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Asnad] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_Asnad  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_Asnad  ' + CHAR(10)
												+' ( ASNADID, AsnadNumber, DateCreate, DateModified, VSabt, GOID, KOLID, HESABID, THESABID, HESABID2, THESABID2, SAID2, Mablagh, ShSeryal, Girandeh, PARID2, PARID3, DateSR, DateSRENG, ' + CHAR(10) 
												+'  DateSO, DateSOENG, DateChange, DateChangeENG, Sharh, FieldN2, FieldN1, FieldN3, FieldN4, FieldT1, FieldT4, Flag1, FieldT3, FieldT2, Flag2, Flag3, Flag4) ' + CHAR(10) 
												+' SELECT  ASNADID, AsnadNumber, DateCreate, DateModified, VSabt, GOID, KOLID, HESABID, THESABID, HESABID2, THESABID2, SAID2, Mablagh, ShSeryal, Girandeh, PARID2, PARID3, DateSR, DateSRENG, ' + CHAR(10) 
												+'  DateSO, DateSOENG, DateChange, DateChangeENG, Sharh, FieldN2, FieldN1, FieldN3, FieldN4, FieldT1, FieldT4, Flag1, FieldT3, FieldT2, 1 AS Expr1, Flag3, Flag4 ' + CHAR(10) 
												+' FROM         T_Asnad WHERE     (FieldN1 = 4) SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Asnad] OFF ' + CHAR(10) 

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

		
    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END


              
   
                      

GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Anbar]    Script Date: 11/07/2017 10:33:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_Anbar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_Anbar]
GO


/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Anbar]    Script Date: 11/07/2017 10:33:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_Anbar] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Anbar] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_Anbar  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_Anbar  ' + CHAR(10)
												+' (AID, AIDNumber, DateCreate, DateModified, kalaid, NameAnbar, Phone, Address, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, hesabid, Thesabid, Goid, Kolid) ' + CHAR(10) 
												+' SELECT AID, AIDNumber, DateCreate, DateModified, kalaid, NameAnbar, Phone, Address, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, hesabid, Thesabid, Goid, Kolid ' + CHAR(10) 
												+' FROM T_Anbar SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Anbar] OFF ' + CHAR(10) 

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

		
    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationMsgT_Sharh]    Script Date: 11/07/2017 10:33:30 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationMsgT_Sharh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationMsgT_Sharh]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationMsgT_Sharh]    Script Date: 11/07/2017 10:33:30 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationMsgT_Sharh] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[MsgT_Sharh] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.MsgT_Sharh  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.MsgT_Sharh  ' + CHAR(10)
												+' (IDSHARH, DateCreate, Name, DateModified) ' + CHAR(10) 
												+' SELECT     IDSHARH, DateCreate, Name, DateModified ' + CHAR(10) 
												+' FROM MsgT_Sharh SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[MsgT_Sharh] OFF ' + CHAR(10) 
												+ ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[MsgT_Sharh1] ON ' + CHAR(10) 
												 + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.MsgT_Sharh1  '
												+N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.MsgT_Sharh1  ' + CHAR(10)
												+' (IDSHARH, DateCreate, Name, DateModified)' + CHAR(10) 
												+' SELECT     IDSHARH, DateCreate, Name, DateModified ' + CHAR(10) 
												+' FROM MsgT_Sharh1 SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[MsgT_Sharh1] OFF'

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationHesab]    Script Date: 11/07/2017 10:33:26 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationHesab]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationHesab]    Script Date: 11/07/2017 10:33:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationHesab] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 



	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_GropHesab] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_GropHesab '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_GropHesab ' + CHAR(10)
												+' (GoID, GoNumber, DateCreate, DateModified, NameGo)' + CHAR(10)
												+' SELECT        GoID, GoNumber, DateCreate, DateModified, NameGo ' + CHAR(10)
												+' FROM dbo.T_GropHesab   SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_GropHesab] OFF'

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

	
	
	

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationGropHesab]    Script Date: 11/07/2017 10:33:14 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationGropHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationGropHesab]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationGropHesab]    Script Date: 11/07/2017 10:33:14 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


Create PROCEDURE [dbo].[CloseHesab_InsertInformationGropHesab] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 



	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_GropHesab] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_GropHesab '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_GropHesab ' + CHAR(10)
												+' (GoID, GoNumber, DateCreate, DateModified, NameGo)' + CHAR(10)
												+' SELECT        GoID, GoNumber, DateCreate, DateModified, NameGo ' + CHAR(10)
												+' FROM dbo.T_GropHesab   SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_GropHesab] OFF'

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

	
	
	

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationDastehChek]    Script Date: 11/07/2017 10:33:09 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationDastehChek]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationDastehChek]
GO


/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationDastehChek]    Script Date: 11/07/2017 10:33:09 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationDastehChek] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = ' ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.DastehChek  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.DastehChek  ' + CHAR(10)
												+' ( DastehCHIDNumber, DateCreate, DateModified, NameBank, SeriDastehCD, Sh_Hesab, Serialnum1, Serialnum2, NameHesab, ' + CHAR(10) 
												+' CodeHesab, Flag) ' + CHAR(10) 
												+' SELECT      DastehCHIDNumber, DateCreate, DateModified, NameBank, SeriDastehCD, Sh_Hesab, Serialnum1, Serialnum2, NameHesab, ' + CHAR(10)
												+' CodeHesab , Flag ' + CHAR(10)
												+' FROM dbo.DastehChek  WHERE (Flag = 0)  ' + CHAR(10) 
												+' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.DastehChek  ' + CHAR(10)
												+' ( DastehCHIDNumber, DateCreate, DateModified, NameBank, SeriDastehCD, Sh_Hesab, Serialnum1, Serialnum2, NameHesab, ' + CHAR(10) 
												+' CodeHesab, Flag) ' + CHAR(10) 
												+' SELECT      DastehCHIDNumber, dbo.DastehChek.DateCreate, dbo.DastehChek.DateModified, NameBank, SeriDastehCD, Sh_Hesab, Serialnum1, Serialnum2, NameHesab,' + CHAR(10)
												+' CodeHesab , Flag ' + CHAR(10)
												+' FROM dbo.DastehChek INNER JOIN dbo.T_Asnad ON dbo.DastehChek.Serialnum1 = dbo.T_Asnad.ShSeryal WHERE(dbo.T_Asnad.FieldN1 = 4) ' + CHAR(10)   
												+' '

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformation_TRANSACTION_Hesab_MandeDare]    Script Date: 11/07/2017 12:18:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformation_TRANSACTION_Hesab_MandeDare]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformation_TRANSACTION_Hesab_MandeDare]
GO

/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformation_TRANSACTION_Hesab_MandeDare]    Script Date: 11/07/2017 12:18:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[CloseHesab_InsertInformation_TRANSACTION_Hesab_MandeDare] 

@StrDes NVARCHAR(255),	 
@SAID2  INT,
@DateSanad NVARCHAR(10),
@DateMiladi DATETIME,
@Sharh NVarchar(255),
@TextShSA NVarchar(255),
@DateCreate  AS NVarchar(255),
@DateModified  AS NVarchar(255)

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

    
    
    SET @DateCreate=GETDATE()
    SET @DateModified=GETDATE()


	SET @SQLQuery1=N' '
	SET @SQLQuery1 = N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.[TRANSACTION] ' + CHAR(10)
												+' ( GOID, KOLID, HESABID, THESABID, ThesabIDSath2, DARBed, PARBes, SAID2, RadifSAID2, SAID2Number, Date1, Dateeng, SAIDType, DateCreate, DateModified, Sharh, TextShSA, Radif) ' + CHAR(10) 
												+' SELECT  GOID, KOLID, HESABID, THESABID, ThesabIDSath2, (CASE WHEN SUM(ROUND(DARBed - PARBes, 2)) > 0 THEN SUM(ROUND(DARBed - PARBes, 2)) ELSE 0 END) AS DARBed,  ' + CHAR(10) 
												+' (CASE WHEN SUM(ROUND(PARBes - DARBed, 2)) > 0 THEN SUM(ROUND(PARBes - DARBed, 2)) ELSE 0 END) AS PARBes, @SAID2 AS SAID2  ' + CHAR(10) 
												+'   , ROW_NUMBER() OVER (ORDER BY dbo.HesabName(KOLID, HESABID, THESABID)) AS RadifSAID2, 1 AS SAID2Number,   ' + CHAR(10) 
												+'   @DateSanad AS DateSanad, @DateMiladi AS DateMiladi, 2 AS SAIDType, @DateCreate AS DateCreate, @DateModified AS DateModified, @Sharh AS Sharh, @TextShSA AS TextShSA  ' + CHAR(10) 
												+'    , ROW_NUMBER() OVER (ORDER BY dbo.HesabName(KOLID, HESABID, THESABID)) AS Radif ' + CHAR(10) 
												+'  FROM         [TRANSACTION] GROUP BY HESABID, THESABID, ThesabIDSath2, KOLID, GOID, dbo.HesabName(KOLID, HESABID, THESABID) HAVING      (GOID < 8) AND (SUM(ROUND(DARBed - PARBes, 2)) <> 0) ORDER BY dbo.HesabName(KOLID, HESABID, THESABID) '

    Set @ParamDefinition = '@StrDes NVARCHAR(255),	 
							@SAID2  INT,
							@DateSanad NVARCHAR(10),
							@DateMiladi DATETIME,
							@Sharh NVarchar(255),
							@TextShSA NVarchar(255),
							@DateCreate NVarchar(255),
							@DateModified NVarchar(255)' 	

		

    Execute sp_Executesql   @SQLQuery1, 
							@ParamDefinition,
							@StrDes,	 
							@SAID2 ,
							@DateSanad ,
							@DateMiladi ,
							@Sharh ,
							@TextShSA ,
							@DateCreate,
							@DateModified

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



                       
                      


GO






                       
                      


GO





                       
                      


GO





/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_ValidDate]    Script Date: 11/13/2017 11:10:13 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdari_ValidDate]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_ValidDate]
GO

/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_ValidDate]    Script Date: 11/13/2017 11:10:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE  PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_ValidDate]
		
		@DateInSAleMali NVARCHAR(10),
		@DBID NVARCHAR(255),
		@ValidDate BIT=0 OUTPUT

AS
BEGIN
	

        
SELECT     @ValidDate=COUNT(*) 
FROM          AC_Setting.dbo.T_YearMony
WHERE     (@DateInSAleMali BETWEEN AC_Setting.dbo.T_YearMony.StartDate AND AC_Setting.dbo.T_YearMony.EndDate) AND (DBID = @DBID)


END






GO



/****** Object:  View [dbo].[QSubPfrm_KhadamatSF4]    Script Date: 11/16/2017 16:18:26 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_KhadamatSF4]'))
DROP VIEW [dbo].[QSubPfrm_KhadamatSF4]
GO


/****** Object:  View [dbo].[QSubPfrm_KhadamatSF4]    Script Date: 11/16/2017 16:18:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE VIEW [dbo].[QSubPfrm_KhadamatSF4] AS SELECT   TOP (100) PERCENT   AID, KALAID, Sharh, Date1, KHARIDID, Tedad, UnitPrice, CONVERT(money, Tedad * UnitPrice) AS Sumation, radif, GOKID, CONVERT(nvarchar(20), AID)                        + CONVERT(nvarchar(20), GOKID) + CONVERT(nvarchar(20), KALAID) AS Expr1, LinkedID, TextKalaName, SumofPrice, FieldN6, OperType, DARBed FROM         dbo.TRANSACTION2 ORDER BY radif 

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKhadamatSF4]    Script Date: 11/16/2017 16:19:25 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListKhadamatSF4]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListKhadamatSF4]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKhadamatSF4]    Script Date: 11/16/2017 16:19:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

  Create PROCEDURE [dbo].[QSubPFrm_ListKhadamatSF4] AS SELECT     T_DKhad.DKHID, T_DKhad.HesabID, T_DKhad.GOID, T_DKhad.KOLID, T_DKhad.MOINID, T_DKhad.THESABID, T_DKhad.TextName, T_DKhad.TextName1, T_DKhad.TextNameT, T_DKhad.DateKh, 
                      T_DKhad.TextShSA, T_DKhad.NumSID2, T_DKhad.Takhfif, T_DKhad.Sharh, T_DKhad.FieldN1, T_DKhad.FieldN2, T_DKhad.FieldN3, T_DKhad.FieldN4, T_DKhad.FieldT1, T_DKhad.FieldT2, 
                      T_DKhad.FieldT3, T_DKhad.FieldT4, T_DKhad.Flag1, T_DKhad.Flag2, T_DKhad.Flag3, T_DKhad.Flag4, T_DKhad.PrimarySaid2, T_DKhad.Dateeng, T_DKhad.DKHNumbr, T_SA2.SAID2Number, 
                      dbo.HesabName(T_DKhad.KOLID, T_DKhad.HesabID, T_DKhad.THESABID) AS namehesab, T_DKhadZanali.TxtNoeKala, T_DKhadZanali.TxtTedadKala, T_DKhadZanali.TxtTedadKantiner, 
                      T_DKhadZanali.TxtTedadBime, T_DKhadZanali.TxtShBl, T_DKhadZanali.TxtShBime, T_DKhadZanali.TxtRadifMarzi, T_DKhadZanali.TxtShomare, T_DKhadZanali.TxtDateKotaj, 
                      T_DKhadZanali.TxtShKotaj, T_DKhadZanali.TxtParvaneTaranzit, T_DKhadZanali.TxtGomrokVorod, T_DKhadZanali.TxtGomrokM, T_DKhadZanali.TxtWazn, T_DKhadZanali.FieldN1 AS TxtDemo
FROM         T_DKhad INNER JOIN
                      T_SA2 ON T_DKhad.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_DKhadZanali ON T_DKhad.DKHID = T_DKhadZanali.DKHID
ORDER BY T_DKhad.DKHID DESC
GO







/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Taf_Rep1]    Script Date: 11/16/2017 18:08:04 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubTreeViewRep_Taraz_Taf_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Taf_Rep1]
GO

/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Taf_Rep1]    Script Date: 11/16/2017 18:08:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Taf_Rep1] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'

                      
	SET @SQLQuery = ' SELECT     T_KolHesab.NameHesab AS NameHesabKol, SUM([TRANSACTION].DARBed) AS Bedehkar, SUM([TRANSACTION].PARBes) AS Bestankar, [TRANSACTION].KOLID, 
                      T_KolHesab.KolNumber AS CodeHesab, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes)
                       ELSE 0 END AS MandeBed, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) 
                      ELSE 0 END AS MandeBes, T_Heasb.HESABNumber AS CodeMoin, T_Heasb.NameHesab AS NameHesabMoin, T_GropHesab.GoNumber, T_GropHesab.NameGo AS NameGroup, 
                      T_T_Heasb.THESABID, T_T_Heasb.THESABNumber AS CodeTafsili, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_T_Heasb.HESABID
FROM         T_GropHesab INNER JOIN
                      [TRANSACTION] ON T_GropHesab.GoID = [TRANSACTION].GOID INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (1 = 1) '
			 
	SET @GROUPBy = ' GROUP BY T_KolHesab.NameHesab, [TRANSACTION].KOLID, T_KolHesab.KolNumber, T_GropHesab.GoID, T_GropHesab.NameGo, T_Heasb.HESABNumber, T_Heasb.NameHesab, T_GropHesab.GoNumber, 
                      T_T_Heasb.THESABID, T_T_Heasb.THESABNumber, T_T_Heasb.NameHesabT, T_T_Heasb.HESABID	 '
	SET @OrderBy = ' '


	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
    SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
       
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END


GO



/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Rep1]    Script Date: 11/16/2017 18:08:00 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubTreeViewRep_Taraz_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Rep1]
GO

/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Rep1]    Script Date: 11/16/2017 18:08:00 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Rep1] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'



	SET @SQLQuery = ' SELECT     T_KolHesab.NameHesab AS NameHesabKol, SUM([TRANSACTION].DARBed) AS Bedehkar, SUM([TRANSACTION].PARBes) AS Bestankar, T_KolHesab.KOLID, 
					  T_KolHesab.KolNumber AS CodeHesab, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes)
					   ELSE 0 END AS MandeBed, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) 
					  ELSE 0 END AS MandeBes, T_GropHesab.GoID, T_GropHesab.NameGo AS NameGroup
FROM         T_KolHesab INNER JOIN
					  [TRANSACTION] ON T_KolHesab.KOLID = [TRANSACTION].KOLID INNER JOIN
					  T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID where 1=1 '
			 
	SET @GROUPBy = ' GROUP BY T_KolHesab.NameHesab, T_KolHesab.KOLID, T_KolHesab.KolNumber, T_GropHesab.GoID, T_GropHesab.NameGo	 '
	SET @OrderBy = ' '


	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
    SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
       
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END


GO



/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Moin_Rep1]    Script Date: 11/16/2017 18:07:55 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubTreeViewRep_Taraz_Moin_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Moin_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Moin_Rep1]    Script Date: 11/16/2017 18:07:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Moin_Rep1] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'

                      
	SET @SQLQuery = ' SELECT     T_KolHesab.NameHesab AS NameHesabKol, SUM([TRANSACTION].DARBed) AS Bedehkar, SUM([TRANSACTION].PARBes) AS Bestankar, [TRANSACTION].KOLID, 
                      T_KolHesab.KolNumber AS CodeHesab, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes)
                       ELSE 0 END AS MandeBed, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) 
                      ELSE 0 END AS MandeBes, T_Heasb.HESABNumber AS CodeMoin, T_Heasb.NameHesab AS NameHesabMoin, T_Heasb.HESABID, T_GropHesab.GoNumber, 
                      T_GropHesab.NameGo AS NameGroup
FROM         T_GropHesab INNER JOIN
                      [TRANSACTION] ON T_GropHesab.GoID = [TRANSACTION].GOID INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (1 = 1) '
			 
	SET @GROUPBy = ' GROUP BY T_KolHesab.NameHesab, [TRANSACTION].KOLID, T_KolHesab.KolNumber, T_GropHesab.GoID, T_GropHesab.NameGo, T_Heasb.HESABNumber, T_Heasb.NameHesab, T_Heasb.HESABID, 
                      T_GropHesab.GoNumber	 '
	SET @OrderBy = ' '


	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
    SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
       
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END


GO



/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_BedonSotoh_Rep1]    Script Date: 11/16/2017 18:07:51 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubTreeViewRep_Taraz_BedonSotoh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubTreeViewRep_Taraz_BedonSotoh_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_BedonSotoh_Rep1]    Script Date: 11/16/2017 18:07:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[QSubTreeViewRep_Taraz_BedonSotoh_Rep1] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'

                      
	SET @SQLQuery = ' SELECT     T_KolHesab.NameHesab AS NameHesabKol, SUM([TRANSACTION].DARBed) AS Bedehkar, SUM([TRANSACTION].PARBes) AS Bestankar, T_KolHesab.KOLID, 
                      T_KolHesab.KolNumber AS CodeHesab, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes)
                       ELSE 0 END AS MandeBed, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) 
                      ELSE 0 END AS MandeBes, T_GropHesab.GoID, T_GropHesab.NameGo AS NameGroup
FROM         T_KolHesab INNER JOIN
                      [TRANSACTION] ON T_KolHesab.KOLID = [TRANSACTION].KOLID INNER JOIN
                      T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID
WHERE     (1 = 1) '
			 
	SET @GROUPBy = ' GROUP BY T_KolHesab.NameHesab, T_KolHesab.KOLID, T_KolHesab.KolNumber, T_GropHesab.GoID, T_GropHesab.NameGo	 '
	SET @OrderBy = ' '


	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
    SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
       
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END


GO



/****** Object:  StoredProcedure [dbo].[QSubPishPFrm_Foroosh_Rep1]    Script Date: 11/16/2017 18:07:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPishPFrm_Foroosh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPishPFrm_Foroosh_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPishPFrm_Foroosh_Rep1]    Script Date: 11/16/2017 18:07:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE  PROCEDURE [dbo].[QSubPishPFrm_Foroosh_Rep1]
		
		@Userid INT,	
		@FOROSHID int

AS

BEGIN
	
SELECT     T_TarSPish.TedadFo AS Tedad, T_TarSPish.UnitPriceFo AS netprice, CASE WHEN isnull(T_PishFactor.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_PishFactor.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_PishFactor.THESABID AS CodeTafsili, T_PishFactor.KOLID AS CodeKol, T_PishFactor.HesabID AS CodeMoin, T_TarSPish.FieldN3 AS Mablagh, 
                      T_PishFactor.Takhfif, T_PishFactor.FieldT3 AS TarikhEtebar, T_PishFactor.DateFo AS Tarikh, T_TarSPish.Sharh AS SharhRadif, T_PishFactor.Sharh AS TozihatFactor, 
                      T_NameVahed.NameVahed AS NameVahedAsli, T_NameVahed_1.NameVahed AS NameVahedFarei, T_NameKala.Zarib, T_NameKala.BarCode, T_NameKala.KalaNumber AS CodeKala, 
                      T_PishFactor.ForoshNumbr AS Serial, T_PishFactor.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, 
                      T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Email, T_NameKala.NameKala1, T_PishFactor.FOROSHID
FROM         T_KolHesab RIGHT OUTER JOIN
                      T_Heasb RIGHT OUTER JOIN
                      T_T_Heasb RIGHT OUTER JOIN
                      T_NameVahed AS T_NameVahed_1 RIGHT OUTER JOIN
                      T_PishFactor INNER JOIN
                      T_TarSPish INNER JOIN
                      T_NameKala ON T_TarSPish.KALAID = T_NameKala.ID ON T_PishFactor.FOROSHID = T_TarSPish.FOROSHID LEFT OUTER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 ON T_NameVahed_1.VAHED1 = T_NameKala.VAHED2 ON T_T_Heasb.THESABID = T_PishFactor.THESABID ON 
                      T_Heasb.HESABID = T_PishFactor.HesabID ON T_KolHesab.KOLID = T_PishFactor.KOLID
WHERE     (T_PishFactor.FOROSHID = @FOROSHID)
               
               
END






GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdariArzi_Rep1]    Script Date: 11/16/2017 18:07:43 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdariArzi_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdariArzi_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdariArzi_Rep1]    Script Date: 11/16/2017 18:07:43 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


Create  PROCEDURE [dbo].[QSubPFrm_SanadHesabdariArzi_Rep1]
		
		@Userid INT,@SAID2 INT

AS

BEGIN

SELECT     T_SA2.SAID2Number AS ShomareSanad, T_SA2.Sharh AS SharhSanad, T_SA2.Date2 AS Tarikh, T_SA2.OperationCode AS Refrence, [TRANSACTION].radif, [TRANSACTION].Sharh AS SharhRadif, 
                      [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].DARBed AS Bedehkar, CASE WHEN [TRANSACTION].Thesabid <> NULL 
                      THEN T_T_Heasb.NameHesabT WHEN [TRANSACTION].hesabid <> NULL THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameHesab, 
                      T_T_Heasb.NameHesabT AS NameTafsili, T_Heasb.NameHesab AS NameMoin, T_KolHesab.NameHesab AS NameKol, T_Heasb.HESABNumber AS CodeMoin, 
                      T_T_Heasb.THESABNumber AS CodeTafsili, T_KolHesab.KolNumber AS CodeKol, T_SA2.SAID2, [TRANSACTION].CurRef AS NoeArz, - (1 * [TRANSACTION].CurRate) AS NerkhArz, 
                      [TRANSACTION].DARBedCur - [TRANSACTION].PARBesCur AS MablaghArz, Currency.Title AS NameArz, [TRANSACTION].RadifSAID2 AS Radifsanad
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      Currency ON [TRANSACTION].CurRef = Currency.CurrencyID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (T_SA2.SAID2 = @SAID2)
                      
END



GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3]    Script Date: 11/16/2017 18:07:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3]    Script Date: 11/16/2017 18:07:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)
                 

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'



	SET @SQLQuery = ' SELECT 1 AS RowNumber,0 AS SelectedRow,
     SUM(PARBes) AS SumPARBes, SUM(DARBed) AS SumDARBed, NameHesabTSath2, CASE WHEN (NameHesabT IS NOT NULL) THEN NameHesabT WHEN (NameHesabMoin IS NOT NULL) 
                      THEN NameHesabMoin ELSE NameHesab END AS NameHesabT1, CASE WHEN (THESABNumber IS NOT NULL) THEN THESABNumber WHEN (HESABNumber IS NOT NULL) 
                      THEN HESABNumber ELSE KolNumber END AS CodHesab, THESABNumberSath2, GOID, KOLID, HESABID, NameGo, NameHesab, KolNumber, HESABNumber, NameHesabMoin, THESABNumber, 
                      NameHesabT, CodGroup, THESABID, ThesabIDSath2, CASE WHEN (SUM(DARBed) - SUM(PARBes)) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS MandeBed, 
				CASE WHEN (SUM(PARBes) - SUM(DARBed)) > 0 THEN (SUM(PARBes) - SUM(DARBed)) ELSE 0 END AS MandeBes
FROM         View_TRANSACTION where 1=1 '
			 
	SET @GROUPBy = ' GROUP BY NameHesabTSath2, THESABNumberSath2, GOID, KOLID, HESABID, NameGo, NameHesab, KolNumber, HESABNumber, NameHesabMoin, THESABNumber, NameHesabT, CodGroup, THESABID, 
                      ThesabIDSath2 '
	SET @OrderBy = ' '

	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END


GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_Rep1]    Script Date: 11/16/2017 18:07:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdari_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_Rep1]    Script Date: 11/16/2017 18:07:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE  PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_Rep1]
		
		@Userid INT,
		@SAID2 NVARCHAR(MAX),
		@StarteDate NVARCHAR(10),
		@ENDDate NVARCHAR(10),
		@Said2AsShomare NVARCHAR(MAX), 
		@Said2TaShomare NVARCHAR(MAX)
AS

BEGIN


SET NOCOUNT ON;

Declare @SQLQuery AS NVarchar(MAX)
Declare @ParamDefinition AS NVarchar(MAX) 
DECLARE @OrderBy AS NVARCHAR(MAX)


IF (@StarteDate IS NULL)
		SET @StarteDate='0'

IF (@ENDDate IS NULL)
		SET @ENDDate='99999999'
			
SET @SQLQuery = ' SELECT     T_SA2.SAID2Number AS ShomareSanad, T_SA2.Sharh AS SharhSanad, T_SA2.Date2 AS Tarikh, T_SA2.OperationCode AS Refrence, [TRANSACTION].radif, [TRANSACTION].Sharh AS SharhRadif, 
                      [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].DARBed AS Bedehkar, CASE WHEN [TRANSACTION].Thesabid <> NULL 
                      THEN T_T_Heasb.NameHesabT WHEN [TRANSACTION].hesabid <> NULL THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameHesab, 
                      T_T_Heasb.NameHesabT AS NameTafsili, T_Heasb.NameHesab AS NameMoin, T_KolHesab.NameHesab AS NameKol, T_Heasb.HESABNumber AS CodeMoin, 
                      T_T_Heasb.THESABNumber AS CodeTafsili, T_KolHesab.KolNumber AS CodeKol, T_SA2.SAID2, [TRANSACTION].RadifSAID2 AS RadifSanad, ISNULL(T_Heasb.NameHesab, N'''') 
                      + CASE WHEN DARBed > 0 THEN ''1'' ELSE ''2'' END AS GrpMoin
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (1 = 1) '

SET @OrderBy = ' ORDER BY T_SA2.SAID2 DESC  '

    /* check for the condition and build the WHERE clause accordingly*/ 
    	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@SAID2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (T_SA2.SAID2 IN('+ @SAID2 +'))'
    ELSE    
    	BEGIN
			
			 Set @SQLQuery = @SQLQuery + ' And (T_SA2.Date2 Between @StarteDate AND @ENDDate )'
		    
			If ((@Said2AsShomare Is NOT NULL) AND (@Said2TaShomare Is NOT NULL))
				 Set @SQLQuery = @SQLQuery + ' And (T_SA2.SAID2Number Between @Said2AsShomare AND @Said2TaShomare )'
 
		END				    

   

   Set @SQLQuery = @SQLQuery 



    Set @ParamDefinition = '@UserID INT,
							@SAID2 NVARCHAR(MAX),
							@StarteDate NVARCHAR(10), 
							@ENDDate NVARCHAR(10), 
							@Said2AsShomare NVARCHAR(MAX), 
							@Said2TaShomare NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery +  char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@SAID2,
							@StarteDate , 
							@ENDDate , 
							@Said2AsShomare , 
							@Said2TaShomare 


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)


END
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepTH1_Rep1]    Script Date: 11/16/2017 18:07:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_RepTH1_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_RepTH1_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepTH1_Rep1]    Script Date: 11/16/2017 18:07:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE  PROCEDURE [dbo].[QSubPFrm_RepTH1_Rep1]
		
		@Userid INT,
		@Date1 NVARCHAR(10),
		@Date2 NVARCHAR(10),
		@THESABID INT,
		@TYPID INT

AS
BEGIN
	

IF (@TYPID=102 ) /* گزارش دفتر تفصيلي */
	
	SELECT     T_T_Heasb.NameHesabT AS NameHesabTafsili, [TRANSACTION].TextShSA AS SharhRadif, DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBed AS Bedehkar, 
						  [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_T_Heasb.THESABID, T_T_Heasb.THESABNumber AS CodeHesab
	FROM         [TRANSACTION] INNER JOIN
						  T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
						  T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
							  (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID
								 FROM         [TRANSACTION]
								 WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
								 GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.THESABID
	WHERE     (T_T_Heasb.THESABID = @THESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)

                             
ELSE

	IF (@TYPID=115  ) /* گزارش حساب تفصيلي ارزی */
	
		SELECT     DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBedCur AS Bedehkar, [TRANSACTION].PARBesCur AS Bestankar, [TRANSACTION].Date1 AS Tarikh, 
							  T_SA2.SAID2Number AS ShomareSanad, [TRANSACTION].TextShSA AS SharhRadif, [TRANSACTION].PARBesCur AS BestankarArzi, [TRANSACTION].DARBedCur AS BedehkarArzi, 
							  [TRANSACTION].CurRef AS NoeArz, [TRANSACTION].CurRate AS NerkhArz, T_SA2.Sharh AS SharhSanad, Currency.Title AS NameArz, T_T_Heasb.THESABID, 
							  T_T_Heasb.THESABNumber AS CodeTafsili, T_T_Heasb.NameHesabT AS NameHesabTafsili
		FROM         [TRANSACTION] INNER JOIN
							  T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
							  Currency ON [TRANSACTION].CurRef = Currency.CurrencyID INNER JOIN
							  T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
								  (SELECT     SUM(DARBedCur) - SUM(PARBesCur) AS mande, SUM(PARBesCur) AS SumOfPARBes, SUM(DARBedCur) AS SumOfDARBed, THESABID
									 FROM         [TRANSACTION]
									 WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
									 GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.THESABID
		WHERE     (T_T_Heasb.THESABID = @THESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)

  ELSE

	IF (@TYPID=116  )        /* گزارش حساب تفصيلي ارزی و ریالی */                    
			SELECT     DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBed AS Bedehkar, [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, 
								  [TRANSACTION].TextShSA AS SharhRadif, [TRANSACTION].PARBesCur AS BestankarArzi, [TRANSACTION].DARBedCur AS BedehkarArzi, [TRANSACTION].CurRef AS NoeArz, 
								  [TRANSACTION].CurRate AS NerkhArz, T_SA2.Sharh AS SharhSanad, Currency.Title AS NameArz, TBL_MandeArz.mandeCur AS mandeArz, T_T_Heasb.THESABID, 
								  T_T_Heasb.THESABNumber AS CodeTafsili, T_T_Heasb.NameHesabT AS NameHesabTafsili
			FROM         (SELECT     SUM(DARBedCur) - SUM(PARBesCur) AS mandeCur, SUM(PARBesCur) AS SumOfPARBesCur, SUM(DARBedCur) AS SumOfDARBedCur, THESABID
									FROM         [TRANSACTION]
									WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
									GROUP BY THESABID) AS TBL_MandeArz RIGHT OUTER JOIN
								  [TRANSACTION] INNER JOIN
								  T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
								  T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID ON TBL_MandeArz.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
									  (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID
										 FROM         [TRANSACTION]
										 WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
										 GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.THESABID LEFT OUTER JOIN
								  Currency ON [TRANSACTION].CurRef = Currency.CurrencyID
			WHERE     (T_T_Heasb.THESABID = @THESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @DATE2) AND (NOT ([TRANSACTION].SAID2 IS NULL))


END






GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepMo1_Rep1]    Script Date: 11/16/2017 18:07:26 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_RepMo1_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_RepMo1_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepMo1_Rep1]    Script Date: 11/16/2017 18:07:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE  PROCEDURE [dbo].[QSubPFrm_RepMo1_Rep1]
		
		@Userid INT,
		@Date1 NVARCHAR(10),
		@Date2 NVARCHAR(10),
		@HESABID INT,
		@TYPID INT

AS
BEGIN
	

IF (@TYPID=101 ) /* گزارش دفتر معين */
	
	
	SELECT     T_Heasb.NameHesab AS NameHesabmoin, [TRANSACTION].TextShSA AS SharhRadif, DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBed AS Bedehkar, 
						  [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_Heasb.HESABID, T_Heasb.HESABNumber AS CodeHesab
	FROM         [TRANSACTION] INNER JOIN
						  T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
						  T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID LEFT OUTER JOIN
							  (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, HESABID
								 FROM         [TRANSACTION]
								 WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
								 GROUP BY HESABID) AS DERIVEDTBL ON T_Heasb.HESABID = DERIVEDTBL.HESABID
	WHERE     (T_Heasb.HESABID = @HESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)

                             
ELSE

	IF (@TYPID=112  ) /* گزارش حساب معين ارزی */
	
			SELECT     DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBedCur AS Bedehkar, [TRANSACTION].PARBesCur AS Bestankar, [TRANSACTION].Date1 AS Tarikh, 
								  T_SA2.SAID2Number AS ShomareSanad, [TRANSACTION].TextShSA AS SharhRadif, [TRANSACTION].PARBesCur AS BestankarArzi, [TRANSACTION].DARBedCur AS BedehkarArzi, 
								  [TRANSACTION].CurRef AS NoeArz, [TRANSACTION].CurRate AS NerkhArz, T_SA2.Sharh AS SharhSanad, Currency.Title AS NameArz, T_Heasb.HESABID, T_Heasb.HESABNumber AS CodeMoin, 
								  T_Heasb.NameHesab AS NameHesabMoin
			FROM         [TRANSACTION] INNER JOIN
								  T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
								  Currency ON [TRANSACTION].CurRef = Currency.CurrencyID INNER JOIN
								  T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID LEFT OUTER JOIN
									  (SELECT     SUM(DARBedCur) - SUM(PARBesCur) AS mande, SUM(PARBesCur) AS SumOfPARBes, SUM(DARBedCur) AS SumOfDARBed, HESABID
										 FROM         [TRANSACTION]
										 WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
										 GROUP BY HESABID) AS DERIVEDTBL ON T_Heasb.HESABID = DERIVEDTBL.HESABID
			WHERE     (T_Heasb.HESABID = @HESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)

  ELSE

	IF (@TYPID=113  )        /* گزارش حساب معین ارزی و ریالی */                    
			SELECT     DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBed AS Bedehkar, [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, 
								  [TRANSACTION].TextShSA AS SharhRadif, [TRANSACTION].PARBesCur AS BestankarArzi, [TRANSACTION].DARBedCur AS BedehkarArzi, [TRANSACTION].CurRef AS NoeArz, 
								  [TRANSACTION].CurRate AS NerkhArz, T_SA2.Sharh AS SharhSanad, Currency.Title AS NameArz, TBL_MandeArz.mandeCur AS mandeArz, T_Heasb.HESABID, T_Heasb.HESABNumber AS CodeMoin, 
								  T_Heasb.NameHesab AS NameHesabMoin
			FROM         (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, HESABID
									FROM         [TRANSACTION]
									WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
									GROUP BY HESABID) AS DERIVEDTBL RIGHT OUTER JOIN
								  [TRANSACTION] INNER JOIN
								  T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
								  T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID LEFT OUTER JOIN
									  (SELECT     SUM(DARBedCur) - SUM(PARBesCur) AS mandeCur, SUM(PARBesCur) AS SumOfPARBesCur, SUM(DARBedCur) AS SumOfDARBedCur, HESABID
										 FROM         [TRANSACTION]
										 WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
										 GROUP BY HESABID) AS TBL_MandeArz ON T_Heasb.HESABID = TBL_MandeArz.HESABID ON DERIVEDTBL.HESABID = T_Heasb.HESABID LEFT OUTER JOIN
								  Currency ON [TRANSACTION].CurRef = Currency.CurrencyID
			WHERE     (T_Heasb.HESABID = @HESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @DATE2) AND (NOT ([TRANSACTION].SAID2 IS NULL))


END






GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepKol1_Rep1]    Script Date: 11/16/2017 18:07:22 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_RepKol1_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_RepKol1_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepKol1_Rep1]    Script Date: 11/16/2017 18:07:22 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_RepKol1_Rep1]
		
		@Userid INT,	
		@Date1 NVARCHAR(10),
		@Date2 NVARCHAR(10),
		@KOLID INT,
		@TYPID INT

AS
BEGIN
	

IF (@TYPID=100 ) /* گزارش دفتر کل */
	
	
SELECT     T_KolHesab.NameHesab AS NameHesabKol, DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBed AS Bedehkar, [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].Date1 AS Tarikh, 
                      T_SA2.SAID2Number AS ShomareSanad, T_KolHesab.KOLID, T_KolHesab.KolNumber AS CodeHesab, [TRANSACTION].TextShSA AS SharhRadif
FROM         T_KolHesab INNER JOIN
                      [TRANSACTION] ON T_KolHesab.KOLID = [TRANSACTION].KOLID INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, KOLID
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @date1)
                             GROUP BY KOLID) AS DERIVEDTBL ON T_KolHesab.KOLID = DERIVEDTBL.KOLID
WHERE     (T_KolHesab.KOLID = @Kolid) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)
                             
ELSE

	IF (@TYPID=110  ) /* گزارش حساب کل ارزی */
		SELECT     T_KolHesab.NameHesab AS NameHesabKol, DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBedCur AS Bedehkar, [TRANSACTION].PARBesCur AS Bestankar, 
							  [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_KolHesab.KOLID, T_KolHesab.KolNumber AS CodeHesab, [TRANSACTION].TextShSA AS SharhRadif, 
							  [TRANSACTION].PARBesCur AS BestankarArzi, [TRANSACTION].DARBedCur AS BedehkarArzi, [TRANSACTION].CurRef AS NoeArz, [TRANSACTION].CurRate AS NerkhArz, T_SA2.Sharh AS SharhSanad, 
							  Currency.Title AS NameArz
		FROM         [TRANSACTION] INNER JOIN
							  T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
							  Currency ON [TRANSACTION].CurRef = Currency.CurrencyID INNER JOIN
							  T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
								  (SELECT     SUM(DARBedCur) - SUM(PARBesCur) AS mande, SUM(PARBesCur) AS SumOfPARBes, SUM(DARBedCur) AS SumOfDARBed, KOLID
									 FROM         [TRANSACTION] AS TRANSACTION_1
									 WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @date1)
									 GROUP BY KOLID) AS DERIVEDTBL ON T_KolHesab.KOLID = DERIVEDTBL.KOLID
		WHERE     (T_KolHesab.KOLID = @Kolid) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)

  ELSE

	IF (@TYPID=111  )        /* گزارش حساب کل ارزی و ریالی */                    
						SELECT     T_KolHesab.NameHesab AS NameHesabKol, DERIVEDTBL.mande AS MandeHesab, TRANSACTION_2.DARBed AS Bedehkar, TRANSACTION_2.PARBes AS Bestankar, 
						TRANSACTION_2.Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_KolHesab.KOLID, T_KolHesab.KolNumber AS CodeHesab, TRANSACTION_2.TextShSA AS SharhRadif, 
						TRANSACTION_2.PARBesCur AS BestankarArzi, TRANSACTION_2.DARBedCur AS BedehkarArzi, TRANSACTION_2.CurRef AS NoeArz, TRANSACTION_2.CurRate AS NerkhArz, 
						T_SA2.Sharh AS SharhSanad, Currency.Title AS NameArz, TBL_MandeArz.mandeCur AS mandeArz, T_SA2.SAID2
						FROM         (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, KOLID
						FROM         [TRANSACTION]
						WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @date1)
						GROUP BY KOLID) AS DERIVEDTBL RIGHT OUTER JOIN
						[TRANSACTION] AS TRANSACTION_2 INNER JOIN
						T_SA2 ON TRANSACTION_2.SAID2 = T_SA2.SAID2 INNER JOIN
						T_KolHesab ON TRANSACTION_2.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
						(SELECT     SUM(DARBedCur) - SUM(PARBesCur) AS mandeCur, SUM(PARBesCur) AS SumOfPARBesCur, SUM(DARBedCur) AS SumOfDARBedCur, KOLID
						FROM         [TRANSACTION] AS TRANSACTION_1
						WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @date1)
						GROUP BY KOLID) AS TBL_MandeArz ON T_KolHesab.KOLID = TBL_MandeArz.KOLID ON DERIVEDTBL.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
						Currency ON TRANSACTION_2.CurRef = Currency.CurrencyID
						WHERE     (T_KolHesab.KOLID = @Kolid) AND (TRANSACTION_2.Date1 BETWEEN @daTE1 AND @DATE2) AND (NOT (TRANSACTION_2.SAID2 IS NULL))
				
	

END






GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Pardakht_Rep1]    Script Date: 11/16/2017 18:07:19 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Pardakht_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Pardakht_Rep1]
GO

/****** Object:  StoredProcedure [dbo].[QSubPFrm_Pardakht_Rep1]    Script Date: 11/16/2017 18:07:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE  PROCEDURE [dbo].[QSubPFrm_Pardakht_Rep1]
		
		@Userid INT,@PardakhtID INT

AS

BEGIN

SELECT     T_Heasb.HESABNumber AS CodeMoin, T_KolHesab.KolNumber AS CodeKol, T_T_Heasb.THESABNumber AS CodeTafsili, [TRANSACTION].DARBed AS mablagh, T_Par1.DATE2 AS Tarikh, 
                      T_Par1.TextSharh AS Sharh, T_Par1.PARID1Number AS Serial, CASE WHEN isnull([TRANSACTION].THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull([TRANSACTION].HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Par1.PARID1
FROM         [TRANSACTION] INNER JOIN
                      T_Par1 ON [TRANSACTION].PARID1 = T_Par1.PARID1 LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID LEFT OUTER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID
WHERE     ([TRANSACTION].DARBed > 0) AND (T_Par1.PARID1 = @PardakhtID)


END



GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1]    Script Date: 11/16/2017 18:07:15 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Kharid_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1]    Script Date: 11/16/2017 18:07:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


Create  PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1]
		
		@Userid INT,	
		@KharidID int

AS
BEGIN
SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, 
                      T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, T_NameVahed.NameVahed AS NameVahedAsli, 
                      T_NameVahed_1.NameVahed AS NameVahedFarei, T_NameKala.Zarib, T_NameKala.BarCode, T_NameKala.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, 
                      T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, 
                      T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Email, T_NameKala.NameKala1, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(dbo.T_NameKala.Zarib, 0) 
                      = 0 THEN 1 ELSE dbo.T_NameKala.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, T_Anbar.AIDNumber, T_Anbar.NameAnbar, T_Anbar.Sharh AS AddAnbar
FROM         T_Heasb RIGHT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 1) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL RIGHT OUTER JOIN
                      T_Anbar INNER JOIN
                      T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON T_Anbar.AID = [TRANSACTION].AID ON TBL.FactID = T_Kharid.KHARIDID LEFT OUTER JOIN
                      T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID ON T_Heasb.HESABID = T_Kharid.HesabID LEFT OUTER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
                      T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1
WHERE     (T_Kharid.KHARIDID = @KharidID)
END



GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Khadamat_Rep1]    Script Date: 11/16/2017 18:07:10 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Khadamat_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Khadamat_Rep1]
GO

/****** Object:  StoredProcedure [dbo].[QSubPFrm_Khadamat_Rep1]    Script Date: 11/16/2017 18:07:10 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE  PROCEDURE [dbo].[QSubPFrm_Khadamat_Rep1]
		
		@Userid INT,@KhadamatID INT

AS

BEGIN

SELECT     TRANSACTION_1.TedadDKh AS Tedad, TRANSACTION_1.UnitPriceDKh AS netprice, 
                      CASE WHEN T_DKhad.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_DKhad.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_DKhad.THESAB AS CodeTafsili, 
                      T_DKhad.KOLID AS CodeKol, T_DKhad.Hesab AS CodeMoin, TRANSACTION_1.FieldN3 AS Mablagh, T_DKhad.Takhfif, T_DKhad.DateKh AS Tarikh, TRANSACTION_1.Sharh AS SharhRadif, 
                      T_DKhad.Sharh AS TozihatFactor, T_NameKhad.Unit AS NameVahedAsli, T_NameKhad.KhadamatNumber AS CodeKala, T_DKhad.DKHNumbr AS Serial, T_DKhad.NumberKH AS ShomareFactor, 
                      T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, 
                      T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_NameKhad.Name AS NameKala1, 
                      T_DKhad.DKHID, DERIVEDTBL.mande AS MandeHesab
FROM         (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESABID, KOLID, ISNULL(THESABID, 0) 
                                                   AS THESABID
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL RIGHT OUTER JOIN
                      T_NameKhad INNER JOIN
                      [TRANSACTION] AS TRANSACTION_1 ON T_NameKhad.NAMEKHID = TRANSACTION_1.NAMEKHID INNER JOIN
                          (SELECT     ISNULL(THESABID, 0) AS THESAB, KOLID, ISNULL(HesabID, 0) AS Hesab, Takhfif, DateKh, Sharh, DKHNumbr, NumberKH, DKHID
                             FROM         T_DKhad AS T_DKhad_1) AS T_DKhad ON TRANSACTION_1.DKHID = T_DKhad.DKHID LEFT OUTER JOIN
                      T_T_Heasb ON T_DKhad.THESAB = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_KolHesab ON T_DKhad.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_Heasb ON T_DKhad.Hesab = T_Heasb.HESABID ON DERIVEDTBL.THESABID = T_DKhad.THESAB AND DERIVEDTBL.KOLID = T_DKhad.KOLID AND DERIVEDTBL.HESABID = T_DKhad.Hesab
WHERE     (T_DKhad.DKHID = @KhadamatID)

END



GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ForooshSandogh_Rep1]    Script Date: 11/16/2017 18:07:07 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ForooshSandogh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ForooshSandogh_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ForooshSandogh_Rep1]    Script Date: 11/16/2017 18:07:07 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


create  PROCEDURE [dbo].[QSubPFrm_ForooshSandogh_Rep1]
		@Userid INT,	
		@FOROSHID int

AS
BEGIN
	
SELECT     [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
                      CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, 
                      T_Forosh.KOLID AS CodeKol, T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, T_Forosh.Takhfif, T_Forosh.DateFo AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, 
                      T_Forosh.Sharh AS TozihatFactor, T_NameVahed.NameVahed AS NameVahedAsli, T_NameVahed_1.NameVahed AS NameVahedFarei, T_NameKala.Zarib, T_NameKala.BarCode, 
                      T_NameKala.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, T_Forosh.NumberKH AS ShomareFactor, 
                      T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, 
                      T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_NameKala.NameKala1, T_Forosh.FOROSHID, 
                      DERIVEDTBL.mande AS MandeHesab, ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(dbo.T_NameKala.Zarib, 0) = 0 THEN 1 ELSE dbo.T_NameKala.Zarib END, 2) AS tedadFarei, 
                      [TRANSACTION].radif, T_Anbar.AIDNumber AS CodeAnbar, T_Anbar.NameAnbar, T_Anbar.Sharh AS AddressAnbar
FROM         T_NameVahed RIGHT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 2) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL RIGHT OUTER JOIN
                      T_KolHesab RIGHT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) 
                                                   AS THESAB
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL RIGHT OUTER JOIN
                      T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID INNER JOIN
                          (SELECT     ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID
                             FROM         T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID INNER JOIN
                      T_Anbar ON [TRANSACTION].AID = T_Anbar.AID ON DERIVEDTBL.KOLID = T_Forosh.KOLID AND DERIVEDTBL.HESAB = T_Forosh.Hesab AND 
                      DERIVEDTBL.THESAB = T_Forosh.THesab LEFT OUTER JOIN
                      T_Heasb ON T_Forosh.Hesab = T_Heasb.HESABID LEFT OUTER JOIN
                      T_T_Heasb ON T_Forosh.THesab = T_T_Heasb.THESABID ON T_KolHesab.KOLID = T_Forosh.KOLID ON TBL.FactID = T_Forosh.FOROSHID ON 
                      T_NameVahed.VAHED1 = T_NameKala.VAHED1 LEFT OUTER JOIN
                      T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1
WHERE     (T_Forosh.FOROSHID = @FOROSHID)
END






GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Rep1]    Script Date: 11/16/2017 18:07:03 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Rep1]    Script Date: 11/16/2017 18:07:03 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_Rep1]
		
		@Userid INT,	
		@FOROSHID int

AS
BEGIN
	
SELECT     [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
                      CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, 
                      T_Forosh.KOLID AS CodeKol, T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, [TRANSACTION].FieldN7 AS Takhfif, T_Forosh.DateFo AS Tarikh, 
                      [TRANSACTION].Sharh AS SharhRadif, T_Forosh.Sharh AS TozihatFactor, T_NameVahed.NameVahed AS NameVahedAsli, T_NameVahed_1.NameVahed AS NameVahedFarei, T_NameKala.Zarib, 
                      T_NameKala.BarCode, T_NameKala.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, T_Forosh.NumberKH AS ShomareFactor, 
                      T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, 
                      T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_NameKala.NameKala1, T_Forosh.FOROSHID, 
                      DERIVEDTBL.mande AS MandeHesab, ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(dbo.T_NameKala.Zarib, 0) = 0 THEN 1 ELSE dbo.T_NameKala.Zarib END, 2) AS tedadFarei, 
                      [TRANSACTION].radif, T_Anbar.AIDNumber AS CodeAnbar, T_Anbar.NameAnbar, T_Anbar.Sharh AS AddressAnbar
FROM         T_NameVahed RIGHT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 2) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL RIGHT OUTER JOIN
                      T_KolHesab RIGHT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) 
                                                   AS THESAB
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL RIGHT OUTER JOIN
                      T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID INNER JOIN
                          (SELECT     ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID
                             FROM         T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID INNER JOIN
                      T_Anbar ON [TRANSACTION].AID = T_Anbar.AID ON DERIVEDTBL.KOLID = T_Forosh.KOLID AND DERIVEDTBL.HESAB = T_Forosh.Hesab AND 
                      DERIVEDTBL.THESAB = T_Forosh.THesab LEFT OUTER JOIN
                      T_Heasb ON T_Forosh.Hesab = T_Heasb.HESABID LEFT OUTER JOIN
                      T_T_Heasb ON T_Forosh.THesab = T_T_Heasb.THESABID ON T_KolHesab.KOLID = T_Forosh.KOLID ON TBL.FactID = T_Forosh.FOROSHID ON 
                      T_NameVahed.VAHED1 = T_NameKala.VAHED1 LEFT OUTER JOIN
                      T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1
WHERE     (T_Forosh.FOROSHID = @FOROSHID)
END






GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Daryaft_Rep1]    Script Date: 11/16/2017 18:06:59 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Daryaft_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Daryaft_Rep1]
GO

/****** Object:  StoredProcedure [dbo].[QSubPFrm_Daryaft_Rep1]    Script Date: 11/16/2017 18:06:59 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE  PROCEDURE [dbo].[QSubPFrm_Daryaft_Rep1]
		
		@Userid INT,@DaryaftID INT

AS

BEGIN

SELECT     T_Heasb.HESABNumber AS CodeMoin, T_KolHesab.KolNumber AS CodeKol, T_T_Heasb.THESABNumber AS CodeTafsili, [TRANSACTION].PARBes AS mablagh, T_Dar1.DATE2 AS Tarikh, 
                      T_Dar1.TextSharh AS Sharh, T_Dar1.DAR1Numbr AS Serial, CASE WHEN isnull([TRANSACTION].THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull([TRANSACTION].HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Dar1.DARID1
FROM         T_Dar1 INNER JOIN
                      [TRANSACTION] ON T_Dar1.DARID1 = [TRANSACTION].DARID1 LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID LEFT OUTER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID
WHERE     ([TRANSACTION].PARBes > 0) AND (T_Dar1.DARID1 = @DaryaftID)


END



GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_BarKharid_Rep1]    Script Date: 11/16/2017 18:06:55 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_BarKharid_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_BarKharid_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_BarKharid_Rep1]    Script Date: 11/16/2017 18:06:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


Create  PROCEDURE [dbo].[QSubPFrm_BarKharid_Rep1]
		
		@Userid INT,
		@BarKharidID int

AS

BEGIN

SELECT     [TRANSACTION].TedadBarKh AS Tedad, [TRANSACTION].UnitPriceBarKh AS netprice, CASE WHEN isnull(T_Dar8.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Dar8.HESABID, 
                      0) <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Dar8.THESABID AS CodeTafsili, T_Dar8.KOLID AS CodeKol, T_Dar8.HesabID AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, T_Dar8.Takhfif, 
                      T_Dar8.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Dar8.Sharh AS TozihatFactor, T_NameVahed.NameVahed AS NameVahedAsli, T_NameVahed_1.NameVahed AS NameVahedFarei, 
                      T_NameKala.Zarib, T_NameKala.BarCode, T_NameKala.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Dar8.DARID8Numbr AS Serial, 
                      T_Dar8.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, 
                      T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, 
                      T_NameKala.NameKala1, T_Dar8.DARID8
FROM         T_KolHesab RIGHT OUTER JOIN
                      T_Dar8 INNER JOIN
                      [TRANSACTION] INNER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID ON T_Dar8.DARID8 = [TRANSACTION].DARID8 LEFT OUTER JOIN
                      T_Heasb ON T_Dar8.HesabID = T_Heasb.HESABID LEFT OUTER JOIN
                      T_T_Heasb ON T_Dar8.THESABID = T_T_Heasb.THESABID ON T_KolHesab.KOLID = T_Dar8.KOLID LEFT OUTER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
                      T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1 LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 3) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Dar8.DARID8 = TBL.FactID
WHERE     (T_Dar8.DARID8 = @BarKharidID)	

END



GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_BarForoosh_Rep1]    Script Date: 11/16/2017 18:06:44 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_BarForoosh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_BarForoosh_Rep1]
GO

/****** Object:  StoredProcedure [dbo].[QSubPFrm_BarForoosh_Rep1]    Script Date: 11/16/2017 18:06:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE  PROCEDURE [dbo].[QSubPFrm_BarForoosh_Rep1]
		
		@Userid INT,@BarForoosh INT

AS

BEGIN

SELECT     [TRANSACTION].TedadBarFo AS Tedad, [TRANSACTION].UnitPriceBarFo AS netprice, CASE WHEN isnull(T_Par7.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Par7.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Par7.THESABID AS CodeTafsili, T_Par7.KOLID AS CodeKol, T_Par7.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, 
                      T_Par7.Takhfif, T_Par7.DateFo AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Par7.Sharh AS TozihatFactor, T_NameVahed.NameVahed AS NameVahedAsli, 
                      T_NameVahed_1.NameVahed AS NameVahedFarei, T_NameKala.Zarib, T_NameKala.BarCode, T_NameKala.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, 
                      T_Par7.PARID7Number AS Serial, T_Par7.NumberFO AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, 
                      T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Email, T_NameKala.NameKala1, T_Par7.PARID7
FROM         T_Heasb RIGHT OUTER JOIN
                      [TRANSACTION] INNER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID INNER JOIN
                      T_Par7 ON [TRANSACTION].PARID7 = T_Par7.PARID7 LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 4) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Par7.PARID7 = TBL.FactID ON T_Heasb.HESABID = T_Par7.HesabID LEFT OUTER JOIN
                      T_KolHesab ON T_Par7.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON T_Par7.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
                      T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1
WHERE     (T_Par7.PARID7 = @BarForoosh)


END



GO




/****** Object:  StoredProcedure [dbo].[Rep_SoratHesab]    Script Date: 11/16/2017 18:13:08 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_SoratHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_SoratHesab]


/****** Object:  StoredProcedure [dbo].[Rep_SoratHesab]    Script Date: 11/16/2017 18:13:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  CREATE PROCEDURE [dbo].[Rep_SoratHesab] ( 
@THESABID int,
@HESABID int,
@KOLID int,
@GoID INT,
@Date1 NVARCHAR(10),
@Date2 NVARCHAR(10)
  )           
  AS     
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';                  	
  SET @ParmDefinition = N'@THESABID int=null, 								
						@HESABID int=null, 								
						@KOLID  int=null, 								
						@GoID int=null,
						@Date1 NVARCHAR(10)=null,
						@Date2 NVARCHAR(10)=null  ';            	
	
	IF(NOT (@THESABID IS NULL))	 
		SET @SQLString =  ' 
SELECT     ISNULL(RadifSAID2, ISNULL(radif, 1000)) AS RadifSAID2, NameHesabmoin, ISNULL(NameKala2, SharhRadif) AS SharhRadif, MandeHesab, Bedehkar, Bestankar, Tarikh, ShomareSanad, HesabID, 
                      CodeHesab, srtfield, tedad, unitprice, NameVahed, NameKala, MatnFi, SAIDType, NoeAmaliyat, TedadSadere, TedadVarede
FROM         dbo.F_SoratHesabTafsili(@Date1) AS F_SoratHesabTafsili_1
WHERE     (Tarikh BETWEEN @Date1 AND @Date2) AND (HesabID = @THESABID)
ORDER BY Tarikh
          
'
        
   else IF(NOT (@HESABID IS NULL))	 
		SET @SQLString ='SELECT     ISNULL(RadifSAID2, ISNULL(radif, 1000)) AS RadifSAID2, NameHesabmoin, ISNULL(NameKala2, SharhRadif) AS SharhRadif, MandeHesab, Bedehkar, Bestankar, Tarikh, ShomareSanad, HesabID, CodeHesab, srtfield, tedad, unitprice, NameVahed, NameKala, 
                      MatnFi, SAIDType, NoeAmaliyat, TedadSadere, TedadVarede
FROM         dbo.F_SoratHesabMoein(@Date1) AS F_SoratHesabMoein_1
WHERE     (Tarikh BETWEEN @Date1 AND @Date2) AND (HesabID = @HESABID) ' 	
	else IF(NOT (@KOLID IS NULL))	 
		SET @SQLString ='
		SELECT     ISNULL(RadifSAID2, ISNULL(radif, 1000)) AS RadifSAID2, NameHesabmoin, ISNULL(NameKala2, SharhRadif) AS SharhRadif, MandeHesab, Bedehkar, Bestankar, Tarikh, ShomareSanad, HesabID, CodeHesab, srtfield, tedad, unitprice, NameVahed, NameKala, 
                      MatnFi, SAIDType, NoeAmaliyat, TedadSadere, TedadVarede
FROM        dbo.F_SoratHesabKol(@Date1) AS F_SoratHesabKol_1 
WHERE     (Tarikh BETWEEN @Date1 AND @Date2) AND (HESABID = @KOLID)
' 	
  /* -----------------------نشان دادن کالاهای فعال */ 
	
/*if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' HAVING      (T_NameKala.Active = 0) '; 					
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' HAVING      (T_NameKala.Active > -1000) '; 	
               
  /* -----------------------شرط انبار */ 			
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Kala.AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 			
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Kala.GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	
*/
						

set @SQLString=@SQLString 	
     			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@THESABID=@THESABID ,@HESABID=@HESABID,@KOLID=@KOLID,@GoID=@GoID,@Date1=@Date1,@Date2=@Date2;  		
return END 



GO





/****** Object:  StoredProcedure [dbo].[RepFST_0523894024SF1]    Script Date: 11/17/2017 20:19:08 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RepFST_0523894024SF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[RepFST_0523894024SF1]
GO



/****** Object:  StoredProcedure [dbo].[RepFST_0523894024SF1]    Script Date: 11/17/2017 20:19:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

   CREATE PROCEDURE [dbo].[RepFST_0523894024SF1] (  @NameKalaID int, @TxtNameKalaID NVARCHAR(255), @AID int, @GOKID int, @Date1 NVARCHAR(10), @Date2 NVARCHAR(10)   )              AS        BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	   DECLARE @WhrStatement  NVARCHAR(2000);         	   DECLARE @ParmDefinition  NVARCHAR(2000);         	   DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   set @WhrStatement  = N'';         	   set @ParmAndstr = N'';                  	   SET @ParmDefinition = N'@NameKalaID int=null,  						@TxtNameKalaID NVARCHAR(255)=null,								 						@AID int=null, 								 						@GOKID  int=null, 								 						@Date1 NVARCHAR(10)=null, 						@Date2 NVARCHAR(10)=null  ';            	 	    	if(@Date1 IS NULL) 			    	BEGIN 				    		set @Date1='0'; 				    	END    	 	       	if(@Date2 IS NULL) 			    	BEGIN 				    		set @Date2='99999999'; 				    	END    	 		SET @SQLString =  '  SELECT     TRaKalaid.KALAID, T_NameGoKala.NameGoKalanumbr,T_NameGoKala.NameGoKala1, T_Anbar.AIDNumber, T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode,                        T_NameVahed.NameVahed, SUM(TRaKalaid.Varde) AS Varde, SUM(TRaKalaid.Sadre) AS Sadre, SUM(TRaKalaid.Varde - TRaKalaid.Sadre) AS Mande FROM         T_Anbar INNER JOIN                           (SELECT     KALAID, AID, GOKID, TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, Date1                              FROM         [TRANSACTION]                              WHERE     (NOT (KALAID IS NULL))                              UNION ALL                              SELECT     NameKalaID, AID, GOKID, Mojodi AS Varde, 0 AS Sadre, dbo.F1StartDate() AS Date12                              FROM         T_Kala                              WHERE     (Mojodi <> 0)) AS TRaKalaid INNER JOIN                       T_NameVahed INNER JOIN                       T_NameKala ON T_NameVahed.VAHED1 = T_NameKala.VAHED1 ON TRaKalaid.KALAID = T_NameKala.ID INNER JOIN                       T_NameGoKala ON TRaKalaid.GOKID = T_NameGoKala.ID ON T_Anbar.AID = TRaKalaid.AID  WHERE (Date1 BetWeen @Date1 and  @Date2)             '   set @ParmAndstr=' AND ' 			 if(NOT (@NameKalaID  IS NULL)) 			 BEGIN 				 set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameKalaID  = @NameKalaID  ) '; 				 set @ParmAndstr=' AND ' 			 END   set @ParmAndstr=' AND ' 			 if(NOT (@TxtNameKalaID   IS NULL)) 			 BEGIN 	set @TxtNameKalaID ='%' + @TxtNameKalaID + '%'	 				 set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameKala1  Like @TxtNameKalaID ) '; 				 set @ParmAndstr=' AND ' 			 END   set @ParmAndstr=' AND ' 			 if(NOT (@AID  IS NULL)) 			 BEGIN 				 set @WhrStatement= @WhrStatement + @ParmAndstr + N' (TRaKalaid.AID = @AID ) '; 				 set @ParmAndstr=' AND ' 			 END 			   /* -----------------------شرط گروه کالا  */  set @ParmAndstr=' AND ' 			 if(NOT (@GoKID  IS NULL)) 			 BEGIN 				 	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (GOKID = @GoKID ) '; 				 	set @ParmAndstr=' AND ' 			 END 	  		  set @SQLString=@SQLString +@WhrStatement+	' GROUP BY TRaKalaid.KALAID, T_NameGoKala.NameGoKalanumbr,T_NameGoKala.NameGoKala1, T_Anbar.AIDNumber, T_Anbar.NameAnbar, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode,                        T_NameVahed.NameVahed	HAVING      (SUM(TRaKalaid.Varde - TRaKalaid.Sadre) <> 0)'	      			 EXECUTE sp_executesql   @SQLString, @ParmDefinition,@nameKalaID=@nameKalaID ,@TxtNameKalaID=@TxtNameKalaID,@aiD=@aiD,@gokID=@gokID,@Date1=@Date1,@Date2=@Date2;  		 return END     
GO




/****** Object:  StoredProcedure [dbo].[Rep_Table1_JabejaeiSF1]    Script Date: 11/17/2017 20:19:04 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_Table1_JabejaeiSF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_Table1_JabejaeiSF1]
GO


/****** Object:  StoredProcedure [dbo].[Rep_Table1_JabejaeiSF1]    Script Date: 11/17/2017 20:19:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

     CREATE PROCEDURE [dbo].[Rep_Table1_JabejaeiSF1] (  @USERID int, @IDKeyFact int   )              AS        BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	   DECLARE @WhrStatement  NVARCHAR(2000);         	   DECLARE @ParmDefinition  NVARCHAR(2000);         	   DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   set @WhrStatement  = N'';         	   set @ParmAndstr = N'';                  	   SET @ParmDefinition = N'@USERID int=null, 								 						@IDKeyFact int=null';            	 	  		SET @SQLString =' SELECT     Table1.ID1, Table1.ID1Number, T_Anbar.AIDNumber AS AIDNumberMabda, T_Anbar.NameAnbar AS NameAnbarMabda, T_Anbar_1.AIDNumber AS AIDNumberMagh,                        T_Anbar_1.NameAnbar AS NameAnbarMagh, Table1.AID1 AS AID1Mabda, Table1.AID2 AS AID2Magh, [TRANSACTION].TedadKhA, [TRANSACTION].MabVahedHazine,                        [TRANSACTION].Sharh AS SharhRadif, Table1.DateID1, T_NameKala.NameKala1, Table1.Sharh, [TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine AS MabHazine FROM         Table1 INNER JOIN                       [TRANSACTION] ON Table1.ID1 = [TRANSACTION].ID1 INNER JOIN                       T_Anbar ON Table1.AID1 = T_Anbar.AID INNER JOIN                       T_Anbar AS T_Anbar_1 ON Table1.AID2 = T_Anbar_1.AID INNER JOIN                       T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID WHERE     ([TRANSACTION].TedadKhA > 0) AND (Table1.ID1 = @IDKeyFact)' 	   						  set @SQLString=@SQLString 	      			 EXECUTE sp_executesql   @SQLString, @ParmDefinition,@USERID=@USERID ,@IDKeyFact=@IDKeyFact;  		 return END   
GO




/****** Object:  StoredProcedure [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1]    Script Date: 11/17/2017 20:18:59 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_Table1_Jabejaei_KolKeriehSF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1]
GO



/****** Object:  StoredProcedure [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1]    Script Date: 11/17/2017 20:18:59 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

       CREATE PROCEDURE [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1] (        @USERID int, @date1 NVARCHAR(10), @date2 NVARCHAR(10), @Goid INT,@Kolid INT,@Hesabid INT,@THesabid INT    )              AS              BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	         DECLARE @WhrStatement  NVARCHAR(2000);         	         DECLARE @ParmDefinition  NVARCHAR(2000);         	         DECLARE @ParmAndstr  NVARCHAR(2000);         	 	         set @WhrStatement  = N'';         	         set @ParmAndstr = N'';                  	         SET @ParmDefinition = N'@USERID int=null, 							 							@date1 NVARCHAR(10)=null, 							 							@date2 NVARCHAR(10)=null, 							 							@Goid INT=null, 							 							@Kolid INT=null, 							 							@Hesabid INT=null, 							 							@THesabid INT=null';            	  		 SET @SQLString =' SELECT     Table1.ID1, Table1.ID1Number, SUM([TRANSACTION].TedadKhA) AS TedadKhA, [TRANSACTION].MabVahedHazine, Table1.DateID1,                        SUM([TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine) AS MabHazine, dbo.HesabName(Table1.KOLID, T_Anbar.hesabid, Table1.THESABID) AS NameDriver, [TRANSACTION].GOKID,                        T_NameGoKala.NameGoKala1, T_NameGoKala.NameGoKalanumbr, T_NameGoKala.ID, CONVERT(nvarchar(255), [TRANSACTION].MabVahedHazine) + T_NameGoKala.NameGoKala1 AS Grp FROM         Table1 INNER JOIN                       [TRANSACTION] ON Table1.ID1 = [TRANSACTION].ID1 INNER JOIN                       T_Anbar ON Table1.AID1 = T_Anbar.AID INNER JOIN                       T_Anbar AS T_Anbar_1 ON Table1.AID2 = T_Anbar_1.AID INNER JOIN                       T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID INNER JOIN                       T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID WHERE     ([TRANSACTION].TedadKhA > 0) AND (Table1.DateID1 BETWEEN @Date1 AND @Date2) AND (Table1.KOLID = @KOLID) AND (Table1.HesabID = @HesabID) AND (Table1.THESABID = @THESABID) GROUP BY Table1.ID1, Table1.ID1Number, [TRANSACTION].MabVahedHazine, Table1.DateID1, dbo.HesabName(Table1.KOLID, T_Anbar.hesabid, Table1.THESABID), [TRANSACTION].GOKID,                        T_NameGoKala.NameGoKala1, T_NameGoKala.NameGoKalanumbr, T_NameGoKala.ID, CONVERT(nvarchar(255), [TRANSACTION].MabVahedHazine) + T_NameGoKala.NameGoKala1 HAVING      (SUM([TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine) > 0)' 	   						  set @SQLString=@SQLString 	  EXECUTE sp_executesql   @SQLString, @ParmDefinition,@USERID=@USERID ,@date1=@date1,@date2=@date2,@Goid=@Goid,@Kolid=@Kolid,@Hesabid=@Hesabid,@THesabid=@THesabid;  		 return END   
GO



/****** Object:  StoredProcedure [dbo].[Rep_SooDRooZanehSF1]    Script Date: 11/17/2017 20:18:55 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_SooDRooZanehSF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_SooDRooZanehSF1]
GO






/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1]    Script Date: 11/17/2017 20:18:51 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1]    Script Date: 11/17/2017 20:18:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[QSubPfrm_ListJabejaeiKala_Table1SF1] AS                                    SELECT      Table1.ID1, CONVERT(float, Table1.ID1Number) AS ID1Number, Table1.NumberKH, Table1.NumSID2, Table1.VSabt, Table1.PrimarySaid2, Table1.Dateeng, Table1.DateCreate,                        Table1.DateModified, Table1.HesabID, Table1.GOID, Table1.KOLID, Table1.MOINID, Table1.THESABID, Table1.THESABIDAmel, Table1.TextName, Table1.TextName1, Table1.TextNameT,                        Table1.DateID1, Table1.Takhfif, Table1.TextShSA, Table1.Sharh, Table1.FieldN1, Table1.FieldN2, Table1.FieldN3, Table1.FieldN4, Table1.FieldT1, Table1.FieldT2, Table1.FieldT3, Table1.FieldT4,                        Table1.Flag1, Table1.Flag2, Table1.Flag3, Table1.Flag4, Table1.Flag5, Table1.Flag6, Table1.Flag7, Table1.Flag8, Table1.UserID, Table1.Shift, Table1.FlgSabtFact, Table1.SalaTypeid,                        Table1.Maliyat, Table1.MablaghKol, Table1.Avarez, Table1.Ezafat, Table1.Poorsant, Table1.userCreate, Table1.userModify, Table1.dateSettlement, Table1.NameAID1, Table1.NameAID2,                        Table1.NameKala, Table1.NameGorup1, Table1.NameGorup2, Table1.AID1, Table1.AID2, T_SA2.SAID2Number, T_SA2.Sharh AS Expr1, T_Anbar_1.NameAnbar AS NameAnbar1,                        T_Anbar.NameAnbar AS NameAnbar2, dbo.HesabName(Table1.KOLID, Table1.HesabID, Table1.THESABID) AS Namehesab FROM         T_Anbar AS T_Anbar_1 RIGHT OUTER JOIN                       T_SA2 RIGHT OUTER JOIN                       Table1 LEFT OUTER JOIN                       T_Anbar ON Table1.AID2 = T_Anbar.AID ON T_SA2.SAID2 = Table1.PrimarySaid2 ON T_Anbar_1.AID = Table1.AID1 ORDER BY Table1.DateID1 DESC, ID1Number DESC
GO













/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdarySF3]    Script Date: 11/23/2017 18:03:16 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdarySF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdarySF3]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdarySF3]    Script Date: 11/23/2017 18:03:16 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO


CREATE PROCEDURE [dbo].[QSubPFrm_SanadHesabdarySF3] (@TxtLinkToSub nvarchar(255), @option1 bit) AS 

SELECT      TRID, Flag1, Flag2, Date1, Sharh, SAID, HESABID, PARBes, DARBed, radif, GOID, KOLID, THESABID, FieldT1, TextNumberSA, TextShSA, FieldN1, TRAID, Flag3, TextNHSID2, 
                      CurMablagh, CurRef, CurRate, FieldT2, ThesabIDSath2
FROM         TRANSACTION2
WHERE     (Date1 = @TxtLinkToSub) AND (Flag1 = @option1)
ORDER BY radif


GO






/****** Object:  StoredProcedure [dbo].[Rep_021239]    Script Date: 11/27/2017 20:42:59 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_021239]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_021239]
GO


/****** Object:  StoredProcedure [dbo].[Rep_021239]    Script Date: 11/27/2017 20:42:59 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[Rep_021239](@startSanadID int, @endSanadID int, @startDate nvarchar(255), @enddate nvarchar(255), @AccID nvarchar(50), @IdKala int) AS SELECT     dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].TedadFo, dbo.[TRANSACTION].UnitPriceFo AS AvgOfUnitPricefo, ABS(dbo.[TRANSACTION].FieldN3)                        AS mablagh, dbo.T_Forosh.GOID, dbo.T_Forosh.KOLID, dbo.T_Forosh.HesabID, dbo.T_Forosh.THESABID,                        SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabid1, dbo.T_Forosh.ForoshNumbr,                        dbo.[TRANSACTION].KALAID, dbo.T_Forosh.DateFo AS MinOfDateFo, RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4) AS Hesab,                        dbo.T_Forosh.DateFo AS MaxOfDateFo, @enddate AS enddate, dbo.T_Forosh.DateFo, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1,                        dbo.T_NameKala.VAHED1, dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID)                        AS namehesab, RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4) AS Expr2, @AccID AS Expr5, @startSanadID AS Expr6,                        @startDate AS startDate, @IdKala AS Expr3, @startDate AS Expr7, dbo.T_Forosh.DateFo AS Expr4 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID INNER JOIN                       dbo.T_Forosh ON dbo.[TRANSACTION].FOROSHID = dbo.T_Forosh.FOROSHID WHERE     (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (@IdKala IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (@IdKala IS NULL) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (@IdKala IS NULL) AND (dbo.T_Forosh.ForoshNumbr BETWEEN                        @startSanadID AND @endSanadID) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (@IdKala IS NULL) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.T_Forosh.ForoshNumbr BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (@IdKala IS NULL) AND (dbo.T_Forosh.DateFo BETWEEN                        @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (@IdKala IS NULL) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (@IdKala IS NULL) AND (dbo.T_Forosh.DateFo BETWEEN                        @startDate AND @enddate) AND (dbo.T_Forosh.ForoshNumbr BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (@IdKala IS NULL) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) AND (dbo.T_Forosh.ForoshNumbr BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (@IdKala IS NOT NULL) AND                        (dbo.[TRANSACTION].KALAID = @IdKala) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (@IdKala IS NOT NULL) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.[TRANSACTION].KALAID = @IdKala) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (@IdKala IS NOT NULL) AND                        (dbo.[TRANSACTION].KALAID = @IdKala) AND (dbo.T_Forosh.ForoshNumbr BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (@IdKala IS NOT NULL) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.[TRANSACTION].KALAID = @IdKala) AND (dbo.T_Forosh.ForoshNumbr BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (@IdKala IS NOT NULL) AND                        (dbo.[TRANSACTION].KALAID = @IdKala) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (@IdKala IS NOT NULL) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.[TRANSACTION].KALAID = @IdKala) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (@IdKala IS NOT NULL) AND                        (dbo.[TRANSACTION].KALAID = @IdKala) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) AND                        (dbo.T_Forosh.ForoshNumbr BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (@IdKala IS NOT NULL) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.[TRANSACTION].KALAID = @IdKala) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) AND                        (dbo.T_Forosh.ForoshNumbr BETWEEN @startSanadID AND @endSanadID)  
GO



/****** Object:  StoredProcedure [dbo].[Rep_021236]    Script Date: 11/27/2017 20:42:55 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_021236]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_021236]
GO



/****** Object:  StoredProcedure [dbo].[Rep_021236]    Script Date: 11/27/2017 20:42:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[Rep_021236](@startSanadID int, @endSanadID int, @startDate nvarchar(255), @enddate nvarchar(255), @AccID nvarchar(50)) AS SELECT     dbo.T_Forosh.FOROSHID, CONVERT(int, dbo.T_Forosh.ForoshNumbr) AS Expr1, SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) AS Expr2,                        dbo.T_Forosh.DateFo, SUM(ABS(dbo.[TRANSACTION].FieldN3)) AS MabFact, ABS(SUM(dbo.[TRANSACTION].FieldN3))                        + ISNULL(F_MabHazine_Foroosh.MabHazine, 0) - ISNULL(dbo.T_Forosh.Takhfif, 0) AS Total, dbo.T_Forosh.Takhfif, MIN(dbo.T_Forosh.DateFo)                        AS MinOfDateFo, MAX(dbo.T_Forosh.DateFo) AS MaxOfDateFo, @enddate AS enddate, F_MabHazine_Foroosh.MabHazine,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS namehesab, CONVERT(int,                        dbo.T_Forosh.ForoshNumbr) AS ForoshNumbr, @startDate AS startDate, @startSanadID AS Expr3, @AccID AS Expr4 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Forosh ON dbo.[TRANSACTION].FOROSHID = dbo.T_Forosh.FOROSHID LEFT OUTER JOIN                       dbo.F_MabHazine_Foroosh() F_MabHazine_Foroosh ON dbo.T_Forosh.FOROSHID = F_MabHazine_Foroosh.FactID GROUP BY dbo.T_Forosh.DateFo, dbo.T_Forosh.Takhfif, F_MabHazine_Foroosh.MabHazine, dbo.HesabName(dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID), CONVERT(int, dbo.T_Forosh.ForoshNumbr), dbo.T_Forosh.FOROSHID,                        SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID),                        4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) HAVING      (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (CONVERT(int, dbo.T_Forosh.ForoshNumbr) BETWEEN                        @startSanadID AND @endSanadID) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID))                        = @AccID) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (CONVERT(int, dbo.T_Forosh.ForoshNumbr) BETWEEN                        @startSanadID AND @endSanadID) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND                        @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (CONVERT(int, dbo.T_Forosh.ForoshNumbr) BETWEEN                        @startSanadID AND @endSanadID) AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Forosh.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID)                        AND (dbo.T_Forosh.DateFo BETWEEN @startDate AND @enddate) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (CONVERT(int, dbo.T_Forosh.ForoshNumbr) BETWEEN                        @startSanadID AND @endSanadID) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.T_Forosh.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.HesabID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Forosh.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND (dbo.T_Forosh.DateFo BETWEEN                        @startDate AND @enddate) ORDER BY CONVERT(int, dbo.T_Forosh.ForoshNumbr) DESC  
GO


/****** Object:  StoredProcedure [dbo].[Rep_011869]    Script Date: 11/27/2017 20:55:53 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_011869]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_011869]
GO


/****** Object:  StoredProcedure [dbo].[Rep_011869]    Script Date: 11/27/2017 20:55:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[Rep_011869](@startSanadID int, @endSanadID int, @startDate nvarchar(255), @enddate nvarchar(255), @AccID nvarchar(50), @IdKala int) AS SELECT     dbo.[TRANSACTION].TextAIDKh, dbo.[TRANSACTION].TedadKh, dbo.[TRANSACTION].UnitPriceKh AS AvgOfUnitPriceKh,                        ABS(dbo.[TRANSACTION].FieldN3) AS mablagh, dbo.T_Kharid.GOID, dbo.T_Kharid.KOLID, dbo.T_Kharid.HesabID, dbo.T_Kharid.THESABID,                        SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) AS hesabid1, dbo.T_Kharid.KharidNumbr,                        dbo.[TRANSACTION].KALAID, dbo.T_Kharid.DateKh AS MinOfDateKh, RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4) AS Hesab,                        dbo.T_Kharid.DateKh AS MaxOfDateKh, @enddate AS enddate, dbo.T_Kharid.DateKh, dbo.T_NameKala.KalaNumber, dbo.T_NameKala.NameKala1,                        dbo.T_NameKala.VAHED1, dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID)                        AS namehesab, RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4) AS Expr2, @AccID AS Expr5, @startSanadID AS Expr6,                        @startDate AS startDate, @IdKala AS Expr3, @startDate AS Expr7, dbo.T_Kharid.DateKh AS Expr4 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Kharid ON dbo.[TRANSACTION].KHARIDID = dbo.T_Kharid.KHARIDID INNER JOIN                       dbo.T_NameKala ON dbo.[TRANSACTION].KALAID = dbo.T_NameKala.ID WHERE     (@IdKala IS NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) OR                       (@IdKala IS NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@IdKala IS NULL) AND (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (dbo.T_Kharid.KharidNumbr BETWEEN                        @startSanadID AND @endSanadID) OR                       (@IdKala IS NULL) AND (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.T_Kharid.KharidNumbr BETWEEN @startSanadID AND @endSanadID) OR                       (@IdKala IS NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (dbo.T_Kharid.DateKh BETWEEN                        @startDate AND @enddate) OR                       (@IdKala IS NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (dbo.T_Kharid.DateKh BETWEEN                        @startDate AND @enddate) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@IdKala IS NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (dbo.T_Kharid.DateKh BETWEEN                        @startDate AND @enddate) AND (dbo.T_Kharid.KharidNumbr BETWEEN @startSanadID AND @endSanadID) OR                       (@IdKala IS NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND                        (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID),                        4) + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (dbo.T_Kharid.KharidNumbr BETWEEN @startSanadID AND @endSanadID) OR                       (@IdKala IS NOT NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND                        (dbo.[TRANSACTION].KALAID = @IdKala) OR                       (@IdKala IS NOT NULL) AND (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND                        (dbo.[TRANSACTION].KALAID = @IdKala) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) OR                       (@IdKala IS NOT NULL) AND (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND                        (dbo.[TRANSACTION].KALAID = @IdKala) AND (dbo.T_Kharid.KharidNumbr BETWEEN @startSanadID AND @endSanadID) OR                       (@IdKala IS NOT NULL) AND (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND                        (dbo.[TRANSACTION].KALAID = @IdKala) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1,                        LEN(@AccID)) = @AccID) AND (dbo.T_Kharid.KharidNumbr BETWEEN @startSanadID AND @endSanadID) OR                       (@IdKala IS NOT NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (dbo.T_Kharid.DateKh BETWEEN                        @startDate AND @enddate) AND (dbo.[TRANSACTION].KALAID = @IdKala) OR                       (@IdKala IS NOT NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND                        (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) AND (dbo.[TRANSACTION].KALAID = @IdKala) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@IdKala IS NOT NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND                        (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) AND (dbo.[TRANSACTION].KALAID = @IdKala) AND                        (dbo.T_Kharid.KharidNumbr BETWEEN @startSanadID AND @endSanadID) OR                       (@IdKala IS NOT NULL) AND (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND                        (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) AND (dbo.[TRANSACTION].KALAID = @IdKala) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].HESABID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.[TRANSACTION].THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND                        (dbo.T_Kharid.KharidNumbr BETWEEN @startSanadID AND @endSanadID)  
GO




/****** Object:  StoredProcedure [dbo].[Rep_011868]    Script Date: 11/27/2017 20:55:49 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_011868]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_011868]
GO


/****** Object:  StoredProcedure [dbo].[Rep_011868]    Script Date: 11/27/2017 20:55:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[Rep_011868](@startSanadID int, @endSanadID int, @startDate nvarchar(255), @enddate nvarchar(255), @AccID nvarchar(50)) AS SELECT     dbo.T_Kharid.KHARIDID, CONVERT(int, dbo.T_Kharid.KharidNumbr) AS Expr1, SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) AS Expr2,                        dbo.T_Kharid.DateKh, SUM(ABS(dbo.[TRANSACTION].FieldN3)) AS MabFact, ABS(SUM(dbo.[TRANSACTION].FieldN3))                        + ISNULL(F_MabHazine_Kharid.MabHazine, 0) - ISNULL(dbo.T_Kharid.Takhfif, 0) AS Total, dbo.T_Kharid.Takhfif, MIN(dbo.T_Kharid.DateKh)                        AS MinOfDateKh, MAX(dbo.T_Kharid.DateKh) AS MaxOfDateKh, @enddate AS enddate, F_MabHazine_Kharid.MabHazine,                        dbo.HesabName(dbo.[TRANSACTION].KOLID, dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID) AS NameHesab, CONVERT(int,                        dbo.T_Kharid.KharidNumbr) AS KharidNumbr, @startDate AS startDate, @startSanadID AS Expr3, @AccID AS Expr4 FROM         dbo.[TRANSACTION] INNER JOIN                       dbo.T_Kharid ON dbo.[TRANSACTION].KHARIDID = dbo.T_Kharid.KHARIDID LEFT OUTER JOIN                       dbo.F_MabHazine_Kharid() F_MabHazine_Kharid ON dbo.T_Kharid.KHARIDID = F_MabHazine_Kharid.FactID GROUP BY dbo.T_Kharid.DateKh, dbo.T_Kharid.Takhfif, F_MabHazine_Kharid.MabHazine, dbo.HesabName(dbo.[TRANSACTION].KOLID,                        dbo.[TRANSACTION].HESABID, dbo.[TRANSACTION].THESABID), CONVERT(int, dbo.T_Kharid.KharidNumbr), dbo.T_Kharid.KHARIDID,                        SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID),                        4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) HAVING      (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (CONVERT(int, dbo.T_Kharid.KharidNumbr) BETWEEN                        @startSanadID AND @endSanadID) OR                       (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4),                        dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID))                        = @AccID) OR                       (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (CONVERT(int, dbo.T_Kharid.KharidNumbr) BETWEEN                        @startSanadID AND @endSanadID) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) AND (@AccID IS NULL) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) AND                        (@AccID IS NULL) AND (CONVERT(int, dbo.T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) AND                        (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4)                        + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR                       (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (dbo.T_Kharid.DateKh BETWEEN @startDate AND @enddate) AND                        (@AccID IS NOT NULL) AND (CONVERT(int, dbo.T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) AND                        (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.[TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), dbo.T_Kharid.KOLID),                        4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(dbo.T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4),                        ISNULL(dbo.T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) ORDER BY CONVERT(int, dbo.T_Kharid.KharidNumbr) DESC  
GO




/****** Object:  StoredProcedure [dbo].[Rep_01186606]    Script Date: 11/27/2017 20:55:44 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_01186606]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_01186606]
GO



/****** Object:  StoredProcedure [dbo].[Rep_01186606]    Script Date: 11/27/2017 20:55:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

      CREATE PROCEDURE [dbo].[Rep_01186606]
      (
      	@SortRep INT,
      	@StartShFact INT, 
      	@EndShFact INT, 
      	@startDate NVARCHAR(10), 
      	@enddate NVARCHAR(10),
      	@AID INT,
      	@Kalaid INT,
      	@Gokid INT,
      	@TxtNameKala  NVARCHAR(255) ,
		@Goid INT, 
		@KolID INT, 
		@HesabID INT, 
		@THesabID INT)
 AS 
      
BEGIN  	

/*
	@SortRep=forms![TreeViewRep_VazKharid]![Sorting],	
	@StartShFact= forms![TreeViewRep_VazKharid]![AsShomare],	
	@EndShFact=forms![TreeViewRep_VazKharid]![TaShomare],	
	@startDate =forms![TreeViewRep_VazKharid]![AsDate],	
	@enddate =forms![TreeViewRep_VazKharid]![TaDate],	
	@AID =forms![TreeViewRep_VazKharid]![AID],	
	@Kalaid =forms![TreeViewRep_VazKharid]![KalaID],	
	@Gokid=forms![TreeViewRep_VazKharid]![GoKala],	
	@TxtNameKala  =forms![TreeViewRep_VazKharid]![FilterNameKala] ,		
	@Goid =forms![TreeViewRep_VazKharid]![Goid],		
	@KolID =forms![TreeViewRep_VazKharid]![KolID],		
	@HesabID =forms![TreeViewRep_VazKharid]![HesabID],		
	@THesabID =forms![TreeViewRep_VazKharid]![THesabID]
*/		
		
	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	/* محتوي رشته پرس و جو*/
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);    
  	DECLARE @WhrStatement1  NVARCHAR(2000);  
  	    	 	
  	set @WhrStatement = N' ';   
  	 set @WhrStatement1  =N' '   	
  	set @ParmAndstr=N' AND '
  	                 
  	SET @ParmDefinition = N'@SortRep INT=NULL,
      						@StartShFact INT=NULL, 
      						@EndShFact INT=NULL, 
      						@startDate NVARCHAR(10)=NULL, 
      						@enddate NVARCHAR(10)=NULL,
      						@AID INT=NULL,
      						@Kalaid INT=NULL,
      						@Gokid INT=NULL,
      						@TxtNameKala  NVARCHAR(255) =NULL,
							@Goid INT=NULL, 
							@KolID INT=NULL, 
							@HesabID INT=NULL, 
							@THesabID INT=NULL ';
	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   
	
	
	If(NOT (@Goid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (GOID = @Goid ) '; 

		
If(NOT (@KOLID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (KOLID = @KOLID ) '; 

If(NOT (@HESABID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (HESABID = @HESABID ) '; 	
		
If(NOT (@THESABID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (THESABID = @THESABID ) '; 	
		

If(NOT (@AID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (AID = @AID ) '; 	

If(NOT (@Kalaid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (Kalaid = @Kalaid ) '; 

If(NOT (@Gokid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (Gokid = @Gokid ) '; 	
		 	
		 		
SET @SQLString =N' 
SELECT      SUM(TRANSACTION1.MabFact0) AS MabFact, ABS(SUM(TRANSACTION1.MabFact0)) + SUM(ISNULL(F_MabHazine_Kharid.MabHazine, 0)) - SUM(ISNULL(T_Kharid.Takhfif, 0)) AS Total,  
                      SUM(F_MabTakhfif_Kharid.Takhfif) AS Takhfif, MIN(T_Kharid.DateKh) AS MinOfDateKh, MAX(T_Kharid.DateKh) AS MaxOfDateKh, @enddate AS enddate, SUM(F_MabHazine_Kharid.MabHazine) 
                      AS MabHazine, dbo.HesabName(TRANSACTION1.KOLID, TRANSACTION1.HESABID, TRANSACTION1.THESABID) AS namehesab, @startDate AS startDate
FROM         dbo.F_MabTakhfif_Kharid() AS F_MabTakhfif_Kharid INNER JOIN
                      T_Kharid ON F_MabTakhfif_Kharid.KHARIDID = T_Kharid.KHARIDID INNER JOIN
                          (SELECT     GOID, KOLID, HESABID, THESABID, SUM(ABS(FieldN3)) AS MabFact0, KHARIDID
                             FROM         [TRANSACTION] AS TRANSACTION_1 WHERE     (NOT (KHARIDID IS NULL)) ' + @WhrStatement1 + '
                             
                             GROUP BY GOID, KOLID, HESABID, THESABID, KHARIDID) AS TRANSACTION1 ON T_Kharid.KHARIDID = TRANSACTION1.KHARIDID LEFT OUTER JOIN
                      dbo.F_MabHazine_Kharid() AS F_MabHazine_Kharid ON T_Kharid.KHARIDID = F_MabHazine_Kharid.FactID
WHERE     (T_Kharid.DateKh BETWEEN @startDate AND @enddate)
                             
'
                      

		

			


If(NOT (@StartShFact  IS NULL) AND NOT (@EndShFact  IS NULL) ) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Kharid.KharidNumbr Between @StartShFact and @EndShFact) '; 



/*
if(NOT (@TxtNameKala   IS NULL)) 			
BEGIN
	set @TxtNameKala ='%' + @TxtNameKala + '%'	 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_NameKala.NameKala1  Like @TxtNameKala ) '; 				
END 
*/

set @SQLString=@SQLString + @WhrStatement+N' GROUP BY dbo.HesabName(TRANSACTION1.KOLID, TRANSACTION1.HESABID, TRANSACTION1.THESABID) ' 				
			
      			
			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@StartShFact=@StartShFact,@EndShFact=@EndShFact,@startDate=@startDate,@enddate=@enddate,@AID=@AID,@Kalaid=@Kalaid,@Gokid=@Gokid,@TxtNameKala =@TxtNameKala ,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID;  		
                                                

return end 		
	
GO



/****** Object:  StoredProcedure [dbo].[Rep_01186605]    Script Date: 11/27/2017 20:55:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_01186605]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_01186605]
GO


/****** Object:  StoredProcedure [dbo].[Rep_01186605]    Script Date: 11/27/2017 20:55:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

      CREATE PROCEDURE [dbo].[Rep_01186605]
      (
      	@SortRep INT,
      	@StartShFact INT, 
      	@EndShFact INT, 
      	@startDate NVARCHAR(10), 
      	@enddate NVARCHAR(10),
      	@AID INT,
      	@Kalaid INT,
      	@Gokid INT,
      	@TxtNameKala  NVARCHAR(255) ,
		@Goid INT, 
		@KolID INT, 
		@HesabID INT, 
		@THesabID INT)
 AS 
      
BEGIN  	

/*
	@SortRep=forms![TreeViewRep_VazKharid]![Sorting],	
	@StartShFact= forms![TreeViewRep_VazKharid]![AsShomare],	
	@EndShFact=forms![TreeViewRep_VazKharid]![TaShomare],	
	@startDate =forms![TreeViewRep_VazKharid]![AsDate],	
	@enddate =forms![TreeViewRep_VazKharid]![TaDate],	
	@AID =forms![TreeViewRep_VazKharid]![AID],	
	@Kalaid =forms![TreeViewRep_VazKharid]![KalaID],	
	@Gokid=forms![TreeViewRep_VazKharid]![GoKala],	
	@TxtNameKala  =forms![TreeViewRep_VazKharid]![FilterNameKala] ,		
	@Goid =forms![TreeViewRep_VazKharid]![Goid],		
	@KolID =forms![TreeViewRep_VazKharid]![KolID],		
	@HesabID =forms![TreeViewRep_VazKharid]![HesabID],		
	@THesabID =forms![TreeViewRep_VazKharid]![THesabID]
*/		
		
	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	/* محتوي رشته پرس و جو*/
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);    
  	DECLARE @WhrStatement1  NVARCHAR(2000);  
  	    	 	
  	set @WhrStatement = N' ';   
  	 set @WhrStatement1  =N' '   	
  	set @ParmAndstr=N' AND '
  	                 
  	SET @ParmDefinition = N'@SortRep INT=NULL,
      						@StartShFact INT=NULL, 
      						@EndShFact INT=NULL, 
      						@startDate NVARCHAR(10)=NULL, 
      						@enddate NVARCHAR(10)=NULL,
      						@AID INT=NULL,
      						@Kalaid INT=NULL,
      						@Gokid INT=NULL,
      						@TxtNameKala  NVARCHAR(255) =NULL,
							@Goid INT=NULL, 
							@KolID INT=NULL, 
							@HesabID INT=NULL, 
							@THesabID INT=NULL ';
	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   
	
	
	If(NOT (@Goid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (GOID = @Goid ) '; 

		
If(NOT (@KOLID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (KOLID = @KOLID ) '; 

If(NOT (@HESABID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (HESABID = @HESABID ) '; 	
		
If(NOT (@THESABID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (THESABID = @THESABID ) '; 	
		

If(NOT (@AID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (AID = @AID ) '; 	

If(NOT (@Kalaid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (Kalaid = @Kalaid ) '; 

If(NOT (@Gokid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (Gokid = @Gokid ) '; 	
		 	
		 		
SET @SQLString =N' 
SELECT     SUM(TRANSACTION1.MabFact0) AS MabFact, ABS(SUM(TRANSACTION1.MabFact0)) + SUM(ISNULL(F_MabHazine_Kharid.MabHazine, 0)) - SUM(ISNULL(T_Kharid.Takhfif, 0)) AS Total, 
                      SUM(F_MabTakhfif_Kharid.Takhfif) AS Takhfif, MIN(T_Kharid.DateKh) AS MinOfDateKh, MAX(T_Kharid.DateKh) AS MaxOfDateKh, @enddate AS enddate, SUM(F_MabHazine_Kharid.MabHazine) 
                      AS MabHazine, dbo.HesabName(TRANSACTION1.KOLID, TRANSACTION1.HESABID, TRANSACTION1.THESABID) AS namehesab, @startDate AS startDate, T_Kharid.DateKh, 
                      SUM(TRANSACTION1.TedadKh0) AS SumOfTedadKh
FROM         dbo.F_MabTakhfif_Kharid() AS F_MabTakhfif_Kharid INNER JOIN
                      T_Kharid ON F_MabTakhfif_Kharid.KHARIDID = T_Kharid.KHARIDID INNER JOIN
                          (SELECT     GOID, KOLID, HESABID, THESABID, SUM(ABS(FieldN3)) AS MabFact0, KHARIDID, SUM(TedadKh) AS TedadKh0
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (KHARIDID IS NULL)) ' + @WhrStatement1 + '
GROUP BY GOID, KOLID, HESABID, THESABID, KHARIDID) AS TRANSACTION1 ON T_Kharid.KHARIDID = TRANSACTION1.KHARIDID LEFT OUTER JOIN
                      dbo.F_MabHazine_Kharid() AS F_MabHazine_Kharid ON T_Kharid.KHARIDID = F_MabHazine_Kharid.FactID
WHERE     (T_Kharid.DateKh BETWEEN @startDate AND @enddate)

'
                      

		

			


If(NOT (@StartShFact  IS NULL) AND NOT (@EndShFact  IS NULL) ) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Kharid.KharidNumbr Between @StartShFact and @EndShFact) '; 



/*
if(NOT (@TxtNameKala   IS NULL)) 			
BEGIN
	set @TxtNameKala ='%' + @TxtNameKala + '%'	 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_NameKala.NameKala1  Like @TxtNameKala ) '; 				
END 
*/

set @SQLString=@SQLString + @WhrStatement+N' GROUP BY dbo.HesabName(TRANSACTION1.KOLID, TRANSACTION1.HESABID, TRANSACTION1.THESABID), T_Kharid.DateKh' 				
			
      			
			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@StartShFact=@StartShFact,@EndShFact=@EndShFact,@startDate=@startDate,@enddate=@enddate,@AID=@AID,@Kalaid=@Kalaid,@Gokid=@Gokid,@TxtNameKala =@TxtNameKala ,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID;  		
                                                

return end 		
	
      
 /*     
      
      SELECT     T_Kharid.KHARIDID, CONVERT(int, T_Kharid.KharidNumbr) AS Expr1, SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) 
                      AS Expr2, T_Kharid.DateKh, SUM(ABS([TRANSACTION].FieldN3)) AS MabFact, ABS(SUM([TRANSACTION].FieldN3)) + ISNULL(F_MabHazine_Kharid.MabHazine, 0) - ISNULL(T_Kharid.Takhfif, 0) 
                      AS Total, T_Kharid.Takhfif, MIN(T_Kharid.DateKh) AS MinOfDateKh, MAX(T_Kharid.DateKh) AS MaxOfDateKh, @enddate AS enddate, F_MabHazine_Kharid.MabHazine, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab, CONVERT(int, T_Kharid.KharidNumbr) AS KharidNumbr, @startDate AS startDate, 
                      @startSanadID AS Expr3, @AccID AS Expr4
FROM         [TRANSACTION] INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID LEFT OUTER JOIN
                      dbo.F_MabHazine_Kharid() AS F_MabHazine_Kharid ON T_Kharid.KHARIDID = F_MabHazine_Kharid.FactID
GROUP BY T_Kharid.DateKh, T_Kharid.Takhfif, F_MabHazine_Kharid.MabHazine, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID), CONVERT(int, 
                      T_Kharid.KharidNumbr), T_Kharid.KHARIDID, SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), T_Kharid.KOLID), 4) 
                      + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID))
HAVING      (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) OR
                      (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (CONVERT(int, T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) OR
                      (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) 
                      + RIGHT(N'0000' + CONVERT(nvarchar(4), T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      ISNULL(T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR
                      (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (CONVERT(int, T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) AND 
                      (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      ISNULL(T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR
                      (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (T_Kharid.DateKh BETWEEN @startDate AND @enddate) OR
                      (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (CONVERT(int, T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) AND 
                      (T_Kharid.DateKh BETWEEN @startDate AND @enddate) OR
                      (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) 
                      + RIGHT(N'0000' + CONVERT(nvarchar(4), T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      ISNULL(T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND (T_Kharid.DateKh BETWEEN @startDate AND @enddate) OR
                      (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (CONVERT(int, T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) AND 
                      (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      ISNULL(T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND (T_Kharid.DateKh BETWEEN @startDate AND 
                      @enddate)
ORDER BY Expr1 DESC*/
GO



/****** Object:  StoredProcedure [dbo].[Rep_01186604]    Script Date: 11/27/2017 20:55:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_01186604]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_01186604]
GO



/****** Object:  StoredProcedure [dbo].[Rep_01186604]    Script Date: 11/27/2017 20:55:33 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[Rep_01186604] (  	@SortRep nvarchar(255),  	@NoeSortRep nvarchar(255), 	@Goid int, 	@IdKala int, 	@KalaGO int, 	@AID int, 	@KolID int,  	@HesabID int, 	@THesabID int,  	@StartDate nvarchar(50)='0', 	@Enddate nvarchar(50)='99999999', 	@StartShFact nvarchar(50), 	@EndShFact nvarchar(50), 	@FilterNameKala nvarchar(255) 	 )              AS        BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	   DECLARE @WhrStatement  NVARCHAR(2000);         	   DECLARE @ParmDefinition  NVARCHAR(2000);         	   DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   set @WhrStatement  = N'';         	   set @ParmAndstr = N'';                  	   SET @ParmDefinition = N'@SortRep nvarchar(255)=null,  							@NoeSortRep nvarchar(255)=null, 							@IdKala int, 							@KalaGO int, 							@AID int, 							@Goid int=null, 							@KolID int=null,  							@HesabID nvarchar(50)=null, 							@THesabID int=null, 							@StartDate nvarchar(50)=''0'', 							@Enddate nvarchar(50)=''99999999'', 							@StartShFact nvarchar(50)=null, 							@EndShFact nvarchar(50)=null, 							@FilterNameKala nvarchar(255)=null ';            	   /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		    BEGIN       if(@StartDate IS NULL) 			   BEGIN 				   set @StartDate='0'; 				   END         if(@Enddate IS NULL) 			   BEGIN 				   set @Enddate='99999999'; 				   END         
  if(@NoeSortRep IS NULL) 			   
  BEGIN 				   set @NoeSortRep=0; 				   END        	  		    
  SET @SQLString ='SELECT     T_Kharid.KHARIDID,convert(float, T_Kharid.KharidNumbr) as KharidNumbr, T_NameKala.NameKala1, T_NameVahed.NameVahed, T_Anbar.NameAnbar, CONVERT(float, T_NameKala.KalaNumber) AS KalaNumber, 
                      T_NameGoKala.NameGoKala1, T_NameKala.ID, T_NameKala.VAHED1, [TRANSACTION].AID, [TRANSACTION].GOKID, [TRANSACTION].GOID, [TRANSACTION].KOLID, [TRANSACTION].HESABID, 
                      [TRANSACTION].THESABID, T_NameKala.Zarib, MIN(dbo.F1StartDate()) AS MinOfDateKh, MAX(dbo.F1EndDate()) AS MaxOfDatekh, SUM(ABS([TRANSACTION].FieldN3)) AS SumFieldN3, 
                      CONVERT(float, SUM([TRANSACTION].TedadKh)) AS SumOftedadkh, @startDate AS startDate, @enddate AS enddate, ROUND(SUM([TRANSACTION].TedadFo) / T_NameKala.Zarib, 2) AS Karton, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab, T_Kharid.DateKh, convert(nvarchar(255),[TRANSACTION].Sharh) as Sharhkala
FROM         [TRANSACTION] INNER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
                      T_Anbar ON [TRANSACTION].AID = T_Anbar.AID INNER JOIN
                      T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID INNER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1
GROUP BY T_NameKala.NameKala1, T_NameKala.VAHED1, T_NameKala.KalaNumber, T_NameKala.Zarib, [TRANSACTION].AID, T_Kharid.KHARIDID, T_Kharid.KharidNumbr, [TRANSACTION].GOID, 
                      [TRANSACTION].KOLID, [TRANSACTION].GOKID, [TRANSACTION].HESABID, [TRANSACTION].THESABID, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, 
                      [TRANSACTION].THESABID), T_NameKala.ID, T_NameVahed.NameVahed, T_Anbar.NameAnbar, T_NameGoKala.NameGoKala1, T_Kharid.DateKh, convert(nvarchar(255),[TRANSACTION].Sharh)
HAVING      (T_Kharid.DateKh BETWEEN @startDate AND @enddate) '              
   /*  --------------------شرط حساب سرگروه  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
   set @ParmAndstr=' AND ' 			    END                   
   /*  --------------------شرط حساب کل  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@KolID  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    set @ParmAndstr=' AND ' 			    END              /*  --------------------شرط حساب معین  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@HESABID  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    set @ParmAndstr=' AND ' 			    END          /*  --------------------شرط حساب تفصیلی  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@THESABID  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    set @ParmAndstr=' AND ' 			    END             /*  --------------------شرط حساب کالا  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@IdKala  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_NameKala.ID = @IdKala )'; 				    set @ParmAndstr=' AND ' 			    END                /*  --------------------شرط حساب گروه کالا  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@KalaGO  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOKID = @KalaGO )'; 				    set @ParmAndstr=' AND ' 			    END   	         /*  --------------------شرط حساب انبار کالا  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@AID  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].AID = @AID )'; 				    set @ParmAndstr=' AND ' 			    END   	 	/*  --------------------فیلتر نام کالا */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@AID  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].AID = @AID )'; 				    set @ParmAndstr=' AND ' 			    END   	 	 	/*  --------------------شرط حساب شماره فاکتور  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@FilterNameKala  IS NULL))     BEGIN 	    	set @FilterNameKala='%'+@FilterNameKala+'%'	    set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_NameKala.NameKala1 LIKE  @FilterNameKala )'; 				    set @ParmAndstr=' AND ' 			    END    	    if(NOT (@WhrStatement  IS NULL)) 			    BEGIN 				 		set @SQLString= @SQLString + @WhrStatement     END       	set @WhrStatement=null 	 	/* --------------------  نام کالا  1 مرتب سازی */ 			     if (@SortRep like 'NameKala1') 			     		     BEGIN      	 		if ((@NoeSortRep<>0) )		 		BEGIN 		 			set @WhrStatement = N'  ORDER BY T_NameKala.NameKala1 DESC '; 	 		END 		 		if ((@NoeSortRep=0) )		 		BEGIN 		 			set @WhrStatement = N'  ORDER BY T_NameKala.NameKala1  ASC '; 	 		END 	    		     END  	 	/* --------------------  کد کالا  2 مرتب سازی */ 			     if (@SortRep like 'KalaNumber')     		     BEGIN      	 		if (@NoeSortRep<>0)		 		BEGIN 		 			set @WhrStatement = N'  ORDER BY KalaNumber DESC '; 	 		END 		 		if ((@NoeSortRep=0) )		 		BEGIN 		 			set @WhrStatement = N'  ORDER BY KalaNumber ASC '; 	 		END 	    		     END       	 	/* --------------------  کد کالا  2 مرتب سازی */ 			     if (@SortRep IS NULL)     		     BEGIN 		set @WhrStatement = N'  ORDER BY KalaNumber ASC '; 	     END  				 	   if(NOT (@WhrStatement  IS NULL)) 			    BEGIN 				 		set @SQLString= @SQLString + @WhrStatement     END     	      EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@NoeSortRep=@NoeSortRep,@AID=@AID,@KalaGO=@KalaGO,@IdKala=@IdKala,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@StartDate=@StartDate,@Enddate=@Enddate,@StartShFact=@StartShFact,@EndShFact=@EndShFact,@FilterNameKala=@FilterNameKala;  		     END 		      /* --------پایان چاپ*/  		 		  return END 

GO




/****** Object:  StoredProcedure [dbo].[Rep_01186603]    Script Date: 11/27/2017 20:55:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_01186603]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_01186603]
GO



/****** Object:  StoredProcedure [dbo].[Rep_01186603]    Script Date: 11/27/2017 20:55:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

      CREATE PROCEDURE [dbo].[Rep_01186603]
      (
      	@SortRep INT,
      	@StartShFact INT, 
      	@EndShFact INT, 
      	@startDate NVARCHAR(10), 
      	@enddate NVARCHAR(10),
      	@AID INT,
      	@Kalaid INT,
      	@Gokid INT,
      	@TxtNameKala  NVARCHAR(255) ,
		@Goid INT, 
		@KolID INT, 
		@HesabID INT, 
		@THesabID INT)
 AS 
      
BEGIN  	

/*
	@SortRep=forms![TreeViewRep_VazKharid]![Sorting],	
	@StartShFact= forms![TreeViewRep_VazKharid]![AsShomare],	
	@EndShFact=forms![TreeViewRep_VazKharid]![TaShomare],	
	@startDate =forms![TreeViewRep_VazKharid]![AsDate],	
	@enddate =forms![TreeViewRep_VazKharid]![TaDate],	
	@AID =forms![TreeViewRep_VazKharid]![AID],	
	@Kalaid =forms![TreeViewRep_VazKharid]![KalaID],	
	@Gokid=forms![TreeViewRep_VazKharid]![GoKala],	
	@TxtNameKala  =forms![TreeViewRep_VazKharid]![FilterNameKala] ,		
	@Goid =forms![TreeViewRep_VazKharid]![Goid],		
	@KolID =forms![TreeViewRep_VazKharid]![KolID],		
	@HesabID =forms![TreeViewRep_VazKharid]![HesabID],		
	@THesabID =forms![TreeViewRep_VazKharid]![THesabID]
*/		
		
	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	/* محتوي رشته پرس و جو*/
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);    
  	DECLARE @WhrStatement1  NVARCHAR(2000);  
  	    	 	
  	set @WhrStatement = N' ';   
  	 set @WhrStatement1  =N' '   	
  	set @ParmAndstr=N' AND '
  	                 
  	SET @ParmDefinition = N'@SortRep INT=NULL,
      						@StartShFact INT=NULL, 
      						@EndShFact INT=NULL, 
      						@startDate NVARCHAR(10)=NULL, 
      						@enddate NVARCHAR(10)=NULL,
      						@AID INT=NULL,
      						@Kalaid INT=NULL,
      						@Gokid INT=NULL,
      						@TxtNameKala  NVARCHAR(255) =NULL,
							@Goid INT=NULL, 
							@KolID INT=NULL, 
							@HesabID INT=NULL, 
							@THesabID INT=NULL ';
	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   
	
	
	If(NOT (@Goid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (GOID = @Goid ) '; 

		
If(NOT (@KOLID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (KOLID = @KOLID ) '; 

If(NOT (@HESABID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (HESABID = @HESABID ) '; 	
		
If(NOT (@THESABID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (THESABID = @THESABID ) '; 	
		

If(NOT (@AID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (AID = @AID ) '; 	

If(NOT (@Kalaid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (Kalaid = @Kalaid ) '; 

If(NOT (@Gokid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (Gokid = @Gokid ) '; 	
		 	
		 		
SET @SQLString =N' 
SELECT     SUM(TRANSACTION1.MabFact0) AS MabFact, ABS(SUM(TRANSACTION1.MabFact0)) + SUM(ISNULL(F_MabHazine_Kharid.MabHazine, 0)) - SUM(ISNULL(T_Kharid.Takhfif, 0)) AS Total, 
                      SUM(F_MabTakhfif_Kharid.Takhfif) AS Takhfif, MIN(T_Kharid.DateKh) AS MinOfDateKh, MAX(T_Kharid.DateKh) AS MaxOfDateKh, @enddate AS enddate, SUM(F_MabHazine_Kharid.MabHazine) 
                      AS MabHazine, dbo.HesabName(TRANSACTION1.KOLID, TRANSACTION1.HESABID, TRANSACTION1.THESABID) AS namehesab, @startDate AS startDate, T_Kharid.KHARIDID, 
                      T_Kharid.KharidNumbr, T_Kharid.DateKh
FROM         dbo.F_MabTakhfif_Kharid() AS F_MabTakhfif_Kharid INNER JOIN
                      T_Kharid ON F_MabTakhfif_Kharid.KHARIDID = T_Kharid.KHARIDID INNER JOIN
                          (SELECT     GOID, KOLID, HESABID, THESABID, SUM(ABS(FieldN3)) AS MabFact0, KHARIDID
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (KHARIDID IS NULL)) ' + @WhrStatement1 + '
                             GROUP BY GOID, KOLID, HESABID, THESABID, KHARIDID) AS TRANSACTION1 ON T_Kharid.KHARIDID = TRANSACTION1.KHARIDID LEFT OUTER JOIN
                      dbo.F_MabHazine_Kharid() AS F_MabHazine_Kharid ON T_Kharid.KHARIDID = F_MabHazine_Kharid.FactID
WHERE     (T_Kharid.DateKh BETWEEN @startDate AND @enddate)
'
                      

		

			


If(NOT (@StartShFact  IS NULL) AND NOT (@EndShFact  IS NULL) ) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Kharid.KharidNumbr Between @StartShFact and @EndShFact) '; 



/*
if(NOT (@TxtNameKala   IS NULL)) 			
BEGIN
	set @TxtNameKala ='%' + @TxtNameKala + '%'	 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_NameKala.NameKala1  Like @TxtNameKala ) '; 				
END 
*/

set @SQLString=@SQLString + @WhrStatement+N' GROUP BY dbo.HesabName(TRANSACTION1.KOLID, TRANSACTION1.HESABID, TRANSACTION1.THESABID), T_Kharid.KHARIDID, T_Kharid.KharidNumbr, T_Kharid.DateKh' 				
			
      			
			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@StartShFact=@StartShFact,@EndShFact=@EndShFact,@startDate=@startDate,@enddate=@enddate,@AID=@AID,@Kalaid=@Kalaid,@Gokid=@Gokid,@TxtNameKala =@TxtNameKala ,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID;  		
                                                

return end 		
	
      
 /*     
      
      SELECT     T_Kharid.KHARIDID, CONVERT(int, T_Kharid.KharidNumbr) AS Expr1, SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) 
                      AS Expr2, T_Kharid.DateKh, SUM(ABS([TRANSACTION].FieldN3)) AS MabFact, ABS(SUM([TRANSACTION].FieldN3)) + ISNULL(F_MabHazine_Kharid.MabHazine, 0) - ISNULL(T_Kharid.Takhfif, 0) 
                      AS Total, T_Kharid.Takhfif, MIN(T_Kharid.DateKh) AS MinOfDateKh, MAX(T_Kharid.DateKh) AS MaxOfDateKh, @enddate AS enddate, F_MabHazine_Kharid.MabHazine, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab, CONVERT(int, T_Kharid.KharidNumbr) AS KharidNumbr, @startDate AS startDate, 
                      @startSanadID AS Expr3, @AccID AS Expr4
FROM         [TRANSACTION] INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID LEFT OUTER JOIN
                      dbo.F_MabHazine_Kharid() AS F_MabHazine_Kharid ON T_Kharid.KHARIDID = F_MabHazine_Kharid.FactID
GROUP BY T_Kharid.DateKh, T_Kharid.Takhfif, F_MabHazine_Kharid.MabHazine, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID), CONVERT(int, 
                      T_Kharid.KharidNumbr), T_Kharid.KHARIDID, SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), T_Kharid.KOLID), 4) 
                      + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID))
HAVING      (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) OR
                      (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (CONVERT(int, T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) OR
                      (@startDate IS NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) 
                      + RIGHT(N'0000' + CONVERT(nvarchar(4), T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      ISNULL(T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR
                      (@startDate IS NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (CONVERT(int, T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) AND 
                      (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      ISNULL(T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) OR
                      (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NULL) AND (T_Kharid.DateKh BETWEEN @startDate AND @enddate) OR
                      (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NULL) AND (CONVERT(int, T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) AND 
                      (T_Kharid.DateKh BETWEEN @startDate AND @enddate) OR
                      (@startDate IS NOT NULL) AND (@startSanadID IS NULL) AND (@AccID IS NOT NULL) AND (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) 
                      + RIGHT(N'0000' + CONVERT(nvarchar(4), T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      ISNULL(T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND (T_Kharid.DateKh BETWEEN @startDate AND @enddate) OR
                      (@startDate IS NOT NULL) AND (@startSanadID IS NOT NULL) AND (@AccID IS NOT NULL) AND (CONVERT(int, T_Kharid.KharidNumbr) BETWEEN @startSanadID AND @endSanadID) AND 
                      (SUBSTRING(RIGHT(N'0000' + CONVERT(nvarchar(4), [TRANSACTION].GOID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), T_Kharid.KOLID), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), 
                      ISNULL(T_Kharid.HesabID, 0)), 4) + RIGHT(N'0000' + CONVERT(nvarchar(4), ISNULL(T_Kharid.THESABID, 0)), 4), 1, LEN(@AccID)) = @AccID) AND (T_Kharid.DateKh BETWEEN @startDate AND 
                      @enddate)
ORDER BY Expr1 DESC*/
GO


/****** Object:  StoredProcedure [dbo].[CQ_ListKhadamat]    Script Date: 01/21/2018 03:47:44 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKhadamat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKhadamat]
GO

/****** Object:  StoredProcedure [dbo].[CQ_ListKhadamat]    Script Date: 01/21/2018 03:47:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


       Create PROCEDURE [dbo].[CQ_ListKhadamat] AS
       
       SELECT     NAMEKHID, KhadamatNumber As [کد خدمات], Name As [نام خدمات], Convert(Money,UnitPrice) As [قیمت], Unit
		FROM         T_NameKhad

GO









/****** Object:  StoredProcedure [dbo].[Rep_030519]    Script Date: 11/29/2017 13:21:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030519]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030519]
GO



/****** Object:  StoredProcedure [dbo].[Rep_030519]    Script Date: 11/29/2017 13:21:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[Rep_030519](
        	@startDate nvarchar(50), 
        	@enddate nvarchar(50),
        	@DateT NVARCHAR(10), 
        	@GOID INT,
        	@KOLID INT,
        	@HESABID INT,
        	@THESABID INT,
			@FlagMande INT) AS 
        	
  	BEGIN 	
  		
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);  
  	
  	DECLARE @startDate1  NVARCHAR(10); 
  	DECLARE @enddate1  NVARCHAR(10); 
  	
  	set @Enddate1=NULL; 				   
	set @StartDate1=null; 
  	       	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'', 
							@DateT NVARCHAR(10)=null, 
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,@FlagMande INT=null,@enddate1  NVARCHAR(10)=null,@startDate1  NVARCHAR(10)=null'; 
  
	if(@FlagMande IS NULL) 	
		SET @FlagMande=0
  
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='00000000'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   
	
	if(@FlagMande<>0) 			   
		BEGIN 
			set @Enddate1=@Enddate; 				   
			set @StartDate1=@StartDate; 
			set @StartDate='00000000'; 				   
		END 
	ELSE
		BEGIN 
			set @Enddate1=@Enddate; 				   
			set @StartDate1=@StartDate; 
		END        

	
	  SET @SQLString =N' SELECT      derivedtbl_1.DateAkharinTarakonesh, Q_Bed_Bes.GOID, Q_Bed_Bes.KOLID, Q_Bed_Bes.HESABID, Q_Bed_Bes.THESABID, SUM(Q_Bed_Bes.SumOfPARBes) 
                      AS SumOfSumOfPARBes, SUM(Q_Bed_Bes.SumOfDARBed) AS SumOfSumOfDARBed, SUM(Q_Bed_Bes.mande) AS SumOfmande, MIN(Q_Bed_Bes.MinOfDate1) AS MinOfDate1, 
                      MAX(Q_Bed_Bes.MaxOfDate1) AS MaxOfDate1, dbo.HesabName(Q_Bed_Bes.KOLID, Q_Bed_Bes.HESABID, Q_Bed_Bes.THESABID) + N''  '' + ISNULL(T_T_Heasb.Phone1, N'''') 
                      + N'' '' + ISNULL(T_T_Heasb.Phone2, N'''') + N'' '' + ISNULL(T_T_Heasb.Mob, N'''') AS namehesab, @startdate1 AS startdate, @enddate1 AS enddate
FROM         (SELECT     GOID, KOLID, HESABID, THESABID, SumOfPARBes, SumOfDARBed, mande, MinOfDate1, MaxOfDate1, ISNULL(HESABID, - 1) AS HesabidLink, ISNULL(THESABID, - 1) AS THesabidLink
FROM         dbo.Q_Bed_Bes(@startDate1, @enddate) AS Q_Bed_Bes01) AS Q_Bed_Bes LEFT OUTER JOIN
                          (SELECT     GOID, KOLID, HESABID, THESABID, MAX(Date1) AS DateAkharinTarakonesh, ISNULL(HESABID, - 1) AS HESABIDLink, ISNULL(THESABID, - 1) AS THESABIDLink
                             FROM         [TRANSACTION]
                             WHERE     (GOID > 0)

'

  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'(GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END  
   
                    
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معین  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصیلی  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
SET @SQLString = @SQLString +@WhrStatement +N'    GROUP BY GOID, KOLID, HESABID, THESABID, ISNULL(HESABID, - 1), ISNULL(THESABID, - 1)) AS derivedtbl_1 ON Q_Bed_Bes.KOLID = derivedtbl_1.KOLID AND Q_Bed_Bes.HESABIDLink = derivedtbl_1.HESABIDLink AND 
                      Q_Bed_Bes.THESABIDLink = derivedtbl_1.THESABIDLink LEFT OUTER JOIN
                      T_T_Heasb ON Q_Bed_Bes.THESABID = T_T_Heasb.THESABID
WHERE     (Q_Bed_Bes.mande > 0) AND (Q_Bed_Bes.GOID > 0)



 '
	
	set @WhrStatement=N' '
	
			    
   if(@FlagMande<>0) 			    
		set @WhrStatement= @WhrStatement + N'  AND (CONVERT(float, CONVERT(nvarchar(255), Q_Bed_Bes.GOID) + CONVERT(nvarchar(255), Q_Bed_Bes.KOLID) + CONVERT(nvarchar(255), 
                      ISNULL(Q_Bed_Bes.HESABID, 0)) + CONVERT(nvarchar(255), ISNULL(Q_Bed_Bes.THESABID, 0))) IN
                          (SELECT      CONVERT(float, CONVERT(nvarchar(255), GOID) +  CONVERT(nvarchar(255), KOLID) + CONVERT(nvarchar(255), ISNULL(HESABID, 0)) +  CONVERT(nvarchar(255), 
                      ISNULL(THESABID, 0))) AS CodHesab
FROM         [TRANSACTION] AS TRANSACTION_1
WHERE     (Date1 BETWEEN @startDate1 AND @enddate1)
GROUP BY CONVERT(float, CONVERT(nvarchar(255), GOID) + CONVERT(nvarchar(255), KOLID) + CONVERT(nvarchar(255), ISNULL(HESABID, 0)) + CONVERT(nvarchar(255), ISNULL(THESABID, 
                      0)))))'; 				   

   
  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([Q_Bed_Bes].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([Q_Bed_Bes].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معین  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([Q_Bed_Bes].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصیلی  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([Q_Bed_Bes].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   
      /*  --------------------شرط آخرين فعاليت  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@DateT  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(derivedtbl_1.DateAkharinTarakonesh <= @DateT )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 

   set @SQLString= @SQLString + @WhrStatement  +N' 
GROUP BY derivedtbl_1.DateAkharinTarakonesh, Q_Bed_Bes.GOID, Q_Bed_Bes.KOLID, Q_Bed_Bes.HESABID, Q_Bed_Bes.THESABID, dbo.HesabName(Q_Bed_Bes.KOLID, Q_Bed_Bes.HESABID, 
                      Q_Bed_Bes.THESABID) + N''  '' + ISNULL(T_T_Heasb.Phone1, N'''') + N'' '' + ISNULL(T_T_Heasb.Phone2, N'''') + N'' '' + ISNULL(T_T_Heasb.Mob, N'''') '

							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@DateT=@DateT,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@FlagMande=@FlagMande,@enddate1 =@enddate1,@startDate1=@startDate1;  		     
	END 		      



GO



/****** Object:  StoredProcedure [dbo].[Rep_030520]    Script Date: 11/29/2017 13:21:24 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030520]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030520]
GO



/****** Object:  StoredProcedure [dbo].[Rep_030520]    Script Date: 11/29/2017 13:21:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



         CREATE PROCEDURE [dbo].[Rep_030520](
         	
         	@startDate nvarchar(50), 
        	@enddate nvarchar(50), 
        	@DateT NVARCHAR(10),
        	@GOID INT,
        	@KOLID INT,
        	@HESABID INT,
        	@THESABID int,
			@FlagMande INT) AS 
BEGIN        	
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);   
  	
  	DECLARE @startDate1  NVARCHAR(10); 
  	DECLARE @enddate1  NVARCHAR(10); 
  	  
  	set @Enddate1=NULL; 				   
	set @StartDate1=null; 
	    	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@DateT NVARCHAR(10)=null,  
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,@FlagMande INT=null,@enddate1  NVARCHAR(10)=null,@startDate1  NVARCHAR(10)=null'; 
	
	if(@FlagMande IS NULL) 	
		SET @FlagMande=0
								
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   
	   
	if(@FlagMande<>0) 			   
	BEGIN 
			set @Enddate1=@Enddate; 				   
			set @StartDate1=@StartDate; 
			set @StartDate='00000000'; 				   
		END 
	ELSE
		BEGIN 
			set @Enddate1=@Enddate; 				   
			set @StartDate1=@StartDate; 
		END  
		     
set @SQLString=N' SELECT     Q_Bed_Bes.GOID, Q_Bed_Bes.KOLID, Q_Bed_Bes.HESABID, Q_Bed_Bes.THESABID, SUM(Q_Bed_Bes.SumOfPARBes) AS SumOfSumOfPARBes, SUM(Q_Bed_Bes.SumOfDARBed) 
                      AS SumOfSumOfDARBed, SUM(Q_Bed_Bes.mande) AS SumOfmande, MIN(Q_Bed_Bes.MinOfDate1) AS MinOfDate1, MAX(Q_Bed_Bes.MaxOfDate1) AS MaxOfDate1, 
                      dbo.HesabName(Q_Bed_Bes.KOLID, Q_Bed_Bes.HESABID, Q_Bed_Bes.THESABID) + N''  '' + ISNULL(T_T_Heasb.Phone1, N'''') + N'' '' + ISNULL(T_T_Heasb.Phone2, N'''') 
                      + N'' '' + ISNULL(T_T_Heasb.Mob, N'''') AS namehesab, @startdate AS startdate, @enddate AS enddate, derivedtbl_1.DateAkharinTarakonesh
FROM         (SELECT     GOID, KOLID, HESABID, THESABID, SumOfPARBes, SumOfDARBed, mande, MinOfDate1, MaxOfDate1, ISNULL(HESABID, - 1) AS HesabidLink, ISNULL(THESABID, - 1) AS THesabidLink
FROM         dbo.Q_Bed_Bes(@startDate1, @enddate) AS Q_Bed_Bes01) AS Q_Bed_Bes LEFT OUTER JOIN
                      T_T_Heasb ON Q_Bed_Bes.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                          (SELECT     GOID, KOLID, HESABID, THESABID, MAX(Date1) AS DateAkharinTarakonesh, ISNULL(HESABID, - 1) AS HESABIDLink, ISNULL(THESABID, - 1) AS THESABIDLink
                             FROM         [TRANSACTION]
                             WHERE     (GOID > 0)
 '


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'(GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معین  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصیلی  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
SET @SQLString = @SQLString +@WhrStatement +N'  GROUP BY GOID, KOLID, HESABID, THESABID, ISNULL(HESABID, - 1) , ISNULL(THESABID, - 1) ) AS derivedtbl_1 ON Q_Bed_Bes.KOLID = derivedtbl_1.KOLID AND Q_Bed_Bes.HESABIDLink = derivedtbl_1.HESABIDLink AND 
                      Q_Bed_Bes.THESABIDLink = derivedtbl_1.THESABIDLink
WHERE     (Q_Bed_Bes.mande > 0) AND (Q_Bed_Bes.GOID > 0)
 '
 
 SET @WhrStatement=' ' 

	    
   if(@FlagMande<>0) 			    
		set @WhrStatement= @WhrStatement + N'  AND (CONVERT(float, CONVERT(nvarchar(255), Q_Bed_Bes.GOID) + CONVERT(nvarchar(255), Q_Bed_Bes.KOLID) + CONVERT(nvarchar(255), 
                      ISNULL(Q_Bed_Bes.HESABID, 0)) + CONVERT(nvarchar(255), ISNULL(Q_Bed_Bes.THESABID, 0))) IN
                          (SELECT      CONVERT(float, CONVERT(nvarchar(255), GOID) +  CONVERT(nvarchar(255), KOLID) + CONVERT(nvarchar(255), ISNULL(HESABID, 0)) +  CONVERT(nvarchar(255), 
                      ISNULL(THESABID, 0))) AS CodHesab
FROM         [TRANSACTION] AS TRANSACTION_1
WHERE     (Date1 BETWEEN @startDate1 AND @enddate1)
GROUP BY CONVERT(float, CONVERT(nvarchar(255), GOID) + CONVERT(nvarchar(255), KOLID) + CONVERT(nvarchar(255), ISNULL(HESABID, 0)) + CONVERT(nvarchar(255), ISNULL(THESABID, 
                      0)))))'; 		
                      
  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([Q_Bed_Bes].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([Q_Bed_Bes].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معین  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([Q_Bed_Bes].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصیلی  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([Q_Bed_Bes].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   /*  --------------------شرط آخرين فعاليت  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@DateT  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(derivedtbl_1.DateAkharinTarakonesh <= @DateT )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   set @SQLString= @SQLString + @WhrStatement  + N' GROUP BY Q_Bed_Bes.GOID, Q_Bed_Bes.KOLID, Q_Bed_Bes.HESABID, Q_Bed_Bes.THESABID, dbo.HesabName(Q_Bed_Bes.KOLID, Q_Bed_Bes.HESABID, Q_Bed_Bes.THESABID) 
                      + N''  '' + ISNULL(T_T_Heasb.Phone1, N'''') + N'' '' + ISNULL(T_T_Heasb.Phone2, N'''') + N'' '' + ISNULL(T_T_Heasb.Mob, N''''), derivedtbl_1.DateAkharinTarakonesh, dbo.HesabName(Q_Bed_Bes.KOLID, 
                      Q_Bed_Bes.HESABID, Q_Bed_Bes.THESABID)
ORDER BY derivedtbl_1.DateAkharinTarakonesh '



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@DateT=@DateT,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@FlagMande=@FlagMande,@enddate1 =@enddate1,@startDate1=@startDate1;  		     

END 


GO




/****** Object:  StoredProcedure [dbo].[Rep_030931SF3]    Script Date: 11/30/2017 10:35:15 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030931SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030931SF3]
GO


/****** Object:  StoredProcedure [dbo].[Rep_030931SF3]    Script Date: 11/30/2017 10:35:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[Rep_030931SF3](@startDate nvarchar(50),
@enddate nvarchar(50),
@startSanadID int,
@endSanadID int,
@GOID INT,
@KOLID INT,
@HESABID INT,
@THESABID INT,
@THESABIDSath2 INT)
AS 

BEGIN 
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=null'; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   


set @SQLString=N'SELECT     [TRANSACTION].Date1, T_SA2.SAID2Number, SUM([TRANSACTION].DARBed) AS DARBed, SUM([TRANSACTION].PARBes) AS PARBes, T_KolHesab.NameHesab, 
                       CASE WHEN [TRANSACTION].DARBed = 0 THEN 1 ELSE 2 END AS SORT1,SUM([TRANSACTION].DARBed - [TRANSACTION].PARBes) AS mande, [TRANSACTION].KOLID, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].GOID, T_SA2.SAID2
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID 
WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND ([TRANSACTION].DARBed IS NOT NULL) AND ([TRANSACTION].PARBes IS NOT NULL)

'


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
      /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
     /*  --------------------تاريخ  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startDate  IS NULL)) AND (NOT (@enddate  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].Date1 BetWeen @startDate and  @enddate)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number BetWeen @startSanadID and  @endSanadID)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
   
   
   set @SQLString= @SQLString + @WhrStatement + ' GROUP BY [TRANSACTION].Date1, T_SA2.SAID2Number, T_KolHesab.NameHesab, [TRANSACTION].KOLID, [TRANSACTION].GOID, T_SA2.SAID2 , CASE WHEN [TRANSACTION].DARBed = 0 THEN 1 ELSE 2 END  '



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END 


GO



/****** Object:  StoredProcedure [dbo].[Rep_030992Sath2]    Script Date: 11/30/2017 20:48:27 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030992Sath2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030992Sath2]
GO


/****** Object:  StoredProcedure [dbo].[Rep_030992Sath2]    Script Date: 11/30/2017 20:48:27 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[Rep_030992Sath2](@startDate nvarchar(50),
@enddate nvarchar(50),
@startSanadID int,
@endSanadID int,
@GOID INT,
@KOLID INT,
@HESABID INT,
@THESABID int,
@THESABIDSath2 INT)
AS 

BEGIN 
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=NUll'; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   


set @SQLString=N' SELECT      [TRANSACTION].Date1, T_SA2.SAID2Number, [TRANSACTION].DARBed, [TRANSACTION].PARBes, T_T_HeasbSath2.NameHesabT, 
                      [TRANSACTION].DARBed - [TRANSACTION].PARBes AS mande, @startDate AS startDate, @enddate AS enddate, T_SA2.SAID2, [TRANSACTION].Sharh, [TRANSACTION].TextShSA, 
                      [TRANSACTION].ThesabIDSath2
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_T_HeasbSath2 ON [TRANSACTION].ThesabIDSath2 = T_T_HeasbSath2.THESABID
WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND ([TRANSACTION].DARBed IS NOT NULL) AND ([TRANSACTION].PARBes IS NOT NULL)'

 /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
     /*  --------------------تاريخ  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startDate  IS NULL)) AND (NOT (@enddate  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].Date1 BetWeen @startDate and  @enddate)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number BetWeen @startSanadID and  @endSanadID)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
     
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   
   
   set @SQLString= @SQLString + @WhrStatement 



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END                       


GO



/****** Object:  StoredProcedure [dbo].[Rep_030992]    Script Date: 11/30/2017 20:48:22 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030992]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030992]
GO


/****** Object:  StoredProcedure [dbo].[Rep_030992]    Script Date: 11/30/2017 20:48:22 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[Rep_030992](@startDate nvarchar(50),
@enddate nvarchar(50),
@startSanadID int,
@endSanadID int,
@GOID INT,
@KOLID INT,
@HESABID INT,
@THESABID int,
@THESABIDSath2 INT)
AS 

BEGIN 
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=NUll'; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   


set @SQLString=N' SELECT      [TRANSACTION].Date1, T_SA2.SAID2Number, SUM([TRANSACTION].DARBed) AS DARBed, SUM([TRANSACTION].PARBes) AS PARBes, T_T_Heasb.NameHesabT, 
                      SUM([TRANSACTION].DARBed - [TRANSACTION].PARBes) AS mande, @startDate AS startDate, @enddate AS enddate, T_SA2.SAID2, [TRANSACTION].THESABID, 
                      CASE WHEN [TRANSACTION].DARBed = 0 THEN 1 ELSE 2 END AS Sort
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID
WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND ([TRANSACTION].DARBed IS NOT NULL) AND ([TRANSACTION].PARBes IS NOT NULL)'

 /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
     /*  --------------------تاريخ  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startDate  IS NULL)) AND (NOT (@enddate  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].Date1 BetWeen @startDate and  @enddate)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number BetWeen @startSanadID and  @endSanadID)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
     
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   
   
   set @SQLString= @SQLString + @WhrStatement + ' GROUP BY [TRANSACTION].Date1, T_SA2.SAID2Number, T_T_Heasb.NameHesabT, T_SA2.SAID2, [TRANSACTION].THESABID, CASE WHEN [TRANSACTION].DARBed = 0 THEN 1 ELSE 2 END '



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END                       


GO



/****** Object:  StoredProcedure [dbo].[Rep_030932]    Script Date: 11/30/2017 20:48:18 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030932]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030932]
GO



/****** Object:  StoredProcedure [dbo].[Rep_030932]    Script Date: 11/30/2017 20:48:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[Rep_030932](@startDate nvarchar(50),
@enddate nvarchar(50),
@startSanadID int,
@endSanadID int,
@GOID INT,
@KOLID INT,
@HESABID INT,
@THESABID int,
@THESABIDSath2 INT)
AS 

BEGIN 
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=NUll'; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   


set @SQLString=N' SELECT     T_SA2.SAID2Number, SUM([TRANSACTION].DARBed) AS DARBed, SUM([TRANSACTION].PARBes) AS PARBes, T_Heasb.NameHesab, SUM([TRANSACTION].DARBed - [TRANSACTION].PARBes) 
                      AS mande, [TRANSACTION].KOLID, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].Date1, T_Heasb.HESABID, T_SA2.SAID2, 
                      CASE WHEN [TRANSACTION].DARBed = 0 THEN 1 ELSE 2 END AS Sort
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     ([TRANSACTION].DARBed IS NOT NULL) AND ([TRANSACTION].PARBes IS NOT NULL) AND ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)
 '
                      
                      
  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
     /*  --------------------تاريخ  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startDate  IS NULL)) AND (NOT (@enddate  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].Date1 BetWeen @startDate and  @enddate)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number BetWeen @startSanadID and  @endSanadID)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
    /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   set @SQLString= @SQLString + @WhrStatement + ' GROUP BY T_SA2.SAID2Number, T_Heasb.NameHesab, [TRANSACTION].KOLID, [TRANSACTION].Date1, T_Heasb.HESABID, T_SA2.SAID2, CASE WHEN [TRANSACTION].DARBed = 0 THEN 1 ELSE 2 END '



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END                       


GO



/****** Object:  StoredProcedure [dbo].[Rep_030931SF3]    Script Date: 11/30/2017 20:48:15 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030931SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030931SF3]
GO



/****** Object:  StoredProcedure [dbo].[Rep_030931SF3]    Script Date: 11/30/2017 20:48:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[Rep_030931SF3](@startDate nvarchar(50),
@enddate nvarchar(50),
@startSanadID int,
@endSanadID int,
@GOID INT,
@KOLID INT,
@HESABID INT,
@THESABID INT,
@THESABIDSath2 INT)
AS 

BEGIN 
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=null'; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   


set @SQLString=N'SELECT     [TRANSACTION].Date1, T_SA2.SAID2Number, SUM([TRANSACTION].DARBed) AS DARBed, SUM([TRANSACTION].PARBes) AS PARBes, T_KolHesab.NameHesab, 
                       CASE WHEN [TRANSACTION].DARBed = 0 THEN 1 ELSE 2 END AS SORT1,SUM([TRANSACTION].DARBed - [TRANSACTION].PARBes) AS mande, [TRANSACTION].KOLID, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].GOID, T_SA2.SAID2
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID 
WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND ([TRANSACTION].DARBed IS NOT NULL) AND ([TRANSACTION].PARBes IS NOT NULL)

'


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
      /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
     /*  --------------------تاريخ  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startDate  IS NULL)) AND (NOT (@enddate  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].Date1 BetWeen @startDate and  @enddate)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number BetWeen @startSanadID and  @endSanadID)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
   
   
   set @SQLString= @SQLString + @WhrStatement + ' GROUP BY [TRANSACTION].Date1, T_SA2.SAID2Number, T_KolHesab.NameHesab, [TRANSACTION].KOLID, [TRANSACTION].GOID, T_SA2.SAID2 , CASE WHEN [TRANSACTION].DARBed = 0 THEN 1 ELSE 2 END  '



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END 


GO



/****** Object:  StoredProcedure [dbo].[Rep_030931]    Script Date: 11/30/2017 20:48:11 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030931]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030931]
GO

/****** Object:  StoredProcedure [dbo].[Rep_030931]    Script Date: 11/30/2017 20:48:11 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[Rep_030931](@startDate nvarchar(50),
@enddate nvarchar(50),
@startSanadID int,
@endSanadID int,
@GOID INT,
@KOLID INT,
@HESABID INT,
@THESABID INT,
@THESABIDSath2 INT)
AS 

BEGIN 
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=null'; 
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   


set @SQLString=N'SELECT     [TRANSACTION].Date1, T_SA2.SAID2Number, [TRANSACTION].DARBed, [TRANSACTION].PARBes, [TRANSACTION].Flag2, [TRANSACTION].Sharh, [TRANSACTION].TextShSA, T_KolHesab.NameHesab, 
                      [TRANSACTION].DARBed - [TRANSACTION].PARBes AS mande, [TRANSACTION].KOLID, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].GOID, [TRANSACTION].DARBedCur, 
                      [TRANSACTION].PARBesCur, [TRANSACTION].CurRate, [TRANSACTION].CurRef, T_SA2.SAID2
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID
WHERE     ([TRANSACTION].DARBed IS NOT NULL) AND ([TRANSACTION].PARBes IS NOT NULL) AND (@startDate IS NOT NULL) AND ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) '


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
      /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
     /*  --------------------تاريخ  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startDate  IS NULL)) AND (NOT (@enddate  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].Date1 BetWeen @startDate and  @enddate)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number BetWeen @startSanadID and  @endSanadID)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
   
   
   set @SQLString= @SQLString + @WhrStatement 



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END 

GO



/****** Object:  StoredProcedure [dbo].[Rep_030930]    Script Date: 11/30/2017 20:48:03 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030930]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030930]
GO


/****** Object:  StoredProcedure [dbo].[Rep_030930]    Script Date: 11/30/2017 20:48:03 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[Rep_030930](@startDate nvarchar(50),
									@enddate nvarchar(50),
									@startSanadID int,
									@endSanadID INT,
									@GOID INT,
									@KOLID INT,
									@HESABID INT,
									@THESABID int,
									@THESABIDSath2 INT)
			AS 
BEGIN        	
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=NUll';
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   
	        
set @SQLString=N' SELECT     [TRANSACTION].Date1, T_SA2.SAID2Number, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab, [TRANSACTION].DARBed, 
                      [TRANSACTION].PARBes, [TRANSACTION].TextShSA, [TRANSACTION].TextNHSID2, @startDate AS startDate, @enddate AS enddate, [TRANSACTION].KOLID, [TRANSACTION].GOID, 
                      [TRANSACTION].HESABID, [TRANSACTION].THESABID, [TRANSACTION].TEMPR, [TRANSACTION].SAID2, CASE WHEN DarBed = 0 THEN 1 ELSE 2 END AS Sort1, [TRANSACTION].radif, 
                      dbo.HesabNameCod([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS CodeHesab, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, NULL) 
                      AS NameHesabMoin, dbo.HesabName([TRANSACTION].KOLID, NULL, NULL) AS NameHesabKol
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2
WHERE     ([TRANSACTION].DARBed IS NOT NULL) AND ([TRANSACTION].PARBes IS NOT NULL) '


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
     /*  --------------------تاريخ  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startDate  IS NULL)) AND (NOT (@enddate  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].Date1 BetWeen @startDate and  @enddate)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number BetWeen @startSanadID and  @endSanadID)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
   
       /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   
   set @SQLString= @SQLString + @WhrStatement 



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END 






GO









/****** Object:  StoredProcedure [dbo].[SabtKala_Table1_InsertSF1]    Script Date: 12/11/2017 09:49:26 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SabtKala_Table1_InsertSF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SabtKala_Table1_InsertSF1]
GO



/****** Object:  StoredProcedure [dbo].[SabtKala_Table1_InsertSF1]    Script Date: 12/11/2017 09:49:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


       CREATE PROCEDURE [dbo].[SabtKala_Table1_InsertSF1] 		 		      
       
       @AID2 INT, 		      
       @PrimeryOperationID INT, 		      
       @PrimarySAnadID INT, 		      
       @SecondrySanadid INT, 		      
       @LinkTosub NVARCHAR(255), 		      
       @TypeOfFact INT, 		      
       @SecondryOperationCode NVARCHAR(255), 		      
       @Date1 NVARCHAR(20), 		      
       @GorupHesabID  INT, 		      
       @KolHesabID INT, 		      
       @MoinHesabID INT, 		      
       @TafsiliHesabID INT, 		      
       @DateMiladi NVARCHAR(20)        
       
       AS              
       BEGIN  
       	SET NOCOUNT ON;          
       	DECLARE @DateNow DATETIME         
       	set @DateNow=GETDATE()         
       	
	INSERT INTO [TRANSACTION]
						  (Zarib, FieldN7, Maliatkala, Avarez, radif, Tedad2, ID1, FieldN1, FieldN2, SAID2Number, OperationCode, FieldN6, AID, GOKID, KALAID, TextAIDKharid, Sharh, TedadKhA, MabVahedHazine, FieldN3, 
						  Date1, GOID, KOLID, HESABID, THESABID, Dateeng, DARBed, PARBes, VKALAID, DateCreate, DateModified, UnitPriceKho)
	SELECT     TRANSACTION2.Zarib, TRANSACTION2.FieldN7, TRANSACTION2.FieldN1, TRANSACTION2.FieldN2, TRANSACTION2.radif, TRANSACTION2.Tedad2, @PrimeryOperationID AS ex1, 
						  @PrimeryOperationID AS ex2, @PrimarySAnadID AS ex3, @SecondrySanadid AS ex4, @SecondryOperationCode AS ex5, TRANSACTION2.FieldN6, TRANSACTION2.AID, TRANSACTION2.GOKID, 
						  TRANSACTION2.KALAID, TRANSACTION2.TextAIDKharid, TRANSACTION2.Sharh, TRANSACTION2.Tedad, TRANSACTION2.UnitPrice, 
						  Fun_SubAkharinKharid_1.MaxUnitPriceKh * TRANSACTION2.Tedad AS ex6, @Date1 AS ex7, @GorupHesabID AS ex8, @KolHesabID AS ex9, @MoinHesabID AS ex10, @TafsiliHesabID AS ex11, 
						  @DateMiladi AS ex12, NULL AS Ex13, NULL AS Ex14, 1 AS ex15, @DateNow AS fsf, @DateNow AS dfghf, Fun_SubAkharinKharid_1.MaxUnitPriceKh
	FROM         (SELECT     TRID, TRAID, SAID2, RadifSAID2, TextNHSID2, TextShSA, HESABID, GOID, KOLID, THESABID, MOINID, KALAID, NAMEKHID, AID, GAID, TedadVA, TedadKhA, VKALAID, ModelKala, SAID, 
												  TextNumberSA, GOKID, PARBes, DARBed, TEMP, TEMPR, TempMandeh, Sharh, radif, Date1, BANKID, KHOROJID, TextAIDKh, TextNumberKHO, TedadKhorooj, VORODID, TextAID, 
												  TextNumberVO, TedadList, TedadVorood, SHomareKOtag, SHomarePArvaneh, SHomarePAking, SHoKAmyon, SHomareLAng, PFOROSHID, FOROSHID, SForoshID, TextAIDFo, 
												  TextNumberFO, TedadFo, UnitPriceFo, PKHARIDID, KHARIDID, TextAIDKharid, TextNumberKH, Tedad, UnitPrice, SumofPrice, BARFOROSHID, TedadBarFo, UnitPriceBarFo, BARKHARIDID, 
												  TedadBarKh, UnitPriceBarKh, DKHID, TedadDKh, UnitPriceDKh, HKHID, TedadHKh, UnitPriceHKh, TAFOID, TAKHID, IDSHARH, NobatSandoghid, PARID1, PARID2, PARID3, PARID4, 
												  PARID5, PARID6, PARID7, PARID8, DARID1, DARID2, DARID3, DARID4, DARID5, DARID6, DARID7, DARID8, ID1, ID2, ID3, ID4, FieldN1, FieldN2, FieldN3, FieldN4, FieldN5, FieldN6, 
												  FieldN7, FieldT1, FieldT2, FieldT3, FieldT4, FieldT5, FieldT6, FieldT7, FieldT8, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, Flag9, Flag10, TextKalaName, LinkedID, OperType, 
												  CurMablagh, CurRef, CurRate, Tedad2, Tedad1, Zarib, TedadHazine, MabVahedHazine, ThesabIDSath2, CONVERT(Float, CONVERT(nvarchar(255), KALAID) + N'.' + (CASE WHEN (AID % 10)
												   = 0 THEN CONVERT(nvarchar(255), AID) + '11111' ELSE CONVERT(nvarchar(255), AID) END)) AS LinkKalaAid
							FROM         TRANSACTION2 AS TRANSACTION2_1) AS TRANSACTION2 LEFT OUTER JOIN
						  dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.LinkKalaAid = Fun_SubAkharinKharid_1.LinkKalaAid
	WHERE     (NOT (TRANSACTION2.KALAID IS NULL)) AND (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.TRAID IS NULL OR
						  TRANSACTION2.TRAID = 0)
	ORDER BY TRANSACTION2.radif
       	
       	
       	DECLARE @AID INT, @GOKID  INT,@KALAID INT, @Keraieh MONEY, @UniPrice1 MONEY, @KeraiehGH  MONEY,@NameKala1 NVARCHAR(255),@KALAIDNew INT,@KALAIDNewGH INT,@KALAIDNewGh1 INT	;    
       	
       	DECLARE KalaID_Cursor   
       	
       	CURSOR FOR     
       	
SELECT     TRANSACTION2.AID, TRANSACTION2.GOKID, TRANSACTION2.KALAID, TRANSACTION2.UnitPrice AS Keraieh, Fun_SubAkharinKharid_1.MaxUnitPriceKh AS UniPrice1, T_NameKala.NameKala1, 
                      T_NameKala.UnitPrice2 AS KeraiehGH
FROM         (SELECT     TRID, TRAID, SAID2, RadifSAID2, TextNHSID2, TextShSA, HESABID, GOID, KOLID, THESABID, MOINID, KALAID, NAMEKHID, AID, GAID, TedadVA, TedadKhA, VKALAID, ModelKala, SAID, 
                                              TextNumberSA, GOKID, PARBes, DARBed, TEMP, TEMPR, TempMandeh, Sharh, radif, Date1, BANKID, KHOROJID, TextAIDKh, TextNumberKHO, TedadKhorooj, VORODID, TextAID, 
                                              TextNumberVO, TedadList, TedadVorood, SHomareKOtag, SHomarePArvaneh, SHomarePAking, SHoKAmyon, SHomareLAng, PFOROSHID, FOROSHID, SForoshID, TextAIDFo, 
                                              TextNumberFO, TedadFo, UnitPriceFo, PKHARIDID, KHARIDID, TextAIDKharid, TextNumberKH, Tedad, UnitPrice, SumofPrice, BARFOROSHID, TedadBarFo, UnitPriceBarFo, BARKHARIDID, 
                                              TedadBarKh, UnitPriceBarKh, DKHID, TedadDKh, UnitPriceDKh, HKHID, TedadHKh, UnitPriceHKh, TAFOID, TAKHID, IDSHARH, NobatSandoghid, PARID1, PARID2, PARID3, PARID4, 
                                              PARID5, PARID6, PARID7, PARID8, DARID1, DARID2, DARID3, DARID4, DARID5, DARID6, DARID7, DARID8, ID1, ID2, ID3, ID4, FieldN1, FieldN2, FieldN3, FieldN4, FieldN5, FieldN6, 
                                              FieldN7, FieldT1, FieldT2, FieldT3, FieldT4, FieldT5, FieldT6, FieldT7, FieldT8, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, Flag9, Flag10, TextKalaName, LinkedID, OperType, 
                                              CurMablagh, CurRef, CurRate, Tedad2, Tedad1, Zarib, TedadHazine, MabVahedHazine, ThesabIDSath2, CONVERT(Float, CONVERT(nvarchar(255), KALAID) + N'.' + (CASE WHEN (AID % 10)
                                               = 0 THEN CONVERT(nvarchar(255), AID) + '11111' ELSE CONVERT(nvarchar(255), AID) END)) AS LinkKalaAid
                        FROM         TRANSACTION2 AS TRANSACTION2_1) AS TRANSACTION2 INNER JOIN
                      T_NameKala ON TRANSACTION2.KALAID = T_NameKala.ID LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.LinkKalaAid = Fun_SubAkharinKharid_1.LinkKalaAid
WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (NOT (TRANSACTION2.KALAID IS NULL))
       	
       	OPEN KalaID_Cursor    
       	
       	FETCH NEXT FROM KalaID_Cursor      
       	
       	INTO @AID, @GOKID  ,@KALAID, @Keraieh, @UniPrice1,@NameKala1,@KeraiehGH    
       	
       	WHILE @@FETCH_STATUS = 0    	
       	
       		BEGIN  	 	 			
       			SET @KALAIDNew=NULL 	 		 			
       				IF ( @Keraieh >0 ) 			 					
       					BEGIN 	 		         								
								SELECT    @KALAIDNew=  FunInfoKalaAll_1.ID
								FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 RIGHT OUTER JOIN
								dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.KALAID = FunInfoKalaAll_1.ID
								WHERE     (FunInfoKalaAll_1.UnitPrice2 = @Keraieh + @KeraiehGH) AND (Fun_SubAkharinKharid_1.MaxUnitPriceKh = @UniPrice1) AND (FunInfoKalaAll_1.NameKala1 = @NameKala1) AND 
								(FunInfoKalaAll_1.AID = @AID2) OR
								(FunInfoKalaAll_1.UnitPrice2 = @Keraieh + @KeraiehGH) AND (Fun_SubAkharinKharid_1.MaxUnitPriceKh IS NULL) AND (FunInfoKalaAll_1.NameKala1 = @NameKala1) AND 
								(FunInfoKalaAll_1.AID = @AID2) 													   							  
       							
       							IF ( @KALAIDNew IS NULL) 					 									
       								BEGIN 						 										
       									DECLARE @KalaNumberNew INT; 					 						 										
										SELECT     @KalaNumberNew=	MAX(CONVERT(INT, KalaNumber) + 1) 
										FROM         T_NameKala 						 										 										
										
										INSERT INTO T_NameKala
										(KalaNumber, DateCreate, DateModified, NameKala1, Active, VAHED1, VAHED2, Zarib, UnitPrice2)
										VALUES     (@KalaNumberNew, GETDATE(), GETDATE(),@NameKala1, 0, 1, 1, 1,@Keraieh +@KeraiehGH) 						 						 										 										
										
										SELECT     @KalaIDNew=ID FROM T_NameKala WHERE     (KalaNumber = @KalaNumberNew) 	 										 										
										
										INSERT INTO T_Kala (NameKalaID, AID, GOKID, DateCreate, DateModified, VKalaID) 						 										
										VALUES     (@KalaIDNew,@AID2,@GOKID, GETDATE(), GETDATE(), 1) 						 					 									
       								END	  								
       								
       							ELSE 									
       								UPDATE    T_NameKala 											
       										SET              Active = 0 	WHERE     (ID = @KalaIDNew)						 					
       							END					 		 					 			
       			ELSE  					 				
       					BEGIN 							 					
       						SET @KALAIDNew=@KALAID 		 					 					
       						
       						SELECT     @KALAIDNewGh=COUNT(*)  					
       						FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 					
       						WHERE     (AID = @AID2) AND (ID = @KALAIDNew)  					 					
       						
       						IF (@KALAIDNewGh=0) 								 							
       							INSERT INTO T_Kala 	(NameKalaID, AID, GOKID, DateCreate, DateModified, VKalaID)  							
       								VALUES			(@KalaIDNew,@AID2,@GOKID, GETDATE(), GETDATE(), 1)  					 				
       					END	 							 			  				 
       					
				INSERT INTO [TRANSACTION]
									  (Zarib, FieldN7, Maliatkala, Avarez, radif, Tedad2, ID1, FieldN1, FieldN2, SAID2Number, OperationCode, FieldN6, AID, GOKID, KALAID, TextAIDKharid, Sharh, TedadVA, MabVahedHazine, FieldN3, 
									  Date1, GOID, KOLID, HESABID, THESABID, Dateeng, DARBed, PARBes, VKALAID, DateCreate, DateModified, UnitPriceVo)
				SELECT     TRANSACTION2.Zarib, TRANSACTION2.FieldN7, TRANSACTION2.FieldN1, TRANSACTION2.FieldN2, TRANSACTION2.radif, TRANSACTION2.Tedad2, @PrimeryOperationID AS ex1, 
									  @PrimeryOperationID AS ex2, @PrimarySAnadID AS ex3, @SecondrySanadid AS ex4, @SecondryOperationCode AS ex5, TRANSACTION2.FieldN6, @AID2 AS Expr1, TRANSACTION2.GOKID, 
									  @KALAIDNew AS Expr2, TRANSACTION2.TextAIDKharid, TRANSACTION2.Sharh, TRANSACTION2.Tedad, TRANSACTION2.UnitPrice, 
									  Fun_SubAkharinKharid_1.MaxUnitPriceKh * TRANSACTION2.Tedad AS ex6, @Date1 AS ex7, @GorupHesabID AS ex8, @KolHesabID AS ex9, @MoinHesabID AS ex10, @TafsiliHesabID AS ex11, 
									  @DateMiladi AS ex12, NULL AS Ex13, NULL AS Ex14, 1 AS ex15, @DateNow AS fsf, @DateNow AS dfghf, Fun_SubAkharinKharid_1.MaxUnitPriceKh
				FROM         (SELECT     TRID, TRAID, SAID2, RadifSAID2, TextNHSID2, TextShSA, HESABID, GOID, KOLID, THESABID, MOINID, KALAID, NAMEKHID, AID, GAID, TedadVA, TedadKhA, VKALAID, ModelKala, SAID, 
															  TextNumberSA, GOKID, PARBes, DARBed, TEMP, TEMPR, TempMandeh, Sharh, radif, Date1, BANKID, KHOROJID, TextAIDKh, TextNumberKHO, TedadKhorooj, VORODID, TextAID, 
															  TextNumberVO, TedadList, TedadVorood, SHomareKOtag, SHomarePArvaneh, SHomarePAking, SHoKAmyon, SHomareLAng, PFOROSHID, FOROSHID, SForoshID, TextAIDFo, 
															  TextNumberFO, TedadFo, UnitPriceFo, PKHARIDID, KHARIDID, TextAIDKharid, TextNumberKH, Tedad, UnitPrice, SumofPrice, BARFOROSHID, TedadBarFo, UnitPriceBarFo, BARKHARIDID, 
															  TedadBarKh, UnitPriceBarKh, DKHID, TedadDKh, UnitPriceDKh, HKHID, TedadHKh, UnitPriceHKh, TAFOID, TAKHID, IDSHARH, NobatSandoghid, PARID1, PARID2, PARID3, PARID4, 
															  PARID5, PARID6, PARID7, PARID8, DARID1, DARID2, DARID3, DARID4, DARID5, DARID6, DARID7, DARID8, ID1, ID2, ID3, ID4, FieldN1, FieldN2, FieldN3, FieldN4, FieldN5, FieldN6, 
															  FieldN7, FieldT1, FieldT2, FieldT3, FieldT4, FieldT5, FieldT6, FieldT7, FieldT8, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, Flag9, Flag10, TextKalaName, LinkedID, OperType, 
															  CurMablagh, CurRef, CurRate, Tedad2, Tedad1, Zarib, TedadHazine, MabVahedHazine, ThesabIDSath2, CONVERT(Float, CONVERT(nvarchar(255), KALAID) + N'.' + (CASE WHEN (AID % 10)
															   = 0 THEN CONVERT(nvarchar(255), AID) + '11111' ELSE CONVERT(nvarchar(255), AID) END)) AS LinkKalaAid
										FROM         TRANSACTION2 AS TRANSACTION2_1) AS TRANSACTION2 LEFT OUTER JOIN
									  dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.LinkKalaAid = Fun_SubAkharinKharid_1.LinkKalaAid
				WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.AID = @AID) AND (TRANSACTION2.KALAID = @KALAID) AND (TRANSACTION2.UnitPrice = @Keraieh) AND 
									  (TRANSACTION2.TRAID IS NULL OR
									  TRANSACTION2.TRAID = 0)
				ORDER BY TRANSACTION2.radif
       					 
				FETCH NEXT FROM KalaID_Cursor    	 

				INTO @AID, @GOKID  ,@KALAID, @Keraieh, @UniPrice1,@NameKala1,@KeraiehGH  	  
		END     
		
		CLOSE KalaID_Cursor;    
		
		DEALLOCATE KalaID_Cursor;  	  
       
       END       

GO




/****** Object:  StoredProcedure [dbo].[EDitKala_Table1_InsertSF1]    Script Date: 12/11/2017 09:49:21 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EDitKala_Table1_InsertSF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[EDitKala_Table1_InsertSF1]
GO


/****** Object:  StoredProcedure [dbo].[EDitKala_Table1_InsertSF1]    Script Date: 12/11/2017 09:49:21 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


       CREATE PROCEDURE [dbo].[EDitKala_Table1_InsertSF1] 		 		      
       
       @AID2 INT, 		      
       @LinkMasterField INT, 		      
       @PrimeryOperationID INT, 		      
       @PrimarySAnadID INT, 		      
       @SecondrySanadid INT, 		      
       @LinkTosub NVARCHAR(255), 		      
       @TypeOfFact INT, 		      
       @SecondryOperationCode NVARCHAR(255), 		      
       @Date1 NVARCHAR(20), 		      
       @GorupHesabID  INT, 		      
       @KolHesabID INT, 		      
       @MoinHesabID INT, 		      
       @TafsiliHesabID INT, 		      
       @DateMiladi NVARCHAR(20)  AS        
       
       BEGIN        
       	
       	SET NOCOUNT ON;          
       	DECLARE @FieldN3 DECIMAL(19,4)       
       	DECLARE @DateNow datetime        
       	
       	set @DateNow=GETDATE()               
       	
		DELETE FROM [TRANSACTION]
		WHERE     (TedadKhA > 0) AND (ID1 = @LinkMasterField) AND (NOT (TRAID IN
		(SELECT     TRAID
		FROM         TRANSACTION2
		WHERE     (Date1 = @LinkTosub) AND (TRAID > 0))))                             
		
		
UPDATE    [TRANSACTION]
SET              Tedad2 = TRANSACTION2.Tedad2, ID1 = @PrimeryOperationID, FieldN1 = @PrimeryOperationID, FieldN2 = @PrimarySAnadID, SAID2Number = @SecondrySanadid, 
                      OperationCode = @SecondryOperationCode, Sharh = TRANSACTION2.Sharh, TedadKhA = TRANSACTION2.Tedad, MabVahedHazine = TRANSACTION2.UnitPrice, 
                      FieldN3 = TRANSACTION2.Tedad * Fun_SubAkharinKharid_1.MaxUnitPriceKh, Date1 = @Date1, GOID = @GorupHesabID, KOLID = @KolHesabID, HESABID = @MoinHesabID, 
                      THESABID = @TafsiliHesabID, Dateeng = @DateMiladi, VKALAID = 1, DateModified = @DateNow, UnitPriceKho = Fun_SubAkharinKharid_1.MaxUnitPriceKh
FROM         (SELECT     TRID, TRAID, SAID2, RadifSAID2, TextNHSID2, TextShSA, HESABID, GOID, KOLID, THESABID, MOINID, KALAID, NAMEKHID, AID, GAID, TedadVA, TedadKhA, VKALAID, ModelKala, SAID, 
                                              TextNumberSA, GOKID, PARBes, DARBed, TEMP, TEMPR, TempMandeh, Sharh, radif, Date1, BANKID, KHOROJID, TextAIDKh, TextNumberKHO, TedadKhorooj, VORODID, TextAID, 
                                              TextNumberVO, TedadList, TedadVorood, SHomareKOtag, SHomarePArvaneh, SHomarePAking, SHoKAmyon, SHomareLAng, PFOROSHID, FOROSHID, SForoshID, TextAIDFo, 
                                              TextNumberFO, TedadFo, UnitPriceFo, PKHARIDID, KHARIDID, TextAIDKharid, TextNumberKH, Tedad, UnitPrice, SumofPrice, BARFOROSHID, TedadBarFo, UnitPriceBarFo, BARKHARIDID, 
                                              TedadBarKh, UnitPriceBarKh, DKHID, TedadDKh, UnitPriceDKh, HKHID, TedadHKh, UnitPriceHKh, TAFOID, TAKHID, IDSHARH, NobatSandoghid, PARID1, PARID2, PARID3, PARID4, 
                                              PARID5, PARID6, PARID7, PARID8, DARID1, DARID2, DARID3, DARID4, DARID5, DARID6, DARID7, DARID8, ID1, ID2, ID3, ID4, FieldN1, FieldN2, FieldN3, FieldN4, FieldN5, FieldN6, 
                                              FieldN7, FieldT1, FieldT2, FieldT3, FieldT4, FieldT5, FieldT6, FieldT7, FieldT8, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, Flag9, Flag10, TextKalaName, LinkedID, OperType, 
                                              CurMablagh, CurRef, CurRate, Tedad2, Tedad1, Zarib, TedadHazine, MabVahedHazine, ThesabIDSath2, CONVERT(Float, CONVERT(nvarchar(255), KALAID) + N'.' + (CASE WHEN (AID % 10)
                                               = 0 THEN CONVERT(nvarchar(255), AID) + '11111' ELSE CONVERT(nvarchar(255), AID) END)) AS LinkKalaAid
                        FROM         TRANSACTION2 AS TRANSACTION2_1) AS TRANSACTION2 INNER JOIN
                      [TRANSACTION] ON TRANSACTION2.TRAID = [TRANSACTION].TRAID LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.LinkKalaAid = Fun_SubAkharinKharid_1.LinkKalaAid
WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (NOT (TRANSACTION2.TRAID IS NULL))



INSERT INTO [TRANSACTION]
                      (Zarib, FieldN7, Maliatkala, Avarez, radif, Tedad2, ID1, FieldN1, FieldN2, SAID2Number, OperationCode, FieldN6, AID, GOKID, KALAID, TextAIDKharid, Sharh, TedadKhA, MabVahedHazine, FieldN3, 
                      Date1, GOID, KOLID, HESABID, THESABID, Dateeng, DARBed, PARBes, VKALAID, DateCreate, DateModified, UnitPriceKho)
SELECT     TRANSACTION2.Zarib, TRANSACTION2.FieldN7, TRANSACTION2.FieldN1, TRANSACTION2.FieldN2, TRANSACTION2.radif, TRANSACTION2.Tedad2, @PrimeryOperationID AS ex1, 
                      @PrimeryOperationID AS ex2, @PrimarySAnadID AS ex3, @SecondrySanadid AS ex4, @SecondryOperationCode AS ex5, TRANSACTION2.FieldN6, TRANSACTION2.AID, TRANSACTION2.GOKID, 
                      TRANSACTION2.KALAID, TRANSACTION2.TextAIDKharid, TRANSACTION2.Sharh, TRANSACTION2.Tedad, TRANSACTION2.UnitPrice, 
                      Fun_SubAkharinKharid_1.MaxUnitPriceKh * TRANSACTION2.TedadKhA AS ex6, @Date1 AS ex7, @GorupHesabID AS ex8, @KolHesabID AS ex9, @MoinHesabID AS ex10, @TafsiliHesabID AS ex11, 
                      @DateMiladi AS ex12, NULL AS Ex13, NULL AS Ex14, 1 AS ex15, @DateNow AS fsf, @DateNow AS dfghf, Fun_SubAkharinKharid_1.MaxUnitPriceKh
FROM         (SELECT     TRID, TRAID, SAID2, RadifSAID2, TextNHSID2, TextShSA, HESABID, GOID, KOLID, THESABID, MOINID, KALAID, NAMEKHID, AID, GAID, TedadVA, TedadKhA, VKALAID, ModelKala, SAID, 
                                              TextNumberSA, GOKID, PARBes, DARBed, TEMP, TEMPR, TempMandeh, Sharh, radif, Date1, BANKID, KHOROJID, TextAIDKh, TextNumberKHO, TedadKhorooj, VORODID, TextAID, 
                                              TextNumberVO, TedadList, TedadVorood, SHomareKOtag, SHomarePArvaneh, SHomarePAking, SHoKAmyon, SHomareLAng, PFOROSHID, FOROSHID, SForoshID, TextAIDFo, 
                                              TextNumberFO, TedadFo, UnitPriceFo, PKHARIDID, KHARIDID, TextAIDKharid, TextNumberKH, Tedad, UnitPrice, SumofPrice, BARFOROSHID, TedadBarFo, UnitPriceBarFo, BARKHARIDID, 
                                              TedadBarKh, UnitPriceBarKh, DKHID, TedadDKh, UnitPriceDKh, HKHID, TedadHKh, UnitPriceHKh, TAFOID, TAKHID, IDSHARH, NobatSandoghid, PARID1, PARID2, PARID3, PARID4, 
                                              PARID5, PARID6, PARID7, PARID8, DARID1, DARID2, DARID3, DARID4, DARID5, DARID6, DARID7, DARID8, ID1, ID2, ID3, ID4, FieldN1, FieldN2, FieldN3, FieldN4, FieldN5, FieldN6, 
                                              FieldN7, FieldT1, FieldT2, FieldT3, FieldT4, FieldT5, FieldT6, FieldT7, FieldT8, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, Flag9, Flag10, TextKalaName, LinkedID, OperType, 
                                              CurMablagh, CurRef, CurRate, Tedad2, Tedad1, Zarib, TedadHazine, MabVahedHazine, ThesabIDSath2, CONVERT(Float, CONVERT(nvarchar(255), KALAID) + N'.' + (CASE WHEN (AID % 10)
                                               = 0 THEN CONVERT(nvarchar(255), AID) + '11111' ELSE CONVERT(nvarchar(255), AID) END)) AS LinkKalaAid
                        FROM         TRANSACTION2 AS TRANSACTION2_1) AS TRANSACTION2 LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.LinkKalaAid = Fun_SubAkharinKharid_1.LinkKalaAid
WHERE     (NOT (TRANSACTION2.KALAID IS NULL)) AND (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.TRAID IS NULL OR
                      TRANSACTION2.TRAID = 0)
ORDER BY TRANSACTION2.radif


DELETE FROM [TRANSACTION]
WHERE     (ID1 = @LinkMasterField) AND (TedadVA > 0) AND (NOT (KALAID IN
                          (SELECT     Tra.KALAID
                             FROM         (SELECT     TRANSACTION2.KALAID, TRANSACTION2.Tedad, TRANSACTION2.UnitPrice + T_NameKala.UnitPrice2 AS Expr1, T_NameKala.NameKala1
                                                     FROM         TRANSACTION2 INNER JOIN
                                                                           T_NameKala ON TRANSACTION2.KALAID = T_NameKala.ID
                                                     WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.TRAID > 0)) AS Temp INNER JOIN
                                                       (SELECT     [TRANSACTION].ID1, [TRANSACTION].KALAID, [TRANSACTION].TedadVA, T_NameKala_1.UnitPrice2, T_NameKala_1.NameKala1
                                                          FROM         [TRANSACTION] INNER JOIN
                                                                                T_NameKala AS T_NameKala_1 ON [TRANSACTION].KALAID = T_NameKala_1.ID
                                                          WHERE     ([TRANSACTION].ID1 = @LinkMasterField) AND ([TRANSACTION].TedadVA > 0)) AS Tra ON Temp.NameKala1 = Tra.NameKala1 AND Temp.Expr1 = Tra.UnitPrice2)))
    

UPDATE    [TRANSACTION]
SET              TedadVA = Temp_1.Tedad, Sharh = Temp_1.Sharh, FieldN3 = Temp_1.Tedad * Fun_SubAkharinKharid_1.MaxUnitPriceKh
FROM         [TRANSACTION] INNER JOIN
                          (SELECT     TRA.KALAID, Temp.Tedad, Temp.Sharh
                             FROM         (SELECT     TRANSACTION_1.ID1, TRANSACTION_1.KALAID, TRANSACTION_1.TedadVA, T_NameKala_1.UnitPrice2, T_NameKala_1.NameKala1
                                                     FROM         [TRANSACTION] AS TRANSACTION_1 INNER JOIN
                                                                           T_NameKala AS T_NameKala_1 ON TRANSACTION_1.KALAID = T_NameKala_1.ID
                                                     WHERE     (TRANSACTION_1.ID1 = @LinkMasterField) AND (TRANSACTION_1.TedadVA > 0)) AS TRA INNER JOIN
                                                       (SELECT     TRANSACTION2.KALAID, TRANSACTION2.Tedad, TRANSACTION2.UnitPrice + T_NameKala.UnitPrice2 AS Keraieh, T_NameKala.NameKala1, TRANSACTION2.Sharh
                                                          FROM         TRANSACTION2 INNER JOIN
                                                                                T_NameKala ON TRANSACTION2.KALAID = T_NameKala.ID
                                                          WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.TRAID > 0)) AS Temp ON TRA.UnitPrice2 = Temp.Keraieh AND TRA.NameKala1 = Temp.NameKala1) 
                      AS Temp_1 ON [TRANSACTION].KALAID = Temp_1.KALAID LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 ON [TRANSACTION].AID = Fun_SubAkharinKharid_1.AID AND [TRANSACTION].KALAID = Fun_SubAkharinKharid_1.KALAID
WHERE     ([TRANSACTION].ID1 = @LinkMasterField) AND ([TRANSACTION].TedadVA > 0)



DECLARE @AID INT, @GOKID  INT,@KALAID INT, @Keraieh MONEY, @UniPrice1 MONEY, @KeraiehGH  MONEY,@NameKala1 NVARCHAR(255),@KALAIDNew INT,@KALAIDNewGH INT;   

DECLARE KalaID_Cursor   CURSOR FOR     

SELECT     TRANSACTION2.AID, TRANSACTION2.GOKID, TRANSACTION2.KALAID, TRANSACTION2.UnitPrice AS Keraieh, Fun_SubAkharinKharid_1.MaxUnitPriceKh AS UniPrice1, T_NameKala.NameKala1, 
                      T_NameKala.UnitPrice2 AS KeraiehGH
FROM         (SELECT     TRID, TRAID, SAID2, RadifSAID2, TextNHSID2, TextShSA, HESABID, GOID, KOLID, THESABID, MOINID, KALAID, NAMEKHID, AID, GAID, TedadVA, TedadKhA, VKALAID, ModelKala, SAID, 
                                              TextNumberSA, GOKID, PARBes, DARBed, TEMP, TEMPR, TempMandeh, Sharh, radif, Date1, BANKID, KHOROJID, TextAIDKh, TextNumberKHO, TedadKhorooj, VORODID, TextAID, 
                                              TextNumberVO, TedadList, TedadVorood, SHomareKOtag, SHomarePArvaneh, SHomarePAking, SHoKAmyon, SHomareLAng, PFOROSHID, FOROSHID, SForoshID, TextAIDFo, 
                                              TextNumberFO, TedadFo, UnitPriceFo, PKHARIDID, KHARIDID, TextAIDKharid, TextNumberKH, Tedad, UnitPrice, SumofPrice, BARFOROSHID, TedadBarFo, UnitPriceBarFo, BARKHARIDID, 
                                              TedadBarKh, UnitPriceBarKh, DKHID, TedadDKh, UnitPriceDKh, HKHID, TedadHKh, UnitPriceHKh, TAFOID, TAKHID, IDSHARH, NobatSandoghid, PARID1, PARID2, PARID3, PARID4, 
                                              PARID5, PARID6, PARID7, PARID8, DARID1, DARID2, DARID3, DARID4, DARID5, DARID6, DARID7, DARID8, ID1, ID2, ID3, ID4, FieldN1, FieldN2, FieldN3, FieldN4, FieldN5, FieldN6, 
                                              FieldN7, FieldT1, FieldT2, FieldT3, FieldT4, FieldT5, FieldT6, FieldT7, FieldT8, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, Flag9, Flag10, TextKalaName, LinkedID, OperType, 
                                              CurMablagh, CurRef, CurRate, Tedad2, Tedad1, Zarib, TedadHazine, MabVahedHazine, ThesabIDSath2, CONVERT(Float, CONVERT(nvarchar(255), KALAID) + N'.' + (CASE WHEN (AID % 10)
                                               = 0 THEN CONVERT(nvarchar(255), AID) + '11111' ELSE CONVERT(nvarchar(255), AID) END)) AS LinkKalaAid
                        FROM         TRANSACTION2 AS TRANSACTION2_1) AS TRANSACTION2 INNER JOIN
                      T_NameKala ON TRANSACTION2.KALAID = T_NameKala.ID LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.LinkKalaAid = Fun_SubAkharinKharid_1.LinkKalaAid
WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.TRAID IS NULL OR
                      TRANSACTION2.TRAID = 0) AND (NOT (TRANSACTION2.KALAID IS NULL))                                                                                

OPEN KalaID_Cursor   

FETCH NEXT FROM KalaID_Cursor     

INTO @AID, @GOKID  ,@KALAID, @Keraieh, @UniPrice1,@NameKala1,@KeraiehGH   

WHILE @@FETCH_STATUS = 0    
	BEGIN  	 	 	
		SET @KALAIDNew=NULL 	 	
			
			IF ( @Keraieh>0 ) 				  				
				BEGIN  		 	 											 				
					SELECT    @KALAIDNew= FunInfoKalaAll_1.ID 				
					FROM         (SELECT     KALAID 										FROM         [TRANSACTION] 										
					              WHERE     (ID1 = @LinkMasterField) AND (TedadVA > 0)) AS derivedtbl_1 INNER JOIN 									  
					              dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 ON derivedtbl_1.KALAID = Fun_SubAkharinKharid_1.KALAID 
					              RIGHT OUTER JOIN 									  
					              dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.KALAID = FunInfoKalaAll_1.ID 				
					WHERE     (FunInfoKalaAll_1.UnitPrice2 = @Keraieh + @KeraiehGH) AND (Fun_SubAkharinKharid_1.MaxUnitPriceKh = @UniPrice1) AND 
					(FunInfoKalaAll_1.NameKala1 = @NameKala1) AND  									  
					(FunInfoKalaAll_1.AID = @AID2) AND (derivedtbl_1.KALAID IS NULL) OR 									  
					(FunInfoKalaAll_1.UnitPrice2 = @Keraieh + @KeraiehGH) AND (Fun_SubAkharinKharid_1.MaxUnitPriceKh IS NULL) AND 
					(FunInfoKalaAll_1.NameKala1 = @NameKala1) AND  									  
					(FunInfoKalaAll_1.AID = @AID2) AND (derivedtbl_1.KALAID IS NULL) OR 									  
					(FunInfoKalaAll_1.UnitPrice2 = @Keraieh + @KeraiehGH) AND (FunInfoKalaAll_1.NameKala1 = @NameKala1) AND (FunInfoKalaAll_1.AID = @AID2) AND (NOT (derivedtbl_1.KALAID IS NULL))						 						 						
					
						IF ( @KALAIDNew IS NULL) 						 												
							BEGIN 							 													
								DECLARE @KalaNumberNew INT;											
								
								SELECT    @KalaNumberNew= MAX(CONVERT(INT, KalaNumber) + 1)  FROM    T_NameKala
								
								INSERT INTO T_NameKala 	(KalaNumber, DateCreate, DateModified, NameKala1, Active, VAHED1, VAHED2, Zarib, UnitPrice2)
								VALUES     (@KalaNumberNew, GETDATE(), GETDATE(),@NameKala1, 0, 1, 1, 1,@Keraieh+@KeraiehGH)
								
								SELECT @KalaIDNew=ID FROM T_NameKala WHERE     (KalaNumber = @KalaNumberNew)
								INSERT INTO T_Kala 	(NameKalaID, AID, GOKID, DateCreate, DateModified, VKalaID) 							 													
								VALUES     (@KalaIDNew,@AID2,@GOKID, GETDATE(), GETDATE(), 1) 							 						 												
							END 						
							
						ELSE 							
							UPDATE    T_NameKala 									
							SET Active = 0 WHERE     (ID = @KalaIDNew) 
						END 			 	 
			ELSE 					 					
					BEGIN 							 						
						SET @KALAIDNew=@KALAID
						SELECT @KALAIDNewGh=COUNT(*) FROM dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 WHERE (AID = @AID2) AND (ID = @KALAIDNew) 
						
						IF (@KALAIDNewGh =0) 
							INSERT INTO T_Kala (NameKalaID, AID, GOKID, DateCreate, DateModified, VKalaID)
							VALUES			(@KalaIDNew,@AID2,@GOKID, GETDATE(), GETDATE(), 1)
						END				  					   
						
						INSERT INTO [TRANSACTION]
											  (Zarib, FieldN7, Maliatkala, Avarez, radif, Tedad2, ID1, FieldN1, FieldN2, SAID2Number, OperationCode, FieldN6, AID, GOKID, KALAID, TextAIDKharid, Sharh, TedadVA, MabVahedHazine, FieldN3, 
											  Date1, GOID, KOLID, HESABID, THESABID, Dateeng, DARBed, PARBes, VKALAID, DateCreate, DateModified, UnitPriceVo)
						SELECT     TRANSACTION2.Zarib, TRANSACTION2.FieldN7, TRANSACTION2.FieldN1, TRANSACTION2.FieldN2, TRANSACTION2.radif, TRANSACTION2.Tedad2, @PrimeryOperationID AS ex1, 
											  @PrimeryOperationID AS ex2, @PrimarySAnadID AS ex3, @SecondrySanadid AS ex4, @SecondryOperationCode AS ex5, TRANSACTION2.FieldN6, @AID2 AS Expr1, TRANSACTION2.GOKID, 
											  @KALAIDNew AS Expr2, TRANSACTION2.TextAIDKharid, TRANSACTION2.Sharh, TRANSACTION2.Tedad, TRANSACTION2.UnitPrice, 
											  Fun_SubAkharinKharid_1.MaxUnitPriceKh * TRANSACTION2.Tedad AS Expr3, @Date1 AS ex7, @GorupHesabID AS ex8, @KolHesabID AS ex9, @MoinHesabID AS ex10, @TafsiliHesabID AS ex11, 
											  @DateMiladi AS ex12, NULL AS Ex13, NULL AS Ex14, 1 AS ex15, @DateNow AS fsf, @DateNow AS dfghf, Fun_SubAkharinKharid_1.MaxUnitPriceKh
						FROM         (SELECT     TRID, TRAID, SAID2, RadifSAID2, TextNHSID2, TextShSA, HESABID, GOID, KOLID, THESABID, MOINID, KALAID, NAMEKHID, AID, GAID, TedadVA, TedadKhA, VKALAID, ModelKala, SAID, 
																	  TextNumberSA, GOKID, PARBes, DARBed, TEMP, TEMPR, TempMandeh, Sharh, radif, Date1, BANKID, KHOROJID, TextAIDKh, TextNumberKHO, TedadKhorooj, VORODID, TextAID, 
																	  TextNumberVO, TedadList, TedadVorood, SHomareKOtag, SHomarePArvaneh, SHomarePAking, SHoKAmyon, SHomareLAng, PFOROSHID, FOROSHID, SForoshID, TextAIDFo, 
																	  TextNumberFO, TedadFo, UnitPriceFo, PKHARIDID, KHARIDID, TextAIDKharid, TextNumberKH, Tedad, UnitPrice, SumofPrice, BARFOROSHID, TedadBarFo, UnitPriceBarFo, BARKHARIDID, 
																	  TedadBarKh, UnitPriceBarKh, DKHID, TedadDKh, UnitPriceDKh, HKHID, TedadHKh, UnitPriceHKh, TAFOID, TAKHID, IDSHARH, NobatSandoghid, PARID1, PARID2, PARID3, PARID4, 
																	  PARID5, PARID6, PARID7, PARID8, DARID1, DARID2, DARID3, DARID4, DARID5, DARID6, DARID7, DARID8, ID1, ID2, ID3, ID4, FieldN1, FieldN2, FieldN3, FieldN4, FieldN5, FieldN6, 
																	  FieldN7, FieldT1, FieldT2, FieldT3, FieldT4, FieldT5, FieldT6, FieldT7, FieldT8, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, Flag9, Flag10, TextKalaName, LinkedID, OperType, 
																	  CurMablagh, CurRef, CurRate, Tedad2, Tedad1, Zarib, TedadHazine, MabVahedHazine, ThesabIDSath2, CONVERT(Float, CONVERT(nvarchar(255), KALAID) + N'.' + (CASE WHEN (AID % 10)
																	   = 0 THEN CONVERT(nvarchar(255), AID) + '11111' ELSE CONVERT(nvarchar(255), AID) END)) AS LinkKalaAid
												FROM         TRANSACTION2 AS TRANSACTION2_1) AS TRANSACTION2 LEFT OUTER JOIN
											  dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.LinkKalaAid = Fun_SubAkharinKharid_1.LinkKalaAid
						WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.OperType = @TypeOfFact) AND (TRANSACTION2.AID = @AID) AND (TRANSACTION2.KALAID = @KALAID) AND 
											  (TRANSACTION2.UnitPrice = @Keraieh)
						ORDER BY TRANSACTION2.radif
										
						FETCH NEXT FROM KalaID_Cursor    	 
						
						INTO @AID, @GOKID  ,@KALAID, @Keraieh, @UniPrice1,@NameKala1,@KeraiehGH  	  
	END     
	
CLOSE KalaID_Cursor;    

DEALLOCATE KalaID_Cursor;  	  
       
       END                   

GO





/****** Object:  StoredProcedure [dbo].[CloseHesab_Preview_ListMojodiManfi]    Script Date: 12/11/2017 09:52:13 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_Preview_ListMojodiManfi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_Preview_ListMojodiManfi]
GO


/****** Object:  StoredProcedure [dbo].[CloseHesab_Preview_ListMojodiManfi]    Script Date: 12/11/2017 09:52:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  CREATE PROCEDURE [dbo].[CloseHesab_Preview_ListMojodiManfi]            
  	
  	AS     
  	
  	
SELECT     SubTransactionKala_1.KALAID, SubTransactionKala_1.AID, SubTransactionKala_1.GOKID, SUM(SubTransactionKala_1.Tedad) AS SumTedad, MAX(ISNULL(FunInfoKalaAll_1.MinMogoody, 0)) 
                      AS MinMogoody, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) AS Mande, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.KalaNumber, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.BarCode
FROM         View_SubTransactionKala AS SubTransactionKala_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON SubTransactionKala_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid 
GROUP BY SubTransactionKala_1.KALAID, SubTransactionKala_1.AID, SubTransactionKala_1.GOKID, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameVahed1, 
                      FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.BarCode
HAVING      (SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) < 0)
ORDER BY FunInfoKalaAll_1.NameKala1

GO







/****** Object:  StoredProcedure [dbo].[CloseHesab_ChekMojodiManfi]    Script Date: 03/15/2018 13:08:40 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_ChekMojodiManfi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_ChekMojodiManfi]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_ChekMojodiManfi]    Script Date: 03/15/2018 13:08:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[CloseHesab_ChekMojodiManfi]

        @ResultVal INT =0 OUTPUT
AS

BEGIN

	declare @cntTedadManfi INT
	
	
	
SELECT     @cntTedadManfi=COUNT(*) 
FROM         (SELECT     TOP (100) PERCENT SubTransactionKala_1.KALAID, SubTransactionKala_1.AID, SubTransactionKala_1.GOKID, SUM(SubTransactionKala_1.Tedad) AS SumTedad, 
                      MAX(ISNULL(FunInfoKalaAll_1.MinMogoody, 0)) AS MinMogoody, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) AS Mande, 
                      FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.BarCode
FROM         View_SubTransactionKala AS SubTransactionKala_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON SubTransactionKala_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid
WHERE     (FunInfoKalaAll_1.VKalaID = 1)
GROUP BY SubTransactionKala_1.KALAID, SubTransactionKala_1.AID, SubTransactionKala_1.GOKID, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameVahed1, 
                      FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.BarCode
HAVING      (SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) < 0)
ORDER BY FunInfoKalaAll_1.NameKala1) AS MojodiManfi

	if (@cntTedadManfi> 0)
		BEGIN
			SET @ResultVal=1
		END
	ELSE
		BEGIN
			SET @ResultVal=0
		END
END



GO




/****** Object:  StoredProcedure [dbo].[Rep_05228729]    Script Date: 12/11/2017 09:59:05 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_05228729]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_05228729]
GO


/****** Object:  StoredProcedure [dbo].[Rep_05228729]    Script Date: 12/11/2017 09:59:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*
@S_PHI=forms![TreeViewRep_HesabAnbar]![Sth_PHI].column(0),
@SortRep=forms![TreeViewRep_HesabAnbar]![Sorting],
@KalaID =forms![TreeViewRep_HesabAnbar]![KalaID],
@NameKala =forms![TreeViewRep_HesabAnbar]![FilterNameKala],
@GbyKalaID =forms![TreeViewRep_HesabAnbar]![ChGbyKalaID],
@Gokid  =forms![TreeViewRep_HesabAnbar]![GoKala],
@NameGokid =forms![TreeViewRep_HesabAnbar]![FilterNameGOKALA],
@GbyGokid =forms![TreeViewRep_HesabAnbar]![ChGbyGokid],
@Aid  =forms![TreeViewRep_HesabAnbar]![AID],
@NameAid =forms![TreeViewRep_HesabAnbar]![FilterNameAID],
@GbyAid =forms![TreeViewRep_HesabAnbar]![ChGbyAid],
@Goid =forms![TreeViewRep_HesabAnbar]![Goid],
@KolID =forms![TreeViewRep_HesabAnbar]![KolID],
@HesabID =forms![TreeViewRep_HesabAnbar]![HesabID],
@THesabID =forms![TreeViewRep_HesabAnbar]![THesabID],
@startDate =forms![TreeViewRep_HesabAnbar]![AsDate],
@ENDdate =forms![TreeViewRep_HesabAnbar]![TaDate]
*/

  CREATE PROCEDURE [dbo].[Rep_05228729] ( 
	@S_PHI int,/****** براساس آخرین خرید (1)  قیمت سطح 1 (2) قیمت سطح 2 (3) قیمت سطح 4 (5) قیمت سطح 5 (6) قیمت سطح 6 (6)قیمت *******/
	@SortRep int, 
	@KalaID int,
	@NameKala nvarchar(255), 
	@GbyKalaID AS BIT,
	@Gokid int,
	@NameGokid nvarchar(255), 
	@GbyGokid AS BIT,
	@Aid int,
	@NameAid  nvarchar(255),
	@GbyAid AS BIT,
	@Goid int, 
	@KolID int, 
	@HesabID int, 
	@THesabID int, 								
	@startDate nvarchar(50),								
	@ENDdate nvarchar(50)
)           
  AS     
  BEGIN 	SET NOCOUNT ON; 
  	 	
  DECLARE @SQLString  NVARCHAR(Max); 
  DECLARE @SQL_SELECT  NVARCHAR(Max); 
  DECLARE @SQL_FROM NVARCHAR(Max); 
  DECLARE @SQL_WHERE  NVARCHAR(Max); 
  DECLARE @SQL_GROUP  NVARCHAR(Max); 
  DECLARE @SQL_HAVING  NVARCHAR(Max); 
  DECLARE @SQL_ORDER NVARCHAR(Max); 
  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max); 
  
  set @SQL_SELECT=N' ';
  Set @SQL_FROM=N' ';
  Set @SQL_WHERE=N' ';
  Set @SQL_GROUP=N' ';
  Set @SQL_ORDER=N' ';	
  set @WhrStatement  = N' ';         	
  set @ParmAndstr = N' ';  
  /*SEt @SQL_HAVING =N' HAVING      (SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) <> 0) ';*/
  SEt @SQL_HAVING =N' HAVING      (1 = 1) ';
  SET @ParmDefinition =N'@S_PHI int=null,
						@GbyKalaID AS BIT=null,
						@GbyGokid AS BIT=null,
						@SortRep int=null, 
						@KalaID int=null,
						@NameKala nvarchar(255)=null, 
						@Gokid int=null,
						@NameGokid nvarchar(255)=null, 
						@Aid int=null,
						@NameAid  nvarchar(255)=null,
						@GbyAid bit=1,
						@Goid int=null, 
						@KolID int=null, 
						@HesabID int=null, 
						@THesabID int=null, 								
						@startDate nvarchar(50)=''0'', 								
						@ENDdate nvarchar(50)=''99999999''';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		

  BEGIN 
  
set @SQL_WHERE=' WHERE     (SubTransactionKala_1.TedadSadre <> 0) '
  


		/* شرط انطباق انبار*/
  
  if (@GbyAid=1) 
			Begin
				Set @SQL_SELECT=@SQL_SELECT + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar '
				Set @SQL_GROUP=@SQL_GROUP + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber , FunInfoKalaAll_1.NameAnbar ' 
				
				if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' ( FunInfoKalaAll_1.AID = @Aid )'; 
					end

				if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 	
					End
			end
  else
		Begin
			if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' WHERE ')
							set @ParmAndstr=N' ' 	
						else
							set @ParmAndstr=N' AND '

						set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'( FunInfoKalaAll_1.AID = @Aid )'; 
					End					

			if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' ')
							set @ParmAndstr=N' WHERE ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						
						Set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 
					End						
		End  			
		
 
   /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@KalaID IS NULL)) 
			Begin
				if(@SQL_HAVING=N' ')
					set @ParmAndstr=N' WHERE ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'   (FunInfoKalaAll_1.ID = @KalaID ) ' 
								
			End
   
      /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@startDate IS NULL) AND NOT (@ENDdate IS NULL)) 
			Begin
				if(@SQL_WHERE=N' ')
					set @ParmAndstr=N' WHERE ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'   (SubTransactionKala_1.Tarikh between @startDate and @ENDdate) ' 
								
			END
			
  
    /* --------------------شرط انطباق قسمتی از نام کالا*/ 			
  if(NOT (@NameKala IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameKala='%'+@NameKala+'%'
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameKala1 LIKE @NameKala) '; 	
		End

	/* --------------------شرط انطباق گروه*/ 			

	if(NOT (@Gokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '
				
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @Gokid )'; 	
		End
  
      /* --------------------شرط انطباق قسمتی از نام گروه کالا*/ 			

	if(NOT (@NameGokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameGokid='%'+@NameGokid+'%'
			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameGoKala1 LIKE @NameGokid) '; 
		End	

      /* --------------------شرط انطباق حساب تفصيلي*/ 			

	if(NOT (@THesabID IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (THesabID=@THesabID) '; 
		End	

      /* --------------------شرط انطباق حساب تفصيلي*/ 			

	if(NOT (@Goid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (GoID=@Goid) '; 
		End	
	
		if(NOT (@KolID IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (KolID=@KolID) '; 
		End	
		
	if(NOT (@HesabID IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (HesabID=@HesabID) '; 
		End	

		
  Set @SQLString =N'SELECT     FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) AS SumTedad, 
                      FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, MAX(Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS Max_UnitPriceKh, MAX(FunInfoKalaAll_1.UnitPrice1) AS MaxUnitPrice1, 
                      MAX(FunInfoKalaAll_1.UnitPrice2) AS MaxUnitPrice2, MAX(FunInfoKalaAll_1.UnitPrice3) AS MaxUnitPrice3, MAX(FunInfoKalaAll_1.UnitPrice4) AS MaxUnitPrice4, MAX(FunInfoKalaAll_1.UnitPrice5) 
                      AS MaxUnitPrice5, MAX(FunInfoKalaAll_1.UnitPrice6) AS MaxUnitPrice6, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode, SUM(CONVERT(float, 
                      SubTransactionKala_1.TedadVarde)) AS TedadVarde, SUM(CONVERT(Float, abs(SubTransactionKala_1.TedadSadre))) AS TedadSadre, SUM(SubTransactionKala_1.TedadKh) AS TedadKh, 
                      SUM(SubTransactionKala_1.TedadVA) AS TedadVA, SUM(SubTransactionKala_1.TedadFo) AS TedadFo, SUM(SubTransactionKala_1.TedadKhorooj) AS TedadKhorooj, 
                      SUM(SubTransactionKala_1.TedadBarKh) AS TedadBarKh, SUM(SubTransactionKala_1.TedadKhA) AS TedadKhA, SUM(SubTransactionKala_1.TedadVorood) AS TedadVorood, 
                      SUM(SubTransactionKala_1.TedadBarFo) AS TedadBarFo, SubTransactionKala_1.GOID, SubTransactionKala_1.KolID, SubTransactionKala_1.HesabID, SubTransactionKala_1.THesabID, 
                      SubTransactionKala_1.Namehesab, SUM(SubTransactionKala_1.MSadre) AS MSadre, @startDate AS startDate,@ENDdate AS  ENDdate, MIN(SubTransactionKala_1.Tarikh) AS FIRST
                      , MAX(SubTransactionKala_1.Tarikh) AS Last
' + @SQL_SELECT + ' 
FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid INNER JOIN
                      View_SubTransactionKala AS SubTransactionKala_1 ON FunInfoKalaAll_1.LinkKalaAid = SubTransactionKala_1.LinkKalaAid  
' +@SQL_WHERE + '
GROUP BY FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode, 
                      SubTransactionKala_1.Namehesab, SubTransactionKala_1.GOID, SubTransactionKala_1.KolID, SubTransactionKala_1.HesabID, SubTransactionKala_1.THesabID
'+ @SQL_HAVING 


                      


		    /* --------------------    1مرتب سازی */ 			
    if(@SortRep=1) 	
			set @SQLString= @SQLString  + N'  ORDER BY KalaNumber,NameKala1 '; 	
    		    /* --------------------    2مرتب سازی */ 			
    if(@SortRep=2) 	
			set @SQLString= @SQLString + N'  ORDER BY NameKala1,KalaNumber '; 
    		    /* --------------------    3مرتب سازی */ 			
    if(@SortRep=3) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKalanumbr,NameKala1 '; 
    		    /* --------------------    4مرتب سازی */ 			
    if(@SortRep=4) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKala1,NameKala1 '; 	
        		    /* --------------------    5مرتب سازی */ 			
	 IF ((@SortRep=5) AND (@GbyAid=1)) 	
			set @SQLString= @SQLString + N'  ORDER BY AIDNumber,NameKala1 '; 
    		    /* --------------------    6مرتب سازی */ 			
    IF ((@SortRep=6) AND (@GbyAid=1))	
			set @SQLString= @SQLString + N' ORDER BY NameAnbar,NameKala1 '; 		


    
    EXECUTE sp_executesql @SQLString, @ParmDefinition,@GbyKalaID=@GbyKalaID,@GbyGokid=@GbyGokid,@SortRep=@SortRep,@S_PHI=@S_PHI,@KalaID=@KalaID,@NameKala=@NameKala,@Gokid=@Gokid,@NameGokid=@NameGokid,@Aid=@Aid,@NameAid=@NameAid,@GbyAid=@GbyAid,@Goid=@Goid,@KolID =@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@startDate=@startDate,@ENDdate =@ENDdate;  		
    END 		 
    /* --------پایان */  		 		 
return END      
	
								
							  
GO



/****** Object:  StoredProcedure [dbo].[Rep_05228730]    Script Date: 12/11/2017 09:59:09 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_05228730]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_05228730]
GO


/****** Object:  StoredProcedure [dbo].[Rep_05228730]    Script Date: 12/11/2017 09:59:09 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*
@S_PHI=forms![TreeViewRep_HesabAnbar]![Sth_PHI].column(0),
@SortRep=forms![TreeViewRep_HesabAnbar]![Sorting],
@KalaID =forms![TreeViewRep_HesabAnbar]![KalaID],
@NameKala =forms![TreeViewRep_HesabAnbar]![FilterNameKala],
@GbyKalaID =forms![TreeViewRep_HesabAnbar]![ChGbyKalaID],
@Gokid  =forms![TreeViewRep_HesabAnbar]![GoKala],
@NameGokid =forms![TreeViewRep_HesabAnbar]![FilterNameGOKALA],
@GbyGokid =forms![TreeViewRep_HesabAnbar]![ChGbyGokid],
@Aid  =forms![TreeViewRep_HesabAnbar]![AID],
@NameAid =forms![TreeViewRep_HesabAnbar]![FilterNameAID],
@GbyAid =forms![TreeViewRep_HesabAnbar]![ChGbyAid],
@Goid =forms![TreeViewRep_HesabAnbar]![Goid],
@KolID =forms![TreeViewRep_HesabAnbar]![KolID],
@HesabID =forms![TreeViewRep_HesabAnbar]![HesabID],
@THesabID =forms![TreeViewRep_HesabAnbar]![THesabID],
@startDate =forms![TreeViewRep_HesabAnbar]![AsDate],
@ENDdate =forms![TreeViewRep_HesabAnbar]![TaDate]
*/

  CREATE PROCEDURE [dbo].[Rep_05228730] ( 
	@S_PHI int,/****** براساس آخرین خرید (1)  قیمت سطح 1 (2) قیمت سطح 2 (3) قیمت سطح 4 (5) قیمت سطح 5 (6) قیمت سطح 6 (6)قیمت *******/
	@SortRep int, 
	@KalaID int,
	@NameKala nvarchar(255), 
	@GbyKalaID AS BIT,
	@Gokid int,
	@NameGokid nvarchar(255), 
	@GbyGokid AS BIT,
	@Aid int,
	@NameAid  nvarchar(255),
	@GbyAid AS BIT,
	@Goid int, 
	@KolID int, 
	@HesabID int, 
	@THesabID int, 								
	@startDate nvarchar(50),								
	@ENDdate nvarchar(50)
)           
  AS     
  BEGIN 	SET NOCOUNT ON; 
  	 	
  DECLARE @SQLString  NVARCHAR(Max); 
  DECLARE @SQL_SELECT  NVARCHAR(Max); 
  DECLARE @SQL_FROM NVARCHAR(Max); 
  DECLARE @SQL_WHERE  NVARCHAR(Max); 
  DECLARE @SQL_GROUP  NVARCHAR(Max); 
  DECLARE @SQL_HAVING  NVARCHAR(Max); 
  DECLARE @SQL_ORDER NVARCHAR(Max); 
  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max); 
  
  set @SQL_SELECT=N' ';
  Set @SQL_FROM=N' ';
  Set @SQL_WHERE=N' ';
  Set @SQL_GROUP=N' ';
  Set @SQL_ORDER=N' ';	
  set @WhrStatement  = N' ';         	
  set @ParmAndstr = N' ';  
  /*SEt @SQL_HAVING =N' HAVING      (SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) <> 0) ';*/
  SEt @SQL_HAVING =N' HAVING      (1 = 1) ';
  SET @ParmDefinition =N'@S_PHI int=null,
						@GbyKalaID AS BIT=null,
						@GbyGokid AS BIT=null,
						@SortRep int=null, 
						@KalaID int=null,
						@NameKala nvarchar(255)=null, 
						@Gokid int=null,
						@NameGokid nvarchar(255)=null, 
						@Aid int=null,
						@NameAid  nvarchar(255)=null,
						@GbyAid bit=1,
						@Goid int=null, 
						@KolID int=null, 
						@HesabID int=null, 
						@THesabID int=null, 								
						@startDate nvarchar(50)=''0'', 								
						@ENDdate nvarchar(50)=''99999999''';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		

  BEGIN 
  
set @SQL_WHERE=' WHERE     (SubTransactionKala_1.TedadSadre <> 0) '
  


		/* شرط انطباق انبار*/
  
  if (@GbyAid=1) 
			Begin
				Set @SQL_SELECT=@SQL_SELECT + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar '
				Set @SQL_GROUP=@SQL_GROUP + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber , FunInfoKalaAll_1.NameAnbar ' 
				
				if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' ( FunInfoKalaAll_1.AID = @Aid )'; 
					end

				if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 	
					End
			end
  else
		Begin
			if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' WHERE ')
							set @ParmAndstr=N' ' 	
						else
							set @ParmAndstr=N' AND '

						set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'( FunInfoKalaAll_1.AID = @Aid )'; 
					End					

			if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' ')
							set @ParmAndstr=N' WHERE ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						
						Set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 
					End						
		End  			
		
 
   /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@KalaID IS NULL)) 
			Begin
				if(@SQL_HAVING=N' ')
					set @ParmAndstr=N' WHERE ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'   (FunInfoKalaAll_1.ID = @KalaID ) ' 
								
			End
   
      /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@startDate IS NULL) AND NOT (@ENDdate IS NULL)) 
			Begin
				if(@SQL_WHERE=N' ')
					set @ParmAndstr=N' WHERE ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'   (SubTransactionKala_1.Tarikh between @startDate and @ENDdate) ' 
								
			END
			
  
    /* --------------------شرط انطباق قسمتی از نام کالا*/ 			
  if(NOT (@NameKala IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameKala='%'+@NameKala+'%'
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameKala1 LIKE @NameKala) '; 	
		End

	/* --------------------شرط انطباق گروه*/ 			

	if(NOT (@Gokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '
				
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @Gokid )'; 	
		End
  
      /* --------------------شرط انطباق قسمتی از نام گروه کالا*/ 			

	if(NOT (@NameGokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameGokid='%'+@NameGokid+'%'
			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameGoKala1 LIKE @NameGokid) '; 
		End	

      /* --------------------شرط انطباق حساب تفصيلي*/ 			

	if(NOT (@THesabID IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (THesabID=@THesabID) '; 
		End	

      /* --------------------شرط انطباق حساب تفصيلي*/ 			

	if(NOT (@Goid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (GoID=@Goid) '; 
		End	
	
		if(NOT (@KolID IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (KolID=@KolID) '; 
		End	
		
	if(NOT (@HesabID IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (HesabID=@HesabID) '; 
		End	

		
  Set @SQLString =N'SELECT     SubTransactionKala_1.ShomareFactor,SubTransactionKala_1.NoeFa1,SubTransactionKala_1.Tarikh,FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) AS SumTedad, 
                      FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, MAX(Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS Max_UnitPriceKh, MAX(FunInfoKalaAll_1.UnitPrice1) AS MaxUnitPrice1, 
                      MAX(FunInfoKalaAll_1.UnitPrice2) AS MaxUnitPrice2, MAX(FunInfoKalaAll_1.UnitPrice3) AS MaxUnitPrice3, MAX(FunInfoKalaAll_1.UnitPrice4) AS MaxUnitPrice4, MAX(FunInfoKalaAll_1.UnitPrice5) 
                      AS MaxUnitPrice5, MAX(FunInfoKalaAll_1.UnitPrice6) AS MaxUnitPrice6, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode, SUM(CONVERT(float, 
                      SubTransactionKala_1.TedadVarde)) AS TedadVarde, SUM(CONVERT(Float, abs(SubTransactionKala_1.TedadSadre))) AS TedadSadre, SUM(SubTransactionKala_1.TedadKh) AS TedadKh, 
                      SUM(SubTransactionKala_1.TedadVA) AS TedadVA, SUM(SubTransactionKala_1.TedadFo) AS TedadFo, SUM(SubTransactionKala_1.TedadKhorooj) AS TedadKhorooj, 
                      SUM(SubTransactionKala_1.TedadBarKh) AS TedadBarKh, SUM(SubTransactionKala_1.TedadKhA) AS TedadKhA, SUM(SubTransactionKala_1.TedadVorood) AS TedadVorood, 
                      SUM(SubTransactionKala_1.TedadBarFo) AS TedadBarFo, SubTransactionKala_1.GOID, SubTransactionKala_1.KolID, SubTransactionKala_1.HesabID, SubTransactionKala_1.THesabID, 
                      SubTransactionKala_1.Namehesab, SUM(SubTransactionKala_1.MSadre) AS MSadre, @startDate AS startDate,@ENDdate AS  ENDdate, MIN(SubTransactionKala_1.Tarikh) AS FIRST
                      , MAX(SubTransactionKala_1.Tarikh) AS Last
' + @SQL_SELECT + ' 
FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid INNER JOIN
                      View_SubTransactionKala AS SubTransactionKala_1 ON FunInfoKalaAll_1.LinkKalaAid = SubTransactionKala_1.LinkKalaAid  ' +@SQL_WHERE + '
GROUP BY SubTransactionKala_1.ShomareFactor,SubTransactionKala_1.NoeFa1,SubTransactionKala_1.Tarikh,FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode, 
                      SubTransactionKala_1.Namehesab, SubTransactionKala_1.GOID, SubTransactionKala_1.KolID, SubTransactionKala_1.HesabID, SubTransactionKala_1.THesabID
'+ @SQL_HAVING 


                      


		    /* --------------------    1مرتب سازی */ 			
    if(@SortRep=1) 	
			set @SQLString= @SQLString  + N'  ORDER BY KalaNumber,NameKala1 '; 	
    		    /* --------------------    2مرتب سازی */ 			
    if(@SortRep=2) 	
			set @SQLString= @SQLString + N'  ORDER BY NameKala1,KalaNumber '; 
    		    /* --------------------    3مرتب سازی */ 			
    if(@SortRep=3) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKalanumbr,NameKala1 '; 
    		    /* --------------------    4مرتب سازی */ 			
    if(@SortRep=4) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKala1,NameKala1 '; 	
        		    /* --------------------    5مرتب سازی */ 			
	 IF ((@SortRep=5) AND (@GbyAid=1)) 	
			set @SQLString= @SQLString + N'  ORDER BY AIDNumber,NameKala1 '; 
    		    /* --------------------    6مرتب سازی */ 			
    IF ((@SortRep=6) AND (@GbyAid=1))	
			set @SQLString= @SQLString + N' ORDER BY NameAnbar,NameKala1 '; 		


    
    EXECUTE sp_executesql @SQLString, @ParmDefinition,@GbyKalaID=@GbyKalaID,@GbyGokid=@GbyGokid,@SortRep=@SortRep,@S_PHI=@S_PHI,@KalaID=@KalaID,@NameKala=@NameKala,@Gokid=@Gokid,@NameGokid=@NameGokid,@Aid=@Aid,@NameAid=@NameAid,@GbyAid=@GbyAid,@Goid=@Goid,@KolID =@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@startDate=@startDate,@ENDdate =@ENDdate;  		
    END 		 
    /* --------پایان */  		 		 
return END      
	
								
							  
GO



/****** Object:  StoredProcedure [dbo].[Rep_05228731]    Script Date: 12/11/2017 09:59:14 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_05228731]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_05228731]
GO



/****** Object:  StoredProcedure [dbo].[Rep_05228731]    Script Date: 12/11/2017 09:59:14 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*
@S_PHI=forms![TreeViewRep_HesabAnbar]![Sth_PHI].column(0),
@SortRep=forms![TreeViewRep_HesabAnbar]![Sorting],
@KalaID =forms![TreeViewRep_HesabAnbar]![KalaID],
@NameKala =forms![TreeViewRep_HesabAnbar]![FilterNameKala],
@GbyKalaID =forms![TreeViewRep_HesabAnbar]![ChGbyKalaID],
@Gokid  =forms![TreeViewRep_HesabAnbar]![GoKala],
@NameGokid =forms![TreeViewRep_HesabAnbar]![FilterNameGOKALA],
@GbyGokid =forms![TreeViewRep_HesabAnbar]![ChGbyGokid],
@Aid  =forms![TreeViewRep_HesabAnbar]![AID],
@NameAid =forms![TreeViewRep_HesabAnbar]![FilterNameAID],
@GbyAid =forms![TreeViewRep_HesabAnbar]![ChGbyAid],
@Goid =forms![TreeViewRep_HesabAnbar]![Goid],
@KolID =forms![TreeViewRep_HesabAnbar]![KolID],
@HesabID =forms![TreeViewRep_HesabAnbar]![HesabID],
@THesabID =forms![TreeViewRep_HesabAnbar]![THesabID],
@startDate =forms![TreeViewRep_HesabAnbar]![AsDate],
@ENDdate =forms![TreeViewRep_HesabAnbar]![TaDate]
*/

  CREATE PROCEDURE [dbo].[Rep_05228731] ( 
	@S_PHI int,/****** براساس آخرین خرید (1)  قیمت سطح 1 (2) قیمت سطح 2 (3) قیمت سطح 4 (5) قیمت سطح 5 (6) قیمت سطح 6 (6)قیمت *******/
	@SortRep int, 
	@KalaID int,
	@NameKala nvarchar(255), 
	@GbyKalaID AS BIT,
	@Gokid int,
	@NameGokid nvarchar(255), 
	@GbyGokid AS BIT,
	@Aid int,
	@NameAid  nvarchar(255),
	@GbyAid AS BIT,
	@Goid int, 
	@KolID int, 
	@HesabID int, 
	@THesabID int, 								
	@startDate nvarchar(50),								
	@ENDdate nvarchar(50)
)           
  AS     
  BEGIN 	SET NOCOUNT ON; 
  	 	
  DECLARE @SQLString  NVARCHAR(Max); 
  DECLARE @SQL_SELECT  NVARCHAR(Max); 
  DECLARE @SQL_FROM NVARCHAR(Max); 
  DECLARE @SQL_WHERE  NVARCHAR(Max); 
  DECLARE @SQL_GROUP  NVARCHAR(Max); 
  DECLARE @SQL_HAVING  NVARCHAR(Max); 
  DECLARE @SQL_ORDER NVARCHAR(Max); 
  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max); 
  
  set @SQL_SELECT=N' ';
  Set @SQL_FROM=N' ';
  Set @SQL_WHERE=N' ';
  Set @SQL_GROUP=N' ';
  Set @SQL_ORDER=N' ';	
  set @WhrStatement  = N' ';         	
  set @ParmAndstr = N' ';  
  /*SEt @SQL_HAVING =N' HAVING      (SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadVarde)) <> 0) ';*/
  SEt @SQL_HAVING =N' HAVING      (1 = 1) ';
  SET @ParmDefinition =N'@S_PHI int=null,
						@GbyKalaID AS BIT=null,
						@GbyGokid AS BIT=null,
						@SortRep int=null, 
						@KalaID int=null,
						@NameKala nvarchar(255)=null, 
						@Gokid int=null,
						@NameGokid nvarchar(255)=null, 
						@Aid int=null,
						@NameAid  nvarchar(255)=null,
						@GbyAid bit=1,
						@Goid int=null, 
						@KolID int=null, 
						@HesabID int=null, 
						@THesabID int=null, 								
						@startDate nvarchar(50)=''0'', 								
						@ENDdate nvarchar(50)=''99999999''';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		

  BEGIN 
  
set @SQL_WHERE=' WHERE     (SubTransactionKala_1.TedadVarde <> 0) '
  


		/* شرط انطباق انبار*/
  
  if (@GbyAid=1) 
			Begin
				Set @SQL_SELECT=@SQL_SELECT + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar '
				Set @SQL_GROUP=@SQL_GROUP + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber , FunInfoKalaAll_1.NameAnbar ' 
				
				if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' ( FunInfoKalaAll_1.AID = @Aid )'; 
					end

				if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 	
					End
			end
  else
		Begin
			if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' WHERE ')
							set @ParmAndstr=N' ' 	
						else
							set @ParmAndstr=N' AND '

						set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'( FunInfoKalaAll_1.AID = @Aid )'; 
					End					

			if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' ')
							set @ParmAndstr=N' WHERE ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						
						Set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 
					End						
		End  			
		
 
   /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@KalaID IS NULL)) 
			Begin
				if(@SQL_HAVING=N' ')
					set @ParmAndstr=N' WHERE ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'   (FunInfoKalaAll_1.ID = @KalaID ) ' 
								
			End
   
      /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@startDate IS NULL) AND NOT (@ENDdate IS NULL)) 
			Begin
				if(@SQL_WHERE=N' ')
					set @ParmAndstr=N' WHERE ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'   (SubTransactionKala_1.Tarikh between @startDate and @ENDdate) ' 
								
			END
			
  
    /* --------------------شرط انطباق قسمتی از نام کالا*/ 			
  if(NOT (@NameKala IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameKala='%'+@NameKala+'%'
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameKala1 LIKE @NameKala) '; 	
		End

	/* --------------------شرط انطباق گروه*/ 			

	if(NOT (@Gokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '
				
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @Gokid )'; 	
		End
  
      /* --------------------شرط انطباق قسمتی از نام گروه کالا*/ 			

	if(NOT (@NameGokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameGokid='%'+@NameGokid+'%'
			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameGoKala1 LIKE @NameGokid) '; 
		End	

      /* --------------------شرط انطباق حساب تفصيلي*/ 			

	if(NOT (@THesabID IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (THesabID=@THesabID) '; 
		End	

      /* --------------------شرط انطباق حساب تفصيلي*/ 			

	if(NOT (@Goid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (GoID=@Goid) '; 
		End	
	
		if(NOT (@KolID IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (KolID=@KolID) '; 
		End	
		
	if(NOT (@HesabID IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (HesabID=@HesabID) '; 
		End	

		/*SubTransactionKala_1.ShomareFactor,SubTransactionKala_1.NoeFa1,SubTransactionKala_1.Tarikh,*/
  Set @SQLString =N'SELECT     FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.Tedadsadre)) AS SumTedad, 
                      FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, MAX(Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS Max_UnitPriceKh, MAX(FunInfoKalaAll_1.UnitPrice1) AS MaxUnitPrice1, 
                      MAX(FunInfoKalaAll_1.UnitPrice2) AS MaxUnitPrice2, MAX(FunInfoKalaAll_1.UnitPrice3) AS MaxUnitPrice3, MAX(FunInfoKalaAll_1.UnitPrice4) AS MaxUnitPrice4, MAX(FunInfoKalaAll_1.UnitPrice5) 
                      AS MaxUnitPrice5, MAX(FunInfoKalaAll_1.UnitPrice6) AS MaxUnitPrice6, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode, SUM(CONVERT(float, 
                      SubTransactionKala_1.Tedadsadre)) AS TedadSadre, SUM(CONVERT(Float, abs(SubTransactionKala_1.TedadVarde))) AS TedadVarde, SUM(SubTransactionKala_1.TedadKh) AS TedadKh, 
                      SUM(SubTransactionKala_1.TedadVA) AS TedadVA, SUM(SubTransactionKala_1.TedadFo) AS TedadFo, SUM(SubTransactionKala_1.TedadKhorooj) AS TedadKhorooj, 
                      SUM(SubTransactionKala_1.TedadBarKh) AS TedadBarKh, SUM(SubTransactionKala_1.TedadKhA) AS TedadKhA, SUM(SubTransactionKala_1.TedadVorood) AS TedadVorood, 
                      SUM(SubTransactionKala_1.TedadBarFo) AS TedadBarFo, SubTransactionKala_1.GOID, SubTransactionKala_1.KolID, SubTransactionKala_1.HesabID, SubTransactionKala_1.THesabID, 
                      SubTransactionKala_1.Namehesab, SUM(CONVERT(Float,SubTransactionKala_1.MVarde)) AS MVarde, @startDate AS startDate,@ENDdate AS  ENDdate, MIN(SubTransactionKala_1.Tarikh) AS FIRST
                      , MAX(SubTransactionKala_1.Tarikh) AS Last
' + @SQL_SELECT + ' 
FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid INNER JOIN
                      View_SubTransactionKala AS SubTransactionKala_1 ON FunInfoKalaAll_1.LinkKalaAid = SubTransactionKala_1.LinkKalaAid ' +@SQL_WHERE + '
GROUP BY FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode, 
                      SubTransactionKala_1.Namehesab, SubTransactionKala_1.GOID, SubTransactionKala_1.KolID, SubTransactionKala_1.HesabID, SubTransactionKala_1.THesabID
'+ @SQL_HAVING 


                      


		    /* --------------------    1مرتب سازی */ 			
    if(@SortRep=1) 	
			set @SQLString= @SQLString  + N'  ORDER BY KalaNumber,NameKala1 '; 	
    		    /* --------------------    2مرتب سازی */ 			
    if(@SortRep=2) 	
			set @SQLString= @SQLString + N'  ORDER BY NameKala1,KalaNumber '; 
    		    /* --------------------    3مرتب سازی */ 			
    if(@SortRep=3) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKalanumbr,NameKala1 '; 
    		    /* --------------------    4مرتب سازی */ 			
    if(@SortRep=4) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKala1,NameKala1 '; 	
        		    /* --------------------    5مرتب سازی */ 			
	 IF ((@SortRep=5) AND (@GbyAid=1)) 	
			set @SQLString= @SQLString + N'  ORDER BY AIDNumber,NameKala1 '; 
    		    /* --------------------    6مرتب سازی */ 			
    IF ((@SortRep=6) AND (@GbyAid=1))	
			set @SQLString= @SQLString + N' ORDER BY NameAnbar,NameKala1 '; 		


    
    EXECUTE sp_executesql @SQLString, @ParmDefinition,@GbyKalaID=@GbyKalaID,@GbyGokid=@GbyGokid,@SortRep=@SortRep,@S_PHI=@S_PHI,@KalaID=@KalaID,@NameKala=@NameKala,@Gokid=@Gokid,@NameGokid=@NameGokid,@Aid=@Aid,@NameAid=@NameAid,@GbyAid=@GbyAid,@Goid=@Goid,@KolID =@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@startDate=@startDate,@ENDdate =@ENDdate;  		
    END 		 
    /* --------پایان */  		 		 
return END      
	
								
							  
GO



/****** Object:  StoredProcedure [dbo].[Rep_05228732]    Script Date: 12/11/2017 09:59:18 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_05228732]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_05228732]
GO



/****** Object:  StoredProcedure [dbo].[Rep_05228732]    Script Date: 12/11/2017 09:59:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*
@S_PHI=forms![TreeViewRep_HesabAnbar]![Sth_PHI].column(0),
@SortRep=forms![TreeViewRep_HesabAnbar]![Sorting],
@KalaID =forms![TreeViewRep_HesabAnbar]![KalaID],
@NameKala =forms![TreeViewRep_HesabAnbar]![FilterNameKala],
@GbyKalaID =forms![TreeViewRep_HesabAnbar]![ChGbyKalaID],
@Gokid  =forms![TreeViewRep_HesabAnbar]![GoKala],
@NameGokid =forms![TreeViewRep_HesabAnbar]![FilterNameGOKALA],
@GbyGokid =forms![TreeViewRep_HesabAnbar]![ChGbyGokid],
@Aid  =forms![TreeViewRep_HesabAnbar]![AID],
@NameAid =forms![TreeViewRep_HesabAnbar]![FilterNameAID],
@GbyAid =forms![TreeViewRep_HesabAnbar]![ChGbyAid],
@Goid =forms![TreeViewRep_HesabAnbar]![Goid],
@KolID =forms![TreeViewRep_HesabAnbar]![KolID],
@HesabID =forms![TreeViewRep_HesabAnbar]![HesabID],
@THesabID =forms![TreeViewRep_HesabAnbar]![THesabID],
@startDate =forms![TreeViewRep_HesabAnbar]![AsDate],
@ENDdate =forms![TreeViewRep_HesabAnbar]![TaDate]
*/

  CREATE PROCEDURE [dbo].[Rep_05228732] ( 
	@S_PHI int,/****** براساس آخرين خريد (1)  قيمت سطح 1 (2) قيمت سطح 2 (3) قيمت سطح 4 (5) قيمت سطح 5 (6) قيمت سطح 6 (6)قيمت *******/
	@SortRep int, 
	@KalaID int,
	@NameKala nvarchar(255), 
	@GbyKalaID AS BIT,
	@Gokid int,
	@NameGokid nvarchar(255), 
	@GbyGokid AS BIT,
	@Aid int,
	@NameAid  nvarchar(255),
	@GbyAid AS BIT,
	@Goid int, 
	@KolID int, 
	@HesabID int, 
	@THesabID int, 								
	@startDate nvarchar(50),								
	@ENDdate nvarchar(50)
)           
  AS     
  BEGIN 	SET NOCOUNT ON; 
  	 	
  DECLARE @SQLString  NVARCHAR(Max); 
  DECLARE @SQL_SELECT  NVARCHAR(Max); 
  DECLARE @SQL_FROM NVARCHAR(Max); 
  DECLARE @SQL_WHERE  NVARCHAR(Max); 
  DECLARE @SQL_GROUP  NVARCHAR(Max); 
  DECLARE @SQL_HAVING  NVARCHAR(Max); 
  DECLARE @SQL_ORDER NVARCHAR(Max); 
  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max); 
  
  set @SQL_SELECT=N' ';
  Set @SQL_FROM=N' ';
  Set @SQL_WHERE=N' ';
  Set @SQL_GROUP=N' ';
  Set @SQL_ORDER=N' ';	
  set @WhrStatement  = N' ';         	
  set @ParmAndstr = N' ';  
  /*SEt @SQL_HAVING =N' HAVING      (SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadVarde)) <> 0) ';*/
  SEt @SQL_HAVING =N' HAVING      (1 = 1) ';
  SET @ParmDefinition =N'@S_PHI int=null,
						@GbyKalaID AS BIT=null,
						@GbyGokid AS BIT=null,
						@SortRep int=null, 
						@KalaID int=null,
						@NameKala nvarchar(255)=null, 
						@Gokid int=null,
						@NameGokid nvarchar(255)=null, 
						@Aid int=null,
						@NameAid  nvarchar(255)=null,
						@GbyAid bit=1,
						@Goid int=null, 
						@KolID int=null, 
						@HesabID int=null, 
						@THesabID int=null, 								
						@startDate nvarchar(50)=''0'', 								
						@ENDdate nvarchar(50)=''99999999''';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		

  BEGIN 
  
set @SQL_WHERE=' WHERE     (SubTransactionKala_1.TedadVarde <> 0) '
  


		/* شرط انطباق انبار*/
  
  if (@GbyAid=1) 
			Begin
				Set @SQL_SELECT=@SQL_SELECT + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar '
				Set @SQL_GROUP=@SQL_GROUP + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber , FunInfoKalaAll_1.NameAnbar ' 
				
				if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' ( FunInfoKalaAll_1.AID = @Aid )'; 
					end

				if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 	
					End
			end
  else
		Begin
			if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' WHERE ')
							set @ParmAndstr=N' ' 	
						else
							set @ParmAndstr=N' AND '

						set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'( FunInfoKalaAll_1.AID = @Aid )'; 
					End					

			if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' ')
							set @ParmAndstr=N' WHERE ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						
						Set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 
					End						
		End  			
		
 
   /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@KalaID IS NULL)) 
			Begin
				if(@SQL_HAVING=N' ')
					set @ParmAndstr=N' WHERE ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'   (FunInfoKalaAll_1.ID = @KalaID ) ' 
								
			End
   
      /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@startDate IS NULL) AND NOT (@ENDdate IS NULL)) 
			Begin
				if(@SQL_WHERE=N' ')
					set @ParmAndstr=N' WHERE ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'   (SubTransactionKala_1.Tarikh between @startDate and @ENDdate) ' 
								
			END
			
  
    /* --------------------شرط انطباق قسمتي از نام کالا*/ 			
  if(NOT (@NameKala IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameKala='%'+@NameKala+'%'
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameKala1 LIKE @NameKala) '; 	
		End

	/* --------------------شرط انطباق گروه*/ 			

	if(NOT (@Gokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '
				
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @Gokid )'; 	
		End
  
      /* --------------------شرط انطباق قسمتي از نام گروه کالا*/ 			

	if(NOT (@NameGokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameGokid='%'+@NameGokid+'%'
			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameGoKala1 LIKE @NameGokid) '; 
		End	

      /* --------------------شرط انطباق حساب تفصيلي*/ 			

	if(NOT (@THesabID IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (THesabID=@THesabID) '; 
		End	

      /* --------------------شرط انطباق حساب تفصيلي*/ 			

	if(NOT (@Goid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (GoID=@Goid) '; 
		End	
	
		if(NOT (@KolID IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (KolID=@KolID) '; 
		End	
		
	if(NOT (@HesabID IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' WHERE ' 	
			else
				set @ParmAndstr=N' AND '

			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (HesabID=@HesabID) '; 
		End	

		/*SubTransactionKala_1.ShomareFactor,SubTransactionKala_1.NoeFa1,SubTransactionKala_1.Tarikh,*/
  Set @SQLString =N'SELECT     SubTransactionKala_1.ShomareFactor,SubTransactionKala_1.NoeFa1,SubTransactionKala_1.Tarikh,FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.Tedadsadre)) AS SumTedad, 
                      FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, MAX(Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS Max_UnitPriceKh, MAX(FunInfoKalaAll_1.UnitPrice1) AS MaxUnitPrice1, 
                      MAX(FunInfoKalaAll_1.UnitPrice2) AS MaxUnitPrice2, MAX(FunInfoKalaAll_1.UnitPrice3) AS MaxUnitPrice3, MAX(FunInfoKalaAll_1.UnitPrice4) AS MaxUnitPrice4, MAX(FunInfoKalaAll_1.UnitPrice5) 
                      AS MaxUnitPrice5, MAX(FunInfoKalaAll_1.UnitPrice6) AS MaxUnitPrice6, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode, SUM(CONVERT(float, 
                      SubTransactionKala_1.Tedadsadre)) AS TedadSadre, SUM(CONVERT(Float, abs(SubTransactionKala_1.TedadVarde))) AS TedadVarde, SUM(SubTransactionKala_1.TedadKh) AS TedadKh, 
                      SUM(SubTransactionKala_1.TedadVA) AS TedadVA, SUM(SubTransactionKala_1.TedadFo) AS TedadFo, SUM(SubTransactionKala_1.TedadKhorooj) AS TedadKhorooj, 
                      SUM(SubTransactionKala_1.TedadBarKh) AS TedadBarKh, SUM(SubTransactionKala_1.TedadKhA) AS TedadKhA, SUM(SubTransactionKala_1.TedadVorood) AS TedadVorood, 
                      SUM(SubTransactionKala_1.TedadBarFo) AS TedadBarFo, SubTransactionKala_1.GOID, SubTransactionKala_1.KolID, SubTransactionKala_1.HesabID, SubTransactionKala_1.THesabID, 
                      SubTransactionKala_1.Namehesab, SUM(CONVERT(Float,SubTransactionKala_1.MVarde)) AS MVarde, @startDate AS startDate,@ENDdate AS  ENDdate, MIN(SubTransactionKala_1.Tarikh) AS FIRST
                      , MAX(SubTransactionKala_1.Tarikh) AS Last
' + @SQL_SELECT + ' 
FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid INNER JOIN
                      View_SubTransactionKala AS SubTransactionKala_1 ON FunInfoKalaAll_1.LinkKalaAid = SubTransactionKala_1.LinkKalaAid 
' +@SQL_WHERE + '
GROUP BY SubTransactionKala_1.ShomareFactor,SubTransactionKala_1.NoeFa1,SubTransactionKala_1.Tarikh,FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode, 
                      SubTransactionKala_1.Namehesab, SubTransactionKala_1.GOID, SubTransactionKala_1.KolID, SubTransactionKala_1.HesabID, SubTransactionKala_1.THesabID
'+ @SQL_HAVING 


                      


		    /* --------------------    1مرتب سازي */ 			
    if(@SortRep=1) 	
			set @SQLString= @SQLString  + N'  ORDER BY KalaNumber,NameKala1 '; 	
    		    /* --------------------    2مرتب سازي */ 			
    if(@SortRep=2) 	
			set @SQLString= @SQLString + N'  ORDER BY NameKala1,KalaNumber '; 
    		    /* --------------------    3مرتب سازي */ 			
    if(@SortRep=3) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKalanumbr,NameKala1 '; 
    		    /* --------------------    4مرتب سازي */ 			
    if(@SortRep=4) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKala1,NameKala1 '; 	
        		    /* --------------------    5مرتب سازي */ 			
	 IF ((@SortRep=5) AND (@GbyAid=1)) 	
			set @SQLString= @SQLString + N'  ORDER BY AIDNumber,NameKala1 '; 
    		    /* --------------------    6مرتب سازي */ 			
    IF ((@SortRep=6) AND (@GbyAid=1))	
			set @SQLString= @SQLString + N' ORDER BY NameAnbar,NameKala1 '; 		


    
    EXECUTE sp_executesql @SQLString, @ParmDefinition,@GbyKalaID=@GbyKalaID,@GbyGokid=@GbyGokid,@SortRep=@SortRep,@S_PHI=@S_PHI,@KalaID=@KalaID,@NameKala=@NameKala,@Gokid=@Gokid,@NameGokid=@NameGokid,@Aid=@Aid,@NameAid=@NameAid,@GbyAid=@GbyAid,@Goid=@Goid,@KolID =@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@startDate=@startDate,@ENDdate =@ENDdate;  		
    END 		 
    /* --------پايان */  		 		 
return END      
	
								
							  
GO



/****** Object:  StoredProcedure [dbo].[Rep_05228516]    Script Date: 12/11/2017 10:19:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_05228516]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_05228516]
GO



/****** Object:  StoredProcedure [dbo].[Rep_05228516]    Script Date: 12/11/2017 10:19:32 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[Rep_05228516] ( 
	@SortRep int, 
	@KalaID int,
	@NameKala nvarchar(255),
	@GbyKalaID AS BIT,
	@Gokid int,
	@NameGokid nvarchar(255), 
	@GbyGokid AS BIT,
	@Aid int,
	@NameAid  nvarchar(255),
	@GbyAid AS BIT)           
  AS     
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(Max);  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max);         	 	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';                  	
  SET @ParmDefinition = N'	@SortRep int=null, 
							@KalaID int=null,
							@NameKala nvarchar(255)=null, 
							@Gokid int=null,
							@NameGokid nvarchar(255)=null, 
							@Aid int=null,
							@NameAid  nvarchar(255)=null,
							@GbyKalaID AS BIT=null,
							@GbyGokid AS BIT=null,
							@GbyAid AS BIT=null';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		

  BEGIN 
  
	 
  SET @SQLString = N' SELECT     ID, KalaNumber, NameKala1, GOKID, NameGoKalanumbr, NameGoKala1, AID, AIDNumber, NameAnbar, VAHED1, VAHED2, BarCode, UnitPrice1, NameVahed1, NameVahed2, UnitPrice2, UnitPrice3, 
                      UnitPrice4, UnitPrice5, UnitPrice6, Active, MinPrice, MaxPrice, Zarib, MinMogoody, MaxMogoody, Note
FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 where 1=1 ' 	
  
		
   /* --------------------شرط انطباق کالا*/ 			
	set @ParmAndstr=N' AND ' 	
  
  if(NOT (@KalaID IS NULL)) 
  set @WhrStatement= @WhrStatement + @ParmAndstr + N' (ID = @KalaID ) ' 				
     
    /* --------------------شرط انطباق قسمتی از نام کالا*/ 			
  	
  if(NOT (@NameKala IS NULL)) 
  Begin
  Set @NameKala='%'+@NameKala+'%'
  set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameKala1 LIKE @NameKala) '; 	
  End

   /* --------------------شرط انطباق گروه*/ 			
  if(NOT (@Gokid IS NULL)) set @WhrStatement= @WhrStatement + @ParmAndstr + N'(GOKID = @Gokid )'; 	
  
      /* --------------------شرط انطباق قسمتی از نام گروه کالا*/ 			
  	
  if(NOT (@NameGokid IS NULL)) 
  Begin
  Set @NameGokid='%'+@NameGokid+'%'
  Set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameGoKala1 LIKE @NameGokid) '; 
  End	

   /* --------------------شرط انطباق انبار*/ 			
	
  	
  if(NOT (@Aid IS NULL)) 
  
  set @WhrStatement= @WhrStatement + @ParmAndstr + N'(AID = @Aid )'; 	
  
      /* --------------------شرط انطباق قسمتی از نام انبار کالا*/ 			
  	
  if(NOT (@NameAid IS NULL)) 
  Begin
  Set @NameAid='%'+@NameAid+'%'
  Set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameAnbar LIKE @NameAid) '; 	
  End

  
	set @SQLString= @SQLString + @WhrStatement
		    /* --------------------    1مرتب سازی */ 			
    if(@SortRep=1) 	set @SQLString= @SQLString  + N'  ORDER BY KalaNumber '; 	
    		    /* --------------------    2مرتب سازی */ 			
    if(@SortRep=2) 	set @SQLString= @SQLString + N'  ORDER BY NameKala1 '; 
    		    /* --------------------    3مرتب سازی */ 			
    if(@SortRep=3) 	set @SQLString= @SQLString + N'  ORDER BY NameGoKalanumbr '; 
    		    /* --------------------    4مرتب سازی */ 			
    if(@SortRep=4) 	set @SQLString= @SQLString + N'  ORDER BY NameGoKala1 '; 	
        		    /* --------------------    5مرتب سازی */ 			
    if(@SortRep=5) 	set @SQLString= @SQLString + N'  ORDER BY KalaNumber '; 
    		    /* --------------------    6مرتب سازی */ 			
    if(@SortRep=6) 	set @SQLString= @SQLString + N' ORDER BY NameAnbar '; 		

    EXECUTE sp_executesql   @SQLString, @ParmDefinition,@GbyAid=@GbyAid,@GbyGokid=@GbyGokid,@GbyKalaID=@GbyKalaID,@SortRep=@SortRep,@KalaID=@KalaID,@NameKala=@NameKala,@Gokid=@Gokid,@NameGokid=@NameGokid,@Aid=@Aid,@NameAid=@NameAid;  		
    END 		 
    /* --------پایان */  		 		 
return END      

GO



/****** Object:  StoredProcedure [dbo].[Rep_05228517]    Script Date: 12/11/2017 10:19:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_05228517]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_05228517]
GO


/****** Object:  StoredProcedure [dbo].[Rep_05228517]    Script Date: 12/11/2017 10:19:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[Rep_05228517] ( 
	@SortRep int, 
	@KalaID int,
	@NameKala nvarchar(255),
	@GbyKalaID AS BIT,
	@Gokid int,
	@NameGokid nvarchar(255), 
	@GbyGokid AS BIT,
	@Aid int,
	@NameAid  nvarchar(255),
	@GbyAid AS BIT)           
  AS     
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(Max);  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max);         	 	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';                  	
  SET @ParmDefinition = N'	@SortRep int=null, 
							@KalaID int=null,
							@NameKala nvarchar(255)=null, 
							@Gokid int=null,
							@NameGokid nvarchar(255)=null, 
							@Aid int=null,
							@NameAid  nvarchar(255)=null,
							@GbyKalaID AS BIT=null,
							@GbyGokid AS BIT=null,
							@GbyAid AS BIT=null';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		

  BEGIN 
  
	 
  SET @SQLString = N' SELECT     ID, KalaNumber, NameKala1, GOKID, NameGoKalanumbr, NameGoKala1, AID, AIDNumber, NameAnbar, VAHED1, VAHED2, BarCode, UnitPrice1, NameVahed1, NameVahed2, UnitPrice2, UnitPrice3, 
                      UnitPrice4, UnitPrice5, UnitPrice6, Active, MinPrice, MaxPrice, Zarib, MinMogoody, MaxMogoody, Note
FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 where 1=1 ' 	
  
		
   /* --------------------شرط انطباق کالا*/ 			
	set @ParmAndstr=N' AND ' 	
  
  if(NOT (@KalaID IS NULL)) 
  set @WhrStatement= @WhrStatement + @ParmAndstr + N' (ID = @KalaID ) ' 				
     
    /* --------------------شرط انطباق قسمتی از نام کالا*/ 			
  	
  if(NOT (@NameKala IS NULL)) 
  Begin
  Set @NameKala='%'+@NameKala+'%'
  set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameKala1 LIKE @NameKala) '; 	
  End

   /* --------------------شرط انطباق گروه*/ 			
  if(NOT (@Gokid IS NULL)) set @WhrStatement= @WhrStatement + @ParmAndstr + N'(GOKID = @Gokid )'; 	
  
      /* --------------------شرط انطباق قسمتی از نام گروه کالا*/ 			
  	
  if(NOT (@NameGokid IS NULL)) 
  Begin
  Set @NameGokid='%'+@NameGokid+'%'
  Set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameGoKala1 LIKE @NameGokid) '; 
  End	

   /* --------------------شرط انطباق انبار*/ 			
	
  	
  if(NOT (@Aid IS NULL)) 
  
  set @WhrStatement= @WhrStatement + @ParmAndstr + N'(AID = @Aid )'; 	
  
      /* --------------------شرط انطباق قسمتی از نام انبار کالا*/ 			
  	
  if(NOT (@NameAid IS NULL)) 
  Begin
  Set @NameAid='%'+@NameAid+'%'
  Set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameAnbar LIKE @NameAid) '; 	
  End

  
	set @SQLString= @SQLString + @WhrStatement
		    /* --------------------    1مرتب سازی */ 			
    if(@SortRep=1) 	set @SQLString= @SQLString  + N'  ORDER BY KalaNumber '; 	
    		    /* --------------------    2مرتب سازی */ 			
    if(@SortRep=2) 	set @SQLString= @SQLString + N'  ORDER BY NameKala1 '; 
    		    /* --------------------    3مرتب سازی */ 			
    if(@SortRep=3) 	set @SQLString= @SQLString + N'  ORDER BY NameGoKalanumbr '; 
    		    /* --------------------    4مرتب سازی */ 			
    if(@SortRep=4) 	set @SQLString= @SQLString + N'  ORDER BY NameGoKala1 '; 	
        		    /* --------------------    5مرتب سازی */ 			
    if(@SortRep=5) 	set @SQLString= @SQLString + N'  ORDER BY KalaNumber '; 
    		    /* --------------------    6مرتب سازی */ 			
    if(@SortRep=6) 	set @SQLString= @SQLString + N' ORDER BY NameAnbar '; 		

    EXECUTE sp_executesql   @SQLString, @ParmDefinition,@GbyAid=@GbyAid,@GbyGokid=@GbyGokid,@GbyKalaID=@GbyKalaID,@SortRep=@SortRep,@KalaID=@KalaID,@NameKala=@NameKala,@Gokid=@Gokid,@NameGokid=@NameGokid,@Aid=@Aid,@NameAid=@NameAid;  		
    END 		 
    /* --------پایان */  		 		 
return END      

GO



/****** Object:  StoredProcedure [dbo].[Rep_05228519]    Script Date: 12/11/2017 10:19:40 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_05228519]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_05228519]
GO

/****** Object:  StoredProcedure [dbo].[Rep_05228519]    Script Date: 12/11/2017 10:19:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[Rep_05228519] ( 
	@SortRep int, 
	@KalaID int,
	@NameKala nvarchar(255),
	@GbyKalaID AS BIT,
	@Gokid int,
	@NameGokid nvarchar(255), 
	@GbyGokid AS BIT,
	@Aid int,
	@NameAid  nvarchar(255),
	@GbyAid AS BIT)           
  AS     
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(Max);  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max);         	 	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';                  	
  SET @ParmDefinition = N'	@SortRep int=null, 
							@KalaID int=null,
							@NameKala nvarchar(255)=null, 
							@Gokid int=null,
							@NameGokid nvarchar(255)=null, 
							@Aid int=null,
							@NameAid  nvarchar(255)=null,
							@GbyKalaID AS BIT=null,
							@GbyGokid AS BIT=null,
							@GbyAid AS BIT=null';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		

  BEGIN 
  
	 
  SET @SQLString = N' SELECT     ID, KalaNumber, NameKala1, GOKID, NameGoKalanumbr, NameGoKala1, AID, AIDNumber, NameAnbar, VAHED1, VAHED2, BarCode, UnitPrice1, NameVahed1, NameVahed2, UnitPrice2, UnitPrice3, 
                      UnitPrice4, UnitPrice5, UnitPrice6, Active, MinPrice, MaxPrice, Zarib, MinMogoody, MaxMogoody, Note
FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 where 1=1 ' 	
  
		
   /* --------------------شرط انطباق کالا*/ 			
	set @ParmAndstr=N' AND ' 	
  
  if(NOT (@KalaID IS NULL)) 
  set @WhrStatement= @WhrStatement + @ParmAndstr + N' (ID = @KalaID ) ' 				
     
    /* --------------------شرط انطباق قسمتی از نام کالا*/ 			
  	
  if(NOT (@NameKala IS NULL)) 
  Begin
  Set @NameKala='%'+@NameKala+'%'
  set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameKala1 LIKE @NameKala) '; 	
  End

   /* --------------------شرط انطباق گروه*/ 			
  if(NOT (@Gokid IS NULL)) set @WhrStatement= @WhrStatement + @ParmAndstr + N'(GOKID = @Gokid )'; 	
  
      /* --------------------شرط انطباق قسمتی از نام گروه کالا*/ 			
  	
  if(NOT (@NameGokid IS NULL)) 
  Begin
  Set @NameGokid='%'+@NameGokid+'%'
  Set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameGoKala1 LIKE @NameGokid) '; 
  End	

   /* --------------------شرط انطباق انبار*/ 			
	
  	
  if(NOT (@Aid IS NULL)) 
  
  set @WhrStatement= @WhrStatement + @ParmAndstr + N'(AID = @Aid )'; 	
  
      /* --------------------شرط انطباق قسمتی از نام انبار کالا*/ 			
  	
  if(NOT (@NameAid IS NULL)) 
  Begin
  Set @NameAid='%'+@NameAid+'%'
  Set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameAnbar LIKE @NameAid) '; 	
  End

  
	set @SQLString= @SQLString + @WhrStatement
		    /* --------------------    1مرتب سازی */ 			
    if(@SortRep=1) 	set @SQLString= @SQLString  + N'  ORDER BY KalaNumber '; 	
    		    /* --------------------    2مرتب سازی */ 			
    if(@SortRep=2) 	set @SQLString= @SQLString + N'  ORDER BY NameKala1 '; 
    		    /* --------------------    3مرتب سازی */ 			
    if(@SortRep=3) 	set @SQLString= @SQLString + N'  ORDER BY NameGoKalanumbr '; 
    		    /* --------------------    4مرتب سازی */ 			
    if(@SortRep=4) 	set @SQLString= @SQLString + N'  ORDER BY NameGoKala1 '; 	
        		    /* --------------------    5مرتب سازی */ 			
    if(@SortRep=5) 	set @SQLString= @SQLString + N'  ORDER BY KalaNumber '; 
    		    /* --------------------    6مرتب سازی */ 			
    if(@SortRep=6) 	set @SQLString= @SQLString + N' ORDER BY NameAnbar '; 		

    EXECUTE sp_executesql   @SQLString, @ParmDefinition,@GbyAid=@GbyAid,@GbyGokid=@GbyGokid,@GbyKalaID=@GbyKalaID,@SortRep=@SortRep,@KalaID=@KalaID,@NameKala=@NameKala,@Gokid=@Gokid,@NameGokid=@NameGokid,@Aid=@Aid,@NameAid=@NameAid;  		
    END 		 
    /* --------پایان */  		 		 
return END      

GO



/****** Object:  StoredProcedure [dbo].[Rep_05228520]    Script Date: 12/11/2017 10:19:44 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_05228520]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_05228520]
GO


/****** Object:  StoredProcedure [dbo].[Rep_05228520]    Script Date: 12/11/2017 10:19:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*
@S_PHI=forms![TreeViewRep_HesabAnbar]![Sth_PHI].column(2),
@SortRep=forms![TreeViewRep_HesabAnbar]![Sorting],
@KalaID =forms![TreeViewRep_HesabAnbar]![KalaID],
@NameKala =forms![TreeViewRep_HesabAnbar]![FilterNameKala],
@GbyKalaID =forms![TreeViewRep_HesabAnbar]![ChGbyKalaID],
@Gokid  =forms![TreeViewRep_HesabAnbar]![GoKala],
@NameGokid =forms![TreeViewRep_HesabAnbar]![FilterNameGOKALA],
@GbyGokid =forms![TreeViewRep_HesabAnbar]![ChGbyGokid],
@Aid  =forms![TreeViewRep_HesabAnbar]![AID],
@NameAid =forms![TreeViewRep_HesabAnbar]![FilterNameAID],
@GbyAid =forms![TreeViewRep_HesabAnbar]![ChGbyAid]
*/

  CREATE PROCEDURE [dbo].[Rep_05228520] ( 
	@S_PHI int,/****** براساس آخرین خرید (1)  قیمت سطح 1 (2) قیمت سطح 2 (3) قیمت سطح 4 (5) قیمت سطح 5 (6) قیمت سطح 6 (6)قیمت *******/
	@SortRep int, 
	@KalaID int,
	@NameKala nvarchar(255), 
	@GbyKalaID AS BIT,
	@Gokid int,
	@NameGokid nvarchar(255), 
	@GbyGokid AS BIT,
	@Aid int,
	@NameAid  nvarchar(255),
	@GbyAid AS BIT)           
  AS     
  BEGIN 	SET NOCOUNT ON; 
  	 	
  DECLARE @SQLString  NVARCHAR(Max); 
  DECLARE @SQL_SELECT  NVARCHAR(Max); 
  DECLARE @SQL_FROM NVARCHAR(Max); 
  DECLARE @SQL_WHERE  NVARCHAR(Max); 
  DECLARE @SQL_GROUP  NVARCHAR(Max); 
  DECLARE @SQL_HAVING  NVARCHAR(Max); 
  DECLARE @SQL_ORDER NVARCHAR(Max); 
  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max); 
  
  set @SQL_SELECT=N' ';
  Set @SQL_FROM=N' ';
  Set @SQL_WHERE=N' ';
  Set @SQL_GROUP=N' ';
  Set @SQL_ORDER=N' ';	
  set @WhrStatement  = N' ';         	
  set @ParmAndstr = N' ';  
  SEt @SQL_HAVING =N' HAVING      (SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) <> 0) ';
  
  SET @ParmDefinition =N'@S_PHI int=null,
						@GbyKalaID AS BIT=null,
						@GbyGokid AS BIT=null,
						@SortRep int=null, 
						@KalaID int=null,
						@NameKala nvarchar(255)=null, 
						@Gokid int=null,
						@NameGokid nvarchar(255)=null, 
						@Aid int=null,
						@NameAid  nvarchar(255)=null,
						@GbyAid bit=1';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		

  BEGIN 
  

  
   if (@S_PHI IS NULL)
		Set @S_PHI=1
		
  if (@S_PHI=1)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(Fun_SubAkharinKharid_1.MaxUnitPriceKh, 0)) AS SumMabUnit, MAX(ISNULL(Fun_SubAkharinKharid_1.MaxUnitPriceKh, 0)) as MabUnit  '
  if (@S_PHI=2)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice1, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice1, 0)) as MabUnit '
  if (@S_PHI=3)
	Set @SQL_SELECT=N' , Sum(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * Max(ISNULL(FunInfoKalaAll_1.UnitPrice2, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice2, 0)) as MabUnit  '	
  if (@S_PHI=4)
	Set @SQL_SELECT=N' , Sum(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice3, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice3, 0)) as MabUnit '
  if (@S_PHI=5)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice4, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice4, 0)) as MabUnit  '	
  if (@S_PHI=6)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * Max(ISNULL(FunInfoKalaAll_1.UnitPrice5, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice5, 0) as MabUnit '
  if (@S_PHI=7)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * max(ISNULL(FunInfoKalaAll_1.UnitPrice6, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice6, 0)) as MabUnit '	
	

		/* شرط انطباق انبار*/
  
  if (@GbyAid=1) 
			Begin
				Set @SQL_SELECT=@SQL_SELECT + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar '
				Set @SQL_GROUP=@SQL_GROUP + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber , FunInfoKalaAll_1.NameAnbar ' 
				
				if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' ( FunInfoKalaAll_1.AID = @Aid )'; 
					end

				if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 	
					End
			end
  else
		Begin
			if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' WHERE ')
							set @ParmAndstr=N' ' 	
						else
							set @ParmAndstr=N' AND '

						set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'( FunInfoKalaAll_1.AID = @Aid )'; 
					End					

			if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' ')
							set @ParmAndstr=N' WHERE ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						
						Set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 
					End						
		End  			
		
 
   /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@KalaID IS NULL)) 
			Begin
				if(@SQL_HAVING=N' ')
					set @ParmAndstr=N' HAVING ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'   (FunInfoKalaAll_1.ID = @KalaID ) ' 
								
			End
     
    /* --------------------شرط انطباق قسمتی از نام کالا*/ 			
  if(NOT (@NameKala IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameKala='%'+@NameKala+'%'
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameKala1 LIKE @NameKala) '; 	
		End

	/* --------------------شرط انطباق گروه*/ 			

	if(NOT (@Gokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @Gokid )'; 	
		End
  
      /* --------------------شرط انطباق قسمتی از نام گروه کالا*/ 			

	if(NOT (@NameGokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameGokid='%'+@NameGokid+'%'
			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameGoKala1 LIKE @NameGokid) '; 
		End	
 

  Set @SQLString =N'SELECT     FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) AS SumTedad, 
                      FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, MAX(Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS Max_UnitPriceKh, MAX(FunInfoKalaAll_1.UnitPrice1) AS MaxUnitPrice1, 
                      MAX(FunInfoKalaAll_1.UnitPrice2) AS MaxUnitPrice2, MAX(FunInfoKalaAll_1.UnitPrice3) AS MaxUnitPrice3, MAX(FunInfoKalaAll_1.UnitPrice4) AS MaxUnitPrice4, MAX(FunInfoKalaAll_1.UnitPrice5) 
                      AS MaxUnitPrice5, MAX(FunInfoKalaAll_1.UnitPrice6) AS MaxUnitPrice6, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode
'+ @SQL_SELECT + 'FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid  LEFT OUTER JOIN
                      (SELECT     KALAID, GOKID, AID, SUM(TedadVarde) AS TedadVarde, SUM(TedadSadre) AS TedadSadre, LinkKalaAid
                             FROM         View_SubTransactionKala AS SubTransactionKala_1
                             GROUP BY KALAID, GOKID, AID, LinkKalaAid) AS SubTransactionKala_1 ON FunInfoKalaAll_1.LinkKalaAid = SubTransactionKala_1.LinkKalaAid 
 ' +@SQL_WHERE + '                      
GROUP BY FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode ' + @SQL_GROUP + @SQL_HAVING




                      


		    /* --------------------    1مرتب سازی */ 			
    if(@SortRep=1) 	
			set @SQLString= @SQLString  + N'  ORDER BY KalaNumber,NameKala1 '; 	
    		    /* --------------------    2مرتب سازی */ 			
    if(@SortRep=2) 	
			set @SQLString= @SQLString + N'  ORDER BY NameKala1,KalaNumber '; 
    		    /* --------------------    3مرتب سازی */ 			
    if(@SortRep=3) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKalanumbr,NameKala1 '; 
    		    /* --------------------    4مرتب سازی */ 			
    if(@SortRep=4) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKala1,NameKala1 '; 	
        		    /* --------------------    5مرتب سازی */ 			
	 IF ((@SortRep=5) AND (@GbyAid=1)) 	
			set @SQLString= @SQLString + N'  ORDER BY AIDNumber,NameKala1 '; 
    		    /* --------------------    6مرتب سازی */ 			
    IF ((@SortRep=6) AND (@GbyAid=1))	
			set @SQLString= @SQLString + N' ORDER BY NameAnbar,NameKala1 '; 		


    
    EXECUTE sp_executesql @SQLString, @ParmDefinition,@GbyKalaID=@GbyKalaID,@GbyGokid=@GbyGokid,@SortRep=@SortRep,@S_PHI=@S_PHI,@KalaID=@KalaID,@NameKala=@NameKala,@Gokid=@Gokid,@NameGokid=@NameGokid,@Aid=@Aid,@NameAid=@NameAid,@GbyAid=@GbyAid;  		
    END 		 
    /* --------پایان */  		 		 
return END      

GO







/****** Object:  StoredProcedure [dbo].[Rep_0522862406]    Script Date: 12/11/2017 10:55:10 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_0522862406]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_0522862406]
GO


/****** Object:  StoredProcedure [dbo].[Rep_0522862406]    Script Date: 12/11/2017 10:55:10 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

  CREATE PROCEDURE [dbo].[Rep_0522862406] ( 
@pvchAction nvarchar(50)='Analyz', 
@startDate nvarchar(50), 
@ENDdate nvarchar(50), 
@IdKala int, 
@IDA int, 
@KalaGO int,  
@Goid int, 
@KolID int, 
@HesabID int, 
@THesabID int, 
@NoeFact INT,
@NameKala nvarchar(255) )           
  	AS     
  	BEGIN 	

/*
 @pvchAction='Analyz',
 @NoeFact=forms![TreeViewRep_HesabAnbar]![Nfaktor],
 @THesabId=forms![TreeViewRep_HesabAnbar]![THesabId],
 @HesabId=forms![TreeViewRep_HesabAnbar]![HesabId],
 @Goid=forms![TreeViewRep_HesabAnbar]![Goid],
 @Kolid=forms![TreeViewRep_HesabAnbar]![Kolid],
 @KalaGo=forms![TreeViewRep_HesabAnbar]![GoKala],
 @IDA=forms![TreeViewRep_HesabAnbar]![AID],
 @startDate=forms![TreeViewRep_HesabAnbar]![AsDate],
 @enddate=forms![TreeViewRep_HesabAnbar]![TaDate],
 @IdKala=forms![TreeViewRep_HesabAnbar]![KalaID],
 @NameKala=forms![TreeViewRep_HesabAnbar]![FilterNameKala]
 */  	
  	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(MAx);  	/* محتوي رشته پرس و جو*/
  	DECLARE @WhrStatement  NVARCHAR(MAx);         	
  	DECLARE @ParmDefinition  NVARCHAR(MAx);         	
  	DECLARE @ParmAndstr  NVARCHAR(MAx);    
  	     	 	
  	set @WhrStatement = N' ';         	
  	set @ParmAndstr = N' ';                  
  	SET @ParmDefinition = N'@IdKala int=null, 								
  							@IDA int=null, 								
  							@KalaGO int=null, 								
  							@startDate nvarchar(50)=''0'', 								
  							@ENDdate nvarchar(50)=''99999999'', 								
  							@KolID int=null, 								
  							@HesabID int=null, 								
  							@Goid int=null , 								
  							@THesabID int=null, 								
  							@NoeFact int=null, 								
  							@pvchAction nvarchar(50)=''Analyz''
  							,@NameKala nvarchar(255)=null';
	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END    							            	
  	
  	/* Build the SQL string one time. */      /* Specify the parameter format one time. */ 	 		 
  	/* -----------------شروع آنالیز*/ 			 
  	/* 	--------------شروع عملیات چاپ*/ 
  	IF (@pvchAction is NULL)
  				SET @pvchAction ='Analyz'
  				
  	if(@pvchAction ='Analyz') 		
  	BEGIN 			 			 
  	DECLARE @tmpdbname varchar(50) 	  		 
  	set @tmpdbname= right( REPLACE( CONVERT(uniqueidentifier, newid()), '-', ''),8) ; 	  		  	  		   	  		  	  		 
  	IF OBJECT_ID('tempdb..##' + @tmpdbname +'' ) IS NOT NULL 				
  	exec ('DROP TABLE ##' + @tmpdbname +''); 				 	  		 
  	SET @SQLString =N' SELECT     FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.BarCode, 
                      FunInfoKalaAll_1.note, SubTransactionKala_1.GOID, SubTransactionKala_1.KolID, SubTransactionKala_1.HesabID, SubTransactionKala_1.THesabID, SubTransactionKala_1.Tarikh, 
                      SubTransactionKala_1.ShomareFactor, SubTransactionKala_1.Namehesab, SubTransactionKala_1.NoeFa1, SubTransactionKala_1.TedadVarde, SubTransactionKala_1.TedadSadre, 
                      convert(money,SubTransactionKala_1.MVarde) as MVarde,convert(money,SubTransactionKala_1.MVarde/(case when SubTransactionKala_1.TedadVarde<>0 then SubTransactionKala_1.TedadVarde else 1 end )) as Phi_M_KH,convert(money,SubTransactionKala_1.MSadre/(case when SubTransactionKala_1.TedadSadre>0 then SubTransactionKala_1.TedadSadre else 1 end ))  as Phi_M_Fo, convert(money,SubTransactionKala_1.MSadre) as MSadre , SubTransactionKala_1.SharhKala, SubTransactionKala_1.NoeFa, 0 AS Radif, 0 AS TedadRadifi, 
                      0 AS MandeRadifi, 0 AS Phi_M ,SubTransactionKala_1.idFact
                      INTO         ##' + @tmpdbname +'
					  FROM         View_SubTransactionKala AS SubTransactionKala_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON SubTransactionKala_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid '
   /* --------------------   شرط حساب */ 			
 /* INTO         ##' + @tmpdbname +'  */
if(@WhrStatement=N' ')
	set @ParmAndstr=N' WHERE ' 	
else
	set @ParmAndstr=N' AND '	
		
If(NOT (@THesabID  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (SubTransactionKala_1.THesabID = @THesabID ) '; 				


if(@WhrStatement=N' ')
	set @ParmAndstr=N' WHERE ' 	
else
	set @ParmAndstr=N' AND '	
	
if (NOT (@HesabID  IS NULL)) 			
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (SubTransactionKala_1.HesabID = @HesabID ) '; 				

if(@WhrStatement=N' ')
	set @ParmAndstr=N' WHERE ' 	
else
	set @ParmAndstr=N' AND '	
 			
if(NOT (@Goid IS NULL)) 			
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (SubTransactionKala_1.Goid = @Goid) '; 				

if(@WhrStatement=N' ')
	set @ParmAndstr=N' WHERE ' 	
else
	set @ParmAndstr=N' AND '	
	
if (NOT (@KolID IS NULL)) 			
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (SubTransactionKala_1.KolID = @KolID ) '; 				
		   			 
/* -----------------------شرط انبار روی ساب پرس وجو*/ 			

if(@WhrStatement=N' ')
	set @ParmAndstr=N' WHERE ' 	
else
	set @ParmAndstr=N' AND '	
	

				
if(NOT (@IDA  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'(FunInfoKalaAll_1.AID = @IDA )'; 				
	 						 
/* -----------------------شرط کالا روی ساب پرس وجو*/ 			
if(@WhrStatement=N' ')
	set @ParmAndstr=N' WHERE ' 	
else
	set @ParmAndstr=N' AND '	
				
if(NOT (@IdKala  IS NULL)) 			
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(FunInfoKalaAll_1.ID = @IdKala )'; 				


if(@WhrStatement=N' ')
	set @ParmAndstr=N' WHERE ' 	
else
	set @ParmAndstr=N' AND '	
				
if(NOT (@NameKala   IS NULL)) 			
BEGIN
	set @NameKala ='%' + @NameKala + '%'	 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameKala1  Like @NameKala ) '; 				
set @ParmAndstr=' AND ' 			
END 				 					 
/* 	-----------------------شرط گروه انبار روی ساب پرس وجو*/ 			
if(@WhrStatement=N' ')
	set @ParmAndstr=N' WHERE ' 	
else
	set @ParmAndstr=N' AND '	
			
if(NOT (@KalaGO  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @KalaGO )'; 				
/* 	-----------------------شرط گروه انبار روی ساب پرس وجو*/ 			
if(@WhrStatement=N' ')
	set @ParmAndstr=N' WHERE ' 	
ELSE 
	set @ParmAndstr=N' AND '	
			
IF ((NOT (@startDate  IS NULL) ) AND NOT (@ENDdate  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'(SubTransactionKala_1.Tarikh  BetWeen @startDate and  @ENDdate)'; 			 			  
/* --------------------  نوع عمليات */             
if(@WhrStatement=N' ')
	set @ParmAndstr=N' WHERE ' 	
else
	set @ParmAndstr=N' AND '	
			
if(NOT (@NoeFact  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' ( SubTransactionKala_1.NoeFa= @NoeFact) '; 				
	             
set @SQLString=@SQLString + @WhrStatement+N' ORDER BY FunInfoKalaAll_1.ID, SubTransactionKala_1.Tarikh, SubTransactionKala_1.TedadVarde DESC ' 							              
 
           			 			 			
set @SQLString=@SQLString + N' 
						
Declare Cr2 Cursor Local for 						
Select ID, TedadVarde, TedadSadre, Mvarde from [##' + @tmpdbname +']  						
for Update 		
				 						
							 						 						
DECLARE @KALAID_1 bigint 						
DECLARE @Radif_2 int 						
DECLARE @TedadVarde_1 decimal(19,4) 						
DECLARE @TedadSadre_1 decimal(19,4) 						 						 						
DECLARE @Mvarde_1 decimal(19,4) 						
DECLARE @PHI_M_2 decimal(19,4) 						
DECLARE @KalaID_GH bigint 						
DECLARE @MTedadi decimal(19,4) 						
DECLARE @MRiali decimal(19,4) 						
DECLARE @PHI_M_GH decimal(19,4)  
														 		 						
set @KalaID_GH=0 						
set @PHI_M_2=0 						
set @MTedadi=0 						
set @MRiali=0 						
set @PHI_M_GH=0 						
Set @Radif_2=0
 						 						 						
Open Cr2 
  						
fetch Next from Cr2 into @KALAID_1,@TedadVarde_1,@TedadSadre_1,@Mvarde_1
						
while @@FETCH_STATUS=0 							

Begin 								
	if(@KALAID_1 <> @KalaID_GH) 								
		begin 									
			Set @Radif_2=0 									
			set	@PHI_M_2 = 0 									
			set @MRiali=0 									
			if (@TedadSadre_1>0) 										
				Set @PHI_M_GH = 1 									 									
			set @MTedadi=0 								
		end 
										 								 								
	Set @KalaID_GH = @KALAID_1 								 								
	Set @Radif_2=@Radif_2 + 1 								 								
	Set @MTedadi=@MTedadi+(@TedadVarde_1-@TedadSadre_1) 														 								
	set @MRiali=@MRiali+@Mvarde_1-(@PHI_M_GH * @TedadSadre_1)

	if (@TedadVarde_1>0) and (@MTedadi<>0)
		Begin
			Set @PHI_M_2 = round(ABS(@MRiali/@MTedadi),0)
		End 

	if (@MTedadi=0)
		Set @PHI_M_2 = 0 

	if (@TedadSadre_1>0)
		Begin
			Set @PHI_M_2=@PHI_M_GH
			set @MRiali=@PHI_M_GH *@MTedadi
		End
		 									 								
Set @PHI_M_GH=@PHI_M_2

Set @MTedadi=Round(Convert(Float,@MTedadi),2)

							 																	 								
Update ##' + @tmpdbname +' set Radif=@Radif_2,PHI_M=@PHI_M_2 , TedadRadifi=@MTedadi, MandeRadifi=@MRiali Where current of cr2; 								
	
fetch Next from Cr2 into @KALAID_1,@TedadVarde_1,@TedadSadre_1,@Mvarde_1			
End	 						 						
Close cr2 						
deallocate cr2	' 	 			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@Goid=@Goid,@pvchAction=@pvchAction,@IDA=@IDA,@NoeFact=@NoeFact,@THesabID=@THesabID,@HesabID=@HesabID,@KolID=@KolID,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@ENDdate=@ENDdate,@NameKala=@NameKala; 			 			
EXEC ('select * from [##' + @tmpdbname +'] 				DROP TABLE ##' + @tmpdbname +''); 		
END                                                  
return END      


              
GO



/****** Object:  StoredProcedure [dbo].[Rep_0522862508]    Script Date: 12/11/2017 10:55:14 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_0522862508]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_0522862508]
GO



/****** Object:  StoredProcedure [dbo].[Rep_0522862508]    Script Date: 12/11/2017 10:55:14 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

  CREATE PROCEDURE [dbo].[Rep_0522862508] ( 
  	@IdKala int, 
  	@IDA int, 
  	@KalaGO int, 
  	@startDate nvarchar(50), 
  	@enddate nvarchar(50)  
  )           AS     
  begin 	
  	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  
  	NVARCHAR(4000);  	
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  	set @WhrStatement  = N'';         	
  	set @ParmAndstr = N'';                  	
  	SET @ParmDefinition = N'@IdKala int=null, 												
  							@IDA int=null, 												
  							@KalaGO int=null, 												
  							@startDate nvarchar(50)=''-1'', 												
  							@enddate nvarchar(50)=''99999999''';            	
  	/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
BEGIN 	 
  		
IF (@startDate IS NULL)
	Set @startDate=N'00000000'
IF (@enddate IS NULL)
	Set @enddate=N'99999999'


set @ParmAndstr=N' AND '
	
if(NOT (@IDA  IS NULL)) 			
BEGIN 	
	SET @WhrStatement= @WhrStatement + @ParmAndstr + N'(FunInfoKalaAll_1.AID = @IDA )'; 				
END
 
set @ParmAndstr=N' AND '	 
 			 			
if(NOT (@IdKala  IS NULL)) 			
BEGIN 				
	SET @WhrStatement= @WhrStatement + @ParmAndstr + N'(FunInfoKalaAll_1.ID = @IdKala )'; 				
END 

set @ParmAndstr=N' AND '	
				 			 			
if(NOT (@KalaGO  IS NULL)) 			
BEGIN 				
	SET @WhrStatement= @WhrStatement + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @KalaGO )'; 				
END 		
  			  		 
SET @SQLString ='SELECT     FunInfoKalaAll_1.AID, FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, 
                      FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) 
                      AS SumTedad, FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, MAX(Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS Max_UnitPriceKh, MAX(FunInfoKalaAll_1.UnitPrice1) AS MaxUnitPrice1, 
                      MAX(FunInfoKalaAll_1.UnitPrice2) AS MaxUnitPrice2, MAX(FunInfoKalaAll_1.UnitPrice3) AS MaxUnitPrice3, MAX(FunInfoKalaAll_1.UnitPrice4) AS MaxUnitPrice4, MAX(FunInfoKalaAll_1.UnitPrice5) 
                      AS MaxUnitPrice5, MAX(FunInfoKalaAll_1.UnitPrice6) AS MaxUnitPrice6, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode, SUM(CONVERT(float, 
                      SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre) * Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS MandeRiali
FROM         View_SubTransactionKala AS SubTransactionKala_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON SubTransactionKala_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid  LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 ON FunInfoKalaAll_1.LinkKalaAid = Fun_SubAkharinKharid_1.LinkKalaAid
WHERE     (SubTransactionKala_1.Tarikh BETWEEN @startDate AND @enddate)
GROUP BY FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode, 
                      FunInfoKalaAll_1.AID
HAVING      (SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) < 0)  ' 			 			 			

set @SQLString=@SQLString + @WhrStatement  	 			
			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@IDA=@IDA,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@enddate=@enddate;  		
END                                                  
RETURN END
GO



/****** Object:  StoredProcedure [dbo].[Rep_0522862818]    Script Date: 12/11/2017 10:55:18 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_0522862818]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_0522862818]
GO



/****** Object:  StoredProcedure [dbo].[Rep_0522862818]    Script Date: 12/11/2017 10:55:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*
@S_PHI=forms![TreeViewRep_HesabAnbar]![Sth_PHI].column(2),
@SortRep=forms![TreeViewRep_HesabAnbar]![Sorting],
@KalaID =forms![TreeViewRep_HesabAnbar]![KalaID],
@NameKala =forms![TreeViewRep_HesabAnbar]![FilterNameKala],
@GbyKalaID =forms![TreeViewRep_HesabAnbar]![ChGbyKalaID],
@Gokid  =forms![TreeViewRep_HesabAnbar]![GoKala],
@NameGokid =forms![TreeViewRep_HesabAnbar]![FilterNameGOKALA],
@GbyGokid =forms![TreeViewRep_HesabAnbar]![ChGbyGokid],
@Aid  =forms![TreeViewRep_HesabAnbar]![AID],
@NameAid =forms![TreeViewRep_HesabAnbar]![FilterNameAID],
@GbyAid =forms![TreeViewRep_HesabAnbar]![ChGbyAid]
*/

  CREATE PROCEDURE [dbo].[Rep_0522862818] ( 
	@S_PHI int,/****** براساس آخرین خرید (1)  قیمت سطح 1 (2) قیمت سطح 2 (3) قیمت سطح 4 (5) قیمت سطح 5 (6) قیمت سطح 6 (6)قیمت *******/
	@SortRep int, 
	@KalaID int,
	@NameKala nvarchar(255), 
	@GbyKalaID AS BIT,
	@Gokid int,
	@NameGokid nvarchar(255), 
	@GbyGokid AS BIT,
	@Aid int,
	@NameAid  nvarchar(255),
	@GbyAid AS BIT)           
  AS     
  BEGIN 	SET NOCOUNT ON; 
  	 	
  DECLARE @SQLString  NVARCHAR(Max); 
  DECLARE @SQL_SELECT  NVARCHAR(Max); 
  DECLARE @SQL_FROM NVARCHAR(Max); 
  DECLARE @SQL_WHERE  NVARCHAR(Max); 
  DECLARE @SQL_GROUP  NVARCHAR(Max); 
  DECLARE @SQL_HAVING  NVARCHAR(Max); 
  DECLARE @SQL_ORDER NVARCHAR(Max); 
  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max); 
  
  set @SQL_SELECT=N' ';
  Set @SQL_FROM=N' ';
  Set @SQL_WHERE=N' ';
  Set @SQL_GROUP=N' ';
  Set @SQL_ORDER=N' ';	
  set @WhrStatement  = N' ';         	
  set @ParmAndstr = N' ';  
  /*SEt @SQL_HAVING =N' HAVING      (SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) <> 0) ';*/
  SEt @SQL_HAVING =N' HAVING      (1 = 1) ';
  SET @ParmDefinition =N'@S_PHI int=null,
						@GbyKalaID AS BIT=null,
						@GbyGokid AS BIT=null,
						@SortRep int=null, 
						@KalaID int=null,
						@NameKala nvarchar(255)=null, 
						@Gokid int=null,
						@NameGokid nvarchar(255)=null, 
						@Aid int=null,
						@NameAid  nvarchar(255)=null,
						@GbyAid bit=1';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		

  BEGIN 
  

  
   if (@S_PHI IS NULL)
		Set @S_PHI=1
		
  if (@S_PHI=1)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(Fun_SubAkharinKharid_1.MaxUnitPriceKh, 0)) AS SumMabUnit, MAX(ISNULL(Fun_SubAkharinKharid_1.MaxUnitPriceKh, 0)) as MabUnit  '
  if (@S_PHI=2)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice1, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice1, 0)) as MabUnit '
  if (@S_PHI=3)
	Set @SQL_SELECT=N' , Sum(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * Max(ISNULL(FunInfoKalaAll_1.UnitPrice2, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice2, 0)) as MabUnit  '	
  if (@S_PHI=4)
	Set @SQL_SELECT=N' , Sum(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice3, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice3, 0)) as MabUnit '
  if (@S_PHI=5)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice4, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice4, 0)) as MabUnit  '	
  if (@S_PHI=6)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * Max(ISNULL(FunInfoKalaAll_1.UnitPrice5, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice5, 0) as MabUnit '
  if (@S_PHI=7)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * max(ISNULL(FunInfoKalaAll_1.UnitPrice6, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice6, 0)) as MabUnit '	
	

		/* شرط انطباق انبار*/
  
  if (@GbyAid=1) 
			Begin
				Set @SQL_SELECT=@SQL_SELECT + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar '
				Set @SQL_GROUP=@SQL_GROUP + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber , FunInfoKalaAll_1.NameAnbar ' 
				
				if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' ( FunInfoKalaAll_1.AID = @Aid )'; 
					end

				if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 	
					End
			end
  else
		Begin
			if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' WHERE ')
							set @ParmAndstr=N' ' 	
						else
							set @ParmAndstr=N' AND '

						set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'( FunInfoKalaAll_1.AID = @Aid )'; 
					End					

			if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' ')
							set @ParmAndstr=N' WHERE ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						
						Set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 
					End						
		End  			
		
 
   /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@KalaID IS NULL)) 
			Begin
				if(@SQL_HAVING=N' ')
					set @ParmAndstr=N' HAVING ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'   (FunInfoKalaAll_1.ID = @KalaID ) ' 
								
			End
     
    /* --------------------شرط انطباق قسمتی از نام کالا*/ 			
  if(NOT (@NameKala IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameKala='%'+@NameKala+'%'
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameKala1 LIKE @NameKala) '; 	
		End

	/* --------------------شرط انطباق گروه*/ 			

	if(NOT (@Gokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @Gokid )'; 	
		End
  
      /* --------------------شرط انطباق قسمتی از نام گروه کالا*/ 			

	if(NOT (@NameGokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameGokid='%'+@NameGokid+'%'
			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameGoKala1 LIKE @NameGokid) '; 
		End	

  Set @SQLString =N'SELECT     FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) AS SumTedad, 
                      FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, MAX(Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS Max_UnitPriceKh, MAX(FunInfoKalaAll_1.UnitPrice1) AS MaxUnitPrice1, 
                      MAX(FunInfoKalaAll_1.UnitPrice2) AS MaxUnitPrice2, MAX(FunInfoKalaAll_1.UnitPrice3) AS MaxUnitPrice3, MAX(FunInfoKalaAll_1.UnitPrice4) AS MaxUnitPrice4, MAX(FunInfoKalaAll_1.UnitPrice5) 
                      AS MaxUnitPrice5, MAX(FunInfoKalaAll_1.UnitPrice6) AS MaxUnitPrice6, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode, SUM(CONVERT(float, 
                      CASE WHEN idFact <> 0 THEN TedadVarde END)) AS TedadVarde, SUM(CONVERT(Float, SubTransactionKala_1.TedadSadre)) AS TedadSadre, Sum(CONVERT(Float, 
                      CASE WHEN idFact = 0 THEN TedadVarde END)) AS TE
' + @SQL_SELECT + ' 
FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid INNER JOIN
                      View_SubTransactionKala AS SubTransactionKala_1 ON FunInfoKalaAll_1.LinkKalaAid = SubTransactionKala_1.LinkKalaAid ' +@SQL_WHERE + ' 
 GROUP BY FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode
'+ @SQL_HAVING  



                      


		    /* --------------------    1مرتب سازی */ 			
    if(@SortRep=1) 	
			set @SQLString= @SQLString  + N'  ORDER BY KalaNumber,NameKala1 '; 	
    		    /* --------------------    2مرتب سازی */ 			
    if(@SortRep=2) 	
			set @SQLString= @SQLString + N'  ORDER BY NameKala1,KalaNumber '; 
    		    /* --------------------    3مرتب سازی */ 			
    if(@SortRep=3) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKalanumbr,NameKala1 '; 
    		    /* --------------------    4مرتب سازی */ 			
    if(@SortRep=4) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKala1,NameKala1 '; 	
        		    /* --------------------    5مرتب سازی */ 			
	 IF ((@SortRep=5) AND (@GbyAid=1)) 	
			set @SQLString= @SQLString + N'  ORDER BY AIDNumber,NameKala1 '; 
    		    /* --------------------    6مرتب سازی */ 			
    IF ((@SortRep=6) AND (@GbyAid=1))	
			set @SQLString= @SQLString + N' ORDER BY NameAnbar,NameKala1 '; 		


    
    EXECUTE sp_executesql @SQLString, @ParmDefinition,@GbyKalaID=@GbyKalaID,@GbyGokid=@GbyGokid,@SortRep=@SortRep,@S_PHI=@S_PHI,@KalaID=@KalaID,@NameKala=@NameKala,@Gokid=@Gokid,@NameGokid=@NameGokid,@Aid=@Aid,@NameAid=@NameAid,@GbyAid=@GbyAid;  		
    END 		 
    /* --------پایان */  		 		 
return END      

GO



/****** Object:  StoredProcedure [dbo].[Rep_0522862828]    Script Date: 12/11/2017 10:55:21 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_0522862828]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_0522862828]
GO



/****** Object:  StoredProcedure [dbo].[Rep_0522862828]    Script Date: 12/11/2017 10:55:21 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*
@S_PHI=forms![TreeViewRep_HesabAnbar]![Sth_PHI].column(2),
@SortRep=forms![TreeViewRep_HesabAnbar]![Sorting],
@KalaID =forms![TreeViewRep_HesabAnbar]![KalaID],
@NameKala =forms![TreeViewRep_HesabAnbar]![FilterNameKala],
@GbyKalaID =forms![TreeViewRep_HesabAnbar]![ChGbyKalaID],
@Gokid  =forms![TreeViewRep_HesabAnbar]![GoKala],
@NameGokid =forms![TreeViewRep_HesabAnbar]![FilterNameGOKALA],
@GbyGokid =forms![TreeViewRep_HesabAnbar]![ChGbyGokid],
@Aid  =forms![TreeViewRep_HesabAnbar]![AID],
@NameAid =forms![TreeViewRep_HesabAnbar]![FilterNameAID],
@GbyAid =forms![TreeViewRep_HesabAnbar]![ChGbyAid]
*/

  CREATE PROCEDURE [dbo].[Rep_0522862828] ( 
	@S_PHI int,/****** براساس آخرین خرید (1)  قیمت سطح 1 (2) قیمت سطح 2 (3) قیمت سطح 4 (5) قیمت سطح 5 (6) قیمت سطح 6 (6)قیمت *******/
	@SortRep int, 
	@KalaID int,
	@NameKala nvarchar(255), 
	@GbyKalaID AS BIT,
	@Gokid int,
	@NameGokid nvarchar(255), 
	@GbyGokid AS BIT,
	@Aid int,
	@NameAid  nvarchar(255),
	@GbyAid AS BIT)           
  AS     
  BEGIN 	SET NOCOUNT ON; 
  	 	
  DECLARE @SQLString  NVARCHAR(Max); 
  DECLARE @SQL_SELECT  NVARCHAR(Max); 
  DECLARE @SQL_FROM NVARCHAR(Max); 
  DECLARE @SQL_WHERE  NVARCHAR(Max); 
  DECLARE @SQL_GROUP  NVARCHAR(Max); 
  DECLARE @SQL_HAVING  NVARCHAR(Max); 
  DECLARE @SQL_ORDER NVARCHAR(Max); 
  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max); 
  
  set @SQL_SELECT=N' ';
  Set @SQL_FROM=N' ';
  Set @SQL_WHERE=N' ';
  Set @SQL_GROUP=N' ';
  Set @SQL_ORDER=N' ';	
  set @WhrStatement  = N' ';         	
  set @ParmAndstr = N' ';  
  /*SEt @SQL_HAVING =N' HAVING      (SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) <> 0) ';*/
  SEt @SQL_HAVING =N' HAVING      (1 = 1) ';
  SET @ParmDefinition =N'@S_PHI int=null,
						@GbyKalaID AS BIT=null,
						@GbyGokid AS BIT=null,
						@SortRep int=null, 
						@KalaID int=null,
						@NameKala nvarchar(255)=null, 
						@Gokid int=null,
						@NameGokid nvarchar(255)=null, 
						@Aid int=null,
						@NameAid  nvarchar(255)=null,
						@GbyAid bit=1';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		

  BEGIN 
  

  
   if (@S_PHI IS NULL)
		Set @S_PHI=1
		
  if (@S_PHI=1)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(Fun_SubAkharinKharid_1.MaxUnitPriceKh, 0)) AS SumMabUnit, MAX(ISNULL(Fun_SubAkharinKharid_1.MaxUnitPriceKh, 0)) as MabUnit  '
  if (@S_PHI=2)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice1, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice1, 0)) as MabUnit '
  if (@S_PHI=3)
	Set @SQL_SELECT=N' , Sum(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * Max(ISNULL(FunInfoKalaAll_1.UnitPrice2, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice2, 0)) as MabUnit  '	
  if (@S_PHI=4)
	Set @SQL_SELECT=N' , Sum(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice3, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice3, 0)) as MabUnit '
  if (@S_PHI=5)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice4, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice4, 0)) as MabUnit  '	
  if (@S_PHI=6)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * Max(ISNULL(FunInfoKalaAll_1.UnitPrice5, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice5, 0) as MabUnit '
  if (@S_PHI=7)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * max(ISNULL(FunInfoKalaAll_1.UnitPrice6, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice6, 0)) as MabUnit '	
	

		/* شرط انطباق انبار*/
  
  if (@GbyAid=1) 
			Begin
				Set @SQL_SELECT=@SQL_SELECT + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar '
				Set @SQL_GROUP=@SQL_GROUP + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber , FunInfoKalaAll_1.NameAnbar ' 
				
				if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' ( FunInfoKalaAll_1.AID = @Aid )'; 
					end

				if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 	
					End
			end
  else
		Begin
			if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' WHERE ')
							set @ParmAndstr=N' ' 	
						else
							set @ParmAndstr=N' AND '

						set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'( FunInfoKalaAll_1.AID = @Aid )'; 
					End					

			if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' ')
							set @ParmAndstr=N' WHERE ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						
						Set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 
					End						
		End  			
		
 
   /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@KalaID IS NULL)) 
			Begin
				if(@SQL_HAVING=N' ')
					set @ParmAndstr=N' HAVING ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'   (FunInfoKalaAll_1.ID = @KalaID ) ' 
								
			End
     
    /* --------------------شرط انطباق قسمتی از نام کالا*/ 			
  if(NOT (@NameKala IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameKala='%'+@NameKala+'%'
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameKala1 LIKE @NameKala) '; 	
		End

	/* --------------------شرط انطباق گروه*/ 			

	if(NOT (@Gokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @Gokid )'; 	
		End
  
      /* --------------------شرط انطباق قسمتی از نام گروه کالا*/ 			

	if(NOT (@NameGokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameGokid='%'+@NameGokid+'%'
			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameGoKala1 LIKE @NameGokid) '; 
		End	

  Set @SQLString =N'SELECT     FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) AS SumTedad, 
                      FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, MAX(Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS Max_UnitPriceKh, MAX(FunInfoKalaAll_1.UnitPrice1) AS MaxUnitPrice1, 
                      MAX(FunInfoKalaAll_1.UnitPrice2) AS MaxUnitPrice2, MAX(FunInfoKalaAll_1.UnitPrice3) AS MaxUnitPrice3, MAX(FunInfoKalaAll_1.UnitPrice4) AS MaxUnitPrice4, MAX(FunInfoKalaAll_1.UnitPrice5) 
                      AS MaxUnitPrice5, MAX(FunInfoKalaAll_1.UnitPrice6) AS MaxUnitPrice6, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode, SUM(CONVERT(float, 
                      CASE WHEN idFact <> 0 THEN TedadVarde END)) AS TedadVarde, SUM(CONVERT(Float, SubTransactionKala_1.TedadSadre)) AS TedadSadre, SUM(CONVERT(Float, 
                      CASE WHEN idFact = 0 THEN TedadVarde END)) AS TE, SUM(SubTransactionKala_1.TedadKh) AS TedadKh, SUM(SubTransactionKala_1.TedadVA) AS TedadVA, 
                      SUM(SubTransactionKala_1.TedadFo) AS TedadFo, SUM(SubTransactionKala_1.TedadKhorooj) AS TedadKhorooj, SUM(SubTransactionKala_1.TedadBarKh) AS TedadBarKh, 
                      SUM(SubTransactionKala_1.TedadKhA) AS TedadKhA, SUM(SubTransactionKala_1.TedadVorood) AS TedadVorood, SUM(SubTransactionKala_1.TedadBarFo) AS TedadBarFo
' + @SQL_SELECT + ' 
FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid INNER JOIN
                      View_SubTransactionKala AS SubTransactionKala_1 ON FunInfoKalaAll_1.LinkKalaAid = SubTransactionKala_1.LinkKalaAid
' +@SQL_WHERE + '
GROUP BY FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode
'+ @SQL_HAVING    



                      


		    /* --------------------    1مرتب سازی */ 			
    if(@SortRep=1) 	
			set @SQLString= @SQLString  + N'  ORDER BY KalaNumber,NameKala1 '; 	
    		    /* --------------------    2مرتب سازی */ 			
    if(@SortRep=2) 	
			set @SQLString= @SQLString + N'  ORDER BY NameKala1,KalaNumber '; 
    		    /* --------------------    3مرتب سازی */ 			
    if(@SortRep=3) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKalanumbr,NameKala1 '; 
    		    /* --------------------    4مرتب سازی */ 			
    if(@SortRep=4) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKala1,NameKala1 '; 	
        		    /* --------------------    5مرتب سازی */ 			
	 IF ((@SortRep=5) AND (@GbyAid=1)) 	
			set @SQLString= @SQLString + N'  ORDER BY AIDNumber,NameKala1 '; 
    		    /* --------------------    6مرتب سازی */ 			
    IF ((@SortRep=6) AND (@GbyAid=1))	
			set @SQLString= @SQLString + N' ORDER BY NameAnbar,NameKala1 '; 		


    
    EXECUTE sp_executesql @SQLString, @ParmDefinition,@GbyKalaID=@GbyKalaID,@GbyGokid=@GbyGokid,@SortRep=@SortRep,@S_PHI=@S_PHI,@KalaID=@KalaID,@NameKala=@NameKala,@Gokid=@Gokid,@NameGokid=@NameGokid,@Aid=@Aid,@NameAid=@NameAid,@GbyAid=@GbyAid;  		
    END 		 
    /* --------پایان */  		 		 
return END      

GO




/****** Object:  StoredProcedure [dbo].[Rep_0522862834]    Script Date: 12/11/2017 10:55:25 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_0522862834]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_0522862834]
GO



/****** Object:  StoredProcedure [dbo].[Rep_0522862834]    Script Date: 12/11/2017 10:55:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/*
@S_PHI=forms![TreeViewRep_HesabAnbar]![Sth_PHI].column(2),
@SortRep=forms![TreeViewRep_HesabAnbar]![Sorting],
@KalaID =forms![TreeViewRep_HesabAnbar]![KalaID],
@NameKala =forms![TreeViewRep_HesabAnbar]![FilterNameKala],
@GbyKalaID =forms![TreeViewRep_HesabAnbar]![ChGbyKalaID],
@Gokid  =forms![TreeViewRep_HesabAnbar]![GoKala],
@NameGokid =forms![TreeViewRep_HesabAnbar]![FilterNameGOKALA],
@GbyGokid =forms![TreeViewRep_HesabAnbar]![ChGbyGokid],
@Aid  =forms![TreeViewRep_HesabAnbar]![AID],
@NameAid =forms![TreeViewRep_HesabAnbar]![FilterNameAID],
@GbyAid =forms![TreeViewRep_HesabAnbar]![ChGbyAid]
*/

  CREATE PROCEDURE [dbo].[Rep_0522862834] ( 
	@S_PHI int,/****** براساس آخرین خرید (1)  قیمت سطح 1 (2) قیمت سطح 2 (3) قیمت سطح 4 (5) قیمت سطح 5 (6) قیمت سطح 6 (6)قیمت *******/
	@SortRep int, 
	@KalaID int,
	@NameKala nvarchar(255), 
	@GbyKalaID AS BIT,
	@Gokid int,
	@NameGokid nvarchar(255), 
	@GbyGokid AS BIT,
	@Aid int,
	@NameAid  nvarchar(255),
	@GbyAid AS BIT)           
  AS     
  BEGIN 	SET NOCOUNT ON; 
  	 	
  DECLARE @SQLString  NVARCHAR(Max); 
  DECLARE @SQL_SELECT  NVARCHAR(Max); 
  DECLARE @SQL_FROM NVARCHAR(Max); 
  DECLARE @SQL_WHERE  NVARCHAR(Max); 
  DECLARE @SQL_GROUP  NVARCHAR(Max); 
  DECLARE @SQL_HAVING  NVARCHAR(Max); 
  DECLARE @SQL_ORDER NVARCHAR(Max); 
  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max); 
  
  set @SQL_SELECT=N' ';
  Set @SQL_FROM=N' ';
  Set @SQL_WHERE=N' ';
  Set @SQL_GROUP=N' ';
  Set @SQL_ORDER=N' ';	
  set @WhrStatement  = N' ';         	
  set @ParmAndstr = N' ';  
  /*SEt @SQL_HAVING =N' HAVING      (SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) <> 0) ';*/
  SEt @SQL_HAVING =N' HAVING      (1 = 1) ';
  SET @ParmDefinition =N'@S_PHI int=null,
						@GbyKalaID AS BIT=null,
						@GbyGokid AS BIT=null,
						@SortRep int=null, 
						@KalaID int=null,
						@NameKala nvarchar(255)=null, 
						@Gokid int=null,
						@NameGokid nvarchar(255)=null, 
						@Aid int=null,
						@NameAid  nvarchar(255)=null,
						@GbyAid bit=1';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		

  BEGIN 
  

  
   if (@S_PHI IS NULL)
		Set @S_PHI=1
		
  if (@S_PHI=1)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(Fun_SubAkharinKharid_1.MaxUnitPriceKh, 0)) AS SumMabUnit, MAX(ISNULL(Fun_SubAkharinKharid_1.MaxUnitPriceKh, 0)) as MabUnit  '
  if (@S_PHI=2)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice1, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice1, 0)) as MabUnit '
  if (@S_PHI=3)
	Set @SQL_SELECT=N' , Sum(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * Max(ISNULL(FunInfoKalaAll_1.UnitPrice2, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice2, 0)) as MabUnit  '	
  if (@S_PHI=4)
	Set @SQL_SELECT=N' , Sum(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice3, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice3, 0)) as MabUnit '
  if (@S_PHI=5)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice4, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice4, 0)) as MabUnit  '	
  if (@S_PHI=6)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * Max(ISNULL(FunInfoKalaAll_1.UnitPrice5, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice5, 0) as MabUnit '
  if (@S_PHI=7)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * max(ISNULL(FunInfoKalaAll_1.UnitPrice6, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice6, 0)) as MabUnit '	
	

		/* شرط انطباق انبار*/
  
  if (@GbyAid=1) 
			Begin
				Set @SQL_SELECT=@SQL_SELECT + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar '
				Set @SQL_GROUP=@SQL_GROUP + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber , FunInfoKalaAll_1.NameAnbar ' 
				
				if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' ( FunInfoKalaAll_1.AID = @Aid )'; 
					end

				if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 	
					End
			end
  else
		Begin
			if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' WHERE ')
							set @ParmAndstr=N' ' 	
						else
							set @ParmAndstr=N' AND '

						set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'( FunInfoKalaAll_1.AID = @Aid )'; 
					End					

			if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' ')
							set @ParmAndstr=N' WHERE ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						
						Set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 
					End						
		End  			
		
 
   /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@KalaID IS NULL)) 
			Begin
				if(@SQL_HAVING=N' ')
					set @ParmAndstr=N' HAVING ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'   (FunInfoKalaAll_1.ID = @KalaID ) ' 
								
			End
     
    /* --------------------شرط انطباق قسمتی از نام کالا*/ 			
  if(NOT (@NameKala IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameKala='%'+@NameKala+'%'
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameKala1 LIKE @NameKala) '; 	
		End

	/* --------------------شرط انطباق گروه*/ 			

	if(NOT (@Gokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @Gokid )'; 	
		End
  
      /* --------------------شرط انطباق قسمتی از نام گروه کالا*/ 			

	if(NOT (@NameGokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameGokid='%'+@NameGokid+'%'
			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameGoKala1 LIKE @NameGokid) '; 
		End	

  Set @SQLString =N'SELECT     FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) 
                      AS SumTedad, MAX(Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS Max_UnitPriceKh, MAX(FunInfoKalaAll_1.UnitPrice1) AS MaxUnitPrice1, MAX(FunInfoKalaAll_1.UnitPrice2) AS MaxUnitPrice2, 
                      MAX(FunInfoKalaAll_1.UnitPrice3) AS MaxUnitPrice3, MAX(FunInfoKalaAll_1.UnitPrice4) AS MaxUnitPrice4, MAX(FunInfoKalaAll_1.UnitPrice5) AS MaxUnitPrice5, MAX(FunInfoKalaAll_1.UnitPrice6) 
                      AS MaxUnitPrice6, SUM(CONVERT(float, CASE WHEN idFact <> 0 THEN TedadVarde END)) AS TedadVarde, SUM(CONVERT(Float, SubTransactionKala_1.TedadSadre)) AS TedadSadre, 
                      SUM(CONVERT(Float, CASE WHEN idFact = 0 THEN TedadVarde END)) AS TE, SUM(SubTransactionKala_1.TedadKh) AS TedadKh, SUM(SubTransactionKala_1.TedadVA) AS TedadVA, 
                      SUM(SubTransactionKala_1.TedadFo) AS TedadFo, SUM(SubTransactionKala_1.TedadKhorooj) AS TedadKhorooj, SUM(SubTransactionKala_1.TedadBarKh) AS TedadBarKh, 
                      SUM(SubTransactionKala_1.TedadKhA) AS TedadKhA, SUM(SubTransactionKala_1.TedadVorood) AS TedadVorood, SUM(SubTransactionKala_1.TedadBarFo) AS TedadBarFo
' + @SQL_SELECT + ' 

FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid INNER JOIN
                      View_SubTransactionKala AS SubTransactionKala_1 ON FunInfoKalaAll_1.LinkKalaAid = SubTransactionKala_1.LinkKalaAid 
' +@SQL_WHERE + '
GROUP BY FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1
'+ @SQL_HAVING  



                      


		    /* --------------------    1مرتب سازی */ 			
    if(@SortRep=1) 	
			set @SQLString= @SQLString  + N'  ORDER BY KalaNumber,NameKala1 '; 	
    		    /* --------------------    2مرتب سازی */ 			
    if(@SortRep=2) 	
			set @SQLString= @SQLString + N'  ORDER BY NameKala1,KalaNumber '; 
    		    /* --------------------    3مرتب سازی */ 			
    if(@SortRep=3) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKalanumbr,NameKala1 '; 
    		    /* --------------------    4مرتب سازی */ 			
    if(@SortRep=4) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKala1,NameKala1 '; 	
        		    /* --------------------    5مرتب سازی */ 			
	 IF ((@SortRep=5) AND (@GbyAid=1)) 	
			set @SQLString= @SQLString + N'  ORDER BY AIDNumber,NameKala1 '; 
    		    /* --------------------    6مرتب سازی */ 			
    IF ((@SortRep=6) AND (@GbyAid=1))	
			set @SQLString= @SQLString + N' ORDER BY NameAnbar,NameKala1 '; 		


    
    EXECUTE sp_executesql @SQLString, @ParmDefinition,@GbyKalaID=@GbyKalaID,@GbyGokid=@GbyGokid,@SortRep=@SortRep,@S_PHI=@S_PHI,@KalaID=@KalaID,@NameKala=@NameKala,@Gokid=@Gokid,@NameGokid=@NameGokid,@Aid=@Aid,@NameAid=@NameAid,@GbyAid=@GbyAid;  		
    END 		 
    /* --------پایان */  		 		 
return END      

GO




/****** Object:  StoredProcedure [dbo].[ACC_Tbl_Device_DELETE]    Script Date: 12/12/2017 08:40:51 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ACC_Tbl_Device_DELETE]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ACC_Tbl_Device_DELETE]
GO


/****** Object:  StoredProcedure [dbo].[ACC_Tbl_Device_DELETE]    Script Date: 12/12/2017 08:40:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    Create PROCEDURE [dbo].[ACC_Tbl_Device_DELETE] 	 		@UserID INT,         @IDAuto INT           AS BEGIN  DELETE FROM Tbl_Device WHERE     (IDAuto = @IDAuto)   END
GO



/****** Object:  StoredProcedure [dbo].[ACC_Tbl_Device_GetList]    Script Date: 12/12/2017 08:41:18 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ACC_Tbl_Device_GetList]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ACC_Tbl_Device_GetList]
GO



/****** Object:  StoredProcedure [dbo].[ACC_Tbl_Device_GetList]    Script Date: 12/12/2017 08:41:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

   CREATE	 PROCEDURE [dbo].[ACC_Tbl_Device_GetList]  		@UserID INT, 		@TxtNote NVARCHAR(255), 		@GOID INT, 		@KOLID INT,  		@HESABID INT,  		@THESABID INT, 		@ShSerial NVARCHAR(255),  		@TxtAddress NVARCHAR(255),  		@NoeDevice NVARCHAR(255),  		@ShPigiri NVARCHAR(255),  		@dateStart1 NVARCHAR(10),  		@dateStart2 NVARCHAR(10),  		@dateEnd1 NVARCHAR(10),  		@dateEnd2 NVARCHAR(10), 		@DatePigiri1 NVARCHAR(10), 		@DatePigiri2 NVARCHAR(10)    AS  BEGIN  SET NOCOUNT ON;  DECLARE @SQLString AS NVARCHAR(2000)	; DECLARE @StrWher AS NVARCHAR(2000);	 DECLARE @ParmDefinition  NVARCHAR(2000);   SET @ParmDefinition = N'@UserID INT, 						@TxtNote NVARCHAR(255), 						@GOID INT, 						@KOLID INT,  						@HESABID INT,  						@THESABID INT, 						@ShSerial NVARCHAR(255),  						@TxtAddress NVARCHAR(255),  						@NoeDevice NVARCHAR(255),  						@ShPigiri NVARCHAR(255),  						@dateStart1 NVARCHAR(10),  						@dateStart2 NVARCHAR(10),  						@dateEnd1 NVARCHAR(10),  						@dateEnd2 NVARCHAR(10), 						@DatePigiri1 NVARCHAR(10), 						@DatePigiri2 NVARCHAR(10)'   SET @StrWher=N' '  IF (NOT (@TxtNote IS NULL)) BEGIN  	SET @TxtNote='%' + @TxtNote + '%' 	SET @StrWher= @StrWher + ' And (TxtNote LIKE @TxtNote)' END 	  IF (NOT (@GOID IS NULL)) BEGIN  	SET @StrWher= @StrWher + ' And (GOID = @GOID)' END   IF (NOT (@KOLID  IS NULL)) BEGIN  	SET @StrWher= @StrWher + ' And (KOLID = @KOLID)' END 	  IF (NOT (@HESABID IS NULL)) BEGIN  	SET @StrWher= @StrWher + ' And (HESABID = @HESABID)' END   IF (NOT (@THESABID IS NULL)) BEGIN  	SET @StrWher= @StrWher + ' And (THESABID = @THESABID)' END   IF (NOT (@ShSerial IS NULL)) BEGIN  	SET @ShSerial='%' + @ShSerial + '%' 	SET @StrWher= @StrWher + ' And (ShSerial LIKE @ShSerial)' END   IF (NOT (@TxtAddress IS NULL)) BEGIN  	SET @TxtAddress='%' + @TxtAddress + '%' 	SET @StrWher= @StrWher + ' And (TxtAddress LIKE @TxtAddress)' END   IF (NOT (@NoeDevice IS NULL)) BEGIN  	SET @NoeDevice='%' + @NoeDevice + '%' 	SET @StrWher= @StrWher + ' And (NoeDevice LIKE @NoeDevice)' END   IF (NOT (@ShPigiri IS NULL)) BEGIN  	SET @ShPigiri='%' + @ShPigiri + '%' 	SET @StrWher= @StrWher + ' And (ShPigiri LIKE @ShPigiri)' END    IF (NOT (@dateStart1 IS NULL) AND NOT (@dateStart2 IS NULL)) BEGIN  	SET @StrWher= @StrWher + ' And (dateStart between @dateStart1 and @dateStart2)' END   IF (NOT (@dateEnd1 IS NULL) AND NOT (@dateEnd2 IS NULL)) BEGIN  	SET @StrWher= @StrWher + ' And (dateEnd between @dateEnd1 and @dateEnd2)' END   IF (NOT (@DatePigiri1 IS NULL) AND NOT (@DatePigiri2 IS NULL)) BEGIN  	SET @StrWher= @StrWher + ' And (DatePigiri between @DatePigiri1 and @DatePigiri2)' END  		 SET  @SQLString = 'SELECT     IDAuto, TxtNote, GOID, KOLID, HESABID, THESABID, NameHesab, ShSerial, TxtAddress, NoeDevice, DateCeate, UserCreated, DateModified, UserModified, Flag1, Flag2, Flag3, Flag4, Flag5, dateStart, dateEnd, ShPigiri, DatePigiri FROM         Tbl_Device WHERE     (IDAuto > 0)  '   SET @SQLString =@SQLString + @StrWher  Execute sp_Executesql    @SQLString,  						@ParmDefinition,  						@UserID , 						@TxtNote , 						@GOID , 						@KOLID ,  						@HESABID ,  						@THESABID , 						@ShSerial ,  						@TxtAddress ,  						@NoeDevice ,  						@ShPigiri ,  						@dateStart1 ,  						@dateStart2 ,  						@dateEnd1 ,  						@dateEnd2 , 						@DatePigiri1 , 						@DatePigiri2       END 
GO




/****** Object:  StoredProcedure [dbo].[ACC_Tbl_Device_Insert]    Script Date: 12/12/2017 08:41:25 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ACC_Tbl_Device_Insert]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ACC_Tbl_Device_Insert]
GO


/****** Object:  StoredProcedure [dbo].[ACC_Tbl_Device_Insert]    Script Date: 12/12/2017 08:41:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

   CREATE PROCEDURE [dbo].[ACC_Tbl_Device_Insert]  		@UserID INT, 		@GOID INT, 		@KOLID INT,         @HESABID INT,         @THESABID INT,         @NameHesab nvarchar(255),         @ShSerial nvarchar(255),         @TxtAddress nvarchar(255),         @TxtNote nvarchar(255),         @NoeDevice nvarchar(255),         @dateStart nvarchar(10),         @dateEnd nvarchar(10),         @ShPigiri  nvarchar(255),         @DatePigiri nvarchar(10),         @IDAuto INT=-1 OUTPUT                    AS BEGIN  INSERT INTO Tbl_Device  (  	GOID,   	KOLID,   	HESABID,   	THESABID,   	NameHesab,   	ShSerial,   	TxtAddress,   	NoeDevice,   	dateStart,   	dateEnd,  	DateCeate,   	UserCreated,  	TxtNote,  	ShPigiri,  	DatePigiri  ) VALUES      ( 	@GOID, 	@KOLID, 	@HESABID, 	@THESABID, 	@NameHesab, 	@ShSerial, 	@TxtAddress, 	@NoeDevice, 	@dateStart, 	@dateEnd, 	GETDATE(), 	@UserID, 	@TxtNote, 	@ShPigiri,  	@DatePigiri )   set @IDAuto =@@IDENTITY;  END
GO




/****** Object:  StoredProcedure [dbo].[ACC_Tbl_Device_Load]    Script Date: 12/12/2017 08:41:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ACC_Tbl_Device_Load]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ACC_Tbl_Device_Load]
GO



/****** Object:  StoredProcedure [dbo].[ACC_Tbl_Device_Load]    Script Date: 12/12/2017 08:41:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE PROCEDURE [dbo].[ACC_Tbl_Device_Load] 	 		@UserID INT,         @IDAuto INT           AS BEGIN  SELECT     IDAuto, TxtNote, GOID, KOLID, HESABID, THESABID, NameHesab, ShSerial, TxtAddress, NoeDevice, DateCeate, UserCreated, DateModified, UserModified, Flag1, Flag2, Flag3, Flag4, Flag5,                        dateStart, dateEnd, ShPigiri, DatePigiri FROM         Tbl_Device WHERE     (IDAuto = @IDAuto)   END
GO



/****** Object:  StoredProcedure [dbo].[ACC_Tbl_Device_Update]    Script Date: 12/12/2017 08:41:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ACC_Tbl_Device_Update]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ACC_Tbl_Device_Update]
GO


/****** Object:  StoredProcedure [dbo].[ACC_Tbl_Device_Update]    Script Date: 12/12/2017 08:41:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE PROCEDURE [dbo].[ACC_Tbl_Device_Update] 	 		@UserID INT, 		@GOID INT, 		@KOLID INT,         @HESABID INT,         @THESABID INT,         @NameHesab nvarchar(255),         @ShSerial nvarchar(255),         @TxtAddress nvarchar(max),         @TxtNote nvarchar(max),         @NoeDevice nvarchar(255),         @dateStart nvarchar(10),         @dateEnd nvarchar(10),                 @ShPigiri  nvarchar(255),         @DatePigiri nvarchar(10),         @IDAuto INT           AS BEGIN  UPDATE    Tbl_Device  SET               	GOID = @GOID,  	KOLID = @KOLID,  	HESABID = @HESABID,  	THESABID = @THESABID,  	NameHesab = @NameHesab,  	ShSerial = @ShSerial,  	TxtAddress = @TxtAddress,  	NoeDevice = @NoeDevice,      DateModified = GETDATE(),      UserModified = @UserID,     dateStart=@dateStart,     dateEnd=@dateEnd,     TxtNote=@TxtNote ,     ShPigiri=@ShPigiri,     DatePigiri=@DatePigiri WHERE     (IDAuto = @IDAuto)  END
GO


/****** Object:  StoredProcedure [dbo].[UpdateFlagPrint_Transaction]    Script Date: 12/17/2017 10:28:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UpdateFlagPrint_Transaction]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[UpdateFlagPrint_Transaction]
GO


/****** Object:  StoredProcedure [dbo].[UpdateFlagPrint_Transaction]    Script Date: 12/17/2017 10:28:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE  PROCEDURE [dbo].[UpdateFlagPrint_Transaction]

		@USERID INT,
		@TRAID INT,	
		@FlagPrint BIT,
		@KOLID INT,
		@Hesabid INT,
		@THesabid INT,
		@Date1 NVARCHAR(10),
		@Date2 NVARCHAR(10)
AS

BEGIN

DECLARE @SAid2 INT

	IF (@FlagPrint=0)
		set @FlagPrint=1
	ELSE
		set @FlagPrint=0
		
	IF (@TRAID>0) 
				BEGIN
					UPDATE    [TRANSACTION]
					SET              FlagPrint =   @FlagPrint ,@SAid2=[TRANSACTION].SAID2
					WHERE     (TRAID = @TRAID) 

					
					UPDATE    [TRANSACTION]
					SET              FlagPrint = @FlagPrint
					WHERE     (NOT (KALAID IS NULL)) AND (FieldN2 = @SAid2)
				END	
	ELSE 
		IF 	(@THesabid>0)		
			UPDATE    [TRANSACTION]
			SET              FlagPrint = @FlagPrint
			WHERE     (THESABID = @THesabid) AND (Date1 BETWEEN @Date1 AND @Date2)
	
	ELSE 
		IF 	(@Hesabid>0)		
			UPDATE    [TRANSACTION]
			SET              FlagPrint = @FlagPrint
			WHERE     (HESABID = @Hesabid) AND (Date1 BETWEEN @Date1 AND @Date2)

	ELSE 
		IF 	(@KOLID>0)		
			UPDATE    [TRANSACTION]
			SET              FlagPrint = @FlagPrint
			WHERE     (kolid= @KOLID) AND (Date1 BETWEEN @Date1 AND @Date2)			
					
							  
END








GO



/****** Object:  StoredProcedure [dbo].[QListKolHesab_SanadHesabdari]    Script Date: 01/02/2018 17:57:21 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QListKolHesab_SanadHesabdari]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QListKolHesab_SanadHesabdari]
GO



/****** Object:  StoredProcedure [dbo].[QListKolHesab_SanadHesabdari]    Script Date: 01/02/2018 17:57:21 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QListKolHesab_SanadHesabdari] (@Said int, @Flag1 bit, @FilteerCode int,@FlagNMande bit)  AS 

if(@FlagNMande=0)
		SELECT     KOLID AS ACCID, GOID, KolNumber AS CodeACC, NameHesab AS NameACC, KolNumber AS كد, NameHesab AS عنوان, Mande AS مانده, CASE WHEN (Mande) > 0 THEN N'بد' WHEN (Mande) 
							  < 0 THEN N'بس' ELSE '--' END AS تشخيص
		FROM         dbo.QMande_HesabKol_SanadHesabdary(@Said, @Flag1) AS QMande_HesabKol_SanadHesabdary
		ORDER BY NameHesab
ELSE
	
SELECT     KOLID AS ACCID, GOID, KolNumber AS CodeACC, NameHesab AS NameACC, KolNumber AS كد, NameHesab AS عنوان, '--' AS مانده, '--' AS تشخيص
FROM         T_KolHesab
ORDER BY NameACC


	     



GO



/****** Object:  StoredProcedure [dbo].[QListMoinHesab_SanadHesabdari]    Script Date: 01/02/2018 17:57:27 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QListMoinHesab_SanadHesabdari]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QListMoinHesab_SanadHesabdari]
GO



/****** Object:  StoredProcedure [dbo].[QListMoinHesab_SanadHesabdari]    Script Date: 01/02/2018 17:57:27 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QListMoinHesab_SanadHesabdari](@Said int, @Flag1 bit, @FilteerCode int,@FlagNMande bit) AS 
BEGIN
	
	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)

IF (@FlagNMande=0)
	Set @SQLQuery='SELECT     HESABID AS ACCID, KOLID, HESABNumber AS CodeACC, NameHesabMoin AS NameACC, HESABNumber AS کد, NameHesabMoin AS عنوان, Mande AS مانده, CASE WHEN (mande) 
						  > 0 THEN N''بد'' WHEN (mande) < 0 THEN N''بس'' ELSE ''--'' END AS تشخيص
	FROM         dbo.QMande_HesabMoin_SanadHesabdary(@Said, @Flag1) AS QMande_HesabMoin_SanadHesabdary
				   WHERE 1=1'

ELSE
	Set @SQLQuery='SELECT     HESABID AS ACCID, KolID, HESABNumber AS CodeACC, NameHesab AS NameACC, HESABNumber AS کد, NameHesab AS عنوان, ''--'' AS مانده, ''--'' AS تشخيص
					FROM         T_Heasb
					WHERE     (1 = 1)'
				   

SET @GROUPBy ='  '
SET @OrderBy = ' ORDER BY NameACC '   
	

/*  If (@Flag1<>0)
      Set @SQLQuery = @SQLQuery + ' And (Active IN(0))'*/
     
         
    If (@FilteerCode>0)
         Set @SQLQuery = @SQLQuery + ' And (KOLID = @FilteerCode)'
            
         	
    Set @ParamDefinition = '@said INT,
							@Flag1 bit,
							@FilteerCode int' 


	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@said ,
							@Flag1,
							@FilteerCode 


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END



GO



/****** Object:  StoredProcedure [dbo].[QListTafsiliHesab_SanadHesabdari]    Script Date: 01/02/2018 17:57:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QListTafsiliHesab_SanadHesabdari]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QListTafsiliHesab_SanadHesabdari]
GO


/****** Object:  StoredProcedure [dbo].[QListTafsiliHesab_SanadHesabdari]    Script Date: 01/02/2018 17:57:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE PROCEDURE [dbo].[QListTafsiliHesab_SanadHesabdari](@said int, @Flag1 bit, @FilteerCode INT,@FlagNMande bit) AS 
      

BEGIN
	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
	IF (@FlagNMande=0)
		 SET @SQLQuery = 'SELECT     THESABID AS ACCID, HESABID, THESABNumber AS CodeACC, NameHesabT AS NameACC, THESABNumber AS کد, Onvane AS عنوان, Mande AS مانده, CASE WHEN (Mande) 
							> 0 THEN N''بد'' WHEN (Mande) < 0 THEN N''بس'' ELSE ''--'' END AS تشخيص
							FROM         dbo.QMande_HesabTafsili_SanadHesabdary(@said, @Flag1) AS QMande_HesabTafsili_SanadHesabdary
							WHERE     (1 = 1)'
	ELSE
		SET @SQLQuery = 'SELECT     THESABID AS ACCID, HESABID, THESABNumber AS CodeACC, NameHesabT AS NameACC, THESABNumber AS کد, NameHesabT + N''   '' + ISNULL(Mob, N'''') + N''    '' + ISNULL(Phone1, N'''') 
                      + N''   '' + ISNULL(Phone2, N'''') AS عنوان, ''--'' AS مانده, ''--'' AS تشخیص
FROM         T_T_Heasb
WHERE     (1 = 1)'
		

SET @GROUPBy ='  '
SET @OrderBy = ' ORDER BY NameACC '   
	

    If (@Flag1<>0)
         Set @SQLQuery = @SQLQuery + ' And (Active IN(0))'
     
         
    If (@FilteerCode>0)
         Set @SQLQuery = @SQLQuery + ' And (HESABID = @FilteerCode)'
            
         	
    Set @ParamDefinition = '@said INT,
							@Flag1 bit,
							@FilteerCode int' 


	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@said ,
							@Flag1,
							@FilteerCode 


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END
								

GO




/****** Object:  StoredProcedure [dbo].[SabtKala_Table1_Insert]    Script Date: 01/03/2018 09:52:04 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SabtKala_Table1_Insert]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SabtKala_Table1_Insert]
GO


/****** Object:  StoredProcedure [dbo].[SabtKala_Table1_Insert]    Script Date: 01/03/2018 09:52:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[SabtKala_Table1_Insert]
		
		@AID2 INT,
		@PrimeryOperationID INT,
		@PrimarySAnadID INT,
		@SecondrySanadid INT,
		@LinkTosub NVARCHAR(255),
		@TypeOfFact INT,
		@SecondryOperationCode NVARCHAR(255),
		@Date1 NVARCHAR(20),
		@DateMiladi NVARCHAR(20)

AS

BEGIN

SET NOCOUNT ON;  

DECLARE @FieldN3 DECIMAL(19,4)
DECLARE @DateNow DATETIME


set @DateNow=GETDATE()



/*  ثبت خروج از انبار */
INSERT INTO [TRANSACTION]
                      (Zarib, FieldN7, Maliatkala, Avarez, radif, Tedad2, ID1, FieldN1, FieldN2, SAID2Number, OperationCode, FieldN6, AID, GOKID, KALAID, TextAIDKharid, Sharh, TedadKhA, MabVahedHazine, FieldN3, 
                      Date1, Dateeng, DARBed, PARBes, VKALAID, DateCreate, DateModified, UnitPriceKho)
SELECT     TRANSACTION2.Zarib AS Expr1, TRANSACTION2.FieldN7 AS Expr2, TRANSACTION2.FieldN1 AS Expr3, TRANSACTION2.FieldN2 AS Expr4, TRANSACTION2.radif AS Expr5, 
                      TRANSACTION2.Tedad2 AS Expr6, @PrimeryOperationID AS ex1, @PrimeryOperationID AS ex2, @PrimarySAnadID AS ex3, @SecondrySanadid AS ex4, @SecondryOperationCode AS ex5, 
                      TRANSACTION2.FieldN6 AS Expr7, TRANSACTION2.AID AS Expr8, TRANSACTION2.GOKID AS Expr9, TRANSACTION2.KALAID AS Expr10, TRANSACTION2.TextAIDKharid AS Expr11, 
                      TRANSACTION2.Sharh AS Expr12, TRANSACTION2.Tedad AS Expr13, TRANSACTION2.UnitPrice AS Expr14, Fun_SubAkharinKharid_1.MaxUnitPriceKh * TRANSACTION2.Tedad AS ex6, 
                      @Date1 AS ex7, @DateMiladi AS ex12, NULL AS Ex13, NULL AS Ex14, 1 AS ex15, @DateNow AS fsf, @DateNow AS dfghf, Fun_SubAkharinKharid_1.MaxUnitPriceKh
FROM         TRANSACTION2 LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(1) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.AID = Fun_SubAkharinKharid_1.AID AND TRANSACTION2.KALAID = Fun_SubAkharinKharid_1.KALAID
WHERE     (NOT (TRANSACTION2.KALAID IS NULL)) AND (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.TRAID IS NULL)
ORDER BY Expr5


/* ثبت ورود به انبار*/
DECLARE @AID INT, @GOKID  INT,@KALAID INT

DECLARE KalaID_Cursor CURSOR FOR 

SELECT     AID, GOKID, KALAID
FROM         TRANSACTION2
WHERE     (Date1 = @LinkTosub) AND (NOT (KALAID IS NULL)) AND (TRAID IS NULL)
GROUP BY AID, GOKID, KALAID


OPEN KalaID_Cursor

	FETCH NEXT FROM KalaID_Cursor   
	INTO @AID, @GOKID  ,@KALAID

	WHILE @@FETCH_STATUS = 0  
	BEGIN 

			DECLARE @KALAIDInAID2 int
										
			SELECT    @KALAIDInAID2= COUNT(*)  
			FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1
			WHERE     (AID = @AID2) AND (ID = @KALAID)

			IF (isnull(@KALAIDInAID2,0)=0)
				INSERT INTO T_Kala
								(NameKalaID, AID, GOKID, DateCreate, DateModified, VKalaID)
							VALUES			(@KalaID,@AID2,@GOKID, GETDATE(), GETDATE(), 1)
								
						
INSERT INTO [TRANSACTION]
                      (Zarib, FieldN7, Maliatkala, Avarez, radif, Tedad2, ID1, FieldN1, FieldN2, SAID2Number, OperationCode, FieldN6, AID, GOKID, KALAID, TextAIDKharid, Sharh, TedadVA, MabVahedHazine, FieldN3, 
                      Date1, Dateeng, DARBed, PARBes, VKALAID, DateCreate, DateModified, UnitPriceVo)
SELECT     TRANSACTION2.Zarib, TRANSACTION2.FieldN7, TRANSACTION2.FieldN1, TRANSACTION2.FieldN2, TRANSACTION2.radif, TRANSACTION2.Tedad2, @PrimeryOperationID AS ex1, 
                      @PrimeryOperationID AS ex2, @PrimarySAnadID AS ex3, @SecondrySanadid AS ex4, @SecondryOperationCode AS ex5, TRANSACTION2.FieldN6, @AID2 AS Expr1, TRANSACTION2.GOKID, 
                      @KALAID AS Expr2, TRANSACTION2.TextAIDKharid, TRANSACTION2.Sharh, TRANSACTION2.Tedad, TRANSACTION2.UnitPrice, 
                      Fun_SubAkharinKharid_1.MaxUnitPriceKh * TRANSACTION2.Tedad AS ex6, @Date1 AS ex7, @DateMiladi AS ex12, NULL AS Ex13, NULL AS Ex14, 1 AS ex15, @DateNow AS fsf, @DateNow AS dfghf, 
                      Fun_SubAkharinKharid_1.MaxUnitPriceKh
FROM         TRANSACTION2 LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(1) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.AID = Fun_SubAkharinKharid_1.AID AND TRANSACTION2.KALAID = Fun_SubAkharinKharid_1.KALAID
WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.AID = @AID) AND (TRANSACTION2.KALAID = @KALAID) AND (TRANSACTION2.TRAID IS NULL)
ORDER BY TRANSACTION2.radif


				FETCH NEXT FROM KalaID_Cursor   
				INTO @AID, @GOKID  ,@KALAID
	 	
	END   
	CLOSE KalaID_Cursor;  
	DEALLOCATE KalaID_Cursor; 
	
END







GO



/****** Object:  StoredProcedure [dbo].[EDitKala_Table1_Insert]    Script Date: 01/03/2018 09:52:10 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EDitKala_Table1_Insert]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[EDitKala_Table1_Insert]
GO


/****** Object:  StoredProcedure [dbo].[EDitKala_Table1_Insert]    Script Date: 01/03/2018 09:52:10 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[EDitKala_Table1_Insert]
		@LinkMasterField INT,
		@PrimeryOperationID INT,
		@PrimarySAnadID INT,
		@SecondrySanadid INT,
		@LinkTosub NVARCHAR(255),
		@SecondryOperationCode NVARCHAR(255),
		@Date1 NVARCHAR(20),
		@DateMiladi NVARCHAR(20)

AS

BEGIN

SET NOCOUNT ON;  

DECLARE @DateNow datetime

set @DateNow=GETDATE()



/* حذف کالاهايي حذف شده*/

DELETE FROM [TRANSACTION]
WHERE     (ID1 = @LinkMasterField) AND (NOT (KALAID IN
                          (SELECT     KALAID
                             FROM         TRANSACTION2
                             WHERE     (Date1 = @LinkTosub) AND (TRAID > 0))))

/*
/* بروزرساني خروجي کالا*/
UPDATE    [TRANSACTION]
SET              Tedad2 = TRANSACTION2.Tedad2, ID1 = @PrimeryOperationID, FieldN1 = @PrimeryOperationID, FieldN2 = @PrimarySAnadID, SAID2Number = @SecondrySanadid, 
                      OperationCode = @SecondryOperationCode, Sharh = TRANSACTION2.Sharh, TedadKhA = TRANSACTION2.Tedad, FieldN3 = TRANSACTION2.Tedad * Fun_SubAkharinKharid_1.MaxUnitPriceKh, 
                      UnitPriceKho = Fun_SubAkharinKharid_1.MaxUnitPriceKh, Date1 = @Date1, Dateeng = @DateMiladi, VKALAID = 1, DateModified = @DateNow
FROM         TRANSACTION2 INNER JOIN
                      [TRANSACTION] ON TRANSACTION2.KALAID = [TRANSACTION].KALAID AND TRANSACTION2.AID = [TRANSACTION].AID LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(1) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.AID = Fun_SubAkharinKharid_1.AID AND TRANSACTION2.KALAID = Fun_SubAkharinKharid_1.KALAID
WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.TRAID > 0) AND ([TRANSACTION].TedadKhA > 0)

/* بروزرساني ورودي انبار*/

UPDATE    [TRANSACTION]
SET              Tedad2 = TRANSACTION2.Tedad2, ID1 = @PrimeryOperationID, FieldN1 = @PrimeryOperationID, FieldN2 = @PrimarySAnadID, SAID2Number = @SecondrySanadid, 
                      OperationCode = @SecondryOperationCode, Sharh = TRANSACTION2.Sharh, TedadVA = TRANSACTION2.Tedad, FieldN3 = TRANSACTION2.Tedad * Fun_SubAkharinKharid_1.MaxUnitPriceKh, 
                      UnitPriceVo = Fun_SubAkharinKharid_1.MaxUnitPriceKh, Date1 = @Date1, Dateeng = @DateMiladi, VKALAID = 1, DateModified = @DateNow
FROM         TRANSACTION2 INNER JOIN
                      [TRANSACTION] ON TRANSACTION2.KALAID = [TRANSACTION].KALAID AND TRANSACTION2.AID = [TRANSACTION].AID LEFT OUTER JOIN
                      dbo.Fun_SubAkharinKharid(1) AS Fun_SubAkharinKharid_1 ON TRANSACTION2.AID = Fun_SubAkharinKharid_1.AID AND TRANSACTION2.KALAID = Fun_SubAkharinKharid_1.KALAID
WHERE     (TRANSACTION2.Date1 = @LinkTosub) AND (TRANSACTION2.TRAID > 0) AND ([TRANSACTION].TedadVA > 0) */

	
END

                

GO



/****** Object:  StoredProcedure [dbo].[QListTafsiliHesab_SanadHesabdariSath2SF3]    Script Date: 01/03/2018 13:35:40 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QListTafsiliHesab_SanadHesabdariSath2SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QListTafsiliHesab_SanadHesabdariSath2SF3]
GO


/****** Object:  StoredProcedure [dbo].[QListTafsiliHesab_SanadHesabdariSath2SF3]    Script Date: 01/03/2018 13:35:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE PROCEDURE [dbo].[QListTafsiliHesab_SanadHesabdariSath2SF3](@said int, @Flag1 bit, @FilteerCode INT,@FlagNMande bit) AS 
      

BEGIN
	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
	IF (@FlagNMande=0)
		 SET @SQLQuery = 'SELECT     QMande_HesabTafsili_SanadHesabdarySath2SF3_1.THESABIDSath2 AS ACCID, [T_RelateTafSiliSath1-2].THesabIDSath1, 
							QMande_HesabTafsili_SanadHesabdarySath2SF3_1.THESABNumber AS CodeACC, QMande_HesabTafsili_SanadHesabdarySath2SF3_1.NameHesabT AS NameACC, 
							QMande_HesabTafsili_SanadHesabdarySath2SF3_1.THESABNumber AS کد, QMande_HesabTafsili_SanadHesabdarySath2SF3_1.Onvane AS عنوان, 
							QMande_HesabTafsili_SanadHesabdarySath2SF3_1.Mande AS مانده, CASE WHEN (Mande) > 0 THEN N''بد'' WHEN (Mande) < 0 THEN N''بس'' ELSE ''--'' END AS تشخيص, 
							QMande_HesabTafsili_SanadHesabdarySath2SF3_1.NameHesabT
							FROM         [T_RelateTafSiliSath1-2] INNER JOIN
							dbo.QMande_HesabTafsili_SanadHesabdarySath2SF3(@said, @Flag1) AS QMande_HesabTafsili_SanadHesabdarySath2SF3_1 ON 
							[T_RelateTafSiliSath1-2].THesabIDSath2 = QMande_HesabTafsili_SanadHesabdarySath2SF3_1.THESABIDSath2'
	ELSE
		SET @SQLQuery = 'SELECT     THESABID AS ACCID, T_T_HeasbSath2.THESABID, THESABNumber AS CodeACC, NameHesabT AS NameACC, THESABNumber AS کد, NameHesabT + N''   '' + ISNULL(Mob, N'''') + N''    '' + ISNULL(Phone1, N'''') 
                      + N''   '' + ISNULL(Phone2, N'''') AS عنوان, ''--'' AS مانده, ''--'' AS تشخیص
		                 from T_T_HeasbSath2 INNER JOIN [T_RelateTafSiliSath1-2] ON T_T_HeasbSath2.THESABID = [T_RelateTafSiliSath1-2].THesabIDSath2
WHERE     (1 = 1)'
		

SET @GROUPBy ='  '
SET @OrderBy = ' ORDER BY NameACC '   
	

    If (@Flag1<>0)
         Set @SQLQuery = @SQLQuery + ' And (Active IN(0))'
     
         
    If (@FilteerCode>0)
         Set @SQLQuery = @SQLQuery + ' And ([T_RelateTafSiliSath1-2].THesabIDSath1 = @FilteerCode)'
            
         	
    Set @ParamDefinition = '@said INT,
							@Flag1 bit,
							@FilteerCode int' 


	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@said ,
							@Flag1,
							@FilteerCode 


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END
								

GO





/****** Object:  StoredProcedure [dbo].[PFrm_Khadamat_SubItemInsert_SF8]    Script Date: 01/21/2018 03:48:48 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_Khadamat_SubItemInsert_SF8]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PFrm_Khadamat_SubItemInsert_SF8]
GO


/****** Object:  StoredProcedure [dbo].[PFrm_Khadamat_SubItemInsert_SF8]    Script Date: 01/21/2018 03:48:48 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[PFrm_Khadamat_SubItemInsert_SF8]
		@CodGroup INT,
        @IndexKalaID INT,
        @LinkSub NVARCHAR(255)
AS
BEGIN
	
DECLARE @CodKala INT
DECLARE @RadifRec INT
DECLARE  @ChDblKala int


SELECT     @CodKala=CodKala
FROM         T_Defult
WHERE     (CodGroup = @CodGroup) AND (OPTIONID = @IndexKalaID) AND (GROUPID = 8)

SELECT     @RadifRec=COUNT(*) 
FROM         TRANSACTION2
WHERE     (Date1 = @LinkSub)

SELECT     @ChDblKala=COUNT(*) 
FROM         TRANSACTION2
WHERE     (Date1 = @LinkSub) AND (KALAID = @CodKala)


IF (ISNULL(@ChDblKala,0)=0)
	INSERT INTO TRANSACTION2
						  (radif, KALAID, TextKalaName, Tedad, UnitPrice, Date1, OperType)
	SELECT     ISNULL(@RadifRec, 0) + 1 AS Radif, NAMEKHID, Name, 1 AS Tedad, UnitPrice, @LinkSub AS Date1, 5 AS OperType
	FROM         T_NameKhad
	WHERE     (NAMEKHID = @CodKala)
else
	UPDATE    TRANSACTION2
	SET              Tedad =isnull(TRANSACTION2.tedad,0) +1
	WHERE     (KALAID = @CodKala) AND (Date1 = @LinkSub)
END

GO


/****** Object:  StoredProcedure [dbo].[PFrm_Khadamat_CALC_SF8]    Script Date: 01/21/2018 03:48:07 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_Khadamat_CALC_SF8]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PFrm_Khadamat_CALC_SF8]
GO



/****** Object:  StoredProcedure [dbo].[PFrm_Khadamat_CALC_SF8]    Script Date: 01/21/2018 03:48:07 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE  PROCEDURE [dbo].[PFrm_Khadamat_CALC_SF8]
		
		@Userid INT,	
		@TxtLinkTosub NVARCHAR(255)

AS
BEGIN
	

 DECLARE   @Tedad DECIMAL(19,4) 
 DECLARE   @MabFactor DECIMAL(19,4) 

        
SELECT      @Tedad=SUM(Tedad),@MabFactor= SUM(Tedad * UnitPrice)  
FROM         TRANSACTION2
WHERE     (date1 = @TxtLinkTosub)



SELECT     @Tedad AS Tedad, @MabFactor AS MabFactor		  

END






GO


/****** Object:  StoredProcedure [dbo].[PFrm_Khadamat_NewShGhabz_SF8]    Script Date: 01/21/2018 03:48:40 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_Khadamat_NewShGhabz_SF8]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PFrm_Khadamat_NewShGhabz_SF8]
GO


/****** Object:  StoredProcedure [dbo].[PFrm_Khadamat_NewShGhabz_SF8]    Script Date: 01/21/2018 03:48:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE  PROCEDURE [dbo].[PFrm_Khadamat_NewShGhabz_SF8]
	@DateKh NVARCHAR(10),
	@NewNumkber INT =0 OUTPUT
AS

BEGIN


		SELECT     @NewNumkber=isnull(MAX(ShGhabz),0)+1 
		FROM         T_DKhad
		WHERE     (DateKh = @DateKh) 

							  
END







GO




/****** Object:  StoredProcedure [dbo].[PFrm_Khadamat_LoadEshterak_SF8]    Script Date: 01/21/2018 03:48:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_Khadamat_LoadEshterak_SF8]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PFrm_Khadamat_LoadEshterak_SF8]
GO


/****** Object:  StoredProcedure [dbo].[PFrm_Khadamat_LoadEshterak_SF8]    Script Date: 01/21/2018 03:48:33 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





Create  PROCEDURE [dbo].[PFrm_Khadamat_LoadEshterak_SF8]
	@THESABID int
AS

BEGIN

SELECT     T_T_Heasb.*
FROM         T_T_Heasb
WHERE     (THESABID = @THESABID)
							  
END







GO




/****** Object:  StoredProcedure [dbo].[PFrm_Khadamat_ExistShomreinLoad_SF8]    Script Date: 01/21/2018 03:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_Khadamat_ExistShomreinLoad_SF8]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PFrm_Khadamat_ExistShomreinLoad_SF8]
GO

/****** Object:  StoredProcedure [dbo].[PFrm_Khadamat_ExistShomreinLoad_SF8]    Script Date: 01/21/2018 03:48:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE  PROCEDURE [dbo].[PFrm_Khadamat_ExistShomreinLoad_SF8]
	@LinkMasterField INT,
	@NextPrevious INT,
	/* Next(1) Previous(2)*/
	@ExistShomreinLoad INT =0 OUTPUT
AS

BEGIN

if (@LinkMasterField=-1)
		SELECT    @ExistShomreinLoad= MAX(DKHID) 
	FROM         T_DKhad
ELSE
	if (@NextPrevious=1) 
			SELECT     TOP (1) @ExistShomreinLoad=isnull(DKHID,0)
			FROM         T_DKhad
			WHERE     (DKHID > @LinkMasterField)
			ORDER BY DKHID
	ELSE		
			SELECT     TOP (1) @ExistShomreinLoad=isnull(DKHID,0)
			FROM         T_DKhad
			WHERE     (DKHID < @LinkMasterField)
			ORDER BY DKHID DESC			  
END







GO






/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_T_HeasbSath2]    Script Date: 01/30/2018 09:42:45 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_T_HeasbSath2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_T_HeasbSath2]
GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_T_HeasbSath2]    Script Date: 01/30/2018 09:42:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_T_HeasbSath2] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 



	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_T_HeasbSath2] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_T_HeasbSath2  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_T_HeasbSath2  ' + CHAR(10)
												+' (THESABID, THESABNumber, DateCreate, DateModified,  NameHesabT, TextName,  Address, Address2, Note, Telex, Shahr, Ostan, CodPosti, CodEghtesdi, Keshvar, BankHesab, Mob, ' + CHAR(10) 
												+'Websit, Phone1, Phone2, Email, VMo, VFo, VKh, VSa, VDa, CurrencyDefault, Active, SaghfBed, SaghfBes, Commision,  UnitPriceFo, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, ' + CHAR(10) 
												+'FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, Balance)' + CHAR(10)
												+' SELECT   T_T_HeasbSath2.THESABID, THESABNumber, DateCreate, DateModified, NameHesabT, TextName, Address, Address2, Note, Telex, Shahr, Ostan, CodPosti, CodEghtesdi, Keshvar, BankHesab, Mob, ' + CHAR(10)
												+' Websit, Phone1, Phone2, Email, VMo, VFo, VKh, VSa, VDa, CurrencyDefault, Active, SaghfBed, SaghfBes, Commision, UnitPriceFo, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, ' + CHAR(10)	
												+' FieldT2 , FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, Balance ' + CHAR(10)	
												+' FROM dbo.T_T_HeasbSath2 LEFT OUTER JOIN ' + CHAR(10)
												+' (SELECT     THESABID, SUM(PARBes - DARBed) AS Mande ' + CHAR(10)
												+' FROM         [TRANSACTION] GROUP BY THESABID) AS drvMande ON T_T_HeasbSath2.THESABID = drvMande.THESABID ' + CHAR(10)
												+' WHERE     (CASE WHEN isnull(MANDE, 0) <> 0 THEN 0 ELSE Active END = 0)    SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_T_HeasbSath2] OFF '
												+ CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.[T_RelateTafSiliSath1-2]  ' + CHAR(10)
												+' (THesabIDSath1, THesabIDSath2) 	SELECT     THesabIDSath1, THesabIDSath2 FROM         [T_RelateTafSiliSath1-2] '

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

	
	SELECT     THesabIDSath1, THesabIDSath2
FROM         [T_RelateTafSiliSath1-2]
	

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_T_Heasb]    Script Date: 01/30/2018 09:42:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_T_Heasb]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_T_Heasb]
GO


/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_T_Heasb]    Script Date: 01/30/2018 09:42:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_T_Heasb] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 



	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_T_Heasb] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_T_Heasb  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_T_Heasb  ' + CHAR(10)
												+' (THESABID, THESABNumber, DateCreate, DateModified, HESABID, NameHesabT, TextName, GOID, KolID, Address, Address2, Note, Telex, Shahr, Ostan, CodPosti, CodEghtesdi, Keshvar, BankHesab, Mob, ' + CHAR(10) 
												+'Websit, Phone1, Phone2, Email, VMo, VFo, VKh, VSa, VDa, CurrencyDefault, Active, SaghfBed, SaghfBes, Commision, KolID2, HesabID2, THesbID2, UnitPriceFo, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, ' + CHAR(10) 
												+'FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, Balance)' + CHAR(10)
												+' SELECT     T_T_Heasb.THESABID, T_T_Heasb.THESABNumber, T_T_Heasb.DateCreate, T_T_Heasb.DateModified, T_T_Heasb.HESABID, T_T_Heasb.NameHesabT, T_T_Heasb.TextName, T_T_Heasb.GOID, ' + CHAR(10)
												+'  T_T_Heasb.KolID, T_T_Heasb.Address, T_T_Heasb.Address2, T_T_Heasb.Note, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, ' + CHAR(10)
												+'  T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_T_Heasb.VMo, T_T_Heasb.VFo, T_T_Heasb.VKh, ' + CHAR(10)
												+'  T_T_Heasb.VSa, T_T_Heasb.VDa, T_T_Heasb.CurrencyDefault,  T_T_Heasb.Active , T_T_Heasb.SaghfBed, T_T_Heasb.SaghfBes, ' + CHAR(10)
												+'  T_T_Heasb.Commision, T_T_Heasb.KolID2, T_T_Heasb.HesabID2, T_T_Heasb.THesbID2, T_T_Heasb.UnitPriceFo, T_T_Heasb.FieldN1, T_T_Heasb.FieldN2, T_T_Heasb.FieldN3, ' + CHAR(10)
												+'  T_T_Heasb.FieldN4, T_T_Heasb.FieldT1, T_T_Heasb.FieldT2, T_T_Heasb.FieldT3, T_T_Heasb.FieldT4, T_T_Heasb.Flag1, T_T_Heasb.Flag2, T_T_Heasb.Flag3, T_T_Heasb.Flag4, T_T_Heasb.Flag5, ' + CHAR(10)
												+'  T_T_Heasb.Flag6, T_T_Heasb.Flag7, T_T_Heasb.Flag8, T_T_Heasb.Balance ' + CHAR(10)	
												+' FROM         T_T_Heasb LEFT OUTER JOIN ' + CHAR(10)
												+' (SELECT     THESABID, SUM(PARBes - DARBed) AS Mande ' + CHAR(10)
												+' FROM         [TRANSACTION] GROUP BY THESABID) AS drvMande ON T_T_Heasb.THESABID = drvMande.THESABID ' + CHAR(10)
												+' WHERE     (CASE WHEN isnull(MANDE, 0) <> 0 THEN 0 ELSE Active END = 0)   SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_T_Heasb] OFF'

    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END



              



GO













/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListBarKHarid]    Script Date: 02/19/2018 11:36:39 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListBarKHarid]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListBarKHarid]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListBarKHarid]    Script Date: 02/19/2018 11:36:39 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



         CREATE PROCEDURE [dbo].[QSubPFrm_ListBarKHarid] AS 
         
         
SELECT     T_Dar8.DARID8, T_Dar8.HesabID, T_Dar8.GOID, T_Dar8.KOLID, T_Dar8.MOINID, T_Dar8.THESABID, T_Dar8.TextName, T_Dar8.TextName1, T_Dar8.TextNameT, T_Dar8.TextNameT AS Expr1, 
                      T_Dar8.NumberKH, T_Dar8.DateKh, T_Dar8.TextShSA, T_Dar8.NumSID2, T_Dar8.Sharh, T_Dar8.DATE2, T_Dar8.FieldN1, T_Dar8.FieldN2, T_Dar8.FieldN3, T_Dar8.FieldN4, T_Dar8.FieldT1, 
                      T_Dar8.FieldT3, T_Dar8.Flag1, T_Dar8.Flag2, T_Dar8.DARID8Numbr, T_Dar8.PrimarySaid2, T_SA2.SAID2Number, dbo.HesabName(T_Dar8.KOLID, T_Dar8.HesabID, T_Dar8.THESABID) 
                      AS namehesab, CONVERT(int, T_Dar8.DARID8Numbr) AS Expr2, T_Dar8.Takhfif, T_Dar8.Maliyat, T_Dar8.Avarez, F_MabHazine_KharidBarKHarid_1.MabHazine, drVTra.FieldN3 AS MabFact, 
                      CONVERT(float, drVTra.TedadBarKh) AS TedadBarKh
FROM         T_Dar8 INNER JOIN
                      T_SA2 ON T_Dar8.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
                          (SELECT     DARID8, SUM(ABS(FieldN3)) AS FieldN3, SUM(TedadBarKh) AS TedadBarKh
                             FROM         [TRANSACTION]
                             GROUP BY DARID8) AS drVTra ON T_Dar8.DARID8 = drVTra.DARID8 LEFT OUTER JOIN
                      dbo.F_MabHazine_KharidBarKHarid() AS F_MabHazine_KharidBarKHarid_1 ON T_Dar8.DARID8 = F_MabHazine_KharidBarKHarid_1.FactID
ORDER BY Expr2 DESC


GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListBarForoosh]    Script Date: 02/22/2018 09:11:17 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListBarForoosh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListBarForoosh]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListBarForoosh]    Script Date: 02/22/2018 09:11:17 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



         CREATE PROCEDURE [dbo].[QSubPFrm_ListBarForoosh] AS 
SELECT     T_Par7.PARID7, T_Par7.HesabID, T_Par7.GOID, T_Par7.KOLID, T_Par7.MOINID, T_Par7.THESABID, T_Par7.TextName, T_Par7.TextName1, T_Par7.TextNameT, T_Par7.NumberFO, T_Par7.DateFo, 
                      T_Par7.TextShSA, T_Par7.NumSID2, T_Par7.Sharh, T_Par7.FieldN1, T_Par7.FieldN2, T_Par7.FieldN3, T_Par7.FieldN4, T_Par7.FieldT1, T_Par7.FieldT2, T_Par7.FieldT3, T_Par7.FieldT3 AS Expr1, 
                      T_Par7.FieldT4, T_Par7.Flag1, T_Par7.Flag2, T_Par7.Flag3, T_Par7.Flag4, T_Par7.PARID7Number, T_Par7.THESABIDAmel, T_Par7.PrimarySaid2, T_SA2.SAID2Number, 
                      dbo.HesabName(T_Par7.KOLID, T_Par7.HesabID, T_Par7.THESABID) AS namehesab, CONVERT(int, T_Par7.PARID7Number) AS Expr2, T_Par7.Avarez, T_Par7.Maliyat, T_Par7.Takhfif, 
                      DrvMabFact.FieldN3 AS MabFact, CONVERT(float, DrvMabFact.TedadBarFo) AS TedadBarFo, F_MabHazine_BarForosh_1.MabHazine
FROM         T_Par7 INNER JOIN
                      T_SA2 ON T_Par7.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
                          (SELECT     PARID7, SUM(ABS(FieldN3)) AS FieldN3, SUM(TedadBarFo) AS TedadBarFo
                             FROM         [TRANSACTION]
                             GROUP BY PARID7) AS DrvMabFact ON T_Par7.PARID7 = DrvMabFact.PARID7 LEFT OUTER JOIN
                      dbo.F_MabHazine_BarForosh() AS F_MabHazine_BarForosh_1 ON T_Par7.PARID7 = F_MabHazine_BarForosh_1.FactID
ORDER BY Expr2 DESC


GO





/****** Object:  StoredProcedure [dbo].[QSub_SubPfrm_ListSanadHesabdary]    Script Date: 03/06/2018 11:30:23 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSub_SubPfrm_ListSanadHesabdary]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSub_SubPfrm_ListSanadHesabdary]
GO



/****** Object:  StoredProcedure [dbo].[QSub_SubPfrm_ListSanadHesabdary]    Script Date: 03/06/2018 11:30:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[QSub_SubPfrm_ListSanadHesabdary]  
			@SAID2 INT
    AS
    
SELECT     [TRANSACTION].SAID2, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab, [TRANSACTION].PARBes, [TRANSACTION].DARBed, 
                      [TRANSACTION].CurRate, [TRANSACTION].Sharh, [TRANSACTION].radif, Currency.Title, [TRANSACTION].RadifSAID2, [TRANSACTION].PARBesCur + [TRANSACTION].DARBedCur AS MabArz
FROM         [TRANSACTION] LEFT OUTER JOIN
                      Currency ON [TRANSACTION].CurRef = Currency.CurrencyID
WHERE     ([TRANSACTION].SAID2 = @SAID2)
ORDER BY [TRANSACTION].RadifSAID2
                      
GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_NameKala]    Script Date: 09/25/2018 10:49:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_NameKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_NameKala]
GO
/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_NameKala]    Script Date: 09/25/2018 10:49:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_NameKala] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_NameKala] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_NameKala  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_NameKala  ' + CHAR(10)
												+' (ID, KalaNumber, DateCreate, DateModified, NameKala1, Active, VAHED1, VAHED2, MinPrice, MaxPrice, Zarib, MinMogoody, MaxMogoody, BarCode, Note, UnitPrice1, UnitPrice2, UnitPrice3, ' + CHAR(10) 
												+' UnitPrice4, UnitPrice5, UnitPrice6, DarsadAmel1, DarsadAmel2, DarsadAmel3, DarsadAmel4, DarsadAmel5, DarsadAmel6, DarsadKasr1, DarsadKasr2, DarsadKasr3, DarsadKasr4, DarsadKasr5, ' + CHAR(10) 
												+' DarsadKasr6, Currency1, Currency2, Currency3, Currency4, Currency5, Currency6, Model, UnitPrice, Unitfo, VKalaID, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, ' + CHAR(10) 
												+' Flag2, Flag3, Flag4, TedadBal) ' + CHAR(10) 
												+' SELECT ID, KalaNumber, DateCreate, DateModified, NameKala1, Active, VAHED1, VAHED2, MinPrice, MaxPrice, Zarib, MinMogoody, MaxMogoody, BarCode, Note, UnitPrice1, UnitPrice2, UnitPrice3,' + CHAR(10) 
												+' UnitPrice4, UnitPrice5, UnitPrice6, DarsadAmel1, DarsadAmel2, DarsadAmel3, DarsadAmel4, DarsadAmel5, DarsadAmel6, DarsadKasr1, DarsadKasr2, DarsadKasr3, DarsadKasr4, DarsadKasr5, ' + CHAR(10) 
												+' DarsadKasr6, Currency1, Currency2, Currency3, Currency4, Currency5, Currency6, Model, UnitPrice, Unitfo, VKalaID, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, ' + CHAR(10) 
												+'  Flag2, Flag3, Flag4, TedadBal ' + CHAR(10) 
												+' FROM T_NameKala WHERE ( Active=0 And (ISNULL(FlagNoeKala, 0) = 0)) SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_NameKala] OFF ' + CHAR(10) 


	SET @SQLQuery1 = @SQLQuery1 + CHAR(10)+' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_RelateKala] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_RelateKala  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_RelateKala  ' + CHAR(10)
												+' (Kalaid2, KalaidRelate, NameKala2, ZaribKala)' + CHAR(10) 
												+' SELECT     Kalaid2, KalaidRelate, NameKala2, ZaribKala ' + CHAR(10) 
												+' FROM         T_RelateKala INNER JOIN ' + CHAR(10) 
												+' T_NameKala ON T_RelateKala.KalaidRelate = T_NameKala.ID ' + CHAR(10) 
												+' WHERE     (T_NameKala.Active = 0) AND (ISNULL(T_NameKala.FlagNoeKala, 0) = 0) '+ @StrDes + '.[dbo].[T_RelateKala] OFF ' + CHAR(10) 






    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

		
    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END


GO



/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Kala]    Script Date: 09/25/2018 10:49:27 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_Kala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_Kala]
GO


/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_Kala]    Script Date: 09/25/2018 10:49:27 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_Kala] 

@StrDes NVARCHAR(255),	
@EnteghalMande bit,
@OnlyKalaMandeDare bit

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 


												

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Kala] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_Kala  '
	
	IF (@EnteghalMande=0)
		SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_Kala  ' + CHAR(10)
											+' (   KALAID, NameKalaID, AID, KALANumbr, GOKID, DateCreate, DateModified, VKalaID, UnitPrice, Unitfo, Unit, Textname, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, ' + CHAR(10) 
											+' Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8 )' + CHAR(10) 
											+' SELECT     T_Kala.KALAID, T_Kala.NameKalaID, T_Kala.AID, T_Kala.KALANumbr, T_Kala.GOKID, T_Kala.DateCreate, T_Kala.DateModified, T_Kala.VKalaID, T_Kala.UnitPrice, T_Kala.Unitfo, T_Kala.Unit, T_Kala.Textname, T_Kala.FieldN1, T_Kala.FieldN2, T_Kala.FieldN3, T_Kala.FieldN4, T_Kala.FieldT1, T_Kala.FieldT2, T_Kala.FieldT3, T_Kala.FieldT4, ' + char(10)
											+ ' T_Kala.Flag1, T_Kala.Flag2, T_Kala.Flag3, T_Kala.Flag4, T_Kala.Flag5, T_Kala.Flag6, T_Kala.Flag7, T_Kala.Flag8 ' + char(10)
											+' FROM         T_Kala INNER JOIN T_NameKala ON T_Kala.NameKalaID = T_NameKala.ID WHERE    ( (T_NameKala.Active = 0) And (ISNULL(T_NameKala.FlagNoeKala, 0) = 0)) ' + char(10)
											+' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Kala] OFF ' + CHAR(10) 
	ELSE
		
		IF (@OnlyKalaMandeDare=0)
				SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_Kala  ' + CHAR(10)
											+' (   Mojodi, AID, NameKalaID, GOKID, UnitPrice,VKalaID,DateCreate, DateModified, ' + CHAR(10) 
											+'  KALANumbr, Unitfo, Unit, Textname, T_Kala.FieldN1, FieldN2, T_Kala.FieldN3, FieldN4, FieldT1, FieldT2,  ' + CHAR(10) 
											+' FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, KALAID)' + CHAR(10) 
											+' SELECT     DrvKala.SumTedad, T_Kala.AID, T_Kala.NameKalaID, T_Kala.GOKID, DrvKala.Max_UnitPriceKh, T_Kala.VKalaID, T_Kala.DateCreate, T_Kala.DateModified, T_Kala.KALANumbr, T_Kala.Unitfo, ' + CHAR(10)
											+' T_Kala.Unit, T_Kala.Textname, T_Kala.FieldN1, T_Kala.FieldN2, T_Kala.FieldN3, T_Kala.FieldN4, T_Kala.FieldT1, T_Kala.FieldT2, T_Kala.FieldT3, T_Kala.FieldT4, T_Kala.Flag1, T_Kala.Flag2, ' + CHAR(10)
											+' T_Kala.Flag3, T_Kala.Flag4, T_Kala.Flag5, T_Kala.Flag6, T_Kala.Flag7, T_Kala.Flag8, T_Kala.KALAID ' + CHAR(10)
											+' FROM         (SELECT     FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, ' + CHAR(10)
											+' FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, SUM(CONVERT(float, SubTransactionKala_1_1.TedadVarde - SubTransactionKala_1_1.TedadSadre)) AS SumTedad, ' + CHAR(10)
											+' FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, MAX(Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS Max_UnitPriceKh, MAX(FunInfoKalaAll_1.UnitPrice1) AS MaxUnitPrice1, ' + CHAR(10)
											+' MAX(FunInfoKalaAll_1.UnitPrice2) AS MaxUnitPrice2, MAX(FunInfoKalaAll_1.UnitPrice3) AS MaxUnitPrice3, MAX(FunInfoKalaAll_1.UnitPrice4) AS MaxUnitPrice4, ' + CHAR(10)
											+' MAX(FunInfoKalaAll_1.UnitPrice5) AS MaxUnitPrice5, MAX(FunInfoKalaAll_1.UnitPrice6) AS MaxUnitPrice6, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, ' + CHAR(10)
											+' FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.AID ' + CHAR(10)
											+' FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 RIGHT OUTER JOIN ' + CHAR(10)
											+' dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid LEFT OUTER JOIN ' + CHAR(10)
											+' (SELECT     KALAID, GOKID, AID, SUM(TedadVarde) AS TedadVarde, SUM(TedadSadre) AS TedadSadre,LinkKalaAid ' + CHAR(10)
											+' FROM         View_SubTransactionKala AS SubTransactionKala_1 ' + CHAR(10)
											+' GROUP BY KALAID, GOKID, AID,LinkKalaAid) AS SubTransactionKala_1_1 ON FunInfoKalaAll_1.LinkKalaAid = SubTransactionKala_1_1.LinkKalaAid   ' + CHAR(10)
											+' ' + CHAR(10)
											+' GROUP BY FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1,  ' + CHAR(10)
											+' FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, ' + CHAR(10)
											+' FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.AID ' + CHAR(10)
											+') AS DrvKala INNER JOIN ' + CHAR(10)
											+' T_Kala ON DrvKala.ID = T_Kala.NameKalaID AND DrvKala.AID = T_Kala.AID ' + CHAR(10)
											+' WHERE     (DrvKala.Active = 0) AND (T_Kala.VKalaID = 1) ' + char(10)										
											+ 'ORDER BY T_Kala.NameKalaID ' + char(10)
											+ 'SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Kala] OFF ' + CHAR(10) 
			ELSE
				SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_Kala  ' + CHAR(10)
											+' (   Mojodi, AID, NameKalaID, GOKID, UnitPrice,VKalaID,DateCreate, DateModified, ' + CHAR(10) 
											+'  KALANumbr, Unitfo, Unit, Textname, T_Kala.FieldN1, FieldN2, T_Kala.FieldN3, FieldN4, FieldT1, FieldT2,  ' + CHAR(10) 
											+' FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, KALAID)' + CHAR(10) 
											+' SELECT     DrvKala.SumTedad, T_Kala.AID, T_Kala.NameKalaID, T_Kala.GOKID, DrvKala.Max_UnitPriceKh, T_Kala.VKalaID, T_Kala.DateCreate, T_Kala.DateModified, T_Kala.KALANumbr, T_Kala.Unitfo, ' + CHAR(10)
											+' T_Kala.Unit, T_Kala.Textname, T_Kala.FieldN1, T_Kala.FieldN2, T_Kala.FieldN3, T_Kala.FieldN4, T_Kala.FieldT1, T_Kala.FieldT2, T_Kala.FieldT3, T_Kala.FieldT4, T_Kala.Flag1, T_Kala.Flag2, ' + CHAR(10)
											+' T_Kala.Flag3, T_Kala.Flag4, T_Kala.Flag5, T_Kala.Flag6, T_Kala.Flag7, T_Kala.Flag8, T_Kala.KALAID ' + CHAR(10)
											+' FROM         (SELECT     FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, ' + CHAR(10)
											+' FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, SUM(CONVERT(float, SubTransactionKala_1_1.TedadVarde - SubTransactionKala_1_1.TedadSadre)) AS SumTedad, ' + CHAR(10)
											+' FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, MAX(Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS Max_UnitPriceKh, MAX(FunInfoKalaAll_1.UnitPrice1) AS MaxUnitPrice1, ' + CHAR(10)
											+' MAX(FunInfoKalaAll_1.UnitPrice2) AS MaxUnitPrice2, MAX(FunInfoKalaAll_1.UnitPrice3) AS MaxUnitPrice3, MAX(FunInfoKalaAll_1.UnitPrice4) AS MaxUnitPrice4, ' + CHAR(10)
											+' MAX(FunInfoKalaAll_1.UnitPrice5) AS MaxUnitPrice5, MAX(FunInfoKalaAll_1.UnitPrice6) AS MaxUnitPrice6, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, ' + CHAR(10)
											+' FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.AID ' + CHAR(10)
											+' FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 RIGHT OUTER JOIN ' + CHAR(10)
											+' dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid LEFT OUTER JOIN ' + CHAR(10)
											+' (SELECT     KALAID, GOKID, AID, SUM(TedadVarde) AS TedadVarde, SUM(TedadSadre) AS TedadSadre,LinkKalaAid ' + CHAR(10)
											+' FROM         View_SubTransactionKala AS SubTransactionKala_1 ' + CHAR(10)
											+' GROUP BY KALAID, GOKID, AID,LinkKalaAid) AS SubTransactionKala_1_1 ON FunInfoKalaAll_1.LinkKalaAid = SubTransactionKala_1_1.LinkKalaAid   ' + CHAR(10)
											+' ' + CHAR(10)
											+' GROUP BY FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1,  ' + CHAR(10)
											+' FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, ' + CHAR(10)
											+' FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.AID ' + CHAR(10)
											+') AS DrvKala INNER JOIN ' + CHAR(10)
											+' T_Kala ON DrvKala.ID = T_Kala.NameKalaID AND DrvKala.AID = T_Kala.AID ' + CHAR(10)
											+' WHERE     (DrvKala.Active = 0) AND (DrvKala.SumTedad <> 0) AND (T_Kala.VKalaID = 1) ' + char(10)										
											+' ORDER BY T_Kala.NameKalaID ' + char(10)
											+' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_Kala] OFF ' + CHAR(10) 
											+'	DELETE FROM '+ @StrDes + '.[dbo].T_NameKala	 FROM	'+ @StrDes + '.[dbo].T_NameKala LEFT OUTER JOIN ' + CHAR(10)								
											+ @StrDes + '.[dbo].T_Kala ON ' + @StrDes + '.[dbo].T_NameKala.ID = ' + @StrDes + '.[dbo].T_Kala.NameKalaID WHERE     (' + @StrDes + '.[dbo].T_Kala.KALAID IS NULL) ' + CHAR(10) 
                       						+'DELETE FROM '+ @StrDes + '.[dbo].T_RelateKala FROM '+ @StrDes + '.[dbo].T_RelateKala LEFT OUTER JOIN  '+ @StrDes + '.[dbo].T_Kala ON '+ @StrDes + '.[dbo].T_RelateKala.KalaidRelate = '+ @StrDes + '.[dbo].T_Kala.NameKalaID ' + CHAR(10) 
											+'WHERE     ('+ @StrDes + '.[dbo].T_Kala.KALAID IS NULL)'


    Set @ParamDefinition = '@StrDes NVARCHAR(255),@EnteghalMande bit ' 	

		
    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes,
						    @EnteghalMande 

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END
         
                  
                  


                












GO







/****** Object:  StoredProcedure [dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_MinimumRep1]    Script Date: 03/25/2018 12:30:49 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_MinimumRep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_MinimumRep1]
GO



/****** Object:  StoredProcedure [dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_MinimumRep1]    Script Date: 03/25/2018 12:30:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  CREATE PROCEDURE [dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_MinimumRep1]            
  	

  	
  	AS     
  	
  	
SELECT     SubTransactionKala_1.KALAID, SubTransactionKala_1.AID, SubTransactionKala_1.GOKID, SUM(SubTransactionKala_1.Tedad) AS SumTedad, MAX(ISNULL(FunInfoKalaAll_1.MinMogoody, 0)) 
                      AS MinMogoody, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) AS Mande, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.KalaNumber, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.BarCode
FROM         View_SubTransactionKala AS SubTransactionKala_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON SubTransactionKala_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid
WHERE     (FunInfoKalaAll_1.VKalaID = 1)
GROUP BY SubTransactionKala_1.KALAID, SubTransactionKala_1.AID, SubTransactionKala_1.GOKID, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameVahed1, 
                      FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.BarCode
HAVING      (MAX(ISNULL(FunInfoKalaAll_1.MinMogoody, 0)) - SUM(SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre) > 0)
ORDER BY FunInfoKalaAll_1.NameKala1           


GO






/****** Object:  StoredProcedure [dbo].[Proc_OperRoozane_Exist_Hesab_ZirShakhe]    Script Date: 04/01/2018 12:22:44 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Proc_OperRoozane_Exist_Hesab_ZirShakhe]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Proc_OperRoozane_Exist_Hesab_ZirShakhe]
GO



/****** Object:  StoredProcedure [dbo].[Proc_OperRoozane_Exist_Hesab_ZirShakhe]    Script Date: 04/01/2018 12:22:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



Create  PROCEDURE [dbo].[Proc_OperRoozane_Exist_Hesab_ZirShakhe]
		
@KolID INT,
@HESABID INT,
@Result INT OUTPUT

AS

BEGIN

			IF (Not(@HESABID IS NULL ))
					SELECT     @Result=COUNT(*) 
					FROM         T_T_Heasb
					WHERE     (HESABID = @HESABID)
			ELSE
				IF (Not(@KolID IS NULL ))
					SELECT     @Result=COUNT(*) 
					FROM         T_Heasb
					WHERE     (KolID = @KolID)
				
		


END







GO



/****** Object:  StoredProcedure [dbo].[Proc_OperRoozane_ValidateHesab]    Script Date: 04/01/2018 12:22:39 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Proc_OperRoozane_ValidateHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Proc_OperRoozane_ValidateHesab]
GO



/****** Object:  StoredProcedure [dbo].[Proc_OperRoozane_ValidateHesab]    Script Date: 04/01/2018 12:22:39 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



Create  PROCEDURE [dbo].[Proc_OperRoozane_ValidateHesab]
		
@GOID INT,
@KolID INT,
@HESABID INT,
@THESABID INT,
@Result INT OUTPUT

AS

BEGIN
		IF (Not(@THESABID IS NULL ))
			SELECT     @Result=COUNT(*) 
			FROM         T_T_Heasb
			WHERE     (GOID = @GOID) AND (KolID = @KolID) AND (HESABID = @HESABID) AND (THESABID = @THESABID)
		ELSE
			IF (Not(@HESABID IS NULL ))
				SELECT     @Result=COUNT(*) 
				FROM         T_Heasb
				WHERE     (GOID = @GOID) AND (KolID = @KolID) AND (HESABID = @HESABID)
			ELSE
				IF (Not(@KolID IS NULL ))
					SELECT     @Result=COUNT(*) 
					FROM         T_KolHesab
					WHERE     (GOID = @GOID) AND (KOLID = @KOLID)
				
		


END







GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKhadamat]    Script Date: 04/15/2018 14:55:46 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListKhadamat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListKhadamat]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKhadamat]    Script Date: 04/15/2018 14:55:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



    CREATE PROCEDURE [dbo].[QSubPFrm_ListKhadamat] AS       
    
    
    
SELECT     T_DKhad.DKHID, T_DKhad.HesabID, T_DKhad.GOID, T_DKhad.KOLID, T_DKhad.MOINID, T_DKhad.THESABID, T_DKhad.TextName, T_DKhad.TextName1, T_DKhad.TextNameT, T_DKhad.DateKh, 
                      T_DKhad.TextShSA, T_DKhad.NumSID2, T_DKhad.Takhfif, T_DKhad.Sharh, T_DKhad.FieldN1, T_DKhad.FieldN2, T_DKhad.FieldN3, T_DKhad.FieldN4, T_DKhad.FieldT1, T_DKhad.FieldT2, 
                      T_DKhad.FieldT3, T_DKhad.FieldT4, T_DKhad.Flag1, T_DKhad.Flag2, T_DKhad.Flag3, T_DKhad.Flag4, T_DKhad.PrimarySaid2, T_DKhad.Dateeng, T_DKhad.DKHNumbr, T_SA2.SAID2Number, 
                      dbo.HesabName(T_DKhad.KOLID, T_DKhad.HesabID, T_DKhad.THESABID) AS namehesab, T_DKhad.dateSettlement, CONVERT(money, 
                      Mabfact.FieldN3 - T_DKhad.Takhfif + ISNULL(T_DKhad.MabService, 0)) AS MabFact, CONVERT(float, Mabfact.TedadDKh) AS TedadDKh, T_DKhad.ShGhabz, T_DKhad.TimeSabt, T_DKhad.Maliyat, 
                      T_DKhad.Avarez, T_DKhad.address2
FROM         T_DKhad INNER JOIN
                      T_SA2 ON T_DKhad.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
                          (SELECT     DKHID, SUM(ABS(FieldN3)) AS FieldN3, SUM(TedadDKh) AS TedadDKh
                             FROM         [TRANSACTION]
                             GROUP BY DKHID) AS Mabfact ON T_DKhad.DKHID = Mabfact.DKHID
ORDER BY T_DKhad.DKHID DESC


GO




/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_InformationKalaSF9]    Script Date: 04/16/2018 12:07:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_Sandoogh_InformationKalaSF9]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_Sandoogh_InformationKalaSF9]
GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_InformationKalaSF9]    Script Date: 04/16/2018 12:07:32 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[CQ_ListKala_Sandoogh_InformationKalaSF9] ( 
@KalaidCurrent int)           
  AS     
  BEGIN 	
	
  		 
SELECT     FunInfoKalaAll_1.ID AS NameKalaID, CONVERT(float, FunInfoKalaAll_1.KalaNumber) AS KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.Zarib, 
                      FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.KalaNumber AS [کد کالا], FunInfoKalaAll_1.NameKala1 + + N' ' + ISNULL(FunInfoKalaAll_1.note, N' ') AS [نام کالا], 
                      FunInfoKalaAll_1.NameVahed1 AS [نام واحد], ROUND(SUM(CONVERT(float, drvTra.Tedad)), 3) AS [مانده انبار], FunInfoKalaAll_1.NameAnbar AS [نام انبار], FunInfoKalaAll_1.UnitPrice1 AS [قیمت فروش1], 
                      ROUND(CONVERT(money, drvTra.MaxUnitPriceKh), 2) AS [آخرین خرید], FunInfoKalaAll_1.UnitPrice2 AS [قیمت فروش2], FunInfoKalaAll_1.UnitPrice3 AS [قیمت فروش3], 
                      FunInfoKalaAll_1.UnitPrice4 AS [قیمت فروش4], FunInfoKalaAll_1.UnitPrice5 AS [قیمت فروش5], FunInfoKalaAll_1.UnitPrice6 AS [قیمت فروش6], FunInfoKalaAll_1.NameVahed2, 
                      drvTra.MaxUnitPriceKh AS UnitPriceKh, FunInfoKalaAll_1.FieldN1, FunInfoKalaAll_1.BarCode AS [ بارکد ], FunInfoKalaAll_1.NameGoKala1 AS [گروه کالا], FunInfoKalaAll_1.NameAnbar, 
                      FunInfoKalaAll_1.UnitPrice1, drvTra.MaxUnitPriceKh AS AkharinKharid, FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, 
                      FunInfoKalaAll_1.UnitPrice6, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) AS MandeAnbar, 
                      CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber) AS CodKala, FunInfoKalaAll_1.VKalaID0, FunInfoKalaAll_1.VKalaID
FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      dbo.Fun_CQ_ListKala_Sandoogh_Analyz() AS drvTra ON FunInfoKalaAll_1.ID = drvTra.KALAID AND FunInfoKalaAll_1.AID = drvTra.AID
WHERE     (FunInfoKalaAll_1.ID = @KalaidCurrent)
GROUP BY FunInfoKalaAll_1.NameKala1 + + N' ' + ISNULL(FunInfoKalaAll_1.note, N' '), ROUND(CONVERT(money, drvTra.MaxUnitPriceKh), 2), FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, 
                      FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.FieldN1, drvTra.MaxUnitPriceKh, FunInfoKalaAll_1.NameAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber), 
                      CONVERT(float, FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice1, 
                      FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.ID, 
                      FunInfoKalaAll_1.Zarib, drvTra.MaxUnitPriceKh, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.VKalaID0, FunInfoKalaAll_1.VKalaID
ORDER BY [مانده انبار] DESC



  END 


GO




/****** Object:  StoredProcedure [dbo].[Rep_Kala_HesabKH]    Script Date: 04/17/2018 10:57:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_Kala_HesabKH]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_Kala_HesabKH]
GO



/****** Object:  StoredProcedure [dbo].[Rep_Kala_HesabKH]    Script Date: 04/17/2018 10:57:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[Rep_Kala_HesabKH](@startDate nvarchar(50),
@enddate nvarchar(50),
@IdKala int)
AS SELECT      [TRANSACTION].TedadKh AS Sadre, [TRANSACTION].AID, [TRANSACTION].Date1, @startDate AS startDate, @enddate AS enddate, @IdKala AS IdKala, 
                      FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.KalaNumber, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS Namehesab, 
                      [TRANSACTION].KALAID, [TRANSACTION].UnitPriceKh, ROUND(CONVERT(money, [TRANSACTION].TedadKh * [TRANSACTION].UnitPriceKh), 0) AS Mab, T_Kharid.KharidNumbr, T_Kharid.KHARIDID, 
                      [TRANSACTION].Sharh, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.UnitPrice2 + [TRANSACTION].UnitPriceKh AS UnitPriceKhSF1, ROUND(CONVERT(money, 
                      [TRANSACTION].TedadKh * (FunInfoKalaAll_1.UnitPrice2 + [TRANSACTION].UnitPriceKh)), 0) AS MabSF1
FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      T_Kharid INNER JOIN
                      [TRANSACTION] ON T_Kharid.KHARIDID = [TRANSACTION].KHARIDID ON FunInfoKalaAll_1.ID = [TRANSACTION].KALAID AND FunInfoKalaAll_1.AID = [TRANSACTION].AID
WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND ([TRANSACTION].KALAID = @IdKala)
ORDER BY [TRANSACTION].Date1 DESC



GO



/****** Object:  StoredProcedure [dbo].[Rep_Kala_HesabFO]    Script Date: 04/17/2018 10:57:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_Kala_HesabFO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_Kala_HesabFO]
GO


/****** Object:  StoredProcedure [dbo].[Rep_Kala_HesabFO]    Script Date: 04/17/2018 10:57:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[Rep_Kala_HesabFO](@startDate nvarchar(50),
@enddate nvarchar(50),
@IdKala int)
AS SELECT      [TRANSACTION].TedadFo AS Sadre, [TRANSACTION].AID, [TRANSACTION].Date1, @startDate AS startDate, @enddate AS enddate, @IdKala AS IdKala, 
                      FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.KalaNumber, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS Namehesab, 
                      [TRANSACTION].KALAID, T_Forosh.ForoshNumbr, [TRANSACTION].UnitPriceFo, ROUND(CONVERT(money, [TRANSACTION].TedadFo * [TRANSACTION].UnitPriceFo), 0) AS Mab, T_Forosh.FOROSHID, 
                      [TRANSACTION].Sharh, FunInfoKalaAll_1.NameAnbar
FROM         T_Forosh INNER JOIN
                      [TRANSACTION] ON T_Forosh.FOROSHID = [TRANSACTION].FOROSHID INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON [TRANSACTION].KALAID = FunInfoKalaAll_1.ID
WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND ([TRANSACTION].KALAID = @IdKala)
ORDER BY [TRANSACTION].Date1 DESC



GO



/****** Object:  StoredProcedure [dbo].[PFrm_SubRepSood]    Script Date: 04/18/2018 11:00:26 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_SubRepSood]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PFrm_SubRepSood]
GO



/****** Object:  StoredProcedure [dbo].[PFrm_SubRepSood]    Script Date: 04/18/2018 11:00:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[PFrm_SubRepSood](
	@KalaID NVARCHAR(MAX), 
	@GOKID NVARCHAR(MAX), 
	@AID NVARCHAR(MAX), 
	@startDate nvarchar(50), 
	@EndDate nvarchar(50)
) AS 

BEGIN
	
SET NOCOUNT ON; 
DECLARE @SQLQuery AS NVarchar(MAX) 
DECLARE @ParamDefinition AS NVarchar(MAX) 
DECLARE @OrderBy AS NVARCHAR(MAX) 
DECLARE @GROUPBy AS NVARCHAR(MAX) 

IF (@startDate IS NULL)
	SET @startDate = '00000000' 
IF (@EndDate IS NULL)
	SET @EndDate = '99999999'
	
SET @SQLQuery = ' SELECT     SUM(View_SubTransactionKala.FieldN5) AS Sood, T_NameKala.ID, PFrm_Rep_SoodKala_Mojoodi.Mojoodi, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, 
                      @KalaID AS Expr1, SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) AS SumForoosh, SUM(CONVERT(float, View_SubTransactionKala.TedadKh)) AS SumKharid, 
                      SUM(View_SubTransactionKala.TedadKh * View_SubTransactionKala.UnitPriceKH) / CASE WHEN SUM(CONVERT(float, View_SubTransactionKala.TedadKh)) > 0 THEN SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadKh)) ELSE 1 END AS MianginKharid, (SUM(View_SubTransactionKala.TedadFo * View_SubTransactionKala.UnitpriceFo) / CASE WHEN SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadFo)) > 0 THEN SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) ELSE 1 END) AS MianginForoosh, 
                      CASE WHEN SUM(dbo.View_SubTransactionKala.fieldn5) > 0 THEN SUM(dbo.View_SubTransactionKala.fieldn5) ELSE 0 END AS sod, CASE WHEN SUM(dbo.View_SubTransactionKala.fieldn5) 
                      < 0 THEN abs(SUM(dbo.View_SubTransactionKala.fieldn5)) ELSE 0 END AS zian, SUM(CONVERT(float, View_SubTransactionKala.TedadBarFo)) AS SumBarForoosh, SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadBarKh)) AS SumBarKharid, @startDate AS startDate, @EndDate AS EndDate, SUM(View_SubTransactionKala.FieldN5) / CASE WHEN (SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadFo))) = 0 THEN 1 ELSE (SUM(CONVERT(float, View_SubTransactionKala.TedadFo))) END AS SoodTak
FROM         View_SubTransactionKala INNER JOIN
                      T_NameKala ON View_SubTransactionKala.KALAID = T_NameKala.ID INNER JOIN
                      PFrm_Rep_SoodKala_Mojoodi ON T_NameKala.ID = PFrm_Rep_SoodKala_Mojoodi.KALAID
WHERE     (View_SubTransactionKala.Tarikh BETWEEN @startDate AND @EndDate) AND (ISNULL(T_NameKala.FlagNoeKala, 0) = 0)

 '
SET @GROUPBy = ' GROUP BY T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, PFrm_Rep_SoodKala_Mojoodi.Mojoodi, T_NameKala.ID '
SET @OrderBy = ' HAVING      (SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) <> 0) ORDER BY T_NameKala.NameKala1 ' 

IF (@KalaID IS NOT NULL)
	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.KALAID IN(' + @KalaID + '))' 

IF (@GOKID IS NOT NULL)
	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.GOKID IN(' + @GOKID + '))' 

IF (@AID IS NOT NULL)
	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.AID IN(' + @AID + ') )'

SET @SQLQuery = @SQLQuery + ' '
	SET @ParamDefinition = '@KalaID NVARCHAR(MAX), 
							@GOKID NVARCHAR(MAX), 
							@AID NVARCHAR(MAX),
							@startDate nvarchar(50), 
							@EndDate nvarchar(50) '
SET               @SQLQuery = @SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 

EXECUTE sp_Executesql @SQLQuery, @ParamDefinition, @KalaID, @GOKID, @AID, @startDate,@EndDate 

SET NoCount OFF

 END
GO



/****** Object:  StoredProcedure [dbo].[Rep_SoodKol]    Script Date: 05/16/2018 10:42:13 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_SoodKol]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_SoodKol]
GO


/****** Object:  StoredProcedure [dbo].[Rep_SoodKol]    Script Date: 05/16/2018 10:42:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


    CREATE PROCEDURE [dbo].[Rep_SoodKol] (@startDate nvarchar(50), @enddate nvarchar(50)) AS 
    
    
    SELECT     [TRANSACTION].GOID, [TRANSACTION].KOLID, SUM([TRANSACTION].DARBed) AS SumOfDARBed, SUM([TRANSACTION].PARBes) AS SumOfPARBes, SUM([TRANSACTION].DARBed) 
                      - SUM([TRANSACTION].PARBes) AS Expr1, @startDate AS startDate, @enddate AS enddate, T_KolHesab.NameHesab, T_GropHesab.NameGo
FROM         [TRANSACTION] INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID INNER JOIN
                      T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2
WHERE     ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) AND (T_SA2.Flag4 <> 1 OR
                      T_SA2.Flag4 IS NULL)
GROUP BY [TRANSACTION].GOID, [TRANSACTION].KOLID, T_KolHesab.NameHesab, T_GropHesab.NameGo
HAVING      ([TRANSACTION].GOID IN (8, 9)) AND (SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes) <> 0)
UNION ALL
SELECT     8 AS GOID, - 1000 AS kolid, SUM(Zian) AS Zian, SUM(Sood) AS Sood, 0 AS Expr1, @startDate AS Expr2, @enddate AS Expr3, 'درآمد حاصل از فروش' AS NameHesab, 'درآمدها' AS Expr4
FROM         dbo.Fun_Sood_Zian_Kala(@startDate, @enddate) AS Fun_Sood_Zian_Kala 

GO




/****** Object:  StoredProcedure [dbo].[Rep_SoodMoin]    Script Date: 05/16/2018 10:42:18 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_SoodMoin]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_SoodMoin]
GO


/****** Object:  StoredProcedure [dbo].[Rep_SoodMoin]    Script Date: 05/16/2018 10:42:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO


    /*  بروز رساني گزارش سود و زيان معين */ CREATE PROCEDURE [dbo].[Rep_SoodMoin] (
    	
    	@startDate nvarchar(50), @enddate nvarchar(50)) AS          
    	
SELECT     [TRANSACTION].GOID, [TRANSACTION].KOLID, [TRANSACTION].HESABID, SUM([TRANSACTION].DARBed) AS SumOfDARBed, SUM([TRANSACTION].PARBes) AS SumOfPARBes, 
                      ABS(SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes)) AS Expr1, T_KolHesab.FieldN1, @startDate AS startDate, @enddate AS enddate, T_KolHesab.KOLID AS Expr2, 
                      MIN([TRANSACTION].Date1) AS MinOfDate1, MAX([TRANSACTION].Date1) AS MaxOfDate1, T_KolHesab.NameHesab, T_GropHesab.NameGo, T_Heasb.NameHesab AS NameHesabMo
FROM         [TRANSACTION] INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID INNER JOIN
                      T_GropHesab ON T_KolHesab.GOID = T_GropHesab.GoID INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (T_SA2.Flag4 IS NULL OR
                      T_SA2.Flag4 <> 1) AND ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate) OR
                      (T_SA2.Flag4 IS NULL OR
                      T_SA2.Flag4 <> 1) AND ([TRANSACTION].Date1 BETWEEN @startDate AND @enddate)
GROUP BY [TRANSACTION].GOID, [TRANSACTION].KOLID, T_KolHesab.FieldN1, T_KolHesab.KOLID, [TRANSACTION].HESABID, T_KolHesab.NameHesab, T_GropHesab.NameGo, T_Heasb.NameHesab
HAVING      ([TRANSACTION].GOID >= 8) AND (SUM([TRANSACTION].DARBed) > 0) AND (ABS(SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes)) <> 0) OR
                      ([TRANSACTION].GOID >= 8) AND (ABS(SUM([TRANSACTION].DARBed) - SUM([TRANSACTION].PARBes)) <> 0) AND (SUM([TRANSACTION].PARBes) > 0)
                      
               UNION ALL
               
SELECT     8 AS GOID, - 1000 AS kolid, - 1000 AS Hesabid, SUM(Zian) AS Zian, SUM(Sood) AS Sood, SUM(ABS(Zian - Sood)) AS Expr1, 0 AS FN1, @startDate AS Expr2, @enddate AS Expr3, - 1000 AS kol, 
                      @startDate AS min, @enddate AS max, 'درآمد حاصل از فروش کالا' AS NameHesab, 'درآمد' AS Expr4, 'درآمد حاصل از فروش کالا' AS Expr5
FROM         dbo.Fun_Sood_Zian_Kala(@startDate, @enddate) AS Fun_Sood_Zian_Kala
GO




/****** Object:  StoredProcedure [dbo].[PFrm_Mojoodi_EditMojodiKala_Insert_ToTemp]    Script Date: 05/16/2018 10:43:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_Mojoodi_EditMojodiKala_Insert_ToTemp]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PFrm_Mojoodi_EditMojodiKala_Insert_ToTemp]
GO



/****** Object:  StoredProcedure [dbo].[PFrm_Mojoodi_EditMojodiKala_Insert_ToTemp]    Script Date: 05/16/2018 10:43:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[PFrm_Mojoodi_EditMojodiKala_Insert_ToTemp]  
                @UserID INT
        
        AS
        BEGIN
        	
        	IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[T_EditMojodiKala]') AND type in (N'U'))
					DROP TABLE [dbo].[T_EditMojodiKala] 
	

SELECT       MojodiEbtedaDore AS Mojodi, UnitPriceEbtedaDore AS UnitPrice, ID AS NameKalaID, GOKID, AID, NameKala1, NameAnbar, NameGoKala1, KalaNumber,0 AS FlagEdit
INTO            T_EditMojodiKala
FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1
ORDER BY NameKala1, NameAnbar, NameGoKala1

   ALTER TABLE  dbo.[T_EditMojodiKala] ADD IDKey int IDENTITY(1,1) primary key NOT NULL
               

End

GO




/****** Object:  StoredProcedure [dbo].[SaveChangesMojodiEbtedaDore_On_T_Kala]    Script Date: 05/16/2018 10:43:59 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SaveChangesMojodiEbtedaDore_On_T_Kala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SaveChangesMojodiEbtedaDore_On_T_Kala]
GO


/****** Object:  StoredProcedure [dbo].[SaveChangesMojodiEbtedaDore_On_T_Kala]    Script Date: 05/16/2018 10:43:59 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        Create PROCEDURE [dbo].[SaveChangesMojodiEbtedaDore_On_T_Kala]  
                @UserID INT
        
        AS
        BEGIN
        	
UPDATE    T_Kala
SET              Mojodi = T_EditMojodiKala.Mojodi, UnitPrice = T_EditMojodiKala.UnitPrice
FROM         T_EditMojodiKala INNER JOIN
                      T_Kala ON T_EditMojodiKala.NameKalaID = T_Kala.NameKalaID AND T_EditMojodiKala.AID = T_Kala.AID AND T_EditMojodiKala.GOKID = T_Kala.GOKID
WHERE     (T_EditMojodiKala.FlagEdit > 0)
               

End

GO






/****** Object:  StoredProcedure [dbo].[Rep_030930_1]    Script Date: 05/24/2018 13:57:53 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_030930_1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_030930_1]
GO


/****** Object:  StoredProcedure [dbo].[Rep_030930_1]    Script Date: 05/24/2018 13:57:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[Rep_030930_1](@startDate nvarchar(50),
									@enddate nvarchar(50),
									@startSanadID int,
									@endSanadID INT,
									@GOID INT,
									@KOLID INT,
									@HESABID INT,
									@THESABID int,
									@THESABIDSath2 INT)
			AS 
BEGIN        	
  	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';     
  	   
  	 SET @ParmDefinition = N'@startDate nvarchar(50)=''0'', 
							@enddate nvarchar(50)=''99999999'',
							@startSanadID int=null,
							@endSanadID INT=null,
							@GOID INT=null,
							@KOLID INT=null,
							@HESABID INT=null,
							@THESABID int=null,
							@THESABIDSath2 INT=NUll';
							
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END   
	        
set @SQLString=N' SELECT     [TRANSACTION].Date1, T_SA2.SAID2Number, SUM([TRANSACTION].DARBed) AS DARBed, SUM([TRANSACTION].PARBes) AS PARBes, @startDate AS startDate, @enddate AS enddate, 
                      [TRANSACTION].KOLID, [TRANSACTION].GOID, [TRANSACTION].HESABID, [TRANSACTION].SAID2, CASE WHEN SUM([TRANSACTION].DARBed) = 0 THEN 1 ELSE 2 END AS Sort1, 
                      dbo.HesabNameCod([TRANSACTION].KOLID, [TRANSACTION].HESABID, NULL) AS CodeHesab, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, NULL) AS NameHesabMoin, 
                      dbo.HesabName([TRANSACTION].KOLID, NULL, NULL) AS NameHesabKol, convert(NVARCHAR(255),T_SA2.Sharh) as SharhSanad
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2


 '


  /*  --------------------شرط حساب سرگروه  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
		set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
		set @ParmAndstr=' AND ' 			    
   END                   
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معين  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END     
        
   /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   
   
     /*  --------------------تاريخ  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startDate  IS NULL)) AND (NOT (@enddate  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].Date1 BetWeen @startDate and  @enddate)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
    /*  --------------------شماره سند  */ 			    
   set @ParmAndstr=' AND ' 			    
   if((NOT (@startSanadID  IS NULL)) AND (NOT (@endSanadID  IS NULL))) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_SA2.SAID2Number BetWeen @startSanadID and  @endSanadID)'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
     
   
       /*  --------------------شرط حساب تفصيلي  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABIDSath2  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABIDSath2 = @THESABIDSath2 )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
   
   
   set @SQLString= @SQLString + @WhrStatement + ' GROUP BY [TRANSACTION].Date1, T_SA2.SAID2Number,   [TRANSACTION].KOLID, 
                      [TRANSACTION].GOID, [TRANSACTION].HESABID,   [TRANSACTION].SAID2, dbo.HesabNameCod([TRANSACTION].KOLID, 
                      [TRANSACTION].HESABID,null),dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, NULL), dbo.HesabName([TRANSACTION].KOLID, NULL, NULL), 
                      CASE WHEN [TRANSACTION].DARBed = 0 THEN 1 ELSE 2 END , convert(NVARCHAR(255),T_SA2.Sharh)
                      
HAVING      (SUM([TRANSACTION].DARBed) IS NOT NULL) AND (SUM([TRANSACTION].PARBes) IS NOT NULL) '



							
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@startDate=@startDate,@enddate=@enddate,@startSanadID=@startSanadID,@endSanadID=@endSanadID,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@THESABIDSath2=@THESABIDSath2;  		     

END 






GO



/****** Object:  StoredProcedure [dbo].[Basic_Hesab_NewNumberCod]    Script Date: 05/31/2018 15:59:37 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Basic_Hesab_NewNumberCod]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Basic_Hesab_NewNumberCod]
GO



/****** Object:  StoredProcedure [dbo].[Basic_Hesab_NewNumberCod]    Script Date: 05/31/2018 15:59:37 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




  CREATE PROCEDURE [dbo].[Basic_Hesab_NewNumberCod] ( 
@GOID INT,@KOLID  INT,@HesabID  INT,@NewNumberCodeHesab NVARCHAR(20) OUTPUT)           
  AS     
  BEGIN
  	 	


IF(NOT(@GOID IS NULL))
BEGIN
DECLARE @MAXKolNumber AS INT
DECLARE @NumberNewCod AS NVARCHAR(50)	
DECLARE @LenKolNumber AS INT
DECLARE @LenGoNumber AS INT
DECLARE @GoNumber AS NVARCHAR(50)

SET @NumberNewCod=''
SET @GoNumber=''

SELECT    @MAXKolNumber= MAX(KolNumber) 
FROM         T_KolHesab
WHERE     (GOID = @GOID)

IF (@MAXKolNumber IS NULL)
	BEGIN	
		
		SELECT     @LenGoNumber=FieldN1, @LenKolNumber=FieldN2
		FROM         T_Defult
		WHERE     (GROUPID = 5) AND (OPTIONID = 1)
		
		SELECT     @GoNumber=GoNumber
		FROM         T_GropHesab
		WHERE     (GoID = @GOID)
		WHILE  @LenKolNumber>1

		BEGIN	
			SET @NumberNewCod=@NumberNewCod+'0'
			set @LenKolNumber=@LenKolNumber-1
		END	
		
		set @MAXKolNumber=@GoNumber+@NumberNewCod+'1'
	END

ELSE
	BEGIN
		set @GoNumber=''
		set @NumberNewCod=''
		SET @MAXKolNumber=@MAXKolNumber+1
	END

	set @NewNumberCodeHesab =  @MAXKolNumber 

End

ELSE 
	IF (NOT (@KOLID IS NULL))
		BEGIN
	DECLARE @MAXMoinNumber AS INT
	DECLARE @LenMoinNumber AS INT
	DECLARE @KolNumber AS NVARCHAR(50)

	SET @NumberNewCod=''
	SET @KolNumber=''


	SELECT     @MAXMoinNumber=  MAX(HESABNumber) 
	FROM         T_Heasb
	WHERE     (KolID = @KOLID)

	IF (@MAXMoinNumber IS NULL)
		BEGIN	
			
			SELECT     @LenMoinNumber=FieldN3
			FROM         T_Defult
			WHERE     (GROUPID = 5) AND (OPTIONID = 1)
			
			
			
			SELECT     @KolNumber=KolNumber
			FROM         T_KolHesab
			WHERE     (KOLID = @KOLID)
			
			WHILE  @LenMoinNumber>1

			BEGIN	
				SET @NumberNewCod=@NumberNewCod+'0'
				set @LenMoinNumber=@LenMoinNumber-1
			END	
			
			set @MAXMoinNumber=@KolNumber+@NumberNewCod+'1'
		END

	ELSE
		BEGIN
			set @KolNumber=''
			set @NumberNewCod=''
			SET @MAXMoinNumber=@MAXMoinNumber+1
		END

	set @NewNumberCodeHesab = @MAXMoinNumber 
	END

ELSE
	IF(NOT(@HesabID IS NULL))
	BEGIN
DECLARE @MAXTafNumber AS BIGINT

DECLARE @LenTafNumber AS INT
DECLARE @MoinNumber AS NVARCHAR(50)

SET @NumberNewCod=''
SET @MoinNumber=''


SELECT     @MAXTafNumber= MAX(THESABNumber) 
FROM         T_T_Heasb
WHERE     (HESABID = @HesabID)

IF (@MAXTafNumber IS NULL)
	BEGIN	
		
		SELECT     @LenTafNumber=FieldT1
		FROM         T_Defult
		WHERE     (GROUPID = 5) AND (OPTIONID = 1)
		
		
		
	SELECT     @MoinNumber=HESABNumber
	FROM         T_Heasb
	WHERE     (HESABID = @HesabID)
		
		WHILE  @LenTafNumber>1

		BEGIN	
			SET @NumberNewCod=@NumberNewCod+'0'
			set @LenTafNumber=@LenTafNumber-1
		END	
		
		set @MAXTafNumber=@MoinNumber+@NumberNewCod+'1'
	END

ELSE
	BEGIN
		set @MoinNumber=''
		set @NumberNewCod=''
		SET @MAXTafNumber=@MAXTafNumber+1
	END

	set @NewNumberCodeHesab = @MAXTafNumber 
END

 END 



GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_BarcodeVariable]    Script Date: 07/12/2018 11:20:05 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_BarcodeVariable]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_BarcodeVariable]
GO

/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_BarcodeVariable]    Script Date: 07/12/2018 11:20:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_BarcodeVariable]
		/* ورودي*/
		@Userid INT,	
		@BarcodeVariable NVARCHAR(50),
		/* خروجي*/
		@BarcodeExtraction NVARCHAR(50) OUTPUT,
		@VaznExtraction DECIMAL(19,4) OUTPUT
AS
BEGIN

DECLARE @LenBarcode INT
DECLARE @BarcodeExists INT
DECLARE @LengthTedad DECIMAL(19,4)
DECLARE @LengthTedad2 DECIMAL(19,4)

set @LengthTedad2=1
SELECT     @LenBarcode=FieldT1
FROM         T_Defult
WHERE     (GROUPID = 7) AND (OPTIONID = 4)


SELECT     @LengthTedad=FieldT1
FROM         T_Defult
WHERE     (GROUPID = 7) AND (OPTIONID = 6)

set @LengthTedad=@LengthTedad-1

WHILE @LengthTedad<>0
BEGIN
	set @LengthTedad2=@LengthTedad2*10
	set @LengthTedad=@LengthTedad-1
END

set @LengthTedad=0
SELECT     @LengthTedad=FieldT1
FROM         T_Defult
WHERE     (GROUPID = 7) AND (OPTIONID = 6)

SELECT     @LengthTedad=isnull(@LengthTedad,0)+isnull(FieldT1,0)
FROM         T_Defult
WHERE     (GROUPID = 7) AND (OPTIONID = 5)


SELECT    @BarcodeExists= COUNT(*) 
FROM         T_NameKala
WHERE     (BarCode LIKE LEFT(@BarcodeVariable, @LenBarcode))

IF (@BarcodeExists>0)
BEGIN
	Set @BarcodeExtraction=left(@BarcodeVariable,@LenBarcode)
	set @VaznExtraction=left(RIGHT(@BarcodeVariable,len(@BarcodeVariable)-@LenBarcode),@LengthTedad-1)/@LengthTedad2
END

IF (@LengthTedad2=0)
	set @VaznExtraction=1

END






GO




/****** Object:  StoredProcedure [dbo].[AC_Setting_Information_User]    Script Date: 08/08/2018 15:10:53 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AC_Setting_Information_User]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[AC_Setting_Information_User]
GO


/****** Object:  StoredProcedure [dbo].[AC_Setting_Information_User]    Script Date: 08/08/2018 15:10:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[AC_Setting_Information_User]
		
		@Userid INT

AS
BEGIN
	SELECT     AC_Setting.Dbo.T_yUsers.*
	FROM         AC_Setting.Dbo.T_yUsers
	WHERE     (USERID = @Userid)
END







GO




/****** Object:  StoredProcedure [dbo].[Rep_05228553]    Script Date: 08/17/2018 22:53:10 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_05228553]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_05228553]
GO



/****** Object:  StoredProcedure [dbo].[Rep_05228553]    Script Date: 08/17/2018 22:53:10 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



/*
@S_PHI=forms![TreeViewRep_HesabAnbar]![Sth_PHI].column(2),
@SortRep=forms![TreeViewRep_HesabAnbar]![Sorting],
@KalaID =forms![TreeViewRep_HesabAnbar]![KalaID],
@NameKala =forms![TreeViewRep_HesabAnbar]![FilterNameKala],
@GbyKalaID =forms![TreeViewRep_HesabAnbar]![ChGbyKalaID],
@Gokid  =forms![TreeViewRep_HesabAnbar]![GoKala],
@NameGokid =forms![TreeViewRep_HesabAnbar]![FilterNameGOKALA],
@GbyGokid =forms![TreeViewRep_HesabAnbar]![ChGbyGokid],
@Aid  =forms![TreeViewRep_HesabAnbar]![AID],
@NameAid =forms![TreeViewRep_HesabAnbar]![FilterNameAID],
@GbyAid =forms![TreeViewRep_HesabAnbar]![ChGbyAid],
@StartDate =forms![TreeViewRep_HesabAnbar]![AsDate],
@Enddate =forms![TreeViewRep_HesabAnbar]![TaDate]
*/

  CREATE PROCEDURE [dbo].[Rep_05228553] ( 
	@S_PHI int,/****** براساس آخرین خرید (1)  قیمت سطح 1 (2) قیمت سطح 2 (3) قیمت سطح 4 (5) قیمت سطح 5 (6) قیمت سطح 6 (6)قیمت *******/
	@SortRep int, 
	@KalaID int,
	@NameKala nvarchar(255), 
	@GbyKalaID AS BIT,
	@Gokid int,
	@NameGokid nvarchar(255), 
	@GbyGokid AS BIT,
	@Aid int,
	@NameAid  nvarchar(255),
	@GbyAid AS BIT,
	@StartDate  nvarchar(10),
	@Enddate  nvarchar(10))           
  AS     
  BEGIN 	SET NOCOUNT ON; 
  	 	
  DECLARE @SQLString  NVARCHAR(Max); 
  DECLARE @SQL_SELECT  NVARCHAR(Max); 
  DECLARE @SQL_FROM NVARCHAR(Max); 
  DECLARE @SQL_WHERE  NVARCHAR(Max); 
  DECLARE @SQL_GROUP  NVARCHAR(Max); 
  DECLARE @SQL_HAVING  NVARCHAR(Max); 
  DECLARE @SQL_ORDER NVARCHAR(Max); 
  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max); 
  
  	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END    							            	
  	
  	
  	
  set @SQL_SELECT=N' ';
  Set @SQL_FROM=N' ';
  Set @SQL_WHERE=N' ';
  Set @SQL_GROUP=N' ';
  Set @SQL_ORDER=N' ';	
  set @WhrStatement  = N' ';         	
  set @ParmAndstr = N' ';  
  SEt @SQL_HAVING =N' HAVING      (SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) <> 0) ';
  
  SET @ParmDefinition =N'@S_PHI int=null,
						@GbyKalaID AS BIT=null,
						@GbyGokid AS BIT=null,
						@SortRep int=null, 
						@KalaID int=null,
						@NameKala nvarchar(255)=null, 
						@Gokid int=null,
						@NameGokid nvarchar(255)=null, 
						@Aid int=null,
						@NameAid  nvarchar(255)=null,
						@GbyAid bit=1,
						@StartDate nvarchar(10)=null,
						@Enddate nvarchar(10)=null';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		

  BEGIN 
  

  
  
   if (@S_PHI IS NULL)
		Set @S_PHI=1
		
  if (@S_PHI=1)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(Fun_SubAkharinKharid_1.MaxUnitPriceKh, 0)) AS SumMabUnit, MAX(ISNULL(Fun_SubAkharinKharid_1.MaxUnitPriceKh, 0)) as MabUnit  '
  if (@S_PHI=2)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice1, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice1, 0)) as MabUnit '
  if (@S_PHI=3)
	Set @SQL_SELECT=N' , Sum(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * Max(ISNULL(FunInfoKalaAll_1.UnitPrice2, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice2, 0)) as MabUnit  '	
  if (@S_PHI=4)
	Set @SQL_SELECT=N' , Sum(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice3, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice3, 0)) as MabUnit '
  if (@S_PHI=5)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * MAX(ISNULL(FunInfoKalaAll_1.UnitPrice4, 0)) AS SumMabUnit, MAX(ISNULL(FunInfoKalaAll_1.UnitPrice4, 0)) as MabUnit  '	
  if (@S_PHI=6)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * Max(ISNULL(FunInfoKalaAll_1.UnitPrice5, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice5, 0) as MabUnit '
  if (@S_PHI=7)
	Set @SQL_SELECT=N' , SUM(CONVERT (float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) * max(ISNULL(FunInfoKalaAll_1.UnitPrice6, 0)) AS SumMabUnit , MAX(ISNULL(FunInfoKalaAll_1.UnitPrice6, 0)) as MabUnit '	
	

		/* شرط انطباق انبار*/
  
  if (@GbyAid=1) 
			Begin
				Set @SQL_SELECT=@SQL_SELECT + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar '
				Set @SQL_GROUP=@SQL_GROUP + N' ,FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber , FunInfoKalaAll_1.NameAnbar ' 
				
				if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' ( FunInfoKalaAll_1.AID = @Aid )'; 
					end

				if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_HAVING=N' ')
							set @ParmAndstr=N' HAVING ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 	
					End
			end
  else
		Begin
			if(NOT (@Aid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' WHERE ')
							set @ParmAndstr=N' ' 	
						else
							set @ParmAndstr=N' AND '

						set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N'( FunInfoKalaAll_1.AID = @Aid )'; 
					End					

			if(NOT (@NameAid IS NULL)) 
					Begin
						if(@SQL_WHERE=N' ')
							set @ParmAndstr=N' WHERE ' 	
						else
							set @ParmAndstr=N' AND '
							
						Set @NameAid='%'+@NameAid+'%'
						
						Set @SQL_WHERE= @SQL_WHERE + @ParmAndstr + N' (FunInfoKalaAll_1.NameAnbar LIKE @NameAid) '; 
					End						
		End  			
		
 
   /* --------------------شرط انطباق نام کالا*/  
  if(NOT (@KalaID IS NULL)) 
			Begin
				if(@SQL_HAVING=N' ')
					set @ParmAndstr=N' HAVING ' 	
				else
					set @ParmAndstr=N' AND '
					
					
				set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'   (FunInfoKalaAll_1.ID = @KalaID ) ' 
								
			End
     
    /* --------------------شرط انطباق قسمتی از نام کالا*/ 			
  if(NOT (@NameKala IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameKala='%'+@NameKala+'%'
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameKala1 LIKE @NameKala) '; 	
		End

	/* --------------------شرط انطباق گروه*/ 			

	if(NOT (@Gokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @Gokid )'; 	
		End
  
      /* --------------------شرط انطباق قسمتی از نام گروه کالا*/ 			

	if(NOT (@NameGokid IS NULL)) 
		Begin
			if(@SQL_HAVING=N' ')
				set @ParmAndstr=N' HAVING ' 	
			else
				set @ParmAndstr=N' AND '
				
			Set @NameGokid='%'+@NameGokid+'%'
			Set @SQL_HAVING= @SQL_HAVING + @ParmAndstr + N' (FunInfoKalaAll_1.NameGoKala1 LIKE @NameGokid) '; 
		End	
 

  Set @SQLString =N'SELECT     FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) AS SumTedad, 
                      FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, MAX(Fun_SubAkharinKharid_1.MaxUnitPriceKh) AS Max_UnitPriceKh, MAX(FunInfoKalaAll_1.UnitPrice1) AS MaxUnitPrice1, 
                      MAX(FunInfoKalaAll_1.UnitPrice2) AS MaxUnitPrice2, MAX(FunInfoKalaAll_1.UnitPrice3) AS MaxUnitPrice3, MAX(FunInfoKalaAll_1.UnitPrice4) AS MaxUnitPrice4, MAX(FunInfoKalaAll_1.UnitPrice5) 
                      AS MaxUnitPrice5, MAX(FunInfoKalaAll_1.UnitPrice6) AS MaxUnitPrice6, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode
'+ @SQL_SELECT + 'FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON Fun_SubAkharinKharid_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid  LEFT OUTER JOIN
                      (SELECT     KALAID, GOKID, AID, SUM(TedadVarde) AS TedadVarde, SUM(TedadSadre) AS TedadSadre, LinkKalaAid
FROM         View_SubTransactionKala AS SubTransactionKala_1
WHERE  Tarikh Between @StartDate and @Enddate
GROUP BY KALAID, GOKID, AID, LinkKalaAid) AS SubTransactionKala_1 ON FunInfoKalaAll_1.LinkKalaAid = SubTransactionKala_1.LinkKalaAid 
 ' +@SQL_WHERE + '                      
GROUP BY FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.VKalaID, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.note, FunInfoKalaAll_1.BarCode ' + @SQL_GROUP + @SQL_HAVING




                      


		    /* --------------------    1مرتب سازی */ 			
    if(@SortRep=1) 	
			set @SQLString= @SQLString  + N'  ORDER BY KalaNumber,NameKala1 '; 	
    		    /* --------------------    2مرتب سازی */ 			
    if(@SortRep=2) 	
			set @SQLString= @SQLString + N'  ORDER BY NameKala1,KalaNumber '; 
    		    /* --------------------    3مرتب سازی */ 			
    if(@SortRep=3) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKalanumbr,NameKala1 '; 
    		    /* --------------------    4مرتب سازی */ 			
    if(@SortRep=4) 	
			set @SQLString= @SQLString + N'  ORDER BY NameGoKala1,NameKala1 '; 	
        		    /* --------------------    5مرتب سازی */ 			
	 IF ((@SortRep=5) AND (@GbyAid=1)) 	
			set @SQLString= @SQLString + N'  ORDER BY AIDNumber,NameKala1 '; 
    		    /* --------------------    6مرتب سازی */ 			
    IF ((@SortRep=6) AND (@GbyAid=1))	
			set @SQLString= @SQLString + N' ORDER BY NameAnbar,NameKala1 '; 		


    
    EXECUTE sp_executesql @SQLString, @ParmDefinition,@GbyKalaID=@GbyKalaID,@GbyGokid=@GbyGokid,@SortRep=@SortRep,@S_PHI=@S_PHI,@KalaID=@KalaID,@NameKala=@NameKala,@Gokid=@Gokid,@NameGokid=@NameGokid,@Aid=@Aid,@NameAid=@NameAid,@GbyAid=@GbyAid,@StartDate=@StartDate,@Enddate=@Enddate;  		
    END 		 
    /* --------پایان */  		 		 
return END      


GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_KhadamatSF11_CALC]    Script Date: 08/27/2018 14:48:53 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_KhadamatSF11_CALC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_KhadamatSF11_CALC]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_KhadamatSF11_CALC]    Script Date: 08/27/2018 14:48:54 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE  PROCEDURE [dbo].[QSubPFrm_KhadamatSF11_CALC]
		
		@Userid INT,	
		@TxtLinkTosub NVARCHAR(255)

AS
BEGIN
	

     DECLARE   @Tedad DECIMAL(19,4) 
     DECLARE   @MabFactor DECIMAL(19,4) 
     DECLARE   @MabHazine DECIMAL(19,4) 
     DECLARE   @MabTakhfif DECIMAL(19,4) 
     DECLARE   @MabMaliat DECIMAL(19,4) 
     DECLARE   @MabAvarez DECIMAL(19,4) 
     DECLARE   @MabFactorSF11 DECIMAL(19,4) 
        
SELECT      @Tedad=SUM(Tedad),@MabFactor= SUM(Tedad * UnitPrice) , @MabTakhfif=SUM(isnull(FieldN7,0)) , @MabMaliat=SUM(isnull(Maliatkala,0)) , @MabAvarez=SUM(isnull(Avarez,0)) ,@MabFactorSF11= SUM(Tedad * isnull(Tedad2,1) * UnitPrice)
FROM         TRANSACTION2
WHERE     (date1 = @TxtLinkTosub)
GROUP BY LinkedID

 
SELECT     @MabHazine=SUM(ISNULL(DARBed, 0)) 
FROM         TRANSACTION2
WHERE     (TextAIDFo = @TxtLinkTosub) AND (Flag1 <> 1)
GROUP BY LinkedID

SELECT     @Tedad AS Tedad, @MabFactor AS MabFactor, isnull(@MabTakhfif,0) AS MabTakhfif, isnull(@MabMaliat,0) AS MabMaliat, isnull(@MabAvarez,0) AS MabAvarez, isnull(@MabHazine,0) AS MabHazine	, isnull(@MabFactorSF11,0) AS MabFactorSF11		  

END








GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListForooshSandogh]    Script Date: 09/03/2018 09:56:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListForooshSandogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListForooshSandogh]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListForooshSandogh]    Script Date: 09/03/2018 09:56:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


         CREATE PROCEDURE [dbo].[QSubPFrm_ListForooshSandogh](
         	
			@UserID INT,
			@DateFo NVARCHAR(10),
			@OnlyActive BIT)
		 AS                                     
          
      
      IF (@OnlyActive<>0)    
      
      
      SELECT     T_Forosh.FOROSHID, T_Forosh.HesabID, T_Forosh.GOID, T_Forosh.KOLID, T_Forosh.MOINID, T_Forosh.THESABID, T_Forosh.TextName, T_Forosh.TextName1, T_Forosh.TextNameT, 
                      T_Forosh.NumberKH, T_Forosh.DateFo, T_Forosh.Takhfif, T_Forosh.TextShSA, T_Forosh.NumSID2, T_Forosh.Sharh, T_Forosh.FieldN1, T_Forosh.FieldN2, T_Forosh.FieldN3, T_Forosh.FieldN4, 
                      T_Forosh.FieldT1, T_Forosh.FieldT2, T_Forosh.FieldT3, T_Forosh.FieldT3 AS Expr1, T_Forosh.FieldT4, T_Forosh.Flag1, T_Forosh.Flag2, T_Forosh.Flag3, T_Forosh.Flag4, T_Forosh.Flag5, 
                      T_Forosh.Flag6, T_Forosh.Flag7, T_Forosh.Flag8, T_Forosh.ForoshNumbr, T_Forosh.PrimarySaid2, T_Forosh.THESABIDAmel, T_SA2.SAID2Number, CASE WHEN isnull(T_Forosh.THESABID, 0) 
                      <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Forosh.HESABID, 0) <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS namehesab, T_Forosh.dateSettlement, 
                      CONVERT(Money, DrvMabFact.FieldN3 - T_Forosh.Takhfif + ISNULL(F_MabHazine_Foroosh_1.MabHazine, 0)) AS MabFact, CONVERT(float, DrvMabFact.TedadFo) AS TedadFo, 
                      F_MabHazine_Foroosh_1.MabHazine, T_Forosh.Maliyat, T_Forosh.Avarez, T_Forosh.FlgSabtFact, T_Forosh.UserID
FROM         T_Forosh INNER JOIN
                          (SELECT     FOROSHID, SUM(ABS(FieldN3)) AS FieldN3, SUM(TedadFo) AS TedadFo
                             FROM         [TRANSACTION]
                             GROUP BY FOROSHID) AS DrvMabFact ON T_Forosh.FOROSHID = DrvMabFact.FOROSHID LEFT OUTER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
                      dbo.F_MabHazine_Foroosh() AS F_MabHazine_Foroosh_1 ON T_Forosh.FOROSHID = F_MabHazine_Foroosh_1.FactID LEFT OUTER JOIN
                      T_KolHesab ON T_Forosh.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON T_Forosh.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON T_Forosh.HesabID = T_Heasb.HESABID
WHERE     (NOT (T_Forosh.FOROSHID IS NULL)) AND (T_Forosh.DateFo = @DateFo) AND (T_Forosh.FlgSabtFact = 0) AND (T_Forosh.UserID = @UserID)
ORDER BY T_Forosh.DateFo DESC, T_Forosh.ForoshNumbr DESC

			
	ELSE
		

SELECT     T_Forosh.FOROSHID, T_Forosh.HesabID, T_Forosh.GOID, T_Forosh.KOLID, T_Forosh.MOINID, T_Forosh.THESABID, T_Forosh.TextName, T_Forosh.TextName1, T_Forosh.TextNameT, 
                      T_Forosh.NumberKH, T_Forosh.DateFo, T_Forosh.Takhfif, T_Forosh.TextShSA, T_Forosh.NumSID2, T_Forosh.Sharh, T_Forosh.FieldN1, T_Forosh.FieldN2, T_Forosh.FieldN3, T_Forosh.FieldN4, 
                      T_Forosh.FieldT1, T_Forosh.FieldT2, T_Forosh.FieldT3, T_Forosh.FieldT3 AS Expr1, T_Forosh.FieldT4, T_Forosh.Flag1, T_Forosh.Flag2, T_Forosh.Flag3, T_Forosh.Flag4, T_Forosh.Flag5, 
                      T_Forosh.Flag6, T_Forosh.Flag7, T_Forosh.Flag8, T_Forosh.ForoshNumbr, T_Forosh.PrimarySaid2, T_Forosh.THESABIDAmel, T_SA2.SAID2Number, CASE WHEN isnull(T_Forosh.THESABID, 0) 
                      <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Forosh.HESABID, 0) <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS namehesab, T_Forosh.dateSettlement, 
                      CONVERT(Money, DrvMabFact.FieldN3 - T_Forosh.Takhfif + ISNULL(F_MabHazine_Foroosh_1.MabHazine, 0)) AS MabFact, CONVERT(float, DrvMabFact.TedadFo) AS TedadFo, 
                      F_MabHazine_Foroosh_1.MabHazine, T_Forosh.Maliyat, T_Forosh.Avarez, T_Forosh.DateCreate, T_Forosh.UserID
FROM         T_Forosh INNER JOIN
                          (SELECT     FOROSHID, SUM(ABS(FieldN3)) AS FieldN3, SUM(TedadFo) AS TedadFo
                             FROM         [TRANSACTION]
                             GROUP BY FOROSHID) AS DrvMabFact ON T_Forosh.FOROSHID = DrvMabFact.FOROSHID LEFT OUTER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
                      dbo.F_MabHazine_Foroosh() AS F_MabHazine_Foroosh_1 ON T_Forosh.FOROSHID = F_MabHazine_Foroosh_1.FactID LEFT OUTER JOIN
                      T_KolHesab ON T_Forosh.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON T_Forosh.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON T_Forosh.HesabID = T_Heasb.HESABID
WHERE     (NOT (T_Forosh.FOROSHID IS NULL)) AND (T_Forosh.DateFo = @DateFo) AND (T_Forosh.UserID = @UserID)
ORDER BY T_Forosh.DateFo DESC, T_Forosh.ForoshNumbr DESC



GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListForooshSandogh_Calc]    Script Date: 09/03/2018 09:56:41 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListForooshSandogh_Calc]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListForooshSandogh_Calc]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListForooshSandogh_Calc]    Script Date: 09/03/2018 09:56:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


         CREATE	 PROCEDURE [dbo].[QSubPFrm_ListForooshSandogh_Calc]
         	
			@UserID INT,
			@OnlyActive BIT,
			@DateFo NVARCHAR(10)
			
		 AS                                     
BEGIN          
          

  
SELECT     Tedad, MabFactor, MabTakhfif, MabHazine, MabMaliat, MabAvarez
FROM         (SELECT     SUM(CONVERT(float, DrvMabFact.TedadFo)) AS Tedad, SUM(CONVERT(Money, DrvMabFact.FieldN3 - T_Forosh.Takhfif + ISNULL(F_MabHazine_Foroosh_1.MabHazine, 0))) AS MabFactor, 
                      SUM(T_Forosh.Takhfif) AS MabTakhfif, SUM(F_MabHazine_Foroosh_1.MabHazine) AS MabHazine, SUM(T_Forosh.Maliyat) AS MabMaliat, SUM(T_Forosh.Avarez) AS MabAvarez
FROM         (SELECT     FOROSHID, ForoshNumbr, NumberKH, NumSID2, VSabt, PrimarySaid2, Dateeng, DateCreate, DateModified, HesabID, GOID, KOLID, MOINID, THESABID, THESABIDAmel, TextName, TextName1, 
                      TextNameT, DateFo, Takhfif, TextShSA, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, UserID, Shift, FlgSabtFact,
                       SalaTypeid, Maliyat, MablaghKol, Avarez, Ezafat, Poorsant, userCreate, userModify, dateSettlement, ThesabIDSath2
FROM         T_Forosh AS T_Forosh_1
WHERE     (UserID = @UserID) AND (CASE WHEN @OnlyActive <> 0 THEN T_Forosh_1.FlgSabtFact ELSE 1 END = CASE WHEN @OnlyActive <> 0 THEN 0 ELSE 1 END) AND (DateFo = @DateFo)) 
                      AS T_Forosh INNER JOIN
                          (SELECT     FOROSHID, SUM(ABS(FieldN3)) AS FieldN3, SUM(TedadFo) AS TedadFo
                             FROM         [TRANSACTION]
                             GROUP BY FOROSHID) AS DrvMabFact ON T_Forosh.FOROSHID = DrvMabFact.FOROSHID LEFT OUTER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
                      dbo.F_MabHazine_Foroosh() AS F_MabHazine_Foroosh_1 ON T_Forosh.FOROSHID = F_MabHazine_Foroosh_1.FactID LEFT OUTER JOIN
                      T_KolHesab ON T_Forosh.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON T_Forosh.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON T_Forosh.HesabID = T_Heasb.HESABID) AS derivedtbl_1
                                              




			  
End
GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_ChoiceInform]    Script Date: 09/03/2018 09:59:09 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_ChoiceInform]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_ChoiceInform]
GO


/****** Object:  StoredProcedure [dbo].[CQ_ListKala_ChoiceInform]    Script Date: 09/03/2018 09:59:09 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[CQ_ListKala_ChoiceInform] ( 
@CodGroup INT,
  @IndexKalaID INT)           
  AS   

BEGIN
	    
DECLARE @CodKala INT


SELECT     @CodKala=CodKala
FROM         T_Defult
WHERE     (CodGroup = @CodGroup) AND (OPTIONID = @IndexKalaID) AND (GROUPID = 11)

SELECT     FunInfoKalaAll_1.ID AS NameKalaID, CONVERT(float, FunInfoKalaAll_1.KalaNumber) AS KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.Zarib, 
                      FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.KalaNumber AS [کد کالا], FunInfoKalaAll_1.NameKala1 + + N' ' + ISNULL(FunInfoKalaAll_1.note, N' ') AS [نام کالا], 
                      FunInfoKalaAll_1.NameVahed1 AS [نام واحد], ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) AS [مانده انبار], FunInfoKalaAll_1.NameAnbar AS [نام انبار], 
                      FunInfoKalaAll_1.UnitPrice1 AS [قیمت فروش1], ROUND(CONVERT(money, drvTra.MaxUnitPriceKh), 2) AS [آخرین خرید], FunInfoKalaAll_1.UnitPrice2 AS [قیمت فروش2], 
                      FunInfoKalaAll_1.UnitPrice3 AS [قیمت فروش3], FunInfoKalaAll_1.UnitPrice4 AS [قیمت فروش4], FunInfoKalaAll_1.UnitPrice5 AS [قیمت فروش5], FunInfoKalaAll_1.UnitPrice6 AS [قیمت فروش6], 
                      FunInfoKalaAll_1.NameVahed2, drvTra.MaxUnitPriceKh AS UnitPriceKh, FunInfoKalaAll_1.FieldN1, FunInfoKalaAll_1.BarCode AS [ بارکد ], FunInfoKalaAll_1.NameGoKala1 AS [گروه کالا], 
                      FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.UnitPrice1, drvTra.MaxUnitPriceKh AS AkharinKharid, FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, 
                      FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, ROUND(SUM(CONVERT(float, 
                      CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) AS MandeAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber) AS CodKala
FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      dbo.Fun_CQ_ListKala_Sandoogh_Analyz() AS drvTra ON FunInfoKalaAll_1.LinkKalaAid = drvTra.LinkKalaAID
WHERE     (FunInfoKalaAll_1.ID = @CodKala)
GROUP BY FunInfoKalaAll_1.NameKala1 + + N' ' + ISNULL(FunInfoKalaAll_1.note, N' '), ROUND(CONVERT(money, drvTra.MaxUnitPriceKh), 2), FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, 
                      FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.FieldN1, drvTra.MaxUnitPriceKh, FunInfoKalaAll_1.NameAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber), 
                      CONVERT(float, FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice1, 
                      FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.ID, 
                      FunInfoKalaAll_1.Zarib, drvTra.MaxUnitPriceKh, FunInfoKalaAll_1.NameVahed2

END

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_CALC]    Script Date: 09/12/2018 14:59:05 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_CALC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_CALC]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_CALC]    Script Date: 09/12/2018 14:59:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_CALC]
		
		@Userid INT,	
		@TxtLinkTosub NVARCHAR(255)

AS
BEGIN
	

     DECLARE   @Tedad DECIMAL(19,4) 
     DECLARE   @MabFactor DECIMAL(19,4) 
     DECLARE   @MabHazine DECIMAL(19,4) 
     DECLARE   @MabTakhfif DECIMAL(19,4) 
     DECLARE   @MabMaliat DECIMAL(19,4) 
     DECLARE   @MabAvarez DECIMAL(19,4) 
     DECLARE   @MabFactorSF11 DECIMAL(19,4) 



SELECT      @Tedad= SUM(Tedad) ,@MabFactor=  SUM(Tedad * UnitPrice)  , @MabTakhfif=SUM(isnull(FieldN7,0)) , @MabMaliat=SUM(isnull(Maliatkala,0)) , @MabAvarez=SUM(isnull(Avarez,0)) ,@MabFactorSF11= 0
FROM         (SELECT     CASE WHEN ZaribChildUser > 0 THEN Tedad1User ELSE Tedad END AS Tedad, CASE WHEN ZaribChildUser > 0 THEN UnitPriceUser ELSE UnitPrice END AS UnitPrice, FieldN7, Maliatkala, Avarez, 
                      Date1,LinkedID
FROM         TRANSACTION2 AS Temp) TRANSACTION2
WHERE     (date1 = @TxtLinkTosub)
GROUP BY LinkedID

 
SELECT     @MabHazine=SUM(ISNULL(DARBed, 0)) 
FROM         TRANSACTION2
WHERE     (TextAIDFo = @TxtLinkTosub) AND (Flag1 <> 1)
GROUP BY LinkedID

SELECT     @Tedad AS Tedad, @MabFactor AS MabFactor, isnull(@MabTakhfif,0) AS MabTakhfif, isnull(@MabMaliat,0) AS MabMaliat, isnull(@MabAvarez,0) AS MabAvarez, isnull(@MabHazine,0) AS MabHazine	, 0 AS MabFactorSF11		  

END







GO


/****** Object:  StoredProcedure [dbo].[Rep_02123301]    Script Date: 09/25/2018 09:35:00 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_02123301]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_02123301]
GO


/****** Object:  StoredProcedure [dbo].[Rep_02123301]    Script Date: 09/25/2018 09:35:00 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[Rep_02123301]
        
        
        (
      	@SortRep INT,
      	@StartShFact INT, 
      	@EndShFact INT, 
      	@startDate NVARCHAR(10), 
      	@enddate NVARCHAR(10),
      	@AID INT,
      	@Kalaid INT,
      	@Gokid INT,
      	@TxtNameKala  NVARCHAR(255) ,
		@Goid INT, 
		@KolID INT, 
		@HesabID INT, 
		@THesabID INT,
		@KalaIdChild int,
		@FlagInCludeChild int)
 AS 
      
BEGIN  	

/*
@SortRep=forms![TreeViewRep_VazForosh]![Sorting],
@StartShFact= forms![TreeViewRep_VazForosh]![AsShomare],
@EndShFact =forms![TreeViewRep_VazForosh]![TaShomare], 
@startDate =forms![TreeViewRep_VazForosh]![AsDate],
@enddate =forms![TreeViewRep_VazForosh]![TaDate],
@AID =forms![TreeViewRep_VazForosh]![AID],
@Kalaid =forms![TreeViewRep_VazForosh]![KalaID],
@Gokid =forms![TreeViewRep_VazForosh]![GoKala],
@TxtNameKala  =forms![TreeViewRep_VazForosh]![FilterNameKala] ,
@Goid =forms![TreeViewRep_VazForosh]![Goid], 
@KolID =forms![TreeViewRep_VazForosh]![KolID], 
@HesabID =forms![TreeViewRep_VazForosh]![HesabID], 
@THesabID =forms![TreeViewRep_VazForosh]![THesabID],
@KalaIdChild=forms![TreeViewRep_VazForosh]![KalaidChild],
@FlagInCludeChild=iif(forms![TreeViewRep_VazForosh]![FlagIncludeChildKala],1,0)
*/		
		
	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	/* محتوي رشته پرس و جو*/
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);    
  	     	 	
  	set @WhrStatement = N' ';   
  	      	
  	set @ParmAndstr=N' AND '
  	                 
  	SET @ParmDefinition = N'@SortRep INT=NULL,
      						@StartShFact INT=NULL, 
      						@EndShFact INT=NULL, 
      						@startDate NVARCHAR(10)=NULL, 
      						@enddate NVARCHAR(10)=NULL,
      						@AID INT=NULL,
      						@Kalaid INT=NULL,
      						@Gokid INT=NULL,
      						@TxtNameKala  NVARCHAR(255) =NULL,
							@Goid INT=NULL, 
							@KolID INT=NULL, 
							@HesabID INT=NULL, 
							@THesabID INT=NULL,
							@KalaIdChild int=null,
							@FlagInCludeChild int=null';
	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END
	    		
SET @SQLString =N'
        
        
        
SELECT     T_Forosh.FOROSHID, CONVERT(int, T_Forosh.ForoshNumbr) AS Expr1, T_Forosh.DateFo, SUM(ABS([TRANSACTION].FieldN3)) AS MabFact, ABS(SUM([TRANSACTION].FieldN3)) 
                      + ISNULL(F_MabHazine_Foroosh.MabHazine, 0) - ISNULL(T_Forosh.Takhfif, 0) AS Total, T_Forosh.Takhfif, MIN(T_Forosh.DateFo) AS MinOfDateFo, MAX(T_Forosh.DateFo) AS MaxOfDateFo, 
                      @enddate AS enddate, F_MabHazine_Foroosh.MabHazine, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab, CONVERT(int, 
                      T_Forosh.ForoshNumbr) AS FOROSHNumbr, @startDate AS startDate
FROM         [TRANSACTION] INNER JOIN
                      T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID LEFT OUTER JOIN
                      dbo.F_MabHazine_Foroosh() AS F_MabHazine_Foroosh ON T_Forosh.FOROSHID = F_MabHazine_Foroosh.FactID
WHERE     (T_Forosh.DateFo BETWEEN @startDate AND @enddate)


'
                      


		
If(NOT (@Goid  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' ([TRANSACTION].GOID = @Goid ) '; 

		
If(NOT (@KOLID  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' ([TRANSACTION].KOLID = @KOLID ) '; 

If(NOT (@HESABID  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' ([TRANSACTION].HESABID = @HESABID ) '; 	
		
If(NOT (@THESABID  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' ([TRANSACTION].THESABID = @THESABID ) '; 	
		

If(NOT (@AID  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' ([TRANSACTION].AID = @AID ) '; 	

If(NOT (@Kalaid  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' ([TRANSACTION].Kalaid = @Kalaid ) '; 

If(NOT (@Gokid  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' ([TRANSACTION].Gokid = @Gokid ) '; 	
			

  /*  --------------------شرط حساب اطلاعات جزيي کالا  */ 			    
	    
IF(NOT (@KalaIdChild  IS NULL)) 			    
				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KalaIDChild = @KalaIdChild )'; 				    



If(NOT (@StartShFact  IS NULL) AND NOT (@EndShFact  IS NULL) ) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Forosh.ForoshNumbr Between @StartShFact and @EndShFact) '; 


If(NOT (@StartShFact  IS NULL) AND NOT (@EndShFact  IS NULL) ) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Forosh.ForoshNumbr Between @StartShFact and @EndShFact) '; 
		

/*
if(NOT (@TxtNameKala   IS NULL)) 			
BEGIN
	set @TxtNameKala ='%' + @TxtNameKala + '%'	 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_NameKala.NameKala1  Like @TxtNameKala ) '; 				
END 
*/

set @SQLString=@SQLString + @WhrStatement+N' GROUP BY T_Forosh.DateFo, T_Forosh.Takhfif, F_MabHazine_Foroosh.MabHazine, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID), CONVERT(int, 
                      T_Forosh.ForoshNumbr),T_Forosh.ForoshNumbr, T_Forosh.FOROSHID
ORDER BY T_Forosh.DateFo,T_Forosh.ForoshNumbr ASC ' 				
			
      			
			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@StartShFact=@StartShFact,@EndShFact=@EndShFact,@startDate=@startDate,@enddate=@enddate,@AID=@AID,@Kalaid=@Kalaid,@Gokid=@Gokid,@TxtNameKala =@TxtNameKala ,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@KalaIdChild=@KalaIdChild,@FlagInCludeChild=@FlagInCludeChild;  		
                                                

return end 		
GO



/****** Object:  StoredProcedure [dbo].[Rep_02123302]    Script Date: 09/25/2018 09:35:06 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_02123302]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_02123302]
GO



/****** Object:  StoredProcedure [dbo].[Rep_02123302]    Script Date: 09/25/2018 09:35:06 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[Rep_02123302] (  	
  	@SortRep nvarchar(255),  	
  	@NoeSortRep nvarchar(255), 	
  	@Goid int, 	
  	@IdKala int, 	
  	@KalaGO int, 	
  	@AID int, 	
  	@KolID int,  	
  	@HesabID int, 	
  	@THesabID int,  	
  	@StartDate nvarchar(50)='0', 	
  	@Enddate nvarchar(50)='99999999', 	
  	@StartShFact nvarchar(50), 	
  	@EndShFact nvarchar(50), 	
  	@FilterNameKala nvarchar(255),
  	@KalaIdChild int,
  	@FlagInCludeChild int 	 )              AS   
  	     
  	     
  	     /*@SortRep=forms![TreeViewRep_VazForosh]![Sorting].column(0),
  	      @NoeSortRep=forms![TreeViewRep_VazForosh]![Check],
  	      @Goid=forms![TreeViewRep_VazForosh]![Goid],
  	      @KolID=forms![TreeViewRep_VazForosh]![KolID],
  	      @HesabID=forms![TreeViewRep_VazForosh]![HesabID],
  	      @THesabID=forms![TreeViewRep_VazForosh]![THesabID],
  	      @KalaGO=forms![TreeViewRep_VazForosh]![GoKala],
  	      @IdKala=forms![TreeViewRep_VazForosh]![KalaID],
  	      @AID=forms![TreeViewRep_VazForosh]![AID],
  	      @StartShFact=forms![TreeViewRep_VazForosh]![AsShomare],
  	      @EndShFact=forms![TreeViewRep_VazForosh]![TaShomare],
  	      @startDate=forms![TreeViewRep_VazForosh]![AsDate],
  	      @enddate=forms![TreeViewRep_VazForosh]![TaDate],
  	      @FilterNameKala=forms![TreeViewRep_VazForosh]![FilterNameKala],
  	      @KalaIdChild=forms![TreeViewRep_VazForosh]![KalaidChild],
  	      @FlagInCludeChild=iif(forms![TreeViewRep_VazForosh]![FlagIncludeChildKala],1,0)*/
  	BEGIN 	SET NOCOUNT ON; 	 	   
  	DECLARE @SQLString  NVARCHAR(4000);  	   
  	DECLARE @WhrStatement  NVARCHAR(2000);         	   
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
  	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
  	set @WhrStatement  = N'';         	   
  	set @ParmAndstr = N'';                  	   
  	SET @ParmDefinition = N'@SortRep nvarchar(255)=null,  							
  							@NoeSortRep nvarchar(255)=null, 							
  							@IdKala int, 							
  							@KalaGO int, 							
  							@AID int, 							
  							@Goid int=null, 							
  							@KolID int=null,  							
  							@HesabID nvarchar(50)=null, 							
  							@THesabID int=null, 							
  							@StartDate nvarchar(50)=''0'', 							
  							@Enddate nvarchar(50)=''99999999'', 							
  							@StartShFact nvarchar(50)=null, 							
  							@EndShFact nvarchar(50)=null, 							
  							@FilterNameKala nvarchar(255)=null,
  							@KalaIdChild int=null,
  							@FlagInCludeChild int=null  ';            	   /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		    BEGIN       if(@StartDate IS NULL) 			   BEGIN 				   set @StartDate='0'; 				   END         if(@Enddate IS NULL) 			   BEGIN 				   set @Enddate='99999999'; 				   END         if(@NoeSortRep IS NULL) 			   BEGIN 				   set @NoeSortRep=0; 				   END        	  		    
  SET @SQLString ='SELECT     T_Forosh.FOROSHID, T_Forosh.ForoshNumbr, CASE WHEN isnull(@FlagInCludeChild, 0) > 0 AND KalaIDChild > 0 THEN T_NameKala.NameKala1 + ''('' + T_RelateKala.NameKala2 + '')'' ELSE T_NameKala.NameKala1 END COLLATE Arabic_CI_AS AS NameKala, 
                      T_NameVahed.NameVahed, T_Anbar.NameAnbar, CONVERT(float, T_NameKala.KalaNumber) AS KalaNumber, T_NameGoKala.NameGoKala1, T_NameKala.ID, T_NameKala.VAHED1, 
                      [TRANSACTION].AID, [TRANSACTION].GOKID, [TRANSACTION].GOID, [TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID, T_NameKala.Zarib, MIN(dbo.F1StartDate()) 
                      AS MinOfDateKh, MAX(dbo.F1EndDate()) AS MaxOfDatekh, SUM(ABS([TRANSACTION].FieldN3)) AS SumFieldN3, CONVERT(float, SUM(CASE WHEN isnull(@FlagInCludeChild, 0) > 0 AND 
                      KalaIDChild > 0 THEN [TRANSACTION].Tedad1User ELSE [TRANSACTION].TedadFo END)) AS SumOftedadkh, @startDate AS startDate, @enddate AS enddate, ROUND(SUM([TRANSACTION].TedadFo) 
                      / T_NameKala.Zarib, 2) AS Karton, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab, T_NameKala.KalaNumber AS Expr1, 
                      T_Forosh.DateFo, CONVERT(nvarchar(255), [TRANSACTION].Sharh) AS Sharhkala, T_RelateKala.NameKala2, [TRANSACTION].Tedad1User, [TRANSACTION].UnitPriceUser, 
                      [TRANSACTION].KalaIDChild
FROM         [TRANSACTION] INNER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID INNER JOIN
                      T_Anbar ON [TRANSACTION].AID = T_Anbar.AID INNER JOIN
                      T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID INNER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 INNER JOIN
                      T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID LEFT OUTER JOIN
                      T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2
GROUP BY CASE WHEN isnull(@FlagInCludeChild, 0) > 0 AND KalaIDChild > 0 THEN T_NameKala.NameKala1 + ''('' + T_RelateKala.NameKala2 + '')'' ELSE T_NameKala.NameKala1 END COLLATE Arabic_CI_AS,T_NameKala.NameKala1, T_NameKala.VAHED1, T_NameKala.KalaNumber, 
                      T_NameKala.Zarib, [TRANSACTION].AID, [TRANSACTION].GOID, [TRANSACTION].KOLID, [TRANSACTION].GOKID, [TRANSACTION].HESABID, [TRANSACTION].THESABID, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID), T_NameKala.ID, T_NameVahed.NameVahed, T_Anbar.NameAnbar, T_NameGoKala.NameGoKala1, 
                      T_Forosh.FOROSHID, T_Forosh.ForoshNumbr, T_Forosh.DateFo, CONVERT(nvarchar(255), [TRANSACTION].Sharh), T_RelateKala.NameKala2, [TRANSACTION].Tedad1User, 
                      [TRANSACTION].UnitPriceUser, [TRANSACTION].KalaIDChild
HAVING      (T_Forosh.DateFo BETWEEN @startDate AND @enddate)  '   

            /*  --------------------شرط حساب سرگروه  */ 			   
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END    
   
   
               /*  --------------------شرط حساب سرگروه  */ 			   
   set @ParmAndstr=' AND ' 			    
   if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END               
   /*  --------------------شرط حساب کل  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KolID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END              
   /*  --------------------شرط حساب معین  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@HESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END          
   /*  --------------------شرط حساب تفصیلی  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@THESABID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END             
   /*  --------------------شرط حساب کالا  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@IdKala  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_NameKala.ID = @IdKala )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END    
   
   /*  --------------------شرط حساب اطلاعات جزيي کالا  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KalaIdChild  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KalaIDChild = @KalaIdChild )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END 
               
   /*  --------------------شرط حساب گروه کالا  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@KalaGO  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOKID = @KalaGO )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END   	         
   /*  --------------------شرط حساب انبار کالا  */ 			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@AID  IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].AID = @AID )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END   	 	
	 	 	
   /*  --------------------شرط  شماره فاکتور  */ 
   
   
   set @ParmAndstr=' AND ' 			    
   if(NOT (@StartShFact IS NULL) AND NOT (@EndShFact IS NULL)) 			    
   BEGIN 				    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'T_Forosh.ForoshNumbr between @StartShFact and @EndShFact '; 				    
   	set @ParmAndstr=' AND ' 			    
   END   

   
   /* فيلتر نام کالا */			    
   set @ParmAndstr=' AND ' 			    
   if(NOT (@FilterNameKala  IS NULL))     
   BEGIN 	    	
   	set @FilterNameKala='%'+@FilterNameKala+'%'	    
   	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_NameKala.NameKala1 LIKE  @FilterNameKala )'; 				    
   	set @ParmAndstr=' AND ' 			    
   END    	    
   if(NOT (@WhrStatement  IS NULL)) 			    
   BEGIN 				 		
   	set @SQLString= @SQLString + @WhrStatement     
   END       	
   set @WhrStatement=null 	 	
   /* --------------------  نام کالا  1 مرتب سازی */ 			     
   if (@SortRep like 'NameKala1') 			     		     
   BEGIN      	 		
   	if ((@NoeSortRep<>0) )		 		
   	BEGIN 		 			
   		set @WhrStatement = N'  ORDER BY T_NameKala.NameKala1 DESC '; 	 		
   	END 		 		
   	if ((@NoeSortRep=0) )		 		
   	BEGIN 		 			
   		set @WhrStatement = N'  ORDER BY T_NameKala.NameKala1  ASC '; 	 		
   	END 	    		     
   END  	 	
   /* --------------------  کد کالا  2 مرتب سازی */ 			     
   if (@SortRep like 'KalaNumber')     		     
   BEGIN      	 		
   	if (@NoeSortRep<>0)		 		
   	BEGIN 		 			
   		set @WhrStatement = N'  ORDER BY KalaNumber DESC '; 	 		
   	END 		 		
   	if ((@NoeSortRep=0) )		 		
   	BEGIN 		 			
   		set @WhrStatement = N'  ORDER BY KalaNumber ASC '; 	 		
   	END 	    		     
   END       	 	
   /* --------------------  کد کالا  2 مرتب سازی */ 			     
   if (@SortRep IS NULL)     		     
   BEGIN 		
   	set @WhrStatement = N'  ORDER BY KalaNumber ASC '; 	     
   END  				 	   
   if(NOT (@WhrStatement  IS NULL)) 			    
   BEGIN 				 		
   	set @SQLString= @SQLString + @WhrStatement     
   END     	      
   EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@NoeSortRep=@NoeSortRep,@AID=@AID,@KalaGO=@KalaGO,@IdKala=@IdKala,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@StartDate=@StartDate,@Enddate=@Enddate,@StartShFact=@StartShFact,@EndShFact=@EndShFact,@FilterNameKala=@FilterNameKala,@KalaIdChild=@KalaIdChild,@FlagInCludeChild=@FlagInCludeChild;  		     
  	END 		      
  	/* --------پایان چاپ*/  		 		  
  	return END 

GO




/****** Object:  StoredProcedure [dbo].[Rep_02123310]    Script Date: 09/25/2018 09:35:13 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_02123310]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_02123310]
GO



/****** Object:  StoredProcedure [dbo].[Rep_02123310]    Script Date: 09/25/2018 09:35:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


        CREATE PROCEDURE [dbo].[Rep_02123310]
        
        
        (
      	@SortRep INT,
      	@StartShFact INT, 
      	@EndShFact INT, 
      	@startDate NVARCHAR(10), 
      	@enddate NVARCHAR(10),
      	@AID INT,
      	@Kalaid INT,
      	@Gokid INT,
      	@TxtNameKala  NVARCHAR(255) ,
		@Goid INT, 
		@KolID INT, 
		@HesabID INT, 
		@THesabID INT,
		@KalaIdChild int,
		@FlagInCludeChild int)
 AS 
      
BEGIN  	

/*
@SortRep=forms![TreeViewRep_VazForosh]![Sorting],
@StartShFact= forms![TreeViewRep_VazForosh]![AsShomare],
@EndShFact =forms![TreeViewRep_VazForosh]![TaShomare], 
@startDate =forms![TreeViewRep_VazForosh]![AsDate],
@enddate =forms![TreeViewRep_VazForosh]![TaDate],
@AID =forms![TreeViewRep_VazForosh]![AID],
@Kalaid =forms![TreeViewRep_VazForosh]![KalaID],
@Gokid =forms![TreeViewRep_VazForosh]![GoKala],
@TxtNameKala  =forms![TreeViewRep_VazForosh]![FilterNameKala] ,
@Goid =forms![TreeViewRep_VazForosh]![Goid], 
@KolID =forms![TreeViewRep_VazForosh]![KolID], 
@HesabID =forms![TreeViewRep_VazForosh]![HesabID], 
@THesabID =forms![TreeViewRep_VazForosh]![THesabID],
@KalaIdChild=forms![TreeViewRep_VazForosh]![KalaidChild],
@FlagInCludeChild=iif(forms![TreeViewRep_VazForosh]![FlagIncludeChildKala],1,0)
*/		
		
	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	/* محتوي رشته پرس و جو*/
  	DECLARE @WhrStatement  NVARCHAR(2000);   
  	DECLARE @WhrStatement1  NVARCHAR(2000);      	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000);    
  	     	 	
  	set @WhrStatement = N' ';   
  	set @WhrStatement1 = N' ';  
  	      	
  	set @ParmAndstr=N' AND '
  	                 
  	SET @ParmDefinition = N'@SortRep INT=NULL,
      						@StartShFact INT=NULL, 
      						@EndShFact INT=NULL, 
      						@startDate NVARCHAR(10)=NULL, 
      						@enddate NVARCHAR(10)=NULL,
      						@AID INT=NULL,
      						@Kalaid INT=NULL,
      						@Gokid INT=NULL,
      						@TxtNameKala  NVARCHAR(255) =NULL,
							@Goid INT=NULL, 
							@KolID INT=NULL, 
							@HesabID INT=NULL, 
							@THesabID INT=NULL,
							@KalaIdChild int=null,
							@FlagInCludeChild int=null';
	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END    
	
	
	
If(NOT (@AID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 +  N' and ([TranForosh].AID = @AID ) '; 	

If(NOT (@Kalaid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 +  N'  and ([TranForosh].Kalaid = @Kalaid ) '; 

If(NOT (@Gokid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 +  N'  and ([TranForosh].Gokid = @Gokid ) '; 

   /*  --------------------شرط حساب اطلاعات جزيي کالا  */ 			    
	    
IF(NOT (@KalaIdChild  IS NULL)) 			    
   	set @WhrStatement1= @WhrStatement1 +  N' and ([TranForosh].KalaIDChild = @KalaIdChild )'; 
   					
SET @SQLString =N'
        
        
        
SELECT     T_Forosh.DateFo, SUM(DrvTRANSACTION.TedadFo) AS SumOfTedadfo, CONVERT(MONEY,SUM(F_MabHazine_Foroosh.MabHazine)) AS MabHazine, CONVERT(MONEY,SUM(T_Forosh.Takhfif)) AS Takhfif, 
                      SUM(ABS(DrvTRANSACTION.FieldN3)) AS MabFact,CONVERT(MONEY, SUM(ABS(DrvTRANSACTION.FieldN3)) + SUM(ISNULL(F_MabHazine_Foroosh.MabHazine, 0)) - SUM(T_Forosh.Takhfif)) AS Total, 
                      MIN(T_Forosh.DateFo) AS MinOfDatefo, MAX(T_Forosh.DateFo) AS MaxOfDatefo, @startDate AS startDate, @enddate AS enddate
FROM         T_Forosh INNER JOIN
                          (SELECT     FOROSHID, Date1, SUM(CASE WHEN isnull(@FlagInCludeChild, 0) > 0 AND KalaIDChild > 0 THEN Tedad1User else TedadFo end) AS TedadFo, SUM(FieldN3) AS FieldN3, 1 AS Expr1
FROM         [TRANSACTION] AS TranForosh
WHERE     (1 = 1)' + @WhrStatement1 + '
GROUP BY Date1, FOROSHID) AS DrvTRANSACTION ON T_Forosh.FOROSHID = DrvTRANSACTION.FOROSHID LEFT OUTER JOIN
                      dbo.F_MabHazine_Foroosh() AS F_MabHazine_Foroosh ON T_Forosh.FOROSHID = F_MabHazine_Foroosh.FactID
WHERE     (T_Forosh.DateFo BETWEEN @startDate AND @enddate)



'
                      


		
If(NOT (@Goid  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Forosh.GOID = @Goid ) '; 

		
If(NOT (@KOLID  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Forosh.KOLID = @KOLID ) '; 

If(NOT (@HESABID  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Forosh.HESABID = @HESABID ) '; 	
		
If(NOT (@THESABID  IS NULL)) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Forosh.THESABID = @THESABID ) '; 	
		

	
			



If(NOT (@StartShFact  IS NULL) AND NOT (@EndShFact  IS NULL) ) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Forosh.ForoshNumbr Between @StartShFact and @EndShFact) '; 



/*
if(NOT (@TxtNameKala   IS NULL)) 			
BEGIN
	set @TxtNameKala ='%' + @TxtNameKala + '%'	 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_NameKala.NameKala1  Like @TxtNameKala ) '; 				
END 
*/

set @SQLString=@SQLString + @WhrStatement+N' GROUP BY T_Forosh.DateFo
ORDER BY T_Forosh.DateFo Asc ' 				
			
      			
			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@StartShFact=@StartShFact,@EndShFact=@EndShFact,@startDate=@startDate,@enddate=@enddate,@AID=@AID,@Kalaid=@Kalaid,@Gokid=@Gokid,@TxtNameKala =@TxtNameKala ,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@FlagInCludeChild=@FlagInCludeChild,@KalaIdChild=@KalaIdChild;  		
                                                

return end 		

GO




/****** Object:  StoredProcedure [dbo].[Rep_02123346]    Script Date: 09/25/2018 09:35:19 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_02123346]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_02123346]
GO



/****** Object:  StoredProcedure [dbo].[Rep_02123346]    Script Date: 09/25/2018 09:35:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE PROCEDURE [dbo].[Rep_02123346](
      	@SortRep INT,
      	@StartShFact INT, 
      	@EndShFact INT, 
      	@startDate NVARCHAR(10), 
      	@enddate NVARCHAR(10),
      	@AID INT,
      	@Kalaid INT,
      	@Gokid INT,
      	@TxtNameKala  NVARCHAR(255) ,
		@Goid INT, 
		@KolID INT, 
		@HesabID INT, 
		@THesabID INT,
  		@KalaIdChild int,
  		@FlagInCludeChild int)
 AS 
      
BEGIN  	

/*


@SortRep =forms![TreeViewRep_VazForosh]![Sorting],
@StartShFact= forms![TreeViewRep_VazForosh]![AsShomare], 
@EndShFact =forms![TreeViewRep_VazForosh]![TaShomare], 
@startDate =forms![TreeViewRep_VazForosh]![AsDate], 
@enddate =forms![TreeViewRep_VazForosh]![TaDate],
@AID =forms![TreeViewRep_VazForosh]![AID],
@Kalaid =forms![TreeViewRep_VazForosh]![KalaID],
@Gokid =forms![TreeViewRep_VazForosh]![GoKala],
@TxtNameKala  =forms![TreeViewRep_VazForosh]![FilterNameKala] ,
@Goid =forms![TreeViewRep_VazForosh]![Goid], 
@KolID =forms![TreeViewRep_VazForosh]![KolID], 
@HesabID =forms![TreeViewRep_VazForosh]![HesabID], 
@THesabID =forms![TreeViewRep_VazForosh]![THesabID],
@KalaIdChild=forms![TreeViewRep_VazForosh]![KalaidChild],
@FlagInCludeChild=iif(forms![TreeViewRep_VazForosh]![FlagIncludeChildKala],1,0)
  	      
*/		
		
	SET NOCOUNT ON; 	 	
  	DECLARE @SQLString  NVARCHAR(4000);  	/* محتوي رشته پرس و جو*/
  	DECLARE @WhrStatement  NVARCHAR(2000);         	
  	DECLARE @ParmDefinition  NVARCHAR(2000);         	
  	DECLARE @ParmAndstr  NVARCHAR(2000); 
  	DECLARE @WhrStatement1  NVARCHAR(2000);    
  	     	 	
  	set @WhrStatement = N' ';   
  	      	
  	set @ParmAndstr=N' AND '
  	  
  	set @WhrStatement1=N' '    
  	      
If(NOT (@Goid  IS NULL)) 			
set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (GOID = @Goid ) '; 

		
If(NOT (@KOLID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (KOLID = @KOLID ) '; 

If(NOT (@HESABID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (HESABID = @HESABID ) '; 	
		
If(NOT (@THESABID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (THESABID = @THESABID ) '; 	
		

If(NOT (@AID  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (AID = @AID ) '; 	

If(NOT (@Kalaid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (Kalaid = @Kalaid ) '; 

If(NOT (@Gokid  IS NULL)) 			
		set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N' (Gokid = @Gokid ) '; 	

   /*  --------------------شرط حساب اطلاعات جزيي کالا  */ 			    
	    
IF(NOT (@KalaIdChild  IS NULL)) 			    
				    
   	set @WhrStatement1= @WhrStatement1 + @ParmAndstr + N'(KalaIDChild = @KalaIdChild )'; 				    

   		           
  	SET @ParmDefinition = N'@SortRep INT=NULL,
      						@StartShFact INT=NULL, 
      						@EndShFact INT=NULL, 
      						@startDate NVARCHAR(10)=NULL, 
      						@enddate NVARCHAR(10)=NULL,
      						@AID INT=NULL,
      						@Kalaid INT=NULL,
      						@Gokid INT=NULL,
      						@TxtNameKala  NVARCHAR(255) =NULL,
							@Goid INT=NULL, 
							@KolID INT=NULL, 
							@HesabID INT=NULL, 
							@THesabID INT=NULL,
							@KalaIdChild int=null,
							@FlagInCludeChild int=null';

	if(@StartDate IS NULL) 			   
	BEGIN 				   
		set @StartDate='0'; 				   
	END         
	if(@Enddate IS NULL) 			   
	BEGIN 				   
		set @Enddate='99999999'; 				   
	END    	
	
	
		
SET @SQLString =N'  SELECT     CONVERT(money, SUM(TRANSACTION1.MabFact0)) AS MabFact, CONVERT(money, SUM(TRANSACTION1.MabFact0) + SUM(ISNULL(F_MabHazine_Foroosh.MabHazine, 0)) 
                      - SUM(ISNULL(T_Forosh.Takhfif, 0))) AS Total, CONVERT(money, SUM(ISNULL(T_Forosh.Takhfif, 0))) AS Takhfif, MIN(T_Forosh.DateFo) AS MinOfDateFo, MAX(T_Forosh.DateFo) AS MaxOfDateFo, 
                      @enddate AS enddate, CONVERT(money, SUM(F_MabHazine_Foroosh.MabHazine)) AS MabHazine, dbo.HesabName(TRANSACTION1.KOLID, TRANSACTION1.HESABID, 
                      TRANSACTION1.THESABID) AS namehesab, @startDate AS startDate, TRANSACTION1.GOID, TRANSACTION1.KOLID, TRANSACTION1.HESABID, TRANSACTION1.THESABID
FROM         T_Forosh INNER JOIN
                          (SELECT     GOID, KOLID, HESABID, THESABID, FOROSHID, SUM(ABS(FieldN3)) AS MabFact0
FROM         [TRANSACTION] AS TRANSACTION_1
WHERE     (NOT (FOROSHID IS NULL)) ' + @WhrStatement1 + '
GROUP BY GOID, KOLID, HESABID, THESABID, FOROSHID) AS TRANSACTION1 ON T_Forosh.FOROSHID = TRANSACTION1.FOROSHID LEFT OUTER JOIN
                      dbo.F_MabHazine_Foroosh() AS F_MabHazine_Foroosh ON T_Forosh.FOROSHID = F_MabHazine_Foroosh.FactID LEFT OUTER JOIN
                      dbo.F_MabTakhfif_Foroosh() AS F_MabTakhfif_Foroosh ON T_Forosh.FOROSHID = F_MabTakhfif_Foroosh.FOROSHID
WHERE     (T_Forosh.DateFo BETWEEN @startDate AND @enddate)

'
  			


If(NOT (@StartShFact  IS NULL) AND NOT (@EndShFact  IS NULL) ) 			
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Forosh.ForoshNumbr Between @StartShFact and @EndShFact) '; 




/*
if(NOT (@TxtNameKala   IS NULL)) 			
BEGIN
	set @TxtNameKala ='%' + @TxtNameKala + '%'	 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_NameKala.NameKala1  Like @TxtNameKala ) '; 				
END 
*/

set @SQLString=@SQLString + @WhrStatement+N' GROUP BY dbo.HesabName(TRANSACTION1.KOLID, TRANSACTION1.HESABID, TRANSACTION1.THESABID), TRANSACTION1.GOID, TRANSACTION1.KOLID,TRANSACTION1.HESABID, 
                      TRANSACTION1.THESABID ORDER BY dbo.HesabName(TRANSACTION1.KOLID, TRANSACTION1.HESABID, TRANSACTION1.THESABID) ASC' 				
			
      			
			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@StartShFact=@StartShFact,@EndShFact=@EndShFact,@startDate=@startDate,@enddate=@enddate,@AID=@AID,@Kalaid=@Kalaid,@Gokid=@Gokid,@TxtNameKala =@TxtNameKala ,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@KalaIdChild=@KalaIdChild,@FlagInCludeChild=@FlagInCludeChild;  		
                                                

return end 		
			
													                      

GO




/****** Object:  StoredProcedure [dbo].[Rep_0523893819]    Script Date: 10/04/2018 10:54:58 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_0523893819]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_0523893819]
GO


/****** Object:  StoredProcedure [dbo].[Rep_0523893819]    Script Date: 10/04/2018 10:54:58 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

    CREATE PROCEDURE [dbo].[Rep_0523893819] (  
    	@pvchAction nvarchar(50)='Analyz',  
    	@startDate nvarchar(50),  
    	@ENDdate nvarchar(50),  
    	@IdKala int,  @IDA int,  
    	@KalaGO int,   
    	@Goid int,  
    	@KolID int,  
    	@HesabID int,  
    	@THesabID int,  
    	@NoeFact INT, 
    	@NameKala nvarchar(255) )              	
    	
    	AS        	
    	BEGIN 	  
    		/*  @pvchAction='Analyz',  
    		*   @NoeFact=forms![TreeViewRep_HesabAnbar]![Nfaktor],  
    		*   @THesabId=forms![TreeViewRep_HesabAnbar]![THesabId],  
    		*   @HesabId=forms![TreeViewRep_HesabAnbar]![HesabId],  
    		*   @Goid=forms![TreeViewRep_HesabAnbar]![Goid],  
    		*   @Kolid=forms![TreeViewRep_HesabAnbar]![Kolid],  
    		*   @KalaGo=forms![TreeViewRep_HesabAnbar]![GoKala],  
    		*   @IDA=forms![TreeViewRep_HesabAnbar]![AID],  
    		*   @startDate=forms![TreeViewRep_HesabAnbar]![AsDate],  
    		*   @enddate=forms![TreeViewRep_HesabAnbar]![TaDate],  
    		*   @IdKala=forms![TreeViewRep_HesabAnbar]![KalaID],  
    		*   @NameKala=forms![TreeViewRep_HesabAnbar]![FilterNameKala]  */  	   	
    		
    		SET NOCOUNT ON; 	 	   	
    		
    		DECLARE @SQLString  NVARCHAR(MAX);  	/* محتوي رشته پرس و جو*/   	
    		DECLARE @WhrStatement  NVARCHAR(MAX);         	   	
    		DECLARE @ParmDefinition  NVARCHAR(MAX);         	   	
    		DECLARE @ParmAndstr  NVARCHAR(MAX);       	     	 	   	
    		
    		set @WhrStatement = N' ';         	   	
    		set @ParmAndstr = N' ';                     	
    		SET @ParmDefinition = N'@IdKala int=null, 								   							
									@IDA int=null, 								   							
									@KalaGO int=null, 								   							
									@startDate nvarchar(50)=''0'', 								   							
									@ENDdate nvarchar(50)=''99999999'', 								   							
									@KolID int=null, 								   							
									@HesabID int=null, 								   							
									@Goid int=null , 								   							
									@THesabID int=null, 								   							
									@NoeFact int=null, 								   							
									@pvchAction nvarchar(50)=''Analyz'',
									@NameKala nvarchar(255)=null'; 	

if(@StartDate IS NULL) 			    	

BEGIN 				    		
	set @StartDate='0'; 				    	
END          	

if(@Enddate IS NULL) 			    	
BEGIN 				    		
	set @Enddate='99999999'; 				    	
END    							            	   	   	

/* Build the SQL string one time. */      
/* Specify the parameter format one time. */ 	 		    	/* -----------------شروع آنالیز*/ 			    	
/* 	--------------شروع عملیات چاپ*/    	

IF (@pvchAction is NULL)   				
SET @pvchAction ='Analyz'   				   	
if(@pvchAction ='Analyz') 		   	
BEGIN 			 			    	
	DECLARE @tmpdbname varchar(50) 	  		    	
	set @tmpdbname= right( REPLACE( CONVERT(uniqueidentifier, newid()), '-', ''),8) ; 	  		  	  		   	  		  	  		    	
	IF OBJECT_ID('tempdb..##' + @tmpdbname +'' ) IS NOT NULL 				   	
	exec ('DROP TABLE ##' + @tmpdbname +''); 				 	  		    	
	SET @SQLString =N' SELECT     FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, 
					   FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1,                        
					   FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.NameVahed1, 
					   FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.BarCode,                        
					   FunInfoKalaAll_1.note, SubTransactionKala_1.GOID, SubTransactionKala_1.KolID, SubTransactionKala_1.HesabID, 
					   SubTransactionKala_1.THesabID, SubTransactionKala_1.Tarikh,SubTransactionKala_1.ShomareFactor, SubTransactionKala_1.Namehesab, 
					   SubTransactionKala_1.NoeFa1, SubTransactionKala_1.TedadVarde, SubTransactionKala_1.TedadSadre,                        
					   convert(money,SubTransactionKala_1.MVarde) as MVarde,
					   convert(money,SubTransactionKala_1.MVarde/(case when SubTransactionKala_1.TedadVarde<>0 then SubTransactionKala_1.TedadVarde else 1 end )) as Phi_M_KH,
					   convert(money,SubTransactionKala_1.MSadre/(case when SubTransactionKala_1.TedadSadre>0 then SubTransactionKala_1.TedadSadre else 1 end ))  as Phi_M_Fo, 
					   convert(money,SubTransactionKala_1.MSadre) as MSadre , SubTransactionKala_1.SharhKala, SubTransactionKala_1.NoeFa, 0 AS Radif, 0 AS TedadRadifi,
					   0 AS MandeRadifi, 0 AS Phi_M ,SubTransactionKala_1.idFact                       
					   INTO         ##' + @tmpdbname +' 					  
	                   FROM         dbo.SubTransactionKala() AS SubTransactionKala_1 INNER JOIN                       
	                   dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON SubTransactionKala_1.KALAID = FunInfoKalaAll_1.ID AND SubTransactionKala_1.AID = FunInfoKalaAll_1.AID '    
/* --------------------   شرط حساب */ 			  
/* INTO         ##' + @tmpdbname +'  */ 
if(@WhrStatement=N' ') 	
	set @ParmAndstr=N' WHERE ' 	 
else 	
	set @ParmAndstr=N' AND '	

If(NOT (@THesabID  IS NULL)) 			 		
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (SubTransactionKala_1.THesabID = @THesabID ) '; 
					   
if(@WhrStatement=N' ') 	
	set @ParmAndstr=N' WHERE ' 	 
else 	
	set @ParmAndstr=N' AND '	
	 	 
if (NOT (@HesabID  IS NULL)) 			 	
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (SubTransactionKala_1.HesabID = @HesabID ) '; 				  

if(@WhrStatement=N' ') 	
	set @ParmAndstr=N' WHERE ' 	 
else 	
	set @ParmAndstr=N' AND '	  	
			 
if(NOT (@Goid IS NULL)) 			 	
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (SubTransactionKala_1.Goid = @Goid) '; 				  

if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 else 	set @ParmAndstr=N' AND '	 	 

if (NOT (@KolID IS NULL)) 			 	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (SubTransactionKala_1.KolID = @KolID ) '; 	

/* -----------------------شرط انبار روی ساب پرس وجو*/ 			  

if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 else 	set @ParmAndstr=N' AND '	 	  				 

if(NOT (@IDA  IS NULL)) set @WhrStatement= @WhrStatement + @ParmAndstr + N'(FunInfoKalaAll_1.AID = @IDA )'; 				 	 						  

/* -----------------------شرط کالا روی ساب پرس وجو*/ 			 

if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 else 	set @ParmAndstr=N' AND '	 				 

if(NOT (@IdKala  IS NULL)) 	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(FunInfoKalaAll_1.ID = @IdKala )'; 				   

if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 else 	set @ParmAndstr=N' AND '	 				 

if(NOT (@NameKala   IS NULL)) 			 
	BEGIN 	
		set @NameKala ='%' + @NameKala + '%'	 				 
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameKala1  Like @NameKala ) '; 				 
		set @ParmAndstr=' AND ' 			 
	END 				 					  

/* 	-----------------------شرط گروه انبار روی ساب پرس وجو*/ 			 
if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 else 	set @ParmAndstr=N' AND '	 			 
if(NOT (@KalaGO  IS NULL)) 	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @KalaGO )'; 				 
/* 	-----------------------شرط گروه انبار روی ساب پرس وجو*/ 			 
if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 ELSE  	set @ParmAndstr=N' AND '	 			 
IF ((NOT (@startDate  IS NULL) ) AND NOT (@ENDdate  IS NULL)) set @WhrStatement= @WhrStatement + @ParmAndstr + N'(SubTransactionKala_1.Tarikh  BetWeen @startDate and  @ENDdate)'; 			 			   

/* --------------------  نوع عمليات */              

if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 else 	set @ParmAndstr=N' AND '	 			 
if(NOT (@NoeFact  IS NULL)) set @WhrStatement= @WhrStatement + @ParmAndstr + N' ( SubTransactionKala_1.NoeFa= @NoeFact) '; 				 	              
set @SQLString=@SQLString + @WhrStatement+N' ORDER BY FunInfoKalaAll_1.ID, SubTransactionKala_1.Tarikh, SubTransactionKala_1.TedadVarde DESC '
set @SQLString=@SQLString + N'Declare Cr2 Cursor Local for 						 
							  Select ID, TedadVarde, TedadSadre, Mvarde from [##' + @tmpdbname +']  						 
							  for Update
							  DECLARE @KALAID_1 bigint 						 
							  DECLARE @Radif_2 int 						 
							  DECLARE @TedadVarde_1 decimal(19,4) 						 
							  DECLARE @TedadSadre_1 decimal(19,4) 						 						 						 
							  DECLARE @Mvarde_1 decimal(19,4) 						 
							  DECLARE @PHI_M_2 decimal(19,4) 						 
							  DECLARE @KalaID_GH bigint 						 
							  DECLARE @MTedadi decimal(19,4) 						 
							  DECLARE @MRiali decimal(19,4) 						 
							  DECLARE @PHI_M_GH decimal(19,4)   														 		 						 
							  set @KalaID_GH=0 						 
							  set @PHI_M_2=0 						 
							  set @MTedadi=0 						 
							  set @MRiali=0 						 
							  set @PHI_M_GH=0 						 
							  Set @Radif_2=0  						 						 						 
							  Open Cr2    						 
							  fetch Next from Cr2 into @KALAID_1,@TedadVarde_1,@TedadSadre_1,@Mvarde_1 						 
							  while @@FETCH_STATUS=0 							  
							  Begin 								 	
							  	if(@KALAID_1 <> @KalaID_GH) 								 		
							  	begin 									 			
							  		Set @Radif_2=0 									 			
							  		set	@PHI_M_2 = 0 									 			
							  		set @MRiali=0 									 			
							  		if (@TedadSadre_1>0) Set @PHI_M_GH = 1 									 									 			
							  		set @MTedadi=0 								 		
							  	end  										 								 								 	
							  	Set @KalaID_GH = @KALAID_1 								 								 	
							  	Set @Radif_2=@Radif_2 + 1 								 								 	
							  	Set @MTedadi=@MTedadi+(@TedadVarde_1-@TedadSadre_1) 		 								 	
							  	set @MRiali=@MRiali+@Mvarde_1-(@PHI_M_GH * @TedadSadre_1)  	
							  	if (@TedadVarde_1>0) and (@MTedadi<>0) Set @PHI_M_2 = round(ABS(@MRiali/@MTedadi),0) 		
							  	if (@MTedadi=0) Set @PHI_M_2 = 0   	
							  	if (@TedadSadre_1>0) 		
							  	Begin 			
							  		Set @PHI_M_2=@PHI_M_GH 			
							  		set @MRiali=@PHI_M_GH *@MTedadi 		
							  	End 		 									 								 
							  	
							  	Set @PHI_M_GH=@PHI_M_2  
							  	Set @MTedadi=Round(Convert(Float,@MTedadi),2)  								 
							  	Update ##' + @tmpdbname +' 
							  	set Radif=@Radif_2,
							  		PHI_M=@PHI_M_2 , 
							  		TedadRadifi=@MTedadi, 
							  		MandeRadifi=@MRiali 
							  							Where current of cr2; 								 	 
							  							fetch Next from Cr2 into @KALAID_1,@TedadVarde_1,@TedadSadre_1,@Mvarde_1			 
							  End	 						 						 
							  Close cr2 						
							  deallocate cr2	' 	 	
							  		 
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@Goid=@Goid,@pvchAction=@pvchAction,@IDA=@IDA,@NoeFact=@NoeFact,@THesabID=@THesabID,@HesabID=@HesabID,@KolID=@KolID,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@ENDdate=@ENDdate,@NameKala=@NameKala; 			 			 
EXEC ('select * from [##' + @tmpdbname +'] 				DROP TABLE ##' + @tmpdbname +''); 		

END                                                   
return END                       
GO







/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1sf1]    Script Date: 10/09/2018 08:59:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_Sandoogh_Analyz1sf1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1sf1]
GO


/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1sf1]    Script Date: 10/09/2018 08:59:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1sf1] (  
      	@AID int, 
      	@OperType int, 
      	@LinkMasterID int, 
      	@GoKID INT,
		@OnlyMandedare BIT,
		@OnlyActive Bit)              
      	
      	AS        

BEGIN 	      
	SET NOCOUNT ON; 	 	   
	
	DECLARE @SQLString  NVARCHAR(4000);  	   
	DECLARE @WhrStatement  NVARCHAR(2000);         	   
	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
	DECLARE @ParmAndstr  NVARCHAR(2000);         	   
	DECLARE @Sortstring  NVARCHAR(2000);  	   
	
	set @WhrStatement  = N'';         	   
	set @ParmAndstr = N'';         
	set @Sortstring=N' ';            	   
	SET @ParmDefinition = N'@AID int=null, 								 							  
							@OperType int=null, 								 							  
							@LinkMasterID  int=null, 								 							  
							@GoKID int=null,
							@OnlyMandedare BIT=null,
							@OnlyActive Bit=null ';            	   

/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		 	  		    
	
	
		SELECT    @OnlyActive=ABS( ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 2)

	SELECT    @OnlyMandedare=ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 3)	
	

	
	
	SET @SQLString =' SELECT     FunInfoKalaAll_1.ID AS NameKalaID, CONVERT(float, FunInfoKalaAll_1.KalaNumber) AS KalaNumber, FunInfoKalaAll_1.NameKala1, 
						FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, 
						FunInfoKalaAll_1.KalaNumber AS [کد کالا], FunInfoKalaAll_1.NameKala1 + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' '') AS [نام کالا], 
						FunInfoKalaAll_1.NameVahed1 AS [نام واحد],                        
						ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) AS [مانده انبار], 
						FunInfoKalaAll_1.NameAnbar AS [نام انبار], FunInfoKalaAll_1.UnitPrice1 AS [قیمت فروش1],                        
						drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2 AS [آخرین خرید], 0 AS [قیمت فروش2], 
						FunInfoKalaAll_1.UnitPrice3 AS [قیمت فروش3],                        
						FunInfoKalaAll_1.UnitPrice2 AS [کرايه حمل], FunInfoKalaAll_1.UnitPrice5 AS [قیمت فروش5], FunInfoKalaAll_1.UnitPrice6 AS [قیمت فروش6],                        
						drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2 AS UnitPriceKh, FunInfoKalaAll_1.FieldN1, FunInfoKalaAll_1.BarCode AS [ بارکد ], FunInfoKalaAll_1.NameGoKala1 AS [گروه کالا],                        
						FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.UnitPrice1, drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2 AS AkharinKharid, FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice3,                        
						FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3)                       
						AS MandeAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber) AS CodKala FROM         dbo.FunInfoKalaAll(@OnlyActive) AS FunInfoKalaAll_1 INNER  JOIN                       
						dbo.Fun_CQ_ListKala_Sandoogh_Analyz() AS drvTra ON FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID WHERE     (1 = 1)  ' 	   
/* -----------------------نشان دادن کالاهای فعال */  

/* if(NOT (@LinkMasterID  IS NULL)) 			 	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				 if (@LinkMasterID  IS NULL) 	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه 	*/                   /* -----------------------شرط انبار */ 			 


		
	set @ParmAndstr=' AND ' 			 
	if(NOT (@AID  IS NULL)) 			 
	
	BEGIN 				 
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.AID = @AID ) '; 				
		 set @ParmAndstr=' AND ' 			 
	END 			   
	
	/* -----------------------شرط گروه کالا  */  
	set @ParmAndstr=' AND ' 			 
	if(NOT (@GoKID  IS NULL)) 			 
	
	BEGIN 				 	
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.GOKID = @GoKID ) '; 				 	
		set @ParmAndstr=' AND ' 			 
	END 	   
	
	/* /* -----------------------شرط گروه کالا  */  set @ParmAndstr=' AND ' 			 
	* if(NOT (@TextBoxFilter  IS NULL) OR LEN(@TextBoxFilter)>1) 			 BEGIN 		 	set @TextBoxFilter=N'%'+@TextBoxFilter +N'%' 	
	* set @WhrStatement= @WhrStatement + @ParmAndstr + N'  (FunInfoKalaAll_1.NameKala1 Like @TextBoxFilter ) Or (KalaNumber like @TextBoxFilter) '; 	
	* set @ParmAndstr=' AND ' 			 END 	  If @CmbSort = 1      set @Sortstring = N' ORDER BY KalaNumber DESC'      Else If @CmbSort = 2      
	* set @Sortstring  = N' ORDER BY NameKala1' Else If @CmbSort = 3 */     
	
	set @Sortstring = N' ORDER BY [مانده انبار] DESC' 
	
	/*Else If @CmbSort = 4     set @Sortstring = N' ORDER BY AkharinKharid' Else If @CmbSort = 5      
	* set @Sortstring =N' ORDER BY UnitPrice1'*/     
	
	set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY FunInfoKalaAll_1.NameKala1+ N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' ''),FunInfoKalaAll_1.NameKala1, 
									FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.UnitPrice1, 
									FunInfoKalaAll_1.FieldN1, drvTra.MaxUnitPriceKh,                        
									FunInfoKalaAll_1.NameAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), 
									CONVERT(float, FunInfoKalaAll_1.KalaNumber),                        FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice3, 
									FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6,                        
									FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.ID, FunInfoKalaAll_1.Zarib, drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2 '     			 
	
	
	if(@OnlyMandedare<>0)
		SET @SQLString =@SQLString + ' HAVING ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) <>0 ' 
		
		SET @SQLString=@SQLString+@Sortstring 	  
									
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@OnlyMandedare=@OnlyMandedare,@OnlyActive=@OnlyActive;  		 

return END    

GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz2sf1]    Script Date: 10/09/2018 09:14:27 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_Sandoogh_Analyz2sf1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz2sf1]
GO

/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz2sf1]    Script Date: 10/09/2018 09:14:27 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz2sf1] (  
      	@AID NVARCHAR(MAX), 
      	@OperType int, 
      	@LinkMasterID int, 
      	@GoKID NVARCHAR(MAX),
        @KalaID NVARCHAR(MAX))              
      	
      	AS        

BEGIN 	      
	SET NOCOUNT ON; 	 	   
	
	DECLARE @SQLString  NVARCHAR(4000);  	   
	DECLARE @WhrStatement  NVARCHAR(2000);         	   
	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
	DECLARE @ParmAndstr  NVARCHAR(2000);         	   
	DECLARE @Sortstring  NVARCHAR(2000);  	   
	
	set @WhrStatement  = N'';         	   
	set @ParmAndstr = N'';         
	set @Sortstring=N' ';            	   
	SET @ParmDefinition = N'@AID NVARCHAR(MAX)=null, 								 							  
							@OperType int=null, 								 							  
							@LinkMasterID  int=null, 								 							  
							@GoKID NVARCHAR(MAX)=null,
							@KalaID NVARCHAR(MAX)=null ';            	   

/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		 	  		    
	
	SET @SQLString =' SELECT     FunInfoKalaAll_1.ID AS NameKalaID, CONVERT(float, FunInfoKalaAll_1.KalaNumber) AS KalaNumber, FunInfoKalaAll_1.NameKala1+ + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' '') as NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.Zarib, 
                      FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.KalaNumber AS [کد کالا], FunInfoKalaAll_1.NameKala1 AS [نام کالا], FunInfoKalaAll_1.NameVahed1 AS [نام واحد], 
                      ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) AS [مانده انبار], FunInfoKalaAll_1.NameAnbar AS [نام انبار], FunInfoKalaAll_1.UnitPrice1 AS [قیمت فروش1], 
                      drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2 AS [آخرین خرید], 0 AS [قیمت فروش2], FunInfoKalaAll_1.UnitPrice3 AS [قیمت فروش3], FunInfoKalaAll_1.UnitPrice2 AS [کرايه حمل], 
                      FunInfoKalaAll_1.UnitPrice5 AS [قیمت فروش5], FunInfoKalaAll_1.UnitPrice6 AS [قیمت فروش6], drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2 AS UnitPriceKh, FunInfoKalaAll_1.FieldN1, 
                      FunInfoKalaAll_1.BarCode AS [ بارکد ], FunInfoKalaAll_1.NameGoKala1 AS [گروه کالا], FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.UnitPrice1 AS UnitPrice1, drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2 AS AkharinKharid, FunInfoKalaAll_1.UnitPrice2, 
                      FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, 
                      ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) AS MandeAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber) AS CodKala,(FunInfoKalaAll_1.UnitPrice1-(drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2)) As Sood
FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER  JOIN
                      dbo.Fun_CQ_ListKala_Sandoogh_Analyz() AS drvTra ON FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID
WHERE     (1 = 1)
 ' 	   
/* -----------------------نشان دادن کالاهای فعال */  

/* if(NOT (@LinkMasterID  IS NULL)) 			 	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				 if (@LinkMasterID  IS NULL) 	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه 	*/                   /* -----------------------شرط انبار */ 			 


	if(NOT (@AID  IS NULL)) 			 
	
	BEGIN 				
			Set @WhrStatement = @WhrStatement  + ' And (FunInfoKalaAll_1.AID IN('+ @AID +'))'		
		 
	END 
	
	/* -----------------------شرط گروه کالا  */  
			 
	if(NOT (@GoKID  IS NULL)) 			 
	
	BEGIN 				 	
		 Set @WhrStatement = @WhrStatement  + ' And (FunInfoKalaAll_1.GoKID IN('+ @GoKID +'))'				 	
	END 	   
	

	/* -----------------------شرط گروه کالا  */  
			 
	if(NOT (@KalaID  IS NULL)) 			 
	
	BEGIN 				 	
		 Set @WhrStatement = @WhrStatement  + ' And (FunInfoKalaAll_1.ID IN('+ @KalaID +'))'				 	
	END 
	
	
	set @Sortstring = N' ORDER BY FunInfoKalaAll_1.NameKala1' 
	
   
	
	set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY FunInfoKalaAll_1.NameKala1+ + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' ''),FunInfoKalaAll_1.NameKala1, 
									FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID,  
									FunInfoKalaAll_1.FieldN1, drvTra.MaxUnitPriceKh,                        
									FunInfoKalaAll_1.NameAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), 
									CONVERT(float, FunInfoKalaAll_1.KalaNumber),                        FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice3, 
									FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6,                        
									FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.ID, FunInfoKalaAll_1.Zarib, drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2,FunInfoKalaAll_1.UnitPrice1
									,FunInfoKalaAll_1.UnitPrice1-(drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2)  HAVING      (ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) <> 0) ' +@Sortstring 	      			 
									
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@KalaID=@KalaID;  		 

return END    



GO







/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListTafsiliHesabSF301]    Script Date: 11/26/2018 12:58:51 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListTafsiliHesabSF301]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListTafsiliHesabSF301]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListTafsiliHesabSF301]    Script Date: 11/26/2018 12:58:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[QSubPFrm_ListTafsiliHesabSF301]
        
        @THesabIDSath2 INT AS 
        
SELECT     T_T_Heasb.THESABID, T_T_Heasb.THESABNumber AS CodeH, T_T_Heasb.HESABID, T_T_Heasb.NameHesabT, T_T_Heasb.GOID, T_T_Heasb.KolID, T_T_Heasb.Mob, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Active, T_KolHesab.NameHesab AS KolName, T_Heasb.NameHesab AS MoinName, T_T_Heasb.THESABNumber, 
                      CASE WHEN T_T_Heasb.Active = 0 THEN N'فعال' ELSE N'غیر فعال' END AS VAShESAB, [T_RelateTafSiliSath1-2].THesabIDSath2, [T_RelateTafSiliSath1-2].THesabIDSath1, 
                      T_T_HeasbSath2.NameHesabT AS Expr1
FROM         T_T_Heasb INNER JOIN
                      [T_RelateTafSiliSath1-2] ON T_T_Heasb.THESABID = [T_RelateTafSiliSath1-2].THesabIDSath1 INNER JOIN
                      T_T_HeasbSath2 ON [T_RelateTafSiliSath1-2].THesabIDSath2 = T_T_HeasbSath2.THESABID LEFT OUTER JOIN
                      T_Heasb ON T_T_Heasb.HESABID = T_Heasb.HESABID LEFT OUTER JOIN
                      T_KolHesab ON T_T_Heasb.KolID = T_KolHesab.KOLID
WHERE     ([T_RelateTafSiliSath1-2].THesabIDSath2 = @THesabIDSath2)
ORDER BY CONVERT(Float, T_T_Heasb.THESABNumber) DESC

GO





/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListTafsiliHesabSF3]    Script Date: 11/26/2018 13:01:46 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListTafsiliHesabSF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListTafsiliHesabSF3]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListTafsiliHesabSF3]    Script Date: 11/26/2018 13:01:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[QSubPFrm_ListTafsiliHesabSF3] AS 
        
SELECT     T_T_HeasbSath2.THESABID, T_T_HeasbSath2.THESABNumber AS CodeH, T_T_HeasbSath2.NameHesabT, T_T_HeasbSath2.TextName, T_T_HeasbSath2.Address, T_T_HeasbSath2.Address2, 
                      T_T_HeasbSath2.Telex, T_T_HeasbSath2.Shahr, T_T_HeasbSath2.CodPosti, T_T_HeasbSath2.Ostan, T_T_HeasbSath2.CodEghtesdi, T_T_HeasbSath2.Keshvar, T_T_HeasbSath2.BankHesab, 
                      T_T_HeasbSath2.Mob, T_T_HeasbSath2.Websit, T_T_HeasbSath2.Phone1, T_T_HeasbSath2.Phone2, T_T_HeasbSath2.Email, T_T_HeasbSath2.Active, T_T_HeasbSath2.SaghfBed, 
                      T_T_HeasbSath2.SaghfBes, T_T_HeasbSath2.Commision, T_T_HeasbSath2.UnitPriceFo, T_T_HeasbSath2.THESABNumber
FROM         T_T_HeasbSath2 INNER JOIN
                      [T_RelateTafSiliSath1-2] ON T_T_HeasbSath2.THESABID = [T_RelateTafSiliSath1-2].THesabIDSath2 INNER JOIN
                      T_T_Heasb ON [T_RelateTafSiliSath1-2].THesabIDSath1 = T_T_Heasb.THESABID INNER JOIN
                      T_Heasb ON T_T_Heasb.HESABID = T_Heasb.HESABID INNER JOIN
                      T_KolHesab ON T_Heasb.KolID = T_KolHesab.KOLID
GROUP BY T_T_HeasbSath2.THESABID, T_T_HeasbSath2.THESABNumber, T_T_HeasbSath2.NameHesabT, T_T_HeasbSath2.TextName, T_T_HeasbSath2.Address, T_T_HeasbSath2.Address2, 
                      T_T_HeasbSath2.Telex, T_T_HeasbSath2.Shahr, T_T_HeasbSath2.CodPosti, T_T_HeasbSath2.Ostan, T_T_HeasbSath2.CodEghtesdi, T_T_HeasbSath2.Keshvar, T_T_HeasbSath2.BankHesab, 
                      T_T_HeasbSath2.Mob, T_T_HeasbSath2.Websit, T_T_HeasbSath2.Phone1, T_T_HeasbSath2.Phone2, T_T_HeasbSath2.Email, T_T_HeasbSath2.Active, T_T_HeasbSath2.SaghfBed, 
                      T_T_HeasbSath2.SaghfBes, T_T_HeasbSath2.Commision, T_T_HeasbSath2.UnitPriceFo
ORDER BY CodeH DESC


GO






/****** Object:  StoredProcedure [dbo].[Q_ListKala_Sandoogh_Barcode_1]    Script Date: 01/03/2019 11:44:54 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_ListKala_Sandoogh_Barcode_1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_ListKala_Sandoogh_Barcode_1]
GO



/****** Object:  StoredProcedure [dbo].[Q_ListKala_Sandoogh_Barcode_1]    Script Date: 01/03/2019 11:44:54 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE PROCEDURE [dbo].[Q_ListKala_Sandoogh_Barcode_1](@AID INT,@BarCode NVARCHAR(255)) AS 
      
      
SELECT     ID AS NameKalaID, BarCode, NameKala1, AID, GOKID, UnitPrice1, Currency1, NameGoKala1, NameAnbar, Zarib, NameVahed1, NameVahed2
FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1
WHERE     (AID = @AID) AND (BarCode=@BarCode)
ORDER BY BarCode

GO




/****** Object:  StoredProcedure [dbo].[Q_Pfrm_Foroosh_Sandogh]    Script Date: 01/28/2019 10:24:30 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Pfrm_Foroosh_Sandogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Pfrm_Foroosh_Sandogh]
GO

/****** Object:  StoredProcedure [dbo].[Q_Pfrm_Foroosh_Sandogh]    Script Date: 01/28/2019 10:24:30 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[Q_Pfrm_Foroosh_Sandogh] (@UserID int, @TypeFact int) AS 
        
SELECT      FOROSHID, ForoshNumbr, NumberKH, NumSID2, VSabt, PrimarySaid2, Dateeng, DateCreate, DateModified, HesabID, GOID, KOLID, MOINID, THESABID, THESABIDAmel, 
                      TextName, TextName1, TextNameT, DateFo, Takhfif, TextShSA, Sharh, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, 
                      UserID, Shift, FlgSabtFact, SalaTypeid, Maliyat, MablaghKol, Avarez, Ezafat, Poorsant, userCreate, userModify, dateSettlement, ThesabIDSath2, FlgSabtFact AS Expr3, Flag1 AS Expr1, 
                      NumberKH AS Expr2, UserID AS Expr4
FROM         View_ForoshSandogh
WHERE     (@TypeFact = 2) AND (@UserID <> 3) AND (UserID = @UserID) AND (FlgSabtFact = 0) OR
                      (@TypeFact = 1) AND (@UserID <> 3) AND (UserID = @UserID) OR
                      (@TypeFact = 2) AND (@UserID = 3) AND (FlgSabtFact = 0) OR
                      (@TypeFact = 1) AND (@UserID = 3)
ORDER BY NumberKH
GO




/****** Object:  StoredProcedure [dbo].[Q_ListKala_Sandoogh_Barcode_1]    Script Date: 01/28/2019 10:29:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_ListKala_Sandoogh_Barcode_1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_ListKala_Sandoogh_Barcode_1]
GO


/****** Object:  StoredProcedure [dbo].[Q_ListKala_Sandoogh_Barcode_1]    Script Date: 01/28/2019 10:29:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE PROCEDURE [dbo].[Q_ListKala_Sandoogh_Barcode_1](@AID INT,@BarCode NVARCHAR(255)) AS 
      
      
SELECT     ID AS NameKalaID, BarCode, NameKala1, AID, GOKID, UnitPrice1, Currency1, NameGoKala1, NameAnbar, Zarib, NameVahed1, NameVahed2
FROM         dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1
WHERE     (AID = @AID) AND (BarCode=@BarCode)
ORDER BY BarCode

GO





IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_ForoshSandogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_ForoshSandogh]
GO




SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[CQ_ListKala_ForoshSandogh] ( 
					@AID int,
					@OperType int,
					@LinkMasterID int,
					@GoKID int,
					@OnlyMandedare BIT,
					@OnlyActive Bit)           
  AS     
  BEGIN 	
  
  SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	
  DECLARE @Sortstring  NVARCHAR(2000);  
  	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';      
  set @Sortstring=N' ';            	
  SET @ParmDefinition = N'@AID int=null, 								
							  @OperType int=null, 								
							  @LinkMasterID  int=null, 								
							  @GoKID int=null,
		@OnlyMandedare BIT=null,
		@OnlyActive Bit=null ';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
	  	
	SELECT    @OnlyActive= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 2)

	SELECT    @OnlyMandedare= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 3)	  		 
	  		 
  SET @SQLString =' SELECT     ID AS NameKalaID, CONVERT(float, KalaNumber) AS KalaNumber, NameKala1, BarCode, Zarib, AID, GOKID, KalaNumber AS [کد کالا], NameKala1 + + N'' '' + ISNULL(note, N'' '') AS [نام کالا], 
                      NameVahed1 AS [نام واحد], 0 AS [مانده انبار], NameAnbar AS [نام انبار], UnitPrice1 AS [قيمت فروش1], 0 AS [آخرين خريد], UnitPrice2 AS [قيمت فروش2], UnitPrice3 AS [قيمت فروش3], UnitPrice4 AS [قيمت فروش4], 
                      UnitPrice5 AS [قيمت فروش5], UnitPrice6 AS [قيمت فروش6], NameVahed2, 0 AS UnitPriceKh, FieldN1, BarCode AS [ بارکد ], NameGoKala1 AS [گروه کالا], NameAnbar, UnitPrice1, 0 AS AkharinKharid, 
                      UnitPrice2, UnitPrice3, UnitPrice4, UnitPrice5, UnitPrice6, Active, NameGoKala1, 0 AS MandeAnbar, CONVERT(Nvarchar(255), KalaNumber) AS CodKala, 0 AS UnitPricekhUser
FROM         dbo.FunInfoKalaAll(@OnlyActive) AS FunInfoKalaAll_1
WHERE     (1 = 1)

' 	
  /* -----------------------نشان دادن کالاهاي فعال */ 
/*
if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه
	*/
               
  /* -----------------------شرط انبار */ 	

		  		
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 		
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	

 /* /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@TextBoxFilter  IS NULL) OR LEN(@TextBoxFilter)>1) 			
BEGIN 		
	set @TextBoxFilter=N'%'+@TextBoxFilter +N'%'
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'  (FunInfoKalaAll_1.NameKala1 Like @TextBoxFilter ) Or (KalaNumber like @TextBoxFilter) '; 				
	set @ParmAndstr=' AND ' 			
END 	

If @CmbSort = 1 
    set @Sortstring = N' ORDER BY KalaNumber DESC'
    
Else
If @CmbSort = 2 
    set @Sortstring  = N' ORDER BY NameKala1'
Else
If @CmbSort = 3 */
   /* set @Sortstring = N' ORDER BY [مانده انبار] DESC' */
/*Else
If @CmbSort = 4 
   set @Sortstring = N' ORDER BY AkharinKharid'
Else
If @CmbSort = 5 
    set @Sortstring =N' ORDER BY UnitPrice1'*/




set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY NameKala1 + + N'' '' + ISNULL(note, N'' ''), NameKala1, BarCode, AID, GOKID, FieldN1, NameAnbar, CONVERT(Nvarchar(255), KalaNumber), CONVERT(float, KalaNumber), CONVERT(float, 
                      KalaNumber), NameVahed1, UnitPrice2, UnitPrice1, UnitPrice3, UnitPrice4, UnitPrice5, UnitPrice6, Active, NameGoKala1, ID, Zarib, NameVahed2 '	
     
	if(@OnlyMandedare<>0)
		SET @SQLString =@SQLString 
		
		SET @SQLString=@SQLString+@Sortstring 	  
					
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@OnlyMandedare=@OnlyMandedare,@OnlyActive=@OnlyActive;  		
return END 




GO






/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1sf1]    Script Date: 01/29/2019 09:40:27 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_Sandoogh_Analyz1sf1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1sf1]
GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1sf1]    Script Date: 01/29/2019 09:40:27 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1sf1] (  
      	@AID int, 
      	@OperType int, 
      	@LinkMasterID int, 
      	@GoKID INT,
		@OnlyMandedare BIT,
		@OnlyActive Bit)              
      	
      	AS        

BEGIN 	      
	SET NOCOUNT ON; 	 	   
	
	DECLARE @SQLString  NVARCHAR(Max);  	   
	DECLARE @WhrStatement  NVARCHAR(Max);         	   
	DECLARE @ParmDefinition  NVARCHAR(Max);         	   
	DECLARE @ParmAndstr  NVARCHAR(Max);         	   
	DECLARE @Sortstring  NVARCHAR(Max);  	   
	DECLARE @CmbSort AS INT;
	
	set @WhrStatement  = N'';         	   
	set @ParmAndstr = N'';         
	set @Sortstring=N' ';            	   
	SET @ParmDefinition = N'@AID int=null, 								 							  
							@OperType int=null, 								 							  
							@LinkMasterID  int=null, 								 							  
							@GoKID int=null,
							@OnlyMandedare BIT=null,
							@OnlyActive Bit=null ';            	   

/* Build the SQL string one time. */      /* Specify the parameter format one time. */  		 	  		    
	
	
		SELECT    @OnlyActive=ABS( ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 2)

	SELECT    @OnlyMandedare=ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 3)	
	
	SELECT    @CmbSort= ABS(ISNULL(FieldN1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 10) AND (OPTIONID = 1)	  
	
	
	SET @SQLString =' SELECT     FunInfoKalaAll_1.ID AS NameKalaID, CONVERT(float, FunInfoKalaAll_1.KalaNumber) AS KalaNumber, FunInfoKalaAll_1.NameKala1, 
						FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, 
						FunInfoKalaAll_1.KalaNumber AS [کد کالا], FunInfoKalaAll_1.NameKala1 + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' '') AS [نام کالا], 
						FunInfoKalaAll_1.NameVahed1 AS [نام واحد],                        
						ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) AS [مانده انبار], 
						FunInfoKalaAll_1.NameAnbar AS [نام انبار], FunInfoKalaAll_1.UnitPrice1 AS [قیمت فروش1],                        
						drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2 AS [آخرین خرید], 0 AS [قیمت فروش2], 
						FunInfoKalaAll_1.UnitPrice3 AS [قیمت فروش3],                        
						FunInfoKalaAll_1.UnitPrice2 AS [کرايه حمل], FunInfoKalaAll_1.UnitPrice5 AS [قیمت فروش5], FunInfoKalaAll_1.UnitPrice6 AS [قیمت فروش6],                        
						drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2 AS UnitPriceKh, FunInfoKalaAll_1.FieldN1, FunInfoKalaAll_1.BarCode AS [ بارکد ], FunInfoKalaAll_1.NameGoKala1 AS [گروه کالا],                        
						FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.UnitPrice1, drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2 AS AkharinKharid, FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice3,                        
						FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3)                       
						AS MandeAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber) AS CodKala FROM         dbo.FunInfoKalaAll(@OnlyActive) AS FunInfoKalaAll_1 INNER  JOIN                       
						dbo.Fun_CQ_ListKala_Sandoogh_Analyz() AS drvTra ON FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID  WHERE     (1 = 1)  ' 	   
/* -----------------------نشان دادن کالاهای فعال */  


/*FunInfoKalaAll_1.AID = drvTra.AID and FunInfoKalaAll_1.ID = drvTra.KALAID  */

/* FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID  */

/* if(NOT (@LinkMasterID  IS NULL)) 			 	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				 if (@LinkMasterID  IS NULL) 	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه 	*/                   /* -----------------------شرط انبار */ 			 


		
	set @ParmAndstr=' AND ' 			 
	if(NOT (@AID  IS NULL)) 			 
	
	BEGIN 				 
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.AID = @AID ) '; 				
		 set @ParmAndstr=' AND ' 			 
	END 			   
	
	/* -----------------------شرط گروه کالا  */  
	set @ParmAndstr=' AND ' 			 
	if(NOT (@GoKID  IS NULL)) 			 
	
	BEGIN 				 	
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.GOKID = @GoKID ) '; 				 	
		set @ParmAndstr=' AND ' 			 
	END 	   
	
	/* /* -----------------------شرط گروه کالا  */  set @ParmAndstr=' AND ' 			 
	* if(NOT (@TextBoxFilter  IS NULL) OR LEN(@TextBoxFilter)>1) 			 BEGIN 		 	set @TextBoxFilter=N'%'+@TextBoxFilter +N'%' 	
	* set @WhrStatement= @WhrStatement + @ParmAndstr + N'  (FunInfoKalaAll_1.NameKala1 Like @TextBoxFilter ) Or (KalaNumber like @TextBoxFilter) '; 	
	* set @ParmAndstr=' AND ' 			 END 	  If @CmbSort = 1      set @Sortstring = N' ORDER BY KalaNumber DESC'      Else If @CmbSort = 2      
	* set @Sortstring  = N' ORDER BY NameKala1' Else If @CmbSort = 3 */     
	
If @CmbSort = 1 
    set @Sortstring = N' ORDER BY KalaNumber DESC'
    
Else
If @CmbSort = 2 
    set @Sortstring  = N' ORDER BY NameKala1'
Else
If @CmbSort = 3 
    set @Sortstring = N' ORDER BY MandeAnbar DESC'
Else
If @CmbSort = 4 
   set @Sortstring = N' ORDER BY AkharinKharid'
Else
If @CmbSort = 5 
    set @Sortstring =N' ORDER BY UnitPrice1'
    
    
	/*Else If @CmbSort = 4     set @Sortstring = N' ORDER BY AkharinKharid' Else If @CmbSort = 5      
	* set @Sortstring =N' ORDER BY UnitPrice1'*/     
	
	set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY FunInfoKalaAll_1.NameKala1+ N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' ''),FunInfoKalaAll_1.NameKala1, 
									FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.UnitPrice1, 
									FunInfoKalaAll_1.FieldN1, drvTra.MaxUnitPriceKh,                        
									FunInfoKalaAll_1.NameAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), 
									CONVERT(float, FunInfoKalaAll_1.KalaNumber),                        FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice3, 
									FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6,                        
									FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.ID, FunInfoKalaAll_1.Zarib, drvTra.MaxUnitPriceKh + FunInfoKalaAll_1.UnitPrice2 '     			 
	
	
	if(@OnlyMandedare<>0)
		SET @SQLString =@SQLString + ' HAVING ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) <>0 ' 
		
		SET @SQLString=@SQLString+@Sortstring 	  
									
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@OnlyMandedare=@OnlyMandedare,@OnlyActive=@OnlyActive;  		 

return END    

GO




/****** Object:  StoredProcedure [dbo].[CQ_ListKala_ForoshSandogh]    Script Date: 01/29/2019 09:40:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_ForoshSandogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_ForoshSandogh]
GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_ForoshSandogh]    Script Date: 01/29/2019 09:40:56 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  CREATE PROCEDURE [dbo].[CQ_ListKala_ForoshSandogh] ( 
					@AID int,
					@OperType int,
					@LinkMasterID int,
					@GoKID int,
					@OnlyMandedare BIT,
					@OnlyActive Bit)           
  AS     
  BEGIN 	
  
  SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(Max);  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max);         	
  DECLARE @Sortstring  NVARCHAR(Max);  
  DECLARE @CmbSort AS INT;
  	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';      
  set @Sortstring=N' ';            	
  SET @ParmDefinition = N'@AID int=null, 								
							  @OperType int=null, 								
							  @LinkMasterID  int=null, 								
							  @GoKID int=null,
		@OnlyMandedare BIT=null,
		@OnlyActive Bit=null ';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
	  	
	SELECT    @OnlyActive= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 2)

	SELECT    @OnlyMandedare= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 3)	  		 
	
	SELECT    @CmbSort= ABS(ISNULL(FieldN1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 10) AND (OPTIONID = 1)	  
	  		 
  SET @SQLString =' SELECT     ID AS NameKalaID, CONVERT(float, KalaNumber) AS KalaNumber, NameKala1, BarCode, Zarib, AID, GOKID, KalaNumber AS [کد کالا], NameKala1 + + N'' '' + ISNULL(note, N'' '') AS [نام کالا], 
                      NameVahed1 AS [نام واحد], 0 AS [مانده انبار], NameAnbar AS [نام انبار], UnitPrice1 AS [قيمت فروش1], 0 AS [آخرين خريد], UnitPrice2 AS [قيمت فروش2], UnitPrice3 AS [قيمت فروش3], UnitPrice4 AS [قيمت فروش4], 
                      UnitPrice5 AS [قيمت فروش5], UnitPrice6 AS [قيمت فروش6], NameVahed2, 0 AS UnitPriceKh, FieldN1, BarCode AS [ بارکد ], NameGoKala1 AS [گروه کالا], NameAnbar, UnitPrice1, 0 AS AkharinKharid, 
                      UnitPrice2, UnitPrice3, UnitPrice4, UnitPrice5, UnitPrice6, Active, NameGoKala1, 0 AS MandeAnbar, CONVERT(Nvarchar(255), KalaNumber) AS CodKala, 0 AS UnitPricekhUser
FROM         dbo.FunInfoKalaAll(@OnlyActive) AS FunInfoKalaAll_1
WHERE     (1 = 1)

' 	
  /* -----------------------نشان دادن کالاهاي فعال */ 
/*
if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه
	*/
               
  /* -----------------------شرط انبار */ 	

		  		
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 		
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	

 /* /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@TextBoxFilter  IS NULL) OR LEN(@TextBoxFilter)>1) 			
BEGIN 		
	set @TextBoxFilter=N'%'+@TextBoxFilter +N'%'
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'  (FunInfoKalaAll_1.NameKala1 Like @TextBoxFilter ) Or (KalaNumber like @TextBoxFilter) '; 				
	set @ParmAndstr=' AND ' 			
END 	
*/

If @CmbSort = 1 
    set @Sortstring = N' ORDER BY KalaNumber DESC'
    
Else
If @CmbSort = 2 
    set @Sortstring  = N' ORDER BY NameKala1'
Else
If @CmbSort = 3 
    set @Sortstring = N' ORDER BY MandeAnbar DESC'
Else
If @CmbSort = 4 
   set @Sortstring = N' ORDER BY AkharinKharid'
Else
If @CmbSort = 5 
    set @Sortstring =N' ORDER BY UnitPrice1'
    


set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY NameKala1 + + N'' '' + ISNULL(note, N'' ''), NameKala1, BarCode, AID, GOKID, FieldN1, NameAnbar, CONVERT(Nvarchar(255), KalaNumber), CONVERT(float, KalaNumber), CONVERT(float, 
                      KalaNumber), NameVahed1, UnitPrice2, UnitPrice1, UnitPrice3, UnitPrice4, UnitPrice5, UnitPrice6, Active, NameGoKala1, ID, Zarib, NameVahed2  '	
     
	if(@OnlyMandedare<>0)
		SET @SQLString =@SQLString 
		
		SET @SQLString=@SQLString+@Sortstring 	  
					
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@OnlyMandedare=@OnlyMandedare,@OnlyActive=@OnlyActive;  		
return END 





GO




/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_NameKala]    Script Date: 02/10/2019 11:42:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseHesab_InsertInformationT_NameKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseHesab_InsertInformationT_NameKala]
GO

/****** Object:  StoredProcedure [dbo].[CloseHesab_InsertInformationT_NameKala]    Script Date: 02/10/2019 11:42:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CloseHesab_InsertInformationT_NameKala] 

@StrDes NVARCHAR(255)	   

AS
BEGIN

	SET NOCOUNT ON;
	

    Declare @SQLQuery1 AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 

	SET @SQLQuery1 = ' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_NameKala] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_NameKala  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_NameKala  ' + CHAR(10)
												+' (ID, KalaNumber, DateCreate, DateModified, NameKala1, Active, VAHED1, VAHED2, MinPrice, MaxPrice, Zarib, MinMogoody, MaxMogoody, BarCode, Note, UnitPrice1, UnitPrice2, UnitPrice3, ' + CHAR(10) 
												+' UnitPrice4, UnitPrice5, UnitPrice6, DarsadAmel1, DarsadAmel2, DarsadAmel3, DarsadAmel4, DarsadAmel5, DarsadAmel6, DarsadKasr1, DarsadKasr2, DarsadKasr3, DarsadKasr4, DarsadKasr5, ' + CHAR(10) 
												+' DarsadKasr6, Currency1, Currency2, Currency3, Currency4, Currency5, Currency6, Model, UnitPrice, Unitfo, VKalaID, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, ' + CHAR(10) 
												+' Flag2, Flag3, Flag4, TedadBal) ' + CHAR(10) 
												+' SELECT ID, KalaNumber, DateCreate, DateModified, NameKala1, Active, VAHED1, VAHED2, MinPrice, MaxPrice, Zarib, MinMogoody, MaxMogoody, BarCode, Note, UnitPrice1, UnitPrice2, UnitPrice3,' + CHAR(10) 
												+' UnitPrice4, UnitPrice5, UnitPrice6, DarsadAmel1, DarsadAmel2, DarsadAmel3, DarsadAmel4, DarsadAmel5, DarsadAmel6, DarsadKasr1, DarsadKasr2, DarsadKasr3, DarsadKasr4, DarsadKasr5, ' + CHAR(10) 
												+' DarsadKasr6, Currency1, Currency2, Currency3, Currency4, Currency5, Currency6, Model, UnitPrice, Unitfo, VKalaID, FieldN1, FieldN2, FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, ' + CHAR(10) 
												+'  Flag2, Flag3, Flag4, TedadBal ' + CHAR(10) 
												+' FROM T_NameKala WHERE ( Active=0 And (ISNULL(FlagNoeKala, 0) = 0)) SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_NameKala] OFF ' + CHAR(10) 


	SET @SQLQuery1 = @SQLQuery1 + CHAR(10)+' SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_RelateKala] ON ' + CHAR(10) + ' Delete FROM' + CHAR(10) + @StrDes +N'.dbo.T_RelateKala  '
	
	SET @SQLQuery1 = @SQLQuery1 + CHAR(10) +N' INSERT INTO ' + CHAR(10) + @StrDes + '.dbo.T_RelateKala  ' + CHAR(10)
												+' (Kalaid2, KalaidRelate, NameKala2, ZaribKala)' + CHAR(10) 
												+' SELECT     Kalaid2, KalaidRelate, NameKala2, ZaribKala ' + CHAR(10) 
												+' FROM         T_RelateKala INNER JOIN ' + CHAR(10) 
												+' T_NameKala ON T_RelateKala.KalaidRelate = T_NameKala.ID ' + CHAR(10) 
												+' WHERE     (T_NameKala.Active = 0) AND (ISNULL(T_NameKala.FlagNoeKala, 0) = 0) SET IDENTITY_INSERT '+ @StrDes + '.[dbo].[T_RelateKala] OFF ' + CHAR(10) 






    Set @ParamDefinition = '@StrDes NVARCHAR(255) ' 	

		
    Execute sp_Executesql   @SQLQuery1, 
						    @ParamDefinition,
						    @StrDes

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)
		
	

END


GO






/****** Object:  StoredProcedure [dbo].[QListTafsiliHesab]    Script Date: 05/23/2019 10:16:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Roozane_Khadamat_GetInformatinOneTafsiliHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Roozane_Khadamat_GetInformatinOneTafsiliHesab]
GO



/****** Object:  StoredProcedure [dbo].[QListTafsiliHesab]    Script Date: 05/23/2019 10:16:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO

        CREATE PROCEDURE [dbo].[Q_Roozane_Khadamat_GetInformatinOneTafsiliHesab](@Thesabid int) AS 
        
SELECT     THESABID, THESABNumber, DateCreate, DateModified, HESABID, NameHesabT, TextName, GOID, KolID, Address, Address2, Note, Telex, Shahr, Ostan, CodPosti, CodEghtesdi, Keshvar, 
                      BankHesab, Mob, Websit, Phone1, Phone2, Email, VMo, VFo, VKh, VSa, VDa, CurrencyDefault, Active, SaghfBed, SaghfBes, Commision, KolID2, HesabID2, THesbID2, UnitPriceFo, FieldN1, FieldN2, 
                      FieldN3, FieldN4, FieldT1, FieldT2, FieldT3, FieldT4, Flag1, Flag2, Flag3, Flag4, Flag5, Flag6, Flag7, Flag8, Balance, boat_Sh_Sabt, boat_Sh_Motor, boat_Kamat, boat_Kamat_Phone
FROM         T_T_Heasb
WHERE     (THESABID = @THESABID)

GO




/****** Object:  StoredProcedure [dbo].[PFrm_Khadamat_NewShGhabz_SF13]    Script Date: 05/23/2019 11:57:54 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_Khadamat_NewShGhabz_SF13]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PFrm_Khadamat_NewShGhabz_SF13]
GO



/****** Object:  StoredProcedure [dbo].[PFrm_Khadamat_NewShGhabz_SF13]    Script Date: 05/23/2019 11:57:54 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





Create  PROCEDURE [dbo].[PFrm_Khadamat_NewShGhabz_SF13]
	@NewNumkber INT =0 OUTPUT
AS

BEGIN


		SELECT     @NewNumkber=isnull(MAX(ShGhabz),0)+1 
		FROM         T_DKhad


							  
END







GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 06/23/2019 14:00:24 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Kharid_Rep1SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 06/23/2019 14:00:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

   CREATE  PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14] 		 	
   
   @Userid INT,	 	
   @KharidID INT, 	
   @GroupID NVARCHAR(MAX),  	
   @KOLID NVARCHAR(MAX),  	
   @HesabidID NVARCHAR(MAX),  	
   @THesabidID NVARCHAR(MAX),  	
   @THesabidIDSath2 NVARCHAR(MAX),  	
   @StarteDate NVARCHAR(10), 	
   @ENDDate NVARCHAR(10), 	
   @KHARIDID1 INT, 	
   @KHARIDID2 INT  AS BEGIN 	 
   
   IF (@KharidID>0) 	
   
   BEGIN 
SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, 
                      T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, 
                      FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, 
                      T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, 
                      T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) 
                      AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 
                      1 AS GRP, 'کالاهای خریداری شده' AS NameGRP
FROM         T_T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 1) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN
                      T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN
                      T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE     (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh > 0)                      
    UNION ALL
    
SELECT     [TRANSACTION].TedadDKh AS Tedad, [TRANSACTION].UnitPriceDKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, 
                      T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, '' AS NameVahedAsli, '' AS NameVahedFarei, 0 AS Expr1, '' AS Expr2, 
                      T_NameKhad.KhadamatNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, 
                      T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, 
                      T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 0 AS tedadFarei, 
                      [TRANSACTION].radif, '' AS Expr3, '' AS Expr4, '' AS AddAnbar, 0 AS Tedad1, 0 AS Tedad2, T_NameKhad.Name, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 2 AS GRP, 'کسورات-هزینه های فاکتور' AS Expr5
FROM         T_KolHesab RIGHT OUTER JOIN
                      [TRANSACTION] INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
                      T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 1) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID ON T_KolHesab.KOLID = T_Kharid.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE     (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh = 0)


    UNION ALL

SELECT  0 AS Tedad, Mablagh AS netprice, '' AS NameTarafHesab, '' AS NameHesabMoin, '' AS NameHesabTafsili, '' AS NameHesabKol, '' AS CodeTafsili, '' AS CodeKol, '' AS CodeMoin, Mablagh, '' AS Expr6, 
               DATE2 AS Tarikh, TextSharh AS SharhRadif, TextSharh AS TozihatFactor, '' AS NameVahedAsli, '' AS NameVahedFarei, 0 AS Expr1, '' AS Expr2, PARID1Number AS CodeKala, 0 AS HazineFactor, 0 AS Serial, 
               0 AS ShomareFactor, 0 AS AddressTarafHesab, 0 AS Address2TarafHesab, 0 AS Expr7, 0 AS Expr8, 0 AS Expr9, 0 AS Expr10, 0 AS Expr11, 0 AS Expr12, 0 AS Expr13, '' AS Expr14, '' AS Expr15, '' AS Expr16, 
               '' AS Expr17, 0 AS Expr18, IDOperation AS KHARIDID, 0 AS tedadFarei, 0 AS Expr19, '' AS Expr3, '' AS Expr4, '' AS AddAnbar, 0 AS Tedad1, 0 AS Tedad2, '' AS Expr20, 0 AS Expr21, 0 AS Expr22, 3 AS GRP, 
               'پرداخت های نقدی' AS Expr5
FROM     T_Par1
WHERE     (IDOperation = @KharidID)

      END               
ELSE 	

BEGIN  			
SET NOCOUNT ON;  			
Declare @SQLQuery AS NVarchar(MAX) 			
Declare @ParamDefinition AS NVarchar(MAX)  			
DECLARE @OrderBy AS NVARCHAR(MAX) 			
DECLARE @GROUPBy AS NVARCHAR(MAX) 		     			
IF (@StarteDate IS NULL) 					
SET @StarteDate='0' 		     				
IF (@ENDDate IS NULL) 					
SET @ENDDate='99999999' 						
SET @SQLQuery = '   SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0)  							  <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili,  							  T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh,  							  T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli,  							  FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor,  							  T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex,  							  T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1,  							  T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2)  							  AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0)  							  > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0)  							  > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala,  							  [TRANSACTION].Avarez 		FROM         T_T_Heasb RIGHT OUTER JOIN 							  dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN 							  [TRANSACTION] INNER JOIN 							  T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN 								  (SELECT     FactID, SUM(MabHazine) AS HazineFactor 									 FROM         Table4 									 WHERE     (TypeFact = 1) AND (Flag1 = 0) 									 GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN 							  T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN 							  T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID where 1=1 		'   			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
Set @ParamDefinition = '@UserID INT, 									
						@GroupID NVARCHAR(MAX), 									
						@KOLID NVARCHAR(MAX),  									
						@HesabidID NVARCHAR(MAX),  									
						@THesabidID NVARCHAR(MAX),  									
						@THesabidIDSath2 NVARCHAR(MAX),  									
						@StarteDate NVARCHAR(MAX),  									
						@ENDDate NVARCHAR(MAX), 									
						@KHARIDID1 INT, 									
						@KHARIDID2 INT '  	 			 			
/* Execute the Transact-SQL String with all parameter value's 	    			   Using sp_executesql Command */ 			    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 			
Execute sp_Executesql   @SQLQuery,  									
						@ParamDefinition, 									
						@UserID , 									
						@GroupID, 									
						@KOLID ,  									
						@HesabidID ,  									
						@THesabidID ,  									
						@THesabidIDSath2 ,  									
						@StarteDate ,  									
						@ENDDate, 									
						@KHARIDID1, 									
						@KHARIDID1   			
If @@ERROR <> 0 GoTo ErrorHandler 			
Set NoCount OFF 		   			
ErrorHandler: 				
Return(@@ERROR) 	
END			 
END    
GO




/****** Object:  StoredProcedure [dbo].[Rep_01186604]    Script Date: 06/25/2019 13:27:43 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_01186604]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_01186604]
GO


/****** Object:  StoredProcedure [dbo].[Rep_01186604]    Script Date: 06/25/2019 13:27:43 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[Rep_01186604] (  	@SortRep nvarchar(255),  	@NoeSortRep nvarchar(255), 	@Goid int, 	@IdKala int, 	@KalaGO int, 	@AID int, 	@KolID int,  	@HesabID int, 	@THesabID int,  	@StartDate nvarchar(50)='0', 	@Enddate nvarchar(50)='99999999', 	@StartShFact nvarchar(50), 	@EndShFact nvarchar(50), 	@FilterNameKala nvarchar(255) 	 )              AS        BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	   DECLARE @WhrStatement  NVARCHAR(2000);         	   DECLARE @ParmDefinition  NVARCHAR(2000);         	   DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   set @WhrStatement  = N'';         	   set @ParmAndstr = N'';                  	   SET @ParmDefinition = N'@SortRep nvarchar(255)=null,  							@NoeSortRep nvarchar(255)=null, 							@IdKala int, 							@KalaGO int, 							@AID int, 							@Goid int=null, 							@KolID int=null,  							@HesabID nvarchar(50)=null, 							@THesabID int=null, 							@StartDate nvarchar(50)=''0'', 							@Enddate nvarchar(50)=''99999999'', 							@StartShFact nvarchar(50)=null, 							@EndShFact nvarchar(50)=null, 							@FilterNameKala nvarchar(255)=null ';            	   /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		    BEGIN       if(@StartDate IS NULL) 			   BEGIN 				   set @StartDate='0'; 				   END         if(@Enddate IS NULL) 			   BEGIN 				   set @Enddate='99999999'; 				   END         
  if(@NoeSortRep IS NULL) 			   
  BEGIN 				   set @NoeSortRep=0; 				   END        	  		    
  SET @SQLString ='SELECT     T_Kharid.KHARIDID, CONVERT(float, T_Kharid.KharidNumbr) AS KharidNumbr, T_NameKala.NameKala1, T_NameVahed.NameVahed, T_Anbar.NameAnbar, CONVERT(float, 
                      T_NameKala.KalaNumber) AS KalaNumber, T_NameGoKala.NameGoKala1, T_NameKala.ID, T_NameKala.VAHED1, [TRANSACTION].AID, [TRANSACTION].GOKID, [TRANSACTION].GOID, 
                      [TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID, T_NameKala.Zarib, MIN(dbo.F1StartDate()) AS MinOfDateKh, MAX(dbo.F1EndDate()) AS MaxOfDatekh, 
                      SUM(ABS([TRANSACTION].FieldN3)) AS SumFieldN3, CONVERT(float, SUM([TRANSACTION].TedadKh)) AS SumOftedadkh, @startDate AS startDate, @enddate AS enddate, 
                      ROUND(SUM([TRANSACTION].TedadFo) / T_NameKala.Zarib, 2) AS Karton, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab, 
                      T_Kharid.DateKh, CONVERT(nvarchar(255), [TRANSACTION].Sharh) AS Sharhkala, SUM([TRANSACTION].Maliatkala) AS Maliatkala, SUM([TRANSACTION].Avarez) AS Avarez
FROM         [TRANSACTION] INNER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
                      T_Anbar ON [TRANSACTION].AID = T_Anbar.AID INNER JOIN
                      T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID INNER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1
GROUP BY T_NameKala.NameKala1, T_NameKala.VAHED1, T_NameKala.KalaNumber, T_NameKala.Zarib, [TRANSACTION].AID, T_Kharid.KHARIDID, T_Kharid.KharidNumbr, [TRANSACTION].GOID, 
                      [TRANSACTION].KOLID, [TRANSACTION].GOKID, [TRANSACTION].HESABID, [TRANSACTION].THESABID, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, 
                      [TRANSACTION].THESABID), T_NameKala.ID, T_NameVahed.NameVahed, T_Anbar.NameAnbar, T_NameGoKala.NameGoKala1, T_Kharid.DateKh, CONVERT(nvarchar(255), [TRANSACTION].Sharh)
HAVING      (T_Kharid.DateKh BETWEEN @startDate AND @enddate)'              
   /*  --------------------شرط حساب سرگروه  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@Goid  IS NULL)) 			    
   BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOID = @Goid )'; 				   
   set @ParmAndstr=' AND ' 			    END                   
   /*  --------------------شرط حساب کل  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@KolID  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].KolID = @KolID )'; 				    set @ParmAndstr=' AND ' 			    END              /*  --------------------شرط حساب معین  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@HESABID  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].HESABID = @HESABID )'; 				    set @ParmAndstr=' AND ' 			    END          /*  --------------------شرط حساب تفصیلی  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@THESABID  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].THESABID = @THESABID )'; 				    set @ParmAndstr=' AND ' 			    END             /*  --------------------شرط حساب کالا  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@IdKala  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_NameKala.ID = @IdKala )'; 				    set @ParmAndstr=' AND ' 			    END                /*  --------------------شرط حساب گروه کالا  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@KalaGO  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].GOKID = @KalaGO )'; 				    set @ParmAndstr=' AND ' 			    END   	         /*  --------------------شرط حساب انبار کالا  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@AID  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].AID = @AID )'; 				    set @ParmAndstr=' AND ' 			    END   	 	/*  --------------------فیلتر نام کالا */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@AID  IS NULL)) 			    BEGIN 				    set @WhrStatement= @WhrStatement + @ParmAndstr + N'([TRANSACTION].AID = @AID )'; 				    set @ParmAndstr=' AND ' 			    END   	 	 	/*  --------------------شرط حساب شماره فاکتور  */ 			    set @ParmAndstr=' AND ' 			    if(NOT (@FilterNameKala  IS NULL))     BEGIN 	    	set @FilterNameKala='%'+@FilterNameKala+'%'	    set @WhrStatement= @WhrStatement + @ParmAndstr + N'(T_NameKala.NameKala1 LIKE  @FilterNameKala )'; 				    set @ParmAndstr=' AND ' 			    END    	    if(NOT (@WhrStatement  IS NULL)) 			    BEGIN 				 		set @SQLString= @SQLString + @WhrStatement     END       	set @WhrStatement=null 	 	/* --------------------  نام کالا  1 مرتب سازی */ 			     if (@SortRep like 'NameKala1') 			     		     BEGIN      	 		if ((@NoeSortRep<>0) )		 		BEGIN 		 			set @WhrStatement = N'  ORDER BY T_NameKala.NameKala1 DESC '; 	 		END 		 		if ((@NoeSortRep=0) )		 		BEGIN 		 			set @WhrStatement = N'  ORDER BY T_NameKala.NameKala1  ASC '; 	 		END 	    		     END  	 	/* --------------------  کد کالا  2 مرتب سازی */ 			     if (@SortRep like 'KalaNumber')     		     BEGIN      	 		if (@NoeSortRep<>0)		 		BEGIN 		 			set @WhrStatement = N'  ORDER BY KalaNumber DESC '; 	 		END 		 		if ((@NoeSortRep=0) )		 		BEGIN 		 			set @WhrStatement = N'  ORDER BY KalaNumber ASC '; 	 		END 	    		     END       	 	/* --------------------  کد کالا  2 مرتب سازی */ 			     if (@SortRep IS NULL)     		     BEGIN 		set @WhrStatement = N'  ORDER BY KalaNumber ASC '; 	     END  				 	   if(NOT (@WhrStatement  IS NULL)) 			    BEGIN 				 		set @SQLString= @SQLString + @WhrStatement     END     	      EXECUTE sp_executesql   @SQLString, @ParmDefinition,@SortRep=@SortRep,@NoeSortRep=@NoeSortRep,@AID=@AID,@KalaGO=@KalaGO,@IdKala=@IdKala,@Goid=@Goid,@KolID=@KolID,@HesabID=@HesabID,@THesabID=@THesabID,@StartDate=@StartDate,@Enddate=@Enddate,@StartShFact=@StartShFact,@EndShFact=@EndShFact,@FilterNameKala=@FilterNameKala;  		     END 		      /* --------پایان چاپ*/  		 		  return END 

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Rep1]    Script Date: 06/25/2019 13:29:07 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Rep1]    Script Date: 06/25/2019 13:29:07 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_Rep1]
		
		@Userid INT,	
		@FOROSHID INT,
		@GroupID NVARCHAR(MAX), 
		@KOLID NVARCHAR(MAX), 
		@HesabidID NVARCHAR(MAX), 
		@THesabidID NVARCHAR(MAX), 
		@THesabidIDSath2 NVARCHAR(MAX), 
		@StarteDate NVARCHAR(10),
		@ENDDate NVARCHAR(10),
		@KHARIDID1 INT,
		@KHARIDID2 INT

AS
BEGIN
	
IF (@FOROSHID>0) 	
	
SELECT     [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
                      CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, 
                      T_Forosh.KOLID AS CodeKol, T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, [TRANSACTION].FieldN7 AS Takhfif, T_Forosh.DateFo AS Tarikh, 
                      [TRANSACTION].Sharh AS SharhRadif, T_Forosh.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, 
                      FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, 
                      T_Forosh.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, 
                      T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, 
                      FunInfoKalaAll_1.NameKala1, T_Forosh.FOROSHID, DERIVEDTBL.mande AS MandeHesab, ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) 
                      = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, T_Forosh.ForoshNumbr + FunInfoKalaAll_1.AIDNumber AS CodeAnbar, FunInfoKalaAll_1.NameAnbar, 
                      FunInfoKalaAll_1.SharhAnbar AS AddressAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad2 END AS Tedad2, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Tedad1User, [TRANSACTION].Tedad2User, [TRANSACTION].UnitPriceUser, 
                      [TRANSACTION].ZaribChildUser, [TRANSACTION].KalaIDChild, CASE WHEN ZaribChildUser > 0 THEN '(' + T_RelateKala.NameKala2 + ')' ELSE '' END AS NameKalaChild
FROM         T_KolHesab RIGHT OUTER JOIN
                      T_T_Heasb RIGHT OUTER JOIN
                      T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                          (SELECT     ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID, GOID, ThesabIDSath2, THESABID, 
                                                   HesabID
                             FROM         T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID ON FunInfoKalaAll_1.ID = [TRANSACTION].KALAID AND 
                      FunInfoKalaAll_1.AID = [TRANSACTION].AID LEFT OUTER JOIN
                      T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2 LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) 
                                                   AS THESAB
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL ON T_Forosh.KOLID = DERIVEDTBL.KOLID AND T_Forosh.Hesab = DERIVEDTBL.HESAB AND 
                      T_Forosh.THesab = DERIVEDTBL.THESAB ON T_Heasb.HESABID = T_Forosh.Hesab ON T_T_Heasb.THESABID = T_Forosh.THesab ON T_KolHesab.KOLID = T_Forosh.KOLID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 2) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Forosh.FOROSHID = TBL.FactID
WHERE     (T_Forosh.FOROSHID = @FOROSHID)

ELSE
	BEGIN

			SET NOCOUNT ON;

			Declare @SQLQuery AS NVarchar(MAX)
			Declare @ParamDefinition AS NVarchar(MAX) 
			DECLARE @OrderBy AS NVARCHAR(MAX)
			DECLARE @GROUPBy AS NVARCHAR(MAX)
		    
			IF (@StarteDate IS NULL)
					SET @StarteDate='0'
		    
				IF (@ENDDate IS NULL)
					SET @ENDDate='99999999'
						SET @SQLQuery = '  SELECT     [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
                      CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, 
                      T_Forosh.KOLID AS CodeKol, T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, [TRANSACTION].FieldN7 AS Takhfif, T_Forosh.DateFo AS Tarikh, 
                      [TRANSACTION].Sharh AS SharhRadif, T_Forosh.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, 
                      FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, 
                      T_Forosh.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, 
                      T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, 
                      FunInfoKalaAll_1.NameKala1, T_Forosh.FOROSHID, DERIVEDTBL.mande AS MandeHesab, ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) 
                      = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, T_Forosh.ForoshNumbr + FunInfoKalaAll_1.AIDNumber AS CodeAnbar, FunInfoKalaAll_1.NameAnbar, 
                      FunInfoKalaAll_1.SharhAnbar AS AddressAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad2 END AS Tedad2, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Tedad1User, [TRANSACTION].Tedad2User, [TRANSACTION].UnitPriceUser, 
                      [TRANSACTION].ZaribChildUser, [TRANSACTION].KalaIDChild, CASE WHEN ZaribChildUser > 0 THEN ''('' + T_RelateKala.NameKala2 + '')'' ELSE '''' END AS NameKalaChild
FROM         T_KolHesab RIGHT OUTER JOIN
                      T_T_Heasb RIGHT OUTER JOIN
                      T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                          (SELECT     ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID, GOID, ThesabIDSath2, THESABID, 
                                                   HesabID
                             FROM         T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID ON FunInfoKalaAll_1.ID = [TRANSACTION].KALAID AND 
                      FunInfoKalaAll_1.AID = [TRANSACTION].AID LEFT OUTER JOIN
                      T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2 LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) 
                                                   AS THESAB
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL ON T_Forosh.KOLID = DERIVEDTBL.KOLID AND T_Forosh.Hesab = DERIVEDTBL.HESAB AND 
                      T_Forosh.THesab = DERIVEDTBL.THESAB ON T_Heasb.HESABID = T_Forosh.Hesab ON T_T_Heasb.THESABID = T_Forosh.THesab ON T_KolHesab.KOLID = T_Forosh.KOLID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 2) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Forosh.FOROSHID = TBL.FactID
WHERE     (1 = 1)
		'


			SET @GROUPBy = '  '
			SET @OrderBy = '  '

			Set @ParamDefinition = '@UserID INT,
									@GroupID NVARCHAR(MAX),
									@KOLID NVARCHAR(MAX), 
									@HesabidID NVARCHAR(MAX), 
									@THesabidID NVARCHAR(MAX), 
									@THesabidIDSath2 NVARCHAR(MAX), 
									@StarteDate NVARCHAR(MAX), 
									@ENDDate NVARCHAR(MAX),
									@KHARIDID1 INT,
									@KHARIDID2 INT '  	
			
			/* Execute the Transact-SQL String with all parameter value's 	   
			   Using sp_executesql Command */
			   
		If (@GroupID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
	         
		If (@KOLID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.KOLID IN('+ @KOLID +'))'
	    
		If (@HesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.HESABID IN('+ @HesabidID +') )'

		If (@THesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.THESABID IN('+ @THesabidID +') )'

		If (@THesabidIDSath2 Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'    
        
        
       If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))
          Set @SQLQuery = @SQLQuery + ' And  T_Forosh.ForoshNumbr Between @KHARIDID1 And @KHARIDID2' 
        
        
        Set @SQLQuery = @SQLQuery + ' And  T_Forosh.DateFo Between @StarteDate And @ENDDate'
        			   
			   SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
			Execute sp_Executesql   @SQLQuery, 
									@ParamDefinition,
									@UserID ,
									@GroupID,
									@KOLID , 
									@HesabidID , 
									@THesabidID , 
									@THesabidIDSath2 , 
									@StarteDate , 
									@ENDDate,
									@KHARIDID1,
									@KHARIDID2


			If @@ERROR <> 0 GoTo ErrorHandler
			Set NoCount OFF
		  
			ErrorHandler:
				Return(@@ERROR)
	END		
END







GO




/****** Object:  StoredProcedure [dbo].[T_NoReportUser_Upate_Delete]    Script Date: 08/25/2019 10:55:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[T_NoReportUser_Upate_Delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[T_NoReportUser_Upate_Delete]
GO



/****** Object:  StoredProcedure [dbo].[T_NoReportUser_Upate_Delete]    Script Date: 08/25/2019 10:55:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  CREATE PROCEDURE [dbo].[T_NoReportUser_Upate_Delete]  		
  
  @USERID INT, 		
  @LinkSub Nvarchar(255)
  AS 
 
  DELETE FROM T_NoReportUser
WHERE     (NOT (FldKey IN
                          (SELECT     TRAID
FROM         TRANSACTION2
WHERE     (Date1 = @LinkSub) AND (NOT (TRAID IS NULL))))) AND (UserID = @UserID)

  



GO




/****** Object:  StoredProcedure [dbo].[T_NoReportUser_Insert]    Script Date: 08/25/2019 10:55:26 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[T_NoReportUser_Insert]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[T_NoReportUser_Insert]
GO



/****** Object:  StoredProcedure [dbo].[T_NoReportUser_Insert]    Script Date: 08/25/2019 10:55:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  CREATE PROCEDURE [dbo].[T_NoReportUser_Insert]  		
  
  @USERID INT, 		
  @LinkSub Nvarchar(255),
  @USERIDCreate INT
  AS 
 
  
INSERT INTO T_NoReportUser
                      (GOID, KOLID, HESABID, THESABID, UserID,userCreate)
SELECT     GOID, KOLID, HESABID, THESABID, @Userid AS UserID,@USERIDCreate
FROM         TRANSACTION2
WHERE     (Date1 = @LinkSub) AND (TRAID IS NULL)
  



GO




/****** Object:  StoredProcedure [dbo].[T_NoReportUser_Insert_To_TEmp]    Script Date: 08/25/2019 10:55:30 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[T_NoReportUser_Insert_To_TEmp]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[T_NoReportUser_Insert_To_TEmp]
GO



/****** Object:  StoredProcedure [dbo].[T_NoReportUser_Insert_To_TEmp]    Script Date: 08/25/2019 10:55:30 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  CREATE PROCEDURE [dbo].[T_NoReportUser_Insert_To_TEmp]  		
  
  @USERID int,
  @LinkSub Nvarchar(255)
  
  AS 
 
INSERT INTO TRANSACTION2
                      (GOID, KOLID, HESABID, THESABID, TRAID, Date1)
SELECT     GOID, KOLID, HESABID, THESABID, FldKey, @LinkSub AS Date1
FROM         T_NoReportUser
WHERE     (UserID = @USERID)

  



GO



/****** Object:  StoredProcedure [dbo].[Q_Calc_TimeOut_Edit]    Script Date: 08/28/2019 16:25:19 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Calc_TimeOut_Edit]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Calc_TimeOut_Edit]
GO



/****** Object:  StoredProcedure [dbo].[Q_Calc_TimeOut_Edit]    Script Date: 08/28/2019 16:25:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[Q_Calc_TimeOut_Edit] 
        
        @UserID INT , @DateCreate datetime,@TimeOut bit output
        
        AS 
        
Begin  

	 declare @EndEditOpNoe int;
	 declare @EndEditOpValue int;
	 declare @EkhtelafDatetime int;
	  
	  
	  if (@UserID<>3)
					SELECT     @EndEditOpNoe=EndEditOpNoe, @EndEditOpValue=EndEditOpValue
					FROM         AC_Setting.dbo.T_yUsers
					WHERE     (USERID = @UserID)
	  else
			begin
					set @EndEditOpNoe=null
					set @EndEditOpValue=null
			end
			
	  if(@EndEditOpNoe=1) /*ساعت*/
	  
				SELECT     @EkhtelafDatetime=DATEDIFF(HH, @DateCreate, GETDATE()) 

	  else if  (@EndEditOpNoe=2) /*روز*/
				SELECT     @EkhtelafDatetime=DATEDIFF(DD, @DateCreate, GETDATE()) 
	  else
			set @EkhtelafDatetime=0
		
		
		if (@EkhtelafDatetime>@EndEditOpValue )
			set @TimeOut=1
		else
			set @TimeOut=0
		
		
		
end

GO


/****** Object:  StoredProcedure [dbo].[Q_Paye_UserAccessReportHesab]    Script Date: 08/28/2019 16:25:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Paye_UserAccessReportHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Paye_UserAccessReportHesab]
GO



/****** Object:  StoredProcedure [dbo].[Q_Paye_UserAccessReportHesab]    Script Date: 08/28/2019 16:25:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




        CREATE PROCEDURE [dbo].[Q_Paye_UserAccessReportHesab] 
        
        @UserID INT ,@KolID INT ,@HESABID INT ,@THESABID INT , @ResultVal bit output
        
        AS 
        
Begin  

set @ResultVal=0

if(NOT (@THesabid IS NULL))
		begin
				SELECT     @KolID=KolID, @HESABID=HESABID
				FROM         T_T_Heasb
				WHERE     (THESABID = @THESABID)

				SELECT     @ResultVal=COUNT(*) 
				FROM         (SELECT     KOLID
				FROM         T_NoReportUser
				WHERE     (UserID = @UserID) AND (KOLID = @KolID) AND (HESABID IS NULL) AND (THESABID IS NULL)
				UNION ALL
				SELECT     HESABID
				FROM         T_NoReportUser AS T_NoReportUser_2
				WHERE     (UserID = @UserID) AND (HESABID = @HESABID) AND (THESABID IS NULL)
				UNION ALL
				SELECT     THESABID
				FROM         T_NoReportUser AS T_NoReportUser_1
				WHERE     (UserID = @UserID) AND (THESABID = @THESABID)) AS derivedtbl_1
		end

else
if(NOT (@Hesabid IS NULL))
		begin
				SELECT     @KolID=KolID
				FROM         T_Heasb
				WHERE     (HESABID = @HESABID)

				SELECT     @ResultVal=COUNT(*) 
				FROM         (SELECT     KOLID
				FROM         T_NoReportUser
				WHERE     (UserID = @UserID) AND (KOLID = @KolID) AND (HESABID IS NULL) AND (THESABID IS NULL)
				UNION ALL
				SELECT     HESABID
				FROM         T_NoReportUser AS T_NoReportUser_2
				WHERE     (UserID = @UserID) AND (HESABID = @HESABID) AND (THESABID IS NULL)) AS derivedtbl_1
		end

else
		begin
			SELECT     @ResultVal=COUNT(*) 
			FROM         (SELECT     KOLID
			FROM         T_NoReportUser
			WHERE     (UserID = @UserID) AND (KOLID = @KolID) AND (HESABID IS NULL) AND (THESABID IS NULL)
			) AS derivedtbl_1
		end
		
		
		
end


GO










/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListBarForoosh]    Script Date: 08/28/2019 16:26:54 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListBarForoosh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListBarForoosh]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListBarForoosh]    Script Date: 08/28/2019 16:26:54 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





         CREATE PROCEDURE [dbo].[QSubPFrm_ListBarForoosh]   @UserID INT AS 
         
         Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
  
		SELECT     T_Par7.PARID7, T_Par7.HesabID, T_Par7.GOID, T_Par7.KOLID, T_Par7.MOINID, T_Par7.THESABID, T_Par7.TextName, T_Par7.TextName1, T_Par7.TextNameT, T_Par7.NumberFO, T_Par7.DateFo, 
							  T_Par7.TextShSA, T_Par7.NumSID2, T_Par7.Sharh, T_Par7.FieldN1, T_Par7.FieldN2, T_Par7.FieldN3, T_Par7.FieldN4, T_Par7.FieldT1, T_Par7.FieldT2, T_Par7.FieldT3, T_Par7.FieldT3 AS Expr1, 
							  T_Par7.FieldT4, T_Par7.Flag1, T_Par7.Flag2, T_Par7.Flag3, T_Par7.Flag4, T_Par7.PARID7Number, T_Par7.THESABIDAmel, T_Par7.PrimarySaid2, T_SA2.SAID2Number, 
							  dbo.HesabName(T_Par7.KOLID, T_Par7.HesabID, T_Par7.THESABID) AS namehesab, CONVERT(int, T_Par7.PARID7Number) AS Expr2, T_Par7.Avarez, T_Par7.Maliyat, T_Par7.Takhfif, 
							  DrvMabFact.FieldN3 AS MabFact, CONVERT(float, DrvMabFact.TedadBarFo) AS TedadBarFo, F_MabHazine_BarForosh_1.MabHazine
		FROM         T_Par7 INNER JOIN
							  T_SA2 ON T_Par7.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
								  (SELECT     PARID7, SUM(ABS(FieldN3)) AS FieldN3, SUM(TedadBarFo) AS TedadBarFo
									 FROM         [TRANSACTION]
									 GROUP BY PARID7) AS DrvMabFact ON T_Par7.PARID7 = DrvMabFact.PARID7 LEFT OUTER JOIN
							  dbo.F_MabHazine_BarForosh() AS F_MabHazine_BarForosh_1 ON T_Par7.PARID7 = F_MabHazine_BarForosh_1.FactID
		ORDER BY Expr2 DESC

else
	SELECT     T_Par7.PARID7, T_Par7.HesabID, T_Par7.GOID, T_Par7.KOLID, T_Par7.MOINID, T_Par7.THESABID, T_Par7.TextName, T_Par7.TextName1, T_Par7.TextNameT, T_Par7.NumberFO, T_Par7.DateFo, 
						  T_Par7.TextShSA, T_Par7.NumSID2, T_Par7.Sharh, T_Par7.FieldN1, T_Par7.FieldN2, T_Par7.FieldN3, T_Par7.FieldN4, T_Par7.FieldT1, T_Par7.FieldT2, T_Par7.FieldT3, T_Par7.FieldT3 AS Expr1, 
						  T_Par7.FieldT4, T_Par7.Flag1, T_Par7.Flag2, T_Par7.Flag3, T_Par7.Flag4, T_Par7.PARID7Number, T_Par7.THESABIDAmel, T_Par7.PrimarySaid2, T_SA2.SAID2Number, 
						  dbo.HesabName(T_Par7.KOLID, T_Par7.HesabID, T_Par7.THESABID) AS namehesab, CONVERT(int, T_Par7.PARID7Number) AS Expr2, T_Par7.Avarez, T_Par7.Maliyat, T_Par7.Takhfif, 
						  DrvMabFact.FieldN3 AS MabFact, CONVERT(float, DrvMabFact.TedadBarFo) AS TedadBarFo, F_MabHazine_BarForosh_1.MabHazine
	FROM         T_Par7 INNER JOIN
						  T_SA2 ON T_Par7.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
							  (SELECT     PARID7, SUM(ABS(FieldN3)) AS FieldN3, SUM(TedadBarFo) AS TedadBarFo
								 FROM         [TRANSACTION]
								 GROUP BY PARID7) AS DrvMabFact ON T_Par7.PARID7 = DrvMabFact.PARID7 LEFT OUTER JOIN
						  dbo.F_MabHazine_BarForosh() AS F_MabHazine_BarForosh_1 ON T_Par7.PARID7 = F_MabHazine_BarForosh_1.FactID
	WHERE     (NOT (ISNULL(T_Par7.userCreate, - 10) IN
							  (SELECT     USERID
								 FROM         AC_Setting.dbo.T_yUsers
								 WHERE     (USERID = 3) OR
													   (FlagAmin <> 0))))
	ORDER BY Expr2 DESC

end


GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListBarKHarid]    Script Date: 08/28/2019 16:27:17 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListBarKHarid]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListBarKHarid]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListBarKHarid]    Script Date: 08/28/2019 16:27:17 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




         CREATE PROCEDURE [dbo].[QSubPFrm_ListBarKHarid] @UserID INT  AS 
         
         Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
							SELECT     T_Dar8.DARID8, T_Dar8.HesabID, T_Dar8.GOID, T_Dar8.KOLID, T_Dar8.MOINID, T_Dar8.THESABID, T_Dar8.TextName, T_Dar8.TextName1, T_Dar8.TextNameT, T_Dar8.TextNameT AS Expr1, 
												  T_Dar8.NumberKH, T_Dar8.DateKh, T_Dar8.TextShSA, T_Dar8.NumSID2, T_Dar8.Sharh, T_Dar8.DATE2, T_Dar8.FieldN1, T_Dar8.FieldN2, T_Dar8.FieldN3, T_Dar8.FieldN4, T_Dar8.FieldT1, 
												  T_Dar8.FieldT3, T_Dar8.Flag1, T_Dar8.Flag2, T_Dar8.DARID8Numbr, T_Dar8.PrimarySaid2, T_SA2.SAID2Number, dbo.HesabName(T_Dar8.KOLID, T_Dar8.HesabID, T_Dar8.THESABID) 
												  AS namehesab, CONVERT(int, T_Dar8.DARID8Numbr) AS Expr2, T_Dar8.Takhfif, T_Dar8.Maliyat, T_Dar8.Avarez, F_MabHazine_KharidBarKHarid_1.MabHazine, drVTra.FieldN3 AS MabFact, 
												  CONVERT(float, drVTra.TedadBarKh) AS TedadBarKh
							FROM         T_Dar8 INNER JOIN
												  T_SA2 ON T_Dar8.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
													  (SELECT     DARID8, SUM(ABS(FieldN3)) AS FieldN3, SUM(TedadBarKh) AS TedadBarKh
														 FROM         [TRANSACTION]
														 GROUP BY DARID8) AS drVTra ON T_Dar8.DARID8 = drVTra.DARID8 LEFT OUTER JOIN
												  dbo.F_MabHazine_KharidBarKHarid() AS F_MabHazine_KharidBarKHarid_1 ON T_Dar8.DARID8 = F_MabHazine_KharidBarKHarid_1.FactID
							ORDER BY Expr2 DESC

else
					SELECT     T_Dar8.DARID8, T_Dar8.HesabID, T_Dar8.GOID, T_Dar8.KOLID, T_Dar8.MOINID, T_Dar8.THESABID, T_Dar8.TextName, T_Dar8.TextName1, T_Dar8.TextNameT, T_Dar8.TextNameT AS Expr1, 
										  T_Dar8.NumberKH, T_Dar8.DateKh, T_Dar8.TextShSA, T_Dar8.NumSID2, T_Dar8.Sharh, T_Dar8.DATE2, T_Dar8.FieldN1, T_Dar8.FieldN2, T_Dar8.FieldN3, T_Dar8.FieldN4, T_Dar8.FieldT1, 
										  T_Dar8.FieldT3, T_Dar8.Flag1, T_Dar8.Flag2, T_Dar8.DARID8Numbr, T_Dar8.PrimarySaid2, T_SA2.SAID2Number, dbo.HesabName(T_Dar8.KOLID, T_Dar8.HesabID, T_Dar8.THESABID) 
										  AS namehesab, CONVERT(int, T_Dar8.DARID8Numbr) AS Expr2, T_Dar8.Takhfif, T_Dar8.Maliyat, T_Dar8.Avarez, F_MabHazine_KharidBarKHarid_1.MabHazine, drVTra.FieldN3 AS MabFact, 
										  CONVERT(float, drVTra.TedadBarKh) AS TedadBarKh
					FROM         T_Dar8 INNER JOIN
										  T_SA2 ON T_Dar8.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
											  (SELECT     DARID8, SUM(ABS(FieldN3)) AS FieldN3, SUM(TedadBarKh) AS TedadBarKh
												 FROM         [TRANSACTION]
												 GROUP BY DARID8) AS drVTra ON T_Dar8.DARID8 = drVTra.DARID8 LEFT OUTER JOIN
										  dbo.F_MabHazine_KharidBarKHarid() AS F_MabHazine_KharidBarKHarid_1 ON T_Dar8.DARID8 = F_MabHazine_KharidBarKHarid_1.FactID
					WHERE     (NOT (ISNULL(T_Dar8.userCreate, - 10) IN
											  (SELECT     USERID
												 FROM         AC_Setting.dbo.T_yUsers
												 WHERE     (USERID = 3) OR
																	   (FlagAmin <> 0))))
					ORDER BY Expr2 DESC
end

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqVosooly]    Script Date: 08/28/2019 16:28:09 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqVosooly]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqVosooly]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqVosooly]    Script Date: 08/28/2019 16:28:09 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqVosooly] 
            @UserID INT
      AS 
                   Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
		 SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 3) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC
else

		SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
		T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
		T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
		T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
		T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
		T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
		FROM         T_Asnad_Dar LEFT OUTER JOIN
		T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2 LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2
		WHERE     (T_Asnad_Dar.FieldN1 = 3) AND (NOT (T_Asnad_Dar.userCreate IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0))))
		ORDER BY T_Asnad_Dar.AsnadDarNumber DESC
		
end		 


GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPassPar]    Script Date: 08/28/2019 16:28:05 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqPassPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqPassPar]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPassPar]    Script Date: 08/28/2019 16:28:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqPassPar] 
        
        
@UserID INT
AS 
Begin  
DECLARE @FieldN1_Vas int=1 ;
declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

if (@UserID<>3)
SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)
else
begin
set @FlagAmin=1
set @FlagAdamRoyatSanadAdmin=0
end

if(@FlagAdamRoyatSanadAdmin=0)
		SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
		SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
		SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
		T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
		FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
		T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
		= 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
		T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
		[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
		AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
		FROM         T_Asnad INNER JOIN
		[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
		WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
		UNION ALL
		SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
		T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
		= 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
		AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
		Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
		T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
		FROM         T_Asnad AS T_Asnad_1 INNER JOIN
		Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
		WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
		T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
		ORDER BY SubAsnad.DateSR
else
		SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
		SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
		SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
		T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
		FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
		T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) 
		= 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, 
		[TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, 
		dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, 
		T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
		FROM         T_Asnad INNER JOIN
		[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
		WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad.userCreate IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0))))
		UNION ALL
		SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
		T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
		= 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, 
		T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, 
		Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, 
		T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
		FROM         T_Asnad AS T_Asnad_1 INNER JOIN
		Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
		WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad_1.userCreate IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0))))) AS SubAsnad LEFT OUTER JOIN
		T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
		ORDER BY SubAsnad.DateSR



end		
    


GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPass]    Script Date: 08/28/2019 16:28:01 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqPass]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqPass]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPass]    Script Date: 08/28/2019 16:28:01 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqPass] 
            @UserID INT
      AS 
                   Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
			SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 1) OR                       (dbo.T_Asnad_Dar.FieldN1 = 5) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC
	else
	
		SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
		T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
		T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
		T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
		T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
		T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
		FROM         T_Asnad_Dar LEFT OUTER JOIN
		T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2 LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2
		WHERE     (T_Asnad_Dar.FieldN1 = 1) AND (NOT (T_Asnad_Dar.userCreate IN (SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0)))) OR
		(T_Asnad_Dar.FieldN1 = 5)
		ORDER BY T_Asnad_Dar.AsnadDarNumber DESC
	
end	

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPassPar]    Script Date: 08/28/2019 16:27:57 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqNoPassPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqNoPassPar]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPassPar]    Script Date: 08/28/2019 16:27:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqNoPassPar]

@UserID INT
AS 
Begin  

DECLARE @FieldN1_Vas int=4 ;
declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

if (@UserID<>3)
SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)
else
begin
set @FlagAmin=1
set @FlagAdamRoyatSanadAdmin=0
end

if(@FlagAdamRoyatSanadAdmin=0)
			SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
			SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
			SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
			T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
			FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
			T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
			= 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
			T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
			[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
			AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
			FROM         T_Asnad INNER JOIN
			[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
			WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
			UNION ALL
			SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
			T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
			= 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
			AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
			Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
			T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
			FROM         T_Asnad AS T_Asnad_1 INNER JOIN
			Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
			WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
			T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
			T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
			ORDER BY SubAsnad.DateSR
else

	SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                      T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) 
                      = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, 
                      [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, 
                      T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
FROM         T_Asnad INNER JOIN
                      [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad.userCreate IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))
UNION ALL
SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                      T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                      = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, 
                      T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, 
                      Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, 
                      T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                      Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad_1.userCreate IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))) AS SubAsnad LEFT OUTER JOIN
T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR


end			

    


GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPass]    Script Date: 08/28/2019 16:27:53 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqNoPass]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqNoPass]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPass]    Script Date: 08/28/2019 16:27:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqNoPass] 
      @UserID INT
      AS 
                   Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
  
		SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
		T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
		T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
		T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
		T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
		T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
		FROM         T_Asnad_Dar LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN
		T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2
		WHERE     (T_Asnad_Dar.FieldN1 = 4)
		ORDER BY T_Asnad_Dar.AsnadDarNumber DESC

else
SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
                      T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
                      T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
                      T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
                      T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
                      T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
FROM         T_Asnad_Dar LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN
                      T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2
WHERE     (T_Asnad_Dar.FieldN1 = 4) AND (NOT (T_Asnad_Dar.userCreate IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))
ORDER BY T_Asnad_Dar.AsnadDarNumber DESC


end
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNaghdiPar]    Script Date: 08/28/2019 16:27:49 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqNaghdiPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqNaghdiPar]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNaghdiPar]    Script Date: 08/28/2019 16:27:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqNaghdiPar] 
        
@UserID INT
AS 
Begin  

DECLARE @FieldN1_Vas int=3 ;

declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

if (@UserID<>3)
SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)
else
begin
set @FlagAmin=1
set @FlagAdamRoyatSanadAdmin=0
end

if(@FlagAdamRoyatSanadAdmin=0)        
SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
                                              T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR


else

SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                      T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) 
                      = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, 
                      [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, 
                      T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
FROM         T_Asnad INNER JOIN
                      [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad.userCreate IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))
                        UNION ALL
            SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                      T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                      = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, 
                      T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, 
                      Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, 
                      T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                      Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad_1.userCreate IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR


end
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBatelShodePar]    Script Date: 08/28/2019 16:27:40 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqBatelShodePar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqBatelShodePar]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBatelShodePar]    Script Date: 08/28/2019 16:27:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqBatelShodePar] 
        
@UserID INT
AS 
Begin  
DECLARE @FieldN1_Vas int=2 ;
declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

if (@UserID<>3)
SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)
else
begin
set @FlagAmin=1
set @FlagAdamRoyatSanadAdmin=0
end

if(@FlagAdamRoyatSanadAdmin=0)
SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
                                              T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR

else

SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                      T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) 
                      = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, 
                      [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, 
                      T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
FROM         T_Asnad INNER JOIN
                      [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad.userCreate IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))
                        UNION ALL
     SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                      T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                      = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, 
                      T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, 
                      Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, 
                      T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                      Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad_1.userCreate IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR


end
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBargashti]    Script Date: 08/28/2019 16:27:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqBargashti]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqBargashti]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBargashti]    Script Date: 08/28/2019 16:27:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqBargashti]
      
            @UserID INT
      AS 
                   Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
			 SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 2) OR                       (dbo.T_Asnad_Dar.FieldN1 = 6) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC
   else
   			 
			SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
			T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
			T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
			T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
			T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
			T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
			FROM         T_Asnad_Dar LEFT OUTER JOIN
			T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2 LEFT OUTER JOIN
			T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2
			WHERE     (T_Asnad_Dar.FieldN1 = 2) AND (NOT (T_Asnad_Dar.userCreate IN
			(SELECT     USERID
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = 3) OR
			   (FlagAmin <> 0)))) OR
			(T_Asnad_Dar.FieldN1 = 6)
			ORDER BY T_Asnad_Dar.AsnadDarNumber DESC

end
GO




/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListSanadHesabdary]    Script Date: 08/28/2019 16:30:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_ListSanadHesabdary]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPfrm_ListSanadHesabdary]
GO


/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListSanadHesabdary]    Script Date: 08/28/2019 16:30:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[QSubPfrm_ListSanadHesabdary] @UserID INT AS 
        
        Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
  
		SELECT     SAID2, Sharh, Date2, FieldN2, FieldN1, FieldT1, Flag1, Flag2, Flag3, SAID2Number, OperationCode
		FROM         T_SA2
		ORDER BY SAID2 DESC

	else
	
		SELECT     SAID2, Sharh, Date2, FieldN2, FieldN1, FieldT1, Flag1, Flag2, Flag3, SAID2Number, OperationCode
		FROM         T_SA2
		WHERE     (NOT (ISNULL(userCreate, - 10) IN
								  (SELECT     USERID
									 FROM         AC_Setting.dbo.T_yUsers
									 WHERE     (USERID = 3) OR
														   (FlagAmin <> 0))))
		ORDER BY SAID2 DESC
	
	end
GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListForoosh]    Script Date: 08/28/2019 16:29:41 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListForoosh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListForoosh]
GO

/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListForoosh]    Script Date: 08/28/2019 16:29:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



          CREATE PROCEDURE [dbo].[QSubPFrm_ListForoosh] 
          
           @UserID INT
           
           AS       
             
             
             Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
			SELECT     T_Forosh.DateCreate, T_Forosh.DateModified, T_Forosh.FOROSHID, T_Forosh.HesabID, T_Forosh.GOID, T_Forosh.KOLID, T_Forosh.MOINID, T_Forosh.THESABID, T_Forosh.TextName, 
								  T_Forosh.TextName1, T_Forosh.TextNameT, T_Forosh.NumberKH, T_Forosh.DateFo, T_Forosh.Takhfif, T_Forosh.TextShSA, T_Forosh.NumSID2, T_Forosh.Sharh, T_Forosh.FieldN1, 
								  T_Forosh.FieldN2, T_Forosh.FieldN3, T_Forosh.FieldN4, T_Forosh.FieldT1, T_Forosh.FieldT2, T_Forosh.FieldT3, T_Forosh.FieldT3 AS Expr1, T_Forosh.FieldT4, T_Forosh.Flag1, T_Forosh.Flag2, 
								  T_Forosh.Flag3, T_Forosh.Flag4, T_Forosh.Flag5, T_Forosh.Flag6, T_Forosh.Flag7, T_Forosh.Flag8, T_Forosh.ForoshNumbr, T_Forosh.PrimarySaid2, T_Forosh.THESABIDAmel, 
								  T_SA2.SAID2Number, dbo.HesabName(T_Forosh.KOLID, T_Forosh.HesabID, T_Forosh.THESABID) AS namehesab, T_Forosh.dateSettlement, F_MabHazine_Foroosh_1.MabHazine, 
								  T_Forosh.Maliyat, T_Forosh.Avarez
			FROM         T_Forosh INNER JOIN
								  T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
								  dbo.F_MabHazine_Foroosh() AS F_MabHazine_Foroosh_1 ON T_Forosh.FOROSHID = F_MabHazine_Foroosh_1.FactID
			ORDER BY T_Forosh.DateFo DESC, T_Forosh.ForoshNumbr DESC

else

			SELECT     T_Forosh.DateCreate, T_Forosh.DateModified, T_Forosh.FOROSHID, T_Forosh.HesabID, T_Forosh.GOID, T_Forosh.KOLID, T_Forosh.MOINID, T_Forosh.THESABID, T_Forosh.TextName, 
								  T_Forosh.TextName1, T_Forosh.TextNameT, T_Forosh.NumberKH, T_Forosh.DateFo, T_Forosh.Takhfif, T_Forosh.TextShSA, T_Forosh.NumSID2, T_Forosh.Sharh, T_Forosh.FieldN1, 
								  T_Forosh.FieldN2, T_Forosh.FieldN3, T_Forosh.FieldN4, T_Forosh.FieldT1, T_Forosh.FieldT2, T_Forosh.FieldT3, T_Forosh.FieldT3 AS Expr1, T_Forosh.FieldT4, T_Forosh.Flag1, T_Forosh.Flag2, 
								  T_Forosh.Flag3, T_Forosh.Flag4, T_Forosh.Flag5, T_Forosh.Flag6, T_Forosh.Flag7, T_Forosh.Flag8, T_Forosh.ForoshNumbr, T_Forosh.PrimarySaid2, T_Forosh.THESABIDAmel, 
								  T_SA2.SAID2Number, dbo.HesabName(T_Forosh.KOLID, T_Forosh.HesabID, T_Forosh.THESABID) AS namehesab, T_Forosh.dateSettlement, F_MabHazine_Foroosh_1.MabHazine, 
								  T_Forosh.Maliyat, T_Forosh.Avarez
			FROM         T_Forosh INNER JOIN
								  T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
								  dbo.F_MabHazine_Foroosh() AS F_MabHazine_Foroosh_1 ON T_Forosh.FOROSHID = F_MabHazine_Foroosh_1.FactID
			WHERE     (NOT (ISNULL(T_Forosh.userCreate, - 10) IN
									  (SELECT     USERID
										 FROM         AC_Setting.dbo.T_yUsers
										 WHERE     (USERID = 3) OR
															   (FlagAmin <> 0))))
			ORDER BY T_Forosh.DateFo DESC, T_Forosh.ForoshNumbr DESC

end



GO



/****** Object:  StoredProcedure [dbo].[Q_ListDaryaftNaghdi]    Script Date: 08/28/2019 16:31:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_ListDaryaftNaghdi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_ListDaryaftNaghdi]
GO



/****** Object:  StoredProcedure [dbo].[Q_ListDaryaftNaghdi]    Script Date: 08/28/2019 16:31:56 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[Q_ListDaryaftNaghdi] @UserID INT  AS 
        
        Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
        
				   SELECT     T_Dar1.TextSharh, T_Dar1.TextName1, T_Dar1.TextName2, T_Dar1.Mablagh, T_Dar1.DARID1, T_SA2.SAID2Number, T_Dar1.DATE2, T_Dar1.DAR1Numbr, T_Dar1.PrimarySaid2
			FROM         T_Dar1 INNER JOIN
								  T_SA2 ON T_Dar1.PrimarySaid2 = T_SA2.SAID2
			ORDER BY T_Dar1.DARID1 DESC
	else
		SELECT     T_Dar1.TextSharh, T_Dar1.TextName1, T_Dar1.TextName2, T_Dar1.Mablagh, T_Dar1.DARID1, T_SA2.SAID2Number, T_Dar1.DATE2, T_Dar1.DAR1Numbr, T_Dar1.PrimarySaid2
		FROM         T_Dar1 INNER JOIN
							  T_SA2 ON T_Dar1.PrimarySaid2 = T_SA2.SAID2
		WHERE     (NOT (ISNULL(T_Dar1.userCreate, - 10) IN
								  (SELECT     USERID
									 FROM         AC_Setting.dbo.T_yUsers
									 WHERE     (USERID = 3) OR
														   (FlagAmin <> 0))))
		ORDER BY T_Dar1.DARID1 DESC
		
		end			


GO



/****** Object:  StoredProcedure [dbo].[Q_ListPardakhtNaghdi]    Script Date: 08/28/2019 16:32:25 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_ListPardakhtNaghdi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_ListPardakhtNaghdi]
GO



/****** Object:  StoredProcedure [dbo].[Q_ListPardakhtNaghdi]    Script Date: 08/28/2019 16:32:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


        CREATE PROCEDURE [dbo].[Q_ListPardakhtNaghdi] @UserID INT AS 
        
        Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
  
				SELECT     T_Par1.TextSharh, T_Par1.TextName2, T_Par1.Mablagh, T_Par1.PARID1, T_SA2.SAID2Number, T_Par1.PrimarySaid2, T_Par1.PARID1Number, T_Par1.DATE2, T_Par1.TextShSA, 
							  T_Par1.TextName1
		FROM         T_Par1 INNER JOIN
							  T_SA2 ON T_Par1.PrimarySaid2 = T_SA2.SAID2
		ORDER BY T_Par1.PARID1 DESC
	else
	
	SELECT     T_Par1.TextSharh, T_Par1.TextName2, T_Par1.Mablagh, T_Par1.PARID1, T_SA2.SAID2Number, T_Par1.PrimarySaid2, T_Par1.PARID1Number, T_Par1.DATE2, T_Par1.TextShSA, 
						  T_Par1.TextName1
	FROM         T_Par1 INNER JOIN
						  T_SA2 ON T_Par1.PrimarySaid2 = T_SA2.SAID2
	WHERE     (NOT (ISNULL(T_Par1.userCreate, - 10) IN
							  (SELECT     USERID
								 FROM         AC_Setting.dbo.T_yUsers
								 WHERE     (USERID = 3) OR
													   (FlagAmin <> 0))))
	ORDER BY T_Par1.PARID1 DESC
		
		
		
end
GO





/****** Object:  StoredProcedure [dbo].[QListTafsiliHesab_SanadHesabdari]    Script Date: 08/28/2019 16:58:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QListTafsiliHesab_SanadHesabdari]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QListTafsiliHesab_SanadHesabdari]
GO

/****** Object:  StoredProcedure [dbo].[QListTafsiliHesab_SanadHesabdari]    Script Date: 08/28/2019 16:58:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QListTafsiliHesab_SanadHesabdari](@Userid int,@said int, @Flag1 bit, @FilteerCode INT,@FlagNMande bit) AS 
      

BEGIN
	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    


                             

   
    
	IF (@FlagNMande=0)
		 SET @SQLQuery = 'SELECT     QMande_HesabTafsili_SanadHesabdary.THESABID AS ACCID, QMande_HesabTafsili_SanadHesabdary.HESABID, QMande_HesabTafsili_SanadHesabdary.THESABNumber AS CodeACC, 
                      QMande_HesabTafsili_SanadHesabdary.NameHesabT AS NameACC, QMande_HesabTafsili_SanadHesabdary.THESABNumber AS کد, QMande_HesabTafsili_SanadHesabdary.Onvane AS عنوان, 
                      CASE WHEN isnull(T_NoRepKol.KOLID, 0) > 0 OR
                      isnull(T_NoRepMoein.HESABID, 0) > 0 OR
                      isnull(T_NoRepTaf.THESABID, 0) > 0 THEN 0 ELSE mande END AS مانده, CASE WHEN (Mande) > 0 THEN N''بد'' WHEN (Mande) < 0 THEN N''بس'' ELSE ''--'' END AS تشخيص
FROM         dbo.QMande_HesabTafsili_SanadHesabdary(@said, @Flag1) AS QMande_HesabTafsili_SanadHesabdary LEFT OUTER JOIN
                          (SELECT     FldKey, UserID, GOID, KOLID, HESABID, THESABID
                             FROM         T_NoReportUser AS T_NoReportUser_1
                             WHERE     (UserID = @UserID) AND (HESABID IS NULL) AND (THESABID IS NULL)) AS T_NoRepKol ON QMande_HesabTafsili_SanadHesabdary.KOLID = T_NoRepKol.KOLID LEFT OUTER JOIN
                          (SELECT     FldKey, UserID, GOID, KOLID, HESABID, THESABID
                             FROM         T_NoReportUser AS T_NoReportUser_1
                             WHERE     (UserID = @UserID) AND (NOT (HESABID IS NULL)) AND (THESABID IS NULL)) AS T_NoRepMoein ON 
                      QMande_HesabTafsili_SanadHesabdary.HESABID = T_NoRepMoein.HESABID LEFT OUTER JOIN
                          (SELECT     FldKey, UserID, GOID, KOLID, HESABID, THESABID
                             FROM         T_NoReportUser AS T_NoReportUser_1
                             WHERE     (UserID = @UserID) AND (NOT (THESABID IS NULL))) AS T_NoRepTaf ON QMande_HesabTafsili_SanadHesabdary.THESABID = T_NoRepTaf.THESABID
WHERE     (1 = 1)'
	ELSE
		SET @SQLQuery = 'SELECT     THESABID AS ACCID, HESABID, THESABNumber AS CodeACC, NameHesabT AS NameACC, THESABNumber AS کد, NameHesabT + N''   '' + ISNULL(Mob, N'''') + N''    '' + ISNULL(Phone1, N'''') 
                      + N''   '' + ISNULL(Phone2, N'''') AS عنوان, ''--'' AS مانده, ''--'' AS تشخیص
FROM         T_T_Heasb
WHERE     (1 = 1)'
		

SET @GROUPBy ='  '
SET @OrderBy = ' ORDER BY NameACC '   
	

    If (@Flag1<>0)
         Set @SQLQuery = @SQLQuery + ' And (Active IN(0))'
     
         
    If (@FilteerCode>0)
    IF (@FlagNMande=0)
         Set @SQLQuery = @SQLQuery + ' And (QMande_HesabTafsili_SanadHesabdary.HESABID = @FilteerCode)'
    else
		Set @SQLQuery = @SQLQuery + ' And (HESABID = @FilteerCode)'
            
         	
    Set @ParamDefinition = '@Userid int,@said INT,
							@Flag1 bit,
							@FilteerCode int' 


	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@Userid,
							@said ,
							@Flag1,
							@FilteerCode 


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END
								


GO



/****** Object:  StoredProcedure [dbo].[QListMoinHesab_SanadHesabdari]    Script Date: 08/28/2019 16:57:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QListMoinHesab_SanadHesabdari]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QListMoinHesab_SanadHesabdari]
GO


/****** Object:  StoredProcedure [dbo].[QListMoinHesab_SanadHesabdari]    Script Date: 08/28/2019 16:57:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QListMoinHesab_SanadHesabdari](@Userid int,@Said int, @Flag1 bit, @FilteerCode int,@FlagNMande bit) AS 
BEGIN
	
	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)



		
IF (@FlagNMande=0)
	Set @SQLQuery='
SELECT     QMande_HesabMoin_SanadHesabdary.HESABID AS ACCID, QMande_HesabMoin_SanadHesabdary.KolID , QMande_HesabMoin_SanadHesabdary.HESABNumber AS CodeACC, 
                      QMande_HesabMoin_SanadHesabdary.NameHesabMoin AS NameACC, QMande_HesabMoin_SanadHesabdary.HESABNumber AS کد, 
                      QMande_HesabMoin_SanadHesabdary.NameHesabMoin AS عنوان, CASE WHEN isnull(T_NoeRepKol.KOLID, 0) > 0 OR
                      isnull(T_NoeRepMoein.HESABID, 0) > 0 THEN 0 ELSE mande END AS مانده, CASE WHEN (mande) > 0 THEN N''بد'' WHEN (mande) < 0 THEN N''بس'' ELSE ''--'' END AS تشخيص
FROM         dbo.QMande_HesabMoin_SanadHesabdary(@Said, @Flag1) AS QMande_HesabMoin_SanadHesabdary LEFT OUTER JOIN
                          (SELECT     FldKey, UserID, GOID, T_NoReportUser_1.KOLID, HESABID, THESABID, userCreate, userModify
                             FROM         T_NoReportUser AS T_NoReportUser_1
                             WHERE     (UserID = @UserID) AND (NOT (HESABID IS NULL)) AND (THESABID IS NULL)) AS T_NoeRepMoein ON 
                      QMande_HesabMoin_SanadHesabdary.HESABID = T_NoeRepMoein.HESABID LEFT OUTER JOIN
                          (SELECT     FldKey, UserID, GOID, T_NoReportUser.KOLID, HESABID, THESABID, userCreate, userModify
                             FROM         T_NoReportUser
                             WHERE     (UserID = @UserID) AND (HESABID IS NULL) AND (THESABID IS NULL)) AS T_NoeRepKol ON QMande_HesabMoin_SanadHesabdary.KolID = T_NoeRepKol.KOLID
WHERE     (1 = 1)
				   '

ELSE
	Set @SQLQuery='SELECT     HESABID AS ACCID, KolID  , HESABNumber AS CodeACC, NameHesab AS NameACC, HESABNumber AS کد, NameHesab AS عنوان, ''--'' AS مانده, ''--'' AS تشخيص
					FROM         T_Heasb
					WHERE     (1 = 1)'
				   

SET @GROUPBy ='  '
SET @OrderBy = ' ORDER BY NameACC '   
	


     
         
    If (@FilteerCode>0)
    IF (@FlagNMande=0)
         Set @SQLQuery = @SQLQuery + ' And ( QMande_HesabMoin_SanadHesabdary.Kolid = @FilteerCode)'
     else
         Set @SQLQuery = @SQLQuery + ' And ( Kolid = @FilteerCode)'   
         	
    Set @ParamDefinition = '@Userid INT, @said INT,
							@Flag1 bit,
							@FilteerCode int' 


	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@Userid,
							@said ,
							@Flag1,
							@FilteerCode 


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END



GO



/****** Object:  StoredProcedure [dbo].[QListKolHesab_SanadHesabdari]    Script Date: 08/28/2019 16:56:55 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QListKolHesab_SanadHesabdari]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QListKolHesab_SanadHesabdari]
GO



/****** Object:  StoredProcedure [dbo].[QListKolHesab_SanadHesabdari]    Script Date: 08/28/2019 16:56:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QListKolHesab_SanadHesabdari] (@Userid int,@Said int, @Flag1 bit, @FilteerCode int,@FlagNMande bit)  AS 

begin



if(@FlagNMande=0)
		SELECT     QMande_HesabKol_SanadHesabdary.KOLID AS ACCID, QMande_HesabKol_SanadHesabdary.GOID, QMande_HesabKol_SanadHesabdary.KolNumber AS CodeACC, 
							  QMande_HesabKol_SanadHesabdary.NameHesab AS NameACC, QMande_HesabKol_SanadHesabdary.KolNumber AS كد, QMande_HesabKol_SanadHesabdary.NameHesab AS عنوان, 
							  CASE WHEN isnull(T_NoReportUser.KOLID, 0) > 0 THEN 0 ELSE Mande END AS مانده, CASE WHEN (Mande) > 0 THEN N'بد' WHEN (Mande) < 0 THEN N'بس' ELSE '--' END AS تشخيص, 
							  QMande_HesabKol_SanadHesabdary.Mande
		FROM         dbo.QMande_HesabKol_SanadHesabdary(@Said, @Flag1) AS QMande_HesabKol_SanadHesabdary LEFT OUTER JOIN
							  (SELECT     FldKey, UserID, GOID, KOLID, HESABID, THESABID, userCreate, userModify
		FROM         T_NoReportUser
		WHERE     (UserID = @UserID) AND (HESABID IS NULL) AND (THESABID IS NULL)) as T_NoReportUser ON QMande_HesabKol_SanadHesabdary.KOLID = T_NoReportUser.KOLID
		ORDER BY NameACC
ELSE

		SELECT     KOLID AS ACCID, GOID, KolNumber AS CodeACC, NameHesab AS NameACC, KolNumber AS كد, NameHesab AS عنوان, '--' AS مانده, '--' AS تشخيص
		FROM         T_KolHesab
		ORDER BY NameACC

end
	     



GO








/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNaghdiPar]    Script Date: 09/02/2019 11:27:13 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqNaghdiPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqNaghdiPar]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNaghdiPar]    Script Date: 09/02/2019 11:27:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqNaghdiPar] 
        
@UserID INT
AS 
Begin  

DECLARE @FieldN1_Vas int=3 ;

declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

if (@UserID<>3)
SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)
else
begin
set @FlagAmin=1
set @FlagAdamRoyatSanadAdmin=0
end

if(@FlagAdamRoyatSanadAdmin=0)        
SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
                                              T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR


else

SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                      T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) 
                      = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, 
                      [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, 
                      T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
FROM         T_Asnad INNER JOIN
                      [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad.userCreate IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))
                        UNION ALL
            SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                      T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                      = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, 
                      T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, 
                      Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, 
                      T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                      Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas) AND (NOT (isnull(T_Asnad_1.userCreate,-10) IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR


end
GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqVosooly]    Script Date: 09/02/2019 11:27:08 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqVosooly]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqVosooly]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqVosooly]    Script Date: 09/02/2019 11:27:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqVosooly] 
            @UserID INT
      AS 
                   Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
		 SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 3) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC
else

		SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
		T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
		T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
		T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
		T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
		T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
		FROM         T_Asnad_Dar LEFT OUTER JOIN
		T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2 LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2
		WHERE     (T_Asnad_Dar.FieldN1 = 3) AND (NOT (isnull(T_Asnad_Dar.userCreate,-10) IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0))))
		ORDER BY T_Asnad_Dar.AsnadDarNumber DESC
		
end		 


GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBatelShodePar]    Script Date: 09/02/2019 11:26:22 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqBatelShodePar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqBatelShodePar]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBatelShodePar]    Script Date: 09/02/2019 11:26:22 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqBatelShodePar] 
        
@UserID INT
AS 
Begin  
DECLARE @FieldN1_Vas int=2 ;
declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

if (@UserID<>3)
SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)
else
begin
set @FlagAmin=1
set @FlagAdamRoyatSanadAdmin=0
end

if(@FlagAdamRoyatSanadAdmin=0)
SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                                              T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
                                              T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
                                              [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                                              AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
                        FROM         T_Asnad INNER JOIN
                                              [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
                        WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
                        UNION ALL
                        SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                                              T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                                              = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
                                              AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
                                              Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
                                              T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
                        FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                                              Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
                        WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR

else

SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
                      SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
                      SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
                      T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                      T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) 
                      = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, 
                      [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, 
                      T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
FROM         T_Asnad INNER JOIN
                      [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad.userCreate IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))
                        UNION ALL
     SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                      T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                      = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, 
                      T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, 
                      Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, 
                      T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                      Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas) AND (NOT (isnull(T_Asnad_1.userCreate,-10) IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))) AS SubAsnad LEFT OUTER JOIN
                      T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR


end
GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBargashti]    Script Date: 09/02/2019 11:25:45 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqBargashti]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqBargashti]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBargashti]    Script Date: 09/02/2019 11:25:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqBargashti]
      
            @UserID INT
      AS 
                   Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
			 SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 2) OR                       (dbo.T_Asnad_Dar.FieldN1 = 6) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC
   else
   			 
			SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
			T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
			T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
			T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
			T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
			T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
			FROM         T_Asnad_Dar LEFT OUTER JOIN
			T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2 LEFT OUTER JOIN
			T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2
			WHERE     (T_Asnad_Dar.FieldN1 = 2) AND (NOT (isnull(T_Asnad_Dar.userCreate,-10) IN
			(SELECT     USERID
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = 3) OR
			   (FlagAmin <> 0)))) OR
			(T_Asnad_Dar.FieldN1 = 6)
			ORDER BY T_Asnad_Dar.AsnadDarNumber DESC

end
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPassPar]    Script Date: 09/02/2019 11:24:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqPassPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqPassPar]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPassPar]    Script Date: 09/02/2019 11:24:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqPassPar] 
        
        
@UserID INT
AS 
Begin  
DECLARE @FieldN1_Vas int=1 ;
declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

if (@UserID<>3)
SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)
else
begin
set @FlagAmin=1
set @FlagAdamRoyatSanadAdmin=0
end

if(@FlagAdamRoyatSanadAdmin=0)
		SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
		SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
		SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
		T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
		FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
		T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
		= 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
		T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
		[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
		AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
		FROM         T_Asnad INNER JOIN
		[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
		WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
		UNION ALL
		SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
		T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
		= 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
		AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
		Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
		T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
		FROM         T_Asnad AS T_Asnad_1 INNER JOIN
		Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
		WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
		T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
		ORDER BY SubAsnad.DateSR
else
		SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
		SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
		SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
		T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
		FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
		T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) 
		= 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, 
		[TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, 
		dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, 
		T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
		FROM         T_Asnad INNER JOIN
		[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
		WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad.userCreate IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0))))
		UNION ALL
		SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
		T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
		= 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, 
		T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, 
		Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, 
		T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
		FROM         T_Asnad AS T_Asnad_1 INNER JOIN
		Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
		WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas) AND (NOT (isnull(T_Asnad_1.userCreate,-10) IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0))))) AS SubAsnad LEFT OUTER JOIN
		T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
		ORDER BY SubAsnad.DateSR



end		
    


GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPass]    Script Date: 09/02/2019 11:24:24 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqPass]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqPass]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPass]    Script Date: 09/02/2019 11:24:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqPass] 
            @UserID INT
      AS 
                   Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
			SELECT     dbo.T_Asnad_Dar.ASNADDARID, dbo.T_Asnad_Dar.AsnadDarNumber, dbo.T_Asnad_Dar.DateCreate, dbo.T_Asnad_Dar.DateModified, dbo.T_Asnad_Dar.VSabt, dbo.T_Asnad_Dar.GOID,                        dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID, dbo.T_Asnad_Dar.TARID, dbo.T_Asnad_Dar.SAID2, dbo.T_Asnad_Dar.Mablagh, dbo.T_Asnad_Dar.ShHesab,                        dbo.T_Asnad_Dar.Namebank, dbo.T_Asnad_Dar.ShSeryal, dbo.T_Asnad_Dar.Girandeh, dbo.T_Asnad_Dar.DARID2, dbo.T_Asnad_Dar.DARID3, dbo.T_Asnad_Dar.DateTA, dbo.T_Asnad_Dar.DateVO,                        dbo.T_Asnad_Dar.DateCH, dbo.T_Asnad_Dar.Sharh, dbo.T_Asnad_Dar.FieldN1, dbo.T_Asnad_Dar.FieldN2, dbo.T_Asnad_Dar.FieldN3, dbo.T_Asnad_Dar.FieldN4, dbo.T_Asnad_Dar.FieldT1,                        dbo.T_Asnad_Dar.FieldT2, dbo.T_Asnad_Dar.FieldT3, dbo.T_Asnad_Dar.FieldT4, dbo.T_Asnad_Dar.Flag1, dbo.T_Asnad_Dar.Flag2, dbo.T_Asnad_Dar.Flag3, dbo.T_Asnad_Dar.Flag4,                        dbo.T_Asnad_Dar.FlagStatus, dbo.T_SA2.SAID2Number, dbo.T_Asnad_Dar.FieldN1 AS Expr2, dbo.T_Asnad_Dar.AsnadDarNumber AS Expr1, T_SA2_1.SAID2Number AS sanad1,                        dbo.HesabName(dbo.T_Asnad_Dar.KOLID, dbo.T_Asnad_Dar.HESABID, dbo.T_Asnad_Dar.THESABID) AS NameHesabT FROM         dbo.T_Asnad_Dar LEFT OUTER JOIN                       dbo.T_SA2 ON dbo.T_Asnad_Dar.FieldN2 = dbo.T_SA2.SAID2 LEFT OUTER JOIN                       dbo.T_SA2 T_SA2_1 ON dbo.T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 WHERE     (dbo.T_Asnad_Dar.FieldN1 = 1) OR                       (dbo.T_Asnad_Dar.FieldN1 = 5) ORDER BY dbo.T_Asnad_Dar.AsnadDarNumber DESC
	else
	
		SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
		T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
		T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
		T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
		T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
		T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
		FROM         T_Asnad_Dar LEFT OUTER JOIN
		T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2 LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2
		WHERE     (T_Asnad_Dar.FieldN1 = 1) AND (NOT (Isnull(T_Asnad_Dar.userCreate,-10) IN (SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0)))) OR
		(T_Asnad_Dar.FieldN1 = 5)
		ORDER BY T_Asnad_Dar.AsnadDarNumber DESC
	
end	

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPassPar]    Script Date: 09/02/2019 11:22:54 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqNoPassPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqNoPassPar]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPassPar]    Script Date: 09/02/2019 11:22:54 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqNoPassPar]

@UserID INT
AS 
Begin  

DECLARE @FieldN1_Vas int=4 ;
declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

if (@UserID<>3)
SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)
else
begin
set @FlagAmin=1
set @FlagAdamRoyatSanadAdmin=0
end

if(@FlagAdamRoyatSanadAdmin=0)
			SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
			SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
			SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
			T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
			FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
			T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
			= 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
			T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
			[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
			AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
			FROM         T_Asnad INNER JOIN
			[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
			WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
			UNION ALL
			SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
			T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
			= 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) 
			AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, 
			Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, 
			T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
			FROM         T_Asnad AS T_Asnad_1 INNER JOIN
			Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
			WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
			T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
			T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
			ORDER BY SubAsnad.DateSR
else

	SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
                      T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) 
                      = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, 
                      [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, [TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, 
                      dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, 
                      T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
FROM         T_Asnad INNER JOIN
                      [TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad.userCreate IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))
UNION ALL
SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, T_Asnad_1.DateSR, 
                      T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) 
                      = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, 
                      T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, 
                      Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, 
                      T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, T_Asnad_1.PARID3
FROM         T_Asnad AS T_Asnad_1 INNER JOIN
                      Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas) AND (NOT (isnull(T_Asnad_1.userCreate,-10) IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))) AS SubAsnad LEFT OUTER JOIN
T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
ORDER BY SubAsnad.DateSR


end			

    


GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPass]    Script Date: 09/02/2019 11:19:21 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqNoPass]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqNoPass]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPass]    Script Date: 09/02/2019 11:19:21 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqNoPass] 
      @UserID INT
      AS 
                   Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
  
		SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
		T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
		T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
		T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
		T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
		T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
		FROM         T_Asnad_Dar LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN
		T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2
		WHERE     (T_Asnad_Dar.FieldN1 = 4)
		ORDER BY T_Asnad_Dar.AsnadDarNumber DESC

else
SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
                      T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
                      T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
                      T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
                      T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
                      T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
FROM         T_Asnad_Dar LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN
                      T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2
WHERE     (T_Asnad_Dar.FieldN1 = 4) AND (NOT (ISNULL(T_Asnad_Dar.userCreate, -10) IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))
ORDER BY T_Asnad_Dar.AsnadDarNumber DESC


end
GO




/****** Object:  StoredProcedure [dbo].[Q_Forosh_Insert_To_Temp]    Script Date: 09/04/2019 08:57:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Forosh_Insert_To_Temp]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Forosh_Insert_To_Temp]
GO



/****** Object:  StoredProcedure [dbo].[Q_Forosh_Insert_To_Temp]    Script Date: 09/04/2019 08:57:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




  CREATE PROCEDURE [dbo].[Q_Forosh_Insert_To_Temp]  		
  
  @USERID int,@FOROSHID int,@LinkToSubRezerve Nvarchar(255), @FlagEditOrEnteshar int /*edit 1 enteshar 0*/
  
  AS 
 
 if(@FlagEditOrEnteshar=1)
			INSERT INTO TRANSACTION2
								  (Zarib, PARBes, FieldN7, Maliatkala, Avarez, Ezafat, Tedad1User, Tedad2User, UnitPriceUser, KalaIDChild, ZaribChildUser, Tedad1, Tedad2, TRAID, Date1, UnitPrice, Tedad, DARBed, radif, AID, 
								  GOKID, KALAID, TextKalaName, Sharh, FieldN6, Flag2, Flag1, OperType, FieldT1, TextAID)
			SELECT     [TRANSACTION].Zarib, CASE WHEN ZaribChildUser > 0 THEN ROUND((ISNULL([TRANSACTION].Tedad1User, 0) * ISNULL([TRANSACTION].UnitPriceUser, 0)) + ISNULL([TRANSACTION].Maliatkala, 0) 
								  + ISNULL([TRANSACTION].Avarez, 0) - ISNULL([TRANSACTION].FieldN7, 0), 2) ELSE ROUND([TRANSACTION].TedadFo * [TRANSACTION].UnitPriceFo + ISNULL([TRANSACTION].Maliatkala, 0) 
								  + ISNULL([TRANSACTION].Avarez, 0) - ISNULL([TRANSACTION].FieldN7, 0), 2) END AS Parbes, [TRANSACTION].FieldN7, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Ezafat, 
								  ISNULL([TRANSACTION].Tedad1User, [TRANSACTION].TedadFo) AS Tedad1User, ISNULL([TRANSACTION].Tedad2User, 0) AS Tedad2User, ISNULL([TRANSACTION].UnitPriceUser, 
								  [TRANSACTION].UnitPriceFo) AS UnitPriceUser, [TRANSACTION].KalaIDChild, [TRANSACTION].ZaribChildUser, CASE WHEN Isnull([TRANSACTION].Tedad1, 0) + isnull([TRANSACTION].Tedad2, 0) 
								  = 0 THEN [TRANSACTION].Tedadfo ELSE [TRANSACTION].Tedad1 END AS Expr2, [TRANSACTION].Tedad2, [TRANSACTION].TRAID, @LinkToSubRezerve AS Expr3, [TRANSACTION].UnitPriceFo, 
								  [TRANSACTION].TedadFo AS Expr4, CASE WHEN ZaribChildUser > 0 THEN ISNULL([TRANSACTION].Tedad1User, 0) * ISNULL([TRANSACTION].UnitPriceUser, 0) 
								  ELSE ISNULL([TRANSACTION].TedadFo, 0) * ISNULL([TRANSACTION].UnitPriceFo, 0) END AS DARBed1, [TRANSACTION].radif, [TRANSACTION].AID, [TRANSACTION].GOKID, [TRANSACTION].KALAID, 
								  dbo.KalaName([TRANSACTION].KALAID) + CASE WHEN ZaribChildUser > 0 THEN '(' + NameKala2 + ')' ELSE '' END AS namKala, [TRANSACTION].Sharh, [TRANSACTION].FieldN6, 0 AS Expr1, 
								  1 AS Expr100, 2 AS Expr10, T_NameGoKala.NameGoKala1, T_Anbar.NameAnbar
			FROM         [TRANSACTION] INNER JOIN
								  T_Anbar ON [TRANSACTION].AID = T_Anbar.AID INNER JOIN
								  T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID LEFT OUTER JOIN
								  T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2
			WHERE     ([TRANSACTION].FOROSHID = @FOROSHID)
			ORDER BY [TRANSACTION].radif
else
 if(@FlagEditOrEnteshar=0)
		INSERT INTO TRANSACTION2
							  (Zarib, PARBes, FieldN7, Maliatkala, Avarez, Ezafat, Tedad1User, Tedad2User, UnitPriceUser, KalaIDChild, ZaribChildUser, Tedad1, Tedad2, TRAID, Date1, UnitPrice, Tedad, DARBed, radif, AID, 
							  GOKID, KALAID, TextKalaName, Sharh, FieldN6, Flag2, Flag1, OperType, FieldT1, TextAID)
		SELECT     [TRANSACTION].Zarib, CASE WHEN ZaribChildUser > 0 THEN ROUND((ISNULL([TRANSACTION].Tedad1User, 0) * ISNULL([TRANSACTION].UnitPriceUser, 0)) + ISNULL([TRANSACTION].Maliatkala, 0) 
							  + ISNULL([TRANSACTION].Avarez, 0) - ISNULL([TRANSACTION].FieldN7, 0), 2) ELSE ROUND([TRANSACTION].TedadFo * [TRANSACTION].UnitPriceFo + ISNULL([TRANSACTION].Maliatkala, 0) 
							  + ISNULL([TRANSACTION].Avarez, 0) - ISNULL([TRANSACTION].FieldN7, 0), 2) END AS Parbes, [TRANSACTION].FieldN7, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Ezafat, 
							  ISNULL([TRANSACTION].Tedad1User, [TRANSACTION].TedadFo) AS Tedad1User, ISNULL([TRANSACTION].Tedad2User, 0) AS Tedad2User, ISNULL([TRANSACTION].UnitPriceUser, 
							  [TRANSACTION].UnitPriceFo) AS UnitPriceUser, [TRANSACTION].KalaIDChild, [TRANSACTION].ZaribChildUser, CASE WHEN Isnull([TRANSACTION].Tedad1, 0) + isnull([TRANSACTION].Tedad2, 0) 
							  = 0 THEN [TRANSACTION].Tedadfo ELSE [TRANSACTION].Tedad1 END AS Expr2, [TRANSACTION].Tedad2, [TRANSACTION].TRAID, @LinkToSubRezerve AS Expr3, [TRANSACTION].UnitPriceFo, 
							  [TRANSACTION].TedadFo AS Expr4, CASE WHEN ZaribChildUser > 0 THEN ISNULL([TRANSACTION].Tedad1User, 0) * ISNULL([TRANSACTION].UnitPriceUser, 0) 
							  ELSE ISNULL([TRANSACTION].TedadFo, 0) * ISNULL([TRANSACTION].UnitPriceFo, 0) END AS DARBed1, [TRANSACTION].radif, [TRANSACTION].AID, [TRANSACTION].GOKID, [TRANSACTION].KALAID, 
							  dbo.KalaName([TRANSACTION].KALAID) + CASE WHEN ZaribChildUser > 0 THEN '(' + NameKala2 + ')' ELSE '' END AS namKala, [TRANSACTION].Sharh, [TRANSACTION].FieldN6, 1 AS Expr1, 
							  0 AS Expr100, 2 AS Expr10, T_NameGoKala.NameGoKala1, T_Anbar.NameAnbar
		FROM         [TRANSACTION] INNER JOIN
							  T_Anbar ON [TRANSACTION].AID = T_Anbar.AID INNER JOIN
							  T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID LEFT OUTER JOIN
							  T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2
		WHERE     ([TRANSACTION].FOROSHID = @FOROSHID)
		ORDER BY [TRANSACTION].radif




GO





/****** Object:  StoredProcedure [dbo].[Q_Paye_UserAccessRayatSanad]    Script Date: 09/04/2019 11:56:40 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Paye_UserAccessRayatSanad]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Paye_UserAccessRayatSanad]
GO



/****** Object:  StoredProcedure [dbo].[Q_Paye_UserAccessRayatSanad]    Script Date: 09/04/2019 11:56:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




        create PROCEDURE [dbo].[Q_Paye_UserAccessRayatSanad] 
        
        @UserID INT ,@SAID2 INT , @ResultVal bit output
        
        AS 
        
Begin  

declare  @FlagAmin int;

set @ResultVal=0



SELECT     @FlagAmin=FlagAmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)

if (@UserID=3)
	set @ResultVal=1 /* دسترسی دارد*/
else
if (@FlagAmin=0) 

SELECT     @ResultVal=COUNT(*) 
FROM         T_SA2
WHERE     (SAID2 = @SAID2) AND (NOT (ISNULL(userCreate, - 10) IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0))))

else
	set @ResultVal=1 /* دسترسی دارد*/
		
		
end


GO






/****** Object:  StoredProcedure [dbo].[Q_Paye_AccessSaghfBedBesHesab]    Script Date: 09/04/2019 13:13:09 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Paye_AccessSaghfBedBesHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Paye_AccessSaghfBedBesHesab]
GO


/****** Object:  StoredProcedure [dbo].[Q_Paye_AccessSaghfBedBesHesab]    Script Date: 09/04/2019 13:13:09 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




Create  PROCEDURE [dbo].[Q_Paye_AccessSaghfBedBesHesab]
		@THESABID INT,
        @SaghfBed DECIMAL(19,4) =0 OUTPUT,
        @SaghfBes DECIMAL(19,4) =0 OUTPUT
        
AS
BEGIN

			
		SELECT     @SaghfBed=SaghfBed, @SaghfBes=SaghfBes
		FROM         T_T_Heasb
		WHERE     (THESABID = @THESABID)
		

End


GO




/****** Object:  StoredProcedure [dbo].[PFrm_SubRepSood]    Script Date: 09/14/2019 12:37:09 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_SubRepSood]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PFrm_SubRepSood]
GO


/****** Object:  StoredProcedure [dbo].[PFrm_SubRepSood]    Script Date: 09/14/2019 12:37:09 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

  CREATE PROCEDURE [dbo].[PFrm_SubRepSood]( 	
  @KalaID NVARCHAR(MAX),  	
  @GOKID NVARCHAR(MAX),  	
  @AID NVARCHAR(MAX), 
  @GOID NVARCHAR(MAX),
  @KolID NVARCHAR(MAX), 	 
  @HesabID NVARCHAR(MAX), 
  @THesabID NVARCHAR(MAX), 
  @startDate nvarchar(50),  	
  @EndDate nvarchar(50) ) AS   
  
  /*@StartDate=forms![PFrm_Rep_SoodKala]![Textdate1],
	@EndDate=forms![PFrm_Rep_SoodKala]![Textdate2],
	@KalaID=forms![PFrm_Rep_SoodKala]![Kalaid],
	@GOKID=forms![PFrm_Rep_SoodKala]![GoKala],
	@AID=forms![PFrm_Rep_SoodKala]![AID],
	@GOID=forms![PFrm_Rep_SoodKala]![GOID],
	@KolID=forms![PFrm_Rep_SoodKala]![KolID],
	@HesabID=forms![PFrm_Rep_SoodKala]![HesabID],
	@THesabID=forms![PFrm_Rep_SoodKala]![THesabID]*/
  BEGIN 	 SET NOCOUNT ON;  
  DECLARE @SQLQuery AS NVarchar(MAX)  
  DECLARE @ParamDefinition AS NVarchar(MAX)  
  DECLARE @OrderBy AS NVARCHAR(MAX)  
  DECLARE @GROUPBy AS NVARCHAR(MAX)   
  
  IF (@startDate IS NULL) 	
  SET @startDate = '00000000'  
  
  IF (@EndDate IS NULL) 	
  SET @EndDate = '99999999' 	 
  SET @SQLQuery = ' SELECT     SUM(View_SubTransactionKala.FieldN5) AS Sood, T_NameKala.ID, PFrm_Rep_SoodKala_Mojoodi.Mojoodi, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, 
                      @KalaID AS Expr1, SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) AS SumForoosh, SUM(CONVERT(float, View_SubTransactionKala.TedadKh)) AS SumKharid, 
                      SUM(View_SubTransactionKala.TedadKh * View_SubTransactionKala.UnitPriceKH) / CASE WHEN SUM(CONVERT(float, View_SubTransactionKala.TedadKh)) > 0 THEN SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadKh)) ELSE 1 END AS MianginKharid, SUM(View_SubTransactionKala.TedadFo * View_SubTransactionKala.UnitpriceFo) / CASE WHEN SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadFo)) > 0 THEN SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) ELSE 1 END AS MianginForoosh, CASE WHEN SUM(dbo.View_SubTransactionKala.fieldn5)
                       > 0 THEN SUM(dbo.View_SubTransactionKala.fieldn5) ELSE 0 END AS sod, CASE WHEN SUM(dbo.View_SubTransactionKala.fieldn5) < 0 THEN abs(SUM(dbo.View_SubTransactionKala.fieldn5)) 
                      ELSE 0 END AS zian, SUM(CONVERT(float, View_SubTransactionKala.TedadBarFo)) AS SumBarForoosh, SUM(CONVERT(float, View_SubTransactionKala.TedadBarKh)) AS SumBarKharid, 
                      @startDate AS startDate, @EndDate AS EndDate, SUM(View_SubTransactionKala.FieldN5) / CASE WHEN (SUM(CONVERT(float, View_SubTransactionKala.TedadFo))) 
                      = 0 THEN 1 ELSE (SUM(CONVERT(float, View_SubTransactionKala.TedadFo))) END AS SoodTak
FROM         View_SubTransactionKala INNER JOIN
                      T_NameKala ON View_SubTransactionKala.KALAID = T_NameKala.ID INNER JOIN
                      PFrm_Rep_SoodKala_Mojoodi ON T_NameKala.ID = PFrm_Rep_SoodKala_Mojoodi.KALAID
WHERE     (View_SubTransactionKala.Tarikh BETWEEN @startDate AND @EndDate) AND (ISNULL(T_NameKala.FlagNoeKala, 0) = 0)   ' 
					
SET @GROUPBy = ' GROUP BY T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, PFrm_Rep_SoodKala_Mojoodi.Mojoodi, T_NameKala.ID ' 
SET @OrderBy = ' HAVING      (SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) <> 0) ORDER BY T_NameKala.NameKala1 '   

IF (@KalaID IS NOT NULL) 	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.KALAID IN(' + @KalaID + '))'   

IF (@GOKID IS NOT NULL) 	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.GOKID IN(' + @GOKID + '))'   

IF (@AID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.AID IN(' + @AID + ') )'  


IF (@GOID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.GOID IN(' + @GOID + ') )'  


IF (@KolID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.KolID IN(' + @KolID + ') )'  

IF (@HesabID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.HesabID IN(' + @HesabID + ') )'  

IF (@THesabID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.THesabID IN(' + @THesabID + ') )'  


SET @SQLQuery = @SQLQuery + ' ' 	
SET @ParamDefinition = '  @KalaID NVARCHAR(MAX),  	
						  @GOKID NVARCHAR(MAX),  	
						  @AID NVARCHAR(MAX), 
						  @GOID NVARCHAR(MAX),
						  @KolID NVARCHAR(MAX), 	 
						  @HesabID NVARCHAR(MAX), 
						  @THesabID NVARCHAR(MAX), 
						  @startDate nvarchar(50),  	
						  @EndDate nvarchar(50) ' 
						
SET               @SQLQuery = @SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy   

EXECUTE sp_Executesql @SQLQuery, @ParamDefinition, @KalaID, @GOKID, @AID, @GOID , @KolID, @HesabID, @THesabID, @startDate, @EndDate   

SET NoCount OFF   
END
GO



/****** Object:  StoredProcedure [dbo].[PFrm_SubRepSoodSF1]    Script Date: 09/14/2019 12:37:13 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_SubRepSoodSF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PFrm_SubRepSoodSF1]
GO



/****** Object:  StoredProcedure [dbo].[PFrm_SubRepSoodSF1]    Script Date: 09/14/2019 12:37:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[PFrm_SubRepSoodSF1](
		@KalaID NVARCHAR(MAX), 
		@GOKID NVARCHAR(MAX), 
		@AID NVARCHAR(MAX), 
		@GOID NVARCHAR(MAX),
		@KolID NVARCHAR(MAX), 	 
		@HesabID NVARCHAR(MAX), 
		@THesabID NVARCHAR(MAX), 
		@startDate nvarchar(50), 
		@EndDate nvarchar(50)
) AS 

BEGIN
	
SET NOCOUNT ON; 
DECLARE @SQLQuery AS NVarchar(MAX) 
DECLARE @ParamDefinition AS NVarchar(MAX) 
DECLARE @OrderBy AS NVARCHAR(MAX) 
DECLARE @GROUPBy AS NVARCHAR(MAX) 

IF (@startDate IS NULL)
	SET @startDate = '00000000' 
IF (@EndDate IS NULL)
	SET @EndDate = '99999999'
	
SET @SQLQuery = ' 
SELECT     SUM(View_SubTransactionKala.FieldN5) AS Sood, T_NameKala.ID, PFrm_Rep_SoodKala_Mojoodi.Mojoodi, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, 
                      @KalaID AS Expr1, SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) AS SumForoosh, SUM(CONVERT(float, View_SubTransactionKala.TedadKh)) AS SumKharid, 
                      SUM(View_SubTransactionKala.TedadKh *  (View_SubTransactionKala.UnitPriceKH + T_NameKala.UnitPrice2)) / CASE WHEN SUM(CONVERT(float, View_SubTransactionKala.TedadKh)) > 0 THEN SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadKh)) ELSE 1 END AS MianginKharid, (SUM(View_SubTransactionKala.TedadFo * View_SubTransactionKala.UnitpriceFo) / CASE WHEN SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadFo)) > 0 THEN SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) ELSE 1 END) AS MianginForoosh, 
                      CASE WHEN SUM(dbo.View_SubTransactionKala.fieldn5) > 0 THEN SUM(dbo.View_SubTransactionKala.fieldn5) ELSE 0 END AS sod, CASE WHEN SUM(dbo.View_SubTransactionKala.fieldn5) 
                      < 0 THEN abs(SUM(dbo.View_SubTransactionKala.fieldn5)) ELSE 0 END AS zian, SUM(CONVERT(float, View_SubTransactionKala.TedadBarFo)) AS SumBarForoosh, SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadBarKh)) AS SumBarKharid, @startDate AS startDate, @EndDate AS EndDate, SUM(View_SubTransactionKala.FieldN5) / CASE WHEN (SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadFo))) = 0 THEN 1 ELSE (SUM(CONVERT(float, View_SubTransactionKala.TedadFo))) END AS SoodTak
FROM         View_SubTransactionKala INNER JOIN
                      T_NameKala ON View_SubTransactionKala.KALAID = T_NameKala.ID INNER JOIN
                      PFrm_Rep_SoodKala_Mojoodi ON T_NameKala.ID = PFrm_Rep_SoodKala_Mojoodi.KALAID
WHERE     (View_SubTransactionKala.Tarikh BETWEEN @startDate AND @EndDate) AND (ISNULL(T_NameKala.FlagNoeKala, 0) = 0)

 '
SET @GROUPBy = ' GROUP BY T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, PFrm_Rep_SoodKala_Mojoodi.Mojoodi, T_NameKala.ID '
SET @OrderBy = '  HAVING      (SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) <> 0) ORDER BY T_NameKala.NameKala1 ' 

IF (@KalaID IS NOT NULL)
	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.KALAID IN(' + @KalaID + '))' 

IF (@GOKID IS NOT NULL)
	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.GOKID IN(' + @GOKID + '))' 

IF (@AID IS NOT NULL)
	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.AID IN(' + @AID + ') )'



IF (@GOID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.GOID IN(' + @GOID + ') )'  


IF (@KolID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.KolID IN(' + @KolID + ') )'  

IF (@HesabID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.HesabID IN(' + @HesabID + ') )'  

IF (@THesabID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.THesabID IN(' + @THesabID + ') )'  


SET @SQLQuery = @SQLQuery + ' '
	SET @ParamDefinition = '@KalaID NVARCHAR(MAX), 
							@GOKID NVARCHAR(MAX), 
							@AID NVARCHAR(MAX),
							@GOID NVARCHAR(MAX),
							@KolID NVARCHAR(MAX), 	 
							@HesabID NVARCHAR(MAX), 
							@THesabID NVARCHAR(MAX), 
							@startDate nvarchar(50), 
							@EndDate nvarchar(50) '
SET               @SQLQuery = @SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 

EXECUTE sp_Executesql @SQLQuery, @ParamDefinition, @KalaID, @GOKID, @AID, @GOID , @KolID, @HesabID, @THesabID,  @startDate,@EndDate 

SET NoCount OFF

 END
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm1_CloseShift]    Script Date: 09/19/2019 08:09:45 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm1_CloseShift]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm1_CloseShift]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm1_CloseShift]    Script Date: 09/19/2019 08:09:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

        CREATE PROCEDURE [dbo].[QSubPFrm1_CloseShift] @StartDateList Nvarchar(10),@EndDateList Nvarchar(10)   AS 
        
        SELECT     COUNT(T_Forosh.FOROSHID) AS Expr1, T_Forosh.DateFo, T_Forosh.Flag1
FROM         T_Forosh LEFT OUTER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2
WHERE     (T_SA2.FieldN2 = 26) AND (NOT (T_Forosh.UserID IS NULL)) AND (T_Forosh.DateFo BETWEEN @StartDateList AND @EndDateList) OR
                      (T_SA2.FieldN2 IS NULL) AND (NOT (T_Forosh.UserID IS NULL)) AND (T_Forosh.Flag1 = 0) AND (T_Forosh.DateFo BETWEEN @StartDateList AND @EndDateList)
GROUP BY T_Forosh.DateFo, T_Forosh.Flag1
ORDER BY T_Forosh.DateFo DESC, T_Forosh.Flag1
GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBargashti]    Script Date: 09/19/2019 09:42:24 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqBargashti]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqBargashti]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBargashti]    Script Date: 09/19/2019 09:42:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqBargashti]@UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10) AS  

Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
                      T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
                      T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
                      T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
                      T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
                      T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
FROM         T_Asnad_Dar LEFT OUTER JOIN
                      T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2
WHERE     (T_Asnad_Dar.FieldN1 = 2) AND (T_Asnad_Dar.DateTA BETWEEN @StartDateList AND @EndDateList) OR
                      (T_Asnad_Dar.FieldN1 = 6) AND (T_Asnad_Dar.DateTA BETWEEN @StartDateList AND @EndDateList)
ORDER BY T_Asnad_Dar.AsnadDarNumber DESC
   else
   			 
SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
                      T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
                      T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
                      T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
                      T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
                      T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
FROM         T_Asnad_Dar LEFT OUTER JOIN
                      T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2
WHERE     (T_Asnad_Dar.FieldN1 = 2) AND (NOT (ISNULL(T_Asnad_Dar.userCreate, - 10) IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0)))) AND (T_Asnad_Dar.DateTA BETWEEN @StartDateList AND @EndDateList) OR
                      (T_Asnad_Dar.FieldN1 = 6) AND (NOT (ISNULL(T_Asnad_Dar.userCreate, - 10) IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers AS T_yUsers_1
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0)))) AND (T_Asnad_Dar.DateTA BETWEEN @StartDateList AND @EndDateList)
ORDER BY T_Asnad_Dar.AsnadDarNumber DESC

end

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBatelShodePar]    Script Date: 09/19/2019 09:42:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqBatelShodePar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqBatelShodePar]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqBatelShodePar]    Script Date: 09/19/2019 09:42:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




        CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqBatelShodePar] @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10) AS  
Begin  
DECLARE @FieldN1_Vas int=2 ;
declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

if (@UserID<>3)
SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)
else
begin
set @FlagAmin=1
set @FlagAdamRoyatSanadAdmin=0
end

if(@FlagAdamRoyatSanadAdmin=0)
			SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
			SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
			SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
			T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
			FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
			T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
			= 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
			T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
			[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
			AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
			FROM         T_Asnad INNER JOIN
			[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
			WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
			UNION ALL
			SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, 
			T_Asnad_1.DateSR, T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) 
			= 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, 
			T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, 
			Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, 
			Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, 
			T_Asnad_1.PARID3
			FROM         T_Asnad AS T_Asnad_1 INNER JOIN
			Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
			WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
			T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
			T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
			WHERE     (SubAsnad.DateSO BETWEEN @StartDateList AND @EndDateList)
			ORDER BY SubAsnad.DateSR

else

			SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
			SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
			SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
			T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
			FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
			T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
			= 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
			T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
			[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
			AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
			FROM         T_Asnad INNER JOIN
			[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
			WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad.userCreate IN
			(SELECT     USERID
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = 3) OR
			(FlagAmin <> 0))))
			UNION ALL
			SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, 
			T_Asnad_1.DateSR, T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) 
			= 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, 
			T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, 
			Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, 
			Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, 
			T_Asnad_1.PARID3
			FROM         T_Asnad AS T_Asnad_1 INNER JOIN
			Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
			WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas) AND (NOT (ISNULL(T_Asnad_1.userCreate, - 10) IN
			(SELECT     USERID
			FROM         AC_Setting.dbo.T_yUsers AS T_yUsers_1
			WHERE     (USERID = 3) OR
			(FlagAmin <> 0))))) AS SubAsnad LEFT OUTER JOIN
			T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
			T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
			WHERE     (SubAsnad.DateSO BETWEEN @StartDateList AND @EndDateList)
			ORDER BY SubAsnad.DateSR


end

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNaghdiPar]    Script Date: 09/19/2019 09:42:44 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqNaghdiPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqNaghdiPar]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNaghdiPar]    Script Date: 09/19/2019 09:42:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




        CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqNaghdiPar] @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10) AS  
Begin  

DECLARE @FieldN1_Vas int=3 ;

declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

if (@UserID<>3)
SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)
else
begin
set @FlagAmin=1
set @FlagAdamRoyatSanadAdmin=0
end

if(@FlagAdamRoyatSanadAdmin=0)        
			SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
			SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
			SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
			T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
			FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
			T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
			= 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
			T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
			[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
			AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
			FROM         T_Asnad INNER JOIN
			[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
			WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
			UNION ALL
			SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, 
			T_Asnad_1.DateSR, T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) 
			= 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, 
			T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, 
			Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, 
			Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, 
			T_Asnad_1.PARID3
			FROM         T_Asnad AS T_Asnad_1 INNER JOIN
			Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
			WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
			T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
			T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
			WHERE     (SubAsnad.DateSO BETWEEN @StartDateList AND @EndDateList)
			ORDER BY SubAsnad.DateSR


else

			SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
			SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
			SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
			T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
			FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
			T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
			= 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
			T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
			[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
			AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
			FROM         T_Asnad INNER JOIN
			[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
			WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad.userCreate IN
			(SELECT     USERID
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = 3) OR
			(FlagAmin <> 0))))
			UNION ALL
			SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, 
			T_Asnad_1.DateSR, T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) 
			= 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, 
			T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, 
			Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, 
			Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, 
			T_Asnad_1.PARID3
			FROM         T_Asnad AS T_Asnad_1 INNER JOIN
			Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
			WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas) AND (NOT (ISNULL(T_Asnad_1.userCreate, - 10) IN
			(SELECT     USERID
			FROM         AC_Setting.dbo.T_yUsers AS T_yUsers_1
			WHERE     (USERID = 3) OR
			(FlagAmin <> 0))))) AS SubAsnad LEFT OUTER JOIN
			T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
			T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
			WHERE     (SubAsnad.DateSO BETWEEN @StartDateList AND @EndDateList)
			ORDER BY SubAsnad.DateSR


end

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPass]    Script Date: 09/19/2019 09:42:48 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqNoPass]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqNoPass]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPass]    Script Date: 09/19/2019 09:42:48 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqNoPass] @UserID INT,@StartDateList Nvarchar(10), @EndDateList Nvarchar(10) AS 


Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
  
		SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
		T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
		T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
		T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
		T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
		T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
		FROM         T_Asnad_Dar LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN
		T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2
		WHERE     (T_Asnad_Dar.FieldN1 = 4) AND (T_Asnad_Dar.DateTA BETWEEN @StartDateList AND @EndDateList)
		ORDER BY T_Asnad_Dar.AsnadDarNumber DESC

else
		SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
		T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
		T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
		T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
		T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
		T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
		FROM         T_Asnad_Dar LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN
		T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2
		WHERE     (T_Asnad_Dar.FieldN1 = 4) AND (NOT (ISNULL(T_Asnad_Dar.userCreate, - 10) IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0)))) AND (T_Asnad_Dar.DateTA BETWEEN @StartDateList AND @EndDateList)
		ORDER BY T_Asnad_Dar.AsnadDarNumber DESC


end

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPassPar]    Script Date: 09/19/2019 09:42:52 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqNoPassPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqNoPassPar]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqNoPassPar]    Script Date: 09/19/2019 09:42:52 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqNoPassPar] @UserID INT,@StartDateList Nvarchar(10), @EndDateList Nvarchar(10) AS 

Begin  

DECLARE @FieldN1_Vas int=4 ;
declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

if (@UserID<>3)
SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)
else
begin
set @FlagAmin=1
set @FlagAdamRoyatSanadAdmin=0
end

if(@FlagAdamRoyatSanadAdmin=0)
		SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
		SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
		SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
		T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
		FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
		T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
		= 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
		T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
		[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
		AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
		FROM         T_Asnad INNER JOIN
		[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
		WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
		UNION ALL
		SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, 
		T_Asnad_1.DateSR, T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) 
		= 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, 
		T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, 
		Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, 
		Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, 
		T_Asnad_1.PARID3
		FROM         T_Asnad AS T_Asnad_1 INNER JOIN
		Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
		WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
		T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
		WHERE     (SubAsnad.DateSO BETWEEN @StartDateList AND @EndDateList)
		ORDER BY SubAsnad.DateSR
else

		SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
		SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
		SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
		T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
		FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
		T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
		= 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
		T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
		[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
		AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
		FROM         T_Asnad INNER JOIN
		[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
		WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad.userCreate IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0))))
		UNION ALL
		SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, 
		T_Asnad_1.DateSR, T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) 
		= 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, 
		T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, 
		Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, 
		Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, 
		T_Asnad_1.PARID3
		FROM         T_Asnad AS T_Asnad_1 INNER JOIN
		Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
		WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas) AND (NOT (ISNULL(T_Asnad_1.userCreate, - 10) IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers AS T_yUsers_1
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0))))) AS SubAsnad LEFT OUTER JOIN
		T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
		WHERE     (SubAsnad.DateSO BETWEEN @StartDateList AND @EndDateList)
		ORDER BY SubAsnad.DateSR


end			

    



GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqVosooly]    Script Date: 09/19/2019 09:43:11 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqVosooly]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqVosooly]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqVosooly]    Script Date: 09/19/2019 09:43:11 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqVosooly] @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10) AS   

Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
		SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
		T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
		T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
		T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
		T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
		T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
		FROM         T_Asnad_Dar LEFT OUTER JOIN
		T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2 LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2
		WHERE     (T_Asnad_Dar.FieldN1 = 3) AND (T_Asnad_Dar.DateTA BETWEEN @StartDateList AND @EndDateList)
		ORDER BY T_Asnad_Dar.AsnadDarNumber DESC
else

		SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
		T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
		T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
		T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
		T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
		T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
		FROM         T_Asnad_Dar LEFT OUTER JOIN
		T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2 LEFT OUTER JOIN
		T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2
		WHERE     (T_Asnad_Dar.FieldN1 = 3) AND (NOT (ISNULL(T_Asnad_Dar.userCreate, - 10) IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0)))) AND (T_Asnad_Dar.DateTA BETWEEN @StartDateList AND @EndDateList)
		ORDER BY T_Asnad_Dar.AsnadDarNumber DESC
		
end		 



GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPass]    Script Date: 09/19/2019 09:42:59 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqPass]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqPass]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPass]    Script Date: 09/19/2019 09:42:59 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




      CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqPass] @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10) AS    

Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
			SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
			T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
			T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
			T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
			T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
			T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
			FROM         T_Asnad_Dar LEFT OUTER JOIN
			T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2 LEFT OUTER JOIN
			T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2
			WHERE     (T_Asnad_Dar.FieldN1 = 1) AND (T_Asnad_Dar.DateTA BETWEEN @StartDateList AND @EndDateList) OR
			(T_Asnad_Dar.FieldN1 = 5) AND (T_Asnad_Dar.DateTA BETWEEN @StartDateList AND @EndDateList)
			ORDER BY T_Asnad_Dar.AsnadDarNumber DESC
	else
	
			SELECT     T_Asnad_Dar.ASNADDARID, T_Asnad_Dar.AsnadDarNumber, T_Asnad_Dar.DateCreate, T_Asnad_Dar.DateModified, T_Asnad_Dar.VSabt, T_Asnad_Dar.GOID, T_Asnad_Dar.KOLID, 
			T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID, T_Asnad_Dar.TARID, T_Asnad_Dar.SAID2, T_Asnad_Dar.Mablagh, T_Asnad_Dar.ShHesab, T_Asnad_Dar.Namebank, T_Asnad_Dar.ShSeryal, 
			T_Asnad_Dar.Girandeh, T_Asnad_Dar.DARID2, T_Asnad_Dar.DARID3, T_Asnad_Dar.DateTA, T_Asnad_Dar.DateVO, T_Asnad_Dar.DateCH, T_Asnad_Dar.Sharh, T_Asnad_Dar.FieldN1, 
			T_Asnad_Dar.FieldN2, T_Asnad_Dar.FieldN3, T_Asnad_Dar.FieldN4, T_Asnad_Dar.FieldT1, T_Asnad_Dar.FieldT2, T_Asnad_Dar.FieldT3, T_Asnad_Dar.FieldT4, T_Asnad_Dar.Flag1, 
			T_Asnad_Dar.Flag2, T_Asnad_Dar.Flag3, T_Asnad_Dar.Flag4, T_Asnad_Dar.FlagStatus, T_SA2.SAID2Number, T_Asnad_Dar.FieldN1 AS Expr2, T_Asnad_Dar.AsnadDarNumber AS Expr1, 
			T_SA2_1.SAID2Number AS sanad1, dbo.HesabName(T_Asnad_Dar.KOLID, T_Asnad_Dar.HESABID, T_Asnad_Dar.THESABID) AS NameHesabT
			FROM         T_Asnad_Dar LEFT OUTER JOIN
			T_SA2 ON T_Asnad_Dar.FieldN2 = T_SA2.SAID2 LEFT OUTER JOIN
			T_SA2 AS T_SA2_1 ON T_Asnad_Dar.SAID2 = T_SA2_1.SAID2
			WHERE     (T_Asnad_Dar.FieldN1 = 1) AND (NOT (ISNULL(T_Asnad_Dar.userCreate, - 10) IN
			(SELECT     USERID
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = 3) OR
			(FlagAmin <> 0)))) AND (T_Asnad_Dar.DateTA BETWEEN @StartDateList AND @EndDateList) OR
			(T_Asnad_Dar.FieldN1 = 5) AND (NOT (ISNULL(T_Asnad_Dar.userCreate, - 10) IN
			(SELECT     USERID
			FROM         AC_Setting.dbo.T_yUsers AS T_yUsers_1
			WHERE     (USERID = 3) OR
			(FlagAmin <> 0)))) AND (T_Asnad_Dar.DateTA BETWEEN @StartDateList AND @EndDateList)
			ORDER BY T_Asnad_Dar.AsnadDarNumber DESC
	
end	


GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPassPar]    Script Date: 09/19/2019 09:43:07 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListCheqPassPar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListCheqPassPar]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListCheqPassPar]    Script Date: 09/19/2019 09:43:07 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




        CREATE PROCEDURE [dbo].[QSubPFrm_ListCheqPassPar] @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10) AS    
         
Begin  
DECLARE @FieldN1_Vas int=1 ;
declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

if (@UserID<>3)
SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
FROM         AC_Setting.dbo.T_yUsers
WHERE     (USERID = @UserID)
else
begin
set @FlagAmin=1
set @FlagAdamRoyatSanadAdmin=0
end

if(@FlagAdamRoyatSanadAdmin=0)
				SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
				SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
				SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
				T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
				FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
				T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
				= 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
				T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
				[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
				AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
				FROM         T_Asnad INNER JOIN
				[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
				WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas)
				UNION ALL
				SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, 
				T_Asnad_1.DateSR, T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) 
				= 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, 
				T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, 
				Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, 
				Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, 
				T_Asnad_1.PARID3
				FROM         T_Asnad AS T_Asnad_1 INNER JOIN
				Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
				WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas)) AS SubAsnad LEFT OUTER JOIN
				T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
				T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
				WHERE     (SubAsnad.DateSO BETWEEN @StartDateList AND @EndDateList)
				ORDER BY SubAsnad.DateSR
else
				SELECT     SubAsnad.Sharh, SubAsnad.ShSeryal, SubAsnad.FieldN1, SubAsnad.GOID, SubAsnad.KOLID, SubAsnad.HESABID, SubAsnad.THESABID, SubAsnad.DateSO, SubAsnad.DateSR, 
				SubAsnad.HESABID2, SubAsnad.THESABID2, SubAsnad.chkvaz, SubAsnad.Pardakht_az_Hesab, SubAsnad.ASNADID, SubAsnad.Mablagh, SubAsnad.Flag2, SubAsnad.SharhSub, SubAsnad.DarBed, 
				SubAsnad.variz_be_Hesab, SubAsnad.HESABIDM, SubAsnad.THESABIDM, SubAsnad.AsnadNumber, T_SA2.SAID2Number AS Sanad1, T_SA2_1.SAID2Number, T_SA2.Date2, 
				T_SA2_1.Date2 AS Expr1, SubAsnad.DateChange, SubAsnad.FieldT1, SubAsnad.PARID2, SubAsnad.PARID3
				FROM         (SELECT     T_Asnad.Sharh, T_Asnad.ShSeryal, T_Asnad.FieldN1, T_Asnad.GOID, T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID, T_Asnad.DateSO, T_Asnad.DateSR, T_Asnad.HESABID2, 
				T_Asnad.THESABID2, CASE WHEN (T_Asnad.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad.[FieldN1]) = 2 THEN 'باطل شده ' WHEN (T_Asnad.[FieldN1]) 
				= 3 THEN 'نقدی ' WHEN (T_Asnad.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad.KOLID, T_Asnad.HESABID, T_Asnad.THESABID) AS Pardakht_az_Hesab, 
				T_Asnad.ASNADID, T_Asnad.Mablagh, T_Asnad.Flag2, [TRANSACTION].Sharh AS SharhSub, [TRANSACTION].DARBed AS DarBed, [TRANSACTION].KOLID AS Expr1, 
				[TRANSACTION].HESABID AS HESABIDM, [TRANSACTION].THESABID AS THESABIDM, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
				AS variz_be_Hesab, T_Asnad.AsnadNumber, T_Asnad.SAID2, T_Asnad.FieldN2, T_Asnad.DateChange, T_Asnad.FieldT1, T_Asnad.PARID2, T_Asnad.PARID3
				FROM         T_Asnad INNER JOIN
				[TRANSACTION] ON T_Asnad.SAID2 = [TRANSACTION].SAID2
				WHERE     ([TRANSACTION].DARBed > 0) AND (T_Asnad.FieldN1 = @FieldN1_Vas) AND (NOT (T_Asnad.userCreate IN
				(SELECT     USERID
				FROM         AC_Setting.dbo.T_yUsers
				WHERE     (USERID = 3) OR
				(FlagAmin <> 0))))
				UNION ALL
				SELECT     T_Asnad_1.Sharh, T_Asnad_1.ShSeryal, T_Asnad_1.FieldN1, T_Asnad_1.GOID, T_Asnad_1.KOLID, T_Asnad_1.HESABID, T_Asnad_1.THESABID, T_Asnad_1.DateSO, 
				T_Asnad_1.DateSR, T_Asnad_1.HESABID2, T_Asnad_1.THESABID2, CASE WHEN (T_Asnad_1.[FieldN1]) = 1 THEN 'پاس شده' WHEN (T_Asnad_1.[FieldN1]) 
				= 2 THEN 'باطل شده ' WHEN (T_Asnad_1.[FieldN1]) = 3 THEN 'نقدی ' WHEN (T_Asnad_1.[FieldN1]) = 4 THEN 'پاس نشده ' END AS chkvaz, dbo.HesabName(T_Asnad_1.KOLID, 
				T_Asnad_1.HESABID, T_Asnad_1.THESABID) AS Pardakht_az_Hesab, T_Asnad_1.ASNADID, T_Asnad_1.Mablagh, T_Asnad_1.Flag2, Table2.FieldT1 AS SharhSub, 
				Table2.FieldN1 AS DarBed, Table2.FieldN2 AS KOLIDM, Table2.FieldN3 AS HESABIDM, Table2.FieldN4 AS THESABIDM, dbo.HesabName(Table2.FieldN2, Table2.FieldN3, 
				Table2.FieldN4) AS variz_be_Hesab, T_Asnad_1.AsnadNumber, T_Asnad_1.SAID2, T_Asnad_1.FieldN2, T_Asnad_1.DateChange, T_Asnad_1.FieldT1, T_Asnad_1.PARID2, 
				T_Asnad_1.PARID3
				FROM         T_Asnad AS T_Asnad_1 INNER JOIN
				Table2 ON T_Asnad_1.ASNADID = Table2.ID2Number
				WHERE     (T_Asnad_1.Flag2 = 1) AND (T_Asnad_1.FieldN1 = @FieldN1_Vas) AND (NOT (ISNULL(T_Asnad_1.userCreate, - 10) IN
				(SELECT     USERID
				FROM         AC_Setting.dbo.T_yUsers AS T_yUsers_1
				WHERE     (USERID = 3) OR
				(FlagAmin <> 0))))) AS SubAsnad LEFT OUTER JOIN
				T_SA2 ON SubAsnad.SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
				T_SA2 AS T_SA2_1 ON SubAsnad.FieldN2 = T_SA2_1.SAID2
				WHERE     (SubAsnad.DateSO BETWEEN @StartDateList AND @EndDateList)
				ORDER BY SubAsnad.DateSR



end		
    



GO





/****** Object:  StoredProcedure [dbo].[Q_ListPardakhtNaghdi]    Script Date: 09/19/2019 10:08:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_ListPardakhtNaghdi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_ListPardakhtNaghdi]
GO



/****** Object:  StoredProcedure [dbo].[Q_ListPardakhtNaghdi]    Script Date: 09/19/2019 10:08:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


        CREATE PROCEDURE [dbo].[Q_ListPardakhtNaghdi] @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10)   AS 
        
        Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
  
		SELECT     T_Par1.TextSharh, T_Par1.TextName2, T_Par1.Mablagh, T_Par1.PARID1, T_SA2.SAID2Number, T_Par1.PrimarySaid2, T_Par1.PARID1Number, T_Par1.DATE2, T_Par1.TextShSA, 
		T_Par1.TextName1
		FROM         T_Par1 INNER JOIN
		T_SA2 ON T_Par1.PrimarySaid2 = T_SA2.SAID2
		WHERE     (T_Par1.DATE2 BETWEEN @StartDateList AND @EndDateList)
		ORDER BY T_Par1.PARID1 DESC
	else
	
		SELECT     T_Par1.TextSharh, T_Par1.TextName2, T_Par1.Mablagh, T_Par1.PARID1, T_SA2.SAID2Number, T_Par1.PrimarySaid2, T_Par1.PARID1Number, T_Par1.DATE2, T_Par1.TextShSA, 
		T_Par1.TextName1
		FROM         T_Par1 INNER JOIN
		T_SA2 ON T_Par1.PrimarySaid2 = T_SA2.SAID2
		WHERE     (NOT (ISNULL(T_Par1.userCreate, - 10) IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0)))) AND (T_Par1.DATE2 BETWEEN @StartDateList AND @EndDateList)
		ORDER BY T_Par1.PARID1 DESC
		
		
		
end
GO




/****** Object:  StoredProcedure [dbo].[Q_ListDaryaftNaghdi]    Script Date: 09/19/2019 09:51:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_ListDaryaftNaghdi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_ListDaryaftNaghdi]
GO



/****** Object:  StoredProcedure [dbo].[Q_ListDaryaftNaghdi]    Script Date: 09/19/2019 09:51:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[Q_ListDaryaftNaghdi] @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10)   AS 
        
        Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
        
			SELECT     T_Dar1.TextSharh, T_Dar1.TextName1, T_Dar1.TextName2, T_Dar1.Mablagh, T_Dar1.DARID1, T_SA2.SAID2Number, T_Dar1.DATE2, T_Dar1.DAR1Numbr, T_Dar1.PrimarySaid2
			FROM         T_Dar1 INNER JOIN
			T_SA2 ON T_Dar1.PrimarySaid2 = T_SA2.SAID2
			WHERE     (T_Dar1.DATE2 BETWEEN @StartDateList AND @EndDateList)
			ORDER BY T_Dar1.DARID1 DESC

	else
			SELECT     T_Dar1.TextSharh, T_Dar1.TextName1, T_Dar1.TextName2, T_Dar1.Mablagh, T_Dar1.DARID1, T_SA2.SAID2Number, T_Dar1.DATE2, T_Dar1.DAR1Numbr, T_Dar1.PrimarySaid2
			FROM         T_Dar1 INNER JOIN
			T_SA2 ON T_Dar1.PrimarySaid2 = T_SA2.SAID2
			WHERE     (NOT (ISNULL(T_Dar1.userCreate, - 10) IN
			(SELECT     USERID
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = 3) OR
			(FlagAmin <> 0)))) AND (T_Dar1.DATE2 BETWEEN @StartDateList AND @EndDateList)
			ORDER BY T_Dar1.DARID1 DESC
		
		end			


GO




/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListSanadHesabdary]    Script Date: 09/19/2019 09:50:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_ListSanadHesabdary]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPfrm_ListSanadHesabdary]
GO


/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListSanadHesabdary]    Script Date: 09/19/2019 09:50:32 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[QSubPfrm_ListSanadHesabdary] @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10)    AS 
        
        Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
  
		SELECT     SAID2, Sharh, Date2, FieldN2, FieldN1, FieldT1, Flag1, Flag2, Flag3, SAID2Number, OperationCode
		FROM         T_SA2
		WHERE     (Date2 BETWEEN @StartDateList AND @EndDateList)
		ORDER BY SAID2 DESC

	else
	
		SELECT     SAID2, Sharh, Date2, FieldN2, FieldN1, FieldT1, Flag1, Flag2, Flag3, SAID2Number, OperationCode
		FROM         T_SA2
		WHERE     (NOT (ISNULL(userCreate, - 10) IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0)))) AND (Date2 BETWEEN @StartDateList AND @EndDateList)
		ORDER BY SAID2 DESC
	
	end
GO






/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListForoosh]    Script Date: 09/19/2019 09:43:15 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListForoosh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListForoosh]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListForoosh]    Script Date: 09/19/2019 09:43:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




          CREATE PROCEDURE [dbo].[QSubPFrm_ListForoosh] 
          
           @UserID INT,
           @StartDateList Nvarchar(10),
           @EndDateList Nvarchar(10)
           
           AS       
             
             
             Begin  

declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

/*
--declare @StartDateList1 date;
--declare @EndDateList1 date;
  
  --if LEN(@StartDateList)<=8 
		--set @StartDateList=LEFT(@StartDateList,4)+'/'+right(LEFT(@StartDateList,6),2)+'/'+right(@StartDateList,2)

  --if LEN(@EndDateList)<=8 
		--set @EndDateList=LEFT(@EndDateList,4)+'/'+right(LEFT(@EndDateList,6),2)+'/'+right(@EndDateList,2)
			
		--set @StartDateList1=dbo.PersianToGregorian(@StartDateList)	
		
		--set @EndDateList1=dbo.PersianToGregorian(@EndDateList)	*/
		


  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
		/* dbo.sgfn_DateToShamsiDate(CONVERT(date, T_Forosh.DateCreate)) AS DateCreate,CONVERT(VARCHAR(8), T_Forosh.DateCreate, 108) AS timeCreate
		dbo.sgfn_DateToShamsiDate(CONVERT(date, T_Forosh.DateModified)) AS DateModified, CONVERT(VARCHAR(8), T_Forosh.DateModified, 108) AS TimeEdit
		 */
		
  if(@FlagAdamRoyatSanadAdmin=0)
SELECT     T_Forosh.DateCreate, T_Forosh.DateModified, T_Forosh.FOROSHID, T_Forosh.HesabID, T_Forosh.GOID, T_Forosh.KOLID, T_Forosh.MOINID, T_Forosh.THESABID, T_Forosh.TextName, 
                      T_Forosh.TextName1, T_Forosh.TextNameT, T_Forosh.NumberKH, T_Forosh.DateFo, T_Forosh.Takhfif, T_Forosh.TextShSA, T_Forosh.NumSID2, T_Forosh.Sharh, T_Forosh.FieldN1, 
                      T_Forosh.FieldN2, T_Forosh.FieldN3, T_Forosh.FieldN4, T_Forosh.FieldT1, T_Forosh.FieldT2, T_Forosh.FieldT3, T_Forosh.FieldT3 AS Expr1, T_Forosh.FieldT4, T_Forosh.Flag1, T_Forosh.Flag2, 
                      T_Forosh.Flag3, T_Forosh.Flag4, T_Forosh.Flag5, T_Forosh.Flag6, T_Forosh.Flag7, T_Forosh.Flag8, T_Forosh.ForoshNumbr, T_Forosh.PrimarySaid2, T_Forosh.THESABIDAmel, 
                      T_SA2.SAID2Number, dbo.HesabName(T_Forosh.KOLID, T_Forosh.HesabID, T_Forosh.THESABID) AS namehesab, T_Forosh.dateSettlement, F_MabHazine_Foroosh_1.MabHazine, 
                      T_Forosh.Maliyat, T_Forosh.Avarez
FROM         T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
                      dbo.F_MabHazine_Foroosh() AS F_MabHazine_Foroosh_1 ON T_Forosh.FOROSHID = F_MabHazine_Foroosh_1.FactID
WHERE     (T_Forosh.DateFo BETWEEN @StartDateList AND @EndDateList)
ORDER BY T_Forosh.DateFo DESC, T_Forosh.ForoshNumbr DESC

else

SELECT     T_Forosh.DateCreate, T_Forosh.DateModified, T_Forosh.FOROSHID, T_Forosh.HesabID, T_Forosh.GOID, T_Forosh.KOLID, T_Forosh.MOINID, T_Forosh.THESABID, T_Forosh.TextName, 
                      T_Forosh.TextName1, T_Forosh.TextNameT, T_Forosh.NumberKH, T_Forosh.DateFo, T_Forosh.Takhfif, T_Forosh.TextShSA, T_Forosh.NumSID2, T_Forosh.Sharh, T_Forosh.FieldN1, 
                      T_Forosh.FieldN2, T_Forosh.FieldN3, T_Forosh.FieldN4, T_Forosh.FieldT1, T_Forosh.FieldT2, T_Forosh.FieldT3, T_Forosh.FieldT3 AS Expr1, T_Forosh.FieldT4, T_Forosh.Flag1, T_Forosh.Flag2, 
                      T_Forosh.Flag3, T_Forosh.Flag4, T_Forosh.Flag5, T_Forosh.Flag6, T_Forosh.Flag7, T_Forosh.Flag8, T_Forosh.ForoshNumbr, T_Forosh.PrimarySaid2, T_Forosh.THESABIDAmel, 
                      T_SA2.SAID2Number, dbo.HesabName(T_Forosh.KOLID, T_Forosh.HesabID, T_Forosh.THESABID) AS namehesab, T_Forosh.dateSettlement, F_MabHazine_Foroosh_1.MabHazine, 
                      T_Forosh.Maliyat, T_Forosh.Avarez
FROM         T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
                      dbo.F_MabHazine_Foroosh() AS F_MabHazine_Foroosh_1 ON T_Forosh.FOROSHID = F_MabHazine_Foroosh_1.FactID
WHERE     (NOT (ISNULL(T_Forosh.userCreate, - 10) IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0)))) AND (T_Forosh.DateFo BETWEEN @StartDateList AND @EndDateList)
ORDER BY T_Forosh.DateFo DESC, T_Forosh.ForoshNumbr DESC

end




GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKhadamat]    Script Date: 09/19/2019 09:49:58 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListKhadamat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListKhadamat]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKhadamat]    Script Date: 09/19/2019 09:49:58 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



    CREATE PROCEDURE [dbo].[QSubPFrm_ListKhadamat] @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10)   AS       
    
  
        Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
if(@FlagAdamRoyatSanadAdmin=0)
			SELECT     T_DKhad.DKHID, T_DKhad.HesabID, T_DKhad.GOID, T_DKhad.KOLID, T_DKhad.MOINID, T_DKhad.THESABID, T_DKhad.TextName, T_DKhad.TextName1, T_DKhad.TextNameT, T_DKhad.DateKh, 
			T_DKhad.TextShSA, T_DKhad.NumSID2, T_DKhad.Takhfif, T_DKhad.Sharh, T_DKhad.FieldN1, T_DKhad.FieldN2, T_DKhad.FieldN3, T_DKhad.FieldN4, T_DKhad.FieldT1, T_DKhad.FieldT2, 
			T_DKhad.FieldT3, T_DKhad.FieldT4, T_DKhad.Flag1, T_DKhad.Flag2, T_DKhad.Flag3, T_DKhad.Flag4, T_DKhad.PrimarySaid2, T_DKhad.Dateeng, T_DKhad.DKHNumbr, T_SA2.SAID2Number, 
			dbo.HesabName(T_DKhad.KOLID, T_DKhad.HesabID, T_DKhad.THESABID) AS namehesab, T_DKhad.dateSettlement, CONVERT(money, 
			Mabfact.FieldN3 - T_DKhad.Takhfif + ISNULL(T_DKhad.MabService, 0)) AS MabFact, CONVERT(float, Mabfact.TedadDKh) AS TedadDKh, T_DKhad.ShGhabz, T_DKhad.TimeSabt, T_DKhad.Maliyat, 
			T_DKhad.Avarez, T_DKhad.address2
			FROM         T_DKhad INNER JOIN
			T_SA2 ON T_DKhad.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
			(SELECT     DKHID, SUM(ABS(FieldN3)) AS FieldN3, SUM(TedadDKh) AS TedadDKh
			FROM         [TRANSACTION]
			GROUP BY DKHID) AS Mabfact ON T_DKhad.DKHID = Mabfact.DKHID
			WHERE     (T_DKhad.DateKh BETWEEN @StartDateList AND @EndDateList)
			ORDER BY T_DKhad.DKHID DESC
else
		SELECT     T_DKhad.DKHID, T_DKhad.HesabID, T_DKhad.GOID, T_DKhad.KOLID, T_DKhad.MOINID, T_DKhad.THESABID, T_DKhad.TextName, T_DKhad.TextName1, T_DKhad.TextNameT, T_DKhad.DateKh, 
		T_DKhad.TextShSA, T_DKhad.NumSID2, T_DKhad.Takhfif, T_DKhad.Sharh, T_DKhad.FieldN1, T_DKhad.FieldN2, T_DKhad.FieldN3, T_DKhad.FieldN4, T_DKhad.FieldT1, T_DKhad.FieldT2, 
		T_DKhad.FieldT3, T_DKhad.FieldT4, T_DKhad.Flag1, T_DKhad.Flag2, T_DKhad.Flag3, T_DKhad.Flag4, T_DKhad.PrimarySaid2, T_DKhad.Dateeng, T_DKhad.DKHNumbr, T_SA2.SAID2Number, 
		dbo.HesabName(T_DKhad.KOLID, T_DKhad.HesabID, T_DKhad.THESABID) AS namehesab, T_DKhad.dateSettlement, CONVERT(money, 
		Mabfact.FieldN3 - T_DKhad.Takhfif + ISNULL(T_DKhad.MabService, 0)) AS MabFact, CONVERT(float, Mabfact.TedadDKh) AS TedadDKh, T_DKhad.ShGhabz, T_DKhad.TimeSabt, T_DKhad.Maliyat, 
		T_DKhad.Avarez, T_DKhad.address2
		FROM         T_DKhad INNER JOIN
		T_SA2 ON T_DKhad.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
		(SELECT     DKHID, SUM(ABS(FieldN3)) AS FieldN3, SUM(TedadDKh) AS TedadDKh
		FROM         [TRANSACTION]
		GROUP BY DKHID) AS Mabfact ON T_DKhad.DKHID = Mabfact.DKHID
		WHERE     (T_DKhad.DateKh BETWEEN @StartDateList AND @EndDateList) AND (NOT (ISNULL(T_DKhad.userCreate, - 10) IN
		(SELECT     USERID
		FROM         AC_Setting.dbo.T_yUsers
		WHERE     (USERID = 3) OR
		(FlagAmin <> 0))))
		ORDER BY T_DKhad.DKHID DESC
			

end

GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1]    Script Date: 10/06/2019 12:08:27 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_Sandoogh_Analyz1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1]
GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1]    Script Date: 10/06/2019 12:08:27 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




  CREATE PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1] ( 
						@AID int,
						@OperType int,
						@LinkMasterID int,
						@GoKID int,
						@OnlyMandedare BIT,
						@OnlyActive Bit)           
  AS     
  BEGIN 	
  
  SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(Max);  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max);         	
  DECLARE @Sortstring  NVARCHAR(Max);  
  DECLARE @CmbSort AS INT;
  	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';      
  set @Sortstring=N' ';            	
  SET @ParmDefinition = N'@AID int=null, 								
							  @OperType int=null, 								
							  @LinkMasterID  int=null, 								
							  @GoKID int=null,
		@OnlyMandedare BIT=null,
		@OnlyActive Bit=null ';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
	  	
	SELECT    @OnlyActive= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 2)

	SELECT    @OnlyMandedare= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 3)	  	
	
	SELECT    @CmbSort= ABS(ISNULL(FieldN1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 10) AND (OPTIONID = 1)	  
		 
	  		 
  SET @SQLString =' SELECT     FunInfoKalaAll_1.ID AS NameKalaID, CONVERT(float, FunInfoKalaAll_1.KalaNumber) AS KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.Zarib, 
                      FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.KalaNumber AS [کد کالا], FunInfoKalaAll_1.NameKala1+ + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' '')  AS [نام کالا], FunInfoKalaAll_1.NameVahed1 AS [نام واحد], 
                      ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) AS [مانده انبار], FunInfoKalaAll_1.NameAnbar AS [نام انبار], FunInfoKalaAll_1.UnitPrice1 AS [قيمت فروش1], ROUND(CONVERT(money, 
                      drvTra.MaxUnitPriceKh), 2) AS [آخرين خريد], FunInfoKalaAll_1.UnitPrice2 AS [قيمت فروش2], FunInfoKalaAll_1.UnitPrice3 AS [قيمت فروش3], FunInfoKalaAll_1.UnitPrice4 AS [قيمت فروش4], 
                      FunInfoKalaAll_1.UnitPrice5 AS [قيمت فروش5], FunInfoKalaAll_1.UnitPrice6 AS [قيمت فروش6], FunInfoKalaAll_1.NameVahed2, drvTra.MaxUnitPriceKh AS UnitPriceKh, FunInfoKalaAll_1.FieldN1, 
                      FunInfoKalaAll_1.BarCode AS [ بارکد ], FunInfoKalaAll_1.NameGoKala1 AS [گروه کالا], FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.UnitPrice1, drvTra.MaxUnitPriceKh AS AkharinKharid, 
                      FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, FunInfoKalaAll_1.Active, 
                      FunInfoKalaAll_1.NameGoKala1, ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3)  AS MandeAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber) AS CodKala,FunInfoKalaAll_1.UnitPriceKharid as UnitPricekhUser  
FROM         dbo.FunInfoKalaAll(@OnlyActive) AS FunInfoKalaAll_1 INNER JOIN
                      dbo.Fun_CQ_ListKala_Sandoogh_Analyz() AS drvTra ON FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID 
WHERE     (1 = 1)

' 	



/*FunInfoKalaAll_1.AID = drvTra.AID and FunInfoKalaAll_1.ID = drvTra.KALAID */

/* FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID  */

  /* -----------------------نشان دادن کالاهاي فعال */ 
/*
if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه
	*/
               
  /* -----------------------شرط انبار */ 	

		  		
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 		
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	

 /* /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@TextBoxFilter  IS NULL) OR LEN(@TextBoxFilter)>1) 			
BEGIN 		
	set @TextBoxFilter=N'%'+@TextBoxFilter +N'%'
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'  (FunInfoKalaAll_1.NameKala1 Like @TextBoxFilter ) Or (KalaNumber like @TextBoxFilter) '; 				
	set @ParmAndstr=' AND ' 			
END 	*/

If @CmbSort = 1 
    set @Sortstring = N' ORDER BY KalaNumber DESC'
    
Else
If @CmbSort = 2 
    set @Sortstring  = N' ORDER BY NameKala1'
Else
If @CmbSort = 3 
    set @Sortstring = N' ORDER BY MandeAnbar DESC'
Else
If @CmbSort = 4 
   set @Sortstring = N' ORDER BY AkharinKharid'
Else
If @CmbSort = 5 
    set @Sortstring =N' ORDER BY UnitPrice1'




set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY FunInfoKalaAll_1.NameKala1+ + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' ''),ROUND(CONVERT(money, drvTra.MaxUnitPriceKh), 2), FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID,  FunInfoKalaAll_1.FieldN1, drvTra.MaxUnitPriceKh, 
                      FunInfoKalaAll_1.NameAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.UnitPrice2,FunInfoKalaAll_1.UnitPrice1, FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, 
                      FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.ID, FunInfoKalaAll_1.Zarib, drvTra.MaxUnitPriceKh,FunInfoKalaAll_1.NameVahed2,FunInfoKalaAll_1.UnitPriceKharid  '	
     
	if(@OnlyMandedare<>0)
		SET @SQLString =@SQLString + ' HAVING ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3)  <>0 ' 
		
		SET @SQLString=@SQLString+@Sortstring 	  
					
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@OnlyMandedare=@OnlyMandedare,@OnlyActive=@OnlyActive;  		
return END 





GO



/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListKharid]    Script Date: 10/06/2019 12:10:16 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPfrm_ListKharid]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPfrm_ListKharid]
GO


/****** Object:  StoredProcedure [dbo].[QSubPfrm_ListKharid]    Script Date: 10/06/2019 12:10:16 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





  CREATE PROCEDURE [dbo].[QSubPfrm_ListKharid]  @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10)    
  
  AS         
  
Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
  if(@FlagAdamRoyatSanadAdmin=0)
SELECT     T_Kharid.KHARIDID, T_Kharid.NumberKH, T_Kharid.KharidNumbr, T_Kharid.VSabt, T_Kharid.DateCreate, T_Kharid.DateModified, T_Kharid.PrimarySaid2, T_Kharid.Dateeng, T_Kharid.HesabID, 
                      T_Kharid.GOID, T_Kharid.KOLID, T_Kharid.MOINID, T_Kharid.THESABID, T_Kharid.THESABIDAmel, T_Kharid.TextName, T_Kharid.TextNameT, T_Kharid.DateKh, T_Kharid.TextShSA, 
                      T_Kharid.NumSID2, T_Kharid.TextName1, T_Kharid.Takhfif, T_Kharid.Sharh, T_Kharid.FieldN1, T_Kharid.FieldN2, T_Kharid.FieldN3, T_Kharid.Flag4, T_Kharid.Flag3, T_Kharid.Flag2, T_Kharid.Flag1, 
                      T_Kharid.FieldT4, T_Kharid.FieldT3, T_Kharid.FieldT2, T_Kharid.FieldT1, T_Kharid.FieldN4, T_SA2.SAID2Number, dbo.HesabName(T_Kharid.KOLID, T_Kharid.HesabID, T_Kharid.THESABID) 
                      AS nameHesab, T_Kharid.dateSettlement, T_Kharid.Maliyat, T_Kharid.Avarez, F_MabHazine_Kharid_1.MabHazine
FROM         T_Kharid INNER JOIN
                      T_SA2 ON T_Kharid.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
                      dbo.F_MabHazine_Kharid() AS F_MabHazine_Kharid_1 ON T_Kharid.KHARIDID = F_MabHazine_Kharid_1.FactID
WHERE     (T_Kharid.DateKh BETWEEN @StartDateList AND @EndDateList)
ORDER BY T_Kharid.DateKh DESC, T_Kharid.KharidNumbr DESC
else

SELECT     T_Kharid.KHARIDID, T_Kharid.NumberKH, T_Kharid.KharidNumbr, T_Kharid.VSabt, T_Kharid.DateCreate, T_Kharid.DateModified, T_Kharid.PrimarySaid2, T_Kharid.Dateeng, T_Kharid.HesabID, 
                      T_Kharid.GOID, T_Kharid.KOLID, T_Kharid.MOINID, T_Kharid.THESABID, T_Kharid.THESABIDAmel, T_Kharid.TextName, T_Kharid.TextNameT, T_Kharid.DateKh, T_Kharid.TextShSA, 
                      T_Kharid.NumSID2, T_Kharid.TextName1, T_Kharid.Takhfif, T_Kharid.Sharh, T_Kharid.FieldN1, T_Kharid.FieldN2, T_Kharid.FieldN3, T_Kharid.Flag4, T_Kharid.Flag3, T_Kharid.Flag2, T_Kharid.Flag1, 
                      T_Kharid.FieldT4, T_Kharid.FieldT3, T_Kharid.FieldT2, T_Kharid.FieldT1, T_Kharid.FieldN4, T_SA2.SAID2Number, dbo.HesabName(T_Kharid.KOLID, T_Kharid.HesabID, T_Kharid.THESABID) 
                      AS nameHesab, T_Kharid.dateSettlement, T_Kharid.Maliyat, T_Kharid.Avarez, F_MabHazine_Kharid_1.MabHazine, T_Kharid.userCreate
FROM         T_Kharid INNER JOIN
                      T_SA2 ON T_Kharid.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
                      dbo.F_MabHazine_Kharid() AS F_MabHazine_Kharid_1 ON T_Kharid.KHARIDID = F_MabHazine_Kharid_1.FactID
WHERE     (NOT (ISNULL(T_Kharid.userCreate, - 10) IN
                          (SELECT     USERID
                             FROM         AC_Setting.dbo.T_yUsers
                             WHERE     (USERID = 3) OR
                                                   (FlagAmin <> 0)))) AND (T_Kharid.DateKh BETWEEN @StartDateList AND @EndDateList)
ORDER BY T_Kharid.DateKh DESC, T_Kharid.KharidNumbr DESC

end


GO



/****** Object:  StoredProcedure [dbo].[Q_Roozane_Forosh_Analyz]    Script Date: 11/03/2019 09:13:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Roozane_Forosh_Analyz]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Roozane_Forosh_Analyz]
GO



/****** Object:  StoredProcedure [dbo].[Q_Roozane_Forosh_Analyz]    Script Date: 11/03/2019 09:13:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO



        CREATE PROCEDURE [dbo].[Q_Roozane_Forosh_Analyz](@Date1 NVARCHAR(10),@Date2 NVARCHAR(10)) AS 
SELECT     View_SubTransactionKala.KALAID, View_SubTransactionKala.AID, View_SubTransactionKala.GOKID, SUM(View_SubTransactionKala.TedadFo) AS TedadFoKol, 
                      ROUND(SUM(View_SubTransactionKala.UnitpriceFo * View_SubTransactionKala.TedadFo) / SUM(View_SubTransactionKala.TedadFo), 0) AS PhiMingin, MIN(View_SubTransactionKala.UnitpriceFo) 
                      AS UnitpriceFoMin, MAX(View_SubTransactionKala.UnitpriceFo) AS UnitpriceFoMAX, SUM(View_SubTransactionKala.UnitpriceFo * View_SubTransactionKala.TedadFo) AS SumForosh, 
                      derivedtbl_1.UnitPriceKhMax, derivedtbl_1.TarikhMax, InformationKala.KalaNumber, InformationKala.NameKala1, InformationKala.NameAnbar, InformationKala.NameVahed1, 
                      InformationKala.NameVahed2, InformationKala.NameGoKala1
FROM         View_SubTransactionKala INNER JOIN
                          (SELECT     DateMaxKharid.AID, DateMaxKharid.KALAID, DateMaxKharid.GOKID, MAX(UnitpriceMaxDate.UnitPriceKh) AS UnitPriceKhMax, MAX(UnitpriceMaxDate.Tarikh) AS TarikhMax
                             FROM         (SELECT     KALAID, AID, GOKID, MAX(Tarikh) AS Tarikh, MAX(CONVERT(nvarchar(255), KALAID) + N'.' + CONVERT(nvarchar(255), Tarikh)) AS LinkKalaAid
                                                     FROM         (SELECT     KALAID, Tarikh, GOKID, AID
                                                                             FROM         View_SubTransactionKala AS View_SubTransactionKala_2
                                                                             WHERE     (TedadKh + TedadVA > 0) AND (Tarikh <= @Date2)
                                                                             UNION ALL
                                                                             SELECT     NameKalaID, dbo.F1StartDate() AS Expr1, GOKID, AID
                                                                             FROM         T_Kala
                                                                             WHERE     (dbo.F1StartDate() <= @Date2)) AS derivedtbl_1_2
                                                     GROUP BY KALAID, GOKID, AID) AS DateMaxKharid INNER JOIN
                                                       (SELECT     KALAID, AID, GOKID, UnitPriceKh, Tarikh, CONVERT(nvarchar(255), KALAID) + N'.' + CONVERT(nvarchar(255), Tarikh) 
                                                                                AS LinkKalaAid
                                                          FROM         (SELECT     KALAID, GOKID, AID, ISNULL(UnitPriceKH, 0) AS UnitPriceKh, Tarikh
                                                                                  FROM         View_SubTransactionKala AS View_SubTransactionKala_1
                                                                                  WHERE     (TedadKh + TedadVA > 0) AND (Tarikh <= @Date2)
                                                                                  UNION ALL
                                                                                  SELECT     NameKalaID, GOKID, AID, UnitPrice, dbo.F1StartDate() AS Expr1
                                                                                  FROM         T_Kala AS T_Kala_1
                                                                                  WHERE     (dbo.F1StartDate() <= @Date2)) AS derivedtbl_1_1) AS UnitpriceMaxDate ON DateMaxKharid.LinkKalaAid = UnitpriceMaxDate.LinkKalaAid
                             GROUP BY DateMaxKharid.AID, DateMaxKharid.KALAID, DateMaxKharid.GOKID) AS derivedtbl_1 ON View_SubTransactionKala.KALAID = derivedtbl_1.KALAID AND 
                      View_SubTransactionKala.AID = derivedtbl_1.AID INNER JOIN
                      dbo.FunInfoKalaAll(0) AS InformationKala ON View_SubTransactionKala.LinkKalaAid = InformationKala.LinkKalaAid
WHERE     (View_SubTransactionKala.TedadFo > 0) AND (View_SubTransactionKala.Tarikh BETWEEN @Date1 AND @Date2)
GROUP BY View_SubTransactionKala.KALAID, View_SubTransactionKala.AID, View_SubTransactionKala.GOKID, derivedtbl_1.UnitPriceKhMax, derivedtbl_1.TarikhMax, InformationKala.KalaNumber, 
                      InformationKala.NameKala1, InformationKala.NameAnbar, InformationKala.NameVahed1, InformationKala.NameVahed2, InformationKala.NameGoKala1



GO



/****** Object:  StoredProcedure [dbo].[PFrm_SubRepSood]    Script Date: 11/03/2019 09:24:11 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_SubRepSood]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PFrm_SubRepSood]
GO



/****** Object:  StoredProcedure [dbo].[PFrm_SubRepSood]    Script Date: 11/03/2019 09:24:11 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


  CREATE PROCEDURE [dbo].[PFrm_SubRepSood]( 	
  @KalaID NVARCHAR(MAX),  	
  @GOKID NVARCHAR(MAX),  	
  @AID NVARCHAR(MAX), 
  @GOID NVARCHAR(MAX),
  @KolID NVARCHAR(MAX), 	 
  @HesabID NVARCHAR(MAX), 
  @THesabID NVARCHAR(MAX), 
  @startDate nvarchar(50),  	
  @EndDate nvarchar(50) ) AS   
  
  /*@StartDate=forms![PFrm_Rep_SoodKala]![Textdate1],
	@EndDate=forms![PFrm_Rep_SoodKala]![Textdate2],
	@KalaID=forms![PFrm_Rep_SoodKala]![Kalaid],
	@GOKID=forms![PFrm_Rep_SoodKala]![GoKala],
	@AID=forms![PFrm_Rep_SoodKala]![AID],
	@GOID=forms![PFrm_Rep_SoodKala]![GOID],
	@KolID=forms![PFrm_Rep_SoodKala]![KolID],
	@HesabID=forms![PFrm_Rep_SoodKala]![HesabID],
	@THesabID=forms![PFrm_Rep_SoodKala]![THesabID]*/
  BEGIN 	 SET NOCOUNT ON;  
  DECLARE @SQLQuery AS NVarchar(MAX)  
  DECLARE @ParamDefinition AS NVarchar(MAX)  
  DECLARE @OrderBy AS NVARCHAR(MAX)  
  DECLARE @GROUPBy AS NVARCHAR(MAX)   
  
  IF (@startDate IS NULL) 	
  SET @startDate = '00000000'  
  
  IF (@EndDate IS NULL) 	
  SET @EndDate = '99999999' 	 
  SET @SQLQuery = ' SELECT     SUM(View_SubTransactionKala.FieldN5) AS Sood, T_NameKala.ID, PFrm_Rep_SoodKala_Mojoodi.Mojoodi, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, 
                      @KalaID AS Expr1, SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) AS SumForoosh, SUM(CONVERT(float, View_SubTransactionKala.TedadKh)) AS SumKharid, 
                      SUM(View_SubTransactionKala.TedadKh * View_SubTransactionKala.UnitPriceKH) / CASE WHEN SUM(CONVERT(float, View_SubTransactionKala.TedadKh)) > 0 THEN SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadKh)) ELSE 1 END AS MianginKharid, SUM(View_SubTransactionKala.TedadFo * View_SubTransactionKala.UnitpriceFo) / CASE WHEN SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadFo)) > 0 THEN SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) ELSE 1 END AS MianginForoosh, CASE WHEN SUM(dbo.View_SubTransactionKala.fieldn5)
                       > 0 THEN SUM(dbo.View_SubTransactionKala.fieldn5) ELSE 0 END AS sod, CASE WHEN SUM(dbo.View_SubTransactionKala.fieldn5) < 0 THEN abs(SUM(dbo.View_SubTransactionKala.fieldn5)) 
                      ELSE 0 END AS zian, SUM(CONVERT(float, View_SubTransactionKala.TedadBarFo)) AS SumBarForoosh, SUM(CONVERT(float, View_SubTransactionKala.TedadBarKh)) AS SumBarKharid, 
                      @startDate AS startDate, @EndDate AS EndDate, SUM(View_SubTransactionKala.FieldN5) / CASE WHEN (SUM(CONVERT(float, View_SubTransactionKala.TedadFo))) 
                      = 0 THEN 1 ELSE (SUM(CONVERT(float, View_SubTransactionKala.TedadFo))) END AS SoodTak
FROM         View_SubTransactionKala INNER JOIN
                      T_NameKala ON View_SubTransactionKala.KALAID = T_NameKala.ID INNER JOIN
                      PFrm_Rep_SoodKala_Mojoodi ON T_NameKala.ID = PFrm_Rep_SoodKala_Mojoodi.KALAID
WHERE     (View_SubTransactionKala.Tarikh BETWEEN @startDate AND @EndDate) AND (ISNULL(T_NameKala.FlagNoeKala, 0) = 0)   ' 
					
SET @GROUPBy = ' GROUP BY T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, PFrm_Rep_SoodKala_Mojoodi.Mojoodi, T_NameKala.ID ' 
SET @OrderBy = ' HAVING      (SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) <> 0) ORDER BY T_NameKala.NameKala1 '   

IF (@KalaID IS NOT NULL) 	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.KALAID IN(' + @KalaID + '))'   

IF (@GOKID IS NOT NULL) 	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.GOKID IN(' + @GOKID + '))'   

IF (@AID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.AID IN(' + @AID + ') )'  


IF (@GOID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.GOID IN(' + @GOID + ') )'  


IF (@KolID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.KolID IN(' + @KolID + ') )'  

IF (@HesabID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.HesabID IN(' + @HesabID + ') )'  

IF (@THesabID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.THesabID IN(' + @THesabID + ') )'  


SET @SQLQuery = @SQLQuery + ' ' 	
SET @ParamDefinition = '  @KalaID NVARCHAR(MAX),  	
						  @GOKID NVARCHAR(MAX),  	
						  @AID NVARCHAR(MAX), 
						  @GOID NVARCHAR(MAX),
						  @KolID NVARCHAR(MAX), 	 
						  @HesabID NVARCHAR(MAX), 
						  @THesabID NVARCHAR(MAX), 
						  @startDate nvarchar(50),  	
						  @EndDate nvarchar(50) ' 
						
SET               @SQLQuery = @SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy   

EXECUTE sp_Executesql @SQLQuery, @ParamDefinition, @KalaID, @GOKID, @AID, @GOID , @KolID, @HesabID, @THesabID, @startDate, @EndDate   

SET NoCount OFF   
END

GO




/****** Object:  StoredProcedure [dbo].[PFrm_SubRepSoodSF1]    Script Date: 11/03/2019 09:24:16 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PFrm_SubRepSoodSF1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PFrm_SubRepSoodSF1]
GO



/****** Object:  StoredProcedure [dbo].[PFrm_SubRepSoodSF1]    Script Date: 11/03/2019 09:24:16 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[PFrm_SubRepSoodSF1](
		@KalaID NVARCHAR(MAX), 
		@GOKID NVARCHAR(MAX), 
		@AID NVARCHAR(MAX), 
		@GOID NVARCHAR(MAX),
		@KolID NVARCHAR(MAX), 	 
		@HesabID NVARCHAR(MAX), 
		@THesabID NVARCHAR(MAX), 
		@startDate nvarchar(50), 
		@EndDate nvarchar(50)
) AS 

BEGIN
	
SET NOCOUNT ON; 
DECLARE @SQLQuery AS NVarchar(MAX) 
DECLARE @ParamDefinition AS NVarchar(MAX) 
DECLARE @OrderBy AS NVARCHAR(MAX) 
DECLARE @GROUPBy AS NVARCHAR(MAX) 

IF (@startDate IS NULL)
	SET @startDate = '00000000' 
IF (@EndDate IS NULL)
	SET @EndDate = '99999999'
	
SET @SQLQuery = ' 
SELECT     SUM(View_SubTransactionKala.FieldN5) AS Sood, T_NameKala.ID, PFrm_Rep_SoodKala_Mojoodi.Mojoodi, T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, 
                      @KalaID AS Expr1, SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) AS SumForoosh, SUM(CONVERT(float, View_SubTransactionKala.TedadKh)) AS SumKharid, 
                      SUM(View_SubTransactionKala.TedadKh *  (View_SubTransactionKala.UnitPriceKH + T_NameKala.UnitPrice2)) / CASE WHEN SUM(CONVERT(float, View_SubTransactionKala.TedadKh)) > 0 THEN SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadKh)) ELSE 1 END AS MianginKharid, (SUM(View_SubTransactionKala.TedadFo * View_SubTransactionKala.UnitpriceFo) / CASE WHEN SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadFo)) > 0 THEN SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) ELSE 1 END) AS MianginForoosh, 
                      CASE WHEN SUM(dbo.View_SubTransactionKala.fieldn5) > 0 THEN SUM(dbo.View_SubTransactionKala.fieldn5) ELSE 0 END AS sod, CASE WHEN SUM(dbo.View_SubTransactionKala.fieldn5) 
                      < 0 THEN abs(SUM(dbo.View_SubTransactionKala.fieldn5)) ELSE 0 END AS zian, SUM(CONVERT(float, View_SubTransactionKala.TedadBarFo)) AS SumBarForoosh, SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadBarKh)) AS SumBarKharid, @startDate AS startDate, @EndDate AS EndDate, SUM(View_SubTransactionKala.FieldN5) / CASE WHEN (SUM(CONVERT(float, 
                      View_SubTransactionKala.TedadFo))) = 0 THEN 1 ELSE (SUM(CONVERT(float, View_SubTransactionKala.TedadFo))) END AS SoodTak
FROM         View_SubTransactionKala INNER JOIN
                      T_NameKala ON View_SubTransactionKala.KALAID = T_NameKala.ID INNER JOIN
                      PFrm_Rep_SoodKala_Mojoodi ON T_NameKala.ID = PFrm_Rep_SoodKala_Mojoodi.KALAID
WHERE     (View_SubTransactionKala.Tarikh BETWEEN @startDate AND @EndDate) AND (ISNULL(T_NameKala.FlagNoeKala, 0) = 0)

 '
SET @GROUPBy = ' GROUP BY T_NameKala.KalaNumber, T_NameKala.NameKala1, T_NameKala.BarCode, PFrm_Rep_SoodKala_Mojoodi.Mojoodi, T_NameKala.ID '
SET @OrderBy = '  HAVING      (SUM(CONVERT(float, View_SubTransactionKala.TedadFo)) <> 0) ORDER BY T_NameKala.NameKala1 ' 

IF (@KalaID IS NOT NULL)
	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.KALAID IN(' + @KalaID + '))' 

IF (@GOKID IS NOT NULL)
	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.GOKID IN(' + @GOKID + '))' 

IF (@AID IS NOT NULL)
	SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.AID IN(' + @AID + ') )'



IF (@GOID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.GOID IN(' + @GOID + ') )'  


IF (@KolID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.KolID IN(' + @KolID + ') )'  

IF (@HesabID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.HesabID IN(' + @HesabID + ') )'  

IF (@THesabID IS NOT NULL) 		SET @SQLQuery = @SQLQuery + ' And (View_SubTransactionKala.THesabID IN(' + @THesabID + ') )'  


SET @SQLQuery = @SQLQuery + ' '
	SET @ParamDefinition = '@KalaID NVARCHAR(MAX), 
							@GOKID NVARCHAR(MAX), 
							@AID NVARCHAR(MAX),
							@GOID NVARCHAR(MAX),
							@KolID NVARCHAR(MAX), 	 
							@HesabID NVARCHAR(MAX), 
							@THesabID NVARCHAR(MAX), 
							@startDate nvarchar(50), 
							@EndDate nvarchar(50) '
SET               @SQLQuery = @SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 

EXECUTE sp_Executesql @SQLQuery, @ParamDefinition, @KalaID, @GOKID, @AID, @GOID , @KolID, @HesabID, @THesabID,  @startDate,@EndDate 

SET NoCount OFF

 END

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKhadamat]    Script Date: 12/18/2019 11:21:54 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListKhadamat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListKhadamat]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKhadamat]    Script Date: 12/18/2019 11:21:54 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




    CREATE PROCEDURE [dbo].[QSubPFrm_ListKhadamat] @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10)   AS       
    
  
        Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
if(@FlagAdamRoyatSanadAdmin=0)
			SELECT  T_DKhad.DKHID, T_DKhad.HesabID, T_DKhad.GOID, T_DKhad.KOLID, T_DKhad.MOINID, T_DKhad.THESABID, T_DKhad.TextName, T_DKhad.TextName1, T_DKhad.TextNameT, T_DKhad.DateKh, 
						   T_DKhad.TextShSA, T_DKhad.NumSID2, T_DKhad.Takhfif, T_DKhad.Sharh, T_DKhad.FieldN1, T_DKhad.FieldN2, T_DKhad.FieldN3, T_DKhad.FieldN4, T_DKhad.FieldT1, T_DKhad.FieldT2, T_DKhad.FieldT3, 
						   T_DKhad.FieldT4, T_DKhad.Flag1, T_DKhad.Flag2, T_DKhad.Flag3, T_DKhad.Flag4, T_DKhad.PrimarySaid2, T_DKhad.Dateeng, T_DKhad.DKHNumbr, T_SA2.SAID2Number, dbo.HesabName(T_DKhad.KOLID, 
						   T_DKhad.HesabID, T_DKhad.THESABID) AS namehesab, T_DKhad.dateSettlement, T_DKhad.ShGhabz, T_DKhad.TimeSabt, T_DKhad.Maliyat, T_DKhad.Avarez, T_DKhad.address2
			FROM     T_DKhad INNER JOIN
						   T_SA2 ON T_DKhad.PrimarySaid2 = T_SA2.SAID2
			WHERE  (T_DKhad.DateKh BETWEEN @StartDateList AND @EndDateList)
			ORDER BY T_DKhad.DKHID DESC
else
SELECT  T_DKhad.DKHID, T_DKhad.HesabID, T_DKhad.GOID, T_DKhad.KOLID, T_DKhad.MOINID, T_DKhad.THESABID, T_DKhad.TextName, T_DKhad.TextName1, T_DKhad.TextNameT, T_DKhad.DateKh, 
               T_DKhad.TextShSA, T_DKhad.NumSID2, T_DKhad.Takhfif, T_DKhad.Sharh, T_DKhad.FieldN1, T_DKhad.FieldN2, T_DKhad.FieldN3, T_DKhad.FieldN4, T_DKhad.FieldT1, T_DKhad.FieldT2, T_DKhad.FieldT3, 
               T_DKhad.FieldT4, T_DKhad.Flag1, T_DKhad.Flag2, T_DKhad.Flag3, T_DKhad.Flag4, T_DKhad.PrimarySaid2, T_DKhad.Dateeng, T_DKhad.DKHNumbr, T_SA2.SAID2Number, dbo.HesabName(T_DKhad.KOLID, 
               T_DKhad.HesabID, T_DKhad.THESABID) AS namehesab, T_DKhad.dateSettlement, T_DKhad.ShGhabz, T_DKhad.TimeSabt, T_DKhad.Maliyat, T_DKhad.Avarez, T_DKhad.address2
FROM     T_DKhad INNER JOIN
               T_SA2 ON T_DKhad.PrimarySaid2 = T_SA2.SAID2
WHERE  (T_DKhad.DateKh BETWEEN @StartDateList AND @EndDateList) AND (NOT (ISNULL(T_DKhad.userCreate, - 10) IN
                   (SELECT  USERID
                    FROM     AC_Setting.dbo.T_yUsers
                    WHERE  (USERID = 3) OR
                                   (FlagAmin <> 0))))
ORDER BY T_DKhad.DKHID DESC
			

end


GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_CALC]    Script Date: 12/21/2019 11:04:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_CALC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_CALC]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_CALC]    Script Date: 12/21/2019 11:04:56 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_CALC]
		
		@Userid INT,	
		@TxtLinkTosub NVARCHAR(255)

AS
BEGIN
	

     DECLARE   @Tedad DECIMAL(19,4) 
     DECLARE   @MabFactor DECIMAL(19,4) 
     DECLARE   @MabHazine DECIMAL(19,4) 
     DECLARE   @MabTakhfif DECIMAL(19,4) 
     DECLARE   @MabMaliat DECIMAL(19,4) 
     DECLARE   @MabAvarez DECIMAL(19,4) 
     DECLARE   @MabFactorSF11 DECIMAL(19,4) 
     DECLARE   @MabKosorat DECIMAL(19,4) 



SELECT      @Tedad= SUM(Tedad) ,@MabFactor=  SUM(Tedad * UnitPrice)  , @MabTakhfif=SUM(isnull(FieldN7,0)) , @MabMaliat=SUM(isnull(Maliatkala,0)) , @MabAvarez=SUM(isnull(Avarez,0)) ,@MabFactorSF11= 0
FROM         (SELECT     CASE WHEN ZaribChildUser > 0 THEN Tedad1User ELSE Tedad END AS Tedad, CASE WHEN ZaribChildUser > 0 THEN UnitPriceUser ELSE UnitPrice END AS UnitPrice, FieldN7, Maliatkala, Avarez, 
                      Date1,LinkedID
FROM         TRANSACTION2 AS Temp) TRANSACTION2
WHERE     (date1 = @TxtLinkTosub)
GROUP BY LinkedID

 
SELECT     @MabHazine=SUM(ISNULL(DARBed, 0)) 
FROM         TRANSACTION2
WHERE     (TextAIDFo = @TxtLinkTosub) AND (Flag1 <> 1)
GROUP BY LinkedID


SELECT   @MabKosorat=SUM(ISNULL(UnitPrice, 0) * ISNULL(Tedad, 0)) 
FROM     TRANSACTION2 AS Temp
WHERE     (FieldT8 = @TxtLinkTosub)



SELECT     @Tedad AS Tedad, @MabFactor AS MabFactor, isnull(@MabTakhfif,0) AS MabTakhfif, isnull(@MabMaliat,0) AS MabMaliat, isnull(@MabAvarez,0) AS MabAvarez, isnull(@MabHazine,0) AS MabHazine	, 0 AS MabFactorSF11,@MabKosorat AS MabKosorat		  

END








GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKhadamat]    Script Date: 12/18/2019 11:21:54 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListKhadamat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListKhadamat]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKhadamat]    Script Date: 12/18/2019 11:21:54 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




    CREATE PROCEDURE [dbo].[QSubPFrm_ListKhadamat] @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10)   AS       
    
  
        Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
if(@FlagAdamRoyatSanadAdmin=0)
			SELECT  T_DKhad.DKHID, T_DKhad.HesabID, T_DKhad.GOID, T_DKhad.KOLID, T_DKhad.MOINID, T_DKhad.THESABID, T_DKhad.TextName, T_DKhad.TextName1, T_DKhad.TextNameT, T_DKhad.DateKh, 
						   T_DKhad.TextShSA, T_DKhad.NumSID2, T_DKhad.Takhfif, T_DKhad.Sharh, T_DKhad.FieldN1, T_DKhad.FieldN2, T_DKhad.FieldN3, T_DKhad.FieldN4, T_DKhad.FieldT1, T_DKhad.FieldT2, T_DKhad.FieldT3, 
						   T_DKhad.FieldT4, T_DKhad.Flag1, T_DKhad.Flag2, T_DKhad.Flag3, T_DKhad.Flag4, T_DKhad.PrimarySaid2, T_DKhad.Dateeng, T_DKhad.DKHNumbr, T_SA2.SAID2Number, dbo.HesabName(T_DKhad.KOLID, 
						   T_DKhad.HesabID, T_DKhad.THESABID) AS namehesab, T_DKhad.dateSettlement, T_DKhad.ShGhabz, T_DKhad.TimeSabt, T_DKhad.Maliyat, T_DKhad.Avarez, T_DKhad.address2
			FROM     T_DKhad INNER JOIN
						   T_SA2 ON T_DKhad.PrimarySaid2 = T_SA2.SAID2
			WHERE  (T_DKhad.DateKh BETWEEN @StartDateList AND @EndDateList)
			ORDER BY T_DKhad.DKHID DESC
else
SELECT  T_DKhad.DKHID, T_DKhad.HesabID, T_DKhad.GOID, T_DKhad.KOLID, T_DKhad.MOINID, T_DKhad.THESABID, T_DKhad.TextName, T_DKhad.TextName1, T_DKhad.TextNameT, T_DKhad.DateKh, 
               T_DKhad.TextShSA, T_DKhad.NumSID2, T_DKhad.Takhfif, T_DKhad.Sharh, T_DKhad.FieldN1, T_DKhad.FieldN2, T_DKhad.FieldN3, T_DKhad.FieldN4, T_DKhad.FieldT1, T_DKhad.FieldT2, T_DKhad.FieldT3, 
               T_DKhad.FieldT4, T_DKhad.Flag1, T_DKhad.Flag2, T_DKhad.Flag3, T_DKhad.Flag4, T_DKhad.PrimarySaid2, T_DKhad.Dateeng, T_DKhad.DKHNumbr, T_SA2.SAID2Number, dbo.HesabName(T_DKhad.KOLID, 
               T_DKhad.HesabID, T_DKhad.THESABID) AS namehesab, T_DKhad.dateSettlement, T_DKhad.ShGhabz, T_DKhad.TimeSabt, T_DKhad.Maliyat, T_DKhad.Avarez, T_DKhad.address2
FROM     T_DKhad INNER JOIN
               T_SA2 ON T_DKhad.PrimarySaid2 = T_SA2.SAID2
WHERE  (T_DKhad.DateKh BETWEEN @StartDateList AND @EndDateList) AND (NOT (ISNULL(T_DKhad.userCreate, - 10) IN
                   (SELECT  USERID
                    FROM     AC_Setting.dbo.T_yUsers
                    WHERE  (USERID = 3) OR
                                   (FlagAmin <> 0))))
ORDER BY T_DKhad.DKHID DESC
			

end


GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 01/04/2020 10:12:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Kharid_Rep1SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 01/04/2020 10:12:33 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


   CREATE  PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14] 		 	
   
   @Userid INT,	 	
   @KharidID INT, 	
   @GroupID NVARCHAR(MAX),  	
   @KOLID NVARCHAR(MAX),  	
   @HesabidID NVARCHAR(MAX),  	
   @THesabidID NVARCHAR(MAX),  	
   @THesabidIDSath2 NVARCHAR(MAX),  	
   @StarteDate NVARCHAR(10), 	
   @ENDDate NVARCHAR(10), 	
   @KHARIDID1 INT, 	
   @KHARIDID2 INT  AS BEGIN 	 
   
   IF (@KharidID>0) 	
   
   BEGIN 
SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, 
                      T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, 
                      FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, 
                      T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, 
                      T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) 
                      AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 
                      1 AS GRP, 'کالاهای خریداری شده' AS NameGRP
FROM         T_T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 1) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN
                      T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN
                      T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE     (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh > 0)                      
    UNION ALL
    
SELECT  [TRANSACTION].TedadDKh AS Tedad, [TRANSACTION].UnitPriceDKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, '' AS NameVahedAsli, '' AS NameVahedFarei, 0 AS Expr1, '' AS Expr2, 
               T_NameKhad.KhadamatNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 0 AS tedadFarei, [TRANSACTION].radif, '' AS Expr3, '' AS Expr4, '' AS AddAnbar, 0 AS Tedad1, 
               0 AS Tedad2, T_NameKhad.Name, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 2 AS GRP, 'کسورات-هزینه های فاکتور' AS Expr5
FROM     T_KolHesab RIGHT OUTER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID ON T_KolHesab.KOLID = T_Kharid.KOLID LEFT OUTER JOIN
               T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh = 0)


    UNION ALL

SELECT  0 AS Tedad, Mablagh AS netprice, '' AS NameTarafHesab, '' AS NameHesabMoin, '' AS NameHesabTafsili, '' AS NameHesabKol, '' AS CodeTafsili, '' AS CodeKol, '' AS CodeMoin, Mablagh, '' AS Expr6, 
               DATE2 AS Tarikh, TextSharh AS SharhRadif, TextSharh AS TozihatFactor, '' AS NameVahedAsli, '' AS NameVahedFarei, '' AS Expr1, '' AS Expr2, PARID1Number AS CodeKala, '' AS HazineFactor, '' AS Serial, 
               '' AS ShomareFactor, '' AS AddressTarafHesab, '' AS Address2TarafHesab, '' AS Expr7, '' AS Expr8, '' AS Expr9, '' AS Expr10, '' AS Expr11, '' AS Expr12, '' AS Expr13, '' AS Expr14, '' AS Expr15, '' AS Expr16, 
               '' AS Expr17, '' AS Expr18, IDOperation AS KHARIDID, 0 AS tedadFarei, '' AS Expr19, '' AS Expr3, '' AS Expr4, '' AS AddAnbar, 0 AS Tedad1, 0 AS Tedad2, '' AS Expr20, '' AS Expr21, '' AS Expr22, 3 AS GRP, 
               'پرداخت های نقدی' AS Expr5
FROM     T_Par1
WHERE     (IDOperation = @KharidID)

      END               
ELSE 	

BEGIN  			
SET NOCOUNT ON;  			
Declare @SQLQuery AS NVarchar(MAX) 			
Declare @ParamDefinition AS NVarchar(MAX)  			
DECLARE @OrderBy AS NVARCHAR(MAX) 			
DECLARE @GROUPBy AS NVARCHAR(MAX) 		     			
IF (@StarteDate IS NULL) 					
SET @StarteDate='0' 		     				
IF (@ENDDate IS NULL) 					
SET @ENDDate='99999999' 						
SET @SQLQuery = '   SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0)  							  <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili,  							  T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh,  							  T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli,  							  FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor,  							  T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex,  							  T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1,  							  T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2)  							  AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0)  							  > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0)  							  > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala,  							  [TRANSACTION].Avarez 		FROM         T_T_Heasb RIGHT OUTER JOIN 							  dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN 							  [TRANSACTION] INNER JOIN 							  T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN 								  (SELECT     FactID, SUM(MabHazine) AS HazineFactor 									 FROM         Table4 									 WHERE     (TypeFact = 1) AND (Flag1 = 0) 									 GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN 							  T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN 							  T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID where 1=1 		'   			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
Set @ParamDefinition = '@UserID INT, 									
						@GroupID NVARCHAR(MAX), 									
						@KOLID NVARCHAR(MAX),  									
						@HesabidID NVARCHAR(MAX),  									
						@THesabidID NVARCHAR(MAX),  									
						@THesabidIDSath2 NVARCHAR(MAX),  									
						@StarteDate NVARCHAR(MAX),  									
						@ENDDate NVARCHAR(MAX), 									
						@KHARIDID1 INT, 									
						@KHARIDID2 INT '  	 			 			
/* Execute the Transact-SQL String with all parameter value's 	    			   Using sp_executesql Command */ 			    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 			
Execute sp_Executesql   @SQLQuery,  									
						@ParamDefinition, 									
						@UserID , 									
						@GroupID, 									
						@KOLID ,  									
						@HesabidID ,  									
						@THesabidID ,  									
						@THesabidIDSath2 ,  									
						@StarteDate ,  									
						@ENDDate, 									
						@KHARIDID1, 									
						@KHARIDID1   			
If @@ERROR <> 0 GoTo ErrorHandler 			
Set NoCount OFF 		   			
ErrorHandler: 				
Return(@@ERROR) 	
END			 
END    

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_CALC]    Script Date: 01/04/2020 10:13:20 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_CALC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_CALC]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_CALC]    Script Date: 01/04/2020 10:13:20 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_CALC]
		
		@Userid INT,	
		@TxtLinkTosub NVARCHAR(255)

AS
BEGIN
	

     DECLARE   @Tedad DECIMAL(19,4) 
     DECLARE   @MabFactor DECIMAL(19,4) 
     DECLARE   @MabHazine DECIMAL(19,4) 
     DECLARE   @MabTakhfif DECIMAL(19,4) 
     DECLARE   @MabMaliat DECIMAL(19,4) 
     DECLARE   @MabAvarez DECIMAL(19,4) 
     DECLARE   @MabFactorSF11 DECIMAL(19,4) 
     DECLARE   @MabKosorat DECIMAL(19,4) 



SELECT      @Tedad= SUM(Tedad) ,@MabFactor=  SUM(Tedad * UnitPrice)  , @MabTakhfif=SUM(isnull(FieldN7,0)) , @MabMaliat=SUM(isnull(Maliatkala,0)) , @MabAvarez=SUM(isnull(Avarez,0)) ,@MabFactorSF11= 0
FROM         (SELECT     CASE WHEN ZaribChildUser > 0 THEN Tedad1User ELSE Tedad END AS Tedad, CASE WHEN ZaribChildUser > 0 THEN UnitPriceUser ELSE UnitPrice END AS UnitPrice, FieldN7, Maliatkala, Avarez, 
                      Date1,LinkedID
FROM         TRANSACTION2 AS Temp) TRANSACTION2
WHERE     (date1 = @TxtLinkTosub)
GROUP BY LinkedID

 
SELECT     @MabHazine=SUM(ISNULL(DARBed, 0)) 
FROM         TRANSACTION2
WHERE     (TextAIDFo = @TxtLinkTosub) AND (Flag1 <> 1)
GROUP BY LinkedID


SELECT   @MabKosorat=SUM(ISNULL(UnitPrice, 0) * ISNULL(Tedad, 0)) 
FROM     TRANSACTION2 AS Temp
WHERE     (FieldT8 = @TxtLinkTosub)



SELECT     @Tedad AS Tedad, @MabFactor AS MabFactor, isnull(@MabTakhfif,0) AS MabTakhfif, isnull(@MabMaliat,0) AS MabMaliat, isnull(@MabAvarez,0) AS MabAvarez, isnull(@MabHazine,0) AS MabHazine	, 0 AS MabFactorSF11,@MabKosorat AS MabKosorat		  

END









GO







/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 01/04/2020 13:33:23 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Kharid_Rep1SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 01/04/2020 13:33:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


   CREATE  PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14] 		 	
   
   @Userid INT,	 	
   @KharidID INT, 	
   @GroupID NVARCHAR(MAX),  	
   @KOLID NVARCHAR(MAX),  	
   @HesabidID NVARCHAR(MAX),  	
   @THesabidID NVARCHAR(MAX),  	
   @THesabidIDSath2 NVARCHAR(MAX),  	
   @StarteDate NVARCHAR(10), 	
   @ENDDate NVARCHAR(10), 	
   @KHARIDID1 INT, 	
   @KHARIDID2 INT  AS BEGIN 	 
   
   IF (@KharidID>0) 	
   
   BEGIN 
SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, 
                      T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, 
                      FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, 
                      T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, 
                      T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) 
                      AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 
                      1 AS GRP, 'کالاهای خریداری شده' AS NameGRP
FROM         T_T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 1) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN
                      T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN
                      T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE     (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh > 0)                      
    UNION ALL
    
SELECT  [TRANSACTION].TedadDKh AS Tedad, [TRANSACTION].UnitPriceDKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, '' AS NameVahedAsli, '' AS NameVahedFarei, 0 AS Expr1, '' AS Expr2, 
               T_NameKhad.KhadamatNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 0 AS tedadFarei, [TRANSACTION].radif, '' AS Expr3, '' AS Expr4, '' AS AddAnbar, 0 AS Tedad1, 
               0 AS Tedad2, T_NameKhad.Name, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 2 AS GRP, 'کسورات-هزینه های فاکتور' AS Expr5
FROM     T_KolHesab RIGHT OUTER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID ON T_KolHesab.KOLID = T_Kharid.KOLID LEFT OUTER JOIN
               T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh = 0)


    UNION ALL

SELECT  0 AS Tedad, Mablagh AS netprice, '' AS NameTarafHesab, '' AS NameHesabMoin, '' AS NameHesabTafsili, '' AS NameHesabKol, '' AS CodeTafsili, '' AS CodeKol, '' AS CodeMoin, Mablagh, '' AS Expr6, 
               DATE2 AS Tarikh, TextSharh AS SharhRadif, TextSharh AS TozihatFactor, '' AS NameVahedAsli, '' AS NameVahedFarei, '' AS Expr1, '' AS Expr2, PARID1Number AS CodeKala, '' AS HazineFactor, '' AS Serial, 
               '' AS ShomareFactor, '' AS AddressTarafHesab, '' AS Address2TarafHesab, '' AS Expr7, '' AS Expr8, '' AS Expr9, '' AS Expr10, '' AS Expr11, '' AS Expr12, '' AS Expr13, '' AS Expr14, '' AS Expr15, '' AS Expr16, 
               '' AS Expr17, '' AS Expr18, IDOperation AS KHARIDID, 0 AS tedadFarei, '' AS Expr19, '' AS Expr3, '' AS Expr4, '' AS AddAnbar, 0 AS Tedad1, 0 AS Tedad2, '' AS Expr20, '' AS Expr21, '' AS Expr22, 3 AS GRP, 
               'پرداخت های نقدی' AS Expr5
FROM     T_Par1
WHERE     (IDOperation = @KharidID)

      END               
ELSE 	

BEGIN  			
SET NOCOUNT ON;  			
Declare @SQLQuery AS NVarchar(MAX) 			
Declare @ParamDefinition AS NVarchar(MAX)  			
DECLARE @OrderBy AS NVARCHAR(MAX) 			
DECLARE @GROUPBy AS NVARCHAR(MAX) 		     			
IF (@StarteDate IS NULL) 					
SET @StarteDate='0' 		     				
IF (@ENDDate IS NULL) 					
SET @ENDDate='99999999' 						
SET @SQLQuery = '   SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0)  							  <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili,  							  T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh,  							  T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli,  							  FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor,  							  T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex,  							  T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1,  							  T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2)  							  AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0)  							  > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0)  							  > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala,  							  [TRANSACTION].Avarez 		FROM         T_T_Heasb RIGHT OUTER JOIN 							  dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN 							  [TRANSACTION] INNER JOIN 							  T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN 								  (SELECT     FactID, SUM(MabHazine) AS HazineFactor 									 FROM         Table4 									 WHERE     (TypeFact = 1) AND (Flag1 = 0) 									 GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN 							  T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN 							  T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID where 1=1 		'   			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
Set @ParamDefinition = '@UserID INT, 									
						@GroupID NVARCHAR(MAX), 									
						@KOLID NVARCHAR(MAX),  									
						@HesabidID NVARCHAR(MAX),  									
						@THesabidID NVARCHAR(MAX),  									
						@THesabidIDSath2 NVARCHAR(MAX),  									
						@StarteDate NVARCHAR(MAX),  									
						@ENDDate NVARCHAR(MAX), 									
						@KHARIDID1 INT, 									
						@KHARIDID2 INT '  	 			 			
/* Execute the Transact-SQL String with all parameter value's 	    			   Using sp_executesql Command */ 			    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 			
Execute sp_Executesql   @SQLQuery,  									
						@ParamDefinition, 									
						@UserID , 									
						@GroupID, 									
						@KOLID ,  									
						@HesabidID ,  									
						@THesabidID ,  									
						@THesabidIDSath2 ,  									
						@StarteDate ,  									
						@ENDDate, 									
						@KHARIDID1, 									
						@KHARIDID1   			
If @@ERROR <> 0 GoTo ErrorHandler 			
Set NoCount OFF 		   			
ErrorHandler: 				
Return(@@ERROR) 	
END			 
END    

GO




/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14]    Script Date: 01/04/2020 13:33:14 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14]
GO


/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14]    Script Date: 01/04/2020 13:33:14 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






CREATE PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14] 

@UserID INT,
@GOID INT,
@KOLID  INT,
@Hesabid INT,
@ThesabID  INT, 
@NameKhID  INT, 
@Date1  NVARCHAR(10),
@Date2  NVARCHAR(10)
AS


BEGIN
		
	Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
	set @SQLQuery='	SELECT  GOID, KOLID, HESABID, THESABID, DARBed, PARBes, dbo.HesabName(KOLID, HESABID, THESABID) AS NameHesab, Sharh, Date1
		FROM     [TRANSACTION]
		WHERE  (1=1) and (Date1 BETWEEN @Date1 AND @Date2) '


         Set @SQLQuery = @SQLQuery + ' And (GOID = @GOID)'
         Set @SQLQuery = @SQLQuery + ' And (KOLID = @KOLID)'
         
    If (@HESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (HESABID = @HESABID)'
    If (@THESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (THESABID = @THESABID)'         

	    Set @ParamDefinition = '@UserID INT,
								@GOID INT,
								@KOLID  INT,
								@Hesabid INT,
								@ThesabID  INT, 
								@NameKhID  INT, 
								@Date1  NVARCHAR(10),
								@Date2  NVARCHAR(10)'  
		    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GOID,
							@KOLID , 
							@Hesabid , 
							@ThesabID , 
							@NameKhID , 
							@Date1 , 
							@Date2
													                           






END




GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14]    Script Date: 01/04/2020 13:33:19 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14]
GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14]    Script Date: 01/04/2020 13:33:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO







CREATE PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14] 

@UserID INT,
@NameKhID  INT, 
@Date1  NVARCHAR(10),
@Date2  NVARCHAR(10)
AS

/* @ResultVal 0 عمليات با موفقيت انجام شده است*/ 

BEGIN


SELECT  [TRANSACTION].GOID, [TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID, [TRANSACTION].NAMEKHID, CONVERT(money, [TRANSACTION].UnitPriceDKh * [TRANSACTION].TedadDKh) 
               AS UnitPriceDKh, [TRANSACTION].TedadDKh, [TRANSACTION].KHARIDID, [TRANSACTION].Date1, T_NameKhad.KhadamatNumber, T_NameKhad.Name, [TRANSACTION].Sharh, 
               dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab
FROM     [TRANSACTION] INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID
WHERE  (NOT ([TRANSACTION].KHARIDID IS NULL)) AND ([TRANSACTION].NAMEKHID = @NAMEKHID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)


END





GO




/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14]    Script Date: 01/04/2020 13:33:10 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14]
GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14]    Script Date: 01/04/2020 13:33:10 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14] 

@UserID INT,
@GOID INT,
@KOLID  INT,
@Hesabid INT,
@ThesabID  INT, 
@NameKhID  INT, 
@Date1  NVARCHAR(10),
@Date2  NVARCHAR(10)
AS


BEGIN
		
	Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @SumHazine AS DECIMAL(19,4)
    DECLARE @SumKoosorat AS DECIMAL(19,4)
    



		set @SQLQuery='	SELECT  SUM(SumHazine) AS SumHazine, SUM(SumKoosorat) AS SumKoosorat
FROM     ( SELECT  SUM(DARBed) as SumHazine ,0 as SumKoosorat
						FROM     [TRANSACTION]
						WHERE  (Date1 BETWEEN @Date1 AND @Date2) '


         Set @SQLQuery = @SQLQuery + ' And (GOID = @GOID)'
         Set @SQLQuery = @SQLQuery + ' And (KOLID = @KOLID)'
         
    If (@HESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (HESABID = @HESABID)'
    If (@THESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (THESABID = @THESABID)'         

		 Set @SQLQuery = @SQLQuery + 	'UNION ALL  SELECT 0 as SumHazine, SUM(UnitPriceDKh * TedadDKh) as SumKoosorat
											FROM     [TRANSACTION]
											WHERE  (NAMEKHID = @NAMEKHID) AND (NOT (KHARIDID IS NULL)) AND (Date1 BETWEEN @Date1 AND @Date2)) AS DrvKoosorat '
	
	    Set @ParamDefinition = '@UserID INT,
								@GOID INT,
								@KOLID  INT,
								@Hesabid INT,
								@ThesabID  INT, 
								@NameKhID  INT, 
								@Date1  NVARCHAR(10),
								@Date2  NVARCHAR(10),
								@SumHazine DECIMAL(19,4)'  
		    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GOID,
							@KOLID , 
							@Hesabid , 
							@ThesabID , 
							@NameKhID , 
							@Date1 , 
							@Date2 ,
							@SumHazine			                           





END





GO




/****** Object:  UserDefinedFunction [dbo].[F_SoratHesabKol]    Script Date: 01/14/2020 10:25:23 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F_SoratHesabKol]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F_SoratHesabKol]
GO



/****** Object:  UserDefinedFunction [dbo].[F_SoratHesabKol]    Script Date: 01/14/2020 10:25:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




    CREATE FUNCTION [dbo].[F_SoratHesabKol] (@Date1 NVARCHAR(255)) RETURNS TABLE AS RETURN ( 
    

SELECT tbl_Tar_1.RadifSAID2,
       T_KolHesab.NameHesab  AS NameHesabmoin,
       CASE 
            WHEN (tbl_Tar_1.NameKala IS NOT NULL) THEN tbl_Tar_1.NameKala
            ELSE CONVERT(NVARCHAR(255), ISNULL(TextShSA, N' '))
       END                   AS SharhRadif,
       DERIVEDTBL.mande      AS MandeHesab,
       tbl_Tar_1.DARBed      AS Bedehkar,
       tbl_Tar_1.PARBes      AS Bestankar,
       tbl_Tar_1.Date1       AS Tarikh,
       T_SA2.SAID2Number     AS ShomareSanad,
       T_KolHesab.KOLID      AS HesabID,
       T_KolHesab.KolNumber  AS CodeHesab,
       tbl_Tar_1.srtfield,
       tbl_Tar_1.tedad,
       tbl_Tar_1.unitprice,
       tbl_Tar_1.NameVahed,
       tbl_Tar_1.NameKala,
       tbl_Tar_1.MatnFi,
       tbl_Tar_1.SAIDType,
       tbl_Tar_1.NoeAmaliyat,
       tbl_Tar_1.TedadSadere,
       tbl_Tar_1.TedadVarede,
       tbl_Tar_1.TRAID,
       tbl_Tar_1.radif,
       tbl_Tar_1.nameKala2,
       tbl_Tar_1.NameKala1,
       tbl_Tar_1.FlagPrint
FROM   (
           SELECT SUM(DARBed) - SUM(PARBes) AS mande,
                  SUM(PARBes)    AS SumOfPARBes,
                  SUM(DARBed)    AS SumOfDARBed,
                  KOLID          AS HESABID
           FROM   [TRANSACTION]  AS Tran1
           WHERE  (NOT (SAID2 IS NULL))
                  AND (Date1 < @Date1)
           GROUP BY
                  KOLID
       )                     AS DERIVEDTBL
       RIGHT OUTER JOIN T_SA2
       INNER JOIN (
                SELECT Tbl_2.RadifSAID2,
                       Tbl_2.Sharh,
                       Tbl_2.Date1,
                       ISNULL(Tbl_2.PARBes, 0) + CONVERT(DECIMAL(19, 4), Tbl_2.TedadBarFo + Tbl_2.TedadKh) * (Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo) AS 
                       PARBes,
                       ISNULL(Tbl_2.DARBed, 0) + CONVERT(
                           DECIMAL(19, 4),
                           Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh
                       ) * (
                           Tbl_2.UnitPriceFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh
                       )  AS DARBed,
                       CONVERT(
                           INT,
                           CASE 
                                WHEN (NOT (Tbl_2.SAID2 IS NULL)) THEN Tbl_2.SAID2
                                ELSE [Tbl_2].fieldn2
                           END
                       )  AS SAID2,
                       CASE 
                            WHEN Tbl_2.FOROSHID > 0
                AND Tbl_2.KalaIDChild > 0 THEN Tedad1User + Tbl_2.TedadKh + Tbl_2.TedadBarFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh 
                    ELSE Tbl_2.TedadFo + Tbl_2.TedadKh + Tbl_2.TedadBarFo + Tbl_2.TedadBarKh
                    + Tbl_2.TedadDKh END AS tedad,
                CASE 
                     WHEN Tbl_2.FOROSHID > 0
                AND Tbl_2.KalaIDChild > 0 THEN UnitPriceUser + Tbl_2.UnitPriceFo + Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo 
                    + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh ELSE Tbl_2.UnitPriceFo + Tbl_2.UnitPriceKh +
                    Tbl_2.UnitPriceBarFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh END AS unitprice,
                T_NameVahed.NameVahed,
                CASE 
                     WHEN (NOT KALAID IS NULL) THEN (
                              (
                                  CASE 
                                       WHEN Tbl_2.FOROSHID > 0
                                  AND Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + 
                                      ')' ELSE T_NameKala.NameKala1 END
                              ) + N' ' + CONVERT(NVARCHAR(255), ISNULL(Tbl_2.Sharh, N''))
                          )
                     ELSE (
                              NAME + N' ' + CONVERT(NVARCHAR(255), ISNULL(Tbl_2.Sharh, N''))
                          )
                END + N' ' + CASE 
                                  WHEN (tedadfo > 0) THEN (N'فاکتور فروش' + ISNULL(Tbl_2.OperationCode, N''))
                                  WHEN (tedaddkh > 0) AND (NOT (Tbl_2.KHARIDID IS NULL))   THEN (N'کسورات فاکتور خريد ' + ISNULL(Tbl_2.OperationCode, N''))
                                  WHEN (tedadkh > 0) THEN (N'فاکتور خريد' + ISNULL(Tbl_2.OperationCode, N''))
                                  WHEN (tedadbarfo > 0) THEN (N'فاکتور برگشت از فروش' + ISNULL(Tbl_2.OperationCode, N''))
                                  WHEN (tedadbarkh > 0) THEN (
                                           N'فاکتور برگشت از خريد ' + ISNULL(Tbl_2.OperationCode, N'')
                                       )
                                  WHEN (tedaddkh > 0) THEN (N'فاکتور خدمات ' + ISNULL(Tbl_2.OperationCode, N''))
                                  ELSE NULL
                             END COLLATE Arabic_CI_AS AS NameKala,
                CASE 
                     WHEN (Tbl_2.SAID2 IS NULL) THEN N'في : '
                     ELSE NULL
                END AS MatnFi,
                CASE 
                     WHEN (NOT KALAID IS NULL) THEN (
                              CASE 
                                   WHEN Tbl_2.FOROSHID > 0
                              AND Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + ')' 
                                  ELSE T_NameKala.NameKala1 END
                          )
                     ELSE (NAME + N' ')
                END COLLATE Arabic_CI_AS AS nameKala2,
                Tbl_2.KOLID AS Hesabid,
                Tbl_2.TextShSA,
                CASE 
                     WHEN (Tbl_2.SAID2 IS NULL) THEN 2
                     ELSE 1
                END AS srtfield,
                Tbl_2.SAIDType,
                Tbl_2.TRAID,
                (
                    CASE 
                         WHEN (tedadfo > 0) THEN (N'فاکتور فروش' + ISNULL(Tbl_2.OperationCode, N''))
                         WHEN (tedadkh > 0) THEN (N'فاکتور خريد' + ISNULL(Tbl_2.OperationCode, N''))
                         WHEN (tedadbarfo > 0) THEN (N'فاکتور برگشت از فروش' + ISNULL(Tbl_2.OperationCode, N''))
                         WHEN (tedadbarkh > 0) THEN (N'فاکتور برگشت از خريد' + ISNULL(Tbl_2.OperationCode, N''))
                         WHEN (tedaddkh > 0) THEN (N'فاکتور خدمات' + ISNULL(Tbl_2.OperationCode, N''))
                         WHEN (Tbl_2.DARID1 IS NOT NULL) THEN (N'دريافت نقدي ' + ISNULL(T_SA2_1.OperationCode, 0))
                         WHEN (Tbl_2.PARID1 IS NOT NULL) THEN (N'پرداخت نقدي ' + ISNULL(T_SA2_1.OperationCode, 0))
                         WHEN ((Tbl_2.PARID3 IS NOT NULL) OR (Tbl_2.PARID2 IS NOT NULL)) THEN (N'پرداخت چک ' + ISNULL(T_SA2_1.OperationCode, 0))
                         WHEN ((Tbl_2.DARID2 IS NOT NULL) OR (Tbl_2.DARID3 IS NOT NULL)) THEN (N'دريافت چک ' + ISNULL(T_SA2_1.OperationCode, 0))
                         ELSE N'سند حسابداري' + STR(T_SA2_1.SAID2Number)
                    END
                ) AS NoeAmaliyat,
                CASE 
                     WHEN Tbl_2.FOROSHID > 0
                AND Tbl_2.KalaIDChild > 0 THEN Tedad1User + Tbl_2.TedadBarKh + Tbl_2.TedadDKh ELSE Tbl_2.TedadFo + Tbl_2.TedadBarKh 
                    + Tbl_2.TedadDKh END AS TedadSadere,
                Tbl_2.TedadBarFo + Tbl_2.TedadKh AS TedadVarede,
                (Tbl_2.TedadBarFo + Tbl_2.TedadKh) * (Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo) AS beskala,
                (Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh) * (
                    Tbl_2.UnitPriceFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh
                ) AS bedkala,
                T_NameKhad.Name,
                CASE 
                     WHEN Tbl_2.FOROSHID > 0
                AND Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + ')' ELSE T_NameKala.NameKala1 
                    END COLLATE Arabic_CI_AS AS NameKala1,
                Tbl_2.OperationCode,
                Tbl_2.radif,
                T_SA2_1.OperationCode AS OperationCode1,
                Tbl_2.FlagPrint
                FROM T_RelateKala RIGHT OUTER JOIN
                [TRANSACTION] AS Tbl_2 ON T_RelateKala.Kalaid2 = Tbl_2.KalaIDChild LEFT OUTER JOIN
                T_SA2 AS T_SA2_1 ON Tbl_2.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN
                T_NameKhad ON Tbl_2.NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                T_NameKala INNER JOIN
                T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 ON Tbl_2.KALAID = T_NameKala.ID
                WHERE (Tbl_2.SAIDType <> 3)
                OR (Tbl_2.SAIDType IS NULL)
            )                AS tbl_Tar_1
            ON  T_SA2.SAID2 = tbl_Tar_1.SAID2
       INNER JOIN T_KolHesab
            ON  tbl_Tar_1.Hesabid = T_KolHesab.KOLID
            ON  DERIVEDTBL.HESABID = T_KolHesab.KOLID
                             
                             UNION ALL
                             
SELECT     1001 AS Expr3, dbo.HesabName(NULL, NULL, tbl_Tar.KOLID) AS NameHesabmoin, N'بابت تخفيفات ' + CONVERT(nvarchar(255), ISNULL(tbl_Tar.TextShSA, N'')) AS SharhRadif, NULL AS MandeHesab, 
                      tbl_Tar.PARBes AS Bedehkar, tbl_Tar.DARBed AS Bestankar, tbl_Tar.Date1 AS Tarikh, tbl_Tar.SAID2Number AS ShomareSanad, WhereHesab.KOLID, NULL AS CodeHesab, 3 AS srtfield, 0 AS tedad, 
                      0 AS unitprice, NULL AS NameVahed, NULL AS NameKala, NULL AS MatnFi, NULL AS SAIDType, NULL AS NoeAmaliyat, 0 AS TedadSadere, 0 AS TedadVarede, tbl_Tar.TRAID, tbl_Tar.radif, NULL 
                      AS Expr1, NULL AS Expr2, tbl_Tar.FlagPrint
FROM         [TRANSACTION] AS tbl_Tar INNER JOIN
                          (SELECT     SAID2, KOLID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (SAID2Number IS NULL)) AND (NOT (RadifSAID2 IS NULL))
                             GROUP BY SAID2, KOLID) AS WhereHesab ON tbl_Tar.SAID2 = WhereHesab.SAID2
WHERE     (tbl_Tar.SAIDType = 6) AND (tbl_Tar.KOLID IN (24, 25, 26))



    UNION ALL

SELECT     999 AS Expr2, dbo.HesabName(T_Forosh.KOLID, NULL, NULL) AS NameHesabmoin, N'عوارض فروش' AS SharhRadif, NULL AS MandeHesab, T_Forosh.Avarez AS Bedehkar, 0 AS Bestankar, 
                      T_Forosh.DateFo AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_Forosh.KOLID, T_KolHesab.KolNumber, 2 AS Expr10, 0 AS Expr3, 0 AS Expr4, NULL AS Expr5, NULL AS Expr6, NULL 
                      AS Expr11, NULL AS Expr12, NULL AS Expr13, 0 AS Expr7, 0 AS Expr8, NULL AS Expr14, NULL AS Expr15, NULL AS Expr9, NULL AS Expr1, 0 AS Expr16
FROM         T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON T_Forosh.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, KOLID AS HESABID
FROM         [TRANSACTION] AS Tran1
WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
GROUP BY KOLID) AS DERIVEDTBL ON T_KolHesab.KOLID = DERIVEDTBL.HESABID
WHERE     (ISNULL(T_Forosh.Avarez, 0) > 0)
                             

UNION ALL



SELECT     999 AS Expr2, dbo.HesabName(T_Forosh.KOLID, NULL, NULL) AS NameHesabmoin, N'مالیات فروش' AS SharhRadif, NULL AS MandeHesab, T_Forosh.Maliyat AS Bedehkar, 0 AS Bestankar, 
                      T_Forosh.DateFo AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_Forosh.KOLID, T_KolHesab.KolNumber, 2 AS Expr10, 0 AS Expr3, 0 AS Expr4, NULL AS Expr5, NULL AS Expr6, NULL 
                      AS Expr11, NULL AS Expr12, NULL AS Expr13, 0 AS Expr7, 0 AS Expr8, NULL AS Expr14, NULL AS Expr15, NULL AS Expr9, NULL AS Expr1, 0 AS Expr16
FROM         T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON T_Forosh.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, KOLID AS HESABID
FROM         [TRANSACTION] AS Tran1
WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
GROUP BY KOLID) AS DERIVEDTBL ON T_KolHesab.KOLID = DERIVEDTBL.HESABID
WHERE     (ISNULL(T_Forosh.Maliyat, 0) > 0)

    	)    	




GO




/****** Object:  UserDefinedFunction [dbo].[F_SoratHesabMoein]    Script Date: 01/14/2020 10:25:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F_SoratHesabMoein]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F_SoratHesabMoein]
GO



/****** Object:  UserDefinedFunction [dbo].[F_SoratHesabMoein]    Script Date: 01/14/2020 10:25:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




    CREATE FUNCTION [dbo].[F_SoratHesabMoein] (@Date1 NVARCHAR(255)) RETURNS TABLE AS RETURN ( 
    	
 	
    	
SELECT tbl_Tar_1.RadifSAID2
      ,T_Heasb.NameHesab    AS NameHesabmoin
      ,CASE 
            WHEN (tbl_Tar_1.NameKala IS NOT NULL) THEN tbl_Tar_1.NameKala
            ELSE CONVERT(NVARCHAR(255) ,ISNULL(TextShSA ,N' '))
       END                  AS SharhRadif
      ,DERIVEDTBL.mande     AS MandeHesab
      ,tbl_Tar_1.DARBed     AS Bedehkar
      ,tbl_Tar_1.PARBes     AS Bestankar
      ,tbl_Tar_1.Date1      AS Tarikh
      ,T_SA2.SAID2Number    AS ShomareSanad
      ,T_Heasb.HESABID      AS HesabID
      ,T_Heasb.HESABNumber  AS CodeHesab
      ,tbl_Tar_1.srtfield
      ,tbl_Tar_1.tedad
      ,tbl_Tar_1.unitprice
      ,tbl_Tar_1.NameVahed
      ,tbl_Tar_1.NameKala
      ,tbl_Tar_1.MatnFi
      ,tbl_Tar_1.SAIDType
      ,tbl_Tar_1.NoeAmaliyat
      ,tbl_Tar_1.TedadSadere
      ,tbl_Tar_1.TedadVarede
      ,tbl_Tar_1.TRAID
      ,tbl_Tar_1.radif
      ,tbl_Tar_1.nameKala2
      ,tbl_Tar_1.NameKala1
      ,tbl_Tar_1.FlagPrint
FROM   T_Heasb
       INNER JOIN T_SA2
       INNER JOIN (
                SELECT Tbl_2.RadifSAID2
                      ,Tbl_2.Sharh
                      ,Tbl_2.Date1
                      ,ISNULL(Tbl_2.PARBes ,0)+CONVERT(DECIMAL(19 ,4) ,Tbl_2.TedadBarFo+Tbl_2.TedadKh)*(Tbl_2.UnitPriceKh+Tbl_2.UnitPriceBarFo) AS 
                       PARBes
                      ,ISNULL(Tbl_2.DARBed ,0)+CONVERT(
                           DECIMAL(19 ,4)
                          ,Tbl_2.TedadFo+Tbl_2.TedadBarKh+Tbl_2.TedadDKh
                       )*(Tbl_2.UnitPriceFo+Tbl_2.UnitPriceBarKh+Tbl_2.UnitPriceDKh) AS DARBed
                      ,CONVERT(
                           INT
                          ,CASE 
                                WHEN (NOT (Tbl_2.SAID2 IS NULL)) THEN Tbl_2.SAID2
                                ELSE [Tbl_2].fieldn2
                           END
                       ) AS SAID2
                      ,CASE 
                            WHEN Tbl_2.FOROSHID>0
                AND Tbl_2.KalaIDChild>0 THEN Tedad1User+Tbl_2.TedadKh+Tbl_2.TedadBarFo+Tbl_2.TedadBarKh+Tbl_2.TedadDKh 
                    ELSE Tbl_2.TedadFo+Tbl_2.TedadKh+Tbl_2.TedadBarFo+Tbl_2.TedadBarKh
                   +Tbl_2.TedadDKh END AS tedad
               ,CASE 
                     WHEN Tbl_2.FOROSHID>0
                AND Tbl_2.KalaIDChild>0 THEN UnitPriceUser+Tbl_2.UnitPriceKh+Tbl_2.UnitPriceBarFo+Tbl_2.UnitPriceBarKh+
                    Tbl_2.UnitPriceDKh ELSE Tbl_2.UnitPriceFo+
                    Tbl_2.UnitPriceKh+Tbl_2.UnitPriceBarFo+Tbl_2.UnitPriceBarKh+Tbl_2.UnitPriceDKh END AS unitprice
               ,T_NameVahed.NameVahed
               ,CASE 
                     WHEN (NOT KALAID IS NULL) THEN (
                              (
                                  CASE 
                                       WHEN Tbl_2.FOROSHID>0
                                  AND Tbl_2.KalaIDChild>0 THEN T_NameKala.NameKala1+'('+T_RelateKala.NameKala2+')' ELSE 
                                      T_NameKala.NameKala1 END
                              ) 
                             +N' '+CONVERT(NVARCHAR(255) ,ISNULL(Tbl_2.Sharh ,N''))
                          )
                     ELSE (NAME+N' '+CONVERT(NVARCHAR(255) ,ISNULL(Tbl_2.Sharh ,N'')))
                END+N' '+CASE 
                              WHEN (tedadfo>0) THEN (N'فاکتور فروش'+ISNULL(Tbl_2.OperationCode ,N''))
                              WHEN (tedaddkh > 0) AND (NOT (Tbl_2.KHARIDID IS NULL))   THEN (N'کسورات فاکتور خريد ' + ISNULL(Tbl_2.OperationCode, N''))
                              WHEN (tedadkh>0) THEN (N'فاکتور خريد'+ISNULL(Tbl_2.OperationCode ,N''))
                              WHEN (tedadbarfo>0) THEN (N'فاکتور برگشت از فروش'+ISNULL(Tbl_2.OperationCode ,N''))
                              WHEN (tedadbarkh>0) THEN (N'فاکتور برگشت از خريد '+ISNULL(Tbl_2.OperationCode ,N''))
                              WHEN (tedaddkh>0) THEN (N'فاکتور خدمات '+ISNULL(Tbl_2.OperationCode ,N''))
                              ELSE NULL
                         END COLLATE Arabic_CI_AS AS NameKala
               ,CASE 
                     WHEN (Tbl_2.SAID2 IS NULL) THEN N'في : '
                     ELSE NULL
                END AS MatnFi
               ,CASE 
                     WHEN (NOT KALAID IS NULL) THEN (
                              CASE 
                                   WHEN Tbl_2.FOROSHID>0
                              AND Tbl_2.KalaIDChild>0 THEN T_NameKala.NameKala1+'('+T_RelateKala.NameKala2+')' ELSE 
                                  T_NameKala.NameKala1 END
                          )
                     ELSE (NAME+N' ')
                END COLLATE Arabic_CI_AS AS nameKala2
               ,Tbl_2.HESABID AS Hesabid
               ,Tbl_2.TextShSA
               ,CASE 
                     WHEN (Tbl_2.SAID2 IS NULL) THEN 2
                     ELSE 1
                END AS srtfield
               ,Tbl_2.SAIDType
               ,Tbl_2.TRAID
               ,(
                    CASE 
                         WHEN (tedadfo>0) THEN (N'فاکتور فروش'+ISNULL(Tbl_2.OperationCode ,N''))
                         WHEN (tedadkh>0) THEN (N'فاکتور خريد'+ISNULL(Tbl_2.OperationCode ,N''))
                         WHEN (tedadbarfo>0) THEN (N'فاکتور برگشت از فروش'+ISNULL(Tbl_2.OperationCode ,N''))
                         WHEN (tedadbarkh>0) THEN (N'فاکتور برگشت از خريد'+ISNULL(Tbl_2.OperationCode ,N''))
                         WHEN (tedaddkh>0) THEN (N'فاکتور خدمات'+ISNULL(Tbl_2.OperationCode ,N''))
                         WHEN (Tbl_2.DARID1 IS NOT NULL) THEN (N'دريافت نقدي '+ISNULL(T_SA2_1.OperationCode ,0))
                         WHEN (Tbl_2.PARID1 IS NOT NULL) THEN (N'پرداخت نقدي '+ISNULL(T_SA2_1.OperationCode ,0))
                         WHEN ((Tbl_2.PARID3 IS NOT NULL) OR (Tbl_2.PARID2 IS NOT NULL)) THEN (N'پرداخت چک '+ISNULL(T_SA2_1.OperationCode ,0))
                         WHEN ((Tbl_2.DARID2 IS NOT NULL) OR (Tbl_2.DARID3 IS NOT NULL)) THEN (N'دريافت چک '+ISNULL(T_SA2_1.OperationCode ,0))
                         ELSE N'سند حسابداري'+STR(T_SA2_1.SAID2Number)
                    END
                ) AS NoeAmaliyat
               ,CASE 
                     WHEN Tbl_2.FOROSHID>0
                AND Tbl_2.KalaIDChild>0 THEN Tedad1User+Tbl_2.TedadBarKh+Tbl_2.TedadDKh ELSE Tbl_2.TedadFo+Tbl_2.TedadBarKh 
                   +Tbl_2.TedadDKh END AS TedadSadere
               ,Tbl_2.TedadBarFo+Tbl_2.TedadKh AS TedadVarede
               ,(Tbl_2.TedadBarFo+Tbl_2.TedadKh)*(Tbl_2.UnitPriceKh+Tbl_2.UnitPriceBarFo) AS beskala
               ,(Tbl_2.TedadFo+Tbl_2.TedadBarKh+Tbl_2.TedadDKh)*(Tbl_2.UnitPriceFo+Tbl_2.UnitPriceBarKh+Tbl_2.UnitPriceDKh) 
                AS bedkala
               ,T_NameKhad.Name
               ,CASE 
                     WHEN Tbl_2.FOROSHID>0
                AND Tbl_2.KalaIDChild>0 THEN T_NameKala.NameKala1+'('+T_RelateKala.NameKala2+')' ELSE T_NameKala.NameKala1 
                    END COLLATE Arabic_CI_AS AS NameKala1
               ,Tbl_2.OperationCode
               ,Tbl_2.radif
               ,T_SA2_1.OperationCode AS OperationCode1
               ,Tbl_2.FlagPrint
                FROM T_RelateKala RIGHT OUTER JOIN
                [TRANSACTION] AS Tbl_2 ON T_RelateKala.Kalaid2=Tbl_2.KalaIDChild LEFT OUTER JOIN
                T_SA2 AS T_SA2_1 ON Tbl_2.SAID2=T_SA2_1.SAID2 LEFT OUTER JOIN
                T_NameKhad ON Tbl_2.NAMEKHID=T_NameKhad.NAMEKHID LEFT OUTER JOIN
                T_NameKala INNER JOIN
                T_NameVahed ON T_NameKala.VAHED1=T_NameVahed.VAHED1 ON Tbl_2.KALAID=T_NameKala.ID
                WHERE (Tbl_2.SAIDType<>3)
                OR (Tbl_2.SAIDType IS NULL)
            )               AS tbl_Tar_1
            ON  T_SA2.SAID2 = tbl_Tar_1.SAID2
            ON  T_Heasb.HESABID = tbl_Tar_1.Hesabid
       LEFT OUTER JOIN (
                SELECT SUM(DARBed)- SUM(PARBes) AS mande
                      ,SUM(PARBes)    AS SumOfPARBes
                      ,SUM(DARBed)    AS SumOfDARBed
                      ,HESABID
                FROM   [TRANSACTION]  AS Tran1
                WHERE  (NOT (SAID2 IS NULL))
                       AND (Date1<@Date1)
                GROUP BY
                       HESABID
            )               AS DERIVEDTBL
            ON  T_Heasb.HESABID = DERIVEDTBL.HESABID
                             
                             UNION ALL
                             
SELECT     1001 AS Expr3, dbo.HesabName(NULL, NULL, tbl_Tar.HESABID) AS NameHesabmoin, N' تخفيف ' + CONVERT(nvarchar(255), ISNULL(tbl_Tar.TextShSA, N'')) AS SharhRadif, NULL AS MandeHesab, 
                      tbl_Tar.PARBes AS Bedehkar, tbl_Tar.DARBed AS Bestankar, tbl_Tar.Date1 AS Tarikh, tbl_Tar.SAID2Number AS ShomareSanad, WhereHesab.HESABID, NULL AS CodeHesab, 3 AS srtfield, 
                      0 AS tedad, 0 AS unitprice, NULL AS NameVahed, NULL AS NameKala, NULL AS MatnFi, NULL AS SAIDType, NULL AS NoeAmaliyat, 0 AS TedadSadere, 0 AS TedadVarede, tbl_Tar.TRAID, 
                      tbl_Tar.radif, NULL AS Expr1, NULL AS Expr2, tbl_Tar.FlagPrint
FROM         [TRANSACTION] AS tbl_Tar INNER JOIN
                          (SELECT     SAID2, HESABID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (SAID2Number IS NULL)) AND (NOT (RadifSAID2 IS NULL))
                             GROUP BY SAID2, HESABID) AS WhereHesab ON tbl_Tar.SAID2 = WhereHesab.SAID2
WHERE     (tbl_Tar.KOLID IN (24, 25, 26)) AND (tbl_Tar.SAIDType = 6)
    	

    UNION ALL

SELECT     999 AS Expr2, dbo.HesabName(NULL, T_Forosh.HesabID, NULL) AS NameHesabmoin, N'عوارض فروش' AS SharhRadif, NULL AS MandeHesab, T_Forosh.Avarez AS Bedehkar, 0 AS Bestankar, 
                      T_Forosh.DateFo AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_Forosh.HesabID, T_Heasb.HESABNumber, 2 AS Expr10, 0 AS Expr3, 0 AS Expr4, NULL AS Expr5, NULL AS Expr6, NULL 
                      AS Expr11, NULL AS Expr12, NULL AS Expr13, 0 AS Expr7, 0 AS Expr8, NULL AS Expr14, NULL AS Expr15, NULL AS Expr9, NULL AS Expr1, 0 AS Expr16
FROM         (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, HESABID
                        FROM         [TRANSACTION] AS Tran1
                        WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                        GROUP BY HESABID) AS DERIVEDTBL RIGHT OUTER JOIN
                      T_Heasb ON DERIVEDTBL.HESABID = T_Heasb.HESABID RIGHT OUTER JOIN
                      T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 ON T_Heasb.HESABID = T_Forosh.HesabID
WHERE     (ISNULL(T_Forosh.Avarez, 0) > 0)
                             

UNION ALL


SELECT     999 AS Expr2, dbo.HesabName(NULL, T_Forosh.HesabID, NULL) AS NameHesabmoin, N'مالیات فروش' AS SharhRadif, NULL AS MandeHesab, T_Forosh.Maliyat AS Bedehkar, 0 AS Bestankar, 
                      T_Forosh.DateFo AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_Forosh.HesabID, T_Heasb.HESABNumber, 2 AS Expr10, 0 AS Expr3, 0 AS Expr4, NULL AS Expr5, NULL AS Expr6, NULL 
                      AS Expr11, NULL AS Expr12, NULL AS Expr13, 0 AS Expr7, 0 AS Expr8, NULL AS Expr14, NULL AS Expr15, NULL AS Expr9, NULL AS Expr1, 0 AS Expr16
FROM         (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, HESABID
                        FROM         [TRANSACTION] AS Tran1
                        WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                        GROUP BY HESABID) AS DERIVEDTBL RIGHT OUTER JOIN
                      T_Heasb ON DERIVEDTBL.HESABID = T_Heasb.HESABID RIGHT OUTER JOIN
                      T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 ON T_Heasb.HESABID = T_Forosh.HesabID
WHERE     (ISNULL(T_Forosh.Maliyat, 0) > 0)



    )    



GO



/****** Object:  UserDefinedFunction [dbo].[F_SoratHesabTafsili]    Script Date: 01/14/2020 10:25:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[F_SoratHesabTafsili]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[F_SoratHesabTafsili]
GO


/****** Object:  UserDefinedFunction [dbo].[F_SoratHesabTafsili]    Script Date: 01/14/2020 10:25:33 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






    CREATE FUNCTION [dbo].[F_SoratHesabTafsili] (@Date1 NVARCHAR(255)) RETURNS TABLE AS RETURN ( 
    	
    	
SELECT tbl_Tar_1.RadifSAID2,
       T_T_Heasb.NameHesabT    AS NameHesabmoin,
       CASE 
            WHEN (tbl_Tar_1.NameKala IS NOT NULL) THEN tbl_Tar_1.NameKala
            ELSE CONVERT(NVARCHAR(255), ISNULL(tbl_Tar_1.TextShSA, N' '))
       END                     AS SharhRadif,
       DERIVEDTBL.mande        AS MandeHesab,
       tbl_Tar_1.DARBed        AS Bedehkar,
       tbl_Tar_1.PARBes        AS Bestankar,
       tbl_Tar_1.Date1         AS Tarikh,
       T_SA2.SAID2Number       AS ShomareSanad,
       T_T_Heasb.THESABID      AS HesabID,
       T_T_Heasb.THESABNumber  AS CodeHesab,
       tbl_Tar_1.srtfield,
       tbl_Tar_1.tedad,
       tbl_Tar_1.unitprice,
       tbl_Tar_1.NameVahed,
       tbl_Tar_1.NameKala,
       tbl_Tar_1.MatnFi,
       tbl_Tar_1.SAIDType,
       tbl_Tar_1.NoeAmaliyat,
       tbl_Tar_1.TedadSadere,
       tbl_Tar_1.TedadVarede,
       tbl_Tar_1.TRAID,
       tbl_Tar_1.radif,
       tbl_Tar_1.nameKala2,
       tbl_Tar_1.NameKala1,
       tbl_Tar_1.FlagPrint
FROM   T_SA2
       INNER JOIN (
                SELECT Tbl_2.RadifSAID2,
                       Tbl_2.Sharh,
                       Tbl_2.Date1,
                       ISNULL(Tbl_2.PARBes, 0) + CONVERT(DECIMAL(19, 4), Tbl_2.TedadBarFo + Tbl_2.TedadKh) * (Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo) AS 
                       PARBes,
                       ISNULL(Tbl_2.DARBed, 0) + CONVERT(
                           DECIMAL(19, 4),
                           Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh
                       ) * (
                           Tbl_2.UnitPriceFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh
                       )  AS DARBed,
                       CONVERT(
                           INT,
                           CASE 
                                WHEN (NOT (Tbl_2.SAID2 IS NULL)) THEN Tbl_2.SAID2
                                ELSE [Tbl_2].fieldn2
                           END
                       )  AS SAID2,
                       CASE 
                            WHEN Tbl_2.FOROSHID > 0
                AND Tbl_2.KalaIDChild > 0 THEN Tedad1User + Tbl_2.TedadKh + Tbl_2.TedadBarFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh 
                    ELSE Tbl_2.TedadFo + Tbl_2.TedadKh + Tbl_2.TedadBarFo + Tbl_2.TedadBarKh
                    + Tbl_2.TedadDKh END AS tedad,
                CASE 
                     WHEN Tbl_2.FOROSHID > 0
                AND Tbl_2.KalaIDChild > 0 THEN UnitPriceUser + Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo + Tbl_2.UnitPriceBarKh 
                    + Tbl_2.UnitPriceDKh ELSE Tbl_2.UnitPriceFo + Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo
                    + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh END AS unitprice,
                T_NameVahed.NameVahed,
                CASE 
                     WHEN (NOT KALAID IS NULL) THEN (
                              (
                                  CASE 
                                       WHEN Tbl_2.FOROSHID > 0
                                  AND Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + 
                                      ')' ELSE T_NameKala.NameKala1 END
                              ) + N' ' + CONVERT(NVARCHAR(255), ISNULL(Tbl_2.Sharh, N''))
                          )
                     ELSE (
                              NAME + N' ' + CONVERT(NVARCHAR(255), ISNULL(Tbl_2.Sharh, N''))
                          )
                END + N' ' + CASE 
                                  WHEN (tedadfo > 0) THEN (N'فاکتور فروش' + ISNULL(Tbl_2.OperationCode, N''))
                                  WHEN (tedaddkh > 0) AND (NOT (Tbl_2.KHARIDID IS NULL))   THEN (N'کسورات فاکتور خريد ' + ISNULL(Tbl_2.OperationCode, N''))
                                  WHEN (tedadkh > 0) THEN (N'فاکتور خريد' + ISNULL(Tbl_2.OperationCode, N''))
                                  WHEN (tedadbarfo > 0) THEN (N'فاکتور برگشت از فروش' + ISNULL(Tbl_2.OperationCode, N''))
                                  WHEN (tedadbarkh > 0) THEN (
                                           N'فاکتور برگشت از خريد ' + ISNULL(Tbl_2.OperationCode, N'')
                                       )
                                  WHEN (tedaddkh > 0) THEN (N'فاکتور خدمات ' + ISNULL(Tbl_2.OperationCode, N''))
                                  ELSE NULL
                             END COLLATE Arabic_CI_AS AS NameKala,
                CASE 
                     WHEN (Tbl_2.SAID2 IS NULL) THEN N'في : '
                     ELSE NULL
                END AS MatnFi,
                CASE 
                     WHEN (NOT KALAID IS NULL) THEN NameKala1
                     ELSE (NAME + N' ')
                END AS nameKala2,
                Tbl_2.THESABID AS Hesabid,
                Tbl_2.TextShSA,
                CASE 
                     WHEN (Tbl_2.SAID2 IS NULL) THEN 2
                     ELSE 1
                END AS srtfield,
                Tbl_2.SAIDType,
                Tbl_2.TRAID,
                (
                    CASE 
                         WHEN (tedadfo > 0) THEN (N'فاکتور فروش' + ISNULL(Tbl_2.OperationCode, N''))
                         WHEN (tedadkh > 0) THEN (N'فاکتور خريد' + ISNULL(Tbl_2.OperationCode, N''))
                         WHEN (tedaddkh > 0) AND (NOT (Tbl_2.KHARIDID IS NULL))   THEN (N'فاکتور خريد' + ISNULL(Tbl_2.OperationCode, N''))
                         WHEN (tedadbarfo > 0) THEN (N'فاکتور برگشت از فروش' + ISNULL(Tbl_2.OperationCode, N''))
                         WHEN (tedadbarkh > 0) THEN (N'فاکتور برگشت از خريد' + ISNULL(Tbl_2.OperationCode, N''))
                         WHEN (tedaddkh > 0) THEN (N'فاکتور خدمات' + ISNULL(Tbl_2.OperationCode, N''))
                         WHEN (Tbl_2.DARID1 IS NOT NULL) THEN (N'دريافت نقدي ' + ISNULL(T_SA2_1.OperationCode, 0))
                         WHEN (Tbl_2.PARID1 IS NOT NULL) THEN (N'پرداخت نقدي ' + ISNULL(T_SA2_1.OperationCode, 0))
                         WHEN ((Tbl_2.PARID3 IS NOT NULL) OR (Tbl_2.PARID2 IS NOT NULL)) THEN (N'پرداخت چک ' + ISNULL(T_SA2_1.OperationCode, 0))
                         WHEN ((Tbl_2.DARID2 IS NOT NULL) OR (Tbl_2.DARID3 IS NOT NULL)) THEN (N'دريافت چک ' + ISNULL(T_SA2_1.OperationCode, 0))
                         ELSE N'سند حسابداري' + STR(T_SA2_1.SAID2Number)
                    END
                ) AS NoeAmaliyat,
                CASE 
                     WHEN Tbl_2.FOROSHID > 0
                AND Tbl_2.KalaIDChild > 0 THEN Tedad1User + Tbl_2.TedadBarKh + Tbl_2.TedadDKh ELSE Tbl_2.TedadFo + Tbl_2.TedadBarKh 
                    + Tbl_2.TedadDKh END AS TedadSadere,
                Tbl_2.TedadBarFo + Tbl_2.TedadKh AS TedadVarede,
                (Tbl_2.TedadBarFo + Tbl_2.TedadKh) * (Tbl_2.UnitPriceKh + Tbl_2.UnitPriceBarFo) AS beskala,
                (Tbl_2.TedadFo + Tbl_2.TedadBarKh + Tbl_2.TedadDKh) * (
                    Tbl_2.UnitPriceFo + Tbl_2.UnitPriceBarKh + Tbl_2.UnitPriceDKh
                ) AS bedkala,
                T_NameKhad.Name,
                Tbl_2.OperationCode,
                Tbl_2.radif,
                T_SA2_1.OperationCode AS OperationCode1,
                Tbl_2.FlagPrint,
                CASE 
                     WHEN Tbl_2.FOROSHID > 0
                AND Tbl_2.KalaIDChild > 0 THEN T_NameKala.NameKala1 + '(' + T_RelateKala.NameKala2 + ')' ELSE T_NameKala.NameKala1 
                    END COLLATE Arabic_CI_AS AS NameKala1,
                Tbl_2.KALAID
                FROM T_RelateKala RIGHT OUTER JOIN
                [TRANSACTION] AS Tbl_2 ON T_RelateKala.Kalaid2 = Tbl_2.KalaIDChild LEFT OUTER JOIN
                T_SA2 AS T_SA2_1 ON Tbl_2.SAID2 = T_SA2_1.SAID2 LEFT OUTER JOIN
                T_NameKhad ON Tbl_2.NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                T_NameKala INNER JOIN
                T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 ON Tbl_2.KALAID = T_NameKala.ID
                WHERE (Tbl_2.SAIDType <> 3)
                OR (Tbl_2.SAIDType IS NULL)
            )                  AS tbl_Tar_1
            ON  T_SA2.SAID2 = tbl_Tar_1.SAID2
       INNER JOIN T_T_Heasb
            ON  tbl_Tar_1.Hesabid = T_T_Heasb.THESABID
       LEFT OUTER JOIN (
                SELECT SUM(DARBed) - SUM(PARBes) AS mande,
                       SUM(PARBes)    AS SumOfPARBes,
                       SUM(DARBed)    AS SumOfDARBed,
                       THESABID       AS HESABID
                FROM   [TRANSACTION]  AS Tran1
                WHERE  (NOT (SAID2 IS NULL))
                       AND (Date1 < @Date1)
                GROUP BY
                       THESABID
            )                  AS DERIVEDTBL
            ON  T_T_Heasb.THESABID = DERIVEDTBL.HESABID
                             
                             UNION ALL

SELECT     1001 AS Expr3, dbo.HesabName(NULL, NULL, tbl_Tar.THESABID) AS NameHesabmoin, N'بابت تخفيفات ' + CONVERT(nvarchar(255), ISNULL(tbl_Tar.TextShSA, N'')) AS SharhRadif, NULL 
                      AS MandeHesab, tbl_Tar.PARBes AS Bedehkar, tbl_Tar.DARBed AS Bestankar, tbl_Tar.Date1 AS Tarikh, tbl_Tar.SAID2Number AS ShomareSanad, WhereHesab.THESABID, NULL AS CodeHesab, 
                      3 AS srtfield, 0 AS tedad, 0 AS unitprice, NULL AS NameVahed, NULL AS NameKala, NULL AS MatnFi, NULL AS SAIDType, NULL AS NoeAmaliyat, 0 AS TedadSadere, 0 AS TedadVarede, 
                      tbl_Tar.TRAID, tbl_Tar.radif, NULL AS Expr1, NULL AS Expr2, tbl_Tar.FlagPrint
FROM         [TRANSACTION] AS tbl_Tar INNER JOIN
                          (SELECT     SAID2, THESABID
                             FROM         [TRANSACTION]
                             WHERE     (NOT (SAID2Number IS NULL)) AND (NOT (RadifSAID2 IS NULL))
                             GROUP BY SAID2, THESABID) AS WhereHesab ON tbl_Tar.SAID2 = WhereHesab.SAID2
WHERE     (tbl_Tar.KOLID IN (24, 25, 26)) AND (tbl_Tar.SAIDType = 6)


UNION ALL

SELECT     999 AS Expr2, dbo.HesabName(NULL, NULL, T_Forosh.THESABID) AS NameHesabmoin, N'عوارض فروش'  AS SharhRadif, NULL AS MandeHesab, 
                       Avarez  AS Bedehkar, 0 AS Bestankar, T_Forosh.DateFo AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, 
                      T_T_Heasb.THESABID AS HesabID, T_T_Heasb.THESABNumber AS CodeHesab, 2 AS Expr10, 0 AS Expr3, 0 AS Expr4, NULL AS Expr5, NULL AS Expr6, NULL AS Expr11, NULL AS Expr12, NULL 
                      AS Expr13, 0 AS Expr7, 0 AS Expr8, NULL AS Expr14, NULL AS Expr15, NULL AS Expr9, NULL AS Expr1, 0 AS Expr16
FROM         T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_T_Heasb ON T_Forosh.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID AS HESABID
                             FROM         [TRANSACTION] AS Tran1
                             WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                             GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.HESABID
WHERE     ISNULL(T_Forosh.Avarez,0)>0
                             

UNION ALL

SELECT     999 AS Expr2, dbo.HesabName(NULL, NULL, T_Forosh.THESABID) AS NameHesabmoin, N'مالیات فروش'  AS SharhRadif, NULL AS MandeHesab, 
                       Maliyat  AS Bedehkar, 0 AS Bestankar, T_Forosh.DateFo AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, 
                      T_T_Heasb.THESABID AS HesabID, T_T_Heasb.THESABNumber AS CodeHesab, 2 AS Expr10, 0 AS Expr3, 0 AS Expr4, NULL AS Expr5, NULL AS Expr6, NULL AS Expr11, NULL AS Expr12, NULL 
                      AS Expr13, 0 AS Expr7, 0 AS Expr8, NULL AS Expr14, NULL AS Expr15, NULL AS Expr9, NULL AS Expr1, 0 AS Expr16
FROM         T_Forosh INNER JOIN
                      T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
                      T_T_Heasb ON T_Forosh.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID AS HESABID
                             FROM         [TRANSACTION] AS Tran1
                             WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                             GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.HESABID
WHERE     ISNULL(T_Forosh.Maliyat,0)>0

	)    





GO




/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14]    Script Date: 01/14/2020 10:56:19 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14]
GO


/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14]    Script Date: 01/14/2020 10:56:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14] 

@UserID INT,
@GOID INT,
@KOLID  INT,
@Hesabid INT,
@ThesabID  INT, 
@NameKhID  INT, 
@Date1  NVARCHAR(10),
@Date2  NVARCHAR(10)
AS


BEGIN
		
	Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @SumHazine AS DECIMAL(19,4)
    DECLARE @SumKoosorat AS DECIMAL(19,4)
    



		set @SQLQuery='	SELECT  SUM(SumHazine) AS SumHazine, SUM(SumKoosorat) AS SumKoosorat
FROM     ( SELECT  SUM(DARBed) as SumHazine ,0 as SumKoosorat
						FROM     [TRANSACTION]
						WHERE  (Date1 BETWEEN @Date1 AND @Date2) '


         Set @SQLQuery = @SQLQuery + ' And (GOID = @GOID)'
         Set @SQLQuery = @SQLQuery + ' And (KOLID = @KOLID)'
         
    If (@HESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (HESABID = @HESABID)'
    If (@THESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (THESABID = @THESABID)'         

		 Set @SQLQuery = @SQLQuery + 	'UNION ALL  SELECT 0 as SumHazine, SUM(UnitPriceDKh * TedadDKh) as SumKoosorat
											FROM     [TRANSACTION]
											WHERE  (NAMEKHID = @NAMEKHID) AND (NOT (KHARIDID IS NULL)) AND (Date1 BETWEEN @Date1 AND @Date2)) AS DrvKoosorat '
	
	    Set @ParamDefinition = '@UserID INT,
								@GOID INT,
								@KOLID  INT,
								@Hesabid INT,
								@ThesabID  INT, 
								@NameKhID  INT, 
								@Date1  NVARCHAR(10),
								@Date2  NVARCHAR(10),
								@SumHazine DECIMAL(19,4)'  
		    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GOID,
							@KOLID , 
							@Hesabid , 
							@ThesabID , 
							@NameKhID , 
							@Date1 , 
							@Date2 ,
							@SumHazine			                           




END





GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14]    Script Date: 01/14/2020 10:56:23 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14]
GO

/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14]    Script Date: 01/14/2020 10:56:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






CREATE PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14] 

@UserID INT,
@GOID INT,
@KOLID  INT,
@Hesabid INT,
@ThesabID  INT, 
@NameKhID  INT, 
@Date1  NVARCHAR(10),
@Date2  NVARCHAR(10)
AS


BEGIN
		
	Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
	set @SQLQuery='	SELECT  GOID, KOLID, HESABID, THESABID, DARBed, PARBes, dbo.HesabName(KOLID, HESABID, THESABID) AS NameHesab, Sharh, Date1
		FROM     [TRANSACTION]
		WHERE  (1=1) and (Date1 BETWEEN @Date1 AND @Date2) '


         Set @SQLQuery = @SQLQuery + ' And (GOID = @GOID)'
         Set @SQLQuery = @SQLQuery + ' And (KOLID = @KOLID)'
         
    If (@HESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (HESABID = @HESABID)'
    If (@THESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (THESABID = @THESABID)'         

	    Set @ParamDefinition = '@UserID INT,
								@GOID INT,
								@KOLID  INT,
								@Hesabid INT,
								@ThesabID  INT, 
								@NameKhID  INT, 
								@Date1  NVARCHAR(10),
								@Date2  NVARCHAR(10)'  
		    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GOID,
							@KOLID , 
							@Hesabid , 
							@ThesabID , 
							@NameKhID , 
							@Date1 , 
							@Date2
													                           






END




GO




/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14]    Script Date: 01/14/2020 10:56:27 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14]
GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14]    Script Date: 01/14/2020 10:56:27 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO







CREATE PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14] 

@UserID INT,
@NameKhID  INT, 
@Date1  NVARCHAR(10),
@Date2  NVARCHAR(10)
AS

/* @ResultVal 0 عمليات با موفقيت انجام شده است*/ 

BEGIN


SELECT  [TRANSACTION].GOID, [TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID, [TRANSACTION].NAMEKHID, CONVERT(money, [TRANSACTION].UnitPriceDKh * [TRANSACTION].TedadDKh) 
               AS UnitPriceDKh, [TRANSACTION].TedadDKh, [TRANSACTION].KHARIDID, [TRANSACTION].Date1, T_NameKhad.KhadamatNumber, T_NameKhad.Name, [TRANSACTION].Sharh, 
               dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab
FROM     [TRANSACTION] INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID
WHERE  (NOT ([TRANSACTION].KHARIDID IS NULL)) AND ([TRANSACTION].NAMEKHID = @NAMEKHID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)


END





GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 01/14/2020 10:56:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Kharid_Rep1SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 01/14/2020 10:56:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


   CREATE  PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14] 		 	
   
   @Userid INT,	 	
   @KharidID INT, 	
   @GroupID NVARCHAR(MAX),  	
   @KOLID NVARCHAR(MAX),  	
   @HesabidID NVARCHAR(MAX),  	
   @THesabidID NVARCHAR(MAX),  	
   @THesabidIDSath2 NVARCHAR(MAX),  	
   @StarteDate NVARCHAR(10), 	
   @ENDDate NVARCHAR(10), 	
   @KHARIDID1 INT, 	
   @KHARIDID2 INT  AS BEGIN 	 
   
   IF (@KharidID>0) 	
   
   BEGIN 
SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, 
                      T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, 
                      FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, 
                      T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, 
                      T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) 
                      AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 
                      1 AS GRP, 'کالاهای خریداری شده' AS NameGRP
FROM         T_T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 1) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN
                      T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN
                      T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE     (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh > 0)                      
    UNION ALL
    
SELECT  [TRANSACTION].TedadDKh AS Tedad, [TRANSACTION].UnitPriceDKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, '' AS NameVahedAsli, '' AS NameVahedFarei, 0 AS Expr1, '' AS Expr2, 
               T_NameKhad.KhadamatNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 0 AS tedadFarei, [TRANSACTION].radif, '' AS Expr3, '' AS Expr4, '' AS AddAnbar, 0 AS Tedad1, 
               0 AS Tedad2, T_NameKhad.Name, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 2 AS GRP, 'کسورات-هزینه های فاکتور' AS Expr5
FROM     T_KolHesab RIGHT OUTER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID ON T_KolHesab.KOLID = T_Kharid.KOLID LEFT OUTER JOIN
               T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh = 0)


    UNION ALL

SELECT  0 AS Tedad, Mablagh AS netprice, '' AS NameTarafHesab, '' AS NameHesabMoin, '' AS NameHesabTafsili, '' AS NameHesabKol, '' AS CodeTafsili, '' AS CodeKol, '' AS CodeMoin, Mablagh, '' AS Expr6, 
               DATE2 AS Tarikh, TextSharh AS SharhRadif, TextSharh AS TozihatFactor, '' AS NameVahedAsli, '' AS NameVahedFarei, '' AS Expr1, '' AS Expr2, PARID1Number AS CodeKala, '' AS HazineFactor, '' AS Serial, 
               '' AS ShomareFactor, '' AS AddressTarafHesab, '' AS Address2TarafHesab, '' AS Expr7, '' AS Expr8, '' AS Expr9, '' AS Expr10, '' AS Expr11, '' AS Expr12, '' AS Expr13, '' AS Expr14, '' AS Expr15, '' AS Expr16, 
               '' AS Expr17, '' AS Expr18, IDOperation AS KHARIDID, 0 AS tedadFarei, '' AS Expr19, '' AS Expr3, '' AS Expr4, '' AS AddAnbar, 0 AS Tedad1, 0 AS Tedad2, '' AS Expr20, '' AS Expr21, '' AS Expr22, 3 AS GRP, 
               'پرداخت های نقدی' AS Expr5
FROM     T_Par1
WHERE     (IDOperation = @KharidID)

      END               
ELSE 	

BEGIN  			
SET NOCOUNT ON;  			
Declare @SQLQuery AS NVarchar(MAX) 			
Declare @ParamDefinition AS NVarchar(MAX)  			
DECLARE @OrderBy AS NVARCHAR(MAX) 			
DECLARE @GROUPBy AS NVARCHAR(MAX) 		     			
IF (@StarteDate IS NULL) 					
SET @StarteDate='0' 		     				
IF (@ENDDate IS NULL) 					
SET @ENDDate='99999999' 						
SET @SQLQuery = '   SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0)  							  <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili,  							  T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh,  							  T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli,  							  FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor,  							  T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex,  							  T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1,  							  T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2)  							  AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0)  							  > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0)  							  > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala,  							  [TRANSACTION].Avarez 		FROM         T_T_Heasb RIGHT OUTER JOIN 							  dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN 							  [TRANSACTION] INNER JOIN 							  T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN 								  (SELECT     FactID, SUM(MabHazine) AS HazineFactor 									 FROM         Table4 									 WHERE     (TypeFact = 1) AND (Flag1 = 0) 									 GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN 							  T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN 							  T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID where 1=1 		'   			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
Set @ParamDefinition = '@UserID INT, 									
						@GroupID NVARCHAR(MAX), 									
						@KOLID NVARCHAR(MAX),  									
						@HesabidID NVARCHAR(MAX),  									
						@THesabidID NVARCHAR(MAX),  									
						@THesabidIDSath2 NVARCHAR(MAX),  									
						@StarteDate NVARCHAR(MAX),  									
						@ENDDate NVARCHAR(MAX), 									
						@KHARIDID1 INT, 									
						@KHARIDID2 INT '  	 			 			
/* Execute the Transact-SQL String with all parameter value's 	    			   Using sp_executesql Command */ 			    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 			
Execute sp_Executesql   @SQLQuery,  									
						@ParamDefinition, 									
						@UserID , 									
						@GroupID, 									
						@KOLID ,  									
						@HesabidID ,  									
						@THesabidID ,  									
						@THesabidIDSath2 ,  									
						@StarteDate ,  									
						@ENDDate, 									
						@KHARIDID1, 									
						@KHARIDID1   			
If @@ERROR <> 0 GoTo ErrorHandler 			
Set NoCount OFF 		   			
ErrorHandler: 				
Return(@@ERROR) 	
END			 
END    

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 01/15/2020 09:13:58 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Kharid_Rep1SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 01/15/2020 09:13:58 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


   CREATE  PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14] 		 	
   
   @Userid INT,	 	
   @KharidID INT, 	
   @GroupID NVARCHAR(MAX),  	
   @KOLID NVARCHAR(MAX),  	
   @HesabidID NVARCHAR(MAX),  	
   @THesabidID NVARCHAR(MAX),  	
   @THesabidIDSath2 NVARCHAR(MAX),  	
   @StarteDate NVARCHAR(10), 	
   @ENDDate NVARCHAR(10), 	
   @KHARIDID1 INT, 	
   @KHARIDID2 INT  AS BEGIN 	 
   
   IF (@KharidID>0) 	
   
   BEGIN 
SELECT  [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, 
               FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, 
               T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, 
               T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 
               ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, 
               FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) 
               ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, 
               FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 1 AS GRP, 'کالاهای خریداری شده' AS NameGRP
FROM     T_T_Heasb RIGHT OUTER JOIN
               dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN
               T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh > 0)                
    UNION ALL
    
SELECT  [TRANSACTION].TedadDKh AS Tedad, [TRANSACTION].UnitPriceDKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL AS Expr1, NULL AS Expr2, 
               T_NameKhad.KhadamatNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, NULL AS tedadFarei, [TRANSACTION].radif, NULL AS Expr3, NULL AS Expr4, NULL 
               AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, T_NameKhad.Name, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 2 AS GRP, 'کسورات-هزینه های فاکتور' AS Expr5
FROM     T_KolHesab RIGHT OUTER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID ON T_KolHesab.KOLID = T_Kharid.KOLID LEFT OUTER JOIN
               T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh = 0)


    UNION ALL

SELECT  NULL AS Tedad, Mablagh AS netprice, NULL AS NameTarafHesab, NULL AS NameHesabMoin, NULL AS NameHesabTafsili, NULL AS NameHesabKol, NULL AS CodeTafsili, NULL AS CodeKol, NULL AS CodeMoin, 
               Mablagh, NULL AS Expr6, DATE2 AS Tarikh, TextSharh AS SharhRadif, TextSharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL AS Expr1, NULL AS Expr2, 
               PARID1Number AS CodeKala, NULL AS HazineFactor, NULL AS Serial, NULL AS ShomareFactor, NULL AS AddressTarafHesab, NULL AS Address2TarafHesab, NULL AS Expr7, NULL AS Expr8, NULL AS Expr9, NULL 
               AS Expr10, NULL AS Expr11, NULL AS Expr12, NULL AS Expr13, NULL AS Expr14, NULL AS Expr15, NULL AS Expr16, NULL AS Expr17, NULL AS Expr18, IDOperation AS KHARIDID, NULL AS tedadFarei, NULL 
               AS Expr19, NULL AS Expr3, NULL AS Expr4, NULL AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, NULL AS Expr20, NULL AS Expr21, NULL AS Expr22, 3 AS GRP, 'پرداخت های نقدی' AS Expr5
FROM     T_Par1
WHERE  (IDOperation = @KharidID)

      END               
ELSE 	

BEGIN  			
SET NOCOUNT ON;  			
Declare @SQLQuery AS NVarchar(MAX) 			
Declare @ParamDefinition AS NVarchar(MAX)  			
DECLARE @OrderBy AS NVARCHAR(MAX) 			
DECLARE @GROUPBy AS NVARCHAR(MAX) 		     			
IF (@StarteDate IS NULL) 					
SET @StarteDate='0' 		     				
IF (@ENDDate IS NULL) 					
SET @ENDDate='99999999' 						
SET @SQLQuery = '   SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0)  							  <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili,  							  T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh,  							  T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli,  							  FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor,  							  T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex,  							  T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1,  							  T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2)  							  AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0)  							  > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0)  							  > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala,  							  [TRANSACTION].Avarez 		FROM         T_T_Heasb RIGHT OUTER JOIN 							  dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN 							  [TRANSACTION] INNER JOIN 							  T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN 								  (SELECT     FactID, SUM(MabHazine) AS HazineFactor 									 FROM         Table4 									 WHERE     (TypeFact = 1) AND (Flag1 = 0) 									 GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN 							  T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN 							  T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID where 1=1 		'   			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
Set @ParamDefinition = '@UserID INT, 									
						@GroupID NVARCHAR(MAX), 									
						@KOLID NVARCHAR(MAX),  									
						@HesabidID NVARCHAR(MAX),  									
						@THesabidID NVARCHAR(MAX),  									
						@THesabidIDSath2 NVARCHAR(MAX),  									
						@StarteDate NVARCHAR(MAX),  									
						@ENDDate NVARCHAR(MAX), 									
						@KHARIDID1 INT, 									
						@KHARIDID2 INT '  	 			 			
/* Execute the Transact-SQL String with all parameter value's 	    			   Using sp_executesql Command */ 			    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 			
Execute sp_Executesql   @SQLQuery,  									
						@ParamDefinition, 									
						@UserID , 									
						@GroupID, 									
						@KOLID ,  									
						@HesabidID ,  									
						@THesabidID ,  									
						@THesabidIDSath2 ,  									
						@StarteDate ,  									
						@ENDDate, 									
						@KHARIDID1, 									
						@KHARIDID1   			
If @@ERROR <> 0 GoTo ErrorHandler 			
Set NoCount OFF 		   			
ErrorHandler: 				
Return(@@ERROR) 	
END			 
END    

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 02/05/2020 10:16:44 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Kharid_Rep1SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 02/05/2020 10:16:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


   CREATE  PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14] 		 	
   
   @Userid INT,	 	
   @KharidID INT, 	
   @GroupID NVARCHAR(MAX),  	
   @KOLID NVARCHAR(MAX),  	
   @HesabidID NVARCHAR(MAX),  	
   @THesabidID NVARCHAR(MAX),  	
   @THesabidIDSath2 NVARCHAR(MAX),  	
   @StarteDate NVARCHAR(10), 	
   @ENDDate NVARCHAR(10), 	
   @KHARIDID1 INT, 	
   @KHARIDID2 INT  AS BEGIN 	 
   
   IF (@KharidID>0) 	
   
   BEGIN 
SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, 
                      T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, 
                      FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, 
                      T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, 
                      T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) 
                      AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 
                      1 AS GRP, 'کالاهای خریداری شده' AS NameGRP
FROM         T_T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 1) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN
                      T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN
                      T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE     (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh > 0)                      
    UNION ALL
    
SELECT  [TRANSACTION].TedadDKh AS Tedad, [TRANSACTION].UnitPriceDKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL AS Expr1, NULL AS Expr2, 
               T_NameKhad.KhadamatNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 0 AS tedadFarei, [TRANSACTION].radif, NULL AS Expr3, NULL AS Expr4, NULL 
               AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, T_NameKhad.Name, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 2 AS GRP, 'کسورات-هزینه های فاکتور' AS Expr5
FROM     T_KolHesab RIGHT OUTER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID ON T_KolHesab.KOLID = T_Kharid.KOLID LEFT OUTER JOIN
               T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh = 0)


    UNION ALL

SELECT  NULL AS Tedad, Mablagh AS netprice, NULL AS NameTarafHesab, NULL AS NameHesabMoin, NULL AS NameHesabTafsili, NULL AS NameHesabKol, NULL AS CodeTafsili, NULL AS CodeKol, NULL AS CodeMoin, 
               Mablagh, NULL AS Expr6, DATE2 AS Tarikh, TextSharh AS SharhRadif, TextSharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL AS Expr1, NULL AS Expr2, 
               PARID1Number AS CodeKala, NULL AS HazineFactor, NULL AS Serial, NULL AS ShomareFactor, NULL AS AddressTarafHesab, NULL AS Address2TarafHesab, NULL AS Expr7, NULL AS Expr8, NULL AS Expr9, NULL 
               AS Expr10, NULL AS Expr11, NULL AS Expr12, NULL AS Expr13, NULL AS Expr14, NULL AS Expr15, NULL AS Expr16, NULL AS Expr17, NULL AS Expr18, IDOperation AS KHARIDID, NULL AS tedadFarei, NULL 
               AS Expr19, NULL AS Expr3, NULL AS Expr4, NULL AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, NULL AS Expr20, NULL AS Expr21, NULL AS Expr22, 3 AS GRP, 'پرداخت های نقدی' AS Expr5
FROM     T_Par1
WHERE  (IDOperation = @KharidID)

      END               
ELSE 	

BEGIN  			
SET NOCOUNT ON;  			
Declare @SQLQuery AS NVarchar(MAX) 			
Declare @ParamDefinition AS NVarchar(MAX)  			
DECLARE @OrderBy AS NVARCHAR(MAX) 			
DECLARE @GROUPBy AS NVARCHAR(MAX) 		     			
IF (@StarteDate IS NULL) 					
SET @StarteDate='0' 		     				
IF (@ENDDate IS NULL) 					
SET @ENDDate='99999999' 						
SET @SQLQuery = '   SELECT  [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, 
               FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, 
               T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, 
               T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 
               ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, 
               FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) 
               ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, 
               FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 1 AS GRP, ''کالاهای خریداری شده'' AS NameGRP
FROM     T_T_Heasb RIGHT OUTER JOIN
               dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN
               T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (NOT (T_Kharid.KHARIDID IS NULL)) AND ([TRANSACTION].UnitPriceKh > 0) 		'   
			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
Set @ParamDefinition = '@UserID INT, 									
						@GroupID NVARCHAR(MAX), 									
						@KOLID NVARCHAR(MAX),  									
						@HesabidID NVARCHAR(MAX),  									
						@THesabidID NVARCHAR(MAX),  									
						@THesabidIDSath2 NVARCHAR(MAX),  									
						@StarteDate NVARCHAR(MAX),  									
						@ENDDate NVARCHAR(MAX), 									
						@KHARIDID1 INT, 									
						@KHARIDID2 INT '  	 			 			
/* Execute the Transact-SQL String with all parameter value's 	    			   Using sp_executesql Command */ 			    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 


SET @SQLQuery =@SQLQuery+ '     UNION ALL
    
SELECT  [TRANSACTION].TedadDKh AS Tedad, [TRANSACTION].UnitPriceDKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL AS Expr1, NULL AS Expr2, 
               T_NameKhad.KhadamatNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 0 AS tedadFarei, [TRANSACTION].radif, NULL AS Expr3, NULL AS Expr4, NULL 
               AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, T_NameKhad.Name, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 2 AS GRP, ''کسورات-هزینه های فاکتور'' AS Expr5
FROM     T_KolHesab RIGHT OUTER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID ON T_KolHesab.KOLID = T_Kharid.KOLID LEFT OUTER JOIN
               T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (NOT (T_Kharid.KHARIDID IS NULL)) AND ([TRANSACTION].UnitPriceKh = 0)		'   
			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
		    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 

SET @SQLQuery =@SQLQuery+ '     UNION ALL
SELECT  NULL AS Tedad, T_Par1.Mablagh AS netprice, NULL AS NameTarafHesab, NULL AS NameHesabMoin, NULL AS NameHesabTafsili, NULL AS NameHesabKol, NULL AS CodeTafsili, NULL AS CodeKol, NULL 
               AS CodeMoin, T_Par1.Mablagh, NULL AS Expr6, T_Par1.DATE2 AS Tarikh, T_Par1.TextSharh AS SharhRadif, T_Par1.TextSharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL 
               AS Expr1, NULL AS Expr2, T_Par1.PARID1Number AS CodeKala, NULL AS HazineFactor, NULL AS Serial, NULL AS ShomareFactor, NULL AS AddressTarafHesab, NULL AS Address2TarafHesab, NULL 
               AS Expr7, NULL AS Expr8, NULL AS Expr9, NULL AS Expr10, NULL AS Expr11, NULL AS Expr12, NULL AS Expr13, NULL AS Expr14, NULL AS Expr15, NULL AS Expr16, NULL AS Expr17, NULL AS Expr18, 
               T_Par1.IDOperation AS KHARIDID, NULL AS tedadFarei, NULL AS Expr19, NULL AS Expr3, NULL AS Expr4, NULL AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, NULL AS Expr20, NULL AS Expr21, NULL 
               AS Expr22, 3 AS GRP, ''پرداخت های نقدی'' AS Expr5
FROM     T_Par1 INNER JOIN
               T_Kharid ON T_Par1.IDOperation = T_Kharid.KHARIDID
WHERE  (NOT (T_Par1.IDOperation IS NULL))
  		'   
			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
		    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 	
		
Execute sp_Executesql   @SQLQuery,  									
						@ParamDefinition, 									
						@UserID , 									
						@GroupID, 									
						@KOLID ,  									
						@HesabidID ,  									
						@THesabidID ,  									
						@THesabidIDSath2 ,  									
						@StarteDate ,  									
						@ENDDate, 									
						@KHARIDID1, 									
						@KHARIDID1   			
If @@ERROR <> 0 GoTo ErrorHandler 			
Set NoCount OFF 		   			
ErrorHandler: 				
Return(@@ERROR) 	
END			 
END    

GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14]    Script Date: 02/05/2020 10:16:40 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14]
GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14]    Script Date: 02/05/2020 10:16:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO







CREATE PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub2_SF14] 

@UserID INT,
@NameKhID  INT, 
@Date1  NVARCHAR(10),
@Date2  NVARCHAR(10)
AS

/* @ResultVal 0 عمليات با موفقيت انجام شده است*/ 

BEGIN


SELECT  [TRANSACTION].GOID, [TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID, [TRANSACTION].NAMEKHID, CONVERT(money, [TRANSACTION].UnitPriceDKh * [TRANSACTION].TedadDKh) 
               AS UnitPriceDKh, [TRANSACTION].TedadDKh, [TRANSACTION].KHARIDID, [TRANSACTION].Date1, T_NameKhad.KhadamatNumber, T_NameKhad.Name, [TRANSACTION].Sharh, 
               dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab
FROM     [TRANSACTION] INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID
WHERE   ([TRANSACTION].NAMEKHID = @NAMEKHID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)


END





GO




/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14]    Script Date: 02/05/2020 10:16:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14]
GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14]    Script Date: 02/05/2020 10:16:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






CREATE PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Sub1_SF14] 

@UserID INT,
@GOID INT,
@KOLID  INT,
@Hesabid INT,
@ThesabID  INT, 
@NameKhID  INT, 
@Date1  NVARCHAR(10),
@Date2  NVARCHAR(10)
AS


BEGIN
		
	Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
	set @SQLQuery='	SELECT  GOID, KOLID, HESABID, THESABID, DARBed, PARBes, dbo.HesabName(KOLID, HESABID, THESABID) AS NameHesab, Sharh, Date1
		FROM     [TRANSACTION]
		WHERE  (1=1) and (Date1 BETWEEN @Date1 AND @Date2) '


         Set @SQLQuery = @SQLQuery + ' And (GOID = @GOID)'
         Set @SQLQuery = @SQLQuery + ' And (KOLID = @KOLID)'
         
    If (@HESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (HESABID = @HESABID)'
    If (@THESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (THESABID = @THESABID)'         

	    Set @ParamDefinition = '@UserID INT,
								@GOID INT,
								@KOLID  INT,
								@Hesabid INT,
								@ThesabID  INT, 
								@NameKhID  INT, 
								@Date1  NVARCHAR(10),
								@Date2  NVARCHAR(10)'  
		    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GOID,
							@KOLID , 
							@Hesabid , 
							@ThesabID , 
							@NameKhID , 
							@Date1 , 
							@Date2
													                           






END




GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Rep1_SF14]    Script Date: 02/05/2020 10:16:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_Kharid_Analyz_Kosorat_Rep1_SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Rep1_SF14]
GO


/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Rep1_SF14]    Script Date: 02/05/2020 10:16:32 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






CREATE PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Rep1_SF14] 

@UserID INT,
@GOID INT,
@KOLID  INT,
@Hesabid INT,
@ThesabID  INT, 
@NameKhID  INT, 
@Date1  NVARCHAR(10),
@Date2  NVARCHAR(10)
AS


BEGIN
		
	Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
	set @SQLQuery='	SELECT  GOID, KOLID, HESABID, THESABID, NULL AS NAMEKHID, DARBed, 0 AS PARBes, 0 AS TedadDKh, dbo.HesabName(KOLID, HESABID, THESABID) AS NameHesab, NULL AS namekhadamat, NULL 
               AS KhadamatNumber, Sharh, Date1, 1 AS Grp,''تامين اعتبارات کسورات'' as NameGRP
FROM     [TRANSACTION]
WHERE  (1 = 1) AND (Date1 BETWEEN @Date1 AND @Date2) '

         Set @SQLQuery = @SQLQuery + ' And (GOID = @GOID)'
         Set @SQLQuery = @SQLQuery + ' And (KOLID = @KOLID)'
         
    If (@HESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (HESABID = @HESABID)'
    If (@THESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (THESABID = @THESABID)'         

Set @SQLQuery = @SQLQuery +' Union all SELECT  GOID, KOLID, HESABID, THESABID
							, [TRANSACTION].NAMEKHID, 0 AS DARBed,CONVERT(money, UnitPriceDKh * TedadDKh)  AS PARBes
							, TedadDKh,dbo.HesabName(KOLID, HESABID, THESABID) AS NameHesab,T_NameKhad.Name, KhadamatNumber,Sharh,  Date1, 2 AS Grp,''کسورات استفاده شده'' as NameGRP
							FROM     [TRANSACTION] INNER JOIN
										   T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID
							WHERE   ([TRANSACTION].NAMEKHID = @NAMEKHID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2) '


	    Set @ParamDefinition = '@UserID INT,
								@GOID INT,
								@KOLID  INT,
								@Hesabid INT,
								@ThesabID  INT, 
								@NameKhID  INT, 
								@Date1  NVARCHAR(10),
								@Date2  NVARCHAR(10)'  
		    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GOID,
							@KOLID , 
							@Hesabid , 
							@ThesabID , 
							@NameKhID , 
							@Date1 , 
							@Date2
													                           






END




GO




/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14]    Script Date: 02/05/2020 10:16:28 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14]
GO


/****** Object:  StoredProcedure [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14]    Script Date: 02/05/2020 10:16:28 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[Op_Paye_Kharid_Analyz_Kosorat_Calc_Sub1_2_SF14] 

@UserID INT,
@GOID INT,
@KOLID  INT,
@Hesabid INT,
@ThesabID  INT, 
@NameKhID  INT, 
@Date1  NVARCHAR(10),
@Date2  NVARCHAR(10)
AS


BEGIN
		
	Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @SumHazine AS DECIMAL(19,4)
    DECLARE @SumKoosorat AS DECIMAL(19,4)
    



		set @SQLQuery='	SELECT  SUM(SumHazine) AS SumHazine, SUM(SumKoosorat) AS SumKoosorat
FROM     ( SELECT  SUM(DARBed) as SumHazine ,0 as SumKoosorat
						FROM     [TRANSACTION]
						WHERE  (Date1 BETWEEN @Date1 AND @Date2) '


         Set @SQLQuery = @SQLQuery + ' And (GOID = @GOID)'
         Set @SQLQuery = @SQLQuery + ' And (KOLID = @KOLID)'
         
    If (@HESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (HESABID = @HESABID)'
    If (@THESABID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (THESABID = @THESABID)'         

		 Set @SQLQuery = @SQLQuery + 	'UNION ALL  SELECT 0 as SumHazine, SUM(UnitPriceDKh * TedadDKh) as SumKoosorat
											FROM     [TRANSACTION]
											WHERE  (NAMEKHID = @NAMEKHID) AND (Date1 BETWEEN @Date1 AND @Date2)) AS DrvKoosorat '
	
	    Set @ParamDefinition = '@UserID INT,
								@GOID INT,
								@KOLID  INT,
								@Hesabid INT,
								@ThesabID  INT, 
								@NameKhID  INT, 
								@Date1  NVARCHAR(10),
								@Date2  NVARCHAR(10),
								@SumHazine DECIMAL(19,4)'  
		    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GOID,
							@KOLID , 
							@Hesabid , 
							@ThesabID , 
							@NameKhID , 
							@Date1 , 
							@Date2 ,
							@SumHazine			                           




END





GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKhadamat]    Script Date: 02/12/2020 08:32:44 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListKhadamat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListKhadamat]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListKhadamat]    Script Date: 02/12/2020 08:32:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





    CREATE PROCEDURE [dbo].[QSubPFrm_ListKhadamat] @UserID INT,@StartDateList Nvarchar(10),@EndDateList Nvarchar(10)   AS       
    
  
        Begin  

 declare @FlagAmin bit;
 declare @FlagAdamRoyatSanadAdmin bit;
  
  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
if(@FlagAdamRoyatSanadAdmin=0)
		SELECT  T_DKhad.DKHID, T_DKhad.HesabID, T_DKhad.GOID, T_DKhad.KOLID, T_DKhad.MOINID, T_DKhad.THESABID, T_DKhad.TextName, T_DKhad.TextName1, T_DKhad.TextNameT, T_DKhad.DateKh, 
					   T_DKhad.TextShSA, T_DKhad.NumSID2, T_DKhad.Takhfif, T_DKhad.Sharh, T_DKhad.FieldN1, T_DKhad.FieldN2, T_DKhad.FieldN3, T_DKhad.FieldN4, T_DKhad.FieldT1, T_DKhad.FieldT2, T_DKhad.FieldT3, 
					   T_DKhad.FieldT4, T_DKhad.Flag1, T_DKhad.Flag2, T_DKhad.Flag3, T_DKhad.Flag4, T_DKhad.PrimarySaid2, T_DKhad.Dateeng, T_DKhad.DKHNumbr, T_SA2.SAID2Number, dbo.HesabName(T_DKhad.KOLID, 
					   T_DKhad.HesabID, T_DKhad.THESABID) AS namehesab, T_DKhad.dateSettlement, T_DKhad.ShGhabz, T_DKhad.TimeSabt, T_DKhad.Maliyat, T_DKhad.Avarez, T_DKhad.address2, T_DKhad.DateKhStartDate, 
					   T_DKhad.DateKhEndDate
		FROM     T_DKhad INNER JOIN
					   T_SA2 ON T_DKhad.PrimarySaid2 = T_SA2.SAID2
		WHERE  (T_DKhad.DateKh BETWEEN @StartDateList AND @EndDateList)
		ORDER BY T_DKhad.DKHID DESC
else
	SELECT  T_DKhad.DKHID, T_DKhad.HesabID, T_DKhad.GOID, T_DKhad.KOLID, T_DKhad.MOINID, T_DKhad.THESABID, T_DKhad.TextName, T_DKhad.TextName1, T_DKhad.TextNameT, T_DKhad.DateKh, 
				   T_DKhad.TextShSA, T_DKhad.NumSID2, T_DKhad.Takhfif, T_DKhad.Sharh, T_DKhad.FieldN1, T_DKhad.FieldN2, T_DKhad.FieldN3, T_DKhad.FieldN4, T_DKhad.FieldT1, T_DKhad.FieldT2, T_DKhad.FieldT3, 
				   T_DKhad.FieldT4, T_DKhad.Flag1, T_DKhad.Flag2, T_DKhad.Flag3, T_DKhad.Flag4, T_DKhad.PrimarySaid2, T_DKhad.Dateeng, T_DKhad.DKHNumbr, T_SA2.SAID2Number, dbo.HesabName(T_DKhad.KOLID, 
				   T_DKhad.HesabID, T_DKhad.THESABID) AS namehesab, T_DKhad.dateSettlement, T_DKhad.ShGhabz, T_DKhad.TimeSabt, T_DKhad.Maliyat, T_DKhad.Avarez, T_DKhad.address2, T_DKhad.DateKhStartDate, 
				   T_DKhad.DateKhEndDate
	FROM     T_DKhad INNER JOIN
				   T_SA2 ON T_DKhad.PrimarySaid2 = T_SA2.SAID2
	WHERE  (T_DKhad.DateKh BETWEEN @StartDateList AND @EndDateList) AND (NOT (ISNULL(T_DKhad.userCreate, - 10) IN
					   (SELECT  USERID
						FROM     AC_Setting.dbo.T_yUsers
						WHERE  (USERID = 3) OR
									   (FlagAmin <> 0))))
	ORDER BY T_DKhad.DKHID DESC
			

end



GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_KhadamatSF11_CALC]    Script Date: 04/07/2020 15:38:43 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_KhadamatSF11_CALC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_KhadamatSF11_CALC]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_KhadamatSF11_CALC]    Script Date: 04/07/2020 15:38:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE  PROCEDURE [dbo].[QSubPFrm_KhadamatSF11_CALC]
		
		@Userid INT,	
		@TxtLinkTosub NVARCHAR(255)

AS
BEGIN
	

     DECLARE   @Tedad DECIMAL(19,4) 
     DECLARE   @MabFactor DECIMAL(19,4) 
     DECLARE   @MabHazine DECIMAL(19,4) 
     DECLARE   @MabTakhfif DECIMAL(19,4) 
     DECLARE   @MabMaliat DECIMAL(19,4) 
     DECLARE   @MabAvarez DECIMAL(19,4) 
     DECLARE   @MabFactorSF11 DECIMAL(19,4) 
        
SELECT      @Tedad=SUM(Tedad),@MabFactor= SUM(Tedad * UnitPrice) , @MabTakhfif=SUM(isnull(FieldN7,0)) , @MabMaliat=SUM(isnull(Maliatkala,0)) , @MabAvarez=SUM(isnull(Avarez,0)) ,@MabFactorSF11= SUM(Tedad * isnull(Tedad2,1) * UnitPrice)
FROM         TRANSACTION2
WHERE     (date1 = @TxtLinkTosub)
GROUP BY LinkedID

 
SELECT     @MabHazine=SUM(ISNULL(DARBed, 0)) 
FROM         TRANSACTION2
WHERE     (TextAIDFo = @TxtLinkTosub) AND (Flag1 <> 1)
GROUP BY LinkedID

SELECT     @Tedad AS Tedad, @MabFactor AS MabFactor, isnull(@MabTakhfif,0) AS MabTakhfif, isnull(@MabMaliat,0) AS MabMaliat, isnull(@MabAvarez,0) AS MabAvarez, isnull(@MabHazine,0) AS MabHazine	, isnull(@MabFactorSF11,0) AS MabFactorSF11,0 as vazn		  

END









GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_CALC]    Script Date: 04/09/2020 18:36:25 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_CALC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_CALC]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_CALC]    Script Date: 04/09/2020 18:36:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_CALC]
		
		@Userid INT,	
		@TxtLinkTosub NVARCHAR(255)

AS
BEGIN
	

     DECLARE   @Tedad DECIMAL(19,4) 
     DECLARE   @MabFactor DECIMAL(19,4) 
     DECLARE   @MabHazine DECIMAL(19,4) 
     DECLARE   @MabTakhfif DECIMAL(19,4) 
     DECLARE   @MabMaliat DECIMAL(19,4) 
     DECLARE   @MabAvarez DECIMAL(19,4) 
     DECLARE   @MabFactorSF11 DECIMAL(19,4) 
     DECLARE   @MabKosorat DECIMAL(19,4) 
     DECLARE   @VazKala DECIMAL(19,4) 



SELECT      @Tedad= SUM(Tedad) ,@MabFactor=  SUM(Tedad * UnitPrice)  , @MabTakhfif=SUM(isnull(FieldN7,0)) , @MabMaliat=SUM(isnull(Maliatkala,0)) , @MabAvarez=SUM(isnull(Avarez,0)) ,@MabFactorSF11= 0
FROM         (SELECT     CASE WHEN ZaribChildUser > 0 THEN Tedad1User ELSE Tedad END AS Tedad, CASE WHEN ZaribChildUser > 0 THEN UnitPriceUser ELSE UnitPrice END AS UnitPrice, FieldN7, Maliatkala, Avarez, 
                      Date1,LinkedID
FROM         TRANSACTION2 AS Temp) TRANSACTION2
WHERE     (date1 = @TxtLinkTosub)
GROUP BY LinkedID

 
SELECT     @MabHazine=SUM(ISNULL(DARBed, 0)) 
FROM         TRANSACTION2
WHERE     (TextAIDFo = @TxtLinkTosub) AND (Flag1 <> 1)
GROUP BY LinkedID


SELECT   @MabKosorat=SUM(ISNULL(UnitPrice, 0) * ISNULL(Tedad, 0)) 
FROM     TRANSACTION2 AS Temp
WHERE     (FieldT8 = @TxtLinkTosub)


SELECT  @VazKala=SUM(TRANSACTION2.Tedad * ISNULL(T_NameKala.vaznKala, 0))
FROM     (SELECT  CASE WHEN ZaribChildUser > 0 THEN Tedad1User ELSE Tedad END AS Tedad, CASE WHEN ZaribChildUser > 0 THEN UnitPriceUser ELSE UnitPrice END AS UnitPrice, FieldN7, Maliatkala, Avarez, Date1, 
                               LinkedID, KALAID
                FROM     TRANSACTION2 AS Temp) AS TRANSACTION2 INNER JOIN
               T_NameKala ON TRANSACTION2.KALAID = T_NameKala.ID
WHERE  (TRANSACTION2.Date1 = @TxtLinkTosub)

SELECT     @Tedad AS Tedad, @MabFactor AS MabFactor, isnull(@MabTakhfif,0) AS MabTakhfif, isnull(@MabMaliat,0) AS MabMaliat, isnull(@MabAvarez,0) AS MabAvarez, isnull(@MabHazine,0) AS MabHazine	, 0 AS MabFactorSF11,@MabKosorat AS MabKosorat,@VazKala AS vazn		  

END









GO




/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1]    Script Date: 04/11/2020 11:20:43 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_Sandoogh_Analyz1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1]
GO


/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1]    Script Date: 04/11/2020 11:20:43 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





  CREATE PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1] ( 
						@AID int,
						@OperType int,
						@LinkMasterID int,
						@GoKID int,
						@OnlyMandedare BIT,
						@OnlyActive Bit)           
  AS     
  BEGIN 	
  
  SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(Max);  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max);         	
  DECLARE @Sortstring  NVARCHAR(Max);  
  DECLARE @CmbSort AS INT;
  	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';      
  set @Sortstring=N' ';            	
  SET @ParmDefinition = N'@AID int=null, 								
							  @OperType int=null, 								
							  @LinkMasterID  int=null, 								
							  @GoKID int=null,
		@OnlyMandedare BIT=null,
		@OnlyActive Bit=null ';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
	  	
	SELECT    @OnlyActive= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 2)

	SELECT    @OnlyMandedare= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 3)	  	
	
	SELECT    @CmbSort= ABS(ISNULL(FieldN1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 10) AND (OPTIONID = 1)	  
		 
	  		 
  SET @SQLString =' SELECT     FunInfoKalaAll_1.ID AS NameKalaID, CONVERT(float, FunInfoKalaAll_1.KalaNumber) AS KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.Zarib, 
                      FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.KalaNumber AS [کد کالا], FunInfoKalaAll_1.NameKala1+ + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' '')  AS [نام کالا], FunInfoKalaAll_1.NameVahed1 AS [نام واحد], 
                      ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) AS [مانده انبار], FunInfoKalaAll_1.NameAnbar AS [نام انبار], FunInfoKalaAll_1.UnitPrice1 AS [قيمت فروش1], ROUND(CONVERT(money, 
                      drvTra.MaxUnitPriceKh), 2) AS [آخرين خريد], FunInfoKalaAll_1.UnitPrice2 AS [قيمت فروش2], FunInfoKalaAll_1.UnitPrice3 AS [قيمت فروش3], FunInfoKalaAll_1.UnitPrice4 AS [قيمت فروش4], 
                      FunInfoKalaAll_1.UnitPrice5 AS [قيمت فروش5], FunInfoKalaAll_1.UnitPrice6 AS [قيمت فروش6], FunInfoKalaAll_1.NameVahed2, drvTra.MaxUnitPriceKh AS UnitPriceKh, FunInfoKalaAll_1.FieldN1, 
                      FunInfoKalaAll_1.BarCode AS [ بارکد ], FunInfoKalaAll_1.NameGoKala1 AS [گروه کالا], FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.UnitPrice1, drvTra.MaxUnitPriceKh AS AkharinKharid, 
                      FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, FunInfoKalaAll_1.Active, 
                      FunInfoKalaAll_1.NameGoKala1, ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3)  AS MandeAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber) AS CodKala,FunInfoKalaAll_1.UnitPriceKharid as UnitPricekhUser , FunInfoKalaAll_1.vaznKala 
FROM         dbo.FunInfoKalaAll(@OnlyActive) AS FunInfoKalaAll_1 INNER JOIN
                      dbo.Fun_CQ_ListKala_Sandoogh_Analyz() AS drvTra ON FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID 
WHERE     (1 = 1)

' 	



/*FunInfoKalaAll_1.AID = drvTra.AID and FunInfoKalaAll_1.ID = drvTra.KALAID */

/* FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID  */

  /* -----------------------نشان دادن کالاهاي فعال */ 
/*
if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه
	*/
               
  /* -----------------------شرط انبار */ 	

		  		
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 		
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	

 /* /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@TextBoxFilter  IS NULL) OR LEN(@TextBoxFilter)>1) 			
BEGIN 		
	set @TextBoxFilter=N'%'+@TextBoxFilter +N'%'
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'  (FunInfoKalaAll_1.NameKala1 Like @TextBoxFilter ) Or (KalaNumber like @TextBoxFilter) '; 				
	set @ParmAndstr=' AND ' 			
END 	*/

If @CmbSort = 1 
    set @Sortstring = N' ORDER BY KalaNumber DESC'
    
Else
If @CmbSort = 2 
    set @Sortstring  = N' ORDER BY NameKala1'
Else
If @CmbSort = 3 
    set @Sortstring = N' ORDER BY MandeAnbar DESC'
Else
If @CmbSort = 4 
   set @Sortstring = N' ORDER BY AkharinKharid'
Else
If @CmbSort = 5 
    set @Sortstring =N' ORDER BY UnitPrice1'




set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY FunInfoKalaAll_1.NameKala1+ + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' ''),ROUND(CONVERT(money, drvTra.MaxUnitPriceKh), 2), FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID,  FunInfoKalaAll_1.FieldN1, drvTra.MaxUnitPriceKh, 
                      FunInfoKalaAll_1.NameAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.UnitPrice2,FunInfoKalaAll_1.UnitPrice1, FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, 
                      FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.ID, FunInfoKalaAll_1.Zarib, drvTra.MaxUnitPriceKh,FunInfoKalaAll_1.NameVahed2,FunInfoKalaAll_1.UnitPriceKharid, FunInfoKalaAll_1.vaznKala  '	
     
	if(@OnlyMandedare<>0)
		SET @SQLString =@SQLString + ' HAVING ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3)  <>0 ' 
		
		SET @SQLString=@SQLString+@Sortstring 	  
					
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@OnlyMandedare=@OnlyMandedare,@OnlyActive=@OnlyActive;  		
return END 






GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_ForoshSandogh]    Script Date: 04/11/2020 11:41:23 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_ForoshSandogh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_ForoshSandogh]
GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_ForoshSandogh]    Script Date: 04/11/2020 11:41:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





  CREATE PROCEDURE [dbo].[CQ_ListKala_ForoshSandogh] ( 
					@AID int,
					@OperType int,
					@LinkMasterID int,
					@GoKID int,
					@OnlyMandedare BIT,
					@OnlyActive Bit)           
  AS     
  BEGIN 	
  
  SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(Max);  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max);         	
  DECLARE @Sortstring  NVARCHAR(Max);  
  DECLARE @CmbSort AS INT;
  	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';      
  set @Sortstring=N' ';            	
  SET @ParmDefinition = N'@AID int=null, 								
							  @OperType int=null, 								
							  @LinkMasterID  int=null, 								
							  @GoKID int=null,
		@OnlyMandedare BIT=null,
		@OnlyActive Bit=null ';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
	  	
	SELECT    @OnlyActive= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 2)

	SELECT    @OnlyMandedare= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 3)	  		 
	
	SELECT    @CmbSort= ABS(ISNULL(FieldN1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 10) AND (OPTIONID = 1)	  
	  		 
  SET @SQLString =' SELECT     ID AS NameKalaID, CONVERT(float, KalaNumber) AS KalaNumber, NameKala1, BarCode, Zarib, AID, GOKID, KalaNumber AS [کد کالا], NameKala1 + + N'' '' + ISNULL(note, N'' '') AS [نام کالا], 
                      NameVahed1 AS [نام واحد], 0 AS [مانده انبار], NameAnbar AS [نام انبار], UnitPrice1 AS [قيمت فروش1], 0 AS [آخرين خريد], UnitPrice2 AS [قيمت فروش2], UnitPrice3 AS [قيمت فروش3], UnitPrice4 AS [قيمت فروش4], 
                      UnitPrice5 AS [قيمت فروش5], UnitPrice6 AS [قيمت فروش6], NameVahed2, 0 AS UnitPriceKh, FieldN1, BarCode AS [ بارکد ], NameGoKala1 AS [گروه کالا], NameAnbar, UnitPrice1, 0 AS AkharinKharid, 
                      UnitPrice2, UnitPrice3, UnitPrice4, UnitPrice5, UnitPrice6, Active, NameGoKala1, 0 AS MandeAnbar, CONVERT(Nvarchar(255), KalaNumber) AS CodKala, 0 AS UnitPricekhUser, vaznKala
FROM         dbo.FunInfoKalaAll(@OnlyActive) AS FunInfoKalaAll_1
WHERE     (1 = 1)

' 	
  /* -----------------------نشان دادن کالاهاي فعال */ 
/*
if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه
	*/
               
  /* -----------------------شرط انبار */ 	

		  		
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 		
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	

 /* /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@TextBoxFilter  IS NULL) OR LEN(@TextBoxFilter)>1) 			
BEGIN 		
	set @TextBoxFilter=N'%'+@TextBoxFilter +N'%'
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'  (FunInfoKalaAll_1.NameKala1 Like @TextBoxFilter ) Or (KalaNumber like @TextBoxFilter) '; 				
	set @ParmAndstr=' AND ' 			
END 	
*/

If @CmbSort = 1 
    set @Sortstring = N' ORDER BY KalaNumber DESC'
    
Else
If @CmbSort = 2 
    set @Sortstring  = N' ORDER BY NameKala1'
Else
If @CmbSort = 3 
    set @Sortstring = N' ORDER BY MandeAnbar DESC'
Else
If @CmbSort = 4 
   set @Sortstring = N' ORDER BY AkharinKharid'
Else
If @CmbSort = 5 
    set @Sortstring =N' ORDER BY UnitPrice1'
    


set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY NameKala1 + + N'' '' + ISNULL(note, N'' ''), NameKala1, BarCode, AID, GOKID, FieldN1, NameAnbar, CONVERT(Nvarchar(255), KalaNumber), CONVERT(float, KalaNumber), CONVERT(float, 
                      KalaNumber), NameVahed1, UnitPrice2, UnitPrice1, UnitPrice3, UnitPrice4, UnitPrice5, UnitPrice6, Active, NameGoKala1, ID, Zarib, NameVahed2, vaznKala  '	
     
	if(@OnlyMandedare<>0)
		SET @SQLString =@SQLString 
		
		SET @SQLString=@SQLString+@Sortstring 	  
					
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@OnlyMandedare=@OnlyMandedare,@OnlyActive=@OnlyActive;  		
return END 







GO



/****** Object:  StoredProcedure [dbo].[Q_Forosh_Insert_To_Temp]    Script Date: 04/11/2020 13:00:51 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Forosh_Insert_To_Temp]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Forosh_Insert_To_Temp]
GO



/****** Object:  StoredProcedure [dbo].[Q_Forosh_Insert_To_Temp]    Script Date: 04/11/2020 13:00:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





  CREATE PROCEDURE [dbo].[Q_Forosh_Insert_To_Temp]  		
  
  @USERID int,@FOROSHID int,@LinkToSubRezerve Nvarchar(255), @FlagEditOrEnteshar int /*edit 1 enteshar 0*/
  
  AS 
 
 if(@FlagEditOrEnteshar=1)
	INSERT INTO TRANSACTION2
               (Zarib, PARBes, FieldN7, Maliatkala, Avarez, Ezafat, Tedad1User, Tedad2User, UnitPriceUser, KalaIDChild, ZaribChildUser, Tedad1, Tedad2, TRAID, Date1, UnitPrice, Tedad, DARBed, radif, AID, GOKID, KALAID, 
               TextKalaName, Sharh, FieldN6, Flag2, Flag1, OperType, FieldT1, TextAID, UnitPriceBarFo)
SELECT  [TRANSACTION].zarib, CASE WHEN ZaribChildUser > 0 THEN ROUND((ISNULL([TRANSACTION].Tedad1User, 0) * ISNULL([TRANSACTION].UnitPriceUser, 0)) + ISNULL([TRANSACTION].Maliatkala, 0) 
               + ISNULL([TRANSACTION].Avarez, 0) - ISNULL([TRANSACTION].FieldN7, 0), 2) ELSE ROUND([TRANSACTION].TedadFo * [TRANSACTION].UnitPriceFo + ISNULL([TRANSACTION].Maliatkala, 0) 
               + ISNULL([TRANSACTION].Avarez, 0) - ISNULL([TRANSACTION].FieldN7, 0), 2) END AS Parbes, [TRANSACTION].FieldN7, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Ezafat, 
               ISNULL([TRANSACTION].Tedad1User, [TRANSACTION].TedadFo) AS Tedad1User, ISNULL([TRANSACTION].Tedad2User, 0) AS Tedad2User, ISNULL([TRANSACTION].UnitPriceUser, [TRANSACTION].UnitPriceFo) 
               AS UnitPriceUser, [TRANSACTION].KalaIDChild, [TRANSACTION].ZaribChildUser, CASE WHEN Isnull([TRANSACTION].Tedad1, 0) + isnull([TRANSACTION].Tedad2, 0) 
               = 0 THEN [TRANSACTION].Tedadfo ELSE [TRANSACTION].Tedad1 END AS Expr2, [TRANSACTION].Tedad2, [TRANSACTION].TRAID, @LinkToSubRezerve AS Expr3, [TRANSACTION].UnitPriceFo, 
               [TRANSACTION].TedadFo AS Expr4, CASE WHEN ZaribChildUser > 0 THEN ISNULL([TRANSACTION].Tedad1User, 0) * ISNULL([TRANSACTION].UnitPriceUser, 0) ELSE ISNULL([TRANSACTION].TedadFo, 0) 
               * ISNULL([TRANSACTION].UnitPriceFo, 0) END AS DARBed1, [TRANSACTION].radif, [TRANSACTION].AID, [TRANSACTION].GOKID, [TRANSACTION].KALAID, dbo.KalaName([TRANSACTION].KALAID) 
               + CASE WHEN ZaribChildUser > 0 THEN '(' + NameKala2 + ')' ELSE '' END AS namKala, [TRANSACTION].Sharh, [TRANSACTION].FieldN6, 0 AS Expr1, 1 AS Expr100, 2 AS Expr10, T_NameGoKala.NameGoKala1, 
               T_Anbar.NameAnbar, T_NameKala.vaznKala
FROM     [TRANSACTION] INNER JOIN
               T_Anbar ON [TRANSACTION].AID = T_Anbar.AID INNER JOIN
               T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID INNER JOIN
               T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID LEFT OUTER JOIN
               T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2
WHERE  ([TRANSACTION].FOROSHID = @FOROSHID)
ORDER BY [TRANSACTION].radif
else
 if(@FlagEditOrEnteshar=0)
		INSERT INTO TRANSACTION2
               (Zarib, PARBes, FieldN7, Maliatkala, Avarez, Ezafat, Tedad1User, Tedad2User, UnitPriceUser, KalaIDChild, ZaribChildUser, Tedad1, Tedad2, TRAID, Date1, UnitPrice, Tedad, DARBed, radif, AID, GOKID, KALAID, 
               TextKalaName, Sharh, FieldN6, Flag2, Flag1, OperType, FieldT1, TextAID, UnitPriceBarFo)
SELECT  [TRANSACTION].zarib, CASE WHEN ZaribChildUser > 0 THEN ROUND((ISNULL([TRANSACTION].Tedad1User, 0) * ISNULL([TRANSACTION].UnitPriceUser, 0)) + ISNULL([TRANSACTION].Maliatkala, 0) 
               + ISNULL([TRANSACTION].Avarez, 0) - ISNULL([TRANSACTION].FieldN7, 0), 2) ELSE ROUND([TRANSACTION].TedadFo * [TRANSACTION].UnitPriceFo + ISNULL([TRANSACTION].Maliatkala, 0) 
               + ISNULL([TRANSACTION].Avarez, 0) - ISNULL([TRANSACTION].FieldN7, 0), 2) END AS Parbes, [TRANSACTION].FieldN7, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Ezafat, 
               ISNULL([TRANSACTION].Tedad1User, [TRANSACTION].TedadFo) AS Tedad1User, ISNULL([TRANSACTION].Tedad2User, 0) AS Tedad2User, ISNULL([TRANSACTION].UnitPriceUser, [TRANSACTION].UnitPriceFo) 
               AS UnitPriceUser, [TRANSACTION].KalaIDChild, [TRANSACTION].ZaribChildUser, CASE WHEN Isnull([TRANSACTION].Tedad1, 0) + isnull([TRANSACTION].Tedad2, 0) 
               = 0 THEN [TRANSACTION].Tedadfo ELSE [TRANSACTION].Tedad1 END AS Expr2, [TRANSACTION].Tedad2, [TRANSACTION].TRAID, @LinkToSubRezerve AS Expr3, [TRANSACTION].UnitPriceFo, 
               [TRANSACTION].TedadFo AS Expr4, CASE WHEN ZaribChildUser > 0 THEN ISNULL([TRANSACTION].Tedad1User, 0) * ISNULL([TRANSACTION].UnitPriceUser, 0) ELSE ISNULL([TRANSACTION].TedadFo, 0) 
               * ISNULL([TRANSACTION].UnitPriceFo, 0) END AS DARBed1, [TRANSACTION].radif, [TRANSACTION].AID, [TRANSACTION].GOKID, [TRANSACTION].KALAID, dbo.KalaName([TRANSACTION].KALAID) 
               + CASE WHEN ZaribChildUser > 0 THEN '(' + NameKala2 + ')' ELSE '' END AS namKala, [TRANSACTION].Sharh, [TRANSACTION].FieldN6, 1 AS Expr1, 0 AS Expr100, 2 AS Expr10, T_NameGoKala.NameGoKala1, 
               T_Anbar.NameAnbar, T_NameKala.vaznKala
FROM     [TRANSACTION] INNER JOIN
               T_Anbar ON [TRANSACTION].AID = T_Anbar.AID INNER JOIN
               T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID INNER JOIN
               T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID LEFT OUTER JOIN
               T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2
WHERE  ([TRANSACTION].FOROSHID = @FOROSHID)
ORDER BY [TRANSACTION].radif




GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListNameKala]    Script Date: 04/12/2020 12:46:01 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListNameKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListNameKala]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListNameKala]    Script Date: 04/12/2020 12:46:01 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



  CREATE PROCEDURE [dbo].[QSubPFrm_ListNameKala] AS 
  
SELECT  ID, KalaNumber, DateCreate, DateModified, NameKala1, Active, VAHED1, VAHED2, MinPrice, MaxPrice, MinMogoody, ABS(Zarib) AS Zarib, MaxMogoody, BarCode, note, UnitPrice1, UnitPrice2, UnitPrice3, 
               UnitPrice4, UnitPrice5, UnitPrice6, DarsadAmel1, Currency1, Currency2, Currency3, Currency4, Currency5, Model, Currency6, CONVERT(int, KalaNumber) AS Expr1, NameVahed1 AS NameVahed, NameVahed2, 
               NameGoKala1, CASE WHEN Isnull(FlagNoeKala, 0) = 0 THEN N'کالا' ELSE N'خدمت' END AS NoeKala, vaznKala
FROM     dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1
GROUP BY ID, KalaNumber, DateCreate, DateModified, NameKala1, VAHED1, VAHED2, MinPrice, MaxPrice, MinMogoody, ABS(Zarib), MaxMogoody, BarCode, note, UnitPrice1, UnitPrice2, UnitPrice3, UnitPrice4, UnitPrice5, 
               UnitPrice6, DarsadAmel1, Currency1, Currency2, Currency3, Currency4, Currency5, Model, Currency6, CONVERT(int, KalaNumber), NameVahed1, NameVahed2, NameGoKala1, Active, 
               CASE WHEN Isnull(FlagNoeKala, 0) = 0 THEN N'کالا' ELSE N'خدمت' END, vaznKala
ORDER BY Expr1 DESC



GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListMoinHesab]    Script Date: 04/18/2020 12:48:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListMoinHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListMoinHesab]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListMoinHesab]    Script Date: 04/18/2020 12:48:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



        CREATE PROCEDURE [dbo].[QSubPFrm_ListMoinHesab] AS 
        
SELECT  T_Heasb.GOIDDeh, T_Heasb.KolIDDeh, T_Heasb.HesabidDeh, T_Heasb.ThesabidDeh, dbo.HesabName(T_Heasb.KolIDDeh, T_Heasb.HesabidDeh, T_Heasb.ThesabidDeh) AS NameDeh, T_Heasb.HESABID, 
               T_Heasb.NameHesab, T_Heasb.GOID, T_Heasb.KolID, CONVERT(int, T_Heasb.HESABNumber) AS CodeH, T_KolHesab.NameHesab AS KolName, T_GropHesab.NameGo AS GoroupName, T_Heasb.HESABNumber, 
               T_Heasb.NameZihesab, T_Heasb.NameBakhsh
FROM     T_Heasb LEFT OUTER JOIN
               T_KolHesab ON T_Heasb.KolID = T_KolHesab.KOLID LEFT OUTER JOIN
               T_GropHesab ON T_Heasb.GOID = T_GropHesab.GoID
ORDER BY CodeH


GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListForoosh]    Script Date: 04/28/2020 08:56:46 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListForoosh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListForoosh]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListForoosh]    Script Date: 04/28/2020 08:56:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





          CREATE PROCEDURE [dbo].[QSubPFrm_ListForoosh] 
          
           @UserID INT,
           @StartDateList Nvarchar(10),
           @EndDateList Nvarchar(10)
           
           AS       
             
             
             Begin  

declare @FlagAmin bit;
declare @FlagAdamRoyatSanadAdmin bit;

/*
--declare @StartDateList1 date;
--declare @EndDateList1 date;
  
  --if LEN(@StartDateList)<=8 
		--set @StartDateList=LEFT(@StartDateList,4)+'/'+right(LEFT(@StartDateList,6),2)+'/'+right(@StartDateList,2)

  --if LEN(@EndDateList)<=8 
		--set @EndDateList=LEFT(@EndDateList,4)+'/'+right(LEFT(@EndDateList,6),2)+'/'+right(@EndDateList,2)
			
		--set @StartDateList1=dbo.PersianToGregorian(@StartDateList)	
		
		--set @EndDateList1=dbo.PersianToGregorian(@EndDateList)	*/
		


  if (@UserID<>3)
			SELECT     @FlagAmin=FlagAmin,@FlagAdamRoyatSanadAdmin=FlagAdamRoyatSanadAdmin
			FROM         AC_Setting.dbo.T_yUsers
			WHERE     (USERID = @UserID)
  else
		begin
			set @FlagAmin=1
			set @FlagAdamRoyatSanadAdmin=0
		end
		
		/* dbo.sgfn_DateToShamsiDate(CONVERT(date, T_Forosh.DateCreate)) AS DateCreate,CONVERT(VARCHAR(8), T_Forosh.DateCreate, 108) AS timeCreate
		dbo.sgfn_DateToShamsiDate(CONVERT(date, T_Forosh.DateModified)) AS DateModified, CONVERT(VARCHAR(8), T_Forosh.DateModified, 108) AS TimeEdit
		 */
		
  if(@FlagAdamRoyatSanadAdmin=0)
SELECT  T_Forosh.DateCreate, T_Forosh.DateModified, T_Forosh.FOROSHID, T_Forosh.HesabID, T_Forosh.GOID, T_Forosh.KOLID, T_Forosh.MOINID, T_Forosh.THESABID, T_Forosh.TextName, T_Forosh.TextName1, 
               T_Forosh.TextNameT, T_Forosh.NumberKH, T_Forosh.DateFo, T_Forosh.Takhfif, T_Forosh.TextShSA, T_Forosh.NumSID2, T_Forosh.Sharh, T_Forosh.FieldN1, T_Forosh.FieldN2, T_Forosh.FieldN3, 
               T_Forosh.FieldN4, T_Forosh.FieldT1, T_Forosh.FieldT2, T_Forosh.FieldT3, T_Forosh.FieldT3 AS Expr1, T_Forosh.FieldT4, T_Forosh.Flag1, T_Forosh.Flag2, T_Forosh.Flag3, T_Forosh.Flag4, T_Forosh.Flag5, 
               T_Forosh.Flag6, T_Forosh.Flag7, T_Forosh.Flag8, T_Forosh.ForoshNumbr, T_Forosh.PrimarySaid2, T_Forosh.THESABIDAmel, T_SA2.SAID2Number, dbo.HesabName(T_Forosh.KOLID, T_Forosh.HesabID, 
               T_Forosh.THESABID) AS namehesab, T_Forosh.dateSettlement, F_MabHazine_Foroosh_1.MabHazine, T_Forosh.Maliyat, T_Forosh.Avarez, T_Forosh.KeraiehHaml, T_Forosh.keraiehbargiri
FROM     T_Forosh INNER JOIN
               T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
               dbo.F_MabHazine_Foroosh() AS F_MabHazine_Foroosh_1 ON T_Forosh.FOROSHID = F_MabHazine_Foroosh_1.FactID
WHERE  (T_Forosh.DateFo BETWEEN @StartDateList AND @EndDateList)
ORDER BY T_Forosh.DateFo DESC, T_Forosh.ForoshNumbr DESC

else

SELECT  T_Forosh.DateCreate, T_Forosh.DateModified, T_Forosh.FOROSHID, T_Forosh.HesabID, T_Forosh.GOID, T_Forosh.KOLID, T_Forosh.MOINID, T_Forosh.THESABID, T_Forosh.TextName, T_Forosh.TextName1, 
               T_Forosh.TextNameT, T_Forosh.NumberKH, T_Forosh.DateFo, T_Forosh.Takhfif, T_Forosh.TextShSA, T_Forosh.NumSID2, T_Forosh.Sharh, T_Forosh.FieldN1, T_Forosh.FieldN2, T_Forosh.FieldN3, 
               T_Forosh.FieldN4, T_Forosh.FieldT1, T_Forosh.FieldT2, T_Forosh.FieldT3, T_Forosh.FieldT3 AS Expr1, T_Forosh.FieldT4, T_Forosh.Flag1, T_Forosh.Flag2, T_Forosh.Flag3, T_Forosh.Flag4, T_Forosh.Flag5, 
               T_Forosh.Flag6, T_Forosh.Flag7, T_Forosh.Flag8, T_Forosh.ForoshNumbr, T_Forosh.PrimarySaid2, T_Forosh.THESABIDAmel, T_SA2.SAID2Number, dbo.HesabName(T_Forosh.KOLID, T_Forosh.HesabID, 
               T_Forosh.THESABID) AS namehesab, T_Forosh.dateSettlement, F_MabHazine_Foroosh_1.MabHazine, T_Forosh.Maliyat, T_Forosh.Avarez, T_Forosh.KeraiehHaml, T_Forosh.keraiehbargiri
FROM     T_Forosh INNER JOIN
               T_SA2 ON T_Forosh.PrimarySaid2 = T_SA2.SAID2 LEFT OUTER JOIN
               dbo.F_MabHazine_Foroosh() AS F_MabHazine_Foroosh_1 ON T_Forosh.FOROSHID = F_MabHazine_Foroosh_1.FactID
WHERE  (NOT (ISNULL(T_Forosh.userCreate, - 10) IN
                   (SELECT  USERID
                    FROM     AC_Setting.dbo.T_yUsers
                    WHERE  (USERID = 3) OR
                                   (FlagAmin <> 0)))) AND (T_Forosh.DateFo BETWEEN @StartDateList AND @EndDateList)
ORDER BY T_Forosh.DateFo DESC, T_Forosh.ForoshNumbr DESC

end





GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Rep1]    Script Date: 04/28/2020 09:08:40 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Rep1]    Script Date: 04/28/2020 09:08:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_Rep1]
		
		@Userid INT,	
		@FOROSHID INT,
		@GroupID NVARCHAR(MAX), 
		@KOLID NVARCHAR(MAX), 
		@HesabidID NVARCHAR(MAX), 
		@THesabidID NVARCHAR(MAX), 
		@THesabidIDSath2 NVARCHAR(MAX), 
		@StarteDate NVARCHAR(10),
		@ENDDate NVARCHAR(10),
		@KHARIDID1 INT,
		@KHARIDID2 INT

AS
BEGIN
	
IF (@FOROSHID>0) 	
	
SELECT  [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
               CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
               T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, T_Forosh.KOLID AS CodeKol, 
               T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, [TRANSACTION].FieldN7 AS Takhfif, T_Forosh.DateFo AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Forosh.Sharh AS TozihatFactor, 
               FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, 
               ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, T_Forosh.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, FunInfoKalaAll_1.NameKala1, T_Forosh.FOROSHID, DERIVEDTBL.mande AS MandeHesab, 
               ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, 
               T_Forosh.ForoshNumbr + FunInfoKalaAll_1.AIDNumber AS CodeAnbar, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddressAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
               > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
               > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Tedad1User, 
               [TRANSACTION].Tedad2User, [TRANSACTION].UnitPriceUser, [TRANSACTION].ZaribChildUser, [TRANSACTION].KalaIDChild, 
               CASE WHEN ZaribChildUser > 0 THEN '(' + T_RelateKala.NameKala2 + ')' ELSE '' END AS NameKalaChild, T_Forosh.KeraiehHaml, T_Forosh.keraiehbargiri
FROM     T_KolHesab RIGHT OUTER JOIN
               T_T_Heasb RIGHT OUTER JOIN
               T_Heasb RIGHT OUTER JOIN
               dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
               [TRANSACTION] INNER JOIN
                   (SELECT  ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID, GOID, ThesabIDSath2, THESABID, HesabID, 
                                   ISNULL(KeraiehHaml, 0) AS KeraiehHaml, ISNULL(keraiehbargiri, 0) AS keraiehbargiri
                    FROM     T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID ON FunInfoKalaAll_1.ID = [TRANSACTION].KALAID AND 
               FunInfoKalaAll_1.AID = [TRANSACTION].AID LEFT OUTER JOIN
               T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2 LEFT OUTER JOIN
                   (SELECT  SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) AS THESAB
                    FROM     [TRANSACTION] AS TRANSACTION_1
                    WHERE  (NOT (SAID2 IS NULL))
                    GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL ON T_Forosh.KOLID = DERIVEDTBL.KOLID AND T_Forosh.Hesab = DERIVEDTBL.HESAB AND 
               T_Forosh.THesab = DERIVEDTBL.THESAB ON T_Heasb.HESABID = T_Forosh.Hesab ON T_T_Heasb.THESABID = T_Forosh.THesab ON T_KolHesab.KOLID = T_Forosh.KOLID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 2) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Forosh.FOROSHID = TBL.FactID
WHERE  (T_Forosh.FOROSHID = @FOROSHID)

ELSE
	BEGIN

			SET NOCOUNT ON;

			Declare @SQLQuery AS NVarchar(MAX)
			Declare @ParamDefinition AS NVarchar(MAX) 
			DECLARE @OrderBy AS NVARCHAR(MAX)
			DECLARE @GROUPBy AS NVARCHAR(MAX)
		    
			IF (@StarteDate IS NULL)
					SET @StarteDate='0'
		    
				IF (@ENDDate IS NULL)
					SET @ENDDate='99999999'
						SET @SQLQuery = '  SELECT     [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
                      CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, 
                      T_Forosh.KOLID AS CodeKol, T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, [TRANSACTION].FieldN7 AS Takhfif, T_Forosh.DateFo AS Tarikh, 
                      [TRANSACTION].Sharh AS SharhRadif, T_Forosh.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, 
                      FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, 
                      T_Forosh.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, 
                      T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, 
                      FunInfoKalaAll_1.NameKala1, T_Forosh.FOROSHID, DERIVEDTBL.mande AS MandeHesab, ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) 
                      = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, T_Forosh.ForoshNumbr + FunInfoKalaAll_1.AIDNumber AS CodeAnbar, FunInfoKalaAll_1.NameAnbar, 
                      FunInfoKalaAll_1.SharhAnbar AS AddressAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad2 END AS Tedad2, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Tedad1User, [TRANSACTION].Tedad2User, [TRANSACTION].UnitPriceUser, 
                      [TRANSACTION].ZaribChildUser, [TRANSACTION].KalaIDChild, CASE WHEN ZaribChildUser > 0 THEN ''('' + T_RelateKala.NameKala2 + '')'' ELSE '''' END AS NameKalaChild, T_Forosh.KeraiehHaml, T_Forosh.keraiehbargiri
FROM         T_KolHesab RIGHT OUTER JOIN
                      T_T_Heasb RIGHT OUTER JOIN
                      T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                          (SELECT  ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID, GOID, ThesabIDSath2, THESABID, HesabID, ISNULL(KeraiehHaml, 0) 
               AS KeraiehHaml, ISNULL(keraiehbargiri, 0) AS keraiehbargiri
FROM     T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID ON FunInfoKalaAll_1.ID = [TRANSACTION].KALAID AND 
                      FunInfoKalaAll_1.AID = [TRANSACTION].AID LEFT OUTER JOIN
                      T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2 LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) 
                                                   AS THESAB
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL ON T_Forosh.KOLID = DERIVEDTBL.KOLID AND T_Forosh.Hesab = DERIVEDTBL.HESAB AND 
                      T_Forosh.THesab = DERIVEDTBL.THESAB ON T_Heasb.HESABID = T_Forosh.Hesab ON T_T_Heasb.THESABID = T_Forosh.THesab ON T_KolHesab.KOLID = T_Forosh.KOLID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 2) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Forosh.FOROSHID = TBL.FactID
WHERE     (1 = 1)
		'


			SET @GROUPBy = '  '
			SET @OrderBy = '  '

			Set @ParamDefinition = '@UserID INT,
									@GroupID NVARCHAR(MAX),
									@KOLID NVARCHAR(MAX), 
									@HesabidID NVARCHAR(MAX), 
									@THesabidID NVARCHAR(MAX), 
									@THesabidIDSath2 NVARCHAR(MAX), 
									@StarteDate NVARCHAR(MAX), 
									@ENDDate NVARCHAR(MAX),
									@KHARIDID1 INT,
									@KHARIDID2 INT '  	
			
			/* Execute the Transact-SQL String with all parameter value's 	   
			   Using sp_executesql Command */
			   
		If (@GroupID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
	         
		If (@KOLID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.KOLID IN('+ @KOLID +'))'
	    
		If (@HesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.HESABID IN('+ @HesabidID +') )'

		If (@THesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.THESABID IN('+ @THesabidID +') )'

		If (@THesabidIDSath2 Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'    
        
        
       If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))
          Set @SQLQuery = @SQLQuery + ' And  T_Forosh.ForoshNumbr Between @KHARIDID1 And @KHARIDID2' 
        
        
        Set @SQLQuery = @SQLQuery + ' And  T_Forosh.DateFo Between @StarteDate And @ENDDate'
        			   
			   SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
			Execute sp_Executesql   @SQLQuery, 
									@ParamDefinition,
									@UserID ,
									@GroupID,
									@KOLID , 
									@HesabidID , 
									@THesabidID , 
									@THesabidIDSath2 , 
									@StarteDate , 
									@ENDDate,
									@KHARIDID1,
									@KHARIDID2


			If @@ERROR <> 0 GoTo ErrorHandler
			Set NoCount OFF
		  
			ErrorHandler:
				Return(@@ERROR)
	END		
END








GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Rep1]    Script Date: 04/28/2020 10:59:13 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Rep1]    Script Date: 04/28/2020 10:59:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_Rep1]
		
		@Userid INT,	
		@FOROSHID INT,
		@GroupID NVARCHAR(MAX), 
		@KOLID NVARCHAR(MAX), 
		@HesabidID NVARCHAR(MAX), 
		@THesabidID NVARCHAR(MAX), 
		@THesabidIDSath2 NVARCHAR(MAX), 
		@StarteDate NVARCHAR(10),
		@ENDDate NVARCHAR(10),
		@KHARIDID1 INT,
		@KHARIDID2 INT

AS
BEGIN
	
IF (@FOROSHID>0) 	
	
SELECT  [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
               CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
               T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, T_Forosh.KOLID AS CodeKol, 
               T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, [TRANSACTION].FieldN7 AS Takhfif, T_Forosh.DateFo AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Forosh.Sharh AS TozihatFactor, 
               FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, 
               ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, T_Forosh.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, FunInfoKalaAll_1.NameKala1, T_Forosh.FOROSHID, DERIVEDTBL.mande AS MandeHesab, 
               ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, 
               T_Forosh.ForoshNumbr + FunInfoKalaAll_1.AIDNumber AS CodeAnbar, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddressAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
               > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
               > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Tedad1User, 
               [TRANSACTION].Tedad2User, [TRANSACTION].UnitPriceUser, [TRANSACTION].ZaribChildUser, [TRANSACTION].KalaIDChild, 
               CASE WHEN ZaribChildUser > 0 THEN '(' + T_RelateKala.NameKala2 + ')' ELSE '' END AS NameKalaChild, T_Forosh.KeraiehHaml, T_Forosh.keraiehbargiri, T_Forosh.TimeSabt
FROM     T_KolHesab RIGHT OUTER JOIN
               T_T_Heasb RIGHT OUTER JOIN
               T_Heasb RIGHT OUTER JOIN
               dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
               [TRANSACTION] INNER JOIN
                   (SELECT  ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID, GOID, ThesabIDSath2, THESABID, HesabID, 
                                   ISNULL(KeraiehHaml, 0) AS KeraiehHaml, ISNULL(keraiehbargiri, 0) AS keraiehbargiri, CONVERT(VARCHAR(8), DateCreate, 108) AS TimeSabt
                    FROM     T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID ON FunInfoKalaAll_1.ID = [TRANSACTION].KALAID AND 
               FunInfoKalaAll_1.AID = [TRANSACTION].AID LEFT OUTER JOIN
               T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2 LEFT OUTER JOIN
                   (SELECT  SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) AS THESAB
                    FROM     [TRANSACTION] AS TRANSACTION_1
                    WHERE  (NOT (SAID2 IS NULL))
                    GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL ON T_Forosh.KOLID = DERIVEDTBL.KOLID AND T_Forosh.Hesab = DERIVEDTBL.HESAB AND 
               T_Forosh.THesab = DERIVEDTBL.THESAB ON T_Heasb.HESABID = T_Forosh.Hesab ON T_T_Heasb.THESABID = T_Forosh.THesab ON T_KolHesab.KOLID = T_Forosh.KOLID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 2) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Forosh.FOROSHID = TBL.FactID
WHERE  (T_Forosh.FOROSHID = @FOROSHID)

ELSE
	BEGIN

			SET NOCOUNT ON;

			Declare @SQLQuery AS NVarchar(MAX)
			Declare @ParamDefinition AS NVarchar(MAX) 
			DECLARE @OrderBy AS NVARCHAR(MAX)
			DECLARE @GROUPBy AS NVARCHAR(MAX)
		    
			IF (@StarteDate IS NULL)
					SET @StarteDate='0'
		    
				IF (@ENDDate IS NULL)
					SET @ENDDate='99999999'
						SET @SQLQuery = '  SELECT     [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
                      CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, 
                      T_Forosh.KOLID AS CodeKol, T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, [TRANSACTION].FieldN7 AS Takhfif, T_Forosh.DateFo AS Tarikh, 
                      [TRANSACTION].Sharh AS SharhRadif, T_Forosh.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, 
                      FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, 
                      T_Forosh.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, 
                      T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, 
                      FunInfoKalaAll_1.NameKala1, T_Forosh.FOROSHID, DERIVEDTBL.mande AS MandeHesab, ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) 
                      = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, T_Forosh.ForoshNumbr + FunInfoKalaAll_1.AIDNumber AS CodeAnbar, FunInfoKalaAll_1.NameAnbar, 
                      FunInfoKalaAll_1.SharhAnbar AS AddressAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad2 END AS Tedad2, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Tedad1User, [TRANSACTION].Tedad2User, [TRANSACTION].UnitPriceUser, 
                      [TRANSACTION].ZaribChildUser, [TRANSACTION].KalaIDChild, CASE WHEN ZaribChildUser > 0 THEN ''('' + T_RelateKala.NameKala2 + '')'' ELSE '''' END AS NameKalaChild, T_Forosh.KeraiehHaml, T_Forosh.keraiehbargiri, T_Forosh.TimeSabt
FROM         T_KolHesab RIGHT OUTER JOIN
                      T_T_Heasb RIGHT OUTER JOIN
                      T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                          (SELECT  ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID, GOID, ThesabIDSath2, THESABID, HesabID, ISNULL(KeraiehHaml, 0) 
               AS KeraiehHaml, ISNULL(keraiehbargiri, 0) AS keraiehbargiri, CONVERT(VARCHAR(8), DateCreate, 108) AS TimeSabt
FROM     T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID ON FunInfoKalaAll_1.ID = [TRANSACTION].KALAID AND 
                      FunInfoKalaAll_1.AID = [TRANSACTION].AID LEFT OUTER JOIN
                      T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2 LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) 
                                                   AS THESAB
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL ON T_Forosh.KOLID = DERIVEDTBL.KOLID AND T_Forosh.Hesab = DERIVEDTBL.HESAB AND 
                      T_Forosh.THesab = DERIVEDTBL.THESAB ON T_Heasb.HESABID = T_Forosh.Hesab ON T_T_Heasb.THESABID = T_Forosh.THesab ON T_KolHesab.KOLID = T_Forosh.KOLID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 2) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Forosh.FOROSHID = TBL.FactID
WHERE     (1 = 1)
		'


			SET @GROUPBy = '  '
			SET @OrderBy = '  '

			Set @ParamDefinition = '@UserID INT,
									@GroupID NVARCHAR(MAX),
									@KOLID NVARCHAR(MAX), 
									@HesabidID NVARCHAR(MAX), 
									@THesabidID NVARCHAR(MAX), 
									@THesabidIDSath2 NVARCHAR(MAX), 
									@StarteDate NVARCHAR(MAX), 
									@ENDDate NVARCHAR(MAX),
									@KHARIDID1 INT,
									@KHARIDID2 INT '  	
			
			/* Execute the Transact-SQL String with all parameter value's 	   
			   Using sp_executesql Command */
			   
		If (@GroupID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
	         
		If (@KOLID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.KOLID IN('+ @KOLID +'))'
	    
		If (@HesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.HESABID IN('+ @HesabidID +') )'

		If (@THesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.THESABID IN('+ @THesabidID +') )'

		If (@THesabidIDSath2 Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'    
        
        
       If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))
          Set @SQLQuery = @SQLQuery + ' And  T_Forosh.ForoshNumbr Between @KHARIDID1 And @KHARIDID2' 
        
        
        Set @SQLQuery = @SQLQuery + ' And  T_Forosh.DateFo Between @StarteDate And @ENDDate'
        			   
			   SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
			Execute sp_Executesql   @SQLQuery, 
									@ParamDefinition,
									@UserID ,
									@GroupID,
									@KOLID , 
									@HesabidID , 
									@THesabidID , 
									@THesabidIDSath2 , 
									@StarteDate , 
									@ENDDate,
									@KHARIDID1,
									@KHARIDID2


			If @@ERROR <> 0 GoTo ErrorHandler
			Set NoCount OFF
		  
			ErrorHandler:
				Return(@@ERROR)
	END		
END









GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_NameKala_ChengeInformatoinkala]    Script Date: 05/12/2020 14:28:49 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_NameKala_ChengeInformatoinkala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_NameKala_ChengeInformatoinkala]
GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_NameKala_ChengeInformatoinkala]    Script Date: 05/12/2020 14:28:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[Op_Paye_NameKala_ChengeInformatoinkala] 

@UserID INT,
@GoKID NVARCHAR(Max),
@AID  NVARCHAR(Max),
@KalaID NVARCHAR(Max)
AS





BEGIN
		
	Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
	set @SQLQuery='SELECT  T_NameKala.*, CONVERT(money, ISNULL(vaznKala, 0) * (ISNULL(UnitPriceKharid, 0) + ISNULL(HazineKala, 0)) + (ISNULL(DarsadSoodKala, 0) * ISNULL(vaznKala, 0)) * (ISNULL(UnitPriceKharid, 0) 
               + ISNULL(HazineKala, 0))) AS GhifoFinal
FROM     T_NameKala
WHERE  (1 = 1)
ORDER BY NameKala1
 '
         
    If (@GoKID Is Not Null)
         Set @SQLQuery = @SQLQuery +' And (GoKID IN ('+ @GoKID +'))'
    If (@AID Is Not Null)
         Set @SQLQuery = @SQLQuery +' And (AID IN ('+ @AID +'))'     
          
    If (@KalaID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (ID IN ('+ @KalaID +'))'   
         
	    Set @ParamDefinition = '@UserID INT,
								@GOKID NVARCHAR(Max),
								@AID  NVARCHAR(Max),
								@KalaID NVARCHAR(Max)'
								  
		    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GOKID,
							@AID  , 
							@KalaID 
													                           






END




GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16]    Script Date: 05/12/2020 15:24:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16]
GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16]    Script Date: 05/12/2020 15:24:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






  CREATE PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16] ( 
						@AID int,
						@OperType int,
						@LinkMasterID int,
						@GoKID int,
						@OnlyMandedare BIT,
						@OnlyActive Bit)           
  AS     
  BEGIN 	
  
  SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(Max);  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max);         	
  DECLARE @Sortstring  NVARCHAR(Max);  
  DECLARE @CmbSort AS INT;
  	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';      
  set @Sortstring=N' ';            	
  SET @ParmDefinition = N'@AID int=null, 								
							  @OperType int=null, 								
							  @LinkMasterID  int=null, 								
							  @GoKID int=null,
		@OnlyMandedare BIT=null,
		@OnlyActive Bit=null ';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
	  	
	SELECT    @OnlyActive= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 2)

	SELECT    @OnlyMandedare= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 3)	  	
	
	SELECT    @CmbSort= ABS(ISNULL(FieldN1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 10) AND (OPTIONID = 1)	  
		 
	  		 
  SET @SQLString =' SELECT     FunInfoKalaAll_1.ID AS NameKalaID, CONVERT(float, FunInfoKalaAll_1.KalaNumber) AS KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.Zarib, 
                      FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.KalaNumber AS [کد کالا], FunInfoKalaAll_1.NameKala1+ + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' '')  AS [نام کالا], FunInfoKalaAll_1.NameVahed1 AS [نام واحد], 
                      ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) AS [مانده انبار], FunInfoKalaAll_1.NameAnbar AS [نام انبار], CONVERT(MONEY, MAX(FunInfoKalaAll_1.DarsadSoodKala * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 
               0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala)) AS [قيمت فروش1], ROUND(CONVERT(money, 
                      drvTra.MaxUnitPriceKh), 2) AS [آخرين خريد], FunInfoKalaAll_1.UnitPrice2 AS [قيمت فروش2], FunInfoKalaAll_1.UnitPrice3 AS [قيمت فروش3], FunInfoKalaAll_1.UnitPrice4 AS [قيمت فروش4], 
                      FunInfoKalaAll_1.UnitPrice5 AS [قيمت فروش5], FunInfoKalaAll_1.UnitPrice6 AS [قيمت فروش6], FunInfoKalaAll_1.NameVahed2, drvTra.MaxUnitPriceKh AS UnitPriceKh, FunInfoKalaAll_1.FieldN1, 
                      FunInfoKalaAll_1.BarCode AS [ بارکد ], FunInfoKalaAll_1.NameGoKala1 AS [گروه کالا], FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.UnitPrice1, drvTra.MaxUnitPriceKh AS AkharinKharid, 
                      FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, FunInfoKalaAll_1.Active, 
                      FunInfoKalaAll_1.NameGoKala1, ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3)  AS MandeAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber) AS CodKala,FunInfoKalaAll_1.UnitPriceKharid as UnitPricekhUser , FunInfoKalaAll_1.vaznKala 
FROM         dbo.FunInfoKalaAll(@OnlyActive) AS FunInfoKalaAll_1 INNER JOIN
                      dbo.Fun_CQ_ListKala_Sandoogh_Analyz() AS drvTra ON FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID 
WHERE     (1 = 1)

' 	



/*FunInfoKalaAll_1.AID = drvTra.AID and FunInfoKalaAll_1.ID = drvTra.KALAID */

/* FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID  */

  /* -----------------------نشان دادن کالاهاي فعال */ 
/*
if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه
	*/
               
  /* -----------------------شرط انبار */ 	

		  		
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 		
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	

 /* /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@TextBoxFilter  IS NULL) OR LEN(@TextBoxFilter)>1) 			
BEGIN 		
	set @TextBoxFilter=N'%'+@TextBoxFilter +N'%'
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'  (FunInfoKalaAll_1.NameKala1 Like @TextBoxFilter ) Or (KalaNumber like @TextBoxFilter) '; 				
	set @ParmAndstr=' AND ' 			
END 	*/

If @CmbSort = 1 
    set @Sortstring = N' ORDER BY KalaNumber DESC'
    
Else
If @CmbSort = 2 
    set @Sortstring  = N' ORDER BY NameKala1'
Else
If @CmbSort = 3 
    set @Sortstring = N' ORDER BY MandeAnbar DESC'
Else
If @CmbSort = 4 
   set @Sortstring = N' ORDER BY AkharinKharid'
Else
If @CmbSort = 5 
    set @Sortstring =N' ORDER BY UnitPrice1'




set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY FunInfoKalaAll_1.NameKala1+ + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' ''),ROUND(CONVERT(money, drvTra.MaxUnitPriceKh), 2), FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID,  FunInfoKalaAll_1.FieldN1, drvTra.MaxUnitPriceKh, 
                      FunInfoKalaAll_1.NameAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.UnitPrice2,FunInfoKalaAll_1.UnitPrice1, FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, 
                      FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.ID, FunInfoKalaAll_1.Zarib, drvTra.MaxUnitPriceKh,FunInfoKalaAll_1.NameVahed2,FunInfoKalaAll_1.UnitPriceKharid, FunInfoKalaAll_1.vaznKala  '	
     
	if(@OnlyMandedare<>0)
		SET @SQLString =@SQLString + ' HAVING ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3)  <>0 ' 
		
		SET @SQLString=@SQLString+@Sortstring 	  
					
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@OnlyMandedare=@OnlyMandedare,@OnlyActive=@OnlyActive;  		
return END 







GO




/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16]    Script Date: 05/18/2020 14:04:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16]
GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16]    Script Date: 05/18/2020 14:04:32 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






  CREATE PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16] ( 
						@AID int,
						@OperType int,
						@LinkMasterID int,
						@GoKID int,
						@OnlyMandedare BIT,
						@OnlyActive Bit)           
  AS     
  BEGIN 	
  
  SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(Max);  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max);         	
  DECLARE @Sortstring  NVARCHAR(Max);  
  DECLARE @CmbSort AS INT;
  	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';      
  set @Sortstring=N' ';            	
  SET @ParmDefinition = N'@AID int=null, 								
							  @OperType int=null, 								
							  @LinkMasterID  int=null, 								
							  @GoKID int=null,
		@OnlyMandedare BIT=null,
		@OnlyActive Bit=null ';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
	  	
	SELECT    @OnlyActive= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 2)

	SELECT    @OnlyMandedare= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 3)	  	
	
	SELECT    @CmbSort= ABS(ISNULL(FieldN1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 10) AND (OPTIONID = 1)	  
		 
	  		 
  SET @SQLString =' SELECT     FunInfoKalaAll_1.ID AS NameKalaID, CONVERT(float, FunInfoKalaAll_1.KalaNumber) AS KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.Zarib, 
                      FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.KalaNumber AS [کد کالا], FunInfoKalaAll_1.NameKala1+ + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' '')  AS [نام کالا], FunInfoKalaAll_1.NameVahed1 AS [نام واحد], 
                      ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) AS [مانده انبار], FunInfoKalaAll_1.NameAnbar AS [نام انبار], CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 
               0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala)) AS [قيمت فروش1], ROUND(CONVERT(money, 
                      drvTra.MaxUnitPriceKh), 2) AS [آخرين خريد], FunInfoKalaAll_1.UnitPrice2 AS [قيمت فروش2], FunInfoKalaAll_1.UnitPrice3 AS [قيمت فروش3], FunInfoKalaAll_1.UnitPrice4 AS [قيمت فروش4], 
                      FunInfoKalaAll_1.UnitPrice5 AS [قيمت فروش5], FunInfoKalaAll_1.UnitPrice6 AS [قيمت فروش6], FunInfoKalaAll_1.NameVahed2, drvTra.MaxUnitPriceKh AS UnitPriceKh, FunInfoKalaAll_1.FieldN1, 
                      FunInfoKalaAll_1.BarCode AS [ بارکد ], FunInfoKalaAll_1.NameGoKala1 AS [گروه کالا], FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.UnitPrice1, drvTra.MaxUnitPriceKh AS AkharinKharid, 
                      FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, FunInfoKalaAll_1.Active, 
                      FunInfoKalaAll_1.NameGoKala1, ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3)  AS MandeAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber) AS CodKala,FunInfoKalaAll_1.UnitPriceKharid as UnitPricekhUser , FunInfoKalaAll_1.vaznKala 
FROM         dbo.FunInfoKalaAll(@OnlyActive) AS FunInfoKalaAll_1 INNER JOIN
                      dbo.Fun_CQ_ListKala_Sandoogh_Analyz() AS drvTra ON FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID 
WHERE     (1 = 1)

' 	



/*FunInfoKalaAll_1.AID = drvTra.AID and FunInfoKalaAll_1.ID = drvTra.KALAID */

/* FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID  */

  /* -----------------------نشان دادن کالاهاي فعال */ 
/*
if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه
	*/
               
  /* -----------------------شرط انبار */ 	

		  		
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 		
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	

 /* /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@TextBoxFilter  IS NULL) OR LEN(@TextBoxFilter)>1) 			
BEGIN 		
	set @TextBoxFilter=N'%'+@TextBoxFilter +N'%'
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'  (FunInfoKalaAll_1.NameKala1 Like @TextBoxFilter ) Or (KalaNumber like @TextBoxFilter) '; 				
	set @ParmAndstr=' AND ' 			
END 	*/

If @CmbSort = 1 
    set @Sortstring = N' ORDER BY KalaNumber DESC'
    
Else
If @CmbSort = 2 
    set @Sortstring  = N' ORDER BY NameKala1'
Else
If @CmbSort = 3 
    set @Sortstring = N' ORDER BY MandeAnbar DESC'
Else
If @CmbSort = 4 
   set @Sortstring = N' ORDER BY AkharinKharid'
Else
If @CmbSort = 5 
    set @Sortstring =N' ORDER BY UnitPrice1'




set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY FunInfoKalaAll_1.NameKala1+ + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' ''),ROUND(CONVERT(money, drvTra.MaxUnitPriceKh), 2), FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID,  FunInfoKalaAll_1.FieldN1, drvTra.MaxUnitPriceKh, 
                      FunInfoKalaAll_1.NameAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.UnitPrice2,FunInfoKalaAll_1.UnitPrice1, FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, 
                      FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.ID, FunInfoKalaAll_1.Zarib, drvTra.MaxUnitPriceKh,FunInfoKalaAll_1.NameVahed2,FunInfoKalaAll_1.UnitPriceKharid, FunInfoKalaAll_1.vaznKala  '	
     
	if(@OnlyMandedare<>0)
		SET @SQLString =@SQLString + ' HAVING ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3)  <>0 ' 
		
		SET @SQLString=@SQLString+@Sortstring 	  
					
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@OnlyMandedare=@OnlyMandedare,@OnlyActive=@OnlyActive;  		
return END 







GO





/****** Object:  StoredProcedure [dbo].[CQ_ListKala_ForoshSandogh_SF16]    Script Date: 05/18/2020 14:09:00 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_ForoshSandogh_SF16]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_ForoshSandogh_SF16]
GO


/****** Object:  StoredProcedure [dbo].[CQ_ListKala_ForoshSandogh_SF16]    Script Date: 05/18/2020 14:09:00 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






  CREATE PROCEDURE [dbo].[CQ_ListKala_ForoshSandogh_SF16] ( 
					@AID int,
					@OperType int,
					@LinkMasterID int,
					@GoKID int,
					@OnlyMandedare BIT,
					@OnlyActive Bit)           
  AS     
  BEGIN 	
  
  SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(Max);  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max);         	
  DECLARE @Sortstring  NVARCHAR(Max);  
  DECLARE @CmbSort AS INT;
  	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';      
  set @Sortstring=N' ';            	
  SET @ParmDefinition = N'@AID int=null, 								
							  @OperType int=null, 								
							  @LinkMasterID  int=null, 								
							  @GoKID int=null,
		@OnlyMandedare BIT=null,
		@OnlyActive Bit=null ';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
	  	
	SELECT    @OnlyActive= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 2)

	SELECT    @OnlyMandedare= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 3)	  		 
	
	SELECT    @CmbSort= ABS(ISNULL(FieldN1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 10) AND (OPTIONID = 1)	  
	  		 
  SET @SQLString =' SELECT     ID AS NameKalaID, CONVERT(float, KalaNumber) AS KalaNumber, NameKala1, BarCode, Zarib, AID, GOKID, KalaNumber AS [کد کالا], NameKala1 + + N'' '' + ISNULL(note, N'' '') AS [نام کالا], 
                      NameVahed1 AS [نام واحد], 0 AS [مانده انبار], NameAnbar AS [نام انبار],
                       CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 
               0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala)) AS [قيمت فروش1]
                       , 0 AS [آخرين خريد], UnitPrice2 AS [قيمت فروش2], UnitPrice3 AS [قيمت فروش3], UnitPrice4 AS [قيمت فروش4], 
                      UnitPrice5 AS [قيمت فروش5], UnitPrice6 AS [قيمت فروش6], NameVahed2, 0 AS UnitPriceKh, FieldN1, BarCode AS [ بارکد ], NameGoKala1 AS [گروه کالا], NameAnbar, UnitPrice1, 0 AS AkharinKharid, 
                      UnitPrice2, UnitPrice3, UnitPrice4, UnitPrice5, UnitPrice6, Active, NameGoKala1, 0 AS MandeAnbar, CONVERT(Nvarchar(255), KalaNumber) AS CodKala, 0 AS UnitPricekhUser, vaznKala
FROM         dbo.FunInfoKalaAll(@OnlyActive) AS FunInfoKalaAll_1
WHERE     (1 = 1)

' 	
  /* -----------------------نشان دادن کالاهاي فعال */ 
/*
if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه
	*/
               
  /* -----------------------شرط انبار */ 	

		  		
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 		
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	

 /* /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@TextBoxFilter  IS NULL) OR LEN(@TextBoxFilter)>1) 			
BEGIN 		
	set @TextBoxFilter=N'%'+@TextBoxFilter +N'%'
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'  (FunInfoKalaAll_1.NameKala1 Like @TextBoxFilter ) Or (KalaNumber like @TextBoxFilter) '; 				
	set @ParmAndstr=' AND ' 			
END 	
*/

If @CmbSort = 1 
    set @Sortstring = N' ORDER BY KalaNumber DESC'
    
Else
If @CmbSort = 2 
    set @Sortstring  = N' ORDER BY NameKala1'
Else
If @CmbSort = 3 
    set @Sortstring = N' ORDER BY MandeAnbar DESC'
Else
If @CmbSort = 4 
   set @Sortstring = N' ORDER BY AkharinKharid'
Else
If @CmbSort = 5 
    set @Sortstring =N' ORDER BY UnitPrice1'
    


set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY NameKala1 + + N'' '' + ISNULL(note, N'' ''), NameKala1, BarCode, AID, GOKID, FieldN1, NameAnbar, CONVERT(Nvarchar(255), KalaNumber), CONVERT(float, KalaNumber), CONVERT(float, 
                      KalaNumber), NameVahed1, UnitPrice2, UnitPrice1, UnitPrice3, UnitPrice4, UnitPrice5, UnitPrice6, Active, NameGoKala1, ID, Zarib, NameVahed2, vaznKala  '	
     
	if(@OnlyMandedare<>0)
		SET @SQLString =@SQLString 
		
		SET @SQLString=@SQLString+@Sortstring 	  
					
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@OnlyMandedare=@OnlyMandedare,@OnlyActive=@OnlyActive;  		
return END 








GO


IF not EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[T_TabgheKala]') AND type in (N'U'))
	CREATE TABLE [dbo].[T_TabgheKala](
		[IDAuto] [int] IDENTITY(1,1) NOT NULL,
		[N4RaghamBarcod] [nvarchar](50) NULL,
		[NameTabaghe] [nvarchar](max) NULL,
	 CONSTRAINT [PK_T_TabgheKala] PRIMARY KEY CLUSTERED 
	(
		[IDAuto] ASC
	)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	) ON [PRIMARY]
GO






/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_CreateKalaFromBarcod]    Script Date: 06/10/2020 13:03:24 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_CreateKalaFromBarcod]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_CreateKalaFromBarcod]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_CreateKalaFromBarcod]    Script Date: 06/10/2020 13:03:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


    CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_CreateKalaFromBarcod] 		
    /* ورودي*/ 		
    @Userid INT,	 		
    @BarcodeVariable NVARCHAR(255), 
    /* خروجي*/ 		
    @Result BIT OUTPUT 
    AS 
    BEGIN  
    	DECLARE @T4RaghBarcode NVARCHAR(255) 
    	DECLARE @PhiForosh DECIMAL(19,4)
    	DECLARE @GhimatBarcode NVARCHAR(255) 	
    	DECLARE @NameTabaghe NVARCHAR(255) 
    	
    	set @Result=0
    	
    	set @PhiForosh=0
    	
    	set @NameTabaghe=NULL
    	
    	SET @T4RaghBarcode=LEFT(@BarcodeVariable,4)
    	
		SELECT  @NameTabaghe=NameTabaghe
		FROM     T_TabgheKala
		WHERE  (N4RaghamBarcod = @T4RaghBarcode)
		
		SET @GhimatBarcode=right(@BarcodeVariable,len(@BarcodeVariable)-4)
		
		IF (len(@GhimatBarcode)<6)
			set @PhiForosh=convert(money,@GhimatBarcode)*10000
		
		
		IF (LEN(@NameTabaghe)>0 AND @PhiForosh>0) 
		BEGIN	
			
			DECLARE @KalaIDAuto INT
			
			set @NameTabaghe=@NameTabaghe+' ' +@GhimatBarcode
			INSERT INTO T_NameKala (KalaNumber, NameKala1, BarCode, VAHED1, VAHED2, Zarib, UnitPrice1)
			SELECT  (SELECT  ISNULL(MAX(KalaNumber), 10000) + 1 AS Expr1
			FROM     T_NameKala) AS Expr1, @NameTabaghe AS Expr2, @BarcodeVariable AS Expr3, 1 AS Expr4, 1 AS Expr5, 1 AS Expr6, @PhiForosh AS Expr7			
			
			SET @KalaIDAuto=SCOPE_IDENTITY()
			
			INSERT INTO T_Kala
						   (NameKalaID, AID, GOKID, VKalaID)
			VALUES  (@KalaIDAuto, 1, 1, 1)
			set @Result=1
			
		END
			
		END			
    	

GO




/****** Object:  StoredProcedure [dbo].[Op_Paye_NameKala_ChengeInformatoinkala]    Script Date: 06/20/2020 15:24:57 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_NameKala_ChengeInformatoinkala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_NameKala_ChengeInformatoinkala]
GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_NameKala_ChengeInformatoinkala]    Script Date: 06/20/2020 15:24:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[Op_Paye_NameKala_ChengeInformatoinkala] 

@UserID INT,
@GoKID NVARCHAR(Max),
@AID  NVARCHAR(Max),
@KalaID NVARCHAR(Max)
AS





BEGIN
		
	Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
	set @SQLQuery='SELECT  T_NameKala.*, CONVERT(money, ISNULL(vaznKala, 0) * (ISNULL(UnitPriceKharid, 0) + ISNULL(HazineKala, 0)) + (ISNULL(DarsadSoodKala, 0) * ISNULL(vaznKala, 0)) * (ISNULL(UnitPriceKharid, 0) 
               + ISNULL(HazineKala, 0))) AS GhifoFinal
FROM     T_NameKala
WHERE  (1 = 1)

 '

         
    If (@GoKID Is Not Null)
    				Set @SQLQuery=@SQLQuery+' And (T_NameKala.ID IN(SELECT  T_NameKala.ID
												FROM     T_NameKala INNER JOIN
												T_Kala ON T_NameKala.ID = T_Kala.NameKalaID WHERE (T_Kala.GoKID IN ('+ @GoKID  +'))))'    
    If (@AID Is Not Null)

		Set @SQLQuery=@SQLQuery+' And (T_NameKala.ID IN(SELECT  T_NameKala.ID
						FROM     T_NameKala INNER JOIN
						T_Kala ON T_NameKala.ID = T_Kala.NameKalaID WHERE (T_Kala.AID IN ('+ @AID +'))))'     


    If (@KalaID Is Not Null)
		BEGIN
			 set @KalaID='%'+@KalaID+'%'
			 
			 Set @SQLQuery = @SQLQuery + ' And (NameKala1 LIKE  @KalaID )'   
		END
		
	    Set @ParamDefinition = '@UserID INT,
								@GOKID NVARCHAR(Max),
								@AID  NVARCHAR(Max),
								@KalaID NVARCHAR(Max)'

Set @SQLQuery=@SQLQuery+' ORDER BY NameKala1'    
								  
		    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GOKID,
							@AID  , 
							@KalaID 
													                           






END




GO





/****** Object:  StoredProcedure [dbo].[CQ_ListKala_ForoshSandogh_SF16]    Script Date: 06/21/2020 09:04:14 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_ForoshSandogh_SF16]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_ForoshSandogh_SF16]
GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_ForoshSandogh_SF16]    Script Date: 06/21/2020 09:04:14 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO







  CREATE PROCEDURE [dbo].[CQ_ListKala_ForoshSandogh_SF16] ( 
					@AID int,
					@OperType int,
					@LinkMasterID int,
					@GoKID int,
					@OnlyMandedare BIT,
					@OnlyActive Bit)           
  AS     
  BEGIN 	
  
  SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(Max);  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max);         	
  DECLARE @Sortstring  NVARCHAR(Max);  
  DECLARE @CmbSort AS INT;
  	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';      
  set @Sortstring=N' ';            	
  SET @ParmDefinition = N'@AID int=null, 								
							  @OperType int=null, 								
							  @LinkMasterID  int=null, 								
							  @GoKID int=null,
		@OnlyMandedare BIT=null,
		@OnlyActive Bit=null ';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
	  	
	SELECT    @OnlyActive= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 2)

	SELECT    @OnlyMandedare= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 3)	  		 
	
	SELECT    @CmbSort= ABS(ISNULL(FieldN1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 10) AND (OPTIONID = 1)	  
	  		 
  SET @SQLString =' SELECT     ID AS NameKalaID, CONVERT(float, KalaNumber) AS KalaNumber, NameKala1, BarCode, Zarib, AID, GOKID, KalaNumber AS [کد کالا], NameKala1 + + N'' '' + ISNULL(note, N'' '') AS [نام کالا], 
                      NameVahed1 AS [نام واحد], 0 AS [مانده انبار], NameAnbar AS [نام انبار],
                       case when (CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 
               0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala)))<10001 then 
               		ROUND(((CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala,0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala))))/10,0)*10
               	else
               		ROUND(((CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala,0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala))))/100,0)*100
               		end  AS [قيمت فروش1]
                       , 0 AS [آخرين خريد], 
                       case when (CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala2/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 
               0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala)))<10001 then 
               		ROUND(((CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala2/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala,0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala))))/10,0)*10
               	else
               		ROUND(((CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala2/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala,0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala))))/100,0)*100
               		end AS [قيمت فروش2], UnitPrice3 AS [قيمت فروش3], UnitPrice4 AS [قيمت فروش4], 
                      UnitPrice5 AS [قيمت فروش5], UnitPrice6 AS [قيمت فروش6], NameVahed2, 0 AS UnitPriceKh, FieldN1, BarCode AS [ بارکد ], NameGoKala1 AS [گروه کالا], NameAnbar, UnitPrice1, 0 AS AkharinKharid, 
                      UnitPrice2, UnitPrice3, UnitPrice4, UnitPrice5, UnitPrice6, Active, NameGoKala1, 0 AS MandeAnbar, CONVERT(Nvarchar(255), KalaNumber) AS CodKala, 0 AS UnitPricekhUser, vaznKala
FROM         dbo.FunInfoKalaAll(@OnlyActive) AS FunInfoKalaAll_1
WHERE     (1 = 1)

' 	




  /* -----------------------نشان دادن کالاهاي فعال */ 
/*
if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه
	*/
               
  /* -----------------------شرط انبار */ 	

		  		
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 		
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	

 /* /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@TextBoxFilter  IS NULL) OR LEN(@TextBoxFilter)>1) 			
BEGIN 		
	set @TextBoxFilter=N'%'+@TextBoxFilter +N'%'
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'  (FunInfoKalaAll_1.NameKala1 Like @TextBoxFilter ) Or (KalaNumber like @TextBoxFilter) '; 				
	set @ParmAndstr=' AND ' 			
END 	
*/

If @CmbSort = 1 
    set @Sortstring = N' ORDER BY KalaNumber DESC'
    
Else
If @CmbSort = 2 
    set @Sortstring  = N' ORDER BY NameKala1'
Else
If @CmbSort = 3 
    set @Sortstring = N' ORDER BY MandeAnbar DESC'
Else
If @CmbSort = 4 
   set @Sortstring = N' ORDER BY AkharinKharid'
Else
If @CmbSort = 5 
    set @Sortstring =N' ORDER BY UnitPrice1'
    


set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY NameKala1 + + N'' '' + ISNULL(note, N'' ''), NameKala1, BarCode, AID, GOKID, FieldN1, NameAnbar, CONVERT(Nvarchar(255), KalaNumber), CONVERT(float, KalaNumber), CONVERT(float, 
                      KalaNumber), NameVahed1, UnitPrice2, UnitPrice1, UnitPrice3, UnitPrice4, UnitPrice5, UnitPrice6, Active, NameGoKala1, ID, Zarib, NameVahed2, vaznKala  '	
     
	if(@OnlyMandedare<>0)
		SET @SQLString =@SQLString 
		
		SET @SQLString=@SQLString+@Sortstring 	  
					
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@OnlyMandedare=@OnlyMandedare,@OnlyActive=@OnlyActive;  		
return END 









GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16]    Script Date: 06/21/2020 09:04:18 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16]
GO



/****** Object:  StoredProcedure [dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16]    Script Date: 06/21/2020 09:04:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






  CREATE PROCEDURE [dbo].[CQ_ListKala_Sandoogh_Analyz1_SF16] ( 
						@AID int,
						@OperType int,
						@LinkMasterID int,
						@GoKID int,
						@OnlyMandedare BIT,
						@OnlyActive Bit)           
  AS     
  BEGIN 	
  
  SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(Max);  	
  DECLARE @WhrStatement  NVARCHAR(Max);         	
  DECLARE @ParmDefinition  NVARCHAR(Max);         	
  DECLARE @ParmAndstr  NVARCHAR(Max);         	
  DECLARE @Sortstring  NVARCHAR(Max);  
  DECLARE @CmbSort AS INT;
  	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';      
  set @Sortstring=N' ';            	
  SET @ParmDefinition = N'@AID int=null, 								
							  @OperType int=null, 								
							  @LinkMasterID  int=null, 								
							  @GoKID int=null,
		@OnlyMandedare BIT=null,
		@OnlyActive Bit=null ';            	
  /* Build the SQL string one time. */      /* Specify the parameter format one time. */  		
	  	
	SELECT    @OnlyActive= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 2)

	SELECT    @OnlyMandedare= ABS(ISNULL(FieldT1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 7) AND (OPTIONID = 3)	  	
	
	SELECT    @CmbSort= ABS(ISNULL(FieldN1, 0))
	FROM         T_Defult
	WHERE     (GROUPID = 10) AND (OPTIONID = 1)	  
		 
	  		 
  SET @SQLString =' SELECT     FunInfoKalaAll_1.ID AS NameKalaID, CONVERT(float, FunInfoKalaAll_1.KalaNumber) AS KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.Zarib, 
                      FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID, FunInfoKalaAll_1.KalaNumber AS [کد کالا], FunInfoKalaAll_1.NameKala1+ + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' '')  AS [نام کالا], FunInfoKalaAll_1.NameVahed1 AS [نام واحد], 
                      ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3) AS [مانده انبار], FunInfoKalaAll_1.NameAnbar AS [نام انبار], 
 case when (CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 
               0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala)))<10001 then 
               		ROUND(((CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala,0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala))))/10,0)*10
               	else
               		ROUND(((CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala,0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala))))/100,0)*100
 end
  AS [قيمت فروش1], ROUND(CONVERT(money, 
                      drvTra.MaxUnitPriceKh), 2) AS [آخرين خريد], 
               case when (CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala2/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 
               0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala)))<10001 then 
               		ROUND(((CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala2/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala,0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala))))/10,0)*10
               	else
               		ROUND(((CONVERT(MONEY, MAX((FunInfoKalaAll_1.DarsadSoodKala2/100) * ((ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala,0)) * FunInfoKalaAll_1.vaznKala) + (ISNULL(FunInfoKalaAll_1.UnitPriceKharid, 0) + ISNULL(FunInfoKalaAll_1.HazineKala, 0)) * FunInfoKalaAll_1.vaznKala))))/100,0)*100
               		end AS [قيمت فروش2], FunInfoKalaAll_1.UnitPrice3 AS [قيمت فروش3], FunInfoKalaAll_1.UnitPrice4 AS [قيمت فروش4], 
                      FunInfoKalaAll_1.UnitPrice5 AS [قيمت فروش5], FunInfoKalaAll_1.UnitPrice6 AS [قيمت فروش6], FunInfoKalaAll_1.NameVahed2, drvTra.MaxUnitPriceKh AS UnitPriceKh, FunInfoKalaAll_1.FieldN1, 
                      FunInfoKalaAll_1.BarCode AS [ بارکد ], FunInfoKalaAll_1.NameGoKala1 AS [گروه کالا], FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.UnitPrice1, drvTra.MaxUnitPriceKh AS AkharinKharid, 
                      FunInfoKalaAll_1.UnitPrice2, FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, FunInfoKalaAll_1.Active, 
                      FunInfoKalaAll_1.NameGoKala1, ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3)  AS MandeAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber) AS CodKala,FunInfoKalaAll_1.UnitPriceKharid as UnitPricekhUser , FunInfoKalaAll_1.vaznKala 
FROM         dbo.FunInfoKalaAll(@OnlyActive) AS FunInfoKalaAll_1 INNER JOIN
                      dbo.Fun_CQ_ListKala_Sandoogh_Analyz() AS drvTra ON FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID 
WHERE     (1 = 1)

' 	



/*FunInfoKalaAll_1.AID = drvTra.AID and FunInfoKalaAll_1.ID = drvTra.KALAID */

/* FunInfoKalaAll_1.LinkKalaAID = drvTra.LinkKalaAID  */

  /* -----------------------نشان دادن کالاهاي فعال */ 
/*
if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active = 0) '; 	فعال				
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' where      (FunInfoKalaAll_1.Active > -1000) '; 	همه
	*/
               
  /* -----------------------شرط انبار */ 	

		  		
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 		
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (FunInfoKalaAll_1.GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	

 /* /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@TextBoxFilter  IS NULL) OR LEN(@TextBoxFilter)>1) 			
BEGIN 		
	set @TextBoxFilter=N'%'+@TextBoxFilter +N'%'
	set @WhrStatement= @WhrStatement + @ParmAndstr + N'  (FunInfoKalaAll_1.NameKala1 Like @TextBoxFilter ) Or (KalaNumber like @TextBoxFilter) '; 				
	set @ParmAndstr=' AND ' 			
END 	*/

If @CmbSort = 1 
    set @Sortstring = N' ORDER BY KalaNumber DESC'
    
Else
If @CmbSort = 2 
    set @Sortstring  = N' ORDER BY NameKala1'
Else
If @CmbSort = 3 
    set @Sortstring = N' ORDER BY MandeAnbar DESC'
Else
If @CmbSort = 4 
   set @Sortstring = N' ORDER BY AkharinKharid'
Else
If @CmbSort = 5 
    set @Sortstring =N' ORDER BY UnitPrice1'




set @SQLString=@SQLString+  @WhrStatement + ' GROUP BY FunInfoKalaAll_1.NameKala1+ + N'' '' + ISNULL(FunInfoKalaAll_1.note, N'' ''),ROUND(CONVERT(money, drvTra.MaxUnitPriceKh), 2), FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.AID, FunInfoKalaAll_1.GOKID,  FunInfoKalaAll_1.FieldN1, drvTra.MaxUnitPriceKh, 
                      FunInfoKalaAll_1.NameAnbar, CONVERT(Nvarchar(255), FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), CONVERT(float, FunInfoKalaAll_1.KalaNumber), 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.UnitPrice2,FunInfoKalaAll_1.UnitPrice1, FunInfoKalaAll_1.UnitPrice3, FunInfoKalaAll_1.UnitPrice4, FunInfoKalaAll_1.UnitPrice5, FunInfoKalaAll_1.UnitPrice6, 
                      FunInfoKalaAll_1.Active, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.ID, FunInfoKalaAll_1.Zarib, drvTra.MaxUnitPriceKh,FunInfoKalaAll_1.NameVahed2,FunInfoKalaAll_1.UnitPriceKharid, FunInfoKalaAll_1.vaznKala  '	
     
	if(@OnlyMandedare<>0)
		SET @SQLString =@SQLString + ' HAVING ROUND(SUM(CONVERT(float, CASE WHEN VKalaID = 2 THEN 0 ELSE drvTra.Tedad END)), 3)  <>0 ' 
		
		SET @SQLString=@SQLString+@Sortstring 	  
					
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@AID=@AID ,@OperType=@OperType,@LinkMasterID=@LinkMasterID,@GoKID=@GoKID,@OnlyMandedare=@OnlyMandedare,@OnlyActive=@OnlyActive;  		
return END 







GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListPishForoosh]    Script Date: 06/21/2020 13:19:51 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ListPishForoosh]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ListPishForoosh]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_ListPishForoosh]    Script Date: 06/21/2020 13:19:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[QSubPFrm_ListPishForoosh]
AS 

SELECT  T_PishFactor.FOROSHID, T_PishFactor.HesabID, T_PishFactor.GOID, T_PishFactor.KOLID, T_PishFactor.MOINID, T_PishFactor.THESABID, T_PishFactor.TextName, T_PishFactor.TextName1, 
               T_PishFactor.TextNameT, T_PishFactor.NumberKH, T_PishFactor.DateFo, T_PishFactor.Takhfif, T_PishFactor.TextShSA, T_PishFactor.NumSID2, T_PishFactor.Sharh, T_PishFactor.FieldN1, T_PishFactor.FieldN2, 
               T_PishFactor.FieldN3, T_PishFactor.FieldN4, T_PishFactor.FieldT1, T_PishFactor.FieldT2, T_PishFactor.FieldT3, T_PishFactor.FieldT3 AS Expr1, T_PishFactor.FieldT4, T_PishFactor.Flag1, T_PishFactor.Flag2, 
               T_PishFactor.Flag3, T_PishFactor.Flag4, T_PishFactor.Flag5, T_PishFactor.Flag6, T_PishFactor.Flag7, T_PishFactor.Flag8, T_PishFactor.ForoshNumbr, T_PishFactor.PrimarySaid2, T_PishFactor.THESABIDAmel, 
               dbo.HesabName(T_PishFactor.KOLID, T_PishFactor.HesabID, T_PishFactor.THESABID) AS namehesab, T_Forosh.ForoshNumbr AS ForoshNumbr_T_Forosh
FROM     T_PishFactor LEFT OUTER JOIN
               T_Forosh ON T_PishFactor.RefForoshid = T_Forosh.FOROSHID
ORDER BY T_PishFactor.FOROSHID DESC
GO



/****** Object:  StoredProcedure [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1Rep1]    Script Date: 06/25/2020 12:46:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_Table1_Jabejaei_KolKeriehSF1Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1Rep1]
GO



/****** Object:  StoredProcedure [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1Rep1]    Script Date: 06/25/2020 12:46:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


          CREATE PROCEDURE [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1Rep1] (        @USERID int, @date1 NVARCHAR(10), @date2 NVARCHAR(10), @Goid INT,@Kolid INT,@Hesabid INT,@THesabid INT    )              AS              BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	         DECLARE @WhrStatement  NVARCHAR(2000);         	         DECLARE @ParmDefinition  NVARCHAR(2000);         	         DECLARE @ParmAndstr  NVARCHAR(2000);         	 	         set @WhrStatement  = N'';         	         set @ParmAndstr = N'';                  	         SET @ParmDefinition = N'@USERID int=null, 							 							@date1 NVARCHAR(10)=null, 							 							@date2 NVARCHAR(10)=null, 							 							@Goid INT=null, 							 							@Kolid INT=null, 							 							@Hesabid INT=null, 							 							@THesabid INT=null';            	  		 SET @SQLString =' SELECT     Table1.ID1, Table1.ID1Number, SUM([TRANSACTION].TedadKhA) AS TedadKhA, [TRANSACTION].MabVahedHazine, Table1.DateID1,                        SUM([TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine) AS MabHazine, dbo.HesabName(Table1.KOLID, T_Anbar.hesabid, Table1.THESABID) AS NameDriver, [TRANSACTION].GOKID,                        T_NameGoKala.NameGoKala1, T_NameGoKala.NameGoKalanumbr, T_NameGoKala.ID, CONVERT(nvarchar(255), [TRANSACTION].MabVahedHazine) + T_NameGoKala.NameGoKala1 AS Grp FROM         Table1 INNER JOIN                       [TRANSACTION] ON Table1.ID1 = [TRANSACTION].ID1 INNER JOIN                       T_Anbar ON Table1.AID1 = T_Anbar.AID INNER JOIN                       T_Anbar AS T_Anbar_1 ON Table1.AID2 = T_Anbar_1.AID INNER JOIN                       T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID INNER JOIN                       T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID WHERE     ([TRANSACTION].TedadKhA > 0) AND (Table1.DateID1 BETWEEN @Date1 AND @Date2) AND (Table1.KOLID = @KOLID) AND (Table1.HesabID = @HesabID) AND (Table1.THESABID = @THESABID) GROUP BY Table1.ID1, Table1.ID1Number, [TRANSACTION].MabVahedHazine, Table1.DateID1, dbo.HesabName(Table1.KOLID, T_Anbar.hesabid, Table1.THESABID), [TRANSACTION].GOKID,                        T_NameGoKala.NameGoKala1, T_NameGoKala.NameGoKalanumbr, T_NameGoKala.ID, CONVERT(nvarchar(255), [TRANSACTION].MabVahedHazine) + T_NameGoKala.NameGoKala1 HAVING      (SUM([TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine) > 0)' 	   						  set @SQLString=@SQLString 	  EXECUTE sp_executesql   @SQLString, @ParmDefinition,@USERID=@USERID ,@date1=@date1,@date2=@date2,@Goid=@Goid,@Kolid=@Kolid,@Hesabid=@Hesabid,@THesabid=@THesabid;  		 return END    

GO




/****** Object:  StoredProcedure [dbo].[Rep_0523893819]    Script Date: 06/25/2020 12:47:24 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_0523893819]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_0523893819]
GO



/****** Object:  StoredProcedure [dbo].[Rep_0523893819]    Script Date: 06/25/2020 12:47:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


    CREATE PROCEDURE [dbo].[Rep_0523893819] (  
    	@pvchAction nvarchar(50)='Analyz',  
    	@startDate nvarchar(50),  
    	@ENDdate nvarchar(50),  
    	@IdKala int,  @IDA int,  
    	@KalaGO int,   
    	@Goid int,  
    	@KolID int,  
    	@HesabID int,  
    	@THesabID int,  
    	@NoeFact INT, 
    	@NameKala nvarchar(255) )              	
    	
    	AS        	
    	BEGIN 	  
    		/*  @pvchAction='Analyz',  
    		*   @NoeFact=forms![TreeViewRep_HesabAnbar]![Nfaktor],  
    		*   @THesabId=forms![TreeViewRep_HesabAnbar]![THesabId],  
    		*   @HesabId=forms![TreeViewRep_HesabAnbar]![HesabId],  
    		*   @Goid=forms![TreeViewRep_HesabAnbar]![Goid],  
    		*   @Kolid=forms![TreeViewRep_HesabAnbar]![Kolid],  
    		*   @KalaGo=forms![TreeViewRep_HesabAnbar]![GoKala],  
    		*   @IDA=forms![TreeViewRep_HesabAnbar]![AID],  
    		*   @startDate=forms![TreeViewRep_HesabAnbar]![AsDate],  
    		*   @enddate=forms![TreeViewRep_HesabAnbar]![TaDate],  
    		*   @IdKala=forms![TreeViewRep_HesabAnbar]![KalaID],  
    		*   @NameKala=forms![TreeViewRep_HesabAnbar]![FilterNameKala]  */  	   	
    		
    		SET NOCOUNT ON; 	 	   	
    		
    		DECLARE @SQLString  NVARCHAR(MAX);  	/* محتوي رشته پرس و جو*/   	
    		DECLARE @WhrStatement  NVARCHAR(MAX);         	   	
    		DECLARE @ParmDefinition  NVARCHAR(MAX);         	   	
    		DECLARE @ParmAndstr  NVARCHAR(MAX);       	     	 	   	
    		
    		set @WhrStatement = N' ';         	   	
    		set @ParmAndstr = N' ';                     	
    		SET @ParmDefinition = N'@IdKala int=null, 								   							
									@IDA int=null, 								   							
									@KalaGO int=null, 								   							
									@startDate nvarchar(50)=''0'', 								   							
									@ENDdate nvarchar(50)=''99999999'', 								   							
									@KolID int=null, 								   							
									@HesabID int=null, 								   							
									@Goid int=null , 								   							
									@THesabID int=null, 								   							
									@NoeFact int=null, 								   							
									@pvchAction nvarchar(50)=''Analyz'',
									@NameKala nvarchar(255)=null'; 	

if(@StartDate IS NULL) 			    	

BEGIN 				    		
	set @StartDate='0'; 				    	
END          	

if(@Enddate IS NULL) 			    	
BEGIN 				    		
	set @Enddate='99999999'; 				    	
END    							            	   	   	

/* Build the SQL string one time. */      
/* Specify the parameter format one time. */ 	 		    	/* -----------------شروع آنالیز*/ 			    	
/* 	--------------شروع عملیات چاپ*/    	

IF (@pvchAction is NULL)   				
SET @pvchAction ='Analyz'   				   	
if(@pvchAction ='Analyz') 		   	
BEGIN 			 			    	
	DECLARE @tmpdbname varchar(50) 	  		    	
	set @tmpdbname= right( REPLACE( CONVERT(uniqueidentifier, newid()), '-', ''),8) ; 	  		  	  		   	  		  	  		    	
	IF OBJECT_ID('tempdb..##' + @tmpdbname +'' ) IS NOT NULL 				   	
	exec ('DROP TABLE ##' + @tmpdbname +''); 				 	  		    	
	SET @SQLString =N' SELECT     FunInfoKalaAll_1.ID, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.GOKID, 
					   FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1,                        
					   FunInfoKalaAll_1.AID, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.NameVahed1, 
					   FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.Active, FunInfoKalaAll_1.BarCode,                        
					   FunInfoKalaAll_1.note, SubTransactionKala_1.GOID, SubTransactionKala_1.KolID, SubTransactionKala_1.HesabID, 
					   SubTransactionKala_1.THesabID, SubTransactionKala_1.Tarikh,SubTransactionKala_1.ShomareFactor, SubTransactionKala_1.Namehesab, 
					   SubTransactionKala_1.NoeFa1, SubTransactionKala_1.TedadVarde, SubTransactionKala_1.TedadSadre,                        
					   convert(money,SubTransactionKala_1.MVarde) as MVarde,
					   convert(money,SubTransactionKala_1.MVarde/(case when SubTransactionKala_1.TedadVarde<>0 then SubTransactionKala_1.TedadVarde else 1 end )) as Phi_M_KH,
					   convert(money,SubTransactionKala_1.MSadre/(case when SubTransactionKala_1.TedadSadre>0 then SubTransactionKala_1.TedadSadre else 1 end ))  as Phi_M_Fo, 
					   convert(money,SubTransactionKala_1.MSadre) as MSadre , SubTransactionKala_1.SharhKala, SubTransactionKala_1.NoeFa, 0 AS Radif, 0 AS TedadRadifi,
					   0 AS MandeRadifi, 0 AS Phi_M ,SubTransactionKala_1.idFact                       
					   INTO         ##' + @tmpdbname +' 					  
	                   FROM         dbo.SubTransactionKala() AS SubTransactionKala_1 INNER JOIN                       
	                   dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON SubTransactionKala_1.KALAID = FunInfoKalaAll_1.ID AND SubTransactionKala_1.AID = FunInfoKalaAll_1.AID '    
/* --------------------   شرط حساب */ 			  
/* INTO         ##' + @tmpdbname +'  */ 
if(@WhrStatement=N' ') 	
	set @ParmAndstr=N' WHERE ' 	 
else 	
	set @ParmAndstr=N' AND '	

If(NOT (@THesabID  IS NULL)) 			 		
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (SubTransactionKala_1.THesabID = @THesabID ) '; 
					   
if(@WhrStatement=N' ') 	
	set @ParmAndstr=N' WHERE ' 	 
else 	
	set @ParmAndstr=N' AND '	
	 	 
if (NOT (@HesabID  IS NULL)) 			 	
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (SubTransactionKala_1.HesabID = @HesabID ) '; 				  

if(@WhrStatement=N' ') 	
	set @ParmAndstr=N' WHERE ' 	 
else 	
	set @ParmAndstr=N' AND '	  	
			 
if(NOT (@Goid IS NULL)) 			 	
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (SubTransactionKala_1.Goid = @Goid) '; 				  

if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 else 	set @ParmAndstr=N' AND '	 	 

if (NOT (@KolID IS NULL)) 			 	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (SubTransactionKala_1.KolID = @KolID ) '; 	

/* -----------------------شرط انبار روی ساب پرس وجو*/ 			  

if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 else 	set @ParmAndstr=N' AND '	 	  				 

if(NOT (@IDA  IS NULL)) set @WhrStatement= @WhrStatement + @ParmAndstr + N'(FunInfoKalaAll_1.AID = @IDA )'; 				 	 						  

/* -----------------------شرط کالا روی ساب پرس وجو*/ 			 

if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 else 	set @ParmAndstr=N' AND '	 				 

if(NOT (@IdKala  IS NULL)) 	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(FunInfoKalaAll_1.ID = @IdKala )'; 				   

if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 else 	set @ParmAndstr=N' AND '	 				 

if(NOT (@NameKala   IS NULL)) 			 
	BEGIN 	
		set @NameKala ='%' + @NameKala + '%'	 				 
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameKala1  Like @NameKala ) '; 				 
		set @ParmAndstr=' AND ' 			 
	END 				 					  

/* 	-----------------------شرط گروه انبار روی ساب پرس وجو*/ 			 
if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 else 	set @ParmAndstr=N' AND '	 			 
if(NOT (@KalaGO  IS NULL)) 	set @WhrStatement= @WhrStatement + @ParmAndstr + N'(FunInfoKalaAll_1.GOKID = @KalaGO )'; 				 
/* 	-----------------------شرط گروه انبار روی ساب پرس وجو*/ 			 
if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 ELSE  	set @ParmAndstr=N' AND '	 			 
IF ((NOT (@startDate  IS NULL) ) AND NOT (@ENDdate  IS NULL)) set @WhrStatement= @WhrStatement + @ParmAndstr + N'(SubTransactionKala_1.Tarikh  BetWeen @startDate and  @ENDdate)'; 			 			   

/* --------------------  نوع عمليات */              

if(@WhrStatement=N' ') 	set @ParmAndstr=N' WHERE ' 	 else 	set @ParmAndstr=N' AND '	 			 
if(NOT (@NoeFact  IS NULL)) set @WhrStatement= @WhrStatement + @ParmAndstr + N' ( SubTransactionKala_1.NoeFa= @NoeFact) '; 				 	              
set @SQLString=@SQLString + @WhrStatement+N' ORDER BY FunInfoKalaAll_1.ID, SubTransactionKala_1.Tarikh, SubTransactionKala_1.TedadVarde DESC '
set @SQLString=@SQLString + N'Declare Cr2 Cursor Local for 						 
							  Select ID, TedadVarde, TedadSadre, Mvarde from [##' + @tmpdbname +']  						 
							  for Update
							  DECLARE @KALAID_1 bigint 						 
							  DECLARE @Radif_2 int 						 
							  DECLARE @TedadVarde_1 decimal(19,4) 						 
							  DECLARE @TedadSadre_1 decimal(19,4) 						 						 						 
							  DECLARE @Mvarde_1 decimal(19,4) 						 
							  DECLARE @PHI_M_2 decimal(19,4) 						 
							  DECLARE @KalaID_GH bigint 						 
							  DECLARE @MTedadi decimal(19,4) 						 
							  DECLARE @MRiali decimal(19,4) 						 
							  DECLARE @PHI_M_GH decimal(19,4)   														 		 						 
							  set @KalaID_GH=0 						 
							  set @PHI_M_2=0 						 
							  set @MTedadi=0 						 
							  set @MRiali=0 						 
							  set @PHI_M_GH=0 						 
							  Set @Radif_2=0  						 						 						 
							  Open Cr2    						 
							  fetch Next from Cr2 into @KALAID_1,@TedadVarde_1,@TedadSadre_1,@Mvarde_1 						 
							  while @@FETCH_STATUS=0 							  
							  Begin 								 	
							  	if(@KALAID_1 <> @KalaID_GH) 								 		
							  	begin 									 			
							  		Set @Radif_2=0 									 			
							  		set	@PHI_M_2 = 0 									 			
							  		set @MRiali=0 									 			
							  		if (@TedadSadre_1>0) Set @PHI_M_GH = 1 									 									 			
							  		set @MTedadi=0 								 		
							  	end  										 								 								 	
							  	Set @KalaID_GH = @KALAID_1 								 								 	
							  	Set @Radif_2=@Radif_2 + 1 								 								 	
							  	Set @MTedadi=@MTedadi+(@TedadVarde_1-@TedadSadre_1) 		 								 	
							  	set @MRiali=@MRiali+@Mvarde_1-(@PHI_M_GH * @TedadSadre_1)  	
							  	if (@TedadVarde_1>0) and (@MTedadi<>0) Set @PHI_M_2 = round(ABS(@MRiali/@MTedadi),0) 		
							  	if (@MTedadi=0) Set @PHI_M_2 = 0   	
							  	if (@TedadSadre_1>0) 		
							  	Begin 			
							  		Set @PHI_M_2=@PHI_M_GH 			
							  		set @MRiali=@PHI_M_GH *@MTedadi 		
							  	End 		 									 								 
							  	
							  	Set @PHI_M_GH=@PHI_M_2  
							  	Set @MTedadi=Round(Convert(Float,@MTedadi),2)  								 
							  	Update ##' + @tmpdbname +' 
							  	set Radif=@Radif_2,
							  		PHI_M=@PHI_M_2 , 
							  		TedadRadifi=@MTedadi, 
							  		MandeRadifi=@MRiali 
							  							Where current of cr2; 								 	 
							  							fetch Next from Cr2 into @KALAID_1,@TedadVarde_1,@TedadSadre_1,@Mvarde_1			 
							  End	 						 						 
							  Close cr2 						
							  deallocate cr2	' 	 	
							  		 
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@Goid=@Goid,@pvchAction=@pvchAction,@IDA=@IDA,@NoeFact=@NoeFact,@THesabID=@THesabID,@HesabID=@HesabID,@KolID=@KolID,@IdKala=@IdKala,@KalaGO=@KalaGO,@startDate=@startDate,@ENDdate=@ENDdate,@NameKala=@NameKala; 			 			 
EXEC ('select * from [##' + @tmpdbname +'] 				DROP TABLE ##' + @tmpdbname +''); 		

END                                                   
return END                       

GO




/****** Object:  StoredProcedure [dbo].[RepFST_0523894024Rep1]    Script Date: 06/25/2020 12:49:09 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RepFST_0523894024Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[RepFST_0523894024Rep1]
GO



/****** Object:  StoredProcedure [dbo].[RepFST_0523894024Rep1]    Script Date: 06/25/2020 12:49:10 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



   CREATE PROCEDURE [dbo].[RepFST_0523894024Rep1] (  
   	
   	@NameKalaID int, 
   	@TxtNameKalaID NVARCHAR(255), 
   	@AID int, 
   	@GOKID int, 
   	@Date1 NVARCHAR(10), 
   	@Date2 NVARCHAR(10)   )              
   	AS        
   	
   	BEGIN 	
   		SET NOCOUNT ON; 	 	   
   		DECLARE @SQLString  NVARCHAR(MAx);  	   
   		DECLARE @WhrStatement  NVARCHAR(MAx);         	   
   		DECLARE @ParmDefinition  NVARCHAR(MAx);         	   
   		DECLARE @ParmAndstr  NVARCHAR(MAx);         	 	   
   		set @WhrStatement  = N'';         	  
   		 set @ParmAndstr = N'';                  	   
   		 SET @ParmDefinition = N'@NameKalaID int=null,  						
   								 @TxtNameKalaID NVARCHAR(255)=null,								 						
   								 @AID int=null,
   								 @GOKID  int=null,
   								 @Date1 NVARCHAR(10)=null,
   								 @Date2 NVARCHAR(10)=null  ';            	 	    	

if(@Date1 IS NULL) 			    	
	BEGIN 				    		
		set @Date1='0';
	END
	
if(@Date2 IS NULL)
	BEGIN
		set @Date2='99999999';
	END

SET @SQLString =  '  SELECT     TRaKalaid.KALAID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.KalaNumber, 
                      FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.NameVahed1 AS NameVahed, SUM(TRaKalaid.Varde) AS Varde, SUM(TRaKalaid.Sadre) AS Sadre, 
                      SUM(TRaKalaid.Varde - TRaKalaid.Sadre) AS Mande
FROM         (SELECT     KALAID, AID, GOKID, TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, Tarikh AS Date1
                        FROM         View_SubTransactionKala
                        WHERE     (NOT (KALAID IS NULL))
                        UNION ALL
                        SELECT     NameKalaID, AID, GOKID, Mojodi AS Varde, 0 AS Sadre, dbo.F1StartDate() AS Date12
                        FROM         T_Kala
                        WHERE     (Mojodi <> 0)) AS TRaKalaid INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON TRaKalaid.AID = FunInfoKalaAll_1.AID AND TRaKalaid.KALAID = FunInfoKalaAll_1.ID
WHERE     (TRaKalaid.Date1 BETWEEN @Date1 AND @Date2) AND (FunInfoKalaAll_1.VKalaID = 1)            '   

set @ParmAndstr=' AND ' 			 

if(NOT (@NameKalaID  IS NULL)) 			 
	BEGIN 				 
		set @WhrStatement= @WhrStatement + @ParmAndstr + N' (TRaKalaid.KALAID  = @NameKalaID  ) '; 				 
		set @ParmAndstr=' AND ' 			 
	END   set @ParmAndstr=' AND ' 			 
	
	if(NOT (@TxtNameKalaID   IS NULL)) 			 
		BEGIN 	set @TxtNameKalaID ='%' + @TxtNameKalaID + '%'	 				 
			set @WhrStatement= @WhrStatement + @ParmAndstr + N' (NameKala1  Like @TxtNameKalaID ) '; 				 
			set @ParmAndstr=' AND ' 			 
		END   set @ParmAndstr=' AND ' 			 
		
		if(NOT (@AID  IS NULL)) 			 
			BEGIN 				 
				set @WhrStatement= @WhrStatement + @ParmAndstr + N' (TRaKalaid.AID = @AID ) '; 				 
				set @ParmAndstr=' AND ' 			 
			END 			   /* -----------------------شرط گروه کالا  */  
			
			set @ParmAndstr=' AND ' 			 
				if(NOT (@GoKID  IS NULL)) 			 
					BEGIN 				 	
							set @WhrStatement= @WhrStatement + @ParmAndstr + N' (TRaKalaid.GOKID = @GoKID ) '; 				 	
							set @ParmAndstr=' AND ' 			 
					END 	  		  
					
					set @SQLString=@SQLString +@WhrStatement+	' GROUP BY TRaKalaid.KALAID, FunInfoKalaAll_1.NameGoKalanumbr, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.KalaNumber, 
                      FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.NameVahed1
HAVING      (SUM(TRaKalaid.Varde - TRaKalaid.Sadre) <> 0)'	      			 

EXECUTE sp_executesql   @SQLString, @ParmDefinition,@nameKalaID=@nameKalaID ,@TxtNameKalaID=@TxtNameKalaID,@aiD=@aiD,@gokID=@gokID,@Date1=@Date1,@Date2=@Date2;  		 

return END     


GO




/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Taf_Rep1]    Script Date: 06/25/2020 12:50:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubTreeViewRep_Taraz_Taf_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Taf_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Taf_Rep1]    Script Date: 06/25/2020 12:50:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Taf_Rep1] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'

                      
	SET @SQLQuery = ' SELECT     T_KolHesab.NameHesab AS NameHesabKol, SUM([TRANSACTION].DARBed) AS Bedehkar, SUM([TRANSACTION].PARBes) AS Bestankar, [TRANSACTION].KOLID, 
                      T_KolHesab.KolNumber AS CodeHesab, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes)
                       ELSE 0 END AS MandeBed, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) 
                      ELSE 0 END AS MandeBes, T_Heasb.HESABNumber AS CodeMoin, T_Heasb.NameHesab AS NameHesabMoin, T_GropHesab.GoNumber, T_GropHesab.NameGo AS NameGroup, 
                      T_T_Heasb.THESABID, T_T_Heasb.THESABNumber AS CodeTafsili, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_T_Heasb.HESABID
FROM         T_GropHesab INNER JOIN
                      [TRANSACTION] ON T_GropHesab.GoID = [TRANSACTION].GOID INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (1 = 1) '
			 
	SET @GROUPBy = ' GROUP BY T_KolHesab.NameHesab, [TRANSACTION].KOLID, T_KolHesab.KolNumber, T_GropHesab.GoID, T_GropHesab.NameGo, T_Heasb.HESABNumber, T_Heasb.NameHesab, T_GropHesab.GoNumber, 
                      T_T_Heasb.THESABID, T_T_Heasb.THESABNumber, T_T_Heasb.NameHesabT, T_T_Heasb.HESABID	 '
	SET @OrderBy = ' '


	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
    SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
       
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END



GO




/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Rep1]    Script Date: 06/25/2020 12:51:18 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubTreeViewRep_Taraz_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Rep1]    Script Date: 06/25/2020 12:51:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Rep1] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'



	SET @SQLQuery = ' SELECT     T_KolHesab.NameHesab AS NameHesabKol, SUM([TRANSACTION].DARBed) AS Bedehkar, SUM([TRANSACTION].PARBes) AS Bestankar, T_KolHesab.KOLID, 
					  T_KolHesab.KolNumber AS CodeHesab, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes)
					   ELSE 0 END AS MandeBed, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) 
					  ELSE 0 END AS MandeBes, T_GropHesab.GoID, T_GropHesab.NameGo AS NameGroup
FROM         T_KolHesab INNER JOIN
					  [TRANSACTION] ON T_KolHesab.KOLID = [TRANSACTION].KOLID INNER JOIN
					  T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID where 1=1 '
			 
	SET @GROUPBy = ' GROUP BY T_KolHesab.NameHesab, T_KolHesab.KOLID, T_KolHesab.KolNumber, T_GropHesab.GoID, T_GropHesab.NameGo	 '
	SET @OrderBy = ' '


	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
    SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
       
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END



GO




/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Moin_Rep1]    Script Date: 06/25/2020 12:52:17 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubTreeViewRep_Taraz_Moin_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Moin_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Moin_Rep1]    Script Date: 06/25/2020 12:52:17 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Moin_Rep1] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'

                      
	SET @SQLQuery = ' SELECT     T_KolHesab.NameHesab AS NameHesabKol, SUM([TRANSACTION].DARBed) AS Bedehkar, SUM([TRANSACTION].PARBes) AS Bestankar, [TRANSACTION].KOLID, 
                      T_KolHesab.KolNumber AS CodeHesab, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes)
                       ELSE 0 END AS MandeBed, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) 
                      ELSE 0 END AS MandeBes, T_Heasb.HESABNumber AS CodeMoin, T_Heasb.NameHesab AS NameHesabMoin, T_Heasb.HESABID, T_GropHesab.GoNumber, 
                      T_GropHesab.NameGo AS NameGroup
FROM         T_GropHesab INNER JOIN
                      [TRANSACTION] ON T_GropHesab.GoID = [TRANSACTION].GOID INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (1 = 1) '
			 
	SET @GROUPBy = ' GROUP BY T_KolHesab.NameHesab, [TRANSACTION].KOLID, T_KolHesab.KolNumber, T_GropHesab.GoID, T_GropHesab.NameGo, T_Heasb.HESABNumber, T_Heasb.NameHesab, T_Heasb.HESABID, 
                      T_GropHesab.GoNumber	 '
	SET @OrderBy = ' '


	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
    SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
       
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END



GO




/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_BedonSotoh_Rep1]    Script Date: 06/25/2020 12:52:58 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubTreeViewRep_Taraz_BedonSotoh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubTreeViewRep_Taraz_BedonSotoh_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_BedonSotoh_Rep1]    Script Date: 06/25/2020 12:52:58 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[QSubTreeViewRep_Taraz_BedonSotoh_Rep1] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'

                      
	SET @SQLQuery = ' SELECT     T_KolHesab.NameHesab AS NameHesabKol, SUM([TRANSACTION].DARBed) AS Bedehkar, SUM([TRANSACTION].PARBes) AS Bestankar, T_KolHesab.KOLID, 
                      T_KolHesab.KolNumber AS CodeHesab, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes)
                       ELSE 0 END AS MandeBed, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) 
                      ELSE 0 END AS MandeBes, T_GropHesab.GoID, T_GropHesab.NameGo AS NameGroup
FROM         T_KolHesab INNER JOIN
                      [TRANSACTION] ON T_KolHesab.KOLID = [TRANSACTION].KOLID INNER JOIN
                      T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID
WHERE     (1 = 1) '
			 
	SET @GROUPBy = ' GROUP BY T_KolHesab.NameHesab, T_KolHesab.KOLID, T_KolHesab.KolNumber, T_GropHesab.GoID, T_GropHesab.NameGo	 '
	SET @OrderBy = ' '


	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
    SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
       
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END



GO




/****** Object:  StoredProcedure [dbo].[QSubPishPFrm_Foroosh_Rep1]    Script Date: 06/25/2020 12:54:08 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPishPFrm_Foroosh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPishPFrm_Foroosh_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPishPFrm_Foroosh_Rep1]    Script Date: 06/25/2020 12:54:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPishPFrm_Foroosh_Rep1]
		
		@Userid INT,	
		@FOROSHID int

AS

BEGIN
	
SELECT  T_TarSPish.TedadFo AS Tedad, T_TarSPish.UnitPriceFo AS netprice, CASE WHEN isnull(T_PishFactor.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_PishFactor.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_PishFactor.THESABID AS CodeTafsili, T_PishFactor.KOLID AS CodeKol, T_PishFactor.HesabID AS CodeMoin, T_TarSPish.FieldN3 AS Mablagh, T_PishFactor.Takhfif, 
               T_PishFactor.FieldT3 AS TarikhEtebar, T_PishFactor.DateFo AS Tarikh, T_TarSPish.Sharh AS SharhRadif, T_PishFactor.Sharh AS TozihatFactor, T_NameVahed.NameVahed AS NameVahedAsli, 
               T_NameVahed_1.NameVahed AS NameVahedFarei, T_NameKala.Zarib, T_NameKala.BarCode, T_NameKala.KalaNumber AS CodeKala, T_PishFactor.ForoshNumbr AS Serial, 
               T_PishFactor.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, 
               T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, 
               T_NameKala.NameKala1, T_PishFactor.FOROSHID, T_NameKala.vaznKala
FROM     T_KolHesab RIGHT OUTER JOIN
               T_Heasb RIGHT OUTER JOIN
               T_T_Heasb RIGHT OUTER JOIN
               T_NameVahed AS T_NameVahed_1 RIGHT OUTER JOIN
               T_PishFactor INNER JOIN
               T_TarSPish INNER JOIN
               T_NameKala ON T_TarSPish.KALAID = T_NameKala.ID ON T_PishFactor.FOROSHID = T_TarSPish.FOROSHID LEFT OUTER JOIN
               T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 ON T_NameVahed_1.VAHED1 = T_NameKala.VAHED2 ON T_T_Heasb.THESABID = T_PishFactor.THESABID ON 
               T_Heasb.HESABID = T_PishFactor.HesabID ON T_KolHesab.KOLID = T_PishFactor.KOLID
WHERE  (T_PishFactor.FOROSHID = @FOROSHID)
               
               
END







GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdariArzi_Rep1]    Script Date: 06/25/2020 12:55:22 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdariArzi_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdariArzi_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdariArzi_Rep1]    Script Date: 06/25/2020 12:55:22 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



Create  PROCEDURE [dbo].[QSubPFrm_SanadHesabdariArzi_Rep1]
		
		@Userid INT,@SAID2 INT

AS

BEGIN

SELECT     T_SA2.SAID2Number AS ShomareSanad, T_SA2.Sharh AS SharhSanad, T_SA2.Date2 AS Tarikh, T_SA2.OperationCode AS Refrence, [TRANSACTION].radif, [TRANSACTION].Sharh AS SharhRadif, 
                      [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].DARBed AS Bedehkar, CASE WHEN [TRANSACTION].Thesabid <> NULL 
                      THEN T_T_Heasb.NameHesabT WHEN [TRANSACTION].hesabid <> NULL THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameHesab, 
                      T_T_Heasb.NameHesabT AS NameTafsili, T_Heasb.NameHesab AS NameMoin, T_KolHesab.NameHesab AS NameKol, T_Heasb.HESABNumber AS CodeMoin, 
                      T_T_Heasb.THESABNumber AS CodeTafsili, T_KolHesab.KolNumber AS CodeKol, T_SA2.SAID2, [TRANSACTION].CurRef AS NoeArz, - (1 * [TRANSACTION].CurRate) AS NerkhArz, 
                      [TRANSACTION].DARBedCur - [TRANSACTION].PARBesCur AS MablaghArz, Currency.Title AS NameArz, [TRANSACTION].RadifSAID2 AS Radifsanad
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      Currency ON [TRANSACTION].CurRef = Currency.CurrencyID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (T_SA2.SAID2 = @SAID2)
                      
END




GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3]    Script Date: 06/25/2020 12:56:18 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3]    Script Date: 06/25/2020 12:56:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)
                 

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'



	SET @SQLQuery = ' SELECT 1 AS RowNumber,0 AS SelectedRow,
     SUM(PARBes) AS SumPARBes, SUM(DARBed) AS SumDARBed, NameHesabTSath2, CASE WHEN (NameHesabT IS NOT NULL) THEN NameHesabT WHEN (NameHesabMoin IS NOT NULL) 
                      THEN NameHesabMoin ELSE NameHesab END AS NameHesabT1, CASE WHEN (THESABNumber IS NOT NULL) THEN THESABNumber WHEN (HESABNumber IS NOT NULL) 
                      THEN HESABNumber ELSE KolNumber END AS CodHesab, THESABNumberSath2, GOID, KOLID, HESABID, NameGo, NameHesab, KolNumber, HESABNumber, NameHesabMoin, THESABNumber, 
                      NameHesabT, CodGroup, THESABID, ThesabIDSath2, CASE WHEN (SUM(DARBed) - SUM(PARBes)) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS MandeBed, 
				CASE WHEN (SUM(PARBes) - SUM(DARBed)) > 0 THEN (SUM(PARBes) - SUM(DARBed)) ELSE 0 END AS MandeBes
FROM         View_TRANSACTION where 1=1 '
			 
	SET @GROUPBy = ' GROUP BY NameHesabTSath2, THESABNumberSath2, GOID, KOLID, HESABID, NameGo, NameHesab, KolNumber, HESABNumber, NameHesabMoin, THESABNumber, NameHesabT, CodGroup, THESABID, 
                      ThesabIDSath2 '
	SET @OrderBy = ' '

	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END



GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_Rep1]    Script Date: 06/25/2020 12:57:05 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdari_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_Rep1]    Script Date: 06/25/2020 12:57:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_Rep1]
		
		@Userid INT,
		@SAID2 NVARCHAR(MAX),
		@StarteDate NVARCHAR(10),
		@ENDDate NVARCHAR(10),
		@Said2AsShomare NVARCHAR(MAX), 
		@Said2TaShomare NVARCHAR(MAX)
AS

BEGIN


SET NOCOUNT ON;

Declare @SQLQuery AS NVarchar(MAX)
Declare @ParamDefinition AS NVarchar(MAX) 
DECLARE @OrderBy AS NVARCHAR(MAX)


IF (@StarteDate IS NULL)
		SET @StarteDate='0'

IF (@ENDDate IS NULL)
		SET @ENDDate='99999999'
			
SET @SQLQuery = ' SELECT     T_SA2.SAID2Number AS ShomareSanad, T_SA2.Sharh AS SharhSanad, T_SA2.Date2 AS Tarikh, T_SA2.OperationCode AS Refrence, [TRANSACTION].radif, [TRANSACTION].Sharh AS SharhRadif, 
                      [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].DARBed AS Bedehkar, CASE WHEN [TRANSACTION].Thesabid <> NULL 
                      THEN T_T_Heasb.NameHesabT WHEN [TRANSACTION].hesabid <> NULL THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameHesab, 
                      T_T_Heasb.NameHesabT AS NameTafsili, T_Heasb.NameHesab AS NameMoin, T_KolHesab.NameHesab AS NameKol, T_Heasb.HESABNumber AS CodeMoin, 
                      T_T_Heasb.THESABNumber AS CodeTafsili, T_KolHesab.KolNumber AS CodeKol, T_SA2.SAID2, [TRANSACTION].RadifSAID2 AS RadifSanad, ISNULL(T_Heasb.NameHesab, N'''') 
                      + CASE WHEN DARBed > 0 THEN ''1'' ELSE ''2'' END AS GrpMoin
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (1 = 1) '

SET @OrderBy = ' ORDER BY T_SA2.SAID2 DESC  '

    /* check for the condition and build the WHERE clause accordingly*/ 
    	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@SAID2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (T_SA2.SAID2 IN('+ @SAID2 +'))'
    ELSE    
    	BEGIN
			
			 Set @SQLQuery = @SQLQuery + ' And (T_SA2.Date2 Between @StarteDate AND @ENDDate )'
		    
			If ((@Said2AsShomare Is NOT NULL) AND (@Said2TaShomare Is NOT NULL))
				 Set @SQLQuery = @SQLQuery + ' And (T_SA2.SAID2Number Between @Said2AsShomare AND @Said2TaShomare )'
 
		END				    

   

   Set @SQLQuery = @SQLQuery 



    Set @ParamDefinition = '@UserID INT,
							@SAID2 NVARCHAR(MAX),
							@StarteDate NVARCHAR(10), 
							@ENDDate NVARCHAR(10), 
							@Said2AsShomare NVARCHAR(MAX), 
							@Said2TaShomare NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery +  char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@SAID2,
							@StarteDate , 
							@ENDDate , 
							@Said2AsShomare , 
							@Said2TaShomare 


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)


END

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepTH1_Rep1]    Script Date: 06/25/2020 12:57:40 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_RepTH1_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_RepTH1_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepTH1_Rep1]    Script Date: 06/25/2020 12:57:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






CREATE  PROCEDURE [dbo].[QSubPFrm_RepTH1_Rep1]
		
		@Userid INT,
		@Date1 NVARCHAR(10),
		@Date2 NVARCHAR(10),
		@THESABID INT,
		@TYPID INT

AS
BEGIN
	

IF (@TYPID=102 ) /* گزارش دفتر تفصيلي */
	
SELECT  T_T_Heasb.NameHesabT AS NameHesabTafsili, [TRANSACTION].TextShSA AS SharhRadif, DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBed AS Bedehkar, [TRANSACTION].PARBes AS Bestankar, 
               [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_T_Heasb.THESABID, T_T_Heasb.THESABNumber AS CodeHesab, [TRANSACTION].RadifSAID2
FROM     [TRANSACTION] INNER JOIN
               T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
               T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                   (SELECT  SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID
                    FROM     [TRANSACTION] AS TRANSACTION_1
                    WHERE  (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                    GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.THESABID
WHERE  (T_T_Heasb.THESABID = @THESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)

                             
ELSE

	IF (@TYPID=115  ) /* گزارش حساب تفصيلي ارزی */
	
SELECT  DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBedCur AS Bedehkar, [TRANSACTION].PARBesCur AS Bestankar, [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, 
               [TRANSACTION].TextShSA AS SharhRadif, [TRANSACTION].PARBesCur AS BestankarArzi, [TRANSACTION].DARBedCur AS BedehkarArzi, [TRANSACTION].CurRef AS NoeArz, [TRANSACTION].CurRate AS NerkhArz, 
               T_SA2.Sharh AS SharhSanad, Currency.Title AS NameArz, T_T_Heasb.THESABID, T_T_Heasb.THESABNumber AS CodeTafsili, T_T_Heasb.NameHesabT AS NameHesabTafsili, [TRANSACTION].RadifSAID2
FROM     [TRANSACTION] INNER JOIN
               T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
               Currency ON [TRANSACTION].CurRef = Currency.CurrencyID INNER JOIN
               T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                   (SELECT  SUM(DARBedCur) - SUM(PARBesCur) AS mande, SUM(PARBesCur) AS SumOfPARBes, SUM(DARBedCur) AS SumOfDARBed, THESABID
                    FROM     [TRANSACTION] AS TRANSACTION_1
                    WHERE  (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                    GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.THESABID
WHERE  (T_T_Heasb.THESABID = @THESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)

  ELSE

	IF (@TYPID=116  )        /* گزارش حساب تفصيلي ارزی و ریالی */                    
			SELECT  DERIVEDTBL.mande AS MandeHesab, TRANSACTION_2.DARBed AS Bedehkar, TRANSACTION_2.PARBes AS Bestankar, TRANSACTION_2.Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, 
               TRANSACTION_2.TextShSA AS SharhRadif, TRANSACTION_2.PARBesCur AS BestankarArzi, TRANSACTION_2.DARBedCur AS BedehkarArzi, TRANSACTION_2.CurRef AS NoeArz, 
               TRANSACTION_2.CurRate AS NerkhArz, T_SA2.Sharh AS SharhSanad, Currency.Title AS NameArz, TBL_MandeArz.mandeCur AS mandeArz, T_T_Heasb.THESABID, T_T_Heasb.THESABNumber AS CodeTafsili, 
               T_T_Heasb.NameHesabT AS NameHesabTafsili, TRANSACTION_2.RadifSAID2
FROM     (SELECT  SUM(DARBedCur) - SUM(PARBesCur) AS mandeCur, SUM(PARBesCur) AS SumOfPARBesCur, SUM(DARBedCur) AS SumOfDARBedCur, THESABID
                FROM     [TRANSACTION]
                WHERE  (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                GROUP BY THESABID) AS TBL_MandeArz RIGHT OUTER JOIN
               [TRANSACTION] AS TRANSACTION_2 INNER JOIN
               T_SA2 ON TRANSACTION_2.SAID2 = T_SA2.SAID2 INNER JOIN
               T_T_Heasb ON TRANSACTION_2.THESABID = T_T_Heasb.THESABID ON TBL_MandeArz.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                   (SELECT  SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID
                    FROM     [TRANSACTION] AS TRANSACTION_1
                    WHERE  (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                    GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.THESABID LEFT OUTER JOIN
               Currency ON TRANSACTION_2.CurRef = Currency.CurrencyID
WHERE  (T_T_Heasb.THESABID = @THESABID) AND (TRANSACTION_2.Date1 BETWEEN @Date1 AND @DATE2) AND (NOT (TRANSACTION_2.SAID2 IS NULL))


END








GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepMo1_Rep1]    Script Date: 06/25/2020 12:58:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_RepMo1_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_RepMo1_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepMo1_Rep1]    Script Date: 06/25/2020 12:58:32 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE  PROCEDURE [dbo].[QSubPFrm_RepMo1_Rep1]
		
		@Userid INT,
		@Date1 NVARCHAR(10),
		@Date2 NVARCHAR(10),
		@HESABID INT,
		@TYPID INT

AS
BEGIN
	

IF (@TYPID=101 ) /* گزارش دفتر معين */
	
	
	SELECT     T_Heasb.NameHesab AS NameHesabmoin, [TRANSACTION].TextShSA AS SharhRadif, DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBed AS Bedehkar, 
						  [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_Heasb.HESABID, T_Heasb.HESABNumber AS CodeHesab
	FROM         [TRANSACTION] INNER JOIN
						  T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
						  T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID LEFT OUTER JOIN
							  (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, HESABID
								 FROM         [TRANSACTION]
								 WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
								 GROUP BY HESABID) AS DERIVEDTBL ON T_Heasb.HESABID = DERIVEDTBL.HESABID
	WHERE     (T_Heasb.HESABID = @HESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)

                             
ELSE

	IF (@TYPID=112  ) /* گزارش حساب معين ارزی */
	
			SELECT     DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBedCur AS Bedehkar, [TRANSACTION].PARBesCur AS Bestankar, [TRANSACTION].Date1 AS Tarikh, 
								  T_SA2.SAID2Number AS ShomareSanad, [TRANSACTION].TextShSA AS SharhRadif, [TRANSACTION].PARBesCur AS BestankarArzi, [TRANSACTION].DARBedCur AS BedehkarArzi, 
								  [TRANSACTION].CurRef AS NoeArz, [TRANSACTION].CurRate AS NerkhArz, T_SA2.Sharh AS SharhSanad, Currency.Title AS NameArz, T_Heasb.HESABID, T_Heasb.HESABNumber AS CodeMoin, 
								  T_Heasb.NameHesab AS NameHesabMoin
			FROM         [TRANSACTION] INNER JOIN
								  T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
								  Currency ON [TRANSACTION].CurRef = Currency.CurrencyID INNER JOIN
								  T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID LEFT OUTER JOIN
									  (SELECT     SUM(DARBedCur) - SUM(PARBesCur) AS mande, SUM(PARBesCur) AS SumOfPARBes, SUM(DARBedCur) AS SumOfDARBed, HESABID
										 FROM         [TRANSACTION]
										 WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
										 GROUP BY HESABID) AS DERIVEDTBL ON T_Heasb.HESABID = DERIVEDTBL.HESABID
			WHERE     (T_Heasb.HESABID = @HESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)

  ELSE

	IF (@TYPID=113  )        /* گزارش حساب معین ارزی و ریالی */                    
			SELECT     DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBed AS Bedehkar, [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, 
								  [TRANSACTION].TextShSA AS SharhRadif, [TRANSACTION].PARBesCur AS BestankarArzi, [TRANSACTION].DARBedCur AS BedehkarArzi, [TRANSACTION].CurRef AS NoeArz, 
								  [TRANSACTION].CurRate AS NerkhArz, T_SA2.Sharh AS SharhSanad, Currency.Title AS NameArz, TBL_MandeArz.mandeCur AS mandeArz, T_Heasb.HESABID, T_Heasb.HESABNumber AS CodeMoin, 
								  T_Heasb.NameHesab AS NameHesabMoin
			FROM         (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, HESABID
									FROM         [TRANSACTION]
									WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
									GROUP BY HESABID) AS DERIVEDTBL RIGHT OUTER JOIN
								  [TRANSACTION] INNER JOIN
								  T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
								  T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID LEFT OUTER JOIN
									  (SELECT     SUM(DARBedCur) - SUM(PARBesCur) AS mandeCur, SUM(PARBesCur) AS SumOfPARBesCur, SUM(DARBedCur) AS SumOfDARBedCur, HESABID
										 FROM         [TRANSACTION]
										 WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
										 GROUP BY HESABID) AS TBL_MandeArz ON T_Heasb.HESABID = TBL_MandeArz.HESABID ON DERIVEDTBL.HESABID = T_Heasb.HESABID LEFT OUTER JOIN
								  Currency ON [TRANSACTION].CurRef = Currency.CurrencyID
			WHERE     (T_Heasb.HESABID = @HESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @DATE2) AND (NOT ([TRANSACTION].SAID2 IS NULL))


END







GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepKol1_Rep1]    Script Date: 06/25/2020 12:59:06 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_RepKol1_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_RepKol1_Rep1]
GO

/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepKol1_Rep1]    Script Date: 06/25/2020 12:59:06 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE  PROCEDURE [dbo].[QSubPFrm_RepKol1_Rep1]
		
		@Userid INT,	
		@Date1 NVARCHAR(10),
		@Date2 NVARCHAR(10),
		@KOLID INT,
		@TYPID INT

AS
BEGIN
	

IF (@TYPID=100 ) /* گزارش دفتر کل */
	
	
SELECT     T_KolHesab.NameHesab AS NameHesabKol, DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBed AS Bedehkar, [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].Date1 AS Tarikh, 
                      T_SA2.SAID2Number AS ShomareSanad, T_KolHesab.KOLID, T_KolHesab.KolNumber AS CodeHesab, [TRANSACTION].TextShSA AS SharhRadif
FROM         T_KolHesab INNER JOIN
                      [TRANSACTION] ON T_KolHesab.KOLID = [TRANSACTION].KOLID INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, KOLID
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @date1)
                             GROUP BY KOLID) AS DERIVEDTBL ON T_KolHesab.KOLID = DERIVEDTBL.KOLID
WHERE     (T_KolHesab.KOLID = @Kolid) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)
                             
ELSE

	IF (@TYPID=110  ) /* گزارش حساب کل ارزی */
		SELECT     T_KolHesab.NameHesab AS NameHesabKol, DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBedCur AS Bedehkar, [TRANSACTION].PARBesCur AS Bestankar, 
							  [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_KolHesab.KOLID, T_KolHesab.KolNumber AS CodeHesab, [TRANSACTION].TextShSA AS SharhRadif, 
							  [TRANSACTION].PARBesCur AS BestankarArzi, [TRANSACTION].DARBedCur AS BedehkarArzi, [TRANSACTION].CurRef AS NoeArz, [TRANSACTION].CurRate AS NerkhArz, T_SA2.Sharh AS SharhSanad, 
							  Currency.Title AS NameArz
		FROM         [TRANSACTION] INNER JOIN
							  T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
							  Currency ON [TRANSACTION].CurRef = Currency.CurrencyID INNER JOIN
							  T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
								  (SELECT     SUM(DARBedCur) - SUM(PARBesCur) AS mande, SUM(PARBesCur) AS SumOfPARBes, SUM(DARBedCur) AS SumOfDARBed, KOLID
									 FROM         [TRANSACTION] AS TRANSACTION_1
									 WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @date1)
									 GROUP BY KOLID) AS DERIVEDTBL ON T_KolHesab.KOLID = DERIVEDTBL.KOLID
		WHERE     (T_KolHesab.KOLID = @Kolid) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)

  ELSE

	IF (@TYPID=111  )        /* گزارش حساب کل ارزی و ریالی */                    
						SELECT     T_KolHesab.NameHesab AS NameHesabKol, DERIVEDTBL.mande AS MandeHesab, TRANSACTION_2.DARBed AS Bedehkar, TRANSACTION_2.PARBes AS Bestankar, 
						TRANSACTION_2.Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_KolHesab.KOLID, T_KolHesab.KolNumber AS CodeHesab, TRANSACTION_2.TextShSA AS SharhRadif, 
						TRANSACTION_2.PARBesCur AS BestankarArzi, TRANSACTION_2.DARBedCur AS BedehkarArzi, TRANSACTION_2.CurRef AS NoeArz, TRANSACTION_2.CurRate AS NerkhArz, 
						T_SA2.Sharh AS SharhSanad, Currency.Title AS NameArz, TBL_MandeArz.mandeCur AS mandeArz, T_SA2.SAID2
						FROM         (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, KOLID
						FROM         [TRANSACTION]
						WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @date1)
						GROUP BY KOLID) AS DERIVEDTBL RIGHT OUTER JOIN
						[TRANSACTION] AS TRANSACTION_2 INNER JOIN
						T_SA2 ON TRANSACTION_2.SAID2 = T_SA2.SAID2 INNER JOIN
						T_KolHesab ON TRANSACTION_2.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
						(SELECT     SUM(DARBedCur) - SUM(PARBesCur) AS mandeCur, SUM(PARBesCur) AS SumOfPARBesCur, SUM(DARBedCur) AS SumOfDARBedCur, KOLID
						FROM         [TRANSACTION] AS TRANSACTION_1
						WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @date1)
						GROUP BY KOLID) AS TBL_MandeArz ON T_KolHesab.KOLID = TBL_MandeArz.KOLID ON DERIVEDTBL.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
						Currency ON TRANSACTION_2.CurRef = Currency.CurrencyID
						WHERE     (T_KolHesab.KOLID = @Kolid) AND (TRANSACTION_2.Date1 BETWEEN @daTE1 AND @DATE2) AND (NOT (TRANSACTION_2.SAID2 IS NULL))
				
	

END







GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Pardakht_Rep1]    Script Date: 06/25/2020 12:59:39 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Pardakht_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Pardakht_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Pardakht_Rep1]    Script Date: 06/25/2020 12:59:39 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_Pardakht_Rep1]
		
		@Userid INT,@PardakhtID INT

AS

BEGIN

SELECT     T_Heasb.HESABNumber AS CodeMoin, T_KolHesab.KolNumber AS CodeKol, T_T_Heasb.THESABNumber AS CodeTafsili, [TRANSACTION].DARBed AS mablagh, T_Par1.DATE2 AS Tarikh, 
                      T_Par1.TextSharh AS Sharh, T_Par1.PARID1Number AS Serial, CASE WHEN isnull([TRANSACTION].THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull([TRANSACTION].HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Par1.PARID1
FROM         [TRANSACTION] INNER JOIN
                      T_Par1 ON [TRANSACTION].PARID1 = T_Par1.PARID1 LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID LEFT OUTER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID
WHERE     ([TRANSACTION].DARBed > 0) AND (T_Par1.PARID1 = @PardakhtID)


END




GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_ForooshSandogh_Rep1]    Script Date: 06/25/2020 13:00:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_ForooshSandogh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_ForooshSandogh_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_ForooshSandogh_Rep1]    Script Date: 06/25/2020 13:00:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



create  PROCEDURE [dbo].[QSubPFrm_ForooshSandogh_Rep1]
		@Userid INT,	
		@FOROSHID int

AS
BEGIN
	
SELECT     [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
                      CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, 
                      T_Forosh.KOLID AS CodeKol, T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, T_Forosh.Takhfif, T_Forosh.DateFo AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, 
                      T_Forosh.Sharh AS TozihatFactor, T_NameVahed.NameVahed AS NameVahedAsli, T_NameVahed_1.NameVahed AS NameVahedFarei, T_NameKala.Zarib, T_NameKala.BarCode, 
                      T_NameKala.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, T_Forosh.NumberKH AS ShomareFactor, 
                      T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, 
                      T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_NameKala.NameKala1, T_Forosh.FOROSHID, 
                      DERIVEDTBL.mande AS MandeHesab, ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(dbo.T_NameKala.Zarib, 0) = 0 THEN 1 ELSE dbo.T_NameKala.Zarib END, 2) AS tedadFarei, 
                      [TRANSACTION].radif, T_Anbar.AIDNumber AS CodeAnbar, T_Anbar.NameAnbar, T_Anbar.Sharh AS AddressAnbar
FROM         T_NameVahed RIGHT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 2) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL RIGHT OUTER JOIN
                      T_KolHesab RIGHT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) 
                                                   AS THESAB
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL RIGHT OUTER JOIN
                      T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID INNER JOIN
                          (SELECT     ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID
                             FROM         T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID INNER JOIN
                      T_Anbar ON [TRANSACTION].AID = T_Anbar.AID ON DERIVEDTBL.KOLID = T_Forosh.KOLID AND DERIVEDTBL.HESAB = T_Forosh.Hesab AND 
                      DERIVEDTBL.THESAB = T_Forosh.THesab LEFT OUTER JOIN
                      T_Heasb ON T_Forosh.Hesab = T_Heasb.HESABID LEFT OUTER JOIN
                      T_T_Heasb ON T_Forosh.THesab = T_T_Heasb.THESABID ON T_KolHesab.KOLID = T_Forosh.KOLID ON TBL.FactID = T_Forosh.FOROSHID ON 
                      T_NameVahed.VAHED1 = T_NameKala.VAHED1 LEFT OUTER JOIN
                      T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1
WHERE     (T_Forosh.FOROSHID = @FOROSHID)
END







GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Daryaft_Rep1]    Script Date: 06/25/2020 13:00:46 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Daryaft_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Daryaft_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Daryaft_Rep1]    Script Date: 06/25/2020 13:00:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_Daryaft_Rep1]
		
		@Userid INT,@DaryaftID INT

AS

BEGIN

SELECT     T_Heasb.HESABNumber AS CodeMoin, T_KolHesab.KolNumber AS CodeKol, T_T_Heasb.THESABNumber AS CodeTafsili, [TRANSACTION].PARBes AS mablagh, T_Dar1.DATE2 AS Tarikh, 
                      T_Dar1.TextSharh AS Sharh, T_Dar1.DAR1Numbr AS Serial, CASE WHEN isnull([TRANSACTION].THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull([TRANSACTION].HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Dar1.DARID1
FROM         T_Dar1 INNER JOIN
                      [TRANSACTION] ON T_Dar1.DARID1 = [TRANSACTION].DARID1 LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID LEFT OUTER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID
WHERE     ([TRANSACTION].PARBes > 0) AND (T_Dar1.DARID1 = @DaryaftID)


END




GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]    Script Date: 06/25/2020 13:01:18 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]    Script Date: 06/25/2020 13:01:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]
		
		@Userid INT,
		@SAID2 NVARCHAR(MAX),
		@StarteDate NVARCHAR(10),
		@ENDDate NVARCHAR(10),
		@Said2AsShomare NVARCHAR(MAX), 
		@Said2TaShomare NVARCHAR(MAX)
AS

BEGIN


SET NOCOUNT ON;

Declare @SQLQuery AS NVarchar(MAX)
Declare @ParamDefinition AS NVarchar(MAX) 
DECLARE @OrderBy AS NVARCHAR(MAX)


IF (@StarteDate IS NULL)
		SET @StarteDate='0'

IF (@ENDDate IS NULL)
		SET @ENDDate='99999999'
			
SET @SQLQuery = ' SELECT     T_SA2.SAID2Number AS ShomareSanad, T_SA2.Sharh AS SharhSanad, T_SA2.Date2 AS Tarikh, T_SA2.OperationCode AS Refrence, [TRANSACTION].RadifSAID2 as radif, [TRANSACTION].Sharh AS SharhRadif, 
                      [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].DARBed AS Bedehkar, CASE WHEN [TRANSACTION].Thesabid <> NULL 
                      THEN T_T_Heasb.NameHesabT WHEN [TRANSACTION].hesabid <> NULL THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameHesab, 
                      T_T_Heasb.NameHesabT AS NameTafsili, T_Heasb.NameHesab AS NameMoin, T_KolHesab.NameHesab AS NameKol, T_Heasb.HESABNumber AS CodeMoin, 
                      T_T_Heasb.THESABNumber AS CodeTafsili, T_KolHesab.KolNumber AS CodeKol, T_SA2.SAID2, [TRANSACTION].RadifSAID2 AS RadifSanad, ISNULL([TRANSACTION].ThesabIDSath2, [TRANSACTION].KOLID) 
                      + CASE WHEN DARBed > 0 THEN ''1'' ELSE ''2'' END AS GrpThesabIDSath2, [TRANSACTION].ThesabIDSath2, T_T_HeasbSath2.THESABNumber AS THESABNumberSath2, 
                      T_T_HeasbSath2.NameHesabT AS NameHesabTSath2, 
                      CASE WHEN (NOT([TRANSACTION].THESABIDSath2 IS NULL)) THEN T_T_Heasb.NameHesabT + N'' * * '' + T_T_HeasbSath2.NameHesabT 
                      WHEN (Not ([TRANSACTION].THESABID IS NULL) ) THEN  T_T_Heasb.NameHesabT WHEN (Not ([TRANSACTION].HESABID IS NULL) ) THEN  T_Heasb.NameHesab ELSE NULL END AS NameHesab0, 
                      
                      CASE WHEN (NOT([TRANSACTION].THESABIDSath2 IS NULL)) THEN  T_T_HeasbSath2.THESABNumber
                      WHEN (Not ([TRANSACTION].THESABID IS NULL) ) THEN T_T_Heasb.THESABNumber  WHEN (Not ([TRANSACTION].HESABID IS NULL) ) THEN T_Heasb.HESABNumber ELSE NULL END AS CodHesab0,
                                            CASE WHEN (NOT([TRANSACTION].THESABIDSath2 IS NULL)) THEN  Abs([TRANSACTION].DARBed)+Abs([TRANSACTION].PARBes)
                      WHEN (Not ([TRANSACTION].THESABID IS NULL) ) THEN Abs([TRANSACTION].DARBed)+Abs([TRANSACTION].PARBes) WHEN (Not ([TRANSACTION].HESABID IS NULL) ) THEN Abs([TRANSACTION].DARBed)+Abs([TRANSACTION].PARBes) ELSE 0 END AS MabJoz
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_HeasbSath2 ON [TRANSACTION].ThesabIDSath2 = T_T_HeasbSath2.THESABID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (1 = 1) '

SET @OrderBy = ' ORDER BY T_SA2.SAID2 DESC  '

    /* check for the condition and build the WHERE clause accordingly*/ 
    	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@SAID2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (T_SA2.SAID2 IN('+ @SAID2 +'))'
    ELSE    
    	BEGIN
			
			 Set @SQLQuery = @SQLQuery + ' And (T_SA2.Date2 Between @StarteDate AND @ENDDate )'
		    
			If ((@Said2AsShomare Is NOT NULL) AND (@Said2TaShomare Is NOT NULL))
				 Set @SQLQuery = @SQLQuery + ' And (T_SA2.SAID2Number Between @Said2AsShomare AND @Said2TaShomare )'
 
		END				    

   

   Set @SQLQuery = @SQLQuery 



    Set @ParamDefinition = '@UserID INT,
							@SAID2 NVARCHAR(MAX),
							@StarteDate NVARCHAR(10), 
							@ENDDate NVARCHAR(10), 
							@Said2AsShomare NVARCHAR(MAX), 
							@Said2TaShomare NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery +  char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@SAID2,
							@StarteDate , 
							@ENDDate , 
							@Said2AsShomare , 
							@Said2TaShomare 


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)


END

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]    Script Date: 06/25/2020 13:02:02 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]    Script Date: 06/25/2020 13:02:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]
		
		@Userid INT,
		@SAID2 NVARCHAR(MAX),
		@StarteDate NVARCHAR(10),
		@ENDDate NVARCHAR(10),
		@Said2AsShomare NVARCHAR(MAX), 
		@Said2TaShomare NVARCHAR(MAX)
AS

BEGIN


SET NOCOUNT ON;

Declare @SQLQuery AS NVarchar(MAX)
Declare @ParamDefinition AS NVarchar(MAX) 
DECLARE @OrderBy AS NVARCHAR(MAX)


IF (@StarteDate IS NULL)
		SET @StarteDate='0'

IF (@ENDDate IS NULL)
		SET @ENDDate='99999999'
			
SET @SQLQuery = ' SELECT     T_SA2.SAID2Number AS ShomareSanad, T_SA2.Sharh AS SharhSanad, T_SA2.Date2 AS Tarikh, T_SA2.OperationCode AS Refrence, [TRANSACTION].RadifSAID2 as radif, [TRANSACTION].Sharh AS SharhRadif, 
                      [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].DARBed AS Bedehkar, CASE WHEN [TRANSACTION].Thesabid <> NULL 
                      THEN T_T_Heasb.NameHesabT WHEN [TRANSACTION].hesabid <> NULL THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameHesab, 
                      T_T_Heasb.NameHesabT AS NameTafsili, T_Heasb.NameHesab AS NameMoin, T_KolHesab.NameHesab AS NameKol, T_Heasb.HESABNumber AS CodeMoin, 
                      T_T_Heasb.THESABNumber AS CodeTafsili, T_KolHesab.KolNumber AS CodeKol, T_SA2.SAID2, [TRANSACTION].RadifSAID2 AS RadifSanad, ISNULL([TRANSACTION].ThesabIDSath2, [TRANSACTION].KOLID) 
                      + CASE WHEN DARBed > 0 THEN ''1'' ELSE ''2'' END AS GrpThesabIDSath2, [TRANSACTION].ThesabIDSath2, T_T_HeasbSath2.THESABNumber AS THESABNumberSath2, 
                      T_T_HeasbSath2.NameHesabT AS NameHesabTSath2, 
                      CASE WHEN (NOT([TRANSACTION].THESABIDSath2 IS NULL)) THEN T_T_Heasb.NameHesabT + N'' * * '' + T_T_HeasbSath2.NameHesabT 
                      WHEN (Not ([TRANSACTION].THESABID IS NULL) ) THEN  T_T_Heasb.NameHesabT WHEN (Not ([TRANSACTION].HESABID IS NULL) ) THEN  T_Heasb.NameHesab ELSE NULL END AS NameHesab0, 
                      
                      CASE WHEN (NOT([TRANSACTION].THESABIDSath2 IS NULL)) THEN  T_T_HeasbSath2.THESABNumber
                      WHEN (Not ([TRANSACTION].THESABID IS NULL) ) THEN T_T_Heasb.THESABNumber  WHEN (Not ([TRANSACTION].HESABID IS NULL) ) THEN T_Heasb.HESABNumber ELSE NULL END AS CodHesab0,
                                            CASE WHEN (NOT([TRANSACTION].THESABIDSath2 IS NULL)) THEN  Abs([TRANSACTION].DARBed)+Abs([TRANSACTION].PARBes)
                      WHEN (Not ([TRANSACTION].THESABID IS NULL) ) THEN Abs([TRANSACTION].DARBed)+Abs([TRANSACTION].PARBes) WHEN (Not ([TRANSACTION].HESABID IS NULL) ) THEN Abs([TRANSACTION].DARBed)+Abs([TRANSACTION].PARBes) ELSE 0 END AS MabJoz
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_HeasbSath2 ON [TRANSACTION].ThesabIDSath2 = T_T_HeasbSath2.THESABID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (1 = 1) '

SET @OrderBy = ' ORDER BY T_SA2.SAID2 DESC  '

    /* check for the condition and build the WHERE clause accordingly*/ 
    	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@SAID2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (T_SA2.SAID2 IN('+ @SAID2 +'))'
    ELSE    
    	BEGIN
			
			 Set @SQLQuery = @SQLQuery + ' And (T_SA2.Date2 Between @StarteDate AND @ENDDate )'
		    
			If ((@Said2AsShomare Is NOT NULL) AND (@Said2TaShomare Is NOT NULL))
				 Set @SQLQuery = @SQLQuery + ' And (T_SA2.SAID2Number Between @Said2AsShomare AND @Said2TaShomare )'
 
		END				    

   

   Set @SQLQuery = @SQLQuery 



    Set @ParamDefinition = '@UserID INT,
							@SAID2 NVARCHAR(MAX),
							@StarteDate NVARCHAR(10), 
							@ENDDate NVARCHAR(10), 
							@Said2AsShomare NVARCHAR(MAX), 
							@Said2TaShomare NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery +  char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@SAID2,
							@StarteDate , 
							@ENDDate , 
							@Said2AsShomare , 
							@Said2TaShomare 


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)


END

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]    Script Date: 06/25/2020 13:02:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]    Script Date: 06/25/2020 13:02:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_SanadHesabdariSath2_Rep1]
		
		@Userid INT,
		@SAID2 NVARCHAR(MAX),
		@StarteDate NVARCHAR(10),
		@ENDDate NVARCHAR(10),
		@Said2AsShomare NVARCHAR(MAX), 
		@Said2TaShomare NVARCHAR(MAX)
AS

BEGIN


SET NOCOUNT ON;

Declare @SQLQuery AS NVarchar(MAX)
Declare @ParamDefinition AS NVarchar(MAX) 
DECLARE @OrderBy AS NVARCHAR(MAX)


IF (@StarteDate IS NULL)
		SET @StarteDate='0'

IF (@ENDDate IS NULL)
		SET @ENDDate='99999999'
			
SET @SQLQuery = ' SELECT     T_SA2.SAID2Number AS ShomareSanad, T_SA2.Sharh AS SharhSanad, T_SA2.Date2 AS Tarikh, T_SA2.OperationCode AS Refrence, [TRANSACTION].RadifSAID2 as radif, [TRANSACTION].Sharh AS SharhRadif, 
                      [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].DARBed AS Bedehkar, CASE WHEN [TRANSACTION].Thesabid <> NULL 
                      THEN T_T_Heasb.NameHesabT WHEN [TRANSACTION].hesabid <> NULL THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameHesab, 
                      T_T_Heasb.NameHesabT AS NameTafsili, T_Heasb.NameHesab AS NameMoin, T_KolHesab.NameHesab AS NameKol, T_Heasb.HESABNumber AS CodeMoin, 
                      T_T_Heasb.THESABNumber AS CodeTafsili, T_KolHesab.KolNumber AS CodeKol, T_SA2.SAID2, [TRANSACTION].RadifSAID2 AS RadifSanad, ISNULL([TRANSACTION].ThesabIDSath2, [TRANSACTION].KOLID) 
                      + CASE WHEN DARBed > 0 THEN ''1'' ELSE ''2'' END AS GrpThesabIDSath2, [TRANSACTION].ThesabIDSath2, T_T_HeasbSath2.THESABNumber AS THESABNumberSath2, 
                      T_T_HeasbSath2.NameHesabT AS NameHesabTSath2, 
                      CASE WHEN (NOT([TRANSACTION].THESABIDSath2 IS NULL)) THEN T_T_Heasb.NameHesabT + N'' * * '' + T_T_HeasbSath2.NameHesabT 
                      WHEN (Not ([TRANSACTION].THESABID IS NULL) ) THEN  T_T_Heasb.NameHesabT WHEN (Not ([TRANSACTION].HESABID IS NULL) ) THEN  T_Heasb.NameHesab ELSE NULL END AS NameHesab0, 
                      
                      CASE WHEN (NOT([TRANSACTION].THESABIDSath2 IS NULL)) THEN  T_T_HeasbSath2.THESABNumber
                      WHEN (Not ([TRANSACTION].THESABID IS NULL) ) THEN T_T_Heasb.THESABNumber  WHEN (Not ([TRANSACTION].HESABID IS NULL) ) THEN T_Heasb.HESABNumber ELSE NULL END AS CodHesab0,
                                            CASE WHEN (NOT([TRANSACTION].THESABIDSath2 IS NULL)) THEN  Abs([TRANSACTION].DARBed)+Abs([TRANSACTION].PARBes)
                      WHEN (Not ([TRANSACTION].THESABID IS NULL) ) THEN Abs([TRANSACTION].DARBed)+Abs([TRANSACTION].PARBes) WHEN (Not ([TRANSACTION].HESABID IS NULL) ) THEN Abs([TRANSACTION].DARBed)+Abs([TRANSACTION].PARBes) ELSE 0 END AS MabJoz
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_HeasbSath2 ON [TRANSACTION].ThesabIDSath2 = T_T_HeasbSath2.THESABID LEFT OUTER JOIN
                      T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (1 = 1) '

SET @OrderBy = ' ORDER BY T_SA2.SAID2 DESC  '

    /* check for the condition and build the WHERE clause accordingly*/ 
    	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@SAID2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (T_SA2.SAID2 IN('+ @SAID2 +'))'
    ELSE    
    	BEGIN
			
			 Set @SQLQuery = @SQLQuery + ' And (T_SA2.Date2 Between @StarteDate AND @ENDDate )'
		    
			If ((@Said2AsShomare Is NOT NULL) AND (@Said2TaShomare Is NOT NULL))
				 Set @SQLQuery = @SQLQuery + ' And (T_SA2.SAID2Number Between @Said2AsShomare AND @Said2TaShomare )'
 
		END				    

   

   Set @SQLQuery = @SQLQuery 



    Set @ParamDefinition = '@UserID INT,
							@SAID2 NVARCHAR(MAX),
							@StarteDate NVARCHAR(10), 
							@ENDDate NVARCHAR(10), 
							@Said2AsShomare NVARCHAR(MAX), 
							@Said2TaShomare NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery +  char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@SAID2,
							@StarteDate , 
							@ENDDate , 
							@Said2AsShomare , 
							@Said2TaShomare 


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)


END

GO







/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 06/25/2020 13:17:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Kharid_Rep1SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 06/25/2020 13:17:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


   CREATE  PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14] 		 	
   
   @Userid INT,	 	
   @KharidID INT, 	
   @GroupID NVARCHAR(MAX),  	
   @KOLID NVARCHAR(MAX),  	
   @HesabidID NVARCHAR(MAX),  	
   @THesabidID NVARCHAR(MAX),  	
   @THesabidIDSath2 NVARCHAR(MAX),  	
   @StarteDate NVARCHAR(10), 	
   @ENDDate NVARCHAR(10), 	
   @KHARIDID1 INT, 	
   @KHARIDID2 INT  AS BEGIN 	 
   
   IF (@KharidID>0) 	
   
   BEGIN 
SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, 
                      T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, 
                      FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, 
                      T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, 
                      T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) 
                      AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 
                      1 AS GRP, 'کالاهای خریداری شده' AS NameGRP
FROM         T_T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 1) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN
                      T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN
                      T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE     (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh > 0)                      
    UNION ALL
    
SELECT  [TRANSACTION].TedadDKh AS Tedad, [TRANSACTION].UnitPriceDKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL AS Expr1, NULL AS Expr2, 
               T_NameKhad.KhadamatNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 0 AS tedadFarei, [TRANSACTION].radif, NULL AS Expr3, NULL AS Expr4, NULL 
               AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, T_NameKhad.Name, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 2 AS GRP, 'کسورات-هزینه های فاکتور' AS Expr5
FROM     T_KolHesab RIGHT OUTER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID ON T_KolHesab.KOLID = T_Kharid.KOLID LEFT OUTER JOIN
               T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh = 0)


    UNION ALL

SELECT  NULL AS Tedad, Mablagh AS netprice, NULL AS NameTarafHesab, NULL AS NameHesabMoin, NULL AS NameHesabTafsili, NULL AS NameHesabKol, NULL AS CodeTafsili, NULL AS CodeKol, NULL AS CodeMoin, 
               Mablagh, NULL AS Expr6, DATE2 AS Tarikh, TextSharh AS SharhRadif, TextSharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL AS Expr1, NULL AS Expr2, 
               PARID1Number AS CodeKala, NULL AS HazineFactor, NULL AS Serial, NULL AS ShomareFactor, NULL AS AddressTarafHesab, NULL AS Address2TarafHesab, NULL AS Expr7, NULL AS Expr8, NULL AS Expr9, NULL 
               AS Expr10, NULL AS Expr11, NULL AS Expr12, NULL AS Expr13, NULL AS Expr14, NULL AS Expr15, NULL AS Expr16, NULL AS Expr17, NULL AS Expr18, IDOperation AS KHARIDID, NULL AS tedadFarei, NULL 
               AS Expr19, NULL AS Expr3, NULL AS Expr4, NULL AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, NULL AS Expr20, NULL AS Expr21, NULL AS Expr22, 3 AS GRP, 'پرداخت های نقدی' AS Expr5
FROM     T_Par1
WHERE  (IDOperation = @KharidID)

      END               
ELSE 	

BEGIN  			
SET NOCOUNT ON;  			
Declare @SQLQuery AS NVarchar(MAX) 			
Declare @ParamDefinition AS NVarchar(MAX)  			
DECLARE @OrderBy AS NVARCHAR(MAX) 			
DECLARE @GROUPBy AS NVARCHAR(MAX) 		     			
IF (@StarteDate IS NULL) 					
SET @StarteDate='0' 		     				
IF (@ENDDate IS NULL) 					
SET @ENDDate='99999999' 						
SET @SQLQuery = '   SELECT  [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, 
               FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, 
               T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, 
               T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 
               ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, 
               FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) 
               ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, 
               FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 1 AS GRP, ''کالاهای خریداری شده'' AS NameGRP
FROM     T_T_Heasb RIGHT OUTER JOIN
               dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN
               T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (NOT (T_Kharid.KHARIDID IS NULL)) AND ([TRANSACTION].UnitPriceKh > 0) 		'   
			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
Set @ParamDefinition = '@UserID INT, 									
						@GroupID NVARCHAR(MAX), 									
						@KOLID NVARCHAR(MAX),  									
						@HesabidID NVARCHAR(MAX),  									
						@THesabidID NVARCHAR(MAX),  									
						@THesabidIDSath2 NVARCHAR(MAX),  									
						@StarteDate NVARCHAR(MAX),  									
						@ENDDate NVARCHAR(MAX), 									
						@KHARIDID1 INT, 									
						@KHARIDID2 INT '  	 			 			
/* Execute the Transact-SQL String with all parameter value's 	    			   Using sp_executesql Command */ 			    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 


SET @SQLQuery =@SQLQuery+ '     UNION ALL
    
SELECT  [TRANSACTION].TedadDKh AS Tedad, [TRANSACTION].UnitPriceDKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL AS Expr1, NULL AS Expr2, 
               T_NameKhad.KhadamatNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 0 AS tedadFarei, [TRANSACTION].radif, NULL AS Expr3, NULL AS Expr4, NULL 
               AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, T_NameKhad.Name, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 2 AS GRP, ''کسورات-هزینه های فاکتور'' AS Expr5
FROM     T_KolHesab RIGHT OUTER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID ON T_KolHesab.KOLID = T_Kharid.KOLID LEFT OUTER JOIN
               T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (NOT (T_Kharid.KHARIDID IS NULL)) AND ([TRANSACTION].UnitPriceKh = 0)		'   
			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
		    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 

SET @SQLQuery =@SQLQuery+ '     UNION ALL
SELECT  NULL AS Tedad, T_Par1.Mablagh AS netprice, NULL AS NameTarafHesab, NULL AS NameHesabMoin, NULL AS NameHesabTafsili, NULL AS NameHesabKol, NULL AS CodeTafsili, NULL AS CodeKol, NULL 
               AS CodeMoin, T_Par1.Mablagh, NULL AS Expr6, T_Par1.DATE2 AS Tarikh, T_Par1.TextSharh AS SharhRadif, T_Par1.TextSharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL 
               AS Expr1, NULL AS Expr2, T_Par1.PARID1Number AS CodeKala, NULL AS HazineFactor, NULL AS Serial, NULL AS ShomareFactor, NULL AS AddressTarafHesab, NULL AS Address2TarafHesab, NULL 
               AS Expr7, NULL AS Expr8, NULL AS Expr9, NULL AS Expr10, NULL AS Expr11, NULL AS Expr12, NULL AS Expr13, NULL AS Expr14, NULL AS Expr15, NULL AS Expr16, NULL AS Expr17, NULL AS Expr18, 
               T_Par1.IDOperation AS KHARIDID, NULL AS tedadFarei, NULL AS Expr19, NULL AS Expr3, NULL AS Expr4, NULL AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, NULL AS Expr20, NULL AS Expr21, NULL 
               AS Expr22, 3 AS GRP, ''پرداخت های نقدی'' AS Expr5
FROM     T_Par1 INNER JOIN
               T_Kharid ON T_Par1.IDOperation = T_Kharid.KHARIDID
WHERE  (NOT (T_Par1.IDOperation IS NULL))
  		'   
			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
		    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 	
		
Execute sp_Executesql   @SQLQuery,  									
						@ParamDefinition, 									
						@UserID , 									
						@GroupID, 									
						@KOLID ,  									
						@HesabidID ,  									
						@THesabidID ,  									
						@THesabidIDSath2 ,  									
						@StarteDate ,  									
						@ENDDate, 									
						@KHARIDID1, 									
						@KHARIDID1   			
If @@ERROR <> 0 GoTo ErrorHandler 			
Set NoCount OFF 		   			
ErrorHandler: 				
Return(@@ERROR) 	
END			 
END    

GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1]    Script Date: 06/25/2020 13:17:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Kharid_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1]    Script Date: 06/25/2020 13:17:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



Create  PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1]
		
		@Userid INT,	
		@KharidID int

AS
BEGIN
SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, 
                      T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, T_NameVahed.NameVahed AS NameVahedAsli, 
                      T_NameVahed_1.NameVahed AS NameVahedFarei, T_NameKala.Zarib, T_NameKala.BarCode, T_NameKala.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, 
                      T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, 
                      T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Email, T_NameKala.NameKala1, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(dbo.T_NameKala.Zarib, 0) 
                      = 0 THEN 1 ELSE dbo.T_NameKala.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, T_Anbar.AIDNumber, T_Anbar.NameAnbar, T_Anbar.Sharh AS AddAnbar
FROM         T_Heasb RIGHT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 1) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL RIGHT OUTER JOIN
                      T_Anbar INNER JOIN
                      T_NameKala INNER JOIN
                      [TRANSACTION] ON T_NameKala.ID = [TRANSACTION].KALAID INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON T_Anbar.AID = [TRANSACTION].AID ON TBL.FactID = T_Kharid.KHARIDID LEFT OUTER JOIN
                      T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID ON T_Heasb.HESABID = T_Kharid.HesabID LEFT OUTER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
                      T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1
WHERE     (T_Kharid.KHARIDID = @KharidID)
END




GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_BarForoosh_Rep1]    Script Date: 06/25/2020 13:17:05 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_BarForoosh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_BarForoosh_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_BarForoosh_Rep1]    Script Date: 06/25/2020 13:17:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPFrm_BarForoosh_Rep1]
		
		@Userid INT,@BarForoosh INT

AS

BEGIN

SELECT     [TRANSACTION].TedadBarFo AS Tedad, [TRANSACTION].UnitPriceBarFo AS netprice, CASE WHEN isnull(T_Par7.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Par7.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Par7.THESABID AS CodeTafsili, T_Par7.KOLID AS CodeKol, T_Par7.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, 
                      T_Par7.Takhfif, T_Par7.DateFo AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Par7.Sharh AS TozihatFactor, T_NameVahed.NameVahed AS NameVahedAsli, 
                      T_NameVahed_1.NameVahed AS NameVahedFarei, T_NameKala.Zarib, T_NameKala.BarCode, T_NameKala.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, 
                      T_Par7.PARID7Number AS Serial, T_Par7.NumberFO AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, 
                      T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Email, T_NameKala.NameKala1, T_Par7.PARID7
FROM         T_Heasb RIGHT OUTER JOIN
                      [TRANSACTION] INNER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID INNER JOIN
                      T_Par7 ON [TRANSACTION].PARID7 = T_Par7.PARID7 LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 4) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Par7.PARID7 = TBL.FactID ON T_Heasb.HESABID = T_Par7.HesabID LEFT OUTER JOIN
                      T_KolHesab ON T_Par7.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_T_Heasb ON T_Par7.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
                      T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1
WHERE     (T_Par7.PARID7 = @BarForoosh)


END




GO



/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Moin_Rep1]    Script Date: 06/25/2020 13:16:30 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubTreeViewRep_Taraz_Moin_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Moin_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Moin_Rep1]    Script Date: 06/25/2020 13:16:30 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Moin_Rep1] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'

                      
	SET @SQLQuery = ' SELECT     T_KolHesab.NameHesab AS NameHesabKol, SUM([TRANSACTION].DARBed) AS Bedehkar, SUM([TRANSACTION].PARBes) AS Bestankar, [TRANSACTION].KOLID, 
                      T_KolHesab.KolNumber AS CodeHesab, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes)
                       ELSE 0 END AS MandeBed, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) 
                      ELSE 0 END AS MandeBes, T_Heasb.HESABNumber AS CodeMoin, T_Heasb.NameHesab AS NameHesabMoin, T_Heasb.HESABID, T_GropHesab.GoNumber, 
                      T_GropHesab.NameGo AS NameGroup
FROM         T_GropHesab INNER JOIN
                      [TRANSACTION] ON T_GropHesab.GoID = [TRANSACTION].GOID INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_Heasb ON [TRANSACTION].HESABID = T_Heasb.HESABID
WHERE     (1 = 1) '
			 
	SET @GROUPBy = ' GROUP BY T_KolHesab.NameHesab, [TRANSACTION].KOLID, T_KolHesab.KolNumber, T_GropHesab.GoID, T_GropHesab.NameGo, T_Heasb.HESABNumber, T_Heasb.NameHesab, T_Heasb.HESABID, 
                      T_GropHesab.GoNumber	 '
	SET @OrderBy = ' '


	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
    SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
       
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END



GO




/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Rep1]    Script Date: 06/25/2020 13:16:01 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubTreeViewRep_Taraz_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubTreeViewRep_Taraz_Rep1]    Script Date: 06/25/2020 13:16:01 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[QSubTreeViewRep_Taraz_Rep1] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'



	SET @SQLQuery = ' SELECT     T_KolHesab.NameHesab AS NameHesabKol, SUM([TRANSACTION].DARBed) AS Bedehkar, SUM([TRANSACTION].PARBes) AS Bestankar, T_KolHesab.KOLID, 
					  T_KolHesab.KolNumber AS CodeHesab, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) > 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes)
					   ELSE 0 END AS MandeBed, CASE WHEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) < 0 THEN SUM(dbo.[TRANSACTION].DARBed - dbo.[TRANSACTION].PARBes) 
					  ELSE 0 END AS MandeBes, T_GropHesab.GoID, T_GropHesab.NameGo AS NameGroup
FROM         T_KolHesab INNER JOIN
					  [TRANSACTION] ON T_KolHesab.KOLID = [TRANSACTION].KOLID INNER JOIN
					  T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID where 1=1 '
			 
	SET @GROUPBy = ' GROUP BY T_KolHesab.NameHesab, T_KolHesab.KOLID, T_KolHesab.KolNumber, T_GropHesab.GoID, T_GropHesab.NameGo	 '
	SET @OrderBy = ' '


	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
    SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
       
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END



GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_TarazKol_Rep1]    Script Date: 06/25/2020 13:15:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdari_TarazKol_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_TarazKol_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_TarazKol_Rep1]    Script Date: 06/25/2020 13:15:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_TarazKol_Rep1] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)
                 

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'



	SET @SQLQuery = ' SELECT 1 AS RowNumber,0 AS SelectedRow,
        SUM(PARBes) AS SumPARBes, SUM(DARBed) AS SumDARBed, GOID, KOLID, NameGo, NameHesab, KolNumber, CodGroup, CASE WHEN (SUM(DARBed) - SUM(PARBes)) 
                      > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS MandeBed, CASE WHEN (SUM(PARBes) - SUM(DARBed)) > 0 THEN (SUM(PARBes) - SUM(DARBed)) ELSE 0 END AS MandeBes
FROM         View_TRANSACTION
WHERE     (1 = 1)'
			 
	SET @GROUPBy = ' GROUP BY  GOID, KOLID,  NameGo, NameHesab, KolNumber, CodGroup 
                       '
	SET @OrderBy = ' '

	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END



GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3]    Script Date: 06/25/2020 13:15:07 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3]    Script Date: 06/25/2020 13:15:07 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_Taraz1_Rep1SF3] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)
                 

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'



	SET @SQLQuery = ' SELECT 1 AS RowNumber,0 AS SelectedRow,
     SUM(PARBes) AS SumPARBes, SUM(DARBed) AS SumDARBed, NameHesabTSath2, CASE WHEN (NameHesabT IS NOT NULL) THEN NameHesabT WHEN (NameHesabMoin IS NOT NULL) 
                      THEN NameHesabMoin ELSE NameHesab END AS NameHesabT1, CASE WHEN (THESABNumber IS NOT NULL) THEN THESABNumber WHEN (HESABNumber IS NOT NULL) 
                      THEN HESABNumber ELSE KolNumber END AS CodHesab, THESABNumberSath2, GOID, KOLID, HESABID, NameGo, NameHesab, KolNumber, HESABNumber, NameHesabMoin, THESABNumber, 
                      NameHesabT, CodGroup, THESABID, ThesabIDSath2, CASE WHEN (SUM(DARBed) - SUM(PARBes)) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS MandeBed, 
				CASE WHEN (SUM(PARBes) - SUM(DARBed)) > 0 THEN (SUM(PARBes) - SUM(DARBed)) ELSE 0 END AS MandeBes
FROM         View_TRANSACTION where 1=1 '
			 
	SET @GROUPBy = ' GROUP BY NameHesabTSath2, THESABNumberSath2, GOID, KOLID, HESABID, NameGo, NameHesab, KolNumber, HESABNumber, NameHesabMoin, THESABNumber, NameHesabT, CodGroup, THESABID, 
                      ThesabIDSath2 '
	SET @OrderBy = ' '

	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END



GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_TarazMoin_Rep1]    Script Date: 06/25/2020 13:14:39 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_SanadHesabdari_TarazMoin_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_TarazMoin_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_SanadHesabdari_TarazMoin_Rep1]    Script Date: 06/25/2020 13:14:39 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[QSubPFrm_SanadHesabdari_TarazMoin_Rep1] 

	@UserID INT,
	@GroupID NVARCHAR(MAX), 
	@KOLID NVARCHAR(MAX), 
	@HesabidID NVARCHAR(MAX), 
	@THesabidID NVARCHAR(MAX), 
    @THesabidIDSath2 NVARCHAR(MAX), 
	@StarteDate NVARCHAR(10),
	@ENDDate NVARCHAR(10)
                 

AS
BEGIN

	SET NOCOUNT ON;

    Declare @SQLQuery AS NVarchar(MAX)
    Declare @ParamDefinition AS NVarchar(MAX) 
    DECLARE @OrderBy AS NVARCHAR(MAX)
    DECLARE @GROUPBy AS NVARCHAR(MAX)
    
    IF (@StarteDate IS NULL)
			SET @StarteDate='0'
    
        IF (@ENDDate IS NULL)
			SET @ENDDate='99999999'



	SET @SQLQuery = ' SELECT     1 AS RowNumber, 0 AS SelectedRow, SUM(PARBes) AS SumPARBes, SUM(DARBed) AS SumDARBed, GOID, KOLID, HESABID, NameGo, NameHesab, KolNumber, HESABNumber, NameHesabMoin, 
                      CodGroup, CASE WHEN (SUM(DARBed) - SUM(PARBes)) > 0 THEN (SUM(DARBed) - SUM(PARBes)) ELSE 0 END AS MandeBed, CASE WHEN (SUM(PARBes) - SUM(DARBed)) 
                      > 0 THEN (SUM(PARBes) - SUM(DARBed)) ELSE 0 END AS MandeBes
FROM         View_TRANSACTION
WHERE     (1 = 1) '
			 
	SET @GROUPBy = ' GROUP BY  GOID, KOLID, HESABID, NameGo, NameHesab, KolNumber, HESABNumber, NameHesabMoin,  CodGroup
                       '
	SET @OrderBy = ' '

	    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@GroupID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.GOID IN('+ @GroupID +'))'
         
    /* check for the condition and build the WHERE clause accordingly*/ 
    If (@KOLID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.KOLID IN('+ @KOLID +'))'
    
    If (@HesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.HESABID IN('+ @HesabidID +') )'

    If (@THesabidID Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABID IN('+ @THesabidID +') )'

    If (@THesabidIDSath2 Is Not Null)
         Set @SQLQuery = @SQLQuery + ' And (View_TRANSACTION.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'         


   Set @SQLQuery = @SQLQuery + ' And  Date1 Between @StarteDate And @ENDDate'



    Set @ParamDefinition = '@UserID INT,
							@GroupID NVARCHAR(MAX),
							@KOLID NVARCHAR(MAX), 
							@HesabidID NVARCHAR(MAX), 
							@THesabidID NVARCHAR(MAX), 
							@THesabidIDSath2 NVARCHAR(MAX), 
							@StarteDate NVARCHAR(MAX), 
							@ENDDate NVARCHAR(MAX) '  	
	
	/* Execute the Transact-SQL String with all parameter value's 	   
       Using sp_executesql Command */
       SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + N'  '+ char(10) + @OrderBy
    Execute sp_Executesql   @SQLQuery, 
							@ParamDefinition,
							@UserID ,
							@GroupID,
							@KOLID , 
							@HesabidID , 
							@THesabidID , 
							@THesabidIDSath2 , 
							@StarteDate , 
							@ENDDate


    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
  
	ErrorHandler:
		Return(@@ERROR)

END



GO



/****** Object:  StoredProcedure [dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_MinimumRep1]    Script Date: 06/25/2020 13:14:11 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_MinimumRep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_MinimumRep1]
GO


/****** Object:  StoredProcedure [dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_MinimumRep1]    Script Date: 06/25/2020 13:14:11 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




  CREATE PROCEDURE [dbo].[MSG_PReviewListKalaForRunProgram_MojodiManfi_MinimumRep1]            
  	

  	
  	AS     
  	
  	
SELECT     SubTransactionKala_1.KALAID, SubTransactionKala_1.AID, SubTransactionKala_1.GOKID, SUM(SubTransactionKala_1.Tedad) AS SumTedad, MAX(ISNULL(FunInfoKalaAll_1.MinMogoody, 0)) 
                      AS MinMogoody, SUM(CONVERT(float, SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre)) AS Mande, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.KalaNumber, 
                      FunInfoKalaAll_1.NameVahed1, FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.BarCode
FROM         View_SubTransactionKala AS SubTransactionKala_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON SubTransactionKala_1.LinkKalaAid = FunInfoKalaAll_1.LinkKalaAid
WHERE     (FunInfoKalaAll_1.VKalaID = 1)
GROUP BY SubTransactionKala_1.KALAID, SubTransactionKala_1.AID, SubTransactionKala_1.GOKID, FunInfoKalaAll_1.NameKala1, FunInfoKalaAll_1.KalaNumber, FunInfoKalaAll_1.NameVahed1, 
                      FunInfoKalaAll_1.NameVahed2, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.BarCode
HAVING      (MAX(ISNULL(FunInfoKalaAll_1.MinMogoody, 0)) - SUM(SubTransactionKala_1.TedadVarde - SubTransactionKala_1.TedadSadre) > 0)
ORDER BY FunInfoKalaAll_1.NameKala1           



GO



/****** Object:  StoredProcedure [dbo].[Rep_Table1_JabejaeiSF1Rep1]    Script Date: 06/25/2020 13:13:42 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_Table1_JabejaeiSF1Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_Table1_JabejaeiSF1Rep1]
GO



/****** Object:  StoredProcedure [dbo].[Rep_Table1_JabejaeiSF1Rep1]    Script Date: 06/25/2020 13:13:42 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


       CREATE PROCEDURE [dbo].[Rep_Table1_JabejaeiSF1Rep1] (  @USERID int, @IDKeyFact int   )              AS        BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	   DECLARE @WhrStatement  NVARCHAR(2000);         	   DECLARE @ParmDefinition  NVARCHAR(2000);         	   DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   set @WhrStatement  = N'';         	   set @ParmAndstr = N'';                  	   SET @ParmDefinition = N'@USERID int=null, 								 						@IDKeyFact int=null';            	 	  		SET @SQLString =' SELECT     Table1.ID1, Table1.ID1Number, T_Anbar.AIDNumber AS AIDNumberMabda, T_Anbar.NameAnbar AS NameAnbarMabda, T_Anbar_1.AIDNumber AS AIDNumberMagh,                        T_Anbar_1.NameAnbar AS NameAnbarMagh, Table1.AID1 AS AID1Mabda, Table1.AID2 AS AID2Magh, [TRANSACTION].TedadKhA, [TRANSACTION].MabVahedHazine,                        [TRANSACTION].Sharh AS SharhRadif, Table1.DateID1, T_NameKala.NameKala1, Table1.Sharh, [TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine AS MabHazine FROM         Table1 INNER JOIN                       [TRANSACTION] ON Table1.ID1 = [TRANSACTION].ID1 INNER JOIN                       T_Anbar ON Table1.AID1 = T_Anbar.AID INNER JOIN                       T_Anbar AS T_Anbar_1 ON Table1.AID2 = T_Anbar_1.AID INNER JOIN                       T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID WHERE     ([TRANSACTION].TedadKhA > 0) AND (Table1.ID1 = @IDKeyFact)' 	   						  set @SQLString=@SQLString 	      			 EXECUTE sp_executesql   @SQLString, @ParmDefinition,@USERID=@USERID ,@IDKeyFact=@IDKeyFact;  		 return END   

GO




/****** Object:  StoredProcedure [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1Rep1]    Script Date: 06/25/2020 13:13:13 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_Table1_Jabejaei_KolKeriehSF1Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1Rep1]
GO



/****** Object:  StoredProcedure [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1Rep1]    Script Date: 06/25/2020 13:13:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


          CREATE PROCEDURE [dbo].[Rep_Table1_Jabejaei_KolKeriehSF1Rep1] (        @USERID int, @date1 NVARCHAR(10), @date2 NVARCHAR(10), @Goid INT,@Kolid INT,@Hesabid INT,@THesabid INT    )              AS              BEGIN 	SET NOCOUNT ON; 	 	   DECLARE @SQLString  NVARCHAR(4000);  	         DECLARE @WhrStatement  NVARCHAR(2000);         	         DECLARE @ParmDefinition  NVARCHAR(2000);         	         DECLARE @ParmAndstr  NVARCHAR(2000);         	 	         set @WhrStatement  = N'';         	         set @ParmAndstr = N'';                  	         SET @ParmDefinition = N'@USERID int=null, 							 							@date1 NVARCHAR(10)=null, 							 							@date2 NVARCHAR(10)=null, 							 							@Goid INT=null, 							 							@Kolid INT=null, 							 							@Hesabid INT=null, 							 							@THesabid INT=null';            	  		 SET @SQLString =' SELECT     Table1.ID1, Table1.ID1Number, SUM([TRANSACTION].TedadKhA) AS TedadKhA, [TRANSACTION].MabVahedHazine, Table1.DateID1,                        SUM([TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine) AS MabHazine, dbo.HesabName(Table1.KOLID, T_Anbar.hesabid, Table1.THESABID) AS NameDriver, [TRANSACTION].GOKID,                        T_NameGoKala.NameGoKala1, T_NameGoKala.NameGoKalanumbr, T_NameGoKala.ID, CONVERT(nvarchar(255), [TRANSACTION].MabVahedHazine) + T_NameGoKala.NameGoKala1 AS Grp FROM         Table1 INNER JOIN                       [TRANSACTION] ON Table1.ID1 = [TRANSACTION].ID1 INNER JOIN                       T_Anbar ON Table1.AID1 = T_Anbar.AID INNER JOIN                       T_Anbar AS T_Anbar_1 ON Table1.AID2 = T_Anbar_1.AID INNER JOIN                       T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID INNER JOIN                       T_NameGoKala ON [TRANSACTION].GOKID = T_NameGoKala.ID WHERE     ([TRANSACTION].TedadKhA > 0) AND (Table1.DateID1 BETWEEN @Date1 AND @Date2) AND (Table1.KOLID = @KOLID) AND (Table1.HesabID = @HesabID) AND (Table1.THESABID = @THESABID) GROUP BY Table1.ID1, Table1.ID1Number, [TRANSACTION].MabVahedHazine, Table1.DateID1, dbo.HesabName(Table1.KOLID, T_Anbar.hesabid, Table1.THESABID), [TRANSACTION].GOKID,                        T_NameGoKala.NameGoKala1, T_NameGoKala.NameGoKalanumbr, T_NameGoKala.ID, CONVERT(nvarchar(255), [TRANSACTION].MabVahedHazine) + T_NameGoKala.NameGoKala1 HAVING      (SUM([TRANSACTION].TedadKhA * [TRANSACTION].MabVahedHazine) > 0)' 	   						  set @SQLString=@SQLString 	  EXECUTE sp_executesql   @SQLString, @ParmDefinition,@USERID=@USERID ,@date1=@date1,@date2=@date2,@Goid=@Goid,@Kolid=@Kolid,@Hesabid=@Hesabid,@THesabid=@THesabid;  		 return END    

GO



/****** Object:  StoredProcedure [dbo].[Rep_SooDRooZanehSF1Rep1]    Script Date: 06/25/2020 13:12:46 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_SooDRooZanehSF1Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_SooDRooZanehSF1Rep1]
GO


/****** Object:  StoredProcedure [dbo].[Rep_SooDRooZanehSF1Rep1]    Script Date: 06/25/2020 13:12:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




  CREATE PROCEDURE [dbo].[Rep_SooDRooZanehSF1Rep1] ( 
@USERID int,
@Date1 NVARCHAR(10),
@Date2 NVARCHAR(10)
  )           
  AS     
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';                  	
  SET @ParmDefinition = N'@USERID int=null, 								
						@Date1 NVARCHAR(10)=null,
						@Date2 NVARCHAR(10)=null';            	

		SET @SQLString ='
			SELECT     SUM([TRANSACTION].PARBes) AS PARBes, SUM([TRANSACTION].DARBed) AS DARBed, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab
			FROM         [TRANSACTION] INNER JOIN
								  T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID
			WHERE     ([TRANSACTION].GOID = 8) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2) AND (ISNULL([TRANSACTION].HESABID, 0) <> 85)
			GROUP BY dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID)

			  
			   UNION ALL
			   
			SELECT     SUM([TRANSACTION].PARBes) AS PARBes, SUM([TRANSACTION].DARBed) AS DARBed, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab
			FROM         [TRANSACTION] INNER JOIN
								  T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID
			WHERE     ([TRANSACTION].GOID = 9) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2) AND (ISNULL([TRANSACTION].HESABID, 0) <> 85)
			GROUP BY dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 

			   UNION ALL
			            

SELECT     SUM(CONVERT(float, [TRANSACTION].TedadFo * ([TRANSACTION].UnitPriceFo - Fun_SubAkharinKharid_1.MaxUnitPriceKh-T_NameKala.UnitPrice2))) AS Sood, 0 AS Expr1, N''سود حاصل از فروش'' AS Expr3
FROM         [TRANSACTION] INNER JOIN
                      T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID INNER JOIN
                      dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 ON [TRANSACTION].KALAID = Fun_SubAkharinKharid_1.KALAID AND [TRANSACTION].AID = Fun_SubAkharinKharid_1.AID LEFT OUTER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID
WHERE     ([TRANSACTION].TedadFo <> 0) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)
HAVING      (SUM(CONVERT(float, [TRANSACTION].TedadFo * ([TRANSACTION].UnitPriceFo - Fun_SubAkharinKharid_1.MaxUnitPriceKh-T_NameKala.UnitPrice2))) <> 0)

' 	
 
						

set @SQLString=@SQLString 	
     			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@USERID=@USERID ,@Date1=@Date1,@Date2=@Date2;  		
return END 









GO



/****** Object:  StoredProcedure [dbo].[Rep_SooDRooZanehSF1Rep1]    Script Date: 06/25/2020 13:12:19 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_SooDRooZanehSF1Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_SooDRooZanehSF1Rep1]
GO


/****** Object:  StoredProcedure [dbo].[Rep_SooDRooZanehSF1Rep1]    Script Date: 06/25/2020 13:12:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




  CREATE PROCEDURE [dbo].[Rep_SooDRooZanehSF1Rep1] ( 
@USERID int,
@Date1 NVARCHAR(10),
@Date2 NVARCHAR(10)
  )           
  AS     
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(4000);  	
  DECLARE @WhrStatement  NVARCHAR(2000);         	
  DECLARE @ParmDefinition  NVARCHAR(2000);         	
  DECLARE @ParmAndstr  NVARCHAR(2000);         	 	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';                  	
  SET @ParmDefinition = N'@USERID int=null, 								
						@Date1 NVARCHAR(10)=null,
						@Date2 NVARCHAR(10)=null';            	

		SET @SQLString ='
			SELECT     SUM([TRANSACTION].PARBes) AS PARBes, SUM([TRANSACTION].DARBed) AS DARBed, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab
			FROM         [TRANSACTION] INNER JOIN
								  T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID
			WHERE     ([TRANSACTION].GOID = 8) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2) AND (ISNULL([TRANSACTION].HESABID, 0) <> 85)
			GROUP BY dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID)

			  
			   UNION ALL
			   
			SELECT     SUM([TRANSACTION].PARBes) AS PARBes, SUM([TRANSACTION].DARBed) AS DARBed, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) AS NameHesab
			FROM         [TRANSACTION] INNER JOIN
								  T_GropHesab ON [TRANSACTION].GOID = T_GropHesab.GoID
			WHERE     ([TRANSACTION].GOID = 9) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2) AND (ISNULL([TRANSACTION].HESABID, 0) <> 85)
			GROUP BY dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 

			   UNION ALL
			            

SELECT     SUM(CONVERT(float, [TRANSACTION].TedadFo * ([TRANSACTION].UnitPriceFo - Fun_SubAkharinKharid_1.MaxUnitPriceKh-T_NameKala.UnitPrice2))) AS Sood, 0 AS Expr1, N''سود حاصل از فروش'' AS Expr3
FROM         [TRANSACTION] INNER JOIN
                      T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID INNER JOIN
                      dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1 ON [TRANSACTION].KALAID = Fun_SubAkharinKharid_1.KALAID AND [TRANSACTION].AID = Fun_SubAkharinKharid_1.AID LEFT OUTER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID
WHERE     ([TRANSACTION].TedadFo <> 0) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)
HAVING      (SUM(CONVERT(float, [TRANSACTION].TedadFo * ([TRANSACTION].UnitPriceFo - Fun_SubAkharinKharid_1.MaxUnitPriceKh-T_NameKala.UnitPrice2))) <> 0)

' 	
 
						

set @SQLString=@SQLString 	
     			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@USERID=@USERID ,@Date1=@Date1,@Date2=@Date2;  		
return END 









GO




/****** Object:  StoredProcedure [dbo].[Rep_SoratHesabRep1]    Script Date: 06/25/2020 13:20:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_SoratHesabRep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_SoratHesabRep1]
GO


/****** Object:  StoredProcedure [dbo].[Rep_SoratHesabRep1]    Script Date: 06/25/2020 13:20:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE PROCEDURE [dbo].[Rep_SoratHesabRep1] (  
      @THESABID int, @HESABID int, @KOLID int, @GoID INT, @Date1 NVARCHAR(10), @Date2 NVARCHAR(10)   )              AS        
      BEGIN 	
      	SET NOCOUNT ON; 	 	   
      	DECLARE @SQLString  NVARCHAR(4000);  	   
      	DECLARE @WhrStatement  NVARCHAR(2000);         	   
      	DECLARE @ParmDefinition  NVARCHAR(2000);         	   
      	DECLARE @ParmAndstr  NVARCHAR(2000);         	 	   
      	set @WhrStatement  = N'';         	   
      	set @ParmAndstr = N'';                  	   
      	SET @ParmDefinition = N'@THESABID int=null, 								 						
      							@HESABID int=null, 								 						
      							@KOLID  int=null, 								 						
      							@GoID int=null, 						
      							@Date1 NVARCHAR(10)=null, 						
      							@Date2 NVARCHAR(10)=null  ';            	 	 	
IF(NOT (@THESABID IS NULL))	
		
SET @SQLString =  '  SELECT     ISNULL(RadifSAID2, ISNULL(radif, 1000)) AS RadifSAID2, NameHesabmoin, 
ISNULL(nameKala2, SharhRadif) AS SharhRadif2, MandeHesab, Bedehkar, Bestankar, Tarikh, ShomareSanad, HesabID,              
CodeHesab, srtfield, tedad, unitprice, NameVahed, NameKala, MatnFi, SAIDType, NoeAmaliyat, TedadSadere, TedadVarede, 
Replace(SharhRadif,NoeAmaliyat,'''') AS SharhRadif3,CASE  WHEN (NoeAmaliyat LIKE N''%خريد%'') THEN SharhRadif ELSE REPLACE(SharhRadif, NoeAmaliyat, '''') END AS SharhRadif4  ,SharhRadif FROM         dbo.F_SoratHesabTafsili(@Date1) AS F_SoratHesabTafsili_1 
                     WHERE     (Tarikh BETWEEN @Date1 AND @Date2) AND (HesabID = @THESABID) ORDER BY Tarikh            '   
else IF(NOT (@HESABID IS NULL))	  		

SET @SQLString ='SELECT     ISNULL(RadifSAID2, ISNULL(radif, 1000)) AS RadifSAID2, NameHesabmoin, 
ISNULL(NameKala2, SharhRadif) AS SharhRadif2, MandeHesab, Bedehkar, Bestankar, Tarikh, ShomareSanad, HesabID, 
CodeHesab, srtfield, tedad, unitprice, NameVahed, NameKala,                        
MatnFi, SAIDType, NoeAmaliyat, TedadSadere, TedadVarede, Replace(SharhRadif,NoeAmaliyat,'''') AS SharhRadif3,CASE  WHEN (NoeAmaliyat LIKE N''%خريد%'') THEN SharhRadif ELSE REPLACE(SharhRadif, NoeAmaliyat, '''') END AS SharhRadif4  ,SharhRadif FROM         
dbo.F_SoratHesabMoein(@Date1) AS F_SoratHesabMoein_1 WHERE     (Tarikh BETWEEN @Date1 AND @Date2) AND (HesabID = @HESABID) ' 	
else IF(NOT (@KOLID IS NULL))	  		

SET @SQLString =' 		SELECT     ISNULL(RadifSAID2, ISNULL(radif, 1000)) AS RadifSAID2, NameHesabmoin, 
ISNULL(NameKala2, SharhRadif) AS SharhRadif2, MandeHesab, Bedehkar, Bestankar, Tarikh, ShomareSanad, 
HesabID, CodeHesab, srtfield, tedad, unitprice, NameVahed, NameKala,                        
MatnFi, SAIDType, NoeAmaliyat, TedadSadere, TedadVarede, Replace(SharhRadif,NoeAmaliyat,'''') AS SharhRadif3,CASE  WHEN (NoeAmaliyat LIKE N''%خريد%'') THEN SharhRadif ELSE REPLACE(SharhRadif, NoeAmaliyat, '''') END AS SharhRadif4  ,SharhRadif 
FROM        dbo.F_SoratHesabKol(@Date1) AS F_SoratHesabKol_1  WHERE     (Tarikh BETWEEN @Date1 AND @Date2) 
AND (HESABID = @KOLID) ' 	   
/* -----------------------نشان دادن کالاهای فعال */  	 

/*if(NOT (@LinkMasterID  IS NULL)) 			 	
* set @SQLString = @SQLString + N' HAVING      (T_NameKala.Active = 0) '; 					 
* if (@LinkMasterID  IS NULL) 	set @SQLString = @SQLString + N' HAVING      (T_NameKala.Active > -1000) '; 	                  
*  /* -----------------------شرط انبار */ 			 set @ParmAndstr=' AND ' 			 
if(NOT (@AID  IS NULL)) 			 
BEGIN 				 set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Kala.AID = @AID ) '; 				 
set @ParmAndstr=' AND ' 			 END 			   
/* -----------------------شرط گروه کالا  */  set @ParmAndstr=' AND ' 			 if(NOT (@GoKID  IS NULL)) 			 
BEGIN 				 	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Kala.GOKID = @GoKID ) '; 				 	
set @ParmAndstr=' AND ' 			 END 	 */ 						  
set @SQLString=@SQLString 	      			 

EXECUTE sp_executesql   @SQLString, @ParmDefinition,@THESABID=@THESABID ,@HESABID=@HESABID,@KOLID=@KOLID,@GoID=@GoID,@Date1=@Date1,@Date2=@Date2;  		 
return END
    


GO




/****** Object:  StoredProcedure [dbo].[QRep_khadamat]    Script Date: 06/25/2020 13:20:08 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QRep_khadamat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QRep_khadamat]
GO


/****** Object:  StoredProcedure [dbo].[QRep_khadamat]    Script Date: 06/25/2020 13:20:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE PROCEDURE [dbo].[QRep_khadamat]( 
      	@prmnvarStartDate nvarchar(50), 
      	@prmnvarenddate nvarchar(50), 
      	@prmintIdKala int, 
      	@prmintGoid int=null, 
      	@prmintkolid int=null, 
      	@prmintHesabid int=null, 
      	@prmintTHesabid int=null  )  AS    
      	DECLARE @cond int;  
      	DECLARE @condKala int;     	
      	if(NOT (@prmintTHesabid IS NULL)) 		
      	begin 			
      		set @cond= @prmintTHesabid; 		
      	end 	
      	else if(NOT (@prmintHesabid IS NULL))  		
      	begin 			
      		set @cond= @prmintHesabid; 		
      	end 	
      	else if(NOT (@prmintkolid IS NULL))  		
      	begin 			
      		set @cond= @prmintkolid; 		
      	end 	
      	else if(NOT (@prmintGoid IS NULL))  		
      	begin 			
      		set @cond= @prmintGoid; 		
      	end 	
      	else 		
      		begin 			
      			set @cond= -1; 		
      		end 	 	
      		if (not (@prmintIdKala is null)) 		
      		begin 			
      			set @condKala= @prmintIdKala; 		
      		end 	
      		else 		
      			begin 			
      				set @condKala= -1; 		
      			end 
SELECT     T_NameKhad.KhadamatNumber, T_NameKhad.Name AS NameKH, SUM(CONVERT(money, [TRANSACTION].TedadDKh) * [TRANSACTION].UnitPriceDKh) AS Mablag, SUM(CONVERT(Float, 
                      [TRANSACTION].TedadDKh)) AS TedadDKh, [TRANSACTION].GOID, T_DKhad.KOLID, T_DKhad.HesabID, T_DKhad.THESABID, dbo.HesabName(T_DKhad.KOLID, T_DKhad.HesabID, 
                      T_DKhad.THESABID) AS namehesab, CASE WHEN NOT (@prmintTHesabid IS NULL) THEN T_DKhad.Thesabid WHEN NOT (@prmintHesabid IS NULL) 
                      THEN T_DKhad.hesabid WHEN NOT (@prmintkolid IS NULL) THEN T_DKhad.kolid WHEN NOT (@prmintgoid IS NULL) THEN [TRANSACTION].goid ELSE - 1 END AS Expr1, 
                      CASE WHEN NOT (@prmintIdKala IS NULL) THEN T_NameKhad.NAMEKHID ELSE - 1 END AS Expr2, T_NameKhad.NAMEKHID, CONVERT(nvarchar, [TRANSACTION].KOLID) 
                      + N'.' + CONVERT(nvarchar, T_DKhad.HesabID) + N'.' + CONVERT(nvarchar, T_DKhad.THESABID) AS codeHesab, [TRANSACTION].TedadHazine, [TRANSACTION].UnitPriceDKh, T_NameKhad.Unit, 
                      SUM([TRANSACTION].Tedad2) AS Tedad2, SUM([TRANSACTION].FieldN3) AS SumSF11
FROM         [TRANSACTION] INNER JOIN
                      T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID INNER JOIN
                      T_DKhad ON [TRANSACTION].DKHID = T_DKhad.DKHID
WHERE     (T_DKhad.DateKh BETWEEN @prmnvarStartDate AND @prmnvarenddate) AND (CASE WHEN NOT (@prmintTHesabid IS NULL) THEN T_DKhad.Thesabid WHEN NOT (@prmintHesabid IS NULL) 
                      THEN T_DKhad.hesabid WHEN NOT (@prmintkolid IS NULL) THEN T_DKhad.kolid WHEN NOT (@prmintgoid IS NULL) THEN [TRANSACTION].goid ELSE - 1 END = @cond) AND 
                      (CASE WHEN NOT (@prmintIdKala IS NULL) THEN T_NameKhad.NAMEKHID ELSE - 1 END = @condkala)
GROUP BY T_NameKhad.KhadamatNumber, T_NameKhad.Name, T_DKhad.KOLID, T_DKhad.THESABID, T_DKhad.HesabID, dbo.HesabName(T_DKhad.KOLID, T_DKhad.HesabID, T_DKhad.THESABID), 
                      [TRANSACTION].GOID, CASE WHEN NOT (@prmintTHesabid IS NULL) THEN T_DKhad.Thesabid WHEN NOT (@prmintHesabid IS NULL) THEN T_DKhad.hesabid WHEN NOT (@prmintkolid IS NULL) 
                      THEN T_DKhad.kolid WHEN NOT (@prmintgoid IS NULL) THEN [TRANSACTION].goid ELSE - 1 END, CASE WHEN NOT (@prmintIdKala IS NULL) THEN T_NameKhad.NAMEKHID ELSE - 1 END, 
                      T_NameKhad.NAMEKHID, CONVERT(nvarchar, [TRANSACTION].KOLID) + N'.' + CONVERT(nvarchar, T_DKhad.HesabID) + N'.' + CONVERT(nvarchar, T_DKhad.THESABID), 
                      [TRANSACTION].TedadHazine, [TRANSACTION].UnitPriceDKh, T_NameKhad.Unit
ORDER BY namehesab


GO




/****** Object:  StoredProcedure [dbo].[QRep_khadamatRizHesab]    Script Date: 06/25/2020 13:19:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QRep_khadamatRizHesab]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QRep_khadamatRizHesab]
GO


/****** Object:  StoredProcedure [dbo].[QRep_khadamatRizHesab]    Script Date: 06/25/2020 13:19:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


    create PROCEDURE [dbo].[QRep_khadamatRizHesab]   ( 
    	@prmnvarStartDate nvarchar(50), 
    	@prmnvarenddate nvarchar(50), 
    	@prmintIdKala int, 
    	@prmintGoid int=null, 
    	@prmintkolid int=null, 
    	@prmintHesabid int=null, 
    	@prmintTHesabid int=null  )   AS       
    	
    	DECLARE @cond int;      
    	DECLARE @condKala int;      	   
    	if(NOT (@prmintTHesabid IS NULL)) 	 	      
    	begin 		 	   
    		set @cond= @prmintTHesabid; 	 	   
    	end  	
    	else if(NOT (@prmintHesabid IS NULL))   		   
    	begin  set @cond= @prmintHesabid; 	 	   
    	end   else if(NOT (@prmintkolid IS NULL))  	 	   
    	begin  set @cond= @prmintkolid; 	 	   
    	end else if(NOT (@prmintGoid IS NULL))  		    
    	begin  set @cond= @prmintGoid; 	 	   
    	end 	 
    	else 	 	
    		begin 	 		
    			set @cond= -1; 	 	   
    		end 	 	 
    		if (not (@prmintIdKala is null)) 	 	
    		begin 			 
    			set @condKala= @prmintIdKala; 	 	
    		end  	
    		else 		 
    			begin 			 
    				set @condKala= -1; 	 	
    			end      
SELECT     T_NameKhad.KhadamatNumber, T_NameKhad.Name AS NameKH, SUM(CONVERT(money, [TRANSACTION].TedadDKh) * [TRANSACTION].UnitPriceDKh) AS Mablag, SUM(CONVERT(FLOAT, 
                      [TRANSACTION].TedadDKh)) AS TedadDKh, [TRANSACTION].GOID, T_DKhad.KOLID, T_DKhad.HesabID, T_DKhad.THESABID, dbo.HesabName(T_DKhad.KOLID, T_DKhad.HesabID, 
                      T_DKhad.THESABID) AS namehesab, CASE WHEN NOT (@prmintTHesabid IS NULL) THEN T_DKhad.Thesabid WHEN NOT (@prmintHesabid IS NULL) 
                      THEN T_DKhad.hesabid WHEN NOT (@prmintkolid IS NULL) THEN T_DKhad.kolid WHEN NOT (@prmintgoid IS NULL) THEN [TRANSACTION].goid ELSE - 1 END AS Expr1, 
                      CASE WHEN NOT (@prmintIdKala IS NULL) THEN T_NameKhad.NAMEKHID ELSE - 1 END AS Expr2, T_NameKhad.NAMEKHID, CONVERT(nvarchar, [TRANSACTION].KOLID) 
                      + N'.' + CONVERT(nvarchar, T_DKhad.HesabID) + N'.' + CONVERT(nvarchar, T_DKhad.THESABID) AS codeHesab, T_DKhad.DateKh, T_NameKhad.Unit, T_DKhad.Sharh, 
                      [TRANSACTION].UnitPriceDKh, T_DKhad.DKHNumbr, T_KolHesab.NameHesab AS kolhesab, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, [TRANSACTION].THESABID) 
                      AS GroupNameHesab, SUM([TRANSACTION].Tedad2) AS Tedad2, SUM([TRANSACTION].FieldN3) AS SUMSF11
FROM         [TRANSACTION] INNER JOIN
                      T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID INNER JOIN
                      T_DKhad ON [TRANSACTION].DKHID = T_DKhad.DKHID INNER JOIN
                      T_KolHesab ON [TRANSACTION].KOLID = T_KolHesab.KOLID
WHERE     (T_DKhad.DateKh BETWEEN @prmnvarStartDate AND @prmnvarenddate) AND (CASE WHEN NOT (@prmintTHesabid IS NULL) THEN T_DKhad.Thesabid WHEN NOT (@prmintHesabid IS NULL) 
                      THEN T_DKhad.hesabid WHEN NOT (@prmintkolid IS NULL) THEN T_DKhad.kolid WHEN NOT (@prmintgoid IS NULL) THEN [TRANSACTION].goid ELSE - 1 END = @cond) AND 
                      (CASE WHEN NOT (@prmintIdKala IS NULL) THEN T_NameKhad.NAMEKHID ELSE - 1 END = @condkala)
GROUP BY T_NameKhad.KhadamatNumber, T_NameKhad.Name, T_DKhad.KOLID, T_DKhad.THESABID, T_DKhad.HesabID, dbo.HesabName(T_DKhad.KOLID, T_DKhad.HesabID, T_DKhad.THESABID), 
                      [TRANSACTION].GOID, CASE WHEN NOT (@prmintTHesabid IS NULL) THEN T_DKhad.Thesabid WHEN NOT (@prmintHesabid IS NULL) THEN T_DKhad.hesabid WHEN NOT (@prmintkolid IS NULL) 
                      THEN T_DKhad.kolid WHEN NOT (@prmintgoid IS NULL) THEN [TRANSACTION].goid ELSE - 1 END, CASE WHEN NOT (@prmintIdKala IS NULL) THEN T_NameKhad.NAMEKHID ELSE - 1 END, 
                      T_NameKhad.NAMEKHID, CONVERT(nvarchar, [TRANSACTION].KOLID) + N'.' + CONVERT(nvarchar, T_DKhad.HesabID) + N'.' + CONVERT(nvarchar, T_DKhad.THESABID), T_DKhad.DateKh, 
                      T_NameKhad.Unit, T_DKhad.Sharh, [TRANSACTION].UnitPriceDKh, T_DKhad.DKHNumbr, T_KolHesab.NameHesab, dbo.HesabName([TRANSACTION].KOLID, [TRANSACTION].HESABID, 
                      [TRANSACTION].THESABID)

GO




/****** Object:  StoredProcedure [dbo].[QRep_khadamat2]    Script Date: 06/25/2020 13:19:10 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QRep_khadamat2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QRep_khadamat2]
GO



/****** Object:  StoredProcedure [dbo].[QRep_khadamat2]    Script Date: 06/25/2020 13:19:10 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE PROCEDURE [dbo].[QRep_khadamat2]( 
      	@prmnvarStartDate nvarchar(50), 
      	@prmnvarenddate nvarchar(50), 
      	@prmintIdKala int ) AS   	
      	DECLARE @condKala int; 	 	
      	if (not (@prmintIdKala is null)) 		
      	begin 			
      		set @condKala= @prmintIdKala; 		
      	end 	
      	else 		
      		begin 			
      			set @condKala= -1; 		
      		end 		 
SELECT     [TRANSACTION].NAMEKHID, T_NameKhad.KhadamatNumber, T_NameKhad.Name AS NameKH, SUM(CONVERT(money, [TRANSACTION].TedadDKh) * [TRANSACTION].UnitPriceDKh) AS Mablag, 
                      SUM(CONVERT(Float, [TRANSACTION].TedadDKh)) AS TedadDKh, T_NameKhad.Unit, [TRANSACTION].UnitPriceDKh, SUM([TRANSACTION].Tedad2) AS Tedad2, SUM([TRANSACTION].FieldN3) 
                      AS SumSF11
FROM         [TRANSACTION] INNER JOIN
                      T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID INNER JOIN
                      T_DKhad ON [TRANSACTION].DKHID = T_DKhad.DKHID
WHERE     (T_DKhad.DateKh BETWEEN @prmnvarStartDate AND @prmnvarenddate) AND (CASE WHEN NOT (@prmintIdKala IS NULL) THEN T_NameKhad.NAMEKHID ELSE - 1 END = @condkala)
GROUP BY [TRANSACTION].NAMEKHID, T_NameKhad.KhadamatNumber, T_NameKhad.Name, T_NameKhad.Unit, [TRANSACTION].UnitPriceDKh

GO



/****** Object:  StoredProcedure [dbo].[Rep_SoratHesabRep2]    Script Date: 06/25/2020 13:18:41 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Rep_SoratHesabRep2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Rep_SoratHesabRep2]
GO


/****** Object:  StoredProcedure [dbo].[Rep_SoratHesabRep2]    Script Date: 06/25/2020 13:18:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




  CREATE PROCEDURE [dbo].[Rep_SoratHesabRep2] ( 
@THESABID int,
@HESABID int,
@KOLID int,
@GoID INT,
@Date1 NVARCHAR(10),
@Date2 NVARCHAR(10)
  )           
  AS     
  BEGIN 	SET NOCOUNT ON; 	 	
  DECLARE @SQLString  NVARCHAR(MAX);  	
  DECLARE @WhrStatement  NVARCHAR(MAX);         	
  DECLARE @ParmDefinition  NVARCHAR(MAX);         	
  DECLARE @ParmAndstr  NVARCHAR(MAX);         	 	
  set @WhrStatement  = N'';         	
  set @ParmAndstr = N'';                  	
  SET @ParmDefinition = N'@THESABID int=null, 								
						@HESABID int=null, 								
						@KOLID  int=null, 								
						@GoID int=null,
						@Date1 NVARCHAR(10)=null,
						@Date2 NVARCHAR(10)=null  ';            	
	
	IF(NOT (@THESABID IS NULL))	 
		SET @SQLString =  ' 
SELECT     ISNULL(F_SoratHesabTafsili_1.RadifSAID2, ISNULL(F_SoratHesabTafsili_1.radif, 1000)) AS RadifSAID2, F_SoratHesabTafsili_1.NameHesabmoin, ISNULL(F_SoratHesabTafsili_1.nameKala2, 
                      F_SoratHesabTafsili_1.SharhRadif) AS SharhRadif2, F_SoratHesabTafsili_1.MandeHesab, F_SoratHesabTafsili_1.Bedehkar, F_SoratHesabTafsili_1.Bestankar, F_SoratHesabTafsili_1.Tarikh, 
                      F_SoratHesabTafsili_1.ShomareSanad, F_SoratHesabTafsili_1.HesabID, F_SoratHesabTafsili_1.CodeHesab, F_SoratHesabTafsili_1.srtfield, F_SoratHesabTafsili_1.tedad, 
                      F_SoratHesabTafsili_1.unitprice, F_SoratHesabTafsili_1.NameVahed, F_SoratHesabTafsili_1.NameKala, F_SoratHesabTafsili_1.MatnFi, F_SoratHesabTafsili_1.SAIDType, 
                      F_SoratHesabTafsili_1.NoeAmaliyat, F_SoratHesabTafsili_1.TedadSadere, F_SoratHesabTafsili_1.TedadVarede, F_SoratHesabTafsili_1.FlagPrint, DrvMandeNahei.mande AS mandeNahei, Replace(SharhRadif,NoeAmaliyat,'''') AS SharhRadif
FROM         dbo.F_SoratHesabTafsili(@Date1) AS F_SoratHesabTafsili_1 INNER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID AS HESABID
                             FROM         [TRANSACTION] AS Tran1
                             WHERE     (NOT (SAID2 IS NULL)) AND (Date1 <= @Date2)
                             GROUP BY THESABID) AS DrvMandeNahei ON F_SoratHesabTafsili_1.HesabID = DrvMandeNahei.HESABID
WHERE     (F_SoratHesabTafsili_1.Tarikh BETWEEN @Date1 AND @Date2) AND (F_SoratHesabTafsili_1.HesabID = @THESABID) AND (F_SoratHesabTafsili_1.FlagPrint = 1)
ORDER BY F_SoratHesabTafsili_1.Tarikh
          
'
        
   else IF(NOT (@HESABID IS NULL))	 
		SET @SQLString ='SELECT     ISNULL(F_SoratHesabMoein_1.RadifSAID2, ISNULL(F_SoratHesabMoein_1.radif, 1000)) AS RadifSAID2, Replace(SharhRadif,NoeAmaliyat,'''') AS SharhRadif, F_SoratHesabMoein_1.NameHesabmoin, ISNULL(F_SoratHesabMoein_1.nameKala2, 
                      F_SoratHesabMoein_1.SharhRadif) AS SharhRadif2, F_SoratHesabMoein_1.MandeHesab, F_SoratHesabMoein_1.Bedehkar, F_SoratHesabMoein_1.Bestankar, F_SoratHesabMoein_1.Tarikh, 
                      F_SoratHesabMoein_1.ShomareSanad, F_SoratHesabMoein_1.HesabID, F_SoratHesabMoein_1.CodeHesab, F_SoratHesabMoein_1.srtfield, F_SoratHesabMoein_1.tedad, 
                      F_SoratHesabMoein_1.unitprice, F_SoratHesabMoein_1.NameVahed, F_SoratHesabMoein_1.NameKala, F_SoratHesabMoein_1.MatnFi, F_SoratHesabMoein_1.SAIDType, 
                      F_SoratHesabMoein_1.NoeAmaliyat, F_SoratHesabMoein_1.TedadSadere, F_SoratHesabMoein_1.TedadVarede, DrvMande.mande AS mandeNahei, F_SoratHesabMoein_1.FlagPrint
FROM         dbo.F_SoratHesabMoein(@Date1) AS F_SoratHesabMoein_1 INNER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, HESABID
                             FROM         [TRANSACTION] AS Tran1
                             WHERE     (NOT (SAID2 IS NULL)) AND (Date1 <= @Date2)
                             GROUP BY HESABID) AS DrvMande ON F_SoratHesabMoein_1.HesabID = DrvMande.HESABID
WHERE     (F_SoratHesabMoein_1.Tarikh BETWEEN @Date1 AND @Date2) AND (F_SoratHesabMoein_1.HesabID = @HESABID) AND (F_SoratHesabMoein_1.FlagPrint = 1) ' 	
	else IF(NOT (@KOLID IS NULL))	 
		SET @SQLString ='
SELECT     ISNULL(F_SoratHesabKol_1.RadifSAID2, ISNULL(F_SoratHesabKol_1.radif, 1000)) AS RadifSAID2, F_SoratHesabKol_1.NameHesabmoin, ISNULL(F_SoratHesabKol_1.nameKala2, 
                      F_SoratHesabKol_1.SharhRadif) AS SharhRadif2, Replace(SharhRadif,NoeAmaliyat,'''') AS SharhRadif, F_SoratHesabKol_1.MandeHesab, F_SoratHesabKol_1.Bedehkar, F_SoratHesabKol_1.Bestankar, F_SoratHesabKol_1.Tarikh, 
                      F_SoratHesabKol_1.ShomareSanad, F_SoratHesabKol_1.HesabID, F_SoratHesabKol_1.CodeHesab, F_SoratHesabKol_1.srtfield, F_SoratHesabKol_1.tedad, F_SoratHesabKol_1.unitprice, 
                      F_SoratHesabKol_1.NameVahed, F_SoratHesabKol_1.NameKala, F_SoratHesabKol_1.MatnFi, F_SoratHesabKol_1.SAIDType, F_SoratHesabKol_1.NoeAmaliyat, F_SoratHesabKol_1.TedadSadere, 
                      F_SoratHesabKol_1.TedadVarede, DrvMande.mande AS mandeNahei, F_SoratHesabKol_1.FlagPrint
FROM         dbo.F_SoratHesabKol(@Date1) AS F_SoratHesabKol_1 INNER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, KOLID
                             FROM         [TRANSACTION] AS Tran1
                             WHERE     (NOT (SAID2 IS NULL)) AND (Date1 <= @Date2)
                             GROUP BY KOLID) AS DrvMande ON F_SoratHesabKol_1.HesabID = DrvMande.KOLID
WHERE     (F_SoratHesabKol_1.Tarikh BETWEEN @Date1 AND @Date2) AND (F_SoratHesabKol_1.HesabID = @KOLID) AND (F_SoratHesabKol_1.FlagPrint = 1)

' 	
  /* -----------------------نشان دادن کالاهای فعال */ 
	
/*if(NOT (@LinkMasterID  IS NULL)) 			
	set @SQLString = @SQLString + N' HAVING      (T_NameKala.Active = 0) '; 					
if (@LinkMasterID  IS NULL)
	set @SQLString = @SQLString + N' HAVING      (T_NameKala.Active > -1000) '; 	
               
  /* -----------------------شرط انبار */ 			
set @ParmAndstr=' AND ' 			
if(NOT (@AID  IS NULL)) 			
BEGIN 				
set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Kala.AID = @AID ) '; 				
set @ParmAndstr=' AND ' 			
END 			
  /* -----------------------شرط گروه کالا  */ 
set @ParmAndstr=' AND ' 			
if(NOT (@GoKID  IS NULL)) 			
BEGIN 				
	set @WhrStatement= @WhrStatement + @ParmAndstr + N' (T_Kala.GOKID = @GoKID ) '; 				
	set @ParmAndstr=' AND ' 			
END 	
*/
						

set @SQLString=@SQLString 	
     			
EXECUTE sp_executesql   @SQLString, @ParmDefinition,@THESABID=@THESABID ,@HESABID=@HESABID,@KOLID=@KOLID,@GoID=@GoID,@Date1=@Date1,@Date2=@Date2;  		
return END 




GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_BarKharid_Rep1]    Script Date: 06/25/2020 13:18:24 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_BarKharid_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_BarKharid_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_BarKharid_Rep1]    Script Date: 06/25/2020 13:18:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



Create  PROCEDURE [dbo].[QSubPFrm_BarKharid_Rep1]
		
		@Userid INT,
		@BarKharidID int

AS

BEGIN

SELECT     [TRANSACTION].TedadBarKh AS Tedad, [TRANSACTION].UnitPriceBarKh AS netprice, CASE WHEN isnull(T_Dar8.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Dar8.HESABID, 
                      0) <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Dar8.THESABID AS CodeTafsili, T_Dar8.KOLID AS CodeKol, T_Dar8.HesabID AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, T_Dar8.Takhfif, 
                      T_Dar8.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Dar8.Sharh AS TozihatFactor, T_NameVahed.NameVahed AS NameVahedAsli, T_NameVahed_1.NameVahed AS NameVahedFarei, 
                      T_NameKala.Zarib, T_NameKala.BarCode, T_NameKala.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Dar8.DARID8Numbr AS Serial, 
                      T_Dar8.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, 
                      T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, 
                      T_NameKala.NameKala1, T_Dar8.DARID8
FROM         T_KolHesab RIGHT OUTER JOIN
                      T_Dar8 INNER JOIN
                      [TRANSACTION] INNER JOIN
                      T_NameKala ON [TRANSACTION].KALAID = T_NameKala.ID ON T_Dar8.DARID8 = [TRANSACTION].DARID8 LEFT OUTER JOIN
                      T_Heasb ON T_Dar8.HesabID = T_Heasb.HESABID LEFT OUTER JOIN
                      T_T_Heasb ON T_Dar8.THESABID = T_T_Heasb.THESABID ON T_KolHesab.KOLID = T_Dar8.KOLID LEFT OUTER JOIN
                      T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 LEFT OUTER JOIN
                      T_NameVahed AS T_NameVahed_1 ON T_NameKala.VAHED2 = T_NameVahed_1.VAHED1 LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 3) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Dar8.DARID8 = TBL.FactID
WHERE     (T_Dar8.DARID8 = @BarKharidID)	

END




GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepTH1Sath2_Rep1]    Script Date: 06/25/2020 13:21:21 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_RepTH1Sath2_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_RepTH1Sath2_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepTH1Sath2_Rep1]    Script Date: 06/25/2020 13:21:21 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


      CREATE  PROCEDURE [dbo].[QSubPFrm_RepTH1Sath2_Rep1] 		 		
      @Userid INT, 		
      @Date1 NVARCHAR(10), 		
      @Date2 NVARCHAR(10), 		
      @THESABIDSath1 INT, 		
      @TYPID INT,
      @THESABIDSath2 INT 
        AS 
      
      BEGIN 	  
SELECT   T_T_HeasbSath2.NameHesabT AS NameHesabTafsili, [TRANSACTION].TextShSA AS SharhRadif, DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBed AS Bedehkar, 
                      [TRANSACTION].PARBes AS Bestankar, [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, [TRANSACTION].THESABID, T_T_HeasbSath2.THESABNumber AS CodeHesab, 
                      [TRANSACTION].ThesabIDSath2
FROM         [TRANSACTION] INNER JOIN
                      T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
                      T_T_HeasbSath2 ON [TRANSACTION].ThesabIDSath2 = T_T_HeasbSath2.THESABID LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ThesabIDSath2
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                             GROUP BY ThesabIDSath2) AS DERIVEDTBL ON T_T_HeasbSath2.THESABID = DERIVEDTBL.ThesabIDSath2
WHERE     ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2) AND ([TRANSACTION].ThesabIDSath2 = @THESABIDSath2) AND ([TRANSACTION].THESABID = @THESABIDSath1)      
      END       

GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Khadamat_Rep1]    Script Date: 06/25/2020 13:21:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Khadamat_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Khadamat_Rep1]
GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Khadamat_Rep1]    Script Date: 06/25/2020 13:21:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



   CREATE  PROCEDURE [dbo].[QSubPFrm_Khadamat_Rep1] 		 		
		@Userid INT,@KhadamatID INT	,
   		@GroupID NVARCHAR(MAX), 
		@KOLID NVARCHAR(MAX), 
		@HesabidID NVARCHAR(MAX), 
		@THesabidID NVARCHAR(MAX),
		@StarteDate NVARCHAR(10),
		@ENDDate NVARCHAR(10),
		@KHARIDID1 INT,
		@KHARIDID2 INT AS  
   
   BEGIN  
   	
  
  IF (@KhadamatID>0) 
   	
SELECT     TRANSACTION_1.TedadDKh AS Tedad, TRANSACTION_1.UnitPriceDKh AS netprice, 
                      CASE WHEN T_DKhad.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_DKhad.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_DKhad.THESAB AS CodeTafsili, 
                      T_DKhad.KOLID AS CodeKol, T_DKhad.Hesab AS CodeMoin, TRANSACTION_1.FieldN3 AS Mablagh, T_DKhad.Takhfif, T_DKhad.DateKh AS Tarikh, TRANSACTION_1.Sharh AS SharhRadif, 
                      T_DKhad.Sharh AS TozihatFactor, T_NameKhad.Unit AS NameVahedAsli, T_NameKhad.KhadamatNumber AS CodeKala, T_DKhad.DKHNumbr AS Serial, T_DKhad.NumberKH AS ShomareFactor, 
                      T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, 
                      T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_NameKhad.Name AS NameKala1, 
                      T_DKhad.DKHID, DERIVEDTBL.mande AS MandeHesab, T_DKhad.DateKhStartDate, T_DKhad.DateKhEndDate, T_DKhad.NerkhService, T_DKhad.MabService, T_DKhad.ShGhabz, 
                      T_DKhad.TimeSabt, RIGHT(T_DKhad.DateKh, 6) AS Tarikh, T_DKhad.address2, T_DKhad.VasPlace, T_DKhad.Footnote, TRANSACTION_1.Maliatkala, TRANSACTION_1.Avarez, 
                      T_T_Heasb.THESABNumber, TRANSACTION_1.Tedad2, T_Heasb.NameBakhsh, T_Heasb.NameZihesab
FROM         (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESABID, KOLID, ISNULL(THESABID, 0) 
                                              AS THESABID
                        FROM         [TRANSACTION] AS TRANSACTION_1
                        WHERE     (NOT (SAID2 IS NULL))
                        GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL RIGHT OUTER JOIN
                      T_NameKhad INNER JOIN
                      [TRANSACTION] AS TRANSACTION_1 ON T_NameKhad.NAMEKHID = TRANSACTION_1.NAMEKHID INNER JOIN
                          (SELECT     ISNULL(THESABID, 0) AS THESAB, KOLID, ISNULL(HesabID, 0) AS Hesab, Takhfif, DateKh, Sharh, DKHNumbr, NumberKH, DKHID, DateKhStartDate, DateKhEndDate, NerkhService, 
                                                   MabService, ShGhabz, TimeSabt, address2, VasPlace, Footnote
                             FROM         T_DKhad AS T_DKhad_1) AS T_DKhad ON TRANSACTION_1.DKHID = T_DKhad.DKHID LEFT OUTER JOIN
                      T_T_Heasb ON T_DKhad.THESAB = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_KolHesab ON T_DKhad.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_Heasb ON T_DKhad.Hesab = T_Heasb.HESABID ON DERIVEDTBL.THESABID = T_DKhad.THESAB AND DERIVEDTBL.KOLID = T_DKhad.KOLID AND 
                      DERIVEDTBL.HESABID = T_DKhad.Hesab
WHERE     (T_DKhad.DKHID = @KhadamatID)
  
  
  ELSE
	BEGIN

			SET NOCOUNT ON;

			Declare @SQLQuery AS NVarchar(MAX)
			Declare @ParamDefinition AS NVarchar(MAX) 
			DECLARE @OrderBy AS NVARCHAR(MAX)
			DECLARE @GROUPBy AS NVARCHAR(MAX)
		    
			IF (@StarteDate IS NULL)
					SET @StarteDate='0'
		    
				IF (@ENDDate IS NULL)
					SET @ENDDate='99999999'
						SET @SQLQuery = '  SELECT     TRANSACTION_1.TedadDKh AS Tedad, TRANSACTION_1.UnitPriceDKh AS netprice, 
                      CASE WHEN T_DKhad.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_DKhad.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_DKhad.THESAB AS CodeTafsili, 
                      T_DKhad.KOLID AS CodeKol, T_DKhad.Hesab AS CodeMoin, TRANSACTION_1.FieldN3 AS Mablagh, T_DKhad.Takhfif, T_DKhad.DateKh AS Tarikh, TRANSACTION_1.Sharh AS SharhRadif, 
                      T_DKhad.Sharh AS TozihatFactor, T_NameKhad.Unit AS NameVahedAsli, T_NameKhad.KhadamatNumber AS CodeKala, T_DKhad.DKHNumbr AS Serial, T_DKhad.NumberKH AS ShomareFactor, 
                      T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, 
                      T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_NameKhad.Name AS NameKala1, 
                      T_DKhad.DKHID, DERIVEDTBL.mande AS MandeHesab, T_DKhad.DateKhStartDate, T_DKhad.DateKhEndDate, T_DKhad.NerkhService, T_DKhad.MabService, T_DKhad.ShGhabz, 
                      T_DKhad.TimeSabt, RIGHT(T_DKhad.DateKh, 6) AS Tarikh, T_DKhad.address2, T_DKhad.VasPlace, T_DKhad.Footnote, TRANSACTION_1.Maliatkala, TRANSACTION_1.Avarez, 
                      T_T_Heasb.THESABNumber, TRANSACTION_1.Tedad2, T_DKhad.GOID, T_Heasb.NameBakhsh, T_Heasb.NameZihesab
FROM         (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESABID, KOLID, ISNULL(THESABID, 0) 
                                              AS THESABID
                        FROM         [TRANSACTION] AS TRANSACTION_1
                        WHERE     (NOT (SAID2 IS NULL))
                        GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL RIGHT OUTER JOIN
                      T_NameKhad INNER JOIN
                      [TRANSACTION] AS TRANSACTION_1 ON T_NameKhad.NAMEKHID = TRANSACTION_1.NAMEKHID INNER JOIN
                          (SELECT     ISNULL(THESABID, 0) AS THESAB, KOLID, ISNULL(HesabID, 0) AS Hesab, Takhfif, DateKh, Sharh, DKHNumbr, NumberKH, DKHID, DateKhStartDate, DateKhEndDate, NerkhService, 
                                                   MabService, ShGhabz, TimeSabt, address2, VasPlace, Footnote, GOID
                             FROM         T_DKhad AS T_DKhad_1) AS T_DKhad ON TRANSACTION_1.DKHID = T_DKhad.DKHID LEFT OUTER JOIN
                      T_T_Heasb ON T_DKhad.THESAB = T_T_Heasb.THESABID LEFT OUTER JOIN
                      T_KolHesab ON T_DKhad.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
                      T_Heasb ON T_DKhad.Hesab = T_Heasb.HESABID ON DERIVEDTBL.THESABID = T_DKhad.THESAB AND DERIVEDTBL.KOLID = T_DKhad.KOLID AND 
                      DERIVEDTBL.HESABID = T_DKhad.Hesab
WHERE     (1 = 1)
		'


			SET @GROUPBy = '  '
			SET @OrderBy = '  '

			Set @ParamDefinition = '@UserID INT,
									@GroupID NVARCHAR(MAX),
									@KOLID NVARCHAR(MAX), 
									@HesabidID NVARCHAR(MAX), 
									@THesabidID NVARCHAR(MAX),
									@StarteDate NVARCHAR(MAX), 
									@ENDDate NVARCHAR(MAX),
									@KHARIDID1 INT,
									@KHARIDID2 INT '  	
			
			/* Execute the Transact-SQL String with all parameter value's 	   
			   Using sp_executesql Command */
			   
		If (@GroupID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_DKhad.GOID IN('+ @GroupID +'))'
	         
		If (@KOLID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_DKhad.KOLID IN('+ @KOLID +'))'
	    
		If (@HesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_DKhad.Hesab IN('+ @HesabidID +') )'

		If (@THesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_DKhad.THESAB IN('+ @THesabidID +') )'
  
        
        
       If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))
          Set @SQLQuery = @SQLQuery + ' And  T_DKhad.DKHNumbr Between @KHARIDID1 And @KHARIDID2' 
        
        
        Set @SQLQuery = @SQLQuery + ' And  T_DKhad.DateKh Between @StarteDate And @ENDDate'
        			   
			   SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
			Execute sp_Executesql   @SQLQuery, 
									@ParamDefinition,
									@UserID ,
									@GroupID,
									@KOLID , 
									@HesabidID , 
									@THesabidID ,
									@StarteDate , 
									@ENDDate,
									@KHARIDID1,
									@KHARIDID2


			If @@ERROR <> 0 GoTo ErrorHandler
			Set NoCount OFF
		  
			ErrorHandler:
				Return(@@ERROR)
	END	
	 
   END



GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_Khadamat_GharardadZihesab_SF5]    Script Date: 06/25/2020 13:22:14 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Paye_Khadamat_GharardadZihesab_SF5]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Paye_Khadamat_GharardadZihesab_SF5]
GO



/****** Object:  StoredProcedure [dbo].[Op_Paye_Khadamat_GharardadZihesab_SF5]    Script Date: 06/25/2020 13:22:14 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




   CREATE  PROCEDURE [dbo].[Op_Paye_Khadamat_GharardadZihesab_SF5] 		 		
		@Userid INT,@KhadamatID INT	,@GroupID NVARCHAR(MAX),@KOLID NVARCHAR(MAX),@HesabidID NVARCHAR(MAX),@THesabidID NVARCHAR(MAX),@StarteDate NVARCHAR(10),@ENDDate NVARCHAR(10),	@KHARIDID1 INT,@KHARIDID2 INT AS  
   

   	

	BEGIN

			SET NOCOUNT ON;

			Declare @SQLQuery AS NVarchar(MAX)
			Declare @ParamDefinition AS NVarchar(MAX) 
			DECLARE @OrderBy AS NVARCHAR(MAX)
			DECLARE @GROUPBy AS NVARCHAR(MAX)
		    
			IF (@StarteDate IS NULL)
					SET @StarteDate='0'
		    
				IF (@ENDDate IS NULL)
					SET @ENDDate='99999999'
						SET @SQLQuery = '  SELECT  SUM(TRANSACTION_1.TedadDKh) AS Tedad, SUM(TRANSACTION_1.UnitPriceDKh) AS netprice, SUM(TRANSACTION_1.FieldN3) AS Mablagh, MIN(T_DKhad.DateKh) AS Tarikh, T_NameKhad.Name AS NameKala1, 
               T_DKhad.DateKhStartDate, T_DKhad.DateKhEndDate, T_DKhad.address2, SUM(TRANSACTION_1.Maliatkala) AS Maliatkala, SUM(TRANSACTION_1.Avarez) AS Avarez, T_Heasb.NameBakhsh, 
               T_Heasb.NameZihesab, T_DehMadar.THESABID AS THESABIDMadar, T_DehMadar.THESABNumber AS THESABNumberMadar, T_DehMadar.NameHesabT AS NameHesabTMadar, 
               T_DehMadar.CodEghtesdi AS CodEghtesdiMadar, T_DehMadar.Address AS AddressMadar, T_DehMadar.CodPosti AS CodPostiMadar, T_DehMadar.Phone1 AS Phone1Madar, T_DehMadar.Mob AS MobMadar, 
               T_DehMadar.CodeMelli AS CodeMelliMadar, T_DehMadar.Phone2 AS TelexMadar, SUM(TRANSACTION_1.TedadDKh * TRANSACTION_1.UnitPriceDKh) AS SumMab
FROM     T_NameKhad INNER JOIN
               [TRANSACTION] AS TRANSACTION_1 ON T_NameKhad.NAMEKHID = TRANSACTION_1.NAMEKHID INNER JOIN
                   (SELECT  ISNULL(THESABID, 0) AS THESAB, KOLID, ISNULL(HesabID, 0) AS Hesab, Takhfif, DateKh, Sharh, DKHNumbr, NumberKH, DKHID, DateKhStartDate, DateKhEndDate, NerkhService, MabService, 
                                   ShGhabz, TimeSabt, address2, VasPlace, Footnote, GOID
                    FROM     T_DKhad AS T_DKhad_1) AS T_DKhad ON TRANSACTION_1.DKHID = T_DKhad.DKHID LEFT OUTER JOIN
               T_T_Heasb ON T_DKhad.THESAB = T_T_Heasb.THESABID LEFT OUTER JOIN
               T_KolHesab ON T_DKhad.KOLID = T_KolHesab.KOLID LEFT OUTER JOIN
               T_T_Heasb AS T_DehMadar INNER JOIN
               T_Heasb ON T_DehMadar.THESABID = T_Heasb.ThesabidDeh ON T_DKhad.Hesab = T_Heasb.HESABID LEFT OUTER JOIN
                   (SELECT  SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESABID, KOLID, ISNULL(THESABID, 0) AS THESABID
                    FROM     [TRANSACTION] AS TRANSACTION_1
                    WHERE  (NOT (SAID2 IS NULL))
                    GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL ON T_DKhad.THESAB = DERIVEDTBL.THESABID AND T_DKhad.KOLID = DERIVEDTBL.KOLID AND 
               T_DKhad.Hesab = DERIVEDTBL.HESABID
WHERE  (1 = 1)
		'


			SET @GROUPBy = ' GROUP BY T_NameKhad.Name, T_DKhad.DateKhStartDate, T_DKhad.DateKhEndDate, T_DKhad.address2, T_Heasb.NameBakhsh, T_Heasb.NameZihesab, T_DehMadar.THESABID, T_DehMadar.THESABNumber, 
               T_DehMadar.NameHesabT, T_DehMadar.CodEghtesdi, T_DehMadar.Address, T_DehMadar.CodPosti, T_DehMadar.Phone1, T_DehMadar.Mob, T_DehMadar.CodeMelli, T_DehMadar.Phone2'
			SET @OrderBy = '  '

			Set @ParamDefinition = '@UserID INT,
									@GroupID NVARCHAR(MAX),
									@KOLID NVARCHAR(MAX), 
									@HesabidID NVARCHAR(MAX), 
									@THesabidID NVARCHAR(MAX),
									@StarteDate NVARCHAR(MAX), 
									@ENDDate NVARCHAR(MAX),
									@KHARIDID1 INT,
									@KHARIDID2 INT '  	
			
			/* Execute the Transact-SQL String with all parameter value's 	   
			   Using sp_executesql Command */
			   
		If (@GroupID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_DKhad.GOID IN('+ @GroupID +'))'
	         
		If (@KOLID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_DKhad.KOLID IN('+ @KOLID +'))'
	    
		If (@HesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_DKhad.Hesab IN('+ @HesabidID +') )'

		If (@THesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_DKhad.THESAB IN('+ @THesabidID +') )'
  
        
        
       If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))
          Set @SQLQuery = @SQLQuery + ' And  T_DKhad.DKHNumbr Between @KHARIDID1 And @KHARIDID2' 
        
        
        Set @SQLQuery = @SQLQuery + ' And  T_DKhad.DateKh Between @StarteDate And @ENDDate'
        			   
			   SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
			Execute sp_Executesql   @SQLQuery, 
									@ParamDefinition,
									@UserID ,
									@GroupID,
									@KOLID , 
									@HesabidID , 
									@THesabidID ,
									@StarteDate , 
									@ENDDate,
									@KHARIDID1,
									@KHARIDID2


			If @@ERROR <> 0 GoTo ErrorHandler
			Set NoCount OFF
		  
			ErrorHandler:
				Return(@@ERROR)


end


GO






/****** Object:  StoredProcedure [dbo].[QSubPishPFrm_Foroosh_Rep1]    Script Date: 06/25/2020 13:22:42 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPishPFrm_Foroosh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPishPFrm_Foroosh_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPishPFrm_Foroosh_Rep1]    Script Date: 06/25/2020 13:22:42 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE  PROCEDURE [dbo].[QSubPishPFrm_Foroosh_Rep1]
		
		@Userid INT,	
		@FOROSHID int

AS

BEGIN
	
SELECT  T_TarSPish.TedadFo AS Tedad, T_TarSPish.UnitPriceFo AS netprice, CASE WHEN isnull(T_PishFactor.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_PishFactor.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_PishFactor.THESABID AS CodeTafsili, T_PishFactor.KOLID AS CodeKol, T_PishFactor.HesabID AS CodeMoin, T_TarSPish.FieldN3 AS Mablagh, T_PishFactor.Takhfif, 
               T_PishFactor.FieldT3 AS TarikhEtebar, T_PishFactor.DateFo AS Tarikh, T_TarSPish.Sharh AS SharhRadif, T_PishFactor.Sharh AS TozihatFactor, T_NameVahed.NameVahed AS NameVahedAsli, 
               T_NameVahed_1.NameVahed AS NameVahedFarei, T_NameKala.Zarib, T_NameKala.BarCode, T_NameKala.KalaNumber AS CodeKala, T_PishFactor.ForoshNumbr AS Serial, 
               T_PishFactor.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, 
               T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, 
               T_NameKala.NameKala1, T_PishFactor.FOROSHID, T_NameKala.vaznKala
FROM     T_KolHesab RIGHT OUTER JOIN
               T_Heasb RIGHT OUTER JOIN
               T_T_Heasb RIGHT OUTER JOIN
               T_NameVahed AS T_NameVahed_1 RIGHT OUTER JOIN
               T_PishFactor INNER JOIN
               T_TarSPish INNER JOIN
               T_NameKala ON T_TarSPish.KALAID = T_NameKala.ID ON T_PishFactor.FOROSHID = T_TarSPish.FOROSHID LEFT OUTER JOIN
               T_NameVahed ON T_NameKala.VAHED1 = T_NameVahed.VAHED1 ON T_NameVahed_1.VAHED1 = T_NameKala.VAHED2 ON T_T_Heasb.THESABID = T_PishFactor.THESABID ON 
               T_Heasb.HESABID = T_PishFactor.HesabID ON T_KolHesab.KOLID = T_PishFactor.KOLID
WHERE  (T_PishFactor.FOROSHID = @FOROSHID)
               
               
END







GO





/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepTH1_Rep1]    Script Date: 06/25/2020 13:23:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_RepTH1_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_RepTH1_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_RepTH1_Rep1]    Script Date: 06/25/2020 13:23:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






CREATE  PROCEDURE [dbo].[QSubPFrm_RepTH1_Rep1]
		
		@Userid INT,
		@Date1 NVARCHAR(10),
		@Date2 NVARCHAR(10),
		@THESABID INT,
		@TYPID INT

AS
BEGIN
	

IF (@TYPID=102 ) /* گزارش دفتر تفصيلي */
	
SELECT  T_T_Heasb.NameHesabT AS NameHesabTafsili, [TRANSACTION].TextShSA AS SharhRadif, DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBed AS Bedehkar, [TRANSACTION].PARBes AS Bestankar, 
               [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, T_T_Heasb.THESABID, T_T_Heasb.THESABNumber AS CodeHesab, [TRANSACTION].RadifSAID2
FROM     [TRANSACTION] INNER JOIN
               T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
               T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                   (SELECT  SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID
                    FROM     [TRANSACTION] AS TRANSACTION_1
                    WHERE  (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                    GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.THESABID
WHERE  (T_T_Heasb.THESABID = @THESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)

                             
ELSE

	IF (@TYPID=115  ) /* گزارش حساب تفصيلي ارزی */
	
SELECT  DERIVEDTBL.mande AS MandeHesab, [TRANSACTION].DARBedCur AS Bedehkar, [TRANSACTION].PARBesCur AS Bestankar, [TRANSACTION].Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, 
               [TRANSACTION].TextShSA AS SharhRadif, [TRANSACTION].PARBesCur AS BestankarArzi, [TRANSACTION].DARBedCur AS BedehkarArzi, [TRANSACTION].CurRef AS NoeArz, [TRANSACTION].CurRate AS NerkhArz, 
               T_SA2.Sharh AS SharhSanad, Currency.Title AS NameArz, T_T_Heasb.THESABID, T_T_Heasb.THESABNumber AS CodeTafsili, T_T_Heasb.NameHesabT AS NameHesabTafsili, [TRANSACTION].RadifSAID2
FROM     [TRANSACTION] INNER JOIN
               T_SA2 ON [TRANSACTION].SAID2 = T_SA2.SAID2 INNER JOIN
               Currency ON [TRANSACTION].CurRef = Currency.CurrencyID INNER JOIN
               T_T_Heasb ON [TRANSACTION].THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                   (SELECT  SUM(DARBedCur) - SUM(PARBesCur) AS mande, SUM(PARBesCur) AS SumOfPARBes, SUM(DARBedCur) AS SumOfDARBed, THESABID
                    FROM     [TRANSACTION] AS TRANSACTION_1
                    WHERE  (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                    GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.THESABID
WHERE  (T_T_Heasb.THESABID = @THESABID) AND ([TRANSACTION].Date1 BETWEEN @Date1 AND @Date2)

  ELSE

	IF (@TYPID=116  )        /* گزارش حساب تفصيلي ارزی و ریالی */                    
			SELECT  DERIVEDTBL.mande AS MandeHesab, TRANSACTION_2.DARBed AS Bedehkar, TRANSACTION_2.PARBes AS Bestankar, TRANSACTION_2.Date1 AS Tarikh, T_SA2.SAID2Number AS ShomareSanad, 
               TRANSACTION_2.TextShSA AS SharhRadif, TRANSACTION_2.PARBesCur AS BestankarArzi, TRANSACTION_2.DARBedCur AS BedehkarArzi, TRANSACTION_2.CurRef AS NoeArz, 
               TRANSACTION_2.CurRate AS NerkhArz, T_SA2.Sharh AS SharhSanad, Currency.Title AS NameArz, TBL_MandeArz.mandeCur AS mandeArz, T_T_Heasb.THESABID, T_T_Heasb.THESABNumber AS CodeTafsili, 
               T_T_Heasb.NameHesabT AS NameHesabTafsili, TRANSACTION_2.RadifSAID2
FROM     (SELECT  SUM(DARBedCur) - SUM(PARBesCur) AS mandeCur, SUM(PARBesCur) AS SumOfPARBesCur, SUM(DARBedCur) AS SumOfDARBedCur, THESABID
                FROM     [TRANSACTION]
                WHERE  (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                GROUP BY THESABID) AS TBL_MandeArz RIGHT OUTER JOIN
               [TRANSACTION] AS TRANSACTION_2 INNER JOIN
               T_SA2 ON TRANSACTION_2.SAID2 = T_SA2.SAID2 INNER JOIN
               T_T_Heasb ON TRANSACTION_2.THESABID = T_T_Heasb.THESABID ON TBL_MandeArz.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
                   (SELECT  SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, THESABID
                    FROM     [TRANSACTION] AS TRANSACTION_1
                    WHERE  (NOT (SAID2 IS NULL)) AND (Date1 < @Date1)
                    GROUP BY THESABID) AS DERIVEDTBL ON T_T_Heasb.THESABID = DERIVEDTBL.THESABID LEFT OUTER JOIN
               Currency ON TRANSACTION_2.CurRef = Currency.CurrencyID
WHERE  (T_T_Heasb.THESABID = @THESABID) AND (TRANSACTION_2.Date1 BETWEEN @Date1 AND @DATE2) AND (NOT (TRANSACTION_2.SAID2 IS NULL))


END








GO



/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Rep1]    Script Date: 06/25/2020 13:23:08 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Rep1]    Script Date: 06/25/2020 13:23:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_Rep1]
		
		@Userid INT,	
		@FOROSHID INT,
		@GroupID NVARCHAR(MAX), 
		@KOLID NVARCHAR(MAX), 
		@HesabidID NVARCHAR(MAX), 
		@THesabidID NVARCHAR(MAX), 
		@THesabidIDSath2 NVARCHAR(MAX), 
		@StarteDate NVARCHAR(10),
		@ENDDate NVARCHAR(10),
		@KHARIDID1 INT,
		@KHARIDID2 INT

AS
BEGIN
	
IF (@FOROSHID>0) 	
	
SELECT  [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
               CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
               T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, T_Forosh.KOLID AS CodeKol, 
               T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, [TRANSACTION].FieldN7 AS Takhfif, T_Forosh.DateFo AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Forosh.Sharh AS TozihatFactor, 
               FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, 
               ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, T_Forosh.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, FunInfoKalaAll_1.NameKala1, T_Forosh.FOROSHID, DERIVEDTBL.mande AS MandeHesab, 
               ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, 
               T_Forosh.ForoshNumbr + FunInfoKalaAll_1.AIDNumber AS CodeAnbar, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddressAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
               > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
               > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Tedad1User, 
               [TRANSACTION].Tedad2User, [TRANSACTION].UnitPriceUser, [TRANSACTION].ZaribChildUser, [TRANSACTION].KalaIDChild, 
               CASE WHEN ZaribChildUser > 0 THEN '(' + T_RelateKala.NameKala2 + ')' ELSE '' END AS NameKalaChild, T_Forosh.KeraiehHaml, T_Forosh.keraiehbargiri, T_Forosh.TimeSabt
FROM     T_KolHesab RIGHT OUTER JOIN
               T_T_Heasb RIGHT OUTER JOIN
               T_Heasb RIGHT OUTER JOIN
               dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
               [TRANSACTION] INNER JOIN
                   (SELECT  ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID, GOID, ThesabIDSath2, THESABID, HesabID, 
                                   ISNULL(KeraiehHaml, 0) AS KeraiehHaml, ISNULL(keraiehbargiri, 0) AS keraiehbargiri, CONVERT(VARCHAR(8), DateCreate, 108) AS TimeSabt
                    FROM     T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID ON FunInfoKalaAll_1.ID = [TRANSACTION].KALAID AND 
               FunInfoKalaAll_1.AID = [TRANSACTION].AID LEFT OUTER JOIN
               T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2 LEFT OUTER JOIN
                   (SELECT  SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) AS THESAB
                    FROM     [TRANSACTION] AS TRANSACTION_1
                    WHERE  (NOT (SAID2 IS NULL))
                    GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL ON T_Forosh.KOLID = DERIVEDTBL.KOLID AND T_Forosh.Hesab = DERIVEDTBL.HESAB AND 
               T_Forosh.THesab = DERIVEDTBL.THESAB ON T_Heasb.HESABID = T_Forosh.Hesab ON T_T_Heasb.THESABID = T_Forosh.THesab ON T_KolHesab.KOLID = T_Forosh.KOLID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 2) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Forosh.FOROSHID = TBL.FactID
WHERE  (T_Forosh.FOROSHID = @FOROSHID)

ELSE
	BEGIN

			SET NOCOUNT ON;

			Declare @SQLQuery AS NVarchar(MAX)
			Declare @ParamDefinition AS NVarchar(MAX) 
			DECLARE @OrderBy AS NVARCHAR(MAX)
			DECLARE @GROUPBy AS NVARCHAR(MAX)
		    
			IF (@StarteDate IS NULL)
					SET @StarteDate='0'
		    
				IF (@ENDDate IS NULL)
					SET @ENDDate='99999999'
						SET @SQLQuery = '  SELECT     [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
                      CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, 
                      T_Forosh.KOLID AS CodeKol, T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, [TRANSACTION].FieldN7 AS Takhfif, T_Forosh.DateFo AS Tarikh, 
                      [TRANSACTION].Sharh AS SharhRadif, T_Forosh.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, 
                      FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, 
                      T_Forosh.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, 
                      T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, 
                      FunInfoKalaAll_1.NameKala1, T_Forosh.FOROSHID, DERIVEDTBL.mande AS MandeHesab, ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) 
                      = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, T_Forosh.ForoshNumbr + FunInfoKalaAll_1.AIDNumber AS CodeAnbar, FunInfoKalaAll_1.NameAnbar, 
                      FunInfoKalaAll_1.SharhAnbar AS AddressAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad2 END AS Tedad2, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Tedad1User, [TRANSACTION].Tedad2User, [TRANSACTION].UnitPriceUser, 
                      [TRANSACTION].ZaribChildUser, [TRANSACTION].KalaIDChild, CASE WHEN ZaribChildUser > 0 THEN ''('' + T_RelateKala.NameKala2 + '')'' ELSE '''' END AS NameKalaChild, T_Forosh.KeraiehHaml, T_Forosh.keraiehbargiri, T_Forosh.TimeSabt
FROM         T_KolHesab RIGHT OUTER JOIN
                      T_T_Heasb RIGHT OUTER JOIN
                      T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                          (SELECT  ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID, GOID, ThesabIDSath2, THESABID, HesabID, ISNULL(KeraiehHaml, 0) 
               AS KeraiehHaml, ISNULL(keraiehbargiri, 0) AS keraiehbargiri, CONVERT(VARCHAR(8), DateCreate, 108) AS TimeSabt
FROM     T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID ON FunInfoKalaAll_1.ID = [TRANSACTION].KALAID AND 
                      FunInfoKalaAll_1.AID = [TRANSACTION].AID LEFT OUTER JOIN
                      T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2 LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) 
                                                   AS THESAB
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL ON T_Forosh.KOLID = DERIVEDTBL.KOLID AND T_Forosh.Hesab = DERIVEDTBL.HESAB AND 
                      T_Forosh.THesab = DERIVEDTBL.THESAB ON T_Heasb.HESABID = T_Forosh.Hesab ON T_T_Heasb.THESABID = T_Forosh.THesab ON T_KolHesab.KOLID = T_Forosh.KOLID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 2) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Forosh.FOROSHID = TBL.FactID
WHERE     (1 = 1)
		'


			SET @GROUPBy = '  '
			SET @OrderBy = '  '

			Set @ParamDefinition = '@UserID INT,
									@GroupID NVARCHAR(MAX),
									@KOLID NVARCHAR(MAX), 
									@HesabidID NVARCHAR(MAX), 
									@THesabidID NVARCHAR(MAX), 
									@THesabidIDSath2 NVARCHAR(MAX), 
									@StarteDate NVARCHAR(MAX), 
									@ENDDate NVARCHAR(MAX),
									@KHARIDID1 INT,
									@KHARIDID2 INT '  	
			
			/* Execute the Transact-SQL String with all parameter value's 	   
			   Using sp_executesql Command */
			   
		If (@GroupID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
	         
		If (@KOLID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.KOLID IN('+ @KOLID +'))'
	    
		If (@HesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.HESABID IN('+ @HesabidID +') )'

		If (@THesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.THESABID IN('+ @THesabidID +') )'

		If (@THesabidIDSath2 Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'    
        
        
       If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))
          Set @SQLQuery = @SQLQuery + ' And  T_Forosh.ForoshNumbr Between @KHARIDID1 And @KHARIDID2' 
        
        
        Set @SQLQuery = @SQLQuery + ' And  T_Forosh.DateFo Between @StarteDate And @ENDDate'
        			   
			   SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
			Execute sp_Executesql   @SQLQuery, 
									@ParamDefinition,
									@UserID ,
									@GroupID,
									@KOLID , 
									@HesabidID , 
									@THesabidID , 
									@THesabidIDSath2 , 
									@StarteDate , 
									@ENDDate,
									@KHARIDID1,
									@KHARIDID2


			If @@ERROR <> 0 GoTo ErrorHandler
			Set NoCount OFF
		  
			ErrorHandler:
				Return(@@ERROR)
	END		
END










GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Rep1]    Script Date: 01/31/2021 09:03:51 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Foroosh_Rep1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Foroosh_Rep1]
GO


/****** Object:  StoredProcedure [dbo].[QSubPFrm_Foroosh_Rep1]    Script Date: 01/31/2021 09:03:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






CREATE  PROCEDURE [dbo].[QSubPFrm_Foroosh_Rep1]
		
		@Userid INT,	
		@FOROSHID INT,
		@GroupID NVARCHAR(MAX), 
		@KOLID NVARCHAR(MAX), 
		@HesabidID NVARCHAR(MAX), 
		@THesabidID NVARCHAR(MAX), 
		@THesabidIDSath2 NVARCHAR(MAX), 
		@StarteDate NVARCHAR(10),
		@ENDDate NVARCHAR(10),
		@KHARIDID1 INT,
		@KHARIDID2 INT

AS
BEGIN
	
IF (@FOROSHID>0) 	
	
SELECT     [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
                      CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, 
                      T_Forosh.KOLID AS CodeKol, T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, [TRANSACTION].FieldN7 AS Takhfif, T_Forosh.DateFo AS Tarikh, 
                      [TRANSACTION].Sharh AS SharhRadif, T_Forosh.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, 
                      FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, 
                      T_Forosh.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, 
                      T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, 
                      FunInfoKalaAll_1.NameKala1, T_Forosh.FOROSHID, DERIVEDTBL.mande AS MandeHesab, ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) 
                      = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, T_Forosh.ForoshNumbr + FunInfoKalaAll_1.AIDNumber AS CodeAnbar, FunInfoKalaAll_1.NameAnbar, 
                      FunInfoKalaAll_1.SharhAnbar AS AddressAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad2 END AS Tedad2, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Tedad1User, [TRANSACTION].Tedad2User, [TRANSACTION].UnitPriceUser, 
                      [TRANSACTION].ZaribChildUser, [TRANSACTION].KalaIDChild, CASE WHEN ZaribChildUser > 0 THEN '(' + T_RelateKala.NameKala2 + ')' ELSE '' END AS NameKalaChild, T_Forosh.KeraiehHaml, 
                      T_Forosh.keraiehbargiri, T_Forosh.TimeSabt, T_T_Heasb.CodeMelli
FROM         T_KolHesab RIGHT OUTER JOIN
                      T_T_Heasb RIGHT OUTER JOIN
                      T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                          (SELECT     ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID, GOID, ThesabIDSath2, THESABID, HesabID, 
                                                   ISNULL(KeraiehHaml, 0) AS KeraiehHaml, ISNULL(keraiehbargiri, 0) AS keraiehbargiri, CONVERT(VARCHAR(8), DateCreate, 108) AS TimeSabt
                             FROM         T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID ON FunInfoKalaAll_1.ID = [TRANSACTION].KALAID AND 
                      FunInfoKalaAll_1.AID = [TRANSACTION].AID LEFT OUTER JOIN
                      T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2 LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) 
                                                   AS THESAB
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL ON T_Forosh.KOLID = DERIVEDTBL.KOLID AND T_Forosh.Hesab = DERIVEDTBL.HESAB AND 
                      T_Forosh.THesab = DERIVEDTBL.THESAB ON T_Heasb.HESABID = T_Forosh.Hesab ON T_T_Heasb.THESABID = T_Forosh.THesab ON T_KolHesab.KOLID = T_Forosh.KOLID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 2) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Forosh.FOROSHID = TBL.FactID
WHERE     (T_Forosh.FOROSHID = @FOROSHID)

ELSE
	BEGIN

			SET NOCOUNT ON;

			Declare @SQLQuery AS NVarchar(MAX)
			Declare @ParamDefinition AS NVarchar(MAX) 
			DECLARE @OrderBy AS NVARCHAR(MAX)
			DECLARE @GROUPBy AS NVARCHAR(MAX)
		    
			IF (@StarteDate IS NULL)
					SET @StarteDate='0'
		    
				IF (@ENDDate IS NULL)
					SET @ENDDate='99999999'
						SET @SQLQuery = '  SELECT     [TRANSACTION].TedadFo AS Tedad, [TRANSACTION].UnitPriceFo AS netprice, 
                      CASE WHEN T_Forosh.THESAB <> 0 THEN T_T_Heasb.NameHesabT WHEN T_Forosh.HESAB <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, 
                      T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, T_KolHesab.NameHesab AS NameHesabKol, T_Forosh.THesab AS CodeTafsili, 
                      T_Forosh.KOLID AS CodeKol, T_Forosh.Hesab AS CodeMoin, [TRANSACTION].FieldN3 AS Mablagh, [TRANSACTION].FieldN7 AS Takhfif, T_Forosh.DateFo AS Tarikh, 
                      [TRANSACTION].Sharh AS SharhRadif, T_Forosh.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, 
                      FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Forosh.ForoshNumbr AS Serial, 
                      T_Forosh.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, 
                      T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, 
                      FunInfoKalaAll_1.NameKala1, T_Forosh.FOROSHID, DERIVEDTBL.mande AS MandeHesab, ROUND([TRANSACTION].TedadFo / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) 
                      = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, T_Forosh.ForoshNumbr + FunInfoKalaAll_1.AIDNumber AS CodeAnbar, FunInfoKalaAll_1.NameAnbar, 
                      FunInfoKalaAll_1.SharhAnbar AS AddressAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) 
                      ELSE [TRANSACTION].Tedad2 END AS Tedad2, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, [TRANSACTION].Tedad1User, [TRANSACTION].Tedad2User, [TRANSACTION].UnitPriceUser, 
                      [TRANSACTION].ZaribChildUser, [TRANSACTION].KalaIDChild, CASE WHEN ZaribChildUser > 0 THEN ''('' + T_RelateKala.NameKala2 + '')'' ELSE '''' END AS NameKalaChild, T_Forosh.KeraiehHaml, T_Forosh.keraiehbargiri, T_Forosh.TimeSabt, T_T_Heasb.CodeMelli
FROM         T_KolHesab RIGHT OUTER JOIN
                      T_T_Heasb RIGHT OUTER JOIN
                      T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                          (SELECT  ISNULL(THESABID, 0) AS THesab, ISNULL(HesabID, 0) AS Hesab, KOLID, Takhfif, DateFo, Sharh, ForoshNumbr, NumberKH, FOROSHID, GOID, ThesabIDSath2, THESABID, HesabID, ISNULL(KeraiehHaml, 0) 
               AS KeraiehHaml, ISNULL(keraiehbargiri, 0) AS keraiehbargiri, CONVERT(VARCHAR(8), DateCreate, 108) AS TimeSabt
FROM     T_Forosh AS T_Forosh_1) AS T_Forosh ON [TRANSACTION].FOROSHID = T_Forosh.FOROSHID ON FunInfoKalaAll_1.ID = [TRANSACTION].KALAID AND 
                      FunInfoKalaAll_1.AID = [TRANSACTION].AID LEFT OUTER JOIN
                      T_RelateKala ON [TRANSACTION].KalaIDChild = T_RelateKala.Kalaid2 LEFT OUTER JOIN
                          (SELECT     SUM(DARBed) - SUM(PARBes) AS mande, SUM(PARBes) AS SumOfPARBes, SUM(DARBed) AS SumOfDARBed, ISNULL(HESABID, 0) AS HESAB, KOLID, ISNULL(THESABID, 0) 
                                                   AS THESAB
                             FROM         [TRANSACTION] AS TRANSACTION_1
                             WHERE     (NOT (SAID2 IS NULL))
                             GROUP BY KOLID, ISNULL(HESABID, 0), ISNULL(THESABID, 0)) AS DERIVEDTBL ON T_Forosh.KOLID = DERIVEDTBL.KOLID AND T_Forosh.Hesab = DERIVEDTBL.HESAB AND 
                      T_Forosh.THesab = DERIVEDTBL.THESAB ON T_Heasb.HESABID = T_Forosh.Hesab ON T_T_Heasb.THESABID = T_Forosh.THesab ON T_KolHesab.KOLID = T_Forosh.KOLID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 2) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Forosh.FOROSHID = TBL.FactID
WHERE     (1 = 1)
		'


			SET @GROUPBy = '  '
			SET @OrderBy = '  '

			Set @ParamDefinition = '@UserID INT,
									@GroupID NVARCHAR(MAX),
									@KOLID NVARCHAR(MAX), 
									@HesabidID NVARCHAR(MAX), 
									@THesabidID NVARCHAR(MAX), 
									@THesabidIDSath2 NVARCHAR(MAX), 
									@StarteDate NVARCHAR(MAX), 
									@ENDDate NVARCHAR(MAX),
									@KHARIDID1 INT,
									@KHARIDID2 INT '  	
			
			/* Execute the Transact-SQL String with all parameter value's 	   
			   Using sp_executesql Command */
			   
		If (@GroupID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))'
	         
		If (@KOLID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.KOLID IN('+ @KOLID +'))'
	    
		If (@HesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.HESABID IN('+ @HesabidID +') )'

		If (@THesabidID Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.THESABID IN('+ @THesabidID +') )'

		If (@THesabidIDSath2 Is Not Null)
			 Set @SQLQuery = @SQLQuery + ' And (T_Forosh.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'    
        
        
       If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))
          Set @SQLQuery = @SQLQuery + ' And  T_Forosh.ForoshNumbr Between @KHARIDID1 And @KHARIDID2' 
        
        
        Set @SQLQuery = @SQLQuery + ' And  T_Forosh.DateFo Between @StarteDate And @ENDDate'
        			   
			   SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy
			Execute sp_Executesql   @SQLQuery, 
									@ParamDefinition,
									@UserID ,
									@GroupID,
									@KOLID , 
									@HesabidID , 
									@THesabidID , 
									@THesabidIDSath2 , 
									@StarteDate , 
									@ENDDate,
									@KHARIDID1,
									@KHARIDID2


			If @@ERROR <> 0 GoTo ErrorHandler
			Set NoCount OFF
		  
			ErrorHandler:
				Return(@@ERROR)
	END		
END










GO





/****** Object:  StoredProcedure [dbo].[Q_Enumeration_Insert_To_Temp_Ezafat]    Script Date: 02/15/2021 13:00:19 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Enumeration_Insert_To_Temp_Ezafat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Enumeration_Insert_To_Temp_Ezafat]
GO


/****** Object:  StoredProcedure [dbo].[Q_Enumeration_Insert_To_Temp_Ezafat]    Script Date: 02/15/2021 13:00:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO








CREATE  PROCEDURE [dbo].[Q_Enumeration_Insert_To_Temp_Ezafat]
		
		@Userid INT,	
		@Date2 NVARCHAR(MAX), 
		@IdHeader INT,
		@OperType INT,
		@LinkFieldToSub NVARCHAR(MAX)
AS
BEGIN
	
INSERT INTO TRANSACTION2
                      (KALAID, AID, GOKID, Tedad, UnitPrice, OperType, Date1, TextKalaName, DARBed, PARBes, Maliatkala, Avarez, Tedad1, Tedad2, Zarib, FieldT1, TextAID, Tedad1User, Tedad2User, UnitPriceUser, 
                      ZaribChildUser, Flag1, radif)
SELECT     derivedtbl_1.KALAID, derivedtbl_1.AID, FunInfoKalaAll_1.GOKID, derivedtbl_1.EzafeMojodi, ISNULL(derivedtbl_1.UnitPriceEnum, ISNULL(derivedtbl_2.MaxUnitPriceKh, 0)) AS Uniprice, 
                      @OperType AS OperType, @LinkFieldToSub AS LinkFieldToSub, FunInfoKalaAll_1.NameKala1, 1 * derivedtbl_1.EzafeMojodi AS DARBed, 1 * derivedtbl_1.EzafeMojodi AS PARBes, 0 AS Maliatkala, 
                      0 AS Avarez, 0 AS Tedad1, 0 AS Tedad2, 0 AS Expr1, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.NameAnbar, 0 AS Tedad1User, 0 AS Tedad2User, 0 AS UnitPriceUser, 0 AS ZaribChildUser, 
                      0 AS Expr2, ROW_NUMBER() OVER (
										ORDER BY FunInfoKalaAll_1.NameKala1
                      ) AS RowNumber
FROM         (SELECT     TRaKalaid.KALAID, CAST(SUM(TRaKalaid.Varde - TRaKalaid.Sadre) AS DECIMAL(19, 4)) AS MandeSystem, SUM(CONVERT(DECIMAL(19, 4), T_Enumeration.Tedad)) AS MandeFeli, 
                                              CASE WHEN CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4)) 
                                              > 0 THEN CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4)) ELSE 0 END AS kasriMojodi, 
                                              CASE WHEN CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4)) 
                                              < 0 THEN abs(CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4))) ELSE 0 END AS EzafeMojodi, TRaKalaid.AID, 
                                              MAX(T_Enumeration.UnitPrice) AS UnitPriceEnum, T_Enumeration.radif
                        FROM         (SELECT     KALAID, AID, GOKID, SUM(Sadre) AS Sadre, SUM(Varde) AS Varde
                                                FROM         (SELECT     KALAID, AID, GOKID, TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
                                                                                              Tarikh AS Date1
                                                                        FROM         View_SubTransactionKala
                                                                        WHERE     (NOT (KALAID IS NULL))
                                                                        UNION ALL
                                                                        SELECT     NameKalaID, AID, GOKID, Mojodi AS Varde, 0 AS Sadre, dbo.F1StartDate() AS Date12
                                                                        FROM         T_Kala
                                                                        WHERE     (Mojodi <> 0)) AS derivedtbl_1
                                                WHERE     (Date1 <= @date2)
                                                GROUP BY KALAID, AID, GOKID) AS TRaKalaid FULL OUTER JOIN
                                                  (SELECT     TRAID, radif, GOID, KOLID, HESABID, THESABID, THESABID2, KALAID, AID, GOKID, Sharh, Date1, Tedad, UnitPrice, TextNameKala, TextNameGroup, TextAID, 
                                                                           TxtLinkTosub
                                                     FROM         T_Enumeration AS T_Enumeration_1
                                                     WHERE     (TxtLinkTosub = @IdHeader)) AS T_Enumeration ON TRaKalaid.AID = T_Enumeration.AID AND TRaKalaid.KALAID = T_Enumeration.KALAID
                        GROUP BY TRaKalaid.KALAID, TRaKalaid.AID, T_Enumeration.radif
                        HAVING      (SUM(ISNULL(TRaKalaid.Varde, 0) - ISNULL(TRaKalaid.Sadre, 0) + ISNULL(T_Enumeration.Tedad, 0)) <> 0)) AS derivedtbl_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON derivedtbl_1.KALAID = FunInfoKalaAll_1.ID AND derivedtbl_1.AID = FunInfoKalaAll_1.AID LEFT OUTER JOIN
                          (SELECT     KALAID, AID, GOKID, 0 AS Expr1, MaxUnitPriceKh
                             FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1
                             WHERE     (MaxDate1 < @Date2)) AS derivedtbl_2 ON FunInfoKalaAll_1.ID = derivedtbl_2.KALAID AND FunInfoKalaAll_1.AID = derivedtbl_2.AID
WHERE     (derivedtbl_1.EzafeMojodi > 0)
ORDER BY FunInfoKalaAll_1.NameKala1
end







GO



/****** Object:  StoredProcedure [dbo].[Op_Roozane_T_Enumeration_Insert]    Script Date: 02/15/2021 13:02:08 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Roozane_T_Enumeration_Insert]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Roozane_T_Enumeration_Insert]
GO


/****** Object:  StoredProcedure [dbo].[Op_Roozane_T_Enumeration_Insert]    Script Date: 02/15/2021 13:02:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE PROCEDURE [dbo].[Op_Roozane_T_Enumeration_Insert] 
@UserID INT,
@KALAID INT,
@AID INT,
@GOKID INT,
@Tedad DECIMAL(19,4),
@UnitPrice DECIMAL(19,4),
@TxtLinkTosub INT,
@TextNameKala NVARCHAR(255),
@TextNameGroup NVARCHAR(255),
@TextAID NVARCHAR(255)
AS
BEGIN

DECLARE @Radif AS INT
	
SELECT     @Radif=COUNT(TRAID)  FROM         T_Enumeration WHERE     (TxtLinkTosub = @TxtLinkTosub)

SET @Radif=@Radif+1

INSERT INTO T_Enumeration
               (Radif,KALAID, AID, GOKID, Tedad, UnitPrice, TxtLinkTosub, TextNameKala, TextNameGroup, TextAID)
VALUES  (@Radif,@KALAID,@AID,@GOKID,@Tedad,@UnitPrice,@TxtLinkTosub,@TextNameKala,@TextNameGroup,@TextAID)


/*set @Kharidid =@@IDENTITY;*/

END



GO



/****** Object:  StoredProcedure [dbo].[Q_Enumeration_Insert_To_Temp_Kasri]    Script Date: 02/15/2021 13:34:02 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Enumeration_Insert_To_Temp_Kasri]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Enumeration_Insert_To_Temp_Kasri]
GO


/****** Object:  StoredProcedure [dbo].[Q_Enumeration_Insert_To_Temp_Kasri]    Script Date: 02/15/2021 13:34:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO









CREATE  PROCEDURE [dbo].[Q_Enumeration_Insert_To_Temp_Kasri]
		
		@Userid INT,	
		@Date2 NVARCHAR(MAX), 
		@IdHeader INT,
		@OperType INT,
		@LinkFieldToSub NVARCHAR(MAX)
AS
BEGIN
	
INSERT INTO TRANSACTION2
               (KALAID, AID, GOKID, Tedad, UnitPrice, OperType, Date1, TextKalaName, DARBed, PARBes, Maliatkala, Avarez, Tedad1, Tedad2, Zarib, FieldT1, TextAID, Tedad1User, Tedad2User, UnitPriceUser, ZaribChildUser, 
               Flag1,radif)
SELECT     derivedtbl_1.KALAID, derivedtbl_1.AID, FunInfoKalaAll_1.GOKID, derivedtbl_1.kasriMojodi, ISNULL(derivedtbl_1.UnitPriceEnum, ISNULL(derivedtbl_2.MaxUnitPriceKh, 0)) AS Uniprice, 
                      @OperType AS OperType, @LinkFieldToSub AS LinkFieldToSub, FunInfoKalaAll_1.NameKala1, derivedtbl_1.kasriMojodi AS DARBed, derivedtbl_1.kasriMojodi AS PARBes, 0 AS Maliatkala, 
                      0 AS Avarez, 0 AS Tedad1, 0 AS Tedad2, 0 AS Expr1, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.NameAnbar, 0 AS Tedad1User, 0 AS Tedad2User, 0 AS UnitPriceUser, 0 AS ZaribChildUser, 
                      0 AS Expr2, ROW_NUMBER() OVER (
										ORDER BY FunInfoKalaAll_1.NameKala1
                      ) AS RowNumber
FROM         (SELECT     TRaKalaid.KALAID, CAST(SUM(TRaKalaid.Varde - TRaKalaid.Sadre) AS DECIMAL(19, 4)) AS MandeSystem, SUM(CONVERT(DECIMAL(19, 4), T_Enumeration.Tedad)) AS MandeFeli, 
                                              CASE WHEN CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4)) 
                                              > 0 THEN CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4)) ELSE 0 END AS kasriMojodi, 
                                              CASE WHEN CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4)) 
                                              < 0 THEN abs(CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4))) ELSE 0 END AS EzafeMojodi, TRaKalaid.AID, 
                                              MAX(T_Enumeration.UnitPrice) AS UnitPriceEnum
                        FROM         (SELECT     KALAID, AID, GOKID, SUM(Sadre) AS Sadre, SUM(Varde) AS Varde
                                                FROM         (SELECT     KALAID, AID, GOKID, TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
                                                                                              Tarikh AS Date1
                                                                        FROM         View_SubTransactionKala
                                                                        WHERE     (NOT (KALAID IS NULL))
                                                                        UNION ALL
                                                                        SELECT     NameKalaID, AID, GOKID, Mojodi AS Varde, 0 AS Sadre, dbo.F1StartDate() AS Date12
                                                                        FROM         T_Kala
                                                                        WHERE     (Mojodi <> 0)) AS derivedtbl_1
                                                WHERE     (Date1 <= @date2)
                                                GROUP BY KALAID, AID, GOKID) AS TRaKalaid FULL OUTER JOIN
                                                  (SELECT     TRAID, radif, GOID, KOLID, HESABID, THESABID, THESABID2, KALAID, AID, GOKID, Sharh, Date1, Tedad, UnitPrice, TextNameKala, TextNameGroup, TextAID, 
                                                                           TxtLinkTosub
                                                     FROM         T_Enumeration AS T_Enumeration_1
                                                     WHERE     (TxtLinkTosub = @IdHeader)) AS T_Enumeration ON TRaKalaid.AID = T_Enumeration.AID AND TRaKalaid.KALAID = T_Enumeration.KALAID
                        GROUP BY TRaKalaid.KALAID, TRaKalaid.AID
                        HAVING      (SUM(ISNULL(TRaKalaid.Varde, 0) - ISNULL(TRaKalaid.Sadre, 0) + ISNULL(T_Enumeration.Tedad, 0)) <> 0)) AS derivedtbl_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON derivedtbl_1.KALAID = FunInfoKalaAll_1.ID AND derivedtbl_1.AID = FunInfoKalaAll_1.AID LEFT OUTER JOIN
                          (SELECT     KALAID, AID, GOKID, 0 AS Expr1, MaxUnitPriceKh
                             FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1
                             WHERE     (MaxDate1 < @Date2)) AS derivedtbl_2 ON FunInfoKalaAll_1.ID = derivedtbl_2.KALAID AND FunInfoKalaAll_1.AID = derivedtbl_2.AID
WHERE     (derivedtbl_1.kasriMojodi > 0)
ORDER BY FunInfoKalaAll_1.NameKala1
end








GO



/****** Object:  StoredProcedure [dbo].[Op_Roozane_T_Enumeration_ChkDblKala]    Script Date: 02/15/2021 17:15:57 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Op_Roozane_T_Enumeration_ChkDblKala]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Op_Roozane_T_Enumeration_ChkDblKala]
GO


/****** Object:  StoredProcedure [dbo].[Op_Roozane_T_Enumeration_ChkDblKala]    Script Date: 02/15/2021 17:15:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





Create PROCEDURE [dbo].[Op_Roozane_T_Enumeration_ChkDblKala] 
@UserID INT,
@KALAID INT,
@TxtLinkTosub INT,
@ChkDblKala INT output
AS
BEGIN

	
SELECT    @ChkDblKala= COUNT(KALAID) 
FROM         T_Enumeration
WHERE     (KALAID = @KALAID) AND (TxtLinkTosub = @TxtLinkTosub)



END



GO


IF  EXISTS (SELECT * FROM sys.columns  WHERE Name = N'TextNameKala' AND Object_ID = Object_ID(N'T_Enumeration'))
	ALTER TABLE dbo.T_Enumeration 	ALTER COLUMN 	TextNameKala NVARCHAR(255)
GO




/****** Object:  StoredProcedure [dbo].[Q_Enumeration_Insert_To_Temp_Ezafat]    Script Date: 02/18/2021 12:24:45 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Enumeration_Insert_To_Temp_Ezafat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Enumeration_Insert_To_Temp_Ezafat]
GO


/****** Object:  StoredProcedure [dbo].[Q_Enumeration_Insert_To_Temp_Ezafat]    Script Date: 02/18/2021 12:24:45 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO








CREATE  PROCEDURE [dbo].[Q_Enumeration_Insert_To_Temp_Ezafat]
		
		@Userid INT,	
		@Date2 NVARCHAR(MAX), 
		@IdHeader INT,
		@OperType INT,
		@LinkFieldToSub NVARCHAR(MAX),
		@AID INT,
		@GOKID INT
		
AS
BEGIN
	
INSERT INTO TRANSACTION2
                      (KALAID, AID, GOKID, Tedad, UnitPrice, OperType, Date1, TextKalaName, DARBed, PARBes, Maliatkala, Avarez, Tedad1, Tedad2, Zarib, FieldT1, TextAID, Tedad1User, Tedad2User, UnitPriceUser, 
                      ZaribChildUser, Flag1, radif)
SELECT     derivedtbl_1.KALAID, derivedtbl_1.AID, FunInfoKalaAll_1.GOKID, derivedtbl_1.EzafeMojodi, ISNULL(derivedtbl_1.UnitPriceEnum, ISNULL(derivedtbl_2.MaxUnitPriceKh, 0)) AS Uniprice, 
                      @OperType AS OperType, @LinkFieldToSub AS LinkFieldToSub, FunInfoKalaAll_1.NameKala1, 1 * derivedtbl_1.EzafeMojodi AS DARBed, 1 * derivedtbl_1.EzafeMojodi AS PARBes, 0 AS Maliatkala, 
                      0 AS Avarez, 0 AS Tedad1, 0 AS Tedad2, 0 AS Expr1, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.NameAnbar, 0 AS Tedad1User, 0 AS Tedad2User, 0 AS UnitPriceUser, 0 AS ZaribChildUser, 
                      0 AS Expr2, ROW_NUMBER() OVER (
										ORDER BY FunInfoKalaAll_1.NameKala1
                      ) AS RowNumber
FROM         (SELECT     TRaKalaid.KALAID, CAST(SUM(TRaKalaid.Varde - TRaKalaid.Sadre) AS DECIMAL(19, 4)) AS MandeSystem, SUM(CONVERT(DECIMAL(19, 4), T_Enumeration.Tedad)) AS MandeFeli, 
                                              CASE WHEN CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4)) 
                                              > 0 THEN CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4)) ELSE 0 END AS kasriMojodi, 
                                              CASE WHEN CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4)) 
                                              < 0 THEN abs(CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4))) ELSE 0 END AS EzafeMojodi, TRaKalaid.AID, 
                                              MAX(T_Enumeration.UnitPrice) AS UnitPriceEnum, T_Enumeration.radif
                        FROM         (SELECT     KALAID, AID, GOKID, SUM(Sadre) AS Sadre, SUM(Varde) AS Varde
                                                FROM         (SELECT     KALAID, AID, GOKID, TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
                                                                                              Tarikh AS Date1
                                                                        FROM         View_SubTransactionKala
                                                                        WHERE     (NOT (KALAID IS NULL))
                                                                        UNION ALL
                                                                        SELECT     NameKalaID, AID, GOKID, Mojodi AS Varde, 0 AS Sadre, dbo.F1StartDate() AS Date12
                                                                        FROM         T_Kala
                                                                        WHERE     (Mojodi <> 0)) AS derivedtbl_1
                                                WHERE     (Date1 <= @date2)
                                                GROUP BY KALAID, AID, GOKID) AS TRaKalaid FULL OUTER JOIN
                                                  (SELECT     TRAID, radif, GOID, KOLID, HESABID, THESABID, THESABID2, KALAID, AID, GOKID, Sharh, Date1, Tedad, UnitPrice, TextNameKala, TextNameGroup, TextAID, 
                                                                           TxtLinkTosub
                                                     FROM         T_Enumeration AS T_Enumeration_1
                                                     WHERE     (TxtLinkTosub = @IdHeader)) AS T_Enumeration ON TRaKalaid.AID = T_Enumeration.AID AND TRaKalaid.KALAID = T_Enumeration.KALAID
                        GROUP BY TRaKalaid.KALAID, TRaKalaid.AID, T_Enumeration.radif
                        HAVING      (SUM(ISNULL(TRaKalaid.Varde, 0) - ISNULL(TRaKalaid.Sadre, 0) + ISNULL(T_Enumeration.Tedad, 0)) <> 0)) AS derivedtbl_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON derivedtbl_1.KALAID = FunInfoKalaAll_1.ID AND derivedtbl_1.AID = FunInfoKalaAll_1.AID LEFT OUTER JOIN
                          (SELECT     KALAID, AID, GOKID, 0 AS Expr1, MaxUnitPriceKh
                             FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1
                             WHERE     (MaxDate1 < @Date2)) AS derivedtbl_2 ON FunInfoKalaAll_1.ID = derivedtbl_2.KALAID AND FunInfoKalaAll_1.AID = derivedtbl_2.AID
WHERE     (derivedtbl_1.EzafeMojodi > 0) AND (derivedtbl_1.AID = @AID)
ORDER BY FunInfoKalaAll_1.NameKala1
end







GO



/****** Object:  StoredProcedure [dbo].[Q_Enumeration_Insert_To_Temp_Kasri]    Script Date: 02/18/2021 12:24:49 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Q_Enumeration_Insert_To_Temp_Kasri]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Q_Enumeration_Insert_To_Temp_Kasri]
GO



/****** Object:  StoredProcedure [dbo].[Q_Enumeration_Insert_To_Temp_Kasri]    Script Date: 02/18/2021 12:24:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO









CREATE  PROCEDURE [dbo].[Q_Enumeration_Insert_To_Temp_Kasri]
		
		@Userid INT,	
		@Date2 NVARCHAR(MAX), 
		@IdHeader INT,
		@OperType INT,
		@LinkFieldToSub NVARCHAR(MAX),
		@AID INT,
		@GOKID INT
AS
BEGIN
	
INSERT INTO TRANSACTION2
               (KALAID, AID, GOKID, Tedad, UnitPrice, OperType, Date1, TextKalaName, DARBed, PARBes, Maliatkala, Avarez, Tedad1, Tedad2, Zarib, FieldT1, TextAID, Tedad1User, Tedad2User, UnitPriceUser, ZaribChildUser, 
               Flag1,radif)
SELECT     derivedtbl_1.KALAID, derivedtbl_1.AID, FunInfoKalaAll_1.GOKID, derivedtbl_1.kasriMojodi, ISNULL(derivedtbl_1.UnitPriceEnum, ISNULL(derivedtbl_2.MaxUnitPriceKh, 0)) AS Uniprice, 
                      @OperType AS OperType, @LinkFieldToSub AS LinkFieldToSub, FunInfoKalaAll_1.NameKala1, derivedtbl_1.kasriMojodi AS DARBed, derivedtbl_1.kasriMojodi AS PARBes, 0 AS Maliatkala, 
                      0 AS Avarez, 0 AS Tedad1, 0 AS Tedad2, 0 AS Expr1, FunInfoKalaAll_1.NameGoKala1, FunInfoKalaAll_1.NameAnbar, 0 AS Tedad1User, 0 AS Tedad2User, 0 AS UnitPriceUser, 0 AS ZaribChildUser, 
                      0 AS Expr2, ROW_NUMBER() OVER (
										ORDER BY FunInfoKalaAll_1.NameKala1
                      )  AS RowNumber
FROM         (SELECT     TRaKalaid.KALAID, CAST(SUM(TRaKalaid.Varde - TRaKalaid.Sadre) AS DECIMAL(19, 4)) AS MandeSystem, SUM(CONVERT(DECIMAL(19, 4), T_Enumeration.Tedad)) AS MandeFeli, 
                                              CASE WHEN CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4)) 
                                              > 0 THEN CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4)) ELSE 0 END AS kasriMojodi, 
                                              CASE WHEN CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4)) 
                                              < 0 THEN abs(CAST((SUM(TRaKalaid.Varde - TRaKalaid.Sadre - CONVERT(money, isnull(T_Enumeration.Tedad, 0)))) AS decimal(19, 4))) ELSE 0 END AS EzafeMojodi, TRaKalaid.AID, 
                                              MAX(T_Enumeration.UnitPrice) AS UnitPriceEnum
                        FROM         (SELECT     KALAID, AID, GOKID, SUM(Sadre) AS Sadre, SUM(Varde) AS Varde
                                                FROM         (SELECT     KALAID, AID, GOKID, TedadKh + TedadVorood + TedadBarFo + TedadVA AS Varde, TedadFo + TedadKhorooj + TedadBarKh + TedadKhA AS Sadre, 
                                                                                              Tarikh AS Date1
                                                                        FROM         View_SubTransactionKala
                                                                        WHERE     (NOT (KALAID IS NULL))
                                                                        UNION ALL
                                                                        SELECT     NameKalaID, AID, GOKID, Mojodi AS Varde, 0 AS Sadre, dbo.F1StartDate() AS Date12
                                                                        FROM         T_Kala
                                                                        WHERE     (Mojodi <> 0)) AS derivedtbl_1
                                                WHERE     (Date1 <= @date2)
                                                GROUP BY KALAID, AID, GOKID) AS TRaKalaid FULL OUTER JOIN
                                                  (SELECT     TRAID, radif, GOID, KOLID, HESABID, THESABID, THESABID2, KALAID, AID, GOKID, Sharh, Date1, Tedad, UnitPrice, TextNameKala, TextNameGroup, TextAID, 
                                                                           TxtLinkTosub
                                                     FROM         T_Enumeration AS T_Enumeration_1
                                                     WHERE     (TxtLinkTosub = @IdHeader)) AS T_Enumeration ON TRaKalaid.AID = T_Enumeration.AID AND TRaKalaid.KALAID = T_Enumeration.KALAID
                        GROUP BY TRaKalaid.KALAID, TRaKalaid.AID
                        HAVING      (SUM(ISNULL(TRaKalaid.Varde, 0) - ISNULL(TRaKalaid.Sadre, 0) + ISNULL(T_Enumeration.Tedad, 0)) <> 0)) AS derivedtbl_1 INNER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 ON derivedtbl_1.KALAID = FunInfoKalaAll_1.ID AND derivedtbl_1.AID = FunInfoKalaAll_1.AID LEFT OUTER JOIN
                          (SELECT     KALAID, AID, GOKID, 0 AS Expr1, MaxUnitPriceKh
                             FROM         dbo.Fun_SubAkharinKharid(0) AS Fun_SubAkharinKharid_1
                             WHERE     (MaxDate1 < @Date2)) AS derivedtbl_2 ON FunInfoKalaAll_1.ID = derivedtbl_2.KALAID AND FunInfoKalaAll_1.AID = derivedtbl_2.AID
WHERE     (derivedtbl_1.kasriMojodi > 0) AND (derivedtbl_1.AID = @AID)
ORDER BY FunInfoKalaAll_1.NameKala1
end








GO




/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 03/02/2021 12:41:55 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QSubPFrm_Kharid_Rep1SF14]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14]
GO

/****** Object:  StoredProcedure [dbo].[QSubPFrm_Kharid_Rep1SF14]    Script Date: 03/02/2021 12:41:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



   CREATE  PROCEDURE [dbo].[QSubPFrm_Kharid_Rep1SF14] 		 	
   
   @Userid INT,	 	
   @KharidID INT, 	
   @GroupID NVARCHAR(MAX),  	
   @KOLID NVARCHAR(MAX),  	
   @HesabidID NVARCHAR(MAX),  	
   @THesabidID NVARCHAR(MAX),  	
   @THesabidIDSath2 NVARCHAR(MAX),  	
   @StarteDate NVARCHAR(10), 	
   @ENDDate NVARCHAR(10), 	
   @KHARIDID1 INT, 	
   @KHARIDID2 INT  AS BEGIN 	 
   
   IF (@KharidID>0) 	
   
   BEGIN 
SELECT     [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
                      <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
                      T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, 
                      T_Kharid.Takhfif, T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, 
                      FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, 
                      T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, 
                      T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, 
                      T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) 
                      AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) 
                      > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 
                      1 AS GRP, 'کالاهای خریداری شده' AS NameGRP
FROM         T_T_Heasb RIGHT OUTER JOIN
                      dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
                      [TRANSACTION] INNER JOIN
                      T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN
                          (SELECT     FactID, SUM(MabHazine) AS HazineFactor
                             FROM         Table4
                             WHERE     (TypeFact = 1) AND (Flag1 = 0)
                             GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN
                      T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN
                      T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE     (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].UnitPriceKh > 0)                      
    UNION ALL
    
SELECT  [TRANSACTION].TedadDKh AS Tedad, [TRANSACTION].UnitPriceDKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL AS Expr1, NULL AS Expr2, 
               T_NameKhad.KhadamatNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 0 AS tedadFarei, [TRANSACTION].radif, NULL AS Expr3, NULL AS Expr4, NULL 
               AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, T_NameKhad.Name, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 2 AS GRP, 'کسورات-هزینه های فاکتور' AS Expr5
FROM     T_KolHesab RIGHT OUTER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID ON T_KolHesab.KOLID = T_Kharid.KOLID LEFT OUTER JOIN
               T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (T_Kharid.KHARIDID = @KharidID) AND ([TRANSACTION].NAMEKHID > 0)


    UNION ALL

SELECT  NULL AS Tedad, Mablagh AS netprice, NULL AS NameTarafHesab, NULL AS NameHesabMoin, NULL AS NameHesabTafsili, NULL AS NameHesabKol, NULL AS CodeTafsili, NULL AS CodeKol, NULL AS CodeMoin, 
               Mablagh, NULL AS Expr6, DATE2 AS Tarikh, TextSharh AS SharhRadif, TextSharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL AS Expr1, NULL AS Expr2, 
               PARID1Number AS CodeKala, NULL AS HazineFactor, NULL AS Serial, NULL AS ShomareFactor, NULL AS AddressTarafHesab, NULL AS Address2TarafHesab, NULL AS Expr7, NULL AS Expr8, NULL AS Expr9, NULL 
               AS Expr10, NULL AS Expr11, NULL AS Expr12, NULL AS Expr13, NULL AS Expr14, NULL AS Expr15, NULL AS Expr16, NULL AS Expr17, NULL AS Expr18, IDOperation AS KHARIDID, NULL AS tedadFarei, NULL 
               AS Expr19, NULL AS Expr3, NULL AS Expr4, NULL AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, NULL AS Expr20, NULL AS Expr21, NULL AS Expr22, 3 AS GRP, 'پرداخت های نقدی' AS Expr5
FROM     T_Par1
WHERE  (IDOperation = @KharidID)

      END               
ELSE 	

BEGIN  			
SET NOCOUNT ON;  			
Declare @SQLQuery AS NVarchar(MAX) 			
Declare @ParamDefinition AS NVarchar(MAX)  			
DECLARE @OrderBy AS NVARCHAR(MAX) 			
DECLARE @GROUPBy AS NVARCHAR(MAX) 		     			
IF (@StarteDate IS NULL) 					
SET @StarteDate='0' 		     				
IF (@ENDDate IS NULL) 					
SET @ENDDate='99999999' 						
SET @SQLQuery = '   SELECT  [TRANSACTION].TedadKh AS Tedad, [TRANSACTION].UnitPriceKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, FunInfoKalaAll_1.NameVahed1 AS NameVahedAsli, FunInfoKalaAll_1.NameVahed2 AS NameVahedFarei, 
               FunInfoKalaAll_1.Zarib, FunInfoKalaAll_1.BarCode, FunInfoKalaAll_1.KalaNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, 
               T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, 
               T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 
               ROUND([TRANSACTION].TedadKh / CASE WHEN ISNULL(FunInfoKalaAll_1.Zarib, 0) = 0 THEN 1 ELSE FunInfoKalaAll_1.Zarib END, 2) AS tedadFarei, [TRANSACTION].radif, FunInfoKalaAll_1.AIDNumber, 
               FunInfoKalaAll_1.NameAnbar, FunInfoKalaAll_1.SharhAnbar AS AddAnbar, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN ([TRANSACTION].Tedad1 % FunInfoKalaAll_1.zarib) 
               ELSE [TRANSACTION].Tedad1 END AS Tedad1, CASE WHEN ISNULL(FunInfoKalaAll_1.zarib, 0) > 1 THEN FLOOR([TRANSACTION].Tedad1 / FunInfoKalaAll_1.zarib) ELSE [TRANSACTION].Tedad2 END AS Tedad2, 
               FunInfoKalaAll_1.NameKala1, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 1 AS GRP, ''کالاهای خریداری شده'' AS NameGRP
FROM     T_T_Heasb RIGHT OUTER JOIN
               dbo.FunInfoKalaAll(0) AS FunInfoKalaAll_1 INNER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID ON FunInfoKalaAll_1.AID = [TRANSACTION].AID AND FunInfoKalaAll_1.ID = [TRANSACTION].KALAID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID LEFT OUTER JOIN
               T_KolHesab ON T_Kharid.KOLID = T_KolHesab.KOLID ON T_T_Heasb.THESABID = T_Kharid.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (NOT (T_Kharid.KHARIDID IS NULL)) AND ([TRANSACTION].UnitPriceKh > 0) 		'   
			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
Set @ParamDefinition = '@UserID INT, 									
						@GroupID NVARCHAR(MAX), 									
						@KOLID NVARCHAR(MAX),  									
						@HesabidID NVARCHAR(MAX),  									
						@THesabidID NVARCHAR(MAX),  									
						@THesabidIDSath2 NVARCHAR(MAX),  									
						@StarteDate NVARCHAR(MAX),  									
						@ENDDate NVARCHAR(MAX), 									
						@KHARIDID1 INT, 									
						@KHARIDID2 INT '  	 			 			
/* Execute the Transact-SQL String with all parameter value's 	    			   Using sp_executesql Command */ 			    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 


SET @SQLQuery =@SQLQuery+ '     UNION ALL
    
SELECT  [TRANSACTION].TedadDKh AS Tedad, [TRANSACTION].UnitPriceDKh AS netprice, CASE WHEN isnull(T_Kharid.THESABID, 0) <> 0 THEN T_T_Heasb.NameHesabT WHEN isnull(T_Kharid.HESABID, 0) 
               <> 0 THEN T_Heasb.NameHesab ELSE T_KolHesab.NameHesab END AS NameTarafHesab, T_Heasb.NameHesab AS NameHesabMoin, T_T_Heasb.NameHesabT AS NameHesabTafsili, 
               T_KolHesab.NameHesab AS NameHesabKol, T_Kharid.THESABID AS CodeTafsili, T_Kharid.KOLID AS CodeKol, T_Kharid.HesabID AS CodeMoin, ABS([TRANSACTION].FieldN3) AS Mablagh, T_Kharid.Takhfif, 
               T_Kharid.DateKh AS Tarikh, [TRANSACTION].Sharh AS SharhRadif, T_Kharid.Sharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL AS Expr1, NULL AS Expr2, 
               T_NameKhad.KhadamatNumber AS CodeKala, ISNULL(TBL.HazineFactor, 0) AS HazineFactor, T_Kharid.KharidNumbr AS Serial, T_Kharid.NumberKH AS ShomareFactor, T_T_Heasb.Address AS AddressTarafHesab, 
               T_T_Heasb.Address2 AS Address2TarafHesab, T_T_Heasb.Telex, T_T_Heasb.Shahr, T_T_Heasb.Ostan, T_T_Heasb.CodPosti, T_T_Heasb.CodEghtesdi, T_T_Heasb.Keshvar, T_T_Heasb.BankHesab, 
               T_T_Heasb.Mob, T_T_Heasb.Websit, T_T_Heasb.Phone1, T_T_Heasb.Phone2, T_T_Heasb.Email, T_Kharid.KHARIDID, 0 AS tedadFarei, [TRANSACTION].radif, NULL AS Expr3, NULL AS Expr4, NULL 
               AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, T_NameKhad.Name, [TRANSACTION].Maliatkala, [TRANSACTION].Avarez, 2 AS GRP, ''کسورات-هزینه های فاکتور'' AS Expr5
FROM     T_KolHesab RIGHT OUTER JOIN
               [TRANSACTION] INNER JOIN
               T_Kharid ON [TRANSACTION].KHARIDID = T_Kharid.KHARIDID INNER JOIN
               T_NameKhad ON [TRANSACTION].NAMEKHID = T_NameKhad.NAMEKHID LEFT OUTER JOIN
                   (SELECT  FactID, SUM(MabHazine) AS HazineFactor
                    FROM     Table4
                    WHERE  (TypeFact = 1) AND (Flag1 = 0)
                    GROUP BY FactID) AS TBL ON T_Kharid.KHARIDID = TBL.FactID ON T_KolHesab.KOLID = T_Kharid.KOLID LEFT OUTER JOIN
               T_T_Heasb ON T_Kharid.THESABID = T_T_Heasb.THESABID LEFT OUTER JOIN
               T_Heasb ON T_Kharid.HesabID = T_Heasb.HESABID
WHERE  (NOT (T_Kharid.KHARIDID IS NULL)) AND ([TRANSACTION].NAMEKHID > 0)		'   
			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
		    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And ([TRANSACTION].GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 

SET @SQLQuery =@SQLQuery+ '     UNION ALL
SELECT  NULL AS Tedad, T_Par1.Mablagh AS netprice, NULL AS NameTarafHesab, NULL AS NameHesabMoin, NULL AS NameHesabTafsili, NULL AS NameHesabKol, NULL AS CodeTafsili, NULL AS CodeKol, NULL 
               AS CodeMoin, T_Par1.Mablagh, NULL AS Expr6, T_Par1.DATE2 AS Tarikh, T_Par1.TextSharh AS SharhRadif, T_Par1.TextSharh AS TozihatFactor, NULL AS NameVahedAsli, NULL AS NameVahedFarei, NULL 
               AS Expr1, NULL AS Expr2, T_Par1.PARID1Number AS CodeKala, NULL AS HazineFactor, NULL AS Serial, NULL AS ShomareFactor, NULL AS AddressTarafHesab, NULL AS Address2TarafHesab, NULL 
               AS Expr7, NULL AS Expr8, NULL AS Expr9, NULL AS Expr10, NULL AS Expr11, NULL AS Expr12, NULL AS Expr13, NULL AS Expr14, NULL AS Expr15, NULL AS Expr16, NULL AS Expr17, NULL AS Expr18, 
               T_Par1.IDOperation AS KHARIDID, NULL AS tedadFarei, NULL AS Expr19, NULL AS Expr3, NULL AS Expr4, NULL AS AddAnbar, NULL AS Tedad1, NULL AS Tedad2, NULL AS Expr20, NULL AS Expr21, NULL 
               AS Expr22, 3 AS GRP, ''پرداخت های نقدی'' AS Expr5
FROM     T_Par1 INNER JOIN
               T_Kharid ON T_Par1.IDOperation = T_Kharid.KHARIDID
WHERE  (NOT (T_Par1.IDOperation IS NULL))
  		'   
			
SET @GROUPBy = '  ' 			
SET @OrderBy = '  '  			
		    		

If (@GroupID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.GOID IN('+ @GroupID +'))' 	          		
If (@KOLID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.KOLID IN('+ @KOLID +'))' 	     		
If (@HesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.HESABID IN('+ @HesabidID +') )'  		
If (@THesabidID Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABID IN('+ @THesabidID +') )'  		
If (@THesabidIDSath2 Is Not Null) 			 
Set @SQLQuery = @SQLQuery + ' And (T_Kharid.THESABIDSath2 IN('+ @THesabidIDSath2 +') )'                              
If ((@KHARIDID1 Is Not Null) AND (@KHARIDID2 Is Not Null))           
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.KharidNumbr Between @KHARIDID1 And @KHARIDID2'                            
Set @SQLQuery = @SQLQuery + ' And  T_Kharid.DateKh Between @StarteDate And @ENDDate'         			    			   
SET @SQLQuery=@SQLQuery + char(10) + @GROUPBy + char(10) + @OrderBy 	
		
Execute sp_Executesql   @SQLQuery,  									
						@ParamDefinition, 									
						@UserID , 									
						@GroupID, 									
						@KOLID ,  									
						@HesabidID ,  									
						@THesabidID ,  									
						@THesabidIDSath2 ,  									
						@StarteDate ,  									
						@ENDDate, 									
						@KHARIDID1, 									
						@KHARIDID1   			
If @@ERROR <> 0 GoTo ErrorHandler 			
Set NoCount OFF 		   			
ErrorHandler: 				
Return(@@ERROR) 	
END			 
END    


GO


